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