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_clear_dns(iface);
94 vlist_flush_all(&iface->proto_addr);
95 vlist_flush_all(&iface->proto_route);
96 if (iface->main_dev.dev)
97 device_release(&iface->main_dev);
101 mark_interface_down(struct interface *iface)
103 interface_flush_state(iface);
104 iface->state = IFS_DOWN;
108 __interface_set_up(struct interface *iface)
112 if (iface->state != IFS_DOWN)
115 if (iface->main_dev.dev) {
116 ret = device_claim(&iface->main_dev);
121 iface->state = IFS_SETUP;
122 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
124 mark_interface_down(iface);
132 __interface_set_down(struct interface *iface, bool force)
134 interface_clear_errors(iface);
136 if (iface->state == IFS_DOWN ||
137 iface->state == IFS_TEARDOWN)
140 iface->state = IFS_TEARDOWN;
141 interface_event(iface, IFEV_DOWN);
142 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
144 interface_flush_state(iface);
148 interface_cb(struct device_user *dep, enum device_event ev)
150 struct interface *iface;
153 iface = container_of(dep, struct interface, main_dev);
158 case DEV_EVENT_REMOVE:
165 interface_set_available(iface, new_state);
169 interface_set_available(struct interface *iface, bool new_state)
171 if (iface->available == new_state)
174 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
175 iface->available = new_state;
178 if (iface->autostart && !config_init)
179 interface_set_up(iface);
181 __interface_set_down(iface, true);
185 interface_add_user(struct interface_user *dep, struct interface *iface)
188 list_add(&dep->list, &iface->users);
189 if (iface->state == IFS_UP)
190 dep->cb(dep, IFEV_UP);
194 interface_remove_user(struct interface_user *dep)
196 list_del_init(&dep->list);
201 interface_claim_device(struct interface *iface)
205 if (iface->ifname && iface->proto_handler &&
206 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
207 dev = device_get(iface->ifname, true);
209 device_add_user(&iface->main_dev, dev);
215 interface_cleanup(struct interface *iface)
217 struct interface_user *dep, *tmp;
219 list_for_each_entry_safe(dep, tmp, &iface->users, list)
220 interface_remove_user(dep);
222 interface_clear_dns(iface);
223 interface_clear_errors(iface);
224 if (iface->main_dev.dev)
225 device_remove_user(&iface->main_dev);
226 iface->l3_dev = &iface->main_dev;
227 interface_set_proto_state(iface, NULL);
231 interface_do_free(struct interface *iface)
233 interface_cleanup(iface);
235 netifd_ubus_remove_interface(iface);
236 avl_delete(&interfaces.avl, &iface->node.avl);
241 interface_do_reload(struct interface *iface)
243 interface_cleanup(iface);
245 interface_claim_device(iface);
246 proto_init_interface(iface, iface->config);
248 if (iface->autostart && !config_init)
249 interface_set_up(iface);
253 interface_handle_config_change(struct interface *iface)
255 switch(iface->config_state) {
259 interface_do_reload(iface);
262 interface_do_free(iface);
268 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
270 struct interface *iface = state->iface;
274 if (iface->state != IFS_SETUP)
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 system_flush_routes();
290 mark_interface_down(iface);
291 interface_handle_config_change(iface);
292 if (iface->l3_dev->dev)
293 device_release(iface->l3_dev);
294 if (iface->autostart && iface->available)
295 __interface_set_up(iface);
297 case IFPEV_LINK_LOST:
298 if (iface->state != IFS_UP)
301 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
302 iface->state = IFS_SETUP;
303 interface_event(iface, IFEV_DOWN);
308 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
311 iface->proto->free(iface->proto);
314 iface->state = IFS_DOWN;
315 iface->proto = state;
319 state->proto_event = interface_proto_cb;
320 state->iface = iface;
324 interface_init(struct interface *iface, const char *name,
325 struct blob_attr *config)
327 struct blob_attr *tb[IFACE_ATTR_MAX];
328 struct blob_attr *cur;
329 const char *proto_name = NULL;
331 strncpy(iface->name, name, sizeof(iface->name) - 1);
332 INIT_LIST_HEAD(&iface->errors);
333 INIT_LIST_HEAD(&iface->users);
334 INIT_LIST_HEAD(&iface->hotplug_list);
335 INIT_LIST_HEAD(&iface->proto_dns_search);
336 INIT_LIST_HEAD(&iface->proto_dns_servers);
338 iface->main_dev.cb = interface_cb;
339 iface->l3_dev = &iface->main_dev;
341 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
342 blob_data(config), blob_len(config));
344 if ((cur = tb[IFACE_ATTR_PROTO]))
345 proto_name = blobmsg_data(cur);
347 proto_attach_interface(iface, proto_name);
349 if ((cur = tb[IFACE_ATTR_AUTO]))
350 iface->autostart = blobmsg_get_bool(cur);
352 iface->autostart = true;
353 iface->config_autostart = iface->autostart;
357 interface_add(struct interface *iface, struct blob_attr *config)
359 struct blob_attr *tb[IFACE_ATTR_MAX];
360 struct blob_attr *cur;
362 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
363 blob_data(config), blob_len(config));
365 if ((cur = tb[IFACE_ATTR_IFNAME]))
366 iface->ifname = blobmsg_data(cur);
368 iface->config = config;
369 vlist_add(&interfaces, &iface->node);
373 interface_remove_link(struct interface *iface, struct device *dev)
375 struct device *mdev = iface->main_dev.dev;
377 if (mdev && mdev->hotplug_ops) {
378 mdev->hotplug_ops->del(mdev, dev);
382 device_remove_user(&iface->main_dev);
386 interface_add_link(struct interface *iface, struct device *dev)
388 struct device *mdev = iface->main_dev.dev;
390 if (mdev && mdev->hotplug_ops)
391 return mdev->hotplug_ops->add(mdev, dev);
393 if (iface->main_dev.dev)
394 interface_remove_link(iface, NULL);
396 device_add_user(&iface->main_dev, dev);
402 interface_set_up(struct interface *iface)
404 iface->autostart = true;
406 if (iface->state != IFS_DOWN)
409 interface_clear_errors(iface);
410 if (!iface->available) {
411 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
415 return __interface_set_up(iface);
419 interface_set_down(struct interface *iface)
422 vlist_for_each_element(&interfaces, iface, node)
423 __interface_set_down(iface, false);
425 iface->autostart = false;
426 __interface_set_down(iface, false);
433 interface_start_pending(void)
435 struct interface *iface;
437 vlist_for_each_element(&interfaces, iface, node) {
438 if (iface->available && iface->autostart)
439 interface_set_up(iface);
444 set_config_state(struct interface *iface, enum interface_config_state s)
446 iface->config_state = s;
447 if (iface->state == IFS_DOWN)
448 interface_handle_config_change(iface);
450 __interface_set_down(iface, false);
454 interface_change_config(struct interface *if_old, struct interface *if_new)
456 struct blob_attr *old_config = if_old->config;
457 const char *old_ifname = if_old->ifname;
458 const struct proto_handler *proto = if_old->proto_handler;
460 interface_clear_errors(if_old);
461 if_old->config = if_new->config;
462 if (!if_old->config_autostart && if_new->config_autostart)
463 if_old->autostart = true;
465 if_old->config_autostart = if_new->config_autostart;
466 if_old->ifname = if_new->ifname;
467 if_old->proto_handler = if_new->proto_handler;
469 if (strcmp(old_ifname, if_new->ifname) != 0 ||
470 proto != if_new->proto_handler) {
471 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
476 if (!proto->config_params)
477 D(INTERFACE, "No config parameters for interface '%s'\n",
479 else if (!config_check_equal(old_config, if_new->config,
480 proto->config_params)) {
481 D(INTERFACE, "Reload interface '%s because of config changes\n",
489 set_config_state(if_old, IFC_RELOAD);
496 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
497 struct vlist_node *node_old)
499 struct interface *if_old = container_of(node_old, struct interface, node);
500 struct interface *if_new = container_of(node_new, struct interface, node);
502 if (node_old && node_new) {
503 D(INTERFACE, "Update interface '%s'\n", if_new->name);
504 interface_change_config(if_old, if_new);
505 } else if (node_old) {
506 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
507 set_config_state(if_old, IFC_REMOVE);
508 } else if (node_new) {
509 D(INTERFACE, "Create interface '%s'\n", if_new->name);
510 interface_claim_device(if_new);
511 proto_init_interface(if_new, if_new->config);
512 interface_ip_init(if_new);
513 netifd_ubus_add_interface(if_new);
519 interface_init_list(void)
521 vlist_init(&interfaces, avl_strcmp, interface_update,
522 struct interface, node, name);
523 interfaces.keep_old = true;
524 interfaces.no_delete = true;