2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
20 #include "interface.h"
21 #include "interface-ip.h"
27 struct vlist_tree interfaces
;
28 static LIST_HEAD(iface_all_users
);
34 IFACE_ATTR_DEFAULTROUTE
,
37 IFACE_ATTR_DNS_SEARCH
,
45 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
46 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
47 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
48 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
49 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
50 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
51 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
52 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
53 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
54 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
55 [IFACE_ATTR_IP6ASSIGN
] = { .name
= "ip6assign", .type
= BLOBMSG_TYPE_INT32
},
56 [IFACE_ATTR_IP6HINT
] = { .name
= "ip6hint", .type
= BLOBMSG_TYPE_STRING
},
59 static const union config_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
60 [IFACE_ATTR_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
63 const struct config_param_list interface_attr_list
= {
64 .n_params
= IFACE_ATTR_MAX
,
65 .params
= iface_attrs
,
66 .info
= iface_attr_info
,
70 interface_clear_errors(struct interface
*iface
)
72 struct interface_error
*error
, *tmp
;
74 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
75 list_del(&error
->list
);
80 void interface_add_error(struct interface
*iface
, const char *subsystem
,
81 const char *code
, const char **data
, int n_data
)
83 struct interface_error
*error
;
86 char *dest
, *d_subsys
, *d_code
;
89 len
= n_data
* sizeof(char *);
90 datalen
= alloca(len
);
91 for (i
= 0; i
< n_data
; i
++) {
92 datalen
[i
] = strlen(data
[i
]) + 1;
97 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
98 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
99 &d_code
, code
? strlen(code
) + 1 : 0);
103 list_add_tail(&error
->list
, &iface
->errors
);
105 dest
= (char *) &error
->data
[n_data
+ 1];
106 for (i
= 0; i
< n_data
; i
++) {
107 error
->data
[i
] = dest
;
108 memcpy(dest
, data
[i
], datalen
[i
]);
111 error
->data
[n_data
++] = NULL
;
114 error
->subsystem
= strcpy(d_subsys
, subsystem
);
117 error
->code
= strcpy(d_code
, code
);
121 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
123 avl_delete(&iface
->data
, &data
->node
);
128 interface_data_flush(struct interface
*iface
)
130 struct interface_data
*d
, *tmp
;
132 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
133 interface_data_del(iface
, d
);
137 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
139 struct interface_data
*n
, *o
;
141 if (!blobmsg_check_attr(data
, true))
142 return UBUS_STATUS_INVALID_ARGUMENT
;
144 n
= calloc(1, sizeof(*n
) + blob_pad_len(data
));
145 memcpy(n
->data
, data
, blob_pad_len(data
));
146 n
->node
.key
= blobmsg_name(data
);
148 o
= avl_find_element(&iface
->data
, n
->node
.key
, o
, node
);
150 interface_data_del(iface
, o
);
152 avl_insert(&iface
->data
, &n
->node
);
157 interface_event(struct interface
*iface
, enum interface_event ev
)
159 struct interface_user
*dep
, *tmp
;
160 struct device
*adev
= NULL
;
162 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
163 dep
->cb(dep
, iface
, ev
);
165 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
166 dep
->cb(dep
, iface
, ev
);
170 adev
= iface
->l3_dev
.dev
;
173 alias_notify_device(iface
->name
, adev
);
181 interface_flush_state(struct interface
*iface
)
183 if (iface
->l3_dev
.dev
)
184 device_release(&iface
->l3_dev
);
185 interface_data_flush(iface
);
189 mark_interface_down(struct interface
*iface
)
191 enum interface_state state
= iface
->state
;
193 iface
->state
= IFS_DOWN
;
195 interface_event(iface
, IFEV_DOWN
);
196 interface_ip_set_enabled(&iface
->config_ip
, false);
197 interface_ip_flush(&iface
->proto_ip
);
198 interface_flush_state(iface
);
199 system_flush_routes();
203 __interface_set_down(struct interface
*iface
, bool force
)
205 if (iface
->state
== IFS_DOWN
||
206 iface
->state
== IFS_TEARDOWN
)
209 if (iface
->state
== IFS_UP
)
210 interface_event(iface
, IFEV_DOWN
);
211 iface
->state
= IFS_TEARDOWN
;
212 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
214 interface_flush_state(iface
);
218 interface_cb(struct device_user
*dep
, enum device_event ev
)
220 struct interface
*iface
;
223 iface
= container_of(dep
, struct interface
, main_dev
);
228 case DEV_EVENT_REMOVE
:
235 interface_set_available(iface
, new_state
);
236 if (!new_state
&& dep
->dev
->external
)
237 interface_set_main_dev(iface
, NULL
);
241 interface_set_available(struct interface
*iface
, bool new_state
)
243 if (iface
->available
== new_state
)
246 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
247 iface
->available
= new_state
;
250 if (iface
->autostart
&& !config_init
)
251 interface_set_up(iface
);
253 __interface_set_down(iface
, true);
257 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
260 list_add(&dep
->list
, &iface_all_users
);
265 list_add(&dep
->list
, &iface
->users
);
266 if (iface
->state
== IFS_UP
)
267 dep
->cb(dep
, iface
, IFEV_UP
);
271 interface_remove_user(struct interface_user
*dep
)
273 list_del_init(&dep
->list
);
278 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
280 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
281 struct device
*dev
= iface
->l3_dev
.dev
;
288 interface_set_main_dev(alias
, dev
);
289 interface_set_available(alias
, true);
292 interface_set_available(alias
, false);
293 interface_set_main_dev(alias
, NULL
);
296 interface_remove_user(dep
);
304 interface_claim_device(struct interface
*iface
)
306 struct interface
*parent
;
307 struct device
*dev
= NULL
;
309 if (iface
->parent_iface
.iface
)
310 interface_remove_user(&iface
->parent_iface
);
312 if (iface
->parent_ifname
) {
313 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
314 iface
->parent_iface
.cb
= interface_alias_cb
;
315 interface_add_user(&iface
->parent_iface
, parent
);
316 } else if (iface
->ifname
&&
317 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
318 dev
= device_get(iface
->ifname
, true);
322 interface_set_main_dev(iface
, dev
);
324 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
325 interface_set_available(iface
, true);
329 interface_cleanup_state(struct interface
*iface
)
331 interface_set_available(iface
, false);
333 interface_flush_state(iface
);
334 interface_clear_errors(iface
);
335 interface_set_proto_state(iface
, NULL
);
337 if (iface
->main_dev
.dev
)
338 interface_set_main_dev(iface
, NULL
);
342 interface_cleanup(struct interface
*iface
)
344 struct interface_user
*dep
, *tmp
;
346 if (iface
->parent_iface
.iface
)
347 interface_remove_user(&iface
->parent_iface
);
349 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
350 interface_remove_user(dep
);
352 interface_ip_flush(&iface
->config_ip
);
353 interface_cleanup_state(iface
);
357 interface_do_free(struct interface
*iface
)
359 interface_event(iface
, IFEV_FREE
);
360 interface_cleanup(iface
);
362 netifd_ubus_remove_interface(iface
);
363 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
368 interface_do_reload(struct interface
*iface
)
370 interface_event(iface
, IFEV_RELOAD
);
371 interface_cleanup_state(iface
);
372 proto_init_interface(iface
, iface
->config
);
373 interface_claim_device(iface
);
377 interface_handle_config_change(struct interface
*iface
)
379 enum interface_config_state state
= iface
->config_state
;
381 iface
->config_state
= IFC_NORMAL
;
386 interface_do_reload(iface
);
389 interface_do_free(iface
);
392 if (iface
->autostart
&& iface
->available
)
393 interface_set_up(iface
);
397 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
399 struct interface
*iface
= state
->iface
;
403 if (iface
->state
!= IFS_SETUP
)
406 interface_ip_set_enabled(&iface
->config_ip
, true);
407 system_flush_routes();
408 iface
->state
= IFS_UP
;
409 iface
->start_time
= system_get_rtime();
410 interface_event(iface
, IFEV_UP
);
411 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
414 if (iface
->state
== IFS_DOWN
)
417 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
418 mark_interface_down(iface
);
419 if (iface
->main_dev
.dev
)
420 device_release(&iface
->main_dev
);
421 interface_handle_config_change(iface
);
423 case IFPEV_LINK_LOST
:
424 if (iface
->state
!= IFS_UP
)
427 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
428 mark_interface_down(iface
);
429 iface
->state
= IFS_SETUP
;
433 interface_write_resolv_conf();
436 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
439 iface
->proto
->free(iface
->proto
);
442 iface
->state
= IFS_DOWN
;
443 iface
->proto
= state
;
447 state
->proto_event
= interface_proto_cb
;
448 state
->iface
= iface
;
452 interface_init(struct interface
*iface
, const char *name
,
453 struct blob_attr
*config
)
455 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
456 struct blob_attr
*cur
;
457 const char *proto_name
= NULL
;
459 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
460 INIT_LIST_HEAD(&iface
->errors
);
461 INIT_LIST_HEAD(&iface
->users
);
462 INIT_LIST_HEAD(&iface
->hotplug_list
);
463 interface_ip_init(iface
);
464 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
465 iface
->config_ip
.enabled
= false;
467 iface
->main_dev
.cb
= interface_cb
;
469 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
470 blob_data(config
), blob_len(config
));
472 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
473 proto_name
= blobmsg_data(cur
);
475 proto_attach_interface(iface
, proto_name
);
477 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
478 iface
->proto_ip
.no_defaultroute
=
479 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
480 iface
->proto_ip
.no_dns
=
481 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
483 if ((cur
= tb
[IFACE_ATTR_DNS
]))
484 interface_add_dns_server_list(&iface
->config_ip
, cur
);
486 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
487 interface_add_dns_search_list(&iface
->config_ip
, cur
);
489 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
490 iface
->metric
= blobmsg_get_u32(cur
);
492 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
493 iface
->config_ip
.assignment_length
= blobmsg_get_u32(cur
);
495 iface
->config_ip
.assignment_hint
= -1;
496 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
497 iface
->config_ip
.assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
498 ~((1 << (64 - iface
->config_ip
.assignment_length
)) - 1);
500 iface
->config_autostart
= iface
->autostart
;
503 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
505 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
506 struct blob_attr
*cur
;
508 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
509 blob_data(config
), blob_len(config
));
512 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
513 iface
->parent_ifname
= blobmsg_data(cur
);
515 if (!iface
->parent_ifname
)
518 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
519 iface
->ifname
= blobmsg_data(cur
);
523 iface
->config
= config
;
524 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
529 interface_add(struct interface
*iface
, struct blob_attr
*config
)
531 __interface_add(iface
, config
, false);
535 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
537 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
540 return __interface_add(iface
, config
, true);
544 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
546 bool enabled
= iface
->config_ip
.enabled
;
547 bool claimed
= iface
->l3_dev
.claimed
;
549 if (iface
->l3_dev
.dev
== dev
)
552 interface_ip_set_enabled(&iface
->config_ip
, false);
553 interface_ip_flush(&iface
->proto_ip
);
554 device_add_user(&iface
->l3_dev
, dev
);
558 device_claim(&iface
->l3_dev
);
559 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
564 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
566 bool set_l3
= (iface
->main_dev
.dev
== iface
->l3_dev
.dev
);
567 bool claimed
= iface
->l3_dev
.claimed
;
569 if (iface
->main_dev
.dev
== dev
)
573 interface_set_l3_dev(iface
, dev
);
575 device_add_user(&iface
->main_dev
, dev
);
577 device_claim(&iface
->l3_dev
);
579 if (!iface
->l3_dev
.dev
)
580 interface_set_l3_dev(iface
, dev
);
584 interface_remove_link(struct interface
*iface
, struct device
*dev
)
586 struct device
*mdev
= iface
->main_dev
.dev
;
588 if (mdev
&& mdev
->hotplug_ops
)
589 return mdev
->hotplug_ops
->del(mdev
, dev
);
591 if (!iface
->main_dev
.hotplug
)
592 return UBUS_STATUS_INVALID_ARGUMENT
;
594 if (dev
!= iface
->main_dev
.dev
)
595 return UBUS_STATUS_INVALID_ARGUMENT
;
597 device_remove_user(&iface
->main_dev
);
602 interface_add_link(struct interface
*iface
, struct device
*dev
)
604 struct device
*mdev
= iface
->main_dev
.dev
;
609 if (iface
->main_dev
.hotplug
)
610 device_remove_user(&iface
->main_dev
);
613 if (mdev
->hotplug_ops
)
614 return mdev
->hotplug_ops
->add(mdev
, dev
);
616 return UBUS_STATUS_NOT_SUPPORTED
;
619 interface_set_main_dev(iface
, dev
);
620 iface
->main_dev
.hotplug
= true;
625 interface_set_up(struct interface
*iface
)
629 iface
->autostart
= true;
631 if (iface
->state
!= IFS_DOWN
)
634 interface_clear_errors(iface
);
635 if (!iface
->available
) {
636 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
640 if (iface
->main_dev
.dev
) {
641 ret
= device_claim(&iface
->main_dev
);
646 iface
->state
= IFS_SETUP
;
647 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
649 mark_interface_down(iface
);
657 interface_set_down(struct interface
*iface
)
660 vlist_for_each_element(&interfaces
, iface
, node
)
661 __interface_set_down(iface
, false);
663 iface
->autostart
= false;
664 __interface_set_down(iface
, false);
671 interface_start_pending(void)
673 struct interface
*iface
;
675 vlist_for_each_element(&interfaces
, iface
, node
) {
676 if (iface
->available
&& iface
->autostart
)
677 interface_set_up(iface
);
682 set_config_state(struct interface
*iface
, enum interface_config_state s
)
684 iface
->config_state
= s
;
685 if (iface
->state
== IFS_DOWN
)
686 interface_handle_config_change(iface
);
688 __interface_set_down(iface
, false);
692 interface_update_start(struct interface
*iface
)
694 interface_ip_update_start(&iface
->proto_ip
);
698 interface_update_complete(struct interface
*iface
)
700 interface_ip_update_complete(&iface
->proto_ip
);
704 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
706 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
707 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
711 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
713 struct blob_attr
*old_config
= if_old
->config
;
714 bool reload
= false, reload_ip
= false, reload_assignment
= false;
716 #define FIELD_CHANGED_STR(field) \
717 ((!!if_old->field != !!if_new->field) || \
719 strcmp(if_old->field, if_new->field) != 0))
721 if (FIELD_CHANGED_STR(parent_ifname
)) {
722 if (if_old
->parent_iface
.iface
)
723 interface_remove_user(&if_old
->parent_iface
);
727 if (FIELD_CHANGED_STR(ifname
) ||
728 if_old
->proto_handler
!= if_new
->proto_handler
)
731 if (!if_old
->proto_handler
->config_params
)
732 D(INTERFACE
, "No config parameters for interface '%s'\n",
734 else if (!config_check_equal(if_old
->config
, if_new
->config
,
735 if_old
->proto_handler
->config_params
))
738 #define UPDATE(field, __var) ({ \
739 bool __changed = (if_old->field != if_new->field); \
740 if_old->field = if_new->field; \
741 __var |= __changed; \
744 if_old
->config
= if_new
->config
;
745 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
746 if_old
->autostart
= true;
748 if_old
->device_config
= if_new
->device_config
;
749 if_old
->config_autostart
= if_new
->config_autostart
;
750 if_old
->ifname
= if_new
->ifname
;
751 if_old
->parent_ifname
= if_new
->parent_ifname
;
752 if_old
->proto_handler
= if_new
->proto_handler
;
754 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
755 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
757 UPDATE(metric
, reload_ip
);
758 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
759 UPDATE(config_ip
.assignment_length
, reload_assignment
);
760 UPDATE(config_ip
.assignment_hint
, reload_assignment
);
765 D(INTERFACE
, "Reload interface '%s because of config changes\n",
767 interface_clear_errors(if_old
);
768 set_config_state(if_old
, IFC_RELOAD
);
773 interface_ip_set_enabled(&if_old
->config_ip
, false);
774 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
775 interface_ip_set_enabled(&if_old
->proto_ip
, false);
776 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
779 if (reload_assignment
)
780 interface_refresh_assignments(true);
782 interface_write_resolv_conf();
785 if_new
->config
= NULL
;
786 interface_cleanup(if_new
);
792 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
793 struct vlist_node
*node_old
)
795 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
796 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
798 if (node_old
&& node_new
) {
799 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
800 interface_change_config(if_old
, if_new
);
801 } else if (node_old
) {
802 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
803 set_config_state(if_old
, IFC_REMOVE
);
804 } else if (node_new
) {
805 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
806 proto_init_interface(if_new
, if_new
->config
);
807 interface_claim_device(if_new
);
808 netifd_ubus_add_interface(if_new
);
814 interface_init_list(void)
816 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
817 interfaces
.keep_old
= true;
818 interfaces
.no_delete
= true;