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