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