2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
17 #include <sys/types.h>
22 #include "interface.h"
23 #include "interface-ip.h"
29 struct vlist_tree interfaces
;
30 static LIST_HEAD(iface_all_users
);
34 IFACE_ATTR_IFNAME
, /* Backward compatibility */
39 IFACE_ATTR_JAIL_DEVICE
,
40 IFACE_ATTR_JAIL_IFNAME
,
41 IFACE_ATTR_HOST_DEVICE
,
42 IFACE_ATTR_DEFAULTROUTE
,
45 IFACE_ATTR_DNS_SEARCH
,
46 IFACE_ATTR_DNS_METRIC
,
55 IFACE_ATTR_IP6IFACEID
,
56 IFACE_ATTR_FORCE_LINK
,
61 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
62 [IFACE_ATTR_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
63 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
64 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
65 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
66 [IFACE_ATTR_ZONE
] = { .name
= "zone", .type
= BLOBMSG_TYPE_STRING
},
67 [IFACE_ATTR_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
68 [IFACE_ATTR_JAIL_DEVICE
] = { .name
= "jail_device", .type
= BLOBMSG_TYPE_STRING
},
69 [IFACE_ATTR_JAIL_IFNAME
] = { .name
= "jail_ifname", .type
= BLOBMSG_TYPE_STRING
},
70 [IFACE_ATTR_HOST_DEVICE
] = { .name
= "host_device", .type
= BLOBMSG_TYPE_STRING
},
71 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
72 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
73 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
74 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
75 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
76 [IFACE_ATTR_DNS_METRIC
] = { .name
= "dns_metric", .type
= BLOBMSG_TYPE_INT32
},
77 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
78 [IFACE_ATTR_IP6ASSIGN
] = { .name
= "ip6assign", .type
= BLOBMSG_TYPE_INT32
},
79 [IFACE_ATTR_IP6HINT
] = { .name
= "ip6hint", .type
= BLOBMSG_TYPE_STRING
},
80 [IFACE_ATTR_IP4TABLE
] = { .name
= "ip4table", .type
= BLOBMSG_TYPE_STRING
},
81 [IFACE_ATTR_IP6TABLE
] = { .name
= "ip6table", .type
= BLOBMSG_TYPE_STRING
},
82 [IFACE_ATTR_IP6CLASS
] = { .name
= "ip6class", .type
= BLOBMSG_TYPE_ARRAY
},
83 [IFACE_ATTR_DELEGATE
] = { .name
= "delegate", .type
= BLOBMSG_TYPE_BOOL
},
84 [IFACE_ATTR_IP6IFACEID
] = { .name
= "ip6ifaceid", .type
= BLOBMSG_TYPE_STRING
},
85 [IFACE_ATTR_FORCE_LINK
] = { .name
= "force_link", .type
= BLOBMSG_TYPE_BOOL
},
86 [IFACE_ATTR_IP6WEIGHT
] = { .name
= "ip6weight", .type
= BLOBMSG_TYPE_INT32
},
89 const struct uci_blob_param_list interface_attr_list
= {
90 .n_params
= IFACE_ATTR_MAX
,
91 .params
= iface_attrs
,
95 interface_set_main_dev(struct interface
*iface
, struct device
*dev
);
97 interface_event(struct interface
*iface
, enum interface_event ev
);
100 interface_error_flush(struct interface
*iface
)
102 struct interface_error
*error
, *tmp
;
104 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
105 list_del(&error
->list
);
111 interface_force_link(struct interface
*iface
)
113 struct device
*dev
= iface
->main_dev
.dev
;
115 if (dev
&& dev
->settings
.auth
)
118 return iface
->force_link
;
122 interface_clear_errors(struct interface
*iface
)
124 /* don't flush the errors in case the configured protocol handler matches the
125 running protocol handler and is having the last error capability */
126 if (!(iface
->proto
&&
127 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
128 (iface
->proto
->handler
->name
== iface
->proto_handler
->name
)))
129 interface_error_flush(iface
);
132 void interface_add_error(struct interface
*iface
, const char *subsystem
,
133 const char *code
, const char **data
, int n_data
)
135 struct interface_error
*error
;
138 char *dest
, *d_subsys
, *d_code
;
140 /* if the configured protocol handler has the last error support capability,
141 errors should only be added if the running protocol handler matches the
144 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
145 (iface
->proto
->handler
->name
!= iface
->proto_handler
->name
))
149 len
= n_data
* sizeof(char *);
150 datalen
= alloca(len
);
151 for (i
= 0; i
< n_data
; i
++) {
152 datalen
[i
] = strlen(data
[i
]) + 1;
157 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
158 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
159 &d_code
, code
? strlen(code
) + 1 : 0);
163 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
164 protocol can't be established (e.g auth failure) */
165 if (iface
->proto_handler
->flags
& PROTO_FLAG_LASTERROR
)
166 interface_error_flush(iface
);
168 list_add_tail(&error
->list
, &iface
->errors
);
170 dest
= (char *) &error
->data
[n_data
+ 1];
171 for (i
= 0; i
< n_data
; i
++) {
172 error
->data
[i
] = dest
;
173 memcpy(dest
, data
[i
], datalen
[i
]);
176 error
->data
[n_data
] = NULL
;
179 error
->subsystem
= strcpy(d_subsys
, subsystem
);
182 error
->code
= strcpy(d_code
, code
);
186 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
188 avl_delete(&iface
->data
, &data
->node
);
193 interface_data_flush(struct interface
*iface
)
195 struct interface_data
*d
, *tmp
;
197 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
198 interface_data_del(iface
, d
);
202 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
204 struct interface_data
*n
, *o
;
206 if (!blobmsg_check_attr(data
, true))
207 return UBUS_STATUS_INVALID_ARGUMENT
;
209 const char *name
= blobmsg_name(data
);
210 unsigned len
= blob_pad_len(data
);
212 o
= avl_find_element(&iface
->data
, name
, o
, node
);
214 if (blob_pad_len(o
->data
) == len
&& !memcmp(o
->data
, data
, len
))
217 interface_data_del(iface
, o
);
220 n
= calloc(1, sizeof(*n
) + len
);
222 return UBUS_STATUS_UNKNOWN_ERROR
;
224 memcpy(n
->data
, data
, len
);
225 n
->node
.key
= blobmsg_name(n
->data
);
226 avl_insert(&iface
->data
, &n
->node
);
228 iface
->updated
|= IUF_DATA
;
232 int interface_parse_data(struct interface
*iface
, const struct blob_attr
*attr
)
234 struct blob_attr
*cur
;
240 blob_for_each_attr(cur
, attr
, rem
) {
241 ret
= interface_add_data(iface
, cur
);
246 if (iface
->updated
&& iface
->state
== IFS_UP
)
247 interface_event(iface
, IFEV_UPDATE
);
253 interface_event(struct interface
*iface
, enum interface_event ev
)
255 struct interface_user
*dep
, *tmp
;
256 struct device
*adev
= NULL
;
258 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
259 dep
->cb(dep
, iface
, ev
);
261 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
262 dep
->cb(dep
, iface
, ev
);
266 interface_error_flush(iface
);
267 adev
= iface
->l3_dev
.dev
;
271 alias_notify_device(iface
->name
, adev
);
279 interface_flush_state(struct interface
*iface
)
281 if (iface
->l3_dev
.dev
)
282 device_release(&iface
->l3_dev
);
283 interface_data_flush(iface
);
287 mark_interface_down(struct interface
*iface
)
289 enum interface_state state
= iface
->state
;
291 if (state
== IFS_DOWN
)
294 iface
->link_up_event
= false;
295 iface
->state
= IFS_DOWN
;
299 interface_event(iface
, IFEV_DOWN
);
302 interface_event(iface
, IFEV_UP_FAILED
);
307 interface_ip_set_enabled(&iface
->config_ip
, false);
308 interface_ip_set_enabled(&iface
->proto_ip
, false);
309 interface_ip_flush(&iface
->proto_ip
);
310 interface_flush_state(iface
);
311 system_flush_routes();
315 __set_config_state(struct interface
*iface
, enum interface_config_state s
)
317 iface
->config_state
= s
;
321 __interface_set_down(struct interface
*iface
, bool force
)
323 enum interface_state state
= iface
->state
;
327 iface
->state
= IFS_TEARDOWN
;
329 __set_config_state(iface
, IFC_REMOVE
);
332 interface_event(iface
, IFEV_DOWN
);
334 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
336 interface_flush_state(iface
);
340 if (iface
->main_dev
.dev
)
341 device_release(&iface
->main_dev
);
350 __interface_set_up(struct interface
*iface
)
354 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
356 iface
->state
= IFS_SETUP
;
357 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
359 mark_interface_down(iface
);
365 interface_check_state(struct interface
*iface
)
367 bool link_state
= iface
->link_state
|| interface_force_link(iface
);
369 switch (iface
->state
) {
372 if (!iface
->enabled
|| !link_state
) {
373 iface
->state
= IFS_TEARDOWN
;
375 __set_config_state(iface
, IFC_REMOVE
);
377 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
381 if (!iface
->available
)
384 if (iface
->autostart
&& iface
->enabled
&& link_state
&& !config_init
)
385 __interface_set_up(iface
);
393 interface_set_enabled(struct interface
*iface
, bool new_state
)
395 if (iface
->enabled
== new_state
)
398 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
399 iface
->enabled
= new_state
;
400 interface_check_state(iface
);
404 interface_set_link_state(struct interface
*iface
, bool new_state
)
406 if (iface
->link_state
== new_state
)
409 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
410 iface
->link_state
= new_state
;
411 interface_check_state(iface
);
413 if (new_state
&& interface_force_link(iface
) &&
414 iface
->state
== IFS_UP
&& !iface
->link_up_event
) {
415 interface_event(iface
, IFEV_LINK_UP
);
416 iface
->link_up_event
= true;
421 interface_ext_dev_cb(struct device_user
*dep
, enum device_event ev
)
423 if (ev
== DEV_EVENT_REMOVE
)
424 device_remove_user(dep
);
428 interface_main_dev_cb(struct device_user
*dep
, enum device_event ev
)
430 struct interface
*iface
;
432 iface
= container_of(dep
, struct interface
, main_dev
);
435 interface_set_available(iface
, true);
437 case DEV_EVENT_REMOVE
:
438 interface_set_available(iface
, false);
439 if (dep
->dev
&& dep
->dev
->external
)
440 interface_set_main_dev(iface
, NULL
);
443 interface_set_enabled(iface
, true);
446 interface_set_enabled(iface
, false);
448 case DEV_EVENT_AUTH_UP
:
449 case DEV_EVENT_LINK_UP
:
450 case DEV_EVENT_LINK_DOWN
:
451 interface_set_link_state(iface
, device_link_active(dep
->dev
));
453 case DEV_EVENT_TOPO_CHANGE
:
454 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
462 interface_l3_dev_cb(struct device_user
*dep
, enum device_event ev
)
464 struct interface
*iface
;
466 iface
= container_of(dep
, struct interface
, l3_dev
);
467 if (iface
->l3_dev
.dev
== iface
->main_dev
.dev
)
471 case DEV_EVENT_LINK_DOWN
:
472 if (iface
->proto_handler
->flags
& PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN
)
473 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
481 interface_set_available(struct interface
*iface
, bool new_state
)
483 if (iface
->available
== new_state
)
486 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
487 iface
->available
= new_state
;
490 if (iface
->autostart
&& !config_init
)
491 interface_set_up(iface
);
493 __interface_set_down(iface
, true);
497 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
500 list_add(&dep
->list
, &iface_all_users
);
505 list_add(&dep
->list
, &iface
->users
);
506 if (iface
->state
== IFS_UP
)
507 dep
->cb(dep
, iface
, IFEV_UP
);
511 interface_remove_user(struct interface_user
*dep
)
513 list_del_init(&dep
->list
);
518 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
520 struct blob_attr
*cur
;
523 blobmsg_for_each_attr(cur
, list
, rem
) {
524 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
527 if (!blobmsg_check_attr(cur
, false))
530 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
531 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
532 list_add(&c
->head
, &iface
->assignment_classes
);
537 interface_clear_assignment_classes(struct interface
*iface
)
539 while (!list_empty(&iface
->assignment_classes
)) {
540 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
541 struct interface_assignment_class
, head
);
548 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
550 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
551 old
->assignment_length
!= new->assignment_length
||
552 old
->assignment_iface_id_selection
!= new->assignment_iface_id_selection
||
553 old
->assignment_weight
!= new->assignment_weight
||
554 (old
->assignment_iface_id_selection
== IFID_FIXED
&&
555 memcmp(&old
->assignment_fixed_iface_id
, &new->assignment_fixed_iface_id
,
556 sizeof(old
->assignment_fixed_iface_id
))) ||
557 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
559 struct interface_assignment_class
*c
;
560 list_for_each_entry(c
, &new->assignment_classes
, head
) {
561 /* Compare list entries one-by-one to see if there was a change */
562 if (list_empty(&old
->assignment_classes
)) /* The new list is longer */
568 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
569 struct interface_assignment_class
, head
);
571 if (strcmp(c_old
->name
, c
->name
)) /* An entry didn't match */
574 list_del(&c_old
->head
);
578 /* The old list was longer than the new one or the last entry didn't match */
579 if (!list_empty(&old
->assignment_classes
)) {
580 interface_clear_assignment_classes(old
);
584 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
587 old
->assignment_hint
= new->assignment_hint
;
588 old
->assignment_length
= new->assignment_length
;
589 old
->assignment_iface_id_selection
= new->assignment_iface_id_selection
;
590 old
->assignment_fixed_iface_id
= new->assignment_fixed_iface_id
;
591 old
->assignment_weight
= new->assignment_weight
;
592 interface_refresh_assignments(true);
597 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
599 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
600 struct device
*dev
= iface
->l3_dev
.dev
;
607 interface_set_main_dev(alias
, dev
);
608 interface_set_available(alias
, true);
612 interface_set_available(alias
, false);
613 interface_set_main_dev(alias
, NULL
);
616 interface_remove_user(dep
);
624 interface_set_device_config(struct interface
*iface
, struct device
*dev
)
626 if (!dev
|| !dev
->default_config
)
629 if (!iface
->device_config
&&
630 (!dev
->iface_config
|| dev
->config_iface
!= iface
))
633 dev
->config_iface
= iface
;
634 dev
->iface_config
= iface
->device_config
;
635 device_apply_config(dev
, dev
->type
, iface
->config
);
639 interface_claim_device(struct interface
*iface
)
641 struct interface
*parent
;
642 struct device
*dev
= NULL
;
644 if (iface
->parent_iface
.iface
)
645 interface_remove_user(&iface
->parent_iface
);
647 if (iface
->parent_ifname
) {
648 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
649 iface
->parent_iface
.cb
= interface_alias_cb
;
650 interface_add_user(&iface
->parent_iface
, parent
);
651 } else if (iface
->device
&&
652 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
653 dev
= device_get(iface
->device
, true);
654 interface_set_device_config(iface
, dev
);
656 dev
= iface
->ext_dev
.dev
;
660 interface_set_main_dev(iface
, dev
);
662 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
663 interface_set_available(iface
, true);
667 interface_cleanup_state(struct interface
*iface
)
669 interface_set_available(iface
, false);
671 interface_flush_state(iface
);
672 interface_clear_errors(iface
);
673 interface_set_proto_state(iface
, NULL
);
675 interface_set_main_dev(iface
, NULL
);
676 interface_set_l3_dev(iface
, NULL
);
680 interface_cleanup(struct interface
*iface
)
682 struct interface_user
*dep
, *tmp
;
684 uloop_timeout_cancel(&iface
->remove_timer
);
685 device_remove_user(&iface
->ext_dev
);
687 if (iface
->parent_iface
.iface
)
688 interface_remove_user(&iface
->parent_iface
);
690 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
691 interface_remove_user(dep
);
693 interface_clear_assignment_classes(iface
);
694 interface_ip_flush(&iface
->config_ip
);
695 interface_cleanup_state(iface
);
699 interface_do_free(struct interface
*iface
)
701 interface_event(iface
, IFEV_FREE
);
702 interface_cleanup(iface
);
704 netifd_ubus_remove_interface(iface
);
705 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
708 if (iface
->jail_device
)
709 free(iface
->jail_device
);
710 if (iface
->host_device
)
711 free(iface
->host_device
);
717 interface_do_reload(struct interface
*iface
)
719 interface_event(iface
, IFEV_RELOAD
);
720 interface_cleanup_state(iface
);
721 proto_init_interface(iface
, iface
->config
);
722 interface_claim_device(iface
);
726 interface_handle_config_change(struct interface
*iface
)
728 enum interface_config_state state
= iface
->config_state
;
730 iface
->config_state
= IFC_NORMAL
;
735 interface_do_reload(iface
);
738 interface_do_free(iface
);
741 if (iface
->autostart
)
742 interface_set_up(iface
);
746 interface_proto_event_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
748 struct interface
*iface
= state
->iface
;
752 if (iface
->state
!= IFS_SETUP
) {
753 if (iface
->state
== IFS_UP
&& iface
->updated
)
754 interface_event(iface
, IFEV_UPDATE
);
758 if (!iface
->l3_dev
.dev
)
759 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
761 interface_ip_set_enabled(&iface
->config_ip
, true);
762 interface_ip_set_enabled(&iface
->proto_ip
, true);
763 system_flush_routes();
764 iface
->state
= IFS_UP
;
765 iface
->start_time
= system_get_rtime();
766 interface_event(iface
, IFEV_UP
);
767 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
770 if (iface
->state
== IFS_DOWN
)
773 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
774 mark_interface_down(iface
);
775 interface_write_resolv_conf(iface
->jail
);
776 if (iface
->main_dev
.dev
)
777 device_release(&iface
->main_dev
);
778 if (iface
->l3_dev
.dev
)
779 device_remove_user(&iface
->l3_dev
);
780 interface_handle_config_change(iface
);
782 case IFPEV_LINK_LOST
:
783 if (iface
->state
!= IFS_UP
)
786 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
787 mark_interface_down(iface
);
788 iface
->state
= IFS_SETUP
;
794 interface_write_resolv_conf(iface
->jail
);
797 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
800 iface
->proto
->free(iface
->proto
);
803 iface
->state
= IFS_DOWN
;
804 iface
->proto
= state
;
808 state
->proto_event
= interface_proto_event_cb
;
809 state
->iface
= iface
;
813 interface_alloc(const char *name
, struct blob_attr
*config
, bool dynamic
)
815 struct interface
*iface
;
816 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
817 struct blob_attr
*cur
;
818 const char *proto_name
= NULL
;
820 bool force_link
= false;
822 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
823 iface
->name
= strcpy(iface_name
, name
);
824 INIT_LIST_HEAD(&iface
->errors
);
825 INIT_LIST_HEAD(&iface
->users
);
826 INIT_LIST_HEAD(&iface
->hotplug_list
);
827 INIT_LIST_HEAD(&iface
->assignment_classes
);
828 interface_ip_init(iface
);
829 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
830 iface
->config_ip
.enabled
= false;
832 iface
->main_dev
.cb
= interface_main_dev_cb
;
833 iface
->l3_dev
.cb
= interface_l3_dev_cb
;
834 iface
->ext_dev
.cb
= interface_ext_dev_cb
;
836 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
837 blob_data(config
), blob_len(config
));
840 if ((cur
= tb
[IFACE_ATTR_ZONE
]))
841 iface
->zone
= strdup(blobmsg_get_string(cur
));
843 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
844 proto_name
= blobmsg_data(cur
);
846 proto_attach_interface(iface
, proto_name
);
847 if (iface
->proto_handler
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
)
850 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
851 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], force_link
);
852 iface
->dynamic
= dynamic
;
853 iface
->proto_ip
.no_defaultroute
=
854 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
855 iface
->proto_ip
.no_dns
=
856 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
858 if ((cur
= tb
[IFACE_ATTR_DNS
]))
859 interface_add_dns_server_list(&iface
->config_ip
, cur
);
861 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
862 interface_add_dns_search_list(&iface
->config_ip
, cur
);
864 if ((cur
= tb
[IFACE_ATTR_DNS_METRIC
]))
865 iface
->dns_metric
= blobmsg_get_u32(cur
);
867 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
868 iface
->metric
= blobmsg_get_u32(cur
);
870 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
871 iface
->assignment_length
= blobmsg_get_u32(cur
);
874 iface
->assignment_iface_id_selection
= IFID_FIXED
;
875 iface
->assignment_fixed_iface_id
= in6addr_any
;
876 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
878 if ((cur
= tb
[IFACE_ATTR_IP6IFACEID
])) {
879 const char *ifaceid
= blobmsg_data(cur
);
880 if (!strcmp(ifaceid
, "random")) {
881 iface
->assignment_iface_id_selection
= IFID_RANDOM
;
883 else if (!strcmp(ifaceid
, "eui64")) {
884 iface
->assignment_iface_id_selection
= IFID_EUI64
;
887 /* we expect an IPv6 address with network id zero here -> fixed iface id
888 if we cannot parse -> revert to iface id 1 */
889 if (inet_pton(AF_INET6
,ifaceid
,&iface
->assignment_fixed_iface_id
) != 1 ||
890 iface
->assignment_fixed_iface_id
.s6_addr32
[0] != 0 ||
891 iface
->assignment_fixed_iface_id
.s6_addr32
[1] != 0) {
892 iface
->assignment_fixed_iface_id
= in6addr_any
;
893 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
894 netifd_log_message(L_WARNING
, "Failed to parse ip6ifaceid for interface '%s', \
895 falling back to iface id 1.\n", iface
->name
);
900 iface
->assignment_hint
= -1;
901 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
902 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
903 ~((1 << (64 - iface
->assignment_length
)) - 1);
905 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
906 interface_add_assignment_classes(iface
, cur
);
908 if ((cur
= tb
[IFACE_ATTR_IP6WEIGHT
]))
909 iface
->assignment_weight
= blobmsg_get_u32(cur
);
911 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
912 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
913 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
916 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
917 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
918 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
921 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
923 iface
->config_autostart
= iface
->autostart
;
926 if ((cur
= tb
[IFACE_ATTR_JAIL
])) {
927 iface
->jail
= strdup(blobmsg_get_string(cur
));
928 iface
->autostart
= false;
931 iface
->jail_device
= NULL
;
932 if ((cur
= tb
[IFACE_ATTR_JAIL_DEVICE
]))
933 iface
->jail_device
= strdup(blobmsg_get_string(cur
));
934 else if ((cur
= tb
[IFACE_ATTR_JAIL_IFNAME
]))
935 iface
->jail_device
= strdup(blobmsg_get_string(cur
));
937 iface
->host_device
= NULL
;
938 if ((cur
= tb
[IFACE_ATTR_HOST_DEVICE
]))
939 iface
->host_device
= strdup(blobmsg_get_string(cur
));
944 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
946 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
947 struct blob_attr
*cur
;
950 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
951 blob_data(config
), blob_len(config
));
954 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
955 iface
->parent_ifname
= blobmsg_data(cur
);
957 if (!iface
->parent_ifname
)
960 cur
= tb
[IFACE_ATTR_DEVICE
];
962 cur
= tb
[IFACE_ATTR_IFNAME
];
964 iface
->device
= blobmsg_data(cur
);
967 if (iface
->dynamic
) {
968 name
= strdup(iface
->name
);
974 iface
->config
= config
;
975 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
978 iface
= vlist_find(&interfaces
, name
, iface
, node
);
981 /* Don't delete dynamic interface on reload */
983 iface
->node
.version
= -1;
990 interface_add(struct interface
*iface
, struct blob_attr
*config
)
992 return __interface_add(iface
, config
, false);
996 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
998 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
1001 return __interface_add(iface
, config
, true);
1005 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
1007 bool enabled
= iface
->config_ip
.enabled
;
1008 bool claimed
= iface
->l3_dev
.claimed
;
1010 if (iface
->l3_dev
.dev
== dev
)
1013 interface_ip_set_enabled(&iface
->config_ip
, false);
1014 interface_ip_set_enabled(&iface
->proto_ip
, false);
1015 interface_ip_flush(&iface
->proto_ip
);
1016 device_add_user(&iface
->l3_dev
, dev
);
1020 if (device_claim(&iface
->l3_dev
) < 0)
1023 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
1024 interface_ip_set_enabled(&iface
->proto_ip
, enabled
);
1029 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
1031 bool claimed
= iface
->l3_dev
.claimed
;
1033 if (iface
->main_dev
.dev
== dev
)
1036 interface_set_available(iface
, false);
1037 device_add_user(&iface
->main_dev
, dev
);
1039 interface_set_link_state(iface
, false);
1044 if (device_claim(&iface
->l3_dev
) < 0)
1048 if (!iface
->l3_dev
.dev
)
1049 interface_set_l3_dev(iface
, dev
);
1053 interface_remove_link(struct interface
*iface
, struct device
*dev
,
1054 struct blob_attr
*vlan
)
1056 struct device
*mdev
= iface
->main_dev
.dev
;
1058 if (mdev
&& mdev
->hotplug_ops
)
1059 return mdev
->hotplug_ops
->del(mdev
, dev
, vlan
);
1061 if (dev
== iface
->ext_dev
.dev
)
1062 device_remove_user(&iface
->ext_dev
);
1064 if (!iface
->main_dev
.hotplug
)
1065 return UBUS_STATUS_INVALID_ARGUMENT
;
1067 if (dev
!= iface
->main_dev
.dev
)
1068 return UBUS_STATUS_INVALID_ARGUMENT
;
1070 interface_set_main_dev(iface
, NULL
);
1075 interface_add_link(struct interface
*iface
, struct device
*dev
,
1076 struct blob_attr
*vlan
, bool link_ext
)
1078 struct device
*mdev
= iface
->main_dev
.dev
;
1083 if (iface
->main_dev
.hotplug
)
1084 device_remove_user(&iface
->main_dev
);
1087 if (mdev
->hotplug_ops
)
1088 return mdev
->hotplug_ops
->add(mdev
, dev
, vlan
);
1090 return UBUS_STATUS_NOT_SUPPORTED
;
1094 device_add_user(&iface
->ext_dev
, dev
);
1096 interface_set_main_dev(iface
, dev
);
1097 iface
->main_dev
.hotplug
= true;
1102 interface_handle_link(struct interface
*iface
, const char *name
,
1103 struct blob_attr
*vlan
, bool add
, bool link_ext
)
1107 dev
= device_get(name
, add
? (link_ext
? 2 : 1) : 0);
1109 return UBUS_STATUS_NOT_FOUND
;
1112 return interface_remove_link(iface
, dev
, vlan
);
1114 interface_set_device_config(iface
, dev
);
1116 device_set_present(dev
, true);
1118 return interface_add_link(iface
, dev
, vlan
, link_ext
);
1122 interface_set_up(struct interface
*iface
)
1125 const char *error
= NULL
;
1127 iface
->autostart
= true;
1129 if (iface
->state
!= IFS_DOWN
)
1132 interface_clear_errors(iface
);
1133 if (iface
->available
) {
1134 if (iface
->main_dev
.dev
) {
1135 ret
= device_claim(&iface
->main_dev
);
1137 interface_check_state(iface
);
1139 error
= "DEVICE_CLAIM_FAILED";
1141 ret
= __interface_set_up(iface
);
1143 error
= "SETUP_FAILED";
1146 error
= "NO_DEVICE";
1149 interface_add_error(iface
, "interface", error
, NULL
, 0);
1153 interface_set_down(struct interface
*iface
)
1156 vlist_for_each_element(&interfaces
, iface
, node
)
1157 __interface_set_down(iface
, false);
1159 iface
->autostart
= false;
1160 __interface_set_down(iface
, false);
1165 interface_renew(struct interface
*iface
)
1167 if (iface
->state
== IFS_TEARDOWN
|| iface
->state
== IFS_DOWN
)
1170 return interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
1174 interface_start_pending(void)
1176 struct interface
*iface
;
1178 vlist_for_each_element(&interfaces
, iface
, node
) {
1179 if (iface
->autostart
)
1180 interface_set_up(iface
);
1185 interface_start_jail(int netns_fd
, const char *jail
)
1187 struct interface
*iface
;
1189 vlist_for_each_element(&interfaces
, iface
, node
) {
1190 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1193 system_link_netns_move(iface
->main_dev
.dev
, netns_fd
, iface
->jail_device
);
1198 interface_stop_jail(int netns_fd
)
1200 struct interface
*iface
;
1203 vlist_for_each_element(&interfaces
, iface
, node
) {
1204 orig_ifname
= iface
->host_device
;
1205 interface_set_down(iface
);
1206 system_link_netns_move(iface
->main_dev
.dev
, netns_fd
, orig_ifname
);
1211 set_config_state(struct interface
*iface
, enum interface_config_state s
)
1213 __set_config_state(iface
, s
);
1214 if (iface
->state
== IFS_DOWN
)
1215 interface_handle_config_change(iface
);
1217 __interface_set_down(iface
, false);
1221 interface_update_start(struct interface
*iface
, const bool keep_old
)
1226 interface_ip_update_start(&iface
->proto_ip
);
1230 interface_update_complete(struct interface
*iface
)
1232 interface_ip_update_complete(&iface
->proto_ip
);
1236 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
1238 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
1239 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
1243 interface_device_config_changed(struct interface
*if_old
, struct interface
*if_new
)
1245 struct blob_attr
*ntb
[__DEV_ATTR_MAX
];
1246 struct blob_attr
*otb
[__DEV_ATTR_MAX
];
1247 struct device
*dev
= if_old
->main_dev
.dev
;
1248 unsigned long diff
= 0;
1250 BUILD_BUG_ON(sizeof(diff
) < __DEV_ATTR_MAX
/ 8);
1255 if (if_old
->device_config
!= if_new
->device_config
)
1258 if (!if_new
->device_config
)
1261 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb
,
1262 blob_data(if_old
->config
), blob_len(if_old
->config
));
1264 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, ntb
,
1265 blob_data(if_new
->config
), blob_len(if_new
->config
));
1267 uci_blob_diff(ntb
, otb
, &device_attr_list
, &diff
);
1272 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
1274 struct blob_attr
*old_config
= if_old
->config
;
1275 bool reload
= false, reload_ip
= false, update_prefix_delegation
= false;
1277 #define FIELD_CHANGED_STR(field) \
1278 ((!!if_old->field != !!if_new->field) || \
1280 strcmp(if_old->field, if_new->field) != 0))
1282 if (FIELD_CHANGED_STR(parent_ifname
)) {
1283 if (if_old
->parent_iface
.iface
)
1284 interface_remove_user(&if_old
->parent_iface
);
1288 if (!reload
&& interface_device_config_changed(if_old
, if_new
))
1291 if (FIELD_CHANGED_STR(device
) ||
1292 if_old
->proto_handler
!= if_new
->proto_handler
)
1295 if (!if_old
->proto_handler
->config_params
)
1296 D(INTERFACE
, "No config parameters for interface '%s'\n",
1298 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
1299 if_old
->proto_handler
->config_params
))
1302 #define UPDATE(field, __var) ({ \
1303 bool __changed = (if_old->field != if_new->field); \
1304 if_old->field = if_new->field; \
1305 __var |= __changed; \
1308 if_old
->config
= if_new
->config
;
1309 if (if_old
->config_autostart
!= if_new
->config_autostart
) {
1310 if (if_old
->config_autostart
)
1313 if_old
->autostart
= if_new
->config_autostart
;
1316 if_old
->device_config
= if_new
->device_config
;
1317 if_old
->config_autostart
= if_new
->config_autostart
;
1321 if_old
->jail
= if_new
->jail
;
1323 if_old
->autostart
= false;
1325 if (if_old
->jail_device
)
1326 free(if_old
->jail_device
);
1328 if_old
->jail_device
= if_new
->jail_device
;
1330 if (if_old
->host_device
)
1331 free(if_old
->host_device
);
1333 if_old
->host_device
= if_new
->host_device
;
1335 if_old
->device
= if_new
->device
;
1336 if_old
->parent_ifname
= if_new
->parent_ifname
;
1337 if_old
->dynamic
= if_new
->dynamic
;
1338 if_old
->proto_handler
= if_new
->proto_handler
;
1339 if_old
->force_link
= if_new
->force_link
;
1340 if_old
->dns_metric
= if_new
->dns_metric
;
1342 if (if_old
->proto_ip
.no_delegation
!= if_new
->proto_ip
.no_delegation
) {
1343 if_old
->proto_ip
.no_delegation
= if_new
->proto_ip
.no_delegation
;
1344 update_prefix_delegation
= true;
1347 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1348 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1350 UPDATE(metric
, reload_ip
);
1351 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1352 UPDATE(ip4table
, reload_ip
);
1353 UPDATE(ip6table
, reload_ip
);
1354 interface_merge_assignment_data(if_old
, if_new
);
1359 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1361 interface_clear_errors(if_old
);
1362 set_config_state(if_old
, IFC_RELOAD
);
1367 bool config_ip_enabled
= if_old
->config_ip
.enabled
;
1368 bool proto_ip_enabled
= if_old
->proto_ip
.enabled
;
1370 interface_ip_set_enabled(&if_old
->config_ip
, false);
1371 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1372 interface_ip_set_enabled(&if_old
->proto_ip
, proto_ip_enabled
);
1373 interface_ip_set_enabled(&if_old
->config_ip
, config_ip_enabled
);
1376 if (update_prefix_delegation
)
1377 interface_update_prefix_delegation(&if_old
->proto_ip
);
1379 interface_write_resolv_conf(if_old
->jail
);
1380 if (if_old
->main_dev
.dev
)
1381 interface_check_state(if_old
);
1384 if_new
->config
= NULL
;
1385 interface_cleanup(if_new
);
1391 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1392 struct vlist_node
*node_old
)
1394 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1395 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1397 if (node_old
&& node_new
) {
1398 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1399 interface_change_config(if_old
, if_new
);
1400 } else if (node_old
) {
1401 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1402 set_config_state(if_old
, IFC_REMOVE
);
1403 } else if (node_new
) {
1404 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1405 interface_event(if_new
, IFEV_CREATE
);
1406 proto_init_interface(if_new
, if_new
->config
);
1407 interface_claim_device(if_new
);
1408 netifd_ubus_add_interface(if_new
);
1414 interface_init_list(void)
1416 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1417 interfaces
.keep_old
= true;
1418 interfaces
.no_delete
= true;