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 mark_interface_down(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);
98 iface->state = IFS_DOWN;
102 __interface_set_up(struct interface *iface)
106 if (iface->state != IFS_DOWN)
109 if (iface->main_dev.dev) {
110 ret = device_claim(&iface->main_dev);
115 iface->state = IFS_SETUP;
116 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
118 mark_interface_down(iface);
126 __interface_set_down(struct interface *iface, bool force)
128 interface_clear_errors(iface);
130 if (iface->state == IFS_DOWN ||
131 iface->state == IFS_TEARDOWN)
134 iface->state = IFS_TEARDOWN;
135 interface_event(iface, IFEV_DOWN);
136 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
140 interface_cb(struct device_user *dep, enum device_event ev)
142 struct interface *iface;
145 iface = container_of(dep, struct interface, main_dev);
150 case DEV_EVENT_REMOVE:
157 interface_set_available(iface, new_state);
161 interface_set_available(struct interface *iface, bool new_state)
163 if (iface->available == new_state)
166 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
167 iface->available = new_state;
170 if (iface->autostart && !config_init)
171 interface_set_up(iface);
173 __interface_set_down(iface, true);
177 interface_add_user(struct interface_user *dep, struct interface *iface)
180 list_add(&dep->list, &iface->users);
181 if (iface->state == IFS_UP)
182 dep->cb(dep, IFEV_UP);
186 interface_remove_user(struct interface_user *dep)
188 list_del_init(&dep->list);
193 interface_claim_device(struct interface *iface)
197 if (iface->ifname && iface->proto_handler &&
198 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
199 dev = device_get(iface->ifname, true);
201 device_add_user(&iface->main_dev, dev);
207 interface_cleanup(struct interface *iface)
209 struct interface_user *dep, *tmp;
211 iface->hotplug_dev = false;
212 list_for_each_entry_safe(dep, tmp, &iface->users, list)
213 interface_remove_user(dep);
215 interface_clear_dns(iface);
216 interface_clear_errors(iface);
217 if (iface->main_dev.dev)
218 device_remove_user(&iface->main_dev);
219 iface->l3_dev = &iface->main_dev;
220 interface_set_proto_state(iface, NULL);
224 interface_do_free(struct interface *iface)
226 interface_cleanup(iface);
228 netifd_ubus_remove_interface(iface);
229 avl_delete(&interfaces.avl, &iface->node.avl);
234 interface_do_reload(struct interface *iface)
236 interface_cleanup(iface);
238 interface_claim_device(iface);
239 proto_init_interface(iface, iface->config);
241 if (iface->autostart && !config_init)
242 interface_set_up(iface);
246 interface_handle_config_change(struct interface *iface)
248 switch(iface->config_state) {
252 interface_do_reload(iface);
255 interface_do_free(iface);
261 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
263 struct interface *iface = state->iface;
267 if (iface->state != IFS_SETUP)
270 system_flush_routes();
271 iface->state = IFS_UP;
272 iface->start_time = system_get_rtime();
273 interface_event(iface, IFEV_UP);
274 interface_write_resolv_conf();
275 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
278 if (iface->state == IFS_DOWN)
281 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
282 system_flush_routes();
283 mark_interface_down(iface);
284 interface_handle_config_change(iface);
285 if (iface->l3_dev->dev)
286 device_release(iface->l3_dev);
287 if (iface->autostart && iface->available)
288 __interface_set_up(iface);
290 case IFPEV_LINK_LOST:
291 if (iface->state != IFS_UP)
294 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
295 iface->state = IFS_SETUP;
296 interface_event(iface, IFEV_DOWN);
301 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
304 iface->proto->free(iface->proto);
307 iface->state = IFS_DOWN;
308 iface->proto = state;
312 state->proto_event = interface_proto_cb;
313 state->iface = iface;
317 interface_init(struct interface *iface, const char *name,
318 struct blob_attr *config)
320 struct blob_attr *tb[IFACE_ATTR_MAX];
321 struct blob_attr *cur;
322 const char *proto_name = NULL;
324 strncpy(iface->name, name, sizeof(iface->name) - 1);
325 INIT_LIST_HEAD(&iface->errors);
326 INIT_LIST_HEAD(&iface->users);
327 INIT_LIST_HEAD(&iface->hotplug_list);
328 INIT_LIST_HEAD(&iface->proto_dns_search);
329 INIT_LIST_HEAD(&iface->proto_dns_servers);
331 iface->main_dev.cb = interface_cb;
332 iface->l3_dev = &iface->main_dev;
334 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
335 blob_data(config), blob_len(config));
337 if ((cur = tb[IFACE_ATTR_PROTO]))
338 proto_name = blobmsg_data(cur);
340 proto_attach_interface(iface, proto_name);
342 if ((cur = tb[IFACE_ATTR_AUTO]))
343 iface->autostart = blobmsg_get_bool(cur);
345 iface->autostart = true;
346 iface->config_autostart = iface->autostart;
350 interface_add(struct interface *iface, struct blob_attr *config)
352 struct blob_attr *tb[IFACE_ATTR_MAX];
353 struct blob_attr *cur;
355 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
356 blob_data(config), blob_len(config));
358 if ((cur = tb[IFACE_ATTR_IFNAME]))
359 iface->ifname = blobmsg_data(cur);
361 iface->config = config;
362 vlist_add(&interfaces, &iface->node);
366 interface_remove_link(struct interface *iface, struct device *dev)
368 struct device *mdev = iface->main_dev.dev;
370 if (mdev && mdev->hotplug_ops) {
371 mdev->hotplug_ops->del(mdev, dev);
375 device_remove_user(&iface->main_dev);
379 interface_add_link(struct interface *iface, struct device *dev)
381 struct device *mdev = iface->main_dev.dev;
383 if (mdev && mdev->hotplug_ops)
384 return mdev->hotplug_ops->add(mdev, dev);
386 if (iface->main_dev.dev)
387 interface_remove_link(iface, NULL);
389 device_add_user(&iface->main_dev, dev);
395 interface_set_up(struct interface *iface)
397 iface->autostart = true;
399 if (iface->state != IFS_DOWN)
402 interface_clear_errors(iface);
403 if (!iface->available) {
404 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
408 return __interface_set_up(iface);
412 interface_set_down(struct interface *iface)
415 vlist_for_each_element(&interfaces, iface, node)
416 __interface_set_down(iface, false);
418 iface->autostart = false;
419 __interface_set_down(iface, false);
426 interface_start_pending(void)
428 struct interface *iface;
430 vlist_for_each_element(&interfaces, iface, node) {
431 if (iface->available && iface->autostart)
432 interface_set_up(iface);
437 set_config_state(struct interface *iface, enum interface_config_state s)
439 iface->config_state = s;
440 if (iface->state == IFS_DOWN)
441 interface_handle_config_change(iface);
443 __interface_set_down(iface, false);
447 interface_change_config(struct interface *if_old, struct interface *if_new)
449 struct blob_attr *old_config = if_old->config;
450 const char *old_ifname = if_old->ifname;
451 const struct proto_handler *proto = if_old->proto_handler;
453 interface_clear_errors(if_old);
454 if_old->config = if_new->config;
455 if (!if_old->config_autostart && if_new->config_autostart)
456 if_old->autostart = true;
458 if_old->config_autostart = if_new->config_autostart;
459 if_old->ifname = if_new->ifname;
460 if_old->proto_handler = if_new->proto_handler;
462 if (strcmp(old_ifname, if_new->ifname) != 0 ||
463 proto != if_new->proto_handler) {
464 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
469 if (!proto->config_params)
470 D(INTERFACE, "No config parameters for interface '%s'\n",
472 else if (!config_check_equal(old_config, if_new->config,
473 proto->config_params)) {
474 D(INTERFACE, "Reload interface '%s because of config changes\n",
482 set_config_state(if_old, IFC_RELOAD);
489 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
490 struct vlist_node *node_old)
492 struct interface *if_old = container_of(node_old, struct interface, node);
493 struct interface *if_new = container_of(node_new, struct interface, node);
495 if (node_old && node_new) {
496 D(INTERFACE, "Update interface '%s'\n", if_new->name);
497 interface_change_config(if_old, if_new);
498 } else if (node_old) {
499 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
500 set_config_state(if_old, IFC_REMOVE);
501 } else if (node_new) {
502 D(INTERFACE, "Create interface '%s'\n", if_new->name);
503 interface_claim_device(if_new);
504 proto_init_interface(if_new, if_new->config);
505 interface_ip_init(if_new);
506 netifd_ubus_add_interface(if_new);
512 interface_init_list(void)
514 vlist_init(&interfaces, avl_strcmp, interface_update,
515 struct interface, node, name);
516 interfaces.keep_old = true;
517 interfaces.no_delete = true;