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