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_ctx_addr(iface, NULL);
93 device_release(&iface->main_dev);
94 iface->state = IFS_DOWN;
98 __interface_set_up(struct interface *iface)
102 if (iface->state != IFS_DOWN)
105 ret = device_claim(&iface->main_dev);
109 iface->state = IFS_SETUP;
110 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
112 mark_interface_down(iface);
121 __interface_set_down(struct interface *iface, bool force)
123 clear_interface_errors(iface);
125 if (iface->state == IFS_DOWN ||
126 iface->state == IFS_TEARDOWN)
129 iface->state = IFS_TEARDOWN;
130 interface_event(iface, IFEV_DOWN);
132 interface_del_all_routes(iface);
133 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
137 interface_cb(struct device_user *dep, enum device_event ev)
139 struct interface *iface;
142 iface = container_of(dep, struct interface, main_dev);
147 case DEV_EVENT_REMOVE:
154 if (iface->active == new_state)
157 iface->active = new_state;
160 if (iface->autostart && !config_init)
161 interface_set_up(iface);
163 __interface_set_down(iface, true);
167 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
169 struct interface *iface = state->iface;
173 if (iface->state != IFS_SETUP)
176 iface->state = IFS_UP;
177 interface_event(iface, IFEV_UP);
180 if (iface->state == IFS_DOWN)
183 mark_interface_down(iface);
188 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
191 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, true);
192 iface->proto->free(iface->proto);
195 iface->state = IFS_DOWN;
196 iface->proto = state;
200 state->proto_event = interface_proto_cb;
201 state->iface = iface;
205 interface_alloc(const char *name, struct blob_attr *attr)
207 struct interface *iface;
208 struct blob_attr *tb[IFACE_ATTR_MAX];
209 struct blob_attr *cur;
211 const char *proto_name = NULL;
213 iface = interface_get(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_tail(&iface->list, &interfaces);
222 INIT_LIST_HEAD(&iface->errors);
223 INIT_LIST_HEAD(&iface->address);
224 INIT_LIST_HEAD(&iface->routes);
226 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
227 blob_data(attr), blob_len(attr));
229 if ((cur = tb[IFACE_ATTR_PROTO]))
230 proto_name = blobmsg_data(cur);
232 proto_attach_interface(iface, proto_name);
234 if ((cur = tb[IFACE_ATTR_IFNAME])) {
235 dev = device_get(blobmsg_data(cur), true);
237 device_add_user(&iface->main_dev, dev);
240 netifd_ubus_add_interface(iface);
241 config_set_state(&iface->config, attr);
247 interface_free(struct interface *iface)
249 netifd_ubus_remove_interface(iface);
250 list_del(&iface->list);
251 if (iface->proto->free)
252 iface->proto->free(iface->proto);
257 interface_get(const char *name)
259 struct interface *iface;
261 list_for_each_entry(iface, &interfaces, list) {
262 if (!strcmp(iface->name, name))
269 interface_remove_link(struct interface *iface, struct device *dev)
271 struct device *mdev = iface->main_dev.dev;
273 if (mdev && mdev->hotplug_ops) {
274 mdev->hotplug_ops->del(mdev, dev);
278 device_remove_user(&iface->main_dev);
282 interface_add_link(struct interface *iface, struct device *dev)
284 struct device *mdev = iface->main_dev.dev;
286 if (mdev && mdev->hotplug_ops)
287 return mdev->hotplug_ops->add(mdev, dev);
289 if (iface->main_dev.dev)
290 interface_remove_link(iface, NULL);
292 device_add_user(&iface->main_dev, dev);
298 interface_set_up(struct interface *iface)
300 iface->autostart = true;
302 if (!iface->active) {
303 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
307 if (iface->state != IFS_DOWN)
310 return __interface_set_up(iface);
314 interface_set_down(struct interface *iface)
316 iface->autostart = false;
317 __interface_set_down(iface, false);
323 interface_start_pending(void)
325 struct interface *iface;
327 list_for_each_entry(iface, &interfaces, list) {
328 if (iface->active && iface->autostart)
329 interface_set_up(iface);