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
,
40 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
41 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
42 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
43 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
44 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
45 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
46 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
49 const struct config_param_list interface_attr_list
= {
50 .n_params
= IFACE_ATTR_MAX
,
51 .params
= iface_attrs
,
55 interface_clear_errors(struct interface
*iface
)
57 struct interface_error
*error
, *tmp
;
59 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
60 list_del(&error
->list
);
65 void interface_add_error(struct interface
*iface
, const char *subsystem
,
66 const char *code
, const char **data
, int n_data
)
68 struct interface_error
*error
;
74 len
= n_data
* sizeof(char *);
75 datalen
= alloca(len
);
76 for (i
= 0; i
< n_data
; i
++) {
77 datalen
[i
] = strlen(data
[i
]) + 1;
82 error
= calloc(1, sizeof(*error
) + sizeof(char *) + len
);
86 list_add_tail(&error
->list
, &iface
->errors
);
87 error
->subsystem
= subsystem
;
90 dest
= (char *) &error
->data
[n_data
+ 1];
91 for (i
= 0; i
< n_data
; i
++) {
92 error
->data
[i
] = dest
;
93 memcpy(dest
, data
[i
], datalen
[i
]);
96 error
->data
[n_data
] = NULL
;
100 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
102 avl_delete(&iface
->data
, &data
->node
);
107 interface_data_flush(struct interface
*iface
)
109 struct interface_data
*d
, *tmp
;
111 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
112 interface_data_del(iface
, d
);
116 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
118 struct interface_data
*n
, *o
;
120 if (!blobmsg_check_attr(data
, true))
121 return UBUS_STATUS_INVALID_ARGUMENT
;
123 n
= calloc(1, sizeof(*n
) + blob_pad_len(data
));
124 memcpy(n
->data
, data
, blob_pad_len(data
));
125 n
->node
.key
= blobmsg_name(data
);
127 o
= avl_find_element(&iface
->data
, n
->node
.key
, o
, node
);
129 interface_data_del(iface
, o
);
131 avl_insert(&iface
->data
, &n
->node
);
136 interface_event(struct interface
*iface
, enum interface_event ev
)
138 struct interface_user
*dep
, *tmp
;
140 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
141 dep
->cb(dep
, iface
, ev
);
143 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
144 dep
->cb(dep
, iface
, ev
);
148 interface_flush_state(struct interface
*iface
)
150 if (iface
->main_dev
.dev
)
151 device_release(&iface
->main_dev
);
152 if (iface
->l3_dev
.dev
)
153 device_release(&iface
->l3_dev
);
154 interface_data_flush(iface
);
158 mark_interface_down(struct interface
*iface
)
160 if (iface
->state
== IFS_UP
)
161 interface_event(iface
, IFEV_DOWN
);
162 interface_ip_set_enabled(&iface
->config_ip
, false);
163 interface_ip_flush(&iface
->proto_ip
);
164 interface_flush_state(iface
);
165 system_flush_routes();
166 iface
->state
= IFS_DOWN
;
170 __interface_set_down(struct interface
*iface
, bool force
)
172 interface_clear_errors(iface
);
174 if (iface
->state
== IFS_DOWN
||
175 iface
->state
== IFS_TEARDOWN
)
178 if (iface
->state
== IFS_UP
)
179 interface_event(iface
, IFEV_DOWN
);
180 iface
->state
= IFS_TEARDOWN
;
181 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
183 interface_flush_state(iface
);
187 interface_cb(struct device_user
*dep
, enum device_event ev
)
189 struct interface
*iface
;
192 iface
= container_of(dep
, struct interface
, main_dev
);
197 case DEV_EVENT_REMOVE
:
204 interface_set_available(iface
, new_state
);
208 interface_set_available(struct interface
*iface
, bool new_state
)
210 if (iface
->available
== new_state
)
213 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
214 iface
->available
= new_state
;
217 if (iface
->autostart
&& !config_init
)
218 interface_set_up(iface
);
220 __interface_set_down(iface
, true);
224 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
227 list_add(&dep
->list
, &iface_all_users
);
232 list_add(&dep
->list
, &iface
->users
);
233 if (iface
->state
== IFS_UP
)
234 dep
->cb(dep
, iface
, IFEV_UP
);
238 interface_remove_user(struct interface_user
*dep
)
240 list_del_init(&dep
->list
);
245 interface_claim_device(struct interface
*iface
)
250 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
251 dev
= device_get(iface
->ifname
, true);
253 interface_set_main_dev(iface
, dev
);
255 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
256 interface_set_available(iface
, true);
261 interface_cleanup(struct interface
*iface
, bool reload
)
263 struct interface_user
*dep
, *tmp
;
265 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
266 interface_remove_user(dep
);
268 interface_ip_flush(&iface
->config_ip
);
269 interface_flush_state(iface
);
270 interface_clear_errors(iface
);
271 if (iface
->main_dev
.dev
&&
272 (!reload
|| !iface
->main_dev
.hotplug
))
273 interface_set_main_dev(iface
, NULL
);
274 interface_set_proto_state(iface
, NULL
);
278 interface_do_free(struct interface
*iface
)
280 interface_event(iface
, IFEV_FREE
);
281 interface_cleanup(iface
, false);
283 netifd_ubus_remove_interface(iface
);
284 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
289 interface_do_reload(struct interface
*iface
)
291 interface_event(iface
, IFEV_RELOAD
);
292 interface_cleanup(iface
, true);
293 proto_init_interface(iface
, iface
->config
);
294 interface_claim_device(iface
);
298 interface_handle_config_change(struct interface
*iface
)
300 switch(iface
->config_state
) {
304 interface_do_reload(iface
);
307 interface_do_free(iface
);
310 if (iface
->autostart
&& iface
->available
)
311 interface_set_up(iface
);
315 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
317 struct interface
*iface
= state
->iface
;
321 if (iface
->state
!= IFS_SETUP
)
324 interface_ip_set_enabled(&iface
->config_ip
, true);
325 system_flush_routes();
326 iface
->state
= IFS_UP
;
327 iface
->start_time
= system_get_rtime();
328 interface_event(iface
, IFEV_UP
);
329 interface_write_resolv_conf();
330 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
333 if (iface
->state
== IFS_DOWN
)
336 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
337 mark_interface_down(iface
);
338 interface_handle_config_change(iface
);
340 case IFPEV_LINK_LOST
:
341 if (iface
->state
!= IFS_UP
)
344 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
345 mark_interface_down(iface
);
346 iface
->state
= IFS_SETUP
;
351 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
354 iface
->proto
->free(iface
->proto
);
357 iface
->state
= IFS_DOWN
;
358 iface
->proto
= state
;
362 state
->proto_event
= interface_proto_cb
;
363 state
->iface
= iface
;
367 interface_init(struct interface
*iface
, const char *name
,
368 struct blob_attr
*config
)
370 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
371 struct blob_attr
*cur
;
372 const char *proto_name
= NULL
;
374 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
375 INIT_LIST_HEAD(&iface
->errors
);
376 INIT_LIST_HEAD(&iface
->users
);
377 INIT_LIST_HEAD(&iface
->hotplug_list
);
378 interface_ip_init(iface
);
379 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
380 iface
->config_ip
.enabled
= false;
382 iface
->main_dev
.cb
= interface_cb
;
384 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
385 blob_data(config
), blob_len(config
));
387 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
388 proto_name
= blobmsg_data(cur
);
390 proto_attach_interface(iface
, proto_name
);
392 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
393 iface
->proto_ip
.no_defaultroute
=
394 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
395 iface
->proto_ip
.no_dns
=
396 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
398 iface
->config_autostart
= iface
->autostart
;
402 interface_add(struct interface
*iface
, struct blob_attr
*config
)
404 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
405 struct blob_attr
*cur
;
407 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
408 blob_data(config
), blob_len(config
));
410 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
411 iface
->ifname
= blobmsg_data(cur
);
413 iface
->config
= config
;
414 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
418 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
420 bool enabled
= iface
->config_ip
.enabled
;
421 bool claimed
= iface
->l3_dev
.claimed
;
423 if (iface
->l3_dev
.dev
== dev
)
426 interface_ip_set_enabled(&iface
->config_ip
, false);
427 interface_ip_flush(&iface
->proto_ip
);
428 device_add_user(&iface
->l3_dev
, dev
);
432 device_claim(&iface
->l3_dev
);
433 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
438 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
440 bool set_l3
= (iface
->main_dev
.dev
== iface
->l3_dev
.dev
);
441 bool claimed
= iface
->l3_dev
.claimed
;
443 if (iface
->main_dev
.dev
== dev
)
447 interface_set_l3_dev(iface
, dev
);
449 device_add_user(&iface
->main_dev
, dev
);
451 device_claim(&iface
->l3_dev
);
453 if (!iface
->l3_dev
.dev
)
454 interface_set_l3_dev(iface
, dev
);
458 interface_remove_link(struct interface
*iface
, struct device
*dev
)
460 struct device
*mdev
= iface
->main_dev
.dev
;
462 if (mdev
&& mdev
->hotplug_ops
)
463 return mdev
->hotplug_ops
->del(mdev
, dev
);
465 if (!iface
->main_dev
.hotplug
)
466 return UBUS_STATUS_INVALID_ARGUMENT
;
468 if (dev
!= iface
->main_dev
.dev
)
469 return UBUS_STATUS_INVALID_ARGUMENT
;
471 device_remove_user(&iface
->main_dev
);
476 interface_add_link(struct interface
*iface
, struct device
*dev
)
478 struct device
*mdev
= iface
->main_dev
.dev
;
483 if (iface
->main_dev
.hotplug
)
484 device_remove_user(&iface
->main_dev
);
487 if (mdev
->hotplug_ops
)
488 return mdev
->hotplug_ops
->add(mdev
, dev
);
490 return UBUS_STATUS_NOT_SUPPORTED
;
493 interface_set_main_dev(iface
, dev
);
494 iface
->main_dev
.hotplug
= true;
499 interface_set_up(struct interface
*iface
)
503 iface
->autostart
= true;
505 if (iface
->state
!= IFS_DOWN
)
508 interface_clear_errors(iface
);
509 if (!iface
->available
) {
510 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
514 if (iface
->main_dev
.dev
) {
515 ret
= device_claim(&iface
->main_dev
);
520 iface
->state
= IFS_SETUP
;
521 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
523 mark_interface_down(iface
);
531 interface_set_down(struct interface
*iface
)
534 vlist_for_each_element(&interfaces
, iface
, node
)
535 __interface_set_down(iface
, false);
537 iface
->autostart
= false;
538 __interface_set_down(iface
, false);
545 interface_start_pending(void)
547 struct interface
*iface
;
549 vlist_for_each_element(&interfaces
, iface
, node
) {
550 if (iface
->available
&& iface
->autostart
)
551 interface_set_up(iface
);
556 set_config_state(struct interface
*iface
, enum interface_config_state s
)
558 iface
->config_state
= s
;
559 if (iface
->state
== IFS_DOWN
)
560 interface_handle_config_change(iface
);
562 __interface_set_down(iface
, false);
566 interface_update_start(struct interface
*iface
)
568 interface_ip_update_start(&iface
->proto_ip
);
572 interface_update_complete(struct interface
*iface
)
574 interface_ip_update_complete(&iface
->proto_ip
);
578 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
580 struct blob_attr
*old_config
= if_old
->config
;
581 const char *old_ifname
= if_old
->ifname
;
582 const struct proto_handler
*proto
= if_old
->proto_handler
;
584 interface_clear_errors(if_old
);
585 if_old
->config
= if_new
->config
;
586 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
587 if_old
->autostart
= true;
589 if_old
->config_autostart
= if_new
->config_autostart
;
590 if_old
->ifname
= if_new
->ifname
;
591 if_old
->proto_handler
= if_new
->proto_handler
;
593 if ((!!old_ifname
!= !!if_new
->ifname
) ||
594 (old_ifname
&& strcmp(old_ifname
, if_new
->ifname
) != 0) ||
595 proto
!= if_new
->proto_handler
) {
596 D(INTERFACE
, "Reload interface '%s' because of ifname/proto change\n",
601 if (!proto
->config_params
)
602 D(INTERFACE
, "No config parameters for interface '%s'\n",
604 else if (!config_check_equal(old_config
, if_new
->config
,
605 proto
->config_params
)) {
606 D(INTERFACE
, "Reload interface '%s because of config changes\n",
611 #define UPDATE(field) ({ \
612 bool __changed = (if_old->field != if_new->field); \
613 if_old->field = if_new->field; \
617 if (UPDATE(metric
) || UPDATE(proto_ip
.no_defaultroute
)) {
618 interface_ip_set_enabled(&if_old
->config_ip
, false);
619 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
620 interface_ip_set_enabled(&if_old
->proto_ip
, false);
621 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
623 if (UPDATE(proto_ip
.no_dns
))
624 interface_write_resolv_conf();
631 set_config_state(if_old
, IFC_RELOAD
);
638 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
639 struct vlist_node
*node_old
)
641 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
642 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
644 if (node_old
&& node_new
) {
645 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
646 interface_change_config(if_old
, if_new
);
647 } else if (node_old
) {
648 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
649 set_config_state(if_old
, IFC_REMOVE
);
650 } else if (node_new
) {
651 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
652 proto_init_interface(if_new
, if_new
->config
);
653 interface_claim_device(if_new
);
654 netifd_ubus_add_interface(if_new
);
660 interface_init_list(void)
662 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
663 interfaces
.keep_old
= true;
664 interfaces
.no_delete
= true;