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 switch (iface
->state
) {
227 interface_event(iface
, IFEV_DOWN
);
229 iface
->state
= IFS_TEARDOWN
;
230 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
232 interface_flush_state(iface
);
235 vlist_delete(&interfaces
, &iface
->node
);
239 if (iface
->main_dev
.dev
)
240 device_release(&iface
->main_dev
);
248 __interface_set_up(struct interface
*iface
)
252 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
254 iface
->state
= IFS_SETUP
;
255 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
257 mark_interface_down(iface
);
263 interface_check_state(struct interface
*iface
)
265 switch (iface
->state
) {
267 if (!iface
->enabled
|| !iface
->link_state
) {
268 mark_interface_down(iface
);
269 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
273 if (iface
->enabled
&& iface
->link_state
&& !config_init
)
274 __interface_set_up(iface
);
282 interface_set_enabled(struct interface
*iface
, bool new_state
)
284 if (iface
->enabled
== new_state
)
287 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
288 iface
->enabled
= new_state
;
289 interface_check_state(iface
);
293 interface_set_link_state(struct interface
*iface
, bool new_state
)
295 if (iface
->link_state
== new_state
)
298 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
299 iface
->link_state
= new_state
;
300 interface_check_state(iface
);
304 interface_cb(struct device_user
*dep
, enum device_event ev
)
306 struct interface
*iface
;
307 bool new_state
= false;
309 iface
= container_of(dep
, struct interface
, main_dev
);
313 case DEV_EVENT_REMOVE
:
314 interface_set_available(iface
, new_state
);
315 if (!new_state
&& dep
->dev
->external
)
316 interface_set_main_dev(iface
, NULL
);
321 interface_set_enabled(iface
, new_state
);
323 case DEV_EVENT_LINK_UP
:
325 case DEV_EVENT_LINK_DOWN
:
326 interface_set_link_state(iface
, new_state
);
334 interface_set_available(struct interface
*iface
, bool new_state
)
336 if (iface
->available
== new_state
)
339 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
340 iface
->available
= new_state
;
343 if (iface
->autostart
&& !config_init
)
344 interface_set_up(iface
);
346 __interface_set_down(iface
, true);
350 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
353 list_add(&dep
->list
, &iface_all_users
);
358 list_add(&dep
->list
, &iface
->users
);
359 if (iface
->state
== IFS_UP
)
360 dep
->cb(dep
, iface
, IFEV_UP
);
364 interface_remove_user(struct interface_user
*dep
)
366 list_del_init(&dep
->list
);
371 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
373 struct blob_attr
*cur
;
376 blobmsg_for_each_attr(cur
, list
, rem
) {
377 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
380 if (!blobmsg_check_attr(cur
, NULL
))
383 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
384 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
385 list_add(&c
->head
, &iface
->assignment_classes
);
390 interface_clear_assignment_classes(struct interface
*iface
)
392 while (!list_empty(&iface
->assignment_classes
)) {
393 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
394 struct interface_assignment_class
, head
);
401 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
403 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
404 old
->assignment_length
!= new->assignment_length
||
405 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
407 struct interface_assignment_class
*c
;
408 list_for_each_entry(c
, &new->assignment_classes
, head
) {
409 // Compare list entries one-by-one to see if there was a change
410 if (list_empty(&old
->assignment_classes
)) // The new list is longer
416 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
417 struct interface_assignment_class
, head
);
419 if (strcmp(c_old
->name
, c
->name
)) // An entry didn't match
422 list_del(&c_old
->head
);
426 // The old list was longer than the new one or the last entry didn't match
427 if (!list_empty(&old
->assignment_classes
)) {
428 interface_clear_assignment_classes(old
);
432 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
435 old
->assignment_hint
= new->assignment_hint
;
436 old
->assignment_length
= new->assignment_length
;
437 interface_refresh_assignments(true);
442 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
444 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
445 struct device
*dev
= iface
->l3_dev
.dev
;
452 interface_set_main_dev(alias
, dev
);
453 interface_set_available(alias
, true);
456 interface_set_available(alias
, false);
457 interface_set_main_dev(alias
, NULL
);
460 interface_remove_user(dep
);
469 interface_claim_device(struct interface
*iface
)
471 struct interface
*parent
;
472 struct device
*dev
= NULL
;
474 if (iface
->parent_iface
.iface
)
475 interface_remove_user(&iface
->parent_iface
);
477 if (iface
->parent_ifname
) {
478 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
479 iface
->parent_iface
.cb
= interface_alias_cb
;
480 interface_add_user(&iface
->parent_iface
, parent
);
481 } else if (iface
->ifname
&&
482 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
483 dev
= device_get(iface
->ifname
, true);
487 interface_set_main_dev(iface
, dev
);
489 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
490 interface_set_available(iface
, true);
494 interface_cleanup_state(struct interface
*iface
)
496 interface_set_available(iface
, false);
498 interface_flush_state(iface
);
499 interface_clear_errors(iface
);
500 interface_set_proto_state(iface
, NULL
);
502 if (iface
->main_dev
.dev
)
503 interface_set_main_dev(iface
, NULL
);
507 interface_cleanup(struct interface
*iface
)
509 struct interface_user
*dep
, *tmp
;
511 if (iface
->parent_iface
.iface
)
512 interface_remove_user(&iface
->parent_iface
);
514 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
515 interface_remove_user(dep
);
517 interface_clear_assignment_classes(iface
);
518 interface_ip_flush(&iface
->config_ip
);
519 interface_cleanup_state(iface
);
523 interface_do_free(struct interface
*iface
)
525 interface_event(iface
, IFEV_FREE
);
526 interface_cleanup(iface
);
528 netifd_ubus_remove_interface(iface
);
529 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
534 interface_do_reload(struct interface
*iface
)
536 interface_event(iface
, IFEV_RELOAD
);
537 interface_cleanup_state(iface
);
538 proto_init_interface(iface
, iface
->config
);
539 interface_claim_device(iface
);
543 interface_handle_config_change(struct interface
*iface
)
545 enum interface_config_state state
= iface
->config_state
;
547 iface
->config_state
= IFC_NORMAL
;
552 interface_do_reload(iface
);
555 interface_do_free(iface
);
558 if (iface
->autostart
&& iface
->available
)
559 interface_set_up(iface
);
563 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
565 struct interface
*iface
= state
->iface
;
569 if (iface
->state
!= IFS_SETUP
) {
570 interface_event(iface
, IFEV_UPDATE
);
574 interface_ip_set_enabled(&iface
->config_ip
, true);
575 system_flush_routes();
576 iface
->state
= IFS_UP
;
577 iface
->start_time
= system_get_rtime();
578 interface_event(iface
, IFEV_UP
);
579 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
582 if (iface
->state
== IFS_DOWN
)
585 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
586 mark_interface_down(iface
);
587 if (iface
->main_dev
.dev
)
588 device_release(&iface
->main_dev
);
589 interface_handle_config_change(iface
);
591 case IFPEV_LINK_LOST
:
592 if (iface
->state
!= IFS_UP
)
595 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
596 mark_interface_down(iface
);
597 iface
->state
= IFS_SETUP
;
601 interface_write_resolv_conf();
604 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
607 iface
->proto
->free(iface
->proto
);
610 iface
->state
= IFS_DOWN
;
611 iface
->proto
= state
;
615 state
->proto_event
= interface_proto_cb
;
616 state
->iface
= iface
;
620 interface_alloc(const char *name
, struct blob_attr
*config
)
622 struct interface
*iface
;
623 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
624 struct blob_attr
*cur
;
625 const char *proto_name
= NULL
;
628 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
629 iface
->name
= strcpy(iface_name
, name
);
630 INIT_LIST_HEAD(&iface
->errors
);
631 INIT_LIST_HEAD(&iface
->users
);
632 INIT_LIST_HEAD(&iface
->hotplug_list
);
633 INIT_LIST_HEAD(&iface
->assignment_classes
);
634 interface_ip_init(iface
);
635 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
636 iface
->config_ip
.enabled
= false;
638 iface
->main_dev
.cb
= interface_cb
;
640 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
641 blob_data(config
), blob_len(config
));
643 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
644 proto_name
= blobmsg_data(cur
);
646 proto_attach_interface(iface
, proto_name
);
648 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
649 iface
->proto_ip
.no_defaultroute
=
650 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
651 iface
->proto_ip
.no_dns
=
652 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
654 if ((cur
= tb
[IFACE_ATTR_DNS
]))
655 interface_add_dns_server_list(&iface
->config_ip
, cur
);
657 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
658 interface_add_dns_search_list(&iface
->config_ip
, cur
);
660 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
661 iface
->metric
= blobmsg_get_u32(cur
);
663 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
664 iface
->assignment_length
= blobmsg_get_u32(cur
);
666 iface
->assignment_hint
= -1;
667 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
668 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
669 ~((1 << (64 - iface
->assignment_length
)) - 1);
671 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
672 interface_add_assignment_classes(iface
, cur
);
675 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
676 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
677 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
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
, dev
->settings
.flags
);
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
)
920 interface_ip_update_start(&iface
->proto_ip
);
924 interface_update_complete(struct interface
*iface
)
926 interface_ip_update_complete(&iface
->proto_ip
);
930 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
932 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
933 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
937 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
939 struct blob_attr
*old_config
= if_old
->config
;
940 bool reload
= false, reload_ip
= false;
942 #define FIELD_CHANGED_STR(field) \
943 ((!!if_old->field != !!if_new->field) || \
945 strcmp(if_old->field, if_new->field) != 0))
947 if (FIELD_CHANGED_STR(parent_ifname
)) {
948 if (if_old
->parent_iface
.iface
)
949 interface_remove_user(&if_old
->parent_iface
);
953 if (FIELD_CHANGED_STR(ifname
) ||
954 if_old
->proto_handler
!= if_new
->proto_handler
)
957 if (!if_old
->proto_handler
->config_params
)
958 D(INTERFACE
, "No config parameters for interface '%s'\n",
960 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
961 if_old
->proto_handler
->config_params
))
964 #define UPDATE(field, __var) ({ \
965 bool __changed = (if_old->field != if_new->field); \
966 if_old->field = if_new->field; \
967 __var |= __changed; \
970 if_old
->config
= if_new
->config
;
971 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
972 if_old
->autostart
= true;
974 if_old
->device_config
= if_new
->device_config
;
975 if_old
->config_autostart
= if_new
->config_autostart
;
976 if_old
->ifname
= if_new
->ifname
;
977 if_old
->parent_ifname
= if_new
->parent_ifname
;
978 if_old
->proto_handler
= if_new
->proto_handler
;
980 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
981 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
983 UPDATE(metric
, reload_ip
);
984 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
985 UPDATE(ip4table
, reload_ip
);
986 UPDATE(ip6table
, reload_ip
);
987 interface_merge_assignment_data(if_old
, if_new
);
992 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
994 interface_clear_errors(if_old
);
995 set_config_state(if_old
, IFC_RELOAD
);
1000 interface_ip_set_enabled(&if_old
->config_ip
, false);
1001 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1002 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
1003 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
1006 interface_write_resolv_conf();
1009 if_new
->config
= NULL
;
1010 interface_cleanup(if_new
);
1016 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1017 struct vlist_node
*node_old
)
1019 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1020 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1022 if (node_old
&& node_new
) {
1023 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1024 interface_change_config(if_old
, if_new
);
1025 } else if (node_old
) {
1026 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1027 set_config_state(if_old
, IFC_REMOVE
);
1028 } else if (node_new
) {
1029 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1030 proto_init_interface(if_new
, if_new
->config
);
1031 interface_claim_device(if_new
);
1032 netifd_ubus_add_interface(if_new
);
1038 interface_init_list(void)
1040 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1041 interfaces
.keep_old
= true;
1042 interfaces
.no_delete
= true;