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"
30 struct vlist_tree interfaces
;
31 static LIST_HEAD(iface_all_users
);
35 IFACE_ATTR_IFNAME
, /* Backward compatibility */
40 IFACE_ATTR_JAIL_DEVICE
,
41 IFACE_ATTR_JAIL_IFNAME
,
42 IFACE_ATTR_HOST_DEVICE
,
43 IFACE_ATTR_DEFAULTROUTE
,
46 IFACE_ATTR_DNS_SEARCH
,
47 IFACE_ATTR_DNS_METRIC
,
56 IFACE_ATTR_IP6IFACEID
,
57 IFACE_ATTR_FORCE_LINK
,
62 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
63 [IFACE_ATTR_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
64 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
65 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
66 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
67 [IFACE_ATTR_ZONE
] = { .name
= "zone", .type
= BLOBMSG_TYPE_STRING
},
68 [IFACE_ATTR_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
69 [IFACE_ATTR_JAIL_DEVICE
] = { .name
= "jail_device", .type
= BLOBMSG_TYPE_STRING
},
70 [IFACE_ATTR_JAIL_IFNAME
] = { .name
= "jail_ifname", .type
= BLOBMSG_TYPE_STRING
},
71 [IFACE_ATTR_HOST_DEVICE
] = { .name
= "host_device", .type
= BLOBMSG_TYPE_STRING
},
72 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
73 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
74 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
75 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
76 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
77 [IFACE_ATTR_DNS_METRIC
] = { .name
= "dns_metric", .type
= BLOBMSG_TYPE_INT32
},
78 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
79 [IFACE_ATTR_IP6ASSIGN
] = { .name
= "ip6assign", .type
= BLOBMSG_TYPE_INT32
},
80 [IFACE_ATTR_IP6HINT
] = { .name
= "ip6hint", .type
= BLOBMSG_TYPE_STRING
},
81 [IFACE_ATTR_IP4TABLE
] = { .name
= "ip4table", .type
= BLOBMSG_TYPE_STRING
},
82 [IFACE_ATTR_IP6TABLE
] = { .name
= "ip6table", .type
= BLOBMSG_TYPE_STRING
},
83 [IFACE_ATTR_IP6CLASS
] = { .name
= "ip6class", .type
= BLOBMSG_TYPE_ARRAY
},
84 [IFACE_ATTR_DELEGATE
] = { .name
= "delegate", .type
= BLOBMSG_TYPE_BOOL
},
85 [IFACE_ATTR_IP6IFACEID
] = { .name
= "ip6ifaceid", .type
= BLOBMSG_TYPE_STRING
},
86 [IFACE_ATTR_FORCE_LINK
] = { .name
= "force_link", .type
= BLOBMSG_TYPE_BOOL
},
87 [IFACE_ATTR_IP6WEIGHT
] = { .name
= "ip6weight", .type
= BLOBMSG_TYPE_INT32
},
90 const struct uci_blob_param_list interface_attr_list
= {
91 .n_params
= IFACE_ATTR_MAX
,
92 .params
= iface_attrs
,
96 interface_set_main_dev(struct interface
*iface
, struct device
*dev
);
98 interface_event(struct interface
*iface
, enum interface_event ev
);
101 interface_error_flush(struct interface
*iface
)
103 struct interface_error
*error
, *tmp
;
105 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
106 list_del(&error
->list
);
112 interface_force_link(struct interface
*iface
)
114 struct device
*dev
= iface
->main_dev
.dev
;
116 if (dev
&& dev
->settings
.auth
)
119 return iface
->force_link
;
123 interface_clear_errors(struct interface
*iface
)
125 /* don't flush the errors in case the configured protocol handler matches the
126 running protocol handler and is having the last error capability */
127 if (!(iface
->proto
&&
128 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
129 (iface
->proto
->handler
->name
== iface
->proto_handler
->name
)))
130 interface_error_flush(iface
);
133 void interface_add_error(struct interface
*iface
, const char *subsystem
,
134 const char *code
, const char **data
, int n_data
)
136 struct interface_error
*error
;
139 char *dest
, *d_subsys
, *d_code
;
141 /* if the configured protocol handler has the last error support capability,
142 errors should only be added if the running protocol handler matches the
145 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
146 (iface
->proto
->handler
->name
!= iface
->proto_handler
->name
))
150 len
= n_data
* sizeof(char *);
151 datalen
= alloca(len
);
152 for (i
= 0; i
< n_data
; i
++) {
153 datalen
[i
] = strlen(data
[i
]) + 1;
158 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
159 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
160 &d_code
, code
? strlen(code
) + 1 : 0);
164 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
165 protocol can't be established (e.g auth failure) */
166 if (iface
->proto_handler
->flags
& PROTO_FLAG_LASTERROR
)
167 interface_error_flush(iface
);
169 list_add_tail(&error
->list
, &iface
->errors
);
171 dest
= (char *) &error
->data
[n_data
+ 1];
172 for (i
= 0; i
< n_data
; i
++) {
173 error
->data
[i
] = dest
;
174 memcpy(dest
, data
[i
], datalen
[i
]);
177 error
->data
[n_data
] = NULL
;
180 error
->subsystem
= strcpy(d_subsys
, subsystem
);
183 error
->code
= strcpy(d_code
, code
);
187 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
189 avl_delete(&iface
->data
, &data
->node
);
194 interface_data_flush(struct interface
*iface
)
196 struct interface_data
*d
, *tmp
;
198 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
199 interface_data_del(iface
, d
);
203 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
205 struct interface_data
*n
, *o
;
207 if (!blobmsg_check_attr(data
, true))
208 return UBUS_STATUS_INVALID_ARGUMENT
;
210 const char *name
= blobmsg_name(data
);
211 unsigned len
= blob_pad_len(data
);
213 o
= avl_find_element(&iface
->data
, name
, o
, node
);
215 if (blob_pad_len(o
->data
) == len
&& !memcmp(o
->data
, data
, len
))
218 interface_data_del(iface
, o
);
221 n
= calloc(1, sizeof(*n
) + len
);
223 return UBUS_STATUS_UNKNOWN_ERROR
;
225 memcpy(n
->data
, data
, len
);
226 n
->node
.key
= blobmsg_name(n
->data
);
227 avl_insert(&iface
->data
, &n
->node
);
229 iface
->updated
|= IUF_DATA
;
233 int interface_parse_data(struct interface
*iface
, const struct blob_attr
*attr
)
235 struct blob_attr
*cur
;
241 blob_for_each_attr(cur
, attr
, rem
) {
242 ret
= interface_add_data(iface
, cur
);
247 if (iface
->updated
&& iface
->state
== IFS_UP
)
248 interface_event(iface
, IFEV_UPDATE
);
254 interface_event(struct interface
*iface
, enum interface_event ev
)
256 struct interface_user
*dep
, *tmp
;
257 struct device
*adev
= NULL
;
259 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
260 dep
->cb(dep
, iface
, ev
);
262 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
263 dep
->cb(dep
, iface
, ev
);
267 interface_error_flush(iface
);
268 adev
= iface
->l3_dev
.dev
;
272 alias_notify_device(iface
->name
, adev
);
280 interface_flush_state(struct interface
*iface
)
282 if (iface
->l3_dev
.dev
)
283 device_release(&iface
->l3_dev
);
284 interface_data_flush(iface
);
288 mark_interface_down(struct interface
*iface
)
290 enum interface_state state
= iface
->state
;
292 if (state
== IFS_DOWN
)
295 iface
->link_up_event
= false;
296 iface
->state
= IFS_DOWN
;
300 interface_event(iface
, IFEV_DOWN
);
303 interface_event(iface
, IFEV_UP_FAILED
);
308 interface_ip_set_enabled(&iface
->config_ip
, false);
309 interface_ip_set_enabled(&iface
->proto_ip
, false);
310 interface_ip_flush(&iface
->proto_ip
);
311 interface_flush_state(iface
);
312 system_flush_routes();
316 __set_config_state(struct interface
*iface
, enum interface_config_state s
)
318 iface
->config_state
= s
;
322 __interface_set_down(struct interface
*iface
, bool force
)
324 enum interface_state state
= iface
->state
;
328 iface
->state
= IFS_TEARDOWN
;
330 __set_config_state(iface
, IFC_REMOVE
);
333 interface_event(iface
, IFEV_DOWN
);
335 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
337 interface_flush_state(iface
);
341 if (iface
->main_dev
.dev
)
342 device_release(&iface
->main_dev
);
351 __interface_set_up(struct interface
*iface
)
355 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
357 iface
->state
= IFS_SETUP
;
358 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
360 mark_interface_down(iface
);
366 interface_check_state(struct interface
*iface
)
368 bool link_state
= iface
->link_state
|| interface_force_link(iface
);
370 switch (iface
->state
) {
373 if (!iface
->enabled
|| !link_state
) {
374 iface
->state
= IFS_TEARDOWN
;
376 __set_config_state(iface
, IFC_REMOVE
);
378 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
382 if (!iface
->available
)
385 if (iface
->autostart
&& iface
->enabled
&& link_state
&& !config_init
)
386 __interface_set_up(iface
);
394 interface_set_enabled(struct interface
*iface
, bool new_state
)
396 if (iface
->enabled
== new_state
)
399 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
400 iface
->enabled
= new_state
;
401 interface_check_state(iface
);
405 interface_set_link_state(struct interface
*iface
, bool new_state
)
407 if (iface
->link_state
== new_state
)
410 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
411 iface
->link_state
= new_state
;
412 interface_check_state(iface
);
414 if (new_state
&& interface_force_link(iface
) &&
415 iface
->state
== IFS_UP
&& !iface
->link_up_event
) {
416 interface_event(iface
, IFEV_LINK_UP
);
417 iface
->link_up_event
= true;
422 interface_ext_dev_cb(struct device_user
*dep
, enum device_event ev
)
424 if (ev
== DEV_EVENT_REMOVE
)
425 device_remove_user(dep
);
429 interface_main_dev_cb(struct device_user
*dep
, enum device_event ev
)
431 struct interface
*iface
;
433 iface
= container_of(dep
, struct interface
, main_dev
);
436 interface_set_available(iface
, true);
438 case DEV_EVENT_REMOVE
:
439 interface_set_available(iface
, false);
440 if (dep
->dev
&& dep
->dev
->external
)
441 interface_set_main_dev(iface
, NULL
);
444 interface_set_enabled(iface
, true);
447 interface_set_enabled(iface
, false);
449 case DEV_EVENT_AUTH_UP
:
450 case DEV_EVENT_LINK_UP
:
451 case DEV_EVENT_LINK_DOWN
:
452 interface_set_link_state(iface
, device_link_active(dep
->dev
));
454 case DEV_EVENT_TOPO_CHANGE
:
455 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
463 interface_l3_dev_cb(struct device_user
*dep
, enum device_event ev
)
465 struct interface
*iface
;
467 iface
= container_of(dep
, struct interface
, l3_dev
);
468 if (iface
->l3_dev
.dev
== iface
->main_dev
.dev
)
472 case DEV_EVENT_LINK_DOWN
:
473 if (iface
->proto_handler
->flags
& PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN
)
474 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
482 interface_set_available(struct interface
*iface
, bool new_state
)
484 if (iface
->available
== new_state
)
487 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
488 iface
->available
= new_state
;
491 if (iface
->autostart
&& !config_init
)
492 interface_set_up(iface
);
494 __interface_set_down(iface
, true);
498 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
501 list_add(&dep
->list
, &iface_all_users
);
506 list_add(&dep
->list
, &iface
->users
);
507 if (iface
->state
== IFS_UP
)
508 dep
->cb(dep
, iface
, IFEV_UP
);
512 interface_remove_user(struct interface_user
*dep
)
514 list_del_init(&dep
->list
);
519 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
521 struct blob_attr
*cur
;
524 blobmsg_for_each_attr(cur
, list
, rem
) {
525 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
528 if (!blobmsg_check_attr(cur
, false))
531 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
532 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
533 list_add(&c
->head
, &iface
->assignment_classes
);
538 interface_clear_assignment_classes(struct interface
*iface
)
540 while (!list_empty(&iface
->assignment_classes
)) {
541 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
542 struct interface_assignment_class
, head
);
549 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
551 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
552 old
->assignment_length
!= new->assignment_length
||
553 old
->assignment_iface_id_selection
!= new->assignment_iface_id_selection
||
554 old
->assignment_weight
!= new->assignment_weight
||
555 (old
->assignment_iface_id_selection
== IFID_FIXED
&&
556 memcmp(&old
->assignment_fixed_iface_id
, &new->assignment_fixed_iface_id
,
557 sizeof(old
->assignment_fixed_iface_id
))) ||
558 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
560 struct interface_assignment_class
*c
;
561 list_for_each_entry(c
, &new->assignment_classes
, head
) {
562 /* Compare list entries one-by-one to see if there was a change */
563 if (list_empty(&old
->assignment_classes
)) /* The new list is longer */
569 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
570 struct interface_assignment_class
, head
);
572 if (strcmp(c_old
->name
, c
->name
)) /* An entry didn't match */
575 list_del(&c_old
->head
);
579 /* The old list was longer than the new one or the last entry didn't match */
580 if (!list_empty(&old
->assignment_classes
)) {
581 interface_clear_assignment_classes(old
);
585 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
588 old
->assignment_hint
= new->assignment_hint
;
589 old
->assignment_length
= new->assignment_length
;
590 old
->assignment_iface_id_selection
= new->assignment_iface_id_selection
;
591 old
->assignment_fixed_iface_id
= new->assignment_fixed_iface_id
;
592 old
->assignment_weight
= new->assignment_weight
;
593 interface_refresh_assignments(true);
598 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
600 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
601 struct device
*dev
= iface
->l3_dev
.dev
;
608 interface_set_main_dev(alias
, dev
);
609 interface_set_available(alias
, true);
613 interface_set_available(alias
, false);
614 interface_set_main_dev(alias
, NULL
);
617 interface_remove_user(dep
);
625 interface_set_device_config(struct interface
*iface
, struct device
*dev
)
627 if (!dev
|| !dev
->default_config
)
630 if (!iface
->device_config
&&
631 (!dev
->iface_config
|| dev
->config_iface
!= iface
))
634 dev
->config_iface
= iface
;
635 dev
->iface_config
= iface
->device_config
;
636 device_apply_config(dev
, dev
->type
, iface
->config
);
640 interface_claim_device(struct interface
*iface
)
642 struct interface
*parent
;
643 struct device
*dev
= NULL
;
645 if (iface
->parent_iface
.iface
)
646 interface_remove_user(&iface
->parent_iface
);
648 if (iface
->parent_ifname
) {
649 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
650 iface
->parent_iface
.cb
= interface_alias_cb
;
651 interface_add_user(&iface
->parent_iface
, parent
);
652 } else if (iface
->device
&&
653 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
654 dev
= device_get(iface
->device
, true);
655 interface_set_device_config(iface
, dev
);
657 dev
= iface
->ext_dev
.dev
;
661 interface_set_main_dev(iface
, dev
);
663 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
664 interface_set_available(iface
, true);
668 interface_cleanup_state(struct interface
*iface
)
670 interface_set_available(iface
, false);
672 interface_flush_state(iface
);
673 interface_clear_errors(iface
);
674 interface_set_proto_state(iface
, NULL
);
676 interface_set_main_dev(iface
, NULL
);
677 interface_set_l3_dev(iface
, NULL
);
681 interface_cleanup(struct interface
*iface
)
683 struct interface_user
*dep
, *tmp
;
685 uloop_timeout_cancel(&iface
->remove_timer
);
686 device_remove_user(&iface
->ext_dev
);
688 if (iface
->parent_iface
.iface
)
689 interface_remove_user(&iface
->parent_iface
);
691 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
692 interface_remove_user(dep
);
694 interface_clear_assignment_classes(iface
);
695 interface_ip_flush(&iface
->config_ip
);
696 interface_cleanup_state(iface
);
700 interface_do_free(struct interface
*iface
)
702 interface_event(iface
, IFEV_FREE
);
703 interface_cleanup(iface
);
705 netifd_ubus_remove_interface(iface
);
706 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
709 if (iface
->jail_device
)
710 free(iface
->jail_device
);
711 if (iface
->host_device
)
712 free(iface
->host_device
);
718 interface_do_reload(struct interface
*iface
)
720 interface_event(iface
, IFEV_RELOAD
);
721 interface_cleanup_state(iface
);
722 proto_init_interface(iface
, iface
->config
);
723 interface_claim_device(iface
);
727 interface_handle_config_change(struct interface
*iface
)
729 enum interface_config_state state
= iface
->config_state
;
731 iface
->config_state
= IFC_NORMAL
;
736 interface_do_reload(iface
);
739 interface_do_free(iface
);
742 if (iface
->autostart
)
743 interface_set_up(iface
);
747 interface_proto_event_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
749 struct interface
*iface
= state
->iface
;
753 if (iface
->state
!= IFS_SETUP
) {
754 if (iface
->state
== IFS_UP
&& iface
->updated
)
755 interface_event(iface
, IFEV_UPDATE
);
759 if (!iface
->l3_dev
.dev
)
760 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
762 interface_ip_set_enabled(&iface
->config_ip
, true);
763 interface_ip_set_enabled(&iface
->proto_ip
, true);
764 system_flush_routes();
765 iface
->state
= IFS_UP
;
766 iface
->start_time
= system_get_rtime();
767 interface_event(iface
, IFEV_UP
);
768 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
771 if (iface
->state
== IFS_DOWN
)
774 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
775 mark_interface_down(iface
);
776 interface_write_resolv_conf(iface
->jail
);
777 if (iface
->main_dev
.dev
&& !(iface
->config_state
== IFC_NORMAL
&& iface
->autostart
&& iface
->available
))
778 device_release(&iface
->main_dev
);
779 if (iface
->l3_dev
.dev
)
780 device_remove_user(&iface
->l3_dev
);
781 interface_handle_config_change(iface
);
783 case IFPEV_LINK_LOST
:
784 if (iface
->state
!= IFS_UP
)
787 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
788 mark_interface_down(iface
);
789 iface
->state
= IFS_SETUP
;
795 interface_write_resolv_conf(iface
->jail
);
798 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
801 iface
->proto
->free(iface
->proto
);
804 iface
->state
= IFS_DOWN
;
805 iface
->proto
= state
;
809 state
->proto_event
= interface_proto_event_cb
;
810 state
->iface
= iface
;
814 interface_alloc(const char *name
, struct blob_attr
*config
, bool dynamic
)
816 struct interface
*iface
;
817 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
818 struct blob_attr
*cur
;
819 const char *proto_name
= NULL
;
821 bool force_link
= false;
823 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
824 iface
->name
= strcpy(iface_name
, name
);
825 INIT_LIST_HEAD(&iface
->errors
);
826 INIT_LIST_HEAD(&iface
->users
);
827 INIT_LIST_HEAD(&iface
->hotplug_list
);
828 INIT_LIST_HEAD(&iface
->assignment_classes
);
829 interface_ip_init(iface
);
830 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
831 iface
->config_ip
.enabled
= false;
833 iface
->main_dev
.cb
= interface_main_dev_cb
;
834 iface
->l3_dev
.cb
= interface_l3_dev_cb
;
835 iface
->ext_dev
.cb
= interface_ext_dev_cb
;
837 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
838 blob_data(config
), blob_len(config
));
841 if ((cur
= tb
[IFACE_ATTR_ZONE
]))
842 iface
->zone
= strdup(blobmsg_get_string(cur
));
844 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
845 proto_name
= blobmsg_data(cur
);
847 proto_attach_interface(iface
, proto_name
);
848 if (iface
->proto_handler
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
)
851 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
852 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], force_link
);
853 iface
->dynamic
= dynamic
;
854 iface
->proto_ip
.no_defaultroute
=
855 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
856 iface
->proto_ip
.no_dns
=
857 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
859 if ((cur
= tb
[IFACE_ATTR_DNS
]))
860 interface_add_dns_server_list(&iface
->config_ip
, cur
);
862 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
863 interface_add_dns_search_list(&iface
->config_ip
, cur
);
865 if ((cur
= tb
[IFACE_ATTR_DNS_METRIC
]))
866 iface
->dns_metric
= blobmsg_get_u32(cur
);
868 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
869 iface
->metric
= blobmsg_get_u32(cur
);
871 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
872 iface
->assignment_length
= blobmsg_get_u32(cur
);
875 iface
->assignment_iface_id_selection
= IFID_FIXED
;
876 iface
->assignment_fixed_iface_id
= in6addr_any
;
877 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
879 if ((cur
= tb
[IFACE_ATTR_IP6IFACEID
])) {
880 const char *ifaceid
= blobmsg_data(cur
);
881 if (!strcmp(ifaceid
, "random")) {
882 iface
->assignment_iface_id_selection
= IFID_RANDOM
;
884 else if (!strcmp(ifaceid
, "eui64")) {
885 iface
->assignment_iface_id_selection
= IFID_EUI64
;
888 /* we expect an IPv6 address with network id zero here -> fixed iface id
889 if we cannot parse -> revert to iface id 1 */
890 if (inet_pton(AF_INET6
,ifaceid
,&iface
->assignment_fixed_iface_id
) != 1 ||
891 iface
->assignment_fixed_iface_id
.s6_addr32
[0] != 0 ||
892 iface
->assignment_fixed_iface_id
.s6_addr32
[1] != 0) {
893 iface
->assignment_fixed_iface_id
= in6addr_any
;
894 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
895 netifd_log_message(L_WARNING
, "Failed to parse ip6ifaceid for interface '%s', \
896 falling back to iface id 1.\n", iface
->name
);
901 iface
->assignment_hint
= -1;
902 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
903 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
904 ~((1 << (64 - iface
->assignment_length
)) - 1);
906 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
907 interface_add_assignment_classes(iface
, cur
);
909 if ((cur
= tb
[IFACE_ATTR_IP6WEIGHT
]))
910 iface
->assignment_weight
= blobmsg_get_u32(cur
);
912 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
913 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
914 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
917 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
918 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
919 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
922 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
924 iface
->config_autostart
= iface
->autostart
;
927 if ((cur
= tb
[IFACE_ATTR_JAIL
])) {
928 iface
->jail
= strdup(blobmsg_get_string(cur
));
929 iface
->autostart
= false;
932 iface
->jail_device
= NULL
;
933 if ((cur
= tb
[IFACE_ATTR_JAIL_DEVICE
]))
934 iface
->jail_device
= strdup(blobmsg_get_string(cur
));
935 else if ((cur
= tb
[IFACE_ATTR_JAIL_IFNAME
]))
936 iface
->jail_device
= strdup(blobmsg_get_string(cur
));
938 iface
->host_device
= NULL
;
939 if ((cur
= tb
[IFACE_ATTR_HOST_DEVICE
]))
940 iface
->host_device
= strdup(blobmsg_get_string(cur
));
945 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
947 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
948 struct blob_attr
*cur
;
951 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
952 blob_data(config
), blob_len(config
));
955 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
956 iface
->parent_ifname
= blobmsg_data(cur
);
958 if (!iface
->parent_ifname
)
961 cur
= tb
[IFACE_ATTR_DEVICE
];
963 cur
= tb
[IFACE_ATTR_IFNAME
];
965 iface
->device
= blobmsg_data(cur
);
968 if (iface
->dynamic
) {
969 name
= strdup(iface
->name
);
975 iface
->config
= config
;
976 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
979 iface
= vlist_find(&interfaces
, name
, iface
, node
);
982 /* Don't delete dynamic interface on reload */
984 iface
->node
.version
= -1;
991 interface_add(struct interface
*iface
, struct blob_attr
*config
)
993 return __interface_add(iface
, config
, false);
997 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
999 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
1002 return __interface_add(iface
, config
, true);
1006 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
1008 bool enabled
= iface
->config_ip
.enabled
;
1009 bool claimed
= iface
->l3_dev
.claimed
;
1011 if (iface
->l3_dev
.dev
== dev
)
1014 interface_ip_set_enabled(&iface
->config_ip
, false);
1015 interface_ip_set_enabled(&iface
->proto_ip
, false);
1016 interface_ip_flush(&iface
->proto_ip
);
1017 device_add_user(&iface
->l3_dev
, dev
);
1021 if (device_claim(&iface
->l3_dev
) < 0)
1024 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
1025 interface_ip_set_enabled(&iface
->proto_ip
, enabled
);
1030 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
1032 bool claimed
= iface
->l3_dev
.claimed
;
1034 if (iface
->main_dev
.dev
== dev
)
1037 interface_set_available(iface
, false);
1038 device_add_user(&iface
->main_dev
, dev
);
1040 interface_set_link_state(iface
, false);
1045 if (device_claim(&iface
->l3_dev
) < 0)
1049 if (!iface
->l3_dev
.dev
)
1050 interface_set_l3_dev(iface
, dev
);
1054 interface_remove_link(struct interface
*iface
, struct device
*dev
,
1055 struct blob_attr
*vlan
)
1057 struct device
*mdev
= iface
->main_dev
.dev
;
1059 if (mdev
&& mdev
->hotplug_ops
)
1060 return mdev
->hotplug_ops
->del(mdev
, dev
, vlan
);
1062 if (dev
== iface
->ext_dev
.dev
)
1063 device_remove_user(&iface
->ext_dev
);
1065 if (!iface
->main_dev
.hotplug
)
1066 return UBUS_STATUS_INVALID_ARGUMENT
;
1068 if (dev
!= iface
->main_dev
.dev
)
1069 return UBUS_STATUS_INVALID_ARGUMENT
;
1071 interface_set_main_dev(iface
, NULL
);
1076 interface_add_link(struct interface
*iface
, struct device
*dev
,
1077 struct blob_attr
*vlan
, bool link_ext
)
1079 struct device
*mdev
= iface
->main_dev
.dev
;
1084 if (iface
->main_dev
.hotplug
)
1085 device_remove_user(&iface
->main_dev
);
1088 if (mdev
->hotplug_ops
)
1089 return mdev
->hotplug_ops
->add(mdev
, dev
, vlan
);
1091 return UBUS_STATUS_NOT_SUPPORTED
;
1095 device_add_user(&iface
->ext_dev
, dev
);
1097 interface_set_main_dev(iface
, dev
);
1098 iface
->main_dev
.hotplug
= true;
1103 interface_handle_link(struct interface
*iface
, const char *name
,
1104 struct blob_attr
*vlan
, bool add
, bool link_ext
)
1108 dev
= device_get(name
, add
? (link_ext
? 2 : 1) : 0);
1110 return UBUS_STATUS_NOT_FOUND
;
1113 return interface_remove_link(iface
, dev
, vlan
);
1115 interface_set_device_config(iface
, dev
);
1117 device_set_present(dev
, true);
1119 return interface_add_link(iface
, dev
, vlan
, link_ext
);
1123 interface_set_up(struct interface
*iface
)
1126 const char *error
= NULL
;
1128 iface
->autostart
= true;
1129 wireless_check_network_enabled();
1131 if (iface
->state
!= IFS_DOWN
)
1134 interface_clear_errors(iface
);
1135 if (iface
->available
) {
1136 if (iface
->main_dev
.dev
) {
1137 ret
= device_claim(&iface
->main_dev
);
1139 interface_check_state(iface
);
1141 error
= "DEVICE_CLAIM_FAILED";
1143 ret
= __interface_set_up(iface
);
1145 error
= "SETUP_FAILED";
1148 error
= "NO_DEVICE";
1151 interface_add_error(iface
, "interface", error
, NULL
, 0);
1155 interface_set_down(struct interface
*iface
)
1158 vlist_for_each_element(&interfaces
, iface
, node
)
1159 __interface_set_down(iface
, false);
1161 iface
->autostart
= false;
1162 wireless_check_network_enabled();
1163 __interface_set_down(iface
, false);
1168 interface_renew(struct interface
*iface
)
1170 if (iface
->state
== IFS_TEARDOWN
|| iface
->state
== IFS_DOWN
)
1173 return interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
1177 interface_start_pending(void)
1179 struct interface
*iface
;
1181 vlist_for_each_element(&interfaces
, iface
, node
) {
1182 if (iface
->autostart
)
1183 interface_set_up(iface
);
1188 interface_start_jail(int netns_fd
, const char *jail
)
1190 struct interface
*iface
;
1192 vlist_for_each_element(&interfaces
, iface
, node
) {
1193 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1196 system_link_netns_move(iface
->main_dev
.dev
, netns_fd
, iface
->jail_device
);
1201 interface_stop_jail(int netns_fd
)
1203 struct interface
*iface
;
1206 vlist_for_each_element(&interfaces
, iface
, node
) {
1207 orig_ifname
= iface
->host_device
;
1208 interface_set_down(iface
);
1209 system_link_netns_move(iface
->main_dev
.dev
, netns_fd
, orig_ifname
);
1214 set_config_state(struct interface
*iface
, enum interface_config_state s
)
1216 __set_config_state(iface
, s
);
1217 if (iface
->state
== IFS_DOWN
)
1218 interface_handle_config_change(iface
);
1220 __interface_set_down(iface
, false);
1224 interface_update_start(struct interface
*iface
, const bool keep_old
)
1229 interface_ip_update_start(&iface
->proto_ip
);
1233 interface_update_complete(struct interface
*iface
)
1235 interface_ip_update_complete(&iface
->proto_ip
);
1239 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
1241 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
1242 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
1246 interface_device_config_changed(struct interface
*if_old
, struct interface
*if_new
)
1248 struct blob_attr
*ntb
[__DEV_ATTR_MAX
];
1249 struct blob_attr
*otb
[__DEV_ATTR_MAX
];
1250 struct device
*dev
= if_old
->main_dev
.dev
;
1251 unsigned long diff
= 0;
1253 BUILD_BUG_ON(sizeof(diff
) < __DEV_ATTR_MAX
/ 8);
1258 if (if_old
->device_config
!= if_new
->device_config
)
1261 if (!if_new
->device_config
)
1264 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb
,
1265 blob_data(if_old
->config
), blob_len(if_old
->config
));
1267 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, ntb
,
1268 blob_data(if_new
->config
), blob_len(if_new
->config
));
1270 uci_blob_diff(ntb
, otb
, &device_attr_list
, &diff
);
1275 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
1277 struct blob_attr
*old_config
= if_old
->config
;
1278 bool reload
= false, reload_ip
= false, update_prefix_delegation
= false;
1280 #define FIELD_CHANGED_STR(field) \
1281 ((!!if_old->field != !!if_new->field) || \
1283 strcmp(if_old->field, if_new->field) != 0))
1285 if (FIELD_CHANGED_STR(parent_ifname
)) {
1286 if (if_old
->parent_iface
.iface
)
1287 interface_remove_user(&if_old
->parent_iface
);
1291 if (!reload
&& interface_device_config_changed(if_old
, if_new
))
1294 if (FIELD_CHANGED_STR(device
) ||
1295 if_old
->proto_handler
!= if_new
->proto_handler
)
1298 if (!if_old
->proto_handler
->config_params
)
1299 D(INTERFACE
, "No config parameters for interface '%s'\n",
1301 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
1302 if_old
->proto_handler
->config_params
))
1305 #define UPDATE(field, __var) ({ \
1306 bool __changed = (if_old->field != if_new->field); \
1307 if_old->field = if_new->field; \
1308 __var |= __changed; \
1311 if_old
->config
= if_new
->config
;
1312 if (if_old
->config_autostart
!= if_new
->config_autostart
) {
1313 if (if_old
->config_autostart
)
1316 if_old
->autostart
= if_new
->config_autostart
;
1319 if_old
->device_config
= if_new
->device_config
;
1320 if_old
->config_autostart
= if_new
->config_autostart
;
1324 if_old
->jail
= if_new
->jail
;
1326 if_old
->autostart
= false;
1328 if (if_old
->jail_device
)
1329 free(if_old
->jail_device
);
1331 if_old
->jail_device
= if_new
->jail_device
;
1333 if (if_old
->host_device
)
1334 free(if_old
->host_device
);
1336 if_old
->host_device
= if_new
->host_device
;
1338 if_old
->device
= if_new
->device
;
1339 if_old
->parent_ifname
= if_new
->parent_ifname
;
1340 if_old
->dynamic
= if_new
->dynamic
;
1341 if_old
->proto_handler
= if_new
->proto_handler
;
1342 if_old
->force_link
= if_new
->force_link
;
1343 if_old
->dns_metric
= if_new
->dns_metric
;
1345 if (if_old
->proto_ip
.no_delegation
!= if_new
->proto_ip
.no_delegation
) {
1346 if_old
->proto_ip
.no_delegation
= if_new
->proto_ip
.no_delegation
;
1347 update_prefix_delegation
= true;
1350 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1351 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1353 UPDATE(metric
, reload_ip
);
1354 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1355 UPDATE(ip4table
, reload_ip
);
1356 UPDATE(ip6table
, reload_ip
);
1357 interface_merge_assignment_data(if_old
, if_new
);
1362 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1364 interface_clear_errors(if_old
);
1365 set_config_state(if_old
, IFC_RELOAD
);
1370 bool config_ip_enabled
= if_old
->config_ip
.enabled
;
1371 bool proto_ip_enabled
= if_old
->proto_ip
.enabled
;
1373 interface_ip_set_enabled(&if_old
->config_ip
, false);
1374 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1375 interface_ip_set_enabled(&if_old
->proto_ip
, proto_ip_enabled
);
1376 interface_ip_set_enabled(&if_old
->config_ip
, config_ip_enabled
);
1379 if (update_prefix_delegation
)
1380 interface_update_prefix_delegation(&if_old
->proto_ip
);
1382 interface_write_resolv_conf(if_old
->jail
);
1383 if (if_old
->main_dev
.dev
)
1384 interface_check_state(if_old
);
1387 if_new
->config
= NULL
;
1388 interface_cleanup(if_new
);
1394 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1395 struct vlist_node
*node_old
)
1397 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1398 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1400 if (node_old
&& node_new
) {
1401 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1402 interface_change_config(if_old
, if_new
);
1403 } else if (node_old
) {
1404 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1405 set_config_state(if_old
, IFC_REMOVE
);
1406 } else if (node_new
) {
1407 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1408 interface_event(if_new
, IFEV_CREATE
);
1409 proto_init_interface(if_new
, if_new
->config
);
1410 interface_claim_device(if_new
);
1411 netifd_ubus_add_interface(if_new
);
1417 interface_init_list(void)
1419 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1420 interfaces
.keep_old
= true;
1421 interfaces
.no_delete
= true;