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