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.
17 #include <sys/types.h>
22 #include "interface.h"
23 #include "interface-ip.h"
29 struct vlist_tree interfaces
;
30 static LIST_HEAD(iface_all_users
);
34 IFACE_ATTR_IFNAME
, /* Backward compatibility */
38 IFACE_ATTR_JAIL_DEVICE
,
39 IFACE_ATTR_JAIL_IFNAME
,
40 IFACE_ATTR_HOST_DEVICE
,
41 IFACE_ATTR_DEFAULTROUTE
,
44 IFACE_ATTR_DNS_SEARCH
,
45 IFACE_ATTR_DNS_METRIC
,
54 IFACE_ATTR_IP6IFACEID
,
55 IFACE_ATTR_FORCE_LINK
,
60 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
61 [IFACE_ATTR_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
62 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
63 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
64 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
65 [IFACE_ATTR_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
66 [IFACE_ATTR_JAIL_DEVICE
] = { .name
= "jail_device", .type
= BLOBMSG_TYPE_STRING
},
67 [IFACE_ATTR_JAIL_IFNAME
] = { .name
= "jail_ifname", .type
= BLOBMSG_TYPE_STRING
},
68 [IFACE_ATTR_HOST_DEVICE
] = { .name
= "host_device", .type
= BLOBMSG_TYPE_STRING
},
69 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
70 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
71 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
72 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
73 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
74 [IFACE_ATTR_DNS_METRIC
] = { .name
= "dns_metric", .type
= BLOBMSG_TYPE_INT32
},
75 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
76 [IFACE_ATTR_IP6ASSIGN
] = { .name
= "ip6assign", .type
= BLOBMSG_TYPE_INT32
},
77 [IFACE_ATTR_IP6HINT
] = { .name
= "ip6hint", .type
= BLOBMSG_TYPE_STRING
},
78 [IFACE_ATTR_IP4TABLE
] = { .name
= "ip4table", .type
= BLOBMSG_TYPE_STRING
},
79 [IFACE_ATTR_IP6TABLE
] = { .name
= "ip6table", .type
= BLOBMSG_TYPE_STRING
},
80 [IFACE_ATTR_IP6CLASS
] = { .name
= "ip6class", .type
= BLOBMSG_TYPE_ARRAY
},
81 [IFACE_ATTR_DELEGATE
] = { .name
= "delegate", .type
= BLOBMSG_TYPE_BOOL
},
82 [IFACE_ATTR_IP6IFACEID
] = { .name
= "ip6ifaceid", .type
= BLOBMSG_TYPE_STRING
},
83 [IFACE_ATTR_FORCE_LINK
] = { .name
= "force_link", .type
= BLOBMSG_TYPE_BOOL
},
84 [IFACE_ATTR_IP6WEIGHT
] = { .name
= "ip6weight", .type
= BLOBMSG_TYPE_INT32
},
87 const struct uci_blob_param_list interface_attr_list
= {
88 .n_params
= IFACE_ATTR_MAX
,
89 .params
= iface_attrs
,
93 interface_set_main_dev(struct interface
*iface
, struct device
*dev
);
95 interface_event(struct interface
*iface
, enum interface_event ev
);
98 interface_error_flush(struct interface
*iface
)
100 struct interface_error
*error
, *tmp
;
102 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
103 list_del(&error
->list
);
109 interface_force_link(struct interface
*iface
)
111 struct device
*dev
= iface
->main_dev
.dev
;
113 if (dev
&& dev
->settings
.auth
)
116 return iface
->force_link
;
120 interface_clear_errors(struct interface
*iface
)
122 /* don't flush the errors in case the configured protocol handler matches the
123 running protocol handler and is having the last error capability */
124 if (!(iface
->proto
&&
125 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
126 (iface
->proto
->handler
->name
== iface
->proto_handler
->name
)))
127 interface_error_flush(iface
);
130 void interface_add_error(struct interface
*iface
, const char *subsystem
,
131 const char *code
, const char **data
, int n_data
)
133 struct interface_error
*error
;
136 char *dest
, *d_subsys
, *d_code
;
138 /* if the configured protocol handler has the last error support capability,
139 errors should only be added if the running protocol handler matches the
142 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
143 (iface
->proto
->handler
->name
!= iface
->proto_handler
->name
))
147 len
= n_data
* sizeof(char *);
148 datalen
= alloca(len
);
149 for (i
= 0; i
< n_data
; i
++) {
150 datalen
[i
] = strlen(data
[i
]) + 1;
155 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
156 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
157 &d_code
, code
? strlen(code
) + 1 : 0);
161 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
162 protocol can't be established (e.g auth failure) */
163 if (iface
->proto_handler
->flags
& PROTO_FLAG_LASTERROR
)
164 interface_error_flush(iface
);
166 list_add_tail(&error
->list
, &iface
->errors
);
168 dest
= (char *) &error
->data
[n_data
+ 1];
169 for (i
= 0; i
< n_data
; i
++) {
170 error
->data
[i
] = dest
;
171 memcpy(dest
, data
[i
], datalen
[i
]);
174 error
->data
[n_data
] = NULL
;
177 error
->subsystem
= strcpy(d_subsys
, subsystem
);
180 error
->code
= strcpy(d_code
, code
);
184 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
186 avl_delete(&iface
->data
, &data
->node
);
191 interface_data_flush(struct interface
*iface
)
193 struct interface_data
*d
, *tmp
;
195 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
196 interface_data_del(iface
, d
);
200 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
202 struct interface_data
*n
, *o
;
204 if (!blobmsg_check_attr(data
, true))
205 return UBUS_STATUS_INVALID_ARGUMENT
;
207 const char *name
= blobmsg_name(data
);
208 unsigned len
= blob_pad_len(data
);
210 o
= avl_find_element(&iface
->data
, name
, o
, node
);
212 if (blob_pad_len(o
->data
) == len
&& !memcmp(o
->data
, data
, len
))
215 interface_data_del(iface
, o
);
218 n
= calloc(1, sizeof(*n
) + len
);
220 return UBUS_STATUS_UNKNOWN_ERROR
;
222 memcpy(n
->data
, data
, len
);
223 n
->node
.key
= blobmsg_name(n
->data
);
224 avl_insert(&iface
->data
, &n
->node
);
226 iface
->updated
|= IUF_DATA
;
230 int interface_parse_data(struct interface
*iface
, const struct blob_attr
*attr
)
232 struct blob_attr
*cur
;
237 blob_for_each_attr(cur
, attr
, rem
) {
238 ret
= interface_add_data(iface
, cur
);
243 if (iface
->updated
&& iface
->state
== IFS_UP
)
244 interface_event(iface
, IFEV_UPDATE
);
250 interface_event(struct interface
*iface
, enum interface_event ev
)
252 struct interface_user
*dep
, *tmp
;
253 struct device
*adev
= NULL
;
255 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
256 dep
->cb(dep
, iface
, ev
);
258 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
259 dep
->cb(dep
, iface
, ev
);
263 interface_error_flush(iface
);
264 adev
= iface
->l3_dev
.dev
;
268 alias_notify_device(iface
->name
, adev
);
276 interface_flush_state(struct interface
*iface
)
278 if (iface
->l3_dev
.dev
)
279 device_release(&iface
->l3_dev
);
280 interface_data_flush(iface
);
284 mark_interface_down(struct interface
*iface
)
286 enum interface_state state
= iface
->state
;
288 if (state
== IFS_DOWN
)
291 iface
->link_up_event
= false;
292 iface
->state
= IFS_DOWN
;
296 interface_event(iface
, IFEV_DOWN
);
299 interface_event(iface
, IFEV_UP_FAILED
);
304 interface_ip_set_enabled(&iface
->config_ip
, false);
305 interface_ip_set_enabled(&iface
->proto_ip
, false);
306 interface_ip_flush(&iface
->proto_ip
);
307 interface_flush_state(iface
);
308 system_flush_routes();
312 __set_config_state(struct interface
*iface
, enum interface_config_state s
)
314 iface
->config_state
= s
;
318 __interface_set_down(struct interface
*iface
, bool force
)
320 enum interface_state state
= iface
->state
;
324 iface
->state
= IFS_TEARDOWN
;
326 __set_config_state(iface
, IFC_REMOVE
);
329 interface_event(iface
, IFEV_DOWN
);
331 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
333 interface_flush_state(iface
);
337 if (iface
->main_dev
.dev
)
338 device_release(&iface
->main_dev
);
347 __interface_set_up(struct interface
*iface
)
351 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
353 iface
->state
= IFS_SETUP
;
354 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
356 mark_interface_down(iface
);
362 interface_check_state(struct interface
*iface
)
364 bool link_state
= iface
->link_state
|| interface_force_link(iface
);
366 switch (iface
->state
) {
369 if (!iface
->enabled
|| !link_state
) {
370 iface
->state
= IFS_TEARDOWN
;
372 __set_config_state(iface
, IFC_REMOVE
);
374 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
378 if (!iface
->available
)
381 if (iface
->autostart
&& iface
->enabled
&& link_state
&& !config_init
)
382 __interface_set_up(iface
);
390 interface_set_enabled(struct interface
*iface
, bool new_state
)
392 if (iface
->enabled
== new_state
)
395 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
396 iface
->enabled
= new_state
;
397 interface_check_state(iface
);
401 interface_set_link_state(struct interface
*iface
, bool new_state
)
403 if (iface
->link_state
== new_state
)
406 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
407 iface
->link_state
= new_state
;
408 interface_check_state(iface
);
410 if (new_state
&& interface_force_link(iface
) &&
411 iface
->state
== IFS_UP
&& !iface
->link_up_event
) {
412 interface_event(iface
, IFEV_LINK_UP
);
413 iface
->link_up_event
= true;
418 interface_ext_dev_cb(struct device_user
*dep
, enum device_event ev
)
420 if (ev
== DEV_EVENT_REMOVE
)
421 device_remove_user(dep
);
425 interface_main_dev_cb(struct device_user
*dep
, enum device_event ev
)
427 struct interface
*iface
;
429 iface
= container_of(dep
, struct interface
, main_dev
);
432 interface_set_available(iface
, true);
434 case DEV_EVENT_REMOVE
:
435 interface_set_available(iface
, false);
436 if (dep
->dev
&& dep
->dev
->external
)
437 interface_set_main_dev(iface
, NULL
);
440 interface_set_enabled(iface
, true);
443 interface_set_enabled(iface
, false);
445 case DEV_EVENT_AUTH_UP
:
446 case DEV_EVENT_LINK_UP
:
447 case DEV_EVENT_LINK_DOWN
:
448 interface_set_link_state(iface
, device_link_active(dep
->dev
));
450 case DEV_EVENT_TOPO_CHANGE
:
451 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
459 interface_l3_dev_cb(struct device_user
*dep
, enum device_event ev
)
461 struct interface
*iface
;
463 iface
= container_of(dep
, struct interface
, l3_dev
);
464 if (iface
->l3_dev
.dev
== iface
->main_dev
.dev
)
468 case DEV_EVENT_LINK_DOWN
:
469 if (iface
->proto_handler
->flags
& PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN
)
470 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
478 interface_set_available(struct interface
*iface
, bool new_state
)
480 if (iface
->available
== new_state
)
483 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
484 iface
->available
= new_state
;
487 if (iface
->autostart
&& !config_init
)
488 interface_set_up(iface
);
490 __interface_set_down(iface
, true);
494 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
497 list_add(&dep
->list
, &iface_all_users
);
502 list_add(&dep
->list
, &iface
->users
);
503 if (iface
->state
== IFS_UP
)
504 dep
->cb(dep
, iface
, IFEV_UP
);
508 interface_remove_user(struct interface_user
*dep
)
510 list_del_init(&dep
->list
);
515 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
517 struct blob_attr
*cur
;
520 blobmsg_for_each_attr(cur
, list
, rem
) {
521 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
524 if (!blobmsg_check_attr(cur
, false))
527 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
528 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
529 list_add(&c
->head
, &iface
->assignment_classes
);
534 interface_clear_assignment_classes(struct interface
*iface
)
536 while (!list_empty(&iface
->assignment_classes
)) {
537 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
538 struct interface_assignment_class
, head
);
545 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
547 bool changed
= (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_weight
!= new->assignment_weight
||
551 (old
->assignment_iface_id_selection
== IFID_FIXED
&&
552 memcmp(&old
->assignment_fixed_iface_id
, &new->assignment_fixed_iface_id
,
553 sizeof(old
->assignment_fixed_iface_id
))) ||
554 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
556 struct interface_assignment_class
*c
;
557 list_for_each_entry(c
, &new->assignment_classes
, head
) {
558 /* Compare list entries one-by-one to see if there was a change */
559 if (list_empty(&old
->assignment_classes
)) /* The new list is longer */
565 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
566 struct interface_assignment_class
, head
);
568 if (strcmp(c_old
->name
, c
->name
)) /* An entry didn't match */
571 list_del(&c_old
->head
);
575 /* The old list was longer than the new one or the last entry didn't match */
576 if (!list_empty(&old
->assignment_classes
)) {
577 interface_clear_assignment_classes(old
);
581 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
584 old
->assignment_hint
= new->assignment_hint
;
585 old
->assignment_length
= new->assignment_length
;
586 old
->assignment_iface_id_selection
= new->assignment_iface_id_selection
;
587 old
->assignment_fixed_iface_id
= new->assignment_fixed_iface_id
;
588 old
->assignment_weight
= new->assignment_weight
;
589 interface_refresh_assignments(true);
594 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
596 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
597 struct device
*dev
= iface
->l3_dev
.dev
;
604 interface_set_main_dev(alias
, dev
);
605 interface_set_available(alias
, true);
609 interface_set_available(alias
, false);
610 interface_set_main_dev(alias
, NULL
);
613 interface_remove_user(dep
);
621 interface_set_device_config(struct interface
*iface
, struct device
*dev
)
623 if (!dev
|| !dev
->default_config
)
626 if (!iface
->device_config
&&
627 (!dev
->iface_config
|| dev
->config_iface
!= iface
))
630 dev
->config_iface
= iface
;
631 dev
->iface_config
= iface
->device_config
;
632 device_apply_config(dev
, dev
->type
, iface
->config
);
636 interface_claim_device(struct interface
*iface
)
638 struct interface
*parent
;
639 struct device
*dev
= NULL
;
641 if (iface
->parent_iface
.iface
)
642 interface_remove_user(&iface
->parent_iface
);
644 if (iface
->parent_ifname
) {
645 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
646 iface
->parent_iface
.cb
= interface_alias_cb
;
647 interface_add_user(&iface
->parent_iface
, parent
);
648 } else if (iface
->device
&&
649 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
650 dev
= device_get(iface
->device
, true);
651 interface_set_device_config(iface
, dev
);
653 dev
= iface
->ext_dev
.dev
;
657 interface_set_main_dev(iface
, dev
);
659 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
660 interface_set_available(iface
, true);
664 interface_cleanup_state(struct interface
*iface
)
666 interface_set_available(iface
, false);
668 interface_flush_state(iface
);
669 interface_clear_errors(iface
);
670 interface_set_proto_state(iface
, NULL
);
672 interface_set_main_dev(iface
, NULL
);
673 interface_set_l3_dev(iface
, NULL
);
677 interface_cleanup(struct interface
*iface
)
679 struct interface_user
*dep
, *tmp
;
681 uloop_timeout_cancel(&iface
->remove_timer
);
682 device_remove_user(&iface
->ext_dev
);
684 if (iface
->parent_iface
.iface
)
685 interface_remove_user(&iface
->parent_iface
);
687 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
688 interface_remove_user(dep
);
690 interface_clear_assignment_classes(iface
);
691 interface_ip_flush(&iface
->config_ip
);
692 interface_cleanup_state(iface
);
696 interface_do_free(struct interface
*iface
)
698 interface_event(iface
, IFEV_FREE
);
699 interface_cleanup(iface
);
701 netifd_ubus_remove_interface(iface
);
702 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
705 if (iface
->jail_device
)
706 free(iface
->jail_device
);
707 if (iface
->host_device
)
708 free(iface
->host_device
);
714 interface_do_reload(struct interface
*iface
)
716 interface_event(iface
, IFEV_RELOAD
);
717 interface_cleanup_state(iface
);
718 proto_init_interface(iface
, iface
->config
);
719 interface_claim_device(iface
);
723 interface_handle_config_change(struct interface
*iface
)
725 enum interface_config_state state
= iface
->config_state
;
727 iface
->config_state
= IFC_NORMAL
;
732 interface_do_reload(iface
);
735 interface_do_free(iface
);
738 if (iface
->autostart
)
739 interface_set_up(iface
);
743 interface_proto_event_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
745 struct interface
*iface
= state
->iface
;
749 if (iface
->state
!= IFS_SETUP
) {
750 if (iface
->state
== IFS_UP
&& iface
->updated
)
751 interface_event(iface
, IFEV_UPDATE
);
755 if (!iface
->l3_dev
.dev
)
756 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
758 interface_ip_set_enabled(&iface
->config_ip
, true);
759 interface_ip_set_enabled(&iface
->proto_ip
, true);
760 system_flush_routes();
761 iface
->state
= IFS_UP
;
762 iface
->start_time
= system_get_rtime();
763 interface_event(iface
, IFEV_UP
);
764 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
767 if (iface
->state
== IFS_DOWN
)
770 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
771 mark_interface_down(iface
);
772 if (iface
->main_dev
.dev
)
773 device_release(&iface
->main_dev
);
774 if (iface
->l3_dev
.dev
)
775 device_remove_user(&iface
->l3_dev
);
776 interface_handle_config_change(iface
);
778 case IFPEV_LINK_LOST
:
779 if (iface
->state
!= IFS_UP
)
782 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
783 mark_interface_down(iface
);
784 iface
->state
= IFS_SETUP
;
790 interface_write_resolv_conf(iface
->jail
);
793 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
796 iface
->proto
->free(iface
->proto
);
799 iface
->state
= IFS_DOWN
;
800 iface
->proto
= state
;
804 state
->proto_event
= interface_proto_event_cb
;
805 state
->iface
= iface
;
809 interface_alloc(const char *name
, struct blob_attr
*config
, bool dynamic
)
811 struct interface
*iface
;
812 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
813 struct blob_attr
*cur
;
814 const char *proto_name
= NULL
;
816 bool force_link
= false;
818 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
819 iface
->name
= strcpy(iface_name
, name
);
820 INIT_LIST_HEAD(&iface
->errors
);
821 INIT_LIST_HEAD(&iface
->users
);
822 INIT_LIST_HEAD(&iface
->hotplug_list
);
823 INIT_LIST_HEAD(&iface
->assignment_classes
);
824 interface_ip_init(iface
);
825 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
826 iface
->config_ip
.enabled
= false;
828 iface
->main_dev
.cb
= interface_main_dev_cb
;
829 iface
->l3_dev
.cb
= interface_l3_dev_cb
;
830 iface
->ext_dev
.cb
= interface_ext_dev_cb
;
832 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
833 blob_data(config
), blob_len(config
));
835 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
836 proto_name
= blobmsg_data(cur
);
838 proto_attach_interface(iface
, proto_name
);
839 if (iface
->proto_handler
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
)
842 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
843 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], force_link
);
844 iface
->dynamic
= dynamic
;
845 iface
->proto_ip
.no_defaultroute
=
846 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
847 iface
->proto_ip
.no_dns
=
848 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
850 if ((cur
= tb
[IFACE_ATTR_DNS
]))
851 interface_add_dns_server_list(&iface
->config_ip
, cur
);
853 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
854 interface_add_dns_search_list(&iface
->config_ip
, cur
);
856 if ((cur
= tb
[IFACE_ATTR_DNS_METRIC
]))
857 iface
->dns_metric
= blobmsg_get_u32(cur
);
859 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
860 iface
->metric
= blobmsg_get_u32(cur
);
862 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
863 iface
->assignment_length
= blobmsg_get_u32(cur
);
866 iface
->assignment_iface_id_selection
= IFID_FIXED
;
867 iface
->assignment_fixed_iface_id
= in6addr_any
;
868 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
870 if ((cur
= tb
[IFACE_ATTR_IP6IFACEID
])) {
871 const char *ifaceid
= blobmsg_data(cur
);
872 if (!strcmp(ifaceid
, "random")) {
873 iface
->assignment_iface_id_selection
= IFID_RANDOM
;
875 else if (!strcmp(ifaceid
, "eui64")) {
876 iface
->assignment_iface_id_selection
= IFID_EUI64
;
879 /* we expect an IPv6 address with network id zero here -> fixed iface id
880 if we cannot parse -> revert to iface id 1 */
881 if (inet_pton(AF_INET6
,ifaceid
,&iface
->assignment_fixed_iface_id
) != 1 ||
882 iface
->assignment_fixed_iface_id
.s6_addr32
[0] != 0 ||
883 iface
->assignment_fixed_iface_id
.s6_addr32
[1] != 0) {
884 iface
->assignment_fixed_iface_id
= in6addr_any
;
885 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
886 netifd_log_message(L_WARNING
, "Failed to parse ip6ifaceid for interface '%s', \
887 falling back to iface id 1.\n", iface
->name
);
892 iface
->assignment_hint
= -1;
893 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
894 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
895 ~((1 << (64 - iface
->assignment_length
)) - 1);
897 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
898 interface_add_assignment_classes(iface
, cur
);
900 if ((cur
= tb
[IFACE_ATTR_IP6WEIGHT
]))
901 iface
->assignment_weight
= blobmsg_get_u32(cur
);
903 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
904 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
905 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
908 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
909 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
910 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
913 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
915 iface
->config_autostart
= iface
->autostart
;
918 if ((cur
= tb
[IFACE_ATTR_JAIL
])) {
919 iface
->jail
= strdup(blobmsg_get_string(cur
));
920 iface
->autostart
= false;
923 iface
->jail_device
= NULL
;
924 if ((cur
= tb
[IFACE_ATTR_JAIL_DEVICE
]))
925 iface
->jail_device
= strdup(blobmsg_get_string(cur
));
926 else if ((cur
= tb
[IFACE_ATTR_JAIL_IFNAME
]))
927 iface
->jail_device
= strdup(blobmsg_get_string(cur
));
929 iface
->host_device
= NULL
;
930 if ((cur
= tb
[IFACE_ATTR_HOST_DEVICE
]))
931 iface
->host_device
= strdup(blobmsg_get_string(cur
));
936 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
938 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
939 struct blob_attr
*cur
;
942 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
943 blob_data(config
), blob_len(config
));
946 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
947 iface
->parent_ifname
= blobmsg_data(cur
);
949 if (!iface
->parent_ifname
)
952 cur
= tb
[IFACE_ATTR_DEVICE
];
954 cur
= tb
[IFACE_ATTR_IFNAME
];
956 iface
->device
= blobmsg_data(cur
);
959 if (iface
->dynamic
) {
960 name
= strdup(iface
->name
);
966 iface
->config
= config
;
967 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
970 iface
= vlist_find(&interfaces
, name
, iface
, node
);
973 /* Don't delete dynamic interface on reload */
975 iface
->node
.version
= -1;
982 interface_add(struct interface
*iface
, struct blob_attr
*config
)
984 return __interface_add(iface
, config
, false);
988 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
990 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
993 return __interface_add(iface
, config
, true);
997 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
999 bool enabled
= iface
->config_ip
.enabled
;
1000 bool claimed
= iface
->l3_dev
.claimed
;
1002 if (iface
->l3_dev
.dev
== dev
)
1005 interface_ip_set_enabled(&iface
->config_ip
, false);
1006 interface_ip_set_enabled(&iface
->proto_ip
, false);
1007 interface_ip_flush(&iface
->proto_ip
);
1008 device_add_user(&iface
->l3_dev
, dev
);
1012 if (device_claim(&iface
->l3_dev
) < 0)
1015 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
1016 interface_ip_set_enabled(&iface
->proto_ip
, enabled
);
1021 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
1023 bool claimed
= iface
->l3_dev
.claimed
;
1025 if (iface
->main_dev
.dev
== dev
)
1028 interface_set_available(iface
, false);
1029 device_add_user(&iface
->main_dev
, dev
);
1031 interface_set_link_state(iface
, false);
1036 if (device_claim(&iface
->l3_dev
) < 0)
1040 if (!iface
->l3_dev
.dev
)
1041 interface_set_l3_dev(iface
, dev
);
1045 interface_remove_link(struct interface
*iface
, struct device
*dev
,
1046 struct blob_attr
*vlan
)
1048 struct device
*mdev
= iface
->main_dev
.dev
;
1050 if (mdev
&& mdev
->hotplug_ops
)
1051 return mdev
->hotplug_ops
->del(mdev
, dev
, vlan
);
1053 if (dev
== iface
->ext_dev
.dev
)
1054 device_remove_user(&iface
->ext_dev
);
1056 if (!iface
->main_dev
.hotplug
)
1057 return UBUS_STATUS_INVALID_ARGUMENT
;
1059 if (dev
!= iface
->main_dev
.dev
)
1060 return UBUS_STATUS_INVALID_ARGUMENT
;
1062 interface_set_main_dev(iface
, NULL
);
1067 interface_add_link(struct interface
*iface
, struct device
*dev
,
1068 struct blob_attr
*vlan
, bool link_ext
)
1070 struct device
*mdev
= iface
->main_dev
.dev
;
1075 if (iface
->main_dev
.hotplug
)
1076 device_remove_user(&iface
->main_dev
);
1079 if (mdev
->hotplug_ops
)
1080 return mdev
->hotplug_ops
->add(mdev
, dev
, vlan
);
1082 return UBUS_STATUS_NOT_SUPPORTED
;
1086 device_add_user(&iface
->ext_dev
, dev
);
1088 interface_set_main_dev(iface
, dev
);
1089 iface
->main_dev
.hotplug
= true;
1094 interface_handle_link(struct interface
*iface
, const char *name
,
1095 struct blob_attr
*vlan
, bool add
, bool link_ext
)
1099 dev
= device_get(name
, add
? (link_ext
? 2 : 1) : 0);
1101 return UBUS_STATUS_NOT_FOUND
;
1104 return interface_remove_link(iface
, dev
, vlan
);
1106 interface_set_device_config(iface
, dev
);
1108 device_set_present(dev
, true);
1110 return interface_add_link(iface
, dev
, vlan
, link_ext
);
1114 interface_set_up(struct interface
*iface
)
1117 const char *error
= NULL
;
1119 iface
->autostart
= true;
1121 if (iface
->state
!= IFS_DOWN
)
1124 interface_clear_errors(iface
);
1125 if (iface
->available
) {
1126 if (iface
->main_dev
.dev
) {
1127 ret
= device_claim(&iface
->main_dev
);
1129 interface_check_state(iface
);
1131 error
= "DEVICE_CLAIM_FAILED";
1133 ret
= __interface_set_up(iface
);
1135 error
= "SETUP_FAILED";
1138 error
= "NO_DEVICE";
1141 interface_add_error(iface
, "interface", error
, NULL
, 0);
1145 interface_set_down(struct interface
*iface
)
1148 vlist_for_each_element(&interfaces
, iface
, node
)
1149 __interface_set_down(iface
, false);
1151 iface
->autostart
= false;
1152 __interface_set_down(iface
, false);
1157 interface_renew(struct interface
*iface
)
1159 if (iface
->state
== IFS_TEARDOWN
|| iface
->state
== IFS_DOWN
)
1162 return interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
1166 interface_start_pending(void)
1168 struct interface
*iface
;
1170 vlist_for_each_element(&interfaces
, iface
, node
) {
1171 if (iface
->autostart
)
1172 interface_set_up(iface
);
1177 interface_start_jail(int netns_fd
, const char *jail
)
1179 struct interface
*iface
;
1181 vlist_for_each_element(&interfaces
, iface
, node
) {
1182 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1185 system_link_netns_move(iface
->main_dev
.dev
, netns_fd
, iface
->jail_device
);
1190 interface_stop_jail(int netns_fd
)
1192 struct interface
*iface
;
1195 vlist_for_each_element(&interfaces
, iface
, node
) {
1196 orig_ifname
= iface
->host_device
;
1197 interface_set_down(iface
);
1198 system_link_netns_move(iface
->main_dev
.dev
, netns_fd
, orig_ifname
);
1203 set_config_state(struct interface
*iface
, enum interface_config_state s
)
1205 __set_config_state(iface
, s
);
1206 if (iface
->state
== IFS_DOWN
)
1207 interface_handle_config_change(iface
);
1209 __interface_set_down(iface
, false);
1213 interface_update_start(struct interface
*iface
, const bool keep_old
)
1218 interface_ip_update_start(&iface
->proto_ip
);
1222 interface_update_complete(struct interface
*iface
)
1224 interface_ip_update_complete(&iface
->proto_ip
);
1228 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
1230 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
1231 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
1235 interface_device_config_changed(struct interface
*if_old
, struct interface
*if_new
)
1237 struct blob_attr
*ntb
[__DEV_ATTR_MAX
];
1238 struct blob_attr
*otb
[__DEV_ATTR_MAX
];
1239 struct device
*dev
= if_old
->main_dev
.dev
;
1240 unsigned long diff
= 0;
1242 BUILD_BUG_ON(sizeof(diff
) < __DEV_ATTR_MAX
/ 8);
1247 if (if_old
->device_config
!= if_new
->device_config
)
1250 if (!if_new
->device_config
)
1253 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb
,
1254 blob_data(if_old
->config
), blob_len(if_old
->config
));
1256 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, ntb
,
1257 blob_data(if_new
->config
), blob_len(if_new
->config
));
1259 uci_blob_diff(ntb
, otb
, &device_attr_list
, &diff
);
1264 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
1266 struct blob_attr
*old_config
= if_old
->config
;
1267 bool reload
= false, reload_ip
= false, update_prefix_delegation
= false;
1269 #define FIELD_CHANGED_STR(field) \
1270 ((!!if_old->field != !!if_new->field) || \
1272 strcmp(if_old->field, if_new->field) != 0))
1274 if (FIELD_CHANGED_STR(parent_ifname
)) {
1275 if (if_old
->parent_iface
.iface
)
1276 interface_remove_user(&if_old
->parent_iface
);
1280 if (!reload
&& interface_device_config_changed(if_old
, if_new
))
1283 if (FIELD_CHANGED_STR(device
) ||
1284 if_old
->proto_handler
!= if_new
->proto_handler
)
1287 if (!if_old
->proto_handler
->config_params
)
1288 D(INTERFACE
, "No config parameters for interface '%s'\n",
1290 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
1291 if_old
->proto_handler
->config_params
))
1294 #define UPDATE(field, __var) ({ \
1295 bool __changed = (if_old->field != if_new->field); \
1296 if_old->field = if_new->field; \
1297 __var |= __changed; \
1300 if_old
->config
= if_new
->config
;
1301 if (if_old
->config_autostart
!= if_new
->config_autostart
) {
1302 if (if_old
->config_autostart
)
1305 if_old
->autostart
= if_new
->config_autostart
;
1308 if_old
->device_config
= if_new
->device_config
;
1309 if_old
->config_autostart
= if_new
->config_autostart
;
1313 if_old
->jail
= if_new
->jail
;
1315 if_old
->autostart
= false;
1317 if (if_old
->jail_device
)
1318 free(if_old
->jail_device
);
1320 if_old
->jail_device
= if_new
->jail_device
;
1322 if (if_old
->host_device
)
1323 free(if_old
->host_device
);
1325 if_old
->host_device
= if_new
->host_device
;
1327 if_old
->device
= if_new
->device
;
1328 if_old
->parent_ifname
= if_new
->parent_ifname
;
1329 if_old
->dynamic
= if_new
->dynamic
;
1330 if_old
->proto_handler
= if_new
->proto_handler
;
1331 if_old
->force_link
= if_new
->force_link
;
1332 if_old
->dns_metric
= if_new
->dns_metric
;
1334 if (if_old
->proto_ip
.no_delegation
!= if_new
->proto_ip
.no_delegation
) {
1335 if_old
->proto_ip
.no_delegation
= if_new
->proto_ip
.no_delegation
;
1336 update_prefix_delegation
= true;
1339 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1340 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1342 UPDATE(metric
, reload_ip
);
1343 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1344 UPDATE(ip4table
, reload_ip
);
1345 UPDATE(ip6table
, reload_ip
);
1346 interface_merge_assignment_data(if_old
, if_new
);
1351 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1353 interface_clear_errors(if_old
);
1354 set_config_state(if_old
, IFC_RELOAD
);
1359 bool config_ip_enabled
= if_old
->config_ip
.enabled
;
1360 bool proto_ip_enabled
= if_old
->proto_ip
.enabled
;
1362 interface_ip_set_enabled(&if_old
->config_ip
, false);
1363 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1364 interface_ip_set_enabled(&if_old
->proto_ip
, proto_ip_enabled
);
1365 interface_ip_set_enabled(&if_old
->config_ip
, config_ip_enabled
);
1368 if (update_prefix_delegation
)
1369 interface_update_prefix_delegation(&if_old
->proto_ip
);
1371 interface_write_resolv_conf(if_old
->jail
);
1372 if (if_old
->main_dev
.dev
)
1373 interface_check_state(if_old
);
1376 if_new
->config
= NULL
;
1377 interface_cleanup(if_new
);
1383 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1384 struct vlist_node
*node_old
)
1386 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1387 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1389 if (node_old
&& node_new
) {
1390 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1391 interface_change_config(if_old
, if_new
);
1392 } else if (node_old
) {
1393 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1394 set_config_state(if_old
, IFC_REMOVE
);
1395 } else if (node_new
) {
1396 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1397 interface_event(if_new
, IFEV_CREATE
);
1398 proto_init_interface(if_new
, if_new
->config
);
1399 interface_claim_device(if_new
);
1400 netifd_ubus_add_interface(if_new
);
1406 interface_init_list(void)
1408 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1409 interfaces
.keep_old
= true;
1410 interfaces
.no_delete
= true;