move l3 device tracking to interface core to enforce proper order of address/route...
[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 #include "system.h"
13
14 struct vlist_tree interfaces;
15
16 enum {
17         IFACE_ATTR_IFNAME,
18         IFACE_ATTR_PROTO,
19         IFACE_ATTR_AUTO,
20         IFACE_ATTR_DEFAULTROUTE,
21         IFACE_ATTR_METRIC,
22         IFACE_ATTR_MAX
23 };
24
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 },
29         [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
30         [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
31 };
32
33 const struct config_param_list interface_attr_list = {
34         .n_params = IFACE_ATTR_MAX,
35         .params = iface_attrs,
36 };
37
38 static void
39 interface_clear_errors(struct interface *iface)
40 {
41         struct interface_error *error, *tmp;
42
43         list_for_each_entry_safe(error, tmp, &iface->errors, list) {
44                 list_del(&error->list);
45                 free(error);
46         }
47 }
48
49 void interface_add_error(struct interface *iface, const char *subsystem,
50                          const char *code, const char **data, int n_data)
51 {
52         struct interface_error *error;
53         int i, len = 0;
54         int *datalen = NULL;
55         char *dest;
56
57         if (n_data) {
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;
62                         len += datalen[i];
63                 }
64         }
65
66         error = calloc(1, sizeof(*error) + sizeof(char *) + len);
67         if (!error)
68                 return;
69
70         list_add_tail(&error->list, &iface->errors);
71         error->subsystem = subsystem;
72         error->code = code;
73
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]);
78                 dest += datalen[i];
79         }
80         error->data[n_data] = NULL;
81 }
82
83 static void
84 interface_event(struct interface *iface, enum interface_event ev)
85 {
86         struct interface_user *dep, *tmp;
87
88         list_for_each_entry_safe(dep, tmp, &iface->users, list)
89                 dep->cb(dep, IFEV_UP);
90
91         interface_queue_event(iface, ev);
92 }
93
94 static void
95 interface_flush_state(struct interface *iface)
96 {
97         if (iface->main_dev.dev)
98                 device_release(&iface->main_dev);
99         if (iface->l3_dev.dev)
100                 device_release(&iface->l3_dev);
101 }
102
103 static void
104 mark_interface_down(struct interface *iface)
105 {
106         if (iface->state == IFS_UP)
107                 interface_event(iface, IFEV_DOWN);
108         interface_flush_state(iface);
109         iface->state = IFS_DOWN;
110 }
111
112 void
113 __interface_set_down(struct interface *iface, bool force)
114 {
115         interface_clear_errors(iface);
116
117         if (iface->state == IFS_DOWN ||
118                 iface->state == IFS_TEARDOWN)
119                 return;
120
121         if (iface->state == IFS_UP)
122                 interface_event(iface, IFEV_DOWN);
123         iface->state = IFS_TEARDOWN;
124         interface_ip_flush(&iface->config_ip);
125         interface_ip_flush(&iface->proto_ip);
126         interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
127         if (force)
128                 interface_flush_state(iface);
129 }
130
131 static void
132 interface_cb(struct device_user *dep, enum device_event ev)
133 {
134         struct interface *iface;
135         bool new_state;
136
137         iface = container_of(dep, struct interface, main_dev);
138         switch (ev) {
139         case DEV_EVENT_ADD:
140                 new_state = true;
141                 break;
142         case DEV_EVENT_REMOVE:
143                 new_state = false;
144                 break;
145         default:
146                 return;
147         }
148
149         interface_set_available(iface, new_state);
150 }
151
152 void
153 interface_set_available(struct interface *iface, bool new_state)
154 {
155         if (iface->available == new_state)
156                 return;
157
158         D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
159         iface->available = new_state;
160
161         if (new_state) {
162                 if (iface->autostart && !config_init)
163                         interface_set_up(iface);
164         } else
165                 __interface_set_down(iface, true);
166 }
167
168 void
169 interface_add_user(struct interface_user *dep, struct interface *iface)
170 {
171         dep->iface = iface;
172         list_add(&dep->list, &iface->users);
173         if (iface->state == IFS_UP)
174                 dep->cb(dep, IFEV_UP);
175 }
176
177 void
178 interface_remove_user(struct interface_user *dep)
179 {
180         list_del_init(&dep->list);
181         dep->iface = NULL;
182 }
183
184 static void
185 interface_claim_device(struct interface *iface)
186 {
187         struct device *dev;
188
189         if (iface->ifname &&
190                 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
191                 dev = device_get(iface->ifname, true);
192                 if (dev)
193                         interface_set_main_dev(iface, dev);
194         }
195         if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
196                 interface_set_available(iface, true);
197 }
198
199
200 static void
201 interface_cleanup(struct interface *iface, bool reload)
202 {
203         struct interface_user *dep, *tmp;
204
205         list_for_each_entry_safe(dep, tmp, &iface->users, list)
206                 interface_remove_user(dep);
207
208         interface_ip_flush(&iface->config_ip);
209         interface_flush_state(iface);
210         interface_clear_errors(iface);
211         if (iface->main_dev.dev &&
212             (!reload || !iface->main_dev.hotplug))
213                 interface_set_main_dev(iface, NULL);
214         interface_set_proto_state(iface, NULL);
215 }
216
217 static void
218 interface_do_free(struct interface *iface)
219 {
220         interface_cleanup(iface, false);
221         free(iface->config);
222         netifd_ubus_remove_interface(iface);
223         avl_delete(&interfaces.avl, &iface->node.avl);
224         free(iface);
225 }
226
227 static void
228 interface_do_reload(struct interface *iface)
229 {
230         interface_cleanup(iface, true);
231         proto_init_interface(iface, iface->config);
232         interface_claim_device(iface);
233 }
234
235 static void
236 interface_handle_config_change(struct interface *iface)
237 {
238         switch(iface->config_state) {
239         case IFC_NORMAL:
240                 break;
241         case IFC_RELOAD:
242                 interface_do_reload(iface);
243                 break;
244         case IFC_REMOVE:
245                 interface_do_free(iface);
246                 return;
247         }
248         if (iface->autostart && iface->available)
249                 interface_set_up(iface);
250 }
251
252 static void
253 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
254 {
255         struct interface *iface = state->iface;
256
257         switch (ev) {
258         case IFPEV_UP:
259                 if (iface->state != IFS_SETUP)
260                         return;
261
262                 interface_ip_set_enabled(&iface->config_ip, true);
263                 system_flush_routes();
264                 iface->state = IFS_UP;
265                 iface->start_time = system_get_rtime();
266                 interface_event(iface, IFEV_UP);
267                 interface_write_resolv_conf();
268                 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
269                 break;
270         case IFPEV_DOWN:
271                 if (iface->state == IFS_DOWN)
272                         return;
273
274                 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
275                 interface_ip_set_enabled(&iface->config_ip, false);
276                 system_flush_routes();
277                 mark_interface_down(iface);
278                 interface_handle_config_change(iface);
279                 break;
280         case IFPEV_LINK_LOST:
281                 if (iface->state != IFS_UP)
282                         return;
283
284                 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
285                 if (iface->state == IFS_UP)
286                         interface_event(iface, IFEV_DOWN);
287                 iface->state = IFS_SETUP;
288                 break;
289         }
290 }
291
292 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
293 {
294         if (iface->proto) {
295                 iface->proto->free(iface->proto);
296                 iface->proto = NULL;
297         }
298         iface->state = IFS_DOWN;
299         iface->proto = state;
300         if (!state)
301                 return;
302
303         state->proto_event = interface_proto_cb;
304         state->iface = iface;
305 }
306
307 void
308 interface_init(struct interface *iface, const char *name,
309                struct blob_attr *config)
310 {
311         struct blob_attr *tb[IFACE_ATTR_MAX];
312         struct blob_attr *cur;
313         const char *proto_name = NULL;
314
315         strncpy(iface->name, name, sizeof(iface->name) - 1);
316         INIT_LIST_HEAD(&iface->errors);
317         INIT_LIST_HEAD(&iface->users);
318         INIT_LIST_HEAD(&iface->hotplug_list);
319         interface_ip_init(&iface->proto_ip, iface);
320         interface_ip_init(&iface->config_ip, iface);
321         iface->config_ip.enabled = false;
322
323         iface->main_dev.cb = interface_cb;
324
325         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
326                       blob_data(config), blob_len(config));
327
328         if ((cur = tb[IFACE_ATTR_PROTO]))
329                 proto_name = blobmsg_data(cur);
330
331         proto_attach_interface(iface, proto_name);
332
333         iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
334         iface->proto_ip.no_defaultroute =
335                 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
336
337         iface->config_autostart = iface->autostart;
338 }
339
340 void
341 interface_add(struct interface *iface, struct blob_attr *config)
342 {
343         struct blob_attr *tb[IFACE_ATTR_MAX];
344         struct blob_attr *cur;
345
346         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
347                       blob_data(config), blob_len(config));
348
349         if ((cur = tb[IFACE_ATTR_IFNAME]))
350                 iface->ifname = blobmsg_data(cur);
351
352         iface->config = config;
353         vlist_add(&interfaces, &iface->node, iface->name);
354 }
355
356 void
357 interface_set_l3_dev(struct interface *iface, struct device *dev)
358 {
359         bool enabled = iface->config_ip.enabled;
360         bool claimed = iface->l3_dev.claimed;
361
362         if (iface->l3_dev.dev == dev)
363                 return;
364
365         interface_ip_set_enabled(&iface->config_ip, false);
366         interface_ip_flush(&iface->proto_ip);
367         device_add_user(&iface->l3_dev, dev);
368
369         if (dev) {
370                 if (claimed)
371                         device_claim(&iface->l3_dev);
372                 interface_ip_set_enabled(&iface->config_ip, enabled);
373         }
374 }
375
376 void
377 interface_set_main_dev(struct interface *iface, struct device *dev)
378 {
379         bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
380         bool claimed = iface->l3_dev.claimed;
381
382         if (iface->main_dev.dev == dev)
383                 return;
384
385         device_add_user(&iface->main_dev, dev);
386         if (set_l3)
387                 interface_set_l3_dev(iface, dev);
388
389         if (claimed)
390                 device_claim(&iface->l3_dev);
391
392         if (!iface->l3_dev.dev)
393                 interface_set_l3_dev(iface, dev);
394 }
395
396 int
397 interface_remove_link(struct interface *iface, struct device *dev)
398 {
399         struct device *mdev = iface->main_dev.dev;
400
401         if (mdev && mdev->hotplug_ops)
402                 return mdev->hotplug_ops->del(mdev, dev);
403
404         if (!iface->main_dev.hotplug)
405                 return UBUS_STATUS_INVALID_ARGUMENT;
406
407         if (dev != iface->main_dev.dev)
408                 return UBUS_STATUS_INVALID_ARGUMENT;
409
410         device_remove_user(&iface->main_dev);
411         return 0;
412 }
413
414 int
415 interface_add_link(struct interface *iface, struct device *dev)
416 {
417         struct device *mdev = iface->main_dev.dev;
418
419         if (mdev == dev)
420                 return 0;
421
422         if (iface->main_dev.hotplug)
423                 device_remove_user(&iface->main_dev);
424
425         if (mdev) {
426                 if (mdev->hotplug_ops)
427                         return mdev->hotplug_ops->add(mdev, dev);
428                 else
429                         return UBUS_STATUS_NOT_SUPPORTED;
430         }
431
432         interface_set_main_dev(iface, dev);
433         iface->main_dev.hotplug = true;
434         return 0;
435 }
436
437 int
438 interface_set_up(struct interface *iface)
439 {
440         int ret;
441
442         iface->autostart = true;
443
444         if (iface->state != IFS_DOWN)
445                 return 0;
446
447         interface_clear_errors(iface);
448         if (!iface->available) {
449                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
450                 return -1;
451         }
452
453         if (iface->main_dev.dev) {
454                 ret = device_claim(&iface->main_dev);
455                 if (ret)
456                         return ret;
457         }
458
459         iface->state = IFS_SETUP;
460         ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
461         if (ret) {
462                 mark_interface_down(iface);
463                 return ret;
464         }
465
466         return 0;
467 }
468
469 int
470 interface_set_down(struct interface *iface)
471 {
472         if (!iface) {
473                 vlist_for_each_element(&interfaces, iface, node)
474                         __interface_set_down(iface, false);
475         } else {
476                 iface->autostart = false;
477                 __interface_set_down(iface, false);
478         }
479
480         return 0;
481 }
482
483 void
484 interface_start_pending(void)
485 {
486         struct interface *iface;
487
488         vlist_for_each_element(&interfaces, iface, node) {
489                 if (iface->available && iface->autostart)
490                         interface_set_up(iface);
491         }
492 }
493
494 static void
495 set_config_state(struct interface *iface, enum interface_config_state s)
496 {
497         iface->config_state = s;
498         if (iface->state == IFS_DOWN)
499                 interface_handle_config_change(iface);
500         else
501                 __interface_set_down(iface, false);
502 }
503
504 void
505 interface_update_start(struct interface *iface)
506 {
507         interface_ip_update_start(&iface->proto_ip);
508 }
509
510 void
511 interface_update_complete(struct interface *iface)
512 {
513         struct device_route *route;
514
515         interface_ip_update_complete(&iface->proto_ip);
516         vlist_for_each_element(&iface->config_ip.route, route, node) {
517                 if (iface->l3_dev.dev) {
518                         system_add_route(iface->l3_dev.dev, route);
519                         route->enabled = true;
520                 }
521         }
522 }
523
524 static void
525 interface_change_config(struct interface *if_old, struct interface *if_new)
526 {
527         struct blob_attr *old_config = if_old->config;
528         const char *old_ifname = if_old->ifname;
529         const struct proto_handler *proto = if_old->proto_handler;
530
531         interface_clear_errors(if_old);
532         if_old->config = if_new->config;
533         if (!if_old->config_autostart && if_new->config_autostart)
534                 if_old->autostart = true;
535
536         if_old->config_autostart = if_new->config_autostart;
537         if_old->ifname = if_new->ifname;
538         if_old->proto_handler = if_new->proto_handler;
539
540         if ((!!old_ifname != !!if_new->ifname) ||
541             (old_ifname && strcmp(old_ifname, if_new->ifname) != 0) ||
542             proto != if_new->proto_handler) {
543                 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
544                   if_old->name);
545                 goto reload;
546         }
547
548         if (!proto->config_params)
549                 D(INTERFACE, "No config parameters for interface '%s'\n",
550                   if_old->name);
551         else if (!config_check_equal(old_config, if_new->config,
552                                 proto->config_params)) {
553                 D(INTERFACE, "Reload interface '%s because of config changes\n",
554                   if_old->name);
555                 goto reload;
556         }
557
558 #define UPDATE(field) ({                                                \
559                 bool __changed = (if_old->field != if_new->field);      \
560                 if_old->field = if_new->field;                          \
561                 __changed;                                              \
562         })
563
564         if (UPDATE(metric) || UPDATE(proto_ip.no_defaultroute)) {
565                 interface_ip_set_enabled(&if_old->config_ip, false);
566                 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
567                 interface_ip_set_enabled(&if_old->proto_ip, false);
568                 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
569         }
570
571 #undef UPDATE
572
573         goto out;
574
575 reload:
576         set_config_state(if_old, IFC_RELOAD);
577 out:
578         free(old_config);
579         free(if_new);
580 }
581
582 static void
583 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
584                  struct vlist_node *node_old)
585 {
586         struct interface *if_old = container_of(node_old, struct interface, node);
587         struct interface *if_new = container_of(node_new, struct interface, node);
588
589         if (node_old && node_new) {
590                 D(INTERFACE, "Update interface '%s'\n", if_new->name);
591                 interface_change_config(if_old, if_new);
592         } else if (node_old) {
593                 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
594                 set_config_state(if_old, IFC_REMOVE);
595         } else if (node_new) {
596                 D(INTERFACE, "Create interface '%s'\n", if_new->name);
597                 proto_init_interface(if_new, if_new->config);
598                 interface_claim_device(if_new);
599                 netifd_ubus_add_interface(if_new);
600         }
601 }
602
603
604 static void __init
605 interface_init_list(void)
606 {
607         vlist_init(&interfaces, avl_strcmp, interface_update);
608         interfaces.keep_old = true;
609         interfaces.no_delete = true;
610 }