8 #include "interface-ip.h"
13 static LIST_HEAD(interfaces);
22 static const union config_param_info iface_attr_info[IFACE_ATTR_MAX] = {
23 [IFACE_ATTR_IFNAME].type = BLOBMSG_TYPE_STRING,
26 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
27 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
28 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
29 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
32 const struct config_param_list interface_attr_list = {
33 .n_params = IFACE_ATTR_MAX,
34 .params = iface_attrs,
35 .info = iface_attr_info,
39 clear_interface_errors(struct interface *iface)
41 struct interface_error *error, *tmp;
43 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
44 list_del(&error->list);
49 void interface_add_error(struct interface *iface, const char *subsystem,
50 const char *code, const char **data, int n_data)
52 struct interface_error *error;
58 len = n_data * sizeof(char *);
59 datalen = alloca(len);
60 for (i = 0; i < n_data; i++) {
61 datalen[i] = strlen(data[i]) + 1;
66 error = calloc(1, sizeof(*error) + sizeof(char *) + len);
70 list_add_tail(&error->list, &iface->errors);
71 error->subsystem = subsystem;
74 dest = (char *) &error->data[n_data + 1];
75 for (i = 0; i < n_data; i++) {
76 error->data[i] = dest;
77 memcpy(dest, data[i], datalen[i]);
80 error->data[n_data] = NULL;
84 interface_event(struct interface *iface, enum interface_event ev)
90 mark_interface_down(struct interface *iface)
92 interface_del_all_routes(iface);
93 interface_del_ctx_addr(iface, NULL);
94 device_release(&iface->main_dev);
95 iface->state = IFS_DOWN;
99 __interface_set_up(struct interface *iface)
103 if (iface->state != IFS_DOWN)
106 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 clear_interface_errors(iface);
126 if (iface->state == IFS_DOWN ||
127 iface->state == IFS_TEARDOWN)
130 iface->state = IFS_TEARDOWN;
131 interface_event(iface, IFEV_DOWN);
133 interface_del_all_routes(iface);
134 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
138 interface_cb(struct device_user *dep, enum device_event ev)
140 struct interface *iface;
143 iface = container_of(dep, struct interface, main_dev);
148 case DEV_EVENT_REMOVE:
155 if (iface->active == new_state)
158 iface->active = new_state;
161 if (iface->autostart && !config_init)
162 interface_set_up(iface);
164 __interface_set_down(iface, true);
168 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
170 struct interface *iface = state->iface;
174 if (iface->state != IFS_SETUP)
177 iface->state = IFS_UP;
178 interface_event(iface, IFEV_UP);
181 if (iface->state == IFS_DOWN)
184 mark_interface_down(iface);
189 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
192 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, true);
193 iface->proto->free(iface->proto);
196 iface->state = IFS_DOWN;
197 iface->proto = state;
201 state->proto_event = interface_proto_cb;
202 state->iface = iface;
206 interface_alloc(const char *name, struct blob_attr *attr)
208 struct interface *iface;
209 struct blob_attr *tb[IFACE_ATTR_MAX];
210 struct blob_attr *cur;
212 const char *proto_name = NULL;
214 iface = interface_get(name);
218 iface = calloc(1, sizeof(*iface));
219 iface->main_dev.cb = interface_cb;
220 iface->l3_iface = &iface->main_dev;
221 strncpy(iface->name, name, sizeof(iface->name) - 1);
222 list_add_tail(&iface->list, &interfaces);
223 INIT_LIST_HEAD(&iface->errors);
224 INIT_LIST_HEAD(&iface->address);
225 INIT_LIST_HEAD(&iface->routes);
227 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
228 blob_data(attr), blob_len(attr));
230 if ((cur = tb[IFACE_ATTR_PROTO]))
231 proto_name = blobmsg_data(cur);
233 proto_attach_interface(iface, proto_name);
235 if ((cur = tb[IFACE_ATTR_IFNAME])) {
236 dev = device_get(blobmsg_data(cur), true);
238 device_add_user(&iface->main_dev, dev);
241 if ((cur = tb[IFACE_ATTR_AUTO]))
242 iface->autostart = blobmsg_get_bool(cur);
244 iface->autostart = true;
246 netifd_ubus_add_interface(iface);
247 config_set_state(&iface->config, attr);
253 interface_free(struct interface *iface)
255 netifd_ubus_remove_interface(iface);
256 list_del(&iface->list);
257 if (iface->proto->free)
258 iface->proto->free(iface->proto);
263 interface_get(const char *name)
265 struct interface *iface;
267 list_for_each_entry(iface, &interfaces, list) {
268 if (!strcmp(iface->name, name))
275 interface_remove_link(struct interface *iface, struct device *dev)
277 struct device *mdev = iface->main_dev.dev;
279 if (mdev && mdev->hotplug_ops) {
280 mdev->hotplug_ops->del(mdev, dev);
284 device_remove_user(&iface->main_dev);
288 interface_add_link(struct interface *iface, struct device *dev)
290 struct device *mdev = iface->main_dev.dev;
292 if (mdev && mdev->hotplug_ops)
293 return mdev->hotplug_ops->add(mdev, dev);
295 if (iface->main_dev.dev)
296 interface_remove_link(iface, NULL);
298 device_add_user(&iface->main_dev, dev);
304 interface_set_up(struct interface *iface)
306 iface->autostart = true;
308 if (!iface->active) {
309 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
313 if (iface->state != IFS_DOWN)
316 return __interface_set_up(iface);
320 interface_set_down(struct interface *iface)
322 iface->autostart = false;
323 __interface_set_down(iface, false);
329 interface_start_pending(void)
331 struct interface *iface;
333 list_for_each_entry(iface, &interfaces, list) {
334 if (iface->active && iface->autostart)
335 interface_set_up(iface);