interface: support "device" attribute and deprecate "ifname"
[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 {
1039 struct device *mdev = iface->main_dev.dev;
1040
1041 if (mdev && mdev->hotplug_ops)
1042 return mdev->hotplug_ops->del(mdev, dev);
1043
1044 if (dev == iface->ext_dev.dev)
1045 device_remove_user(&iface->ext_dev);
1046
1047 if (!iface->main_dev.hotplug)
1048 return UBUS_STATUS_INVALID_ARGUMENT;
1049
1050 if (dev != iface->main_dev.dev)
1051 return UBUS_STATUS_INVALID_ARGUMENT;
1052
1053 interface_set_main_dev(iface, NULL);
1054 return 0;
1055 }
1056
1057 static int
1058 interface_add_link(struct interface *iface, struct device *dev,
1059 struct blob_attr *vlan, bool link_ext)
1060 {
1061 struct device *mdev = iface->main_dev.dev;
1062
1063 if (mdev == dev)
1064 return 0;
1065
1066 if (iface->main_dev.hotplug)
1067 device_remove_user(&iface->main_dev);
1068
1069 if (mdev) {
1070 if (mdev->hotplug_ops)
1071 return mdev->hotplug_ops->add(mdev, dev, vlan);
1072 else
1073 return UBUS_STATUS_NOT_SUPPORTED;
1074 }
1075
1076 if (link_ext)
1077 device_add_user(&iface->ext_dev, dev);
1078
1079 interface_set_main_dev(iface, dev);
1080 iface->main_dev.hotplug = true;
1081 return 0;
1082 }
1083
1084 int
1085 interface_handle_link(struct interface *iface, const char *name,
1086 struct blob_attr *vlan, bool add, bool link_ext)
1087 {
1088 struct device *dev;
1089 int ret;
1090
1091 device_lock();
1092
1093 dev = device_get(name, add ? (link_ext ? 2 : 1) : 0);
1094 if (!dev) {
1095 ret = UBUS_STATUS_NOT_FOUND;
1096 goto out;
1097 }
1098
1099 if (add) {
1100 interface_set_device_config(iface, dev);
1101 if (!link_ext)
1102 device_set_present(dev, true);
1103
1104 ret = interface_add_link(iface, dev, vlan, link_ext);
1105 } else {
1106 ret = interface_remove_link(iface, dev);
1107 }
1108
1109 out:
1110 device_unlock();
1111
1112 return ret;
1113 }
1114
1115 void
1116 interface_set_up(struct interface *iface)
1117 {
1118 int ret;
1119 const char *error = NULL;
1120
1121 iface->autostart = true;
1122
1123 if (iface->state != IFS_DOWN)
1124 return;
1125
1126 interface_clear_errors(iface);
1127 if (iface->available) {
1128 if (iface->main_dev.dev) {
1129 ret = device_claim(&iface->main_dev);
1130 if (!ret)
1131 interface_check_state(iface);
1132 else
1133 error = "DEVICE_CLAIM_FAILED";
1134 } else {
1135 ret = __interface_set_up(iface);
1136 if (ret)
1137 error = "SETUP_FAILED";
1138 }
1139 } else
1140 error = "NO_DEVICE";
1141
1142 if (error)
1143 interface_add_error(iface, "interface", error, NULL, 0);
1144 }
1145
1146 void
1147 interface_set_down(struct interface *iface)
1148 {
1149 if (!iface) {
1150 vlist_for_each_element(&interfaces, iface, node)
1151 __interface_set_down(iface, false);
1152 } else {
1153 iface->autostart = false;
1154 __interface_set_down(iface, false);
1155 }
1156 }
1157
1158 int
1159 interface_renew(struct interface *iface)
1160 {
1161 if (iface->state == IFS_TEARDOWN || iface->state == IFS_DOWN)
1162 return -1;
1163
1164 return interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
1165 }
1166
1167 void
1168 interface_start_pending(void)
1169 {
1170 struct interface *iface;
1171
1172 vlist_for_each_element(&interfaces, iface, node) {
1173 if (iface->autostart)
1174 interface_set_up(iface);
1175 }
1176 }
1177
1178 void
1179 interface_start_jail(const char *jail, const pid_t netns_pid)
1180 {
1181 struct interface *iface;
1182 int netns_fd;
1183 int wstatus;
1184 pid_t pr = 0;
1185
1186 netns_fd = system_netns_open(netns_pid);
1187 if (netns_fd < 0)
1188 return;
1189
1190 vlist_for_each_element(&interfaces, iface, node) {
1191 if (!iface->jail || strcmp(iface->jail, jail))
1192 continue;
1193
1194 system_link_netns_move(iface->main_dev.dev, netns_fd, iface->jail_ifname);
1195 }
1196
1197 close(netns_fd);
1198
1199 pr = fork();
1200 if (pr) {
1201 waitpid(pr, &wstatus, WUNTRACED | WCONTINUED);
1202 return;
1203 }
1204
1205 /* child process */
1206 netns_fd = system_netns_open(netns_pid);
1207 if (netns_fd < 0)
1208 return;
1209
1210 system_netns_set(netns_fd);
1211 system_init();
1212 vlist_for_each_element(&interfaces, iface, node) {
1213 if (!iface->jail || strcmp(iface->jail, jail))
1214 continue;
1215
1216 /*
1217 * The interface has already been renamed and is inside target
1218 * namespace, hence overwrite ifname with jail_ifname for
1219 * interface_set_up().
1220 * We are inside a fork which got it's own copy of the interfaces
1221 * list, so we can mess with it :)
1222 */
1223 if (iface->jail_ifname)
1224 iface->device = iface->jail_ifname;
1225
1226 interface_do_reload(iface);
1227 interface_set_up(iface);
1228 }
1229
1230 close(netns_fd);
1231 _exit(0);
1232 }
1233
1234 void
1235 interface_stop_jail(const char *jail, const pid_t netns_pid)
1236 {
1237 struct interface *iface;
1238 int netns_fd, root_netns;
1239 int wstatus;
1240 pid_t parent_pid = getpid();
1241 pid_t pr = 0;
1242 const char *orig_ifname;
1243
1244 pr = fork();
1245 if (pr) {
1246 waitpid(pr, &wstatus, WUNTRACED | WCONTINUED);
1247 return;
1248 }
1249
1250 /* child process */
1251 root_netns = system_netns_open(parent_pid);
1252 if (root_netns < 0)
1253 return;
1254
1255 netns_fd = system_netns_open(netns_pid);
1256 if (netns_fd < 0)
1257 return;
1258
1259 system_netns_set(netns_fd);
1260 system_init();
1261 vlist_for_each_element(&interfaces, iface, node) {
1262 if (!iface->jail || strcmp(iface->jail, jail))
1263 continue;
1264
1265 orig_ifname = iface->device;
1266 if (iface->jail_ifname)
1267 iface->device = iface->jail_ifname;
1268
1269 interface_do_reload(iface);
1270 interface_set_down(iface);
1271 system_link_netns_move(iface->main_dev.dev, root_netns, orig_ifname);
1272 }
1273
1274 close(root_netns);
1275 close(netns_fd);
1276 _exit(0);
1277 }
1278
1279 static void
1280 set_config_state(struct interface *iface, enum interface_config_state s)
1281 {
1282 __set_config_state(iface, s);
1283 if (iface->state == IFS_DOWN)
1284 interface_handle_config_change(iface);
1285 else
1286 __interface_set_down(iface, false);
1287 }
1288
1289 void
1290 interface_update_start(struct interface *iface, const bool keep_old)
1291 {
1292 iface->updated = 0;
1293
1294 if (!keep_old)
1295 interface_ip_update_start(&iface->proto_ip);
1296 }
1297
1298 void
1299 interface_update_complete(struct interface *iface)
1300 {
1301 interface_ip_update_complete(&iface->proto_ip);
1302 }
1303
1304 static void
1305 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
1306 {
1307 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
1308 vlist_simple_replace(&new->dns_search, &old->dns_search);
1309 }
1310
1311 static bool
1312 interface_device_config_changed(struct interface *if_old, struct interface *if_new)
1313 {
1314 struct blob_attr *ntb[__DEV_ATTR_MAX];
1315 struct blob_attr *otb[__DEV_ATTR_MAX];
1316 struct device *dev = if_old->main_dev.dev;
1317 unsigned long diff = 0;
1318
1319 BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
1320
1321 if (!dev)
1322 return false;
1323
1324 if (if_old->device_config != if_new->device_config)
1325 return true;
1326
1327 if (!if_new->device_config)
1328 return false;
1329
1330 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb,
1331 blob_data(if_old->config), blob_len(if_old->config));
1332
1333 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, ntb,
1334 blob_data(if_new->config), blob_len(if_new->config));
1335
1336 uci_blob_diff(ntb, otb, &device_attr_list, &diff);
1337 return diff;
1338 }
1339
1340 static void
1341 interface_change_config(struct interface *if_old, struct interface *if_new)
1342 {
1343 struct blob_attr *old_config = if_old->config;
1344 bool reload = false, reload_ip = false, update_prefix_delegation = false;
1345
1346 #define FIELD_CHANGED_STR(field) \
1347 ((!!if_old->field != !!if_new->field) || \
1348 (if_old->field && \
1349 strcmp(if_old->field, if_new->field) != 0))
1350
1351 if (FIELD_CHANGED_STR(parent_ifname)) {
1352 if (if_old->parent_iface.iface)
1353 interface_remove_user(&if_old->parent_iface);
1354 reload = true;
1355 }
1356
1357 if (!reload && interface_device_config_changed(if_old, if_new))
1358 reload = true;
1359
1360 if (FIELD_CHANGED_STR(device) ||
1361 if_old->proto_handler != if_new->proto_handler)
1362 reload = true;
1363
1364 if (!if_old->proto_handler->config_params)
1365 D(INTERFACE, "No config parameters for interface '%s'\n",
1366 if_old->name);
1367 else if (!uci_blob_check_equal(if_old->config, if_new->config,
1368 if_old->proto_handler->config_params))
1369 reload = true;
1370
1371 #define UPDATE(field, __var) ({ \
1372 bool __changed = (if_old->field != if_new->field); \
1373 if_old->field = if_new->field; \
1374 __var |= __changed; \
1375 })
1376
1377 if_old->config = if_new->config;
1378 if (if_old->config_autostart != if_new->config_autostart) {
1379 if (if_old->config_autostart)
1380 reload = true;
1381
1382 if_old->autostart = if_new->config_autostart;
1383 }
1384
1385 if_old->device_config = if_new->device_config;
1386 if_old->config_autostart = if_new->config_autostart;
1387 if (if_old->jail)
1388 free(if_old->jail);
1389
1390 if_old->jail = if_new->jail;
1391 if (if_old->jail)
1392 if_old->autostart = false;
1393
1394 if (if_old->jail_ifname)
1395 free(if_old->jail_ifname);
1396
1397 if_old->jail_ifname = if_new->jail_ifname;
1398
1399 if_old->device = if_new->device;
1400 if_old->parent_ifname = if_new->parent_ifname;
1401 if_old->dynamic = if_new->dynamic;
1402 if_old->proto_handler = if_new->proto_handler;
1403 if_old->force_link = if_new->force_link;
1404 if_old->dns_metric = if_new->dns_metric;
1405
1406 if (if_old->proto_ip.no_delegation != if_new->proto_ip.no_delegation) {
1407 if_old->proto_ip.no_delegation = if_new->proto_ip.no_delegation;
1408 update_prefix_delegation = true;
1409 }
1410
1411 if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
1412 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
1413
1414 UPDATE(metric, reload_ip);
1415 UPDATE(proto_ip.no_defaultroute, reload_ip);
1416 UPDATE(ip4table, reload_ip);
1417 UPDATE(ip6table, reload_ip);
1418 interface_merge_assignment_data(if_old, if_new);
1419
1420 #undef UPDATE
1421
1422 if (reload) {
1423 D(INTERFACE, "Reload interface '%s' because of config changes\n",
1424 if_old->name);
1425 interface_clear_errors(if_old);
1426 set_config_state(if_old, IFC_RELOAD);
1427 goto out;
1428 }
1429
1430 if (reload_ip) {
1431 bool config_ip_enabled = if_old->config_ip.enabled;
1432 bool proto_ip_enabled = if_old->proto_ip.enabled;
1433
1434 interface_ip_set_enabled(&if_old->config_ip, false);
1435 interface_ip_set_enabled(&if_old->proto_ip, false);
1436 interface_ip_set_enabled(&if_old->proto_ip, proto_ip_enabled);
1437 interface_ip_set_enabled(&if_old->config_ip, config_ip_enabled);
1438 }
1439
1440 if (update_prefix_delegation)
1441 interface_update_prefix_delegation(&if_old->proto_ip);
1442
1443 interface_write_resolv_conf(if_old->jail);
1444 if (if_old->main_dev.dev)
1445 interface_check_state(if_old);
1446
1447 out:
1448 if_new->config = NULL;
1449 interface_cleanup(if_new);
1450 free(old_config);
1451 free(if_new);
1452 }
1453
1454 static void
1455 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
1456 struct vlist_node *node_old)
1457 {
1458 struct interface *if_old = container_of(node_old, struct interface, node);
1459 struct interface *if_new = container_of(node_new, struct interface, node);
1460
1461 if (node_old && node_new) {
1462 D(INTERFACE, "Update interface '%s'\n", if_new->name);
1463 interface_change_config(if_old, if_new);
1464 } else if (node_old) {
1465 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
1466 set_config_state(if_old, IFC_REMOVE);
1467 } else if (node_new) {
1468 D(INTERFACE, "Create interface '%s'\n", if_new->name);
1469 interface_event(if_new, IFEV_CREATE);
1470 proto_init_interface(if_new, if_new->config);
1471 interface_claim_device(if_new);
1472 netifd_ubus_add_interface(if_new);
1473 }
1474 }
1475
1476
1477 static void __init
1478 interface_init_list(void)
1479 {
1480 vlist_init(&interfaces, avl_strcmp, interface_update);
1481 interfaces.keep_old = true;
1482 interfaces.no_delete = true;
1483 }