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