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