8 #include "interface-ip.h"
14 struct vlist_tree interfaces
;
23 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
24 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
25 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
26 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
29 const struct config_param_list interface_attr_list
= {
30 .n_params
= IFACE_ATTR_MAX
,
31 .params
= iface_attrs
,
35 interface_clear_errors(struct interface
*iface
)
37 struct interface_error
*error
, *tmp
;
39 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
40 list_del(&error
->list
);
45 void interface_add_error(struct interface
*iface
, const char *subsystem
,
46 const char *code
, const char **data
, int n_data
)
48 struct interface_error
*error
;
54 len
= n_data
* sizeof(char *);
55 datalen
= alloca(len
);
56 for (i
= 0; i
< n_data
; i
++) {
57 datalen
[i
] = strlen(data
[i
]) + 1;
62 error
= calloc(1, sizeof(*error
) + sizeof(char *) + len
);
66 list_add_tail(&error
->list
, &iface
->errors
);
67 error
->subsystem
= subsystem
;
70 dest
= (char *) &error
->data
[n_data
+ 1];
71 for (i
= 0; i
< n_data
; i
++) {
72 error
->data
[i
] = dest
;
73 memcpy(dest
, data
[i
], datalen
[i
]);
76 error
->data
[n_data
] = NULL
;
80 interface_event(struct interface
*iface
, enum interface_event ev
)
82 struct interface_user
*dep
, *tmp
;
84 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
85 dep
->cb(dep
, IFEV_UP
);
87 interface_queue_event(iface
, ev
);
91 interface_flush_state(struct interface
*iface
)
93 interface_ip_flush(&iface
->proto_ip
);
94 if (iface
->main_dev
.dev
)
95 device_release(&iface
->main_dev
);
96 if (iface
->l3_dev
!= &iface
->main_dev
&& iface
->l3_dev
->dev
)
97 device_release(iface
->l3_dev
);
101 mark_interface_down(struct interface
*iface
)
103 interface_flush_state(iface
);
104 iface
->state
= IFS_DOWN
;
108 __interface_set_down(struct interface
*iface
, bool force
)
110 interface_clear_errors(iface
);
112 if (iface
->state
== IFS_DOWN
||
113 iface
->state
== IFS_TEARDOWN
)
116 iface
->state
= IFS_TEARDOWN
;
117 interface_event(iface
, IFEV_DOWN
);
118 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
120 interface_flush_state(iface
);
124 interface_cb(struct device_user
*dep
, enum device_event ev
)
126 struct interface
*iface
;
129 iface
= container_of(dep
, struct interface
, main_dev
);
134 case DEV_EVENT_REMOVE
:
141 interface_set_available(iface
, new_state
);
145 interface_set_available(struct interface
*iface
, bool new_state
)
147 if (iface
->available
== new_state
)
150 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
151 iface
->available
= new_state
;
154 if (iface
->autostart
&& !config_init
)
155 interface_set_up(iface
);
157 __interface_set_down(iface
, true);
161 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
164 list_add(&dep
->list
, &iface
->users
);
165 if (iface
->state
== IFS_UP
)
166 dep
->cb(dep
, IFEV_UP
);
170 interface_remove_user(struct interface_user
*dep
)
172 list_del_init(&dep
->list
);
177 interface_claim_device(struct interface
*iface
)
181 if (iface
->ifname
&& iface
->proto_handler
&&
182 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
183 dev
= device_get(iface
->ifname
, true);
185 device_add_user(&iface
->main_dev
, dev
);
191 interface_cleanup(struct interface
*iface
)
193 struct interface_user
*dep
, *tmp
;
195 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
196 interface_remove_user(dep
);
198 interface_ip_flush(&iface
->config_ip
);
199 interface_flush_state(iface
);
200 interface_clear_errors(iface
);
201 if (iface
->main_dev
.dev
)
202 device_remove_user(&iface
->main_dev
);
203 iface
->l3_dev
= &iface
->main_dev
;
204 interface_set_proto_state(iface
, NULL
);
208 interface_do_free(struct interface
*iface
)
210 interface_cleanup(iface
);
212 netifd_ubus_remove_interface(iface
);
213 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
218 interface_do_reload(struct interface
*iface
)
220 interface_cleanup(iface
);
221 proto_init_interface(iface
, iface
->config
);
222 interface_claim_device(iface
);
226 interface_handle_config_change(struct interface
*iface
)
228 switch(iface
->config_state
) {
232 interface_do_reload(iface
);
235 interface_do_free(iface
);
238 if (iface
->autostart
&& iface
->available
)
239 interface_set_up(iface
);
243 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
245 struct interface
*iface
= state
->iface
;
249 if (iface
->state
!= IFS_SETUP
)
252 interface_ip_set_enabled(&iface
->config_ip
, true);
253 system_flush_routes();
254 iface
->state
= IFS_UP
;
255 iface
->start_time
= system_get_rtime();
256 interface_event(iface
, IFEV_UP
);
257 interface_write_resolv_conf();
258 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
261 if (iface
->state
== IFS_DOWN
)
264 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
265 interface_ip_set_enabled(&iface
->config_ip
, false);
266 system_flush_routes();
267 mark_interface_down(iface
);
268 interface_handle_config_change(iface
);
270 case IFPEV_LINK_LOST
:
271 if (iface
->state
!= IFS_UP
)
274 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
275 iface
->state
= IFS_SETUP
;
276 interface_event(iface
, IFEV_DOWN
);
281 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
284 iface
->proto
->free(iface
->proto
);
287 iface
->state
= IFS_DOWN
;
288 iface
->proto
= state
;
292 state
->proto_event
= interface_proto_cb
;
293 state
->iface
= iface
;
297 interface_init(struct interface
*iface
, const char *name
,
298 struct blob_attr
*config
)
300 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
301 struct blob_attr
*cur
;
302 const char *proto_name
= NULL
;
304 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
305 INIT_LIST_HEAD(&iface
->errors
);
306 INIT_LIST_HEAD(&iface
->users
);
307 INIT_LIST_HEAD(&iface
->hotplug_list
);
308 interface_ip_init(&iface
->proto_ip
, iface
);
309 interface_ip_init(&iface
->config_ip
, iface
);
310 iface
->config_ip
.enabled
= false;
312 iface
->main_dev
.cb
= interface_cb
;
313 iface
->l3_dev
= &iface
->main_dev
;
315 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
316 blob_data(config
), blob_len(config
));
318 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
319 proto_name
= blobmsg_data(cur
);
321 proto_attach_interface(iface
, proto_name
);
323 if ((cur
= tb
[IFACE_ATTR_AUTO
]))
324 iface
->autostart
= blobmsg_get_bool(cur
);
326 iface
->autostart
= true;
327 iface
->config_autostart
= iface
->autostart
;
331 interface_add(struct interface
*iface
, struct blob_attr
*config
)
333 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
334 struct blob_attr
*cur
;
336 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
337 blob_data(config
), blob_len(config
));
339 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
340 iface
->ifname
= blobmsg_data(cur
);
342 iface
->config
= config
;
343 vlist_add(&interfaces
, &iface
->node
);
347 interface_remove_link(struct interface
*iface
, struct device
*dev
)
349 struct device
*mdev
= iface
->main_dev
.dev
;
351 if (mdev
&& mdev
->hotplug_ops
) {
352 mdev
->hotplug_ops
->del(mdev
, dev
);
356 device_remove_user(&iface
->main_dev
);
360 interface_add_link(struct interface
*iface
, struct device
*dev
)
362 struct device
*mdev
= iface
->main_dev
.dev
;
364 if (mdev
&& mdev
->hotplug_ops
)
365 return mdev
->hotplug_ops
->add(mdev
, dev
);
367 if (iface
->main_dev
.dev
)
368 interface_remove_link(iface
, NULL
);
370 device_add_user(&iface
->main_dev
, dev
);
376 interface_set_up(struct interface
*iface
)
380 iface
->autostart
= true;
382 if (iface
->state
!= IFS_DOWN
)
385 interface_clear_errors(iface
);
386 if (!iface
->available
) {
387 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
391 if (iface
->main_dev
.dev
) {
392 ret
= device_claim(&iface
->main_dev
);
397 iface
->state
= IFS_SETUP
;
398 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
400 mark_interface_down(iface
);
408 interface_set_down(struct interface
*iface
)
411 vlist_for_each_element(&interfaces
, iface
, node
)
412 __interface_set_down(iface
, false);
414 iface
->autostart
= false;
415 __interface_set_down(iface
, false);
422 interface_start_pending(void)
424 struct interface
*iface
;
426 vlist_for_each_element(&interfaces
, iface
, node
) {
427 if (iface
->available
&& iface
->autostart
)
428 interface_set_up(iface
);
433 set_config_state(struct interface
*iface
, enum interface_config_state s
)
435 iface
->config_state
= s
;
436 if (iface
->state
== IFS_DOWN
)
437 interface_handle_config_change(iface
);
439 __interface_set_down(iface
, false);
443 interface_update_start(struct interface
*iface
)
445 interface_ip_update_start(&iface
->proto_ip
);
449 interface_update_complete(struct interface
*iface
)
451 struct device_route
*route
;
453 interface_ip_update_complete(&iface
->proto_ip
);
454 vlist_for_each_element(&iface
->config_ip
.route
, route
, node
) {
455 if (iface
->l3_dev
->dev
) {
456 system_add_route(iface
->l3_dev
->dev
, route
);
457 route
->enabled
= true;
463 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
465 struct blob_attr
*old_config
= if_old
->config
;
466 const char *old_ifname
= if_old
->ifname
;
467 const struct proto_handler
*proto
= if_old
->proto_handler
;
469 interface_clear_errors(if_old
);
470 if_old
->config
= if_new
->config
;
471 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
472 if_old
->autostart
= true;
474 if_old
->config_autostart
= if_new
->config_autostart
;
475 if_old
->ifname
= if_new
->ifname
;
476 if_old
->proto_handler
= if_new
->proto_handler
;
478 if (strcmp(old_ifname
, if_new
->ifname
) != 0 ||
479 proto
!= if_new
->proto_handler
) {
480 D(INTERFACE
, "Reload interface '%s' because of ifname/proto change\n",
485 if (!proto
->config_params
)
486 D(INTERFACE
, "No config parameters for interface '%s'\n",
488 else if (!config_check_equal(old_config
, if_new
->config
,
489 proto
->config_params
)) {
490 D(INTERFACE
, "Reload interface '%s because of config changes\n",
498 set_config_state(if_old
, IFC_RELOAD
);
505 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
506 struct vlist_node
*node_old
)
508 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
509 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
511 if (node_old
&& node_new
) {
512 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
513 interface_change_config(if_old
, if_new
);
514 } else if (node_old
) {
515 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
516 set_config_state(if_old
, IFC_REMOVE
);
517 } else if (node_new
) {
518 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
519 interface_claim_device(if_new
);
520 proto_init_interface(if_new
, if_new
->config
);
521 netifd_ubus_add_interface(if_new
);
527 interface_init_list(void)
529 vlist_init(&interfaces
, avl_strcmp
, interface_update
,
530 struct interface
, node
, name
);
531 interfaces
.keep_old
= true;
532 interfaces
.no_delete
= true;