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