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_JAIL_IFNAME
,
38 IFACE_ATTR_DEFAULTROUTE
,
41 IFACE_ATTR_DNS_SEARCH
,
42 IFACE_ATTR_DNS_METRIC
,
51 IFACE_ATTR_IP6IFACEID
,
52 IFACE_ATTR_FORCE_LINK
,
57 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
58 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
59 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
60 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
61 [IFACE_ATTR_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
62 [IFACE_ATTR_JAIL_IFNAME
] = { .name
= "jail_ifname", .type
= BLOBMSG_TYPE_STRING
},
63 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
64 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
65 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
66 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
67 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
68 [IFACE_ATTR_DNS_METRIC
] = { .name
= "dns_metric", .type
= BLOBMSG_TYPE_INT32
},
69 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
70 [IFACE_ATTR_IP6ASSIGN
] = { .name
= "ip6assign", .type
= BLOBMSG_TYPE_INT32
},
71 [IFACE_ATTR_IP6HINT
] = { .name
= "ip6hint", .type
= BLOBMSG_TYPE_STRING
},
72 [IFACE_ATTR_IP4TABLE
] = { .name
= "ip4table", .type
= BLOBMSG_TYPE_STRING
},
73 [IFACE_ATTR_IP6TABLE
] = { .name
= "ip6table", .type
= BLOBMSG_TYPE_STRING
},
74 [IFACE_ATTR_IP6CLASS
] = { .name
= "ip6class", .type
= BLOBMSG_TYPE_ARRAY
},
75 [IFACE_ATTR_DELEGATE
] = { .name
= "delegate", .type
= BLOBMSG_TYPE_BOOL
},
76 [IFACE_ATTR_IP6IFACEID
] = { .name
= "ip6ifaceid", .type
= BLOBMSG_TYPE_STRING
},
77 [IFACE_ATTR_FORCE_LINK
] = { .name
= "force_link", .type
= BLOBMSG_TYPE_BOOL
},
78 [IFACE_ATTR_IP6WEIGHT
] = { .name
= "ip6weight", .type
= BLOBMSG_TYPE_INT32
},
81 const struct uci_blob_param_list interface_attr_list
= {
82 .n_params
= IFACE_ATTR_MAX
,
83 .params
= iface_attrs
,
87 interface_set_main_dev(struct interface
*iface
, struct device
*dev
);
89 interface_event(struct interface
*iface
, enum interface_event ev
);
92 interface_error_flush(struct interface
*iface
)
94 struct interface_error
*error
, *tmp
;
96 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
97 list_del(&error
->list
);
103 interface_clear_errors(struct interface
*iface
)
105 /* don't flush the errors in case the configured protocol handler matches the
106 running protocol handler and is having the last error capability */
107 if (!(iface
->proto
&&
108 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
109 (iface
->proto
->handler
->name
== iface
->proto_handler
->name
)))
110 interface_error_flush(iface
);
113 void interface_add_error(struct interface
*iface
, const char *subsystem
,
114 const char *code
, const char **data
, int n_data
)
116 struct interface_error
*error
;
119 char *dest
, *d_subsys
, *d_code
;
121 /* if the configured protocol handler has the last error support capability,
122 errors should only be added if the running protocol handler matches the
125 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
126 (iface
->proto
->handler
->name
!= iface
->proto_handler
->name
))
130 len
= n_data
* sizeof(char *);
131 datalen
= alloca(len
);
132 for (i
= 0; i
< n_data
; i
++) {
133 datalen
[i
] = strlen(data
[i
]) + 1;
138 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
139 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
140 &d_code
, code
? strlen(code
) + 1 : 0);
144 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
145 protocol can't be established (e.g auth failure) */
146 if (iface
->proto_handler
->flags
& PROTO_FLAG_LASTERROR
)
147 interface_error_flush(iface
);
149 list_add_tail(&error
->list
, &iface
->errors
);
151 dest
= (char *) &error
->data
[n_data
+ 1];
152 for (i
= 0; i
< n_data
; i
++) {
153 error
->data
[i
] = dest
;
154 memcpy(dest
, data
[i
], datalen
[i
]);
157 error
->data
[n_data
] = NULL
;
160 error
->subsystem
= strcpy(d_subsys
, subsystem
);
163 error
->code
= strcpy(d_code
, code
);
167 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
169 avl_delete(&iface
->data
, &data
->node
);
174 interface_data_flush(struct interface
*iface
)
176 struct interface_data
*d
, *tmp
;
178 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
179 interface_data_del(iface
, d
);
183 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
185 struct interface_data
*n
, *o
;
187 if (!blobmsg_check_attr(data
, true))
188 return UBUS_STATUS_INVALID_ARGUMENT
;
190 const char *name
= blobmsg_name(data
);
191 unsigned len
= blob_pad_len(data
);
193 o
= avl_find_element(&iface
->data
, name
, o
, node
);
195 if (blob_pad_len(o
->data
) == len
&& !memcmp(o
->data
, data
, len
))
198 interface_data_del(iface
, o
);
201 n
= calloc(1, sizeof(*n
) + len
);
203 return UBUS_STATUS_UNKNOWN_ERROR
;
205 memcpy(n
->data
, data
, len
);
206 n
->node
.key
= blobmsg_name(n
->data
);
207 avl_insert(&iface
->data
, &n
->node
);
209 iface
->updated
|= IUF_DATA
;
213 int interface_parse_data(struct interface
*iface
, const struct blob_attr
*attr
)
215 struct blob_attr
*cur
;
220 blob_for_each_attr(cur
, attr
, rem
) {
221 ret
= interface_add_data(iface
, cur
);
226 if (iface
->updated
&& iface
->state
== IFS_UP
)
227 interface_event(iface
, IFEV_UPDATE
);
233 interface_event(struct interface
*iface
, enum interface_event ev
)
235 struct interface_user
*dep
, *tmp
;
236 struct device
*adev
= NULL
;
238 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
239 dep
->cb(dep
, iface
, ev
);
241 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
242 dep
->cb(dep
, iface
, ev
);
246 interface_error_flush(iface
);
247 adev
= iface
->l3_dev
.dev
;
251 alias_notify_device(iface
->name
, adev
);
259 interface_flush_state(struct interface
*iface
)
261 if (iface
->l3_dev
.dev
)
262 device_release(&iface
->l3_dev
);
263 interface_data_flush(iface
);
267 mark_interface_down(struct interface
*iface
)
269 enum interface_state state
= iface
->state
;
271 if (state
== IFS_DOWN
)
274 iface
->link_up_event
= false;
275 iface
->state
= IFS_DOWN
;
279 interface_event(iface
, IFEV_DOWN
);
282 interface_event(iface
, IFEV_UP_FAILED
);
287 interface_ip_set_enabled(&iface
->config_ip
, false);
288 interface_ip_set_enabled(&iface
->proto_ip
, false);
289 interface_ip_flush(&iface
->proto_ip
);
290 interface_flush_state(iface
);
291 system_flush_routes();
295 __set_config_state(struct interface
*iface
, enum interface_config_state s
)
297 iface
->config_state
= s
;
301 __interface_set_down(struct interface
*iface
, bool force
)
303 enum interface_state state
= iface
->state
;
307 iface
->state
= IFS_TEARDOWN
;
309 __set_config_state(iface
, IFC_REMOVE
);
312 interface_event(iface
, IFEV_DOWN
);
314 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
316 interface_flush_state(iface
);
320 if (iface
->main_dev
.dev
)
321 device_release(&iface
->main_dev
);
330 __interface_set_up(struct interface
*iface
)
334 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
336 iface
->state
= IFS_SETUP
;
337 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
339 mark_interface_down(iface
);
345 interface_check_state(struct interface
*iface
)
347 bool link_state
= iface
->link_state
|| iface
->force_link
;
349 switch (iface
->state
) {
352 if (!iface
->enabled
|| !link_state
) {
353 iface
->state
= IFS_TEARDOWN
;
355 __set_config_state(iface
, IFC_REMOVE
);
357 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
361 if (!iface
->available
)
364 if (iface
->autostart
&& iface
->enabled
&& link_state
&& !config_init
)
365 __interface_set_up(iface
);
373 interface_set_enabled(struct interface
*iface
, bool new_state
)
375 if (iface
->enabled
== new_state
)
378 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
379 iface
->enabled
= new_state
;
380 interface_check_state(iface
);
384 interface_set_link_state(struct interface
*iface
, bool new_state
)
386 if (iface
->link_state
== new_state
)
389 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
390 iface
->link_state
= new_state
;
391 interface_check_state(iface
);
393 if (new_state
&& iface
->force_link
&& iface
->state
== IFS_UP
&& !iface
->link_up_event
) {
394 interface_event(iface
, IFEV_LINK_UP
);
395 iface
->link_up_event
= true;
400 interface_ext_dev_cb(struct device_user
*dep
, enum device_event ev
)
402 if (ev
== DEV_EVENT_REMOVE
)
403 device_remove_user(dep
);
407 interface_main_dev_cb(struct device_user
*dep
, enum device_event ev
)
409 struct interface
*iface
;
411 iface
= container_of(dep
, struct interface
, main_dev
);
414 interface_set_available(iface
, true);
416 case DEV_EVENT_REMOVE
:
417 interface_set_available(iface
, false);
418 if (dep
->dev
&& dep
->dev
->external
)
419 interface_set_main_dev(iface
, NULL
);
422 interface_set_enabled(iface
, true);
425 interface_set_enabled(iface
, false);
427 case DEV_EVENT_LINK_UP
:
428 interface_set_link_state(iface
, true);
430 case DEV_EVENT_LINK_DOWN
:
431 interface_set_link_state(iface
, false);
433 case DEV_EVENT_TOPO_CHANGE
:
434 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
442 interface_l3_dev_cb(struct device_user
*dep
, enum device_event ev
)
444 struct interface
*iface
;
446 iface
= container_of(dep
, struct interface
, l3_dev
);
447 if (iface
->l3_dev
.dev
== iface
->main_dev
.dev
)
451 case DEV_EVENT_LINK_DOWN
:
452 if (iface
->proto_handler
->flags
& PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN
)
453 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
461 interface_set_available(struct interface
*iface
, bool new_state
)
463 if (iface
->available
== new_state
)
466 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
467 iface
->available
= new_state
;
470 if (iface
->autostart
&& !config_init
)
471 interface_set_up(iface
);
473 __interface_set_down(iface
, true);
477 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
480 list_add(&dep
->list
, &iface_all_users
);
485 list_add(&dep
->list
, &iface
->users
);
486 if (iface
->state
== IFS_UP
)
487 dep
->cb(dep
, iface
, IFEV_UP
);
491 interface_remove_user(struct interface_user
*dep
)
493 list_del_init(&dep
->list
);
498 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
500 struct blob_attr
*cur
;
503 blobmsg_for_each_attr(cur
, list
, rem
) {
504 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
507 if (!blobmsg_check_attr(cur
, false))
510 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
511 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
512 list_add(&c
->head
, &iface
->assignment_classes
);
517 interface_clear_assignment_classes(struct interface
*iface
)
519 while (!list_empty(&iface
->assignment_classes
)) {
520 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
521 struct interface_assignment_class
, head
);
528 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
530 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
531 old
->assignment_length
!= new->assignment_length
||
532 old
->assignment_iface_id_selection
!= new->assignment_iface_id_selection
||
533 old
->assignment_weight
!= new->assignment_weight
||
534 (old
->assignment_iface_id_selection
== IFID_FIXED
&&
535 memcmp(&old
->assignment_fixed_iface_id
, &new->assignment_fixed_iface_id
,
536 sizeof(old
->assignment_fixed_iface_id
))) ||
537 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
539 struct interface_assignment_class
*c
;
540 list_for_each_entry(c
, &new->assignment_classes
, head
) {
541 /* Compare list entries one-by-one to see if there was a change */
542 if (list_empty(&old
->assignment_classes
)) /* The new list is longer */
548 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
549 struct interface_assignment_class
, head
);
551 if (strcmp(c_old
->name
, c
->name
)) /* An entry didn't match */
554 list_del(&c_old
->head
);
558 /* The old list was longer than the new one or the last entry didn't match */
559 if (!list_empty(&old
->assignment_classes
)) {
560 interface_clear_assignment_classes(old
);
564 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
567 old
->assignment_hint
= new->assignment_hint
;
568 old
->assignment_length
= new->assignment_length
;
569 old
->assignment_iface_id_selection
= new->assignment_iface_id_selection
;
570 old
->assignment_fixed_iface_id
= new->assignment_fixed_iface_id
;
571 old
->assignment_weight
= new->assignment_weight
;
572 interface_refresh_assignments(true);
577 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
579 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
580 struct device
*dev
= iface
->l3_dev
.dev
;
587 interface_set_main_dev(alias
, dev
);
588 interface_set_available(alias
, true);
592 interface_set_available(alias
, false);
593 interface_set_main_dev(alias
, NULL
);
596 interface_remove_user(dep
);
604 interface_set_device_config(struct interface
*iface
, struct device
*dev
)
606 if (!dev
|| !dev
->default_config
)
609 if (!iface
->device_config
&&
610 (!dev
->iface_config
|| dev
->config_iface
!= iface
))
613 dev
->config_iface
= iface
;
614 dev
->iface_config
= iface
->device_config
;
615 device_apply_config(dev
, dev
->type
, iface
->config
);
619 interface_claim_device(struct interface
*iface
)
621 struct interface
*parent
;
622 struct device
*dev
= NULL
;
624 if (iface
->parent_iface
.iface
)
625 interface_remove_user(&iface
->parent_iface
);
629 if (iface
->parent_ifname
) {
630 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
631 iface
->parent_iface
.cb
= interface_alias_cb
;
632 interface_add_user(&iface
->parent_iface
, parent
);
633 } else if (iface
->ifname
&&
634 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
635 dev
= device_get(iface
->ifname
, true);
636 interface_set_device_config(iface
, dev
);
638 dev
= iface
->ext_dev
.dev
;
642 interface_set_main_dev(iface
, dev
);
646 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
647 interface_set_available(iface
, true);
651 interface_cleanup_state(struct interface
*iface
)
653 interface_set_available(iface
, false);
655 interface_flush_state(iface
);
656 interface_clear_errors(iface
);
657 interface_set_proto_state(iface
, NULL
);
659 interface_set_main_dev(iface
, NULL
);
660 interface_set_l3_dev(iface
, NULL
);
664 interface_cleanup(struct interface
*iface
)
666 struct interface_user
*dep
, *tmp
;
668 uloop_timeout_cancel(&iface
->remove_timer
);
669 device_remove_user(&iface
->ext_dev
);
671 if (iface
->parent_iface
.iface
)
672 interface_remove_user(&iface
->parent_iface
);
674 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
675 interface_remove_user(dep
);
677 interface_clear_assignment_classes(iface
);
678 interface_ip_flush(&iface
->config_ip
);
679 interface_cleanup_state(iface
);
683 interface_do_free(struct interface
*iface
)
685 interface_event(iface
, IFEV_FREE
);
686 interface_cleanup(iface
);
688 netifd_ubus_remove_interface(iface
);
689 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
692 if (iface
->jail_ifname
)
693 free(iface
->jail_ifname
);
699 interface_do_reload(struct interface
*iface
)
701 interface_event(iface
, IFEV_RELOAD
);
702 interface_cleanup_state(iface
);
703 proto_init_interface(iface
, iface
->config
);
704 interface_claim_device(iface
);
708 interface_handle_config_change(struct interface
*iface
)
710 enum interface_config_state state
= iface
->config_state
;
712 iface
->config_state
= IFC_NORMAL
;
717 interface_do_reload(iface
);
720 interface_do_free(iface
);
723 if (iface
->autostart
)
724 interface_set_up(iface
);
728 interface_proto_event_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
730 struct interface
*iface
= state
->iface
;
734 if (iface
->state
!= IFS_SETUP
) {
735 if (iface
->state
== IFS_UP
&& iface
->updated
)
736 interface_event(iface
, IFEV_UPDATE
);
740 if (!iface
->l3_dev
.dev
)
741 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
743 interface_ip_set_enabled(&iface
->config_ip
, true);
744 interface_ip_set_enabled(&iface
->proto_ip
, true);
745 system_flush_routes();
746 iface
->state
= IFS_UP
;
747 iface
->start_time
= system_get_rtime();
748 interface_event(iface
, IFEV_UP
);
749 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
752 if (iface
->state
== IFS_DOWN
)
755 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
756 mark_interface_down(iface
);
757 if (iface
->main_dev
.dev
)
758 device_release(&iface
->main_dev
);
759 if (iface
->l3_dev
.dev
)
760 device_remove_user(&iface
->l3_dev
);
761 interface_handle_config_change(iface
);
763 case IFPEV_LINK_LOST
:
764 if (iface
->state
!= IFS_UP
)
767 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
768 mark_interface_down(iface
);
769 iface
->state
= IFS_SETUP
;
775 interface_write_resolv_conf(iface
->jail
);
778 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
781 iface
->proto
->free(iface
->proto
);
784 iface
->state
= IFS_DOWN
;
785 iface
->proto
= state
;
789 state
->proto_event
= interface_proto_event_cb
;
790 state
->iface
= iface
;
794 interface_alloc(const char *name
, struct blob_attr
*config
, bool dynamic
)
796 struct interface
*iface
;
797 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
798 struct blob_attr
*cur
;
799 const char *proto_name
= NULL
;
801 bool force_link
= false;
803 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
804 iface
->name
= strcpy(iface_name
, name
);
805 INIT_LIST_HEAD(&iface
->errors
);
806 INIT_LIST_HEAD(&iface
->users
);
807 INIT_LIST_HEAD(&iface
->hotplug_list
);
808 INIT_LIST_HEAD(&iface
->assignment_classes
);
809 interface_ip_init(iface
);
810 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
811 iface
->config_ip
.enabled
= false;
813 iface
->main_dev
.cb
= interface_main_dev_cb
;
814 iface
->l3_dev
.cb
= interface_l3_dev_cb
;
815 iface
->ext_dev
.cb
= interface_ext_dev_cb
;
817 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
818 blob_data(config
), blob_len(config
));
820 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
821 proto_name
= blobmsg_data(cur
);
823 proto_attach_interface(iface
, proto_name
);
824 if (iface
->proto_handler
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
)
827 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
828 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], force_link
);
829 iface
->dynamic
= dynamic
;
830 iface
->proto_ip
.no_defaultroute
=
831 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
832 iface
->proto_ip
.no_dns
=
833 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
835 if ((cur
= tb
[IFACE_ATTR_DNS
]))
836 interface_add_dns_server_list(&iface
->config_ip
, cur
);
838 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
839 interface_add_dns_search_list(&iface
->config_ip
, cur
);
841 if ((cur
= tb
[IFACE_ATTR_DNS_METRIC
]))
842 iface
->dns_metric
= blobmsg_get_u32(cur
);
844 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
845 iface
->metric
= blobmsg_get_u32(cur
);
847 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
848 iface
->assignment_length
= blobmsg_get_u32(cur
);
851 iface
->assignment_iface_id_selection
= IFID_FIXED
;
852 iface
->assignment_fixed_iface_id
= in6addr_any
;
853 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
855 if ((cur
= tb
[IFACE_ATTR_IP6IFACEID
])) {
856 const char *ifaceid
= blobmsg_data(cur
);
857 if (!strcmp(ifaceid
, "random")) {
858 iface
->assignment_iface_id_selection
= IFID_RANDOM
;
860 else if (!strcmp(ifaceid
, "eui64")) {
861 iface
->assignment_iface_id_selection
= IFID_EUI64
;
864 /* we expect an IPv6 address with network id zero here -> fixed iface id
865 if we cannot parse -> revert to iface id 1 */
866 if (inet_pton(AF_INET6
,ifaceid
,&iface
->assignment_fixed_iface_id
) != 1 ||
867 iface
->assignment_fixed_iface_id
.s6_addr32
[0] != 0 ||
868 iface
->assignment_fixed_iface_id
.s6_addr32
[1] != 0) {
869 iface
->assignment_fixed_iface_id
= in6addr_any
;
870 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
871 netifd_log_message(L_WARNING
, "Failed to parse ip6ifaceid for interface '%s', \
872 falling back to iface id 1.\n", iface
->name
);
877 iface
->assignment_hint
= -1;
878 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
879 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
880 ~((1 << (64 - iface
->assignment_length
)) - 1);
882 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
883 interface_add_assignment_classes(iface
, cur
);
885 if ((cur
= tb
[IFACE_ATTR_IP6WEIGHT
]))
886 iface
->assignment_weight
= blobmsg_get_u32(cur
);
888 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
889 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
890 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
893 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
894 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
895 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
898 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
900 iface
->config_autostart
= iface
->autostart
;
903 if ((cur
= tb
[IFACE_ATTR_JAIL
])) {
904 iface
->jail
= strdup(blobmsg_get_string(cur
));
905 iface
->autostart
= false;
908 iface
->jail_ifname
= NULL
;
909 if ((cur
= tb
[IFACE_ATTR_JAIL_IFNAME
]))
910 iface
->jail_ifname
= strdup(blobmsg_get_string(cur
));
915 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
917 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
918 struct blob_attr
*cur
;
921 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
922 blob_data(config
), blob_len(config
));
925 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
926 iface
->parent_ifname
= blobmsg_data(cur
);
928 if (!iface
->parent_ifname
)
931 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
932 iface
->ifname
= blobmsg_data(cur
);
935 if (iface
->dynamic
) {
936 name
= strdup(iface
->name
);
942 iface
->config
= config
;
943 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
946 iface
= vlist_find(&interfaces
, name
, iface
, node
);
949 /* Don't delete dynamic interface on reload */
951 iface
->node
.version
= -1;
958 interface_add(struct interface
*iface
, struct blob_attr
*config
)
960 return __interface_add(iface
, config
, false);
964 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
966 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
969 return __interface_add(iface
, config
, true);
973 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
975 bool enabled
= iface
->config_ip
.enabled
;
976 bool claimed
= iface
->l3_dev
.claimed
;
978 if (iface
->l3_dev
.dev
== dev
)
981 interface_ip_set_enabled(&iface
->config_ip
, false);
982 interface_ip_set_enabled(&iface
->proto_ip
, false);
983 interface_ip_flush(&iface
->proto_ip
);
984 device_add_user(&iface
->l3_dev
, dev
);
988 if (device_claim(&iface
->l3_dev
) < 0)
991 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
992 interface_ip_set_enabled(&iface
->proto_ip
, enabled
);
997 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
999 bool claimed
= iface
->l3_dev
.claimed
;
1001 if (iface
->main_dev
.dev
== dev
)
1004 interface_set_available(iface
, false);
1005 device_add_user(&iface
->main_dev
, dev
);
1007 interface_set_link_state(iface
, false);
1012 if (device_claim(&iface
->l3_dev
) < 0)
1016 if (!iface
->l3_dev
.dev
)
1017 interface_set_l3_dev(iface
, dev
);
1021 interface_remove_link(struct interface
*iface
, struct device
*dev
)
1023 struct device
*mdev
= iface
->main_dev
.dev
;
1025 if (mdev
&& mdev
->hotplug_ops
)
1026 return mdev
->hotplug_ops
->del(mdev
, dev
);
1028 if (dev
== iface
->ext_dev
.dev
)
1029 device_remove_user(&iface
->ext_dev
);
1031 if (!iface
->main_dev
.hotplug
)
1032 return UBUS_STATUS_INVALID_ARGUMENT
;
1034 if (dev
!= iface
->main_dev
.dev
)
1035 return UBUS_STATUS_INVALID_ARGUMENT
;
1037 interface_set_main_dev(iface
, NULL
);
1042 interface_add_link(struct interface
*iface
, struct device
*dev
,
1043 struct blob_attr
*vlan
, bool link_ext
)
1045 struct device
*mdev
= iface
->main_dev
.dev
;
1050 if (iface
->main_dev
.hotplug
)
1051 device_remove_user(&iface
->main_dev
);
1054 if (mdev
->hotplug_ops
)
1055 return mdev
->hotplug_ops
->add(mdev
, dev
, vlan
);
1057 return UBUS_STATUS_NOT_SUPPORTED
;
1061 device_add_user(&iface
->ext_dev
, dev
);
1063 interface_set_main_dev(iface
, dev
);
1064 iface
->main_dev
.hotplug
= true;
1069 interface_handle_link(struct interface
*iface
, const char *name
,
1070 struct blob_attr
*vlan
, bool add
, bool link_ext
)
1077 dev
= device_get(name
, add
? (link_ext
? 2 : 1) : 0);
1079 ret
= UBUS_STATUS_NOT_FOUND
;
1084 interface_set_device_config(iface
, dev
);
1086 device_set_present(dev
, true);
1088 ret
= interface_add_link(iface
, dev
, vlan
, link_ext
);
1090 ret
= interface_remove_link(iface
, dev
);
1100 interface_set_up(struct interface
*iface
)
1103 const char *error
= NULL
;
1105 iface
->autostart
= true;
1107 if (iface
->state
!= IFS_DOWN
)
1110 interface_clear_errors(iface
);
1111 if (iface
->available
) {
1112 if (iface
->main_dev
.dev
) {
1113 ret
= device_claim(&iface
->main_dev
);
1115 interface_check_state(iface
);
1117 error
= "DEVICE_CLAIM_FAILED";
1119 ret
= __interface_set_up(iface
);
1121 error
= "SETUP_FAILED";
1124 error
= "NO_DEVICE";
1127 interface_add_error(iface
, "interface", error
, NULL
, 0);
1131 interface_set_down(struct interface
*iface
)
1134 vlist_for_each_element(&interfaces
, iface
, node
)
1135 __interface_set_down(iface
, false);
1137 iface
->autostart
= false;
1138 __interface_set_down(iface
, false);
1143 interface_renew(struct interface
*iface
)
1145 if (iface
->state
== IFS_TEARDOWN
|| iface
->state
== IFS_DOWN
)
1148 return interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
1152 interface_start_pending(void)
1154 struct interface
*iface
;
1156 vlist_for_each_element(&interfaces
, iface
, node
) {
1157 if (iface
->autostart
)
1158 interface_set_up(iface
);
1163 interface_start_jail(const char *jail
, const pid_t netns_pid
)
1165 struct interface
*iface
;
1170 netns_fd
= system_netns_open(netns_pid
);
1174 vlist_for_each_element(&interfaces
, iface
, node
) {
1175 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1178 system_link_netns_move(iface
->main_dev
.dev
, netns_fd
, iface
->jail_ifname
);
1185 waitpid(pr
, &wstatus
, WUNTRACED
| WCONTINUED
);
1190 netns_fd
= system_netns_open(netns_pid
);
1194 system_netns_set(netns_fd
);
1196 vlist_for_each_element(&interfaces
, iface
, node
) {
1197 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1201 * The interface has already been renamed and is inside target
1202 * namespace, hence overwrite ifname with jail_ifname for
1203 * interface_set_up().
1204 * We are inside a fork which got it's own copy of the interfaces
1205 * list, so we can mess with it :)
1207 if (iface
->jail_ifname
)
1208 iface
->ifname
= iface
->jail_ifname
;
1210 interface_do_reload(iface
);
1211 interface_set_up(iface
);
1219 interface_stop_jail(const char *jail
, const pid_t netns_pid
)
1221 struct interface
*iface
;
1222 int netns_fd
, root_netns
;
1224 pid_t parent_pid
= getpid();
1226 const char *orig_ifname
;
1230 waitpid(pr
, &wstatus
, WUNTRACED
| WCONTINUED
);
1235 root_netns
= system_netns_open(parent_pid
);
1239 netns_fd
= system_netns_open(netns_pid
);
1243 system_netns_set(netns_fd
);
1245 vlist_for_each_element(&interfaces
, iface
, node
) {
1246 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1249 orig_ifname
= iface
->ifname
;
1250 if (iface
->jail_ifname
)
1251 iface
->ifname
= iface
->jail_ifname
;
1253 interface_do_reload(iface
);
1254 interface_set_down(iface
);
1255 system_link_netns_move(iface
->main_dev
.dev
, root_netns
, orig_ifname
);
1264 set_config_state(struct interface
*iface
, enum interface_config_state s
)
1266 __set_config_state(iface
, s
);
1267 if (iface
->state
== IFS_DOWN
)
1268 interface_handle_config_change(iface
);
1270 __interface_set_down(iface
, false);
1274 interface_update_start(struct interface
*iface
, const bool keep_old
)
1279 interface_ip_update_start(&iface
->proto_ip
);
1283 interface_update_complete(struct interface
*iface
)
1285 interface_ip_update_complete(&iface
->proto_ip
);
1289 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
1291 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
1292 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
1296 interface_device_config_changed(struct interface
*if_old
, struct interface
*if_new
)
1298 struct blob_attr
*ntb
[__DEV_ATTR_MAX
];
1299 struct blob_attr
*otb
[__DEV_ATTR_MAX
];
1300 struct device
*dev
= if_old
->main_dev
.dev
;
1301 unsigned long diff
= 0;
1303 BUILD_BUG_ON(sizeof(diff
) < __DEV_ATTR_MAX
/ 8);
1308 if (if_old
->device_config
!= if_new
->device_config
)
1311 if (!if_new
->device_config
)
1314 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb
,
1315 blob_data(if_old
->config
), blob_len(if_old
->config
));
1317 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, ntb
,
1318 blob_data(if_new
->config
), blob_len(if_new
->config
));
1320 uci_blob_diff(ntb
, otb
, &device_attr_list
, &diff
);
1325 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
1327 struct blob_attr
*old_config
= if_old
->config
;
1328 bool reload
= false, reload_ip
= false, update_prefix_delegation
= false;
1330 #define FIELD_CHANGED_STR(field) \
1331 ((!!if_old->field != !!if_new->field) || \
1333 strcmp(if_old->field, if_new->field) != 0))
1335 if (FIELD_CHANGED_STR(parent_ifname
)) {
1336 if (if_old
->parent_iface
.iface
)
1337 interface_remove_user(&if_old
->parent_iface
);
1341 if (!reload
&& interface_device_config_changed(if_old
, if_new
))
1344 if (FIELD_CHANGED_STR(ifname
) ||
1345 if_old
->proto_handler
!= if_new
->proto_handler
)
1348 if (!if_old
->proto_handler
->config_params
)
1349 D(INTERFACE
, "No config parameters for interface '%s'\n",
1351 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
1352 if_old
->proto_handler
->config_params
))
1355 #define UPDATE(field, __var) ({ \
1356 bool __changed = (if_old->field != if_new->field); \
1357 if_old->field = if_new->field; \
1358 __var |= __changed; \
1361 if_old
->config
= if_new
->config
;
1362 if (if_old
->config_autostart
!= if_new
->config_autostart
) {
1363 if (if_old
->config_autostart
)
1366 if_old
->autostart
= if_new
->config_autostart
;
1369 if_old
->device_config
= if_new
->device_config
;
1370 if_old
->config_autostart
= if_new
->config_autostart
;
1374 if_old
->jail
= if_new
->jail
;
1376 if_old
->autostart
= false;
1378 if (if_old
->jail_ifname
)
1379 free(if_old
->jail_ifname
);
1381 if_old
->jail_ifname
= if_new
->jail_ifname
;
1383 if_old
->ifname
= if_new
->ifname
;
1384 if_old
->parent_ifname
= if_new
->parent_ifname
;
1385 if_old
->dynamic
= if_new
->dynamic
;
1386 if_old
->proto_handler
= if_new
->proto_handler
;
1387 if_old
->force_link
= if_new
->force_link
;
1388 if_old
->dns_metric
= if_new
->dns_metric
;
1390 if (if_old
->proto_ip
.no_delegation
!= if_new
->proto_ip
.no_delegation
) {
1391 if_old
->proto_ip
.no_delegation
= if_new
->proto_ip
.no_delegation
;
1392 update_prefix_delegation
= true;
1395 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1396 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1398 UPDATE(metric
, reload_ip
);
1399 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1400 UPDATE(ip4table
, reload_ip
);
1401 UPDATE(ip6table
, reload_ip
);
1402 interface_merge_assignment_data(if_old
, if_new
);
1407 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1409 interface_clear_errors(if_old
);
1410 set_config_state(if_old
, IFC_RELOAD
);
1415 bool config_ip_enabled
= if_old
->config_ip
.enabled
;
1416 bool proto_ip_enabled
= if_old
->proto_ip
.enabled
;
1418 interface_ip_set_enabled(&if_old
->config_ip
, false);
1419 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1420 interface_ip_set_enabled(&if_old
->proto_ip
, proto_ip_enabled
);
1421 interface_ip_set_enabled(&if_old
->config_ip
, config_ip_enabled
);
1424 if (update_prefix_delegation
)
1425 interface_update_prefix_delegation(&if_old
->proto_ip
);
1427 interface_write_resolv_conf(if_old
->jail
);
1428 if (if_old
->main_dev
.dev
)
1429 interface_check_state(if_old
);
1432 if_new
->config
= NULL
;
1433 interface_cleanup(if_new
);
1439 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1440 struct vlist_node
*node_old
)
1442 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1443 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1445 if (node_old
&& node_new
) {
1446 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1447 interface_change_config(if_old
, if_new
);
1448 } else if (node_old
) {
1449 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1450 set_config_state(if_old
, IFC_REMOVE
);
1451 } else if (node_new
) {
1452 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1453 interface_event(if_new
, IFEV_CREATE
);
1454 proto_init_interface(if_new
, if_new
->config
);
1455 interface_claim_device(if_new
);
1456 netifd_ubus_add_interface(if_new
);
1462 interface_init_list(void)
1464 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1465 interfaces
.keep_old
= true;
1466 interfaces
.no_delete
= true;