66e0e80d72ba027f194210b3610c486709c80b7a
[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 #include <sys/types.h>
18 #include <sys/wait.h>
19
20 #include "netifd.h"
21 #include "device.h"
22 #include "interface.h"
23 #include "interface-ip.h"
24 #include "proto.h"
25 #include "ubus.h"
26 #include "config.h"
27 #include "system.h"
28
29 struct vlist_tree interfaces;
30 static LIST_HEAD(iface_all_users);
31
32 enum {
33 IFACE_ATTR_DEVICE,
34 IFACE_ATTR_IFNAME, /* Backward compatibility */
35 IFACE_ATTR_PROTO,
36 IFACE_ATTR_AUTO,
37 IFACE_ATTR_ZONE,
38 IFACE_ATTR_JAIL,
39 IFACE_ATTR_JAIL_DEVICE,
40 IFACE_ATTR_JAIL_IFNAME,
41 IFACE_ATTR_HOST_DEVICE,
42 IFACE_ATTR_DEFAULTROUTE,
43 IFACE_ATTR_PEERDNS,
44 IFACE_ATTR_DNS,
45 IFACE_ATTR_DNS_SEARCH,
46 IFACE_ATTR_DNS_METRIC,
47 IFACE_ATTR_METRIC,
48 IFACE_ATTR_INTERFACE,
49 IFACE_ATTR_IP6ASSIGN,
50 IFACE_ATTR_IP6HINT,
51 IFACE_ATTR_IP4TABLE,
52 IFACE_ATTR_IP6TABLE,
53 IFACE_ATTR_IP6CLASS,
54 IFACE_ATTR_DELEGATE,
55 IFACE_ATTR_IP6IFACEID,
56 IFACE_ATTR_FORCE_LINK,
57 IFACE_ATTR_IP6WEIGHT,
58 IFACE_ATTR_MAX
59 };
60
61 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
62 [IFACE_ATTR_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
63 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
64 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
65 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
66 [IFACE_ATTR_ZONE] = { .name = "zone", .type = BLOBMSG_TYPE_STRING },
67 [IFACE_ATTR_JAIL] = { .name = "jail", .type = BLOBMSG_TYPE_STRING },
68 [IFACE_ATTR_JAIL_DEVICE] = { .name = "jail_device", .type = BLOBMSG_TYPE_STRING },
69 [IFACE_ATTR_JAIL_IFNAME] = { .name = "jail_ifname", .type = BLOBMSG_TYPE_STRING },
70 [IFACE_ATTR_HOST_DEVICE] = { .name = "host_device", .type = BLOBMSG_TYPE_STRING },
71 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
72 [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
73 [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
74 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
75 [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
76 [IFACE_ATTR_DNS_METRIC] = { .name = "dns_metric", .type = BLOBMSG_TYPE_INT32 },
77 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
78 [IFACE_ATTR_IP6ASSIGN] = { .name = "ip6assign", .type = BLOBMSG_TYPE_INT32 },
79 [IFACE_ATTR_IP6HINT] = { .name = "ip6hint", .type = BLOBMSG_TYPE_STRING },
80 [IFACE_ATTR_IP4TABLE] = { .name = "ip4table", .type = BLOBMSG_TYPE_STRING },
81 [IFACE_ATTR_IP6TABLE] = { .name = "ip6table", .type = BLOBMSG_TYPE_STRING },
82 [IFACE_ATTR_IP6CLASS] = { .name = "ip6class", .type = BLOBMSG_TYPE_ARRAY },
83 [IFACE_ATTR_DELEGATE] = { .name = "delegate", .type = BLOBMSG_TYPE_BOOL },
84 [IFACE_ATTR_IP6IFACEID] = { .name = "ip6ifaceid", .type = BLOBMSG_TYPE_STRING },
85 [IFACE_ATTR_FORCE_LINK] = { .name = "force_link", .type = BLOBMSG_TYPE_BOOL },
86 [IFACE_ATTR_IP6WEIGHT] = { .name = "ip6weight", .type = BLOBMSG_TYPE_INT32 },
87 };
88
89 const struct uci_blob_param_list interface_attr_list = {
90 .n_params = IFACE_ATTR_MAX,
91 .params = iface_attrs,
92 };
93
94 static void
95 interface_set_main_dev(struct interface *iface, struct device *dev);
96 static void
97 interface_event(struct interface *iface, enum interface_event ev);
98
99 static void
100 interface_error_flush(struct interface *iface)
101 {
102 struct interface_error *error, *tmp;
103
104 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
105 list_del(&error->list);
106 free(error);
107 }
108 }
109
110 static bool
111 interface_force_link(struct interface *iface)
112 {
113 struct device *dev = iface->main_dev.dev;
114
115 if (dev && dev->settings.auth)
116 return false;
117
118 return iface->force_link;
119 }
120
121 static void
122 interface_clear_errors(struct interface *iface)
123 {
124 /* don't flush the errors in case the configured protocol handler matches the
125 running protocol handler and is having the last error capability */
126 if (!(iface->proto &&
127 (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
128 (iface->proto->handler->name == iface->proto_handler->name)))
129 interface_error_flush(iface);
130 }
131
132 void interface_add_error(struct interface *iface, const char *subsystem,
133 const char *code, const char **data, int n_data)
134 {
135 struct interface_error *error;
136 int i, len = 0;
137 int *datalen = NULL;
138 char *dest, *d_subsys, *d_code;
139
140 /* if the configured protocol handler has the last error support capability,
141 errors should only be added if the running protocol handler matches the
142 configured one */
143 if (iface->proto &&
144 (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
145 (iface->proto->handler->name != iface->proto_handler->name))
146 return;
147
148 if (n_data) {
149 len = n_data * sizeof(char *);
150 datalen = alloca(len);
151 for (i = 0; i < n_data; i++) {
152 datalen[i] = strlen(data[i]) + 1;
153 len += datalen[i];
154 }
155 }
156
157 error = calloc_a(sizeof(*error) + sizeof(char *) + len,
158 &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
159 &d_code, code ? strlen(code) + 1 : 0);
160 if (!error)
161 return;
162
163 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
164 protocol can't be established (e.g auth failure) */
165 if (iface->proto_handler->flags & PROTO_FLAG_LASTERROR)
166 interface_error_flush(iface);
167
168 list_add_tail(&error->list, &iface->errors);
169
170 dest = (char *) &error->data[n_data + 1];
171 for (i = 0; i < n_data; i++) {
172 error->data[i] = dest;
173 memcpy(dest, data[i], datalen[i]);
174 dest += datalen[i];
175 }
176 error->data[n_data] = NULL;
177
178 if (subsystem)
179 error->subsystem = strcpy(d_subsys, subsystem);
180
181 if (code)
182 error->code = strcpy(d_code, code);
183 }
184
185 static void
186 interface_data_del(struct interface *iface, struct interface_data *data)
187 {
188 avl_delete(&iface->data, &data->node);
189 free(data);
190 }
191
192 static void
193 interface_data_flush(struct interface *iface)
194 {
195 struct interface_data *d, *tmp;
196
197 avl_for_each_element_safe(&iface->data, d, node, tmp)
198 interface_data_del(iface, d);
199 }
200
201 int
202 interface_add_data(struct interface *iface, const struct blob_attr *data)
203 {
204 struct interface_data *n, *o;
205
206 if (!blobmsg_check_attr(data, true))
207 return UBUS_STATUS_INVALID_ARGUMENT;
208
209 const char *name = blobmsg_name(data);
210 unsigned len = blob_pad_len(data);
211
212 o = avl_find_element(&iface->data, name, o, node);
213 if (o) {
214 if (blob_pad_len(o->data) == len && !memcmp(o->data, data, len))
215 return 0;
216
217 interface_data_del(iface, o);
218 }
219
220 n = calloc(1, sizeof(*n) + len);
221 if (!n)
222 return UBUS_STATUS_UNKNOWN_ERROR;
223
224 memcpy(n->data, data, len);
225 n->node.key = blobmsg_name(n->data);
226 avl_insert(&iface->data, &n->node);
227
228 iface->updated |= IUF_DATA;
229 return 0;
230 }
231
232 int interface_parse_data(struct interface *iface, const struct blob_attr *attr)
233 {
234 struct blob_attr *cur;
235 size_t rem;
236 int ret;
237
238 iface->updated = 0;
239
240 blob_for_each_attr(cur, attr, rem) {
241 ret = interface_add_data(iface, cur);
242 if (ret)
243 return ret;
244 }
245
246 if (iface->updated && iface->state == IFS_UP)
247 interface_event(iface, IFEV_UPDATE);
248
249 return 0;
250 }
251
252 static void
253 interface_event(struct interface *iface, enum interface_event ev)
254 {
255 struct interface_user *dep, *tmp;
256 struct device *adev = NULL;
257
258 list_for_each_entry_safe(dep, tmp, &iface->users, list)
259 dep->cb(dep, iface, ev);
260
261 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
262 dep->cb(dep, iface, ev);
263
264 switch (ev) {
265 case IFEV_UP:
266 interface_error_flush(iface);
267 adev = iface->l3_dev.dev;
268 fallthrough;
269 case IFEV_DOWN:
270 case IFEV_UP_FAILED:
271 alias_notify_device(iface->name, adev);
272 break;
273 default:
274 break;
275 }
276 }
277
278 static void
279 interface_flush_state(struct interface *iface)
280 {
281 if (iface->l3_dev.dev)
282 device_release(&iface->l3_dev);
283 interface_data_flush(iface);
284 }
285
286 static void
287 mark_interface_down(struct interface *iface)
288 {
289 enum interface_state state = iface->state;
290
291 if (state == IFS_DOWN)
292 return;
293
294 iface->link_up_event = false;
295 iface->state = IFS_DOWN;
296 switch (state) {
297 case IFS_UP:
298 case IFS_TEARDOWN:
299 interface_event(iface, IFEV_DOWN);
300 break;
301 case IFS_SETUP:
302 interface_event(iface, IFEV_UP_FAILED);
303 break;
304 default:
305 break;
306 }
307 interface_ip_set_enabled(&iface->config_ip, false);
308 interface_ip_set_enabled(&iface->proto_ip, false);
309 interface_ip_flush(&iface->proto_ip);
310 interface_flush_state(iface);
311 system_flush_routes();
312 }
313
314 static inline void
315 __set_config_state(struct interface *iface, enum interface_config_state s)
316 {
317 iface->config_state = s;
318 }
319
320 static void
321 __interface_set_down(struct interface *iface, bool force)
322 {
323 enum interface_state state = iface->state;
324 switch (state) {
325 case IFS_UP:
326 case IFS_SETUP:
327 iface->state = IFS_TEARDOWN;
328 if (iface->dynamic)
329 __set_config_state(iface, IFC_REMOVE);
330
331 if (state == IFS_UP)
332 interface_event(iface, IFEV_DOWN);
333
334 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
335 if (force)
336 interface_flush_state(iface);
337 break;
338
339 case IFS_DOWN:
340 if (iface->main_dev.dev)
341 device_release(&iface->main_dev);
342 break;
343 case IFS_TEARDOWN:
344 default:
345 break;
346 }
347 }
348
349 static int
350 __interface_set_up(struct interface *iface)
351 {
352 int ret;
353
354 netifd_log_message(L_NOTICE, "Interface '%s' is setting up now\n", iface->name);
355
356 iface->state = IFS_SETUP;
357 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
358 if (ret)
359 mark_interface_down(iface);
360
361 return ret;
362 }
363
364 static void
365 interface_check_state(struct interface *iface)
366 {
367 bool link_state = iface->link_state || interface_force_link(iface);
368
369 switch (iface->state) {
370 case IFS_UP:
371 case IFS_SETUP:
372 if (!iface->enabled || !link_state) {
373 iface->state = IFS_TEARDOWN;
374 if (iface->dynamic)
375 __set_config_state(iface, IFC_REMOVE);
376
377 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
378 }
379 break;
380 case IFS_DOWN:
381 if (!iface->available)
382 return;
383
384 if (iface->autostart && iface->enabled && link_state && !config_init)
385 __interface_set_up(iface);
386 break;
387 default:
388 break;
389 }
390 }
391
392 static void
393 interface_set_enabled(struct interface *iface, bool new_state)
394 {
395 if (iface->enabled == new_state)
396 return;
397
398 netifd_log_message(L_NOTICE, "Interface '%s' is %s\n", iface->name, new_state ? "enabled" : "disabled");
399 iface->enabled = new_state;
400 interface_check_state(iface);
401 }
402
403 static void
404 interface_set_link_state(struct interface *iface, bool new_state)
405 {
406 if (iface->link_state == new_state)
407 return;
408
409 netifd_log_message(L_NOTICE, "Interface '%s' has link connectivity %s\n", iface->name, new_state ? "" : "loss");
410 iface->link_state = new_state;
411 interface_check_state(iface);
412
413 if (new_state && interface_force_link(iface) &&
414 iface->state == IFS_UP && !iface->link_up_event) {
415 interface_event(iface, IFEV_LINK_UP);
416 iface->link_up_event = true;
417 }
418 }
419
420 static void
421 interface_ext_dev_cb(struct device_user *dep, enum device_event ev)
422 {
423 if (ev == DEV_EVENT_REMOVE)
424 device_remove_user(dep);
425 }
426
427 static void
428 interface_main_dev_cb(struct device_user *dep, enum device_event ev)
429 {
430 struct interface *iface;
431
432 iface = container_of(dep, struct interface, main_dev);
433 switch (ev) {
434 case DEV_EVENT_ADD:
435 interface_set_available(iface, true);
436 break;
437 case DEV_EVENT_REMOVE:
438 interface_set_available(iface, false);
439 if (dep->dev && dep->dev->external)
440 interface_set_main_dev(iface, NULL);
441 break;
442 case DEV_EVENT_UP:
443 interface_set_enabled(iface, true);
444 break;
445 case DEV_EVENT_DOWN:
446 interface_set_enabled(iface, false);
447 break;
448 case DEV_EVENT_AUTH_UP:
449 case DEV_EVENT_LINK_UP:
450 case DEV_EVENT_LINK_DOWN:
451 interface_set_link_state(iface, device_link_active(dep->dev));
452 break;
453 case DEV_EVENT_TOPO_CHANGE:
454 interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
455 return;
456 default:
457 break;
458 }
459 }
460
461 static void
462 interface_l3_dev_cb(struct device_user *dep, enum device_event ev)
463 {
464 struct interface *iface;
465
466 iface = container_of(dep, struct interface, l3_dev);
467 if (iface->l3_dev.dev == iface->main_dev.dev)
468 return;
469
470 switch (ev) {
471 case DEV_EVENT_LINK_DOWN:
472 if (iface->proto_handler->flags & PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN)
473 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
474 break;
475 default:
476 break;
477 }
478 }
479
480 void
481 interface_set_available(struct interface *iface, bool new_state)
482 {
483 if (iface->available == new_state)
484 return;
485
486 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
487 iface->available = new_state;
488
489 if (new_state) {
490 if (iface->autostart && !config_init)
491 interface_set_up(iface);
492 } else
493 __interface_set_down(iface, true);
494 }
495
496 void
497 interface_add_user(struct interface_user *dep, struct interface *iface)
498 {
499 if (!iface) {
500 list_add(&dep->list, &iface_all_users);
501 return;
502 }
503
504 dep->iface = iface;
505 list_add(&dep->list, &iface->users);
506 if (iface->state == IFS_UP)
507 dep->cb(dep, iface, IFEV_UP);
508 }
509
510 void
511 interface_remove_user(struct interface_user *dep)
512 {
513 list_del_init(&dep->list);
514 dep->iface = NULL;
515 }
516
517 static void
518 interface_add_assignment_classes(struct interface *iface, struct blob_attr *list)
519 {
520 struct blob_attr *cur;
521 size_t rem;
522
523 blobmsg_for_each_attr(cur, list, rem) {
524 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
525 continue;
526
527 if (!blobmsg_check_attr(cur, false))
528 continue;
529
530 struct interface_assignment_class *c = malloc(sizeof(*c) + blobmsg_data_len(cur));
531 memcpy(c->name, blobmsg_data(cur), blobmsg_data_len(cur));
532 list_add(&c->head, &iface->assignment_classes);
533 }
534 }
535
536 static void
537 interface_clear_assignment_classes(struct interface *iface)
538 {
539 while (!list_empty(&iface->assignment_classes)) {
540 struct interface_assignment_class *c = list_first_entry(&iface->assignment_classes,
541 struct interface_assignment_class, head);
542 list_del(&c->head);
543 free(c);
544 }
545 }
546
547 static void
548 interface_merge_assignment_data(struct interface *old, struct interface *new)
549 {
550 bool changed = (old->assignment_hint != new->assignment_hint ||
551 old->assignment_length != new->assignment_length ||
552 old->assignment_iface_id_selection != new->assignment_iface_id_selection ||
553 old->assignment_weight != new->assignment_weight ||
554 (old->assignment_iface_id_selection == IFID_FIXED &&
555 memcmp(&old->assignment_fixed_iface_id, &new->assignment_fixed_iface_id,
556 sizeof(old->assignment_fixed_iface_id))) ||
557 list_empty(&old->assignment_classes) != list_empty(&new->assignment_classes));
558
559 struct interface_assignment_class *c;
560 list_for_each_entry(c, &new->assignment_classes, head) {
561 /* Compare list entries one-by-one to see if there was a change */
562 if (list_empty(&old->assignment_classes)) /* The new list is longer */
563 changed = true;
564
565 if (changed)
566 break;
567
568 struct interface_assignment_class *c_old = list_first_entry(&old->assignment_classes,
569 struct interface_assignment_class, head);
570
571 if (strcmp(c_old->name, c->name)) /* An entry didn't match */
572 break;
573
574 list_del(&c_old->head);
575 free(c_old);
576 }
577
578 /* The old list was longer than the new one or the last entry didn't match */
579 if (!list_empty(&old->assignment_classes)) {
580 interface_clear_assignment_classes(old);
581 changed = true;
582 }
583
584 list_splice_init(&new->assignment_classes, &old->assignment_classes);
585
586 if (changed) {
587 old->assignment_hint = new->assignment_hint;
588 old->assignment_length = new->assignment_length;
589 old->assignment_iface_id_selection = new->assignment_iface_id_selection;
590 old->assignment_fixed_iface_id = new->assignment_fixed_iface_id;
591 old->assignment_weight = new->assignment_weight;
592 interface_refresh_assignments(true);
593 }
594 }
595
596 static void
597 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
598 {
599 struct interface *alias = container_of(dep, struct interface, parent_iface);
600 struct device *dev = iface->l3_dev.dev;
601
602 switch (ev) {
603 case IFEV_UP:
604 if (!dev)
605 return;
606
607 interface_set_main_dev(alias, dev);
608 interface_set_available(alias, true);
609 break;
610 case IFEV_DOWN:
611 case IFEV_UP_FAILED:
612 interface_set_available(alias, false);
613 interface_set_main_dev(alias, NULL);
614 break;
615 case IFEV_FREE:
616 interface_remove_user(dep);
617 break;
618 default:
619 break;
620 }
621 }
622
623 static void
624 interface_set_device_config(struct interface *iface, struct device *dev)
625 {
626 if (!dev || !dev->default_config)
627 return;
628
629 if (!iface->device_config &&
630 (!dev->iface_config || dev->config_iface != iface))
631 return;
632
633 dev->config_iface = iface;
634 dev->iface_config = iface->device_config;
635 device_apply_config(dev, dev->type, iface->config);
636 }
637
638 static void
639 interface_claim_device(struct interface *iface)
640 {
641 struct interface *parent;
642 struct device *dev = NULL;
643
644 if (iface->parent_iface.iface)
645 interface_remove_user(&iface->parent_iface);
646
647 if (iface->parent_ifname) {
648 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
649 iface->parent_iface.cb = interface_alias_cb;
650 interface_add_user(&iface->parent_iface, parent);
651 } else if (iface->device &&
652 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
653 dev = device_get(iface->device, true);
654 interface_set_device_config(iface, dev);
655 } else {
656 dev = iface->ext_dev.dev;
657 }
658
659 if (dev)
660 interface_set_main_dev(iface, dev);
661
662 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
663 interface_set_available(iface, true);
664 }
665
666 static void
667 interface_cleanup_state(struct interface *iface)
668 {
669 interface_set_available(iface, false);
670
671 interface_flush_state(iface);
672 interface_clear_errors(iface);
673 interface_set_proto_state(iface, NULL);
674
675 interface_set_main_dev(iface, NULL);
676 interface_set_l3_dev(iface, NULL);
677 }
678
679 static void
680 interface_cleanup(struct interface *iface)
681 {
682 struct interface_user *dep, *tmp;
683
684 uloop_timeout_cancel(&iface->remove_timer);
685 device_remove_user(&iface->ext_dev);
686
687 if (iface->parent_iface.iface)
688 interface_remove_user(&iface->parent_iface);
689
690 list_for_each_entry_safe(dep, tmp, &iface->users, list)
691 interface_remove_user(dep);
692
693 interface_clear_assignment_classes(iface);
694 interface_ip_flush(&iface->config_ip);
695 interface_cleanup_state(iface);
696 }
697
698 static void
699 interface_do_free(struct interface *iface)
700 {
701 interface_event(iface, IFEV_FREE);
702 interface_cleanup(iface);
703 free(iface->config);
704 netifd_ubus_remove_interface(iface);
705 avl_delete(&interfaces.avl, &iface->node.avl);
706 if (iface->jail)
707 free(iface->jail);
708 if (iface->jail_device)
709 free(iface->jail_device);
710 if (iface->host_device)
711 free(iface->host_device);
712
713 free(iface);
714 }
715
716 static void
717 interface_do_reload(struct interface *iface)
718 {
719 interface_event(iface, IFEV_RELOAD);
720 interface_cleanup_state(iface);
721 proto_init_interface(iface, iface->config);
722 interface_claim_device(iface);
723 }
724
725 static void
726 interface_handle_config_change(struct interface *iface)
727 {
728 enum interface_config_state state = iface->config_state;
729
730 iface->config_state = IFC_NORMAL;
731 switch(state) {
732 case IFC_NORMAL:
733 break;
734 case IFC_RELOAD:
735 interface_do_reload(iface);
736 break;
737 case IFC_REMOVE:
738 interface_do_free(iface);
739 return;
740 }
741 if (iface->autostart)
742 interface_set_up(iface);
743 }
744
745 static void
746 interface_proto_event_cb(struct interface_proto_state *state, enum interface_proto_event ev)
747 {
748 struct interface *iface = state->iface;
749
750 switch (ev) {
751 case IFPEV_UP:
752 if (iface->state != IFS_SETUP) {
753 if (iface->state == IFS_UP && iface->updated)
754 interface_event(iface, IFEV_UPDATE);
755 return;
756 }
757
758 if (!iface->l3_dev.dev)
759 interface_set_l3_dev(iface, iface->main_dev.dev);
760
761 interface_ip_set_enabled(&iface->config_ip, true);
762 interface_ip_set_enabled(&iface->proto_ip, true);
763 system_flush_routes();
764 iface->state = IFS_UP;
765 iface->start_time = system_get_rtime();
766 interface_event(iface, IFEV_UP);
767 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
768 break;
769 case IFPEV_DOWN:
770 if (iface->state == IFS_DOWN)
771 return;
772
773 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
774 mark_interface_down(iface);
775 interface_write_resolv_conf(iface->jail);
776 if (iface->main_dev.dev)
777 device_release(&iface->main_dev);
778 if (iface->l3_dev.dev)
779 device_remove_user(&iface->l3_dev);
780 interface_handle_config_change(iface);
781 return;
782 case IFPEV_LINK_LOST:
783 if (iface->state != IFS_UP)
784 return;
785
786 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
787 mark_interface_down(iface);
788 iface->state = IFS_SETUP;
789 break;
790 default:
791 return;
792 }
793
794 interface_write_resolv_conf(iface->jail);
795 }
796
797 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
798 {
799 if (iface->proto) {
800 iface->proto->free(iface->proto);
801 iface->proto = NULL;
802 }
803 iface->state = IFS_DOWN;
804 iface->proto = state;
805 if (!state)
806 return;
807
808 state->proto_event = interface_proto_event_cb;
809 state->iface = iface;
810 }
811
812 struct interface *
813 interface_alloc(const char *name, struct blob_attr *config, bool dynamic)
814 {
815 struct interface *iface;
816 struct blob_attr *tb[IFACE_ATTR_MAX];
817 struct blob_attr *cur;
818 const char *proto_name = NULL;
819 char *iface_name;
820 bool force_link = false;
821
822 iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
823 iface->name = strcpy(iface_name, name);
824 INIT_LIST_HEAD(&iface->errors);
825 INIT_LIST_HEAD(&iface->users);
826 INIT_LIST_HEAD(&iface->hotplug_list);
827 INIT_LIST_HEAD(&iface->assignment_classes);
828 interface_ip_init(iface);
829 avl_init(&iface->data, avl_strcmp, false, NULL);
830 iface->config_ip.enabled = false;
831
832 iface->main_dev.cb = interface_main_dev_cb;
833 iface->l3_dev.cb = interface_l3_dev_cb;
834 iface->ext_dev.cb = interface_ext_dev_cb;
835
836 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
837 blob_data(config), blob_len(config));
838
839 iface->zone = NULL;
840 if ((cur = tb[IFACE_ATTR_ZONE]))
841 iface->zone = strdup(blobmsg_get_string(cur));
842
843 if ((cur = tb[IFACE_ATTR_PROTO]))
844 proto_name = blobmsg_data(cur);
845
846 proto_attach_interface(iface, proto_name);
847 if (iface->proto_handler->flags & PROTO_FLAG_FORCE_LINK_DEFAULT)
848 force_link = true;
849
850 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
851 iface->force_link = blobmsg_get_bool_default(tb[IFACE_ATTR_FORCE_LINK], force_link);
852 iface->dynamic = dynamic;
853 iface->proto_ip.no_defaultroute =
854 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
855 iface->proto_ip.no_dns =
856 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
857
858 if ((cur = tb[IFACE_ATTR_DNS]))
859 interface_add_dns_server_list(&iface->config_ip, cur);
860
861 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
862 interface_add_dns_search_list(&iface->config_ip, cur);
863
864 if ((cur = tb[IFACE_ATTR_DNS_METRIC]))
865 iface->dns_metric = blobmsg_get_u32(cur);
866
867 if ((cur = tb[IFACE_ATTR_METRIC]))
868 iface->metric = blobmsg_get_u32(cur);
869
870 if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
871 iface->assignment_length = blobmsg_get_u32(cur);
872
873 /* defaults */
874 iface->assignment_iface_id_selection = IFID_FIXED;
875 iface->assignment_fixed_iface_id = in6addr_any;
876 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
877
878 if ((cur = tb[IFACE_ATTR_IP6IFACEID])) {
879 const char *ifaceid = blobmsg_data(cur);
880 if (!strcmp(ifaceid, "random")) {
881 iface->assignment_iface_id_selection = IFID_RANDOM;
882 }
883 else if (!strcmp(ifaceid, "eui64")) {
884 iface->assignment_iface_id_selection = IFID_EUI64;
885 }
886 else {
887 /* we expect an IPv6 address with network id zero here -> fixed iface id
888 if we cannot parse -> revert to iface id 1 */
889 if (inet_pton(AF_INET6,ifaceid,&iface->assignment_fixed_iface_id) != 1 ||
890 iface->assignment_fixed_iface_id.s6_addr32[0] != 0 ||
891 iface->assignment_fixed_iface_id.s6_addr32[1] != 0) {
892 iface->assignment_fixed_iface_id = in6addr_any;
893 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
894 netifd_log_message(L_WARNING, "Failed to parse ip6ifaceid for interface '%s', \
895 falling back to iface id 1.\n", iface->name);
896 }
897 }
898 }
899
900 iface->assignment_hint = -1;
901 if ((cur = tb[IFACE_ATTR_IP6HINT]))
902 iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
903 ~((1 << (64 - iface->assignment_length)) - 1);
904
905 if ((cur = tb[IFACE_ATTR_IP6CLASS]))
906 interface_add_assignment_classes(iface, cur);
907
908 if ((cur = tb[IFACE_ATTR_IP6WEIGHT]))
909 iface->assignment_weight = blobmsg_get_u32(cur);
910
911 if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
912 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
913 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
914 }
915
916 if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
917 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
918 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
919 }
920
921 iface->proto_ip.no_delegation = !blobmsg_get_bool_default(tb[IFACE_ATTR_DELEGATE], true);
922
923 iface->config_autostart = iface->autostart;
924 iface->jail = NULL;
925
926 if ((cur = tb[IFACE_ATTR_JAIL])) {
927 iface->jail = strdup(blobmsg_get_string(cur));
928 iface->autostart = false;
929 }
930
931 iface->jail_device = NULL;
932 if ((cur = tb[IFACE_ATTR_JAIL_DEVICE]))
933 iface->jail_device = strdup(blobmsg_get_string(cur));
934 else if ((cur = tb[IFACE_ATTR_JAIL_IFNAME]))
935 iface->jail_device = strdup(blobmsg_get_string(cur));
936
937 iface->host_device = NULL;
938 if ((cur = tb[IFACE_ATTR_HOST_DEVICE]))
939 iface->host_device = strdup(blobmsg_get_string(cur));
940
941 return iface;
942 }
943
944 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
945 {
946 struct blob_attr *tb[IFACE_ATTR_MAX];
947 struct blob_attr *cur;
948 char *name = NULL;
949
950 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
951 blob_data(config), blob_len(config));
952
953 if (alias) {
954 if ((cur = tb[IFACE_ATTR_INTERFACE]))
955 iface->parent_ifname = blobmsg_data(cur);
956
957 if (!iface->parent_ifname)
958 return false;
959 } else {
960 cur = tb[IFACE_ATTR_DEVICE];
961 if (!cur)
962 cur = tb[IFACE_ATTR_IFNAME];
963 if (cur)
964 iface->device = blobmsg_data(cur);
965 }
966
967 if (iface->dynamic) {
968 name = strdup(iface->name);
969
970 if (!name)
971 return false;
972 }
973
974 iface->config = config;
975 vlist_add(&interfaces, &iface->node, iface->name);
976
977 if (name) {
978 iface = vlist_find(&interfaces, name, iface, node);
979 free(name);
980
981 /* Don't delete dynamic interface on reload */
982 if (iface)
983 iface->node.version = -1;
984 }
985
986 return true;
987 }
988
989 bool
990 interface_add(struct interface *iface, struct blob_attr *config)
991 {
992 return __interface_add(iface, config, false);
993 }
994
995 bool
996 interface_add_alias(struct interface *iface, struct blob_attr *config)
997 {
998 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
999 return false;
1000
1001 return __interface_add(iface, config, true);
1002 }
1003
1004 void
1005 interface_set_l3_dev(struct interface *iface, struct device *dev)
1006 {
1007 bool enabled = iface->config_ip.enabled;
1008 bool claimed = iface->l3_dev.claimed;
1009
1010 if (iface->l3_dev.dev == dev)
1011 return;
1012
1013 interface_ip_set_enabled(&iface->config_ip, false);
1014 interface_ip_set_enabled(&iface->proto_ip, false);
1015 interface_ip_flush(&iface->proto_ip);
1016 device_add_user(&iface->l3_dev, dev);
1017
1018 if (dev) {
1019 if (claimed) {
1020 if (device_claim(&iface->l3_dev) < 0)
1021 return;
1022 }
1023 interface_ip_set_enabled(&iface->config_ip, enabled);
1024 interface_ip_set_enabled(&iface->proto_ip, enabled);
1025 }
1026 }
1027
1028 static void
1029 interface_set_main_dev(struct interface *iface, struct device *dev)
1030 {
1031 bool claimed = iface->l3_dev.claimed;
1032
1033 if (iface->main_dev.dev == dev)
1034 return;
1035
1036 interface_set_available(iface, false);
1037 device_add_user(&iface->main_dev, dev);
1038 if (!dev) {
1039 interface_set_link_state(iface, false);
1040 return;
1041 }
1042
1043 if (claimed) {
1044 if (device_claim(&iface->l3_dev) < 0)
1045 return;
1046 }
1047
1048 if (!iface->l3_dev.dev)
1049 interface_set_l3_dev(iface, dev);
1050 }
1051
1052 static int
1053 interface_remove_link(struct interface *iface, struct device *dev,
1054 struct blob_attr *vlan)
1055 {
1056 struct device *mdev = iface->main_dev.dev;
1057
1058 if (mdev && mdev->hotplug_ops)
1059 return mdev->hotplug_ops->del(mdev, dev, vlan);
1060
1061 if (dev == iface->ext_dev.dev)
1062 device_remove_user(&iface->ext_dev);
1063
1064 if (!iface->main_dev.hotplug)
1065 return UBUS_STATUS_INVALID_ARGUMENT;
1066
1067 if (dev != iface->main_dev.dev)
1068 return UBUS_STATUS_INVALID_ARGUMENT;
1069
1070 interface_set_main_dev(iface, NULL);
1071 return 0;
1072 }
1073
1074 static int
1075 interface_add_link(struct interface *iface, struct device *dev,
1076 struct blob_attr *vlan, bool link_ext)
1077 {
1078 struct device *mdev = iface->main_dev.dev;
1079
1080 if (mdev == dev)
1081 return 0;
1082
1083 if (iface->main_dev.hotplug)
1084 device_remove_user(&iface->main_dev);
1085
1086 if (mdev) {
1087 if (mdev->hotplug_ops)
1088 return mdev->hotplug_ops->add(mdev, dev, vlan);
1089 else
1090 return UBUS_STATUS_NOT_SUPPORTED;
1091 }
1092
1093 if (link_ext)
1094 device_add_user(&iface->ext_dev, dev);
1095
1096 interface_set_main_dev(iface, dev);
1097 iface->main_dev.hotplug = true;
1098 return 0;
1099 }
1100
1101 int
1102 interface_handle_link(struct interface *iface, const char *name,
1103 struct blob_attr *vlan, bool add, bool link_ext)
1104 {
1105 struct device *dev;
1106
1107 dev = device_get(name, add ? (link_ext ? 2 : 1) : 0);
1108 if (!dev)
1109 return UBUS_STATUS_NOT_FOUND;
1110
1111 if (!add)
1112 return interface_remove_link(iface, dev, vlan);
1113
1114 interface_set_device_config(iface, dev);
1115 if (!link_ext)
1116 device_set_present(dev, true);
1117
1118 return interface_add_link(iface, dev, vlan, link_ext);
1119 }
1120
1121 void
1122 interface_set_up(struct interface *iface)
1123 {
1124 int ret;
1125 const char *error = NULL;
1126
1127 iface->autostart = true;
1128
1129 if (iface->state != IFS_DOWN)
1130 return;
1131
1132 interface_clear_errors(iface);
1133 if (iface->available) {
1134 if (iface->main_dev.dev) {
1135 ret = device_claim(&iface->main_dev);
1136 if (!ret)
1137 interface_check_state(iface);
1138 else
1139 error = "DEVICE_CLAIM_FAILED";
1140 } else {
1141 ret = __interface_set_up(iface);
1142 if (ret)
1143 error = "SETUP_FAILED";
1144 }
1145 } else
1146 error = "NO_DEVICE";
1147
1148 if (error)
1149 interface_add_error(iface, "interface", error, NULL, 0);
1150 }
1151
1152 void
1153 interface_set_down(struct interface *iface)
1154 {
1155 if (!iface) {
1156 vlist_for_each_element(&interfaces, iface, node)
1157 __interface_set_down(iface, false);
1158 } else {
1159 iface->autostart = false;
1160 __interface_set_down(iface, false);
1161 }
1162 }
1163
1164 int
1165 interface_renew(struct interface *iface)
1166 {
1167 if (iface->state == IFS_TEARDOWN || iface->state == IFS_DOWN)
1168 return -1;
1169
1170 return interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
1171 }
1172
1173 void
1174 interface_start_pending(void)
1175 {
1176 struct interface *iface;
1177
1178 vlist_for_each_element(&interfaces, iface, node) {
1179 if (iface->autostart)
1180 interface_set_up(iface);
1181 }
1182 }
1183
1184 void
1185 interface_start_jail(int netns_fd, const char *jail)
1186 {
1187 struct interface *iface;
1188
1189 vlist_for_each_element(&interfaces, iface, node) {
1190 if (!iface->jail || strcmp(iface->jail, jail))
1191 continue;
1192
1193 system_link_netns_move(iface->main_dev.dev, netns_fd, iface->jail_device);
1194 }
1195 }
1196
1197 void
1198 interface_stop_jail(int netns_fd)
1199 {
1200 struct interface *iface;
1201 char *orig_ifname;
1202
1203 vlist_for_each_element(&interfaces, iface, node) {
1204 orig_ifname = iface->host_device;
1205 interface_set_down(iface);
1206 system_link_netns_move(iface->main_dev.dev, netns_fd, orig_ifname);
1207 }
1208 }
1209
1210 static void
1211 set_config_state(struct interface *iface, enum interface_config_state s)
1212 {
1213 __set_config_state(iface, s);
1214 if (iface->state == IFS_DOWN)
1215 interface_handle_config_change(iface);
1216 else
1217 __interface_set_down(iface, false);
1218 }
1219
1220 void
1221 interface_update_start(struct interface *iface, const bool keep_old)
1222 {
1223 iface->updated = 0;
1224
1225 if (!keep_old)
1226 interface_ip_update_start(&iface->proto_ip);
1227 }
1228
1229 void
1230 interface_update_complete(struct interface *iface)
1231 {
1232 interface_ip_update_complete(&iface->proto_ip);
1233 }
1234
1235 static void
1236 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
1237 {
1238 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
1239 vlist_simple_replace(&new->dns_search, &old->dns_search);
1240 }
1241
1242 static bool
1243 interface_device_config_changed(struct interface *if_old, struct interface *if_new)
1244 {
1245 struct blob_attr *ntb[__DEV_ATTR_MAX];
1246 struct blob_attr *otb[__DEV_ATTR_MAX];
1247 struct device *dev = if_old->main_dev.dev;
1248 unsigned long diff = 0;
1249
1250 BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
1251
1252 if (!dev)
1253 return false;
1254
1255 if (if_old->device_config != if_new->device_config)
1256 return true;
1257
1258 if (!if_new->device_config)
1259 return false;
1260
1261 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb,
1262 blob_data(if_old->config), blob_len(if_old->config));
1263
1264 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, ntb,
1265 blob_data(if_new->config), blob_len(if_new->config));
1266
1267 uci_blob_diff(ntb, otb, &device_attr_list, &diff);
1268 return diff;
1269 }
1270
1271 static void
1272 interface_change_config(struct interface *if_old, struct interface *if_new)
1273 {
1274 struct blob_attr *old_config = if_old->config;
1275 bool reload = false, reload_ip = false, update_prefix_delegation = false;
1276
1277 #define FIELD_CHANGED_STR(field) \
1278 ((!!if_old->field != !!if_new->field) || \
1279 (if_old->field && \
1280 strcmp(if_old->field, if_new->field) != 0))
1281
1282 if (FIELD_CHANGED_STR(parent_ifname)) {
1283 if (if_old->parent_iface.iface)
1284 interface_remove_user(&if_old->parent_iface);
1285 reload = true;
1286 }
1287
1288 if (!reload && interface_device_config_changed(if_old, if_new))
1289 reload = true;
1290
1291 if (FIELD_CHANGED_STR(device) ||
1292 if_old->proto_handler != if_new->proto_handler)
1293 reload = true;
1294
1295 if (!if_old->proto_handler->config_params)
1296 D(INTERFACE, "No config parameters for interface '%s'\n",
1297 if_old->name);
1298 else if (!uci_blob_check_equal(if_old->config, if_new->config,
1299 if_old->proto_handler->config_params))
1300 reload = true;
1301
1302 #define UPDATE(field, __var) ({ \
1303 bool __changed = (if_old->field != if_new->field); \
1304 if_old->field = if_new->field; \
1305 __var |= __changed; \
1306 })
1307
1308 if_old->config = if_new->config;
1309 if (if_old->config_autostart != if_new->config_autostart) {
1310 if (if_old->config_autostart)
1311 reload = true;
1312
1313 if_old->autostart = if_new->config_autostart;
1314 }
1315
1316 if_old->device_config = if_new->device_config;
1317 if_old->config_autostart = if_new->config_autostart;
1318 if (if_old->jail)
1319 free(if_old->jail);
1320
1321 if_old->jail = if_new->jail;
1322 if (if_old->jail)
1323 if_old->autostart = false;
1324
1325 if (if_old->jail_device)
1326 free(if_old->jail_device);
1327
1328 if_old->jail_device = if_new->jail_device;
1329
1330 if (if_old->host_device)
1331 free(if_old->host_device);
1332
1333 if_old->host_device = if_new->host_device;
1334
1335 if_old->device = if_new->device;
1336 if_old->parent_ifname = if_new->parent_ifname;
1337 if_old->dynamic = if_new->dynamic;
1338 if_old->proto_handler = if_new->proto_handler;
1339 if_old->force_link = if_new->force_link;
1340 if_old->dns_metric = if_new->dns_metric;
1341
1342 if (if_old->proto_ip.no_delegation != if_new->proto_ip.no_delegation) {
1343 if_old->proto_ip.no_delegation = if_new->proto_ip.no_delegation;
1344 update_prefix_delegation = true;
1345 }
1346
1347 if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
1348 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
1349
1350 UPDATE(metric, reload_ip);
1351 UPDATE(proto_ip.no_defaultroute, reload_ip);
1352 UPDATE(ip4table, reload_ip);
1353 UPDATE(ip6table, reload_ip);
1354 interface_merge_assignment_data(if_old, if_new);
1355
1356 #undef UPDATE
1357
1358 if (reload) {
1359 D(INTERFACE, "Reload interface '%s' because of config changes\n",
1360 if_old->name);
1361 interface_clear_errors(if_old);
1362 set_config_state(if_old, IFC_RELOAD);
1363 goto out;
1364 }
1365
1366 if (reload_ip) {
1367 bool config_ip_enabled = if_old->config_ip.enabled;
1368 bool proto_ip_enabled = if_old->proto_ip.enabled;
1369
1370 interface_ip_set_enabled(&if_old->config_ip, false);
1371 interface_ip_set_enabled(&if_old->proto_ip, false);
1372 interface_ip_set_enabled(&if_old->proto_ip, proto_ip_enabled);
1373 interface_ip_set_enabled(&if_old->config_ip, config_ip_enabled);
1374 }
1375
1376 if (update_prefix_delegation)
1377 interface_update_prefix_delegation(&if_old->proto_ip);
1378
1379 interface_write_resolv_conf(if_old->jail);
1380 if (if_old->main_dev.dev)
1381 interface_check_state(if_old);
1382
1383 out:
1384 if_new->config = NULL;
1385 interface_cleanup(if_new);
1386 free(old_config);
1387 free(if_new);
1388 }
1389
1390 static void
1391 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
1392 struct vlist_node *node_old)
1393 {
1394 struct interface *if_old = container_of(node_old, struct interface, node);
1395 struct interface *if_new = container_of(node_new, struct interface, node);
1396
1397 if (node_old && node_new) {
1398 D(INTERFACE, "Update interface '%s'\n", if_new->name);
1399 interface_change_config(if_old, if_new);
1400 } else if (node_old) {
1401 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
1402 set_config_state(if_old, IFC_REMOVE);
1403 } else if (node_new) {
1404 D(INTERFACE, "Create interface '%s'\n", if_new->name);
1405 interface_event(if_new, IFEV_CREATE);
1406 proto_init_interface(if_new, if_new->config);
1407 interface_claim_device(if_new);
1408 netifd_ubus_add_interface(if_new);
1409 }
1410 }
1411
1412
1413 static void __init
1414 interface_init_list(void)
1415 {
1416 vlist_init(&interfaces, avl_strcmp, interface_update);
1417 interfaces.keep_old = true;
1418 interfaces.no_delete = true;
1419 }