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
->autostart
&& 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
);
331 case DEV_EVENT_TOPO_CHANGE
:
332 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
340 interface_set_available(struct interface
*iface
, bool new_state
)
342 if (iface
->available
== new_state
)
345 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
346 iface
->available
= new_state
;
349 if (iface
->autostart
&& !config_init
)
350 interface_set_up(iface
);
352 __interface_set_down(iface
, true);
356 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
359 list_add(&dep
->list
, &iface_all_users
);
364 list_add(&dep
->list
, &iface
->users
);
365 if (iface
->state
== IFS_UP
)
366 dep
->cb(dep
, iface
, IFEV_UP
);
370 interface_remove_user(struct interface_user
*dep
)
372 list_del_init(&dep
->list
);
377 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
379 struct blob_attr
*cur
;
382 blobmsg_for_each_attr(cur
, list
, rem
) {
383 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
386 if (!blobmsg_check_attr(cur
, NULL
))
389 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
390 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
391 list_add(&c
->head
, &iface
->assignment_classes
);
396 interface_clear_assignment_classes(struct interface
*iface
)
398 while (!list_empty(&iface
->assignment_classes
)) {
399 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
400 struct interface_assignment_class
, head
);
407 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
409 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
410 old
->assignment_length
!= new->assignment_length
||
411 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
413 struct interface_assignment_class
*c
;
414 list_for_each_entry(c
, &new->assignment_classes
, head
) {
415 // Compare list entries one-by-one to see if there was a change
416 if (list_empty(&old
->assignment_classes
)) // The new list is longer
422 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
423 struct interface_assignment_class
, head
);
425 if (strcmp(c_old
->name
, c
->name
)) // An entry didn't match
428 list_del(&c_old
->head
);
432 // The old list was longer than the new one or the last entry didn't match
433 if (!list_empty(&old
->assignment_classes
)) {
434 interface_clear_assignment_classes(old
);
438 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
441 old
->assignment_hint
= new->assignment_hint
;
442 old
->assignment_length
= new->assignment_length
;
443 interface_refresh_assignments(true);
448 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
450 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
451 struct device
*dev
= iface
->l3_dev
.dev
;
458 interface_set_main_dev(alias
, dev
);
459 interface_set_available(alias
, true);
462 interface_set_available(alias
, false);
463 interface_set_main_dev(alias
, NULL
);
466 interface_remove_user(dep
);
475 interface_claim_device(struct interface
*iface
)
477 struct interface
*parent
;
478 struct device
*dev
= NULL
;
480 if (iface
->parent_iface
.iface
)
481 interface_remove_user(&iface
->parent_iface
);
483 if (iface
->parent_ifname
) {
484 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
485 iface
->parent_iface
.cb
= interface_alias_cb
;
486 interface_add_user(&iface
->parent_iface
, parent
);
487 } else if (iface
->ifname
&&
488 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
489 dev
= device_get(iface
->ifname
, true);
493 interface_set_main_dev(iface
, dev
);
495 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
496 interface_set_available(iface
, true);
500 interface_cleanup_state(struct interface
*iface
)
502 interface_set_available(iface
, false);
504 interface_flush_state(iface
);
505 interface_clear_errors(iface
);
506 interface_set_proto_state(iface
, NULL
);
508 if (iface
->main_dev
.dev
)
509 interface_set_main_dev(iface
, NULL
);
513 interface_cleanup(struct interface
*iface
)
515 struct interface_user
*dep
, *tmp
;
517 if (iface
->parent_iface
.iface
)
518 interface_remove_user(&iface
->parent_iface
);
520 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
521 interface_remove_user(dep
);
523 interface_clear_assignment_classes(iface
);
524 interface_ip_flush(&iface
->config_ip
);
525 interface_cleanup_state(iface
);
529 interface_do_free(struct interface
*iface
)
531 interface_event(iface
, IFEV_FREE
);
532 interface_cleanup(iface
);
534 netifd_ubus_remove_interface(iface
);
535 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
540 interface_do_reload(struct interface
*iface
)
542 interface_event(iface
, IFEV_RELOAD
);
543 interface_cleanup_state(iface
);
544 proto_init_interface(iface
, iface
->config
);
545 interface_claim_device(iface
);
549 interface_handle_config_change(struct interface
*iface
)
551 enum interface_config_state state
= iface
->config_state
;
553 iface
->config_state
= IFC_NORMAL
;
558 interface_do_reload(iface
);
561 interface_do_free(iface
);
564 if (iface
->autostart
&& iface
->available
)
565 interface_set_up(iface
);
569 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
571 struct interface
*iface
= state
->iface
;
575 if (iface
->state
!= IFS_SETUP
) {
576 interface_event(iface
, IFEV_UPDATE
);
580 interface_ip_set_enabled(&iface
->config_ip
, true);
581 system_flush_routes();
582 iface
->state
= IFS_UP
;
583 iface
->start_time
= system_get_rtime();
584 interface_event(iface
, IFEV_UP
);
585 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
588 if (iface
->state
== IFS_DOWN
)
591 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
592 mark_interface_down(iface
);
593 if (iface
->main_dev
.dev
)
594 device_release(&iface
->main_dev
);
595 interface_handle_config_change(iface
);
597 case IFPEV_LINK_LOST
:
598 if (iface
->state
!= IFS_UP
)
601 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
602 mark_interface_down(iface
);
603 iface
->state
= IFS_SETUP
;
609 interface_write_resolv_conf();
612 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
615 iface
->proto
->free(iface
->proto
);
618 iface
->state
= IFS_DOWN
;
619 iface
->proto
= state
;
623 state
->proto_event
= interface_proto_cb
;
624 state
->iface
= iface
;
628 interface_alloc(const char *name
, struct blob_attr
*config
)
630 struct interface
*iface
;
631 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
632 struct blob_attr
*cur
;
633 const char *proto_name
= NULL
;
636 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
637 iface
->name
= strcpy(iface_name
, name
);
638 INIT_LIST_HEAD(&iface
->errors
);
639 INIT_LIST_HEAD(&iface
->users
);
640 INIT_LIST_HEAD(&iface
->hotplug_list
);
641 INIT_LIST_HEAD(&iface
->assignment_classes
);
642 interface_ip_init(iface
);
643 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
644 iface
->config_ip
.enabled
= false;
646 iface
->main_dev
.cb
= interface_cb
;
648 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
649 blob_data(config
), blob_len(config
));
651 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
652 proto_name
= blobmsg_data(cur
);
654 proto_attach_interface(iface
, proto_name
);
656 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
657 iface
->proto_ip
.no_defaultroute
=
658 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
659 iface
->proto_ip
.no_dns
=
660 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
662 if ((cur
= tb
[IFACE_ATTR_DNS
]))
663 interface_add_dns_server_list(&iface
->config_ip
, cur
);
665 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
666 interface_add_dns_search_list(&iface
->config_ip
, cur
);
668 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
669 iface
->metric
= blobmsg_get_u32(cur
);
671 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
672 iface
->assignment_length
= blobmsg_get_u32(cur
);
674 iface
->assignment_hint
= -1;
675 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
676 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
677 ~((1 << (64 - iface
->assignment_length
)) - 1);
679 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
680 interface_add_assignment_classes(iface
, cur
);
683 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
684 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
685 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
688 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
689 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
690 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
693 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
695 iface
->config_autostart
= iface
->autostart
;
699 void interface_set_dynamic(struct interface
*iface
)
701 iface
->dynamic
= true;
702 iface
->node
.version
= -1; // Don't delete on reload
705 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
707 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
708 struct blob_attr
*cur
;
710 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
711 blob_data(config
), blob_len(config
));
714 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
715 iface
->parent_ifname
= blobmsg_data(cur
);
717 if (!iface
->parent_ifname
)
720 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
721 iface
->ifname
= blobmsg_data(cur
);
725 iface
->config
= config
;
726 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
731 interface_add(struct interface
*iface
, struct blob_attr
*config
)
733 __interface_add(iface
, config
, false);
737 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
739 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
742 return __interface_add(iface
, config
, true);
746 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
748 bool enabled
= iface
->config_ip
.enabled
;
749 bool claimed
= iface
->l3_dev
.claimed
;
751 if (iface
->l3_dev
.dev
== dev
)
754 interface_ip_set_enabled(&iface
->config_ip
, false);
755 interface_ip_flush(&iface
->proto_ip
);
756 device_add_user(&iface
->l3_dev
, dev
);
760 device_claim(&iface
->l3_dev
);
761 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
766 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
768 bool set_l3
= (!dev
|| iface
->main_dev
.dev
== iface
->l3_dev
.dev
);
769 bool claimed
= iface
->l3_dev
.claimed
;
771 if (iface
->main_dev
.dev
== dev
)
775 interface_set_l3_dev(iface
, dev
);
777 device_add_user(&iface
->main_dev
, dev
);
779 interface_set_link_state(iface
, false);
784 device_claim(&iface
->l3_dev
);
786 if (!iface
->l3_dev
.dev
)
787 interface_set_l3_dev(iface
, dev
);
791 interface_remove_link(struct interface
*iface
, struct device
*dev
)
793 struct device
*mdev
= iface
->main_dev
.dev
;
795 if (mdev
&& mdev
->hotplug_ops
)
796 return mdev
->hotplug_ops
->del(mdev
, dev
);
798 if (!iface
->main_dev
.hotplug
)
799 return UBUS_STATUS_INVALID_ARGUMENT
;
801 if (dev
!= iface
->main_dev
.dev
)
802 return UBUS_STATUS_INVALID_ARGUMENT
;
804 device_remove_user(&iface
->main_dev
);
809 interface_add_link(struct interface
*iface
, struct device
*dev
)
811 struct device
*mdev
= iface
->main_dev
.dev
;
816 if (iface
->main_dev
.hotplug
)
817 device_remove_user(&iface
->main_dev
);
820 if (mdev
->hotplug_ops
)
821 return mdev
->hotplug_ops
->add(mdev
, dev
);
823 return UBUS_STATUS_NOT_SUPPORTED
;
826 interface_set_main_dev(iface
, dev
);
827 iface
->main_dev
.hotplug
= true;
832 interface_handle_link(struct interface
*iface
, const char *name
, bool add
)
839 dev
= device_get(name
, add
? 2 : 0);
841 ret
= UBUS_STATUS_NOT_FOUND
;
846 device_set_present(dev
, true);
847 if (iface
->device_config
)
848 device_set_config(dev
, &simple_device_type
, iface
->config
);
850 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
851 ret
= interface_add_link(iface
, dev
);
853 ret
= interface_remove_link(iface
, dev
);
863 interface_set_up(struct interface
*iface
)
867 iface
->autostart
= true;
869 if (iface
->state
!= IFS_DOWN
)
872 interface_clear_errors(iface
);
873 if (!iface
->available
) {
874 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
878 if (iface
->main_dev
.dev
) {
879 ret
= device_claim(&iface
->main_dev
);
881 interface_check_state(iface
);
884 ret
= __interface_set_up(iface
);
890 interface_set_down(struct interface
*iface
)
893 vlist_for_each_element(&interfaces
, iface
, node
)
894 __interface_set_down(iface
, false);
896 iface
->autostart
= false;
897 __interface_set_down(iface
, false);
904 interface_start_pending(void)
906 struct interface
*iface
;
908 vlist_for_each_element(&interfaces
, iface
, node
) {
909 if (iface
->available
&& iface
->autostart
)
910 interface_set_up(iface
);
915 set_config_state(struct interface
*iface
, enum interface_config_state s
)
917 iface
->config_state
= s
;
918 if (iface
->state
== IFS_DOWN
)
919 interface_handle_config_change(iface
);
921 __interface_set_down(iface
, false);
925 interface_update_start(struct interface
*iface
)
928 interface_ip_update_start(&iface
->proto_ip
);
932 interface_update_complete(struct interface
*iface
)
934 interface_ip_update_complete(&iface
->proto_ip
);
938 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
940 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
941 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
945 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
947 struct blob_attr
*old_config
= if_old
->config
;
948 bool reload
= false, reload_ip
= false;
950 #define FIELD_CHANGED_STR(field) \
951 ((!!if_old->field != !!if_new->field) || \
953 strcmp(if_old->field, if_new->field) != 0))
955 if (FIELD_CHANGED_STR(parent_ifname
)) {
956 if (if_old
->parent_iface
.iface
)
957 interface_remove_user(&if_old
->parent_iface
);
961 if (FIELD_CHANGED_STR(ifname
) ||
962 if_old
->proto_handler
!= if_new
->proto_handler
)
965 if (!if_old
->proto_handler
->config_params
)
966 D(INTERFACE
, "No config parameters for interface '%s'\n",
968 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
969 if_old
->proto_handler
->config_params
))
972 #define UPDATE(field, __var) ({ \
973 bool __changed = (if_old->field != if_new->field); \
974 if_old->field = if_new->field; \
975 __var |= __changed; \
978 if_old
->config
= if_new
->config
;
979 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
980 if_old
->autostart
= true;
982 if_old
->device_config
= if_new
->device_config
;
983 if_old
->config_autostart
= if_new
->config_autostart
;
984 if_old
->ifname
= if_new
->ifname
;
985 if_old
->parent_ifname
= if_new
->parent_ifname
;
986 if_old
->proto_handler
= if_new
->proto_handler
;
988 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
989 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
991 UPDATE(metric
, reload_ip
);
992 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
993 UPDATE(ip4table
, reload_ip
);
994 UPDATE(ip6table
, reload_ip
);
995 interface_merge_assignment_data(if_old
, if_new
);
1000 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1002 interface_clear_errors(if_old
);
1003 set_config_state(if_old
, IFC_RELOAD
);
1008 interface_ip_set_enabled(&if_old
->config_ip
, false);
1009 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1010 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
1011 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
1014 interface_write_resolv_conf();
1017 if_new
->config
= NULL
;
1018 interface_cleanup(if_new
);
1024 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1025 struct vlist_node
*node_old
)
1027 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1028 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1030 if (node_old
&& node_new
) {
1031 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1032 interface_change_config(if_old
, if_new
);
1033 } else if (node_old
) {
1034 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1035 set_config_state(if_old
, IFC_REMOVE
);
1036 } else if (node_new
) {
1037 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1038 proto_init_interface(if_new
, if_new
->config
);
1039 interface_claim_device(if_new
);
1040 netifd_ubus_add_interface(if_new
);
1046 interface_init_list(void)
1048 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1049 interfaces
.keep_old
= true;
1050 interfaces
.no_delete
= true;