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
;
83 int subsystem_len
= 0, code_len
= 0;
86 len
= n_data
* sizeof(char *);
87 datalen
= alloca(len
);
88 for (i
= 0; i
< n_data
; i
++) {
89 datalen
[i
] = strlen(data
[i
]) + 1;
95 subsystem_len
= strlen(subsystem
) + 1;
100 code_len
= strlen(code
) + 1;
104 error
= calloc(1, sizeof(*error
) + sizeof(char *) + len
);
108 list_add_tail(&error
->list
, &iface
->errors
);
110 dest
= (char *) &error
->data
[n_data
+ 1];
111 for (i
= 0; i
< n_data
; i
++) {
112 error
->data
[i
] = dest
;
113 memcpy(dest
, data
[i
], datalen
[i
]);
116 error
->data
[n_data
++] = NULL
;
118 dest
= (char *) &error
->data
[n_data
];
120 error
->subsystem
= dest
;
121 strcpy(dest
, subsystem
);
122 dest
+= subsystem_len
;
133 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
135 avl_delete(&iface
->data
, &data
->node
);
140 interface_data_flush(struct interface
*iface
)
142 struct interface_data
*d
, *tmp
;
144 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
145 interface_data_del(iface
, d
);
149 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
151 struct interface_data
*n
, *o
;
153 if (!blobmsg_check_attr(data
, true))
154 return UBUS_STATUS_INVALID_ARGUMENT
;
156 n
= calloc(1, sizeof(*n
) + blob_pad_len(data
));
157 memcpy(n
->data
, data
, blob_pad_len(data
));
158 n
->node
.key
= blobmsg_name(data
);
160 o
= avl_find_element(&iface
->data
, n
->node
.key
, o
, node
);
162 interface_data_del(iface
, o
);
164 avl_insert(&iface
->data
, &n
->node
);
169 interface_event(struct interface
*iface
, enum interface_event ev
)
171 struct interface_user
*dep
, *tmp
;
172 struct device
*adev
= NULL
;
174 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
175 dep
->cb(dep
, iface
, ev
);
177 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
178 dep
->cb(dep
, iface
, ev
);
182 adev
= iface
->main_dev
.dev
;
185 alias_notify_device(iface
->name
, adev
);
193 interface_flush_state(struct interface
*iface
)
195 if (iface
->l3_dev
.dev
)
196 device_release(&iface
->l3_dev
);
197 interface_data_flush(iface
);
201 mark_interface_down(struct interface
*iface
)
203 enum interface_state state
= iface
->state
;
205 iface
->state
= IFS_DOWN
;
207 interface_event(iface
, IFEV_DOWN
);
208 interface_ip_set_enabled(&iface
->config_ip
, false);
209 interface_ip_flush(&iface
->proto_ip
);
210 interface_flush_state(iface
);
211 system_flush_routes();
215 __interface_set_down(struct interface
*iface
, bool force
)
217 interface_clear_errors(iface
);
219 if (iface
->state
== IFS_DOWN
||
220 iface
->state
== IFS_TEARDOWN
)
223 if (iface
->state
== IFS_UP
)
224 interface_event(iface
, IFEV_DOWN
);
225 iface
->state
= IFS_TEARDOWN
;
226 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
228 interface_flush_state(iface
);
232 interface_cb(struct device_user
*dep
, enum device_event ev
)
234 struct interface
*iface
;
237 iface
= container_of(dep
, struct interface
, main_dev
);
242 case DEV_EVENT_REMOVE
:
249 interface_set_available(iface
, new_state
);
250 if (!new_state
&& dep
->dev
->external
)
251 interface_set_main_dev(iface
, NULL
);
255 interface_set_available(struct interface
*iface
, bool new_state
)
257 if (iface
->available
== new_state
)
260 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
261 iface
->available
= new_state
;
264 if (iface
->autostart
&& !config_init
)
265 interface_set_up(iface
);
267 __interface_set_down(iface
, true);
271 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
274 list_add(&dep
->list
, &iface_all_users
);
279 list_add(&dep
->list
, &iface
->users
);
280 if (iface
->state
== IFS_UP
)
281 dep
->cb(dep
, iface
, IFEV_UP
);
285 interface_remove_user(struct interface_user
*dep
)
287 list_del_init(&dep
->list
);
292 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
294 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
295 struct device
*dev
= iface
->l3_dev
.dev
;
302 interface_set_main_dev(alias
, dev
);
303 interface_set_available(alias
, true);
306 interface_set_available(alias
, false);
307 interface_set_main_dev(alias
, NULL
);
310 interface_remove_user(dep
);
318 interface_claim_device(struct interface
*iface
)
320 struct interface
*parent
;
321 struct device
*dev
= NULL
;
323 if (iface
->parent_iface
.iface
)
324 interface_remove_user(&iface
->parent_iface
);
326 if (iface
->parent_ifname
) {
327 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
328 iface
->parent_iface
.cb
= interface_alias_cb
;
329 interface_add_user(&iface
->parent_iface
, parent
);
330 } else if (iface
->ifname
&&
331 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
332 dev
= device_get(iface
->ifname
, true);
336 interface_set_main_dev(iface
, dev
);
338 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
339 interface_set_available(iface
, true);
344 interface_cleanup(struct interface
*iface
, bool reload
)
346 struct interface_user
*dep
, *tmp
;
348 if (iface
->parent_iface
.iface
)
349 interface_remove_user(&iface
->parent_iface
);
351 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
352 interface_remove_user(dep
);
354 interface_ip_flush(&iface
->config_ip
);
355 interface_flush_state(iface
);
356 interface_clear_errors(iface
);
358 if (iface
->main_dev
.dev
&& !reload
)
359 interface_set_main_dev(iface
, NULL
);
360 interface_set_proto_state(iface
, NULL
);
364 interface_do_free(struct interface
*iface
)
366 interface_event(iface
, IFEV_FREE
);
367 interface_cleanup(iface
, false);
369 netifd_ubus_remove_interface(iface
);
370 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
375 interface_do_reload(struct interface
*iface
)
377 interface_event(iface
, IFEV_RELOAD
);
378 interface_cleanup(iface
, true);
379 proto_init_interface(iface
, iface
->config
);
380 interface_claim_device(iface
);
384 interface_handle_config_change(struct interface
*iface
)
386 enum interface_config_state state
= iface
->config_state
;
388 iface
->config_state
= IFC_NORMAL
;
393 interface_do_reload(iface
);
396 interface_do_free(iface
);
399 if (iface
->autostart
&& iface
->available
)
400 interface_set_up(iface
);
404 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
406 struct interface
*iface
= state
->iface
;
410 if (iface
->state
!= IFS_SETUP
)
413 interface_ip_set_enabled(&iface
->config_ip
, true);
414 system_flush_routes();
415 iface
->state
= IFS_UP
;
416 iface
->start_time
= system_get_rtime();
417 interface_event(iface
, IFEV_UP
);
418 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
421 if (iface
->state
== IFS_DOWN
)
424 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
425 mark_interface_down(iface
);
426 if (iface
->main_dev
.dev
)
427 device_release(&iface
->main_dev
);
428 interface_handle_config_change(iface
);
430 case IFPEV_LINK_LOST
:
431 if (iface
->state
!= IFS_UP
)
434 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
435 mark_interface_down(iface
);
436 iface
->state
= IFS_SETUP
;
440 interface_write_resolv_conf();
443 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
446 iface
->proto
->free(iface
->proto
);
449 iface
->state
= IFS_DOWN
;
450 iface
->proto
= state
;
454 state
->proto_event
= interface_proto_cb
;
455 state
->iface
= iface
;
459 interface_init(struct interface
*iface
, const char *name
,
460 struct blob_attr
*config
)
462 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
463 struct blob_attr
*cur
;
464 const char *proto_name
= NULL
;
466 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
467 INIT_LIST_HEAD(&iface
->errors
);
468 INIT_LIST_HEAD(&iface
->users
);
469 INIT_LIST_HEAD(&iface
->hotplug_list
);
470 interface_ip_init(iface
);
471 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
472 iface
->config_ip
.enabled
= false;
474 iface
->main_dev
.cb
= interface_cb
;
476 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
477 blob_data(config
), blob_len(config
));
479 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
480 proto_name
= blobmsg_data(cur
);
482 proto_attach_interface(iface
, proto_name
);
484 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
485 iface
->proto_ip
.no_defaultroute
=
486 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
487 iface
->proto_ip
.no_dns
=
488 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
490 if ((cur
= tb
[IFACE_ATTR_DNS
]))
491 interface_add_dns_server_list(&iface
->config_ip
, cur
);
493 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
494 interface_add_dns_search_list(&iface
->config_ip
, cur
);
496 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
497 iface
->metric
= blobmsg_get_u32(cur
);
499 iface
->config_autostart
= iface
->autostart
;
502 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
504 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
505 struct blob_attr
*cur
;
507 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
508 blob_data(config
), blob_len(config
));
511 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
512 iface
->parent_ifname
= blobmsg_data(cur
);
514 if (!iface
->parent_ifname
)
517 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
518 iface
->ifname
= blobmsg_data(cur
);
522 iface
->config
= config
;
523 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
528 interface_add(struct interface
*iface
, struct blob_attr
*config
)
530 __interface_add(iface
, config
, false);
534 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
536 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
539 return __interface_add(iface
, config
, true);
543 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
545 bool enabled
= iface
->config_ip
.enabled
;
546 bool claimed
= iface
->l3_dev
.claimed
;
548 if (iface
->l3_dev
.dev
== dev
)
551 interface_ip_set_enabled(&iface
->config_ip
, false);
552 interface_ip_flush(&iface
->proto_ip
);
553 device_add_user(&iface
->l3_dev
, dev
);
557 device_claim(&iface
->l3_dev
);
558 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
563 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
565 bool set_l3
= (iface
->main_dev
.dev
== iface
->l3_dev
.dev
);
566 bool claimed
= iface
->l3_dev
.claimed
;
568 if (iface
->main_dev
.dev
== dev
)
572 interface_set_l3_dev(iface
, dev
);
574 device_add_user(&iface
->main_dev
, dev
);
576 device_claim(&iface
->l3_dev
);
578 if (!iface
->l3_dev
.dev
)
579 interface_set_l3_dev(iface
, dev
);
583 interface_remove_link(struct interface
*iface
, struct device
*dev
)
585 struct device
*mdev
= iface
->main_dev
.dev
;
587 if (mdev
&& mdev
->hotplug_ops
)
588 return mdev
->hotplug_ops
->del(mdev
, dev
);
590 if (!iface
->main_dev
.hotplug
)
591 return UBUS_STATUS_INVALID_ARGUMENT
;
593 if (dev
!= iface
->main_dev
.dev
)
594 return UBUS_STATUS_INVALID_ARGUMENT
;
596 device_remove_user(&iface
->main_dev
);
601 interface_add_link(struct interface
*iface
, struct device
*dev
)
603 struct device
*mdev
= iface
->main_dev
.dev
;
608 if (iface
->main_dev
.hotplug
)
609 device_remove_user(&iface
->main_dev
);
612 if (mdev
->hotplug_ops
)
613 return mdev
->hotplug_ops
->add(mdev
, dev
);
615 return UBUS_STATUS_NOT_SUPPORTED
;
618 interface_set_main_dev(iface
, dev
);
619 iface
->main_dev
.hotplug
= true;
624 interface_set_up(struct interface
*iface
)
628 iface
->autostart
= true;
630 if (iface
->state
!= IFS_DOWN
)
633 interface_clear_errors(iface
);
634 if (!iface
->available
) {
635 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
639 if (iface
->main_dev
.dev
) {
640 ret
= device_claim(&iface
->main_dev
);
645 iface
->state
= IFS_SETUP
;
646 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
648 mark_interface_down(iface
);
656 interface_set_down(struct interface
*iface
)
659 vlist_for_each_element(&interfaces
, iface
, node
)
660 __interface_set_down(iface
, false);
662 iface
->autostart
= false;
663 __interface_set_down(iface
, false);
670 interface_start_pending(void)
672 struct interface
*iface
;
674 vlist_for_each_element(&interfaces
, iface
, node
) {
675 if (iface
->available
&& iface
->autostart
)
676 interface_set_up(iface
);
681 set_config_state(struct interface
*iface
, enum interface_config_state s
)
683 iface
->config_state
= s
;
684 if (iface
->state
== IFS_DOWN
)
685 interface_handle_config_change(iface
);
687 __interface_set_down(iface
, false);
691 interface_update_start(struct interface
*iface
)
693 interface_ip_update_start(&iface
->proto_ip
);
697 interface_update_complete(struct interface
*iface
)
699 interface_ip_update_complete(&iface
->proto_ip
);
703 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
705 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
706 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
710 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
712 struct blob_attr
*old_config
= if_old
->config
;
713 const char *old_ifname
= if_old
->ifname
;
714 const char *old_parent_ifname
= if_old
->parent_ifname
;
715 const struct proto_handler
*proto
= if_old
->proto_handler
;
717 interface_clear_errors(if_old
);
718 if_old
->config
= if_new
->config
;
719 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
720 if_old
->autostart
= true;
722 if_old
->device_config
= if_new
->device_config
;
723 if_old
->config_autostart
= if_new
->config_autostart
;
724 if_old
->ifname
= if_new
->ifname
;
725 if_old
->parent_ifname
= if_new
->parent_ifname
;
726 if_old
->proto_handler
= if_new
->proto_handler
;
728 #define FIELD_CHANGED_STR(field) \
729 ((!!if_old->field != !!old_ ## field) || \
731 strcmp(old_ ## field, if_old->field) != 0))
733 if (FIELD_CHANGED_STR(parent_ifname
)) {
734 if (if_old
->parent_iface
.iface
)
735 interface_remove_user(&if_old
->parent_iface
);
739 if (FIELD_CHANGED_STR(ifname
) || proto
!= if_new
->proto_handler
) {
740 D(INTERFACE
, "Reload interface '%s' because of ifname/proto change\n",
745 if (!proto
->config_params
)
746 D(INTERFACE
, "No config parameters for interface '%s'\n",
748 else if (!config_check_equal(old_config
, if_new
->config
,
749 proto
->config_params
)) {
750 D(INTERFACE
, "Reload interface '%s because of config changes\n",
755 #define UPDATE(field) ({ \
756 bool __changed = (if_old->field != if_new->field); \
757 if_old->field = if_new->field; \
761 if (UPDATE(metric
) || UPDATE(proto_ip
.no_defaultroute
)) {
762 interface_ip_set_enabled(&if_old
->config_ip
, false);
763 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
764 interface_ip_set_enabled(&if_old
->proto_ip
, false);
765 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
768 UPDATE(proto_ip
.no_dns
);
769 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
770 interface_write_resolv_conf();
777 set_config_state(if_old
, IFC_RELOAD
);
779 if_new
->config
= NULL
;
780 interface_cleanup(if_new
, false);
786 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
787 struct vlist_node
*node_old
)
789 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
790 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
792 if (node_old
&& node_new
) {
793 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
794 interface_change_config(if_old
, if_new
);
795 } else if (node_old
) {
796 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
797 set_config_state(if_old
, IFC_REMOVE
);
798 } else if (node_new
) {
799 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
800 proto_init_interface(if_new
, if_new
->config
);
801 interface_claim_device(if_new
);
802 netifd_ubus_add_interface(if_new
);
808 interface_init_list(void)
810 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
811 interfaces
.keep_old
= true;
812 interfaces
.no_delete
= true;