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
);
34 IFACE_ATTR_DEFAULTROUTE
,
37 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
},
64 [IFACE_ATTR_DELEGATE
] = { .name
= "delegate", .type
= BLOBMSG_TYPE_BOOL
},
67 static const struct uci_blob_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
68 [IFACE_ATTR_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
69 [IFACE_ATTR_IP6CLASS
] = { .type
= BLOBMSG_TYPE_STRING
},
72 const struct uci_blob_param_list interface_attr_list
= {
73 .n_params
= IFACE_ATTR_MAX
,
74 .params
= iface_attrs
,
75 .info
= iface_attr_info
,
79 interface_clear_errors(struct interface
*iface
)
81 struct interface_error
*error
, *tmp
;
83 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
84 list_del(&error
->list
);
89 void interface_add_error(struct interface
*iface
, const char *subsystem
,
90 const char *code
, const char **data
, int n_data
)
92 struct interface_error
*error
;
95 char *dest
, *d_subsys
, *d_code
;
98 len
= n_data
* sizeof(char *);
99 datalen
= alloca(len
);
100 for (i
= 0; i
< n_data
; i
++) {
101 datalen
[i
] = strlen(data
[i
]) + 1;
106 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
107 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
108 &d_code
, code
? strlen(code
) + 1 : 0);
112 list_add_tail(&error
->list
, &iface
->errors
);
114 dest
= (char *) &error
->data
[n_data
+ 1];
115 for (i
= 0; i
< n_data
; i
++) {
116 error
->data
[i
] = dest
;
117 memcpy(dest
, data
[i
], datalen
[i
]);
120 error
->data
[n_data
++] = NULL
;
123 error
->subsystem
= strcpy(d_subsys
, subsystem
);
126 error
->code
= strcpy(d_code
, code
);
130 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
132 avl_delete(&iface
->data
, &data
->node
);
137 interface_data_flush(struct interface
*iface
)
139 struct interface_data
*d
, *tmp
;
141 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
142 interface_data_del(iface
, d
);
146 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
148 struct interface_data
*n
, *o
;
150 if (!blobmsg_check_attr(data
, true))
151 return UBUS_STATUS_INVALID_ARGUMENT
;
153 const char *name
= blobmsg_name(data
);
154 unsigned len
= blob_pad_len(data
);
156 o
= avl_find_element(&iface
->data
, name
, o
, node
);
158 if (blob_pad_len(o
->data
) == len
&& !memcmp(o
->data
, data
, len
))
161 interface_data_del(iface
, o
);
164 n
= calloc(1, sizeof(*n
) + len
);
165 memcpy(n
->data
, data
, len
);
166 n
->node
.key
= blobmsg_name(n
->data
);
167 avl_insert(&iface
->data
, &n
->node
);
169 iface
->updated
|= IUF_DATA
;
174 interface_event(struct interface
*iface
, enum interface_event ev
)
176 struct interface_user
*dep
, *tmp
;
177 struct device
*adev
= NULL
;
179 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
180 dep
->cb(dep
, iface
, ev
);
182 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
183 dep
->cb(dep
, iface
, ev
);
187 adev
= iface
->l3_dev
.dev
;
190 alias_notify_device(iface
->name
, adev
);
198 interface_flush_state(struct interface
*iface
)
200 if (iface
->l3_dev
.dev
)
201 device_release(&iface
->l3_dev
);
202 interface_data_flush(iface
);
206 mark_interface_down(struct interface
*iface
)
208 enum interface_state state
= iface
->state
;
210 if (state
== IFS_DOWN
)
213 iface
->state
= IFS_DOWN
;
215 interface_event(iface
, IFEV_DOWN
);
216 interface_ip_set_enabled(&iface
->config_ip
, false);
217 interface_ip_flush(&iface
->proto_ip
);
218 interface_flush_state(iface
);
219 system_flush_routes();
223 __interface_set_down(struct interface
*iface
, bool force
)
225 enum interface_state state
= iface
->state
;
229 iface
->state
= IFS_TEARDOWN
;
231 interface_event(iface
, IFEV_DOWN
);
233 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
235 interface_flush_state(iface
);
238 vlist_delete(&interfaces
, &iface
->node
);
242 if (iface
->main_dev
.dev
)
243 device_release(&iface
->main_dev
);
251 __interface_set_up(struct interface
*iface
)
255 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
257 iface
->state
= IFS_SETUP
;
258 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
260 mark_interface_down(iface
);
266 interface_check_state(struct interface
*iface
)
268 switch (iface
->state
) {
270 if (!iface
->enabled
|| !iface
->link_state
) {
271 mark_interface_down(iface
);
272 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
276 if (iface
->enabled
&& iface
->link_state
&& !config_init
)
277 __interface_set_up(iface
);
285 interface_set_enabled(struct interface
*iface
, bool new_state
)
287 if (iface
->enabled
== new_state
)
290 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
291 iface
->enabled
= new_state
;
292 interface_check_state(iface
);
296 interface_set_link_state(struct interface
*iface
, bool new_state
)
298 if (iface
->link_state
== new_state
)
301 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
302 iface
->link_state
= new_state
;
303 interface_check_state(iface
);
307 interface_cb(struct device_user
*dep
, enum device_event ev
)
309 struct interface
*iface
;
310 bool new_state
= false;
312 iface
= container_of(dep
, struct interface
, main_dev
);
316 case DEV_EVENT_REMOVE
:
317 interface_set_available(iface
, new_state
);
318 if (!new_state
&& dep
->dev
->external
)
319 interface_set_main_dev(iface
, NULL
);
324 interface_set_enabled(iface
, new_state
);
326 case DEV_EVENT_LINK_UP
:
328 case DEV_EVENT_LINK_DOWN
:
329 interface_set_link_state(iface
, new_state
);
337 interface_set_available(struct interface
*iface
, bool new_state
)
339 if (iface
->available
== new_state
)
342 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
343 iface
->available
= new_state
;
346 if (iface
->autostart
&& !config_init
)
347 interface_set_up(iface
);
349 __interface_set_down(iface
, true);
353 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
356 list_add(&dep
->list
, &iface_all_users
);
361 list_add(&dep
->list
, &iface
->users
);
362 if (iface
->state
== IFS_UP
)
363 dep
->cb(dep
, iface
, IFEV_UP
);
367 interface_remove_user(struct interface_user
*dep
)
369 list_del_init(&dep
->list
);
374 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
376 struct blob_attr
*cur
;
379 blobmsg_for_each_attr(cur
, list
, rem
) {
380 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
383 if (!blobmsg_check_attr(cur
, NULL
))
386 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
387 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
388 list_add(&c
->head
, &iface
->assignment_classes
);
393 interface_clear_assignment_classes(struct interface
*iface
)
395 while (!list_empty(&iface
->assignment_classes
)) {
396 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
397 struct interface_assignment_class
, head
);
404 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
406 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
407 old
->assignment_length
!= new->assignment_length
||
408 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
410 struct interface_assignment_class
*c
;
411 list_for_each_entry(c
, &new->assignment_classes
, head
) {
412 // Compare list entries one-by-one to see if there was a change
413 if (list_empty(&old
->assignment_classes
)) // The new list is longer
419 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
420 struct interface_assignment_class
, head
);
422 if (strcmp(c_old
->name
, c
->name
)) // An entry didn't match
425 list_del(&c_old
->head
);
429 // The old list was longer than the new one or the last entry didn't match
430 if (!list_empty(&old
->assignment_classes
)) {
431 interface_clear_assignment_classes(old
);
435 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
438 old
->assignment_hint
= new->assignment_hint
;
439 old
->assignment_length
= new->assignment_length
;
440 interface_refresh_assignments(true);
445 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
447 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
448 struct device
*dev
= iface
->l3_dev
.dev
;
455 interface_set_main_dev(alias
, dev
);
456 interface_set_available(alias
, true);
459 interface_set_available(alias
, false);
460 interface_set_main_dev(alias
, NULL
);
463 interface_remove_user(dep
);
472 interface_claim_device(struct interface
*iface
)
474 struct interface
*parent
;
475 struct device
*dev
= NULL
;
477 if (iface
->parent_iface
.iface
)
478 interface_remove_user(&iface
->parent_iface
);
480 if (iface
->parent_ifname
) {
481 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
482 iface
->parent_iface
.cb
= interface_alias_cb
;
483 interface_add_user(&iface
->parent_iface
, parent
);
484 } else if (iface
->ifname
&&
485 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
486 dev
= device_get(iface
->ifname
, true);
490 interface_set_main_dev(iface
, dev
);
492 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
493 interface_set_available(iface
, true);
497 interface_cleanup_state(struct interface
*iface
)
499 interface_set_available(iface
, false);
501 interface_flush_state(iface
);
502 interface_clear_errors(iface
);
503 interface_set_proto_state(iface
, NULL
);
505 if (iface
->main_dev
.dev
)
506 interface_set_main_dev(iface
, NULL
);
510 interface_cleanup(struct interface
*iface
)
512 struct interface_user
*dep
, *tmp
;
514 if (iface
->parent_iface
.iface
)
515 interface_remove_user(&iface
->parent_iface
);
517 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
518 interface_remove_user(dep
);
520 interface_clear_assignment_classes(iface
);
521 interface_ip_flush(&iface
->config_ip
);
522 interface_cleanup_state(iface
);
526 interface_do_free(struct interface
*iface
)
528 interface_event(iface
, IFEV_FREE
);
529 interface_cleanup(iface
);
531 netifd_ubus_remove_interface(iface
);
532 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
537 interface_do_reload(struct interface
*iface
)
539 interface_event(iface
, IFEV_RELOAD
);
540 interface_cleanup_state(iface
);
541 proto_init_interface(iface
, iface
->config
);
542 interface_claim_device(iface
);
546 interface_handle_config_change(struct interface
*iface
)
548 enum interface_config_state state
= iface
->config_state
;
550 iface
->config_state
= IFC_NORMAL
;
555 interface_do_reload(iface
);
558 interface_do_free(iface
);
561 if (iface
->autostart
&& iface
->available
)
562 interface_set_up(iface
);
566 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
568 struct interface
*iface
= state
->iface
;
572 if (iface
->state
!= IFS_SETUP
) {
573 interface_event(iface
, IFEV_UPDATE
);
577 interface_ip_set_enabled(&iface
->config_ip
, true);
578 system_flush_routes();
579 iface
->state
= IFS_UP
;
580 iface
->start_time
= system_get_rtime();
581 interface_event(iface
, IFEV_UP
);
582 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
585 if (iface
->state
== IFS_DOWN
)
588 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
589 mark_interface_down(iface
);
590 if (iface
->main_dev
.dev
)
591 device_release(&iface
->main_dev
);
592 interface_handle_config_change(iface
);
594 case IFPEV_LINK_LOST
:
595 if (iface
->state
!= IFS_UP
)
598 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
599 mark_interface_down(iface
);
600 iface
->state
= IFS_SETUP
;
604 interface_write_resolv_conf();
607 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
610 iface
->proto
->free(iface
->proto
);
613 iface
->state
= IFS_DOWN
;
614 iface
->proto
= state
;
618 state
->proto_event
= interface_proto_cb
;
619 state
->iface
= iface
;
623 interface_alloc(const char *name
, struct blob_attr
*config
)
625 struct interface
*iface
;
626 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
627 struct blob_attr
*cur
;
628 const char *proto_name
= NULL
;
631 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
632 iface
->name
= strcpy(iface_name
, name
);
633 INIT_LIST_HEAD(&iface
->errors
);
634 INIT_LIST_HEAD(&iface
->users
);
635 INIT_LIST_HEAD(&iface
->hotplug_list
);
636 INIT_LIST_HEAD(&iface
->assignment_classes
);
637 interface_ip_init(iface
);
638 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
639 iface
->config_ip
.enabled
= false;
641 iface
->main_dev
.cb
= interface_cb
;
643 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
644 blob_data(config
), blob_len(config
));
646 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
647 proto_name
= blobmsg_data(cur
);
649 proto_attach_interface(iface
, proto_name
);
651 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
652 iface
->proto_ip
.no_defaultroute
=
653 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
654 iface
->proto_ip
.no_dns
=
655 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
657 if ((cur
= tb
[IFACE_ATTR_DNS
]))
658 interface_add_dns_server_list(&iface
->config_ip
, cur
);
660 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
661 interface_add_dns_search_list(&iface
->config_ip
, cur
);
663 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
664 iface
->metric
= blobmsg_get_u32(cur
);
666 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
667 iface
->assignment_length
= blobmsg_get_u32(cur
);
669 iface
->assignment_hint
= -1;
670 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
671 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
672 ~((1 << (64 - iface
->assignment_length
)) - 1);
674 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
675 interface_add_assignment_classes(iface
, cur
);
678 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
679 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
680 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
683 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
684 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
685 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
688 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
690 iface
->config_autostart
= iface
->autostart
;
694 void interface_set_dynamic(struct interface
*iface
)
696 iface
->dynamic
= true;
697 iface
->node
.version
= -1; // Don't delete on reload
700 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
702 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
703 struct blob_attr
*cur
;
705 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
706 blob_data(config
), blob_len(config
));
709 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
710 iface
->parent_ifname
= blobmsg_data(cur
);
712 if (!iface
->parent_ifname
)
715 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
716 iface
->ifname
= blobmsg_data(cur
);
720 iface
->config
= config
;
721 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
726 interface_add(struct interface
*iface
, struct blob_attr
*config
)
728 __interface_add(iface
, config
, false);
732 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
734 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
737 return __interface_add(iface
, config
, true);
741 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
743 bool enabled
= iface
->config_ip
.enabled
;
744 bool claimed
= iface
->l3_dev
.claimed
;
746 if (iface
->l3_dev
.dev
== dev
)
749 interface_ip_set_enabled(&iface
->config_ip
, false);
750 interface_ip_flush(&iface
->proto_ip
);
751 device_add_user(&iface
->l3_dev
, dev
);
755 device_claim(&iface
->l3_dev
);
756 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
761 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
763 bool set_l3
= (!dev
|| iface
->main_dev
.dev
== iface
->l3_dev
.dev
);
764 bool claimed
= iface
->l3_dev
.claimed
;
766 if (iface
->main_dev
.dev
== dev
)
770 interface_set_l3_dev(iface
, dev
);
772 device_add_user(&iface
->main_dev
, dev
);
774 interface_set_link_state(iface
, false);
779 device_claim(&iface
->l3_dev
);
781 if (!iface
->l3_dev
.dev
)
782 interface_set_l3_dev(iface
, dev
);
786 interface_remove_link(struct interface
*iface
, struct device
*dev
)
788 struct device
*mdev
= iface
->main_dev
.dev
;
790 if (mdev
&& mdev
->hotplug_ops
)
791 return mdev
->hotplug_ops
->del(mdev
, dev
);
793 if (!iface
->main_dev
.hotplug
)
794 return UBUS_STATUS_INVALID_ARGUMENT
;
796 if (dev
!= iface
->main_dev
.dev
)
797 return UBUS_STATUS_INVALID_ARGUMENT
;
799 device_remove_user(&iface
->main_dev
);
804 interface_add_link(struct interface
*iface
, struct device
*dev
)
806 struct device
*mdev
= iface
->main_dev
.dev
;
811 if (iface
->main_dev
.hotplug
)
812 device_remove_user(&iface
->main_dev
);
815 if (mdev
->hotplug_ops
)
816 return mdev
->hotplug_ops
->add(mdev
, dev
);
818 return UBUS_STATUS_NOT_SUPPORTED
;
821 interface_set_main_dev(iface
, dev
);
822 iface
->main_dev
.hotplug
= true;
827 interface_handle_link(struct interface
*iface
, const char *name
, bool add
)
834 dev
= device_get(name
, add
? 2 : 0);
836 ret
= UBUS_STATUS_NOT_FOUND
;
841 device_set_present(dev
, true);
842 if (iface
->device_config
)
843 device_set_config(dev
, &simple_device_type
, iface
->config
);
845 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
846 ret
= interface_add_link(iface
, dev
);
848 ret
= interface_remove_link(iface
, dev
);
858 interface_set_up(struct interface
*iface
)
862 iface
->autostart
= true;
864 if (iface
->state
!= IFS_DOWN
)
867 interface_clear_errors(iface
);
868 if (!iface
->available
) {
869 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
873 if (iface
->main_dev
.dev
) {
874 ret
= device_claim(&iface
->main_dev
);
876 interface_check_state(iface
);
879 ret
= __interface_set_up(iface
);
885 interface_set_down(struct interface
*iface
)
888 vlist_for_each_element(&interfaces
, iface
, node
)
889 __interface_set_down(iface
, false);
891 iface
->autostart
= false;
892 __interface_set_down(iface
, false);
899 interface_start_pending(void)
901 struct interface
*iface
;
903 vlist_for_each_element(&interfaces
, iface
, node
) {
904 if (iface
->available
&& iface
->autostart
)
905 interface_set_up(iface
);
910 set_config_state(struct interface
*iface
, enum interface_config_state s
)
912 iface
->config_state
= s
;
913 if (iface
->state
== IFS_DOWN
)
914 interface_handle_config_change(iface
);
916 __interface_set_down(iface
, false);
920 interface_update_start(struct interface
*iface
)
923 interface_ip_update_start(&iface
->proto_ip
);
927 interface_update_complete(struct interface
*iface
)
929 interface_ip_update_complete(&iface
->proto_ip
);
933 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
935 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
936 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
940 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
942 struct blob_attr
*old_config
= if_old
->config
;
943 bool reload
= false, reload_ip
= false;
945 #define FIELD_CHANGED_STR(field) \
946 ((!!if_old->field != !!if_new->field) || \
948 strcmp(if_old->field, if_new->field) != 0))
950 if (FIELD_CHANGED_STR(parent_ifname
)) {
951 if (if_old
->parent_iface
.iface
)
952 interface_remove_user(&if_old
->parent_iface
);
956 if (FIELD_CHANGED_STR(ifname
) ||
957 if_old
->proto_handler
!= if_new
->proto_handler
)
960 if (!if_old
->proto_handler
->config_params
)
961 D(INTERFACE
, "No config parameters for interface '%s'\n",
963 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
964 if_old
->proto_handler
->config_params
))
967 #define UPDATE(field, __var) ({ \
968 bool __changed = (if_old->field != if_new->field); \
969 if_old->field = if_new->field; \
970 __var |= __changed; \
973 if_old
->config
= if_new
->config
;
974 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
975 if_old
->autostart
= true;
977 if_old
->device_config
= if_new
->device_config
;
978 if_old
->config_autostart
= if_new
->config_autostart
;
979 if_old
->ifname
= if_new
->ifname
;
980 if_old
->parent_ifname
= if_new
->parent_ifname
;
981 if_old
->proto_handler
= if_new
->proto_handler
;
983 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
984 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
986 UPDATE(metric
, reload_ip
);
987 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
988 UPDATE(ip4table
, reload_ip
);
989 UPDATE(ip6table
, reload_ip
);
990 interface_merge_assignment_data(if_old
, if_new
);
995 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
997 interface_clear_errors(if_old
);
998 set_config_state(if_old
, IFC_RELOAD
);
1003 interface_ip_set_enabled(&if_old
->config_ip
, false);
1004 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1005 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
1006 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
1009 interface_write_resolv_conf();
1012 if_new
->config
= NULL
;
1013 interface_cleanup(if_new
);
1019 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1020 struct vlist_node
*node_old
)
1022 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1023 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1025 if (node_old
&& node_new
) {
1026 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1027 interface_change_config(if_old
, if_new
);
1028 } else if (node_old
) {
1029 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1030 set_config_state(if_old
, IFC_REMOVE
);
1031 } else if (node_new
) {
1032 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1033 proto_init_interface(if_new
, if_new
->config
);
1034 interface_claim_device(if_new
);
1035 netifd_ubus_add_interface(if_new
);
1041 interface_init_list(void)
1043 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1044 interfaces
.keep_old
= true;
1045 interfaces
.no_delete
= true;