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_force_link(struct interface
*iface
)
105 struct device
*dev
= iface
->main_dev
.dev
;
107 if (dev
&& dev
->settings
.auth
)
110 return iface
->force_link
;
114 interface_clear_errors(struct interface
*iface
)
116 /* don't flush the errors in case the configured protocol handler matches the
117 running protocol handler and is having the last error capability */
118 if (!(iface
->proto
&&
119 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
120 (iface
->proto
->handler
->name
== iface
->proto_handler
->name
)))
121 interface_error_flush(iface
);
124 void interface_add_error(struct interface
*iface
, const char *subsystem
,
125 const char *code
, const char **data
, int n_data
)
127 struct interface_error
*error
;
130 char *dest
, *d_subsys
, *d_code
;
132 /* if the configured protocol handler has the last error support capability,
133 errors should only be added if the running protocol handler matches the
136 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
137 (iface
->proto
->handler
->name
!= iface
->proto_handler
->name
))
141 len
= n_data
* sizeof(char *);
142 datalen
= alloca(len
);
143 for (i
= 0; i
< n_data
; i
++) {
144 datalen
[i
] = strlen(data
[i
]) + 1;
149 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
150 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
151 &d_code
, code
? strlen(code
) + 1 : 0);
155 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
156 protocol can't be established (e.g auth failure) */
157 if (iface
->proto_handler
->flags
& PROTO_FLAG_LASTERROR
)
158 interface_error_flush(iface
);
160 list_add_tail(&error
->list
, &iface
->errors
);
162 dest
= (char *) &error
->data
[n_data
+ 1];
163 for (i
= 0; i
< n_data
; i
++) {
164 error
->data
[i
] = dest
;
165 memcpy(dest
, data
[i
], datalen
[i
]);
168 error
->data
[n_data
] = NULL
;
171 error
->subsystem
= strcpy(d_subsys
, subsystem
);
174 error
->code
= strcpy(d_code
, code
);
178 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
180 avl_delete(&iface
->data
, &data
->node
);
185 interface_data_flush(struct interface
*iface
)
187 struct interface_data
*d
, *tmp
;
189 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
190 interface_data_del(iface
, d
);
194 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
196 struct interface_data
*n
, *o
;
198 if (!blobmsg_check_attr(data
, true))
199 return UBUS_STATUS_INVALID_ARGUMENT
;
201 const char *name
= blobmsg_name(data
);
202 unsigned len
= blob_pad_len(data
);
204 o
= avl_find_element(&iface
->data
, name
, o
, node
);
206 if (blob_pad_len(o
->data
) == len
&& !memcmp(o
->data
, data
, len
))
209 interface_data_del(iface
, o
);
212 n
= calloc(1, sizeof(*n
) + len
);
214 return UBUS_STATUS_UNKNOWN_ERROR
;
216 memcpy(n
->data
, data
, len
);
217 n
->node
.key
= blobmsg_name(n
->data
);
218 avl_insert(&iface
->data
, &n
->node
);
220 iface
->updated
|= IUF_DATA
;
224 int interface_parse_data(struct interface
*iface
, const struct blob_attr
*attr
)
226 struct blob_attr
*cur
;
231 blob_for_each_attr(cur
, attr
, rem
) {
232 ret
= interface_add_data(iface
, cur
);
237 if (iface
->updated
&& iface
->state
== IFS_UP
)
238 interface_event(iface
, IFEV_UPDATE
);
244 interface_event(struct interface
*iface
, enum interface_event ev
)
246 struct interface_user
*dep
, *tmp
;
247 struct device
*adev
= NULL
;
249 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
250 dep
->cb(dep
, iface
, ev
);
252 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
253 dep
->cb(dep
, iface
, ev
);
257 interface_error_flush(iface
);
258 adev
= iface
->l3_dev
.dev
;
262 alias_notify_device(iface
->name
, adev
);
270 interface_flush_state(struct interface
*iface
)
272 if (iface
->l3_dev
.dev
)
273 device_release(&iface
->l3_dev
);
274 interface_data_flush(iface
);
278 mark_interface_down(struct interface
*iface
)
280 enum interface_state state
= iface
->state
;
282 if (state
== IFS_DOWN
)
285 iface
->link_up_event
= false;
286 iface
->state
= IFS_DOWN
;
290 interface_event(iface
, IFEV_DOWN
);
293 interface_event(iface
, IFEV_UP_FAILED
);
298 interface_ip_set_enabled(&iface
->config_ip
, false);
299 interface_ip_set_enabled(&iface
->proto_ip
, false);
300 interface_ip_flush(&iface
->proto_ip
);
301 interface_flush_state(iface
);
302 system_flush_routes();
306 __set_config_state(struct interface
*iface
, enum interface_config_state s
)
308 iface
->config_state
= s
;
312 __interface_set_down(struct interface
*iface
, bool force
)
314 enum interface_state state
= iface
->state
;
318 iface
->state
= IFS_TEARDOWN
;
320 __set_config_state(iface
, IFC_REMOVE
);
323 interface_event(iface
, IFEV_DOWN
);
325 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
327 interface_flush_state(iface
);
331 if (iface
->main_dev
.dev
)
332 device_release(&iface
->main_dev
);
341 __interface_set_up(struct interface
*iface
)
345 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
347 iface
->state
= IFS_SETUP
;
348 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
350 mark_interface_down(iface
);
356 interface_check_state(struct interface
*iface
)
358 bool link_state
= iface
->link_state
|| interface_force_link(iface
);
360 switch (iface
->state
) {
363 if (!iface
->enabled
|| !link_state
) {
364 iface
->state
= IFS_TEARDOWN
;
366 __set_config_state(iface
, IFC_REMOVE
);
368 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
372 if (!iface
->available
)
375 if (iface
->autostart
&& iface
->enabled
&& link_state
&& !config_init
)
376 __interface_set_up(iface
);
384 interface_set_enabled(struct interface
*iface
, bool new_state
)
386 if (iface
->enabled
== new_state
)
389 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
390 iface
->enabled
= new_state
;
391 interface_check_state(iface
);
395 interface_set_link_state(struct interface
*iface
, bool new_state
)
397 if (iface
->link_state
== new_state
)
400 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
401 iface
->link_state
= new_state
;
402 interface_check_state(iface
);
404 if (new_state
&& interface_force_link(iface
) &&
405 iface
->state
== IFS_UP
&& !iface
->link_up_event
) {
406 interface_event(iface
, IFEV_LINK_UP
);
407 iface
->link_up_event
= true;
412 interface_ext_dev_cb(struct device_user
*dep
, enum device_event ev
)
414 if (ev
== DEV_EVENT_REMOVE
)
415 device_remove_user(dep
);
419 interface_main_dev_cb(struct device_user
*dep
, enum device_event ev
)
421 struct interface
*iface
;
423 iface
= container_of(dep
, struct interface
, main_dev
);
426 interface_set_available(iface
, true);
428 case DEV_EVENT_REMOVE
:
429 interface_set_available(iface
, false);
430 if (dep
->dev
&& dep
->dev
->external
)
431 interface_set_main_dev(iface
, NULL
);
434 interface_set_enabled(iface
, true);
437 interface_set_enabled(iface
, false);
439 case DEV_EVENT_AUTH_UP
:
440 case DEV_EVENT_LINK_UP
:
441 case DEV_EVENT_LINK_DOWN
:
442 interface_set_link_state(iface
, device_link_active(dep
->dev
));
444 case DEV_EVENT_TOPO_CHANGE
:
445 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
453 interface_l3_dev_cb(struct device_user
*dep
, enum device_event ev
)
455 struct interface
*iface
;
457 iface
= container_of(dep
, struct interface
, l3_dev
);
458 if (iface
->l3_dev
.dev
== iface
->main_dev
.dev
)
462 case DEV_EVENT_LINK_DOWN
:
463 if (iface
->proto_handler
->flags
& PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN
)
464 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
472 interface_set_available(struct interface
*iface
, bool new_state
)
474 if (iface
->available
== new_state
)
477 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
478 iface
->available
= new_state
;
481 if (iface
->autostart
&& !config_init
)
482 interface_set_up(iface
);
484 __interface_set_down(iface
, true);
488 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
491 list_add(&dep
->list
, &iface_all_users
);
496 list_add(&dep
->list
, &iface
->users
);
497 if (iface
->state
== IFS_UP
)
498 dep
->cb(dep
, iface
, IFEV_UP
);
502 interface_remove_user(struct interface_user
*dep
)
504 list_del_init(&dep
->list
);
509 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
511 struct blob_attr
*cur
;
514 blobmsg_for_each_attr(cur
, list
, rem
) {
515 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
518 if (!blobmsg_check_attr(cur
, false))
521 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
522 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
523 list_add(&c
->head
, &iface
->assignment_classes
);
528 interface_clear_assignment_classes(struct interface
*iface
)
530 while (!list_empty(&iface
->assignment_classes
)) {
531 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
532 struct interface_assignment_class
, head
);
539 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
541 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
542 old
->assignment_length
!= new->assignment_length
||
543 old
->assignment_iface_id_selection
!= new->assignment_iface_id_selection
||
544 old
->assignment_weight
!= new->assignment_weight
||
545 (old
->assignment_iface_id_selection
== IFID_FIXED
&&
546 memcmp(&old
->assignment_fixed_iface_id
, &new->assignment_fixed_iface_id
,
547 sizeof(old
->assignment_fixed_iface_id
))) ||
548 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
550 struct interface_assignment_class
*c
;
551 list_for_each_entry(c
, &new->assignment_classes
, head
) {
552 /* Compare list entries one-by-one to see if there was a change */
553 if (list_empty(&old
->assignment_classes
)) /* The new list is longer */
559 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
560 struct interface_assignment_class
, head
);
562 if (strcmp(c_old
->name
, c
->name
)) /* An entry didn't match */
565 list_del(&c_old
->head
);
569 /* The old list was longer than the new one or the last entry didn't match */
570 if (!list_empty(&old
->assignment_classes
)) {
571 interface_clear_assignment_classes(old
);
575 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
578 old
->assignment_hint
= new->assignment_hint
;
579 old
->assignment_length
= new->assignment_length
;
580 old
->assignment_iface_id_selection
= new->assignment_iface_id_selection
;
581 old
->assignment_fixed_iface_id
= new->assignment_fixed_iface_id
;
582 old
->assignment_weight
= new->assignment_weight
;
583 interface_refresh_assignments(true);
588 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
590 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
591 struct device
*dev
= iface
->l3_dev
.dev
;
598 interface_set_main_dev(alias
, dev
);
599 interface_set_available(alias
, true);
603 interface_set_available(alias
, false);
604 interface_set_main_dev(alias
, NULL
);
607 interface_remove_user(dep
);
615 interface_set_device_config(struct interface
*iface
, struct device
*dev
)
617 if (!dev
|| !dev
->default_config
)
620 if (!iface
->device_config
&&
621 (!dev
->iface_config
|| dev
->config_iface
!= iface
))
624 dev
->config_iface
= iface
;
625 dev
->iface_config
= iface
->device_config
;
626 device_apply_config(dev
, dev
->type
, iface
->config
);
630 interface_claim_device(struct interface
*iface
)
632 struct interface
*parent
;
633 struct device
*dev
= NULL
;
635 if (iface
->parent_iface
.iface
)
636 interface_remove_user(&iface
->parent_iface
);
640 if (iface
->parent_ifname
) {
641 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
642 iface
->parent_iface
.cb
= interface_alias_cb
;
643 interface_add_user(&iface
->parent_iface
, parent
);
644 } else if (iface
->ifname
&&
645 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
646 dev
= device_get(iface
->ifname
, true);
647 interface_set_device_config(iface
, dev
);
649 dev
= iface
->ext_dev
.dev
;
653 interface_set_main_dev(iface
, dev
);
657 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
658 interface_set_available(iface
, true);
662 interface_cleanup_state(struct interface
*iface
)
664 interface_set_available(iface
, false);
666 interface_flush_state(iface
);
667 interface_clear_errors(iface
);
668 interface_set_proto_state(iface
, NULL
);
670 interface_set_main_dev(iface
, NULL
);
671 interface_set_l3_dev(iface
, NULL
);
675 interface_cleanup(struct interface
*iface
)
677 struct interface_user
*dep
, *tmp
;
679 uloop_timeout_cancel(&iface
->remove_timer
);
680 device_remove_user(&iface
->ext_dev
);
682 if (iface
->parent_iface
.iface
)
683 interface_remove_user(&iface
->parent_iface
);
685 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
686 interface_remove_user(dep
);
688 interface_clear_assignment_classes(iface
);
689 interface_ip_flush(&iface
->config_ip
);
690 interface_cleanup_state(iface
);
694 interface_do_free(struct interface
*iface
)
696 interface_event(iface
, IFEV_FREE
);
697 interface_cleanup(iface
);
699 netifd_ubus_remove_interface(iface
);
700 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
703 if (iface
->jail_ifname
)
704 free(iface
->jail_ifname
);
710 interface_do_reload(struct interface
*iface
)
712 interface_event(iface
, IFEV_RELOAD
);
713 interface_cleanup_state(iface
);
714 proto_init_interface(iface
, iface
->config
);
715 interface_claim_device(iface
);
719 interface_handle_config_change(struct interface
*iface
)
721 enum interface_config_state state
= iface
->config_state
;
723 iface
->config_state
= IFC_NORMAL
;
728 interface_do_reload(iface
);
731 interface_do_free(iface
);
734 if (iface
->autostart
)
735 interface_set_up(iface
);
739 interface_proto_event_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
741 struct interface
*iface
= state
->iface
;
745 if (iface
->state
!= IFS_SETUP
) {
746 if (iface
->state
== IFS_UP
&& iface
->updated
)
747 interface_event(iface
, IFEV_UPDATE
);
751 if (!iface
->l3_dev
.dev
)
752 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
754 interface_ip_set_enabled(&iface
->config_ip
, true);
755 interface_ip_set_enabled(&iface
->proto_ip
, true);
756 system_flush_routes();
757 iface
->state
= IFS_UP
;
758 iface
->start_time
= system_get_rtime();
759 interface_event(iface
, IFEV_UP
);
760 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
763 if (iface
->state
== IFS_DOWN
)
766 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
767 mark_interface_down(iface
);
768 if (iface
->main_dev
.dev
)
769 device_release(&iface
->main_dev
);
770 if (iface
->l3_dev
.dev
)
771 device_remove_user(&iface
->l3_dev
);
772 interface_handle_config_change(iface
);
774 case IFPEV_LINK_LOST
:
775 if (iface
->state
!= IFS_UP
)
778 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
779 mark_interface_down(iface
);
780 iface
->state
= IFS_SETUP
;
786 interface_write_resolv_conf(iface
->jail
);
789 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
792 iface
->proto
->free(iface
->proto
);
795 iface
->state
= IFS_DOWN
;
796 iface
->proto
= state
;
800 state
->proto_event
= interface_proto_event_cb
;
801 state
->iface
= iface
;
805 interface_alloc(const char *name
, struct blob_attr
*config
, bool dynamic
)
807 struct interface
*iface
;
808 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
809 struct blob_attr
*cur
;
810 const char *proto_name
= NULL
;
812 bool force_link
= false;
814 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
815 iface
->name
= strcpy(iface_name
, name
);
816 INIT_LIST_HEAD(&iface
->errors
);
817 INIT_LIST_HEAD(&iface
->users
);
818 INIT_LIST_HEAD(&iface
->hotplug_list
);
819 INIT_LIST_HEAD(&iface
->assignment_classes
);
820 interface_ip_init(iface
);
821 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
822 iface
->config_ip
.enabled
= false;
824 iface
->main_dev
.cb
= interface_main_dev_cb
;
825 iface
->l3_dev
.cb
= interface_l3_dev_cb
;
826 iface
->ext_dev
.cb
= interface_ext_dev_cb
;
828 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
829 blob_data(config
), blob_len(config
));
831 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
832 proto_name
= blobmsg_data(cur
);
834 proto_attach_interface(iface
, proto_name
);
835 if (iface
->proto_handler
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
)
838 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
839 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], force_link
);
840 iface
->dynamic
= dynamic
;
841 iface
->proto_ip
.no_defaultroute
=
842 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
843 iface
->proto_ip
.no_dns
=
844 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
846 if ((cur
= tb
[IFACE_ATTR_DNS
]))
847 interface_add_dns_server_list(&iface
->config_ip
, cur
);
849 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
850 interface_add_dns_search_list(&iface
->config_ip
, cur
);
852 if ((cur
= tb
[IFACE_ATTR_DNS_METRIC
]))
853 iface
->dns_metric
= blobmsg_get_u32(cur
);
855 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
856 iface
->metric
= blobmsg_get_u32(cur
);
858 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
859 iface
->assignment_length
= blobmsg_get_u32(cur
);
862 iface
->assignment_iface_id_selection
= IFID_FIXED
;
863 iface
->assignment_fixed_iface_id
= in6addr_any
;
864 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
866 if ((cur
= tb
[IFACE_ATTR_IP6IFACEID
])) {
867 const char *ifaceid
= blobmsg_data(cur
);
868 if (!strcmp(ifaceid
, "random")) {
869 iface
->assignment_iface_id_selection
= IFID_RANDOM
;
871 else if (!strcmp(ifaceid
, "eui64")) {
872 iface
->assignment_iface_id_selection
= IFID_EUI64
;
875 /* we expect an IPv6 address with network id zero here -> fixed iface id
876 if we cannot parse -> revert to iface id 1 */
877 if (inet_pton(AF_INET6
,ifaceid
,&iface
->assignment_fixed_iface_id
) != 1 ||
878 iface
->assignment_fixed_iface_id
.s6_addr32
[0] != 0 ||
879 iface
->assignment_fixed_iface_id
.s6_addr32
[1] != 0) {
880 iface
->assignment_fixed_iface_id
= in6addr_any
;
881 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
882 netifd_log_message(L_WARNING
, "Failed to parse ip6ifaceid for interface '%s', \
883 falling back to iface id 1.\n", iface
->name
);
888 iface
->assignment_hint
= -1;
889 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
890 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
891 ~((1 << (64 - iface
->assignment_length
)) - 1);
893 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
894 interface_add_assignment_classes(iface
, cur
);
896 if ((cur
= tb
[IFACE_ATTR_IP6WEIGHT
]))
897 iface
->assignment_weight
= blobmsg_get_u32(cur
);
899 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
900 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
901 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
904 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
905 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
906 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
909 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
911 iface
->config_autostart
= iface
->autostart
;
914 if ((cur
= tb
[IFACE_ATTR_JAIL
])) {
915 iface
->jail
= strdup(blobmsg_get_string(cur
));
916 iface
->autostart
= false;
919 iface
->jail_ifname
= NULL
;
920 if ((cur
= tb
[IFACE_ATTR_JAIL_IFNAME
]))
921 iface
->jail_ifname
= strdup(blobmsg_get_string(cur
));
926 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
928 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
929 struct blob_attr
*cur
;
932 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
933 blob_data(config
), blob_len(config
));
936 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
937 iface
->parent_ifname
= blobmsg_data(cur
);
939 if (!iface
->parent_ifname
)
942 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
943 iface
->ifname
= blobmsg_data(cur
);
946 if (iface
->dynamic
) {
947 name
= strdup(iface
->name
);
953 iface
->config
= config
;
954 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
957 iface
= vlist_find(&interfaces
, name
, iface
, node
);
960 /* Don't delete dynamic interface on reload */
962 iface
->node
.version
= -1;
969 interface_add(struct interface
*iface
, struct blob_attr
*config
)
971 return __interface_add(iface
, config
, false);
975 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
977 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
980 return __interface_add(iface
, config
, true);
984 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
986 bool enabled
= iface
->config_ip
.enabled
;
987 bool claimed
= iface
->l3_dev
.claimed
;
989 if (iface
->l3_dev
.dev
== dev
)
992 interface_ip_set_enabled(&iface
->config_ip
, false);
993 interface_ip_set_enabled(&iface
->proto_ip
, false);
994 interface_ip_flush(&iface
->proto_ip
);
995 device_add_user(&iface
->l3_dev
, dev
);
999 if (device_claim(&iface
->l3_dev
) < 0)
1002 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
1003 interface_ip_set_enabled(&iface
->proto_ip
, enabled
);
1008 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
1010 bool claimed
= iface
->l3_dev
.claimed
;
1012 if (iface
->main_dev
.dev
== dev
)
1015 interface_set_available(iface
, false);
1016 device_add_user(&iface
->main_dev
, dev
);
1018 interface_set_link_state(iface
, false);
1023 if (device_claim(&iface
->l3_dev
) < 0)
1027 if (!iface
->l3_dev
.dev
)
1028 interface_set_l3_dev(iface
, dev
);
1032 interface_remove_link(struct interface
*iface
, struct device
*dev
)
1034 struct device
*mdev
= iface
->main_dev
.dev
;
1036 if (mdev
&& mdev
->hotplug_ops
)
1037 return mdev
->hotplug_ops
->del(mdev
, dev
);
1039 if (dev
== iface
->ext_dev
.dev
)
1040 device_remove_user(&iface
->ext_dev
);
1042 if (!iface
->main_dev
.hotplug
)
1043 return UBUS_STATUS_INVALID_ARGUMENT
;
1045 if (dev
!= iface
->main_dev
.dev
)
1046 return UBUS_STATUS_INVALID_ARGUMENT
;
1048 interface_set_main_dev(iface
, NULL
);
1053 interface_add_link(struct interface
*iface
, struct device
*dev
,
1054 struct blob_attr
*vlan
, bool link_ext
)
1056 struct device
*mdev
= iface
->main_dev
.dev
;
1061 if (iface
->main_dev
.hotplug
)
1062 device_remove_user(&iface
->main_dev
);
1065 if (mdev
->hotplug_ops
)
1066 return mdev
->hotplug_ops
->add(mdev
, dev
, vlan
);
1068 return UBUS_STATUS_NOT_SUPPORTED
;
1072 device_add_user(&iface
->ext_dev
, dev
);
1074 interface_set_main_dev(iface
, dev
);
1075 iface
->main_dev
.hotplug
= true;
1080 interface_handle_link(struct interface
*iface
, const char *name
,
1081 struct blob_attr
*vlan
, bool add
, bool link_ext
)
1088 dev
= device_get(name
, add
? (link_ext
? 2 : 1) : 0);
1090 ret
= UBUS_STATUS_NOT_FOUND
;
1095 interface_set_device_config(iface
, dev
);
1097 device_set_present(dev
, true);
1099 ret
= interface_add_link(iface
, dev
, vlan
, link_ext
);
1101 ret
= interface_remove_link(iface
, dev
);
1111 interface_set_up(struct interface
*iface
)
1114 const char *error
= NULL
;
1116 iface
->autostart
= true;
1118 if (iface
->state
!= IFS_DOWN
)
1121 interface_clear_errors(iface
);
1122 if (iface
->available
) {
1123 if (iface
->main_dev
.dev
) {
1124 ret
= device_claim(&iface
->main_dev
);
1126 interface_check_state(iface
);
1128 error
= "DEVICE_CLAIM_FAILED";
1130 ret
= __interface_set_up(iface
);
1132 error
= "SETUP_FAILED";
1135 error
= "NO_DEVICE";
1138 interface_add_error(iface
, "interface", error
, NULL
, 0);
1142 interface_set_down(struct interface
*iface
)
1145 vlist_for_each_element(&interfaces
, iface
, node
)
1146 __interface_set_down(iface
, false);
1148 iface
->autostart
= false;
1149 __interface_set_down(iface
, false);
1154 interface_renew(struct interface
*iface
)
1156 if (iface
->state
== IFS_TEARDOWN
|| iface
->state
== IFS_DOWN
)
1159 return interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
1163 interface_start_pending(void)
1165 struct interface
*iface
;
1167 vlist_for_each_element(&interfaces
, iface
, node
) {
1168 if (iface
->autostart
)
1169 interface_set_up(iface
);
1174 interface_start_jail(const char *jail
, const pid_t netns_pid
)
1176 struct interface
*iface
;
1181 netns_fd
= system_netns_open(netns_pid
);
1185 vlist_for_each_element(&interfaces
, iface
, node
) {
1186 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1189 system_link_netns_move(iface
->main_dev
.dev
, netns_fd
, iface
->jail_ifname
);
1196 waitpid(pr
, &wstatus
, WUNTRACED
| WCONTINUED
);
1201 netns_fd
= system_netns_open(netns_pid
);
1205 system_netns_set(netns_fd
);
1207 vlist_for_each_element(&interfaces
, iface
, node
) {
1208 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1212 * The interface has already been renamed and is inside target
1213 * namespace, hence overwrite ifname with jail_ifname for
1214 * interface_set_up().
1215 * We are inside a fork which got it's own copy of the interfaces
1216 * list, so we can mess with it :)
1218 if (iface
->jail_ifname
)
1219 iface
->ifname
= iface
->jail_ifname
;
1221 interface_do_reload(iface
);
1222 interface_set_up(iface
);
1230 interface_stop_jail(const char *jail
, const pid_t netns_pid
)
1232 struct interface
*iface
;
1233 int netns_fd
, root_netns
;
1235 pid_t parent_pid
= getpid();
1237 const char *orig_ifname
;
1241 waitpid(pr
, &wstatus
, WUNTRACED
| WCONTINUED
);
1246 root_netns
= system_netns_open(parent_pid
);
1250 netns_fd
= system_netns_open(netns_pid
);
1254 system_netns_set(netns_fd
);
1256 vlist_for_each_element(&interfaces
, iface
, node
) {
1257 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1260 orig_ifname
= iface
->ifname
;
1261 if (iface
->jail_ifname
)
1262 iface
->ifname
= iface
->jail_ifname
;
1264 interface_do_reload(iface
);
1265 interface_set_down(iface
);
1266 system_link_netns_move(iface
->main_dev
.dev
, root_netns
, orig_ifname
);
1275 set_config_state(struct interface
*iface
, enum interface_config_state s
)
1277 __set_config_state(iface
, s
);
1278 if (iface
->state
== IFS_DOWN
)
1279 interface_handle_config_change(iface
);
1281 __interface_set_down(iface
, false);
1285 interface_update_start(struct interface
*iface
, const bool keep_old
)
1290 interface_ip_update_start(&iface
->proto_ip
);
1294 interface_update_complete(struct interface
*iface
)
1296 interface_ip_update_complete(&iface
->proto_ip
);
1300 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
1302 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
1303 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
1307 interface_device_config_changed(struct interface
*if_old
, struct interface
*if_new
)
1309 struct blob_attr
*ntb
[__DEV_ATTR_MAX
];
1310 struct blob_attr
*otb
[__DEV_ATTR_MAX
];
1311 struct device
*dev
= if_old
->main_dev
.dev
;
1312 unsigned long diff
= 0;
1314 BUILD_BUG_ON(sizeof(diff
) < __DEV_ATTR_MAX
/ 8);
1319 if (if_old
->device_config
!= if_new
->device_config
)
1322 if (!if_new
->device_config
)
1325 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb
,
1326 blob_data(if_old
->config
), blob_len(if_old
->config
));
1328 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, ntb
,
1329 blob_data(if_new
->config
), blob_len(if_new
->config
));
1331 uci_blob_diff(ntb
, otb
, &device_attr_list
, &diff
);
1336 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
1338 struct blob_attr
*old_config
= if_old
->config
;
1339 bool reload
= false, reload_ip
= false, update_prefix_delegation
= false;
1341 #define FIELD_CHANGED_STR(field) \
1342 ((!!if_old->field != !!if_new->field) || \
1344 strcmp(if_old->field, if_new->field) != 0))
1346 if (FIELD_CHANGED_STR(parent_ifname
)) {
1347 if (if_old
->parent_iface
.iface
)
1348 interface_remove_user(&if_old
->parent_iface
);
1352 if (!reload
&& interface_device_config_changed(if_old
, if_new
))
1355 if (FIELD_CHANGED_STR(ifname
) ||
1356 if_old
->proto_handler
!= if_new
->proto_handler
)
1359 if (!if_old
->proto_handler
->config_params
)
1360 D(INTERFACE
, "No config parameters for interface '%s'\n",
1362 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
1363 if_old
->proto_handler
->config_params
))
1366 #define UPDATE(field, __var) ({ \
1367 bool __changed = (if_old->field != if_new->field); \
1368 if_old->field = if_new->field; \
1369 __var |= __changed; \
1372 if_old
->config
= if_new
->config
;
1373 if (if_old
->config_autostart
!= if_new
->config_autostart
) {
1374 if (if_old
->config_autostart
)
1377 if_old
->autostart
= if_new
->config_autostart
;
1380 if_old
->device_config
= if_new
->device_config
;
1381 if_old
->config_autostart
= if_new
->config_autostart
;
1385 if_old
->jail
= if_new
->jail
;
1387 if_old
->autostart
= false;
1389 if (if_old
->jail_ifname
)
1390 free(if_old
->jail_ifname
);
1392 if_old
->jail_ifname
= if_new
->jail_ifname
;
1394 if_old
->ifname
= if_new
->ifname
;
1395 if_old
->parent_ifname
= if_new
->parent_ifname
;
1396 if_old
->dynamic
= if_new
->dynamic
;
1397 if_old
->proto_handler
= if_new
->proto_handler
;
1398 if_old
->force_link
= if_new
->force_link
;
1399 if_old
->dns_metric
= if_new
->dns_metric
;
1401 if (if_old
->proto_ip
.no_delegation
!= if_new
->proto_ip
.no_delegation
) {
1402 if_old
->proto_ip
.no_delegation
= if_new
->proto_ip
.no_delegation
;
1403 update_prefix_delegation
= true;
1406 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1407 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1409 UPDATE(metric
, reload_ip
);
1410 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1411 UPDATE(ip4table
, reload_ip
);
1412 UPDATE(ip6table
, reload_ip
);
1413 interface_merge_assignment_data(if_old
, if_new
);
1418 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1420 interface_clear_errors(if_old
);
1421 set_config_state(if_old
, IFC_RELOAD
);
1426 bool config_ip_enabled
= if_old
->config_ip
.enabled
;
1427 bool proto_ip_enabled
= if_old
->proto_ip
.enabled
;
1429 interface_ip_set_enabled(&if_old
->config_ip
, false);
1430 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1431 interface_ip_set_enabled(&if_old
->proto_ip
, proto_ip_enabled
);
1432 interface_ip_set_enabled(&if_old
->config_ip
, config_ip_enabled
);
1435 if (update_prefix_delegation
)
1436 interface_update_prefix_delegation(&if_old
->proto_ip
);
1438 interface_write_resolv_conf(if_old
->jail
);
1439 if (if_old
->main_dev
.dev
)
1440 interface_check_state(if_old
);
1443 if_new
->config
= NULL
;
1444 interface_cleanup(if_new
);
1450 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1451 struct vlist_node
*node_old
)
1453 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1454 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1456 if (node_old
&& node_new
) {
1457 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1458 interface_change_config(if_old
, if_new
);
1459 } else if (node_old
) {
1460 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1461 set_config_state(if_old
, IFC_REMOVE
);
1462 } else if (node_new
) {
1463 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1464 interface_event(if_new
, IFEV_CREATE
);
1465 proto_init_interface(if_new
, if_new
->config
);
1466 interface_claim_device(if_new
);
1467 netifd_ubus_add_interface(if_new
);
1473 interface_init_list(void)
1475 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1476 interfaces
.keep_old
= true;
1477 interfaces
.no_delete
= true;