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