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