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