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
);
29 static unsigned int interface_serial
= 0;
35 IFACE_ATTR_DEFAULTROUTE
,
38 IFACE_ATTR_DNS_SEARCH
,
48 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
49 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
50 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
51 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
52 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
53 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
54 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
55 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
56 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
57 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
58 [IFACE_ATTR_IP6ASSIGN
] = { .name
= "ip6assign", .type
= BLOBMSG_TYPE_INT32
},
59 [IFACE_ATTR_IP6HINT
] = { .name
= "ip6hint", .type
= BLOBMSG_TYPE_STRING
},
60 [IFACE_ATTR_IP4TABLE
] = { .name
= "ip4table", .type
= BLOBMSG_TYPE_STRING
},
61 [IFACE_ATTR_IP6TABLE
] = { .name
= "ip6table", .type
= BLOBMSG_TYPE_STRING
},
64 static const union config_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
65 [IFACE_ATTR_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
68 const struct config_param_list interface_attr_list
= {
69 .n_params
= IFACE_ATTR_MAX
,
70 .params
= iface_attrs
,
71 .info
= iface_attr_info
,
75 interface_clear_errors(struct interface
*iface
)
77 struct interface_error
*error
, *tmp
;
79 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
80 list_del(&error
->list
);
85 void interface_add_error(struct interface
*iface
, const char *subsystem
,
86 const char *code
, const char **data
, int n_data
)
88 struct interface_error
*error
;
91 char *dest
, *d_subsys
, *d_code
;
94 len
= n_data
* sizeof(char *);
95 datalen
= alloca(len
);
96 for (i
= 0; i
< n_data
; i
++) {
97 datalen
[i
] = strlen(data
[i
]) + 1;
102 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
103 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
104 &d_code
, code
? strlen(code
) + 1 : 0);
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
;
119 error
->subsystem
= strcpy(d_subsys
, subsystem
);
122 error
->code
= strcpy(d_code
, code
);
126 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
128 avl_delete(&iface
->data
, &data
->node
);
133 interface_data_flush(struct interface
*iface
)
135 struct interface_data
*d
, *tmp
;
137 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
138 interface_data_del(iface
, d
);
142 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
144 struct interface_data
*n
, *o
;
146 if (!blobmsg_check_attr(data
, true))
147 return UBUS_STATUS_INVALID_ARGUMENT
;
149 n
= calloc(1, sizeof(*n
) + blob_pad_len(data
));
150 memcpy(n
->data
, data
, blob_pad_len(data
));
151 n
->node
.key
= blobmsg_name(data
);
153 o
= avl_find_element(&iface
->data
, n
->node
.key
, o
, node
);
155 interface_data_del(iface
, o
);
157 avl_insert(&iface
->data
, &n
->node
);
162 interface_event(struct interface
*iface
, enum interface_event ev
)
164 struct interface_user
*dep
, *tmp
;
165 struct device
*adev
= NULL
;
167 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
168 dep
->cb(dep
, iface
, ev
);
170 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
171 dep
->cb(dep
, iface
, ev
);
175 adev
= iface
->l3_dev
.dev
;
178 alias_notify_device(iface
->name
, adev
);
186 interface_flush_state(struct interface
*iface
)
188 if (iface
->l3_dev
.dev
)
189 device_release(&iface
->l3_dev
);
190 interface_data_flush(iface
);
194 mark_interface_down(struct interface
*iface
)
196 enum interface_state state
= iface
->state
;
198 iface
->state
= IFS_DOWN
;
200 interface_event(iface
, IFEV_DOWN
);
201 interface_ip_set_enabled(&iface
->config_ip
, false);
202 interface_ip_flush(&iface
->proto_ip
);
203 interface_flush_state(iface
);
204 system_flush_routes();
208 __interface_set_down(struct interface
*iface
, bool force
)
210 if (iface
->state
== IFS_DOWN
||
211 iface
->state
== IFS_TEARDOWN
)
214 if (iface
->state
== IFS_UP
)
215 interface_event(iface
, IFEV_DOWN
);
216 iface
->state
= IFS_TEARDOWN
;
217 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
219 interface_flush_state(iface
);
223 interface_cb(struct device_user
*dep
, enum device_event ev
)
225 struct interface
*iface
;
228 iface
= container_of(dep
, struct interface
, main_dev
);
233 case DEV_EVENT_REMOVE
:
240 interface_set_available(iface
, new_state
);
241 if (!new_state
&& dep
->dev
->external
)
242 interface_set_main_dev(iface
, NULL
);
246 interface_set_available(struct interface
*iface
, bool new_state
)
248 if (iface
->available
== new_state
)
251 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
252 iface
->available
= new_state
;
255 if (iface
->autostart
&& !config_init
)
256 interface_set_up(iface
);
258 __interface_set_down(iface
, true);
262 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
265 list_add(&dep
->list
, &iface_all_users
);
270 list_add(&dep
->list
, &iface
->users
);
271 if (iface
->state
== IFS_UP
)
272 dep
->cb(dep
, iface
, IFEV_UP
);
276 interface_remove_user(struct interface_user
*dep
)
278 list_del_init(&dep
->list
);
283 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
285 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
286 struct device
*dev
= iface
->l3_dev
.dev
;
293 interface_set_main_dev(alias
, dev
);
294 interface_set_available(alias
, true);
297 interface_set_available(alias
, false);
298 interface_set_main_dev(alias
, NULL
);
301 interface_remove_user(dep
);
309 interface_claim_device(struct interface
*iface
)
311 struct interface
*parent
;
312 struct device
*dev
= NULL
;
314 if (iface
->parent_iface
.iface
)
315 interface_remove_user(&iface
->parent_iface
);
317 if (iface
->parent_ifname
) {
318 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
319 iface
->parent_iface
.cb
= interface_alias_cb
;
320 interface_add_user(&iface
->parent_iface
, parent
);
321 } else if (iface
->ifname
&&
322 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
323 dev
= device_get(iface
->ifname
, true);
327 interface_set_main_dev(iface
, dev
);
329 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
330 interface_set_available(iface
, true);
334 interface_cleanup_state(struct interface
*iface
)
336 interface_set_available(iface
, false);
338 interface_flush_state(iface
);
339 interface_clear_errors(iface
);
340 interface_set_proto_state(iface
, NULL
);
342 if (iface
->main_dev
.dev
)
343 interface_set_main_dev(iface
, NULL
);
347 interface_cleanup(struct interface
*iface
)
349 struct interface_user
*dep
, *tmp
;
351 if (iface
->parent_iface
.iface
)
352 interface_remove_user(&iface
->parent_iface
);
354 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
355 interface_remove_user(dep
);
357 interface_ip_flush(&iface
->config_ip
);
358 interface_cleanup_state(iface
);
362 interface_do_free(struct interface
*iface
)
364 interface_event(iface
, IFEV_FREE
);
365 interface_cleanup(iface
);
367 netifd_ubus_remove_interface(iface
);
368 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
373 interface_do_reload(struct interface
*iface
)
375 interface_event(iface
, IFEV_RELOAD
);
376 interface_cleanup_state(iface
);
377 proto_init_interface(iface
, iface
->config
);
378 interface_claim_device(iface
);
382 interface_handle_config_change(struct interface
*iface
)
384 enum interface_config_state state
= iface
->config_state
;
386 iface
->config_state
= IFC_NORMAL
;
391 interface_do_reload(iface
);
394 interface_do_free(iface
);
397 if (iface
->autostart
&& iface
->available
)
398 interface_set_up(iface
);
402 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
404 struct interface
*iface
= state
->iface
;
408 if (iface
->state
!= IFS_SETUP
)
411 interface_ip_set_enabled(&iface
->config_ip
, true);
412 system_flush_routes();
413 iface
->state
= IFS_UP
;
414 iface
->start_time
= system_get_rtime();
415 interface_event(iface
, IFEV_UP
);
416 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
419 if (iface
->state
== IFS_DOWN
)
422 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
423 mark_interface_down(iface
);
424 if (iface
->main_dev
.dev
)
425 device_release(&iface
->main_dev
);
426 interface_handle_config_change(iface
);
428 case IFPEV_LINK_LOST
:
429 if (iface
->state
!= IFS_UP
)
432 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
433 mark_interface_down(iface
);
434 iface
->state
= IFS_SETUP
;
438 interface_write_resolv_conf();
441 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
444 iface
->proto
->free(iface
->proto
);
447 iface
->state
= IFS_DOWN
;
448 iface
->proto
= state
;
452 state
->proto_event
= interface_proto_cb
;
453 state
->iface
= iface
;
457 interface_init(struct interface
*iface
, const char *name
,
458 struct blob_attr
*config
)
460 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
461 struct blob_attr
*cur
;
462 const char *proto_name
= NULL
;
464 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
465 INIT_LIST_HEAD(&iface
->errors
);
466 INIT_LIST_HEAD(&iface
->users
);
467 INIT_LIST_HEAD(&iface
->hotplug_list
);
468 interface_ip_init(iface
);
469 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
470 iface
->config_ip
.enabled
= false;
472 iface
->main_dev
.cb
= interface_cb
;
474 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
475 blob_data(config
), blob_len(config
));
477 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
478 proto_name
= blobmsg_data(cur
);
480 proto_attach_interface(iface
, proto_name
);
482 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
483 iface
->proto_ip
.no_defaultroute
=
484 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
485 iface
->proto_ip
.no_dns
=
486 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
488 if ((cur
= tb
[IFACE_ATTR_DNS
]))
489 interface_add_dns_server_list(&iface
->config_ip
, cur
);
491 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
492 interface_add_dns_search_list(&iface
->config_ip
, cur
);
494 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
495 iface
->metric
= blobmsg_get_u32(cur
);
497 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
498 iface
->config_ip
.assignment_length
= blobmsg_get_u32(cur
);
500 iface
->config_ip
.assignment_hint
= -1;
501 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
502 iface
->config_ip
.assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
503 ~((1 << (64 - iface
->config_ip
.assignment_length
)) - 1);
505 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
506 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
507 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
510 // Set a default exteranl routing table for IPv6 to do source-based-filtering
511 iface
->ip6table
= 1000 + ++interface_serial
;
512 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
513 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
514 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
517 iface
->config_autostart
= iface
->autostart
;
520 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
522 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
523 struct blob_attr
*cur
;
525 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
526 blob_data(config
), blob_len(config
));
529 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
530 iface
->parent_ifname
= blobmsg_data(cur
);
532 if (!iface
->parent_ifname
)
535 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
536 iface
->ifname
= blobmsg_data(cur
);
540 iface
->config
= config
;
541 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
546 interface_add(struct interface
*iface
, struct blob_attr
*config
)
548 __interface_add(iface
, config
, false);
552 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
554 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
557 return __interface_add(iface
, config
, true);
561 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
563 bool enabled
= iface
->config_ip
.enabled
;
564 bool claimed
= iface
->l3_dev
.claimed
;
566 if (iface
->l3_dev
.dev
== dev
)
569 interface_ip_set_enabled(&iface
->config_ip
, false);
570 interface_ip_flush(&iface
->proto_ip
);
571 device_add_user(&iface
->l3_dev
, dev
);
575 device_claim(&iface
->l3_dev
);
576 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
581 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
583 bool set_l3
= (iface
->main_dev
.dev
== iface
->l3_dev
.dev
);
584 bool claimed
= iface
->l3_dev
.claimed
;
586 if (iface
->main_dev
.dev
== dev
)
590 interface_set_l3_dev(iface
, dev
);
592 device_add_user(&iface
->main_dev
, dev
);
594 device_claim(&iface
->l3_dev
);
596 if (!iface
->l3_dev
.dev
)
597 interface_set_l3_dev(iface
, dev
);
601 interface_remove_link(struct interface
*iface
, struct device
*dev
)
603 struct device
*mdev
= iface
->main_dev
.dev
;
605 if (mdev
&& mdev
->hotplug_ops
)
606 return mdev
->hotplug_ops
->del(mdev
, dev
);
608 if (!iface
->main_dev
.hotplug
)
609 return UBUS_STATUS_INVALID_ARGUMENT
;
611 if (dev
!= iface
->main_dev
.dev
)
612 return UBUS_STATUS_INVALID_ARGUMENT
;
614 device_remove_user(&iface
->main_dev
);
619 interface_add_link(struct interface
*iface
, struct device
*dev
)
621 struct device
*mdev
= iface
->main_dev
.dev
;
626 if (iface
->main_dev
.hotplug
)
627 device_remove_user(&iface
->main_dev
);
630 if (mdev
->hotplug_ops
)
631 return mdev
->hotplug_ops
->add(mdev
, dev
);
633 return UBUS_STATUS_NOT_SUPPORTED
;
636 interface_set_main_dev(iface
, dev
);
637 iface
->main_dev
.hotplug
= true;
642 interface_set_up(struct interface
*iface
)
646 iface
->autostart
= true;
648 if (iface
->state
!= IFS_DOWN
)
651 interface_clear_errors(iface
);
652 if (!iface
->available
) {
653 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
657 if (iface
->main_dev
.dev
) {
658 ret
= device_claim(&iface
->main_dev
);
663 iface
->state
= IFS_SETUP
;
664 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
666 mark_interface_down(iface
);
674 interface_set_down(struct interface
*iface
)
677 vlist_for_each_element(&interfaces
, iface
, node
)
678 __interface_set_down(iface
, false);
680 iface
->autostart
= false;
681 __interface_set_down(iface
, false);
688 interface_start_pending(void)
690 struct interface
*iface
;
692 vlist_for_each_element(&interfaces
, iface
, node
) {
693 if (iface
->available
&& iface
->autostart
)
694 interface_set_up(iface
);
699 set_config_state(struct interface
*iface
, enum interface_config_state s
)
701 iface
->config_state
= s
;
702 if (iface
->state
== IFS_DOWN
)
703 interface_handle_config_change(iface
);
705 __interface_set_down(iface
, false);
709 interface_update_start(struct interface
*iface
)
711 interface_ip_update_start(&iface
->proto_ip
);
715 interface_update_complete(struct interface
*iface
)
717 interface_ip_update_complete(&iface
->proto_ip
);
721 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
723 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
724 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
728 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
730 struct blob_attr
*old_config
= if_old
->config
;
731 bool reload
= false, reload_ip
= false, reload_assignment
= false;
733 #define FIELD_CHANGED_STR(field) \
734 ((!!if_old->field != !!if_new->field) || \
736 strcmp(if_old->field, if_new->field) != 0))
738 if (FIELD_CHANGED_STR(parent_ifname
)) {
739 if (if_old
->parent_iface
.iface
)
740 interface_remove_user(&if_old
->parent_iface
);
744 if (FIELD_CHANGED_STR(ifname
) ||
745 if_old
->proto_handler
!= if_new
->proto_handler
)
748 if (!if_old
->proto_handler
->config_params
)
749 D(INTERFACE
, "No config parameters for interface '%s'\n",
751 else if (!config_check_equal(if_old
->config
, if_new
->config
,
752 if_old
->proto_handler
->config_params
))
755 #define UPDATE(field, __var) ({ \
756 bool __changed = (if_old->field != if_new->field); \
757 if_old->field = if_new->field; \
758 __var |= __changed; \
761 if_old
->config
= if_new
->config
;
762 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
763 if_old
->autostart
= true;
765 if_old
->device_config
= if_new
->device_config
;
766 if_old
->config_autostart
= if_new
->config_autostart
;
767 if_old
->ifname
= if_new
->ifname
;
768 if_old
->parent_ifname
= if_new
->parent_ifname
;
769 if_old
->proto_handler
= if_new
->proto_handler
;
771 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
772 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
774 UPDATE(metric
, reload_ip
);
775 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
776 UPDATE(config_ip
.assignment_length
, reload_assignment
);
777 UPDATE(config_ip
.assignment_hint
, reload_assignment
);
782 D(INTERFACE
, "Reload interface '%s because of config changes\n",
784 interface_clear_errors(if_old
);
785 set_config_state(if_old
, IFC_RELOAD
);
790 interface_ip_set_enabled(&if_old
->config_ip
, false);
791 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
792 interface_ip_set_enabled(&if_old
->proto_ip
, false);
793 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
796 if (reload_assignment
)
797 interface_refresh_assignments(true);
799 interface_write_resolv_conf();
802 if_new
->config
= NULL
;
803 interface_cleanup(if_new
);
809 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
810 struct vlist_node
*node_old
)
812 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
813 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
815 if (node_old
&& node_new
) {
816 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
817 interface_change_config(if_old
, if_new
);
818 } else if (node_old
) {
819 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
820 set_config_state(if_old
, IFC_REMOVE
);
821 } else if (node_new
) {
822 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
823 proto_init_interface(if_new
, if_new
->config
);
824 interface_claim_device(if_new
);
825 netifd_ubus_add_interface(if_new
);
831 interface_init_list(void)
833 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
834 interfaces
.keep_old
= true;
835 interfaces
.no_delete
= true;