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 if (state
== IFS_DOWN
)
206 iface
->state
= IFS_DOWN
;
208 interface_event(iface
, IFEV_DOWN
);
209 interface_ip_set_enabled(&iface
->config_ip
, false);
210 interface_ip_flush(&iface
->proto_ip
);
211 interface_flush_state(iface
);
212 system_flush_routes();
216 __interface_set_down(struct interface
*iface
, bool force
)
218 switch (iface
->state
) {
220 interface_event(iface
, IFEV_DOWN
);
222 iface
->state
= IFS_TEARDOWN
;
223 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
225 interface_flush_state(iface
);
228 vlist_delete(&interfaces
, &iface
->node
);
232 if (iface
->main_dev
.dev
)
233 device_release(&iface
->main_dev
);
241 __interface_set_up(struct interface
*iface
)
245 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
247 iface
->state
= IFS_SETUP
;
248 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
250 mark_interface_down(iface
);
256 interface_check_state(struct interface
*iface
)
258 switch (iface
->state
) {
260 if (!iface
->enabled
|| !iface
->link_state
) {
261 mark_interface_down(iface
);
262 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
266 if (iface
->enabled
&& iface
->link_state
&& !config_init
)
267 __interface_set_up(iface
);
275 interface_set_enabled(struct interface
*iface
, bool new_state
)
277 if (iface
->enabled
== new_state
)
280 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
281 iface
->enabled
= new_state
;
282 interface_check_state(iface
);
286 interface_set_link_state(struct interface
*iface
, bool new_state
)
288 if (iface
->link_state
== new_state
)
291 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
292 iface
->link_state
= new_state
;
293 interface_check_state(iface
);
297 interface_cb(struct device_user
*dep
, enum device_event ev
)
299 struct interface
*iface
;
300 bool new_state
= false;
302 iface
= container_of(dep
, struct interface
, main_dev
);
306 case DEV_EVENT_REMOVE
:
307 interface_set_available(iface
, new_state
);
308 if (!new_state
&& dep
->dev
->external
)
309 interface_set_main_dev(iface
, NULL
);
314 interface_set_enabled(iface
, new_state
);
316 case DEV_EVENT_LINK_UP
:
318 case DEV_EVENT_LINK_DOWN
:
319 interface_set_link_state(iface
, new_state
);
327 interface_set_available(struct interface
*iface
, bool new_state
)
329 if (iface
->available
== new_state
)
332 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
333 iface
->available
= new_state
;
336 if (iface
->autostart
&& !config_init
)
337 interface_set_up(iface
);
339 __interface_set_down(iface
, true);
343 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
346 list_add(&dep
->list
, &iface_all_users
);
351 list_add(&dep
->list
, &iface
->users
);
352 if (iface
->state
== IFS_UP
)
353 dep
->cb(dep
, iface
, IFEV_UP
);
357 interface_remove_user(struct interface_user
*dep
)
359 list_del_init(&dep
->list
);
364 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
366 struct blob_attr
*cur
;
369 blobmsg_for_each_attr(cur
, list
, rem
) {
370 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
373 if (!blobmsg_check_attr(cur
, NULL
))
376 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
377 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
378 list_add(&c
->head
, &iface
->assignment_classes
);
383 interface_clear_assignment_classes(struct interface
*iface
)
385 while (!list_empty(&iface
->assignment_classes
)) {
386 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
387 struct interface_assignment_class
, head
);
394 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
396 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
397 old
->assignment_length
!= new->assignment_length
||
398 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
400 struct interface_assignment_class
*c
;
401 list_for_each_entry(c
, &new->assignment_classes
, head
) {
402 // Compare list entries one-by-one to see if there was a change
403 if (list_empty(&old
->assignment_classes
)) // The new list is longer
409 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
410 struct interface_assignment_class
, head
);
412 if (strcmp(c_old
->name
, c
->name
)) // An entry didn't match
415 list_del(&c_old
->head
);
419 // The old list was longer than the new one or the last entry didn't match
420 if (!list_empty(&old
->assignment_classes
)) {
421 interface_clear_assignment_classes(old
);
425 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
428 old
->assignment_hint
= new->assignment_hint
;
429 old
->assignment_length
= new->assignment_length
;
430 interface_refresh_assignments(true);
435 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
437 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
438 struct device
*dev
= iface
->l3_dev
.dev
;
445 interface_set_main_dev(alias
, dev
);
446 interface_set_available(alias
, true);
449 interface_set_available(alias
, false);
450 interface_set_main_dev(alias
, NULL
);
453 interface_remove_user(dep
);
462 interface_claim_device(struct interface
*iface
)
464 struct interface
*parent
;
465 struct device
*dev
= NULL
;
467 if (iface
->parent_iface
.iface
)
468 interface_remove_user(&iface
->parent_iface
);
470 if (iface
->parent_ifname
) {
471 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
472 iface
->parent_iface
.cb
= interface_alias_cb
;
473 interface_add_user(&iface
->parent_iface
, parent
);
474 } else if (iface
->ifname
&&
475 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
476 dev
= device_get(iface
->ifname
, true);
480 interface_set_main_dev(iface
, dev
);
482 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
483 interface_set_available(iface
, true);
487 interface_cleanup_state(struct interface
*iface
)
489 interface_set_available(iface
, false);
491 interface_flush_state(iface
);
492 interface_clear_errors(iface
);
493 interface_set_proto_state(iface
, NULL
);
495 if (iface
->main_dev
.dev
)
496 interface_set_main_dev(iface
, NULL
);
500 interface_cleanup(struct interface
*iface
)
502 struct interface_user
*dep
, *tmp
;
504 if (iface
->parent_iface
.iface
)
505 interface_remove_user(&iface
->parent_iface
);
507 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
508 interface_remove_user(dep
);
510 interface_clear_assignment_classes(iface
);
511 interface_ip_flush(&iface
->config_ip
);
512 interface_cleanup_state(iface
);
516 interface_do_free(struct interface
*iface
)
518 interface_event(iface
, IFEV_FREE
);
519 interface_cleanup(iface
);
521 netifd_ubus_remove_interface(iface
);
522 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
527 interface_do_reload(struct interface
*iface
)
529 interface_event(iface
, IFEV_RELOAD
);
530 interface_cleanup_state(iface
);
531 proto_init_interface(iface
, iface
->config
);
532 interface_claim_device(iface
);
536 interface_handle_config_change(struct interface
*iface
)
538 enum interface_config_state state
= iface
->config_state
;
540 iface
->config_state
= IFC_NORMAL
;
545 interface_do_reload(iface
);
548 interface_do_free(iface
);
551 if (iface
->autostart
&& iface
->available
)
552 interface_set_up(iface
);
556 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
558 struct interface
*iface
= state
->iface
;
562 if (iface
->state
!= IFS_SETUP
) {
563 interface_event(iface
, IFEV_UPDATE
);
567 interface_ip_set_enabled(&iface
->config_ip
, true);
568 system_flush_routes();
569 iface
->state
= IFS_UP
;
570 iface
->start_time
= system_get_rtime();
571 interface_event(iface
, IFEV_UP
);
572 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
575 if (iface
->state
== IFS_DOWN
)
578 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
579 mark_interface_down(iface
);
580 if (iface
->main_dev
.dev
)
581 device_release(&iface
->main_dev
);
582 interface_handle_config_change(iface
);
584 case IFPEV_LINK_LOST
:
585 if (iface
->state
!= IFS_UP
)
588 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
589 mark_interface_down(iface
);
590 iface
->state
= IFS_SETUP
;
594 interface_write_resolv_conf();
597 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
600 iface
->proto
->free(iface
->proto
);
603 iface
->state
= IFS_DOWN
;
604 iface
->proto
= state
;
608 state
->proto_event
= interface_proto_cb
;
609 state
->iface
= iface
;
613 interface_alloc(const char *name
, struct blob_attr
*config
)
615 struct interface
*iface
;
616 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
617 struct blob_attr
*cur
;
618 const char *proto_name
= NULL
;
621 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
622 iface
->name
= strcpy(iface_name
, name
);
623 INIT_LIST_HEAD(&iface
->errors
);
624 INIT_LIST_HEAD(&iface
->users
);
625 INIT_LIST_HEAD(&iface
->hotplug_list
);
626 INIT_LIST_HEAD(&iface
->assignment_classes
);
627 interface_ip_init(iface
);
628 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
629 iface
->config_ip
.enabled
= false;
631 iface
->main_dev
.cb
= interface_cb
;
633 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
634 blob_data(config
), blob_len(config
));
636 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
637 proto_name
= blobmsg_data(cur
);
639 proto_attach_interface(iface
, proto_name
);
641 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
642 iface
->proto_ip
.no_defaultroute
=
643 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
644 iface
->proto_ip
.no_dns
=
645 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
647 if ((cur
= tb
[IFACE_ATTR_DNS
]))
648 interface_add_dns_server_list(&iface
->config_ip
, cur
);
650 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
651 interface_add_dns_search_list(&iface
->config_ip
, cur
);
653 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
654 iface
->metric
= blobmsg_get_u32(cur
);
656 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
657 iface
->assignment_length
= blobmsg_get_u32(cur
);
659 iface
->assignment_hint
= -1;
660 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
661 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
662 ~((1 << (64 - iface
->assignment_length
)) - 1);
664 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
665 interface_add_assignment_classes(iface
, cur
);
668 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
669 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
670 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
673 // Set a default exteranl routing table for IPv6 to do source-based-filtering
674 struct interface
*iface_old
= vlist_find(&interfaces
, name
, iface_old
, node
);
675 if (iface_old
&& iface_old
->ip6table
> 1000 && iface_old
->ip6table
< 2000)
676 iface
->ip6table
= iface_old
->ip6table
;
678 iface
->ip6table
= 1000 + ++interface_serial
;
680 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
681 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
682 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
685 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
687 iface
->config_autostart
= iface
->autostart
;
691 void interface_set_dynamic(struct interface
*iface
)
693 iface
->dynamic
= true;
694 iface
->node
.version
= -1; // Don't delete on reload
697 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
699 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
700 struct blob_attr
*cur
;
702 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
703 blob_data(config
), blob_len(config
));
706 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
707 iface
->parent_ifname
= blobmsg_data(cur
);
709 if (!iface
->parent_ifname
)
712 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
713 iface
->ifname
= blobmsg_data(cur
);
717 iface
->config
= config
;
718 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
723 interface_add(struct interface
*iface
, struct blob_attr
*config
)
725 __interface_add(iface
, config
, false);
729 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
731 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
734 return __interface_add(iface
, config
, true);
738 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
740 bool enabled
= iface
->config_ip
.enabled
;
741 bool claimed
= iface
->l3_dev
.claimed
;
743 if (iface
->l3_dev
.dev
== dev
)
746 interface_ip_set_enabled(&iface
->config_ip
, false);
747 interface_ip_flush(&iface
->proto_ip
);
748 device_add_user(&iface
->l3_dev
, dev
);
752 device_claim(&iface
->l3_dev
);
753 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
758 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
760 bool set_l3
= (!dev
|| iface
->main_dev
.dev
== iface
->l3_dev
.dev
);
761 bool claimed
= iface
->l3_dev
.claimed
;
763 if (iface
->main_dev
.dev
== dev
)
767 interface_set_l3_dev(iface
, dev
);
769 device_add_user(&iface
->main_dev
, dev
);
771 interface_set_link_state(iface
, false);
776 device_claim(&iface
->l3_dev
);
778 if (!iface
->l3_dev
.dev
)
779 interface_set_l3_dev(iface
, dev
);
783 interface_remove_link(struct interface
*iface
, struct device
*dev
)
785 struct device
*mdev
= iface
->main_dev
.dev
;
787 if (mdev
&& mdev
->hotplug_ops
)
788 return mdev
->hotplug_ops
->del(mdev
, dev
);
790 if (!iface
->main_dev
.hotplug
)
791 return UBUS_STATUS_INVALID_ARGUMENT
;
793 if (dev
!= iface
->main_dev
.dev
)
794 return UBUS_STATUS_INVALID_ARGUMENT
;
796 device_remove_user(&iface
->main_dev
);
801 interface_add_link(struct interface
*iface
, struct device
*dev
)
803 struct device
*mdev
= iface
->main_dev
.dev
;
808 if (iface
->main_dev
.hotplug
)
809 device_remove_user(&iface
->main_dev
);
812 if (mdev
->hotplug_ops
)
813 return mdev
->hotplug_ops
->add(mdev
, dev
);
815 return UBUS_STATUS_NOT_SUPPORTED
;
818 interface_set_main_dev(iface
, dev
);
819 iface
->main_dev
.hotplug
= true;
824 interface_handle_link(struct interface
*iface
, const char *name
, bool add
)
831 dev
= device_get(name
, add
? 2 : 0);
833 ret
= UBUS_STATUS_NOT_FOUND
;
838 device_set_present(dev
, true);
839 if (iface
->device_config
)
840 device_set_config(dev
, &simple_device_type
, iface
->config
);
842 system_if_apply_settings(dev
, &dev
->settings
);
843 ret
= interface_add_link(iface
, dev
);
845 ret
= interface_remove_link(iface
, dev
);
855 interface_set_up(struct interface
*iface
)
859 iface
->autostart
= true;
861 if (iface
->state
!= IFS_DOWN
)
864 interface_clear_errors(iface
);
865 if (!iface
->available
) {
866 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
870 if (iface
->main_dev
.dev
) {
871 ret
= device_claim(&iface
->main_dev
);
873 interface_check_state(iface
);
876 ret
= __interface_set_up(iface
);
882 interface_set_down(struct interface
*iface
)
885 vlist_for_each_element(&interfaces
, iface
, node
)
886 __interface_set_down(iface
, false);
888 iface
->autostart
= false;
889 __interface_set_down(iface
, false);
896 interface_start_pending(void)
898 struct interface
*iface
;
900 vlist_for_each_element(&interfaces
, iface
, node
) {
901 if (iface
->available
&& iface
->autostart
)
902 interface_set_up(iface
);
907 set_config_state(struct interface
*iface
, enum interface_config_state s
)
909 iface
->config_state
= s
;
910 if (iface
->state
== IFS_DOWN
)
911 interface_handle_config_change(iface
);
913 __interface_set_down(iface
, false);
917 interface_update_start(struct interface
*iface
)
919 interface_ip_update_start(&iface
->proto_ip
);
923 interface_update_complete(struct interface
*iface
)
925 interface_ip_update_complete(&iface
->proto_ip
);
929 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
931 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
932 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
936 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
938 struct blob_attr
*old_config
= if_old
->config
;
939 bool reload
= false, reload_ip
= false;
941 #define FIELD_CHANGED_STR(field) \
942 ((!!if_old->field != !!if_new->field) || \
944 strcmp(if_old->field, if_new->field) != 0))
946 if (FIELD_CHANGED_STR(parent_ifname
)) {
947 if (if_old
->parent_iface
.iface
)
948 interface_remove_user(&if_old
->parent_iface
);
952 if (FIELD_CHANGED_STR(ifname
) ||
953 if_old
->proto_handler
!= if_new
->proto_handler
)
956 if (!if_old
->proto_handler
->config_params
)
957 D(INTERFACE
, "No config parameters for interface '%s'\n",
959 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
960 if_old
->proto_handler
->config_params
))
963 #define UPDATE(field, __var) ({ \
964 bool __changed = (if_old->field != if_new->field); \
965 if_old->field = if_new->field; \
966 __var |= __changed; \
969 if_old
->config
= if_new
->config
;
970 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
971 if_old
->autostart
= true;
973 if_old
->device_config
= if_new
->device_config
;
974 if_old
->config_autostart
= if_new
->config_autostart
;
975 if_old
->ifname
= if_new
->ifname
;
976 if_old
->parent_ifname
= if_new
->parent_ifname
;
977 if_old
->proto_handler
= if_new
->proto_handler
;
979 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
980 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
982 UPDATE(metric
, reload_ip
);
983 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
984 UPDATE(ip4table
, reload_ip
);
985 UPDATE(ip6table
, reload_ip
);
986 interface_merge_assignment_data(if_old
, if_new
);
991 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
993 interface_clear_errors(if_old
);
994 set_config_state(if_old
, IFC_RELOAD
);
999 interface_ip_set_enabled(&if_old
->config_ip
, false);
1000 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1001 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
1002 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
1005 interface_write_resolv_conf();
1008 if_new
->config
= NULL
;
1009 interface_cleanup(if_new
);
1015 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1016 struct vlist_node
*node_old
)
1018 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1019 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1021 if (node_old
&& node_new
) {
1022 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1023 interface_change_config(if_old
, if_new
);
1024 } else if (node_old
) {
1025 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1026 set_config_state(if_old
, IFC_REMOVE
);
1027 } else if (node_new
) {
1028 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1029 proto_init_interface(if_new
, if_new
->config
);
1030 interface_claim_device(if_new
);
1031 netifd_ubus_add_interface(if_new
);
1037 interface_init_list(void)
1039 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1040 interfaces
.keep_old
= true;
1041 interfaces
.no_delete
= true;