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