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