12 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_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
28 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
29 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_ARRAY },
30 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
33 const struct config_param_list interface_attr_list = {
34 .n_params = IFACE_ATTR_MAX,
35 .params = iface_attrs,
36 .info = iface_attr_info,
40 clear_interface_errors(struct interface *iface)
42 struct interface_error *error, *tmp;
44 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
45 list_del(&error->list);
50 void interface_add_error(struct interface *iface, const char *subsystem,
51 const char *code, const char **data, int n_data)
53 struct interface_error *error;
59 len = n_data * sizeof(char *);
60 datalen = alloca(len);
61 for (i = 0; i < n_data; i++) {
62 datalen[i] = strlen(data[i]) + 1;
67 error = calloc(1, sizeof(*error) + sizeof(char *) + len);
71 list_add_tail(&error->list, &iface->errors);
72 error->subsystem = subsystem;
75 dest = (char *) &error->data[n_data + 1];
76 for (i = 0; i < n_data; i++) {
77 error->data[i] = dest;
78 memcpy(dest, data[i], datalen[i]);
81 error->data[n_data] = NULL;
85 interface_event(struct interface *iface, enum interface_event ev)
91 mark_interface_down(struct interface *iface)
93 interface_del_ctx_addr(iface, NULL);
94 device_release(iface->main_dev.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.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 alloc_interface(const char *name, struct uci_section *s, struct blob_attr *attr)
208 struct interface *iface;
209 struct blob_attr *tb[IFACE_ATTR_MAX];
210 struct blob_attr *cur;
213 iface = get_interface(name);
217 iface = calloc(1, sizeof(*iface));
218 iface->main_dev.cb = interface_cb;
219 iface->l3_iface = &iface->main_dev;
220 strncpy(iface->name, name, sizeof(iface->name) - 1);
221 list_add(&iface->list, &interfaces);
222 INIT_LIST_HEAD(&iface->errors);
223 INIT_LIST_HEAD(&iface->address);
224 INIT_LIST_HEAD(&iface->routes);
226 proto_attach_interface(iface, s);
228 netifd_ubus_add_interface(iface);
230 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
231 blob_data(attr), blob_len(attr));
233 if ((cur = tb[IFACE_ATTR_TYPE])) {
234 if (!strcmp(blobmsg_data(cur), "bridge"))
235 interface_attach_bridge(iface, s);
238 if ((cur = tb[IFACE_ATTR_IFNAME])) {
239 dev = device_get(blobmsg_data(cur), true);
241 device_add_user(&iface->main_dev, dev);
248 free_interface(struct interface *iface)
250 netifd_ubus_remove_interface(iface);
251 list_del(&iface->list);
252 if (iface->proto->free)
253 iface->proto->free(iface->proto);
258 get_interface(const char *name)
260 struct interface *iface;
262 list_for_each_entry(iface, &interfaces, list) {
263 if (!strcmp(iface->name, name))
270 interface_remove_link(struct interface *iface, struct device *dev)
272 struct device *mdev = iface->main_dev.dev;
274 if (mdev && mdev->hotplug_ops) {
275 mdev->hotplug_ops->del(mdev, dev);
279 device_remove_user(&iface->main_dev);
283 interface_add_link(struct interface *iface, struct device *dev)
285 struct device *mdev = iface->main_dev.dev;
287 if (mdev && mdev->hotplug_ops)
288 return mdev->hotplug_ops->add(mdev, dev);
290 if (iface->main_dev.dev)
291 interface_remove_link(iface, NULL);
293 device_add_user(&iface->main_dev, dev);
299 interface_set_up(struct interface *iface)
301 iface->autostart = true;
303 if (!iface->active) {
304 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
308 if (iface->state != IFS_DOWN)
311 return __interface_set_up(iface);
315 interface_set_down(struct interface *iface)
317 iface->autostart = false;
318 __interface_set_down(iface, false);
324 start_pending_interfaces(void)
326 struct interface *iface;
328 list_for_each_entry(iface, &interfaces, list) {
329 if (iface->active && iface->autostart)
330 interface_set_up(iface);