ubus: show proto handler in interface status
[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 }
219
220 void
221 interface_set_available(struct interface *iface, bool new_state)
222 {
223 if (iface->available == new_state)
224 return;
225
226 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
227 iface->available = new_state;
228
229 if (new_state) {
230 if (iface->autostart && !config_init)
231 interface_set_up(iface);
232 } else
233 __interface_set_down(iface, true);
234 }
235
236 void
237 interface_add_user(struct interface_user *dep, struct interface *iface)
238 {
239 if (!iface) {
240 list_add(&dep->list, &iface_all_users);
241 return;
242 }
243
244 dep->iface = iface;
245 list_add(&dep->list, &iface->users);
246 if (iface->state == IFS_UP)
247 dep->cb(dep, iface, IFEV_UP);
248 }
249
250 void
251 interface_remove_user(struct interface_user *dep)
252 {
253 list_del_init(&dep->list);
254 dep->iface = NULL;
255 }
256
257 static void
258 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
259 {
260 struct interface *alias = container_of(dep, struct interface, parent_iface);
261 struct device *dev = iface->l3_dev.dev;
262
263 switch (ev) {
264 case IFEV_UP:
265 if (!dev)
266 return;
267
268 interface_set_main_dev(alias, dev);
269 interface_set_available(alias, true);
270 break;
271 case IFEV_DOWN:
272 interface_set_available(alias, false);
273 interface_set_main_dev(alias, NULL);
274 break;
275 case IFEV_FREE:
276 interface_remove_user(dep);
277 break;
278 case IFEV_RELOAD:
279 break;
280 }
281 }
282
283 static void
284 interface_claim_device(struct interface *iface)
285 {
286 struct interface *parent;
287 struct device *dev = NULL;
288
289 if (iface->parent_iface.iface)
290 interface_remove_user(&iface->parent_iface);
291
292 if (iface->parent_ifname) {
293 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
294 iface->parent_iface.cb = interface_alias_cb;
295 interface_add_user(&iface->parent_iface, parent);
296 } else if (iface->ifname &&
297 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
298 dev = device_get(iface->ifname, true);
299 }
300
301 if (dev)
302 interface_set_main_dev(iface, dev);
303
304 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
305 interface_set_available(iface, true);
306 }
307
308
309 static void
310 interface_cleanup(struct interface *iface, bool reload)
311 {
312 struct interface_user *dep, *tmp;
313
314 if (iface->parent_iface.iface)
315 interface_remove_user(&iface->parent_iface);
316
317 list_for_each_entry_safe(dep, tmp, &iface->users, list)
318 interface_remove_user(dep);
319
320 interface_ip_flush(&iface->config_ip);
321 interface_flush_state(iface);
322 interface_clear_errors(iface);
323
324 if (iface->main_dev.dev && !reload)
325 interface_set_main_dev(iface, NULL);
326 interface_set_proto_state(iface, NULL);
327 }
328
329 static void
330 interface_do_free(struct interface *iface)
331 {
332 interface_event(iface, IFEV_FREE);
333 interface_cleanup(iface, false);
334 free(iface->config);
335 netifd_ubus_remove_interface(iface);
336 avl_delete(&interfaces.avl, &iface->node.avl);
337 free(iface);
338 }
339
340 static void
341 interface_do_reload(struct interface *iface)
342 {
343 interface_event(iface, IFEV_RELOAD);
344 interface_cleanup(iface, true);
345 proto_init_interface(iface, iface->config);
346 interface_claim_device(iface);
347 }
348
349 static void
350 interface_handle_config_change(struct interface *iface)
351 {
352 enum interface_config_state state = iface->config_state;
353
354 iface->config_state = IFC_NORMAL;
355 switch(state) {
356 case IFC_NORMAL:
357 break;
358 case IFC_RELOAD:
359 interface_do_reload(iface);
360 break;
361 case IFC_REMOVE:
362 interface_do_free(iface);
363 return;
364 }
365 if (iface->autostart && iface->available)
366 interface_set_up(iface);
367 }
368
369 static void
370 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
371 {
372 struct interface *iface = state->iface;
373
374 switch (ev) {
375 case IFPEV_UP:
376 if (iface->state != IFS_SETUP)
377 return;
378
379 interface_ip_set_enabled(&iface->config_ip, true);
380 system_flush_routes();
381 iface->state = IFS_UP;
382 iface->start_time = system_get_rtime();
383 interface_event(iface, IFEV_UP);
384 interface_write_resolv_conf();
385 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
386 break;
387 case IFPEV_DOWN:
388 if (iface->state == IFS_DOWN)
389 return;
390
391 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
392 mark_interface_down(iface);
393 interface_handle_config_change(iface);
394 break;
395 case IFPEV_LINK_LOST:
396 if (iface->state != IFS_UP)
397 return;
398
399 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
400 mark_interface_down(iface);
401 iface->state = IFS_SETUP;
402 break;
403 }
404 }
405
406 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
407 {
408 if (iface->proto) {
409 iface->proto->free(iface->proto);
410 iface->proto = NULL;
411 }
412 iface->state = IFS_DOWN;
413 iface->proto = state;
414 if (!state)
415 return;
416
417 state->proto_event = interface_proto_cb;
418 state->iface = iface;
419 }
420
421 void
422 interface_init(struct interface *iface, const char *name,
423 struct blob_attr *config)
424 {
425 struct blob_attr *tb[IFACE_ATTR_MAX];
426 struct blob_attr *cur;
427 const char *proto_name = NULL;
428
429 strncpy(iface->name, name, sizeof(iface->name) - 1);
430 INIT_LIST_HEAD(&iface->errors);
431 INIT_LIST_HEAD(&iface->users);
432 INIT_LIST_HEAD(&iface->hotplug_list);
433 interface_ip_init(iface);
434 avl_init(&iface->data, avl_strcmp, false, NULL);
435 iface->config_ip.enabled = false;
436
437 iface->main_dev.cb = interface_cb;
438
439 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
440 blob_data(config), blob_len(config));
441
442 if ((cur = tb[IFACE_ATTR_PROTO]))
443 proto_name = blobmsg_data(cur);
444
445 proto_attach_interface(iface, proto_name);
446
447 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
448 iface->proto_ip.no_defaultroute =
449 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
450 iface->proto_ip.no_dns =
451 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
452
453 if ((cur = tb[IFACE_ATTR_DNS]))
454 interface_add_dns_server_list(&iface->config_ip, cur);
455
456 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
457 interface_add_dns_search_list(&iface->config_ip, cur);
458
459 if ((cur = tb[IFACE_ATTR_METRIC]))
460 iface->metric = blobmsg_get_u32(cur);
461
462 iface->config_autostart = iface->autostart;
463 }
464
465 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
466 {
467 struct blob_attr *tb[IFACE_ATTR_MAX];
468 struct blob_attr *cur;
469
470 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
471 blob_data(config), blob_len(config));
472
473 if (alias) {
474 if ((cur = tb[IFACE_ATTR_INTERFACE]))
475 iface->parent_ifname = blobmsg_data(cur);
476
477 if (!iface->parent_ifname)
478 return false;
479 } else {
480 if ((cur = tb[IFACE_ATTR_IFNAME]))
481 iface->ifname = blobmsg_data(cur);
482 }
483
484
485 iface->config = config;
486 vlist_add(&interfaces, &iface->node, iface->name);
487 return true;
488 }
489
490 void
491 interface_add(struct interface *iface, struct blob_attr *config)
492 {
493 __interface_add(iface, config, false);
494 }
495
496 bool
497 interface_add_alias(struct interface *iface, struct blob_attr *config)
498 {
499 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
500 return false;
501
502 return __interface_add(iface, config, true);
503 }
504
505 void
506 interface_set_l3_dev(struct interface *iface, struct device *dev)
507 {
508 bool enabled = iface->config_ip.enabled;
509 bool claimed = iface->l3_dev.claimed;
510
511 if (iface->l3_dev.dev == dev)
512 return;
513
514 interface_ip_set_enabled(&iface->config_ip, false);
515 interface_ip_flush(&iface->proto_ip);
516 device_add_user(&iface->l3_dev, dev);
517
518 if (dev) {
519 if (claimed)
520 device_claim(&iface->l3_dev);
521 interface_ip_set_enabled(&iface->config_ip, enabled);
522 }
523 }
524
525 void
526 interface_set_main_dev(struct interface *iface, struct device *dev)
527 {
528 bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
529 bool claimed = iface->l3_dev.claimed;
530
531 if (iface->main_dev.dev == dev)
532 return;
533
534 if (set_l3)
535 interface_set_l3_dev(iface, dev);
536
537 device_add_user(&iface->main_dev, dev);
538 if (claimed)
539 device_claim(&iface->l3_dev);
540
541 if (!iface->l3_dev.dev)
542 interface_set_l3_dev(iface, dev);
543 }
544
545 int
546 interface_remove_link(struct interface *iface, struct device *dev)
547 {
548 struct device *mdev = iface->main_dev.dev;
549
550 if (mdev && mdev->hotplug_ops)
551 return mdev->hotplug_ops->del(mdev, dev);
552
553 if (!iface->main_dev.hotplug)
554 return UBUS_STATUS_INVALID_ARGUMENT;
555
556 if (dev != iface->main_dev.dev)
557 return UBUS_STATUS_INVALID_ARGUMENT;
558
559 device_remove_user(&iface->main_dev);
560 return 0;
561 }
562
563 int
564 interface_add_link(struct interface *iface, struct device *dev)
565 {
566 struct device *mdev = iface->main_dev.dev;
567
568 if (mdev == dev)
569 return 0;
570
571 if (iface->main_dev.hotplug)
572 device_remove_user(&iface->main_dev);
573
574 if (mdev) {
575 if (mdev->hotplug_ops)
576 return mdev->hotplug_ops->add(mdev, dev);
577 else
578 return UBUS_STATUS_NOT_SUPPORTED;
579 }
580
581 interface_set_main_dev(iface, dev);
582 iface->main_dev.hotplug = true;
583 return 0;
584 }
585
586 int
587 interface_set_up(struct interface *iface)
588 {
589 int ret;
590
591 iface->autostart = true;
592
593 if (iface->state != IFS_DOWN)
594 return 0;
595
596 interface_clear_errors(iface);
597 if (!iface->available) {
598 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
599 return -1;
600 }
601
602 if (iface->main_dev.dev) {
603 ret = device_claim(&iface->main_dev);
604 if (ret)
605 return ret;
606 }
607
608 iface->state = IFS_SETUP;
609 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
610 if (ret) {
611 mark_interface_down(iface);
612 return ret;
613 }
614
615 return 0;
616 }
617
618 int
619 interface_set_down(struct interface *iface)
620 {
621 if (!iface) {
622 vlist_for_each_element(&interfaces, iface, node)
623 __interface_set_down(iface, false);
624 } else {
625 iface->autostart = false;
626 __interface_set_down(iface, false);
627 }
628
629 return 0;
630 }
631
632 void
633 interface_start_pending(void)
634 {
635 struct interface *iface;
636
637 vlist_for_each_element(&interfaces, iface, node) {
638 if (iface->available && iface->autostart)
639 interface_set_up(iface);
640 }
641 }
642
643 static void
644 set_config_state(struct interface *iface, enum interface_config_state s)
645 {
646 iface->config_state = s;
647 if (iface->state == IFS_DOWN)
648 interface_handle_config_change(iface);
649 else
650 __interface_set_down(iface, false);
651 }
652
653 void
654 interface_update_start(struct interface *iface)
655 {
656 interface_ip_update_start(&iface->proto_ip);
657 }
658
659 void
660 interface_update_complete(struct interface *iface)
661 {
662 interface_ip_update_complete(&iface->proto_ip);
663 }
664
665 static void
666 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
667 {
668 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
669 vlist_simple_replace(&new->dns_search, &old->dns_search);
670 }
671
672 static void
673 interface_change_config(struct interface *if_old, struct interface *if_new)
674 {
675 struct blob_attr *old_config = if_old->config;
676 const char *old_ifname = if_old->ifname;
677 const char *old_parent_ifname = if_old->parent_ifname;
678 const struct proto_handler *proto = if_old->proto_handler;
679
680 interface_clear_errors(if_old);
681 if_old->config = if_new->config;
682 if (!if_old->config_autostart && if_new->config_autostart)
683 if_old->autostart = true;
684
685 if_old->config_autostart = if_new->config_autostart;
686 if_old->ifname = if_new->ifname;
687 if_old->parent_ifname = if_new->parent_ifname;
688 if_old->proto_handler = if_new->proto_handler;
689
690 #define FIELD_CHANGED_STR(field) \
691 ((!!if_old->field != !!old_ ## field) || \
692 (old_ ## field && \
693 strcmp(old_ ## field, if_old->field) != 0))
694
695 if (FIELD_CHANGED_STR(parent_ifname)) {
696 if (if_old->parent_iface.iface)
697 interface_remove_user(&if_old->parent_iface);
698 goto reload;
699 }
700
701 if (FIELD_CHANGED_STR(ifname) || proto != if_new->proto_handler) {
702 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
703 if_old->name);
704 goto reload;
705 }
706
707 if (!proto->config_params)
708 D(INTERFACE, "No config parameters for interface '%s'\n",
709 if_old->name);
710 else if (!config_check_equal(old_config, if_new->config,
711 proto->config_params)) {
712 D(INTERFACE, "Reload interface '%s because of config changes\n",
713 if_old->name);
714 goto reload;
715 }
716
717 #define UPDATE(field) ({ \
718 bool __changed = (if_old->field != if_new->field); \
719 if_old->field = if_new->field; \
720 __changed; \
721 })
722
723 if (UPDATE(metric) || UPDATE(proto_ip.no_defaultroute)) {
724 interface_ip_set_enabled(&if_old->config_ip, false);
725 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
726 interface_ip_set_enabled(&if_old->proto_ip, false);
727 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
728 }
729
730 UPDATE(proto_ip.no_dns);
731 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
732 interface_write_resolv_conf();
733
734 #undef UPDATE
735
736 goto out;
737
738 reload:
739 set_config_state(if_old, IFC_RELOAD);
740 out:
741 if_new->config = NULL;
742 interface_cleanup(if_new, false);
743 free(old_config);
744 free(if_new);
745 }
746
747 static void
748 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
749 struct vlist_node *node_old)
750 {
751 struct interface *if_old = container_of(node_old, struct interface, node);
752 struct interface *if_new = container_of(node_new, struct interface, node);
753
754 if (node_old && node_new) {
755 D(INTERFACE, "Update interface '%s'\n", if_new->name);
756 interface_change_config(if_old, if_new);
757 } else if (node_old) {
758 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
759 set_config_state(if_old, IFC_REMOVE);
760 } else if (node_new) {
761 D(INTERFACE, "Create interface '%s'\n", if_new->name);
762 proto_init_interface(if_new, if_new->config);
763 interface_claim_device(if_new);
764 netifd_ubus_add_interface(if_new);
765 }
766 }
767
768
769 static void __init
770 interface_init_list(void)
771 {
772 vlist_init(&interfaces, avl_strcmp, interface_update);
773 interfaces.keep_old = true;
774 interfaces.no_delete = true;
775 }