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 if (iface
->state
== IFS_UP
&& iface
->updated
)
697 interface_event(iface
, IFEV_UPDATE
);
701 if (!iface
->l3_dev
.dev
)
702 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
704 interface_ip_set_enabled(&iface
->config_ip
, true);
705 interface_ip_set_enabled(&iface
->proto_ip
, true);
706 system_flush_routes();
707 iface
->state
= IFS_UP
;
708 iface
->start_time
= system_get_rtime();
709 interface_event(iface
, IFEV_UP
);
710 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
713 if (iface
->state
== IFS_DOWN
)
716 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
717 mark_interface_down(iface
);
718 if (iface
->main_dev
.dev
)
719 device_release(&iface
->main_dev
);
720 if (iface
->l3_dev
.dev
)
721 device_remove_user(&iface
->l3_dev
);
722 interface_handle_config_change(iface
);
724 case IFPEV_LINK_LOST
:
725 if (iface
->state
!= IFS_UP
)
728 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
729 mark_interface_down(iface
);
730 iface
->state
= IFS_SETUP
;
736 interface_write_resolv_conf();
739 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
742 iface
->proto
->free(iface
->proto
);
745 iface
->state
= IFS_DOWN
;
746 iface
->proto
= state
;
750 state
->proto_event
= interface_proto_event_cb
;
751 state
->iface
= iface
;
755 interface_alloc(const char *name
, struct blob_attr
*config
)
757 struct interface
*iface
;
758 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
759 struct blob_attr
*cur
;
760 const char *proto_name
= NULL
;
762 bool force_link
= false;
764 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
765 iface
->name
= strcpy(iface_name
, name
);
766 INIT_LIST_HEAD(&iface
->errors
);
767 INIT_LIST_HEAD(&iface
->users
);
768 INIT_LIST_HEAD(&iface
->hotplug_list
);
769 INIT_LIST_HEAD(&iface
->assignment_classes
);
770 interface_ip_init(iface
);
771 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
772 iface
->config_ip
.enabled
= false;
774 iface
->main_dev
.cb
= interface_main_dev_cb
;
775 iface
->l3_dev
.cb
= interface_l3_dev_cb
;
776 iface
->ext_dev
.cb
= interface_ext_dev_cb
;
778 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
779 blob_data(config
), blob_len(config
));
781 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
782 proto_name
= blobmsg_data(cur
);
784 proto_attach_interface(iface
, proto_name
);
785 if (iface
->proto_handler
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
)
788 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
789 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], force_link
);
790 iface
->proto_ip
.no_defaultroute
=
791 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
792 iface
->proto_ip
.no_dns
=
793 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
795 if ((cur
= tb
[IFACE_ATTR_DNS
]))
796 interface_add_dns_server_list(&iface
->config_ip
, cur
);
798 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
799 interface_add_dns_search_list(&iface
->config_ip
, cur
);
801 if ((cur
= tb
[IFACE_ATTR_DNS_METRIC
]))
802 iface
->dns_metric
= blobmsg_get_u32(cur
);
804 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
805 iface
->metric
= blobmsg_get_u32(cur
);
807 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
808 iface
->assignment_length
= blobmsg_get_u32(cur
);
811 iface
->assignment_iface_id_selection
= IFID_FIXED
;
812 iface
->assignment_fixed_iface_id
= in6addr_any
;
813 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
815 if ((cur
= tb
[IFACE_ATTR_IP6IFACEID
])) {
816 const char *ifaceid
= blobmsg_data(cur
);
817 if (!strcmp(ifaceid
, "random")) {
818 iface
->assignment_iface_id_selection
= IFID_RANDOM
;
820 else if (!strcmp(ifaceid
, "eui64")) {
821 iface
->assignment_iface_id_selection
= IFID_EUI64
;
824 /* we expect an IPv6 address with network id zero here -> fixed iface id
825 if we cannot parse -> revert to iface id 1 */
826 if (inet_pton(AF_INET6
,ifaceid
,&iface
->assignment_fixed_iface_id
) != 1 ||
827 iface
->assignment_fixed_iface_id
.s6_addr32
[0] != 0 ||
828 iface
->assignment_fixed_iface_id
.s6_addr32
[1] != 0) {
829 iface
->assignment_fixed_iface_id
= in6addr_any
;
830 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
831 netifd_log_message(L_WARNING
, "Failed to parse ip6ifaceid for interface '%s', \
832 falling back to iface id 1.\n", iface
->name
);
837 iface
->assignment_hint
= -1;
838 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
839 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
840 ~((1 << (64 - iface
->assignment_length
)) - 1);
842 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
843 interface_add_assignment_classes(iface
, cur
);
846 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
847 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
848 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
851 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
852 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
853 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
856 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
858 iface
->config_autostart
= iface
->autostart
;
862 void interface_set_dynamic(struct interface
*iface
)
864 iface
->dynamic
= true;
865 iface
->autostart
= true;
866 iface
->node
.version
= -1; // Don't delete on reload
869 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
871 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
872 struct blob_attr
*cur
;
874 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
875 blob_data(config
), blob_len(config
));
878 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
879 iface
->parent_ifname
= blobmsg_data(cur
);
881 if (!iface
->parent_ifname
)
884 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
885 iface
->ifname
= blobmsg_data(cur
);
888 iface
->config
= config
;
889 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
894 interface_add(struct interface
*iface
, struct blob_attr
*config
)
896 __interface_add(iface
, config
, false);
900 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
902 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
905 return __interface_add(iface
, config
, true);
909 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
911 bool enabled
= iface
->config_ip
.enabled
;
912 bool claimed
= iface
->l3_dev
.claimed
;
914 if (iface
->l3_dev
.dev
== dev
)
917 interface_ip_set_enabled(&iface
->config_ip
, false);
918 interface_ip_set_enabled(&iface
->proto_ip
, false);
919 interface_ip_flush(&iface
->proto_ip
);
920 device_add_user(&iface
->l3_dev
, dev
);
924 if (device_claim(&iface
->l3_dev
) < 0)
927 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
928 interface_ip_set_enabled(&iface
->proto_ip
, enabled
);
933 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
935 bool claimed
= iface
->l3_dev
.claimed
;
937 if (iface
->main_dev
.dev
== dev
)
940 interface_set_available(iface
, false);
941 device_add_user(&iface
->main_dev
, dev
);
943 interface_set_link_state(iface
, false);
948 if (device_claim(&iface
->l3_dev
) < 0)
952 if (!iface
->l3_dev
.dev
)
953 interface_set_l3_dev(iface
, dev
);
957 interface_remove_link(struct interface
*iface
, struct device
*dev
)
959 struct device
*mdev
= iface
->main_dev
.dev
;
961 if (mdev
&& mdev
->hotplug_ops
)
962 return mdev
->hotplug_ops
->del(mdev
, dev
);
964 if (dev
== iface
->ext_dev
.dev
)
965 device_remove_user(&iface
->ext_dev
);
967 if (!iface
->main_dev
.hotplug
)
968 return UBUS_STATUS_INVALID_ARGUMENT
;
970 if (dev
!= iface
->main_dev
.dev
)
971 return UBUS_STATUS_INVALID_ARGUMENT
;
973 interface_set_main_dev(iface
, NULL
);
978 interface_add_link(struct interface
*iface
, struct device
*dev
, bool link_ext
)
980 struct device
*mdev
= iface
->main_dev
.dev
;
985 if (iface
->main_dev
.hotplug
)
986 device_remove_user(&iface
->main_dev
);
989 if (mdev
->hotplug_ops
)
990 return mdev
->hotplug_ops
->add(mdev
, dev
);
992 return UBUS_STATUS_NOT_SUPPORTED
;
996 device_add_user(&iface
->ext_dev
, dev
);
998 interface_set_main_dev(iface
, dev
);
999 iface
->main_dev
.hotplug
= true;
1004 interface_handle_link(struct interface
*iface
, const char *name
, bool add
, bool link_ext
)
1011 dev
= device_get(name
, add
? (link_ext
? 2 : 1) : 0);
1013 ret
= UBUS_STATUS_NOT_FOUND
;
1018 interface_set_device_config(iface
, dev
);
1019 device_set_present(dev
, true);
1021 ret
= interface_add_link(iface
, dev
, link_ext
);
1023 ret
= interface_remove_link(iface
, dev
);
1033 interface_set_up(struct interface
*iface
)
1037 iface
->autostart
= true;
1039 if (iface
->state
!= IFS_DOWN
)
1042 interface_clear_errors(iface
);
1043 if (!iface
->available
) {
1044 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
1048 if (iface
->main_dev
.dev
) {
1049 ret
= device_claim(&iface
->main_dev
);
1051 interface_check_state(iface
);
1054 ret
= __interface_set_up(iface
);
1060 interface_set_down(struct interface
*iface
)
1063 vlist_for_each_element(&interfaces
, iface
, node
)
1064 __interface_set_down(iface
, false);
1066 iface
->autostart
= false;
1067 __interface_set_down(iface
, false);
1074 interface_start_pending(void)
1076 struct interface
*iface
;
1078 vlist_for_each_element(&interfaces
, iface
, node
) {
1079 if (iface
->available
&& iface
->autostart
)
1080 interface_set_up(iface
);
1085 set_config_state(struct interface
*iface
, enum interface_config_state s
)
1087 iface
->config_state
= s
;
1088 if (iface
->state
== IFS_DOWN
)
1089 interface_handle_config_change(iface
);
1091 __interface_set_down(iface
, false);
1095 interface_update_start(struct interface
*iface
, const bool keep_old
)
1100 interface_ip_update_start(&iface
->proto_ip
);
1104 interface_update_complete(struct interface
*iface
)
1106 interface_ip_update_complete(&iface
->proto_ip
);
1110 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
1112 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
1113 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
1117 interface_device_config_changed(struct interface
*if_old
, struct interface
*if_new
)
1119 struct blob_attr
*ntb
[__DEV_ATTR_MAX
];
1120 struct blob_attr
*otb
[__DEV_ATTR_MAX
];
1121 struct device
*dev
= if_old
->main_dev
.dev
;
1122 unsigned long diff
= 0;
1124 BUILD_BUG_ON(sizeof(diff
) < __DEV_ATTR_MAX
/ 8);
1129 if (if_old
->device_config
!= if_new
->device_config
)
1132 if (!if_new
->device_config
)
1135 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb
,
1136 blob_data(if_old
->config
), blob_len(if_old
->config
));
1138 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, ntb
,
1139 blob_data(if_new
->config
), blob_len(if_new
->config
));
1141 uci_blob_diff(ntb
, otb
, &device_attr_list
, &diff
);
1146 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
1148 struct blob_attr
*old_config
= if_old
->config
;
1149 bool reload
= false, reload_ip
= false;
1151 #define FIELD_CHANGED_STR(field) \
1152 ((!!if_old->field != !!if_new->field) || \
1154 strcmp(if_old->field, if_new->field) != 0))
1156 if (FIELD_CHANGED_STR(parent_ifname
)) {
1157 if (if_old
->parent_iface
.iface
)
1158 interface_remove_user(&if_old
->parent_iface
);
1162 if (!reload
&& interface_device_config_changed(if_old
, if_new
))
1165 if (FIELD_CHANGED_STR(ifname
) ||
1166 if_old
->proto_handler
!= if_new
->proto_handler
)
1169 if (!if_old
->proto_handler
->config_params
)
1170 D(INTERFACE
, "No config parameters for interface '%s'\n",
1172 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
1173 if_old
->proto_handler
->config_params
))
1176 #define UPDATE(field, __var) ({ \
1177 bool __changed = (if_old->field != if_new->field); \
1178 if_old->field = if_new->field; \
1179 __var |= __changed; \
1182 if_old
->config
= if_new
->config
;
1183 if (if_old
->config_autostart
!= if_new
->config_autostart
) {
1184 if (if_old
->config_autostart
)
1187 if_old
->autostart
= if_new
->config_autostart
;
1190 if_old
->device_config
= if_new
->device_config
;
1191 if_old
->config_autostart
= if_new
->config_autostart
;
1192 if_old
->ifname
= if_new
->ifname
;
1193 if_old
->parent_ifname
= if_new
->parent_ifname
;
1194 if_old
->proto_handler
= if_new
->proto_handler
;
1195 if_old
->force_link
= if_new
->force_link
;
1196 if_old
->dns_metric
= if_new
->dns_metric
;
1198 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1199 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1201 UPDATE(metric
, reload_ip
);
1202 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1203 UPDATE(ip4table
, reload_ip
);
1204 UPDATE(ip6table
, reload_ip
);
1205 interface_merge_assignment_data(if_old
, if_new
);
1210 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1212 interface_clear_errors(if_old
);
1213 set_config_state(if_old
, IFC_RELOAD
);
1218 bool config_ip_enabled
= if_old
->config_ip
.enabled
;
1219 bool proto_ip_enabled
= if_old
->proto_ip
.enabled
;
1221 interface_ip_set_enabled(&if_old
->config_ip
, false);
1222 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1223 interface_ip_set_enabled(&if_old
->proto_ip
, proto_ip_enabled
);
1224 interface_ip_set_enabled(&if_old
->config_ip
, config_ip_enabled
);
1227 interface_write_resolv_conf();
1228 if (if_old
->main_dev
.dev
)
1229 interface_check_state(if_old
);
1232 if_new
->config
= NULL
;
1233 interface_cleanup(if_new
);
1239 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1240 struct vlist_node
*node_old
)
1242 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1243 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1245 if (node_old
&& node_new
) {
1246 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1247 interface_change_config(if_old
, if_new
);
1248 } else if (node_old
) {
1249 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1250 set_config_state(if_old
, IFC_REMOVE
);
1251 } else if (node_new
) {
1252 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1253 proto_init_interface(if_new
, if_new
->config
);
1254 interface_claim_device(if_new
);
1255 netifd_ubus_add_interface(if_new
);
1261 interface_init_list(void)
1263 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1264 interfaces
.keep_old
= true;
1265 interfaces
.no_delete
= true;