system-linux: resolve interface index at device_up
[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 struct vlist_tree 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 interface_clear_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         interface_queue_event(iface, ev);
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         interface_clear_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         D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
161         iface->available = new_state;
162
163         if (new_state) {
164                 if (iface->autostart && !config_init)
165                         interface_set_up(iface);
166         } else
167                 __interface_set_down(iface, true);
168 }
169
170 static void
171 interface_claim_device(struct interface *iface)
172 {
173         struct device *dev;
174
175         if (iface->proto_handler &&
176                 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
177                 dev = device_get(iface->ifname, true);
178                 if (dev)
179                         device_add_user(&iface->main_dev, dev);
180         }
181 }
182
183
184 static void
185 interface_cleanup(struct interface *iface)
186 {
187         interface_clear_errors(iface);
188         if (iface->main_dev.dev)
189                 device_remove_user(&iface->main_dev);
190         interface_set_proto_state(iface, NULL);
191 }
192
193 static void
194 interface_do_free(struct interface *iface)
195 {
196         interface_cleanup(iface);
197         free(iface->config);
198         netifd_ubus_remove_interface(iface);
199         avl_delete(&interfaces.avl, &iface->node.avl);
200         free(iface);
201 }
202
203 static void
204 interface_do_reload(struct interface *iface)
205 {
206         interface_cleanup(iface);
207
208         interface_claim_device(iface);
209         proto_init_interface(iface, iface->config);
210
211         if (iface->autostart && !config_init)
212                 interface_set_up(iface);
213 }
214
215 static void
216 interface_handle_config_change(struct interface *iface)
217 {
218         switch(iface->config_state) {
219         case IFC_NORMAL:
220                 break;
221         case IFC_RELOAD:
222                 interface_do_reload(iface);
223                 break;
224         case IFC_REMOVE:
225                 interface_do_free(iface);
226                 break;
227         }
228 }
229
230 static void
231 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
232 {
233         struct interface *iface = state->iface;
234
235         switch (ev) {
236         case IFPEV_UP:
237                 if (iface->state != IFS_SETUP)
238                         return;
239
240                 iface->state = IFS_UP;
241                 interface_event(iface, IFEV_UP);
242                 break;
243         case IFPEV_DOWN:
244                 if (iface->state == IFS_DOWN)
245                         return;
246
247                 mark_interface_down(iface);
248                 interface_handle_config_change(iface);
249                 break;
250         case IFPEV_LINK_LOST:
251                 if (iface->state != IFS_UP)
252                         return;
253
254                 iface->state = IFS_SETUP;
255                 interface_event(iface, IFEV_DOWN);
256                 break;
257         }
258 }
259
260 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
261 {
262         if (iface->proto) {
263                 iface->proto->free(iface->proto);
264                 iface->proto = NULL;
265         }
266         iface->state = IFS_DOWN;
267         iface->proto = state;
268         if (!state)
269                 return;
270
271         state->proto_event = interface_proto_cb;
272         state->iface = iface;
273 }
274
275 void
276 interface_init(struct interface *iface, const char *name,
277                struct blob_attr *config)
278 {
279         struct blob_attr *tb[IFACE_ATTR_MAX];
280         struct blob_attr *cur;
281         const char *proto_name = NULL;
282
283         strncpy(iface->name, name, sizeof(iface->name) - 1);
284         INIT_LIST_HEAD(&iface->errors);
285         INIT_LIST_HEAD(&iface->hotplug_list);
286
287         iface->main_dev.cb = interface_cb;
288         iface->l3_dev = &iface->main_dev;
289
290         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
291                       blob_data(config), blob_len(config));
292
293         if ((cur = tb[IFACE_ATTR_PROTO]))
294                 proto_name = blobmsg_data(cur);
295
296         proto_attach_interface(iface, proto_name);
297
298         if ((cur = tb[IFACE_ATTR_AUTO]))
299                 iface->autostart = blobmsg_get_bool(cur);
300         else
301                 iface->autostart = true;
302         iface->config_autostart = iface->autostart;
303 }
304
305 void
306 interface_add(struct interface *iface, struct blob_attr *config)
307 {
308         struct blob_attr *tb[IFACE_ATTR_MAX];
309         struct blob_attr *cur;
310
311         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
312                       blob_data(config), blob_len(config));
313
314         if ((cur = tb[IFACE_ATTR_IFNAME]))
315                 iface->ifname = blobmsg_data(cur);
316
317         iface->config = config;
318         vlist_add(&interfaces, &iface->node);
319 }
320
321 void
322 interface_remove_link(struct interface *iface, struct device *dev)
323 {
324         struct device *mdev = iface->main_dev.dev;
325
326         if (mdev && mdev->hotplug_ops) {
327                 mdev->hotplug_ops->del(mdev, dev);
328                 return;
329         }
330
331         device_remove_user(&iface->main_dev);
332 }
333
334 int
335 interface_add_link(struct interface *iface, struct device *dev)
336 {
337         struct device *mdev = iface->main_dev.dev;
338
339         if (mdev && mdev->hotplug_ops)
340                 return mdev->hotplug_ops->add(mdev, dev);
341
342         if (iface->main_dev.dev)
343                 interface_remove_link(iface, NULL);
344
345         device_add_user(&iface->main_dev, dev);
346
347         return 0;
348 }
349
350 int
351 interface_set_up(struct interface *iface)
352 {
353         iface->autostart = true;
354
355         if (!iface->available) {
356                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
357                 return -1;
358         }
359
360         if (iface->state != IFS_DOWN)
361                 return 0;
362
363         return __interface_set_up(iface);
364 }
365
366 int
367 interface_set_down(struct interface *iface)
368 {
369         if (!iface) {
370                 vlist_for_each_element(&interfaces, iface, node)
371                         __interface_set_down(iface, false);
372         } else {
373                 iface->autostart = false;
374                 __interface_set_down(iface, false);
375         }
376
377         return 0;
378 }
379
380 void
381 interface_start_pending(void)
382 {
383         struct interface *iface;
384
385         vlist_for_each_element(&interfaces, iface, node) {
386                 if (iface->available && iface->autostart)
387                         interface_set_up(iface);
388         }
389 }
390
391 static void
392 set_config_state(struct interface *iface, enum interface_config_state s)
393 {
394         iface->config_state = s;
395         if (iface->state == IFS_DOWN)
396                 interface_handle_config_change(iface);
397         else
398                 __interface_set_down(iface, false);
399 }
400
401 static void
402 interface_change_config(struct interface *if_old, struct interface *if_new)
403 {
404         struct blob_attr *old_config = if_old->config;
405         const char *old_ifname = if_old->ifname;
406         const struct proto_handler *proto = if_old->proto_handler;
407
408         interface_clear_errors(if_old);
409         if_old->config = if_new->config;
410         if (!if_old->config_autostart && if_new->config_autostart)
411                 if_old->autostart = true;
412
413         if_old->config_autostart = if_new->config_autostart;
414         if_old->ifname = if_new->ifname;
415         if_old->proto_handler = if_new->proto_handler;
416
417         if (strcmp(old_ifname, if_new->ifname) != 0 ||
418                 proto != if_new->proto_handler) {
419                 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
420                   if_old->name);
421                 goto reload;
422         }
423
424         if (!proto->config_params)
425                 D(INTERFACE, "No config parameters for interface '%s'\n",
426                   if_old->name);
427         else if (!config_check_equal(old_config, if_new->config,
428                                 proto->config_params)) {
429                 D(INTERFACE, "Reload interface '%s because of config changes\n",
430                   if_old->name);
431                 goto reload;
432         }
433
434         goto out;
435
436 reload:
437         set_config_state(if_old, IFC_RELOAD);
438 out:
439         free(old_config);
440         free(if_new);
441 }
442
443 static void
444 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
445                  struct vlist_node *node_old)
446 {
447         struct interface *if_old = container_of(node_old, struct interface, node);
448         struct interface *if_new = container_of(node_new, struct interface, node);
449
450         if (node_old && node_new) {
451                 D(INTERFACE, "Update interface '%s'\n", if_new->name);
452                 interface_change_config(if_old, if_new);
453         } else if (node_old) {
454                 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
455                 set_config_state(if_old, IFC_REMOVE);
456         } else if (node_new) {
457                 D(INTERFACE, "Create interface '%s'\n", if_new->name);
458                 interface_claim_device(if_new);
459                 proto_init_interface(if_new, if_new->config);
460                 interface_ip_init(if_new);
461                 netifd_ubus_add_interface(if_new);
462         }
463 }
464
465
466 static void __init
467 interface_init_list(void)
468 {
469         vlist_init(&interfaces, avl_strcmp, interface_update,
470                    struct interface, node, name);
471         interfaces.keep_old = true;
472         interfaces.no_delete = true;
473 }