system-linux: improve handling of device rename
[oweals/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 #include <sys/types.h>
18 #include <sys/wait.h>
19
20 #include "netifd.h"
21 #include "device.h"
22 #include "interface.h"
23 #include "interface-ip.h"
24 #include "proto.h"
25 #include "ubus.h"
26 #include "config.h"
27 #include "system.h"
28
29 struct vlist_tree interfaces;
30 static LIST_HEAD(iface_all_users);
31
32 enum {
33         IFACE_ATTR_IFNAME,
34         IFACE_ATTR_PROTO,
35         IFACE_ATTR_AUTO,
36         IFACE_ATTR_JAIL,
37         IFACE_ATTR_JAIL_IFNAME,
38         IFACE_ATTR_DEFAULTROUTE,
39         IFACE_ATTR_PEERDNS,
40         IFACE_ATTR_DNS,
41         IFACE_ATTR_DNS_SEARCH,
42         IFACE_ATTR_DNS_METRIC,
43         IFACE_ATTR_METRIC,
44         IFACE_ATTR_INTERFACE,
45         IFACE_ATTR_IP6ASSIGN,
46         IFACE_ATTR_IP6HINT,
47         IFACE_ATTR_IP4TABLE,
48         IFACE_ATTR_IP6TABLE,
49         IFACE_ATTR_IP6CLASS,
50         IFACE_ATTR_DELEGATE,
51         IFACE_ATTR_IP6IFACEID,
52         IFACE_ATTR_FORCE_LINK,
53         IFACE_ATTR_IP6WEIGHT,
54         IFACE_ATTR_MAX
55 };
56
57 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
58         [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
59         [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
60         [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
61         [IFACE_ATTR_JAIL] = { .name = "jail", .type = BLOBMSG_TYPE_STRING },
62         [IFACE_ATTR_JAIL_IFNAME] = { .name = "jail_ifname", .type = BLOBMSG_TYPE_STRING },
63         [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
64         [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
65         [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
66         [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
67         [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
68         [IFACE_ATTR_DNS_METRIC] = { .name = "dns_metric", .type = BLOBMSG_TYPE_INT32 },
69         [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
70         [IFACE_ATTR_IP6ASSIGN] = { .name = "ip6assign", .type = BLOBMSG_TYPE_INT32 },
71         [IFACE_ATTR_IP6HINT] = { .name = "ip6hint", .type = BLOBMSG_TYPE_STRING },
72         [IFACE_ATTR_IP4TABLE] = { .name = "ip4table", .type = BLOBMSG_TYPE_STRING },
73         [IFACE_ATTR_IP6TABLE] = { .name = "ip6table", .type = BLOBMSG_TYPE_STRING },
74         [IFACE_ATTR_IP6CLASS] = { .name = "ip6class", .type = BLOBMSG_TYPE_ARRAY },
75         [IFACE_ATTR_DELEGATE] = { .name = "delegate", .type = BLOBMSG_TYPE_BOOL },
76         [IFACE_ATTR_IP6IFACEID] = { .name = "ip6ifaceid", .type = BLOBMSG_TYPE_STRING },
77         [IFACE_ATTR_FORCE_LINK] = { .name = "force_link", .type = BLOBMSG_TYPE_BOOL },
78         [IFACE_ATTR_IP6WEIGHT] = { .name = "ip6weight", .type = BLOBMSG_TYPE_INT32 },
79 };
80
81 const struct uci_blob_param_list interface_attr_list = {
82         .n_params = IFACE_ATTR_MAX,
83         .params = iface_attrs,
84 };
85
86 static void
87 interface_set_main_dev(struct interface *iface, struct device *dev);
88 static void
89 interface_event(struct interface *iface, enum interface_event ev);
90
91 static void
92 interface_error_flush(struct interface *iface)
93 {
94         struct interface_error *error, *tmp;
95
96         list_for_each_entry_safe(error, tmp, &iface->errors, list) {
97                 list_del(&error->list);
98                 free(error);
99         }
100 }
101
102 static void
103 interface_clear_errors(struct interface *iface)
104 {
105         /* don't flush the errors in case the configured protocol handler matches the
106            running protocol handler and is having the last error capability */
107         if (!(iface->proto &&
108               (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
109               (iface->proto->handler->name == iface->proto_handler->name)))
110                 interface_error_flush(iface);
111 }
112
113 void interface_add_error(struct interface *iface, const char *subsystem,
114                          const char *code, const char **data, int n_data)
115 {
116         struct interface_error *error;
117         int i, len = 0;
118         int *datalen = NULL;
119         char *dest, *d_subsys, *d_code;
120
121         /* if the configured protocol handler has the last error support capability,
122            errors should only be added if the running protocol handler matches the
123            configured one */
124         if (iface->proto &&
125             (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
126             (iface->proto->handler->name != iface->proto_handler->name))
127                 return;
128
129         if (n_data) {
130                 len = n_data * sizeof(char *);
131                 datalen = alloca(len);
132                 for (i = 0; i < n_data; i++) {
133                         datalen[i] = strlen(data[i]) + 1;
134                         len += datalen[i];
135                 }
136         }
137
138         error = calloc_a(sizeof(*error) + sizeof(char *) + len,
139                 &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
140                 &d_code, code ? strlen(code) + 1 : 0);
141         if (!error)
142                 return;
143
144         /* Only keep the last flagged error, prevent this list grows unlimitted in case the
145            protocol can't be established (e.g auth failure) */
146         if (iface->proto_handler->flags & PROTO_FLAG_LASTERROR)
147                 interface_error_flush(iface);
148
149         list_add_tail(&error->list, &iface->errors);
150
151         dest = (char *) &error->data[n_data + 1];
152         for (i = 0; i < n_data; i++) {
153                 error->data[i] = dest;
154                 memcpy(dest, data[i], datalen[i]);
155                 dest += datalen[i];
156         }
157         error->data[n_data] = NULL;
158
159         if (subsystem)
160                 error->subsystem = strcpy(d_subsys, subsystem);
161
162         if (code)
163                 error->code = strcpy(d_code, code);
164 }
165
166 static void
167 interface_data_del(struct interface *iface, struct interface_data *data)
168 {
169         avl_delete(&iface->data, &data->node);
170         free(data);
171 }
172
173 static void
174 interface_data_flush(struct interface *iface)
175 {
176         struct interface_data *d, *tmp;
177
178         avl_for_each_element_safe(&iface->data, d, node, tmp)
179                 interface_data_del(iface, d);
180 }
181
182 int
183 interface_add_data(struct interface *iface, const struct blob_attr *data)
184 {
185         struct interface_data *n, *o;
186
187         if (!blobmsg_check_attr(data, true))
188                 return UBUS_STATUS_INVALID_ARGUMENT;
189
190         const char *name = blobmsg_name(data);
191         unsigned len = blob_pad_len(data);
192
193         o = avl_find_element(&iface->data, name, o, node);
194         if (o) {
195                 if (blob_pad_len(o->data) == len && !memcmp(o->data, data, len))
196                         return 0;
197
198                 interface_data_del(iface, o);
199         }
200
201         n = calloc(1, sizeof(*n) + len);
202         if (!n)
203                 return UBUS_STATUS_UNKNOWN_ERROR;
204
205         memcpy(n->data, data, len);
206         n->node.key = blobmsg_name(n->data);
207         avl_insert(&iface->data, &n->node);
208
209         iface->updated |= IUF_DATA;
210         return 0;
211 }
212
213 int interface_parse_data(struct interface *iface, const struct blob_attr *attr)
214 {
215         struct blob_attr *cur;
216         int rem, ret;
217
218         iface->updated = 0;
219
220         blob_for_each_attr(cur, attr, rem) {
221                 ret = interface_add_data(iface, cur);
222                 if (ret)
223                         return ret;
224         }
225
226         if (iface->updated && iface->state == IFS_UP)
227                 interface_event(iface, IFEV_UPDATE);
228
229         return 0;
230 }
231
232 static void
233 interface_event(struct interface *iface, enum interface_event ev)
234 {
235         struct interface_user *dep, *tmp;
236         struct device *adev = NULL;
237
238         list_for_each_entry_safe(dep, tmp, &iface->users, list)
239                 dep->cb(dep, iface, ev);
240
241         list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
242                 dep->cb(dep, iface, ev);
243
244         switch (ev) {
245         case IFEV_UP:
246                 interface_error_flush(iface);
247                 adev = iface->l3_dev.dev;
248                 /* fall through */
249         case IFEV_DOWN:
250         case IFEV_UP_FAILED:
251                 alias_notify_device(iface->name, adev);
252                 break;
253         default:
254                 break;
255         }
256 }
257
258 static void
259 interface_flush_state(struct interface *iface)
260 {
261         if (iface->l3_dev.dev)
262                 device_release(&iface->l3_dev);
263         interface_data_flush(iface);
264 }
265
266 static void
267 mark_interface_down(struct interface *iface)
268 {
269         enum interface_state state = iface->state;
270
271         if (state == IFS_DOWN)
272                 return;
273
274         iface->link_up_event = false;
275         iface->state = IFS_DOWN;
276         switch (state) {
277         case IFS_UP:
278         case IFS_TEARDOWN:
279                 interface_event(iface, IFEV_DOWN);
280                 break;
281         case IFS_SETUP:
282                 interface_event(iface, IFEV_UP_FAILED);
283                 break;
284         default:
285                 break;
286         }
287         interface_ip_set_enabled(&iface->config_ip, false);
288         interface_ip_set_enabled(&iface->proto_ip, false);
289         interface_ip_flush(&iface->proto_ip);
290         interface_flush_state(iface);
291         system_flush_routes();
292 }
293
294 static inline void
295 __set_config_state(struct interface *iface, enum interface_config_state s)
296 {
297         iface->config_state = s;
298 }
299
300 static void
301 __interface_set_down(struct interface *iface, bool force)
302 {
303         enum interface_state state = iface->state;
304         switch (state) {
305         case IFS_UP:
306         case IFS_SETUP:
307                 iface->state = IFS_TEARDOWN;
308                 if (iface->dynamic)
309                         __set_config_state(iface, IFC_REMOVE);
310
311                 if (state == IFS_UP)
312                         interface_event(iface, IFEV_DOWN);
313
314                 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
315                 if (force)
316                         interface_flush_state(iface);
317                 break;
318
319         case IFS_DOWN:
320                 if (iface->main_dev.dev)
321                         device_release(&iface->main_dev);
322         case IFS_TEARDOWN:
323         default:
324                 break;
325         }
326 }
327
328 static int
329 __interface_set_up(struct interface *iface)
330 {
331         int ret;
332
333         netifd_log_message(L_NOTICE, "Interface '%s' is setting up now\n", iface->name);
334
335         iface->state = IFS_SETUP;
336         ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
337         if (ret)
338                 mark_interface_down(iface);
339
340         return ret;
341 }
342
343 static void
344 interface_check_state(struct interface *iface)
345 {
346         bool link_state = iface->link_state || iface->force_link;
347
348         switch (iface->state) {
349         case IFS_UP:
350         case IFS_SETUP:
351                 if (!iface->enabled || !link_state) {
352                         iface->state = IFS_TEARDOWN;
353                         if (iface->dynamic)
354                                 __set_config_state(iface, IFC_REMOVE);
355
356                         interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
357                 }
358                 break;
359         case IFS_DOWN:
360                 if (!iface->available)
361                         return;
362
363                 if (iface->autostart && iface->enabled && link_state && !config_init)
364                         __interface_set_up(iface);
365                 break;
366         default:
367                 break;
368         }
369 }
370
371 static void
372 interface_set_enabled(struct interface *iface, bool new_state)
373 {
374         if (iface->enabled == new_state)
375                 return;
376
377         netifd_log_message(L_NOTICE, "Interface '%s' is %s\n", iface->name, new_state ? "enabled" : "disabled");
378         iface->enabled = new_state;
379         interface_check_state(iface);
380 }
381
382 static void
383 interface_set_link_state(struct interface *iface, bool new_state)
384 {
385         if (iface->link_state == new_state)
386                 return;
387
388         netifd_log_message(L_NOTICE, "Interface '%s' has link connectivity %s\n", iface->name, new_state ? "" : "loss");
389         iface->link_state = new_state;
390         interface_check_state(iface);
391
392         if (new_state && iface->force_link && iface->state == IFS_UP && !iface->link_up_event) {
393                 interface_event(iface, IFEV_LINK_UP);
394                 iface->link_up_event = true;
395         }
396 }
397
398 static void
399 interface_ext_dev_cb(struct device_user *dep, enum device_event ev)
400 {
401         if (ev == DEV_EVENT_REMOVE)
402                 device_remove_user(dep);
403 }
404
405 static void
406 interface_main_dev_cb(struct device_user *dep, enum device_event ev)
407 {
408         struct interface *iface;
409
410         iface = container_of(dep, struct interface, main_dev);
411         switch (ev) {
412         case DEV_EVENT_ADD:
413                 interface_set_available(iface, true);
414                 break;
415         case DEV_EVENT_REMOVE:
416                 interface_set_available(iface, false);
417                 if (dep->dev && dep->dev->external)
418                         interface_set_main_dev(iface, NULL);
419                 break;
420         case DEV_EVENT_UP:
421                 interface_set_enabled(iface, true);
422                 break;
423         case DEV_EVENT_DOWN:
424                 interface_set_enabled(iface, false);
425                 break;
426         case DEV_EVENT_LINK_UP:
427                 interface_set_link_state(iface, true);
428                 break;
429         case DEV_EVENT_LINK_DOWN:
430                 interface_set_link_state(iface, false);
431                 break;
432         case DEV_EVENT_TOPO_CHANGE:
433                 interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
434                 return;
435         default:
436                 break;
437         }
438 }
439
440 static void
441 interface_l3_dev_cb(struct device_user *dep, enum device_event ev)
442 {
443         struct interface *iface;
444
445         iface = container_of(dep, struct interface, l3_dev);
446         if (iface->l3_dev.dev == iface->main_dev.dev)
447                 return;
448
449         switch (ev) {
450         case DEV_EVENT_LINK_DOWN:
451                 if (iface->proto_handler->flags & PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN)
452                         interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
453                 break;
454         default:
455                 break;
456         }
457 }
458
459 void
460 interface_set_available(struct interface *iface, bool new_state)
461 {
462         if (iface->available == new_state)
463                 return;
464
465         D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
466         iface->available = new_state;
467
468         if (new_state) {
469                 if (iface->autostart && !config_init)
470                         interface_set_up(iface);
471         } else
472                 __interface_set_down(iface, true);
473 }
474
475 void
476 interface_add_user(struct interface_user *dep, struct interface *iface)
477 {
478         if (!iface) {
479                 list_add(&dep->list, &iface_all_users);
480                 return;
481         }
482
483         dep->iface = iface;
484         list_add(&dep->list, &iface->users);
485         if (iface->state == IFS_UP)
486                 dep->cb(dep, iface, IFEV_UP);
487 }
488
489 void
490 interface_remove_user(struct interface_user *dep)
491 {
492         list_del_init(&dep->list);
493         dep->iface = NULL;
494 }
495
496 static void
497 interface_add_assignment_classes(struct interface *iface, struct blob_attr *list)
498 {
499         struct blob_attr *cur;
500         int rem;
501
502         blobmsg_for_each_attr(cur, list, rem) {
503                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
504                         continue;
505
506                 if (!blobmsg_check_attr(cur, false))
507                         continue;
508
509                 struct interface_assignment_class *c = malloc(sizeof(*c) + blobmsg_data_len(cur));
510                 memcpy(c->name, blobmsg_data(cur), blobmsg_data_len(cur));
511                 list_add(&c->head, &iface->assignment_classes);
512         }
513 }
514
515 static void
516 interface_clear_assignment_classes(struct interface *iface)
517 {
518         while (!list_empty(&iface->assignment_classes)) {
519                 struct interface_assignment_class *c = list_first_entry(&iface->assignment_classes,
520                                 struct interface_assignment_class, head);
521                 list_del(&c->head);
522                 free(c);
523         }
524 }
525
526 static void
527 interface_merge_assignment_data(struct interface *old, struct interface *new)
528 {
529         bool changed = (old->assignment_hint != new->assignment_hint ||
530                         old->assignment_length != new->assignment_length ||
531                         old->assignment_iface_id_selection != new->assignment_iface_id_selection ||
532                         old->assignment_weight != new->assignment_weight ||
533                         (old->assignment_iface_id_selection == IFID_FIXED &&
534                          memcmp(&old->assignment_fixed_iface_id, &new->assignment_fixed_iface_id,
535                                 sizeof(old->assignment_fixed_iface_id))) ||
536                         list_empty(&old->assignment_classes) != list_empty(&new->assignment_classes));
537
538         struct interface_assignment_class *c;
539         list_for_each_entry(c, &new->assignment_classes, head) {
540                 /* Compare list entries one-by-one to see if there was a change */
541                 if (list_empty(&old->assignment_classes)) /* The new list is longer */
542                         changed = true;
543
544                 if (changed)
545                         break;
546
547                 struct interface_assignment_class *c_old = list_first_entry(&old->assignment_classes,
548                                 struct interface_assignment_class, head);
549
550                 if (strcmp(c_old->name, c->name)) /* An entry didn't match */
551                         break;
552
553                 list_del(&c_old->head);
554                 free(c_old);
555         }
556
557         /* The old list was longer than the new one or the last entry didn't match */
558         if (!list_empty(&old->assignment_classes)) {
559                 interface_clear_assignment_classes(old);
560                 changed = true;
561         }
562
563         list_splice_init(&new->assignment_classes, &old->assignment_classes);
564
565         if (changed) {
566                 old->assignment_hint = new->assignment_hint;
567                 old->assignment_length = new->assignment_length;
568                 old->assignment_iface_id_selection = new->assignment_iface_id_selection;
569                 old->assignment_fixed_iface_id = new->assignment_fixed_iface_id;
570                 old->assignment_weight = new->assignment_weight;
571                 interface_refresh_assignments(true);
572         }
573 }
574
575 static void
576 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
577 {
578         struct interface *alias = container_of(dep, struct interface, parent_iface);
579         struct device *dev = iface->l3_dev.dev;
580
581         switch (ev) {
582         case IFEV_UP:
583                 if (!dev)
584                         return;
585
586                 interface_set_main_dev(alias, dev);
587                 interface_set_available(alias, true);
588                 break;
589         case IFEV_DOWN:
590         case IFEV_UP_FAILED:
591                 interface_set_available(alias, false);
592                 interface_set_main_dev(alias, NULL);
593                 break;
594         case IFEV_FREE:
595                 interface_remove_user(dep);
596                 break;
597         default:
598                 break;
599         }
600 }
601
602 static void
603 interface_set_device_config(struct interface *iface, struct device *dev)
604 {
605         if (!dev || !dev->default_config)
606                 return;
607
608         if (!iface->device_config &&
609             (!dev->iface_config || dev->config_iface != iface))
610                 return;
611
612         dev->config_iface = iface;
613         dev->iface_config = iface->device_config;
614         device_apply_config(dev, dev->type, iface->config);
615 }
616
617 static void
618 interface_claim_device(struct interface *iface)
619 {
620         struct interface *parent;
621         struct device *dev = NULL;
622
623         if (iface->parent_iface.iface)
624                 interface_remove_user(&iface->parent_iface);
625
626         device_lock();
627
628         if (iface->parent_ifname) {
629                 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
630                 iface->parent_iface.cb = interface_alias_cb;
631                 interface_add_user(&iface->parent_iface, parent);
632         } else if (iface->ifname &&
633                 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
634                 dev = device_get(iface->ifname, true);
635                 interface_set_device_config(iface, dev);
636         } else {
637                 dev = iface->ext_dev.dev;
638         }
639
640         if (dev)
641                 interface_set_main_dev(iface, dev);
642
643         device_unlock();
644
645         if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
646                 interface_set_available(iface, true);
647 }
648
649 static void
650 interface_cleanup_state(struct interface *iface)
651 {
652         interface_set_available(iface, false);
653
654         interface_flush_state(iface);
655         interface_clear_errors(iface);
656         interface_set_proto_state(iface, NULL);
657
658         interface_set_main_dev(iface, NULL);
659         interface_set_l3_dev(iface, NULL);
660 }
661
662 static void
663 interface_cleanup(struct interface *iface)
664 {
665         struct interface_user *dep, *tmp;
666
667         uloop_timeout_cancel(&iface->remove_timer);
668         device_remove_user(&iface->ext_dev);
669
670         if (iface->parent_iface.iface)
671                 interface_remove_user(&iface->parent_iface);
672
673         list_for_each_entry_safe(dep, tmp, &iface->users, list)
674                 interface_remove_user(dep);
675
676         interface_clear_assignment_classes(iface);
677         interface_ip_flush(&iface->config_ip);
678         interface_cleanup_state(iface);
679 }
680
681 static void
682 interface_do_free(struct interface *iface)
683 {
684         interface_event(iface, IFEV_FREE);
685         interface_cleanup(iface);
686         free(iface->config);
687         netifd_ubus_remove_interface(iface);
688         avl_delete(&interfaces.avl, &iface->node.avl);
689         if (iface->jail)
690                 free(iface->jail);
691         if (iface->jail_ifname)
692                 free(iface->jail_ifname);
693
694         free(iface);
695 }
696
697 static void
698 interface_do_reload(struct interface *iface)
699 {
700         interface_event(iface, IFEV_RELOAD);
701         interface_cleanup_state(iface);
702         proto_init_interface(iface, iface->config);
703         interface_claim_device(iface);
704 }
705
706 static void
707 interface_handle_config_change(struct interface *iface)
708 {
709         enum interface_config_state state = iface->config_state;
710
711         iface->config_state = IFC_NORMAL;
712         switch(state) {
713         case IFC_NORMAL:
714                 break;
715         case IFC_RELOAD:
716                 interface_do_reload(iface);
717                 break;
718         case IFC_REMOVE:
719                 interface_do_free(iface);
720                 return;
721         }
722         if (iface->autostart)
723                 interface_set_up(iface);
724 }
725
726 static void
727 interface_proto_event_cb(struct interface_proto_state *state, enum interface_proto_event ev)
728 {
729         struct interface *iface = state->iface;
730
731         switch (ev) {
732         case IFPEV_UP:
733                 if (iface->state != IFS_SETUP) {
734                         if (iface->state == IFS_UP && iface->updated)
735                                 interface_event(iface, IFEV_UPDATE);
736                         return;
737                 }
738
739                 if (!iface->l3_dev.dev)
740                         interface_set_l3_dev(iface, iface->main_dev.dev);
741
742                 interface_ip_set_enabled(&iface->config_ip, true);
743                 interface_ip_set_enabled(&iface->proto_ip, true);
744                 system_flush_routes();
745                 iface->state = IFS_UP;
746                 iface->start_time = system_get_rtime();
747                 interface_event(iface, IFEV_UP);
748                 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
749                 break;
750         case IFPEV_DOWN:
751                 if (iface->state == IFS_DOWN)
752                         return;
753
754                 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
755                 mark_interface_down(iface);
756                 if (iface->main_dev.dev)
757                         device_release(&iface->main_dev);
758                 if (iface->l3_dev.dev)
759                         device_remove_user(&iface->l3_dev);
760                 interface_handle_config_change(iface);
761                 break;
762         case IFPEV_LINK_LOST:
763                 if (iface->state != IFS_UP)
764                         return;
765
766                 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
767                 mark_interface_down(iface);
768                 iface->state = IFS_SETUP;
769                 break;
770         default:
771                 return;
772         }
773
774         interface_write_resolv_conf(iface->jail);
775 }
776
777 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
778 {
779         if (iface->proto) {
780                 iface->proto->free(iface->proto);
781                 iface->proto = NULL;
782         }
783         iface->state = IFS_DOWN;
784         iface->proto = state;
785         if (!state)
786                 return;
787
788         state->proto_event = interface_proto_event_cb;
789         state->iface = iface;
790 }
791
792 struct interface *
793 interface_alloc(const char *name, struct blob_attr *config, bool dynamic)
794 {
795         struct interface *iface;
796         struct blob_attr *tb[IFACE_ATTR_MAX];
797         struct blob_attr *cur;
798         const char *proto_name = NULL;
799         char *iface_name;
800         bool force_link = false;
801
802         iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
803         iface->name = strcpy(iface_name, name);
804         INIT_LIST_HEAD(&iface->errors);
805         INIT_LIST_HEAD(&iface->users);
806         INIT_LIST_HEAD(&iface->hotplug_list);
807         INIT_LIST_HEAD(&iface->assignment_classes);
808         interface_ip_init(iface);
809         avl_init(&iface->data, avl_strcmp, false, NULL);
810         iface->config_ip.enabled = false;
811
812         iface->main_dev.cb = interface_main_dev_cb;
813         iface->l3_dev.cb = interface_l3_dev_cb;
814         iface->ext_dev.cb = interface_ext_dev_cb;
815
816         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
817                       blob_data(config), blob_len(config));
818
819         if ((cur = tb[IFACE_ATTR_PROTO]))
820                 proto_name = blobmsg_data(cur);
821
822         proto_attach_interface(iface, proto_name);
823         if (iface->proto_handler->flags & PROTO_FLAG_FORCE_LINK_DEFAULT)
824                 force_link = true;
825
826         iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
827         iface->force_link = blobmsg_get_bool_default(tb[IFACE_ATTR_FORCE_LINK], force_link);
828         iface->dynamic = dynamic;
829         iface->proto_ip.no_defaultroute =
830                 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
831         iface->proto_ip.no_dns =
832                 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
833
834         if ((cur = tb[IFACE_ATTR_DNS]))
835                 interface_add_dns_server_list(&iface->config_ip, cur);
836
837         if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
838                 interface_add_dns_search_list(&iface->config_ip, cur);
839
840         if ((cur = tb[IFACE_ATTR_DNS_METRIC]))
841                 iface->dns_metric = blobmsg_get_u32(cur);
842
843         if ((cur = tb[IFACE_ATTR_METRIC]))
844                 iface->metric = blobmsg_get_u32(cur);
845
846         if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
847                 iface->assignment_length = blobmsg_get_u32(cur);
848
849         /* defaults */
850         iface->assignment_iface_id_selection = IFID_FIXED;
851         iface->assignment_fixed_iface_id = in6addr_any;
852         iface->assignment_fixed_iface_id.s6_addr[15] = 1;
853
854         if ((cur = tb[IFACE_ATTR_IP6IFACEID])) {
855                 const char *ifaceid = blobmsg_data(cur);
856                 if (!strcmp(ifaceid, "random")) {
857                         iface->assignment_iface_id_selection = IFID_RANDOM;
858                 }
859                 else if (!strcmp(ifaceid, "eui64")) {
860                         iface->assignment_iface_id_selection = IFID_EUI64;
861                 }
862                 else {
863                         /* we expect an IPv6 address with network id zero here -> fixed iface id
864                            if we cannot parse -> revert to iface id 1 */
865                         if (inet_pton(AF_INET6,ifaceid,&iface->assignment_fixed_iface_id) != 1 ||
866                                         iface->assignment_fixed_iface_id.s6_addr32[0] != 0 ||
867                                         iface->assignment_fixed_iface_id.s6_addr32[1] != 0) {
868                                 iface->assignment_fixed_iface_id = in6addr_any;
869                                 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
870                                 netifd_log_message(L_WARNING, "Failed to parse ip6ifaceid for interface '%s', \
871                                                         falling back to iface id 1.\n", iface->name);
872                         }
873                 }
874         }
875
876         iface->assignment_hint = -1;
877         if ((cur = tb[IFACE_ATTR_IP6HINT]))
878                 iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
879                                 ~((1 << (64 - iface->assignment_length)) - 1);
880
881         if ((cur = tb[IFACE_ATTR_IP6CLASS]))
882                 interface_add_assignment_classes(iface, cur);
883
884         if ((cur = tb[IFACE_ATTR_IP6WEIGHT]))
885                 iface->assignment_weight = blobmsg_get_u32(cur);
886
887         if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
888                 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
889                         DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
890         }
891
892         if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
893                 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
894                         DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
895         }
896
897         iface->proto_ip.no_delegation = !blobmsg_get_bool_default(tb[IFACE_ATTR_DELEGATE], true);
898
899         iface->config_autostart = iface->autostart;
900         iface->jail = NULL;
901
902         if ((cur = tb[IFACE_ATTR_JAIL])) {
903                 iface->jail = strdup(blobmsg_get_string(cur));
904                 iface->autostart = false;
905         }
906
907         iface->jail_ifname = NULL;
908         if ((cur = tb[IFACE_ATTR_JAIL_IFNAME]))
909                 iface->jail_ifname = strdup(blobmsg_get_string(cur));
910
911         return iface;
912 }
913
914 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
915 {
916         struct blob_attr *tb[IFACE_ATTR_MAX];
917         struct blob_attr *cur;
918         char *name = NULL;
919
920         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
921                       blob_data(config), blob_len(config));
922
923         if (alias) {
924                 if ((cur = tb[IFACE_ATTR_INTERFACE]))
925                         iface->parent_ifname = blobmsg_data(cur);
926
927                 if (!iface->parent_ifname)
928                         return false;
929         } else {
930                 if ((cur = tb[IFACE_ATTR_IFNAME]))
931                         iface->ifname = blobmsg_data(cur);
932         }
933
934         if (iface->dynamic) {
935                 name = strdup(iface->name);
936
937                 if (!name)
938                         return false;
939         }
940
941         iface->config = config;
942         vlist_add(&interfaces, &iface->node, iface->name);
943
944         if (name) {
945                 iface = vlist_find(&interfaces, name, iface, node);
946                 free(name);
947
948                 /* Don't delete dynamic interface on reload */
949                 if (iface)
950                         iface->node.version = -1;
951         }
952
953         return true;
954 }
955
956 bool
957 interface_add(struct interface *iface, struct blob_attr *config)
958 {
959         return __interface_add(iface, config, false);
960 }
961
962 bool
963 interface_add_alias(struct interface *iface, struct blob_attr *config)
964 {
965         if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
966                 return false;
967
968         return __interface_add(iface, config, true);
969 }
970
971 void
972 interface_set_l3_dev(struct interface *iface, struct device *dev)
973 {
974         bool enabled = iface->config_ip.enabled;
975         bool claimed = iface->l3_dev.claimed;
976
977         if (iface->l3_dev.dev == dev)
978                 return;
979
980         interface_ip_set_enabled(&iface->config_ip, false);
981         interface_ip_set_enabled(&iface->proto_ip, false);
982         interface_ip_flush(&iface->proto_ip);
983         device_add_user(&iface->l3_dev, dev);
984
985         if (dev) {
986                 if (claimed) {
987                         if (device_claim(&iface->l3_dev) < 0)
988                                 return;
989                 }
990                 interface_ip_set_enabled(&iface->config_ip, enabled);
991                 interface_ip_set_enabled(&iface->proto_ip, enabled);
992         }
993 }
994
995 static void
996 interface_set_main_dev(struct interface *iface, struct device *dev)
997 {
998         bool claimed = iface->l3_dev.claimed;
999
1000         if (iface->main_dev.dev == dev)
1001                 return;
1002
1003         interface_set_available(iface, false);
1004         device_add_user(&iface->main_dev, dev);
1005         if (!dev) {
1006                 interface_set_link_state(iface, false);
1007                 return;
1008         }
1009
1010         if (claimed) {
1011                 if (device_claim(&iface->l3_dev) < 0)
1012                         return;
1013         }
1014
1015         if (!iface->l3_dev.dev)
1016                 interface_set_l3_dev(iface, dev);
1017 }
1018
1019 static int
1020 interface_remove_link(struct interface *iface, struct device *dev)
1021 {
1022         struct device *mdev = iface->main_dev.dev;
1023
1024         if (mdev && mdev->hotplug_ops)
1025                 return mdev->hotplug_ops->del(mdev, dev);
1026
1027         if (dev == iface->ext_dev.dev)
1028                 device_remove_user(&iface->ext_dev);
1029
1030         if (!iface->main_dev.hotplug)
1031                 return UBUS_STATUS_INVALID_ARGUMENT;
1032
1033         if (dev != iface->main_dev.dev)
1034                 return UBUS_STATUS_INVALID_ARGUMENT;
1035
1036         interface_set_main_dev(iface, NULL);
1037         return 0;
1038 }
1039
1040 static int
1041 interface_add_link(struct interface *iface, struct device *dev, bool link_ext)
1042 {
1043         struct device *mdev = iface->main_dev.dev;
1044
1045         if (mdev == dev)
1046                 return 0;
1047
1048         if (iface->main_dev.hotplug)
1049                 device_remove_user(&iface->main_dev);
1050
1051         if (mdev) {
1052                 if (mdev->hotplug_ops)
1053                         return mdev->hotplug_ops->add(mdev, dev);
1054                 else
1055                         return UBUS_STATUS_NOT_SUPPORTED;
1056         }
1057
1058         if (link_ext)
1059                 device_add_user(&iface->ext_dev, dev);
1060
1061         interface_set_main_dev(iface, dev);
1062         iface->main_dev.hotplug = true;
1063         return 0;
1064 }
1065
1066 int
1067 interface_handle_link(struct interface *iface, const char *name, bool add, bool link_ext)
1068 {
1069         struct device *dev;
1070         int ret;
1071
1072         device_lock();
1073
1074         dev = device_get(name, add ? (link_ext ? 2 : 1) : 0);
1075         if (!dev) {
1076                 ret = UBUS_STATUS_NOT_FOUND;
1077                 goto out;
1078         }
1079
1080         if (add) {
1081                 interface_set_device_config(iface, dev);
1082                 device_set_present(dev, true);
1083
1084                 ret = interface_add_link(iface, dev, link_ext);
1085         } else {
1086                 ret = interface_remove_link(iface, dev);
1087         }
1088
1089 out:
1090         device_unlock();
1091
1092         return ret;
1093 }
1094
1095 void
1096 interface_set_up(struct interface *iface)
1097 {
1098         int ret;
1099         const char *error = NULL;
1100
1101         iface->autostart = true;
1102
1103         if (iface->state != IFS_DOWN)
1104                 return;
1105
1106         interface_clear_errors(iface);
1107         if (iface->available) {
1108                 if (iface->main_dev.dev) {
1109                         ret = device_claim(&iface->main_dev);
1110                         if (!ret)
1111                                 interface_check_state(iface);
1112                         else
1113                                 error = "DEVICE_CLAIM_FAILED";
1114                 } else {
1115                         ret = __interface_set_up(iface);
1116                         if (ret)
1117                                 error = "SETUP_FAILED";
1118                 }
1119         } else
1120                 error = "NO_DEVICE";
1121
1122         if (error)
1123                 interface_add_error(iface, "interface", error, NULL, 0);
1124 }
1125
1126 void
1127 interface_set_down(struct interface *iface)
1128 {
1129         if (!iface) {
1130                 vlist_for_each_element(&interfaces, iface, node)
1131                         __interface_set_down(iface, false);
1132         } else {
1133                 iface->autostart = false;
1134                 __interface_set_down(iface, false);
1135         }
1136 }
1137
1138 int
1139 interface_renew(struct interface *iface)
1140 {
1141         if (iface->state == IFS_TEARDOWN || iface->state == IFS_DOWN)
1142                 return -1;
1143
1144         return interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
1145 }
1146
1147 void
1148 interface_start_pending(void)
1149 {
1150         struct interface *iface;
1151
1152         vlist_for_each_element(&interfaces, iface, node) {
1153                 if (iface->autostart)
1154                         interface_set_up(iface);
1155         }
1156 }
1157
1158 void
1159 interface_start_jail(const char *jail, const pid_t netns_pid)
1160 {
1161         struct interface *iface;
1162         int netns_fd;
1163         int wstatus;
1164         pid_t pr = 0;
1165
1166         netns_fd = system_netns_open(netns_pid);
1167         if (netns_fd < 0)
1168                 return;
1169
1170         vlist_for_each_element(&interfaces, iface, node) {
1171                 if (!iface->jail || strcmp(iface->jail, jail))
1172                         continue;
1173
1174                 system_link_netns_move(iface->main_dev.dev, netns_fd, iface->jail_ifname);
1175         }
1176
1177         close(netns_fd);
1178
1179         pr = fork();
1180         if (pr) {
1181                 waitpid(pr, &wstatus, WUNTRACED | WCONTINUED);
1182                 return;
1183         }
1184
1185         /* child process */
1186         netns_fd = system_netns_open(netns_pid);
1187         if (netns_fd < 0)
1188                 return;
1189
1190         system_netns_set(netns_fd);
1191         system_init();
1192         vlist_for_each_element(&interfaces, iface, node) {
1193                 if (!iface->jail || strcmp(iface->jail, jail))
1194                         continue;
1195
1196                 /*
1197                  * The interface has already been renamed and is inside target
1198                  * namespace, hence overwrite ifname with jail_ifname for
1199                  * interface_set_up().
1200                  * We are inside a fork which got it's own copy of the interfaces
1201                  * list, so we can mess with it :)
1202                  */
1203                 if (iface->jail_ifname)
1204                         iface->ifname = iface->jail_ifname;
1205
1206                 interface_do_reload(iface);
1207                 interface_set_up(iface);
1208         }
1209
1210         close(netns_fd);
1211         _exit(0);
1212 }
1213
1214 void
1215 interface_stop_jail(const char *jail, const pid_t netns_pid)
1216 {
1217         struct interface *iface;
1218         int netns_fd, root_netns;
1219         int wstatus;
1220         pid_t parent_pid = getpid();
1221         pid_t pr = 0;
1222         const char *orig_ifname;
1223
1224         pr = fork();
1225         if (pr) {
1226                 waitpid(pr, &wstatus, WUNTRACED | WCONTINUED);
1227                 return;
1228         }
1229
1230         /* child process */
1231         root_netns = system_netns_open(parent_pid);
1232         if (root_netns < 0)
1233                 return;
1234
1235         netns_fd = system_netns_open(netns_pid);
1236         if (netns_fd < 0)
1237                 return;
1238
1239         system_netns_set(netns_fd);
1240         system_init();
1241         vlist_for_each_element(&interfaces, iface, node) {
1242                 if (!iface->jail || strcmp(iface->jail, jail))
1243                         continue;
1244
1245                 orig_ifname = iface->ifname;
1246                 if (iface->jail_ifname)
1247                         iface->ifname = iface->jail_ifname;
1248
1249                 interface_do_reload(iface);
1250                 interface_set_down(iface);
1251                 system_link_netns_move(iface->main_dev.dev, root_netns, orig_ifname);
1252         }
1253
1254         close(root_netns);
1255         close(netns_fd);
1256         _exit(0);
1257 }
1258
1259 static void
1260 set_config_state(struct interface *iface, enum interface_config_state s)
1261 {
1262         __set_config_state(iface, s);
1263         if (iface->state == IFS_DOWN)
1264                 interface_handle_config_change(iface);
1265         else
1266                 __interface_set_down(iface, false);
1267 }
1268
1269 void
1270 interface_update_start(struct interface *iface, const bool keep_old)
1271 {
1272         iface->updated = 0;
1273
1274         if (!keep_old)
1275                 interface_ip_update_start(&iface->proto_ip);
1276 }
1277
1278 void
1279 interface_update_complete(struct interface *iface)
1280 {
1281         interface_ip_update_complete(&iface->proto_ip);
1282 }
1283
1284 static void
1285 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
1286 {
1287         vlist_simple_replace(&new->dns_servers, &old->dns_servers);
1288         vlist_simple_replace(&new->dns_search, &old->dns_search);
1289 }
1290
1291 static bool
1292 interface_device_config_changed(struct interface *if_old, struct interface *if_new)
1293 {
1294         struct blob_attr *ntb[__DEV_ATTR_MAX];
1295         struct blob_attr *otb[__DEV_ATTR_MAX];
1296         struct device *dev = if_old->main_dev.dev;
1297         unsigned long diff = 0;
1298
1299         BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
1300
1301         if (!dev)
1302                 return false;
1303
1304         if (if_old->device_config != if_new->device_config)
1305                 return true;
1306
1307         if (!if_new->device_config)
1308                 return false;
1309
1310         blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb,
1311                 blob_data(if_old->config), blob_len(if_old->config));
1312
1313         blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, ntb,
1314                 blob_data(if_new->config), blob_len(if_new->config));
1315
1316         uci_blob_diff(ntb, otb, &device_attr_list, &diff);
1317         return diff;
1318 }
1319
1320 static void
1321 interface_change_config(struct interface *if_old, struct interface *if_new)
1322 {
1323         struct blob_attr *old_config = if_old->config;
1324         bool reload = false, reload_ip = false, update_prefix_delegation = false;
1325
1326 #define FIELD_CHANGED_STR(field)                                        \
1327                 ((!!if_old->field != !!if_new->field) ||                \
1328                  (if_old->field &&                                      \
1329                   strcmp(if_old->field, if_new->field) != 0))
1330
1331         if (FIELD_CHANGED_STR(parent_ifname)) {
1332                 if (if_old->parent_iface.iface)
1333                         interface_remove_user(&if_old->parent_iface);
1334                 reload = true;
1335         }
1336
1337         if (!reload && interface_device_config_changed(if_old, if_new))
1338                 reload = true;
1339
1340         if (FIELD_CHANGED_STR(ifname) ||
1341             if_old->proto_handler != if_new->proto_handler)
1342                 reload = true;
1343
1344         if (!if_old->proto_handler->config_params)
1345                 D(INTERFACE, "No config parameters for interface '%s'\n",
1346                   if_old->name);
1347         else if (!uci_blob_check_equal(if_old->config, if_new->config,
1348                                        if_old->proto_handler->config_params))
1349                 reload = true;
1350
1351 #define UPDATE(field, __var) ({                                         \
1352                 bool __changed = (if_old->field != if_new->field);      \
1353                 if_old->field = if_new->field;                          \
1354                 __var |= __changed;                                     \
1355         })
1356
1357         if_old->config = if_new->config;
1358         if (if_old->config_autostart != if_new->config_autostart) {
1359                 if (if_old->config_autostart)
1360                         reload = true;
1361
1362                 if_old->autostart = if_new->config_autostart;
1363         }
1364
1365         if_old->device_config = if_new->device_config;
1366         if_old->config_autostart = if_new->config_autostart;
1367         if (if_old->jail)
1368                 free(if_old->jail);
1369
1370         if_old->jail = if_new->jail;
1371         if (if_old->jail)
1372                 if_old->autostart = false;
1373
1374         if (if_old->jail_ifname)
1375                 free(if_old->jail_ifname);
1376
1377         if_old->jail_ifname = if_new->jail_ifname;
1378
1379         if_old->ifname = if_new->ifname;
1380         if_old->parent_ifname = if_new->parent_ifname;
1381         if_old->dynamic = if_new->dynamic;
1382         if_old->proto_handler = if_new->proto_handler;
1383         if_old->force_link = if_new->force_link;
1384         if_old->dns_metric = if_new->dns_metric;
1385
1386         if (if_old->proto_ip.no_delegation != if_new->proto_ip.no_delegation) {
1387                 if_old->proto_ip.no_delegation = if_new->proto_ip.no_delegation;
1388                 update_prefix_delegation = true;
1389         }
1390
1391         if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
1392         interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
1393
1394         UPDATE(metric, reload_ip);
1395         UPDATE(proto_ip.no_defaultroute, reload_ip);
1396         UPDATE(ip4table, reload_ip);
1397         UPDATE(ip6table, reload_ip);
1398         interface_merge_assignment_data(if_old, if_new);
1399
1400 #undef UPDATE
1401
1402         if (reload) {
1403                 D(INTERFACE, "Reload interface '%s' because of config changes\n",
1404                   if_old->name);
1405                 interface_clear_errors(if_old);
1406                 set_config_state(if_old, IFC_RELOAD);
1407                 goto out;
1408         }
1409
1410         if (reload_ip) {
1411                 bool config_ip_enabled = if_old->config_ip.enabled;
1412                 bool proto_ip_enabled = if_old->proto_ip.enabled;
1413
1414                 interface_ip_set_enabled(&if_old->config_ip, false);
1415                 interface_ip_set_enabled(&if_old->proto_ip, false);
1416                 interface_ip_set_enabled(&if_old->proto_ip, proto_ip_enabled);
1417                 interface_ip_set_enabled(&if_old->config_ip, config_ip_enabled);
1418         }
1419
1420         if (update_prefix_delegation)
1421                 interface_update_prefix_delegation(&if_old->proto_ip);
1422
1423         interface_write_resolv_conf(if_old->jail);
1424         if (if_old->main_dev.dev)
1425                 interface_check_state(if_old);
1426
1427 out:
1428         if_new->config = NULL;
1429         interface_cleanup(if_new);
1430         free(old_config);
1431         free(if_new);
1432 }
1433
1434 static void
1435 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
1436                  struct vlist_node *node_old)
1437 {
1438         struct interface *if_old = container_of(node_old, struct interface, node);
1439         struct interface *if_new = container_of(node_new, struct interface, node);
1440
1441         if (node_old && node_new) {
1442                 D(INTERFACE, "Update interface '%s'\n", if_new->name);
1443                 interface_change_config(if_old, if_new);
1444         } else if (node_old) {
1445                 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
1446                 set_config_state(if_old, IFC_REMOVE);
1447         } else if (node_new) {
1448                 D(INTERFACE, "Create interface '%s'\n", if_new->name);
1449                 interface_event(if_new, IFEV_CREATE);
1450                 proto_init_interface(if_new, if_new->config);
1451                 interface_claim_device(if_new);
1452                 netifd_ubus_add_interface(if_new);
1453         }
1454 }
1455
1456
1457 static void __init
1458 interface_init_list(void)
1459 {
1460         vlist_init(&interfaces, avl_strcmp, interface_update);
1461         interfaces.keep_old = true;
1462         interfaces.no_delete = true;
1463 }