b304e00e6a407b251132840e6d664aa6833e6de9
[project/netifd.git] / interface.c
1 /*
2  * netifd - network interface daemon
3  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17
18 #include "netifd.h"
19 #include "device.h"
20 #include "interface.h"
21 #include "interface-ip.h"
22 #include "proto.h"
23 #include "ubus.h"
24 #include "config.h"
25 #include "system.h"
26
27 struct vlist_tree interfaces;
28 static LIST_HEAD(iface_all_users);
29
30 enum {
31         IFACE_ATTR_IFNAME,
32         IFACE_ATTR_PROTO,
33         IFACE_ATTR_AUTO,
34         IFACE_ATTR_DEFAULTROUTE,
35         IFACE_ATTR_PEERDNS,
36         IFACE_ATTR_DNS,
37         IFACE_ATTR_DNS_SEARCH,
38         IFACE_ATTR_METRIC,
39         IFACE_ATTR_INTERFACE,
40         IFACE_ATTR_MAX
41 };
42
43 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
44         [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
45         [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
46         [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
47         [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
48         [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
49         [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
50         [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
51         [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
52         [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
53 };
54
55 static const union config_param_info iface_attr_info[IFACE_ATTR_MAX] = {
56         [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
57 };
58
59 const struct config_param_list interface_attr_list = {
60         .n_params = IFACE_ATTR_MAX,
61         .params = iface_attrs,
62         .info = iface_attr_info,
63 };
64
65 static void
66 interface_clear_errors(struct interface *iface)
67 {
68         struct interface_error *error, *tmp;
69
70         list_for_each_entry_safe(error, tmp, &iface->errors, list) {
71                 list_del(&error->list);
72                 free(error);
73         }
74 }
75
76 void interface_add_error(struct interface *iface, const char *subsystem,
77                          const char *code, const char **data, int n_data)
78 {
79         struct interface_error *error;
80         int i, len = 0;
81         int *datalen = NULL;
82         char *dest, *d_subsys, *d_code;
83
84         if (n_data) {
85                 len = n_data * sizeof(char *);
86                 datalen = alloca(len);
87                 for (i = 0; i < n_data; i++) {
88                         datalen[i] = strlen(data[i]) + 1;
89                         len += datalen[i];
90                 }
91         }
92
93         error = calloc_a(sizeof(*error) + sizeof(char *) + len,
94                 &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
95                 &d_code, code ? strlen(code) + 1 : 0);
96         if (!error)
97                 return;
98
99         list_add_tail(&error->list, &iface->errors);
100
101         dest = (char *) &error->data[n_data + 1];
102         for (i = 0; i < n_data; i++) {
103                 error->data[i] = dest;
104                 memcpy(dest, data[i], datalen[i]);
105                 dest += datalen[i];
106         }
107         error->data[n_data++] = NULL;
108
109         if (subsystem)
110                 error->subsystem = strcpy(d_subsys, subsystem);
111
112         if (code)
113                 error->code = strcpy(d_code, code);
114 }
115
116 static void
117 interface_data_del(struct interface *iface, struct interface_data *data)
118 {
119         avl_delete(&iface->data, &data->node);
120         free(data);
121 }
122
123 static void
124 interface_data_flush(struct interface *iface)
125 {
126         struct interface_data *d, *tmp;
127
128         avl_for_each_element_safe(&iface->data, d, node, tmp)
129                 interface_data_del(iface, d);
130 }
131
132 int
133 interface_add_data(struct interface *iface, const struct blob_attr *data)
134 {
135         struct interface_data *n, *o;
136
137         if (!blobmsg_check_attr(data, true))
138                 return UBUS_STATUS_INVALID_ARGUMENT;
139
140         n = calloc(1, sizeof(*n) + blob_pad_len(data));
141         memcpy(n->data, data, blob_pad_len(data));
142         n->node.key = blobmsg_name(data);
143
144         o = avl_find_element(&iface->data, n->node.key, o, node);
145         if (o)
146                 interface_data_del(iface, o);
147
148         avl_insert(&iface->data, &n->node);
149         return 0;
150 }
151
152 static void
153 interface_event(struct interface *iface, enum interface_event ev)
154 {
155         struct interface_user *dep, *tmp;
156         struct device *adev = NULL;
157
158         list_for_each_entry_safe(dep, tmp, &iface->users, list)
159                 dep->cb(dep, iface, ev);
160
161         list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
162                 dep->cb(dep, iface, ev);
163
164         switch (ev) {
165         case IFEV_UP:
166                 adev = iface->l3_dev.dev;
167                 /* fall through */
168         case IFEV_DOWN:
169                 alias_notify_device(iface->name, adev);
170                 break;
171         default:
172                 break;
173         }
174 }
175
176 static void
177 interface_flush_state(struct interface *iface)
178 {
179         if (iface->l3_dev.dev)
180                 device_release(&iface->l3_dev);
181         interface_data_flush(iface);
182 }
183
184 static void
185 mark_interface_down(struct interface *iface)
186 {
187         enum interface_state state = iface->state;
188
189         iface->state = IFS_DOWN;
190         if (state == IFS_UP)
191                 interface_event(iface, IFEV_DOWN);
192         interface_ip_set_enabled(&iface->config_ip, false);
193         interface_ip_flush(&iface->proto_ip);
194         interface_flush_state(iface);
195         system_flush_routes();
196 }
197
198 void
199 __interface_set_down(struct interface *iface, bool force)
200 {
201         if (iface->state == IFS_DOWN ||
202                 iface->state == IFS_TEARDOWN)
203                 return;
204
205         if (iface->state == IFS_UP)
206                 interface_event(iface, IFEV_DOWN);
207         iface->state = IFS_TEARDOWN;
208         interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
209         if (force)
210                 interface_flush_state(iface);
211 }
212
213 static void
214 interface_cb(struct device_user *dep, enum device_event ev)
215 {
216         struct interface *iface;
217         bool new_state;
218
219         iface = container_of(dep, struct interface, main_dev);
220         switch (ev) {
221         case DEV_EVENT_ADD:
222                 new_state = true;
223                 break;
224         case DEV_EVENT_REMOVE:
225                 new_state = false;
226                 break;
227         default:
228                 return;
229         }
230
231         interface_set_available(iface, new_state);
232         if (!new_state && dep->dev->external)
233                 interface_set_main_dev(iface, NULL);
234 }
235
236 void
237 interface_set_available(struct interface *iface, bool new_state)
238 {
239         if (iface->available == new_state)
240                 return;
241
242         D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
243         iface->available = new_state;
244
245         if (new_state) {
246                 if (iface->autostart && !config_init)
247                         interface_set_up(iface);
248         } else
249                 __interface_set_down(iface, true);
250 }
251
252 void
253 interface_add_user(struct interface_user *dep, struct interface *iface)
254 {
255         if (!iface) {
256                 list_add(&dep->list, &iface_all_users);
257                 return;
258         }
259
260         dep->iface = iface;
261         list_add(&dep->list, &iface->users);
262         if (iface->state == IFS_UP)
263                 dep->cb(dep, iface, IFEV_UP);
264 }
265
266 void
267 interface_remove_user(struct interface_user *dep)
268 {
269         list_del_init(&dep->list);
270         dep->iface = NULL;
271 }
272
273 static void
274 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
275 {
276         struct interface *alias = container_of(dep, struct interface, parent_iface);
277         struct device *dev = iface->l3_dev.dev;
278
279         switch (ev) {
280         case IFEV_UP:
281                 if (!dev)
282                         return;
283
284                 interface_set_main_dev(alias, dev);
285                 interface_set_available(alias, true);
286                 break;
287         case IFEV_DOWN:
288                 interface_set_available(alias, false);
289                 interface_set_main_dev(alias, NULL);
290                 break;
291         case IFEV_FREE:
292                 interface_remove_user(dep);
293                 break;
294         case IFEV_RELOAD:
295                 break;
296         }
297 }
298
299 static void
300 interface_claim_device(struct interface *iface)
301 {
302         struct interface *parent;
303         struct device *dev = NULL;
304
305         if (iface->parent_iface.iface)
306                 interface_remove_user(&iface->parent_iface);
307
308         if (iface->parent_ifname) {
309                 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
310                 iface->parent_iface.cb = interface_alias_cb;
311                 interface_add_user(&iface->parent_iface, parent);
312         } else if (iface->ifname &&
313                 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
314                 dev = device_get(iface->ifname, true);
315         }
316
317         if (dev)
318                 interface_set_main_dev(iface, dev);
319
320         if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
321                 interface_set_available(iface, true);
322 }
323
324 static void
325 interface_cleanup_state(struct interface *iface)
326 {
327         interface_flush_state(iface);
328         interface_clear_errors(iface);
329         interface_set_proto_state(iface, NULL);
330 }
331
332 static void
333 interface_cleanup(struct interface *iface)
334 {
335         struct interface_user *dep, *tmp;
336
337         if (iface->parent_iface.iface)
338                 interface_remove_user(&iface->parent_iface);
339
340         list_for_each_entry_safe(dep, tmp, &iface->users, list)
341                 interface_remove_user(dep);
342
343         interface_ip_flush(&iface->config_ip);
344         if (iface->main_dev.dev)
345                 interface_set_main_dev(iface, NULL);
346
347         interface_cleanup_state(iface);
348 }
349
350 static void
351 interface_do_free(struct interface *iface)
352 {
353         interface_event(iface, IFEV_FREE);
354         interface_cleanup(iface);
355         free(iface->config);
356         netifd_ubus_remove_interface(iface);
357         avl_delete(&interfaces.avl, &iface->node.avl);
358         free(iface);
359 }
360
361 static void
362 interface_do_reload(struct interface *iface)
363 {
364         interface_event(iface, IFEV_RELOAD);
365         interface_cleanup_state(iface);
366         proto_init_interface(iface, iface->config);
367         interface_claim_device(iface);
368 }
369
370 static void
371 interface_handle_config_change(struct interface *iface)
372 {
373         enum interface_config_state state = iface->config_state;
374
375         iface->config_state = IFC_NORMAL;
376         switch(state) {
377         case IFC_NORMAL:
378                 break;
379         case IFC_RELOAD:
380                 interface_do_reload(iface);
381                 break;
382         case IFC_REMOVE:
383                 interface_do_free(iface);
384                 return;
385         }
386         if (iface->autostart && iface->available)
387                 interface_set_up(iface);
388 }
389
390 static void
391 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
392 {
393         struct interface *iface = state->iface;
394
395         switch (ev) {
396         case IFPEV_UP:
397                 if (iface->state != IFS_SETUP)
398                         return;
399
400                 interface_ip_set_enabled(&iface->config_ip, true);
401                 system_flush_routes();
402                 iface->state = IFS_UP;
403                 iface->start_time = system_get_rtime();
404                 interface_event(iface, IFEV_UP);
405                 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
406                 break;
407         case IFPEV_DOWN:
408                 if (iface->state == IFS_DOWN)
409                         return;
410
411                 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
412                 mark_interface_down(iface);
413                 if (iface->main_dev.dev)
414                         device_release(&iface->main_dev);
415                 interface_handle_config_change(iface);
416                 break;
417         case IFPEV_LINK_LOST:
418                 if (iface->state != IFS_UP)
419                         return;
420
421                 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
422                 mark_interface_down(iface);
423                 iface->state = IFS_SETUP;
424                 break;
425         }
426
427         interface_write_resolv_conf();
428 }
429
430 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
431 {
432         if (iface->proto) {
433                 iface->proto->free(iface->proto);
434                 iface->proto = NULL;
435         }
436         iface->state = IFS_DOWN;
437         iface->proto = state;
438         if (!state)
439                 return;
440
441         state->proto_event = interface_proto_cb;
442         state->iface = iface;
443 }
444
445 void
446 interface_init(struct interface *iface, const char *name,
447                struct blob_attr *config)
448 {
449         struct blob_attr *tb[IFACE_ATTR_MAX];
450         struct blob_attr *cur;
451         const char *proto_name = NULL;
452
453         strncpy(iface->name, name, sizeof(iface->name) - 1);
454         INIT_LIST_HEAD(&iface->errors);
455         INIT_LIST_HEAD(&iface->users);
456         INIT_LIST_HEAD(&iface->hotplug_list);
457         interface_ip_init(iface);
458         avl_init(&iface->data, avl_strcmp, false, NULL);
459         iface->config_ip.enabled = false;
460
461         iface->main_dev.cb = interface_cb;
462
463         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
464                       blob_data(config), blob_len(config));
465
466         if ((cur = tb[IFACE_ATTR_PROTO]))
467                 proto_name = blobmsg_data(cur);
468
469         proto_attach_interface(iface, proto_name);
470
471         iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
472         iface->proto_ip.no_defaultroute =
473                 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
474         iface->proto_ip.no_dns =
475                 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
476
477         if ((cur = tb[IFACE_ATTR_DNS]))
478                 interface_add_dns_server_list(&iface->config_ip, cur);
479
480         if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
481                 interface_add_dns_search_list(&iface->config_ip, cur);
482
483         if ((cur = tb[IFACE_ATTR_METRIC]))
484                 iface->metric = blobmsg_get_u32(cur);
485
486         iface->config_autostart = iface->autostart;
487 }
488
489 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
490 {
491         struct blob_attr *tb[IFACE_ATTR_MAX];
492         struct blob_attr *cur;
493
494         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
495                       blob_data(config), blob_len(config));
496
497         if (alias) {
498                 if ((cur = tb[IFACE_ATTR_INTERFACE]))
499                         iface->parent_ifname = blobmsg_data(cur);
500
501                 if (!iface->parent_ifname)
502                         return false;
503         } else {
504                 if ((cur = tb[IFACE_ATTR_IFNAME]))
505                         iface->ifname = blobmsg_data(cur);
506         }
507
508
509         iface->config = config;
510         vlist_add(&interfaces, &iface->node, iface->name);
511         return true;
512 }
513
514 void
515 interface_add(struct interface *iface, struct blob_attr *config)
516 {
517         __interface_add(iface, config, false);
518 }
519
520 bool
521 interface_add_alias(struct interface *iface, struct blob_attr *config)
522 {
523         if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
524                 return false;
525
526         return __interface_add(iface, config, true);
527 }
528
529 void
530 interface_set_l3_dev(struct interface *iface, struct device *dev)
531 {
532         bool enabled = iface->config_ip.enabled;
533         bool claimed = iface->l3_dev.claimed;
534
535         if (iface->l3_dev.dev == dev)
536                 return;
537
538         interface_ip_set_enabled(&iface->config_ip, false);
539         interface_ip_flush(&iface->proto_ip);
540         device_add_user(&iface->l3_dev, dev);
541
542         if (dev) {
543                 if (claimed)
544                         device_claim(&iface->l3_dev);
545                 interface_ip_set_enabled(&iface->config_ip, enabled);
546         }
547 }
548
549 void
550 interface_set_main_dev(struct interface *iface, struct device *dev)
551 {
552         bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
553         bool claimed = iface->l3_dev.claimed;
554
555         if (iface->main_dev.dev == dev)
556                 return;
557
558         if (set_l3)
559                 interface_set_l3_dev(iface, dev);
560
561         device_add_user(&iface->main_dev, dev);
562         if (claimed)
563                 device_claim(&iface->l3_dev);
564
565         if (!iface->l3_dev.dev)
566                 interface_set_l3_dev(iface, dev);
567 }
568
569 int
570 interface_remove_link(struct interface *iface, struct device *dev)
571 {
572         struct device *mdev = iface->main_dev.dev;
573
574         if (mdev && mdev->hotplug_ops)
575                 return mdev->hotplug_ops->del(mdev, dev);
576
577         if (!iface->main_dev.hotplug)
578                 return UBUS_STATUS_INVALID_ARGUMENT;
579
580         if (dev != iface->main_dev.dev)
581                 return UBUS_STATUS_INVALID_ARGUMENT;
582
583         device_remove_user(&iface->main_dev);
584         return 0;
585 }
586
587 int
588 interface_add_link(struct interface *iface, struct device *dev)
589 {
590         struct device *mdev = iface->main_dev.dev;
591
592         if (mdev == dev)
593                 return 0;
594
595         if (iface->main_dev.hotplug)
596                 device_remove_user(&iface->main_dev);
597
598         if (mdev) {
599                 if (mdev->hotplug_ops)
600                         return mdev->hotplug_ops->add(mdev, dev);
601                 else
602                         return UBUS_STATUS_NOT_SUPPORTED;
603         }
604
605         interface_set_main_dev(iface, dev);
606         iface->main_dev.hotplug = true;
607         return 0;
608 }
609
610 int
611 interface_set_up(struct interface *iface)
612 {
613         int ret;
614
615         iface->autostart = true;
616
617         if (iface->state != IFS_DOWN)
618                 return 0;
619
620         interface_clear_errors(iface);
621         if (!iface->available) {
622                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
623                 return -1;
624         }
625
626         if (iface->main_dev.dev) {
627                 ret = device_claim(&iface->main_dev);
628                 if (ret)
629                         return ret;
630         }
631
632         iface->state = IFS_SETUP;
633         ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
634         if (ret) {
635                 mark_interface_down(iface);
636                 return ret;
637         }
638
639         return 0;
640 }
641
642 int
643 interface_set_down(struct interface *iface)
644 {
645         if (!iface) {
646                 vlist_for_each_element(&interfaces, iface, node)
647                         __interface_set_down(iface, false);
648         } else {
649                 iface->autostart = false;
650                 __interface_set_down(iface, false);
651         }
652
653         return 0;
654 }
655
656 void
657 interface_start_pending(void)
658 {
659         struct interface *iface;
660
661         vlist_for_each_element(&interfaces, iface, node) {
662                 if (iface->available && iface->autostart)
663                         interface_set_up(iface);
664         }
665 }
666
667 static void
668 set_config_state(struct interface *iface, enum interface_config_state s)
669 {
670         iface->config_state = s;
671         if (iface->state == IFS_DOWN)
672                 interface_handle_config_change(iface);
673         else
674                 __interface_set_down(iface, false);
675 }
676
677 void
678 interface_update_start(struct interface *iface)
679 {
680         interface_ip_update_start(&iface->proto_ip);
681 }
682
683 void
684 interface_update_complete(struct interface *iface)
685 {
686         interface_ip_update_complete(&iface->proto_ip);
687 }
688
689 static void
690 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
691 {
692         vlist_simple_replace(&new->dns_servers, &old->dns_servers);
693         vlist_simple_replace(&new->dns_search, &old->dns_search);
694 }
695
696 static void
697 interface_change_config(struct interface *if_old, struct interface *if_new)
698 {
699         struct blob_attr *old_config = if_old->config;
700         const char *old_ifname = if_old->ifname;
701         const char *old_parent_ifname = if_old->parent_ifname;
702         const struct proto_handler *proto = if_old->proto_handler;
703
704         interface_clear_errors(if_old);
705         if_old->config = if_new->config;
706         if (!if_old->config_autostart && if_new->config_autostart)
707                 if_old->autostart = true;
708
709         if_old->device_config = if_new->device_config;
710         if_old->config_autostart = if_new->config_autostart;
711         if_old->ifname = if_new->ifname;
712         if_old->parent_ifname = if_new->parent_ifname;
713         if_old->proto_handler = if_new->proto_handler;
714
715         if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
716         interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
717
718 #define FIELD_CHANGED_STR(field)                                        \
719                 ((!!if_old->field != !!old_ ## field) ||                \
720                  (old_ ## field &&                                      \
721                   strcmp(old_ ## field, if_old->field) != 0))
722
723         if (FIELD_CHANGED_STR(parent_ifname)) {
724                 if (if_old->parent_iface.iface)
725                         interface_remove_user(&if_old->parent_iface);
726                 goto reload;
727         }
728
729         if (FIELD_CHANGED_STR(ifname) || proto != if_new->proto_handler) {
730                 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
731                   if_old->name);
732                 goto reload;
733         }
734
735         if (!proto->config_params)
736                 D(INTERFACE, "No config parameters for interface '%s'\n",
737                   if_old->name);
738         else if (!config_check_equal(old_config, if_new->config,
739                                 proto->config_params)) {
740                 D(INTERFACE, "Reload interface '%s because of config changes\n",
741                   if_old->name);
742                 goto reload;
743         }
744
745 #define UPDATE(field) ({                                                \
746                 bool __changed = (if_old->field != if_new->field);      \
747                 if_old->field = if_new->field;                          \
748                 __changed;                                              \
749         })
750
751         if (UPDATE(metric) || UPDATE(proto_ip.no_defaultroute)) {
752                 interface_ip_set_enabled(&if_old->config_ip, false);
753                 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
754                 interface_ip_set_enabled(&if_old->proto_ip, false);
755                 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
756         }
757
758         interface_write_resolv_conf();
759
760 #undef UPDATE
761
762         goto out;
763
764 reload:
765         set_config_state(if_old, IFC_RELOAD);
766 out:
767         if_new->config = NULL;
768         interface_cleanup(if_new);
769         free(old_config);
770         free(if_new);
771 }
772
773 static void
774 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
775                  struct vlist_node *node_old)
776 {
777         struct interface *if_old = container_of(node_old, struct interface, node);
778         struct interface *if_new = container_of(node_new, struct interface, node);
779
780         if (node_old && node_new) {
781                 D(INTERFACE, "Update interface '%s'\n", if_new->name);
782                 interface_change_config(if_old, if_new);
783         } else if (node_old) {
784                 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
785                 set_config_state(if_old, IFC_REMOVE);
786         } else if (node_new) {
787                 D(INTERFACE, "Create interface '%s'\n", if_new->name);
788                 proto_init_interface(if_new, if_new->config);
789                 interface_claim_device(if_new);
790                 netifd_ubus_add_interface(if_new);
791         }
792 }
793
794
795 static void __init
796 interface_init_list(void)
797 {
798         vlist_init(&interfaces, avl_strcmp, interface_update);
799         interfaces.keep_old = true;
800         interfaces.no_delete = true;
801 }