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
);
642 if (iface
->parent_ifname
) {
643 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
644 iface
->parent_iface
.cb
= interface_alias_cb
;
645 interface_add_user(&iface
->parent_iface
, parent
);
646 } else if (iface
->device
&&
647 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
648 dev
= device_get(iface
->device
, true);
649 interface_set_device_config(iface
, dev
);
651 dev
= iface
->ext_dev
.dev
;
655 interface_set_main_dev(iface
, dev
);
659 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
660 interface_set_available(iface
, true);
664 interface_cleanup_state(struct interface
*iface
)
666 interface_set_available(iface
, false);
668 interface_flush_state(iface
);
669 interface_clear_errors(iface
);
670 interface_set_proto_state(iface
, NULL
);
672 interface_set_main_dev(iface
, NULL
);
673 interface_set_l3_dev(iface
, NULL
);
677 interface_cleanup(struct interface
*iface
)
679 struct interface_user
*dep
, *tmp
;
681 uloop_timeout_cancel(&iface
->remove_timer
);
682 device_remove_user(&iface
->ext_dev
);
684 if (iface
->parent_iface
.iface
)
685 interface_remove_user(&iface
->parent_iface
);
687 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
688 interface_remove_user(dep
);
690 interface_clear_assignment_classes(iface
);
691 interface_ip_flush(&iface
->config_ip
);
692 interface_cleanup_state(iface
);
696 interface_do_free(struct interface
*iface
)
698 interface_event(iface
, IFEV_FREE
);
699 interface_cleanup(iface
);
701 netifd_ubus_remove_interface(iface
);
702 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
705 if (iface
->jail_ifname
)
706 free(iface
->jail_ifname
);
712 interface_do_reload(struct interface
*iface
)
714 interface_event(iface
, IFEV_RELOAD
);
715 interface_cleanup_state(iface
);
716 proto_init_interface(iface
, iface
->config
);
717 interface_claim_device(iface
);
721 interface_handle_config_change(struct interface
*iface
)
723 enum interface_config_state state
= iface
->config_state
;
725 iface
->config_state
= IFC_NORMAL
;
730 interface_do_reload(iface
);
733 interface_do_free(iface
);
736 if (iface
->autostart
)
737 interface_set_up(iface
);
741 interface_proto_event_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
743 struct interface
*iface
= state
->iface
;
747 if (iface
->state
!= IFS_SETUP
) {
748 if (iface
->state
== IFS_UP
&& iface
->updated
)
749 interface_event(iface
, IFEV_UPDATE
);
753 if (!iface
->l3_dev
.dev
)
754 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
756 interface_ip_set_enabled(&iface
->config_ip
, true);
757 interface_ip_set_enabled(&iface
->proto_ip
, true);
758 system_flush_routes();
759 iface
->state
= IFS_UP
;
760 iface
->start_time
= system_get_rtime();
761 interface_event(iface
, IFEV_UP
);
762 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
765 if (iface
->state
== IFS_DOWN
)
768 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
769 mark_interface_down(iface
);
770 if (iface
->main_dev
.dev
)
771 device_release(&iface
->main_dev
);
772 if (iface
->l3_dev
.dev
)
773 device_remove_user(&iface
->l3_dev
);
774 interface_handle_config_change(iface
);
776 case IFPEV_LINK_LOST
:
777 if (iface
->state
!= IFS_UP
)
780 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
781 mark_interface_down(iface
);
782 iface
->state
= IFS_SETUP
;
788 interface_write_resolv_conf(iface
->jail
);
791 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
794 iface
->proto
->free(iface
->proto
);
797 iface
->state
= IFS_DOWN
;
798 iface
->proto
= state
;
802 state
->proto_event
= interface_proto_event_cb
;
803 state
->iface
= iface
;
807 interface_alloc(const char *name
, struct blob_attr
*config
, bool dynamic
)
809 struct interface
*iface
;
810 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
811 struct blob_attr
*cur
;
812 const char *proto_name
= NULL
;
814 bool force_link
= false;
816 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
817 iface
->name
= strcpy(iface_name
, name
);
818 INIT_LIST_HEAD(&iface
->errors
);
819 INIT_LIST_HEAD(&iface
->users
);
820 INIT_LIST_HEAD(&iface
->hotplug_list
);
821 INIT_LIST_HEAD(&iface
->assignment_classes
);
822 interface_ip_init(iface
);
823 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
824 iface
->config_ip
.enabled
= false;
826 iface
->main_dev
.cb
= interface_main_dev_cb
;
827 iface
->l3_dev
.cb
= interface_l3_dev_cb
;
828 iface
->ext_dev
.cb
= interface_ext_dev_cb
;
830 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
831 blob_data(config
), blob_len(config
));
833 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
834 proto_name
= blobmsg_data(cur
);
836 proto_attach_interface(iface
, proto_name
);
837 if (iface
->proto_handler
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
)
840 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
841 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], force_link
);
842 iface
->dynamic
= dynamic
;
843 iface
->proto_ip
.no_defaultroute
=
844 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
845 iface
->proto_ip
.no_dns
=
846 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
848 if ((cur
= tb
[IFACE_ATTR_DNS
]))
849 interface_add_dns_server_list(&iface
->config_ip
, cur
);
851 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
852 interface_add_dns_search_list(&iface
->config_ip
, cur
);
854 if ((cur
= tb
[IFACE_ATTR_DNS_METRIC
]))
855 iface
->dns_metric
= blobmsg_get_u32(cur
);
857 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
858 iface
->metric
= blobmsg_get_u32(cur
);
860 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
861 iface
->assignment_length
= blobmsg_get_u32(cur
);
864 iface
->assignment_iface_id_selection
= IFID_FIXED
;
865 iface
->assignment_fixed_iface_id
= in6addr_any
;
866 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
868 if ((cur
= tb
[IFACE_ATTR_IP6IFACEID
])) {
869 const char *ifaceid
= blobmsg_data(cur
);
870 if (!strcmp(ifaceid
, "random")) {
871 iface
->assignment_iface_id_selection
= IFID_RANDOM
;
873 else if (!strcmp(ifaceid
, "eui64")) {
874 iface
->assignment_iface_id_selection
= IFID_EUI64
;
877 /* we expect an IPv6 address with network id zero here -> fixed iface id
878 if we cannot parse -> revert to iface id 1 */
879 if (inet_pton(AF_INET6
,ifaceid
,&iface
->assignment_fixed_iface_id
) != 1 ||
880 iface
->assignment_fixed_iface_id
.s6_addr32
[0] != 0 ||
881 iface
->assignment_fixed_iface_id
.s6_addr32
[1] != 0) {
882 iface
->assignment_fixed_iface_id
= in6addr_any
;
883 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
884 netifd_log_message(L_WARNING
, "Failed to parse ip6ifaceid for interface '%s', \
885 falling back to iface id 1.\n", iface
->name
);
890 iface
->assignment_hint
= -1;
891 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
892 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
893 ~((1 << (64 - iface
->assignment_length
)) - 1);
895 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
896 interface_add_assignment_classes(iface
, cur
);
898 if ((cur
= tb
[IFACE_ATTR_IP6WEIGHT
]))
899 iface
->assignment_weight
= blobmsg_get_u32(cur
);
901 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
902 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
903 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
906 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
907 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
908 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
911 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
913 iface
->config_autostart
= iface
->autostart
;
916 if ((cur
= tb
[IFACE_ATTR_JAIL
])) {
917 iface
->jail
= strdup(blobmsg_get_string(cur
));
918 iface
->autostart
= false;
921 iface
->jail_ifname
= NULL
;
922 if ((cur
= tb
[IFACE_ATTR_JAIL_IFNAME
]))
923 iface
->jail_ifname
= strdup(blobmsg_get_string(cur
));
928 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
930 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
931 struct blob_attr
*cur
;
934 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
935 blob_data(config
), blob_len(config
));
938 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
939 iface
->parent_ifname
= blobmsg_data(cur
);
941 if (!iface
->parent_ifname
)
944 cur
= tb
[IFACE_ATTR_DEVICE
];
946 cur
= tb
[IFACE_ATTR_IFNAME
];
948 iface
->device
= blobmsg_data(cur
);
951 if (iface
->dynamic
) {
952 name
= strdup(iface
->name
);
958 iface
->config
= config
;
959 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
962 iface
= vlist_find(&interfaces
, name
, iface
, node
);
965 /* Don't delete dynamic interface on reload */
967 iface
->node
.version
= -1;
974 interface_add(struct interface
*iface
, struct blob_attr
*config
)
976 return __interface_add(iface
, config
, false);
980 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
982 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
985 return __interface_add(iface
, config
, true);
989 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
991 bool enabled
= iface
->config_ip
.enabled
;
992 bool claimed
= iface
->l3_dev
.claimed
;
994 if (iface
->l3_dev
.dev
== dev
)
997 interface_ip_set_enabled(&iface
->config_ip
, false);
998 interface_ip_set_enabled(&iface
->proto_ip
, false);
999 interface_ip_flush(&iface
->proto_ip
);
1000 device_add_user(&iface
->l3_dev
, dev
);
1004 if (device_claim(&iface
->l3_dev
) < 0)
1007 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
1008 interface_ip_set_enabled(&iface
->proto_ip
, enabled
);
1013 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
1015 bool claimed
= iface
->l3_dev
.claimed
;
1017 if (iface
->main_dev
.dev
== dev
)
1020 interface_set_available(iface
, false);
1021 device_add_user(&iface
->main_dev
, dev
);
1023 interface_set_link_state(iface
, false);
1028 if (device_claim(&iface
->l3_dev
) < 0)
1032 if (!iface
->l3_dev
.dev
)
1033 interface_set_l3_dev(iface
, dev
);
1037 interface_remove_link(struct interface
*iface
, struct device
*dev
,
1038 struct blob_attr
*vlan
)
1040 struct device
*mdev
= iface
->main_dev
.dev
;
1042 if (mdev
&& mdev
->hotplug_ops
)
1043 return mdev
->hotplug_ops
->del(mdev
, dev
, vlan
);
1045 if (dev
== iface
->ext_dev
.dev
)
1046 device_remove_user(&iface
->ext_dev
);
1048 if (!iface
->main_dev
.hotplug
)
1049 return UBUS_STATUS_INVALID_ARGUMENT
;
1051 if (dev
!= iface
->main_dev
.dev
)
1052 return UBUS_STATUS_INVALID_ARGUMENT
;
1054 interface_set_main_dev(iface
, NULL
);
1059 interface_add_link(struct interface
*iface
, struct device
*dev
,
1060 struct blob_attr
*vlan
, bool link_ext
)
1062 struct device
*mdev
= iface
->main_dev
.dev
;
1067 if (iface
->main_dev
.hotplug
)
1068 device_remove_user(&iface
->main_dev
);
1071 if (mdev
->hotplug_ops
)
1072 return mdev
->hotplug_ops
->add(mdev
, dev
, vlan
);
1074 return UBUS_STATUS_NOT_SUPPORTED
;
1078 device_add_user(&iface
->ext_dev
, dev
);
1080 interface_set_main_dev(iface
, dev
);
1081 iface
->main_dev
.hotplug
= true;
1086 interface_handle_link(struct interface
*iface
, const char *name
,
1087 struct blob_attr
*vlan
, bool add
, bool link_ext
)
1094 dev
= device_get(name
, add
? (link_ext
? 2 : 1) : 0);
1096 ret
= UBUS_STATUS_NOT_FOUND
;
1101 interface_set_device_config(iface
, dev
);
1103 device_set_present(dev
, true);
1105 ret
= interface_add_link(iface
, dev
, vlan
, link_ext
);
1107 ret
= interface_remove_link(iface
, dev
, vlan
);
1117 interface_set_up(struct interface
*iface
)
1120 const char *error
= NULL
;
1122 iface
->autostart
= true;
1124 if (iface
->state
!= IFS_DOWN
)
1127 interface_clear_errors(iface
);
1128 if (iface
->available
) {
1129 if (iface
->main_dev
.dev
) {
1130 ret
= device_claim(&iface
->main_dev
);
1132 interface_check_state(iface
);
1134 error
= "DEVICE_CLAIM_FAILED";
1136 ret
= __interface_set_up(iface
);
1138 error
= "SETUP_FAILED";
1141 error
= "NO_DEVICE";
1144 interface_add_error(iface
, "interface", error
, NULL
, 0);
1148 interface_set_down(struct interface
*iface
)
1151 vlist_for_each_element(&interfaces
, iface
, node
)
1152 __interface_set_down(iface
, false);
1154 iface
->autostart
= false;
1155 __interface_set_down(iface
, false);
1160 interface_renew(struct interface
*iface
)
1162 if (iface
->state
== IFS_TEARDOWN
|| iface
->state
== IFS_DOWN
)
1165 return interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
1169 interface_start_pending(void)
1171 struct interface
*iface
;
1173 vlist_for_each_element(&interfaces
, iface
, node
) {
1174 if (iface
->autostart
)
1175 interface_set_up(iface
);
1180 interface_start_jail(const char *jail
, const pid_t netns_pid
)
1182 struct interface
*iface
;
1187 netns_fd
= system_netns_open(netns_pid
);
1191 vlist_for_each_element(&interfaces
, iface
, node
) {
1192 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1195 system_link_netns_move(iface
->main_dev
.dev
, netns_fd
, iface
->jail_ifname
);
1202 waitpid(pr
, &wstatus
, WUNTRACED
| WCONTINUED
);
1207 netns_fd
= system_netns_open(netns_pid
);
1211 system_netns_set(netns_fd
);
1213 vlist_for_each_element(&interfaces
, iface
, node
) {
1214 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1218 * The interface has already been renamed and is inside target
1219 * namespace, hence overwrite ifname with jail_ifname for
1220 * interface_set_up().
1221 * We are inside a fork which got it's own copy of the interfaces
1222 * list, so we can mess with it :)
1224 if (iface
->jail_ifname
)
1225 iface
->device
= iface
->jail_ifname
;
1227 interface_do_reload(iface
);
1228 interface_set_up(iface
);
1236 interface_stop_jail(const char *jail
, const pid_t netns_pid
)
1238 struct interface
*iface
;
1239 int netns_fd
, root_netns
;
1241 pid_t parent_pid
= getpid();
1243 const char *orig_ifname
;
1247 waitpid(pr
, &wstatus
, WUNTRACED
| WCONTINUED
);
1252 root_netns
= system_netns_open(parent_pid
);
1256 netns_fd
= system_netns_open(netns_pid
);
1260 system_netns_set(netns_fd
);
1262 vlist_for_each_element(&interfaces
, iface
, node
) {
1263 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1266 orig_ifname
= iface
->device
;
1267 if (iface
->jail_ifname
)
1268 iface
->device
= iface
->jail_ifname
;
1270 interface_do_reload(iface
);
1271 interface_set_down(iface
);
1272 system_link_netns_move(iface
->main_dev
.dev
, root_netns
, orig_ifname
);
1281 set_config_state(struct interface
*iface
, enum interface_config_state s
)
1283 __set_config_state(iface
, s
);
1284 if (iface
->state
== IFS_DOWN
)
1285 interface_handle_config_change(iface
);
1287 __interface_set_down(iface
, false);
1291 interface_update_start(struct interface
*iface
, const bool keep_old
)
1296 interface_ip_update_start(&iface
->proto_ip
);
1300 interface_update_complete(struct interface
*iface
)
1302 interface_ip_update_complete(&iface
->proto_ip
);
1306 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
1308 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
1309 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
1313 interface_device_config_changed(struct interface
*if_old
, struct interface
*if_new
)
1315 struct blob_attr
*ntb
[__DEV_ATTR_MAX
];
1316 struct blob_attr
*otb
[__DEV_ATTR_MAX
];
1317 struct device
*dev
= if_old
->main_dev
.dev
;
1318 unsigned long diff
= 0;
1320 BUILD_BUG_ON(sizeof(diff
) < __DEV_ATTR_MAX
/ 8);
1325 if (if_old
->device_config
!= if_new
->device_config
)
1328 if (!if_new
->device_config
)
1331 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb
,
1332 blob_data(if_old
->config
), blob_len(if_old
->config
));
1334 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, ntb
,
1335 blob_data(if_new
->config
), blob_len(if_new
->config
));
1337 uci_blob_diff(ntb
, otb
, &device_attr_list
, &diff
);
1342 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
1344 struct blob_attr
*old_config
= if_old
->config
;
1345 bool reload
= false, reload_ip
= false, update_prefix_delegation
= false;
1347 #define FIELD_CHANGED_STR(field) \
1348 ((!!if_old->field != !!if_new->field) || \
1350 strcmp(if_old->field, if_new->field) != 0))
1352 if (FIELD_CHANGED_STR(parent_ifname
)) {
1353 if (if_old
->parent_iface
.iface
)
1354 interface_remove_user(&if_old
->parent_iface
);
1358 if (!reload
&& interface_device_config_changed(if_old
, if_new
))
1361 if (FIELD_CHANGED_STR(device
) ||
1362 if_old
->proto_handler
!= if_new
->proto_handler
)
1365 if (!if_old
->proto_handler
->config_params
)
1366 D(INTERFACE
, "No config parameters for interface '%s'\n",
1368 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
1369 if_old
->proto_handler
->config_params
))
1372 #define UPDATE(field, __var) ({ \
1373 bool __changed = (if_old->field != if_new->field); \
1374 if_old->field = if_new->field; \
1375 __var |= __changed; \
1378 if_old
->config
= if_new
->config
;
1379 if (if_old
->config_autostart
!= if_new
->config_autostart
) {
1380 if (if_old
->config_autostart
)
1383 if_old
->autostart
= if_new
->config_autostart
;
1386 if_old
->device_config
= if_new
->device_config
;
1387 if_old
->config_autostart
= if_new
->config_autostart
;
1391 if_old
->jail
= if_new
->jail
;
1393 if_old
->autostart
= false;
1395 if (if_old
->jail_ifname
)
1396 free(if_old
->jail_ifname
);
1398 if_old
->jail_ifname
= if_new
->jail_ifname
;
1400 if_old
->device
= if_new
->device
;
1401 if_old
->parent_ifname
= if_new
->parent_ifname
;
1402 if_old
->dynamic
= if_new
->dynamic
;
1403 if_old
->proto_handler
= if_new
->proto_handler
;
1404 if_old
->force_link
= if_new
->force_link
;
1405 if_old
->dns_metric
= if_new
->dns_metric
;
1407 if (if_old
->proto_ip
.no_delegation
!= if_new
->proto_ip
.no_delegation
) {
1408 if_old
->proto_ip
.no_delegation
= if_new
->proto_ip
.no_delegation
;
1409 update_prefix_delegation
= true;
1412 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1413 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1415 UPDATE(metric
, reload_ip
);
1416 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1417 UPDATE(ip4table
, reload_ip
);
1418 UPDATE(ip6table
, reload_ip
);
1419 interface_merge_assignment_data(if_old
, if_new
);
1424 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1426 interface_clear_errors(if_old
);
1427 set_config_state(if_old
, IFC_RELOAD
);
1432 bool config_ip_enabled
= if_old
->config_ip
.enabled
;
1433 bool proto_ip_enabled
= if_old
->proto_ip
.enabled
;
1435 interface_ip_set_enabled(&if_old
->config_ip
, false);
1436 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1437 interface_ip_set_enabled(&if_old
->proto_ip
, proto_ip_enabled
);
1438 interface_ip_set_enabled(&if_old
->config_ip
, config_ip_enabled
);
1441 if (update_prefix_delegation
)
1442 interface_update_prefix_delegation(&if_old
->proto_ip
);
1444 interface_write_resolv_conf(if_old
->jail
);
1445 if (if_old
->main_dev
.dev
)
1446 interface_check_state(if_old
);
1449 if_new
->config
= NULL
;
1450 interface_cleanup(if_new
);
1456 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1457 struct vlist_node
*node_old
)
1459 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1460 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1462 if (node_old
&& node_new
) {
1463 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1464 interface_change_config(if_old
, if_new
);
1465 } else if (node_old
) {
1466 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1467 set_config_state(if_old
, IFC_REMOVE
);
1468 } else if (node_new
) {
1469 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1470 interface_event(if_new
, IFEV_CREATE
);
1471 proto_init_interface(if_new
, if_new
->config
);
1472 interface_claim_device(if_new
);
1473 netifd_ubus_add_interface(if_new
);
1479 interface_init_list(void)
1481 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1482 interfaces
.keep_old
= true;
1483 interfaces
.no_delete
= true;