netifd: Add mldversion config support
[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_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         case IFS_SETUP:
275                 if (!iface->enabled || !link_state) {
276                         interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
277                         mark_interface_down(iface);
278                 }
279                 break;
280         case IFS_DOWN:
281                 if (!iface->available)
282                         return;
283
284                 if (iface->autostart && iface->enabled && link_state && !config_init)
285                         __interface_set_up(iface);
286                 break;
287         default:
288                 break;
289         }
290 }
291
292 static void
293 interface_set_enabled(struct interface *iface, bool new_state)
294 {
295         if (iface->enabled == new_state)
296                 return;
297
298         netifd_log_message(L_NOTICE, "Interface '%s' is %s\n", iface->name, new_state ? "enabled" : "disabled");
299         iface->enabled = new_state;
300         interface_check_state(iface);
301 }
302
303 static void
304 interface_set_link_state(struct interface *iface, bool new_state)
305 {
306         if (iface->link_state == new_state)
307                 return;
308
309         netifd_log_message(L_NOTICE, "Interface '%s' has link connectivity %s\n", iface->name, new_state ? "" : "loss");
310         iface->link_state = new_state;
311         interface_check_state(iface);
312 }
313
314 static void
315 interface_ext_cb(struct device_user *dep, enum device_event ev)
316 {
317         if (ev == DEV_EVENT_REMOVE)
318                 device_remove_user(dep);
319 }
320
321 static void
322 interface_cb(struct device_user *dep, enum device_event ev)
323 {
324         struct interface *iface;
325         bool new_state = false;
326
327         iface = container_of(dep, struct interface, main_dev);
328         switch (ev) {
329         case DEV_EVENT_ADD:
330                 new_state = true;
331         case DEV_EVENT_REMOVE:
332                 interface_set_available(iface, new_state);
333                 if (!new_state && dep->dev && dep->dev->external)
334                         interface_set_main_dev(iface, NULL);
335                 break;
336         case DEV_EVENT_UP:
337                 new_state = true;
338         case DEV_EVENT_DOWN:
339                 interface_set_enabled(iface, new_state);
340                 break;
341         case DEV_EVENT_LINK_UP:
342                 new_state = true;
343         case DEV_EVENT_LINK_DOWN:
344                 interface_set_link_state(iface, new_state);
345                 break;
346         case DEV_EVENT_TOPO_CHANGE:
347                 interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
348                 return;
349         default:
350                 break;
351         }
352 }
353
354 void
355 interface_set_available(struct interface *iface, bool new_state)
356 {
357         if (iface->available == new_state)
358                 return;
359
360         D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
361         iface->available = new_state;
362
363         if (new_state) {
364                 if (iface->autostart && !config_init)
365                         interface_set_up(iface);
366         } else
367                 __interface_set_down(iface, true);
368 }
369
370 void
371 interface_add_user(struct interface_user *dep, struct interface *iface)
372 {
373         if (!iface) {
374                 list_add(&dep->list, &iface_all_users);
375                 return;
376         }
377
378         dep->iface = iface;
379         list_add(&dep->list, &iface->users);
380         if (iface->state == IFS_UP)
381                 dep->cb(dep, iface, IFEV_UP);
382 }
383
384 void
385 interface_remove_user(struct interface_user *dep)
386 {
387         list_del_init(&dep->list);
388         dep->iface = NULL;
389 }
390
391 static void
392 interface_add_assignment_classes(struct interface *iface, struct blob_attr *list)
393 {
394         struct blob_attr *cur;
395         int rem;
396
397         blobmsg_for_each_attr(cur, list, rem) {
398                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
399                         continue;
400
401                 if (!blobmsg_check_attr(cur, NULL))
402                         continue;
403
404                 struct interface_assignment_class *c = malloc(sizeof(*c) + blobmsg_data_len(cur));
405                 memcpy(c->name, blobmsg_data(cur), blobmsg_data_len(cur));
406                 list_add(&c->head, &iface->assignment_classes);
407         }
408 }
409
410 static void
411 interface_clear_assignment_classes(struct interface *iface)
412 {
413         while (!list_empty(&iface->assignment_classes)) {
414                 struct interface_assignment_class *c = list_first_entry(&iface->assignment_classes,
415                                 struct interface_assignment_class, head);
416                 list_del(&c->head);
417                 free(c);
418         }
419 }
420
421 static void
422 interface_merge_assignment_data(struct interface *old, struct interface *new)
423 {
424         bool changed = (old->assignment_hint != new->assignment_hint ||
425                         old->assignment_length != new->assignment_length ||
426                         list_empty(&old->assignment_classes) != list_empty(&new->assignment_classes));
427
428         struct interface_assignment_class *c;
429         list_for_each_entry(c, &new->assignment_classes, head) {
430                 // Compare list entries one-by-one to see if there was a change
431                 if (list_empty(&old->assignment_classes)) // The new list is longer
432                         changed = true;
433
434                 if (changed)
435                         break;
436
437                 struct interface_assignment_class *c_old = list_first_entry(&old->assignment_classes,
438                                 struct interface_assignment_class, head);
439
440                 if (strcmp(c_old->name, c->name)) // An entry didn't match
441                         break;
442
443                 list_del(&c_old->head);
444                 free(c_old);
445         }
446
447         // The old list was longer than the new one or the last entry didn't match
448         if (!list_empty(&old->assignment_classes)) {
449                 interface_clear_assignment_classes(old);
450                 changed = true;
451         }
452
453         list_splice_init(&new->assignment_classes, &old->assignment_classes);
454
455         if (changed) {
456                 old->assignment_hint = new->assignment_hint;
457                 old->assignment_length = new->assignment_length;
458                 interface_refresh_assignments(true);
459         }
460 }
461
462 static void
463 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
464 {
465         struct interface *alias = container_of(dep, struct interface, parent_iface);
466         struct device *dev = iface->l3_dev.dev;
467
468         switch (ev) {
469         case IFEV_UP:
470                 if (!dev)
471                         return;
472
473                 interface_set_main_dev(alias, dev);
474                 interface_set_available(alias, true);
475                 break;
476         case IFEV_DOWN:
477                 interface_set_available(alias, false);
478                 interface_set_main_dev(alias, NULL);
479                 break;
480         case IFEV_FREE:
481                 interface_remove_user(dep);
482                 break;
483         case IFEV_RELOAD:
484         case IFEV_UPDATE:
485                 break;
486         }
487 }
488
489 static void
490 interface_claim_device(struct interface *iface)
491 {
492         struct interface *parent;
493         struct device *dev = NULL;
494
495         if (iface->parent_iface.iface)
496                 interface_remove_user(&iface->parent_iface);
497
498         if (iface->parent_ifname) {
499                 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
500                 iface->parent_iface.cb = interface_alias_cb;
501                 interface_add_user(&iface->parent_iface, parent);
502         } else if (iface->ifname &&
503                 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
504                 dev = device_get(iface->ifname, true);
505                 if (dev && dev->default_config && iface->device_config)
506                         device_set_config(dev, dev->type, iface->config);
507         } else {
508                 dev = iface->ext_dev.dev;
509         }
510
511         if (dev)
512                 interface_set_main_dev(iface, dev);
513
514         if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
515                 interface_set_available(iface, true);
516 }
517
518 static void
519 interface_cleanup_state(struct interface *iface)
520 {
521         interface_set_available(iface, false);
522
523         interface_flush_state(iface);
524         interface_clear_errors(iface);
525         interface_set_proto_state(iface, NULL);
526
527         interface_set_main_dev(iface, NULL);
528         interface_set_l3_dev(iface, NULL);
529 }
530
531 static void
532 interface_cleanup(struct interface *iface)
533 {
534         struct interface_user *dep, *tmp;
535
536         uloop_timeout_cancel(&iface->remove_timer);
537         device_remove_user(&iface->ext_dev);
538
539         if (iface->parent_iface.iface)
540                 interface_remove_user(&iface->parent_iface);
541
542         list_for_each_entry_safe(dep, tmp, &iface->users, list)
543                 interface_remove_user(dep);
544
545         interface_clear_assignment_classes(iface);
546         interface_ip_flush(&iface->config_ip);
547         interface_cleanup_state(iface);
548 }
549
550 static void
551 interface_do_free(struct interface *iface)
552 {
553         interface_event(iface, IFEV_FREE);
554         interface_cleanup(iface);
555         free(iface->config);
556         netifd_ubus_remove_interface(iface);
557         avl_delete(&interfaces.avl, &iface->node.avl);
558         free(iface);
559 }
560
561 static void
562 interface_do_reload(struct interface *iface)
563 {
564         interface_event(iface, IFEV_RELOAD);
565         interface_cleanup_state(iface);
566         proto_init_interface(iface, iface->config);
567         interface_claim_device(iface);
568 }
569
570 static void
571 interface_handle_config_change(struct interface *iface)
572 {
573         enum interface_config_state state = iface->config_state;
574
575         iface->config_state = IFC_NORMAL;
576         switch(state) {
577         case IFC_NORMAL:
578                 break;
579         case IFC_RELOAD:
580                 interface_do_reload(iface);
581                 break;
582         case IFC_REMOVE:
583                 interface_do_free(iface);
584                 return;
585         }
586         if (iface->autostart && iface->available)
587                 interface_set_up(iface);
588 }
589
590 static void
591 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
592 {
593         struct interface *iface = state->iface;
594
595         switch (ev) {
596         case IFPEV_UP:
597                 if (iface->state != IFS_SETUP) {
598                         interface_event(iface, IFEV_UPDATE);
599                         return;
600                 }
601
602                 if (!iface->l3_dev.dev)
603                         interface_set_l3_dev(iface, iface->main_dev.dev);
604
605                 interface_ip_set_enabled(&iface->config_ip, true);
606                 system_flush_routes();
607                 iface->state = IFS_UP;
608                 iface->start_time = system_get_rtime();
609                 interface_event(iface, IFEV_UP);
610                 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
611                 break;
612         case IFPEV_DOWN:
613                 if (iface->state == IFS_DOWN)
614                         return;
615
616                 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
617                 mark_interface_down(iface);
618                 if (iface->main_dev.dev)
619                         device_release(&iface->main_dev);
620                 if (iface->l3_dev.dev)
621                         device_remove_user(&iface->l3_dev);
622                 interface_handle_config_change(iface);
623                 break;
624         case IFPEV_LINK_LOST:
625                 if (iface->state != IFS_UP)
626                         return;
627
628                 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
629                 mark_interface_down(iface);
630                 iface->state = IFS_SETUP;
631                 break;
632         default:
633                 return;
634         }
635
636         interface_write_resolv_conf();
637 }
638
639 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
640 {
641         if (iface->proto) {
642                 iface->proto->free(iface->proto);
643                 iface->proto = NULL;
644         }
645         iface->state = IFS_DOWN;
646         iface->proto = state;
647         if (!state)
648                 return;
649
650         state->proto_event = interface_proto_cb;
651         state->iface = iface;
652 }
653
654 struct interface *
655 interface_alloc(const char *name, struct blob_attr *config)
656 {
657         struct interface *iface;
658         struct blob_attr *tb[IFACE_ATTR_MAX];
659         struct blob_attr *cur;
660         const char *proto_name = NULL;
661         char *iface_name;
662         bool force_link = false;
663
664         iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
665         iface->name = strcpy(iface_name, name);
666         INIT_LIST_HEAD(&iface->errors);
667         INIT_LIST_HEAD(&iface->users);
668         INIT_LIST_HEAD(&iface->hotplug_list);
669         INIT_LIST_HEAD(&iface->assignment_classes);
670         interface_ip_init(iface);
671         avl_init(&iface->data, avl_strcmp, false, NULL);
672         iface->config_ip.enabled = false;
673
674         iface->main_dev.cb = interface_cb;
675         iface->ext_dev.cb = interface_ext_cb;
676
677         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
678                       blob_data(config), blob_len(config));
679
680         if ((cur = tb[IFACE_ATTR_PROTO]))
681                 proto_name = blobmsg_data(cur);
682
683         proto_attach_interface(iface, proto_name);
684         if (iface->proto_handler->flags & PROTO_FLAG_FORCE_LINK_DEFAULT)
685                 force_link = true;
686
687         iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
688         iface->force_link = blobmsg_get_bool_default(tb[IFACE_ATTR_FORCE_LINK], force_link);
689         iface->proto_ip.no_defaultroute =
690                 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
691         iface->proto_ip.no_dns =
692                 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
693
694         if ((cur = tb[IFACE_ATTR_DNS]))
695                 interface_add_dns_server_list(&iface->config_ip, cur);
696
697         if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
698                 interface_add_dns_search_list(&iface->config_ip, cur);
699
700         if ((cur = tb[IFACE_ATTR_METRIC]))
701                 iface->metric = blobmsg_get_u32(cur);
702
703         if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
704                 iface->assignment_length = blobmsg_get_u32(cur);
705
706         iface->assignment_hint = -1;
707         if ((cur = tb[IFACE_ATTR_IP6HINT]))
708                 iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
709                                 ~((1 << (64 - iface->assignment_length)) - 1);
710
711         if ((cur = tb[IFACE_ATTR_IP6CLASS]))
712                 interface_add_assignment_classes(iface, cur);
713
714
715         if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
716                 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
717                         DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
718         }
719
720         if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
721                 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
722                         DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
723         }
724
725         iface->proto_ip.no_delegation = !blobmsg_get_bool_default(tb[IFACE_ATTR_DELEGATE], true);
726
727         iface->config_autostart = iface->autostart;
728         return iface;
729 }
730
731 void interface_set_dynamic(struct interface *iface)
732 {
733         iface->dynamic = true;
734         iface->autostart = true;
735         iface->node.version = -1; // Don't delete on reload
736 }
737
738 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
739 {
740         struct blob_attr *tb[IFACE_ATTR_MAX];
741         struct blob_attr *cur;
742
743         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
744                       blob_data(config), blob_len(config));
745
746         if (alias) {
747                 if ((cur = tb[IFACE_ATTR_INTERFACE]))
748                         iface->parent_ifname = blobmsg_data(cur);
749
750                 if (!iface->parent_ifname)
751                         return false;
752         } else {
753                 if ((cur = tb[IFACE_ATTR_IFNAME]))
754                         iface->ifname = blobmsg_data(cur);
755         }
756
757
758         iface->config = config;
759         vlist_add(&interfaces, &iface->node, iface->name);
760         return true;
761 }
762
763 void
764 interface_add(struct interface *iface, struct blob_attr *config)
765 {
766         __interface_add(iface, config, false);
767 }
768
769 bool
770 interface_add_alias(struct interface *iface, struct blob_attr *config)
771 {
772         if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
773                 return false;
774
775         return __interface_add(iface, config, true);
776 }
777
778 void
779 interface_set_l3_dev(struct interface *iface, struct device *dev)
780 {
781         bool enabled = iface->config_ip.enabled;
782         bool claimed = iface->l3_dev.claimed;
783
784         if (iface->l3_dev.dev == dev)
785                 return;
786
787         interface_ip_set_enabled(&iface->config_ip, false);
788         interface_ip_flush(&iface->proto_ip);
789         device_add_user(&iface->l3_dev, dev);
790
791         if (dev) {
792                 if (claimed) {
793                         if (device_claim(&iface->l3_dev) < 0)
794                                 return;
795                 }
796                 interface_ip_set_enabled(&iface->config_ip, enabled);
797         }
798 }
799
800 void
801 interface_set_main_dev(struct interface *iface, struct device *dev)
802 {
803         bool claimed = iface->l3_dev.claimed;
804
805         if (iface->main_dev.dev == dev)
806                 return;
807
808         interface_set_available(iface, false);
809         device_add_user(&iface->main_dev, dev);
810         if (!dev) {
811                 interface_set_link_state(iface, false);
812                 return;
813         }
814
815         if (claimed) {
816                 if (device_claim(&iface->l3_dev) < 0)
817                         return;
818         }
819
820         if (!iface->l3_dev.dev)
821                 interface_set_l3_dev(iface, dev);
822 }
823
824 int
825 interface_remove_link(struct interface *iface, struct device *dev)
826 {
827         struct device *mdev = iface->main_dev.dev;
828
829         if (mdev && mdev->hotplug_ops)
830                 return mdev->hotplug_ops->del(mdev, dev);
831
832         if (dev == iface->ext_dev.dev)
833                 device_remove_user(&iface->ext_dev);
834
835         if (!iface->main_dev.hotplug)
836                 return UBUS_STATUS_INVALID_ARGUMENT;
837
838         if (dev != iface->main_dev.dev)
839                 return UBUS_STATUS_INVALID_ARGUMENT;
840
841         interface_set_main_dev(iface, NULL);
842         return 0;
843 }
844
845 static int
846 interface_add_link(struct interface *iface, struct device *dev, bool link_ext)
847 {
848         struct device *mdev = iface->main_dev.dev;
849
850         if (mdev == dev)
851                 return 0;
852
853         if (iface->main_dev.hotplug)
854                 device_remove_user(&iface->main_dev);
855
856         if (mdev) {
857                 if (mdev->hotplug_ops)
858                         return mdev->hotplug_ops->add(mdev, dev);
859                 else
860                         return UBUS_STATUS_NOT_SUPPORTED;
861         }
862
863         if (link_ext)
864                 device_add_user(&iface->ext_dev, dev);
865
866         interface_set_main_dev(iface, dev);
867         iface->main_dev.hotplug = true;
868         return 0;
869 }
870
871 int
872 interface_handle_link(struct interface *iface, const char *name, bool add, bool link_ext)
873 {
874         struct device *dev;
875         int ret;
876
877         device_lock();
878
879         dev = device_get(name, add ? (link_ext ? 2 : 1) : 0);
880         if (!dev) {
881                 ret = UBUS_STATUS_NOT_FOUND;
882                 goto out;
883         }
884
885         if (add) {
886                 device_set_present(dev, true);
887                 if (iface->device_config && dev->default_config)
888                         device_set_config(dev, dev->type, iface->config);
889
890                 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
891                 ret = interface_add_link(iface, dev, link_ext);
892         } else {
893                 ret = interface_remove_link(iface, dev);
894         }
895
896 out:
897         device_unlock();
898
899         return ret;
900 }
901
902 int
903 interface_set_up(struct interface *iface)
904 {
905         int ret;
906
907         iface->autostart = true;
908
909         if (iface->state != IFS_DOWN)
910                 return 0;
911
912         interface_clear_errors(iface);
913         if (!iface->available) {
914                 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
915                 return -1;
916         }
917
918         if (iface->main_dev.dev) {
919                 ret = device_claim(&iface->main_dev);
920                 if (!ret)
921                         interface_check_state(iface);
922         }
923         else
924                 ret = __interface_set_up(iface);
925
926         return ret;
927 }
928
929 int
930 interface_set_down(struct interface *iface)
931 {
932         if (!iface) {
933                 vlist_for_each_element(&interfaces, iface, node)
934                         __interface_set_down(iface, false);
935         } else {
936                 iface->autostart = false;
937                 __interface_set_down(iface, false);
938         }
939
940         return 0;
941 }
942
943 void
944 interface_start_pending(void)
945 {
946         struct interface *iface;
947
948         vlist_for_each_element(&interfaces, iface, node) {
949                 if (iface->available && iface->autostart)
950                         interface_set_up(iface);
951         }
952 }
953
954 static void
955 set_config_state(struct interface *iface, enum interface_config_state s)
956 {
957         iface->config_state = s;
958         if (iface->state == IFS_DOWN)
959                 interface_handle_config_change(iface);
960         else
961                 __interface_set_down(iface, false);
962 }
963
964 void
965 interface_update_start(struct interface *iface)
966 {
967         iface->updated = 0;
968         interface_ip_update_start(&iface->proto_ip);
969 }
970
971 void
972 interface_update_complete(struct interface *iface)
973 {
974         interface_ip_update_complete(&iface->proto_ip);
975 }
976
977 static void
978 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
979 {
980         vlist_simple_replace(&new->dns_servers, &old->dns_servers);
981         vlist_simple_replace(&new->dns_search, &old->dns_search);
982 }
983
984 static void
985 interface_change_config(struct interface *if_old, struct interface *if_new)
986 {
987         struct blob_attr *old_config = if_old->config;
988         bool reload = false, reload_ip = false;
989
990 #define FIELD_CHANGED_STR(field)                                        \
991                 ((!!if_old->field != !!if_new->field) ||                \
992                  (if_old->field &&                                      \
993                   strcmp(if_old->field, if_new->field) != 0))
994
995         if (FIELD_CHANGED_STR(parent_ifname)) {
996                 if (if_old->parent_iface.iface)
997                         interface_remove_user(&if_old->parent_iface);
998                 reload = true;
999         }
1000
1001         if (FIELD_CHANGED_STR(ifname) ||
1002             if_old->proto_handler != if_new->proto_handler)
1003                 reload = true;
1004
1005         if (!if_old->proto_handler->config_params)
1006                 D(INTERFACE, "No config parameters for interface '%s'\n",
1007                   if_old->name);
1008         else if (!uci_blob_check_equal(if_old->config, if_new->config,
1009                                        if_old->proto_handler->config_params))
1010                 reload = true;
1011
1012 #define UPDATE(field, __var) ({                                         \
1013                 bool __changed = (if_old->field != if_new->field);      \
1014                 if_old->field = if_new->field;                          \
1015                 __var |= __changed;                                     \
1016         })
1017
1018         if_old->config = if_new->config;
1019         if (if_old->config_autostart != if_new->config_autostart) {
1020                 if (if_old->config_autostart)
1021                         reload = true;
1022
1023                 if_old->autostart = if_new->config_autostart;
1024         }
1025
1026         if_old->device_config = if_new->device_config;
1027         if_old->config_autostart = if_new->config_autostart;
1028         if_old->ifname = if_new->ifname;
1029         if_old->parent_ifname = if_new->parent_ifname;
1030         if_old->proto_handler = if_new->proto_handler;
1031         if_old->force_link = if_new->force_link;
1032
1033         if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
1034         interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
1035
1036         UPDATE(metric, reload_ip);
1037         UPDATE(proto_ip.no_defaultroute, reload_ip);
1038         UPDATE(ip4table, reload_ip);
1039         UPDATE(ip6table, reload_ip);
1040         interface_merge_assignment_data(if_old, if_new);
1041
1042 #undef UPDATE
1043
1044         if (reload) {
1045                 D(INTERFACE, "Reload interface '%s' because of config changes\n",
1046                   if_old->name);
1047                 interface_clear_errors(if_old);
1048                 set_config_state(if_old, IFC_RELOAD);
1049                 goto out;
1050         }
1051
1052         if (reload_ip) {
1053                 bool config_ip_enabled = if_old->config_ip.enabled;
1054                 bool proto_ip_enabled = if_old->proto_ip.enabled;
1055
1056                 interface_ip_set_enabled(&if_old->config_ip, false);
1057                 interface_ip_set_enabled(&if_old->proto_ip, false);
1058                 interface_ip_set_enabled(&if_old->proto_ip, proto_ip_enabled);
1059                 interface_ip_set_enabled(&if_old->config_ip, config_ip_enabled);
1060         }
1061
1062         interface_write_resolv_conf();
1063         if (if_old->main_dev.dev)
1064                 interface_check_state(if_old);
1065
1066 out:
1067         if_new->config = NULL;
1068         interface_cleanup(if_new);
1069         free(old_config);
1070         free(if_new);
1071 }
1072
1073 static void
1074 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
1075                  struct vlist_node *node_old)
1076 {
1077         struct interface *if_old = container_of(node_old, struct interface, node);
1078         struct interface *if_new = container_of(node_new, struct interface, node);
1079
1080         if (node_old && node_new) {
1081                 D(INTERFACE, "Update interface '%s'\n", if_new->name);
1082                 interface_change_config(if_old, if_new);
1083         } else if (node_old) {
1084                 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
1085                 set_config_state(if_old, IFC_REMOVE);
1086         } else if (node_new) {
1087                 D(INTERFACE, "Create interface '%s'\n", if_new->name);
1088                 proto_init_interface(if_new, if_new->config);
1089                 interface_claim_device(if_new);
1090                 netifd_ubus_add_interface(if_new);
1091         }
1092 }
1093
1094
1095 static void __init
1096 interface_init_list(void)
1097 {
1098         vlist_init(&interfaces, avl_strcmp, interface_update);
1099         interfaces.keep_old = true;
1100         interfaces.no_delete = true;
1101 }