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