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