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