11 static LIST_HEAD(interfaces);
14 clear_interface_errors(struct interface *iface)
16 struct interface_error *error, *tmp;
18 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
19 list_del(&error->list);
24 void interface_add_error(struct interface *iface, const char *subsystem,
25 const char *code, const char **data, int n_data)
27 struct interface_error *error;
33 len = n_data * sizeof(char *);
34 datalen = alloca(len);
35 for (i = 0; i < n_data; i++) {
36 datalen[i] = strlen(data[i]) + 1;
41 error = calloc(1, sizeof(*error) + sizeof(char *) + len);
45 list_add_tail(&error->list, &iface->errors);
46 error->subsystem = subsystem;
49 dest = (char *) &error->data[n_data + 1];
50 for (i = 0; i < n_data; i++) {
51 error->data[i] = dest;
52 memcpy(dest, data[i], datalen[i]);
55 error->data[n_data] = NULL;
59 interface_event(struct interface *iface, enum interface_event ev)
65 mark_interface_down(struct interface *iface)
67 release_device(iface->main_dev.dev);
68 iface->state = IFS_DOWN;
72 __set_interface_up(struct interface *iface)
76 if (iface->state != IFS_DOWN)
79 ret = claim_device(iface->main_dev.dev);
83 iface->state = IFS_SETUP;
84 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
86 mark_interface_down(iface);
95 __set_interface_down(struct interface *iface, bool force)
97 clear_interface_errors(iface);
99 if (iface->state == IFS_DOWN ||
100 iface->state == IFS_TEARDOWN)
103 iface->state = IFS_TEARDOWN;
104 interface_event(iface, IFEV_DOWN);
106 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
110 interface_cb(struct device_user *dep, enum device_event ev)
112 struct interface *iface;
115 iface = container_of(dep, struct interface, main_dev);
120 case DEV_EVENT_REMOVE:
127 if (iface->active == new_state)
130 iface->active = new_state;
133 if (iface->autostart)
134 set_interface_up(iface);
136 __set_interface_down(iface, true);
140 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
142 struct interface *iface = state->iface;
146 if (iface->state != IFS_SETUP)
149 iface->state = IFS_UP;
150 interface_event(iface, IFEV_UP);
153 if (iface->state == IFS_DOWN)
156 mark_interface_down(iface);
161 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
164 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, true);
165 iface->proto->free(iface->proto);
168 iface->state = IFS_DOWN;
169 iface->proto = state;
173 state->proto_event = interface_proto_cb;
174 state->iface = iface;
178 alloc_interface(const char *name)
180 struct interface *iface;
182 iface = get_interface(name);
186 iface = calloc(1, sizeof(*iface));
188 interface_set_proto_state(iface, get_default_proto());
194 iface->main_dev.cb = interface_cb;
195 iface->l3_iface = &iface->main_dev;
196 strncpy(iface->name, name, sizeof(iface->name) - 1);
197 list_add(&iface->list, &interfaces);
198 INIT_LIST_HEAD(&iface->errors);
200 netifd_ubus_add_interface(iface);
206 free_interface(struct interface *iface)
208 netifd_ubus_remove_interface(iface);
209 list_del(&iface->list);
210 if (iface->proto->free)
211 iface->proto->free(iface->proto);
216 get_interface(const char *name)
218 struct interface *iface;
220 list_for_each_entry(iface, &interfaces, list) {
221 if (!strcmp(iface->name, name))
228 interface_remove_link(struct interface *iface, struct device *dev)
230 struct device *mdev = iface->main_dev.dev;
232 if (mdev && mdev->hotplug_ops) {
233 mdev->hotplug_ops->del(mdev, dev);
237 remove_device_user(&iface->main_dev);
241 interface_add_link(struct interface *iface, struct device *dev)
243 struct device *mdev = iface->main_dev.dev;
245 if (mdev && mdev->hotplug_ops)
246 return mdev->hotplug_ops->add(mdev, dev);
248 if (iface->main_dev.dev)
249 interface_remove_link(iface, NULL);
251 add_device_user(&iface->main_dev, dev);
257 set_interface_up(struct interface *iface)
259 iface->autostart = true;
261 if (!iface->active) {
262 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
266 if (iface->state != IFS_DOWN)
269 return __set_interface_up(iface);
273 set_interface_down(struct interface *iface)
275 iface->autostart = false;
276 __set_interface_down(iface, false);