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