netifd: Explicitly zero initialize variables
[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 interface_write_resolv_conf(iface->jail);
775 if (iface->main_dev.dev)
776 device_release(&iface->main_dev);
777 if (iface->l3_dev.dev)
778 device_remove_user(&iface->l3_dev);
779 interface_handle_config_change(iface);
780 return;
781 case IFPEV_LINK_LOST:
782 if (iface->state != IFS_UP)
783 return;
784
785 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
786 mark_interface_down(iface);
787 iface->state = IFS_SETUP;
788 break;
789 default:
790 return;
791 }
792
793 interface_write_resolv_conf(iface->jail);
794 }
795
796 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
797 {
798 if (iface->proto) {
799 iface->proto->free(iface->proto);
800 iface->proto = NULL;
801 }
802 iface->state = IFS_DOWN;
803 iface->proto = state;
804 if (!state)
805 return;
806
807 state->proto_event = interface_proto_event_cb;
808 state->iface = iface;
809 }
810
811 struct interface *
812 interface_alloc(const char *name, struct blob_attr *config, bool dynamic)
813 {
814 struct interface *iface;
815 struct blob_attr *tb[IFACE_ATTR_MAX];
816 struct blob_attr *cur;
817 const char *proto_name = NULL;
818 char *iface_name;
819 bool force_link = false;
820
821 iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
822 iface->name = strcpy(iface_name, name);
823 INIT_LIST_HEAD(&iface->errors);
824 INIT_LIST_HEAD(&iface->users);
825 INIT_LIST_HEAD(&iface->hotplug_list);
826 INIT_LIST_HEAD(&iface->assignment_classes);
827 interface_ip_init(iface);
828 avl_init(&iface->data, avl_strcmp, false, NULL);
829 iface->config_ip.enabled = false;
830
831 iface->main_dev.cb = interface_main_dev_cb;
832 iface->l3_dev.cb = interface_l3_dev_cb;
833 iface->ext_dev.cb = interface_ext_dev_cb;
834
835 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
836 blob_data(config), blob_len(config));
837
838 iface->zone = NULL;
839 if ((cur = tb[IFACE_ATTR_ZONE]))
840 iface->zone = strdup(blobmsg_get_string(cur));
841
842 if ((cur = tb[IFACE_ATTR_PROTO]))
843 proto_name = blobmsg_data(cur);
844
845 proto_attach_interface(iface, proto_name);
846 if (iface->proto_handler->flags & PROTO_FLAG_FORCE_LINK_DEFAULT)
847 force_link = true;
848
849 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
850 iface->force_link = blobmsg_get_bool_default(tb[IFACE_ATTR_FORCE_LINK], force_link);
851 iface->dynamic = dynamic;
852 iface->proto_ip.no_defaultroute =
853 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
854 iface->proto_ip.no_dns =
855 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
856
857 if ((cur = tb[IFACE_ATTR_DNS]))
858 interface_add_dns_server_list(&iface->config_ip, cur);
859
860 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
861 interface_add_dns_search_list(&iface->config_ip, cur);
862
863 if ((cur = tb[IFACE_ATTR_DNS_METRIC]))
864 iface->dns_metric = blobmsg_get_u32(cur);
865
866 if ((cur = tb[IFACE_ATTR_METRIC]))
867 iface->metric = blobmsg_get_u32(cur);
868
869 if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
870 iface->assignment_length = blobmsg_get_u32(cur);
871
872 /* defaults */
873 iface->assignment_iface_id_selection = IFID_FIXED;
874 iface->assignment_fixed_iface_id = in6addr_any;
875 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
876
877 if ((cur = tb[IFACE_ATTR_IP6IFACEID])) {
878 const char *ifaceid = blobmsg_data(cur);
879 if (!strcmp(ifaceid, "random")) {
880 iface->assignment_iface_id_selection = IFID_RANDOM;
881 }
882 else if (!strcmp(ifaceid, "eui64")) {
883 iface->assignment_iface_id_selection = IFID_EUI64;
884 }
885 else {
886 /* we expect an IPv6 address with network id zero here -> fixed iface id
887 if we cannot parse -> revert to iface id 1 */
888 if (inet_pton(AF_INET6,ifaceid,&iface->assignment_fixed_iface_id) != 1 ||
889 iface->assignment_fixed_iface_id.s6_addr32[0] != 0 ||
890 iface->assignment_fixed_iface_id.s6_addr32[1] != 0) {
891 iface->assignment_fixed_iface_id = in6addr_any;
892 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
893 netifd_log_message(L_WARNING, "Failed to parse ip6ifaceid for interface '%s', \
894 falling back to iface id 1.\n", iface->name);
895 }
896 }
897 }
898
899 iface->assignment_hint = -1;
900 if ((cur = tb[IFACE_ATTR_IP6HINT]))
901 iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
902 ~((1 << (64 - iface->assignment_length)) - 1);
903
904 if ((cur = tb[IFACE_ATTR_IP6CLASS]))
905 interface_add_assignment_classes(iface, cur);
906
907 if ((cur = tb[IFACE_ATTR_IP6WEIGHT]))
908 iface->assignment_weight = blobmsg_get_u32(cur);
909
910 if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
911 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
912 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
913 }
914
915 if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
916 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
917 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
918 }
919
920 iface->proto_ip.no_delegation = !blobmsg_get_bool_default(tb[IFACE_ATTR_DELEGATE], true);
921
922 iface->config_autostart = iface->autostart;
923 iface->jail = NULL;
924
925 if ((cur = tb[IFACE_ATTR_JAIL])) {
926 iface->jail = strdup(blobmsg_get_string(cur));
927 iface->autostart = false;
928 }
929
930 iface->jail_device = NULL;
931 if ((cur = tb[IFACE_ATTR_JAIL_DEVICE]))
932 iface->jail_device = strdup(blobmsg_get_string(cur));
933 else if ((cur = tb[IFACE_ATTR_JAIL_IFNAME]))
934 iface->jail_device = strdup(blobmsg_get_string(cur));
935
936 iface->host_device = NULL;
937 if ((cur = tb[IFACE_ATTR_HOST_DEVICE]))
938 iface->host_device = strdup(blobmsg_get_string(cur));
939
940 return iface;
941 }
942
943 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
944 {
945 struct blob_attr *tb[IFACE_ATTR_MAX];
946 struct blob_attr *cur;
947 char *name = NULL;
948
949 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
950 blob_data(config), blob_len(config));
951
952 if (alias) {
953 if ((cur = tb[IFACE_ATTR_INTERFACE]))
954 iface->parent_ifname = blobmsg_data(cur);
955
956 if (!iface->parent_ifname)
957 return false;
958 } else {
959 cur = tb[IFACE_ATTR_DEVICE];
960 if (!cur)
961 cur = tb[IFACE_ATTR_IFNAME];
962 if (cur)
963 iface->device = blobmsg_data(cur);
964 }
965
966 if (iface->dynamic) {
967 name = strdup(iface->name);
968
969 if (!name)
970 return false;
971 }
972
973 iface->config = config;
974 vlist_add(&interfaces, &iface->node, iface->name);
975
976 if (name) {
977 iface = vlist_find(&interfaces, name, iface, node);
978 free(name);
979
980 /* Don't delete dynamic interface on reload */
981 if (iface)
982 iface->node.version = -1;
983 }
984
985 return true;
986 }
987
988 bool
989 interface_add(struct interface *iface, struct blob_attr *config)
990 {
991 return __interface_add(iface, config, false);
992 }
993
994 bool
995 interface_add_alias(struct interface *iface, struct blob_attr *config)
996 {
997 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
998 return false;
999
1000 return __interface_add(iface, config, true);
1001 }
1002
1003 void
1004 interface_set_l3_dev(struct interface *iface, struct device *dev)
1005 {
1006 bool enabled = iface->config_ip.enabled;
1007 bool claimed = iface->l3_dev.claimed;
1008
1009 if (iface->l3_dev.dev == dev)
1010 return;
1011
1012 interface_ip_set_enabled(&iface->config_ip, false);
1013 interface_ip_set_enabled(&iface->proto_ip, false);
1014 interface_ip_flush(&iface->proto_ip);
1015 device_add_user(&iface->l3_dev, dev);
1016
1017 if (dev) {
1018 if (claimed) {
1019 if (device_claim(&iface->l3_dev) < 0)
1020 return;
1021 }
1022 interface_ip_set_enabled(&iface->config_ip, enabled);
1023 interface_ip_set_enabled(&iface->proto_ip, enabled);
1024 }
1025 }
1026
1027 static void
1028 interface_set_main_dev(struct interface *iface, struct device *dev)
1029 {
1030 bool claimed = iface->l3_dev.claimed;
1031
1032 if (iface->main_dev.dev == dev)
1033 return;
1034
1035 interface_set_available(iface, false);
1036 device_add_user(&iface->main_dev, dev);
1037 if (!dev) {
1038 interface_set_link_state(iface, false);
1039 return;
1040 }
1041
1042 if (claimed) {
1043 if (device_claim(&iface->l3_dev) < 0)
1044 return;
1045 }
1046
1047 if (!iface->l3_dev.dev)
1048 interface_set_l3_dev(iface, dev);
1049 }
1050
1051 static int
1052 interface_remove_link(struct interface *iface, struct device *dev,
1053 struct blob_attr *vlan)
1054 {
1055 struct device *mdev = iface->main_dev.dev;
1056
1057 if (mdev && mdev->hotplug_ops)
1058 return mdev->hotplug_ops->del(mdev, dev, vlan);
1059
1060 if (dev == iface->ext_dev.dev)
1061 device_remove_user(&iface->ext_dev);
1062
1063 if (!iface->main_dev.hotplug)
1064 return UBUS_STATUS_INVALID_ARGUMENT;
1065
1066 if (dev != iface->main_dev.dev)
1067 return UBUS_STATUS_INVALID_ARGUMENT;
1068
1069 interface_set_main_dev(iface, NULL);
1070 return 0;
1071 }
1072
1073 static int
1074 interface_add_link(struct interface *iface, struct device *dev,
1075 struct blob_attr *vlan, bool link_ext)
1076 {
1077 struct device *mdev = iface->main_dev.dev;
1078
1079 if (mdev == dev)
1080 return 0;
1081
1082 if (iface->main_dev.hotplug)
1083 device_remove_user(&iface->main_dev);
1084
1085 if (mdev) {
1086 if (mdev->hotplug_ops)
1087 return mdev->hotplug_ops->add(mdev, dev, vlan);
1088 else
1089 return UBUS_STATUS_NOT_SUPPORTED;
1090 }
1091
1092 if (link_ext)
1093 device_add_user(&iface->ext_dev, dev);
1094
1095 interface_set_main_dev(iface, dev);
1096 iface->main_dev.hotplug = true;
1097 return 0;
1098 }
1099
1100 int
1101 interface_handle_link(struct interface *iface, const char *name,
1102 struct blob_attr *vlan, bool add, bool link_ext)
1103 {
1104 struct device *dev;
1105
1106 dev = device_get(name, add ? (link_ext ? 2 : 1) : 0);
1107 if (!dev)
1108 return UBUS_STATUS_NOT_FOUND;
1109
1110 if (!add)
1111 return interface_remove_link(iface, dev, vlan);
1112
1113 interface_set_device_config(iface, dev);
1114 if (!link_ext)
1115 device_set_present(dev, true);
1116
1117 return interface_add_link(iface, dev, vlan, link_ext);
1118 }
1119
1120 void
1121 interface_set_up(struct interface *iface)
1122 {
1123 int ret;
1124 const char *error = NULL;
1125
1126 iface->autostart = true;
1127
1128 if (iface->state != IFS_DOWN)
1129 return;
1130
1131 interface_clear_errors(iface);
1132 if (iface->available) {
1133 if (iface->main_dev.dev) {
1134 ret = device_claim(&iface->main_dev);
1135 if (!ret)
1136 interface_check_state(iface);
1137 else
1138 error = "DEVICE_CLAIM_FAILED";
1139 } else {
1140 ret = __interface_set_up(iface);
1141 if (ret)
1142 error = "SETUP_FAILED";
1143 }
1144 } else
1145 error = "NO_DEVICE";
1146
1147 if (error)
1148 interface_add_error(iface, "interface", error, NULL, 0);
1149 }
1150
1151 void
1152 interface_set_down(struct interface *iface)
1153 {
1154 if (!iface) {
1155 vlist_for_each_element(&interfaces, iface, node)
1156 __interface_set_down(iface, false);
1157 } else {
1158 iface->autostart = false;
1159 __interface_set_down(iface, false);
1160 }
1161 }
1162
1163 int
1164 interface_renew(struct interface *iface)
1165 {
1166 if (iface->state == IFS_TEARDOWN || iface->state == IFS_DOWN)
1167 return -1;
1168
1169 return interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
1170 }
1171
1172 void
1173 interface_start_pending(void)
1174 {
1175 struct interface *iface;
1176
1177 vlist_for_each_element(&interfaces, iface, node) {
1178 if (iface->autostart)
1179 interface_set_up(iface);
1180 }
1181 }
1182
1183 void
1184 interface_start_jail(int netns_fd, const char *jail)
1185 {
1186 struct interface *iface;
1187
1188 vlist_for_each_element(&interfaces, iface, node) {
1189 if (!iface->jail || strcmp(iface->jail, jail))
1190 continue;
1191
1192 system_link_netns_move(iface->main_dev.dev, netns_fd, iface->jail_device);
1193 }
1194 }
1195
1196 void
1197 interface_stop_jail(int netns_fd)
1198 {
1199 struct interface *iface;
1200 char *orig_ifname;
1201
1202 vlist_for_each_element(&interfaces, iface, node) {
1203 orig_ifname = iface->host_device;
1204 interface_set_down(iface);
1205 system_link_netns_move(iface->main_dev.dev, netns_fd, orig_ifname);
1206 }
1207 }
1208
1209 static void
1210 set_config_state(struct interface *iface, enum interface_config_state s)
1211 {
1212 __set_config_state(iface, s);
1213 if (iface->state == IFS_DOWN)
1214 interface_handle_config_change(iface);
1215 else
1216 __interface_set_down(iface, false);
1217 }
1218
1219 void
1220 interface_update_start(struct interface *iface, const bool keep_old)
1221 {
1222 iface->updated = 0;
1223
1224 if (!keep_old)
1225 interface_ip_update_start(&iface->proto_ip);
1226 }
1227
1228 void
1229 interface_update_complete(struct interface *iface)
1230 {
1231 interface_ip_update_complete(&iface->proto_ip);
1232 }
1233
1234 static void
1235 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
1236 {
1237 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
1238 vlist_simple_replace(&new->dns_search, &old->dns_search);
1239 }
1240
1241 static bool
1242 interface_device_config_changed(struct interface *if_old, struct interface *if_new)
1243 {
1244 struct blob_attr *ntb[__DEV_ATTR_MAX];
1245 struct blob_attr *otb[__DEV_ATTR_MAX];
1246 struct device *dev = if_old->main_dev.dev;
1247 unsigned long diff = 0;
1248
1249 BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
1250
1251 if (!dev)
1252 return false;
1253
1254 if (if_old->device_config != if_new->device_config)
1255 return true;
1256
1257 if (!if_new->device_config)
1258 return false;
1259
1260 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb,
1261 blob_data(if_old->config), blob_len(if_old->config));
1262
1263 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, ntb,
1264 blob_data(if_new->config), blob_len(if_new->config));
1265
1266 uci_blob_diff(ntb, otb, &device_attr_list, &diff);
1267 return diff;
1268 }
1269
1270 static void
1271 interface_change_config(struct interface *if_old, struct interface *if_new)
1272 {
1273 struct blob_attr *old_config = if_old->config;
1274 bool reload = false, reload_ip = false, update_prefix_delegation = false;
1275
1276 #define FIELD_CHANGED_STR(field) \
1277 ((!!if_old->field != !!if_new->field) || \
1278 (if_old->field && \
1279 strcmp(if_old->field, if_new->field) != 0))
1280
1281 if (FIELD_CHANGED_STR(parent_ifname)) {
1282 if (if_old->parent_iface.iface)
1283 interface_remove_user(&if_old->parent_iface);
1284 reload = true;
1285 }
1286
1287 if (!reload && interface_device_config_changed(if_old, if_new))
1288 reload = true;
1289
1290 if (FIELD_CHANGED_STR(device) ||
1291 if_old->proto_handler != if_new->proto_handler)
1292 reload = true;
1293
1294 if (!if_old->proto_handler->config_params)
1295 D(INTERFACE, "No config parameters for interface '%s'\n",
1296 if_old->name);
1297 else if (!uci_blob_check_equal(if_old->config, if_new->config,
1298 if_old->proto_handler->config_params))
1299 reload = true;
1300
1301 #define UPDATE(field, __var) ({ \
1302 bool __changed = (if_old->field != if_new->field); \
1303 if_old->field = if_new->field; \
1304 __var |= __changed; \
1305 })
1306
1307 if_old->config = if_new->config;
1308 if (if_old->config_autostart != if_new->config_autostart) {
1309 if (if_old->config_autostart)
1310 reload = true;
1311
1312 if_old->autostart = if_new->config_autostart;
1313 }
1314
1315 if_old->device_config = if_new->device_config;
1316 if_old->config_autostart = if_new->config_autostart;
1317 if (if_old->jail)
1318 free(if_old->jail);
1319
1320 if_old->jail = if_new->jail;
1321 if (if_old->jail)
1322 if_old->autostart = false;
1323
1324 if (if_old->jail_device)
1325 free(if_old->jail_device);
1326
1327 if_old->jail_device = if_new->jail_device;
1328
1329 if (if_old->host_device)
1330 free(if_old->host_device);
1331
1332 if_old->host_device = if_new->host_device;
1333
1334 if_old->device = if_new->device;
1335 if_old->parent_ifname = if_new->parent_ifname;
1336 if_old->dynamic = if_new->dynamic;
1337 if_old->proto_handler = if_new->proto_handler;
1338 if_old->force_link = if_new->force_link;
1339 if_old->dns_metric = if_new->dns_metric;
1340
1341 if (if_old->proto_ip.no_delegation != if_new->proto_ip.no_delegation) {
1342 if_old->proto_ip.no_delegation = if_new->proto_ip.no_delegation;
1343 update_prefix_delegation = true;
1344 }
1345
1346 if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
1347 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
1348
1349 UPDATE(metric, reload_ip);
1350 UPDATE(proto_ip.no_defaultroute, reload_ip);
1351 UPDATE(ip4table, reload_ip);
1352 UPDATE(ip6table, reload_ip);
1353 interface_merge_assignment_data(if_old, if_new);
1354
1355 #undef UPDATE
1356
1357 if (reload) {
1358 D(INTERFACE, "Reload interface '%s' because of config changes\n",
1359 if_old->name);
1360 interface_clear_errors(if_old);
1361 set_config_state(if_old, IFC_RELOAD);
1362 goto out;
1363 }
1364
1365 if (reload_ip) {
1366 bool config_ip_enabled = if_old->config_ip.enabled;
1367 bool proto_ip_enabled = if_old->proto_ip.enabled;
1368
1369 interface_ip_set_enabled(&if_old->config_ip, false);
1370 interface_ip_set_enabled(&if_old->proto_ip, false);
1371 interface_ip_set_enabled(&if_old->proto_ip, proto_ip_enabled);
1372 interface_ip_set_enabled(&if_old->config_ip, config_ip_enabled);
1373 }
1374
1375 if (update_prefix_delegation)
1376 interface_update_prefix_delegation(&if_old->proto_ip);
1377
1378 interface_write_resolv_conf(if_old->jail);
1379 if (if_old->main_dev.dev)
1380 interface_check_state(if_old);
1381
1382 out:
1383 if_new->config = NULL;
1384 interface_cleanup(if_new);
1385 free(old_config);
1386 free(if_new);
1387 }
1388
1389 static void
1390 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
1391 struct vlist_node *node_old)
1392 {
1393 struct interface *if_old = container_of(node_old, struct interface, node);
1394 struct interface *if_new = container_of(node_new, struct interface, node);
1395
1396 if (node_old && node_new) {
1397 D(INTERFACE, "Update interface '%s'\n", if_new->name);
1398 interface_change_config(if_old, if_new);
1399 } else if (node_old) {
1400 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
1401 set_config_state(if_old, IFC_REMOVE);
1402 } else if (node_new) {
1403 D(INTERFACE, "Create interface '%s'\n", if_new->name);
1404 interface_event(if_new, IFEV_CREATE);
1405 proto_init_interface(if_new, if_new->config);
1406 interface_claim_device(if_new);
1407 netifd_ubus_add_interface(if_new);
1408 }
1409 }
1410
1411
1412 static void __init
1413 interface_init_list(void)
1414 {
1415 vlist_init(&interfaces, avl_strcmp, interface_update);
1416 interfaces.keep_old = true;
1417 interfaces.no_delete = true;
1418 }