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