8 #include "interface-ip.h"
14 struct vlist_tree interfaces;
23 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
24 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
25 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
26 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
29 const struct config_param_list interface_attr_list = {
30 .n_params = IFACE_ATTR_MAX,
31 .params = iface_attrs,
35 interface_clear_errors(struct interface *iface)
37 struct interface_error *error, *tmp;
39 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
40 list_del(&error->list);
45 void interface_add_error(struct interface *iface, const char *subsystem,
46 const char *code, const char **data, int n_data)
48 struct interface_error *error;
54 len = n_data * sizeof(char *);
55 datalen = alloca(len);
56 for (i = 0; i < n_data; i++) {
57 datalen[i] = strlen(data[i]) + 1;
62 error = calloc(1, sizeof(*error) + sizeof(char *) + len);
66 list_add_tail(&error->list, &iface->errors);
67 error->subsystem = subsystem;
70 dest = (char *) &error->data[n_data + 1];
71 for (i = 0; i < n_data; i++) {
72 error->data[i] = dest;
73 memcpy(dest, data[i], datalen[i]);
76 error->data[n_data] = NULL;
80 interface_event(struct interface *iface, enum interface_event ev)
82 struct interface_user *dep, *tmp;
84 list_for_each_entry_safe(dep, tmp, &iface->users, list)
85 dep->cb(dep, IFEV_UP);
87 interface_queue_event(iface, ev);
91 interface_flush_state(struct interface *iface)
93 interface_ip_flush(&iface->proto_ip);
94 if (iface->main_dev.dev)
95 device_release(&iface->main_dev);
99 mark_interface_down(struct interface *iface)
101 interface_flush_state(iface);
102 iface->state = IFS_DOWN;
106 __interface_set_up(struct interface *iface)
110 if (iface->state != IFS_DOWN)
113 if (iface->main_dev.dev) {
114 ret = device_claim(&iface->main_dev);
119 iface->state = IFS_SETUP;
120 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
122 mark_interface_down(iface);
130 __interface_set_down(struct interface *iface, bool force)
132 interface_clear_errors(iface);
134 if (iface->state == IFS_DOWN ||
135 iface->state == IFS_TEARDOWN)
138 iface->state = IFS_TEARDOWN;
139 interface_event(iface, IFEV_DOWN);
140 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
142 interface_flush_state(iface);
146 interface_cb(struct device_user *dep, enum device_event ev)
148 struct interface *iface;
151 iface = container_of(dep, struct interface, main_dev);
156 case DEV_EVENT_REMOVE:
163 interface_set_available(iface, new_state);
167 interface_set_available(struct interface *iface, bool new_state)
169 if (iface->available == new_state)
172 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
173 iface->available = new_state;
176 if (iface->autostart && !config_init)
177 interface_set_up(iface);
179 __interface_set_down(iface, true);
183 interface_add_user(struct interface_user *dep, struct interface *iface)
186 list_add(&dep->list, &iface->users);
187 if (iface->state == IFS_UP)
188 dep->cb(dep, IFEV_UP);
192 interface_remove_user(struct interface_user *dep)
194 list_del_init(&dep->list);
199 interface_claim_device(struct interface *iface)
203 if (iface->ifname && iface->proto_handler &&
204 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
205 dev = device_get(iface->ifname, true);
207 device_add_user(&iface->main_dev, dev);
213 interface_cleanup(struct interface *iface)
215 struct interface_user *dep, *tmp;
217 list_for_each_entry_safe(dep, tmp, &iface->users, list)
218 interface_remove_user(dep);
220 interface_ip_flush(&iface->config_ip);
221 interface_flush_state(iface);
222 interface_clear_errors(iface);
223 if (iface->main_dev.dev)
224 device_remove_user(&iface->main_dev);
225 iface->l3_dev = &iface->main_dev;
226 interface_set_proto_state(iface, NULL);
230 interface_do_free(struct interface *iface)
232 interface_cleanup(iface);
234 netifd_ubus_remove_interface(iface);
235 avl_delete(&interfaces.avl, &iface->node.avl);
240 interface_do_reload(struct interface *iface)
242 interface_cleanup(iface);
244 interface_claim_device(iface);
245 proto_init_interface(iface, iface->config);
247 if (iface->autostart && !config_init)
248 interface_set_up(iface);
252 interface_handle_config_change(struct interface *iface)
254 switch(iface->config_state) {
258 interface_do_reload(iface);
261 interface_do_free(iface);
267 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
269 struct interface *iface = state->iface;
273 if (iface->state != IFS_SETUP)
276 interface_ip_set_enabled(&iface->config_ip, true);
277 system_flush_routes();
278 iface->state = IFS_UP;
279 iface->start_time = system_get_rtime();
280 interface_event(iface, IFEV_UP);
281 interface_write_resolv_conf();
282 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
285 if (iface->state == IFS_DOWN)
288 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
289 interface_ip_set_enabled(&iface->config_ip, false);
290 system_flush_routes();
291 mark_interface_down(iface);
292 interface_handle_config_change(iface);
293 if (iface->l3_dev->dev)
294 device_release(iface->l3_dev);
295 if (iface->autostart && iface->available)
296 __interface_set_up(iface);
298 case IFPEV_LINK_LOST:
299 if (iface->state != IFS_UP)
302 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
303 iface->state = IFS_SETUP;
304 interface_event(iface, IFEV_DOWN);
309 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
312 iface->proto->free(iface->proto);
315 iface->state = IFS_DOWN;
316 iface->proto = state;
320 state->proto_event = interface_proto_cb;
321 state->iface = iface;
325 interface_init(struct interface *iface, const char *name,
326 struct blob_attr *config)
328 struct blob_attr *tb[IFACE_ATTR_MAX];
329 struct blob_attr *cur;
330 const char *proto_name = NULL;
332 strncpy(iface->name, name, sizeof(iface->name) - 1);
333 INIT_LIST_HEAD(&iface->errors);
334 INIT_LIST_HEAD(&iface->users);
335 INIT_LIST_HEAD(&iface->hotplug_list);
336 interface_ip_init(&iface->proto_ip, iface);
337 interface_ip_init(&iface->config_ip, iface);
338 iface->config_ip.enabled = false;
340 iface->main_dev.cb = interface_cb;
341 iface->l3_dev = &iface->main_dev;
343 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
344 blob_data(config), blob_len(config));
346 if ((cur = tb[IFACE_ATTR_PROTO]))
347 proto_name = blobmsg_data(cur);
349 proto_attach_interface(iface, proto_name);
351 if ((cur = tb[IFACE_ATTR_AUTO]))
352 iface->autostart = blobmsg_get_bool(cur);
354 iface->autostart = true;
355 iface->config_autostart = iface->autostart;
359 interface_add(struct interface *iface, struct blob_attr *config)
361 struct blob_attr *tb[IFACE_ATTR_MAX];
362 struct blob_attr *cur;
364 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
365 blob_data(config), blob_len(config));
367 if ((cur = tb[IFACE_ATTR_IFNAME]))
368 iface->ifname = blobmsg_data(cur);
370 iface->config = config;
371 vlist_add(&interfaces, &iface->node);
375 interface_remove_link(struct interface *iface, struct device *dev)
377 struct device *mdev = iface->main_dev.dev;
379 if (mdev && mdev->hotplug_ops) {
380 mdev->hotplug_ops->del(mdev, dev);
384 device_remove_user(&iface->main_dev);
388 interface_add_link(struct interface *iface, struct device *dev)
390 struct device *mdev = iface->main_dev.dev;
392 if (mdev && mdev->hotplug_ops)
393 return mdev->hotplug_ops->add(mdev, dev);
395 if (iface->main_dev.dev)
396 interface_remove_link(iface, NULL);
398 device_add_user(&iface->main_dev, dev);
404 interface_set_up(struct interface *iface)
406 iface->autostart = true;
408 if (iface->state != IFS_DOWN)
411 interface_clear_errors(iface);
412 if (!iface->available) {
413 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
417 return __interface_set_up(iface);
421 interface_set_down(struct interface *iface)
424 vlist_for_each_element(&interfaces, iface, node)
425 __interface_set_down(iface, false);
427 iface->autostart = false;
428 __interface_set_down(iface, false);
435 interface_start_pending(void)
437 struct interface *iface;
439 vlist_for_each_element(&interfaces, iface, node) {
440 if (iface->available && iface->autostart)
441 interface_set_up(iface);
446 set_config_state(struct interface *iface, enum interface_config_state s)
448 iface->config_state = s;
449 if (iface->state == IFS_DOWN)
450 interface_handle_config_change(iface);
452 __interface_set_down(iface, false);
456 interface_update_start(struct interface *iface)
458 interface_ip_update_start(&iface->proto_ip);
462 interface_update_complete(struct interface *iface)
464 struct device_route *route;
466 interface_ip_update_complete(&iface->proto_ip);
467 vlist_for_each_element(&iface->config_ip.route, route, node) {
468 if (iface->l3_dev->dev)
469 system_add_route(iface->l3_dev->dev, route);
474 interface_change_config(struct interface *if_old, struct interface *if_new)
476 struct blob_attr *old_config = if_old->config;
477 const char *old_ifname = if_old->ifname;
478 const struct proto_handler *proto = if_old->proto_handler;
480 interface_clear_errors(if_old);
481 if_old->config = if_new->config;
482 if (!if_old->config_autostart && if_new->config_autostart)
483 if_old->autostart = true;
485 if_old->config_autostart = if_new->config_autostart;
486 if_old->ifname = if_new->ifname;
487 if_old->proto_handler = if_new->proto_handler;
489 if (strcmp(old_ifname, if_new->ifname) != 0 ||
490 proto != if_new->proto_handler) {
491 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
496 if (!proto->config_params)
497 D(INTERFACE, "No config parameters for interface '%s'\n",
499 else if (!config_check_equal(old_config, if_new->config,
500 proto->config_params)) {
501 D(INTERFACE, "Reload interface '%s because of config changes\n",
509 set_config_state(if_old, IFC_RELOAD);
516 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
517 struct vlist_node *node_old)
519 struct interface *if_old = container_of(node_old, struct interface, node);
520 struct interface *if_new = container_of(node_new, struct interface, node);
522 if (node_old && node_new) {
523 D(INTERFACE, "Update interface '%s'\n", if_new->name);
524 interface_change_config(if_old, if_new);
525 } else if (node_old) {
526 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
527 set_config_state(if_old, IFC_REMOVE);
528 } else if (node_new) {
529 D(INTERFACE, "Create interface '%s'\n", if_new->name);
530 interface_claim_device(if_new);
531 proto_init_interface(if_new, if_new->config);
532 netifd_ubus_add_interface(if_new);
538 interface_init_list(void)
540 vlist_init(&interfaces, avl_strcmp, interface_update,
541 struct interface, node, name);
542 interfaces.keep_old = true;
543 interfaces.no_delete = true;