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