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