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.
20 #include "interface.h"
21 #include "interface-ip.h"
27 struct vlist_tree interfaces
;
28 static LIST_HEAD(iface_all_users
);
34 IFACE_ATTR_DEFAULTROUTE
,
37 IFACE_ATTR_DNS_SEARCH
,
38 IFACE_ATTR_DNS_METRIC
,
47 IFACE_ATTR_IP6IFACEID
,
48 IFACE_ATTR_FORCE_LINK
,
53 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
54 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
55 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
56 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
57 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
58 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
59 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
60 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
61 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
62 [IFACE_ATTR_DNS_METRIC
] = { .name
= "dns_metric", .type
= BLOBMSG_TYPE_INT32
},
63 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
64 [IFACE_ATTR_IP6ASSIGN
] = { .name
= "ip6assign", .type
= BLOBMSG_TYPE_INT32
},
65 [IFACE_ATTR_IP6HINT
] = { .name
= "ip6hint", .type
= BLOBMSG_TYPE_STRING
},
66 [IFACE_ATTR_IP4TABLE
] = { .name
= "ip4table", .type
= BLOBMSG_TYPE_STRING
},
67 [IFACE_ATTR_IP6TABLE
] = { .name
= "ip6table", .type
= BLOBMSG_TYPE_STRING
},
68 [IFACE_ATTR_IP6CLASS
] = { .name
= "ip6class", .type
= BLOBMSG_TYPE_ARRAY
},
69 [IFACE_ATTR_DELEGATE
] = { .name
= "delegate", .type
= BLOBMSG_TYPE_BOOL
},
70 [IFACE_ATTR_IP6IFACEID
] = { .name
= "ip6ifaceid", .type
= BLOBMSG_TYPE_STRING
},
71 [IFACE_ATTR_FORCE_LINK
] = { .name
= "force_link", .type
= BLOBMSG_TYPE_BOOL
},
72 [IFACE_ATTR_IP6WEIGHT
] = { .name
= "ip6weight", .type
= BLOBMSG_TYPE_INT32
},
75 const struct uci_blob_param_list interface_attr_list
= {
76 .n_params
= IFACE_ATTR_MAX
,
77 .params
= iface_attrs
,
81 interface_set_main_dev(struct interface
*iface
, struct device
*dev
);
83 interface_event(struct interface
*iface
, enum interface_event ev
);
86 interface_error_flush(struct interface
*iface
)
88 struct interface_error
*error
, *tmp
;
90 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
91 list_del(&error
->list
);
97 interface_clear_errors(struct interface
*iface
)
99 /* don't flush the errors in case the configured protocol handler matches the
100 running protocol handler and is having the last error capability */
101 if (!(iface
->proto
&&
102 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
103 (iface
->proto
->handler
->name
== iface
->proto_handler
->name
)))
104 interface_error_flush(iface
);
107 void interface_add_error(struct interface
*iface
, const char *subsystem
,
108 const char *code
, const char **data
, int n_data
)
110 struct interface_error
*error
;
113 char *dest
, *d_subsys
, *d_code
;
115 /* if the configured protocol handler has the last error support capability,
116 errors should only be added if the running protocol handler matches the
119 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
120 (iface
->proto
->handler
->name
!= iface
->proto_handler
->name
))
124 len
= n_data
* sizeof(char *);
125 datalen
= alloca(len
);
126 for (i
= 0; i
< n_data
; i
++) {
127 datalen
[i
] = strlen(data
[i
]) + 1;
132 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
133 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
134 &d_code
, code
? strlen(code
) + 1 : 0);
138 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
139 protocol can't be established (e.g auth failure) */
140 if (iface
->proto_handler
->flags
& PROTO_FLAG_LASTERROR
)
141 interface_error_flush(iface
);
143 list_add_tail(&error
->list
, &iface
->errors
);
145 dest
= (char *) &error
->data
[n_data
+ 1];
146 for (i
= 0; i
< n_data
; i
++) {
147 error
->data
[i
] = dest
;
148 memcpy(dest
, data
[i
], datalen
[i
]);
151 error
->data
[n_data
] = NULL
;
154 error
->subsystem
= strcpy(d_subsys
, subsystem
);
157 error
->code
= strcpy(d_code
, code
);
161 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
163 avl_delete(&iface
->data
, &data
->node
);
168 interface_data_flush(struct interface
*iface
)
170 struct interface_data
*d
, *tmp
;
172 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
173 interface_data_del(iface
, d
);
177 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
179 struct interface_data
*n
, *o
;
181 if (!blobmsg_check_attr(data
, true))
182 return UBUS_STATUS_INVALID_ARGUMENT
;
184 const char *name
= blobmsg_name(data
);
185 unsigned len
= blob_pad_len(data
);
187 o
= avl_find_element(&iface
->data
, name
, o
, node
);
189 if (blob_pad_len(o
->data
) == len
&& !memcmp(o
->data
, data
, len
))
192 interface_data_del(iface
, o
);
195 n
= calloc(1, sizeof(*n
) + len
);
197 return UBUS_STATUS_UNKNOWN_ERROR
;
199 memcpy(n
->data
, data
, len
);
200 n
->node
.key
= blobmsg_name(n
->data
);
201 avl_insert(&iface
->data
, &n
->node
);
203 iface
->updated
|= IUF_DATA
;
207 int interface_parse_data(struct interface
*iface
, const struct blob_attr
*attr
)
209 struct blob_attr
*cur
;
214 blob_for_each_attr(cur
, attr
, rem
) {
215 ret
= interface_add_data(iface
, cur
);
220 if (iface
->updated
&& iface
->state
== IFS_UP
)
221 interface_event(iface
, IFEV_UPDATE
);
227 interface_event(struct interface
*iface
, enum interface_event ev
)
229 struct interface_user
*dep
, *tmp
;
230 struct device
*adev
= NULL
;
232 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
233 dep
->cb(dep
, iface
, ev
);
235 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
236 dep
->cb(dep
, iface
, ev
);
240 interface_error_flush(iface
);
241 adev
= iface
->l3_dev
.dev
;
245 alias_notify_device(iface
->name
, adev
);
253 interface_flush_state(struct interface
*iface
)
255 if (iface
->l3_dev
.dev
)
256 device_release(&iface
->l3_dev
);
257 interface_data_flush(iface
);
261 mark_interface_down(struct interface
*iface
)
263 enum interface_state state
= iface
->state
;
265 if (state
== IFS_DOWN
)
268 iface
->link_up_event
= false;
269 iface
->state
= IFS_DOWN
;
273 interface_event(iface
, IFEV_DOWN
);
276 interface_event(iface
, IFEV_UP_FAILED
);
281 interface_ip_set_enabled(&iface
->config_ip
, false);
282 interface_ip_set_enabled(&iface
->proto_ip
, false);
283 interface_ip_flush(&iface
->proto_ip
);
284 interface_flush_state(iface
);
285 system_flush_routes();
289 __set_config_state(struct interface
*iface
, enum interface_config_state s
)
291 iface
->config_state
= s
;
295 __interface_set_down(struct interface
*iface
, bool force
)
297 enum interface_state state
= iface
->state
;
301 iface
->state
= IFS_TEARDOWN
;
303 __set_config_state(iface
, IFC_REMOVE
);
306 interface_event(iface
, IFEV_DOWN
);
308 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
310 interface_flush_state(iface
);
314 if (iface
->main_dev
.dev
)
315 device_release(&iface
->main_dev
);
323 __interface_set_up(struct interface
*iface
)
327 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
329 iface
->state
= IFS_SETUP
;
330 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
332 mark_interface_down(iface
);
338 interface_check_state(struct interface
*iface
)
340 bool link_state
= iface
->link_state
|| iface
->force_link
;
342 switch (iface
->state
) {
345 if (!iface
->enabled
|| !link_state
) {
346 iface
->state
= IFS_TEARDOWN
;
348 __set_config_state(iface
, IFC_REMOVE
);
350 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
354 if (!iface
->available
)
357 if (iface
->autostart
&& iface
->enabled
&& link_state
&& !config_init
)
358 __interface_set_up(iface
);
366 interface_set_enabled(struct interface
*iface
, bool new_state
)
368 if (iface
->enabled
== new_state
)
371 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
372 iface
->enabled
= new_state
;
373 interface_check_state(iface
);
377 interface_set_link_state(struct interface
*iface
, bool new_state
)
379 if (iface
->link_state
== new_state
)
382 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
383 iface
->link_state
= new_state
;
384 interface_check_state(iface
);
386 if (new_state
&& iface
->force_link
&& iface
->state
== IFS_UP
&& !iface
->link_up_event
) {
387 interface_event(iface
, IFEV_LINK_UP
);
388 iface
->link_up_event
= true;
393 interface_ext_dev_cb(struct device_user
*dep
, enum device_event ev
)
395 if (ev
== DEV_EVENT_REMOVE
)
396 device_remove_user(dep
);
400 interface_main_dev_cb(struct device_user
*dep
, enum device_event ev
)
402 struct interface
*iface
;
404 iface
= container_of(dep
, struct interface
, main_dev
);
407 interface_set_available(iface
, true);
409 case DEV_EVENT_REMOVE
:
410 interface_set_available(iface
, false);
411 if (dep
->dev
&& dep
->dev
->external
)
412 interface_set_main_dev(iface
, NULL
);
415 interface_set_enabled(iface
, true);
418 interface_set_enabled(iface
, false);
420 case DEV_EVENT_LINK_UP
:
421 interface_set_link_state(iface
, true);
423 case DEV_EVENT_LINK_DOWN
:
424 interface_set_link_state(iface
, false);
426 case DEV_EVENT_TOPO_CHANGE
:
427 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
435 interface_l3_dev_cb(struct device_user
*dep
, enum device_event ev
)
437 struct interface
*iface
;
439 iface
= container_of(dep
, struct interface
, l3_dev
);
440 if (iface
->l3_dev
.dev
== iface
->main_dev
.dev
)
444 case DEV_EVENT_LINK_DOWN
:
445 if (iface
->proto_handler
->flags
& PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN
)
446 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
454 interface_set_available(struct interface
*iface
, bool new_state
)
456 if (iface
->available
== new_state
)
459 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
460 iface
->available
= new_state
;
463 if (iface
->autostart
&& !config_init
)
464 interface_set_up(iface
);
466 __interface_set_down(iface
, true);
470 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
473 list_add(&dep
->list
, &iface_all_users
);
478 list_add(&dep
->list
, &iface
->users
);
479 if (iface
->state
== IFS_UP
)
480 dep
->cb(dep
, iface
, IFEV_UP
);
484 interface_remove_user(struct interface_user
*dep
)
486 list_del_init(&dep
->list
);
491 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
493 struct blob_attr
*cur
;
496 blobmsg_for_each_attr(cur
, list
, rem
) {
497 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
500 if (!blobmsg_check_attr(cur
, false))
503 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
504 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
505 list_add(&c
->head
, &iface
->assignment_classes
);
510 interface_clear_assignment_classes(struct interface
*iface
)
512 while (!list_empty(&iface
->assignment_classes
)) {
513 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
514 struct interface_assignment_class
, head
);
521 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
523 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
524 old
->assignment_length
!= new->assignment_length
||
525 old
->assignment_iface_id_selection
!= new->assignment_iface_id_selection
||
526 old
->assignment_weight
!= new->assignment_weight
||
527 (old
->assignment_iface_id_selection
== IFID_FIXED
&&
528 memcmp(&old
->assignment_fixed_iface_id
, &new->assignment_fixed_iface_id
,
529 sizeof(old
->assignment_fixed_iface_id
))) ||
530 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
532 struct interface_assignment_class
*c
;
533 list_for_each_entry(c
, &new->assignment_classes
, head
) {
534 /* Compare list entries one-by-one to see if there was a change */
535 if (list_empty(&old
->assignment_classes
)) /* The new list is longer */
541 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
542 struct interface_assignment_class
, head
);
544 if (strcmp(c_old
->name
, c
->name
)) /* An entry didn't match */
547 list_del(&c_old
->head
);
551 /* The old list was longer than the new one or the last entry didn't match */
552 if (!list_empty(&old
->assignment_classes
)) {
553 interface_clear_assignment_classes(old
);
557 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
560 old
->assignment_hint
= new->assignment_hint
;
561 old
->assignment_length
= new->assignment_length
;
562 old
->assignment_iface_id_selection
= new->assignment_iface_id_selection
;
563 old
->assignment_fixed_iface_id
= new->assignment_fixed_iface_id
;
564 old
->assignment_weight
= new->assignment_weight
;
565 interface_refresh_assignments(true);
570 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
572 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
573 struct device
*dev
= iface
->l3_dev
.dev
;
580 interface_set_main_dev(alias
, dev
);
581 interface_set_available(alias
, true);
585 interface_set_available(alias
, false);
586 interface_set_main_dev(alias
, NULL
);
589 interface_remove_user(dep
);
597 interface_set_device_config(struct interface
*iface
, struct device
*dev
)
599 if (!dev
|| !dev
->default_config
)
602 if (!iface
->device_config
&&
603 (!dev
->iface_config
|| dev
->config_iface
!= iface
))
606 dev
->config_iface
= iface
;
607 dev
->iface_config
= iface
->device_config
;
608 device_apply_config(dev
, dev
->type
, iface
->config
);
612 interface_claim_device(struct interface
*iface
)
614 struct interface
*parent
;
615 struct device
*dev
= NULL
;
617 if (iface
->parent_iface
.iface
)
618 interface_remove_user(&iface
->parent_iface
);
622 if (iface
->parent_ifname
) {
623 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
624 iface
->parent_iface
.cb
= interface_alias_cb
;
625 interface_add_user(&iface
->parent_iface
, parent
);
626 } else if (iface
->ifname
&&
627 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
628 dev
= device_get(iface
->ifname
, true);
629 interface_set_device_config(iface
, dev
);
631 dev
= iface
->ext_dev
.dev
;
635 interface_set_main_dev(iface
, dev
);
639 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
640 interface_set_available(iface
, true);
644 interface_cleanup_state(struct interface
*iface
)
646 interface_set_available(iface
, false);
648 interface_flush_state(iface
);
649 interface_clear_errors(iface
);
650 interface_set_proto_state(iface
, NULL
);
652 interface_set_main_dev(iface
, NULL
);
653 interface_set_l3_dev(iface
, NULL
);
657 interface_cleanup(struct interface
*iface
)
659 struct interface_user
*dep
, *tmp
;
661 uloop_timeout_cancel(&iface
->remove_timer
);
662 device_remove_user(&iface
->ext_dev
);
664 if (iface
->parent_iface
.iface
)
665 interface_remove_user(&iface
->parent_iface
);
667 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
668 interface_remove_user(dep
);
670 interface_clear_assignment_classes(iface
);
671 interface_ip_flush(&iface
->config_ip
);
672 interface_cleanup_state(iface
);
676 interface_do_free(struct interface
*iface
)
678 interface_event(iface
, IFEV_FREE
);
679 interface_cleanup(iface
);
681 netifd_ubus_remove_interface(iface
);
682 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
687 interface_do_reload(struct interface
*iface
)
689 interface_event(iface
, IFEV_RELOAD
);
690 interface_cleanup_state(iface
);
691 proto_init_interface(iface
, iface
->config
);
692 interface_claim_device(iface
);
696 interface_handle_config_change(struct interface
*iface
)
698 enum interface_config_state state
= iface
->config_state
;
700 iface
->config_state
= IFC_NORMAL
;
705 interface_do_reload(iface
);
708 interface_do_free(iface
);
711 if (iface
->autostart
)
712 interface_set_up(iface
);
716 interface_proto_event_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
718 struct interface
*iface
= state
->iface
;
722 if (iface
->state
!= IFS_SETUP
) {
723 if (iface
->state
== IFS_UP
&& iface
->updated
)
724 interface_event(iface
, IFEV_UPDATE
);
728 if (!iface
->l3_dev
.dev
)
729 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
731 interface_ip_set_enabled(&iface
->config_ip
, true);
732 interface_ip_set_enabled(&iface
->proto_ip
, true);
733 system_flush_routes();
734 iface
->state
= IFS_UP
;
735 iface
->start_time
= system_get_rtime();
736 interface_event(iface
, IFEV_UP
);
737 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
740 if (iface
->state
== IFS_DOWN
)
743 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
744 mark_interface_down(iface
);
745 if (iface
->main_dev
.dev
)
746 device_release(&iface
->main_dev
);
747 if (iface
->l3_dev
.dev
)
748 device_remove_user(&iface
->l3_dev
);
749 interface_handle_config_change(iface
);
751 case IFPEV_LINK_LOST
:
752 if (iface
->state
!= IFS_UP
)
755 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
756 mark_interface_down(iface
);
757 iface
->state
= IFS_SETUP
;
763 interface_write_resolv_conf();
766 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
769 iface
->proto
->free(iface
->proto
);
772 iface
->state
= IFS_DOWN
;
773 iface
->proto
= state
;
777 state
->proto_event
= interface_proto_event_cb
;
778 state
->iface
= iface
;
782 interface_alloc(const char *name
, struct blob_attr
*config
, bool dynamic
)
784 struct interface
*iface
;
785 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
786 struct blob_attr
*cur
;
787 const char *proto_name
= NULL
;
789 bool force_link
= false;
791 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
792 iface
->name
= strcpy(iface_name
, name
);
793 INIT_LIST_HEAD(&iface
->errors
);
794 INIT_LIST_HEAD(&iface
->users
);
795 INIT_LIST_HEAD(&iface
->hotplug_list
);
796 INIT_LIST_HEAD(&iface
->assignment_classes
);
797 interface_ip_init(iface
);
798 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
799 iface
->config_ip
.enabled
= false;
801 iface
->main_dev
.cb
= interface_main_dev_cb
;
802 iface
->l3_dev
.cb
= interface_l3_dev_cb
;
803 iface
->ext_dev
.cb
= interface_ext_dev_cb
;
805 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
806 blob_data(config
), blob_len(config
));
808 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
809 proto_name
= blobmsg_data(cur
);
811 proto_attach_interface(iface
, proto_name
);
812 if (iface
->proto_handler
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
)
815 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
816 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], force_link
);
817 iface
->dynamic
= dynamic
;
818 iface
->proto_ip
.no_defaultroute
=
819 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
820 iface
->proto_ip
.no_dns
=
821 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
823 if ((cur
= tb
[IFACE_ATTR_DNS
]))
824 interface_add_dns_server_list(&iface
->config_ip
, cur
);
826 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
827 interface_add_dns_search_list(&iface
->config_ip
, cur
);
829 if ((cur
= tb
[IFACE_ATTR_DNS_METRIC
]))
830 iface
->dns_metric
= blobmsg_get_u32(cur
);
832 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
833 iface
->metric
= blobmsg_get_u32(cur
);
835 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
836 iface
->assignment_length
= blobmsg_get_u32(cur
);
839 iface
->assignment_iface_id_selection
= IFID_FIXED
;
840 iface
->assignment_fixed_iface_id
= in6addr_any
;
841 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
843 if ((cur
= tb
[IFACE_ATTR_IP6IFACEID
])) {
844 const char *ifaceid
= blobmsg_data(cur
);
845 if (!strcmp(ifaceid
, "random")) {
846 iface
->assignment_iface_id_selection
= IFID_RANDOM
;
848 else if (!strcmp(ifaceid
, "eui64")) {
849 iface
->assignment_iface_id_selection
= IFID_EUI64
;
852 /* we expect an IPv6 address with network id zero here -> fixed iface id
853 if we cannot parse -> revert to iface id 1 */
854 if (inet_pton(AF_INET6
,ifaceid
,&iface
->assignment_fixed_iface_id
) != 1 ||
855 iface
->assignment_fixed_iface_id
.s6_addr32
[0] != 0 ||
856 iface
->assignment_fixed_iface_id
.s6_addr32
[1] != 0) {
857 iface
->assignment_fixed_iface_id
= in6addr_any
;
858 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
859 netifd_log_message(L_WARNING
, "Failed to parse ip6ifaceid for interface '%s', \
860 falling back to iface id 1.\n", iface
->name
);
865 iface
->assignment_hint
= -1;
866 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
867 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
868 ~((1 << (64 - iface
->assignment_length
)) - 1);
870 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
871 interface_add_assignment_classes(iface
, cur
);
873 if ((cur
= tb
[IFACE_ATTR_IP6WEIGHT
]))
874 iface
->assignment_weight
= blobmsg_get_u32(cur
);
876 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
877 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
878 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
881 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
882 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
883 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
886 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
888 iface
->config_autostart
= iface
->autostart
;
892 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
894 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
895 struct blob_attr
*cur
;
898 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
899 blob_data(config
), blob_len(config
));
902 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
903 iface
->parent_ifname
= blobmsg_data(cur
);
905 if (!iface
->parent_ifname
)
908 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
909 iface
->ifname
= blobmsg_data(cur
);
912 if (iface
->dynamic
) {
913 name
= strdup(iface
->name
);
919 iface
->config
= config
;
920 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
923 iface
= vlist_find(&interfaces
, name
, iface
, node
);
926 /* Don't delete dynamic interface on reload */
928 iface
->node
.version
= -1;
935 interface_add(struct interface
*iface
, struct blob_attr
*config
)
937 return __interface_add(iface
, config
, false);
941 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
943 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
946 return __interface_add(iface
, config
, true);
950 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
952 bool enabled
= iface
->config_ip
.enabled
;
953 bool claimed
= iface
->l3_dev
.claimed
;
955 if (iface
->l3_dev
.dev
== dev
)
958 interface_ip_set_enabled(&iface
->config_ip
, false);
959 interface_ip_set_enabled(&iface
->proto_ip
, false);
960 interface_ip_flush(&iface
->proto_ip
);
961 device_add_user(&iface
->l3_dev
, dev
);
965 if (device_claim(&iface
->l3_dev
) < 0)
968 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
969 interface_ip_set_enabled(&iface
->proto_ip
, enabled
);
974 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
976 bool claimed
= iface
->l3_dev
.claimed
;
978 if (iface
->main_dev
.dev
== dev
)
981 interface_set_available(iface
, false);
982 device_add_user(&iface
->main_dev
, dev
);
984 interface_set_link_state(iface
, false);
989 if (device_claim(&iface
->l3_dev
) < 0)
993 if (!iface
->l3_dev
.dev
)
994 interface_set_l3_dev(iface
, dev
);
998 interface_remove_link(struct interface
*iface
, struct device
*dev
)
1000 struct device
*mdev
= iface
->main_dev
.dev
;
1002 if (mdev
&& mdev
->hotplug_ops
)
1003 return mdev
->hotplug_ops
->del(mdev
, dev
);
1005 if (dev
== iface
->ext_dev
.dev
)
1006 device_remove_user(&iface
->ext_dev
);
1008 if (!iface
->main_dev
.hotplug
)
1009 return UBUS_STATUS_INVALID_ARGUMENT
;
1011 if (dev
!= iface
->main_dev
.dev
)
1012 return UBUS_STATUS_INVALID_ARGUMENT
;
1014 interface_set_main_dev(iface
, NULL
);
1019 interface_add_link(struct interface
*iface
, struct device
*dev
, bool link_ext
)
1021 struct device
*mdev
= iface
->main_dev
.dev
;
1026 if (iface
->main_dev
.hotplug
)
1027 device_remove_user(&iface
->main_dev
);
1030 if (mdev
->hotplug_ops
)
1031 return mdev
->hotplug_ops
->add(mdev
, dev
);
1033 return UBUS_STATUS_NOT_SUPPORTED
;
1037 device_add_user(&iface
->ext_dev
, dev
);
1039 interface_set_main_dev(iface
, dev
);
1040 iface
->main_dev
.hotplug
= true;
1045 interface_handle_link(struct interface
*iface
, const char *name
, bool add
, bool link_ext
)
1052 dev
= device_get(name
, add
? (link_ext
? 2 : 1) : 0);
1054 ret
= UBUS_STATUS_NOT_FOUND
;
1059 interface_set_device_config(iface
, dev
);
1060 device_set_present(dev
, true);
1062 ret
= interface_add_link(iface
, dev
, link_ext
);
1064 ret
= interface_remove_link(iface
, dev
);
1074 interface_set_up(struct interface
*iface
)
1077 const char *error
= NULL
;
1079 iface
->autostart
= true;
1081 if (iface
->state
!= IFS_DOWN
)
1084 interface_clear_errors(iface
);
1085 if (iface
->available
) {
1086 if (iface
->main_dev
.dev
) {
1087 ret
= device_claim(&iface
->main_dev
);
1089 interface_check_state(iface
);
1091 error
= "DEVICE_CLAIM_FAILED";
1093 ret
= __interface_set_up(iface
);
1095 error
= "SETUP_FAILED";
1098 error
= "NO_DEVICE";
1101 interface_add_error(iface
, "interface", error
, NULL
, 0);
1105 interface_set_down(struct interface
*iface
)
1108 vlist_for_each_element(&interfaces
, iface
, node
)
1109 __interface_set_down(iface
, false);
1111 iface
->autostart
= false;
1112 __interface_set_down(iface
, false);
1117 interface_renew(struct interface
*iface
)
1119 if (iface
->state
== IFS_TEARDOWN
|| iface
->state
== IFS_DOWN
)
1122 return interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
1126 interface_start_pending(void)
1128 struct interface
*iface
;
1130 vlist_for_each_element(&interfaces
, iface
, node
) {
1131 if (iface
->autostart
)
1132 interface_set_up(iface
);
1137 set_config_state(struct interface
*iface
, enum interface_config_state s
)
1139 __set_config_state(iface
, s
);
1140 if (iface
->state
== IFS_DOWN
)
1141 interface_handle_config_change(iface
);
1143 __interface_set_down(iface
, false);
1147 interface_update_start(struct interface
*iface
, const bool keep_old
)
1152 interface_ip_update_start(&iface
->proto_ip
);
1156 interface_update_complete(struct interface
*iface
)
1158 interface_ip_update_complete(&iface
->proto_ip
);
1162 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
1164 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
1165 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
1169 interface_device_config_changed(struct interface
*if_old
, struct interface
*if_new
)
1171 struct blob_attr
*ntb
[__DEV_ATTR_MAX
];
1172 struct blob_attr
*otb
[__DEV_ATTR_MAX
];
1173 struct device
*dev
= if_old
->main_dev
.dev
;
1174 unsigned long diff
= 0;
1176 BUILD_BUG_ON(sizeof(diff
) < __DEV_ATTR_MAX
/ 8);
1181 if (if_old
->device_config
!= if_new
->device_config
)
1184 if (!if_new
->device_config
)
1187 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb
,
1188 blob_data(if_old
->config
), blob_len(if_old
->config
));
1190 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, ntb
,
1191 blob_data(if_new
->config
), blob_len(if_new
->config
));
1193 uci_blob_diff(ntb
, otb
, &device_attr_list
, &diff
);
1198 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
1200 struct blob_attr
*old_config
= if_old
->config
;
1201 bool reload
= false, reload_ip
= false, update_prefix_delegation
= false;
1203 #define FIELD_CHANGED_STR(field) \
1204 ((!!if_old->field != !!if_new->field) || \
1206 strcmp(if_old->field, if_new->field) != 0))
1208 if (FIELD_CHANGED_STR(parent_ifname
)) {
1209 if (if_old
->parent_iface
.iface
)
1210 interface_remove_user(&if_old
->parent_iface
);
1214 if (!reload
&& interface_device_config_changed(if_old
, if_new
))
1217 if (FIELD_CHANGED_STR(ifname
) ||
1218 if_old
->proto_handler
!= if_new
->proto_handler
)
1221 if (!if_old
->proto_handler
->config_params
)
1222 D(INTERFACE
, "No config parameters for interface '%s'\n",
1224 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
1225 if_old
->proto_handler
->config_params
))
1228 #define UPDATE(field, __var) ({ \
1229 bool __changed = (if_old->field != if_new->field); \
1230 if_old->field = if_new->field; \
1231 __var |= __changed; \
1234 if_old
->config
= if_new
->config
;
1235 if (if_old
->config_autostart
!= if_new
->config_autostart
) {
1236 if (if_old
->config_autostart
)
1239 if_old
->autostart
= if_new
->config_autostart
;
1242 if_old
->device_config
= if_new
->device_config
;
1243 if_old
->config_autostart
= if_new
->config_autostart
;
1244 if_old
->ifname
= if_new
->ifname
;
1245 if_old
->parent_ifname
= if_new
->parent_ifname
;
1246 if_old
->dynamic
= if_new
->dynamic
;
1247 if_old
->proto_handler
= if_new
->proto_handler
;
1248 if_old
->force_link
= if_new
->force_link
;
1249 if_old
->dns_metric
= if_new
->dns_metric
;
1251 if (if_old
->proto_ip
.no_delegation
!= if_new
->proto_ip
.no_delegation
) {
1252 if_old
->proto_ip
.no_delegation
= if_new
->proto_ip
.no_delegation
;
1253 update_prefix_delegation
= true;
1256 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1257 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1259 UPDATE(metric
, reload_ip
);
1260 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1261 UPDATE(ip4table
, reload_ip
);
1262 UPDATE(ip6table
, reload_ip
);
1263 interface_merge_assignment_data(if_old
, if_new
);
1268 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1270 interface_clear_errors(if_old
);
1271 set_config_state(if_old
, IFC_RELOAD
);
1276 bool config_ip_enabled
= if_old
->config_ip
.enabled
;
1277 bool proto_ip_enabled
= if_old
->proto_ip
.enabled
;
1279 interface_ip_set_enabled(&if_old
->config_ip
, false);
1280 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1281 interface_ip_set_enabled(&if_old
->proto_ip
, proto_ip_enabled
);
1282 interface_ip_set_enabled(&if_old
->config_ip
, config_ip_enabled
);
1285 if (update_prefix_delegation
)
1286 interface_update_prefix_delegation(&if_old
->proto_ip
);
1288 interface_write_resolv_conf();
1289 if (if_old
->main_dev
.dev
)
1290 interface_check_state(if_old
);
1293 if_new
->config
= NULL
;
1294 interface_cleanup(if_new
);
1300 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1301 struct vlist_node
*node_old
)
1303 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1304 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1306 if (node_old
&& node_new
) {
1307 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1308 interface_change_config(if_old
, if_new
);
1309 } else if (node_old
) {
1310 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1311 set_config_state(if_old
, IFC_REMOVE
);
1312 } else if (node_new
) {
1313 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1314 interface_event(if_new
, IFEV_CREATE
);
1315 proto_init_interface(if_new
, if_new
->config
);
1316 interface_claim_device(if_new
);
1317 netifd_ubus_add_interface(if_new
);
1323 interface_init_list(void)
1325 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1326 interfaces
.keep_old
= true;
1327 interfaces
.no_delete
= true;