8 #include "interface-ip.h"
13 struct vlist_tree interfaces;
22 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
23 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
24 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
25 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
28 const struct config_param_list interface_attr_list = {
29 .n_params = IFACE_ATTR_MAX,
30 .params = iface_attrs,
34 interface_clear_errors(struct interface *iface)
36 struct interface_error *error, *tmp;
38 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
39 list_del(&error->list);
44 void interface_add_error(struct interface *iface, const char *subsystem,
45 const char *code, const char **data, int n_data)
47 struct interface_error *error;
53 len = n_data * sizeof(char *);
54 datalen = alloca(len);
55 for (i = 0; i < n_data; i++) {
56 datalen[i] = strlen(data[i]) + 1;
61 error = calloc(1, sizeof(*error) + sizeof(char *) + len);
65 list_add_tail(&error->list, &iface->errors);
66 error->subsystem = subsystem;
69 dest = (char *) &error->data[n_data + 1];
70 for (i = 0; i < n_data; i++) {
71 error->data[i] = dest;
72 memcpy(dest, data[i], datalen[i]);
75 error->data[n_data] = NULL;
79 interface_event(struct interface *iface, enum interface_event ev)
85 mark_interface_down(struct interface *iface)
87 vlist_flush_all(&iface->proto_addr);
88 vlist_flush_all(&iface->proto_route);
89 if (iface->main_dev.dev)
90 device_release(&iface->main_dev);
91 iface->state = IFS_DOWN;
95 __interface_set_up(struct interface *iface)
99 if (iface->state != IFS_DOWN)
102 if (iface->main_dev.dev) {
103 ret = device_claim(&iface->main_dev);
108 iface->state = IFS_SETUP;
109 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
111 mark_interface_down(iface);
120 __interface_set_down(struct interface *iface, bool force)
122 interface_clear_errors(iface);
124 if (iface->state == IFS_DOWN ||
125 iface->state == IFS_TEARDOWN)
128 iface->state = IFS_TEARDOWN;
129 interface_event(iface, IFEV_DOWN);
130 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
134 interface_cb(struct device_user *dep, enum device_event ev)
136 struct interface *iface;
139 iface = container_of(dep, struct interface, main_dev);
144 case DEV_EVENT_REMOVE:
151 interface_set_available(iface, new_state);
155 interface_set_available(struct interface *iface, bool new_state)
157 if (iface->available == new_state)
160 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
161 iface->available = new_state;
164 if (iface->autostart && !config_init)
165 interface_set_up(iface);
167 __interface_set_down(iface, true);
171 interface_claim_device(struct interface *iface)
175 if (iface->proto_handler &&
176 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
177 dev = device_get(iface->ifname, true);
179 device_add_user(&iface->main_dev, dev);
185 interface_cleanup(struct interface *iface)
187 interface_clear_errors(iface);
188 if (iface->main_dev.dev)
189 device_remove_user(&iface->main_dev);
190 interface_set_proto_state(iface, NULL);
194 interface_do_free(struct interface *iface)
196 interface_cleanup(iface);
198 netifd_ubus_remove_interface(iface);
199 avl_delete(&interfaces.avl, &iface->node.avl);
204 interface_do_reload(struct interface *iface)
206 interface_cleanup(iface);
208 interface_claim_device(iface);
209 proto_init_interface(iface, iface->config);
211 if (iface->autostart && !config_init)
212 interface_set_up(iface);
216 interface_handle_config_change(struct interface *iface)
218 switch(iface->config_state) {
222 interface_do_reload(iface);
225 interface_do_free(iface);
231 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
233 struct interface *iface = state->iface;
237 if (iface->state != IFS_SETUP)
240 iface->state = IFS_UP;
241 interface_event(iface, IFEV_UP);
244 if (iface->state == IFS_DOWN)
247 mark_interface_down(iface);
248 interface_handle_config_change(iface);
250 case IFPEV_LINK_LOST:
251 if (iface->state != IFS_UP)
254 iface->state = IFS_SETUP;
255 interface_event(iface, IFEV_DOWN);
260 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
263 iface->proto->free(iface->proto);
266 iface->state = IFS_DOWN;
267 iface->proto = state;
271 state->proto_event = interface_proto_cb;
272 state->iface = iface;
276 interface_init(struct interface *iface, const char *name,
277 struct blob_attr *config)
279 struct blob_attr *tb[IFACE_ATTR_MAX];
280 struct blob_attr *cur;
281 const char *proto_name = NULL;
283 strncpy(iface->name, name, sizeof(iface->name) - 1);
284 INIT_LIST_HEAD(&iface->errors);
286 iface->main_dev.cb = interface_cb;
287 iface->l3_dev = &iface->main_dev;
289 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
290 blob_data(config), blob_len(config));
292 if ((cur = tb[IFACE_ATTR_PROTO]))
293 proto_name = blobmsg_data(cur);
295 proto_attach_interface(iface, proto_name);
297 if ((cur = tb[IFACE_ATTR_AUTO]))
298 iface->autostart = blobmsg_get_bool(cur);
300 iface->autostart = true;
301 iface->config_autostart = iface->autostart;
305 interface_add(struct interface *iface, struct blob_attr *config)
307 struct blob_attr *tb[IFACE_ATTR_MAX];
308 struct blob_attr *cur;
310 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
311 blob_data(config), blob_len(config));
313 if ((cur = tb[IFACE_ATTR_IFNAME]))
314 iface->ifname = blobmsg_data(cur);
316 iface->config = config;
317 vlist_add(&interfaces, &iface->node);
321 interface_remove_link(struct interface *iface, struct device *dev)
323 struct device *mdev = iface->main_dev.dev;
325 if (mdev && mdev->hotplug_ops) {
326 mdev->hotplug_ops->del(mdev, dev);
330 device_remove_user(&iface->main_dev);
334 interface_add_link(struct interface *iface, struct device *dev)
336 struct device *mdev = iface->main_dev.dev;
338 if (mdev && mdev->hotplug_ops)
339 return mdev->hotplug_ops->add(mdev, dev);
341 if (iface->main_dev.dev)
342 interface_remove_link(iface, NULL);
344 device_add_user(&iface->main_dev, dev);
350 interface_set_up(struct interface *iface)
352 iface->autostart = true;
354 if (!iface->available) {
355 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
359 if (iface->state != IFS_DOWN)
362 return __interface_set_up(iface);
366 interface_set_down(struct interface *iface)
369 vlist_for_each_element(&interfaces, iface, node)
370 __interface_set_down(iface, false);
372 iface->autostart = false;
373 __interface_set_down(iface, false);
380 interface_start_pending(void)
382 struct interface *iface;
384 vlist_for_each_element(&interfaces, iface, node) {
385 if (iface->available && iface->autostart)
386 interface_set_up(iface);
391 set_config_state(struct interface *iface, enum interface_config_state s)
393 iface->config_state = s;
394 if (iface->state == IFS_DOWN)
395 interface_handle_config_change(iface);
397 __interface_set_down(iface, false);
401 interface_change_config(struct interface *if_old, struct interface *if_new)
403 struct blob_attr *old_config = if_old->config;
404 const char *old_ifname = if_old->ifname;
405 const struct proto_handler *proto = if_old->proto_handler;
407 interface_clear_errors(if_old);
408 if_old->config = if_new->config;
409 if (!if_old->config_autostart && if_new->config_autostart)
410 if_old->autostart = true;
412 if_old->config_autostart = if_new->config_autostart;
413 if_old->ifname = if_new->ifname;
414 if_old->proto_handler = if_new->proto_handler;
416 if (strcmp(old_ifname, if_new->ifname) != 0 ||
417 proto != if_new->proto_handler) {
418 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
423 if (!proto->config_params)
424 D(INTERFACE, "No config parameters for interface '%s'\n",
426 else if (!config_check_equal(old_config, if_new->config,
427 proto->config_params)) {
428 D(INTERFACE, "Reload interface '%s because of config changes\n",
436 set_config_state(if_old, IFC_RELOAD);
443 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
444 struct vlist_node *node_old)
446 struct interface *if_old = container_of(node_old, struct interface, node);
447 struct interface *if_new = container_of(node_new, struct interface, node);
449 if (node_old && node_new) {
450 D(INTERFACE, "Update interface '%s'\n", if_new->name);
451 interface_change_config(if_old, if_new);
452 } else if (node_old) {
453 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
454 set_config_state(if_old, IFC_REMOVE);
455 } else if (node_new) {
456 D(INTERFACE, "Create interface '%s'\n", if_new->name);
457 interface_claim_device(if_new);
458 proto_init_interface(if_new, if_new->config);
459 interface_ip_init(if_new);
460 netifd_ubus_add_interface(if_new);
466 interface_init_list(void)
468 vlist_init(&interfaces, avl_strcmp, interface_update,
469 struct interface, node, name);
470 interfaces.keep_old = true;
471 interfaces.no_delete = true;