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
,
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
},
66 static const struct uci_blob_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
67 [IFACE_ATTR_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
68 [IFACE_ATTR_IP6CLASS
] = { .type
= BLOBMSG_TYPE_STRING
},
71 const struct uci_blob_param_list interface_attr_list
= {
72 .n_params
= IFACE_ATTR_MAX
,
73 .params
= iface_attrs
,
74 .info
= iface_attr_info
,
78 interface_clear_errors(struct interface
*iface
)
80 struct interface_error
*error
, *tmp
;
82 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
83 list_del(&error
->list
);
88 void interface_add_error(struct interface
*iface
, const char *subsystem
,
89 const char *code
, const char **data
, int n_data
)
91 struct interface_error
*error
;
94 char *dest
, *d_subsys
, *d_code
;
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;
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);
111 list_add_tail(&error
->list
, &iface
->errors
);
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
]);
119 error
->data
[n_data
++] = NULL
;
122 error
->subsystem
= strcpy(d_subsys
, subsystem
);
125 error
->code
= strcpy(d_code
, code
);
129 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
131 avl_delete(&iface
->data
, &data
->node
);
136 interface_data_flush(struct interface
*iface
)
138 struct interface_data
*d
, *tmp
;
140 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
141 interface_data_del(iface
, d
);
145 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
147 struct interface_data
*n
, *o
;
149 if (!blobmsg_check_attr(data
, true))
150 return UBUS_STATUS_INVALID_ARGUMENT
;
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(n
->data
);
156 o
= avl_find_element(&iface
->data
, n
->node
.key
, o
, node
);
158 interface_data_del(iface
, o
);
160 avl_insert(&iface
->data
, &n
->node
);
165 interface_event(struct interface
*iface
, enum interface_event ev
)
167 struct interface_user
*dep
, *tmp
;
168 struct device
*adev
= NULL
;
170 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
171 dep
->cb(dep
, iface
, ev
);
173 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
174 dep
->cb(dep
, iface
, ev
);
178 adev
= iface
->l3_dev
.dev
;
181 alias_notify_device(iface
->name
, adev
);
189 interface_flush_state(struct interface
*iface
)
191 if (iface
->l3_dev
.dev
)
192 device_release(&iface
->l3_dev
);
193 interface_data_flush(iface
);
197 mark_interface_down(struct interface
*iface
)
199 enum interface_state state
= iface
->state
;
201 iface
->state
= IFS_DOWN
;
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();
211 __interface_set_down(struct interface
*iface
, bool force
)
213 if (iface
->state
== IFS_DOWN
||
214 iface
->state
== IFS_TEARDOWN
)
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
);
222 interface_flush_state(iface
);
225 vlist_delete(&interfaces
, &iface
->node
);
229 interface_cb(struct device_user
*dep
, enum device_event ev
)
231 struct interface
*iface
;
234 iface
= container_of(dep
, struct interface
, main_dev
);
239 case DEV_EVENT_REMOVE
:
246 interface_set_available(iface
, new_state
);
247 if (!new_state
&& dep
->dev
->external
)
248 interface_set_main_dev(iface
, NULL
);
252 interface_set_available(struct interface
*iface
, bool new_state
)
254 if (iface
->available
== new_state
)
257 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
258 iface
->available
= new_state
;
261 if (iface
->autostart
&& !config_init
)
262 interface_set_up(iface
);
264 __interface_set_down(iface
, true);
268 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
271 list_add(&dep
->list
, &iface_all_users
);
276 list_add(&dep
->list
, &iface
->users
);
277 if (iface
->state
== IFS_UP
)
278 dep
->cb(dep
, iface
, IFEV_UP
);
282 interface_remove_user(struct interface_user
*dep
)
284 list_del_init(&dep
->list
);
289 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
291 struct blob_attr
*cur
;
294 blobmsg_for_each_attr(cur
, list
, rem
) {
295 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
298 if (!blobmsg_check_attr(cur
, NULL
))
301 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
302 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
303 list_add(&c
->head
, &iface
->assignment_classes
);
308 interface_clear_assignment_classes(struct interface
*iface
)
310 while (!list_empty(&iface
->assignment_classes
)) {
311 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
312 struct interface_assignment_class
, head
);
319 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
321 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
322 old
->assignment_length
!= new->assignment_length
||
323 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
325 struct interface_assignment_class
*c
;
326 list_for_each_entry(c
, &new->assignment_classes
, head
) {
327 // Compare list entries one-by-one to see if there was a change
328 if (list_empty(&old
->assignment_classes
)) // The new list is longer
334 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
335 struct interface_assignment_class
, head
);
337 if (strcmp(c_old
->name
, c
->name
)) // An entry didn't match
340 list_del(&c_old
->head
);
344 // The old list was longer than the new one or the last entry didn't match
345 if (!list_empty(&old
->assignment_classes
)) {
346 interface_clear_assignment_classes(old
);
350 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
353 old
->assignment_hint
= new->assignment_hint
;
354 old
->assignment_length
= new->assignment_length
;
355 interface_refresh_assignments(true);
360 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
362 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
363 struct device
*dev
= iface
->l3_dev
.dev
;
370 interface_set_main_dev(alias
, dev
);
371 interface_set_available(alias
, true);
374 interface_set_available(alias
, false);
375 interface_set_main_dev(alias
, NULL
);
378 interface_remove_user(dep
);
387 interface_claim_device(struct interface
*iface
)
389 struct interface
*parent
;
390 struct device
*dev
= NULL
;
392 if (iface
->parent_iface
.iface
)
393 interface_remove_user(&iface
->parent_iface
);
395 if (iface
->parent_ifname
) {
396 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
397 iface
->parent_iface
.cb
= interface_alias_cb
;
398 interface_add_user(&iface
->parent_iface
, parent
);
399 } else if (iface
->ifname
&&
400 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
401 dev
= device_get(iface
->ifname
, true);
405 interface_set_main_dev(iface
, dev
);
407 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
408 interface_set_available(iface
, true);
412 interface_cleanup_state(struct interface
*iface
)
414 interface_set_available(iface
, false);
416 interface_flush_state(iface
);
417 interface_clear_errors(iface
);
418 interface_set_proto_state(iface
, NULL
);
420 if (iface
->main_dev
.dev
)
421 interface_set_main_dev(iface
, NULL
);
425 interface_cleanup(struct interface
*iface
)
427 struct interface_user
*dep
, *tmp
;
429 if (iface
->parent_iface
.iface
)
430 interface_remove_user(&iface
->parent_iface
);
432 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
433 interface_remove_user(dep
);
435 interface_clear_assignment_classes(iface
);
436 interface_ip_flush(&iface
->config_ip
);
437 interface_cleanup_state(iface
);
441 interface_do_free(struct interface
*iface
)
443 interface_event(iface
, IFEV_FREE
);
444 interface_cleanup(iface
);
446 netifd_ubus_remove_interface(iface
);
447 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
452 interface_do_reload(struct interface
*iface
)
454 interface_event(iface
, IFEV_RELOAD
);
455 interface_cleanup_state(iface
);
456 proto_init_interface(iface
, iface
->config
);
457 interface_claim_device(iface
);
461 interface_handle_config_change(struct interface
*iface
)
463 enum interface_config_state state
= iface
->config_state
;
465 iface
->config_state
= IFC_NORMAL
;
470 interface_do_reload(iface
);
473 interface_do_free(iface
);
476 if (iface
->autostart
&& iface
->available
)
477 interface_set_up(iface
);
481 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
483 struct interface
*iface
= state
->iface
;
487 if (iface
->state
!= IFS_SETUP
) {
488 interface_event(iface
, IFEV_UPDATE
);
492 interface_ip_set_enabled(&iface
->config_ip
, true);
493 system_flush_routes();
494 iface
->state
= IFS_UP
;
495 iface
->start_time
= system_get_rtime();
496 interface_event(iface
, IFEV_UP
);
497 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
500 if (iface
->state
== IFS_DOWN
)
503 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
504 mark_interface_down(iface
);
505 if (iface
->main_dev
.dev
)
506 device_release(&iface
->main_dev
);
507 interface_handle_config_change(iface
);
509 case IFPEV_LINK_LOST
:
510 if (iface
->state
!= IFS_UP
)
513 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
514 mark_interface_down(iface
);
515 iface
->state
= IFS_SETUP
;
519 interface_write_resolv_conf();
522 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
525 iface
->proto
->free(iface
->proto
);
528 iface
->state
= IFS_DOWN
;
529 iface
->proto
= state
;
533 state
->proto_event
= interface_proto_cb
;
534 state
->iface
= iface
;
538 interface_init(struct interface
*iface
, const char *name
,
539 struct blob_attr
*config
, bool dynamic
)
541 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
542 struct blob_attr
*cur
;
543 const char *proto_name
= NULL
;
545 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
546 INIT_LIST_HEAD(&iface
->errors
);
547 INIT_LIST_HEAD(&iface
->users
);
548 INIT_LIST_HEAD(&iface
->hotplug_list
);
549 INIT_LIST_HEAD(&iface
->assignment_classes
);
550 interface_ip_init(iface
);
551 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
552 iface
->config_ip
.enabled
= false;
554 iface
->main_dev
.cb
= interface_cb
;
556 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
557 blob_data(config
), blob_len(config
));
559 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
560 proto_name
= blobmsg_data(cur
);
562 proto_attach_interface(iface
, proto_name
);
564 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
565 iface
->proto_ip
.no_defaultroute
=
566 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
567 iface
->proto_ip
.no_dns
=
568 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
570 if ((cur
= tb
[IFACE_ATTR_DNS
]))
571 interface_add_dns_server_list(&iface
->config_ip
, cur
);
573 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
574 interface_add_dns_search_list(&iface
->config_ip
, cur
);
576 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
577 iface
->metric
= blobmsg_get_u32(cur
);
579 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
580 iface
->assignment_length
= blobmsg_get_u32(cur
);
582 iface
->assignment_hint
= -1;
583 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
584 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
585 ~((1 << (64 - iface
->assignment_length
)) - 1);
587 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
588 interface_add_assignment_classes(iface
, cur
);
591 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
592 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
593 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
596 // Set a default exteranl routing table for IPv6 to do source-based-filtering
597 struct interface
*iface_old
= vlist_find(&interfaces
, name
, iface_old
, node
);
598 if (iface_old
&& iface_old
->ip6table
> 1000 && iface_old
->ip6table
< 2000)
599 iface
->ip6table
= iface_old
->ip6table
;
601 iface
->ip6table
= 1000 + ++interface_serial
;
603 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
604 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
605 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
608 iface
->config_autostart
= iface
->autostart
;
609 iface
->dynamic
= dynamic
;
612 iface
->node
.version
= -1; // Don't delete on reload
615 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
617 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
618 struct blob_attr
*cur
;
620 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
621 blob_data(config
), blob_len(config
));
624 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
625 iface
->parent_ifname
= blobmsg_data(cur
);
627 if (!iface
->parent_ifname
)
630 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
631 iface
->ifname
= blobmsg_data(cur
);
635 iface
->config
= config
;
636 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
641 interface_add(struct interface
*iface
, struct blob_attr
*config
)
643 __interface_add(iface
, config
, false);
647 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
649 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
652 return __interface_add(iface
, config
, true);
656 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
658 bool enabled
= iface
->config_ip
.enabled
;
659 bool claimed
= iface
->l3_dev
.claimed
;
661 if (iface
->l3_dev
.dev
== dev
)
664 interface_ip_set_enabled(&iface
->config_ip
, false);
665 interface_ip_flush(&iface
->proto_ip
);
666 device_add_user(&iface
->l3_dev
, dev
);
670 device_claim(&iface
->l3_dev
);
671 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
676 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
678 bool set_l3
= (iface
->main_dev
.dev
== iface
->l3_dev
.dev
);
679 bool claimed
= iface
->l3_dev
.claimed
;
681 if (iface
->main_dev
.dev
== dev
)
685 interface_set_l3_dev(iface
, dev
);
687 device_add_user(&iface
->main_dev
, dev
);
692 device_claim(&iface
->l3_dev
);
694 if (!iface
->l3_dev
.dev
)
695 interface_set_l3_dev(iface
, dev
);
699 interface_remove_link(struct interface
*iface
, struct device
*dev
)
701 struct device
*mdev
= iface
->main_dev
.dev
;
703 if (mdev
&& mdev
->hotplug_ops
)
704 return mdev
->hotplug_ops
->del(mdev
, dev
);
706 if (!iface
->main_dev
.hotplug
)
707 return UBUS_STATUS_INVALID_ARGUMENT
;
709 if (dev
!= iface
->main_dev
.dev
)
710 return UBUS_STATUS_INVALID_ARGUMENT
;
712 device_remove_user(&iface
->main_dev
);
717 interface_add_link(struct interface
*iface
, struct device
*dev
)
719 struct device
*mdev
= iface
->main_dev
.dev
;
724 if (iface
->main_dev
.hotplug
)
725 device_remove_user(&iface
->main_dev
);
728 if (mdev
->hotplug_ops
)
729 return mdev
->hotplug_ops
->add(mdev
, dev
);
731 return UBUS_STATUS_NOT_SUPPORTED
;
734 interface_set_main_dev(iface
, dev
);
735 iface
->main_dev
.hotplug
= true;
740 interface_set_up(struct interface
*iface
)
744 iface
->autostart
= true;
746 if (iface
->state
!= IFS_DOWN
)
749 interface_clear_errors(iface
);
750 if (!iface
->available
) {
751 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
755 if (iface
->main_dev
.dev
) {
756 ret
= device_claim(&iface
->main_dev
);
761 iface
->state
= IFS_SETUP
;
762 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
764 mark_interface_down(iface
);
772 interface_set_down(struct interface
*iface
)
775 vlist_for_each_element(&interfaces
, iface
, node
)
776 __interface_set_down(iface
, false);
778 iface
->autostart
= false;
779 __interface_set_down(iface
, false);
786 interface_start_pending(void)
788 struct interface
*iface
;
790 vlist_for_each_element(&interfaces
, iface
, node
) {
791 if (iface
->available
&& iface
->autostart
)
792 interface_set_up(iface
);
797 set_config_state(struct interface
*iface
, enum interface_config_state s
)
799 iface
->config_state
= s
;
800 if (iface
->state
== IFS_DOWN
)
801 interface_handle_config_change(iface
);
803 __interface_set_down(iface
, false);
807 interface_update_start(struct interface
*iface
)
809 interface_ip_update_start(&iface
->proto_ip
);
813 interface_update_complete(struct interface
*iface
)
815 interface_ip_update_complete(&iface
->proto_ip
);
819 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
821 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
822 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
826 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
828 struct blob_attr
*old_config
= if_old
->config
;
829 bool reload
= false, reload_ip
= false;
831 #define FIELD_CHANGED_STR(field) \
832 ((!!if_old->field != !!if_new->field) || \
834 strcmp(if_old->field, if_new->field) != 0))
836 if (FIELD_CHANGED_STR(parent_ifname
)) {
837 if (if_old
->parent_iface
.iface
)
838 interface_remove_user(&if_old
->parent_iface
);
842 if (FIELD_CHANGED_STR(ifname
) ||
843 if_old
->proto_handler
!= if_new
->proto_handler
)
846 if (!if_old
->proto_handler
->config_params
)
847 D(INTERFACE
, "No config parameters for interface '%s'\n",
849 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
850 if_old
->proto_handler
->config_params
))
853 #define UPDATE(field, __var) ({ \
854 bool __changed = (if_old->field != if_new->field); \
855 if_old->field = if_new->field; \
856 __var |= __changed; \
859 if_old
->config
= if_new
->config
;
860 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
861 if_old
->autostart
= true;
863 if_old
->device_config
= if_new
->device_config
;
864 if_old
->config_autostart
= if_new
->config_autostart
;
865 if_old
->ifname
= if_new
->ifname
;
866 if_old
->parent_ifname
= if_new
->parent_ifname
;
867 if_old
->proto_handler
= if_new
->proto_handler
;
869 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
870 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
872 UPDATE(metric
, reload_ip
);
873 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
874 UPDATE(ip4table
, reload_ip
);
875 UPDATE(ip6table
, reload_ip
);
876 interface_merge_assignment_data(if_old
, if_new
);
881 D(INTERFACE
, "Reload interface '%s because of config changes\n",
883 interface_clear_errors(if_old
);
884 set_config_state(if_old
, IFC_RELOAD
);
889 interface_ip_set_enabled(&if_old
->config_ip
, false);
890 interface_ip_set_enabled(&if_old
->proto_ip
, false);
891 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
892 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
895 interface_write_resolv_conf();
898 if_new
->config
= NULL
;
899 interface_cleanup(if_new
);
905 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
906 struct vlist_node
*node_old
)
908 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
909 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
911 if (node_old
&& node_new
) {
912 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
913 interface_change_config(if_old
, if_new
);
914 } else if (node_old
) {
915 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
916 set_config_state(if_old
, IFC_REMOVE
);
917 } else if (node_new
) {
918 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
919 proto_init_interface(if_new
, if_new
->config
);
920 interface_claim_device(if_new
);
921 netifd_ubus_add_interface(if_new
);
927 interface_init_list(void)
929 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
930 interfaces
.keep_old
= true;
931 interfaces
.no_delete
= true;