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