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
,
43 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
44 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
45 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
46 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
47 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
48 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
49 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
50 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
51 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
52 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
55 static const union config_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
56 [IFACE_ATTR_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
59 const struct config_param_list interface_attr_list
= {
60 .n_params
= IFACE_ATTR_MAX
,
61 .params
= iface_attrs
,
62 .info
= iface_attr_info
,
66 interface_clear_errors(struct interface
*iface
)
68 struct interface_error
*error
, *tmp
;
70 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
71 list_del(&error
->list
);
76 void interface_add_error(struct interface
*iface
, const char *subsystem
,
77 const char *code
, const char **data
, int n_data
)
79 struct interface_error
*error
;
85 len
= n_data
* sizeof(char *);
86 datalen
= alloca(len
);
87 for (i
= 0; i
< n_data
; i
++) {
88 datalen
[i
] = strlen(data
[i
]) + 1;
93 error
= calloc(1, sizeof(*error
) + sizeof(char *) + len
);
97 list_add_tail(&error
->list
, &iface
->errors
);
98 error
->subsystem
= subsystem
;
101 dest
= (char *) &error
->data
[n_data
+ 1];
102 for (i
= 0; i
< n_data
; i
++) {
103 error
->data
[i
] = dest
;
104 memcpy(dest
, data
[i
], datalen
[i
]);
107 error
->data
[n_data
] = NULL
;
111 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
113 avl_delete(&iface
->data
, &data
->node
);
118 interface_data_flush(struct interface
*iface
)
120 struct interface_data
*d
, *tmp
;
122 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
123 interface_data_del(iface
, d
);
127 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
129 struct interface_data
*n
, *o
;
131 if (!blobmsg_check_attr(data
, true))
132 return UBUS_STATUS_INVALID_ARGUMENT
;
134 n
= calloc(1, sizeof(*n
) + blob_pad_len(data
));
135 memcpy(n
->data
, data
, blob_pad_len(data
));
136 n
->node
.key
= blobmsg_name(data
);
138 o
= avl_find_element(&iface
->data
, n
->node
.key
, o
, node
);
140 interface_data_del(iface
, o
);
142 avl_insert(&iface
->data
, &n
->node
);
147 interface_event(struct interface
*iface
, enum interface_event ev
)
149 struct interface_user
*dep
, *tmp
;
150 struct device
*adev
= NULL
;
152 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
153 dep
->cb(dep
, iface
, ev
);
155 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
156 dep
->cb(dep
, iface
, ev
);
160 adev
= iface
->main_dev
.dev
;
163 alias_notify_device(iface
->name
, adev
);
171 interface_flush_state(struct interface
*iface
)
173 if (iface
->l3_dev
.dev
)
174 device_release(&iface
->l3_dev
);
175 interface_data_flush(iface
);
179 mark_interface_down(struct interface
*iface
)
181 enum interface_state state
= iface
->state
;
183 iface
->state
= IFS_DOWN
;
185 interface_event(iface
, IFEV_DOWN
);
186 interface_ip_set_enabled(&iface
->config_ip
, false);
187 interface_ip_flush(&iface
->proto_ip
);
188 interface_flush_state(iface
);
189 system_flush_routes();
193 __interface_set_down(struct interface
*iface
, bool force
)
195 interface_clear_errors(iface
);
197 if (iface
->state
== IFS_DOWN
||
198 iface
->state
== IFS_TEARDOWN
)
201 if (iface
->state
== IFS_UP
)
202 interface_event(iface
, IFEV_DOWN
);
203 iface
->state
= IFS_TEARDOWN
;
204 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
206 interface_flush_state(iface
);
210 interface_cb(struct device_user
*dep
, enum device_event ev
)
212 struct interface
*iface
;
215 iface
= container_of(dep
, struct interface
, main_dev
);
220 case DEV_EVENT_REMOVE
:
227 interface_set_available(iface
, new_state
);
228 if (!new_state
&& dep
->dev
->external
)
229 interface_set_main_dev(iface
, NULL
);
233 interface_set_available(struct interface
*iface
, bool new_state
)
235 if (iface
->available
== new_state
)
238 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
239 iface
->available
= new_state
;
242 if (iface
->autostart
&& !config_init
)
243 interface_set_up(iface
);
245 __interface_set_down(iface
, true);
249 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
252 list_add(&dep
->list
, &iface_all_users
);
257 list_add(&dep
->list
, &iface
->users
);
258 if (iface
->state
== IFS_UP
)
259 dep
->cb(dep
, iface
, IFEV_UP
);
263 interface_remove_user(struct interface_user
*dep
)
265 list_del_init(&dep
->list
);
270 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
272 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
273 struct device
*dev
= iface
->l3_dev
.dev
;
280 interface_set_main_dev(alias
, dev
);
281 interface_set_available(alias
, true);
284 interface_set_available(alias
, false);
285 interface_set_main_dev(alias
, NULL
);
288 interface_remove_user(dep
);
296 interface_claim_device(struct interface
*iface
)
298 struct interface
*parent
;
299 struct device
*dev
= NULL
;
301 if (iface
->parent_iface
.iface
)
302 interface_remove_user(&iface
->parent_iface
);
304 if (iface
->parent_ifname
) {
305 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
306 iface
->parent_iface
.cb
= interface_alias_cb
;
307 interface_add_user(&iface
->parent_iface
, parent
);
308 } else if (iface
->ifname
&&
309 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
310 dev
= device_get(iface
->ifname
, true);
314 interface_set_main_dev(iface
, dev
);
316 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
317 interface_set_available(iface
, true);
322 interface_cleanup(struct interface
*iface
, bool reload
)
324 struct interface_user
*dep
, *tmp
;
326 if (iface
->parent_iface
.iface
)
327 interface_remove_user(&iface
->parent_iface
);
329 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
330 interface_remove_user(dep
);
332 interface_ip_flush(&iface
->config_ip
);
333 interface_flush_state(iface
);
334 interface_clear_errors(iface
);
336 if (iface
->main_dev
.dev
&& !reload
)
337 interface_set_main_dev(iface
, NULL
);
338 interface_set_proto_state(iface
, NULL
);
342 interface_do_free(struct interface
*iface
)
344 interface_event(iface
, IFEV_FREE
);
345 interface_cleanup(iface
, false);
347 netifd_ubus_remove_interface(iface
);
348 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
353 interface_do_reload(struct interface
*iface
)
355 interface_event(iface
, IFEV_RELOAD
);
356 interface_cleanup(iface
, true);
357 proto_init_interface(iface
, iface
->config
);
358 interface_claim_device(iface
);
362 interface_handle_config_change(struct interface
*iface
)
364 enum interface_config_state state
= iface
->config_state
;
366 iface
->config_state
= IFC_NORMAL
;
371 interface_do_reload(iface
);
374 interface_do_free(iface
);
377 if (iface
->autostart
&& iface
->available
)
378 interface_set_up(iface
);
382 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
384 struct interface
*iface
= state
->iface
;
388 if (iface
->state
!= IFS_SETUP
)
391 interface_ip_set_enabled(&iface
->config_ip
, true);
392 system_flush_routes();
393 iface
->state
= IFS_UP
;
394 iface
->start_time
= system_get_rtime();
395 interface_event(iface
, IFEV_UP
);
396 interface_write_resolv_conf();
397 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
400 if (iface
->state
== IFS_DOWN
)
403 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
404 mark_interface_down(iface
);
405 if (iface
->main_dev
.dev
)
406 device_release(&iface
->main_dev
);
407 interface_handle_config_change(iface
);
409 case IFPEV_LINK_LOST
:
410 if (iface
->state
!= IFS_UP
)
413 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
414 mark_interface_down(iface
);
415 iface
->state
= IFS_SETUP
;
420 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
423 iface
->proto
->free(iface
->proto
);
426 iface
->state
= IFS_DOWN
;
427 iface
->proto
= state
;
431 state
->proto_event
= interface_proto_cb
;
432 state
->iface
= iface
;
436 interface_init(struct interface
*iface
, const char *name
,
437 struct blob_attr
*config
)
439 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
440 struct blob_attr
*cur
;
441 const char *proto_name
= NULL
;
443 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
444 INIT_LIST_HEAD(&iface
->errors
);
445 INIT_LIST_HEAD(&iface
->users
);
446 INIT_LIST_HEAD(&iface
->hotplug_list
);
447 interface_ip_init(iface
);
448 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
449 iface
->config_ip
.enabled
= false;
451 iface
->main_dev
.cb
= interface_cb
;
453 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
454 blob_data(config
), blob_len(config
));
456 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
457 proto_name
= blobmsg_data(cur
);
459 proto_attach_interface(iface
, proto_name
);
461 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
462 iface
->proto_ip
.no_defaultroute
=
463 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
464 iface
->proto_ip
.no_dns
=
465 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
467 if ((cur
= tb
[IFACE_ATTR_DNS
]))
468 interface_add_dns_server_list(&iface
->config_ip
, cur
);
470 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
471 interface_add_dns_search_list(&iface
->config_ip
, cur
);
473 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
474 iface
->metric
= blobmsg_get_u32(cur
);
476 iface
->config_autostart
= iface
->autostart
;
479 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
481 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
482 struct blob_attr
*cur
;
484 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
485 blob_data(config
), blob_len(config
));
488 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
489 iface
->parent_ifname
= blobmsg_data(cur
);
491 if (!iface
->parent_ifname
)
494 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
495 iface
->ifname
= blobmsg_data(cur
);
499 iface
->config
= config
;
500 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
505 interface_add(struct interface
*iface
, struct blob_attr
*config
)
507 __interface_add(iface
, config
, false);
511 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
513 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
516 return __interface_add(iface
, config
, true);
520 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
522 bool enabled
= iface
->config_ip
.enabled
;
523 bool claimed
= iface
->l3_dev
.claimed
;
525 if (iface
->l3_dev
.dev
== dev
)
528 interface_ip_set_enabled(&iface
->config_ip
, false);
529 interface_ip_flush(&iface
->proto_ip
);
530 device_add_user(&iface
->l3_dev
, dev
);
534 device_claim(&iface
->l3_dev
);
535 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
540 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
542 bool set_l3
= (iface
->main_dev
.dev
== iface
->l3_dev
.dev
);
543 bool claimed
= iface
->l3_dev
.claimed
;
545 if (iface
->main_dev
.dev
== dev
)
549 interface_set_l3_dev(iface
, dev
);
551 device_add_user(&iface
->main_dev
, dev
);
553 device_claim(&iface
->l3_dev
);
555 if (!iface
->l3_dev
.dev
)
556 interface_set_l3_dev(iface
, dev
);
560 interface_remove_link(struct interface
*iface
, struct device
*dev
)
562 struct device
*mdev
= iface
->main_dev
.dev
;
564 if (mdev
&& mdev
->hotplug_ops
)
565 return mdev
->hotplug_ops
->del(mdev
, dev
);
567 if (!iface
->main_dev
.hotplug
)
568 return UBUS_STATUS_INVALID_ARGUMENT
;
570 if (dev
!= iface
->main_dev
.dev
)
571 return UBUS_STATUS_INVALID_ARGUMENT
;
573 device_remove_user(&iface
->main_dev
);
578 interface_add_link(struct interface
*iface
, struct device
*dev
)
580 struct device
*mdev
= iface
->main_dev
.dev
;
585 if (iface
->main_dev
.hotplug
)
586 device_remove_user(&iface
->main_dev
);
589 if (mdev
->hotplug_ops
)
590 return mdev
->hotplug_ops
->add(mdev
, dev
);
592 return UBUS_STATUS_NOT_SUPPORTED
;
595 interface_set_main_dev(iface
, dev
);
596 iface
->main_dev
.hotplug
= true;
601 interface_set_up(struct interface
*iface
)
605 iface
->autostart
= true;
607 if (iface
->state
!= IFS_DOWN
)
610 interface_clear_errors(iface
);
611 if (!iface
->available
) {
612 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
616 if (iface
->main_dev
.dev
) {
617 ret
= device_claim(&iface
->main_dev
);
622 iface
->state
= IFS_SETUP
;
623 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
625 mark_interface_down(iface
);
633 interface_set_down(struct interface
*iface
)
636 vlist_for_each_element(&interfaces
, iface
, node
)
637 __interface_set_down(iface
, false);
639 iface
->autostart
= false;
640 __interface_set_down(iface
, false);
647 interface_start_pending(void)
649 struct interface
*iface
;
651 vlist_for_each_element(&interfaces
, iface
, node
) {
652 if (iface
->available
&& iface
->autostart
)
653 interface_set_up(iface
);
658 set_config_state(struct interface
*iface
, enum interface_config_state s
)
660 iface
->config_state
= s
;
661 if (iface
->state
== IFS_DOWN
)
662 interface_handle_config_change(iface
);
664 __interface_set_down(iface
, false);
668 interface_update_start(struct interface
*iface
)
670 interface_ip_update_start(&iface
->proto_ip
);
674 interface_update_complete(struct interface
*iface
)
676 interface_ip_update_complete(&iface
->proto_ip
);
680 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
682 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
683 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
687 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
689 struct blob_attr
*old_config
= if_old
->config
;
690 const char *old_ifname
= if_old
->ifname
;
691 const char *old_parent_ifname
= if_old
->parent_ifname
;
692 const struct proto_handler
*proto
= if_old
->proto_handler
;
694 interface_clear_errors(if_old
);
695 if_old
->config
= if_new
->config
;
696 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
697 if_old
->autostart
= true;
699 if_old
->device_config
= if_new
->device_config
;
700 if_old
->config_autostart
= if_new
->config_autostart
;
701 if_old
->ifname
= if_new
->ifname
;
702 if_old
->parent_ifname
= if_new
->parent_ifname
;
703 if_old
->proto_handler
= if_new
->proto_handler
;
705 #define FIELD_CHANGED_STR(field) \
706 ((!!if_old->field != !!old_ ## field) || \
708 strcmp(old_ ## field, if_old->field) != 0))
710 if (FIELD_CHANGED_STR(parent_ifname
)) {
711 if (if_old
->parent_iface
.iface
)
712 interface_remove_user(&if_old
->parent_iface
);
716 if (FIELD_CHANGED_STR(ifname
) || proto
!= if_new
->proto_handler
) {
717 D(INTERFACE
, "Reload interface '%s' because of ifname/proto change\n",
722 if (!proto
->config_params
)
723 D(INTERFACE
, "No config parameters for interface '%s'\n",
725 else if (!config_check_equal(old_config
, if_new
->config
,
726 proto
->config_params
)) {
727 D(INTERFACE
, "Reload interface '%s because of config changes\n",
732 #define UPDATE(field) ({ \
733 bool __changed = (if_old->field != if_new->field); \
734 if_old->field = if_new->field; \
738 if (UPDATE(metric
) || UPDATE(proto_ip
.no_defaultroute
)) {
739 interface_ip_set_enabled(&if_old
->config_ip
, false);
740 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
741 interface_ip_set_enabled(&if_old
->proto_ip
, false);
742 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
745 UPDATE(proto_ip
.no_dns
);
746 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
747 interface_write_resolv_conf();
754 set_config_state(if_old
, IFC_RELOAD
);
756 if_new
->config
= NULL
;
757 interface_cleanup(if_new
, false);
763 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
764 struct vlist_node
*node_old
)
766 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
767 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
769 if (node_old
&& node_new
) {
770 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
771 interface_change_config(if_old
, if_new
);
772 } else if (node_old
) {
773 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
774 set_config_state(if_old
, IFC_REMOVE
);
775 } else if (node_new
) {
776 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
777 proto_init_interface(if_new
, if_new
->config
);
778 interface_claim_device(if_new
);
779 netifd_ubus_add_interface(if_new
);
785 interface_init_list(void)
787 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
788 interfaces
.keep_old
= true;
789 interfaces
.no_delete
= true;