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