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