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
,
52 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
53 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
54 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
55 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
56 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
57 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
58 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
59 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
60 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
61 [IFACE_ATTR_DNS_METRIC
] = { .name
= "dns_metric", .type
= BLOBMSG_TYPE_INT32
},
62 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
63 [IFACE_ATTR_IP6ASSIGN
] = { .name
= "ip6assign", .type
= BLOBMSG_TYPE_INT32
},
64 [IFACE_ATTR_IP6HINT
] = { .name
= "ip6hint", .type
= BLOBMSG_TYPE_STRING
},
65 [IFACE_ATTR_IP4TABLE
] = { .name
= "ip4table", .type
= BLOBMSG_TYPE_STRING
},
66 [IFACE_ATTR_IP6TABLE
] = { .name
= "ip6table", .type
= BLOBMSG_TYPE_STRING
},
67 [IFACE_ATTR_IP6CLASS
] = { .name
= "ip6class", .type
= BLOBMSG_TYPE_ARRAY
},
68 [IFACE_ATTR_DELEGATE
] = { .name
= "delegate", .type
= BLOBMSG_TYPE_BOOL
},
69 [IFACE_ATTR_IP6IFACEID
] = { .name
= "ip6ifaceid", .type
= BLOBMSG_TYPE_STRING
},
70 [IFACE_ATTR_FORCE_LINK
] = { .name
= "force_link", .type
= BLOBMSG_TYPE_BOOL
},
73 const struct uci_blob_param_list interface_attr_list
= {
74 .n_params
= IFACE_ATTR_MAX
,
75 .params
= iface_attrs
,
79 set_config_state(struct interface
*iface
, enum interface_config_state s
);
81 interface_event(struct interface
*iface
, enum interface_event ev
);
84 interface_error_flush(struct interface
*iface
)
86 struct interface_error
*error
, *tmp
;
88 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
89 list_del(&error
->list
);
95 interface_clear_errors(struct interface
*iface
)
97 /* don't flush the errors in case the configured protocol handler matches the
98 running protocol handler and is having the last error capability */
100 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
101 (iface
->proto
->handler
->name
== iface
->proto_handler
->name
)))
102 interface_error_flush(iface
);
105 void interface_add_error(struct interface
*iface
, const char *subsystem
,
106 const char *code
, const char **data
, int n_data
)
108 struct interface_error
*error
;
111 char *dest
, *d_subsys
, *d_code
;
113 /* if the configured protocol handler has the last error support capability,
114 errors should only be added if the running protocol handler matches the
117 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
118 (iface
->proto
->handler
->name
!= iface
->proto_handler
->name
))
122 len
= n_data
* sizeof(char *);
123 datalen
= alloca(len
);
124 for (i
= 0; i
< n_data
; i
++) {
125 datalen
[i
] = strlen(data
[i
]) + 1;
130 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
131 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
132 &d_code
, code
? strlen(code
) + 1 : 0);
136 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
137 protocol can't be established (e.g auth failure) */
138 if (iface
->proto_handler
->flags
& PROTO_FLAG_LASTERROR
)
139 interface_error_flush(iface
);
141 list_add_tail(&error
->list
, &iface
->errors
);
143 dest
= (char *) &error
->data
[n_data
+ 1];
144 for (i
= 0; i
< n_data
; i
++) {
145 error
->data
[i
] = dest
;
146 memcpy(dest
, data
[i
], datalen
[i
]);
149 error
->data
[n_data
] = NULL
;
152 error
->subsystem
= strcpy(d_subsys
, subsystem
);
155 error
->code
= strcpy(d_code
, code
);
159 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
161 avl_delete(&iface
->data
, &data
->node
);
166 interface_data_flush(struct interface
*iface
)
168 struct interface_data
*d
, *tmp
;
170 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
171 interface_data_del(iface
, d
);
175 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
177 struct interface_data
*n
, *o
;
179 if (!blobmsg_check_attr(data
, true))
180 return UBUS_STATUS_INVALID_ARGUMENT
;
182 const char *name
= blobmsg_name(data
);
183 unsigned len
= blob_pad_len(data
);
185 o
= avl_find_element(&iface
->data
, name
, o
, node
);
187 if (blob_pad_len(o
->data
) == len
&& !memcmp(o
->data
, data
, len
))
190 interface_data_del(iface
, o
);
193 n
= calloc(1, sizeof(*n
) + len
);
195 return UBUS_STATUS_UNKNOWN_ERROR
;
197 memcpy(n
->data
, data
, len
);
198 n
->node
.key
= blobmsg_name(n
->data
);
199 avl_insert(&iface
->data
, &n
->node
);
201 iface
->updated
|= IUF_DATA
;
205 int interface_parse_data(struct interface
*iface
, const struct blob_attr
*attr
)
207 struct blob_attr
*cur
;
212 blob_for_each_attr(cur
, attr
, rem
) {
213 ret
= interface_add_data(iface
, cur
);
218 if (iface
->updated
&& iface
->state
== IFS_UP
)
219 interface_event(iface
, IFEV_UPDATE
);
225 interface_event(struct interface
*iface
, enum interface_event ev
)
227 struct interface_user
*dep
, *tmp
;
228 struct device
*adev
= NULL
;
230 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
231 dep
->cb(dep
, iface
, ev
);
233 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
234 dep
->cb(dep
, iface
, ev
);
238 interface_error_flush(iface
);
239 adev
= iface
->l3_dev
.dev
;
242 alias_notify_device(iface
->name
, adev
);
250 interface_flush_state(struct interface
*iface
)
252 if (iface
->l3_dev
.dev
)
253 device_release(&iface
->l3_dev
);
254 interface_data_flush(iface
);
258 mark_interface_down(struct interface
*iface
)
260 enum interface_state state
= iface
->state
;
262 if (state
== IFS_DOWN
)
265 iface
->link_up_event
= false;
266 iface
->state
= IFS_DOWN
;
268 interface_event(iface
, IFEV_DOWN
);
269 interface_ip_set_enabled(&iface
->config_ip
, false);
270 interface_ip_set_enabled(&iface
->proto_ip
, false);
271 interface_ip_flush(&iface
->proto_ip
);
272 interface_flush_state(iface
);
273 system_flush_routes();
277 __interface_set_down(struct interface
*iface
, bool force
)
279 enum interface_state state
= iface
->state
;
283 iface
->state
= IFS_TEARDOWN
;
285 interface_event(iface
, IFEV_DOWN
);
287 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
289 interface_flush_state(iface
);
293 if (iface
->main_dev
.dev
)
294 device_release(&iface
->main_dev
);
302 __interface_set_up(struct interface
*iface
)
306 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
308 iface
->state
= IFS_SETUP
;
309 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
311 mark_interface_down(iface
);
317 interface_check_state(struct interface
*iface
)
319 bool link_state
= iface
->link_state
|| iface
->force_link
;
321 switch (iface
->state
) {
324 if (!iface
->enabled
|| !link_state
) {
325 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
326 mark_interface_down(iface
);
330 if (!iface
->available
)
333 if (iface
->autostart
&& iface
->enabled
&& link_state
&& !config_init
)
334 __interface_set_up(iface
);
342 interface_set_enabled(struct interface
*iface
, bool new_state
)
344 if (iface
->enabled
== new_state
)
347 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
348 iface
->enabled
= new_state
;
349 interface_check_state(iface
);
353 interface_set_link_state(struct interface
*iface
, bool new_state
)
355 if (iface
->link_state
== new_state
)
358 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
359 iface
->link_state
= new_state
;
360 interface_check_state(iface
);
362 if (new_state
&& iface
->force_link
&& iface
->state
== IFS_UP
&& !iface
->link_up_event
) {
363 interface_event(iface
, IFEV_LINK_UP
);
364 iface
->link_up_event
= true;
369 interface_ext_dev_cb(struct device_user
*dep
, enum device_event ev
)
371 if (ev
== DEV_EVENT_REMOVE
)
372 device_remove_user(dep
);
376 interface_main_dev_cb(struct device_user
*dep
, enum device_event ev
)
378 struct interface
*iface
;
379 bool new_state
= false;
381 iface
= container_of(dep
, struct interface
, main_dev
);
385 case DEV_EVENT_REMOVE
:
386 interface_set_available(iface
, new_state
);
387 if (!new_state
&& dep
->dev
&& dep
->dev
->external
)
388 interface_set_main_dev(iface
, NULL
);
393 interface_set_enabled(iface
, new_state
);
395 case DEV_EVENT_LINK_UP
:
397 case DEV_EVENT_LINK_DOWN
:
398 interface_set_link_state(iface
, new_state
);
400 case DEV_EVENT_TOPO_CHANGE
:
401 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
409 interface_l3_dev_cb(struct device_user
*dep
, enum device_event ev
)
411 struct interface
*iface
;
413 iface
= container_of(dep
, struct interface
, l3_dev
);
414 if (iface
->l3_dev
.dev
== iface
->main_dev
.dev
)
418 case DEV_EVENT_LINK_DOWN
:
419 if (iface
->proto_handler
->flags
& PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN
)
420 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
428 interface_set_available(struct interface
*iface
, bool new_state
)
430 if (iface
->available
== new_state
)
433 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
434 iface
->available
= new_state
;
437 if (iface
->autostart
&& !config_init
)
438 interface_set_up(iface
);
440 __interface_set_down(iface
, true);
444 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
447 list_add(&dep
->list
, &iface_all_users
);
452 list_add(&dep
->list
, &iface
->users
);
453 if (iface
->state
== IFS_UP
)
454 dep
->cb(dep
, iface
, IFEV_UP
);
458 interface_remove_user(struct interface_user
*dep
)
460 list_del_init(&dep
->list
);
465 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
467 struct blob_attr
*cur
;
470 blobmsg_for_each_attr(cur
, list
, rem
) {
471 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
474 if (!blobmsg_check_attr(cur
, NULL
))
477 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
478 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
479 list_add(&c
->head
, &iface
->assignment_classes
);
484 interface_clear_assignment_classes(struct interface
*iface
)
486 while (!list_empty(&iface
->assignment_classes
)) {
487 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
488 struct interface_assignment_class
, head
);
495 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
497 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
498 old
->assignment_length
!= new->assignment_length
||
499 old
->assignment_iface_id_selection
!= new->assignment_iface_id_selection
||
500 (old
->assignment_iface_id_selection
== IFID_FIXED
&&
501 memcmp(&old
->assignment_fixed_iface_id
, &new->assignment_fixed_iface_id
,
502 sizeof(old
->assignment_fixed_iface_id
))) ||
503 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
505 struct interface_assignment_class
*c
;
506 list_for_each_entry(c
, &new->assignment_classes
, head
) {
507 // Compare list entries one-by-one to see if there was a change
508 if (list_empty(&old
->assignment_classes
)) // The new list is longer
514 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
515 struct interface_assignment_class
, head
);
517 if (strcmp(c_old
->name
, c
->name
)) // An entry didn't match
520 list_del(&c_old
->head
);
524 // The old list was longer than the new one or the last entry didn't match
525 if (!list_empty(&old
->assignment_classes
)) {
526 interface_clear_assignment_classes(old
);
530 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
533 old
->assignment_hint
= new->assignment_hint
;
534 old
->assignment_length
= new->assignment_length
;
535 old
->assignment_iface_id_selection
= new->assignment_iface_id_selection
;
536 old
->assignment_fixed_iface_id
= new->assignment_fixed_iface_id
;
537 interface_refresh_assignments(true);
542 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
544 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
545 struct device
*dev
= iface
->l3_dev
.dev
;
552 interface_set_main_dev(alias
, dev
);
553 interface_set_available(alias
, true);
556 interface_set_available(alias
, false);
557 interface_set_main_dev(alias
, NULL
);
560 interface_remove_user(dep
);
568 interface_set_device_config(struct interface
*iface
, struct device
*dev
)
570 if (!dev
|| !dev
->default_config
)
573 if (!iface
->device_config
&&
574 (!dev
->iface_config
|| dev
->config_iface
!= iface
))
577 dev
->config_iface
= iface
;
578 dev
->iface_config
= iface
->device_config
;
579 device_apply_config(dev
, dev
->type
, iface
->config
);
583 interface_claim_device(struct interface
*iface
)
585 struct interface
*parent
;
586 struct device
*dev
= NULL
;
588 if (iface
->parent_iface
.iface
)
589 interface_remove_user(&iface
->parent_iface
);
593 if (iface
->parent_ifname
) {
594 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
595 iface
->parent_iface
.cb
= interface_alias_cb
;
596 interface_add_user(&iface
->parent_iface
, parent
);
597 } else if (iface
->ifname
&&
598 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
599 dev
= device_get(iface
->ifname
, true);
600 interface_set_device_config(iface
, dev
);
602 dev
= iface
->ext_dev
.dev
;
606 interface_set_main_dev(iface
, dev
);
610 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
611 interface_set_available(iface
, true);
615 interface_cleanup_state(struct interface
*iface
)
617 interface_set_available(iface
, false);
619 interface_flush_state(iface
);
620 interface_clear_errors(iface
);
621 interface_set_proto_state(iface
, NULL
);
623 interface_set_main_dev(iface
, NULL
);
624 interface_set_l3_dev(iface
, NULL
);
628 interface_cleanup(struct interface
*iface
)
630 struct interface_user
*dep
, *tmp
;
632 uloop_timeout_cancel(&iface
->remove_timer
);
633 device_remove_user(&iface
->ext_dev
);
635 if (iface
->parent_iface
.iface
)
636 interface_remove_user(&iface
->parent_iface
);
638 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
639 interface_remove_user(dep
);
641 interface_clear_assignment_classes(iface
);
642 interface_ip_flush(&iface
->config_ip
);
643 interface_cleanup_state(iface
);
647 interface_do_free(struct interface
*iface
)
649 interface_event(iface
, IFEV_FREE
);
650 interface_cleanup(iface
);
652 netifd_ubus_remove_interface(iface
);
653 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
658 interface_do_reload(struct interface
*iface
)
660 interface_event(iface
, IFEV_RELOAD
);
661 interface_cleanup_state(iface
);
662 proto_init_interface(iface
, iface
->config
);
663 interface_claim_device(iface
);
667 interface_handle_config_change(struct interface
*iface
)
669 enum interface_config_state state
= iface
->config_state
;
671 iface
->config_state
= IFC_NORMAL
;
676 interface_do_reload(iface
);
679 interface_do_free(iface
);
682 if (iface
->autostart
&& iface
->available
)
683 interface_set_up(iface
);
684 else if (iface
->dynamic
)
685 set_config_state(iface
, IFC_REMOVE
);
689 interface_proto_event_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
691 struct interface
*iface
= state
->iface
;
695 if (iface
->state
!= IFS_SETUP
) {
696 interface_event(iface
, IFEV_UPDATE
);
700 if (!iface
->l3_dev
.dev
)
701 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
703 interface_ip_set_enabled(&iface
->config_ip
, true);
704 interface_ip_set_enabled(&iface
->proto_ip
, true);
705 system_flush_routes();
706 iface
->state
= IFS_UP
;
707 iface
->start_time
= system_get_rtime();
708 interface_event(iface
, IFEV_UP
);
709 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
712 if (iface
->state
== IFS_DOWN
)
715 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
716 mark_interface_down(iface
);
717 if (iface
->main_dev
.dev
)
718 device_release(&iface
->main_dev
);
719 if (iface
->l3_dev
.dev
)
720 device_remove_user(&iface
->l3_dev
);
721 interface_handle_config_change(iface
);
723 case IFPEV_LINK_LOST
:
724 if (iface
->state
!= IFS_UP
)
727 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
728 mark_interface_down(iface
);
729 iface
->state
= IFS_SETUP
;
735 interface_write_resolv_conf();
738 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
741 iface
->proto
->free(iface
->proto
);
744 iface
->state
= IFS_DOWN
;
745 iface
->proto
= state
;
749 state
->proto_event
= interface_proto_event_cb
;
750 state
->iface
= iface
;
754 interface_alloc(const char *name
, struct blob_attr
*config
)
756 struct interface
*iface
;
757 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
758 struct blob_attr
*cur
;
759 const char *proto_name
= NULL
;
761 bool force_link
= false;
763 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
764 iface
->name
= strcpy(iface_name
, name
);
765 INIT_LIST_HEAD(&iface
->errors
);
766 INIT_LIST_HEAD(&iface
->users
);
767 INIT_LIST_HEAD(&iface
->hotplug_list
);
768 INIT_LIST_HEAD(&iface
->assignment_classes
);
769 interface_ip_init(iface
);
770 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
771 iface
->config_ip
.enabled
= false;
773 iface
->main_dev
.cb
= interface_main_dev_cb
;
774 iface
->l3_dev
.cb
= interface_l3_dev_cb
;
775 iface
->ext_dev
.cb
= interface_ext_dev_cb
;
777 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
778 blob_data(config
), blob_len(config
));
780 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
781 proto_name
= blobmsg_data(cur
);
783 proto_attach_interface(iface
, proto_name
);
784 if (iface
->proto_handler
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
)
787 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
788 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], force_link
);
789 iface
->proto_ip
.no_defaultroute
=
790 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
791 iface
->proto_ip
.no_dns
=
792 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
794 if ((cur
= tb
[IFACE_ATTR_DNS
]))
795 interface_add_dns_server_list(&iface
->config_ip
, cur
);
797 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
798 interface_add_dns_search_list(&iface
->config_ip
, cur
);
800 if ((cur
= tb
[IFACE_ATTR_DNS_METRIC
]))
801 iface
->dns_metric
= blobmsg_get_u32(cur
);
803 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
804 iface
->metric
= blobmsg_get_u32(cur
);
806 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
807 iface
->assignment_length
= blobmsg_get_u32(cur
);
810 iface
->assignment_iface_id_selection
= IFID_FIXED
;
811 iface
->assignment_fixed_iface_id
= in6addr_any
;
812 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
814 if ((cur
= tb
[IFACE_ATTR_IP6IFACEID
])) {
815 const char *ifaceid
= blobmsg_data(cur
);
816 if (!strcmp(ifaceid
, "random")) {
817 iface
->assignment_iface_id_selection
= IFID_RANDOM
;
819 else if (!strcmp(ifaceid
, "eui64")) {
820 iface
->assignment_iface_id_selection
= IFID_EUI64
;
823 /* we expect an IPv6 address with network id zero here -> fixed iface id
824 if we cannot parse -> revert to iface id 1 */
825 if (inet_pton(AF_INET6
,ifaceid
,&iface
->assignment_fixed_iface_id
) != 1 ||
826 iface
->assignment_fixed_iface_id
.s6_addr32
[0] != 0 ||
827 iface
->assignment_fixed_iface_id
.s6_addr32
[1] != 0) {
828 iface
->assignment_fixed_iface_id
= in6addr_any
;
829 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
830 netifd_log_message(L_WARNING
, "Failed to parse ip6ifaceid for interface '%s', \
831 falling back to iface id 1.\n", iface
->name
);
836 iface
->assignment_hint
= -1;
837 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
838 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
839 ~((1 << (64 - iface
->assignment_length
)) - 1);
841 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
842 interface_add_assignment_classes(iface
, cur
);
845 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
846 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
847 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
850 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
851 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
852 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
855 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
857 iface
->config_autostart
= iface
->autostart
;
861 void interface_set_dynamic(struct interface
*iface
)
863 iface
->dynamic
= true;
864 iface
->autostart
= true;
865 iface
->node
.version
= -1; // Don't delete on reload
868 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
870 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
871 struct blob_attr
*cur
;
873 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
874 blob_data(config
), blob_len(config
));
877 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
878 iface
->parent_ifname
= blobmsg_data(cur
);
880 if (!iface
->parent_ifname
)
883 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
884 iface
->ifname
= blobmsg_data(cur
);
887 iface
->config
= config
;
888 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
893 interface_add(struct interface
*iface
, struct blob_attr
*config
)
895 __interface_add(iface
, config
, false);
899 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
901 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
904 return __interface_add(iface
, config
, true);
908 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
910 bool enabled
= iface
->config_ip
.enabled
;
911 bool claimed
= iface
->l3_dev
.claimed
;
913 if (iface
->l3_dev
.dev
== dev
)
916 interface_ip_set_enabled(&iface
->config_ip
, false);
917 interface_ip_set_enabled(&iface
->proto_ip
, false);
918 interface_ip_flush(&iface
->proto_ip
);
919 device_add_user(&iface
->l3_dev
, dev
);
923 if (device_claim(&iface
->l3_dev
) < 0)
926 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
927 interface_ip_set_enabled(&iface
->proto_ip
, enabled
);
932 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
934 bool claimed
= iface
->l3_dev
.claimed
;
936 if (iface
->main_dev
.dev
== dev
)
939 interface_set_available(iface
, false);
940 device_add_user(&iface
->main_dev
, dev
);
942 interface_set_link_state(iface
, false);
947 if (device_claim(&iface
->l3_dev
) < 0)
951 if (!iface
->l3_dev
.dev
)
952 interface_set_l3_dev(iface
, dev
);
956 interface_remove_link(struct interface
*iface
, struct device
*dev
)
958 struct device
*mdev
= iface
->main_dev
.dev
;
960 if (mdev
&& mdev
->hotplug_ops
)
961 return mdev
->hotplug_ops
->del(mdev
, dev
);
963 if (dev
== iface
->ext_dev
.dev
)
964 device_remove_user(&iface
->ext_dev
);
966 if (!iface
->main_dev
.hotplug
)
967 return UBUS_STATUS_INVALID_ARGUMENT
;
969 if (dev
!= iface
->main_dev
.dev
)
970 return UBUS_STATUS_INVALID_ARGUMENT
;
972 interface_set_main_dev(iface
, NULL
);
977 interface_add_link(struct interface
*iface
, struct device
*dev
, bool link_ext
)
979 struct device
*mdev
= iface
->main_dev
.dev
;
984 if (iface
->main_dev
.hotplug
)
985 device_remove_user(&iface
->main_dev
);
988 if (mdev
->hotplug_ops
)
989 return mdev
->hotplug_ops
->add(mdev
, dev
);
991 return UBUS_STATUS_NOT_SUPPORTED
;
995 device_add_user(&iface
->ext_dev
, dev
);
997 interface_set_main_dev(iface
, dev
);
998 iface
->main_dev
.hotplug
= true;
1003 interface_handle_link(struct interface
*iface
, const char *name
, bool add
, bool link_ext
)
1010 dev
= device_get(name
, add
? (link_ext
? 2 : 1) : 0);
1012 ret
= UBUS_STATUS_NOT_FOUND
;
1017 interface_set_device_config(iface
, dev
);
1018 device_set_present(dev
, true);
1020 ret
= interface_add_link(iface
, dev
, link_ext
);
1022 ret
= interface_remove_link(iface
, dev
);
1032 interface_set_up(struct interface
*iface
)
1036 iface
->autostart
= true;
1038 if (iface
->state
!= IFS_DOWN
)
1041 interface_clear_errors(iface
);
1042 if (!iface
->available
) {
1043 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
1047 if (iface
->main_dev
.dev
) {
1048 ret
= device_claim(&iface
->main_dev
);
1050 interface_check_state(iface
);
1053 ret
= __interface_set_up(iface
);
1059 interface_set_down(struct interface
*iface
)
1062 vlist_for_each_element(&interfaces
, iface
, node
)
1063 __interface_set_down(iface
, false);
1065 iface
->autostart
= false;
1066 __interface_set_down(iface
, false);
1073 interface_start_pending(void)
1075 struct interface
*iface
;
1077 vlist_for_each_element(&interfaces
, iface
, node
) {
1078 if (iface
->available
&& iface
->autostart
)
1079 interface_set_up(iface
);
1084 set_config_state(struct interface
*iface
, enum interface_config_state s
)
1086 iface
->config_state
= s
;
1087 if (iface
->state
== IFS_DOWN
)
1088 interface_handle_config_change(iface
);
1090 __interface_set_down(iface
, false);
1094 interface_update_start(struct interface
*iface
)
1097 interface_ip_update_start(&iface
->proto_ip
);
1101 interface_update_complete(struct interface
*iface
)
1103 interface_ip_update_complete(&iface
->proto_ip
);
1107 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
1109 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
1110 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
1114 interface_device_config_changed(struct interface
*if_old
, struct interface
*if_new
)
1116 struct blob_attr
*ntb
[__DEV_ATTR_MAX
];
1117 struct blob_attr
*otb
[__DEV_ATTR_MAX
];
1118 struct device
*dev
= if_old
->main_dev
.dev
;
1119 unsigned long diff
= 0;
1121 BUILD_BUG_ON(sizeof(diff
) < __DEV_ATTR_MAX
/ 8);
1126 if (if_old
->device_config
!= if_new
->device_config
)
1129 if (!if_new
->device_config
)
1132 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb
,
1133 blob_data(if_old
->config
), blob_len(if_old
->config
));
1135 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, ntb
,
1136 blob_data(if_new
->config
), blob_len(if_new
->config
));
1138 uci_blob_diff(ntb
, otb
, &device_attr_list
, &diff
);
1143 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
1145 struct blob_attr
*old_config
= if_old
->config
;
1146 bool reload
= false, reload_ip
= false;
1148 #define FIELD_CHANGED_STR(field) \
1149 ((!!if_old->field != !!if_new->field) || \
1151 strcmp(if_old->field, if_new->field) != 0))
1153 if (FIELD_CHANGED_STR(parent_ifname
)) {
1154 if (if_old
->parent_iface
.iface
)
1155 interface_remove_user(&if_old
->parent_iface
);
1159 if (!reload
&& interface_device_config_changed(if_old
, if_new
))
1162 if (FIELD_CHANGED_STR(ifname
) ||
1163 if_old
->proto_handler
!= if_new
->proto_handler
)
1166 if (!if_old
->proto_handler
->config_params
)
1167 D(INTERFACE
, "No config parameters for interface '%s'\n",
1169 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
1170 if_old
->proto_handler
->config_params
))
1173 #define UPDATE(field, __var) ({ \
1174 bool __changed = (if_old->field != if_new->field); \
1175 if_old->field = if_new->field; \
1176 __var |= __changed; \
1179 if_old
->config
= if_new
->config
;
1180 if (if_old
->config_autostart
!= if_new
->config_autostart
) {
1181 if (if_old
->config_autostart
)
1184 if_old
->autostart
= if_new
->config_autostart
;
1187 if_old
->device_config
= if_new
->device_config
;
1188 if_old
->config_autostart
= if_new
->config_autostart
;
1189 if_old
->ifname
= if_new
->ifname
;
1190 if_old
->parent_ifname
= if_new
->parent_ifname
;
1191 if_old
->proto_handler
= if_new
->proto_handler
;
1192 if_old
->force_link
= if_new
->force_link
;
1193 if_old
->dns_metric
= if_new
->dns_metric
;
1195 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1196 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1198 UPDATE(metric
, reload_ip
);
1199 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1200 UPDATE(ip4table
, reload_ip
);
1201 UPDATE(ip6table
, reload_ip
);
1202 interface_merge_assignment_data(if_old
, if_new
);
1207 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1209 interface_clear_errors(if_old
);
1210 set_config_state(if_old
, IFC_RELOAD
);
1215 bool config_ip_enabled
= if_old
->config_ip
.enabled
;
1216 bool proto_ip_enabled
= if_old
->proto_ip
.enabled
;
1218 interface_ip_set_enabled(&if_old
->config_ip
, false);
1219 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1220 interface_ip_set_enabled(&if_old
->proto_ip
, proto_ip_enabled
);
1221 interface_ip_set_enabled(&if_old
->config_ip
, config_ip_enabled
);
1224 interface_write_resolv_conf();
1225 if (if_old
->main_dev
.dev
)
1226 interface_check_state(if_old
);
1229 if_new
->config
= NULL
;
1230 interface_cleanup(if_new
);
1236 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1237 struct vlist_node
*node_old
)
1239 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1240 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1242 if (node_old
&& node_new
) {
1243 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1244 interface_change_config(if_old
, if_new
);
1245 } else if (node_old
) {
1246 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1247 set_config_state(if_old
, IFC_REMOVE
);
1248 } else if (node_new
) {
1249 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1250 proto_init_interface(if_new
, if_new
->config
);
1251 interface_claim_device(if_new
);
1252 netifd_ubus_add_interface(if_new
);
1258 interface_init_list(void)
1260 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1261 interfaces
.keep_old
= true;
1262 interfaces
.no_delete
= true;