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