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