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