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