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 n
= calloc(1, sizeof(*n
) + blob_pad_len(data
));
155 memcpy(n
->data
, data
, blob_pad_len(data
));
156 n
->node
.key
= blobmsg_name(n
->data
);
158 o
= avl_find_element(&iface
->data
, n
->node
.key
, o
, node
);
160 interface_data_del(iface
, o
);
162 avl_insert(&iface
->data
, &n
->node
);
167 interface_event(struct interface
*iface
, enum interface_event ev
)
169 struct interface_user
*dep
, *tmp
;
170 struct device
*adev
= NULL
;
172 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
173 dep
->cb(dep
, iface
, ev
);
175 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
176 dep
->cb(dep
, iface
, ev
);
180 adev
= iface
->l3_dev
.dev
;
183 alias_notify_device(iface
->name
, adev
);
191 interface_flush_state(struct interface
*iface
)
193 if (iface
->l3_dev
.dev
)
194 device_release(&iface
->l3_dev
);
195 interface_data_flush(iface
);
199 mark_interface_down(struct interface
*iface
)
201 enum interface_state state
= iface
->state
;
203 iface
->state
= IFS_DOWN
;
205 interface_event(iface
, IFEV_DOWN
);
206 interface_ip_set_enabled(&iface
->config_ip
, false);
207 interface_ip_flush(&iface
->proto_ip
);
208 interface_flush_state(iface
);
209 system_flush_routes();
213 __interface_set_down(struct interface
*iface
, bool force
)
215 if (iface
->state
== IFS_DOWN
||
216 iface
->state
== IFS_TEARDOWN
)
219 if (iface
->state
== IFS_UP
)
220 interface_event(iface
, IFEV_DOWN
);
221 iface
->state
= IFS_TEARDOWN
;
222 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
224 interface_flush_state(iface
);
227 vlist_delete(&interfaces
, &iface
->node
);
231 interface_cb(struct device_user
*dep
, enum device_event ev
)
233 struct interface
*iface
;
236 iface
= container_of(dep
, struct interface
, main_dev
);
241 case DEV_EVENT_REMOVE
:
248 interface_set_available(iface
, new_state
);
249 if (!new_state
&& dep
->dev
->external
)
250 interface_set_main_dev(iface
, NULL
);
254 interface_set_available(struct interface
*iface
, bool new_state
)
256 if (iface
->available
== new_state
)
259 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
260 iface
->available
= new_state
;
263 if (iface
->autostart
&& !config_init
)
264 interface_set_up(iface
);
266 __interface_set_down(iface
, true);
270 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
273 list_add(&dep
->list
, &iface_all_users
);
278 list_add(&dep
->list
, &iface
->users
);
279 if (iface
->state
== IFS_UP
)
280 dep
->cb(dep
, iface
, IFEV_UP
);
284 interface_remove_user(struct interface_user
*dep
)
286 list_del_init(&dep
->list
);
291 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
293 struct blob_attr
*cur
;
296 blobmsg_for_each_attr(cur
, list
, rem
) {
297 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
300 if (!blobmsg_check_attr(cur
, NULL
))
303 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
304 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
305 list_add(&c
->head
, &iface
->assignment_classes
);
310 interface_clear_assignment_classes(struct interface
*iface
)
312 while (!list_empty(&iface
->assignment_classes
)) {
313 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
314 struct interface_assignment_class
, head
);
321 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
323 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
324 old
->assignment_length
!= new->assignment_length
||
325 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
327 struct interface_assignment_class
*c
;
328 list_for_each_entry(c
, &new->assignment_classes
, head
) {
329 // Compare list entries one-by-one to see if there was a change
330 if (list_empty(&old
->assignment_classes
)) // The new list is longer
336 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
337 struct interface_assignment_class
, head
);
339 if (strcmp(c_old
->name
, c
->name
)) // An entry didn't match
342 list_del(&c_old
->head
);
346 // The old list was longer than the new one or the last entry didn't match
347 if (!list_empty(&old
->assignment_classes
)) {
348 interface_clear_assignment_classes(old
);
352 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
355 old
->assignment_hint
= new->assignment_hint
;
356 old
->assignment_length
= new->assignment_length
;
357 interface_refresh_assignments(true);
362 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
364 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
365 struct device
*dev
= iface
->l3_dev
.dev
;
372 interface_set_main_dev(alias
, dev
);
373 interface_set_available(alias
, true);
376 interface_set_available(alias
, false);
377 interface_set_main_dev(alias
, NULL
);
380 interface_remove_user(dep
);
389 interface_claim_device(struct interface
*iface
)
391 struct interface
*parent
;
392 struct device
*dev
= NULL
;
394 if (iface
->parent_iface
.iface
)
395 interface_remove_user(&iface
->parent_iface
);
397 if (iface
->parent_ifname
) {
398 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
399 iface
->parent_iface
.cb
= interface_alias_cb
;
400 interface_add_user(&iface
->parent_iface
, parent
);
401 } else if (iface
->ifname
&&
402 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
403 dev
= device_get(iface
->ifname
, true);
407 interface_set_main_dev(iface
, dev
);
409 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
410 interface_set_available(iface
, true);
414 interface_cleanup_state(struct interface
*iface
)
416 interface_set_available(iface
, false);
418 interface_flush_state(iface
);
419 interface_clear_errors(iface
);
420 interface_set_proto_state(iface
, NULL
);
422 if (iface
->main_dev
.dev
)
423 interface_set_main_dev(iface
, NULL
);
427 interface_cleanup(struct interface
*iface
)
429 struct interface_user
*dep
, *tmp
;
431 if (iface
->parent_iface
.iface
)
432 interface_remove_user(&iface
->parent_iface
);
434 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
435 interface_remove_user(dep
);
437 interface_clear_assignment_classes(iface
);
438 interface_ip_flush(&iface
->config_ip
);
439 interface_cleanup_state(iface
);
443 interface_do_free(struct interface
*iface
)
445 interface_event(iface
, IFEV_FREE
);
446 interface_cleanup(iface
);
448 netifd_ubus_remove_interface(iface
);
449 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
454 interface_do_reload(struct interface
*iface
)
456 interface_event(iface
, IFEV_RELOAD
);
457 interface_cleanup_state(iface
);
458 proto_init_interface(iface
, iface
->config
);
459 interface_claim_device(iface
);
463 interface_handle_config_change(struct interface
*iface
)
465 enum interface_config_state state
= iface
->config_state
;
467 iface
->config_state
= IFC_NORMAL
;
472 interface_do_reload(iface
);
475 interface_do_free(iface
);
478 if (iface
->autostart
&& iface
->available
)
479 interface_set_up(iface
);
483 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
485 struct interface
*iface
= state
->iface
;
489 if (iface
->state
!= IFS_SETUP
) {
490 interface_event(iface
, IFEV_UPDATE
);
494 interface_ip_set_enabled(&iface
->config_ip
, true);
495 system_flush_routes();
496 iface
->state
= IFS_UP
;
497 iface
->start_time
= system_get_rtime();
498 interface_event(iface
, IFEV_UP
);
499 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
502 if (iface
->state
== IFS_DOWN
)
505 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
506 mark_interface_down(iface
);
507 if (iface
->main_dev
.dev
)
508 device_release(&iface
->main_dev
);
509 interface_handle_config_change(iface
);
511 case IFPEV_LINK_LOST
:
512 if (iface
->state
!= IFS_UP
)
515 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
516 mark_interface_down(iface
);
517 iface
->state
= IFS_SETUP
;
521 interface_write_resolv_conf();
524 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
527 iface
->proto
->free(iface
->proto
);
530 iface
->state
= IFS_DOWN
;
531 iface
->proto
= state
;
535 state
->proto_event
= interface_proto_cb
;
536 state
->iface
= iface
;
540 interface_init(struct interface
*iface
, const char *name
,
541 struct blob_attr
*config
, bool dynamic
)
543 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
544 struct blob_attr
*cur
;
545 const char *proto_name
= NULL
;
547 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
548 INIT_LIST_HEAD(&iface
->errors
);
549 INIT_LIST_HEAD(&iface
->users
);
550 INIT_LIST_HEAD(&iface
->hotplug_list
);
551 INIT_LIST_HEAD(&iface
->assignment_classes
);
552 interface_ip_init(iface
);
553 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
554 iface
->config_ip
.enabled
= false;
556 iface
->main_dev
.cb
= interface_cb
;
558 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
559 blob_data(config
), blob_len(config
));
561 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
562 proto_name
= blobmsg_data(cur
);
564 proto_attach_interface(iface
, proto_name
);
566 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
567 iface
->proto_ip
.no_defaultroute
=
568 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
569 iface
->proto_ip
.no_dns
=
570 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
572 if ((cur
= tb
[IFACE_ATTR_DNS
]))
573 interface_add_dns_server_list(&iface
->config_ip
, cur
);
575 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
576 interface_add_dns_search_list(&iface
->config_ip
, cur
);
578 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
579 iface
->metric
= blobmsg_get_u32(cur
);
581 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
582 iface
->assignment_length
= blobmsg_get_u32(cur
);
584 iface
->assignment_hint
= -1;
585 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
586 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
587 ~((1 << (64 - iface
->assignment_length
)) - 1);
589 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
590 interface_add_assignment_classes(iface
, cur
);
593 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
594 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
595 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
598 // Set a default exteranl routing table for IPv6 to do source-based-filtering
599 struct interface
*iface_old
= vlist_find(&interfaces
, name
, iface_old
, node
);
600 if (iface_old
&& iface_old
->ip6table
> 1000 && iface_old
->ip6table
< 2000)
601 iface
->ip6table
= iface_old
->ip6table
;
603 iface
->ip6table
= 1000 + ++interface_serial
;
605 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
606 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
607 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
610 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
612 iface
->config_autostart
= iface
->autostart
;
613 iface
->dynamic
= dynamic
;
616 iface
->node
.version
= -1; // Don't delete on reload
619 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
621 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
622 struct blob_attr
*cur
;
624 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
625 blob_data(config
), blob_len(config
));
628 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
629 iface
->parent_ifname
= blobmsg_data(cur
);
631 if (!iface
->parent_ifname
)
634 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
635 iface
->ifname
= blobmsg_data(cur
);
639 iface
->config
= config
;
640 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
645 interface_add(struct interface
*iface
, struct blob_attr
*config
)
647 __interface_add(iface
, config
, false);
651 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
653 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
656 return __interface_add(iface
, config
, true);
660 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
662 bool enabled
= iface
->config_ip
.enabled
;
663 bool claimed
= iface
->l3_dev
.claimed
;
665 if (iface
->l3_dev
.dev
== dev
)
668 interface_ip_set_enabled(&iface
->config_ip
, false);
669 interface_ip_flush(&iface
->proto_ip
);
670 device_add_user(&iface
->l3_dev
, dev
);
674 device_claim(&iface
->l3_dev
);
675 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
680 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
682 bool set_l3
= (iface
->main_dev
.dev
== iface
->l3_dev
.dev
);
683 bool claimed
= iface
->l3_dev
.claimed
;
685 if (iface
->main_dev
.dev
== dev
)
689 interface_set_l3_dev(iface
, dev
);
691 device_add_user(&iface
->main_dev
, dev
);
696 device_claim(&iface
->l3_dev
);
698 if (!iface
->l3_dev
.dev
)
699 interface_set_l3_dev(iface
, dev
);
703 interface_remove_link(struct interface
*iface
, struct device
*dev
)
705 struct device
*mdev
= iface
->main_dev
.dev
;
707 if (mdev
&& mdev
->hotplug_ops
)
708 return mdev
->hotplug_ops
->del(mdev
, dev
);
710 if (!iface
->main_dev
.hotplug
)
711 return UBUS_STATUS_INVALID_ARGUMENT
;
713 if (dev
!= iface
->main_dev
.dev
)
714 return UBUS_STATUS_INVALID_ARGUMENT
;
716 device_remove_user(&iface
->main_dev
);
721 interface_add_link(struct interface
*iface
, struct device
*dev
)
723 struct device
*mdev
= iface
->main_dev
.dev
;
728 if (iface
->main_dev
.hotplug
)
729 device_remove_user(&iface
->main_dev
);
732 if (mdev
->hotplug_ops
)
733 return mdev
->hotplug_ops
->add(mdev
, dev
);
735 return UBUS_STATUS_NOT_SUPPORTED
;
738 interface_set_main_dev(iface
, dev
);
739 iface
->main_dev
.hotplug
= true;
744 interface_set_up(struct interface
*iface
)
748 iface
->autostart
= true;
750 if (iface
->state
!= IFS_DOWN
)
753 interface_clear_errors(iface
);
754 if (!iface
->available
) {
755 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
759 if (iface
->main_dev
.dev
) {
760 ret
= device_claim(&iface
->main_dev
);
765 iface
->state
= IFS_SETUP
;
766 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
768 mark_interface_down(iface
);
776 interface_set_down(struct interface
*iface
)
779 vlist_for_each_element(&interfaces
, iface
, node
)
780 __interface_set_down(iface
, false);
782 iface
->autostart
= false;
783 __interface_set_down(iface
, false);
790 interface_start_pending(void)
792 struct interface
*iface
;
794 vlist_for_each_element(&interfaces
, iface
, node
) {
795 if (iface
->available
&& iface
->autostart
)
796 interface_set_up(iface
);
801 set_config_state(struct interface
*iface
, enum interface_config_state s
)
803 iface
->config_state
= s
;
804 if (iface
->state
== IFS_DOWN
)
805 interface_handle_config_change(iface
);
807 __interface_set_down(iface
, false);
811 interface_update_start(struct interface
*iface
)
813 interface_ip_update_start(&iface
->proto_ip
);
817 interface_update_complete(struct interface
*iface
)
819 interface_ip_update_complete(&iface
->proto_ip
);
823 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
825 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
826 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
830 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
832 struct blob_attr
*old_config
= if_old
->config
;
833 bool reload
= false, reload_ip
= false;
835 #define FIELD_CHANGED_STR(field) \
836 ((!!if_old->field != !!if_new->field) || \
838 strcmp(if_old->field, if_new->field) != 0))
840 if (FIELD_CHANGED_STR(parent_ifname
)) {
841 if (if_old
->parent_iface
.iface
)
842 interface_remove_user(&if_old
->parent_iface
);
846 if (FIELD_CHANGED_STR(ifname
) ||
847 if_old
->proto_handler
!= if_new
->proto_handler
)
850 if (!if_old
->proto_handler
->config_params
)
851 D(INTERFACE
, "No config parameters for interface '%s'\n",
853 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
854 if_old
->proto_handler
->config_params
))
857 #define UPDATE(field, __var) ({ \
858 bool __changed = (if_old->field != if_new->field); \
859 if_old->field = if_new->field; \
860 __var |= __changed; \
863 if_old
->config
= if_new
->config
;
864 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
865 if_old
->autostart
= true;
867 if_old
->device_config
= if_new
->device_config
;
868 if_old
->config_autostart
= if_new
->config_autostart
;
869 if_old
->ifname
= if_new
->ifname
;
870 if_old
->parent_ifname
= if_new
->parent_ifname
;
871 if_old
->proto_handler
= if_new
->proto_handler
;
873 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
874 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
876 UPDATE(metric
, reload_ip
);
877 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
878 UPDATE(ip4table
, reload_ip
);
879 UPDATE(ip6table
, reload_ip
);
880 interface_merge_assignment_data(if_old
, if_new
);
885 D(INTERFACE
, "Reload interface '%s because of config changes\n",
887 interface_clear_errors(if_old
);
888 set_config_state(if_old
, IFC_RELOAD
);
893 interface_ip_set_enabled(&if_old
->config_ip
, false);
894 interface_ip_set_enabled(&if_old
->proto_ip
, false);
895 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
896 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
899 interface_write_resolv_conf();
902 if_new
->config
= NULL
;
903 interface_cleanup(if_new
);
909 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
910 struct vlist_node
*node_old
)
912 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
913 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
915 if (node_old
&& node_new
) {
916 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
917 interface_change_config(if_old
, if_new
);
918 } else if (node_old
) {
919 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
920 set_config_state(if_old
, IFC_REMOVE
);
921 } else if (node_new
) {
922 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
923 proto_init_interface(if_new
, if_new
->config
);
924 interface_claim_device(if_new
);
925 netifd_ubus_add_interface(if_new
);
931 interface_init_list(void)
933 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
934 interfaces
.keep_old
= true;
935 interfaces
.no_delete
= true;