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