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