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