move dns server/search list parsing to interface core to support peerdns=0 + static...
[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         iface->config_autostart = iface->autostart;
414 }
415
416 void
417 interface_add(struct interface *iface, struct blob_attr *config)
418 {
419         struct blob_attr *tb[IFACE_ATTR_MAX];
420         struct blob_attr *cur;
421
422         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
423                       blob_data(config), blob_len(config));
424
425         if ((cur = tb[IFACE_ATTR_IFNAME]))
426                 iface->ifname = blobmsg_data(cur);
427
428         iface->config = config;
429         vlist_add(&interfaces, &iface->node, iface->name);
430 }
431
432 void
433 interface_set_l3_dev(struct interface *iface, struct device *dev)
434 {
435         bool enabled = iface->config_ip.enabled;
436         bool claimed = iface->l3_dev.claimed;
437
438         if (iface->l3_dev.dev == dev)
439                 return;
440
441         interface_ip_set_enabled(&iface->config_ip, false);
442         interface_ip_flush(&iface->proto_ip);
443         device_add_user(&iface->l3_dev, dev);
444
445         if (dev) {
446                 if (claimed)
447                         device_claim(&iface->l3_dev);
448                 interface_ip_set_enabled(&iface->config_ip, enabled);
449         }
450 }
451
452 void
453 interface_set_main_dev(struct interface *iface, struct device *dev)
454 {
455         bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
456         bool claimed = iface->l3_dev.claimed;
457
458         if (iface->main_dev.dev == dev)
459                 return;
460
461         if (set_l3)
462                 interface_set_l3_dev(iface, dev);
463
464         device_add_user(&iface->main_dev, dev);
465         if (claimed)
466                 device_claim(&iface->l3_dev);
467
468         if (!iface->l3_dev.dev)
469                 interface_set_l3_dev(iface, dev);
470 }
471
472 int
473 interface_remove_link(struct interface *iface, struct device *dev)
474 {
475         struct device *mdev = iface->main_dev.dev;
476
477         if (mdev && mdev->hotplug_ops)
478                 return mdev->hotplug_ops->del(mdev, dev);
479
480         if (!iface->main_dev.hotplug)
481                 return UBUS_STATUS_INVALID_ARGUMENT;
482
483         if (dev != iface->main_dev.dev)
484                 return UBUS_STATUS_INVALID_ARGUMENT;
485
486         device_remove_user(&iface->main_dev);
487         return 0;
488 }
489
490 int
491 interface_add_link(struct interface *iface, struct device *dev)
492 {
493         struct device *mdev = iface->main_dev.dev;
494
495         if (mdev == dev)
496                 return 0;
497
498         if (iface->main_dev.hotplug)
499                 device_remove_user(&iface->main_dev);
500
501         if (mdev) {
502                 if (mdev->hotplug_ops)
503                         return mdev->hotplug_ops->add(mdev, dev);
504                 else
505                         return UBUS_STATUS_NOT_SUPPORTED;
506         }
507
508         interface_set_main_dev(iface, dev);
509         iface->main_dev.hotplug = true;
510         return 0;
511 }
512
513 int
514 interface_set_up(struct interface *iface)
515 {
516         int ret;
517
518         iface->autostart = true;
519
520         if (iface->state != IFS_DOWN)
521                 return 0;
522
523         interface_clear_errors(iface);
524         if (!iface->available) {
525                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
526                 return -1;
527         }
528
529         if (iface->main_dev.dev) {
530                 ret = device_claim(&iface->main_dev);
531                 if (ret)
532                         return ret;
533         }
534
535         iface->state = IFS_SETUP;
536         ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
537         if (ret) {
538                 mark_interface_down(iface);
539                 return ret;
540         }
541
542         return 0;
543 }
544
545 int
546 interface_set_down(struct interface *iface)
547 {
548         if (!iface) {
549                 vlist_for_each_element(&interfaces, iface, node)
550                         __interface_set_down(iface, false);
551         } else {
552                 iface->autostart = false;
553                 __interface_set_down(iface, false);
554         }
555
556         return 0;
557 }
558
559 void
560 interface_start_pending(void)
561 {
562         struct interface *iface;
563
564         vlist_for_each_element(&interfaces, iface, node) {
565                 if (iface->available && iface->autostart)
566                         interface_set_up(iface);
567         }
568 }
569
570 static void
571 set_config_state(struct interface *iface, enum interface_config_state s)
572 {
573         iface->config_state = s;
574         if (iface->state == IFS_DOWN)
575                 interface_handle_config_change(iface);
576         else
577                 __interface_set_down(iface, false);
578 }
579
580 void
581 interface_update_start(struct interface *iface)
582 {
583         interface_ip_update_start(&iface->proto_ip);
584 }
585
586 void
587 interface_update_complete(struct interface *iface)
588 {
589         interface_ip_update_complete(&iface->proto_ip);
590 }
591
592 static void
593 interface_replace_dns(struct interface_ip_settings *old, struct interface_ip_settings *new)
594 {
595         vlist_simple_replace(&new->dns_servers, &old->dns_servers);
596         vlist_simple_replace(&new->dns_search, &old->dns_search);
597 }
598
599 static void
600 interface_change_config(struct interface *if_old, struct interface *if_new)
601 {
602         struct blob_attr *old_config = if_old->config;
603         const char *old_ifname = if_old->ifname;
604         const struct proto_handler *proto = if_old->proto_handler;
605
606         interface_clear_errors(if_old);
607         if_old->config = if_new->config;
608         if (!if_old->config_autostart && if_new->config_autostart)
609                 if_old->autostart = true;
610
611         if_old->config_autostart = if_new->config_autostart;
612         if_old->ifname = if_new->ifname;
613         if_old->proto_handler = if_new->proto_handler;
614
615         if ((!!old_ifname != !!if_new->ifname) ||
616             (old_ifname && strcmp(old_ifname, if_new->ifname) != 0) ||
617             proto != if_new->proto_handler) {
618                 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
619                   if_old->name);
620                 goto reload;
621         }
622
623         if (!proto->config_params)
624                 D(INTERFACE, "No config parameters for interface '%s'\n",
625                   if_old->name);
626         else if (!config_check_equal(old_config, if_new->config,
627                                 proto->config_params)) {
628                 D(INTERFACE, "Reload interface '%s because of config changes\n",
629                   if_old->name);
630                 goto reload;
631         }
632
633 #define UPDATE(field) ({                                                \
634                 bool __changed = (if_old->field != if_new->field);      \
635                 if_old->field = if_new->field;                          \
636                 __changed;                                              \
637         })
638
639         if (UPDATE(metric) || UPDATE(proto_ip.no_defaultroute)) {
640                 interface_ip_set_enabled(&if_old->config_ip, false);
641                 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
642                 interface_ip_set_enabled(&if_old->proto_ip, false);
643                 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
644         }
645
646         UPDATE(proto_ip.no_dns);
647         interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
648         interface_replace_dns(&if_old->proto_ip, &if_new->proto_ip);
649         interface_write_resolv_conf();
650
651 #undef UPDATE
652
653         goto out;
654
655 reload:
656         set_config_state(if_old, IFC_RELOAD);
657 out:
658         free(old_config);
659         free(if_new);
660 }
661
662 static void
663 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
664                  struct vlist_node *node_old)
665 {
666         struct interface *if_old = container_of(node_old, struct interface, node);
667         struct interface *if_new = container_of(node_new, struct interface, node);
668
669         if (node_old && node_new) {
670                 D(INTERFACE, "Update interface '%s'\n", if_new->name);
671                 interface_change_config(if_old, if_new);
672         } else if (node_old) {
673                 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
674                 set_config_state(if_old, IFC_REMOVE);
675         } else if (node_new) {
676                 D(INTERFACE, "Create interface '%s'\n", if_new->name);
677                 proto_init_interface(if_new, if_new->config);
678                 interface_claim_device(if_new);
679                 netifd_ubus_add_interface(if_new);
680         }
681 }
682
683
684 static void __init
685 interface_init_list(void)
686 {
687         vlist_init(&interfaces, avl_strcmp, interface_update);
688         interfaces.keep_old = true;
689         interfaces.no_delete = true;
690 }