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