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 set_config_state(struct interface
*iface
, enum interface_config_state s
);
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
;
272 interface_event(iface
, IFEV_DOWN
);
275 interface_event(iface
, IFEV_UP_FAILED
);
280 interface_ip_set_enabled(&iface
->config_ip
, false);
281 interface_ip_set_enabled(&iface
->proto_ip
, false);
282 interface_ip_flush(&iface
->proto_ip
);
283 interface_flush_state(iface
);
284 system_flush_routes();
288 __interface_set_down(struct interface
*iface
, bool force
)
290 enum interface_state state
= iface
->state
;
294 iface
->state
= IFS_TEARDOWN
;
296 interface_event(iface
, IFEV_DOWN
);
298 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
300 interface_flush_state(iface
);
304 if (iface
->main_dev
.dev
)
305 device_release(&iface
->main_dev
);
313 __interface_set_up(struct interface
*iface
)
317 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
319 iface
->state
= IFS_SETUP
;
320 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
322 mark_interface_down(iface
);
328 interface_check_state(struct interface
*iface
)
330 bool link_state
= iface
->link_state
|| iface
->force_link
;
332 switch (iface
->state
) {
335 if (!iface
->enabled
|| !link_state
) {
336 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
337 mark_interface_down(iface
);
341 if (!iface
->available
)
344 if (iface
->autostart
&& iface
->enabled
&& link_state
&& !config_init
)
345 __interface_set_up(iface
);
353 interface_set_enabled(struct interface
*iface
, bool new_state
)
355 if (iface
->enabled
== new_state
)
358 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
359 iface
->enabled
= new_state
;
360 interface_check_state(iface
);
364 interface_set_link_state(struct interface
*iface
, bool new_state
)
366 if (iface
->link_state
== new_state
)
369 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
370 iface
->link_state
= new_state
;
371 interface_check_state(iface
);
373 if (new_state
&& iface
->force_link
&& iface
->state
== IFS_UP
&& !iface
->link_up_event
) {
374 interface_event(iface
, IFEV_LINK_UP
);
375 iface
->link_up_event
= true;
380 interface_ext_dev_cb(struct device_user
*dep
, enum device_event ev
)
382 if (ev
== DEV_EVENT_REMOVE
)
383 device_remove_user(dep
);
387 interface_main_dev_cb(struct device_user
*dep
, enum device_event ev
)
389 struct interface
*iface
;
391 iface
= container_of(dep
, struct interface
, main_dev
);
394 interface_set_available(iface
, true);
396 case DEV_EVENT_REMOVE
:
397 interface_set_available(iface
, false);
398 if (dep
->dev
&& dep
->dev
->external
)
399 interface_set_main_dev(iface
, NULL
);
402 interface_set_enabled(iface
, true);
405 interface_set_enabled(iface
, false);
407 case DEV_EVENT_LINK_UP
:
408 interface_set_link_state(iface
, true);
410 case DEV_EVENT_LINK_DOWN
:
411 interface_set_link_state(iface
, false);
413 case DEV_EVENT_TOPO_CHANGE
:
414 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
422 interface_l3_dev_cb(struct device_user
*dep
, enum device_event ev
)
424 struct interface
*iface
;
426 iface
= container_of(dep
, struct interface
, l3_dev
);
427 if (iface
->l3_dev
.dev
== iface
->main_dev
.dev
)
431 case DEV_EVENT_LINK_DOWN
:
432 if (iface
->proto_handler
->flags
& PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN
)
433 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
441 interface_set_available(struct interface
*iface
, bool new_state
)
443 if (iface
->available
== new_state
)
446 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
447 iface
->available
= new_state
;
450 if (iface
->autostart
&& !config_init
)
451 interface_set_up(iface
);
453 __interface_set_down(iface
, true);
457 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
460 list_add(&dep
->list
, &iface_all_users
);
465 list_add(&dep
->list
, &iface
->users
);
466 if (iface
->state
== IFS_UP
)
467 dep
->cb(dep
, iface
, IFEV_UP
);
471 interface_remove_user(struct interface_user
*dep
)
473 list_del_init(&dep
->list
);
478 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
480 struct blob_attr
*cur
;
483 blobmsg_for_each_attr(cur
, list
, rem
) {
484 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
487 if (!blobmsg_check_attr(cur
, NULL
))
490 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
491 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
492 list_add(&c
->head
, &iface
->assignment_classes
);
497 interface_clear_assignment_classes(struct interface
*iface
)
499 while (!list_empty(&iface
->assignment_classes
)) {
500 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
501 struct interface_assignment_class
, head
);
508 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
510 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
511 old
->assignment_length
!= new->assignment_length
||
512 old
->assignment_iface_id_selection
!= new->assignment_iface_id_selection
||
513 old
->assignment_weight
!= new->assignment_weight
||
514 (old
->assignment_iface_id_selection
== IFID_FIXED
&&
515 memcmp(&old
->assignment_fixed_iface_id
, &new->assignment_fixed_iface_id
,
516 sizeof(old
->assignment_fixed_iface_id
))) ||
517 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
519 struct interface_assignment_class
*c
;
520 list_for_each_entry(c
, &new->assignment_classes
, head
) {
521 // Compare list entries one-by-one to see if there was a change
522 if (list_empty(&old
->assignment_classes
)) // The new list is longer
528 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
529 struct interface_assignment_class
, head
);
531 if (strcmp(c_old
->name
, c
->name
)) // An entry didn't match
534 list_del(&c_old
->head
);
538 // The old list was longer than the new one or the last entry didn't match
539 if (!list_empty(&old
->assignment_classes
)) {
540 interface_clear_assignment_classes(old
);
544 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
547 old
->assignment_hint
= new->assignment_hint
;
548 old
->assignment_length
= new->assignment_length
;
549 old
->assignment_iface_id_selection
= new->assignment_iface_id_selection
;
550 old
->assignment_fixed_iface_id
= new->assignment_fixed_iface_id
;
551 old
->assignment_weight
= new->assignment_weight
;
552 interface_refresh_assignments(true);
557 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
559 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
560 struct device
*dev
= iface
->l3_dev
.dev
;
567 interface_set_main_dev(alias
, dev
);
568 interface_set_available(alias
, true);
572 interface_set_available(alias
, false);
573 interface_set_main_dev(alias
, NULL
);
576 interface_remove_user(dep
);
584 interface_set_device_config(struct interface
*iface
, struct device
*dev
)
586 if (!dev
|| !dev
->default_config
)
589 if (!iface
->device_config
&&
590 (!dev
->iface_config
|| dev
->config_iface
!= iface
))
593 dev
->config_iface
= iface
;
594 dev
->iface_config
= iface
->device_config
;
595 device_apply_config(dev
, dev
->type
, iface
->config
);
599 interface_claim_device(struct interface
*iface
)
601 struct interface
*parent
;
602 struct device
*dev
= NULL
;
604 if (iface
->parent_iface
.iface
)
605 interface_remove_user(&iface
->parent_iface
);
609 if (iface
->parent_ifname
) {
610 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
611 iface
->parent_iface
.cb
= interface_alias_cb
;
612 interface_add_user(&iface
->parent_iface
, parent
);
613 } else if (iface
->ifname
&&
614 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
615 dev
= device_get(iface
->ifname
, true);
616 interface_set_device_config(iface
, dev
);
618 dev
= iface
->ext_dev
.dev
;
622 interface_set_main_dev(iface
, dev
);
626 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
627 interface_set_available(iface
, true);
631 interface_cleanup_state(struct interface
*iface
)
633 interface_set_available(iface
, false);
635 interface_flush_state(iface
);
636 interface_clear_errors(iface
);
637 interface_set_proto_state(iface
, NULL
);
639 interface_set_main_dev(iface
, NULL
);
640 interface_set_l3_dev(iface
, NULL
);
644 interface_cleanup(struct interface
*iface
)
646 struct interface_user
*dep
, *tmp
;
648 uloop_timeout_cancel(&iface
->remove_timer
);
649 device_remove_user(&iface
->ext_dev
);
651 if (iface
->parent_iface
.iface
)
652 interface_remove_user(&iface
->parent_iface
);
654 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
655 interface_remove_user(dep
);
657 interface_clear_assignment_classes(iface
);
658 interface_ip_flush(&iface
->config_ip
);
659 interface_cleanup_state(iface
);
663 interface_do_free(struct interface
*iface
)
665 interface_event(iface
, IFEV_FREE
);
666 interface_cleanup(iface
);
668 netifd_ubus_remove_interface(iface
);
669 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
674 interface_do_reload(struct interface
*iface
)
676 interface_event(iface
, IFEV_RELOAD
);
677 interface_cleanup_state(iface
);
678 proto_init_interface(iface
, iface
->config
);
679 interface_claim_device(iface
);
683 interface_handle_config_change(struct interface
*iface
)
685 enum interface_config_state state
= iface
->config_state
;
687 iface
->config_state
= IFC_NORMAL
;
692 interface_do_reload(iface
);
695 interface_do_free(iface
);
698 if (iface
->autostart
&& iface
->available
)
699 interface_set_up(iface
);
700 else if (iface
->dynamic
)
701 set_config_state(iface
, IFC_REMOVE
);
705 interface_proto_event_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
707 struct interface
*iface
= state
->iface
;
711 if (iface
->state
!= IFS_SETUP
) {
712 if (iface
->state
== IFS_UP
&& iface
->updated
)
713 interface_event(iface
, IFEV_UPDATE
);
717 if (!iface
->l3_dev
.dev
)
718 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
720 interface_ip_set_enabled(&iface
->config_ip
, true);
721 interface_ip_set_enabled(&iface
->proto_ip
, true);
722 system_flush_routes();
723 iface
->state
= IFS_UP
;
724 iface
->start_time
= system_get_rtime();
725 interface_event(iface
, IFEV_UP
);
726 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
729 if (iface
->state
== IFS_DOWN
)
732 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
733 mark_interface_down(iface
);
734 if (iface
->main_dev
.dev
)
735 device_release(&iface
->main_dev
);
736 if (iface
->l3_dev
.dev
)
737 device_remove_user(&iface
->l3_dev
);
738 interface_handle_config_change(iface
);
740 case IFPEV_LINK_LOST
:
741 if (iface
->state
!= IFS_UP
)
744 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
745 mark_interface_down(iface
);
746 iface
->state
= IFS_SETUP
;
752 interface_write_resolv_conf();
755 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
758 iface
->proto
->free(iface
->proto
);
761 iface
->state
= IFS_DOWN
;
762 iface
->proto
= state
;
766 state
->proto_event
= interface_proto_event_cb
;
767 state
->iface
= iface
;
771 interface_alloc(const char *name
, struct blob_attr
*config
)
773 struct interface
*iface
;
774 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
775 struct blob_attr
*cur
;
776 const char *proto_name
= NULL
;
778 bool force_link
= false;
780 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
781 iface
->name
= strcpy(iface_name
, name
);
782 INIT_LIST_HEAD(&iface
->errors
);
783 INIT_LIST_HEAD(&iface
->users
);
784 INIT_LIST_HEAD(&iface
->hotplug_list
);
785 INIT_LIST_HEAD(&iface
->assignment_classes
);
786 interface_ip_init(iface
);
787 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
788 iface
->config_ip
.enabled
= false;
790 iface
->main_dev
.cb
= interface_main_dev_cb
;
791 iface
->l3_dev
.cb
= interface_l3_dev_cb
;
792 iface
->ext_dev
.cb
= interface_ext_dev_cb
;
794 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
795 blob_data(config
), blob_len(config
));
797 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
798 proto_name
= blobmsg_data(cur
);
800 proto_attach_interface(iface
, proto_name
);
801 if (iface
->proto_handler
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
)
804 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
805 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], force_link
);
806 iface
->proto_ip
.no_defaultroute
=
807 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
808 iface
->proto_ip
.no_dns
=
809 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
811 if ((cur
= tb
[IFACE_ATTR_DNS
]))
812 interface_add_dns_server_list(&iface
->config_ip
, cur
);
814 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
815 interface_add_dns_search_list(&iface
->config_ip
, cur
);
817 if ((cur
= tb
[IFACE_ATTR_DNS_METRIC
]))
818 iface
->dns_metric
= blobmsg_get_u32(cur
);
820 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
821 iface
->metric
= blobmsg_get_u32(cur
);
823 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
824 iface
->assignment_length
= blobmsg_get_u32(cur
);
827 iface
->assignment_iface_id_selection
= IFID_FIXED
;
828 iface
->assignment_fixed_iface_id
= in6addr_any
;
829 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
831 if ((cur
= tb
[IFACE_ATTR_IP6IFACEID
])) {
832 const char *ifaceid
= blobmsg_data(cur
);
833 if (!strcmp(ifaceid
, "random")) {
834 iface
->assignment_iface_id_selection
= IFID_RANDOM
;
836 else if (!strcmp(ifaceid
, "eui64")) {
837 iface
->assignment_iface_id_selection
= IFID_EUI64
;
840 /* we expect an IPv6 address with network id zero here -> fixed iface id
841 if we cannot parse -> revert to iface id 1 */
842 if (inet_pton(AF_INET6
,ifaceid
,&iface
->assignment_fixed_iface_id
) != 1 ||
843 iface
->assignment_fixed_iface_id
.s6_addr32
[0] != 0 ||
844 iface
->assignment_fixed_iface_id
.s6_addr32
[1] != 0) {
845 iface
->assignment_fixed_iface_id
= in6addr_any
;
846 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
847 netifd_log_message(L_WARNING
, "Failed to parse ip6ifaceid for interface '%s', \
848 falling back to iface id 1.\n", iface
->name
);
853 iface
->assignment_hint
= -1;
854 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
855 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
856 ~((1 << (64 - iface
->assignment_length
)) - 1);
858 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
859 interface_add_assignment_classes(iface
, cur
);
861 if ((cur
= tb
[IFACE_ATTR_IP6WEIGHT
]))
862 iface
->assignment_weight
= blobmsg_get_u32(cur
);
864 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
865 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
866 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
869 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
870 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
871 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
874 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
876 iface
->config_autostart
= iface
->autostart
;
880 void interface_set_dynamic(struct interface
*iface
)
882 iface
->dynamic
= true;
883 iface
->autostart
= true;
884 iface
->node
.version
= -1; // Don't delete on reload
887 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
889 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
890 struct blob_attr
*cur
;
892 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
893 blob_data(config
), blob_len(config
));
896 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
897 iface
->parent_ifname
= blobmsg_data(cur
);
899 if (!iface
->parent_ifname
)
902 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
903 iface
->ifname
= blobmsg_data(cur
);
906 iface
->config
= config
;
907 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
912 interface_add(struct interface
*iface
, struct blob_attr
*config
)
914 __interface_add(iface
, config
, false);
918 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
920 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
923 return __interface_add(iface
, config
, true);
927 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
929 bool enabled
= iface
->config_ip
.enabled
;
930 bool claimed
= iface
->l3_dev
.claimed
;
932 if (iface
->l3_dev
.dev
== dev
)
935 interface_ip_set_enabled(&iface
->config_ip
, false);
936 interface_ip_set_enabled(&iface
->proto_ip
, false);
937 interface_ip_flush(&iface
->proto_ip
);
938 device_add_user(&iface
->l3_dev
, dev
);
942 if (device_claim(&iface
->l3_dev
) < 0)
945 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
946 interface_ip_set_enabled(&iface
->proto_ip
, enabled
);
951 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
953 bool claimed
= iface
->l3_dev
.claimed
;
955 if (iface
->main_dev
.dev
== dev
)
958 interface_set_available(iface
, false);
959 device_add_user(&iface
->main_dev
, dev
);
961 interface_set_link_state(iface
, false);
966 if (device_claim(&iface
->l3_dev
) < 0)
970 if (!iface
->l3_dev
.dev
)
971 interface_set_l3_dev(iface
, dev
);
975 interface_remove_link(struct interface
*iface
, struct device
*dev
)
977 struct device
*mdev
= iface
->main_dev
.dev
;
979 if (mdev
&& mdev
->hotplug_ops
)
980 return mdev
->hotplug_ops
->del(mdev
, dev
);
982 if (dev
== iface
->ext_dev
.dev
)
983 device_remove_user(&iface
->ext_dev
);
985 if (!iface
->main_dev
.hotplug
)
986 return UBUS_STATUS_INVALID_ARGUMENT
;
988 if (dev
!= iface
->main_dev
.dev
)
989 return UBUS_STATUS_INVALID_ARGUMENT
;
991 interface_set_main_dev(iface
, NULL
);
996 interface_add_link(struct interface
*iface
, struct device
*dev
, bool link_ext
)
998 struct device
*mdev
= iface
->main_dev
.dev
;
1003 if (iface
->main_dev
.hotplug
)
1004 device_remove_user(&iface
->main_dev
);
1007 if (mdev
->hotplug_ops
)
1008 return mdev
->hotplug_ops
->add(mdev
, dev
);
1010 return UBUS_STATUS_NOT_SUPPORTED
;
1014 device_add_user(&iface
->ext_dev
, dev
);
1016 interface_set_main_dev(iface
, dev
);
1017 iface
->main_dev
.hotplug
= true;
1022 interface_handle_link(struct interface
*iface
, const char *name
, bool add
, bool link_ext
)
1029 dev
= device_get(name
, add
? (link_ext
? 2 : 1) : 0);
1031 ret
= UBUS_STATUS_NOT_FOUND
;
1036 interface_set_device_config(iface
, dev
);
1037 device_set_present(dev
, true);
1039 ret
= interface_add_link(iface
, dev
, link_ext
);
1041 ret
= interface_remove_link(iface
, dev
);
1051 interface_set_up(struct interface
*iface
)
1055 iface
->autostart
= true;
1057 if (iface
->state
!= IFS_DOWN
)
1060 interface_clear_errors(iface
);
1061 if (!iface
->available
) {
1062 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
1066 if (iface
->main_dev
.dev
) {
1067 ret
= device_claim(&iface
->main_dev
);
1069 interface_check_state(iface
);
1072 ret
= __interface_set_up(iface
);
1078 interface_set_down(struct interface
*iface
)
1081 vlist_for_each_element(&interfaces
, iface
, node
)
1082 __interface_set_down(iface
, false);
1084 iface
->autostart
= false;
1085 __interface_set_down(iface
, false);
1092 interface_renew(struct interface
*iface
)
1094 if (iface
->state
== IFS_TEARDOWN
|| iface
->state
== IFS_DOWN
)
1097 return interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
1101 interface_start_pending(void)
1103 struct interface
*iface
;
1105 vlist_for_each_element(&interfaces
, iface
, node
) {
1106 if (iface
->available
&& iface
->autostart
)
1107 interface_set_up(iface
);
1112 set_config_state(struct interface
*iface
, enum interface_config_state s
)
1114 iface
->config_state
= s
;
1115 if (iface
->state
== IFS_DOWN
)
1116 interface_handle_config_change(iface
);
1118 __interface_set_down(iface
, false);
1122 interface_update_start(struct interface
*iface
, const bool keep_old
)
1127 interface_ip_update_start(&iface
->proto_ip
);
1131 interface_update_complete(struct interface
*iface
)
1133 interface_ip_update_complete(&iface
->proto_ip
);
1137 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
1139 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
1140 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
1144 interface_device_config_changed(struct interface
*if_old
, struct interface
*if_new
)
1146 struct blob_attr
*ntb
[__DEV_ATTR_MAX
];
1147 struct blob_attr
*otb
[__DEV_ATTR_MAX
];
1148 struct device
*dev
= if_old
->main_dev
.dev
;
1149 unsigned long diff
= 0;
1151 BUILD_BUG_ON(sizeof(diff
) < __DEV_ATTR_MAX
/ 8);
1156 if (if_old
->device_config
!= if_new
->device_config
)
1159 if (!if_new
->device_config
)
1162 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb
,
1163 blob_data(if_old
->config
), blob_len(if_old
->config
));
1165 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, ntb
,
1166 blob_data(if_new
->config
), blob_len(if_new
->config
));
1168 uci_blob_diff(ntb
, otb
, &device_attr_list
, &diff
);
1173 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
1175 struct blob_attr
*old_config
= if_old
->config
;
1176 bool reload
= false, reload_ip
= false;
1178 #define FIELD_CHANGED_STR(field) \
1179 ((!!if_old->field != !!if_new->field) || \
1181 strcmp(if_old->field, if_new->field) != 0))
1183 if (FIELD_CHANGED_STR(parent_ifname
)) {
1184 if (if_old
->parent_iface
.iface
)
1185 interface_remove_user(&if_old
->parent_iface
);
1189 if (!reload
&& interface_device_config_changed(if_old
, if_new
))
1192 if (FIELD_CHANGED_STR(ifname
) ||
1193 if_old
->proto_handler
!= if_new
->proto_handler
)
1196 if (!if_old
->proto_handler
->config_params
)
1197 D(INTERFACE
, "No config parameters for interface '%s'\n",
1199 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
1200 if_old
->proto_handler
->config_params
))
1203 #define UPDATE(field, __var) ({ \
1204 bool __changed = (if_old->field != if_new->field); \
1205 if_old->field = if_new->field; \
1206 __var |= __changed; \
1209 if_old
->config
= if_new
->config
;
1210 if (if_old
->config_autostart
!= if_new
->config_autostart
) {
1211 if (if_old
->config_autostart
)
1214 if_old
->autostart
= if_new
->config_autostart
;
1217 if_old
->device_config
= if_new
->device_config
;
1218 if_old
->config_autostart
= if_new
->config_autostart
;
1219 if_old
->ifname
= if_new
->ifname
;
1220 if_old
->parent_ifname
= if_new
->parent_ifname
;
1221 if_old
->proto_handler
= if_new
->proto_handler
;
1222 if_old
->force_link
= if_new
->force_link
;
1223 if_old
->dns_metric
= if_new
->dns_metric
;
1225 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1226 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1228 UPDATE(metric
, reload_ip
);
1229 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1230 UPDATE(ip4table
, reload_ip
);
1231 UPDATE(ip6table
, reload_ip
);
1232 interface_merge_assignment_data(if_old
, if_new
);
1237 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1239 interface_clear_errors(if_old
);
1240 set_config_state(if_old
, IFC_RELOAD
);
1245 bool config_ip_enabled
= if_old
->config_ip
.enabled
;
1246 bool proto_ip_enabled
= if_old
->proto_ip
.enabled
;
1248 interface_ip_set_enabled(&if_old
->config_ip
, false);
1249 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1250 interface_ip_set_enabled(&if_old
->proto_ip
, proto_ip_enabled
);
1251 interface_ip_set_enabled(&if_old
->config_ip
, config_ip_enabled
);
1254 interface_write_resolv_conf();
1255 if (if_old
->main_dev
.dev
)
1256 interface_check_state(if_old
);
1259 if_new
->config
= NULL
;
1260 interface_cleanup(if_new
);
1266 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1267 struct vlist_node
*node_old
)
1269 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1270 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1272 if (node_old
&& node_new
) {
1273 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1274 interface_change_config(if_old
, if_new
);
1275 } else if (node_old
) {
1276 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1277 set_config_state(if_old
, IFC_REMOVE
);
1278 } else if (node_new
) {
1279 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1280 interface_event(if_new
, IFEV_CREATE
);
1281 proto_init_interface(if_new
, if_new
->config
);
1282 interface_claim_device(if_new
);
1283 netifd_ubus_add_interface(if_new
);
1289 interface_init_list(void)
1291 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1292 interfaces
.keep_old
= true;
1293 interfaces
.no_delete
= true;