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