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