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