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
);
37 IFACE_ATTR_DEFAULTROUTE
,
40 IFACE_ATTR_DNS_SEARCH
,
41 IFACE_ATTR_DNS_METRIC
,
50 IFACE_ATTR_IP6IFACEID
,
51 IFACE_ATTR_FORCE_LINK
,
56 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
57 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
58 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
59 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
60 [IFACE_ATTR_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
61 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
62 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
63 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
64 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
65 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
66 [IFACE_ATTR_DNS_METRIC
] = { .name
= "dns_metric", .type
= BLOBMSG_TYPE_INT32
},
67 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
68 [IFACE_ATTR_IP6ASSIGN
] = { .name
= "ip6assign", .type
= BLOBMSG_TYPE_INT32
},
69 [IFACE_ATTR_IP6HINT
] = { .name
= "ip6hint", .type
= BLOBMSG_TYPE_STRING
},
70 [IFACE_ATTR_IP4TABLE
] = { .name
= "ip4table", .type
= BLOBMSG_TYPE_STRING
},
71 [IFACE_ATTR_IP6TABLE
] = { .name
= "ip6table", .type
= BLOBMSG_TYPE_STRING
},
72 [IFACE_ATTR_IP6CLASS
] = { .name
= "ip6class", .type
= BLOBMSG_TYPE_ARRAY
},
73 [IFACE_ATTR_DELEGATE
] = { .name
= "delegate", .type
= BLOBMSG_TYPE_BOOL
},
74 [IFACE_ATTR_IP6IFACEID
] = { .name
= "ip6ifaceid", .type
= BLOBMSG_TYPE_STRING
},
75 [IFACE_ATTR_FORCE_LINK
] = { .name
= "force_link", .type
= BLOBMSG_TYPE_BOOL
},
76 [IFACE_ATTR_IP6WEIGHT
] = { .name
= "ip6weight", .type
= BLOBMSG_TYPE_INT32
},
79 const struct uci_blob_param_list interface_attr_list
= {
80 .n_params
= IFACE_ATTR_MAX
,
81 .params
= iface_attrs
,
85 interface_set_main_dev(struct interface
*iface
, struct device
*dev
);
87 interface_event(struct interface
*iface
, enum interface_event ev
);
90 interface_error_flush(struct interface
*iface
)
92 struct interface_error
*error
, *tmp
;
94 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
95 list_del(&error
->list
);
101 interface_clear_errors(struct interface
*iface
)
103 /* don't flush the errors in case the configured protocol handler matches the
104 running protocol handler and is having the last error capability */
105 if (!(iface
->proto
&&
106 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
107 (iface
->proto
->handler
->name
== iface
->proto_handler
->name
)))
108 interface_error_flush(iface
);
111 void interface_add_error(struct interface
*iface
, const char *subsystem
,
112 const char *code
, const char **data
, int n_data
)
114 struct interface_error
*error
;
117 char *dest
, *d_subsys
, *d_code
;
119 /* if the configured protocol handler has the last error support capability,
120 errors should only be added if the running protocol handler matches the
123 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
124 (iface
->proto
->handler
->name
!= iface
->proto_handler
->name
))
128 len
= n_data
* sizeof(char *);
129 datalen
= alloca(len
);
130 for (i
= 0; i
< n_data
; i
++) {
131 datalen
[i
] = strlen(data
[i
]) + 1;
136 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
137 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
138 &d_code
, code
? strlen(code
) + 1 : 0);
142 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
143 protocol can't be established (e.g auth failure) */
144 if (iface
->proto_handler
->flags
& PROTO_FLAG_LASTERROR
)
145 interface_error_flush(iface
);
147 list_add_tail(&error
->list
, &iface
->errors
);
149 dest
= (char *) &error
->data
[n_data
+ 1];
150 for (i
= 0; i
< n_data
; i
++) {
151 error
->data
[i
] = dest
;
152 memcpy(dest
, data
[i
], datalen
[i
]);
155 error
->data
[n_data
] = NULL
;
158 error
->subsystem
= strcpy(d_subsys
, subsystem
);
161 error
->code
= strcpy(d_code
, code
);
165 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
167 avl_delete(&iface
->data
, &data
->node
);
172 interface_data_flush(struct interface
*iface
)
174 struct interface_data
*d
, *tmp
;
176 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
177 interface_data_del(iface
, d
);
181 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
183 struct interface_data
*n
, *o
;
185 if (!blobmsg_check_attr(data
, true))
186 return UBUS_STATUS_INVALID_ARGUMENT
;
188 const char *name
= blobmsg_name(data
);
189 unsigned len
= blob_pad_len(data
);
191 o
= avl_find_element(&iface
->data
, name
, o
, node
);
193 if (blob_pad_len(o
->data
) == len
&& !memcmp(o
->data
, data
, len
))
196 interface_data_del(iface
, o
);
199 n
= calloc(1, sizeof(*n
) + len
);
201 return UBUS_STATUS_UNKNOWN_ERROR
;
203 memcpy(n
->data
, data
, len
);
204 n
->node
.key
= blobmsg_name(n
->data
);
205 avl_insert(&iface
->data
, &n
->node
);
207 iface
->updated
|= IUF_DATA
;
211 int interface_parse_data(struct interface
*iface
, const struct blob_attr
*attr
)
213 struct blob_attr
*cur
;
218 blob_for_each_attr(cur
, attr
, rem
) {
219 ret
= interface_add_data(iface
, cur
);
224 if (iface
->updated
&& iface
->state
== IFS_UP
)
225 interface_event(iface
, IFEV_UPDATE
);
231 interface_event(struct interface
*iface
, enum interface_event ev
)
233 struct interface_user
*dep
, *tmp
;
234 struct device
*adev
= NULL
;
236 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
237 dep
->cb(dep
, iface
, ev
);
239 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
240 dep
->cb(dep
, iface
, ev
);
244 interface_error_flush(iface
);
245 adev
= iface
->l3_dev
.dev
;
249 alias_notify_device(iface
->name
, adev
);
257 interface_flush_state(struct interface
*iface
)
259 if (iface
->l3_dev
.dev
)
260 device_release(&iface
->l3_dev
);
261 interface_data_flush(iface
);
265 mark_interface_down(struct interface
*iface
)
267 enum interface_state state
= iface
->state
;
269 if (state
== IFS_DOWN
)
272 iface
->link_up_event
= false;
273 iface
->state
= IFS_DOWN
;
277 interface_event(iface
, IFEV_DOWN
);
280 interface_event(iface
, IFEV_UP_FAILED
);
285 interface_ip_set_enabled(&iface
->config_ip
, false);
286 interface_ip_set_enabled(&iface
->proto_ip
, false);
287 interface_ip_flush(&iface
->proto_ip
);
288 interface_flush_state(iface
);
289 system_flush_routes();
293 __set_config_state(struct interface
*iface
, enum interface_config_state s
)
295 iface
->config_state
= s
;
299 __interface_set_down(struct interface
*iface
, bool force
)
301 enum interface_state state
= iface
->state
;
305 iface
->state
= IFS_TEARDOWN
;
307 __set_config_state(iface
, IFC_REMOVE
);
310 interface_event(iface
, IFEV_DOWN
);
312 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
314 interface_flush_state(iface
);
318 if (iface
->main_dev
.dev
)
319 device_release(&iface
->main_dev
);
327 __interface_set_up(struct interface
*iface
)
331 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
333 iface
->state
= IFS_SETUP
;
334 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
336 mark_interface_down(iface
);
342 interface_check_state(struct interface
*iface
)
344 bool link_state
= iface
->link_state
|| iface
->force_link
;
346 switch (iface
->state
) {
349 if (!iface
->enabled
|| !link_state
) {
350 iface
->state
= IFS_TEARDOWN
;
352 __set_config_state(iface
, IFC_REMOVE
);
354 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
358 if (!iface
->available
)
361 if (iface
->autostart
&& iface
->enabled
&& link_state
&& !config_init
)
362 __interface_set_up(iface
);
370 interface_set_enabled(struct interface
*iface
, bool new_state
)
372 if (iface
->enabled
== new_state
)
375 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
376 iface
->enabled
= new_state
;
377 interface_check_state(iface
);
381 interface_set_link_state(struct interface
*iface
, bool new_state
)
383 if (iface
->link_state
== new_state
)
386 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
387 iface
->link_state
= new_state
;
388 interface_check_state(iface
);
390 if (new_state
&& iface
->force_link
&& iface
->state
== IFS_UP
&& !iface
->link_up_event
) {
391 interface_event(iface
, IFEV_LINK_UP
);
392 iface
->link_up_event
= true;
397 interface_ext_dev_cb(struct device_user
*dep
, enum device_event ev
)
399 if (ev
== DEV_EVENT_REMOVE
)
400 device_remove_user(dep
);
404 interface_main_dev_cb(struct device_user
*dep
, enum device_event ev
)
406 struct interface
*iface
;
408 iface
= container_of(dep
, struct interface
, main_dev
);
411 interface_set_available(iface
, true);
413 case DEV_EVENT_REMOVE
:
414 interface_set_available(iface
, false);
415 if (dep
->dev
&& dep
->dev
->external
)
416 interface_set_main_dev(iface
, NULL
);
419 interface_set_enabled(iface
, true);
422 interface_set_enabled(iface
, false);
424 case DEV_EVENT_LINK_UP
:
425 interface_set_link_state(iface
, true);
427 case DEV_EVENT_LINK_DOWN
:
428 interface_set_link_state(iface
, false);
430 case DEV_EVENT_TOPO_CHANGE
:
431 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
439 interface_l3_dev_cb(struct device_user
*dep
, enum device_event ev
)
441 struct interface
*iface
;
443 iface
= container_of(dep
, struct interface
, l3_dev
);
444 if (iface
->l3_dev
.dev
== iface
->main_dev
.dev
)
448 case DEV_EVENT_LINK_DOWN
:
449 if (iface
->proto_handler
->flags
& PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN
)
450 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
458 interface_set_available(struct interface
*iface
, bool new_state
)
460 if (iface
->available
== new_state
)
463 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
464 iface
->available
= new_state
;
467 if (iface
->autostart
&& !config_init
)
468 interface_set_up(iface
);
470 __interface_set_down(iface
, true);
474 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
477 list_add(&dep
->list
, &iface_all_users
);
482 list_add(&dep
->list
, &iface
->users
);
483 if (iface
->state
== IFS_UP
)
484 dep
->cb(dep
, iface
, IFEV_UP
);
488 interface_remove_user(struct interface_user
*dep
)
490 list_del_init(&dep
->list
);
495 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
497 struct blob_attr
*cur
;
500 blobmsg_for_each_attr(cur
, list
, rem
) {
501 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
504 if (!blobmsg_check_attr(cur
, false))
507 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
508 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
509 list_add(&c
->head
, &iface
->assignment_classes
);
514 interface_clear_assignment_classes(struct interface
*iface
)
516 while (!list_empty(&iface
->assignment_classes
)) {
517 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
518 struct interface_assignment_class
, head
);
525 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
527 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
528 old
->assignment_length
!= new->assignment_length
||
529 old
->assignment_iface_id_selection
!= new->assignment_iface_id_selection
||
530 old
->assignment_weight
!= new->assignment_weight
||
531 (old
->assignment_iface_id_selection
== IFID_FIXED
&&
532 memcmp(&old
->assignment_fixed_iface_id
, &new->assignment_fixed_iface_id
,
533 sizeof(old
->assignment_fixed_iface_id
))) ||
534 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
536 struct interface_assignment_class
*c
;
537 list_for_each_entry(c
, &new->assignment_classes
, head
) {
538 /* Compare list entries one-by-one to see if there was a change */
539 if (list_empty(&old
->assignment_classes
)) /* The new list is longer */
545 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
546 struct interface_assignment_class
, head
);
548 if (strcmp(c_old
->name
, c
->name
)) /* An entry didn't match */
551 list_del(&c_old
->head
);
555 /* The old list was longer than the new one or the last entry didn't match */
556 if (!list_empty(&old
->assignment_classes
)) {
557 interface_clear_assignment_classes(old
);
561 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
564 old
->assignment_hint
= new->assignment_hint
;
565 old
->assignment_length
= new->assignment_length
;
566 old
->assignment_iface_id_selection
= new->assignment_iface_id_selection
;
567 old
->assignment_fixed_iface_id
= new->assignment_fixed_iface_id
;
568 old
->assignment_weight
= new->assignment_weight
;
569 interface_refresh_assignments(true);
574 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
576 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
577 struct device
*dev
= iface
->l3_dev
.dev
;
584 interface_set_main_dev(alias
, dev
);
585 interface_set_available(alias
, true);
589 interface_set_available(alias
, false);
590 interface_set_main_dev(alias
, NULL
);
593 interface_remove_user(dep
);
601 interface_set_device_config(struct interface
*iface
, struct device
*dev
)
603 if (!dev
|| !dev
->default_config
)
606 if (!iface
->device_config
&&
607 (!dev
->iface_config
|| dev
->config_iface
!= iface
))
610 dev
->config_iface
= iface
;
611 dev
->iface_config
= iface
->device_config
;
612 device_apply_config(dev
, dev
->type
, iface
->config
);
616 interface_claim_device(struct interface
*iface
)
618 struct interface
*parent
;
619 struct device
*dev
= NULL
;
621 if (iface
->parent_iface
.iface
)
622 interface_remove_user(&iface
->parent_iface
);
626 if (iface
->parent_ifname
) {
627 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
628 iface
->parent_iface
.cb
= interface_alias_cb
;
629 interface_add_user(&iface
->parent_iface
, parent
);
630 } else if (iface
->ifname
&&
631 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
632 dev
= device_get(iface
->ifname
, true);
633 interface_set_device_config(iface
, dev
);
635 dev
= iface
->ext_dev
.dev
;
639 interface_set_main_dev(iface
, dev
);
643 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
644 interface_set_available(iface
, true);
648 interface_cleanup_state(struct interface
*iface
)
650 interface_set_available(iface
, false);
652 interface_flush_state(iface
);
653 interface_clear_errors(iface
);
654 interface_set_proto_state(iface
, NULL
);
656 interface_set_main_dev(iface
, NULL
);
657 interface_set_l3_dev(iface
, NULL
);
661 interface_cleanup(struct interface
*iface
)
663 struct interface_user
*dep
, *tmp
;
665 uloop_timeout_cancel(&iface
->remove_timer
);
666 device_remove_user(&iface
->ext_dev
);
668 if (iface
->parent_iface
.iface
)
669 interface_remove_user(&iface
->parent_iface
);
671 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
672 interface_remove_user(dep
);
674 interface_clear_assignment_classes(iface
);
675 interface_ip_flush(&iface
->config_ip
);
676 interface_cleanup_state(iface
);
680 interface_do_free(struct interface
*iface
)
682 interface_event(iface
, IFEV_FREE
);
683 interface_cleanup(iface
);
685 netifd_ubus_remove_interface(iface
);
686 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
694 interface_do_reload(struct interface
*iface
)
696 interface_event(iface
, IFEV_RELOAD
);
697 interface_cleanup_state(iface
);
698 proto_init_interface(iface
, iface
->config
);
699 interface_claim_device(iface
);
703 interface_handle_config_change(struct interface
*iface
)
705 enum interface_config_state state
= iface
->config_state
;
707 iface
->config_state
= IFC_NORMAL
;
712 interface_do_reload(iface
);
715 interface_do_free(iface
);
718 if (iface
->autostart
)
719 interface_set_up(iface
);
723 interface_proto_event_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
725 struct interface
*iface
= state
->iface
;
729 if (iface
->state
!= IFS_SETUP
) {
730 if (iface
->state
== IFS_UP
&& iface
->updated
)
731 interface_event(iface
, IFEV_UPDATE
);
735 if (!iface
->l3_dev
.dev
)
736 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
738 interface_ip_set_enabled(&iface
->config_ip
, true);
739 interface_ip_set_enabled(&iface
->proto_ip
, true);
740 system_flush_routes();
741 iface
->state
= IFS_UP
;
742 iface
->start_time
= system_get_rtime();
743 interface_event(iface
, IFEV_UP
);
744 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
747 if (iface
->state
== IFS_DOWN
)
750 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
751 mark_interface_down(iface
);
752 if (iface
->main_dev
.dev
)
753 device_release(&iface
->main_dev
);
754 if (iface
->l3_dev
.dev
)
755 device_remove_user(&iface
->l3_dev
);
756 interface_handle_config_change(iface
);
758 case IFPEV_LINK_LOST
:
759 if (iface
->state
!= IFS_UP
)
762 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
763 mark_interface_down(iface
);
764 iface
->state
= IFS_SETUP
;
770 interface_write_resolv_conf(iface
->jail
);
773 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
776 iface
->proto
->free(iface
->proto
);
779 iface
->state
= IFS_DOWN
;
780 iface
->proto
= state
;
784 state
->proto_event
= interface_proto_event_cb
;
785 state
->iface
= iface
;
789 interface_alloc(const char *name
, struct blob_attr
*config
, bool dynamic
)
791 struct interface
*iface
;
792 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
793 struct blob_attr
*cur
;
794 const char *proto_name
= NULL
;
796 bool force_link
= false;
798 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
799 iface
->name
= strcpy(iface_name
, name
);
800 INIT_LIST_HEAD(&iface
->errors
);
801 INIT_LIST_HEAD(&iface
->users
);
802 INIT_LIST_HEAD(&iface
->hotplug_list
);
803 INIT_LIST_HEAD(&iface
->assignment_classes
);
804 interface_ip_init(iface
);
805 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
806 iface
->config_ip
.enabled
= false;
808 iface
->main_dev
.cb
= interface_main_dev_cb
;
809 iface
->l3_dev
.cb
= interface_l3_dev_cb
;
810 iface
->ext_dev
.cb
= interface_ext_dev_cb
;
812 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
813 blob_data(config
), blob_len(config
));
815 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
816 proto_name
= blobmsg_data(cur
);
818 proto_attach_interface(iface
, proto_name
);
819 if (iface
->proto_handler
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
)
822 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
823 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], force_link
);
824 iface
->dynamic
= dynamic
;
825 iface
->proto_ip
.no_defaultroute
=
826 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
827 iface
->proto_ip
.no_dns
=
828 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
830 if ((cur
= tb
[IFACE_ATTR_DNS
]))
831 interface_add_dns_server_list(&iface
->config_ip
, cur
);
833 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
834 interface_add_dns_search_list(&iface
->config_ip
, cur
);
836 if ((cur
= tb
[IFACE_ATTR_DNS_METRIC
]))
837 iface
->dns_metric
= blobmsg_get_u32(cur
);
839 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
840 iface
->metric
= blobmsg_get_u32(cur
);
842 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
843 iface
->assignment_length
= blobmsg_get_u32(cur
);
846 iface
->assignment_iface_id_selection
= IFID_FIXED
;
847 iface
->assignment_fixed_iface_id
= in6addr_any
;
848 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
850 if ((cur
= tb
[IFACE_ATTR_IP6IFACEID
])) {
851 const char *ifaceid
= blobmsg_data(cur
);
852 if (!strcmp(ifaceid
, "random")) {
853 iface
->assignment_iface_id_selection
= IFID_RANDOM
;
855 else if (!strcmp(ifaceid
, "eui64")) {
856 iface
->assignment_iface_id_selection
= IFID_EUI64
;
859 /* we expect an IPv6 address with network id zero here -> fixed iface id
860 if we cannot parse -> revert to iface id 1 */
861 if (inet_pton(AF_INET6
,ifaceid
,&iface
->assignment_fixed_iface_id
) != 1 ||
862 iface
->assignment_fixed_iface_id
.s6_addr32
[0] != 0 ||
863 iface
->assignment_fixed_iface_id
.s6_addr32
[1] != 0) {
864 iface
->assignment_fixed_iface_id
= in6addr_any
;
865 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
866 netifd_log_message(L_WARNING
, "Failed to parse ip6ifaceid for interface '%s', \
867 falling back to iface id 1.\n", iface
->name
);
872 iface
->assignment_hint
= -1;
873 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
874 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
875 ~((1 << (64 - iface
->assignment_length
)) - 1);
877 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
878 interface_add_assignment_classes(iface
, cur
);
880 if ((cur
= tb
[IFACE_ATTR_IP6WEIGHT
]))
881 iface
->assignment_weight
= blobmsg_get_u32(cur
);
883 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
884 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
885 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
888 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
889 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
890 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
893 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
895 iface
->config_autostart
= iface
->autostart
;
898 if ((cur
= tb
[IFACE_ATTR_JAIL
])) {
899 iface
->jail
= strdup(blobmsg_get_string(cur
));
900 iface
->autostart
= false;
906 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
908 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
909 struct blob_attr
*cur
;
912 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
913 blob_data(config
), blob_len(config
));
916 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
917 iface
->parent_ifname
= blobmsg_data(cur
);
919 if (!iface
->parent_ifname
)
922 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
923 iface
->ifname
= blobmsg_data(cur
);
926 if (iface
->dynamic
) {
927 name
= strdup(iface
->name
);
933 iface
->config
= config
;
934 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
937 iface
= vlist_find(&interfaces
, name
, iface
, node
);
940 /* Don't delete dynamic interface on reload */
942 iface
->node
.version
= -1;
949 interface_add(struct interface
*iface
, struct blob_attr
*config
)
951 return __interface_add(iface
, config
, false);
955 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
957 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
960 return __interface_add(iface
, config
, true);
964 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
966 bool enabled
= iface
->config_ip
.enabled
;
967 bool claimed
= iface
->l3_dev
.claimed
;
969 if (iface
->l3_dev
.dev
== dev
)
972 interface_ip_set_enabled(&iface
->config_ip
, false);
973 interface_ip_set_enabled(&iface
->proto_ip
, false);
974 interface_ip_flush(&iface
->proto_ip
);
975 device_add_user(&iface
->l3_dev
, dev
);
979 if (device_claim(&iface
->l3_dev
) < 0)
982 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
983 interface_ip_set_enabled(&iface
->proto_ip
, enabled
);
988 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
990 bool claimed
= iface
->l3_dev
.claimed
;
992 if (iface
->main_dev
.dev
== dev
)
995 interface_set_available(iface
, false);
996 device_add_user(&iface
->main_dev
, dev
);
998 interface_set_link_state(iface
, false);
1003 if (device_claim(&iface
->l3_dev
) < 0)
1007 if (!iface
->l3_dev
.dev
)
1008 interface_set_l3_dev(iface
, dev
);
1012 interface_remove_link(struct interface
*iface
, struct device
*dev
)
1014 struct device
*mdev
= iface
->main_dev
.dev
;
1016 if (mdev
&& mdev
->hotplug_ops
)
1017 return mdev
->hotplug_ops
->del(mdev
, dev
);
1019 if (dev
== iface
->ext_dev
.dev
)
1020 device_remove_user(&iface
->ext_dev
);
1022 if (!iface
->main_dev
.hotplug
)
1023 return UBUS_STATUS_INVALID_ARGUMENT
;
1025 if (dev
!= iface
->main_dev
.dev
)
1026 return UBUS_STATUS_INVALID_ARGUMENT
;
1028 interface_set_main_dev(iface
, NULL
);
1033 interface_add_link(struct interface
*iface
, struct device
*dev
, bool link_ext
)
1035 struct device
*mdev
= iface
->main_dev
.dev
;
1040 if (iface
->main_dev
.hotplug
)
1041 device_remove_user(&iface
->main_dev
);
1044 if (mdev
->hotplug_ops
)
1045 return mdev
->hotplug_ops
->add(mdev
, dev
);
1047 return UBUS_STATUS_NOT_SUPPORTED
;
1051 device_add_user(&iface
->ext_dev
, dev
);
1053 interface_set_main_dev(iface
, dev
);
1054 iface
->main_dev
.hotplug
= true;
1059 interface_handle_link(struct interface
*iface
, const char *name
, bool add
, bool link_ext
)
1066 dev
= device_get(name
, add
? (link_ext
? 2 : 1) : 0);
1068 ret
= UBUS_STATUS_NOT_FOUND
;
1073 interface_set_device_config(iface
, dev
);
1074 device_set_present(dev
, true);
1076 ret
= interface_add_link(iface
, dev
, link_ext
);
1078 ret
= interface_remove_link(iface
, dev
);
1088 interface_set_up(struct interface
*iface
)
1091 const char *error
= NULL
;
1093 iface
->autostart
= true;
1095 if (iface
->state
!= IFS_DOWN
)
1098 interface_clear_errors(iface
);
1099 if (iface
->available
) {
1100 if (iface
->main_dev
.dev
) {
1101 ret
= device_claim(&iface
->main_dev
);
1103 interface_check_state(iface
);
1105 error
= "DEVICE_CLAIM_FAILED";
1107 ret
= __interface_set_up(iface
);
1109 error
= "SETUP_FAILED";
1112 error
= "NO_DEVICE";
1115 interface_add_error(iface
, "interface", error
, NULL
, 0);
1119 interface_set_down(struct interface
*iface
)
1122 vlist_for_each_element(&interfaces
, iface
, node
)
1123 __interface_set_down(iface
, false);
1125 iface
->autostart
= false;
1126 __interface_set_down(iface
, false);
1131 interface_renew(struct interface
*iface
)
1133 if (iface
->state
== IFS_TEARDOWN
|| iface
->state
== IFS_DOWN
)
1136 return interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
1140 interface_start_pending(void)
1142 struct interface
*iface
;
1144 vlist_for_each_element(&interfaces
, iface
, node
) {
1145 if (iface
->autostart
)
1146 interface_set_up(iface
);
1151 interface_start_jail(const char *jail
, const pid_t netns_pid
)
1153 struct interface
*iface
;
1158 netns_fd
= system_netns_open(netns_pid
);
1162 vlist_for_each_element(&interfaces
, iface
, node
) {
1163 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1166 system_link_netns_move(iface
->ifname
, netns_fd
);
1171 waitpid(pr
, &wstatus
, WUNTRACED
| WCONTINUED
);
1176 system_netns_set(netns_fd
);
1178 vlist_for_each_element(&interfaces
, iface
, node
) {
1179 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1182 interface_set_up(iface
);
1188 interface_stop_jail(const char *jail
, const pid_t netns_pid
)
1190 struct interface
*iface
;
1191 int netns_fd
, root_netns
;
1195 netns_fd
= system_netns_open(netns_pid
);
1199 root_netns
= system_netns_open(getpid());
1205 waitpid(pr
, &wstatus
, WUNTRACED
| WCONTINUED
);
1211 system_netns_set(netns_fd
);
1213 vlist_for_each_element(&interfaces
, iface
, node
) {
1214 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1217 interface_set_down(iface
);
1218 system_link_netns_move(iface
->ifname
, root_netns
);
1224 set_config_state(struct interface
*iface
, enum interface_config_state s
)
1226 __set_config_state(iface
, s
);
1227 if (iface
->state
== IFS_DOWN
)
1228 interface_handle_config_change(iface
);
1230 __interface_set_down(iface
, false);
1234 interface_update_start(struct interface
*iface
, const bool keep_old
)
1239 interface_ip_update_start(&iface
->proto_ip
);
1243 interface_update_complete(struct interface
*iface
)
1245 interface_ip_update_complete(&iface
->proto_ip
);
1249 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
1251 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
1252 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
1256 interface_device_config_changed(struct interface
*if_old
, struct interface
*if_new
)
1258 struct blob_attr
*ntb
[__DEV_ATTR_MAX
];
1259 struct blob_attr
*otb
[__DEV_ATTR_MAX
];
1260 struct device
*dev
= if_old
->main_dev
.dev
;
1261 unsigned long diff
= 0;
1263 BUILD_BUG_ON(sizeof(diff
) < __DEV_ATTR_MAX
/ 8);
1268 if (if_old
->device_config
!= if_new
->device_config
)
1271 if (!if_new
->device_config
)
1274 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb
,
1275 blob_data(if_old
->config
), blob_len(if_old
->config
));
1277 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, ntb
,
1278 blob_data(if_new
->config
), blob_len(if_new
->config
));
1280 uci_blob_diff(ntb
, otb
, &device_attr_list
, &diff
);
1285 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
1287 struct blob_attr
*old_config
= if_old
->config
;
1288 bool reload
= false, reload_ip
= false, update_prefix_delegation
= false;
1290 #define FIELD_CHANGED_STR(field) \
1291 ((!!if_old->field != !!if_new->field) || \
1293 strcmp(if_old->field, if_new->field) != 0))
1295 if (FIELD_CHANGED_STR(parent_ifname
)) {
1296 if (if_old
->parent_iface
.iface
)
1297 interface_remove_user(&if_old
->parent_iface
);
1301 if (!reload
&& interface_device_config_changed(if_old
, if_new
))
1304 if (FIELD_CHANGED_STR(ifname
) ||
1305 if_old
->proto_handler
!= if_new
->proto_handler
)
1308 if (!if_old
->proto_handler
->config_params
)
1309 D(INTERFACE
, "No config parameters for interface '%s'\n",
1311 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
1312 if_old
->proto_handler
->config_params
))
1315 #define UPDATE(field, __var) ({ \
1316 bool __changed = (if_old->field != if_new->field); \
1317 if_old->field = if_new->field; \
1318 __var |= __changed; \
1321 if_old
->config
= if_new
->config
;
1322 if (if_old
->config_autostart
!= if_new
->config_autostart
) {
1323 if (if_old
->config_autostart
)
1326 if_old
->autostart
= if_new
->config_autostart
;
1329 if_old
->device_config
= if_new
->device_config
;
1330 if_old
->config_autostart
= if_new
->config_autostart
;
1334 if_old
->jail
= if_new
->jail
;
1336 if_old
->autostart
= false;
1338 if_old
->ifname
= if_new
->ifname
;
1339 if_old
->parent_ifname
= if_new
->parent_ifname
;
1340 if_old
->dynamic
= if_new
->dynamic
;
1341 if_old
->proto_handler
= if_new
->proto_handler
;
1342 if_old
->force_link
= if_new
->force_link
;
1343 if_old
->dns_metric
= if_new
->dns_metric
;
1345 if (if_old
->proto_ip
.no_delegation
!= if_new
->proto_ip
.no_delegation
) {
1346 if_old
->proto_ip
.no_delegation
= if_new
->proto_ip
.no_delegation
;
1347 update_prefix_delegation
= true;
1350 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1351 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1353 UPDATE(metric
, reload_ip
);
1354 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1355 UPDATE(ip4table
, reload_ip
);
1356 UPDATE(ip6table
, reload_ip
);
1357 interface_merge_assignment_data(if_old
, if_new
);
1362 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1364 interface_clear_errors(if_old
);
1365 set_config_state(if_old
, IFC_RELOAD
);
1370 bool config_ip_enabled
= if_old
->config_ip
.enabled
;
1371 bool proto_ip_enabled
= if_old
->proto_ip
.enabled
;
1373 interface_ip_set_enabled(&if_old
->config_ip
, false);
1374 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1375 interface_ip_set_enabled(&if_old
->proto_ip
, proto_ip_enabled
);
1376 interface_ip_set_enabled(&if_old
->config_ip
, config_ip_enabled
);
1379 if (update_prefix_delegation
)
1380 interface_update_prefix_delegation(&if_old
->proto_ip
);
1382 interface_write_resolv_conf(if_old
->jail
);
1383 if (if_old
->main_dev
.dev
)
1384 interface_check_state(if_old
);
1387 if_new
->config
= NULL
;
1388 interface_cleanup(if_new
);
1394 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1395 struct vlist_node
*node_old
)
1397 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1398 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1400 if (node_old
&& node_new
) {
1401 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1402 interface_change_config(if_old
, if_new
);
1403 } else if (node_old
) {
1404 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1405 set_config_state(if_old
, IFC_REMOVE
);
1406 } else if (node_new
) {
1407 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1408 interface_event(if_new
, IFEV_CREATE
);
1409 proto_init_interface(if_new
, if_new
->config
);
1410 interface_claim_device(if_new
);
1411 netifd_ubus_add_interface(if_new
);
1417 interface_init_list(void)
1419 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1420 interfaces
.keep_old
= true;
1421 interfaces
.no_delete
= true;