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