Add option to define target routing table for protocol routes.
[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 static unsigned int interface_serial = 0;
30
31 enum {
32         IFACE_ATTR_IFNAME,
33         IFACE_ATTR_PROTO,
34         IFACE_ATTR_AUTO,
35         IFACE_ATTR_DEFAULTROUTE,
36         IFACE_ATTR_PEERDNS,
37         IFACE_ATTR_DNS,
38         IFACE_ATTR_DNS_SEARCH,
39         IFACE_ATTR_METRIC,
40         IFACE_ATTR_INTERFACE,
41         IFACE_ATTR_IP6ASSIGN,
42         IFACE_ATTR_IP6HINT,
43         IFACE_ATTR_IP4TABLE,
44         IFACE_ATTR_IP6TABLE,
45         IFACE_ATTR_MAX
46 };
47
48 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
49         [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
50         [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
51         [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
52         [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
53         [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
54         [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
55         [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
56         [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
57         [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
58         [IFACE_ATTR_IP6ASSIGN] = { .name = "ip6assign", .type = BLOBMSG_TYPE_INT32 },
59         [IFACE_ATTR_IP6HINT] = { .name = "ip6hint", .type = BLOBMSG_TYPE_STRING },
60         [IFACE_ATTR_IP4TABLE] = { .name = "ip4table", .type = BLOBMSG_TYPE_STRING },
61         [IFACE_ATTR_IP6TABLE] = { .name = "ip6table", .type = BLOBMSG_TYPE_STRING },
62 };
63
64 static const union config_param_info iface_attr_info[IFACE_ATTR_MAX] = {
65         [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
66 };
67
68 const struct config_param_list interface_attr_list = {
69         .n_params = IFACE_ATTR_MAX,
70         .params = iface_attrs,
71         .info = iface_attr_info,
72 };
73
74 static void
75 interface_clear_errors(struct interface *iface)
76 {
77         struct interface_error *error, *tmp;
78
79         list_for_each_entry_safe(error, tmp, &iface->errors, list) {
80                 list_del(&error->list);
81                 free(error);
82         }
83 }
84
85 void interface_add_error(struct interface *iface, const char *subsystem,
86                          const char *code, const char **data, int n_data)
87 {
88         struct interface_error *error;
89         int i, len = 0;
90         int *datalen = NULL;
91         char *dest, *d_subsys, *d_code;
92
93         if (n_data) {
94                 len = n_data * sizeof(char *);
95                 datalen = alloca(len);
96                 for (i = 0; i < n_data; i++) {
97                         datalen[i] = strlen(data[i]) + 1;
98                         len += datalen[i];
99                 }
100         }
101
102         error = calloc_a(sizeof(*error) + sizeof(char *) + len,
103                 &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
104                 &d_code, code ? strlen(code) + 1 : 0);
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         if (subsystem)
119                 error->subsystem = strcpy(d_subsys, subsystem);
120
121         if (code)
122                 error->code = strcpy(d_code, code);
123 }
124
125 static void
126 interface_data_del(struct interface *iface, struct interface_data *data)
127 {
128         avl_delete(&iface->data, &data->node);
129         free(data);
130 }
131
132 static void
133 interface_data_flush(struct interface *iface)
134 {
135         struct interface_data *d, *tmp;
136
137         avl_for_each_element_safe(&iface->data, d, node, tmp)
138                 interface_data_del(iface, d);
139 }
140
141 int
142 interface_add_data(struct interface *iface, const struct blob_attr *data)
143 {
144         struct interface_data *n, *o;
145
146         if (!blobmsg_check_attr(data, true))
147                 return UBUS_STATUS_INVALID_ARGUMENT;
148
149         n = calloc(1, sizeof(*n) + blob_pad_len(data));
150         memcpy(n->data, data, blob_pad_len(data));
151         n->node.key = blobmsg_name(data);
152
153         o = avl_find_element(&iface->data, n->node.key, o, node);
154         if (o)
155                 interface_data_del(iface, o);
156
157         avl_insert(&iface->data, &n->node);
158         return 0;
159 }
160
161 static void
162 interface_event(struct interface *iface, enum interface_event ev)
163 {
164         struct interface_user *dep, *tmp;
165         struct device *adev = NULL;
166
167         list_for_each_entry_safe(dep, tmp, &iface->users, list)
168                 dep->cb(dep, iface, ev);
169
170         list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
171                 dep->cb(dep, iface, ev);
172
173         switch (ev) {
174         case IFEV_UP:
175                 adev = iface->l3_dev.dev;
176                 /* fall through */
177         case IFEV_DOWN:
178                 alias_notify_device(iface->name, adev);
179                 break;
180         default:
181                 break;
182         }
183 }
184
185 static void
186 interface_flush_state(struct interface *iface)
187 {
188         if (iface->l3_dev.dev)
189                 device_release(&iface->l3_dev);
190         interface_data_flush(iface);
191 }
192
193 static void
194 mark_interface_down(struct interface *iface)
195 {
196         enum interface_state state = iface->state;
197
198         iface->state = IFS_DOWN;
199         if (state == IFS_UP)
200                 interface_event(iface, IFEV_DOWN);
201         interface_ip_set_enabled(&iface->config_ip, false);
202         interface_ip_flush(&iface->proto_ip);
203         interface_flush_state(iface);
204         system_flush_routes();
205 }
206
207 void
208 __interface_set_down(struct interface *iface, bool force)
209 {
210         if (iface->state == IFS_DOWN ||
211                 iface->state == IFS_TEARDOWN)
212                 return;
213
214         if (iface->state == IFS_UP)
215                 interface_event(iface, IFEV_DOWN);
216         iface->state = IFS_TEARDOWN;
217         interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
218         if (force)
219                 interface_flush_state(iface);
220 }
221
222 static void
223 interface_cb(struct device_user *dep, enum device_event ev)
224 {
225         struct interface *iface;
226         bool new_state;
227
228         iface = container_of(dep, struct interface, main_dev);
229         switch (ev) {
230         case DEV_EVENT_ADD:
231                 new_state = true;
232                 break;
233         case DEV_EVENT_REMOVE:
234                 new_state = false;
235                 break;
236         default:
237                 return;
238         }
239
240         interface_set_available(iface, new_state);
241         if (!new_state && dep->dev->external)
242                 interface_set_main_dev(iface, NULL);
243 }
244
245 void
246 interface_set_available(struct interface *iface, bool new_state)
247 {
248         if (iface->available == new_state)
249                 return;
250
251         D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
252         iface->available = new_state;
253
254         if (new_state) {
255                 if (iface->autostart && !config_init)
256                         interface_set_up(iface);
257         } else
258                 __interface_set_down(iface, true);
259 }
260
261 void
262 interface_add_user(struct interface_user *dep, struct interface *iface)
263 {
264         if (!iface) {
265                 list_add(&dep->list, &iface_all_users);
266                 return;
267         }
268
269         dep->iface = iface;
270         list_add(&dep->list, &iface->users);
271         if (iface->state == IFS_UP)
272                 dep->cb(dep, iface, IFEV_UP);
273 }
274
275 void
276 interface_remove_user(struct interface_user *dep)
277 {
278         list_del_init(&dep->list);
279         dep->iface = NULL;
280 }
281
282 static void
283 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
284 {
285         struct interface *alias = container_of(dep, struct interface, parent_iface);
286         struct device *dev = iface->l3_dev.dev;
287
288         switch (ev) {
289         case IFEV_UP:
290                 if (!dev)
291                         return;
292
293                 interface_set_main_dev(alias, dev);
294                 interface_set_available(alias, true);
295                 break;
296         case IFEV_DOWN:
297                 interface_set_available(alias, false);
298                 interface_set_main_dev(alias, NULL);
299                 break;
300         case IFEV_FREE:
301                 interface_remove_user(dep);
302                 break;
303         case IFEV_RELOAD:
304                 break;
305         }
306 }
307
308 static void
309 interface_claim_device(struct interface *iface)
310 {
311         struct interface *parent;
312         struct device *dev = NULL;
313
314         if (iface->parent_iface.iface)
315                 interface_remove_user(&iface->parent_iface);
316
317         if (iface->parent_ifname) {
318                 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
319                 iface->parent_iface.cb = interface_alias_cb;
320                 interface_add_user(&iface->parent_iface, parent);
321         } else if (iface->ifname &&
322                 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
323                 dev = device_get(iface->ifname, true);
324         }
325
326         if (dev)
327                 interface_set_main_dev(iface, dev);
328
329         if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
330                 interface_set_available(iface, true);
331 }
332
333 static void
334 interface_cleanup_state(struct interface *iface)
335 {
336         interface_set_available(iface, false);
337
338         interface_flush_state(iface);
339         interface_clear_errors(iface);
340         interface_set_proto_state(iface, NULL);
341
342         if (iface->main_dev.dev)
343                 interface_set_main_dev(iface, NULL);
344 }
345
346 static void
347 interface_cleanup(struct interface *iface)
348 {
349         struct interface_user *dep, *tmp;
350
351         if (iface->parent_iface.iface)
352                 interface_remove_user(&iface->parent_iface);
353
354         list_for_each_entry_safe(dep, tmp, &iface->users, list)
355                 interface_remove_user(dep);
356
357         interface_ip_flush(&iface->config_ip);
358         interface_cleanup_state(iface);
359 }
360
361 static void
362 interface_do_free(struct interface *iface)
363 {
364         interface_event(iface, IFEV_FREE);
365         interface_cleanup(iface);
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_state(iface);
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         if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
498                 iface->config_ip.assignment_length = blobmsg_get_u32(cur);
499
500         iface->config_ip.assignment_hint = -1;
501         if ((cur = tb[IFACE_ATTR_IP6HINT]))
502                 iface->config_ip.assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
503                                 ~((1 << (64 - iface->config_ip.assignment_length)) - 1);
504
505         if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
506                 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
507                         DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
508         }
509
510         // Set a default exteranl routing table for IPv6 to do source-based-filtering
511         iface->ip6table = 1000 + ++interface_serial;
512         if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
513                 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
514                         DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
515         }
516
517         iface->config_autostart = iface->autostart;
518 }
519
520 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
521 {
522         struct blob_attr *tb[IFACE_ATTR_MAX];
523         struct blob_attr *cur;
524
525         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
526                       blob_data(config), blob_len(config));
527
528         if (alias) {
529                 if ((cur = tb[IFACE_ATTR_INTERFACE]))
530                         iface->parent_ifname = blobmsg_data(cur);
531
532                 if (!iface->parent_ifname)
533                         return false;
534         } else {
535                 if ((cur = tb[IFACE_ATTR_IFNAME]))
536                         iface->ifname = blobmsg_data(cur);
537         }
538
539
540         iface->config = config;
541         vlist_add(&interfaces, &iface->node, iface->name);
542         return true;
543 }
544
545 void
546 interface_add(struct interface *iface, struct blob_attr *config)
547 {
548         __interface_add(iface, config, false);
549 }
550
551 bool
552 interface_add_alias(struct interface *iface, struct blob_attr *config)
553 {
554         if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
555                 return false;
556
557         return __interface_add(iface, config, true);
558 }
559
560 void
561 interface_set_l3_dev(struct interface *iface, struct device *dev)
562 {
563         bool enabled = iface->config_ip.enabled;
564         bool claimed = iface->l3_dev.claimed;
565
566         if (iface->l3_dev.dev == dev)
567                 return;
568
569         interface_ip_set_enabled(&iface->config_ip, false);
570         interface_ip_flush(&iface->proto_ip);
571         device_add_user(&iface->l3_dev, dev);
572
573         if (dev) {
574                 if (claimed)
575                         device_claim(&iface->l3_dev);
576                 interface_ip_set_enabled(&iface->config_ip, enabled);
577         }
578 }
579
580 void
581 interface_set_main_dev(struct interface *iface, struct device *dev)
582 {
583         bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
584         bool claimed = iface->l3_dev.claimed;
585
586         if (iface->main_dev.dev == dev)
587                 return;
588
589         if (set_l3)
590                 interface_set_l3_dev(iface, dev);
591
592         device_add_user(&iface->main_dev, dev);
593         if (claimed)
594                 device_claim(&iface->l3_dev);
595
596         if (!iface->l3_dev.dev)
597                 interface_set_l3_dev(iface, dev);
598 }
599
600 int
601 interface_remove_link(struct interface *iface, struct device *dev)
602 {
603         struct device *mdev = iface->main_dev.dev;
604
605         if (mdev && mdev->hotplug_ops)
606                 return mdev->hotplug_ops->del(mdev, dev);
607
608         if (!iface->main_dev.hotplug)
609                 return UBUS_STATUS_INVALID_ARGUMENT;
610
611         if (dev != iface->main_dev.dev)
612                 return UBUS_STATUS_INVALID_ARGUMENT;
613
614         device_remove_user(&iface->main_dev);
615         return 0;
616 }
617
618 int
619 interface_add_link(struct interface *iface, struct device *dev)
620 {
621         struct device *mdev = iface->main_dev.dev;
622
623         if (mdev == dev)
624                 return 0;
625
626         if (iface->main_dev.hotplug)
627                 device_remove_user(&iface->main_dev);
628
629         if (mdev) {
630                 if (mdev->hotplug_ops)
631                         return mdev->hotplug_ops->add(mdev, dev);
632                 else
633                         return UBUS_STATUS_NOT_SUPPORTED;
634         }
635
636         interface_set_main_dev(iface, dev);
637         iface->main_dev.hotplug = true;
638         return 0;
639 }
640
641 int
642 interface_set_up(struct interface *iface)
643 {
644         int ret;
645
646         iface->autostart = true;
647
648         if (iface->state != IFS_DOWN)
649                 return 0;
650
651         interface_clear_errors(iface);
652         if (!iface->available) {
653                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
654                 return -1;
655         }
656
657         if (iface->main_dev.dev) {
658                 ret = device_claim(&iface->main_dev);
659                 if (ret)
660                         return ret;
661         }
662
663         iface->state = IFS_SETUP;
664         ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
665         if (ret) {
666                 mark_interface_down(iface);
667                 return ret;
668         }
669
670         return 0;
671 }
672
673 int
674 interface_set_down(struct interface *iface)
675 {
676         if (!iface) {
677                 vlist_for_each_element(&interfaces, iface, node)
678                         __interface_set_down(iface, false);
679         } else {
680                 iface->autostart = false;
681                 __interface_set_down(iface, false);
682         }
683
684         return 0;
685 }
686
687 void
688 interface_start_pending(void)
689 {
690         struct interface *iface;
691
692         vlist_for_each_element(&interfaces, iface, node) {
693                 if (iface->available && iface->autostart)
694                         interface_set_up(iface);
695         }
696 }
697
698 static void
699 set_config_state(struct interface *iface, enum interface_config_state s)
700 {
701         iface->config_state = s;
702         if (iface->state == IFS_DOWN)
703                 interface_handle_config_change(iface);
704         else
705                 __interface_set_down(iface, false);
706 }
707
708 void
709 interface_update_start(struct interface *iface)
710 {
711         interface_ip_update_start(&iface->proto_ip);
712 }
713
714 void
715 interface_update_complete(struct interface *iface)
716 {
717         interface_ip_update_complete(&iface->proto_ip);
718 }
719
720 static void
721 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
722 {
723         vlist_simple_replace(&new->dns_servers, &old->dns_servers);
724         vlist_simple_replace(&new->dns_search, &old->dns_search);
725 }
726
727 static void
728 interface_change_config(struct interface *if_old, struct interface *if_new)
729 {
730         struct blob_attr *old_config = if_old->config;
731         bool reload = false, reload_ip = false, reload_assignment = false;
732
733 #define FIELD_CHANGED_STR(field)                                        \
734                 ((!!if_old->field != !!if_new->field) ||                \
735                  (if_old->field &&                                      \
736                   strcmp(if_old->field, if_new->field) != 0))
737
738         if (FIELD_CHANGED_STR(parent_ifname)) {
739                 if (if_old->parent_iface.iface)
740                         interface_remove_user(&if_old->parent_iface);
741                 reload = true;
742         }
743
744         if (FIELD_CHANGED_STR(ifname) ||
745             if_old->proto_handler != if_new->proto_handler)
746                 reload = true;
747
748         if (!if_old->proto_handler->config_params)
749                 D(INTERFACE, "No config parameters for interface '%s'\n",
750                   if_old->name);
751         else if (!config_check_equal(if_old->config, if_new->config,
752                                      if_old->proto_handler->config_params))
753                 reload = true;
754
755 #define UPDATE(field, __var) ({                                         \
756                 bool __changed = (if_old->field != if_new->field);      \
757                 if_old->field = if_new->field;                          \
758                 __var |= __changed;                                     \
759         })
760
761         if_old->config = if_new->config;
762         if (!if_old->config_autostart && if_new->config_autostart)
763                 if_old->autostart = true;
764
765         if_old->device_config = if_new->device_config;
766         if_old->config_autostart = if_new->config_autostart;
767         if_old->ifname = if_new->ifname;
768         if_old->parent_ifname = if_new->parent_ifname;
769         if_old->proto_handler = if_new->proto_handler;
770
771         if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
772         interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
773
774         UPDATE(metric, reload_ip);
775         UPDATE(proto_ip.no_defaultroute, reload_ip);
776         UPDATE(config_ip.assignment_length, reload_assignment);
777         UPDATE(config_ip.assignment_hint, reload_assignment);
778
779 #undef UPDATE
780
781         if (reload) {
782                 D(INTERFACE, "Reload interface '%s because of config changes\n",
783                   if_old->name);
784                 interface_clear_errors(if_old);
785                 set_config_state(if_old, IFC_RELOAD);
786                 goto out;
787         }
788
789         if (reload_ip) {
790                 interface_ip_set_enabled(&if_old->config_ip, false);
791                 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
792                 interface_ip_set_enabled(&if_old->proto_ip, false);
793                 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
794         }
795
796         if (reload_assignment)
797                 interface_refresh_assignments(true);
798
799         interface_write_resolv_conf();
800
801 out:
802         if_new->config = NULL;
803         interface_cleanup(if_new);
804         free(old_config);
805         free(if_new);
806 }
807
808 static void
809 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
810                  struct vlist_node *node_old)
811 {
812         struct interface *if_old = container_of(node_old, struct interface, node);
813         struct interface *if_new = container_of(node_new, struct interface, node);
814
815         if (node_old && node_new) {
816                 D(INTERFACE, "Update interface '%s'\n", if_new->name);
817                 interface_change_config(if_old, if_new);
818         } else if (node_old) {
819                 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
820                 set_config_state(if_old, IFC_REMOVE);
821         } else if (node_new) {
822                 D(INTERFACE, "Create interface '%s'\n", if_new->name);
823                 proto_init_interface(if_new, if_new->config);
824                 interface_claim_device(if_new);
825                 netifd_ubus_add_interface(if_new);
826         }
827 }
828
829
830 static void __init
831 interface_init_list(void)
832 {
833         vlist_init(&interfaces, avl_strcmp, interface_update);
834         interfaces.keep_old = true;
835         interfaces.no_delete = true;
836 }