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 */
39 IFACE_ATTR_JAIL_DEVICE
,
40 IFACE_ATTR_JAIL_IFNAME
,
41 IFACE_ATTR_HOST_DEVICE
,
42 IFACE_ATTR_DEFAULTROUTE
,
45 IFACE_ATTR_DNS_SEARCH
,
46 IFACE_ATTR_DNS_METRIC
,
55 IFACE_ATTR_IP6IFACEID
,
56 IFACE_ATTR_FORCE_LINK
,
61 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
62 [IFACE_ATTR_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
63 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
64 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
65 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
66 [IFACE_ATTR_ZONE
] = { .name
= "zone", .type
= BLOBMSG_TYPE_STRING
},
67 [IFACE_ATTR_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
68 [IFACE_ATTR_JAIL_DEVICE
] = { .name
= "jail_device", .type
= BLOBMSG_TYPE_STRING
},
69 [IFACE_ATTR_JAIL_IFNAME
] = { .name
= "jail_ifname", .type
= BLOBMSG_TYPE_STRING
},
70 [IFACE_ATTR_HOST_DEVICE
] = { .name
= "host_device", .type
= BLOBMSG_TYPE_STRING
},
71 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
72 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
73 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
74 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
75 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
76 [IFACE_ATTR_DNS_METRIC
] = { .name
= "dns_metric", .type
= BLOBMSG_TYPE_INT32
},
77 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
78 [IFACE_ATTR_IP6ASSIGN
] = { .name
= "ip6assign", .type
= BLOBMSG_TYPE_INT32
},
79 [IFACE_ATTR_IP6HINT
] = { .name
= "ip6hint", .type
= BLOBMSG_TYPE_STRING
},
80 [IFACE_ATTR_IP4TABLE
] = { .name
= "ip4table", .type
= BLOBMSG_TYPE_STRING
},
81 [IFACE_ATTR_IP6TABLE
] = { .name
= "ip6table", .type
= BLOBMSG_TYPE_STRING
},
82 [IFACE_ATTR_IP6CLASS
] = { .name
= "ip6class", .type
= BLOBMSG_TYPE_ARRAY
},
83 [IFACE_ATTR_DELEGATE
] = { .name
= "delegate", .type
= BLOBMSG_TYPE_BOOL
},
84 [IFACE_ATTR_IP6IFACEID
] = { .name
= "ip6ifaceid", .type
= BLOBMSG_TYPE_STRING
},
85 [IFACE_ATTR_FORCE_LINK
] = { .name
= "force_link", .type
= BLOBMSG_TYPE_BOOL
},
86 [IFACE_ATTR_IP6WEIGHT
] = { .name
= "ip6weight", .type
= BLOBMSG_TYPE_INT32
},
89 const struct uci_blob_param_list interface_attr_list
= {
90 .n_params
= IFACE_ATTR_MAX
,
91 .params
= iface_attrs
,
95 interface_set_main_dev(struct interface
*iface
, struct device
*dev
);
97 interface_event(struct interface
*iface
, enum interface_event ev
);
100 interface_error_flush(struct interface
*iface
)
102 struct interface_error
*error
, *tmp
;
104 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
105 list_del(&error
->list
);
111 interface_force_link(struct interface
*iface
)
113 struct device
*dev
= iface
->main_dev
.dev
;
115 if (dev
&& dev
->settings
.auth
)
118 return iface
->force_link
;
122 interface_clear_errors(struct interface
*iface
)
124 /* don't flush the errors in case the configured protocol handler matches the
125 running protocol handler and is having the last error capability */
126 if (!(iface
->proto
&&
127 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
128 (iface
->proto
->handler
->name
== iface
->proto_handler
->name
)))
129 interface_error_flush(iface
);
132 void interface_add_error(struct interface
*iface
, const char *subsystem
,
133 const char *code
, const char **data
, int n_data
)
135 struct interface_error
*error
;
138 char *dest
, *d_subsys
, *d_code
;
140 /* if the configured protocol handler has the last error support capability,
141 errors should only be added if the running protocol handler matches the
144 (iface
->proto
->handler
->flags
& PROTO_FLAG_LASTERROR
) &&
145 (iface
->proto
->handler
->name
!= iface
->proto_handler
->name
))
149 len
= n_data
* sizeof(char *);
150 datalen
= alloca(len
);
151 for (i
= 0; i
< n_data
; i
++) {
152 datalen
[i
] = strlen(data
[i
]) + 1;
157 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
158 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
159 &d_code
, code
? strlen(code
) + 1 : 0);
163 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
164 protocol can't be established (e.g auth failure) */
165 if (iface
->proto_handler
->flags
& PROTO_FLAG_LASTERROR
)
166 interface_error_flush(iface
);
168 list_add_tail(&error
->list
, &iface
->errors
);
170 dest
= (char *) &error
->data
[n_data
+ 1];
171 for (i
= 0; i
< n_data
; i
++) {
172 error
->data
[i
] = dest
;
173 memcpy(dest
, data
[i
], datalen
[i
]);
176 error
->data
[n_data
] = NULL
;
179 error
->subsystem
= strcpy(d_subsys
, subsystem
);
182 error
->code
= strcpy(d_code
, code
);
186 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
188 avl_delete(&iface
->data
, &data
->node
);
193 interface_data_flush(struct interface
*iface
)
195 struct interface_data
*d
, *tmp
;
197 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
198 interface_data_del(iface
, d
);
202 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
204 struct interface_data
*n
, *o
;
206 if (!blobmsg_check_attr(data
, true))
207 return UBUS_STATUS_INVALID_ARGUMENT
;
209 const char *name
= blobmsg_name(data
);
210 unsigned len
= blob_pad_len(data
);
212 o
= avl_find_element(&iface
->data
, name
, o
, node
);
214 if (blob_pad_len(o
->data
) == len
&& !memcmp(o
->data
, data
, len
))
217 interface_data_del(iface
, o
);
220 n
= calloc(1, sizeof(*n
) + len
);
222 return UBUS_STATUS_UNKNOWN_ERROR
;
224 memcpy(n
->data
, data
, len
);
225 n
->node
.key
= blobmsg_name(n
->data
);
226 avl_insert(&iface
->data
, &n
->node
);
228 iface
->updated
|= IUF_DATA
;
232 int interface_parse_data(struct interface
*iface
, const struct blob_attr
*attr
)
234 struct blob_attr
*cur
;
239 blob_for_each_attr(cur
, attr
, rem
) {
240 ret
= interface_add_data(iface
, cur
);
245 if (iface
->updated
&& iface
->state
== IFS_UP
)
246 interface_event(iface
, IFEV_UPDATE
);
252 interface_event(struct interface
*iface
, enum interface_event ev
)
254 struct interface_user
*dep
, *tmp
;
255 struct device
*adev
= NULL
;
257 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
258 dep
->cb(dep
, iface
, ev
);
260 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
261 dep
->cb(dep
, iface
, ev
);
265 interface_error_flush(iface
);
266 adev
= iface
->l3_dev
.dev
;
270 alias_notify_device(iface
->name
, adev
);
278 interface_flush_state(struct interface
*iface
)
280 if (iface
->l3_dev
.dev
)
281 device_release(&iface
->l3_dev
);
282 interface_data_flush(iface
);
286 mark_interface_down(struct interface
*iface
)
288 enum interface_state state
= iface
->state
;
290 if (state
== IFS_DOWN
)
293 iface
->link_up_event
= false;
294 iface
->state
= IFS_DOWN
;
298 interface_event(iface
, IFEV_DOWN
);
301 interface_event(iface
, IFEV_UP_FAILED
);
306 interface_ip_set_enabled(&iface
->config_ip
, false);
307 interface_ip_set_enabled(&iface
->proto_ip
, false);
308 interface_ip_flush(&iface
->proto_ip
);
309 interface_flush_state(iface
);
310 system_flush_routes();
314 __set_config_state(struct interface
*iface
, enum interface_config_state s
)
316 iface
->config_state
= s
;
320 __interface_set_down(struct interface
*iface
, bool force
)
322 enum interface_state state
= iface
->state
;
326 iface
->state
= IFS_TEARDOWN
;
328 __set_config_state(iface
, IFC_REMOVE
);
331 interface_event(iface
, IFEV_DOWN
);
333 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
335 interface_flush_state(iface
);
339 if (iface
->main_dev
.dev
)
340 device_release(&iface
->main_dev
);
349 __interface_set_up(struct interface
*iface
)
353 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
355 iface
->state
= IFS_SETUP
;
356 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
358 mark_interface_down(iface
);
364 interface_check_state(struct interface
*iface
)
366 bool link_state
= iface
->link_state
|| interface_force_link(iface
);
368 switch (iface
->state
) {
371 if (!iface
->enabled
|| !link_state
) {
372 iface
->state
= IFS_TEARDOWN
;
374 __set_config_state(iface
, IFC_REMOVE
);
376 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
380 if (!iface
->available
)
383 if (iface
->autostart
&& iface
->enabled
&& link_state
&& !config_init
)
384 __interface_set_up(iface
);
392 interface_set_enabled(struct interface
*iface
, bool new_state
)
394 if (iface
->enabled
== new_state
)
397 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
398 iface
->enabled
= new_state
;
399 interface_check_state(iface
);
403 interface_set_link_state(struct interface
*iface
, bool new_state
)
405 if (iface
->link_state
== new_state
)
408 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
409 iface
->link_state
= new_state
;
410 interface_check_state(iface
);
412 if (new_state
&& interface_force_link(iface
) &&
413 iface
->state
== IFS_UP
&& !iface
->link_up_event
) {
414 interface_event(iface
, IFEV_LINK_UP
);
415 iface
->link_up_event
= true;
420 interface_ext_dev_cb(struct device_user
*dep
, enum device_event ev
)
422 if (ev
== DEV_EVENT_REMOVE
)
423 device_remove_user(dep
);
427 interface_main_dev_cb(struct device_user
*dep
, enum device_event ev
)
429 struct interface
*iface
;
431 iface
= container_of(dep
, struct interface
, main_dev
);
434 interface_set_available(iface
, true);
436 case DEV_EVENT_REMOVE
:
437 interface_set_available(iface
, false);
438 if (dep
->dev
&& dep
->dev
->external
)
439 interface_set_main_dev(iface
, NULL
);
442 interface_set_enabled(iface
, true);
445 interface_set_enabled(iface
, false);
447 case DEV_EVENT_AUTH_UP
:
448 case DEV_EVENT_LINK_UP
:
449 case DEV_EVENT_LINK_DOWN
:
450 interface_set_link_state(iface
, device_link_active(dep
->dev
));
452 case DEV_EVENT_TOPO_CHANGE
:
453 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
461 interface_l3_dev_cb(struct device_user
*dep
, enum device_event ev
)
463 struct interface
*iface
;
465 iface
= container_of(dep
, struct interface
, l3_dev
);
466 if (iface
->l3_dev
.dev
== iface
->main_dev
.dev
)
470 case DEV_EVENT_LINK_DOWN
:
471 if (iface
->proto_handler
->flags
& PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN
)
472 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
480 interface_set_available(struct interface
*iface
, bool new_state
)
482 if (iface
->available
== new_state
)
485 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
486 iface
->available
= new_state
;
489 if (iface
->autostart
&& !config_init
)
490 interface_set_up(iface
);
492 __interface_set_down(iface
, true);
496 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
499 list_add(&dep
->list
, &iface_all_users
);
504 list_add(&dep
->list
, &iface
->users
);
505 if (iface
->state
== IFS_UP
)
506 dep
->cb(dep
, iface
, IFEV_UP
);
510 interface_remove_user(struct interface_user
*dep
)
512 list_del_init(&dep
->list
);
517 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
519 struct blob_attr
*cur
;
522 blobmsg_for_each_attr(cur
, list
, rem
) {
523 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
526 if (!blobmsg_check_attr(cur
, false))
529 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
530 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
531 list_add(&c
->head
, &iface
->assignment_classes
);
536 interface_clear_assignment_classes(struct interface
*iface
)
538 while (!list_empty(&iface
->assignment_classes
)) {
539 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
540 struct interface_assignment_class
, head
);
547 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
549 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
550 old
->assignment_length
!= new->assignment_length
||
551 old
->assignment_iface_id_selection
!= new->assignment_iface_id_selection
||
552 old
->assignment_weight
!= new->assignment_weight
||
553 (old
->assignment_iface_id_selection
== IFID_FIXED
&&
554 memcmp(&old
->assignment_fixed_iface_id
, &new->assignment_fixed_iface_id
,
555 sizeof(old
->assignment_fixed_iface_id
))) ||
556 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
558 struct interface_assignment_class
*c
;
559 list_for_each_entry(c
, &new->assignment_classes
, head
) {
560 /* Compare list entries one-by-one to see if there was a change */
561 if (list_empty(&old
->assignment_classes
)) /* The new list is longer */
567 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
568 struct interface_assignment_class
, head
);
570 if (strcmp(c_old
->name
, c
->name
)) /* An entry didn't match */
573 list_del(&c_old
->head
);
577 /* The old list was longer than the new one or the last entry didn't match */
578 if (!list_empty(&old
->assignment_classes
)) {
579 interface_clear_assignment_classes(old
);
583 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
586 old
->assignment_hint
= new->assignment_hint
;
587 old
->assignment_length
= new->assignment_length
;
588 old
->assignment_iface_id_selection
= new->assignment_iface_id_selection
;
589 old
->assignment_fixed_iface_id
= new->assignment_fixed_iface_id
;
590 old
->assignment_weight
= new->assignment_weight
;
591 interface_refresh_assignments(true);
596 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
598 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
599 struct device
*dev
= iface
->l3_dev
.dev
;
606 interface_set_main_dev(alias
, dev
);
607 interface_set_available(alias
, true);
611 interface_set_available(alias
, false);
612 interface_set_main_dev(alias
, NULL
);
615 interface_remove_user(dep
);
623 interface_set_device_config(struct interface
*iface
, struct device
*dev
)
625 if (!dev
|| !dev
->default_config
)
628 if (!iface
->device_config
&&
629 (!dev
->iface_config
|| dev
->config_iface
!= iface
))
632 dev
->config_iface
= iface
;
633 dev
->iface_config
= iface
->device_config
;
634 device_apply_config(dev
, dev
->type
, iface
->config
);
638 interface_claim_device(struct interface
*iface
)
640 struct interface
*parent
;
641 struct device
*dev
= NULL
;
643 if (iface
->parent_iface
.iface
)
644 interface_remove_user(&iface
->parent_iface
);
646 if (iface
->parent_ifname
) {
647 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
648 iface
->parent_iface
.cb
= interface_alias_cb
;
649 interface_add_user(&iface
->parent_iface
, parent
);
650 } else if (iface
->device
&&
651 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
652 dev
= device_get(iface
->device
, true);
653 interface_set_device_config(iface
, dev
);
655 dev
= iface
->ext_dev
.dev
;
659 interface_set_main_dev(iface
, dev
);
661 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
662 interface_set_available(iface
, true);
666 interface_cleanup_state(struct interface
*iface
)
668 interface_set_available(iface
, false);
670 interface_flush_state(iface
);
671 interface_clear_errors(iface
);
672 interface_set_proto_state(iface
, NULL
);
674 interface_set_main_dev(iface
, NULL
);
675 interface_set_l3_dev(iface
, NULL
);
679 interface_cleanup(struct interface
*iface
)
681 struct interface_user
*dep
, *tmp
;
683 uloop_timeout_cancel(&iface
->remove_timer
);
684 device_remove_user(&iface
->ext_dev
);
686 if (iface
->parent_iface
.iface
)
687 interface_remove_user(&iface
->parent_iface
);
689 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
690 interface_remove_user(dep
);
692 interface_clear_assignment_classes(iface
);
693 interface_ip_flush(&iface
->config_ip
);
694 interface_cleanup_state(iface
);
698 interface_do_free(struct interface
*iface
)
700 interface_event(iface
, IFEV_FREE
);
701 interface_cleanup(iface
);
703 netifd_ubus_remove_interface(iface
);
704 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
707 if (iface
->jail_device
)
708 free(iface
->jail_device
);
709 if (iface
->host_device
)
710 free(iface
->host_device
);
716 interface_do_reload(struct interface
*iface
)
718 interface_event(iface
, IFEV_RELOAD
);
719 interface_cleanup_state(iface
);
720 proto_init_interface(iface
, iface
->config
);
721 interface_claim_device(iface
);
725 interface_handle_config_change(struct interface
*iface
)
727 enum interface_config_state state
= iface
->config_state
;
729 iface
->config_state
= IFC_NORMAL
;
734 interface_do_reload(iface
);
737 interface_do_free(iface
);
740 if (iface
->autostart
)
741 interface_set_up(iface
);
745 interface_proto_event_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
747 struct interface
*iface
= state
->iface
;
751 if (iface
->state
!= IFS_SETUP
) {
752 if (iface
->state
== IFS_UP
&& iface
->updated
)
753 interface_event(iface
, IFEV_UPDATE
);
757 if (!iface
->l3_dev
.dev
)
758 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
760 interface_ip_set_enabled(&iface
->config_ip
, true);
761 interface_ip_set_enabled(&iface
->proto_ip
, true);
762 system_flush_routes();
763 iface
->state
= IFS_UP
;
764 iface
->start_time
= system_get_rtime();
765 interface_event(iface
, IFEV_UP
);
766 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
769 if (iface
->state
== IFS_DOWN
)
772 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
773 mark_interface_down(iface
);
774 interface_write_resolv_conf(iface
->jail
);
775 if (iface
->main_dev
.dev
)
776 device_release(&iface
->main_dev
);
777 if (iface
->l3_dev
.dev
)
778 device_remove_user(&iface
->l3_dev
);
779 interface_handle_config_change(iface
);
781 case IFPEV_LINK_LOST
:
782 if (iface
->state
!= IFS_UP
)
785 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
786 mark_interface_down(iface
);
787 iface
->state
= IFS_SETUP
;
793 interface_write_resolv_conf(iface
->jail
);
796 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
799 iface
->proto
->free(iface
->proto
);
802 iface
->state
= IFS_DOWN
;
803 iface
->proto
= state
;
807 state
->proto_event
= interface_proto_event_cb
;
808 state
->iface
= iface
;
812 interface_alloc(const char *name
, struct blob_attr
*config
, bool dynamic
)
814 struct interface
*iface
;
815 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
816 struct blob_attr
*cur
;
817 const char *proto_name
= NULL
;
819 bool force_link
= false;
821 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
822 iface
->name
= strcpy(iface_name
, name
);
823 INIT_LIST_HEAD(&iface
->errors
);
824 INIT_LIST_HEAD(&iface
->users
);
825 INIT_LIST_HEAD(&iface
->hotplug_list
);
826 INIT_LIST_HEAD(&iface
->assignment_classes
);
827 interface_ip_init(iface
);
828 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
829 iface
->config_ip
.enabled
= false;
831 iface
->main_dev
.cb
= interface_main_dev_cb
;
832 iface
->l3_dev
.cb
= interface_l3_dev_cb
;
833 iface
->ext_dev
.cb
= interface_ext_dev_cb
;
835 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
836 blob_data(config
), blob_len(config
));
839 if ((cur
= tb
[IFACE_ATTR_ZONE
]))
840 iface
->zone
= strdup(blobmsg_get_string(cur
));
842 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
843 proto_name
= blobmsg_data(cur
);
845 proto_attach_interface(iface
, proto_name
);
846 if (iface
->proto_handler
->flags
& PROTO_FLAG_FORCE_LINK_DEFAULT
)
849 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
850 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], force_link
);
851 iface
->dynamic
= dynamic
;
852 iface
->proto_ip
.no_defaultroute
=
853 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
854 iface
->proto_ip
.no_dns
=
855 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
857 if ((cur
= tb
[IFACE_ATTR_DNS
]))
858 interface_add_dns_server_list(&iface
->config_ip
, cur
);
860 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
861 interface_add_dns_search_list(&iface
->config_ip
, cur
);
863 if ((cur
= tb
[IFACE_ATTR_DNS_METRIC
]))
864 iface
->dns_metric
= blobmsg_get_u32(cur
);
866 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
867 iface
->metric
= blobmsg_get_u32(cur
);
869 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
870 iface
->assignment_length
= blobmsg_get_u32(cur
);
873 iface
->assignment_iface_id_selection
= IFID_FIXED
;
874 iface
->assignment_fixed_iface_id
= in6addr_any
;
875 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
877 if ((cur
= tb
[IFACE_ATTR_IP6IFACEID
])) {
878 const char *ifaceid
= blobmsg_data(cur
);
879 if (!strcmp(ifaceid
, "random")) {
880 iface
->assignment_iface_id_selection
= IFID_RANDOM
;
882 else if (!strcmp(ifaceid
, "eui64")) {
883 iface
->assignment_iface_id_selection
= IFID_EUI64
;
886 /* we expect an IPv6 address with network id zero here -> fixed iface id
887 if we cannot parse -> revert to iface id 1 */
888 if (inet_pton(AF_INET6
,ifaceid
,&iface
->assignment_fixed_iface_id
) != 1 ||
889 iface
->assignment_fixed_iface_id
.s6_addr32
[0] != 0 ||
890 iface
->assignment_fixed_iface_id
.s6_addr32
[1] != 0) {
891 iface
->assignment_fixed_iface_id
= in6addr_any
;
892 iface
->assignment_fixed_iface_id
.s6_addr
[15] = 1;
893 netifd_log_message(L_WARNING
, "Failed to parse ip6ifaceid for interface '%s', \
894 falling back to iface id 1.\n", iface
->name
);
899 iface
->assignment_hint
= -1;
900 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
901 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
902 ~((1 << (64 - iface
->assignment_length
)) - 1);
904 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
905 interface_add_assignment_classes(iface
, cur
);
907 if ((cur
= tb
[IFACE_ATTR_IP6WEIGHT
]))
908 iface
->assignment_weight
= blobmsg_get_u32(cur
);
910 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
911 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
912 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
915 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
916 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
917 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
920 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
922 iface
->config_autostart
= iface
->autostart
;
925 if ((cur
= tb
[IFACE_ATTR_JAIL
])) {
926 iface
->jail
= strdup(blobmsg_get_string(cur
));
927 iface
->autostart
= false;
930 iface
->jail_device
= NULL
;
931 if ((cur
= tb
[IFACE_ATTR_JAIL_DEVICE
]))
932 iface
->jail_device
= strdup(blobmsg_get_string(cur
));
933 else if ((cur
= tb
[IFACE_ATTR_JAIL_IFNAME
]))
934 iface
->jail_device
= strdup(blobmsg_get_string(cur
));
936 iface
->host_device
= NULL
;
937 if ((cur
= tb
[IFACE_ATTR_HOST_DEVICE
]))
938 iface
->host_device
= strdup(blobmsg_get_string(cur
));
943 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
945 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
946 struct blob_attr
*cur
;
949 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
950 blob_data(config
), blob_len(config
));
953 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
954 iface
->parent_ifname
= blobmsg_data(cur
);
956 if (!iface
->parent_ifname
)
959 cur
= tb
[IFACE_ATTR_DEVICE
];
961 cur
= tb
[IFACE_ATTR_IFNAME
];
963 iface
->device
= blobmsg_data(cur
);
966 if (iface
->dynamic
) {
967 name
= strdup(iface
->name
);
973 iface
->config
= config
;
974 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
977 iface
= vlist_find(&interfaces
, name
, iface
, node
);
980 /* Don't delete dynamic interface on reload */
982 iface
->node
.version
= -1;
989 interface_add(struct interface
*iface
, struct blob_attr
*config
)
991 return __interface_add(iface
, config
, false);
995 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
997 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
1000 return __interface_add(iface
, config
, true);
1004 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
1006 bool enabled
= iface
->config_ip
.enabled
;
1007 bool claimed
= iface
->l3_dev
.claimed
;
1009 if (iface
->l3_dev
.dev
== dev
)
1012 interface_ip_set_enabled(&iface
->config_ip
, false);
1013 interface_ip_set_enabled(&iface
->proto_ip
, false);
1014 interface_ip_flush(&iface
->proto_ip
);
1015 device_add_user(&iface
->l3_dev
, dev
);
1019 if (device_claim(&iface
->l3_dev
) < 0)
1022 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
1023 interface_ip_set_enabled(&iface
->proto_ip
, enabled
);
1028 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
1030 bool claimed
= iface
->l3_dev
.claimed
;
1032 if (iface
->main_dev
.dev
== dev
)
1035 interface_set_available(iface
, false);
1036 device_add_user(&iface
->main_dev
, dev
);
1038 interface_set_link_state(iface
, false);
1043 if (device_claim(&iface
->l3_dev
) < 0)
1047 if (!iface
->l3_dev
.dev
)
1048 interface_set_l3_dev(iface
, dev
);
1052 interface_remove_link(struct interface
*iface
, struct device
*dev
,
1053 struct blob_attr
*vlan
)
1055 struct device
*mdev
= iface
->main_dev
.dev
;
1057 if (mdev
&& mdev
->hotplug_ops
)
1058 return mdev
->hotplug_ops
->del(mdev
, dev
, vlan
);
1060 if (dev
== iface
->ext_dev
.dev
)
1061 device_remove_user(&iface
->ext_dev
);
1063 if (!iface
->main_dev
.hotplug
)
1064 return UBUS_STATUS_INVALID_ARGUMENT
;
1066 if (dev
!= iface
->main_dev
.dev
)
1067 return UBUS_STATUS_INVALID_ARGUMENT
;
1069 interface_set_main_dev(iface
, NULL
);
1074 interface_add_link(struct interface
*iface
, struct device
*dev
,
1075 struct blob_attr
*vlan
, bool link_ext
)
1077 struct device
*mdev
= iface
->main_dev
.dev
;
1082 if (iface
->main_dev
.hotplug
)
1083 device_remove_user(&iface
->main_dev
);
1086 if (mdev
->hotplug_ops
)
1087 return mdev
->hotplug_ops
->add(mdev
, dev
, vlan
);
1089 return UBUS_STATUS_NOT_SUPPORTED
;
1093 device_add_user(&iface
->ext_dev
, dev
);
1095 interface_set_main_dev(iface
, dev
);
1096 iface
->main_dev
.hotplug
= true;
1101 interface_handle_link(struct interface
*iface
, const char *name
,
1102 struct blob_attr
*vlan
, bool add
, bool link_ext
)
1106 dev
= device_get(name
, add
? (link_ext
? 2 : 1) : 0);
1108 return UBUS_STATUS_NOT_FOUND
;
1111 return interface_remove_link(iface
, dev
, vlan
);
1113 interface_set_device_config(iface
, dev
);
1115 device_set_present(dev
, true);
1117 return interface_add_link(iface
, dev
, vlan
, link_ext
);
1121 interface_set_up(struct interface
*iface
)
1124 const char *error
= NULL
;
1126 iface
->autostart
= true;
1128 if (iface
->state
!= IFS_DOWN
)
1131 interface_clear_errors(iface
);
1132 if (iface
->available
) {
1133 if (iface
->main_dev
.dev
) {
1134 ret
= device_claim(&iface
->main_dev
);
1136 interface_check_state(iface
);
1138 error
= "DEVICE_CLAIM_FAILED";
1140 ret
= __interface_set_up(iface
);
1142 error
= "SETUP_FAILED";
1145 error
= "NO_DEVICE";
1148 interface_add_error(iface
, "interface", error
, NULL
, 0);
1152 interface_set_down(struct interface
*iface
)
1155 vlist_for_each_element(&interfaces
, iface
, node
)
1156 __interface_set_down(iface
, false);
1158 iface
->autostart
= false;
1159 __interface_set_down(iface
, false);
1164 interface_renew(struct interface
*iface
)
1166 if (iface
->state
== IFS_TEARDOWN
|| iface
->state
== IFS_DOWN
)
1169 return interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
1173 interface_start_pending(void)
1175 struct interface
*iface
;
1177 vlist_for_each_element(&interfaces
, iface
, node
) {
1178 if (iface
->autostart
)
1179 interface_set_up(iface
);
1184 interface_start_jail(int netns_fd
, const char *jail
)
1186 struct interface
*iface
;
1188 vlist_for_each_element(&interfaces
, iface
, node
) {
1189 if (!iface
->jail
|| strcmp(iface
->jail
, jail
))
1192 system_link_netns_move(iface
->main_dev
.dev
, netns_fd
, iface
->jail_device
);
1197 interface_stop_jail(int netns_fd
)
1199 struct interface
*iface
;
1202 vlist_for_each_element(&interfaces
, iface
, node
) {
1203 orig_ifname
= iface
->host_device
;
1204 interface_set_down(iface
);
1205 system_link_netns_move(iface
->main_dev
.dev
, netns_fd
, orig_ifname
);
1210 set_config_state(struct interface
*iface
, enum interface_config_state s
)
1212 __set_config_state(iface
, s
);
1213 if (iface
->state
== IFS_DOWN
)
1214 interface_handle_config_change(iface
);
1216 __interface_set_down(iface
, false);
1220 interface_update_start(struct interface
*iface
, const bool keep_old
)
1225 interface_ip_update_start(&iface
->proto_ip
);
1229 interface_update_complete(struct interface
*iface
)
1231 interface_ip_update_complete(&iface
->proto_ip
);
1235 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
1237 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
1238 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
1242 interface_device_config_changed(struct interface
*if_old
, struct interface
*if_new
)
1244 struct blob_attr
*ntb
[__DEV_ATTR_MAX
];
1245 struct blob_attr
*otb
[__DEV_ATTR_MAX
];
1246 struct device
*dev
= if_old
->main_dev
.dev
;
1247 unsigned long diff
= 0;
1249 BUILD_BUG_ON(sizeof(diff
) < __DEV_ATTR_MAX
/ 8);
1254 if (if_old
->device_config
!= if_new
->device_config
)
1257 if (!if_new
->device_config
)
1260 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, otb
,
1261 blob_data(if_old
->config
), blob_len(if_old
->config
));
1263 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, ntb
,
1264 blob_data(if_new
->config
), blob_len(if_new
->config
));
1266 uci_blob_diff(ntb
, otb
, &device_attr_list
, &diff
);
1271 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
1273 struct blob_attr
*old_config
= if_old
->config
;
1274 bool reload
= false, reload_ip
= false, update_prefix_delegation
= false;
1276 #define FIELD_CHANGED_STR(field) \
1277 ((!!if_old->field != !!if_new->field) || \
1279 strcmp(if_old->field, if_new->field) != 0))
1281 if (FIELD_CHANGED_STR(parent_ifname
)) {
1282 if (if_old
->parent_iface
.iface
)
1283 interface_remove_user(&if_old
->parent_iface
);
1287 if (!reload
&& interface_device_config_changed(if_old
, if_new
))
1290 if (FIELD_CHANGED_STR(device
) ||
1291 if_old
->proto_handler
!= if_new
->proto_handler
)
1294 if (!if_old
->proto_handler
->config_params
)
1295 D(INTERFACE
, "No config parameters for interface '%s'\n",
1297 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
1298 if_old
->proto_handler
->config_params
))
1301 #define UPDATE(field, __var) ({ \
1302 bool __changed = (if_old->field != if_new->field); \
1303 if_old->field = if_new->field; \
1304 __var |= __changed; \
1307 if_old
->config
= if_new
->config
;
1308 if (if_old
->config_autostart
!= if_new
->config_autostart
) {
1309 if (if_old
->config_autostart
)
1312 if_old
->autostart
= if_new
->config_autostart
;
1315 if_old
->device_config
= if_new
->device_config
;
1316 if_old
->config_autostart
= if_new
->config_autostart
;
1320 if_old
->jail
= if_new
->jail
;
1322 if_old
->autostart
= false;
1324 if (if_old
->jail_device
)
1325 free(if_old
->jail_device
);
1327 if_old
->jail_device
= if_new
->jail_device
;
1329 if (if_old
->host_device
)
1330 free(if_old
->host_device
);
1332 if_old
->host_device
= if_new
->host_device
;
1334 if_old
->device
= if_new
->device
;
1335 if_old
->parent_ifname
= if_new
->parent_ifname
;
1336 if_old
->dynamic
= if_new
->dynamic
;
1337 if_old
->proto_handler
= if_new
->proto_handler
;
1338 if_old
->force_link
= if_new
->force_link
;
1339 if_old
->dns_metric
= if_new
->dns_metric
;
1341 if (if_old
->proto_ip
.no_delegation
!= if_new
->proto_ip
.no_delegation
) {
1342 if_old
->proto_ip
.no_delegation
= if_new
->proto_ip
.no_delegation
;
1343 update_prefix_delegation
= true;
1346 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1347 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1349 UPDATE(metric
, reload_ip
);
1350 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1351 UPDATE(ip4table
, reload_ip
);
1352 UPDATE(ip6table
, reload_ip
);
1353 interface_merge_assignment_data(if_old
, if_new
);
1358 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1360 interface_clear_errors(if_old
);
1361 set_config_state(if_old
, IFC_RELOAD
);
1366 bool config_ip_enabled
= if_old
->config_ip
.enabled
;
1367 bool proto_ip_enabled
= if_old
->proto_ip
.enabled
;
1369 interface_ip_set_enabled(&if_old
->config_ip
, false);
1370 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1371 interface_ip_set_enabled(&if_old
->proto_ip
, proto_ip_enabled
);
1372 interface_ip_set_enabled(&if_old
->config_ip
, config_ip_enabled
);
1375 if (update_prefix_delegation
)
1376 interface_update_prefix_delegation(&if_old
->proto_ip
);
1378 interface_write_resolv_conf(if_old
->jail
);
1379 if (if_old
->main_dev
.dev
)
1380 interface_check_state(if_old
);
1383 if_new
->config
= NULL
;
1384 interface_cleanup(if_new
);
1390 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1391 struct vlist_node
*node_old
)
1393 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1394 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1396 if (node_old
&& node_new
) {
1397 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1398 interface_change_config(if_old
, if_new
);
1399 } else if (node_old
) {
1400 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1401 set_config_state(if_old
, IFC_REMOVE
);
1402 } else if (node_new
) {
1403 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1404 interface_event(if_new
, IFEV_CREATE
);
1405 proto_init_interface(if_new
, if_new
->config
);
1406 interface_claim_device(if_new
);
1407 netifd_ubus_add_interface(if_new
);
1413 interface_init_list(void)
1415 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1416 interfaces
.keep_old
= true;
1417 interfaces
.no_delete
= true;