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