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