4b381a431cbe3c0ae27e8afb15800cf903870b8d
[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 static unsigned int interface_serial = 0;
30
31 enum {
32 IFACE_ATTR_IFNAME,
33 IFACE_ATTR_PROTO,
34 IFACE_ATTR_AUTO,
35 IFACE_ATTR_DEFAULTROUTE,
36 IFACE_ATTR_PEERDNS,
37 IFACE_ATTR_DNS,
38 IFACE_ATTR_DNS_SEARCH,
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_MAX
48 };
49
50 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
51 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
52 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
53 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
54 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
55 [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
56 [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
57 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
58 [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
59 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
60 [IFACE_ATTR_IP6ASSIGN] = { .name = "ip6assign", .type = BLOBMSG_TYPE_INT32 },
61 [IFACE_ATTR_IP6HINT] = { .name = "ip6hint", .type = BLOBMSG_TYPE_STRING },
62 [IFACE_ATTR_IP4TABLE] = { .name = "ip4table", .type = BLOBMSG_TYPE_STRING },
63 [IFACE_ATTR_IP6TABLE] = { .name = "ip6table", .type = BLOBMSG_TYPE_STRING },
64 [IFACE_ATTR_IP6CLASS] = { .name = "ip6class", .type = BLOBMSG_TYPE_ARRAY },
65 [IFACE_ATTR_DELEGATE] = { .name = "delegate", .type = BLOBMSG_TYPE_BOOL },
66 };
67
68 static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
69 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
70 [IFACE_ATTR_IP6CLASS] = { .type = BLOBMSG_TYPE_STRING },
71 };
72
73 const struct uci_blob_param_list interface_attr_list = {
74 .n_params = IFACE_ATTR_MAX,
75 .params = iface_attrs,
76 .info = iface_attr_info,
77 };
78
79 static void
80 interface_clear_errors(struct interface *iface)
81 {
82 struct interface_error *error, *tmp;
83
84 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
85 list_del(&error->list);
86 free(error);
87 }
88 }
89
90 void interface_add_error(struct interface *iface, const char *subsystem,
91 const char *code, const char **data, int n_data)
92 {
93 struct interface_error *error;
94 int i, len = 0;
95 int *datalen = NULL;
96 char *dest, *d_subsys, *d_code;
97
98 if (n_data) {
99 len = n_data * sizeof(char *);
100 datalen = alloca(len);
101 for (i = 0; i < n_data; i++) {
102 datalen[i] = strlen(data[i]) + 1;
103 len += datalen[i];
104 }
105 }
106
107 error = calloc_a(sizeof(*error) + sizeof(char *) + len,
108 &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
109 &d_code, code ? strlen(code) + 1 : 0);
110 if (!error)
111 return;
112
113 list_add_tail(&error->list, &iface->errors);
114
115 dest = (char *) &error->data[n_data + 1];
116 for (i = 0; i < n_data; i++) {
117 error->data[i] = dest;
118 memcpy(dest, data[i], datalen[i]);
119 dest += datalen[i];
120 }
121 error->data[n_data++] = NULL;
122
123 if (subsystem)
124 error->subsystem = strcpy(d_subsys, subsystem);
125
126 if (code)
127 error->code = strcpy(d_code, code);
128 }
129
130 static void
131 interface_data_del(struct interface *iface, struct interface_data *data)
132 {
133 avl_delete(&iface->data, &data->node);
134 free(data);
135 }
136
137 static void
138 interface_data_flush(struct interface *iface)
139 {
140 struct interface_data *d, *tmp;
141
142 avl_for_each_element_safe(&iface->data, d, node, tmp)
143 interface_data_del(iface, d);
144 }
145
146 int
147 interface_add_data(struct interface *iface, const struct blob_attr *data)
148 {
149 struct interface_data *n, *o;
150
151 if (!blobmsg_check_attr(data, true))
152 return UBUS_STATUS_INVALID_ARGUMENT;
153
154 n = calloc(1, sizeof(*n) + blob_pad_len(data));
155 memcpy(n->data, data, blob_pad_len(data));
156 n->node.key = blobmsg_name(n->data);
157
158 o = avl_find_element(&iface->data, n->node.key, o, node);
159 if (o)
160 interface_data_del(iface, o);
161
162 avl_insert(&iface->data, &n->node);
163 return 0;
164 }
165
166 static void
167 interface_event(struct interface *iface, enum interface_event ev)
168 {
169 struct interface_user *dep, *tmp;
170 struct device *adev = NULL;
171
172 list_for_each_entry_safe(dep, tmp, &iface->users, list)
173 dep->cb(dep, iface, ev);
174
175 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
176 dep->cb(dep, iface, ev);
177
178 switch (ev) {
179 case IFEV_UP:
180 adev = iface->l3_dev.dev;
181 /* fall through */
182 case IFEV_DOWN:
183 alias_notify_device(iface->name, adev);
184 break;
185 default:
186 break;
187 }
188 }
189
190 static void
191 interface_flush_state(struct interface *iface)
192 {
193 if (iface->l3_dev.dev)
194 device_release(&iface->l3_dev);
195 interface_data_flush(iface);
196 }
197
198 static void
199 mark_interface_down(struct interface *iface)
200 {
201 enum interface_state state = iface->state;
202
203 if (state == IFS_DOWN)
204 return;
205
206 iface->state = IFS_DOWN;
207 if (state == IFS_UP)
208 interface_event(iface, IFEV_DOWN);
209 interface_ip_set_enabled(&iface->config_ip, false);
210 interface_ip_flush(&iface->proto_ip);
211 interface_flush_state(iface);
212 system_flush_routes();
213 }
214
215 void
216 __interface_set_down(struct interface *iface, bool force)
217 {
218 switch (iface->state) {
219 case IFS_UP:
220 interface_event(iface, IFEV_DOWN);
221 case IFS_SETUP:
222 iface->state = IFS_TEARDOWN;
223 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
224 if (force)
225 interface_flush_state(iface);
226
227 if (iface->dynamic)
228 vlist_delete(&interfaces, &iface->node);
229 break;
230
231 case IFS_DOWN:
232 if (iface->main_dev.dev)
233 device_release(&iface->main_dev);
234 case IFS_TEARDOWN:
235 default:
236 break;
237 }
238 }
239
240 static int
241 __interface_set_up(struct interface *iface)
242 {
243 int ret;
244
245 netifd_log_message(L_NOTICE, "Interface '%s' is setting up now\n", iface->name);
246
247 iface->state = IFS_SETUP;
248 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
249 if (ret)
250 mark_interface_down(iface);
251
252 return ret;
253 }
254
255 static void
256 interface_check_state(struct interface *iface)
257 {
258 switch (iface->state) {
259 case IFS_UP:
260 if (!iface->enabled || !iface->link_state) {
261 mark_interface_down(iface);
262 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
263 }
264 break;
265 case IFS_DOWN:
266 if (iface->enabled && iface->link_state && !config_init)
267 __interface_set_up(iface);
268 break;
269 default:
270 break;
271 }
272 }
273
274 static void
275 interface_set_enabled(struct interface *iface, bool new_state)
276 {
277 if (iface->enabled == new_state)
278 return;
279
280 netifd_log_message(L_NOTICE, "Interface '%s' is %s\n", iface->name, new_state ? "enabled" : "disabled");
281 iface->enabled = new_state;
282 interface_check_state(iface);
283 }
284
285 static void
286 interface_set_link_state(struct interface *iface, bool new_state)
287 {
288 if (iface->link_state == new_state)
289 return;
290
291 netifd_log_message(L_NOTICE, "Interface '%s' has link connectivity %s\n", iface->name, new_state ? "" : "loss");
292 iface->link_state = new_state;
293 interface_check_state(iface);
294 }
295
296 static void
297 interface_cb(struct device_user *dep, enum device_event ev)
298 {
299 struct interface *iface;
300 bool new_state = false;
301
302 iface = container_of(dep, struct interface, main_dev);
303 switch (ev) {
304 case DEV_EVENT_ADD:
305 new_state = true;
306 case DEV_EVENT_REMOVE:
307 interface_set_available(iface, new_state);
308 if (!new_state && dep->dev->external)
309 interface_set_main_dev(iface, NULL);
310 break;
311 case DEV_EVENT_UP:
312 new_state = true;
313 case DEV_EVENT_DOWN:
314 interface_set_enabled(iface, new_state);
315 break;
316 case DEV_EVENT_LINK_UP:
317 new_state = true;
318 case DEV_EVENT_LINK_DOWN:
319 interface_set_link_state(iface, new_state);
320 break;
321 default:
322 break;
323 }
324 }
325
326 void
327 interface_set_available(struct interface *iface, bool new_state)
328 {
329 if (iface->available == new_state)
330 return;
331
332 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
333 iface->available = new_state;
334
335 if (new_state) {
336 if (iface->autostart && !config_init)
337 interface_set_up(iface);
338 } else
339 __interface_set_down(iface, true);
340 }
341
342 void
343 interface_add_user(struct interface_user *dep, struct interface *iface)
344 {
345 if (!iface) {
346 list_add(&dep->list, &iface_all_users);
347 return;
348 }
349
350 dep->iface = iface;
351 list_add(&dep->list, &iface->users);
352 if (iface->state == IFS_UP)
353 dep->cb(dep, iface, IFEV_UP);
354 }
355
356 void
357 interface_remove_user(struct interface_user *dep)
358 {
359 list_del_init(&dep->list);
360 dep->iface = NULL;
361 }
362
363 static void
364 interface_add_assignment_classes(struct interface *iface, struct blob_attr *list)
365 {
366 struct blob_attr *cur;
367 int rem;
368
369 blobmsg_for_each_attr(cur, list, rem) {
370 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
371 continue;
372
373 if (!blobmsg_check_attr(cur, NULL))
374 continue;
375
376 struct interface_assignment_class *c = malloc(sizeof(*c) + blobmsg_data_len(cur));
377 memcpy(c->name, blobmsg_data(cur), blobmsg_data_len(cur));
378 list_add(&c->head, &iface->assignment_classes);
379 }
380 }
381
382 static void
383 interface_clear_assignment_classes(struct interface *iface)
384 {
385 while (!list_empty(&iface->assignment_classes)) {
386 struct interface_assignment_class *c = list_first_entry(&iface->assignment_classes,
387 struct interface_assignment_class, head);
388 list_del(&c->head);
389 free(c);
390 }
391 }
392
393 static void
394 interface_merge_assignment_data(struct interface *old, struct interface *new)
395 {
396 bool changed = (old->assignment_hint != new->assignment_hint ||
397 old->assignment_length != new->assignment_length ||
398 list_empty(&old->assignment_classes) != list_empty(&new->assignment_classes));
399
400 struct interface_assignment_class *c;
401 list_for_each_entry(c, &new->assignment_classes, head) {
402 // Compare list entries one-by-one to see if there was a change
403 if (list_empty(&old->assignment_classes)) // The new list is longer
404 changed = true;
405
406 if (changed)
407 break;
408
409 struct interface_assignment_class *c_old = list_first_entry(&old->assignment_classes,
410 struct interface_assignment_class, head);
411
412 if (strcmp(c_old->name, c->name)) // An entry didn't match
413 break;
414
415 list_del(&c_old->head);
416 free(c_old);
417 }
418
419 // The old list was longer than the new one or the last entry didn't match
420 if (!list_empty(&old->assignment_classes)) {
421 interface_clear_assignment_classes(old);
422 changed = true;
423 }
424
425 list_splice_init(&new->assignment_classes, &old->assignment_classes);
426
427 if (changed) {
428 old->assignment_hint = new->assignment_hint;
429 old->assignment_length = new->assignment_length;
430 interface_refresh_assignments(true);
431 }
432 }
433
434 static void
435 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
436 {
437 struct interface *alias = container_of(dep, struct interface, parent_iface);
438 struct device *dev = iface->l3_dev.dev;
439
440 switch (ev) {
441 case IFEV_UP:
442 if (!dev)
443 return;
444
445 interface_set_main_dev(alias, dev);
446 interface_set_available(alias, true);
447 break;
448 case IFEV_DOWN:
449 interface_set_available(alias, false);
450 interface_set_main_dev(alias, NULL);
451 break;
452 case IFEV_FREE:
453 interface_remove_user(dep);
454 break;
455 case IFEV_RELOAD:
456 case IFEV_UPDATE:
457 break;
458 }
459 }
460
461 static void
462 interface_claim_device(struct interface *iface)
463 {
464 struct interface *parent;
465 struct device *dev = NULL;
466
467 if (iface->parent_iface.iface)
468 interface_remove_user(&iface->parent_iface);
469
470 if (iface->parent_ifname) {
471 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
472 iface->parent_iface.cb = interface_alias_cb;
473 interface_add_user(&iface->parent_iface, parent);
474 } else if (iface->ifname &&
475 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
476 dev = device_get(iface->ifname, true);
477 }
478
479 if (dev)
480 interface_set_main_dev(iface, dev);
481
482 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
483 interface_set_available(iface, true);
484 }
485
486 static void
487 interface_cleanup_state(struct interface *iface)
488 {
489 interface_set_available(iface, false);
490
491 interface_flush_state(iface);
492 interface_clear_errors(iface);
493 interface_set_proto_state(iface, NULL);
494
495 if (iface->main_dev.dev)
496 interface_set_main_dev(iface, NULL);
497 }
498
499 static void
500 interface_cleanup(struct interface *iface)
501 {
502 struct interface_user *dep, *tmp;
503
504 if (iface->parent_iface.iface)
505 interface_remove_user(&iface->parent_iface);
506
507 list_for_each_entry_safe(dep, tmp, &iface->users, list)
508 interface_remove_user(dep);
509
510 interface_clear_assignment_classes(iface);
511 interface_ip_flush(&iface->config_ip);
512 interface_cleanup_state(iface);
513 }
514
515 static void
516 interface_do_free(struct interface *iface)
517 {
518 interface_event(iface, IFEV_FREE);
519 interface_cleanup(iface);
520 free(iface->config);
521 netifd_ubus_remove_interface(iface);
522 avl_delete(&interfaces.avl, &iface->node.avl);
523 free(iface);
524 }
525
526 static void
527 interface_do_reload(struct interface *iface)
528 {
529 interface_event(iface, IFEV_RELOAD);
530 interface_cleanup_state(iface);
531 proto_init_interface(iface, iface->config);
532 interface_claim_device(iface);
533 }
534
535 static void
536 interface_handle_config_change(struct interface *iface)
537 {
538 enum interface_config_state state = iface->config_state;
539
540 iface->config_state = IFC_NORMAL;
541 switch(state) {
542 case IFC_NORMAL:
543 break;
544 case IFC_RELOAD:
545 interface_do_reload(iface);
546 break;
547 case IFC_REMOVE:
548 interface_do_free(iface);
549 return;
550 }
551 if (iface->autostart && iface->available)
552 interface_set_up(iface);
553 }
554
555 static void
556 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
557 {
558 struct interface *iface = state->iface;
559
560 switch (ev) {
561 case IFPEV_UP:
562 if (iface->state != IFS_SETUP) {
563 interface_event(iface, IFEV_UPDATE);
564 return;
565 }
566
567 interface_ip_set_enabled(&iface->config_ip, true);
568 system_flush_routes();
569 iface->state = IFS_UP;
570 iface->start_time = system_get_rtime();
571 interface_event(iface, IFEV_UP);
572 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
573 break;
574 case IFPEV_DOWN:
575 if (iface->state == IFS_DOWN)
576 return;
577
578 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
579 mark_interface_down(iface);
580 if (iface->main_dev.dev)
581 device_release(&iface->main_dev);
582 interface_handle_config_change(iface);
583 break;
584 case IFPEV_LINK_LOST:
585 if (iface->state != IFS_UP)
586 return;
587
588 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
589 mark_interface_down(iface);
590 iface->state = IFS_SETUP;
591 break;
592 }
593
594 interface_write_resolv_conf();
595 }
596
597 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
598 {
599 if (iface->proto) {
600 iface->proto->free(iface->proto);
601 iface->proto = NULL;
602 }
603 iface->state = IFS_DOWN;
604 iface->proto = state;
605 if (!state)
606 return;
607
608 state->proto_event = interface_proto_cb;
609 state->iface = iface;
610 }
611
612 struct interface *
613 interface_alloc(const char *name, struct blob_attr *config)
614 {
615 struct interface *iface;
616 struct blob_attr *tb[IFACE_ATTR_MAX];
617 struct blob_attr *cur;
618 const char *proto_name = NULL;
619 char *iface_name;
620
621 iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
622 iface->name = strcpy(iface_name, name);
623 INIT_LIST_HEAD(&iface->errors);
624 INIT_LIST_HEAD(&iface->users);
625 INIT_LIST_HEAD(&iface->hotplug_list);
626 INIT_LIST_HEAD(&iface->assignment_classes);
627 interface_ip_init(iface);
628 avl_init(&iface->data, avl_strcmp, false, NULL);
629 iface->config_ip.enabled = false;
630
631 iface->main_dev.cb = interface_cb;
632
633 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
634 blob_data(config), blob_len(config));
635
636 if ((cur = tb[IFACE_ATTR_PROTO]))
637 proto_name = blobmsg_data(cur);
638
639 proto_attach_interface(iface, proto_name);
640
641 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
642 iface->proto_ip.no_defaultroute =
643 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
644 iface->proto_ip.no_dns =
645 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
646
647 if ((cur = tb[IFACE_ATTR_DNS]))
648 interface_add_dns_server_list(&iface->config_ip, cur);
649
650 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
651 interface_add_dns_search_list(&iface->config_ip, cur);
652
653 if ((cur = tb[IFACE_ATTR_METRIC]))
654 iface->metric = blobmsg_get_u32(cur);
655
656 if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
657 iface->assignment_length = blobmsg_get_u32(cur);
658
659 iface->assignment_hint = -1;
660 if ((cur = tb[IFACE_ATTR_IP6HINT]))
661 iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
662 ~((1 << (64 - iface->assignment_length)) - 1);
663
664 if ((cur = tb[IFACE_ATTR_IP6CLASS]))
665 interface_add_assignment_classes(iface, cur);
666
667
668 if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
669 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
670 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
671 }
672
673 // Set a default exteranl routing table for IPv6 to do source-based-filtering
674 struct interface *iface_old = vlist_find(&interfaces, name, iface_old, node);
675 if (iface_old && iface_old->ip6table > 1000 && iface_old->ip6table < 2000)
676 iface->ip6table = iface_old->ip6table;
677 else
678 iface->ip6table = 1000 + ++interface_serial;
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);
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 interface_ip_update_start(&iface->proto_ip);
920 }
921
922 void
923 interface_update_complete(struct interface *iface)
924 {
925 interface_ip_update_complete(&iface->proto_ip);
926 }
927
928 static void
929 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
930 {
931 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
932 vlist_simple_replace(&new->dns_search, &old->dns_search);
933 }
934
935 static void
936 interface_change_config(struct interface *if_old, struct interface *if_new)
937 {
938 struct blob_attr *old_config = if_old->config;
939 bool reload = false, reload_ip = false;
940
941 #define FIELD_CHANGED_STR(field) \
942 ((!!if_old->field != !!if_new->field) || \
943 (if_old->field && \
944 strcmp(if_old->field, if_new->field) != 0))
945
946 if (FIELD_CHANGED_STR(parent_ifname)) {
947 if (if_old->parent_iface.iface)
948 interface_remove_user(&if_old->parent_iface);
949 reload = true;
950 }
951
952 if (FIELD_CHANGED_STR(ifname) ||
953 if_old->proto_handler != if_new->proto_handler)
954 reload = true;
955
956 if (!if_old->proto_handler->config_params)
957 D(INTERFACE, "No config parameters for interface '%s'\n",
958 if_old->name);
959 else if (!uci_blob_check_equal(if_old->config, if_new->config,
960 if_old->proto_handler->config_params))
961 reload = true;
962
963 #define UPDATE(field, __var) ({ \
964 bool __changed = (if_old->field != if_new->field); \
965 if_old->field = if_new->field; \
966 __var |= __changed; \
967 })
968
969 if_old->config = if_new->config;
970 if (!if_old->config_autostart && if_new->config_autostart)
971 if_old->autostart = true;
972
973 if_old->device_config = if_new->device_config;
974 if_old->config_autostart = if_new->config_autostart;
975 if_old->ifname = if_new->ifname;
976 if_old->parent_ifname = if_new->parent_ifname;
977 if_old->proto_handler = if_new->proto_handler;
978
979 if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
980 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
981
982 UPDATE(metric, reload_ip);
983 UPDATE(proto_ip.no_defaultroute, reload_ip);
984 UPDATE(ip4table, reload_ip);
985 UPDATE(ip6table, reload_ip);
986 interface_merge_assignment_data(if_old, if_new);
987
988 #undef UPDATE
989
990 if (reload) {
991 D(INTERFACE, "Reload interface '%s' because of config changes\n",
992 if_old->name);
993 interface_clear_errors(if_old);
994 set_config_state(if_old, IFC_RELOAD);
995 goto out;
996 }
997
998 if (reload_ip) {
999 interface_ip_set_enabled(&if_old->config_ip, false);
1000 interface_ip_set_enabled(&if_old->proto_ip, false);
1001 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
1002 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
1003 }
1004
1005 interface_write_resolv_conf();
1006
1007 out:
1008 if_new->config = NULL;
1009 interface_cleanup(if_new);
1010 free(old_config);
1011 free(if_new);
1012 }
1013
1014 static void
1015 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
1016 struct vlist_node *node_old)
1017 {
1018 struct interface *if_old = container_of(node_old, struct interface, node);
1019 struct interface *if_new = container_of(node_new, struct interface, node);
1020
1021 if (node_old && node_new) {
1022 D(INTERFACE, "Update interface '%s'\n", if_new->name);
1023 interface_change_config(if_old, if_new);
1024 } else if (node_old) {
1025 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
1026 set_config_state(if_old, IFC_REMOVE);
1027 } else if (node_new) {
1028 D(INTERFACE, "Create interface '%s'\n", if_new->name);
1029 proto_init_interface(if_new, if_new->config);
1030 interface_claim_device(if_new);
1031 netifd_ubus_add_interface(if_new);
1032 }
1033 }
1034
1035
1036 static void __init
1037 interface_init_list(void)
1038 {
1039 vlist_init(&interfaces, avl_strcmp, interface_update);
1040 interfaces.keep_old = true;
1041 interfaces.no_delete = true;
1042 }