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 interface_queue_event(iface, ev);
86 mark_interface_down(struct interface *iface)
88 interface_clear_dns(iface);
89 vlist_flush_all(&iface->proto_addr);
90 vlist_flush_all(&iface->proto_route);
91 if (iface->main_dev.dev)
92 device_release(&iface->main_dev);
93 iface->state = IFS_DOWN;
97 __interface_set_up(struct interface *iface)
101 if (iface->state != IFS_DOWN)
104 if (iface->main_dev.dev) {
105 ret = device_claim(&iface->main_dev);
110 iface->state = IFS_SETUP;
111 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
113 mark_interface_down(iface);
122 __interface_set_down(struct interface *iface, bool force)
124 interface_clear_errors(iface);
126 if (iface->state == IFS_DOWN ||
127 iface->state == IFS_TEARDOWN)
130 iface->state = IFS_TEARDOWN;
131 interface_event(iface, IFEV_DOWN);
132 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
136 interface_cb(struct device_user *dep, enum device_event ev)
138 struct interface *iface;
141 iface = container_of(dep, struct interface, main_dev);
146 case DEV_EVENT_REMOVE:
153 interface_set_available(iface, new_state);
157 interface_set_available(struct interface *iface, bool new_state)
159 if (iface->available == new_state)
162 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
163 iface->available = new_state;
166 if (iface->autostart && !config_init)
167 interface_set_up(iface);
169 __interface_set_down(iface, true);
173 interface_claim_device(struct interface *iface)
177 if (iface->proto_handler &&
178 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
179 dev = device_get(iface->ifname, true);
181 device_add_user(&iface->main_dev, dev);
187 interface_cleanup(struct interface *iface)
189 interface_clear_dns(iface);
190 interface_clear_errors(iface);
191 if (iface->main_dev.dev)
192 device_remove_user(&iface->main_dev);
193 interface_set_proto_state(iface, NULL);
197 interface_do_free(struct interface *iface)
199 interface_cleanup(iface);
201 netifd_ubus_remove_interface(iface);
202 avl_delete(&interfaces.avl, &iface->node.avl);
207 interface_do_reload(struct interface *iface)
209 interface_cleanup(iface);
211 interface_claim_device(iface);
212 proto_init_interface(iface, iface->config);
214 if (iface->autostart && !config_init)
215 interface_set_up(iface);
219 interface_handle_config_change(struct interface *iface)
221 switch(iface->config_state) {
225 interface_do_reload(iface);
228 interface_do_free(iface);
234 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
236 struct interface *iface = state->iface;
240 if (iface->state != IFS_SETUP)
243 system_flush_routes();
244 iface->state = IFS_UP;
245 iface->start_time = system_get_rtime();
246 interface_event(iface, IFEV_UP);
247 interface_write_resolv_conf();
250 if (iface->state == IFS_DOWN)
253 system_flush_routes();
254 mark_interface_down(iface);
255 interface_handle_config_change(iface);
256 if (iface->autostart)
257 __interface_set_up(iface);
259 case IFPEV_LINK_LOST:
260 if (iface->state != IFS_UP)
263 iface->state = IFS_SETUP;
264 interface_event(iface, IFEV_DOWN);
269 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
272 iface->proto->free(iface->proto);
275 iface->state = IFS_DOWN;
276 iface->proto = state;
280 state->proto_event = interface_proto_cb;
281 state->iface = iface;
285 interface_init(struct interface *iface, const char *name,
286 struct blob_attr *config)
288 struct blob_attr *tb[IFACE_ATTR_MAX];
289 struct blob_attr *cur;
290 const char *proto_name = NULL;
292 strncpy(iface->name, name, sizeof(iface->name) - 1);
293 INIT_LIST_HEAD(&iface->errors);
294 INIT_LIST_HEAD(&iface->hotplug_list);
295 INIT_LIST_HEAD(&iface->proto_dns_search);
296 INIT_LIST_HEAD(&iface->proto_dns_servers);
298 iface->main_dev.cb = interface_cb;
299 iface->l3_dev = &iface->main_dev;
301 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
302 blob_data(config), blob_len(config));
304 if ((cur = tb[IFACE_ATTR_PROTO]))
305 proto_name = blobmsg_data(cur);
307 proto_attach_interface(iface, proto_name);
309 if ((cur = tb[IFACE_ATTR_AUTO]))
310 iface->autostart = blobmsg_get_bool(cur);
312 iface->autostart = true;
313 iface->config_autostart = iface->autostart;
317 interface_add(struct interface *iface, struct blob_attr *config)
319 struct blob_attr *tb[IFACE_ATTR_MAX];
320 struct blob_attr *cur;
322 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
323 blob_data(config), blob_len(config));
325 if ((cur = tb[IFACE_ATTR_IFNAME]))
326 iface->ifname = blobmsg_data(cur);
328 iface->config = config;
329 vlist_add(&interfaces, &iface->node);
333 interface_remove_link(struct interface *iface, struct device *dev)
335 struct device *mdev = iface->main_dev.dev;
337 if (mdev && mdev->hotplug_ops) {
338 mdev->hotplug_ops->del(mdev, dev);
342 device_remove_user(&iface->main_dev);
346 interface_add_link(struct interface *iface, struct device *dev)
348 struct device *mdev = iface->main_dev.dev;
350 if (mdev && mdev->hotplug_ops)
351 return mdev->hotplug_ops->add(mdev, dev);
353 if (iface->main_dev.dev)
354 interface_remove_link(iface, NULL);
356 device_add_user(&iface->main_dev, dev);
362 interface_set_up(struct interface *iface)
364 iface->autostart = true;
366 if (!iface->available) {
367 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
371 if (iface->state != IFS_DOWN)
374 return __interface_set_up(iface);
378 interface_set_down(struct interface *iface)
381 vlist_for_each_element(&interfaces, iface, node)
382 __interface_set_down(iface, false);
384 iface->autostart = false;
385 __interface_set_down(iface, false);
392 interface_start_pending(void)
394 struct interface *iface;
396 vlist_for_each_element(&interfaces, iface, node) {
397 if (iface->available && iface->autostart)
398 interface_set_up(iface);
403 set_config_state(struct interface *iface, enum interface_config_state s)
405 iface->config_state = s;
406 if (iface->state == IFS_DOWN)
407 interface_handle_config_change(iface);
409 __interface_set_down(iface, false);
413 interface_change_config(struct interface *if_old, struct interface *if_new)
415 struct blob_attr *old_config = if_old->config;
416 const char *old_ifname = if_old->ifname;
417 const struct proto_handler *proto = if_old->proto_handler;
419 interface_clear_errors(if_old);
420 if_old->config = if_new->config;
421 if (!if_old->config_autostart && if_new->config_autostart)
422 if_old->autostart = true;
424 if_old->config_autostart = if_new->config_autostart;
425 if_old->ifname = if_new->ifname;
426 if_old->proto_handler = if_new->proto_handler;
428 if (strcmp(old_ifname, if_new->ifname) != 0 ||
429 proto != if_new->proto_handler) {
430 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
435 if (!proto->config_params)
436 D(INTERFACE, "No config parameters for interface '%s'\n",
438 else if (!config_check_equal(old_config, if_new->config,
439 proto->config_params)) {
440 D(INTERFACE, "Reload interface '%s because of config changes\n",
448 set_config_state(if_old, IFC_RELOAD);
455 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
456 struct vlist_node *node_old)
458 struct interface *if_old = container_of(node_old, struct interface, node);
459 struct interface *if_new = container_of(node_new, struct interface, node);
461 if (node_old && node_new) {
462 D(INTERFACE, "Update interface '%s'\n", if_new->name);
463 interface_change_config(if_old, if_new);
464 } else if (node_old) {
465 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
466 set_config_state(if_old, IFC_REMOVE);
467 } else if (node_new) {
468 D(INTERFACE, "Create interface '%s'\n", if_new->name);
469 interface_claim_device(if_new);
470 proto_init_interface(if_new, if_new->config);
471 interface_ip_init(if_new);
472 netifd_ubus_add_interface(if_new);
478 interface_init_list(void)
480 vlist_init(&interfaces, avl_strcmp, interface_update,
481 struct interface, node, name);
482 interfaces.keep_old = true;
483 interfaces.no_delete = true;