2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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
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.
20 #include "interface.h"
21 #include "interface-ip.h"
27 struct vlist_tree interfaces
;
28 static LIST_HEAD(iface_all_users
);
29 static unsigned int interface_serial
= 0;
35 IFACE_ATTR_DEFAULTROUTE
,
38 IFACE_ATTR_DNS_SEARCH
,
50 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
51 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
52 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
53 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
54 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
55 [IFACE_ATTR_PEERDNS
] = { .name
= "peerdns", .type
= BLOBMSG_TYPE_BOOL
},
56 [IFACE_ATTR_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
57 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
58 [IFACE_ATTR_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
59 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
60 [IFACE_ATTR_IP6ASSIGN
] = { .name
= "ip6assign", .type
= BLOBMSG_TYPE_INT32
},
61 [IFACE_ATTR_IP6HINT
] = { .name
= "ip6hint", .type
= BLOBMSG_TYPE_STRING
},
62 [IFACE_ATTR_IP4TABLE
] = { .name
= "ip4table", .type
= BLOBMSG_TYPE_STRING
},
63 [IFACE_ATTR_IP6TABLE
] = { .name
= "ip6table", .type
= BLOBMSG_TYPE_STRING
},
64 [IFACE_ATTR_IP6CLASS
] = { .name
= "ip6class", .type
= BLOBMSG_TYPE_ARRAY
},
65 [IFACE_ATTR_DELEGATE
] = { .name
= "delegate", .type
= BLOBMSG_TYPE_BOOL
},
68 static const struct uci_blob_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
69 [IFACE_ATTR_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
70 [IFACE_ATTR_IP6CLASS
] = { .type
= BLOBMSG_TYPE_STRING
},
73 const struct uci_blob_param_list interface_attr_list
= {
74 .n_params
= IFACE_ATTR_MAX
,
75 .params
= iface_attrs
,
76 .info
= iface_attr_info
,
80 interface_clear_errors(struct interface
*iface
)
82 struct interface_error
*error
, *tmp
;
84 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
85 list_del(&error
->list
);
90 void interface_add_error(struct interface
*iface
, const char *subsystem
,
91 const char *code
, const char **data
, int n_data
)
93 struct interface_error
*error
;
96 char *dest
, *d_subsys
, *d_code
;
99 len
= n_data
* sizeof(char *);
100 datalen
= alloca(len
);
101 for (i
= 0; i
< n_data
; i
++) {
102 datalen
[i
] = strlen(data
[i
]) + 1;
107 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
108 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
109 &d_code
, code
? strlen(code
) + 1 : 0);
113 list_add_tail(&error
->list
, &iface
->errors
);
115 dest
= (char *) &error
->data
[n_data
+ 1];
116 for (i
= 0; i
< n_data
; i
++) {
117 error
->data
[i
] = dest
;
118 memcpy(dest
, data
[i
], datalen
[i
]);
121 error
->data
[n_data
++] = NULL
;
124 error
->subsystem
= strcpy(d_subsys
, subsystem
);
127 error
->code
= strcpy(d_code
, code
);
131 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
133 avl_delete(&iface
->data
, &data
->node
);
138 interface_data_flush(struct interface
*iface
)
140 struct interface_data
*d
, *tmp
;
142 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
143 interface_data_del(iface
, d
);
147 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
149 struct interface_data
*n
, *o
;
151 if (!blobmsg_check_attr(data
, true))
152 return UBUS_STATUS_INVALID_ARGUMENT
;
154 const char *name
= blobmsg_name(data
);
155 unsigned len
= blob_pad_len(data
);
157 o
= avl_find_element(&iface
->data
, name
, o
, node
);
159 if (blob_pad_len(o
->data
) == len
&& !memcmp(o
->data
, data
, len
))
162 interface_data_del(iface
, o
);
165 n
= calloc(1, sizeof(*n
) + len
);
166 memcpy(n
->data
, data
, len
);
167 n
->node
.key
= blobmsg_name(n
->data
);
168 avl_insert(&iface
->data
, &n
->node
);
170 iface
->updated
|= IUF_DATA
;
175 interface_event(struct interface
*iface
, enum interface_event ev
)
177 struct interface_user
*dep
, *tmp
;
178 struct device
*adev
= NULL
;
180 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
181 dep
->cb(dep
, iface
, ev
);
183 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
184 dep
->cb(dep
, iface
, ev
);
188 adev
= iface
->l3_dev
.dev
;
191 alias_notify_device(iface
->name
, adev
);
199 interface_flush_state(struct interface
*iface
)
201 if (iface
->l3_dev
.dev
)
202 device_release(&iface
->l3_dev
);
203 interface_data_flush(iface
);
207 mark_interface_down(struct interface
*iface
)
209 enum interface_state state
= iface
->state
;
211 iface
->state
= IFS_DOWN
;
213 interface_event(iface
, IFEV_DOWN
);
214 interface_ip_set_enabled(&iface
->config_ip
, false);
215 interface_ip_flush(&iface
->proto_ip
);
216 interface_flush_state(iface
);
217 system_flush_routes();
221 __interface_set_down(struct interface
*iface
, bool force
)
223 if (iface
->state
== IFS_DOWN
||
224 iface
->state
== IFS_TEARDOWN
)
227 if (iface
->state
== IFS_UP
)
228 interface_event(iface
, IFEV_DOWN
);
229 iface
->state
= IFS_TEARDOWN
;
230 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
232 interface_flush_state(iface
);
235 vlist_delete(&interfaces
, &iface
->node
);
239 interface_cb(struct device_user
*dep
, enum device_event ev
)
241 struct interface
*iface
;
244 iface
= container_of(dep
, struct interface
, main_dev
);
249 case DEV_EVENT_REMOVE
:
256 interface_set_available(iface
, new_state
);
257 if (!new_state
&& dep
->dev
->external
)
258 interface_set_main_dev(iface
, NULL
);
262 interface_set_available(struct interface
*iface
, bool new_state
)
264 if (iface
->available
== new_state
)
267 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
268 iface
->available
= new_state
;
271 if (iface
->autostart
&& !config_init
)
272 interface_set_up(iface
);
274 __interface_set_down(iface
, true);
278 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
281 list_add(&dep
->list
, &iface_all_users
);
286 list_add(&dep
->list
, &iface
->users
);
287 if (iface
->state
== IFS_UP
)
288 dep
->cb(dep
, iface
, IFEV_UP
);
292 interface_remove_user(struct interface_user
*dep
)
294 list_del_init(&dep
->list
);
299 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
301 struct blob_attr
*cur
;
304 blobmsg_for_each_attr(cur
, list
, rem
) {
305 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
308 if (!blobmsg_check_attr(cur
, NULL
))
311 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
312 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
313 list_add(&c
->head
, &iface
->assignment_classes
);
318 interface_clear_assignment_classes(struct interface
*iface
)
320 while (!list_empty(&iface
->assignment_classes
)) {
321 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
322 struct interface_assignment_class
, head
);
329 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
331 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
332 old
->assignment_length
!= new->assignment_length
||
333 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
335 struct interface_assignment_class
*c
;
336 list_for_each_entry(c
, &new->assignment_classes
, head
) {
337 // Compare list entries one-by-one to see if there was a change
338 if (list_empty(&old
->assignment_classes
)) // The new list is longer
344 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
345 struct interface_assignment_class
, head
);
347 if (strcmp(c_old
->name
, c
->name
)) // An entry didn't match
350 list_del(&c_old
->head
);
354 // The old list was longer than the new one or the last entry didn't match
355 if (!list_empty(&old
->assignment_classes
)) {
356 interface_clear_assignment_classes(old
);
360 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
363 old
->assignment_hint
= new->assignment_hint
;
364 old
->assignment_length
= new->assignment_length
;
365 interface_refresh_assignments(true);
370 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
372 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
373 struct device
*dev
= iface
->l3_dev
.dev
;
380 interface_set_main_dev(alias
, dev
);
381 interface_set_available(alias
, true);
384 interface_set_available(alias
, false);
385 interface_set_main_dev(alias
, NULL
);
388 interface_remove_user(dep
);
397 interface_claim_device(struct interface
*iface
)
399 struct interface
*parent
;
400 struct device
*dev
= NULL
;
402 if (iface
->parent_iface
.iface
)
403 interface_remove_user(&iface
->parent_iface
);
405 if (iface
->parent_ifname
) {
406 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
407 iface
->parent_iface
.cb
= interface_alias_cb
;
408 interface_add_user(&iface
->parent_iface
, parent
);
409 } else if (iface
->ifname
&&
410 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
411 dev
= device_get(iface
->ifname
, true);
415 interface_set_main_dev(iface
, dev
);
417 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
418 interface_set_available(iface
, true);
422 interface_cleanup_state(struct interface
*iface
)
424 interface_set_available(iface
, false);
426 interface_flush_state(iface
);
427 interface_clear_errors(iface
);
428 interface_set_proto_state(iface
, NULL
);
430 if (iface
->main_dev
.dev
)
431 interface_set_main_dev(iface
, NULL
);
435 interface_cleanup(struct interface
*iface
)
437 struct interface_user
*dep
, *tmp
;
439 if (iface
->parent_iface
.iface
)
440 interface_remove_user(&iface
->parent_iface
);
442 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
443 interface_remove_user(dep
);
445 interface_clear_assignment_classes(iface
);
446 interface_ip_flush(&iface
->config_ip
);
447 interface_cleanup_state(iface
);
451 interface_do_free(struct interface
*iface
)
453 interface_event(iface
, IFEV_FREE
);
454 interface_cleanup(iface
);
456 netifd_ubus_remove_interface(iface
);
457 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
462 interface_do_reload(struct interface
*iface
)
464 interface_event(iface
, IFEV_RELOAD
);
465 interface_cleanup_state(iface
);
466 proto_init_interface(iface
, iface
->config
);
467 interface_claim_device(iface
);
471 interface_handle_config_change(struct interface
*iface
)
473 enum interface_config_state state
= iface
->config_state
;
475 iface
->config_state
= IFC_NORMAL
;
480 interface_do_reload(iface
);
483 interface_do_free(iface
);
486 if (iface
->autostart
&& iface
->available
)
487 interface_set_up(iface
);
491 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
493 struct interface
*iface
= state
->iface
;
497 if (iface
->state
!= IFS_SETUP
) {
498 interface_event(iface
, IFEV_UPDATE
);
502 interface_ip_set_enabled(&iface
->config_ip
, true);
503 system_flush_routes();
504 iface
->state
= IFS_UP
;
505 iface
->start_time
= system_get_rtime();
506 interface_event(iface
, IFEV_UP
);
507 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
510 if (iface
->state
== IFS_DOWN
)
513 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
514 mark_interface_down(iface
);
515 if (iface
->main_dev
.dev
)
516 device_release(&iface
->main_dev
);
517 interface_handle_config_change(iface
);
519 case IFPEV_LINK_LOST
:
520 if (iface
->state
!= IFS_UP
)
523 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
524 mark_interface_down(iface
);
525 iface
->state
= IFS_SETUP
;
529 interface_write_resolv_conf();
532 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
535 iface
->proto
->free(iface
->proto
);
538 iface
->state
= IFS_DOWN
;
539 iface
->proto
= state
;
543 state
->proto_event
= interface_proto_cb
;
544 state
->iface
= iface
;
548 interface_alloc(const char *name
, struct blob_attr
*config
)
550 struct interface
*iface
;
551 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
552 struct blob_attr
*cur
;
553 const char *proto_name
= NULL
;
556 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
557 iface
->name
= strcpy(iface_name
, name
);
558 INIT_LIST_HEAD(&iface
->errors
);
559 INIT_LIST_HEAD(&iface
->users
);
560 INIT_LIST_HEAD(&iface
->hotplug_list
);
561 INIT_LIST_HEAD(&iface
->assignment_classes
);
562 interface_ip_init(iface
);
563 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
564 iface
->config_ip
.enabled
= false;
566 iface
->main_dev
.cb
= interface_cb
;
568 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
569 blob_data(config
), blob_len(config
));
571 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
572 proto_name
= blobmsg_data(cur
);
574 proto_attach_interface(iface
, proto_name
);
576 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
577 iface
->proto_ip
.no_defaultroute
=
578 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
579 iface
->proto_ip
.no_dns
=
580 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
582 if ((cur
= tb
[IFACE_ATTR_DNS
]))
583 interface_add_dns_server_list(&iface
->config_ip
, cur
);
585 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
586 interface_add_dns_search_list(&iface
->config_ip
, cur
);
588 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
589 iface
->metric
= blobmsg_get_u32(cur
);
591 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
592 iface
->assignment_length
= blobmsg_get_u32(cur
);
594 iface
->assignment_hint
= -1;
595 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
596 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
597 ~((1 << (64 - iface
->assignment_length
)) - 1);
599 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
600 interface_add_assignment_classes(iface
, cur
);
603 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
604 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
605 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
608 // Set a default exteranl routing table for IPv6 to do source-based-filtering
609 struct interface
*iface_old
= vlist_find(&interfaces
, name
, iface_old
, node
);
610 if (iface_old
&& iface_old
->ip6table
> 1000 && iface_old
->ip6table
< 2000)
611 iface
->ip6table
= iface_old
->ip6table
;
613 iface
->ip6table
= 1000 + ++interface_serial
;
615 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
616 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
617 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
620 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
622 iface
->config_autostart
= iface
->autostart
;
626 void interface_set_dynamic(struct interface
*iface
)
628 iface
->dynamic
= true;
629 iface
->node
.version
= -1; // Don't delete on reload
632 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
634 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
635 struct blob_attr
*cur
;
637 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
638 blob_data(config
), blob_len(config
));
641 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
642 iface
->parent_ifname
= blobmsg_data(cur
);
644 if (!iface
->parent_ifname
)
647 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
648 iface
->ifname
= blobmsg_data(cur
);
652 iface
->config
= config
;
653 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
658 interface_add(struct interface
*iface
, struct blob_attr
*config
)
660 __interface_add(iface
, config
, false);
664 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
666 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
669 return __interface_add(iface
, config
, true);
673 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
675 bool enabled
= iface
->config_ip
.enabled
;
676 bool claimed
= iface
->l3_dev
.claimed
;
678 if (iface
->l3_dev
.dev
== dev
)
681 interface_ip_set_enabled(&iface
->config_ip
, false);
682 interface_ip_flush(&iface
->proto_ip
);
683 device_add_user(&iface
->l3_dev
, dev
);
687 device_claim(&iface
->l3_dev
);
688 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
693 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
695 bool set_l3
= (iface
->main_dev
.dev
== iface
->l3_dev
.dev
);
696 bool claimed
= iface
->l3_dev
.claimed
;
698 if (iface
->main_dev
.dev
== dev
)
702 interface_set_l3_dev(iface
, dev
);
704 device_add_user(&iface
->main_dev
, dev
);
709 device_claim(&iface
->l3_dev
);
711 if (!iface
->l3_dev
.dev
)
712 interface_set_l3_dev(iface
, dev
);
716 interface_remove_link(struct interface
*iface
, struct device
*dev
)
718 struct device
*mdev
= iface
->main_dev
.dev
;
720 if (mdev
&& mdev
->hotplug_ops
)
721 return mdev
->hotplug_ops
->del(mdev
, dev
);
723 if (!iface
->main_dev
.hotplug
)
724 return UBUS_STATUS_INVALID_ARGUMENT
;
726 if (dev
!= iface
->main_dev
.dev
)
727 return UBUS_STATUS_INVALID_ARGUMENT
;
729 device_remove_user(&iface
->main_dev
);
734 interface_add_link(struct interface
*iface
, struct device
*dev
)
736 struct device
*mdev
= iface
->main_dev
.dev
;
741 if (iface
->main_dev
.hotplug
)
742 device_remove_user(&iface
->main_dev
);
745 if (mdev
->hotplug_ops
)
746 return mdev
->hotplug_ops
->add(mdev
, dev
);
748 return UBUS_STATUS_NOT_SUPPORTED
;
751 interface_set_main_dev(iface
, dev
);
752 iface
->main_dev
.hotplug
= true;
757 interface_handle_link(struct interface
*iface
, const char *name
, bool add
)
764 dev
= device_get(name
, add
? 2 : 0);
766 ret
= UBUS_STATUS_NOT_FOUND
;
771 device_set_present(dev
, true);
772 if (iface
->device_config
)
773 device_set_config(dev
, &simple_device_type
, iface
->config
);
775 system_if_apply_settings(dev
, &dev
->settings
);
776 ret
= interface_add_link(iface
, dev
);
778 ret
= interface_remove_link(iface
, dev
);
788 interface_set_up(struct interface
*iface
)
792 iface
->autostart
= true;
794 if (iface
->state
!= IFS_DOWN
)
797 interface_clear_errors(iface
);
798 if (!iface
->available
) {
799 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
803 if (iface
->main_dev
.dev
) {
804 ret
= device_claim(&iface
->main_dev
);
809 iface
->state
= IFS_SETUP
;
810 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
812 mark_interface_down(iface
);
820 interface_set_down(struct interface
*iface
)
823 vlist_for_each_element(&interfaces
, iface
, node
)
824 __interface_set_down(iface
, false);
826 iface
->autostart
= false;
827 __interface_set_down(iface
, false);
834 interface_start_pending(void)
836 struct interface
*iface
;
838 vlist_for_each_element(&interfaces
, iface
, node
) {
839 if (iface
->available
&& iface
->autostart
)
840 interface_set_up(iface
);
845 set_config_state(struct interface
*iface
, enum interface_config_state s
)
847 iface
->config_state
= s
;
848 if (iface
->state
== IFS_DOWN
)
849 interface_handle_config_change(iface
);
851 __interface_set_down(iface
, false);
855 interface_update_start(struct interface
*iface
)
858 interface_ip_update_start(&iface
->proto_ip
);
862 interface_update_complete(struct interface
*iface
)
864 interface_ip_update_complete(&iface
->proto_ip
);
868 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
870 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
871 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
875 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
877 struct blob_attr
*old_config
= if_old
->config
;
878 bool reload
= false, reload_ip
= false;
880 #define FIELD_CHANGED_STR(field) \
881 ((!!if_old->field != !!if_new->field) || \
883 strcmp(if_old->field, if_new->field) != 0))
885 if (FIELD_CHANGED_STR(parent_ifname
)) {
886 if (if_old
->parent_iface
.iface
)
887 interface_remove_user(&if_old
->parent_iface
);
891 if (FIELD_CHANGED_STR(ifname
) ||
892 if_old
->proto_handler
!= if_new
->proto_handler
)
895 if (!if_old
->proto_handler
->config_params
)
896 D(INTERFACE
, "No config parameters for interface '%s'\n",
898 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
899 if_old
->proto_handler
->config_params
))
902 #define UPDATE(field, __var) ({ \
903 bool __changed = (if_old->field != if_new->field); \
904 if_old->field = if_new->field; \
905 __var |= __changed; \
908 if_old
->config
= if_new
->config
;
909 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
910 if_old
->autostart
= true;
912 if_old
->device_config
= if_new
->device_config
;
913 if_old
->config_autostart
= if_new
->config_autostart
;
914 if_old
->ifname
= if_new
->ifname
;
915 if_old
->parent_ifname
= if_new
->parent_ifname
;
916 if_old
->proto_handler
= if_new
->proto_handler
;
918 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
919 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
921 UPDATE(metric
, reload_ip
);
922 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
923 UPDATE(ip4table
, reload_ip
);
924 UPDATE(ip6table
, reload_ip
);
925 interface_merge_assignment_data(if_old
, if_new
);
930 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
932 interface_clear_errors(if_old
);
933 set_config_state(if_old
, IFC_RELOAD
);
938 interface_ip_set_enabled(&if_old
->config_ip
, false);
939 interface_ip_set_enabled(&if_old
->proto_ip
, false);
940 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
941 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
944 interface_write_resolv_conf();
947 if_new
->config
= NULL
;
948 interface_cleanup(if_new
);
954 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
955 struct vlist_node
*node_old
)
957 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
958 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
960 if (node_old
&& node_new
) {
961 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
962 interface_change_config(if_old
, if_new
);
963 } else if (node_old
) {
964 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
965 set_config_state(if_old
, IFC_REMOVE
);
966 } else if (node_new
) {
967 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
968 proto_init_interface(if_new
, if_new
->config
);
969 interface_claim_device(if_new
);
970 netifd_ubus_add_interface(if_new
);
976 interface_init_list(void)
978 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
979 interfaces
.keep_old
= true;
980 interfaces
.no_delete
= true;