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