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
,
46 IFACE_ATTR_IP6IFACEID
,
47 IFACE_ATTR_FORCE_LINK
,
51 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
52 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
53 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
54 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
55 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
56 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
57 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
58 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
59 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
60 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
61 [IFACE_ATTR_IP6ASSIGN
] = { .name
= "ip6assign", .type
= BLOBMSG_TYPE_INT32
},
62 [IFACE_ATTR_IP6HINT
] = { .name
= "ip6hint", .type
= BLOBMSG_TYPE_STRING
},
63 [IFACE_ATTR_IP4TABLE
] = { .name
= "ip4table", .type
= BLOBMSG_TYPE_STRING
},
64 [IFACE_ATTR_IP6TABLE
] = { .name
= "ip6table", .type
= BLOBMSG_TYPE_STRING
},
65 [IFACE_ATTR_IP6CLASS
] = { .name
= "ip6class", .type
= BLOBMSG_TYPE_ARRAY
},
66 [IFACE_ATTR_DELEGATE
] = { .name
= "delegate", .type
= BLOBMSG_TYPE_BOOL
},
67 [IFACE_ATTR_IP6IFACEID
] = { .name
= "ip6ifaceid", .type
= BLOBMSG_TYPE_STRING
},
68 [IFACE_ATTR_FORCE_LINK
] = { .name
= "force_link", .type
= BLOBMSG_TYPE_BOOL
},
71 static const struct uci_blob_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
72 [IFACE_ATTR_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
73 [IFACE_ATTR_IP6CLASS
] = { .type
= BLOBMSG_TYPE_STRING
},
76 const struct uci_blob_param_list interface_attr_list
= {
77 .n_params
= IFACE_ATTR_MAX
,
78 .params
= iface_attrs
,
79 .info
= iface_attr_info
,
83 interface_error_flush(struct interface
*iface
)
85 struct interface_error
*error
, *tmp
;
87 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
88 list_del(&error
->list
);
94 interface_clear_errors(struct interface
*iface
)
96 /* don't flush the errors in case the configured protocol handler matches the
97 running protocol handler and is having the last error capability */
99 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
100 (iface
->proto
->handler
->name
== iface
->proto_handler
->name
)))
101 interface_error_flush(iface
);
104 void interface_add_error(struct interface
*iface
, const char *subsystem
,
105 const char *code
, const char **data
, int n_data
)
107 struct interface_error
*error
;
110 char *dest
, *d_subsys
, *d_code
;
112 /* if the configured protocol handler has the last error support capability,
113 errors should only be added if the running protocol handler matches the
116 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
117 (iface
->proto
->handler
->name
!= iface
->proto_handler
->name
))
121 len
= n_data
* sizeof(char *);
122 datalen
= alloca(len
);
123 for (i
= 0; i
< n_data
; i
++) {
124 datalen
[i
] = strlen(data
[i
]) + 1;
129 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
130 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
131 &d_code
, code
? strlen(code
) + 1 : 0);
135 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
136 protocol can't be established (e.g auth failure) */
137 if (iface
->proto_handler
->flags
& PROTO_FLAG_LASTERROR
)
138 interface_error_flush(iface
);
140 list_add_tail(&error
->list
, &iface
->errors
);
142 dest
= (char *) &error
->data
[n_data
+ 1];
143 for (i
= 0; i
< n_data
; i
++) {
144 error
->data
[i
] = dest
;
145 memcpy(dest
, data
[i
], datalen
[i
]);
148 error
->data
[n_data
++] = NULL
;
151 error
->subsystem
= strcpy(d_subsys
, subsystem
);
154 error
->code
= strcpy(d_code
, code
);
158 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
160 avl_delete(&iface
->data
, &data
->node
);
165 interface_data_flush(struct interface
*iface
)
167 struct interface_data
*d
, *tmp
;
169 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
170 interface_data_del(iface
, d
);
174 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
176 struct interface_data
*n
, *o
;
178 if (!blobmsg_check_attr(data
, true))
179 return UBUS_STATUS_INVALID_ARGUMENT
;
181 const char *name
= blobmsg_name(data
);
182 unsigned len
= blob_pad_len(data
);
184 o
= avl_find_element(&iface
->data
, name
, o
, node
);
186 if (blob_pad_len(o
->data
) == len
&& !memcmp(o
->data
, data
, len
))
189 interface_data_del(iface
, o
);
192 n
= calloc(1, sizeof(*n
) + len
);
193 memcpy(n
->data
, data
, len
);
194 n
->node
.key
= blobmsg_name(n
->data
);
195 avl_insert(&iface
->data
, &n
->node
);
197 iface
->updated
|= IUF_DATA
;
202 interface_event(struct interface
*iface
, enum interface_event ev
)
204 struct interface_user
*dep
, *tmp
;
205 struct device
*adev
= NULL
;
207 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
208 dep
->cb(dep
, iface
, ev
);
210 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
211 dep
->cb(dep
, iface
, ev
);
215 interface_error_flush(iface
);
216 adev
= iface
->l3_dev
.dev
;
219 alias_notify_device(iface
->name
, adev
);
227 interface_flush_state(struct interface
*iface
)
229 if (iface
->l3_dev
.dev
)
230 device_release(&iface
->l3_dev
);
231 interface_data_flush(iface
);
235 mark_interface_down(struct interface
*iface
)
237 enum interface_state state
= iface
->state
;
239 if (state
== IFS_DOWN
)
242 iface
->state
= IFS_DOWN
;
244 interface_event(iface
, IFEV_DOWN
);
245 interface_ip_set_enabled(&iface
->config_ip
, false);
246 interface_ip_flush(&iface
->proto_ip
);
247 interface_flush_state(iface
);
248 system_flush_routes();
252 __interface_set_down(struct interface
*iface
, bool force
)
254 enum interface_state state
= iface
->state
;
258 iface
->state
= IFS_TEARDOWN
;
260 interface_event(iface
, IFEV_DOWN
);
262 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
264 interface_flush_state(iface
);
267 vlist_delete(&interfaces
, &iface
->node
);
271 if (iface
->main_dev
.dev
)
272 device_release(&iface
->main_dev
);
280 __interface_set_up(struct interface
*iface
)
284 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
286 iface
->state
= IFS_SETUP
;
287 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
289 mark_interface_down(iface
);
295 interface_check_state(struct interface
*iface
)
297 bool link_state
= iface
->link_state
|| iface
->force_link
;
299 switch (iface
->state
) {
302 if (!iface
->enabled
|| !link_state
) {
303 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
304 mark_interface_down(iface
);
308 if (!iface
->available
)
311 if (iface
->autostart
&& iface
->enabled
&& link_state
&& !config_init
)
312 __interface_set_up(iface
);
320 interface_set_enabled(struct interface
*iface
, bool new_state
)
322 if (iface
->enabled
== new_state
)
325 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
326 iface
->enabled
= new_state
;
327 interface_check_state(iface
);
331 interface_set_link_state(struct interface
*iface
, bool new_state
)
333 if (iface
->link_state
== new_state
)
336 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
337 iface
->link_state
= new_state
;
338 interface_check_state(iface
);
342 interface_ext_cb(struct device_user
*dep
, enum device_event ev
)
344 if (ev
== DEV_EVENT_REMOVE
)
345 device_remove_user(dep
);
349 interface_cb(struct device_user
*dep
, enum device_event ev
)
351 struct interface
*iface
;
352 bool new_state
= false;
354 iface
= container_of(dep
, struct interface
, main_dev
);
358 case DEV_EVENT_REMOVE
:
359 interface_set_available(iface
, new_state
);
360 if (!new_state
&& dep
->dev
&& dep
->dev
->external
)
361 interface_set_main_dev(iface
, NULL
);
366 interface_set_enabled(iface
, new_state
);
368 case DEV_EVENT_LINK_UP
:
370 case DEV_EVENT_LINK_DOWN
:
371 interface_set_link_state(iface
, new_state
);
373 case DEV_EVENT_TOPO_CHANGE
:
374 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
382 interface_set_available(struct interface
*iface
, bool new_state
)
384 if (iface
->available
== new_state
)
387 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
388 iface
->available
= new_state
;
391 if (iface
->autostart
&& !config_init
)
392 interface_set_up(iface
);
394 __interface_set_down(iface
, true);
398 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
401 list_add(&dep
->list
, &iface_all_users
);
406 list_add(&dep
->list
, &iface
->users
);
407 if (iface
->state
== IFS_UP
)
408 dep
->cb(dep
, iface
, IFEV_UP
);
412 interface_remove_user(struct interface_user
*dep
)
414 list_del_init(&dep
->list
);
419 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
421 struct blob_attr
*cur
;
424 blobmsg_for_each_attr(cur
, list
, rem
) {
425 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
428 if (!blobmsg_check_attr(cur
, NULL
))
431 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
432 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
433 list_add(&c
->head
, &iface
->assignment_classes
);
438 interface_clear_assignment_classes(struct interface
*iface
)
440 while (!list_empty(&iface
->assignment_classes
)) {
441 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
442 struct interface_assignment_class
, head
);
449 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
451 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
452 old
->assignment_length
!= new->assignment_length
||
453 old
->assignment_iface_id_selection
!= new->assignment_iface_id_selection
||
454 (old
->assignment_iface_id_selection
== IFID_FIXED
&&
455 memcmp(&old
->assignment_fixed_iface_id
, &new->assignment_fixed_iface_id
,
456 sizeof(old
->assignment_fixed_iface_id
))) ||
457 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
459 struct interface_assignment_class
*c
;
460 list_for_each_entry(c
, &new->assignment_classes
, head
) {
461 // Compare list entries one-by-one to see if there was a change
462 if (list_empty(&old
->assignment_classes
)) // The new list is longer
468 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
469 struct interface_assignment_class
, head
);
471 if (strcmp(c_old
->name
, c
->name
)) // An entry didn't match
474 list_del(&c_old
->head
);
478 // The old list was longer than the new one or the last entry didn't match
479 if (!list_empty(&old
->assignment_classes
)) {
480 interface_clear_assignment_classes(old
);
484 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
487 old
->assignment_hint
= new->assignment_hint
;
488 old
->assignment_length
= new->assignment_length
;
489 old
->assignment_iface_id_selection
= new->assignment_iface_id_selection
;
490 old
->assignment_fixed_iface_id
= new->assignment_fixed_iface_id
;
491 interface_refresh_assignments(true);
496 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
498 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
499 struct device
*dev
= iface
->l3_dev
.dev
;
506 interface_set_main_dev(alias
, dev
);
507 interface_set_available(alias
, true);
510 interface_set_available(alias
, false);
511 interface_set_main_dev(alias
, NULL
);
514 interface_remove_user(dep
);
523 interface_set_device_config(struct interface
*iface
, struct device
*dev
)
525 if (!dev
|| !dev
->default_config
)
528 if (!iface
->device_config
&&
529 (!dev
->iface_config
|| dev
->config_iface
!= iface
))
532 dev
->config_iface
= iface
;
533 dev
->iface_config
= iface
->device_config
;
534 device_apply_config(dev
, dev
->type
, iface
->config
);
538 interface_claim_device(struct interface
*iface
)
540 struct interface
*parent
;
541 struct device
*dev
= NULL
;
543 if (iface
->parent_iface
.iface
)
544 interface_remove_user(&iface
->parent_iface
);
546 if (iface
->parent_ifname
) {
547 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
548 iface
->parent_iface
.cb
= interface_alias_cb
;
549 interface_add_user(&iface
->parent_iface
, parent
);
550 } else if (iface
->ifname
&&
551 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
552 dev
= device_get(iface
->ifname
, true);
553 interface_set_device_config(iface
, dev
);
555 dev
= iface
->ext_dev
.dev
;
559 interface_set_main_dev(iface
, dev
);
561 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
562 interface_set_available(iface
, true);
566 interface_cleanup_state(struct interface
*iface
)
568 interface_set_available(iface
, false);
570 interface_flush_state(iface
);
571 interface_clear_errors(iface
);
572 interface_set_proto_state(iface
, NULL
);
574 interface_set_main_dev(iface
, NULL
);
575 interface_set_l3_dev(iface
, NULL
);
579 interface_cleanup(struct interface
*iface
)
581 struct interface_user
*dep
, *tmp
;
583 uloop_timeout_cancel(&iface
->remove_timer
);
584 device_remove_user(&iface
->ext_dev
);
586 if (iface
->parent_iface
.iface
)
587 interface_remove_user(&iface
->parent_iface
);
589 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
590 interface_remove_user(dep
);
592 interface_clear_assignment_classes(iface
);
593 interface_ip_flush(&iface
->config_ip
);
594 interface_cleanup_state(iface
);
598 interface_do_free(struct interface
*iface
)
600 interface_event(iface
, IFEV_FREE
);
601 interface_cleanup(iface
);
603 netifd_ubus_remove_interface(iface
);
604 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
609 interface_do_reload(struct interface
*iface
)
611 interface_event(iface
, IFEV_RELOAD
);
612 interface_cleanup_state(iface
);
613 proto_init_interface(iface
, iface
->config
);
614 interface_claim_device(iface
);
618 interface_handle_config_change(struct interface
*iface
)
620 enum interface_config_state state
= iface
->config_state
;
622 iface
->config_state
= IFC_NORMAL
;
627 interface_do_reload(iface
);
630 interface_do_free(iface
);
633 if (iface
->autostart
&& iface
->available
)
634 interface_set_up(iface
);
638 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
640 struct interface
*iface
= state
->iface
;
644 if (iface
->state
!= IFS_SETUP
) {
645 interface_event(iface
, IFEV_UPDATE
);
649 if (!iface
->l3_dev
.dev
)
650 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
652 interface_ip_set_enabled(&iface
->config_ip
, true);
653 system_flush_routes();
654 iface
->state
= IFS_UP
;
655 iface
->start_time
= system_get_rtime();
656 interface_event(iface
, IFEV_UP
);
657 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
660 if (iface
->state
== IFS_DOWN
)
663 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
664 mark_interface_down(iface
);
665 if (iface
->main_dev
.dev
)
666 device_release(&iface
->main_dev
);
667 if (iface
->l3_dev
.dev
)
668 device_remove_user(&iface
->l3_dev
);
669 interface_handle_config_change(iface
);
671 case IFPEV_LINK_LOST
:
672 if (iface
->state
!= IFS_UP
)
675 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
676 mark_interface_down(iface
);
677 iface
->state
= IFS_SETUP
;
683 interface_write_resolv_conf();
686 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
689 iface
->proto
->free(iface
->proto
);
692 iface
->state
= IFS_DOWN
;
693 iface
->proto
= state
;
697 state
->proto_event
= interface_proto_cb
;
698 state
->iface
= iface
;
702 interface_alloc(const char *name
, struct blob_attr
*config
)
704 struct interface
*iface
;
705 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
706 struct blob_attr
*cur
;
707 const char *proto_name
= NULL
;
709 bool force_link
= false;
711 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
712 iface
->name
= strcpy(iface_name
, name
);
713 INIT_LIST_HEAD(&iface
->errors
);
714 INIT_LIST_HEAD(&iface
->users
);
715 INIT_LIST_HEAD(&iface
->hotplug_list
);
716 INIT_LIST_HEAD(&iface
->assignment_classes
);
717 interface_ip_init(iface
);
718 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
719 iface
->config_ip
.enabled
= false;
721 iface
->main_dev
.cb
= interface_cb
;
722 iface
->ext_dev
.cb
= interface_ext_cb
;
724 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
725 blob_data(config
), blob_len(config
));
727 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
728 proto_name
= blobmsg_data(cur
);
730 proto_attach_interface(iface
, proto_name
);
731 if (iface
->proto_handler
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
)
734 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
735 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], force_link
);
736 iface
->proto_ip
.no_defaultroute
=
737 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
738 iface
->proto_ip
.no_dns
=
739 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
741 if ((cur
= tb
[IFACE_ATTR_DNS
]))
742 interface_add_dns_server_list(&iface
->config_ip
, cur
);
744 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
745 interface_add_dns_search_list(&iface
->config_ip
, cur
);
747 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
748 iface
->metric
= blobmsg_get_u32(cur
);
750 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
751 iface
->assignment_length
= blobmsg_get_u32(cur
);
754 iface
->assignment_iface_id_selection
= IFID_FIXED
;
755 iface
->assignment_fixed_iface_id
= in6addr_any
;
756 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
758 if ((cur
= tb
[IFACE_ATTR_IP6IFACEID
])) {
759 const char *ifaceid
= blobmsg_data(cur
);
760 if (!strcmp(ifaceid
, "random")) {
761 iface
->assignment_iface_id_selection
= IFID_RANDOM
;
763 else if (!strcmp(ifaceid
, "eui64")) {
764 iface
->assignment_iface_id_selection
= IFID_EUI64
;
767 /* we expect an IPv6 address with network id zero here -> fixed iface id
768 if we cannot parse -> revert to iface id 1 */
769 if (inet_pton(AF_INET6
,ifaceid
,&iface
->assignment_fixed_iface_id
) != 1 ||
770 iface
->assignment_fixed_iface_id
.s6_addr32
[0] != 0 ||
771 iface
->assignment_fixed_iface_id
.s6_addr32
[1] != 0) {
772 iface
->assignment_fixed_iface_id
= in6addr_any
;
773 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
774 netifd_log_message(L_WARNING
, "Failed to parse ip6ifaceid for interface '%s', \
775 falling back to iface id 1.\n", iface
->name
);
780 iface
->assignment_hint
= -1;
781 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
782 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
783 ~((1 << (64 - iface
->assignment_length
)) - 1);
785 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
786 interface_add_assignment_classes(iface
, cur
);
789 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
790 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
791 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
794 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
795 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
796 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
799 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
801 iface
->config_autostart
= iface
->autostart
;
805 void interface_set_dynamic(struct interface
*iface
)
807 iface
->dynamic
= true;
808 iface
->autostart
= true;
809 iface
->node
.version
= -1; // Don't delete on reload
812 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
814 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
815 struct blob_attr
*cur
;
817 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
818 blob_data(config
), blob_len(config
));
821 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
822 iface
->parent_ifname
= blobmsg_data(cur
);
824 if (!iface
->parent_ifname
)
827 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
828 iface
->ifname
= blobmsg_data(cur
);
832 iface
->config
= config
;
833 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
838 interface_add(struct interface
*iface
, struct blob_attr
*config
)
840 __interface_add(iface
, config
, false);
844 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
846 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
849 return __interface_add(iface
, config
, true);
853 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
855 bool enabled
= iface
->config_ip
.enabled
;
856 bool claimed
= iface
->l3_dev
.claimed
;
858 if (iface
->l3_dev
.dev
== dev
)
861 interface_ip_set_enabled(&iface
->config_ip
, false);
862 interface_ip_flush(&iface
->proto_ip
);
863 device_add_user(&iface
->l3_dev
, dev
);
867 if (device_claim(&iface
->l3_dev
) < 0)
870 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
875 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
877 bool claimed
= iface
->l3_dev
.claimed
;
879 if (iface
->main_dev
.dev
== dev
)
882 interface_set_available(iface
, false);
883 device_add_user(&iface
->main_dev
, dev
);
885 interface_set_link_state(iface
, false);
890 if (device_claim(&iface
->l3_dev
) < 0)
894 if (!iface
->l3_dev
.dev
)
895 interface_set_l3_dev(iface
, dev
);
899 interface_remove_link(struct interface
*iface
, struct device
*dev
)
901 struct device
*mdev
= iface
->main_dev
.dev
;
903 if (mdev
&& mdev
->hotplug_ops
)
904 return mdev
->hotplug_ops
->del(mdev
, dev
);
906 if (dev
== iface
->ext_dev
.dev
)
907 device_remove_user(&iface
->ext_dev
);
909 if (!iface
->main_dev
.hotplug
)
910 return UBUS_STATUS_INVALID_ARGUMENT
;
912 if (dev
!= iface
->main_dev
.dev
)
913 return UBUS_STATUS_INVALID_ARGUMENT
;
915 interface_set_main_dev(iface
, NULL
);
920 interface_add_link(struct interface
*iface
, struct device
*dev
, bool link_ext
)
922 struct device
*mdev
= iface
->main_dev
.dev
;
927 if (iface
->main_dev
.hotplug
)
928 device_remove_user(&iface
->main_dev
);
931 if (mdev
->hotplug_ops
)
932 return mdev
->hotplug_ops
->add(mdev
, dev
);
934 return UBUS_STATUS_NOT_SUPPORTED
;
938 device_add_user(&iface
->ext_dev
, dev
);
940 interface_set_main_dev(iface
, dev
);
941 iface
->main_dev
.hotplug
= true;
946 interface_handle_link(struct interface
*iface
, const char *name
, bool add
, bool link_ext
)
953 dev
= device_get(name
, add
? (link_ext
? 2 : 1) : 0);
955 ret
= UBUS_STATUS_NOT_FOUND
;
960 interface_set_device_config(iface
, dev
);
961 device_set_present(dev
, true);
963 ret
= interface_add_link(iface
, dev
, link_ext
);
965 ret
= interface_remove_link(iface
, dev
);
975 interface_set_up(struct interface
*iface
)
979 iface
->autostart
= true;
981 if (iface
->state
!= IFS_DOWN
)
984 interface_clear_errors(iface
);
985 if (!iface
->available
) {
986 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
990 if (iface
->main_dev
.dev
) {
991 ret
= device_claim(&iface
->main_dev
);
993 interface_check_state(iface
);
996 ret
= __interface_set_up(iface
);
1002 interface_set_down(struct interface
*iface
)
1005 vlist_for_each_element(&interfaces
, iface
, node
)
1006 __interface_set_down(iface
, false);
1008 iface
->autostart
= false;
1009 __interface_set_down(iface
, false);
1016 interface_start_pending(void)
1018 struct interface
*iface
;
1020 vlist_for_each_element(&interfaces
, iface
, node
) {
1021 if (iface
->available
&& iface
->autostart
)
1022 interface_set_up(iface
);
1027 set_config_state(struct interface
*iface
, enum interface_config_state s
)
1029 iface
->config_state
= s
;
1030 if (iface
->state
== IFS_DOWN
)
1031 interface_handle_config_change(iface
);
1033 __interface_set_down(iface
, false);
1037 interface_update_start(struct interface
*iface
)
1040 interface_ip_update_start(&iface
->proto_ip
);
1044 interface_update_complete(struct interface
*iface
)
1046 interface_ip_update_complete(&iface
->proto_ip
);
1050 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
1052 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
1053 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
1057 interface_device_config_changed(struct interface
*if_old
, struct interface
*if_new
)
1059 struct blob_attr
*ntb
[__DEV_ATTR_MAX
];
1060 struct blob_attr
*otb
[__DEV_ATTR_MAX
];
1061 struct device
*dev
= if_old
->main_dev
.dev
;
1062 unsigned long diff
= 0;
1064 BUILD_BUG_ON(sizeof(diff
) < __DEV_ATTR_MAX
/ 8);
1069 if (if_old
->device_config
!= if_new
->device_config
)
1072 if (!if_new
->device_config
)
1075 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb
,
1076 blob_data(if_old
->config
), blob_len(if_old
->config
));
1078 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, ntb
,
1079 blob_data(if_new
->config
), blob_len(if_new
->config
));
1081 uci_blob_diff(ntb
, otb
, &device_attr_list
, &diff
);
1086 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
1088 struct blob_attr
*old_config
= if_old
->config
;
1089 bool reload
= false, reload_ip
= false;
1091 #define FIELD_CHANGED_STR(field) \
1092 ((!!if_old->field != !!if_new->field) || \
1094 strcmp(if_old->field, if_new->field) != 0))
1096 if (FIELD_CHANGED_STR(parent_ifname
)) {
1097 if (if_old
->parent_iface
.iface
)
1098 interface_remove_user(&if_old
->parent_iface
);
1102 if (!reload
&& interface_device_config_changed(if_old
, if_new
))
1105 if (FIELD_CHANGED_STR(ifname
) ||
1106 if_old
->proto_handler
!= if_new
->proto_handler
)
1109 if (!if_old
->proto_handler
->config_params
)
1110 D(INTERFACE
, "No config parameters for interface '%s'\n",
1112 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
1113 if_old
->proto_handler
->config_params
))
1116 #define UPDATE(field, __var) ({ \
1117 bool __changed = (if_old->field != if_new->field); \
1118 if_old->field = if_new->field; \
1119 __var |= __changed; \
1122 if_old
->config
= if_new
->config
;
1123 if (if_old
->config_autostart
!= if_new
->config_autostart
) {
1124 if (if_old
->config_autostart
)
1127 if_old
->autostart
= if_new
->config_autostart
;
1130 if_old
->device_config
= if_new
->device_config
;
1131 if_old
->config_autostart
= if_new
->config_autostart
;
1132 if_old
->ifname
= if_new
->ifname
;
1133 if_old
->parent_ifname
= if_new
->parent_ifname
;
1134 if_old
->proto_handler
= if_new
->proto_handler
;
1135 if_old
->force_link
= if_new
->force_link
;
1137 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1138 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1140 UPDATE(metric
, reload_ip
);
1141 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1142 UPDATE(ip4table
, reload_ip
);
1143 UPDATE(ip6table
, reload_ip
);
1144 interface_merge_assignment_data(if_old
, if_new
);
1149 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1151 interface_clear_errors(if_old
);
1152 set_config_state(if_old
, IFC_RELOAD
);
1157 bool config_ip_enabled
= if_old
->config_ip
.enabled
;
1158 bool proto_ip_enabled
= if_old
->proto_ip
.enabled
;
1160 interface_ip_set_enabled(&if_old
->config_ip
, false);
1161 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1162 interface_ip_set_enabled(&if_old
->proto_ip
, proto_ip_enabled
);
1163 interface_ip_set_enabled(&if_old
->config_ip
, config_ip_enabled
);
1166 interface_write_resolv_conf();
1167 if (if_old
->main_dev
.dev
)
1168 interface_check_state(if_old
);
1171 if_new
->config
= NULL
;
1172 interface_cleanup(if_new
);
1178 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1179 struct vlist_node
*node_old
)
1181 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1182 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1184 if (node_old
&& node_new
) {
1185 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1186 interface_change_config(if_old
, if_new
);
1187 } else if (node_old
) {
1188 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1189 set_config_state(if_old
, IFC_REMOVE
);
1190 } else if (node_new
) {
1191 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1192 proto_init_interface(if_new
, if_new
->config
);
1193 interface_claim_device(if_new
);
1194 netifd_ubus_add_interface(if_new
);
1200 interface_init_list(void)
1202 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1203 interfaces
.keep_old
= true;
1204 interfaces
.no_delete
= true;