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