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