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
,
42 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
43 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
44 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
45 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
46 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
47 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
48 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
49 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
50 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
53 static const union config_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
54 [IFACE_ATTR_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
57 const struct config_param_list interface_attr_list
= {
58 .n_params
= IFACE_ATTR_MAX
,
59 .params
= iface_attrs
,
60 .info
= iface_attr_info
,
64 interface_clear_errors(struct interface
*iface
)
66 struct interface_error
*error
, *tmp
;
68 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
69 list_del(&error
->list
);
74 void interface_add_error(struct interface
*iface
, const char *subsystem
,
75 const char *code
, const char **data
, int n_data
)
77 struct interface_error
*error
;
83 len
= n_data
* sizeof(char *);
84 datalen
= alloca(len
);
85 for (i
= 0; i
< n_data
; i
++) {
86 datalen
[i
] = strlen(data
[i
]) + 1;
91 error
= calloc(1, sizeof(*error
) + sizeof(char *) + len
);
95 list_add_tail(&error
->list
, &iface
->errors
);
96 error
->subsystem
= subsystem
;
99 dest
= (char *) &error
->data
[n_data
+ 1];
100 for (i
= 0; i
< n_data
; i
++) {
101 error
->data
[i
] = dest
;
102 memcpy(dest
, data
[i
], datalen
[i
]);
105 error
->data
[n_data
] = NULL
;
109 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
111 avl_delete(&iface
->data
, &data
->node
);
116 interface_data_flush(struct interface
*iface
)
118 struct interface_data
*d
, *tmp
;
120 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
121 interface_data_del(iface
, d
);
125 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
127 struct interface_data
*n
, *o
;
129 if (!blobmsg_check_attr(data
, true))
130 return UBUS_STATUS_INVALID_ARGUMENT
;
132 n
= calloc(1, sizeof(*n
) + blob_pad_len(data
));
133 memcpy(n
->data
, data
, blob_pad_len(data
));
134 n
->node
.key
= blobmsg_name(data
);
136 o
= avl_find_element(&iface
->data
, n
->node
.key
, o
, node
);
138 interface_data_del(iface
, o
);
140 avl_insert(&iface
->data
, &n
->node
);
145 interface_event(struct interface
*iface
, enum interface_event ev
)
147 struct interface_user
*dep
, *tmp
;
149 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
150 dep
->cb(dep
, iface
, ev
);
152 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
153 dep
->cb(dep
, iface
, ev
);
157 interface_flush_state(struct interface
*iface
)
159 if (iface
->main_dev
.dev
)
160 device_release(&iface
->main_dev
);
161 if (iface
->l3_dev
.dev
)
162 device_release(&iface
->l3_dev
);
163 interface_data_flush(iface
);
167 mark_interface_down(struct interface
*iface
)
169 if (iface
->state
== IFS_UP
)
170 interface_event(iface
, IFEV_DOWN
);
171 interface_ip_set_enabled(&iface
->config_ip
, false);
172 interface_ip_flush(&iface
->proto_ip
);
173 interface_flush_state(iface
);
174 system_flush_routes();
175 iface
->state
= IFS_DOWN
;
179 __interface_set_down(struct interface
*iface
, bool force
)
181 interface_clear_errors(iface
);
183 if (iface
->state
== IFS_DOWN
||
184 iface
->state
== IFS_TEARDOWN
)
187 if (iface
->state
== IFS_UP
)
188 interface_event(iface
, IFEV_DOWN
);
189 iface
->state
= IFS_TEARDOWN
;
190 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
192 interface_flush_state(iface
);
196 interface_cb(struct device_user
*dep
, enum device_event ev
)
198 struct interface
*iface
;
201 iface
= container_of(dep
, struct interface
, main_dev
);
206 case DEV_EVENT_REMOVE
:
213 interface_set_available(iface
, new_state
);
217 interface_set_available(struct interface
*iface
, bool new_state
)
219 if (iface
->available
== new_state
)
222 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
223 iface
->available
= new_state
;
226 if (iface
->autostart
&& !config_init
)
227 interface_set_up(iface
);
229 __interface_set_down(iface
, true);
233 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
236 list_add(&dep
->list
, &iface_all_users
);
241 list_add(&dep
->list
, &iface
->users
);
242 if (iface
->state
== IFS_UP
)
243 dep
->cb(dep
, iface
, IFEV_UP
);
247 interface_remove_user(struct interface_user
*dep
)
249 list_del_init(&dep
->list
);
254 interface_claim_device(struct interface
*iface
)
259 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
260 dev
= device_get(iface
->ifname
, true);
262 interface_set_main_dev(iface
, dev
);
264 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
265 interface_set_available(iface
, true);
270 interface_cleanup(struct interface
*iface
, bool reload
)
272 struct interface_user
*dep
, *tmp
;
274 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
275 interface_remove_user(dep
);
277 interface_ip_flush(&iface
->config_ip
);
278 interface_flush_state(iface
);
279 interface_clear_errors(iface
);
280 if (iface
->main_dev
.dev
&&
281 (!reload
|| !iface
->main_dev
.hotplug
))
282 interface_set_main_dev(iface
, NULL
);
283 interface_set_proto_state(iface
, NULL
);
287 interface_do_free(struct interface
*iface
)
289 interface_event(iface
, IFEV_FREE
);
290 interface_cleanup(iface
, false);
292 netifd_ubus_remove_interface(iface
);
293 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
298 interface_do_reload(struct interface
*iface
)
300 interface_event(iface
, IFEV_RELOAD
);
301 interface_cleanup(iface
, true);
302 proto_init_interface(iface
, iface
->config
);
303 interface_claim_device(iface
);
307 interface_handle_config_change(struct interface
*iface
)
309 switch(iface
->config_state
) {
313 interface_do_reload(iface
);
316 interface_do_free(iface
);
319 if (iface
->autostart
&& iface
->available
)
320 interface_set_up(iface
);
324 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
326 struct interface
*iface
= state
->iface
;
330 if (iface
->state
!= IFS_SETUP
)
333 interface_ip_set_enabled(&iface
->config_ip
, true);
334 system_flush_routes();
335 iface
->state
= IFS_UP
;
336 iface
->start_time
= system_get_rtime();
337 interface_event(iface
, IFEV_UP
);
338 interface_write_resolv_conf();
339 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
342 if (iface
->state
== IFS_DOWN
)
345 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
346 mark_interface_down(iface
);
347 interface_handle_config_change(iface
);
349 case IFPEV_LINK_LOST
:
350 if (iface
->state
!= IFS_UP
)
353 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
354 mark_interface_down(iface
);
355 iface
->state
= IFS_SETUP
;
360 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
363 iface
->proto
->free(iface
->proto
);
366 iface
->state
= IFS_DOWN
;
367 iface
->proto
= state
;
371 state
->proto_event
= interface_proto_cb
;
372 state
->iface
= iface
;
376 interface_init(struct interface
*iface
, const char *name
,
377 struct blob_attr
*config
)
379 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
380 struct blob_attr
*cur
;
381 const char *proto_name
= NULL
;
383 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
384 INIT_LIST_HEAD(&iface
->errors
);
385 INIT_LIST_HEAD(&iface
->users
);
386 INIT_LIST_HEAD(&iface
->hotplug_list
);
387 interface_ip_init(iface
);
388 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
389 iface
->config_ip
.enabled
= false;
391 iface
->main_dev
.cb
= interface_cb
;
393 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
394 blob_data(config
), blob_len(config
));
396 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
397 proto_name
= blobmsg_data(cur
);
399 proto_attach_interface(iface
, proto_name
);
401 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
402 iface
->proto_ip
.no_defaultroute
=
403 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
404 iface
->proto_ip
.no_dns
=
405 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
407 if ((cur
= tb
[IFACE_ATTR_DNS
]))
408 interface_add_dns_server_list(&iface
->config_ip
, cur
);
410 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
411 interface_add_dns_search_list(&iface
->config_ip
, cur
);
413 iface
->config_autostart
= iface
->autostart
;
417 interface_add(struct interface
*iface
, struct blob_attr
*config
)
419 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
420 struct blob_attr
*cur
;
422 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
423 blob_data(config
), blob_len(config
));
425 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
426 iface
->ifname
= blobmsg_data(cur
);
428 iface
->config
= config
;
429 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
433 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
435 bool enabled
= iface
->config_ip
.enabled
;
436 bool claimed
= iface
->l3_dev
.claimed
;
438 if (iface
->l3_dev
.dev
== dev
)
441 interface_ip_set_enabled(&iface
->config_ip
, false);
442 interface_ip_flush(&iface
->proto_ip
);
443 device_add_user(&iface
->l3_dev
, dev
);
447 device_claim(&iface
->l3_dev
);
448 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
453 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
455 bool set_l3
= (iface
->main_dev
.dev
== iface
->l3_dev
.dev
);
456 bool claimed
= iface
->l3_dev
.claimed
;
458 if (iface
->main_dev
.dev
== dev
)
462 interface_set_l3_dev(iface
, dev
);
464 device_add_user(&iface
->main_dev
, dev
);
466 device_claim(&iface
->l3_dev
);
468 if (!iface
->l3_dev
.dev
)
469 interface_set_l3_dev(iface
, dev
);
473 interface_remove_link(struct interface
*iface
, struct device
*dev
)
475 struct device
*mdev
= iface
->main_dev
.dev
;
477 if (mdev
&& mdev
->hotplug_ops
)
478 return mdev
->hotplug_ops
->del(mdev
, dev
);
480 if (!iface
->main_dev
.hotplug
)
481 return UBUS_STATUS_INVALID_ARGUMENT
;
483 if (dev
!= iface
->main_dev
.dev
)
484 return UBUS_STATUS_INVALID_ARGUMENT
;
486 device_remove_user(&iface
->main_dev
);
491 interface_add_link(struct interface
*iface
, struct device
*dev
)
493 struct device
*mdev
= iface
->main_dev
.dev
;
498 if (iface
->main_dev
.hotplug
)
499 device_remove_user(&iface
->main_dev
);
502 if (mdev
->hotplug_ops
)
503 return mdev
->hotplug_ops
->add(mdev
, dev
);
505 return UBUS_STATUS_NOT_SUPPORTED
;
508 interface_set_main_dev(iface
, dev
);
509 iface
->main_dev
.hotplug
= true;
514 interface_set_up(struct interface
*iface
)
518 iface
->autostart
= true;
520 if (iface
->state
!= IFS_DOWN
)
523 interface_clear_errors(iface
);
524 if (!iface
->available
) {
525 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
529 if (iface
->main_dev
.dev
) {
530 ret
= device_claim(&iface
->main_dev
);
535 iface
->state
= IFS_SETUP
;
536 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
538 mark_interface_down(iface
);
546 interface_set_down(struct interface
*iface
)
549 vlist_for_each_element(&interfaces
, iface
, node
)
550 __interface_set_down(iface
, false);
552 iface
->autostart
= false;
553 __interface_set_down(iface
, false);
560 interface_start_pending(void)
562 struct interface
*iface
;
564 vlist_for_each_element(&interfaces
, iface
, node
) {
565 if (iface
->available
&& iface
->autostart
)
566 interface_set_up(iface
);
571 set_config_state(struct interface
*iface
, enum interface_config_state s
)
573 iface
->config_state
= s
;
574 if (iface
->state
== IFS_DOWN
)
575 interface_handle_config_change(iface
);
577 __interface_set_down(iface
, false);
581 interface_update_start(struct interface
*iface
)
583 interface_ip_update_start(&iface
->proto_ip
);
587 interface_update_complete(struct interface
*iface
)
589 interface_ip_update_complete(&iface
->proto_ip
);
593 interface_replace_dns(struct interface_ip_settings
*old
, struct interface_ip_settings
*new)
595 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
596 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
600 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
602 struct blob_attr
*old_config
= if_old
->config
;
603 const char *old_ifname
= if_old
->ifname
;
604 const struct proto_handler
*proto
= if_old
->proto_handler
;
606 interface_clear_errors(if_old
);
607 if_old
->config
= if_new
->config
;
608 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
609 if_old
->autostart
= true;
611 if_old
->config_autostart
= if_new
->config_autostart
;
612 if_old
->ifname
= if_new
->ifname
;
613 if_old
->proto_handler
= if_new
->proto_handler
;
615 if ((!!old_ifname
!= !!if_new
->ifname
) ||
616 (old_ifname
&& strcmp(old_ifname
, if_new
->ifname
) != 0) ||
617 proto
!= if_new
->proto_handler
) {
618 D(INTERFACE
, "Reload interface '%s' because of ifname/proto change\n",
623 if (!proto
->config_params
)
624 D(INTERFACE
, "No config parameters for interface '%s'\n",
626 else if (!config_check_equal(old_config
, if_new
->config
,
627 proto
->config_params
)) {
628 D(INTERFACE
, "Reload interface '%s because of config changes\n",
633 #define UPDATE(field) ({ \
634 bool __changed = (if_old->field != if_new->field); \
635 if_old->field = if_new->field; \
639 if (UPDATE(metric
) || UPDATE(proto_ip
.no_defaultroute
)) {
640 interface_ip_set_enabled(&if_old
->config_ip
, false);
641 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
642 interface_ip_set_enabled(&if_old
->proto_ip
, false);
643 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
646 UPDATE(proto_ip
.no_dns
);
647 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
648 interface_replace_dns(&if_old
->proto_ip
, &if_new
->proto_ip
);
649 interface_write_resolv_conf();
656 set_config_state(if_old
, IFC_RELOAD
);
663 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
664 struct vlist_node
*node_old
)
666 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
667 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
669 if (node_old
&& node_new
) {
670 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
671 interface_change_config(if_old
, if_new
);
672 } else if (node_old
) {
673 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
674 set_config_state(if_old
, IFC_REMOVE
);
675 } else if (node_new
) {
676 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
677 proto_init_interface(if_new
, if_new
->config
);
678 interface_claim_device(if_new
);
679 netifd_ubus_add_interface(if_new
);
685 interface_init_list(void)
687 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
688 interfaces
.keep_old
= true;
689 interfaces
.no_delete
= true;