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 vlist_flush_all(&iface->proto_addr);
89 vlist_flush_all(&iface->proto_route);
90 if (iface->main_dev.dev)
91 device_release(&iface->main_dev);
92 iface->state = IFS_DOWN;
96 __interface_set_up(struct interface *iface)
100 if (iface->state != IFS_DOWN)
103 if (iface->main_dev.dev) {
104 ret = device_claim(&iface->main_dev);
109 iface->state = IFS_SETUP;
110 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
112 mark_interface_down(iface);
121 __interface_set_down(struct interface *iface, bool force)
123 interface_clear_errors(iface);
125 if (iface->state == IFS_DOWN ||
126 iface->state == IFS_TEARDOWN)
129 iface->state = IFS_TEARDOWN;
130 interface_event(iface, IFEV_DOWN);
131 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
135 interface_cb(struct device_user *dep, enum device_event ev)
137 struct interface *iface;
140 iface = container_of(dep, struct interface, main_dev);
145 case DEV_EVENT_REMOVE:
152 interface_set_available(iface, new_state);
156 interface_set_available(struct interface *iface, bool new_state)
158 if (iface->available == new_state)
161 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
162 iface->available = new_state;
165 if (iface->autostart && !config_init)
166 interface_set_up(iface);
168 __interface_set_down(iface, true);
172 interface_claim_device(struct interface *iface)
176 if (iface->proto_handler &&
177 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
178 dev = device_get(iface->ifname, true);
180 device_add_user(&iface->main_dev, dev);
186 interface_cleanup(struct interface *iface)
188 interface_clear_dns(iface);
189 interface_clear_errors(iface);
190 if (iface->main_dev.dev)
191 device_remove_user(&iface->main_dev);
192 interface_set_proto_state(iface, NULL);
196 interface_do_free(struct interface *iface)
198 interface_cleanup(iface);
200 netifd_ubus_remove_interface(iface);
201 avl_delete(&interfaces.avl, &iface->node.avl);
206 interface_do_reload(struct interface *iface)
208 interface_cleanup(iface);
210 interface_claim_device(iface);
211 proto_init_interface(iface, iface->config);
213 if (iface->autostart && !config_init)
214 interface_set_up(iface);
218 interface_handle_config_change(struct interface *iface)
220 switch(iface->config_state) {
224 interface_do_reload(iface);
227 interface_do_free(iface);
233 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
235 struct interface *iface = state->iface;
239 if (iface->state != IFS_SETUP)
242 iface->state = IFS_UP;
243 iface->start_time = system_get_rtime();
244 interface_event(iface, IFEV_UP);
245 interface_write_resolv_conf();
248 if (iface->state == IFS_DOWN)
251 mark_interface_down(iface);
252 interface_handle_config_change(iface);
253 if (iface->autostart)
254 __interface_set_up(iface);
256 case IFPEV_LINK_LOST:
257 if (iface->state != IFS_UP)
260 iface->state = IFS_SETUP;
261 interface_event(iface, IFEV_DOWN);
266 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
269 iface->proto->free(iface->proto);
272 iface->state = IFS_DOWN;
273 iface->proto = state;
277 state->proto_event = interface_proto_cb;
278 state->iface = iface;
282 interface_init(struct interface *iface, const char *name,
283 struct blob_attr *config)
285 struct blob_attr *tb[IFACE_ATTR_MAX];
286 struct blob_attr *cur;
287 const char *proto_name = NULL;
289 strncpy(iface->name, name, sizeof(iface->name) - 1);
290 INIT_LIST_HEAD(&iface->errors);
291 INIT_LIST_HEAD(&iface->hotplug_list);
292 INIT_LIST_HEAD(&iface->proto_dns_search);
293 INIT_LIST_HEAD(&iface->proto_dns_servers);
295 iface->main_dev.cb = interface_cb;
296 iface->l3_dev = &iface->main_dev;
298 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
299 blob_data(config), blob_len(config));
301 if ((cur = tb[IFACE_ATTR_PROTO]))
302 proto_name = blobmsg_data(cur);
304 proto_attach_interface(iface, proto_name);
306 if ((cur = tb[IFACE_ATTR_AUTO]))
307 iface->autostart = blobmsg_get_bool(cur);
309 iface->autostart = true;
310 iface->config_autostart = iface->autostart;
314 interface_add(struct interface *iface, struct blob_attr *config)
316 struct blob_attr *tb[IFACE_ATTR_MAX];
317 struct blob_attr *cur;
319 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
320 blob_data(config), blob_len(config));
322 if ((cur = tb[IFACE_ATTR_IFNAME]))
323 iface->ifname = blobmsg_data(cur);
325 iface->config = config;
326 vlist_add(&interfaces, &iface->node);
330 interface_remove_link(struct interface *iface, struct device *dev)
332 struct device *mdev = iface->main_dev.dev;
334 if (mdev && mdev->hotplug_ops) {
335 mdev->hotplug_ops->del(mdev, dev);
339 device_remove_user(&iface->main_dev);
343 interface_add_link(struct interface *iface, struct device *dev)
345 struct device *mdev = iface->main_dev.dev;
347 if (mdev && mdev->hotplug_ops)
348 return mdev->hotplug_ops->add(mdev, dev);
350 if (iface->main_dev.dev)
351 interface_remove_link(iface, NULL);
353 device_add_user(&iface->main_dev, dev);
359 interface_set_up(struct interface *iface)
361 iface->autostart = true;
363 if (!iface->available) {
364 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
368 if (iface->state != IFS_DOWN)
371 return __interface_set_up(iface);
375 interface_set_down(struct interface *iface)
378 vlist_for_each_element(&interfaces, iface, node)
379 __interface_set_down(iface, false);
381 iface->autostart = false;
382 __interface_set_down(iface, false);
389 interface_start_pending(void)
391 struct interface *iface;
393 vlist_for_each_element(&interfaces, iface, node) {
394 if (iface->available && iface->autostart)
395 interface_set_up(iface);
400 set_config_state(struct interface *iface, enum interface_config_state s)
402 iface->config_state = s;
403 if (iface->state == IFS_DOWN)
404 interface_handle_config_change(iface);
406 __interface_set_down(iface, false);
410 interface_change_config(struct interface *if_old, struct interface *if_new)
412 struct blob_attr *old_config = if_old->config;
413 const char *old_ifname = if_old->ifname;
414 const struct proto_handler *proto = if_old->proto_handler;
416 interface_clear_errors(if_old);
417 if_old->config = if_new->config;
418 if (!if_old->config_autostart && if_new->config_autostart)
419 if_old->autostart = true;
421 if_old->config_autostart = if_new->config_autostart;
422 if_old->ifname = if_new->ifname;
423 if_old->proto_handler = if_new->proto_handler;
425 if (strcmp(old_ifname, if_new->ifname) != 0 ||
426 proto != if_new->proto_handler) {
427 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
432 if (!proto->config_params)
433 D(INTERFACE, "No config parameters for interface '%s'\n",
435 else if (!config_check_equal(old_config, if_new->config,
436 proto->config_params)) {
437 D(INTERFACE, "Reload interface '%s because of config changes\n",
445 set_config_state(if_old, IFC_RELOAD);
452 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
453 struct vlist_node *node_old)
455 struct interface *if_old = container_of(node_old, struct interface, node);
456 struct interface *if_new = container_of(node_new, struct interface, node);
458 if (node_old && node_new) {
459 D(INTERFACE, "Update interface '%s'\n", if_new->name);
460 interface_change_config(if_old, if_new);
461 } else if (node_old) {
462 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
463 set_config_state(if_old, IFC_REMOVE);
464 } else if (node_new) {
465 D(INTERFACE, "Create interface '%s'\n", if_new->name);
466 interface_claim_device(if_new);
467 proto_init_interface(if_new, if_new->config);
468 interface_ip_init(if_new);
469 netifd_ubus_add_interface(if_new);
475 interface_init_list(void)
477 vlist_init(&interfaces, avl_strcmp, interface_update,
478 struct interface, node, name);
479 interfaces.keep_old = true;
480 interfaces.no_delete = true;