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