2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
20 #include "interface.h"
21 #include "interface-ip.h"
27 struct vlist_tree interfaces
;
28 static LIST_HEAD(iface_all_users
);
34 IFACE_ATTR_DEFAULTROUTE
,
37 IFACE_ATTR_DNS_SEARCH
,
46 IFACE_ATTR_IP6IFACEID
,
47 IFACE_ATTR_FORCE_LINK
,
51 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
52 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
53 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
54 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
55 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
56 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
57 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
58 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
59 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
60 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
61 [IFACE_ATTR_IP6ASSIGN
] = { .name
= "ip6assign", .type
= BLOBMSG_TYPE_INT32
},
62 [IFACE_ATTR_IP6HINT
] = { .name
= "ip6hint", .type
= BLOBMSG_TYPE_STRING
},
63 [IFACE_ATTR_IP4TABLE
] = { .name
= "ip4table", .type
= BLOBMSG_TYPE_STRING
},
64 [IFACE_ATTR_IP6TABLE
] = { .name
= "ip6table", .type
= BLOBMSG_TYPE_STRING
},
65 [IFACE_ATTR_IP6CLASS
] = { .name
= "ip6class", .type
= BLOBMSG_TYPE_ARRAY
},
66 [IFACE_ATTR_DELEGATE
] = { .name
= "delegate", .type
= BLOBMSG_TYPE_BOOL
},
67 [IFACE_ATTR_IP6IFACEID
] = { .name
= "ip6ifaceid", .type
= BLOBMSG_TYPE_STRING
},
68 [IFACE_ATTR_FORCE_LINK
] = { .name
= "force_link", .type
= BLOBMSG_TYPE_BOOL
},
71 static const struct uci_blob_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
72 [IFACE_ATTR_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
73 [IFACE_ATTR_IP6CLASS
] = { .type
= BLOBMSG_TYPE_STRING
},
76 const struct uci_blob_param_list interface_attr_list
= {
77 .n_params
= IFACE_ATTR_MAX
,
78 .params
= iface_attrs
,
79 .info
= iface_attr_info
,
83 interface_clear_errors(struct interface
*iface
)
85 struct interface_error
*error
, *tmp
;
87 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
88 list_del(&error
->list
);
93 void interface_add_error(struct interface
*iface
, const char *subsystem
,
94 const char *code
, const char **data
, int n_data
)
96 struct interface_error
*error
;
99 char *dest
, *d_subsys
, *d_code
;
102 len
= n_data
* sizeof(char *);
103 datalen
= alloca(len
);
104 for (i
= 0; i
< n_data
; i
++) {
105 datalen
[i
] = strlen(data
[i
]) + 1;
110 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
111 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
112 &d_code
, code
? strlen(code
) + 1 : 0);
116 list_add_tail(&error
->list
, &iface
->errors
);
118 dest
= (char *) &error
->data
[n_data
+ 1];
119 for (i
= 0; i
< n_data
; i
++) {
120 error
->data
[i
] = dest
;
121 memcpy(dest
, data
[i
], datalen
[i
]);
124 error
->data
[n_data
++] = NULL
;
127 error
->subsystem
= strcpy(d_subsys
, subsystem
);
130 error
->code
= strcpy(d_code
, code
);
134 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
136 avl_delete(&iface
->data
, &data
->node
);
141 interface_data_flush(struct interface
*iface
)
143 struct interface_data
*d
, *tmp
;
145 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
146 interface_data_del(iface
, d
);
150 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
152 struct interface_data
*n
, *o
;
154 if (!blobmsg_check_attr(data
, true))
155 return UBUS_STATUS_INVALID_ARGUMENT
;
157 const char *name
= blobmsg_name(data
);
158 unsigned len
= blob_pad_len(data
);
160 o
= avl_find_element(&iface
->data
, name
, o
, node
);
162 if (blob_pad_len(o
->data
) == len
&& !memcmp(o
->data
, data
, len
))
165 interface_data_del(iface
, o
);
168 n
= calloc(1, sizeof(*n
) + len
);
169 memcpy(n
->data
, data
, len
);
170 n
->node
.key
= blobmsg_name(n
->data
);
171 avl_insert(&iface
->data
, &n
->node
);
173 iface
->updated
|= IUF_DATA
;
178 interface_event(struct interface
*iface
, enum interface_event ev
)
180 struct interface_user
*dep
, *tmp
;
181 struct device
*adev
= NULL
;
183 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
184 dep
->cb(dep
, iface
, ev
);
186 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
187 dep
->cb(dep
, iface
, ev
);
191 adev
= iface
->l3_dev
.dev
;
194 alias_notify_device(iface
->name
, adev
);
202 interface_flush_state(struct interface
*iface
)
204 if (iface
->l3_dev
.dev
)
205 device_release(&iface
->l3_dev
);
206 interface_data_flush(iface
);
210 mark_interface_down(struct interface
*iface
)
212 enum interface_state state
= iface
->state
;
214 if (state
== IFS_DOWN
)
217 iface
->state
= IFS_DOWN
;
219 interface_event(iface
, IFEV_DOWN
);
220 interface_ip_set_enabled(&iface
->config_ip
, false);
221 interface_ip_flush(&iface
->proto_ip
);
222 interface_flush_state(iface
);
223 system_flush_routes();
227 __interface_set_down(struct interface
*iface
, bool force
)
229 enum interface_state state
= iface
->state
;
233 iface
->state
= IFS_TEARDOWN
;
235 interface_event(iface
, IFEV_DOWN
);
237 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
239 interface_flush_state(iface
);
242 vlist_delete(&interfaces
, &iface
->node
);
246 if (iface
->main_dev
.dev
)
247 device_release(&iface
->main_dev
);
255 __interface_set_up(struct interface
*iface
)
259 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
261 iface
->state
= IFS_SETUP
;
262 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
264 mark_interface_down(iface
);
270 interface_check_state(struct interface
*iface
)
272 bool link_state
= iface
->link_state
|| iface
->force_link
;
274 switch (iface
->state
) {
277 if (!iface
->enabled
|| !link_state
) {
278 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
279 mark_interface_down(iface
);
283 if (!iface
->available
)
286 if (iface
->autostart
&& iface
->enabled
&& link_state
&& !config_init
)
287 __interface_set_up(iface
);
295 interface_set_enabled(struct interface
*iface
, bool new_state
)
297 if (iface
->enabled
== new_state
)
300 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
301 iface
->enabled
= new_state
;
302 interface_check_state(iface
);
306 interface_set_link_state(struct interface
*iface
, bool new_state
)
308 if (iface
->link_state
== new_state
)
311 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
312 iface
->link_state
= new_state
;
313 interface_check_state(iface
);
317 interface_ext_cb(struct device_user
*dep
, enum device_event ev
)
319 if (ev
== DEV_EVENT_REMOVE
)
320 device_remove_user(dep
);
324 interface_cb(struct device_user
*dep
, enum device_event ev
)
326 struct interface
*iface
;
327 bool new_state
= false;
329 iface
= container_of(dep
, struct interface
, main_dev
);
333 case DEV_EVENT_REMOVE
:
334 interface_set_available(iface
, new_state
);
335 if (!new_state
&& dep
->dev
&& dep
->dev
->external
)
336 interface_set_main_dev(iface
, NULL
);
341 interface_set_enabled(iface
, new_state
);
343 case DEV_EVENT_LINK_UP
:
345 case DEV_EVENT_LINK_DOWN
:
346 interface_set_link_state(iface
, new_state
);
348 case DEV_EVENT_TOPO_CHANGE
:
349 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
357 interface_set_available(struct interface
*iface
, bool new_state
)
359 if (iface
->available
== new_state
)
362 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
363 iface
->available
= new_state
;
366 if (iface
->autostart
&& !config_init
)
367 interface_set_up(iface
);
369 __interface_set_down(iface
, true);
373 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
376 list_add(&dep
->list
, &iface_all_users
);
381 list_add(&dep
->list
, &iface
->users
);
382 if (iface
->state
== IFS_UP
)
383 dep
->cb(dep
, iface
, IFEV_UP
);
387 interface_remove_user(struct interface_user
*dep
)
389 list_del_init(&dep
->list
);
394 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
396 struct blob_attr
*cur
;
399 blobmsg_for_each_attr(cur
, list
, rem
) {
400 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
403 if (!blobmsg_check_attr(cur
, NULL
))
406 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
407 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
408 list_add(&c
->head
, &iface
->assignment_classes
);
413 interface_clear_assignment_classes(struct interface
*iface
)
415 while (!list_empty(&iface
->assignment_classes
)) {
416 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
417 struct interface_assignment_class
, head
);
424 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
426 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
427 old
->assignment_length
!= new->assignment_length
||
428 old
->assignment_iface_id_selection
!= new->assignment_iface_id_selection
||
429 (old
->assignment_iface_id_selection
== IFID_FIXED
&&
430 memcmp(&old
->assignment_fixed_iface_id
, &new->assignment_fixed_iface_id
,
431 sizeof(old
->assignment_fixed_iface_id
))) ||
432 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
434 struct interface_assignment_class
*c
;
435 list_for_each_entry(c
, &new->assignment_classes
, head
) {
436 // Compare list entries one-by-one to see if there was a change
437 if (list_empty(&old
->assignment_classes
)) // The new list is longer
443 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
444 struct interface_assignment_class
, head
);
446 if (strcmp(c_old
->name
, c
->name
)) // An entry didn't match
449 list_del(&c_old
->head
);
453 // The old list was longer than the new one or the last entry didn't match
454 if (!list_empty(&old
->assignment_classes
)) {
455 interface_clear_assignment_classes(old
);
459 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
462 old
->assignment_hint
= new->assignment_hint
;
463 old
->assignment_length
= new->assignment_length
;
464 old
->assignment_iface_id_selection
= new->assignment_iface_id_selection
;
465 old
->assignment_fixed_iface_id
= new->assignment_fixed_iface_id
;
466 interface_refresh_assignments(true);
471 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
473 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
474 struct device
*dev
= iface
->l3_dev
.dev
;
481 interface_set_main_dev(alias
, dev
);
482 interface_set_available(alias
, true);
485 interface_set_available(alias
, false);
486 interface_set_main_dev(alias
, NULL
);
489 interface_remove_user(dep
);
498 interface_claim_device(struct interface
*iface
)
500 struct interface
*parent
;
501 struct device
*dev
= NULL
;
503 if (iface
->parent_iface
.iface
)
504 interface_remove_user(&iface
->parent_iface
);
506 if (iface
->parent_ifname
) {
507 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
508 iface
->parent_iface
.cb
= interface_alias_cb
;
509 interface_add_user(&iface
->parent_iface
, parent
);
510 } else if (iface
->ifname
&&
511 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
512 dev
= device_get(iface
->ifname
, true);
513 if (dev
&& dev
->default_config
&& iface
->device_config
)
514 device_set_config(dev
, dev
->type
, iface
->config
);
516 dev
= iface
->ext_dev
.dev
;
520 interface_set_main_dev(iface
, dev
);
522 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
523 interface_set_available(iface
, true);
527 interface_cleanup_state(struct interface
*iface
)
529 interface_set_available(iface
, false);
531 interface_flush_state(iface
);
532 interface_clear_errors(iface
);
533 interface_set_proto_state(iface
, NULL
);
535 interface_set_main_dev(iface
, NULL
);
536 interface_set_l3_dev(iface
, NULL
);
540 interface_cleanup(struct interface
*iface
)
542 struct interface_user
*dep
, *tmp
;
544 uloop_timeout_cancel(&iface
->remove_timer
);
545 device_remove_user(&iface
->ext_dev
);
547 if (iface
->parent_iface
.iface
)
548 interface_remove_user(&iface
->parent_iface
);
550 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
551 interface_remove_user(dep
);
553 interface_clear_assignment_classes(iface
);
554 interface_ip_flush(&iface
->config_ip
);
555 interface_cleanup_state(iface
);
559 interface_do_free(struct interface
*iface
)
561 interface_event(iface
, IFEV_FREE
);
562 interface_cleanup(iface
);
564 netifd_ubus_remove_interface(iface
);
565 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
570 interface_do_reload(struct interface
*iface
)
572 interface_event(iface
, IFEV_RELOAD
);
573 interface_cleanup_state(iface
);
574 proto_init_interface(iface
, iface
->config
);
575 interface_claim_device(iface
);
579 interface_handle_config_change(struct interface
*iface
)
581 enum interface_config_state state
= iface
->config_state
;
583 iface
->config_state
= IFC_NORMAL
;
588 interface_do_reload(iface
);
591 interface_do_free(iface
);
594 if (iface
->autostart
&& iface
->available
)
595 interface_set_up(iface
);
599 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
601 struct interface
*iface
= state
->iface
;
605 if (iface
->state
!= IFS_SETUP
) {
606 interface_event(iface
, IFEV_UPDATE
);
610 if (!iface
->l3_dev
.dev
)
611 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
613 interface_ip_set_enabled(&iface
->config_ip
, true);
614 system_flush_routes();
615 iface
->state
= IFS_UP
;
616 iface
->start_time
= system_get_rtime();
617 interface_event(iface
, IFEV_UP
);
618 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
621 if (iface
->state
== IFS_DOWN
)
624 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
625 mark_interface_down(iface
);
626 if (iface
->main_dev
.dev
)
627 device_release(&iface
->main_dev
);
628 if (iface
->l3_dev
.dev
)
629 device_remove_user(&iface
->l3_dev
);
630 interface_handle_config_change(iface
);
632 case IFPEV_LINK_LOST
:
633 if (iface
->state
!= IFS_UP
)
636 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
637 mark_interface_down(iface
);
638 iface
->state
= IFS_SETUP
;
644 interface_write_resolv_conf();
647 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
650 iface
->proto
->free(iface
->proto
);
653 iface
->state
= IFS_DOWN
;
654 iface
->proto
= state
;
658 state
->proto_event
= interface_proto_cb
;
659 state
->iface
= iface
;
663 interface_alloc(const char *name
, struct blob_attr
*config
)
665 struct interface
*iface
;
666 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
667 struct blob_attr
*cur
;
668 const char *proto_name
= NULL
;
670 bool force_link
= false;
672 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
673 iface
->name
= strcpy(iface_name
, name
);
674 INIT_LIST_HEAD(&iface
->errors
);
675 INIT_LIST_HEAD(&iface
->users
);
676 INIT_LIST_HEAD(&iface
->hotplug_list
);
677 INIT_LIST_HEAD(&iface
->assignment_classes
);
678 interface_ip_init(iface
);
679 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
680 iface
->config_ip
.enabled
= false;
682 iface
->main_dev
.cb
= interface_cb
;
683 iface
->ext_dev
.cb
= interface_ext_cb
;
685 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
686 blob_data(config
), blob_len(config
));
688 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
689 proto_name
= blobmsg_data(cur
);
691 proto_attach_interface(iface
, proto_name
);
692 if (iface
->proto_handler
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
)
695 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
696 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], force_link
);
697 iface
->proto_ip
.no_defaultroute
=
698 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
699 iface
->proto_ip
.no_dns
=
700 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
702 if ((cur
= tb
[IFACE_ATTR_DNS
]))
703 interface_add_dns_server_list(&iface
->config_ip
, cur
);
705 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
706 interface_add_dns_search_list(&iface
->config_ip
, cur
);
708 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
709 iface
->metric
= blobmsg_get_u32(cur
);
711 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
712 iface
->assignment_length
= blobmsg_get_u32(cur
);
715 iface
->assignment_iface_id_selection
= IFID_FIXED
;
716 iface
->assignment_fixed_iface_id
= in6addr_any
;
717 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
719 if ((cur
= tb
[IFACE_ATTR_IP6IFACEID
])) {
720 const char *ifaceid
= blobmsg_data(cur
);
721 if (!strcmp(ifaceid
, "random")) {
722 iface
->assignment_iface_id_selection
= IFID_RANDOM
;
724 else if (!strcmp(ifaceid
, "eui64")) {
725 iface
->assignment_iface_id_selection
= IFID_EUI64
;
728 /* we expect an IPv6 address with network id zero here -> fixed iface id
729 if we cannot parse -> revert to iface id 1 */
730 if (inet_pton(AF_INET6
,ifaceid
,&iface
->assignment_fixed_iface_id
) != 1 ||
731 iface
->assignment_fixed_iface_id
.s6_addr32
[0] != 0 ||
732 iface
->assignment_fixed_iface_id
.s6_addr32
[1] != 0) {
733 iface
->assignment_fixed_iface_id
= in6addr_any
;
734 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
735 netifd_log_message(L_WARNING
, "Failed to parse ip6ifaceid for interface '%s', \
736 falling back to iface id 1.\n", iface
->name
);
741 iface
->assignment_hint
= -1;
742 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
743 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
744 ~((1 << (64 - iface
->assignment_length
)) - 1);
746 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
747 interface_add_assignment_classes(iface
, cur
);
750 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
751 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
752 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
755 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
756 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
757 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
760 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
762 iface
->config_autostart
= iface
->autostart
;
766 void interface_set_dynamic(struct interface
*iface
)
768 iface
->dynamic
= true;
769 iface
->autostart
= true;
770 iface
->node
.version
= -1; // Don't delete on reload
773 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
775 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
776 struct blob_attr
*cur
;
778 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
779 blob_data(config
), blob_len(config
));
782 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
783 iface
->parent_ifname
= blobmsg_data(cur
);
785 if (!iface
->parent_ifname
)
788 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
789 iface
->ifname
= blobmsg_data(cur
);
793 iface
->config
= config
;
794 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
799 interface_add(struct interface
*iface
, struct blob_attr
*config
)
801 __interface_add(iface
, config
, false);
805 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
807 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
810 return __interface_add(iface
, config
, true);
814 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
816 bool enabled
= iface
->config_ip
.enabled
;
817 bool claimed
= iface
->l3_dev
.claimed
;
819 if (iface
->l3_dev
.dev
== dev
)
822 interface_ip_set_enabled(&iface
->config_ip
, false);
823 interface_ip_flush(&iface
->proto_ip
);
824 device_add_user(&iface
->l3_dev
, dev
);
828 if (device_claim(&iface
->l3_dev
) < 0)
831 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
836 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
838 bool claimed
= iface
->l3_dev
.claimed
;
840 if (iface
->main_dev
.dev
== dev
)
843 interface_set_available(iface
, false);
844 device_add_user(&iface
->main_dev
, dev
);
846 interface_set_link_state(iface
, false);
851 if (device_claim(&iface
->l3_dev
) < 0)
855 if (!iface
->l3_dev
.dev
)
856 interface_set_l3_dev(iface
, dev
);
860 interface_remove_link(struct interface
*iface
, struct device
*dev
)
862 struct device
*mdev
= iface
->main_dev
.dev
;
864 if (mdev
&& mdev
->hotplug_ops
)
865 return mdev
->hotplug_ops
->del(mdev
, dev
);
867 if (dev
== iface
->ext_dev
.dev
)
868 device_remove_user(&iface
->ext_dev
);
870 if (!iface
->main_dev
.hotplug
)
871 return UBUS_STATUS_INVALID_ARGUMENT
;
873 if (dev
!= iface
->main_dev
.dev
)
874 return UBUS_STATUS_INVALID_ARGUMENT
;
876 interface_set_main_dev(iface
, NULL
);
881 interface_add_link(struct interface
*iface
, struct device
*dev
, bool link_ext
)
883 struct device
*mdev
= iface
->main_dev
.dev
;
888 if (iface
->main_dev
.hotplug
)
889 device_remove_user(&iface
->main_dev
);
892 if (mdev
->hotplug_ops
)
893 return mdev
->hotplug_ops
->add(mdev
, dev
);
895 return UBUS_STATUS_NOT_SUPPORTED
;
899 device_add_user(&iface
->ext_dev
, dev
);
901 interface_set_main_dev(iface
, dev
);
902 iface
->main_dev
.hotplug
= true;
907 interface_handle_link(struct interface
*iface
, const char *name
, bool add
, bool link_ext
)
914 dev
= device_get(name
, add
? (link_ext
? 2 : 1) : 0);
916 ret
= UBUS_STATUS_NOT_FOUND
;
921 device_set_present(dev
, true);
922 if (iface
->device_config
&& dev
->default_config
)
923 device_set_config(dev
, dev
->type
, iface
->config
);
925 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
926 ret
= interface_add_link(iface
, dev
, link_ext
);
928 ret
= interface_remove_link(iface
, dev
);
938 interface_set_up(struct interface
*iface
)
942 iface
->autostart
= true;
944 if (iface
->state
!= IFS_DOWN
)
947 interface_clear_errors(iface
);
948 if (!iface
->available
) {
949 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
953 if (iface
->main_dev
.dev
) {
954 ret
= device_claim(&iface
->main_dev
);
956 interface_check_state(iface
);
959 ret
= __interface_set_up(iface
);
965 interface_set_down(struct interface
*iface
)
968 vlist_for_each_element(&interfaces
, iface
, node
)
969 __interface_set_down(iface
, false);
971 iface
->autostart
= false;
972 __interface_set_down(iface
, false);
979 interface_start_pending(void)
981 struct interface
*iface
;
983 vlist_for_each_element(&interfaces
, iface
, node
) {
984 if (iface
->available
&& iface
->autostart
)
985 interface_set_up(iface
);
990 set_config_state(struct interface
*iface
, enum interface_config_state s
)
992 iface
->config_state
= s
;
993 if (iface
->state
== IFS_DOWN
)
994 interface_handle_config_change(iface
);
996 __interface_set_down(iface
, false);
1000 interface_update_start(struct interface
*iface
)
1003 interface_ip_update_start(&iface
->proto_ip
);
1007 interface_update_complete(struct interface
*iface
)
1009 interface_ip_update_complete(&iface
->proto_ip
);
1013 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
1015 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
1016 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
1020 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
1022 struct blob_attr
*old_config
= if_old
->config
;
1023 bool reload
= false, reload_ip
= false;
1025 #define FIELD_CHANGED_STR(field) \
1026 ((!!if_old->field != !!if_new->field) || \
1028 strcmp(if_old->field, if_new->field) != 0))
1030 if (FIELD_CHANGED_STR(parent_ifname
)) {
1031 if (if_old
->parent_iface
.iface
)
1032 interface_remove_user(&if_old
->parent_iface
);
1036 if (FIELD_CHANGED_STR(ifname
) ||
1037 if_old
->proto_handler
!= if_new
->proto_handler
)
1040 if (!if_old
->proto_handler
->config_params
)
1041 D(INTERFACE
, "No config parameters for interface '%s'\n",
1043 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
1044 if_old
->proto_handler
->config_params
))
1047 #define UPDATE(field, __var) ({ \
1048 bool __changed = (if_old->field != if_new->field); \
1049 if_old->field = if_new->field; \
1050 __var |= __changed; \
1053 if_old
->config
= if_new
->config
;
1054 if (if_old
->config_autostart
!= if_new
->config_autostart
) {
1055 if (if_old
->config_autostart
)
1058 if_old
->autostart
= if_new
->config_autostart
;
1061 if_old
->device_config
= if_new
->device_config
;
1062 if_old
->config_autostart
= if_new
->config_autostart
;
1063 if_old
->ifname
= if_new
->ifname
;
1064 if_old
->parent_ifname
= if_new
->parent_ifname
;
1065 if_old
->proto_handler
= if_new
->proto_handler
;
1066 if_old
->force_link
= if_new
->force_link
;
1068 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1069 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1071 UPDATE(metric
, reload_ip
);
1072 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1073 UPDATE(ip4table
, reload_ip
);
1074 UPDATE(ip6table
, reload_ip
);
1075 interface_merge_assignment_data(if_old
, if_new
);
1080 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1082 interface_clear_errors(if_old
);
1083 set_config_state(if_old
, IFC_RELOAD
);
1088 bool config_ip_enabled
= if_old
->config_ip
.enabled
;
1089 bool proto_ip_enabled
= if_old
->proto_ip
.enabled
;
1091 interface_ip_set_enabled(&if_old
->config_ip
, false);
1092 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1093 interface_ip_set_enabled(&if_old
->proto_ip
, proto_ip_enabled
);
1094 interface_ip_set_enabled(&if_old
->config_ip
, config_ip_enabled
);
1097 interface_write_resolv_conf();
1098 if (if_old
->main_dev
.dev
)
1099 interface_check_state(if_old
);
1102 if_new
->config
= NULL
;
1103 interface_cleanup(if_new
);
1109 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1110 struct vlist_node
*node_old
)
1112 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1113 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1115 if (node_old
&& node_new
) {
1116 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1117 interface_change_config(if_old
, if_new
);
1118 } else if (node_old
) {
1119 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1120 set_config_state(if_old
, IFC_REMOVE
);
1121 } else if (node_new
) {
1122 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1123 proto_init_interface(if_new
, if_new
->config
);
1124 interface_claim_device(if_new
);
1125 netifd_ubus_add_interface(if_new
);
1131 interface_init_list(void)
1133 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1134 interfaces
.keep_old
= true;
1135 interfaces
.no_delete
= true;