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.
17 #include <sys/types.h>
22 #include "interface.h"
23 #include "interface-ip.h"
29 struct vlist_tree interfaces
;
30 static LIST_HEAD(iface_all_users
);
34 IFACE_ATTR_IFNAME
, /* Backward compatibility */
38 IFACE_ATTR_JAIL_IFNAME
,
39 IFACE_ATTR_DEFAULTROUTE
,
42 IFACE_ATTR_DNS_SEARCH
,
43 IFACE_ATTR_DNS_METRIC
,
52 IFACE_ATTR_IP6IFACEID
,
53 IFACE_ATTR_FORCE_LINK
,
58 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
59 [IFACE_ATTR_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
60 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
61 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
62 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
63 [IFACE_ATTR_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
64 [IFACE_ATTR_JAIL_IFNAME
] = { .name
= "jail_ifname", .type
= BLOBMSG_TYPE_STRING
},
65 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
66 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
67 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
68 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
69 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
70 [IFACE_ATTR_DNS_METRIC
] = { .name
= "dns_metric", .type
= BLOBMSG_TYPE_INT32
},
71 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
72 [IFACE_ATTR_IP6ASSIGN
] = { .name
= "ip6assign", .type
= BLOBMSG_TYPE_INT32
},
73 [IFACE_ATTR_IP6HINT
] = { .name
= "ip6hint", .type
= BLOBMSG_TYPE_STRING
},
74 [IFACE_ATTR_IP4TABLE
] = { .name
= "ip4table", .type
= BLOBMSG_TYPE_STRING
},
75 [IFACE_ATTR_IP6TABLE
] = { .name
= "ip6table", .type
= BLOBMSG_TYPE_STRING
},
76 [IFACE_ATTR_IP6CLASS
] = { .name
= "ip6class", .type
= BLOBMSG_TYPE_ARRAY
},
77 [IFACE_ATTR_DELEGATE
] = { .name
= "delegate", .type
= BLOBMSG_TYPE_BOOL
},
78 [IFACE_ATTR_IP6IFACEID
] = { .name
= "ip6ifaceid", .type
= BLOBMSG_TYPE_STRING
},
79 [IFACE_ATTR_FORCE_LINK
] = { .name
= "force_link", .type
= BLOBMSG_TYPE_BOOL
},
80 [IFACE_ATTR_IP6WEIGHT
] = { .name
= "ip6weight", .type
= BLOBMSG_TYPE_INT32
},
83 const struct uci_blob_param_list interface_attr_list
= {
84 .n_params
= IFACE_ATTR_MAX
,
85 .params
= iface_attrs
,
89 interface_set_main_dev(struct interface
*iface
, struct device
*dev
);
91 interface_event(struct interface
*iface
, enum interface_event ev
);
94 interface_error_flush(struct interface
*iface
)
96 struct interface_error
*error
, *tmp
;
98 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
99 list_del(&error
->list
);
105 interface_force_link(struct interface
*iface
)
107 struct device
*dev
= iface
->main_dev
.dev
;
109 if (dev
&& dev
->settings
.auth
)
112 return iface
->force_link
;
116 interface_clear_errors(struct interface
*iface
)
118 /* don't flush the errors in case the configured protocol handler matches the
119 running protocol handler and is having the last error capability */
120 if (!(iface
->proto
&&
121 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
122 (iface
->proto
->handler
->name
== iface
->proto_handler
->name
)))
123 interface_error_flush(iface
);
126 void interface_add_error(struct interface
*iface
, const char *subsystem
,
127 const char *code
, const char **data
, int n_data
)
129 struct interface_error
*error
;
132 char *dest
, *d_subsys
, *d_code
;
134 /* if the configured protocol handler has the last error support capability,
135 errors should only be added if the running protocol handler matches the
138 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
139 (iface
->proto
->handler
->name
!= iface
->proto_handler
->name
))
143 len
= n_data
* sizeof(char *);
144 datalen
= alloca(len
);
145 for (i
= 0; i
< n_data
; i
++) {
146 datalen
[i
] = strlen(data
[i
]) + 1;
151 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
152 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
153 &d_code
, code
? strlen(code
) + 1 : 0);
157 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
158 protocol can't be established (e.g auth failure) */
159 if (iface
->proto_handler
->flags
& PROTO_FLAG_LASTERROR
)
160 interface_error_flush(iface
);
162 list_add_tail(&error
->list
, &iface
->errors
);
164 dest
= (char *) &error
->data
[n_data
+ 1];
165 for (i
= 0; i
< n_data
; i
++) {
166 error
->data
[i
] = dest
;
167 memcpy(dest
, data
[i
], datalen
[i
]);
170 error
->data
[n_data
] = NULL
;
173 error
->subsystem
= strcpy(d_subsys
, subsystem
);
176 error
->code
= strcpy(d_code
, code
);
180 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
182 avl_delete(&iface
->data
, &data
->node
);
187 interface_data_flush(struct interface
*iface
)
189 struct interface_data
*d
, *tmp
;
191 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
192 interface_data_del(iface
, d
);
196 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
198 struct interface_data
*n
, *o
;
200 if (!blobmsg_check_attr(data
, true))
201 return UBUS_STATUS_INVALID_ARGUMENT
;
203 const char *name
= blobmsg_name(data
);
204 unsigned len
= blob_pad_len(data
);
206 o
= avl_find_element(&iface
->data
, name
, o
, node
);
208 if (blob_pad_len(o
->data
) == len
&& !memcmp(o
->data
, data
, len
))
211 interface_data_del(iface
, o
);
214 n
= calloc(1, sizeof(*n
) + len
);
216 return UBUS_STATUS_UNKNOWN_ERROR
;
218 memcpy(n
->data
, data
, len
);
219 n
->node
.key
= blobmsg_name(n
->data
);
220 avl_insert(&iface
->data
, &n
->node
);
222 iface
->updated
|= IUF_DATA
;
226 int interface_parse_data(struct interface
*iface
, const struct blob_attr
*attr
)
228 struct blob_attr
*cur
;
233 blob_for_each_attr(cur
, attr
, rem
) {
234 ret
= interface_add_data(iface
, cur
);
239 if (iface
->updated
&& iface
->state
== IFS_UP
)
240 interface_event(iface
, IFEV_UPDATE
);
246 interface_event(struct interface
*iface
, enum interface_event ev
)
248 struct interface_user
*dep
, *tmp
;
249 struct device
*adev
= NULL
;
251 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
252 dep
->cb(dep
, iface
, ev
);
254 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
255 dep
->cb(dep
, iface
, ev
);
259 interface_error_flush(iface
);
260 adev
= iface
->l3_dev
.dev
;
264 alias_notify_device(iface
->name
, adev
);
272 interface_flush_state(struct interface
*iface
)
274 if (iface
->l3_dev
.dev
)
275 device_release(&iface
->l3_dev
);
276 interface_data_flush(iface
);
280 mark_interface_down(struct interface
*iface
)
282 enum interface_state state
= iface
->state
;
284 if (state
== IFS_DOWN
)
287 iface
->link_up_event
= false;
288 iface
->state
= IFS_DOWN
;
292 interface_event(iface
, IFEV_DOWN
);
295 interface_event(iface
, IFEV_UP_FAILED
);
300 interface_ip_set_enabled(&iface
->config_ip
, false);
301 interface_ip_set_enabled(&iface
->proto_ip
, false);
302 interface_ip_flush(&iface
->proto_ip
);
303 interface_flush_state(iface
);
304 system_flush_routes();
308 __set_config_state(struct interface
*iface
, enum interface_config_state s
)
310 iface
->config_state
= s
;
314 __interface_set_down(struct interface
*iface
, bool force
)
316 enum interface_state state
= iface
->state
;
320 iface
->state
= IFS_TEARDOWN
;
322 __set_config_state(iface
, IFC_REMOVE
);
325 interface_event(iface
, IFEV_DOWN
);
327 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
329 interface_flush_state(iface
);
333 if (iface
->main_dev
.dev
)
334 device_release(&iface
->main_dev
);
343 __interface_set_up(struct interface
*iface
)
347 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
349 iface
->state
= IFS_SETUP
;
350 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
352 mark_interface_down(iface
);
358 interface_check_state(struct interface
*iface
)
360 bool link_state
= iface
->link_state
|| interface_force_link(iface
);
362 switch (iface
->state
) {
365 if (!iface
->enabled
|| !link_state
) {
366 iface
->state
= IFS_TEARDOWN
;
368 __set_config_state(iface
, IFC_REMOVE
);
370 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
374 if (!iface
->available
)
377 if (iface
->autostart
&& iface
->enabled
&& link_state
&& !config_init
)
378 __interface_set_up(iface
);
386 interface_set_enabled(struct interface
*iface
, bool new_state
)
388 if (iface
->enabled
== new_state
)
391 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
392 iface
->enabled
= new_state
;
393 interface_check_state(iface
);
397 interface_set_link_state(struct interface
*iface
, bool new_state
)
399 if (iface
->link_state
== new_state
)
402 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
403 iface
->link_state
= new_state
;
404 interface_check_state(iface
);
406 if (new_state
&& interface_force_link(iface
) &&
407 iface
->state
== IFS_UP
&& !iface
->link_up_event
) {
408 interface_event(iface
, IFEV_LINK_UP
);
409 iface
->link_up_event
= true;
414 interface_ext_dev_cb(struct device_user
*dep
, enum device_event ev
)
416 if (ev
== DEV_EVENT_REMOVE
)
417 device_remove_user(dep
);
421 interface_main_dev_cb(struct device_user
*dep
, enum device_event ev
)
423 struct interface
*iface
;
425 iface
= container_of(dep
, struct interface
, main_dev
);
428 interface_set_available(iface
, true);
430 case DEV_EVENT_REMOVE
:
431 interface_set_available(iface
, false);
432 if (dep
->dev
&& dep
->dev
->external
)
433 interface_set_main_dev(iface
, NULL
);
436 interface_set_enabled(iface
, true);
439 interface_set_enabled(iface
, false);
441 case DEV_EVENT_AUTH_UP
:
442 case DEV_EVENT_LINK_UP
:
443 case DEV_EVENT_LINK_DOWN
:
444 interface_set_link_state(iface
, device_link_active(dep
->dev
));
446 case DEV_EVENT_TOPO_CHANGE
:
447 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
455 interface_l3_dev_cb(struct device_user
*dep
, enum device_event ev
)
457 struct interface
*iface
;
459 iface
= container_of(dep
, struct interface
, l3_dev
);
460 if (iface
->l3_dev
.dev
== iface
->main_dev
.dev
)
464 case DEV_EVENT_LINK_DOWN
:
465 if (iface
->proto_handler
->flags
& PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN
)
466 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
474 interface_set_available(struct interface
*iface
, bool new_state
)
476 if (iface
->available
== new_state
)
479 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
480 iface
->available
= new_state
;
483 if (iface
->autostart
&& !config_init
)
484 interface_set_up(iface
);
486 __interface_set_down(iface
, true);
490 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
493 list_add(&dep
->list
, &iface_all_users
);
498 list_add(&dep
->list
, &iface
->users
);
499 if (iface
->state
== IFS_UP
)
500 dep
->cb(dep
, iface
, IFEV_UP
);
504 interface_remove_user(struct interface_user
*dep
)
506 list_del_init(&dep
->list
);
511 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
513 struct blob_attr
*cur
;
516 blobmsg_for_each_attr(cur
, list
, rem
) {
517 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
520 if (!blobmsg_check_attr(cur
, false))
523 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
524 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
525 list_add(&c
->head
, &iface
->assignment_classes
);
530 interface_clear_assignment_classes(struct interface
*iface
)
532 while (!list_empty(&iface
->assignment_classes
)) {
533 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
534 struct interface_assignment_class
, head
);
541 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
543 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
544 old
->assignment_length
!= new->assignment_length
||
545 old
->assignment_iface_id_selection
!= new->assignment_iface_id_selection
||
546 old
->assignment_weight
!= new->assignment_weight
||
547 (old
->assignment_iface_id_selection
== IFID_FIXED
&&
548 memcmp(&old
->assignment_fixed_iface_id
, &new->assignment_fixed_iface_id
,
549 sizeof(old
->assignment_fixed_iface_id
))) ||
550 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
552 struct interface_assignment_class
*c
;
553 list_for_each_entry(c
, &new->assignment_classes
, head
) {
554 /* Compare list entries one-by-one to see if there was a change */
555 if (list_empty(&old
->assignment_classes
)) /* The new list is longer */
561 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
562 struct interface_assignment_class
, head
);
564 if (strcmp(c_old
->name
, c
->name
)) /* An entry didn't match */
567 list_del(&c_old
->head
);
571 /* The old list was longer than the new one or the last entry didn't match */
572 if (!list_empty(&old
->assignment_classes
)) {
573 interface_clear_assignment_classes(old
);
577 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
580 old
->assignment_hint
= new->assignment_hint
;
581 old
->assignment_length
= new->assignment_length
;
582 old
->assignment_iface_id_selection
= new->assignment_iface_id_selection
;
583 old
->assignment_fixed_iface_id
= new->assignment_fixed_iface_id
;
584 old
->assignment_weight
= new->assignment_weight
;
585 interface_refresh_assignments(true);
590 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
592 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
593 struct device
*dev
= iface
->l3_dev
.dev
;
600 interface_set_main_dev(alias
, dev
);
601 interface_set_available(alias
, true);
605 interface_set_available(alias
, false);
606 interface_set_main_dev(alias
, NULL
);
609 interface_remove_user(dep
);
617 interface_set_device_config(struct interface
*iface
, struct device
*dev
)
619 if (!dev
|| !dev
->default_config
)
622 if (!iface
->device_config
&&
623 (!dev
->iface_config
|| dev
->config_iface
!= iface
))
626 dev
->config_iface
= iface
;
627 dev
->iface_config
= iface
->device_config
;
628 device_apply_config(dev
, dev
->type
, iface
->config
);
632 interface_claim_device(struct interface
*iface
)
634 struct interface
*parent
;
635 struct device
*dev
= NULL
;
637 if (iface
->parent_iface
.iface
)
638 interface_remove_user(&iface
->parent_iface
);
640 if (iface
->parent_ifname
) {
641 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
642 iface
->parent_iface
.cb
= interface_alias_cb
;
643 interface_add_user(&iface
->parent_iface
, parent
);
644 } else if (iface
->device
&&
645 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
646 dev
= device_get(iface
->device
, true);
647 interface_set_device_config(iface
, dev
);
649 dev
= iface
->ext_dev
.dev
;
653 interface_set_main_dev(iface
, dev
);
655 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
656 interface_set_available(iface
, true);
660 interface_cleanup_state(struct interface
*iface
)
662 interface_set_available(iface
, false);
664 interface_flush_state(iface
);
665 interface_clear_errors(iface
);
666 interface_set_proto_state(iface
, NULL
);
668 interface_set_main_dev(iface
, NULL
);
669 interface_set_l3_dev(iface
, NULL
);
673 interface_cleanup(struct interface
*iface
)
675 struct interface_user
*dep
, *tmp
;
677 uloop_timeout_cancel(&iface
->remove_timer
);
678 device_remove_user(&iface
->ext_dev
);
680 if (iface
->parent_iface
.iface
)
681 interface_remove_user(&iface
->parent_iface
);
683 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
684 interface_remove_user(dep
);
686 interface_clear_assignment_classes(iface
);
687 interface_ip_flush(&iface
->config_ip
);
688 interface_cleanup_state(iface
);
692 interface_do_free(struct interface
*iface
)
694 interface_event(iface
, IFEV_FREE
);
695 interface_cleanup(iface
);
697 netifd_ubus_remove_interface(iface
);
698 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
701 if (iface
->jail_ifname
)
702 free(iface
->jail_ifname
);
708 interface_do_reload(struct interface
*iface
)
710 interface_event(iface
, IFEV_RELOAD
);
711 interface_cleanup_state(iface
);
712 proto_init_interface(iface
, iface
->config
);
713 interface_claim_device(iface
);
717 interface_handle_config_change(struct interface
*iface
)
719 enum interface_config_state state
= iface
->config_state
;
721 iface
->config_state
= IFC_NORMAL
;
726 interface_do_reload(iface
);
729 interface_do_free(iface
);
732 if (iface
->autostart
)
733 interface_set_up(iface
);
737 interface_proto_event_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
739 struct interface
*iface
= state
->iface
;
743 if (iface
->state
!= IFS_SETUP
) {
744 if (iface
->state
== IFS_UP
&& iface
->updated
)
745 interface_event(iface
, IFEV_UPDATE
);
749 if (!iface
->l3_dev
.dev
)
750 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
752 interface_ip_set_enabled(&iface
->config_ip
, true);
753 interface_ip_set_enabled(&iface
->proto_ip
, true);
754 system_flush_routes();
755 iface
->state
= IFS_UP
;
756 iface
->start_time
= system_get_rtime();
757 interface_event(iface
, IFEV_UP
);
758 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
761 if (iface
->state
== IFS_DOWN
)
764 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
765 mark_interface_down(iface
);
766 if (iface
->main_dev
.dev
)
767 device_release(&iface
->main_dev
);
768 if (iface
->l3_dev
.dev
)
769 device_remove_user(&iface
->l3_dev
);
770 interface_handle_config_change(iface
);
772 case IFPEV_LINK_LOST
:
773 if (iface
->state
!= IFS_UP
)
776 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
777 mark_interface_down(iface
);
778 iface
->state
= IFS_SETUP
;
784 interface_write_resolv_conf(iface
->jail
);
787 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
790 iface
->proto
->free(iface
->proto
);
793 iface
->state
= IFS_DOWN
;
794 iface
->proto
= state
;
798 state
->proto_event
= interface_proto_event_cb
;
799 state
->iface
= iface
;
803 interface_alloc(const char *name
, struct blob_attr
*config
, bool dynamic
)
805 struct interface
*iface
;
806 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
807 struct blob_attr
*cur
;
808 const char *proto_name
= NULL
;
810 bool force_link
= false;
812 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
813 iface
->name
= strcpy(iface_name
, name
);
814 INIT_LIST_HEAD(&iface
->errors
);
815 INIT_LIST_HEAD(&iface
->users
);
816 INIT_LIST_HEAD(&iface
->hotplug_list
);
817 INIT_LIST_HEAD(&iface
->assignment_classes
);
818 interface_ip_init(iface
);
819 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
820 iface
->config_ip
.enabled
= false;
822 iface
->main_dev
.cb
= interface_main_dev_cb
;
823 iface
->l3_dev
.cb
= interface_l3_dev_cb
;
824 iface
->ext_dev
.cb
= interface_ext_dev_cb
;
826 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
827 blob_data(config
), blob_len(config
));
829 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
830 proto_name
= blobmsg_data(cur
);
832 proto_attach_interface(iface
, proto_name
);
833 if (iface
->proto_handler
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
)
836 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
837 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], force_link
);
838 iface
->dynamic
= dynamic
;
839 iface
->proto_ip
.no_defaultroute
=
840 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
841 iface
->proto_ip
.no_dns
=
842 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
844 if ((cur
= tb
[IFACE_ATTR_DNS
]))
845 interface_add_dns_server_list(&iface
->config_ip
, cur
);
847 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
848 interface_add_dns_search_list(&iface
->config_ip
, cur
);
850 if ((cur
= tb
[IFACE_ATTR_DNS_METRIC
]))
851 iface
->dns_metric
= blobmsg_get_u32(cur
);
853 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
854 iface
->metric
= blobmsg_get_u32(cur
);
856 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
857 iface
->assignment_length
= blobmsg_get_u32(cur
);
860 iface
->assignment_iface_id_selection
= IFID_FIXED
;
861 iface
->assignment_fixed_iface_id
= in6addr_any
;
862 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
864 if ((cur
= tb
[IFACE_ATTR_IP6IFACEID
])) {
865 const char *ifaceid
= blobmsg_data(cur
);
866 if (!strcmp(ifaceid
, "random")) {
867 iface
->assignment_iface_id_selection
= IFID_RANDOM
;
869 else if (!strcmp(ifaceid
, "eui64")) {
870 iface
->assignment_iface_id_selection
= IFID_EUI64
;
873 /* we expect an IPv6 address with network id zero here -> fixed iface id
874 if we cannot parse -> revert to iface id 1 */
875 if (inet_pton(AF_INET6
,ifaceid
,&iface
->assignment_fixed_iface_id
) != 1 ||
876 iface
->assignment_fixed_iface_id
.s6_addr32
[0] != 0 ||
877 iface
->assignment_fixed_iface_id
.s6_addr32
[1] != 0) {
878 iface
->assignment_fixed_iface_id
= in6addr_any
;
879 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
880 netifd_log_message(L_WARNING
, "Failed to parse ip6ifaceid for interface '%s', \
881 falling back to iface id 1.\n", iface
->name
);
886 iface
->assignment_hint
= -1;
887 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
888 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
889 ~((1 << (64 - iface
->assignment_length
)) - 1);
891 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
892 interface_add_assignment_classes(iface
, cur
);
894 if ((cur
= tb
[IFACE_ATTR_IP6WEIGHT
]))
895 iface
->assignment_weight
= blobmsg_get_u32(cur
);
897 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
898 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
899 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
902 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
903 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
904 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
907 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
909 iface
->config_autostart
= iface
->autostart
;
912 if ((cur
= tb
[IFACE_ATTR_JAIL
])) {
913 iface
->jail
= strdup(blobmsg_get_string(cur
));
914 iface
->autostart
= false;
917 iface
->jail_ifname
= NULL
;
918 if ((cur
= tb
[IFACE_ATTR_JAIL_IFNAME
]))
919 iface
->jail_ifname
= strdup(blobmsg_get_string(cur
));
924 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
926 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
927 struct blob_attr
*cur
;
930 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
931 blob_data(config
), blob_len(config
));
934 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
935 iface
->parent_ifname
= blobmsg_data(cur
);
937 if (!iface
->parent_ifname
)
940 cur
= tb
[IFACE_ATTR_DEVICE
];
942 cur
= tb
[IFACE_ATTR_IFNAME
];
944 iface
->device
= blobmsg_data(cur
);
947 if (iface
->dynamic
) {
948 name
= strdup(iface
->name
);
954 iface
->config
= config
;
955 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
958 iface
= vlist_find(&interfaces
, name
, iface
, node
);
961 /* Don't delete dynamic interface on reload */
963 iface
->node
.version
= -1;
970 interface_add(struct interface
*iface
, struct blob_attr
*config
)
972 return __interface_add(iface
, config
, false);
976 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
978 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
981 return __interface_add(iface
, config
, true);
985 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
987 bool enabled
= iface
->config_ip
.enabled
;
988 bool claimed
= iface
->l3_dev
.claimed
;
990 if (iface
->l3_dev
.dev
== dev
)
993 interface_ip_set_enabled(&iface
->config_ip
, false);
994 interface_ip_set_enabled(&iface
->proto_ip
, false);
995 interface_ip_flush(&iface
->proto_ip
);
996 device_add_user(&iface
->l3_dev
, dev
);
1000 if (device_claim(&iface
->l3_dev
) < 0)
1003 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
1004 interface_ip_set_enabled(&iface
->proto_ip
, enabled
);
1009 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
1011 bool claimed
= iface
->l3_dev
.claimed
;
1013 if (iface
->main_dev
.dev
== dev
)
1016 interface_set_available(iface
, false);
1017 device_add_user(&iface
->main_dev
, dev
);
1019 interface_set_link_state(iface
, false);
1024 if (device_claim(&iface
->l3_dev
) < 0)
1028 if (!iface
->l3_dev
.dev
)
1029 interface_set_l3_dev(iface
, dev
);
1033 interface_remove_link(struct interface
*iface
, struct device
*dev
,
1034 struct blob_attr
*vlan
)
1036 struct device
*mdev
= iface
->main_dev
.dev
;
1038 if (mdev
&& mdev
->hotplug_ops
)
1039 return mdev
->hotplug_ops
->del(mdev
, dev
, vlan
);
1041 if (dev
== iface
->ext_dev
.dev
)
1042 device_remove_user(&iface
->ext_dev
);
1044 if (!iface
->main_dev
.hotplug
)
1045 return UBUS_STATUS_INVALID_ARGUMENT
;
1047 if (dev
!= iface
->main_dev
.dev
)
1048 return UBUS_STATUS_INVALID_ARGUMENT
;
1050 interface_set_main_dev(iface
, NULL
);
1055 interface_add_link(struct interface
*iface
, struct device
*dev
,
1056 struct blob_attr
*vlan
, bool link_ext
)
1058 struct device
*mdev
= iface
->main_dev
.dev
;
1063 if (iface
->main_dev
.hotplug
)
1064 device_remove_user(&iface
->main_dev
);
1067 if (mdev
->hotplug_ops
)
1068 return mdev
->hotplug_ops
->add(mdev
, dev
, vlan
);
1070 return UBUS_STATUS_NOT_SUPPORTED
;
1074 device_add_user(&iface
->ext_dev
, dev
);
1076 interface_set_main_dev(iface
, dev
);
1077 iface
->main_dev
.hotplug
= true;
1082 interface_handle_link(struct interface
*iface
, const char *name
,
1083 struct blob_attr
*vlan
, bool add
, bool link_ext
)
1087 dev
= device_get(name
, add
? (link_ext
? 2 : 1) : 0);
1089 return UBUS_STATUS_NOT_FOUND
;
1092 return interface_remove_link(iface
, dev
, vlan
);
1094 interface_set_device_config(iface
, dev
);
1096 device_set_present(dev
, true);
1098 return interface_add_link(iface
, dev
, vlan
, link_ext
);
1102 interface_set_up(struct interface
*iface
)
1105 const char *error
= NULL
;
1107 iface
->autostart
= true;
1109 if (iface
->state
!= IFS_DOWN
)
1112 interface_clear_errors(iface
);
1113 if (iface
->available
) {
1114 if (iface
->main_dev
.dev
) {
1115 ret
= device_claim(&iface
->main_dev
);
1117 interface_check_state(iface
);
1119 error
= "DEVICE_CLAIM_FAILED";
1121 ret
= __interface_set_up(iface
);
1123 error
= "SETUP_FAILED";
1126 error
= "NO_DEVICE";
1129 interface_add_error(iface
, "interface", error
, NULL
, 0);
1133 interface_set_down(struct interface
*iface
)
1136 vlist_for_each_element(&interfaces
, iface
, node
)
1137 __interface_set_down(iface
, false);
1139 iface
->autostart
= false;
1140 __interface_set_down(iface
, false);
1145 interface_renew(struct interface
*iface
)
1147 if (iface
->state
== IFS_TEARDOWN
|| iface
->state
== IFS_DOWN
)
1150 return interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
1154 interface_start_pending(void)
1156 struct interface
*iface
;
1158 vlist_for_each_element(&interfaces
, iface
, node
) {
1159 if (iface
->autostart
)
1160 interface_set_up(iface
);
1165 interface_start_jail(const char *jail
, const pid_t netns_pid
)
1167 struct interface
*iface
;
1172 netns_fd
= system_netns_open(netns_pid
);
1176 vlist_for_each_element(&interfaces
, iface
, node
) {
1177 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1180 system_link_netns_move(iface
->main_dev
.dev
, netns_fd
, iface
->jail_ifname
);
1187 waitpid(pr
, &wstatus
, WUNTRACED
| WCONTINUED
);
1192 netns_fd
= system_netns_open(netns_pid
);
1196 system_netns_set(netns_fd
);
1198 vlist_for_each_element(&interfaces
, iface
, node
) {
1199 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1203 * The interface has already been renamed and is inside target
1204 * namespace, hence overwrite ifname with jail_ifname for
1205 * interface_set_up().
1206 * We are inside a fork which got it's own copy of the interfaces
1207 * list, so we can mess with it :)
1209 if (iface
->jail_ifname
)
1210 iface
->device
= iface
->jail_ifname
;
1212 interface_do_reload(iface
);
1213 interface_set_up(iface
);
1221 interface_stop_jail(const char *jail
, const pid_t netns_pid
)
1223 struct interface
*iface
;
1224 int netns_fd
, root_netns
;
1226 pid_t parent_pid
= getpid();
1228 const char *orig_ifname
;
1232 waitpid(pr
, &wstatus
, WUNTRACED
| WCONTINUED
);
1237 root_netns
= system_netns_open(parent_pid
);
1241 netns_fd
= system_netns_open(netns_pid
);
1245 system_netns_set(netns_fd
);
1247 vlist_for_each_element(&interfaces
, iface
, node
) {
1248 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1251 orig_ifname
= iface
->device
;
1252 if (iface
->jail_ifname
)
1253 iface
->device
= iface
->jail_ifname
;
1255 interface_do_reload(iface
);
1256 interface_set_down(iface
);
1257 system_link_netns_move(iface
->main_dev
.dev
, root_netns
, orig_ifname
);
1266 set_config_state(struct interface
*iface
, enum interface_config_state s
)
1268 __set_config_state(iface
, s
);
1269 if (iface
->state
== IFS_DOWN
)
1270 interface_handle_config_change(iface
);
1272 __interface_set_down(iface
, false);
1276 interface_update_start(struct interface
*iface
, const bool keep_old
)
1281 interface_ip_update_start(&iface
->proto_ip
);
1285 interface_update_complete(struct interface
*iface
)
1287 interface_ip_update_complete(&iface
->proto_ip
);
1291 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
1293 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
1294 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
1298 interface_device_config_changed(struct interface
*if_old
, struct interface
*if_new
)
1300 struct blob_attr
*ntb
[__DEV_ATTR_MAX
];
1301 struct blob_attr
*otb
[__DEV_ATTR_MAX
];
1302 struct device
*dev
= if_old
->main_dev
.dev
;
1303 unsigned long diff
= 0;
1305 BUILD_BUG_ON(sizeof(diff
) < __DEV_ATTR_MAX
/ 8);
1310 if (if_old
->device_config
!= if_new
->device_config
)
1313 if (!if_new
->device_config
)
1316 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb
,
1317 blob_data(if_old
->config
), blob_len(if_old
->config
));
1319 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, ntb
,
1320 blob_data(if_new
->config
), blob_len(if_new
->config
));
1322 uci_blob_diff(ntb
, otb
, &device_attr_list
, &diff
);
1327 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
1329 struct blob_attr
*old_config
= if_old
->config
;
1330 bool reload
= false, reload_ip
= false, update_prefix_delegation
= false;
1332 #define FIELD_CHANGED_STR(field) \
1333 ((!!if_old->field != !!if_new->field) || \
1335 strcmp(if_old->field, if_new->field) != 0))
1337 if (FIELD_CHANGED_STR(parent_ifname
)) {
1338 if (if_old
->parent_iface
.iface
)
1339 interface_remove_user(&if_old
->parent_iface
);
1343 if (!reload
&& interface_device_config_changed(if_old
, if_new
))
1346 if (FIELD_CHANGED_STR(device
) ||
1347 if_old
->proto_handler
!= if_new
->proto_handler
)
1350 if (!if_old
->proto_handler
->config_params
)
1351 D(INTERFACE
, "No config parameters for interface '%s'\n",
1353 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
1354 if_old
->proto_handler
->config_params
))
1357 #define UPDATE(field, __var) ({ \
1358 bool __changed = (if_old->field != if_new->field); \
1359 if_old->field = if_new->field; \
1360 __var |= __changed; \
1363 if_old
->config
= if_new
->config
;
1364 if (if_old
->config_autostart
!= if_new
->config_autostart
) {
1365 if (if_old
->config_autostart
)
1368 if_old
->autostart
= if_new
->config_autostart
;
1371 if_old
->device_config
= if_new
->device_config
;
1372 if_old
->config_autostart
= if_new
->config_autostart
;
1376 if_old
->jail
= if_new
->jail
;
1378 if_old
->autostart
= false;
1380 if (if_old
->jail_ifname
)
1381 free(if_old
->jail_ifname
);
1383 if_old
->jail_ifname
= if_new
->jail_ifname
;
1385 if_old
->device
= if_new
->device
;
1386 if_old
->parent_ifname
= if_new
->parent_ifname
;
1387 if_old
->dynamic
= if_new
->dynamic
;
1388 if_old
->proto_handler
= if_new
->proto_handler
;
1389 if_old
->force_link
= if_new
->force_link
;
1390 if_old
->dns_metric
= if_new
->dns_metric
;
1392 if (if_old
->proto_ip
.no_delegation
!= if_new
->proto_ip
.no_delegation
) {
1393 if_old
->proto_ip
.no_delegation
= if_new
->proto_ip
.no_delegation
;
1394 update_prefix_delegation
= true;
1397 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1398 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1400 UPDATE(metric
, reload_ip
);
1401 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1402 UPDATE(ip4table
, reload_ip
);
1403 UPDATE(ip6table
, reload_ip
);
1404 interface_merge_assignment_data(if_old
, if_new
);
1409 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1411 interface_clear_errors(if_old
);
1412 set_config_state(if_old
, IFC_RELOAD
);
1417 bool config_ip_enabled
= if_old
->config_ip
.enabled
;
1418 bool proto_ip_enabled
= if_old
->proto_ip
.enabled
;
1420 interface_ip_set_enabled(&if_old
->config_ip
, false);
1421 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1422 interface_ip_set_enabled(&if_old
->proto_ip
, proto_ip_enabled
);
1423 interface_ip_set_enabled(&if_old
->config_ip
, config_ip_enabled
);
1426 if (update_prefix_delegation
)
1427 interface_update_prefix_delegation(&if_old
->proto_ip
);
1429 interface_write_resolv_conf(if_old
->jail
);
1430 if (if_old
->main_dev
.dev
)
1431 interface_check_state(if_old
);
1434 if_new
->config
= NULL
;
1435 interface_cleanup(if_new
);
1441 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1442 struct vlist_node
*node_old
)
1444 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1445 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1447 if (node_old
&& node_new
) {
1448 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1449 interface_change_config(if_old
, if_new
);
1450 } else if (node_old
) {
1451 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1452 set_config_state(if_old
, IFC_REMOVE
);
1453 } else if (node_new
) {
1454 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1455 interface_event(if_new
, IFEV_CREATE
);
1456 proto_init_interface(if_new
, if_new
->config
);
1457 interface_claim_device(if_new
);
1458 netifd_ubus_add_interface(if_new
);
1464 interface_init_list(void)
1466 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1467 interfaces
.keep_old
= true;
1468 interfaces
.no_delete
= true;