add vlist_for_each_element iterator
[project/netifd.git] / interface.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include "netifd.h"
6 #include "device.h"
7 #include "interface.h"
8 #include "interface-ip.h"
9 #include "proto.h"
10 #include "ubus.h"
11 #include "config.h"
12
13 static LIST_HEAD(interfaces);
14
15 enum {
16         IFACE_ATTR_IFNAME,
17         IFACE_ATTR_PROTO,
18         IFACE_ATTR_AUTO,
19         IFACE_ATTR_MAX
20 };
21
22 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
23         [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
24         [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
25         [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
26 };
27
28 const struct config_param_list interface_attr_list = {
29         .n_params = IFACE_ATTR_MAX,
30         .params = iface_attrs,
31 };
32
33 static void
34 clear_interface_errors(struct interface *iface)
35 {
36         struct interface_error *error, *tmp;
37
38         list_for_each_entry_safe(error, tmp, &iface->errors, list) {
39                 list_del(&error->list);
40                 free(error);
41         }
42 }
43
44 void interface_add_error(struct interface *iface, const char *subsystem,
45                          const char *code, const char **data, int n_data)
46 {
47         struct interface_error *error;
48         int i, len = 0;
49         int *datalen = NULL;
50         char *dest;
51
52         if (n_data) {
53                 len = n_data * sizeof(char *);
54                 datalen = alloca(len);
55                 for (i = 0; i < n_data; i++) {
56                         datalen[i] = strlen(data[i]) + 1;
57                         len += datalen[i];
58                 }
59         }
60
61         error = calloc(1, sizeof(*error) + sizeof(char *) + len);
62         if (!error)
63                 return;
64
65         list_add_tail(&error->list, &iface->errors);
66         error->subsystem = subsystem;
67         error->code = code;
68
69         dest = (char *) &error->data[n_data + 1];
70         for (i = 0; i < n_data; i++) {
71                 error->data[i] = dest;
72                 memcpy(dest, data[i], datalen[i]);
73                 dest += datalen[i];
74         }
75         error->data[n_data] = NULL;
76 }
77
78 static void
79 interface_event(struct interface *iface, enum interface_event ev)
80 {
81         /* TODO */
82 }
83
84 static void
85 mark_interface_down(struct interface *iface)
86 {
87         vlist_flush_all(&iface->proto_addr);
88         vlist_flush_all(&iface->proto_route);
89         if (iface->main_dev.dev)
90                 device_release(&iface->main_dev);
91         iface->state = IFS_DOWN;
92 }
93
94 static int
95 __interface_set_up(struct interface *iface)
96 {
97         int ret;
98
99         if (iface->state != IFS_DOWN)
100                 return 0;
101
102         if (iface->main_dev.dev) {
103                 ret = device_claim(&iface->main_dev);
104                 if (ret)
105                         return ret;
106         }
107
108         iface->state = IFS_SETUP;
109         ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
110         if (ret) {
111                 mark_interface_down(iface);
112                 return ret;
113         }
114
115         return 0;
116
117 }
118
119 static void
120 __interface_set_down(struct interface *iface, bool force)
121 {
122         clear_interface_errors(iface);
123
124         if (iface->state == IFS_DOWN ||
125                 iface->state == IFS_TEARDOWN)
126                 return;
127
128         iface->state = IFS_TEARDOWN;
129         interface_event(iface, IFEV_DOWN);
130         interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
131 }
132
133 static void
134 interface_cb(struct device_user *dep, enum device_event ev)
135 {
136         struct interface *iface;
137         bool new_state;
138
139         iface = container_of(dep, struct interface, main_dev);
140         switch (ev) {
141         case DEV_EVENT_ADD:
142                 new_state = true;
143                 break;
144         case DEV_EVENT_REMOVE:
145                 new_state = false;
146                 break;
147         default:
148                 return;
149         }
150
151         interface_set_available(iface, new_state);
152 }
153
154 void
155 interface_set_available(struct interface *iface, bool new_state)
156 {
157         if (iface->available == new_state)
158                 return;
159
160         iface->available = new_state;
161
162         if (new_state) {
163                 if (iface->autostart && !config_init)
164                         interface_set_up(iface);
165         } else
166                 __interface_set_down(iface, true);
167 }
168
169 static void
170 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
171 {
172         struct interface *iface = state->iface;
173
174         switch (ev) {
175         case IFPEV_UP:
176                 if (iface->state != IFS_SETUP)
177                         return;
178
179                 iface->state = IFS_UP;
180                 interface_event(iface, IFEV_UP);
181                 break;
182         case IFPEV_DOWN:
183                 if (iface->state == IFS_DOWN)
184                         return;
185
186                 mark_interface_down(iface);
187                 break;
188         case IFPEV_LINK_LOST:
189                 if (iface->state != IFS_UP)
190                         return;
191
192                 iface->state = IFS_SETUP;
193                 interface_event(iface, IFEV_DOWN);
194                 break;
195         }
196 }
197
198 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
199 {
200         if (iface->proto) {
201                 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, true);
202                 iface->proto->free(iface->proto);
203                 iface->proto = NULL;
204         }
205         iface->state = IFS_DOWN;
206         iface->proto = state;
207         if (!state)
208                 return;
209
210         state->proto_event = interface_proto_cb;
211         state->iface = iface;
212 }
213
214 struct interface *
215 interface_alloc(const char *name, struct blob_attr *attr)
216 {
217         struct interface *iface;
218         struct blob_attr *tb[IFACE_ATTR_MAX];
219         struct blob_attr *cur;
220         struct device *dev;
221         const char *proto_name = NULL;
222
223         iface = interface_get(name);
224         if (iface)
225                 return iface;
226
227         iface = calloc(1, sizeof(*iface));
228         iface->main_dev.cb = interface_cb;
229         iface->l3_dev = &iface->main_dev;
230         strncpy(iface->name, name, sizeof(iface->name) - 1);
231         list_add_tail(&iface->list, &interfaces);
232         INIT_LIST_HEAD(&iface->errors);
233
234         interface_ip_init(iface);
235
236         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
237                       blob_data(attr), blob_len(attr));
238
239         if ((cur = tb[IFACE_ATTR_PROTO]))
240                 proto_name = blobmsg_data(cur);
241
242         proto_attach_interface(iface, proto_name);
243
244         if (iface->proto_handler &&
245             !(iface->proto_handler->flags & PROTO_FLAG_NODEV) &&
246             (cur = tb[IFACE_ATTR_IFNAME])) {
247                 dev = device_get(blobmsg_data(cur), true);
248                 if (dev)
249                         device_add_user(&iface->main_dev, dev);
250         }
251
252         if ((cur = tb[IFACE_ATTR_AUTO]))
253                 iface->autostart = blobmsg_get_bool(cur);
254         else
255                 iface->autostart = true;
256
257         netifd_ubus_add_interface(iface);
258         config_set_state(&iface->config, attr);
259
260         return iface;
261 }
262
263 void
264 interface_free(struct interface *iface)
265 {
266         netifd_ubus_remove_interface(iface);
267         list_del(&iface->list);
268         if (iface->proto->free)
269                 iface->proto->free(iface->proto);
270         free(iface);
271 }
272
273 struct interface *
274 interface_get(const char *name)
275 {
276         struct interface *iface;
277
278         list_for_each_entry(iface, &interfaces, list) {
279                 if (!strcmp(iface->name, name))
280                         return iface;
281         }
282         return NULL;
283 }
284
285 void
286 interface_remove_link(struct interface *iface, struct device *dev)
287 {
288         struct device *mdev = iface->main_dev.dev;
289
290         if (mdev && mdev->hotplug_ops) {
291                 mdev->hotplug_ops->del(mdev, dev);
292                 return;
293         }
294
295         device_remove_user(&iface->main_dev);
296 }
297
298 int
299 interface_add_link(struct interface *iface, struct device *dev)
300 {
301         struct device *mdev = iface->main_dev.dev;
302
303         if (mdev && mdev->hotplug_ops)
304                 return mdev->hotplug_ops->add(mdev, dev);
305
306         if (iface->main_dev.dev)
307                 interface_remove_link(iface, NULL);
308
309         device_add_user(&iface->main_dev, dev);
310
311         return 0;
312 }
313
314 int
315 interface_set_up(struct interface *iface)
316 {
317         iface->autostart = true;
318
319         if (!iface->available) {
320                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
321                 return -1;
322         }
323
324         if (iface->state != IFS_DOWN)
325                 return 0;
326
327         return __interface_set_up(iface);
328 }
329
330 int
331 interface_set_down(struct interface *iface)
332 {
333         if (!iface) {
334                 list_for_each_entry(iface, &interfaces, list)
335                         __interface_set_down(iface, false);
336         } else {
337                 iface->autostart = false;
338                 __interface_set_down(iface, false);
339         }
340
341         return 0;
342 }
343
344 void
345 interface_start_pending(void)
346 {
347         struct interface *iface;
348
349         list_for_each_entry(iface, &interfaces, list) {
350                 if (iface->available && iface->autostart)
351                         interface_set_up(iface);
352         }
353 }