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
,
46 IFACE_ATTR_FORCE_LINK
,
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
},
66 [IFACE_ATTR_FORCE_LINK
] = { .name
= "force_link", .type
= BLOBMSG_TYPE_BOOL
},
69 static const struct uci_blob_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
70 [IFACE_ATTR_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
71 [IFACE_ATTR_IP6CLASS
] = { .type
= BLOBMSG_TYPE_STRING
},
74 const struct uci_blob_param_list interface_attr_list
= {
75 .n_params
= IFACE_ATTR_MAX
,
76 .params
= iface_attrs
,
77 .info
= iface_attr_info
,
81 interface_clear_errors(struct interface
*iface
)
83 struct interface_error
*error
, *tmp
;
85 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
86 list_del(&error
->list
);
91 void interface_add_error(struct interface
*iface
, const char *subsystem
,
92 const char *code
, const char **data
, int n_data
)
94 struct interface_error
*error
;
97 char *dest
, *d_subsys
, *d_code
;
100 len
= n_data
* sizeof(char *);
101 datalen
= alloca(len
);
102 for (i
= 0; i
< n_data
; i
++) {
103 datalen
[i
] = strlen(data
[i
]) + 1;
108 error
= calloc_a(sizeof(*error
) + sizeof(char *) + len
,
109 &d_subsys
, subsystem
? strlen(subsystem
) + 1 : 0,
110 &d_code
, code
? strlen(code
) + 1 : 0);
114 list_add_tail(&error
->list
, &iface
->errors
);
116 dest
= (char *) &error
->data
[n_data
+ 1];
117 for (i
= 0; i
< n_data
; i
++) {
118 error
->data
[i
] = dest
;
119 memcpy(dest
, data
[i
], datalen
[i
]);
122 error
->data
[n_data
++] = NULL
;
125 error
->subsystem
= strcpy(d_subsys
, subsystem
);
128 error
->code
= strcpy(d_code
, code
);
132 interface_data_del(struct interface
*iface
, struct interface_data
*data
)
134 avl_delete(&iface
->data
, &data
->node
);
139 interface_data_flush(struct interface
*iface
)
141 struct interface_data
*d
, *tmp
;
143 avl_for_each_element_safe(&iface
->data
, d
, node
, tmp
)
144 interface_data_del(iface
, d
);
148 interface_add_data(struct interface
*iface
, const struct blob_attr
*data
)
150 struct interface_data
*n
, *o
;
152 if (!blobmsg_check_attr(data
, true))
153 return UBUS_STATUS_INVALID_ARGUMENT
;
155 const char *name
= blobmsg_name(data
);
156 unsigned len
= blob_pad_len(data
);
158 o
= avl_find_element(&iface
->data
, name
, o
, node
);
160 if (blob_pad_len(o
->data
) == len
&& !memcmp(o
->data
, data
, len
))
163 interface_data_del(iface
, o
);
166 n
= calloc(1, sizeof(*n
) + len
);
167 memcpy(n
->data
, data
, len
);
168 n
->node
.key
= blobmsg_name(n
->data
);
169 avl_insert(&iface
->data
, &n
->node
);
171 iface
->updated
|= IUF_DATA
;
176 interface_event(struct interface
*iface
, enum interface_event ev
)
178 struct interface_user
*dep
, *tmp
;
179 struct device
*adev
= NULL
;
181 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
182 dep
->cb(dep
, iface
, ev
);
184 list_for_each_entry_safe(dep
, tmp
, &iface_all_users
, list
)
185 dep
->cb(dep
, iface
, ev
);
189 adev
= iface
->l3_dev
.dev
;
192 alias_notify_device(iface
->name
, adev
);
200 interface_flush_state(struct interface
*iface
)
202 if (iface
->l3_dev
.dev
)
203 device_release(&iface
->l3_dev
);
204 interface_data_flush(iface
);
208 mark_interface_down(struct interface
*iface
)
210 enum interface_state state
= iface
->state
;
212 if (state
== IFS_DOWN
)
215 iface
->state
= IFS_DOWN
;
217 interface_event(iface
, IFEV_DOWN
);
218 interface_ip_set_enabled(&iface
->config_ip
, false);
219 interface_ip_flush(&iface
->proto_ip
);
220 interface_flush_state(iface
);
221 system_flush_routes();
225 __interface_set_down(struct interface
*iface
, bool force
)
227 enum interface_state state
= iface
->state
;
231 iface
->state
= IFS_TEARDOWN
;
233 interface_event(iface
, IFEV_DOWN
);
235 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
237 interface_flush_state(iface
);
240 vlist_delete(&interfaces
, &iface
->node
);
244 if (iface
->main_dev
.dev
)
245 device_release(&iface
->main_dev
);
253 __interface_set_up(struct interface
*iface
)
257 netifd_log_message(L_NOTICE
, "Interface '%s' is setting up now\n", iface
->name
);
259 iface
->state
= IFS_SETUP
;
260 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
262 mark_interface_down(iface
);
268 interface_check_state(struct interface
*iface
)
270 bool link_state
= iface
->link_state
|| iface
->force_link
;
272 switch (iface
->state
) {
274 if (!iface
->enabled
|| !link_state
) {
275 mark_interface_down(iface
);
276 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, false);
280 if (iface
->autostart
&& iface
->enabled
&& link_state
&& !config_init
)
281 __interface_set_up(iface
);
289 interface_set_enabled(struct interface
*iface
, bool new_state
)
291 if (iface
->enabled
== new_state
)
294 netifd_log_message(L_NOTICE
, "Interface '%s' is %s\n", iface
->name
, new_state
? "enabled" : "disabled");
295 iface
->enabled
= new_state
;
296 interface_check_state(iface
);
300 interface_set_link_state(struct interface
*iface
, bool new_state
)
302 if (iface
->link_state
== new_state
)
305 netifd_log_message(L_NOTICE
, "Interface '%s' has link connectivity %s\n", iface
->name
, new_state
? "" : "loss");
306 iface
->link_state
= new_state
;
307 interface_check_state(iface
);
311 interface_cb(struct device_user
*dep
, enum device_event ev
)
313 struct interface
*iface
;
314 bool new_state
= false;
316 iface
= container_of(dep
, struct interface
, main_dev
);
320 case DEV_EVENT_REMOVE
:
321 interface_set_available(iface
, new_state
);
322 if (!new_state
&& dep
->dev
&& dep
->dev
->external
)
323 interface_set_main_dev(iface
, NULL
);
328 interface_set_enabled(iface
, new_state
);
330 case DEV_EVENT_LINK_UP
:
332 case DEV_EVENT_LINK_DOWN
:
333 interface_set_link_state(iface
, new_state
);
335 case DEV_EVENT_TOPO_CHANGE
:
336 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
344 interface_set_available(struct interface
*iface
, bool new_state
)
346 if (iface
->available
== new_state
)
349 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
350 iface
->available
= new_state
;
353 if (iface
->autostart
&& !config_init
)
354 interface_set_up(iface
);
356 __interface_set_down(iface
, true);
360 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
363 list_add(&dep
->list
, &iface_all_users
);
368 list_add(&dep
->list
, &iface
->users
);
369 if (iface
->state
== IFS_UP
)
370 dep
->cb(dep
, iface
, IFEV_UP
);
374 interface_remove_user(struct interface_user
*dep
)
376 list_del_init(&dep
->list
);
381 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
383 struct blob_attr
*cur
;
386 blobmsg_for_each_attr(cur
, list
, rem
) {
387 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
390 if (!blobmsg_check_attr(cur
, NULL
))
393 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
394 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
395 list_add(&c
->head
, &iface
->assignment_classes
);
400 interface_clear_assignment_classes(struct interface
*iface
)
402 while (!list_empty(&iface
->assignment_classes
)) {
403 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
404 struct interface_assignment_class
, head
);
411 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
413 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
414 old
->assignment_length
!= new->assignment_length
||
415 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
417 struct interface_assignment_class
*c
;
418 list_for_each_entry(c
, &new->assignment_classes
, head
) {
419 // Compare list entries one-by-one to see if there was a change
420 if (list_empty(&old
->assignment_classes
)) // The new list is longer
426 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
427 struct interface_assignment_class
, head
);
429 if (strcmp(c_old
->name
, c
->name
)) // An entry didn't match
432 list_del(&c_old
->head
);
436 // The old list was longer than the new one or the last entry didn't match
437 if (!list_empty(&old
->assignment_classes
)) {
438 interface_clear_assignment_classes(old
);
442 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
445 old
->assignment_hint
= new->assignment_hint
;
446 old
->assignment_length
= new->assignment_length
;
447 interface_refresh_assignments(true);
452 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
454 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
455 struct device
*dev
= iface
->l3_dev
.dev
;
462 interface_set_main_dev(alias
, dev
);
463 interface_set_available(alias
, true);
466 interface_set_available(alias
, false);
467 interface_set_main_dev(alias
, NULL
);
470 interface_remove_user(dep
);
479 interface_claim_device(struct interface
*iface
)
481 struct interface
*parent
;
482 struct device
*dev
= NULL
;
484 if (iface
->parent_iface
.iface
)
485 interface_remove_user(&iface
->parent_iface
);
487 if (iface
->parent_ifname
) {
488 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
489 iface
->parent_iface
.cb
= interface_alias_cb
;
490 interface_add_user(&iface
->parent_iface
, parent
);
491 } else if (iface
->ifname
&&
492 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
493 dev
= device_get(iface
->ifname
, true);
497 interface_set_main_dev(iface
, dev
);
499 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
500 interface_set_available(iface
, true);
504 interface_cleanup_state(struct interface
*iface
)
506 interface_set_available(iface
, false);
508 interface_flush_state(iface
);
509 interface_clear_errors(iface
);
510 interface_set_proto_state(iface
, NULL
);
512 if (iface
->main_dev
.dev
)
513 interface_set_main_dev(iface
, NULL
);
517 interface_cleanup(struct interface
*iface
)
519 struct interface_user
*dep
, *tmp
;
521 if (iface
->parent_iface
.iface
)
522 interface_remove_user(&iface
->parent_iface
);
524 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
525 interface_remove_user(dep
);
527 interface_clear_assignment_classes(iface
);
528 interface_ip_flush(&iface
->config_ip
);
529 interface_cleanup_state(iface
);
533 interface_do_free(struct interface
*iface
)
535 interface_event(iface
, IFEV_FREE
);
536 interface_cleanup(iface
);
538 netifd_ubus_remove_interface(iface
);
539 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
544 interface_do_reload(struct interface
*iface
)
546 interface_event(iface
, IFEV_RELOAD
);
547 interface_cleanup_state(iface
);
548 proto_init_interface(iface
, iface
->config
);
549 interface_claim_device(iface
);
553 interface_handle_config_change(struct interface
*iface
)
555 enum interface_config_state state
= iface
->config_state
;
557 iface
->config_state
= IFC_NORMAL
;
562 interface_do_reload(iface
);
565 interface_do_free(iface
);
568 if (iface
->autostart
&& iface
->available
)
569 interface_set_up(iface
);
573 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
575 struct interface
*iface
= state
->iface
;
579 if (iface
->state
!= IFS_SETUP
) {
580 interface_event(iface
, IFEV_UPDATE
);
584 if (!iface
->l3_dev
.dev
)
585 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
587 interface_ip_set_enabled(&iface
->config_ip
, true);
588 system_flush_routes();
589 iface
->state
= IFS_UP
;
590 iface
->start_time
= system_get_rtime();
591 interface_event(iface
, IFEV_UP
);
592 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
595 if (iface
->state
== IFS_DOWN
)
598 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
599 mark_interface_down(iface
);
600 if (iface
->main_dev
.dev
)
601 device_release(&iface
->main_dev
);
602 if (iface
->l3_dev
.dev
)
603 device_remove_user(&iface
->l3_dev
);
604 interface_handle_config_change(iface
);
606 case IFPEV_LINK_LOST
:
607 if (iface
->state
!= IFS_UP
)
610 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
611 mark_interface_down(iface
);
612 iface
->state
= IFS_SETUP
;
618 interface_write_resolv_conf();
621 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
624 iface
->proto
->free(iface
->proto
);
627 iface
->state
= IFS_DOWN
;
628 iface
->proto
= state
;
632 state
->proto_event
= interface_proto_cb
;
633 state
->iface
= iface
;
637 interface_alloc(const char *name
, struct blob_attr
*config
)
639 struct interface
*iface
;
640 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
641 struct blob_attr
*cur
;
642 const char *proto_name
= NULL
;
645 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
646 iface
->name
= strcpy(iface_name
, name
);
647 INIT_LIST_HEAD(&iface
->errors
);
648 INIT_LIST_HEAD(&iface
->users
);
649 INIT_LIST_HEAD(&iface
->hotplug_list
);
650 INIT_LIST_HEAD(&iface
->assignment_classes
);
651 interface_ip_init(iface
);
652 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
653 iface
->config_ip
.enabled
= false;
655 iface
->main_dev
.cb
= interface_cb
;
657 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
658 blob_data(config
), blob_len(config
));
660 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
661 proto_name
= blobmsg_data(cur
);
663 proto_attach_interface(iface
, proto_name
);
665 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
666 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], false);
667 iface
->proto_ip
.no_defaultroute
=
668 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
669 iface
->proto_ip
.no_dns
=
670 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
672 if ((cur
= tb
[IFACE_ATTR_DNS
]))
673 interface_add_dns_server_list(&iface
->config_ip
, cur
);
675 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
676 interface_add_dns_search_list(&iface
->config_ip
, cur
);
678 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
679 iface
->metric
= blobmsg_get_u32(cur
);
681 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
682 iface
->assignment_length
= blobmsg_get_u32(cur
);
684 iface
->assignment_hint
= -1;
685 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
686 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
687 ~((1 << (64 - iface
->assignment_length
)) - 1);
689 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
690 interface_add_assignment_classes(iface
, cur
);
693 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
694 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
695 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
698 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
699 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
700 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
703 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
705 iface
->config_autostart
= iface
->autostart
;
709 void interface_set_dynamic(struct interface
*iface
)
711 iface
->dynamic
= true;
712 iface
->node
.version
= -1; // Don't delete on reload
715 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
717 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
718 struct blob_attr
*cur
;
720 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
721 blob_data(config
), blob_len(config
));
724 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
725 iface
->parent_ifname
= blobmsg_data(cur
);
727 if (!iface
->parent_ifname
)
730 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
731 iface
->ifname
= blobmsg_data(cur
);
735 iface
->config
= config
;
736 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
741 interface_add(struct interface
*iface
, struct blob_attr
*config
)
743 __interface_add(iface
, config
, false);
747 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
749 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
752 return __interface_add(iface
, config
, true);
756 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
758 bool enabled
= iface
->config_ip
.enabled
;
759 bool claimed
= iface
->l3_dev
.claimed
;
761 if (iface
->l3_dev
.dev
== dev
)
764 interface_ip_set_enabled(&iface
->config_ip
, false);
765 interface_ip_flush(&iface
->proto_ip
);
766 device_add_user(&iface
->l3_dev
, dev
);
770 if (device_claim(&iface
->l3_dev
) < 0)
773 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
778 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
780 bool claimed
= iface
->l3_dev
.claimed
;
782 if (iface
->main_dev
.dev
== dev
)
785 interface_set_available(iface
, false);
786 device_add_user(&iface
->main_dev
, dev
);
788 interface_set_link_state(iface
, false);
793 if (device_claim(&iface
->l3_dev
) < 0)
797 if (!iface
->l3_dev
.dev
)
798 interface_set_l3_dev(iface
, dev
);
802 interface_remove_link(struct interface
*iface
, struct device
*dev
)
804 struct device
*mdev
= iface
->main_dev
.dev
;
806 if (mdev
&& mdev
->hotplug_ops
)
807 return mdev
->hotplug_ops
->del(mdev
, dev
);
809 if (!iface
->main_dev
.hotplug
)
810 return UBUS_STATUS_INVALID_ARGUMENT
;
812 if (dev
!= iface
->main_dev
.dev
)
813 return UBUS_STATUS_INVALID_ARGUMENT
;
815 interface_set_main_dev(iface
, NULL
);
820 interface_add_link(struct interface
*iface
, struct device
*dev
)
822 struct device
*mdev
= iface
->main_dev
.dev
;
827 if (iface
->main_dev
.hotplug
)
828 device_remove_user(&iface
->main_dev
);
831 if (mdev
->hotplug_ops
)
832 return mdev
->hotplug_ops
->add(mdev
, dev
);
834 return UBUS_STATUS_NOT_SUPPORTED
;
837 interface_set_main_dev(iface
, dev
);
838 iface
->main_dev
.hotplug
= true;
843 interface_handle_link(struct interface
*iface
, const char *name
, bool add
)
850 dev
= device_get(name
, add
? 2 : 0);
852 ret
= UBUS_STATUS_NOT_FOUND
;
857 device_set_present(dev
, true);
858 if (iface
->device_config
)
859 device_set_config(dev
, &simple_device_type
, iface
->config
);
861 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
862 ret
= interface_add_link(iface
, dev
);
864 ret
= interface_remove_link(iface
, dev
);
874 interface_set_up(struct interface
*iface
)
878 iface
->autostart
= true;
880 if (iface
->state
!= IFS_DOWN
)
883 interface_clear_errors(iface
);
884 if (!iface
->available
) {
885 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
889 if (iface
->main_dev
.dev
) {
890 ret
= device_claim(&iface
->main_dev
);
892 interface_check_state(iface
);
895 ret
= __interface_set_up(iface
);
901 interface_set_down(struct interface
*iface
)
904 vlist_for_each_element(&interfaces
, iface
, node
)
905 __interface_set_down(iface
, false);
907 iface
->autostart
= false;
908 __interface_set_down(iface
, false);
915 interface_start_pending(void)
917 struct interface
*iface
;
919 vlist_for_each_element(&interfaces
, iface
, node
) {
920 if (iface
->available
&& iface
->autostart
)
921 interface_set_up(iface
);
926 set_config_state(struct interface
*iface
, enum interface_config_state s
)
928 iface
->config_state
= s
;
929 if (iface
->state
== IFS_DOWN
)
930 interface_handle_config_change(iface
);
932 __interface_set_down(iface
, false);
936 interface_update_start(struct interface
*iface
)
939 interface_ip_update_start(&iface
->proto_ip
);
943 interface_update_complete(struct interface
*iface
)
945 interface_ip_update_complete(&iface
->proto_ip
);
949 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
951 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
952 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
956 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
958 struct blob_attr
*old_config
= if_old
->config
;
959 bool reload
= false, reload_ip
= false;
961 #define FIELD_CHANGED_STR(field) \
962 ((!!if_old->field != !!if_new->field) || \
964 strcmp(if_old->field, if_new->field) != 0))
966 if (FIELD_CHANGED_STR(parent_ifname
)) {
967 if (if_old
->parent_iface
.iface
)
968 interface_remove_user(&if_old
->parent_iface
);
972 if (FIELD_CHANGED_STR(ifname
) ||
973 if_old
->proto_handler
!= if_new
->proto_handler
)
976 if (!if_old
->proto_handler
->config_params
)
977 D(INTERFACE
, "No config parameters for interface '%s'\n",
979 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
980 if_old
->proto_handler
->config_params
))
983 #define UPDATE(field, __var) ({ \
984 bool __changed = (if_old->field != if_new->field); \
985 if_old->field = if_new->field; \
986 __var |= __changed; \
989 if_old
->config
= if_new
->config
;
990 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
991 if_old
->autostart
= true;
993 if_old
->device_config
= if_new
->device_config
;
994 if_old
->config_autostart
= if_new
->config_autostart
;
995 if_old
->ifname
= if_new
->ifname
;
996 if_old
->parent_ifname
= if_new
->parent_ifname
;
997 if_old
->proto_handler
= if_new
->proto_handler
;
998 if_old
->force_link
= if_new
->force_link
;
1000 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1001 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1003 UPDATE(metric
, reload_ip
);
1004 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1005 UPDATE(ip4table
, reload_ip
);
1006 UPDATE(ip6table
, reload_ip
);
1007 interface_merge_assignment_data(if_old
, if_new
);
1012 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1014 interface_clear_errors(if_old
);
1015 set_config_state(if_old
, IFC_RELOAD
);
1020 interface_ip_set_enabled(&if_old
->config_ip
, false);
1021 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1022 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
1023 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
1026 interface_write_resolv_conf();
1027 if (if_old
->main_dev
.dev
)
1028 interface_check_state(if_old
);
1031 if_new
->config
= NULL
;
1032 interface_cleanup(if_new
);
1038 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1039 struct vlist_node
*node_old
)
1041 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1042 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1044 if (node_old
&& node_new
) {
1045 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1046 interface_change_config(if_old
, if_new
);
1047 } else if (node_old
) {
1048 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1049 set_config_state(if_old
, IFC_REMOVE
);
1050 } else if (node_new
) {
1051 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1052 proto_init_interface(if_new
, if_new
->config
);
1053 interface_claim_device(if_new
);
1054 netifd_ubus_add_interface(if_new
);
1060 interface_init_list(void)
1062 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1063 interfaces
.keep_old
= true;
1064 interfaces
.no_delete
= true;