12 static LIST_HEAD(interfaces);
21 static const union config_param_info iface_attr_info[IFACE_ATTR_MAX] = {
22 [IFACE_ATTR_IFNAME].type = BLOBMSG_TYPE_STRING,
25 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
26 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
27 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
28 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
31 const struct config_param_list interface_attr_list = {
32 .n_params = IFACE_ATTR_MAX,
33 .params = iface_attrs,
34 .info = iface_attr_info,
38 clear_interface_errors(struct interface *iface)
40 struct interface_error *error, *tmp;
42 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
43 list_del(&error->list);
48 void interface_add_error(struct interface *iface, const char *subsystem,
49 const char *code, const char **data, int n_data)
51 struct interface_error *error;
57 len = n_data * sizeof(char *);
58 datalen = alloca(len);
59 for (i = 0; i < n_data; i++) {
60 datalen[i] = strlen(data[i]) + 1;
65 error = calloc(1, sizeof(*error) + sizeof(char *) + len);
69 list_add_tail(&error->list, &iface->errors);
70 error->subsystem = subsystem;
73 dest = (char *) &error->data[n_data + 1];
74 for (i = 0; i < n_data; i++) {
75 error->data[i] = dest;
76 memcpy(dest, data[i], datalen[i]);
79 error->data[n_data] = NULL;
83 interface_event(struct interface *iface, enum interface_event ev)
89 mark_interface_down(struct interface *iface)
91 interface_del_ctx_addr(iface, NULL);
92 device_release(&iface->main_dev);
93 iface->state = IFS_DOWN;
97 __interface_set_up(struct interface *iface)
101 if (iface->state != IFS_DOWN)
104 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 clear_interface_errors(iface);
124 if (iface->state == IFS_DOWN ||
125 iface->state == IFS_TEARDOWN)
128 iface->state = IFS_TEARDOWN;
129 interface_event(iface, IFEV_DOWN);
131 interface_del_all_routes(iface);
132 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
136 interface_cb(struct device_user *dep, enum device_event ev)
138 struct interface *iface;
141 iface = container_of(dep, struct interface, main_dev);
146 case DEV_EVENT_REMOVE:
153 if (iface->active == new_state)
156 iface->active = new_state;
159 if (iface->autostart && !config_init)
160 interface_set_up(iface);
162 __interface_set_down(iface, true);
166 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
168 struct interface *iface = state->iface;
172 if (iface->state != IFS_SETUP)
175 iface->state = IFS_UP;
176 interface_event(iface, IFEV_UP);
179 if (iface->state == IFS_DOWN)
182 mark_interface_down(iface);
187 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
190 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, true);
191 iface->proto->free(iface->proto);
194 iface->state = IFS_DOWN;
195 iface->proto = state;
199 state->proto_event = interface_proto_cb;
200 state->iface = iface;
204 interface_alloc(const char *name, struct blob_attr *attr)
206 struct interface *iface;
207 struct blob_attr *tb[IFACE_ATTR_MAX];
208 struct blob_attr *cur;
210 const char *proto_name = NULL;
212 iface = interface_get(name);
216 iface = calloc(1, sizeof(*iface));
217 iface->main_dev.cb = interface_cb;
218 iface->l3_iface = &iface->main_dev;
219 strncpy(iface->name, name, sizeof(iface->name) - 1);
220 list_add_tail(&iface->list, &interfaces);
221 INIT_LIST_HEAD(&iface->errors);
222 INIT_LIST_HEAD(&iface->address);
223 INIT_LIST_HEAD(&iface->routes);
225 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
226 blob_data(attr), blob_len(attr));
228 if ((cur = tb[IFACE_ATTR_PROTO]))
229 proto_name = blobmsg_data(cur);
231 proto_attach_interface(iface, proto_name);
233 if ((cur = tb[IFACE_ATTR_IFNAME])) {
234 dev = device_get(blobmsg_data(cur), true);
236 device_add_user(&iface->main_dev, dev);
239 netifd_ubus_add_interface(iface);
245 interface_free(struct interface *iface)
247 netifd_ubus_remove_interface(iface);
248 list_del(&iface->list);
249 if (iface->proto->free)
250 iface->proto->free(iface->proto);
255 interface_get(const char *name)
257 struct interface *iface;
259 list_for_each_entry(iface, &interfaces, list) {
260 if (!strcmp(iface->name, name))
267 interface_remove_link(struct interface *iface, struct device *dev)
269 struct device *mdev = iface->main_dev.dev;
271 if (mdev && mdev->hotplug_ops) {
272 mdev->hotplug_ops->del(mdev, dev);
276 device_remove_user(&iface->main_dev);
280 interface_add_link(struct interface *iface, struct device *dev)
282 struct device *mdev = iface->main_dev.dev;
284 if (mdev && mdev->hotplug_ops)
285 return mdev->hotplug_ops->add(mdev, dev);
287 if (iface->main_dev.dev)
288 interface_remove_link(iface, NULL);
290 device_add_user(&iface->main_dev, dev);
296 interface_set_up(struct interface *iface)
298 iface->autostart = true;
300 if (!iface->active) {
301 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
305 if (iface->state != IFS_DOWN)
308 return __interface_set_up(iface);
312 interface_set_down(struct interface *iface)
314 iface->autostart = false;
315 __interface_set_down(iface, false);
321 interface_start_pending(void)
323 struct interface *iface;
325 list_for_each_entry(iface, &interfaces, list) {
326 if (iface->active && iface->autostart)
327 interface_set_up(iface);