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_ext_cb(struct device_user
*dep
, enum device_event ev
)
313 if (ev
== DEV_EVENT_REMOVE
)
314 device_remove_user(dep
);
318 interface_cb(struct device_user
*dep
, enum device_event ev
)
320 struct interface
*iface
;
321 bool new_state
= false;
323 iface
= container_of(dep
, struct interface
, main_dev
);
327 case DEV_EVENT_REMOVE
:
328 interface_set_available(iface
, new_state
);
329 if (!new_state
&& dep
->dev
&& dep
->dev
->external
)
330 interface_set_main_dev(iface
, NULL
);
335 interface_set_enabled(iface
, new_state
);
337 case DEV_EVENT_LINK_UP
:
339 case DEV_EVENT_LINK_DOWN
:
340 interface_set_link_state(iface
, new_state
);
342 case DEV_EVENT_TOPO_CHANGE
:
343 interface_proto_event(iface
->proto
, PROTO_CMD_RENEW
, false);
351 interface_set_available(struct interface
*iface
, bool new_state
)
353 if (iface
->available
== new_state
)
356 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
357 iface
->available
= new_state
;
360 if (iface
->autostart
&& !config_init
)
361 interface_set_up(iface
);
363 __interface_set_down(iface
, true);
367 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
370 list_add(&dep
->list
, &iface_all_users
);
375 list_add(&dep
->list
, &iface
->users
);
376 if (iface
->state
== IFS_UP
)
377 dep
->cb(dep
, iface
, IFEV_UP
);
381 interface_remove_user(struct interface_user
*dep
)
383 list_del_init(&dep
->list
);
388 interface_add_assignment_classes(struct interface
*iface
, struct blob_attr
*list
)
390 struct blob_attr
*cur
;
393 blobmsg_for_each_attr(cur
, list
, rem
) {
394 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
397 if (!blobmsg_check_attr(cur
, NULL
))
400 struct interface_assignment_class
*c
= malloc(sizeof(*c
) + blobmsg_data_len(cur
));
401 memcpy(c
->name
, blobmsg_data(cur
), blobmsg_data_len(cur
));
402 list_add(&c
->head
, &iface
->assignment_classes
);
407 interface_clear_assignment_classes(struct interface
*iface
)
409 while (!list_empty(&iface
->assignment_classes
)) {
410 struct interface_assignment_class
*c
= list_first_entry(&iface
->assignment_classes
,
411 struct interface_assignment_class
, head
);
418 interface_merge_assignment_data(struct interface
*old
, struct interface
*new)
420 bool changed
= (old
->assignment_hint
!= new->assignment_hint
||
421 old
->assignment_length
!= new->assignment_length
||
422 list_empty(&old
->assignment_classes
) != list_empty(&new->assignment_classes
));
424 struct interface_assignment_class
*c
;
425 list_for_each_entry(c
, &new->assignment_classes
, head
) {
426 // Compare list entries one-by-one to see if there was a change
427 if (list_empty(&old
->assignment_classes
)) // The new list is longer
433 struct interface_assignment_class
*c_old
= list_first_entry(&old
->assignment_classes
,
434 struct interface_assignment_class
, head
);
436 if (strcmp(c_old
->name
, c
->name
)) // An entry didn't match
439 list_del(&c_old
->head
);
443 // The old list was longer than the new one or the last entry didn't match
444 if (!list_empty(&old
->assignment_classes
)) {
445 interface_clear_assignment_classes(old
);
449 list_splice_init(&new->assignment_classes
, &old
->assignment_classes
);
452 old
->assignment_hint
= new->assignment_hint
;
453 old
->assignment_length
= new->assignment_length
;
454 interface_refresh_assignments(true);
459 interface_alias_cb(struct interface_user
*dep
, struct interface
*iface
, enum interface_event ev
)
461 struct interface
*alias
= container_of(dep
, struct interface
, parent_iface
);
462 struct device
*dev
= iface
->l3_dev
.dev
;
469 interface_set_main_dev(alias
, dev
);
470 interface_set_available(alias
, true);
473 interface_set_available(alias
, false);
474 interface_set_main_dev(alias
, NULL
);
477 interface_remove_user(dep
);
486 interface_claim_device(struct interface
*iface
)
488 struct interface
*parent
;
489 struct device
*dev
= NULL
;
491 if (iface
->parent_iface
.iface
)
492 interface_remove_user(&iface
->parent_iface
);
494 if (iface
->parent_ifname
) {
495 parent
= vlist_find(&interfaces
, iface
->parent_ifname
, parent
, node
);
496 iface
->parent_iface
.cb
= interface_alias_cb
;
497 interface_add_user(&iface
->parent_iface
, parent
);
498 } else if (iface
->ifname
&&
499 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
500 dev
= device_get(iface
->ifname
, true);
502 dev
= iface
->ext_dev
.dev
;
506 interface_set_main_dev(iface
, dev
);
508 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
509 interface_set_available(iface
, true);
513 interface_cleanup_state(struct interface
*iface
)
515 interface_set_available(iface
, false);
517 interface_flush_state(iface
);
518 interface_clear_errors(iface
);
519 interface_set_proto_state(iface
, NULL
);
521 interface_set_main_dev(iface
, NULL
);
522 interface_set_l3_dev(iface
, NULL
);
526 interface_cleanup(struct interface
*iface
)
528 struct interface_user
*dep
, *tmp
;
530 device_remove_user(&iface
->ext_dev
);
532 if (iface
->parent_iface
.iface
)
533 interface_remove_user(&iface
->parent_iface
);
535 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
536 interface_remove_user(dep
);
538 interface_clear_assignment_classes(iface
);
539 interface_ip_flush(&iface
->config_ip
);
540 interface_cleanup_state(iface
);
544 interface_do_free(struct interface
*iface
)
546 interface_event(iface
, IFEV_FREE
);
547 interface_cleanup(iface
);
549 netifd_ubus_remove_interface(iface
);
550 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
555 interface_do_reload(struct interface
*iface
)
557 interface_event(iface
, IFEV_RELOAD
);
558 interface_cleanup_state(iface
);
559 proto_init_interface(iface
, iface
->config
);
560 interface_claim_device(iface
);
564 interface_handle_config_change(struct interface
*iface
)
566 enum interface_config_state state
= iface
->config_state
;
568 iface
->config_state
= IFC_NORMAL
;
573 interface_do_reload(iface
);
576 interface_do_free(iface
);
579 if (iface
->autostart
&& iface
->available
)
580 interface_set_up(iface
);
584 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
586 struct interface
*iface
= state
->iface
;
590 if (iface
->state
!= IFS_SETUP
) {
591 interface_event(iface
, IFEV_UPDATE
);
595 if (!iface
->l3_dev
.dev
)
596 interface_set_l3_dev(iface
, iface
->main_dev
.dev
);
598 interface_ip_set_enabled(&iface
->config_ip
, true);
599 system_flush_routes();
600 iface
->state
= IFS_UP
;
601 iface
->start_time
= system_get_rtime();
602 interface_event(iface
, IFEV_UP
);
603 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
606 if (iface
->state
== IFS_DOWN
)
609 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
610 mark_interface_down(iface
);
611 if (iface
->main_dev
.dev
)
612 device_release(&iface
->main_dev
);
613 if (iface
->l3_dev
.dev
)
614 device_remove_user(&iface
->l3_dev
);
615 interface_handle_config_change(iface
);
617 case IFPEV_LINK_LOST
:
618 if (iface
->state
!= IFS_UP
)
621 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
622 mark_interface_down(iface
);
623 iface
->state
= IFS_SETUP
;
629 interface_write_resolv_conf();
632 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
635 iface
->proto
->free(iface
->proto
);
638 iface
->state
= IFS_DOWN
;
639 iface
->proto
= state
;
643 state
->proto_event
= interface_proto_cb
;
644 state
->iface
= iface
;
648 interface_alloc(const char *name
, struct blob_attr
*config
)
650 struct interface
*iface
;
651 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
652 struct blob_attr
*cur
;
653 const char *proto_name
= NULL
;
656 iface
= calloc_a(sizeof(*iface
), &iface_name
, strlen(name
) + 1);
657 iface
->name
= strcpy(iface_name
, name
);
658 INIT_LIST_HEAD(&iface
->errors
);
659 INIT_LIST_HEAD(&iface
->users
);
660 INIT_LIST_HEAD(&iface
->hotplug_list
);
661 INIT_LIST_HEAD(&iface
->assignment_classes
);
662 interface_ip_init(iface
);
663 avl_init(&iface
->data
, avl_strcmp
, false, NULL
);
664 iface
->config_ip
.enabled
= false;
666 iface
->main_dev
.cb
= interface_cb
;
667 iface
->ext_dev
.cb
= interface_ext_cb
;
669 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
670 blob_data(config
), blob_len(config
));
672 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
673 proto_name
= blobmsg_data(cur
);
675 proto_attach_interface(iface
, proto_name
);
677 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
678 iface
->force_link
= blobmsg_get_bool_default(tb
[IFACE_ATTR_FORCE_LINK
], false);
679 iface
->proto_ip
.no_defaultroute
=
680 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
681 iface
->proto_ip
.no_dns
=
682 !blobmsg_get_bool_default(tb
[IFACE_ATTR_PEERDNS
], true);
684 if ((cur
= tb
[IFACE_ATTR_DNS
]))
685 interface_add_dns_server_list(&iface
->config_ip
, cur
);
687 if ((cur
= tb
[IFACE_ATTR_DNS_SEARCH
]))
688 interface_add_dns_search_list(&iface
->config_ip
, cur
);
690 if ((cur
= tb
[IFACE_ATTR_METRIC
]))
691 iface
->metric
= blobmsg_get_u32(cur
);
693 if ((cur
= tb
[IFACE_ATTR_IP6ASSIGN
]))
694 iface
->assignment_length
= blobmsg_get_u32(cur
);
696 iface
->assignment_hint
= -1;
697 if ((cur
= tb
[IFACE_ATTR_IP6HINT
]))
698 iface
->assignment_hint
= strtol(blobmsg_get_string(cur
), NULL
, 16) &
699 ~((1 << (64 - iface
->assignment_length
)) - 1);
701 if ((cur
= tb
[IFACE_ATTR_IP6CLASS
]))
702 interface_add_assignment_classes(iface
, cur
);
705 if ((cur
= tb
[IFACE_ATTR_IP4TABLE
])) {
706 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip4table
))
707 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
710 if ((cur
= tb
[IFACE_ATTR_IP6TABLE
])) {
711 if (!system_resolve_rt_table(blobmsg_data(cur
), &iface
->ip6table
))
712 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
715 iface
->proto_ip
.no_delegation
= !blobmsg_get_bool_default(tb
[IFACE_ATTR_DELEGATE
], true);
717 iface
->config_autostart
= iface
->autostart
;
721 void interface_set_dynamic(struct interface
*iface
)
723 iface
->dynamic
= true;
724 iface
->autostart
= true;
725 iface
->node
.version
= -1; // Don't delete on reload
728 static bool __interface_add(struct interface
*iface
, struct blob_attr
*config
, bool alias
)
730 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
731 struct blob_attr
*cur
;
733 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
734 blob_data(config
), blob_len(config
));
737 if ((cur
= tb
[IFACE_ATTR_INTERFACE
]))
738 iface
->parent_ifname
= blobmsg_data(cur
);
740 if (!iface
->parent_ifname
)
743 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
744 iface
->ifname
= blobmsg_data(cur
);
748 iface
->config
= config
;
749 vlist_add(&interfaces
, &iface
->node
, iface
->name
);
754 interface_add(struct interface
*iface
, struct blob_attr
*config
)
756 __interface_add(iface
, config
, false);
760 interface_add_alias(struct interface
*iface
, struct blob_attr
*config
)
762 if (iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)
765 return __interface_add(iface
, config
, true);
769 interface_set_l3_dev(struct interface
*iface
, struct device
*dev
)
771 bool enabled
= iface
->config_ip
.enabled
;
772 bool claimed
= iface
->l3_dev
.claimed
;
774 if (iface
->l3_dev
.dev
== dev
)
777 interface_ip_set_enabled(&iface
->config_ip
, false);
778 interface_ip_flush(&iface
->proto_ip
);
779 device_add_user(&iface
->l3_dev
, dev
);
783 if (device_claim(&iface
->l3_dev
) < 0)
786 interface_ip_set_enabled(&iface
->config_ip
, enabled
);
791 interface_set_main_dev(struct interface
*iface
, struct device
*dev
)
793 bool claimed
= iface
->l3_dev
.claimed
;
795 if (iface
->main_dev
.dev
== dev
)
798 interface_set_available(iface
, false);
799 device_add_user(&iface
->main_dev
, dev
);
801 interface_set_link_state(iface
, false);
806 if (device_claim(&iface
->l3_dev
) < 0)
810 if (!iface
->l3_dev
.dev
)
811 interface_set_l3_dev(iface
, dev
);
815 interface_remove_link(struct interface
*iface
, struct device
*dev
)
817 struct device
*mdev
= iface
->main_dev
.dev
;
819 if (mdev
&& mdev
->hotplug_ops
)
820 return mdev
->hotplug_ops
->del(mdev
, dev
);
822 if (dev
== iface
->ext_dev
.dev
)
823 device_remove_user(&iface
->ext_dev
);
825 if (!iface
->main_dev
.hotplug
)
826 return UBUS_STATUS_INVALID_ARGUMENT
;
828 if (dev
!= iface
->main_dev
.dev
)
829 return UBUS_STATUS_INVALID_ARGUMENT
;
831 interface_set_main_dev(iface
, NULL
);
836 interface_add_link(struct interface
*iface
, struct device
*dev
)
838 struct device
*mdev
= iface
->main_dev
.dev
;
843 if (iface
->main_dev
.hotplug
)
844 device_remove_user(&iface
->main_dev
);
847 if (mdev
->hotplug_ops
)
848 return mdev
->hotplug_ops
->add(mdev
, dev
);
850 return UBUS_STATUS_NOT_SUPPORTED
;
853 device_add_user(&iface
->ext_dev
, dev
);
854 interface_set_main_dev(iface
, dev
);
855 iface
->main_dev
.hotplug
= true;
860 interface_handle_link(struct interface
*iface
, const char *name
, bool add
)
867 dev
= device_get(name
, add
? 2 : 0);
869 ret
= UBUS_STATUS_NOT_FOUND
;
874 device_set_present(dev
, true);
875 if (iface
->device_config
)
876 device_set_config(dev
, &simple_device_type
, iface
->config
);
878 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
879 ret
= interface_add_link(iface
, dev
);
881 ret
= interface_remove_link(iface
, dev
);
891 interface_set_up(struct interface
*iface
)
895 iface
->autostart
= true;
897 if (iface
->state
!= IFS_DOWN
)
900 interface_clear_errors(iface
);
901 if (!iface
->available
) {
902 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
906 if (iface
->main_dev
.dev
) {
907 ret
= device_claim(&iface
->main_dev
);
909 interface_check_state(iface
);
912 ret
= __interface_set_up(iface
);
918 interface_set_down(struct interface
*iface
)
921 vlist_for_each_element(&interfaces
, iface
, node
)
922 __interface_set_down(iface
, false);
924 iface
->autostart
= false;
925 __interface_set_down(iface
, false);
932 interface_start_pending(void)
934 struct interface
*iface
;
936 vlist_for_each_element(&interfaces
, iface
, node
) {
937 if (iface
->available
&& iface
->autostart
)
938 interface_set_up(iface
);
943 set_config_state(struct interface
*iface
, enum interface_config_state s
)
945 iface
->config_state
= s
;
946 if (iface
->state
== IFS_DOWN
)
947 interface_handle_config_change(iface
);
949 __interface_set_down(iface
, false);
953 interface_update_start(struct interface
*iface
)
956 interface_ip_update_start(&iface
->proto_ip
);
960 interface_update_complete(struct interface
*iface
)
962 interface_ip_update_complete(&iface
->proto_ip
);
966 interface_replace_dns(struct interface_ip_settings
*new, struct interface_ip_settings
*old
)
968 vlist_simple_replace(&new->dns_servers
, &old
->dns_servers
);
969 vlist_simple_replace(&new->dns_search
, &old
->dns_search
);
973 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
975 struct blob_attr
*old_config
= if_old
->config
;
976 bool reload
= false, reload_ip
= false;
978 #define FIELD_CHANGED_STR(field) \
979 ((!!if_old->field != !!if_new->field) || \
981 strcmp(if_old->field, if_new->field) != 0))
983 if (FIELD_CHANGED_STR(parent_ifname
)) {
984 if (if_old
->parent_iface
.iface
)
985 interface_remove_user(&if_old
->parent_iface
);
989 if (FIELD_CHANGED_STR(ifname
) ||
990 if_old
->proto_handler
!= if_new
->proto_handler
)
993 if (!if_old
->proto_handler
->config_params
)
994 D(INTERFACE
, "No config parameters for interface '%s'\n",
996 else if (!uci_blob_check_equal(if_old
->config
, if_new
->config
,
997 if_old
->proto_handler
->config_params
))
1000 #define UPDATE(field, __var) ({ \
1001 bool __changed = (if_old->field != if_new->field); \
1002 if_old->field = if_new->field; \
1003 __var |= __changed; \
1006 if_old
->config
= if_new
->config
;
1007 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
1008 if_old
->autostart
= true;
1010 if_old
->device_config
= if_new
->device_config
;
1011 if_old
->config_autostart
= if_new
->config_autostart
;
1012 if_old
->ifname
= if_new
->ifname
;
1013 if_old
->parent_ifname
= if_new
->parent_ifname
;
1014 if_old
->proto_handler
= if_new
->proto_handler
;
1015 if_old
->force_link
= if_new
->force_link
;
1017 if_old
->proto_ip
.no_dns
= if_new
->proto_ip
.no_dns
;
1018 interface_replace_dns(&if_old
->config_ip
, &if_new
->config_ip
);
1020 UPDATE(metric
, reload_ip
);
1021 UPDATE(proto_ip
.no_defaultroute
, reload_ip
);
1022 UPDATE(ip4table
, reload_ip
);
1023 UPDATE(ip6table
, reload_ip
);
1024 interface_merge_assignment_data(if_old
, if_new
);
1029 D(INTERFACE
, "Reload interface '%s' because of config changes\n",
1031 interface_clear_errors(if_old
);
1032 set_config_state(if_old
, IFC_RELOAD
);
1037 interface_ip_set_enabled(&if_old
->config_ip
, false);
1038 interface_ip_set_enabled(&if_old
->proto_ip
, false);
1039 interface_ip_set_enabled(&if_old
->proto_ip
, if_new
->proto_ip
.enabled
);
1040 interface_ip_set_enabled(&if_old
->config_ip
, if_new
->config_ip
.enabled
);
1043 interface_write_resolv_conf();
1044 if (if_old
->main_dev
.dev
)
1045 interface_check_state(if_old
);
1048 if_new
->config
= NULL
;
1049 interface_cleanup(if_new
);
1055 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
1056 struct vlist_node
*node_old
)
1058 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
1059 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
1061 if (node_old
&& node_new
) {
1062 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
1063 interface_change_config(if_old
, if_new
);
1064 } else if (node_old
) {
1065 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
1066 set_config_state(if_old
, IFC_REMOVE
);
1067 } else if (node_new
) {
1068 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
1069 proto_init_interface(if_new
, if_new
->config
);
1070 interface_claim_device(if_new
);
1071 netifd_ubus_add_interface(if_new
);
1077 interface_init_list(void)
1079 vlist_init(&interfaces
, avl_strcmp
, interface_update
);
1080 interfaces
.keep_old
= true;
1081 interfaces
.no_delete
= true;