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 mark_interface_down(struct interface
*iface
)
93 interface_clear_dns(iface
);
94 vlist_flush_all(&iface
->proto_addr
);
95 vlist_flush_all(&iface
->proto_route
);
96 if (iface
->main_dev
.dev
)
97 device_release(&iface
->main_dev
);
98 iface
->state
= IFS_DOWN
;
102 __interface_set_up(struct interface
*iface
)
106 if (iface
->state
!= IFS_DOWN
)
109 if (iface
->main_dev
.dev
) {
110 ret
= device_claim(&iface
->main_dev
);
115 iface
->state
= IFS_SETUP
;
116 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
118 mark_interface_down(iface
);
126 __interface_set_down(struct interface
*iface
, bool force
)
128 interface_clear_errors(iface
);
130 if (iface
->state
== IFS_DOWN
||
131 iface
->state
== IFS_TEARDOWN
)
134 iface
->state
= IFS_TEARDOWN
;
135 interface_event(iface
, IFEV_DOWN
);
136 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
140 interface_cb(struct device_user
*dep
, enum device_event ev
)
142 struct interface
*iface
;
145 iface
= container_of(dep
, struct interface
, main_dev
);
150 case DEV_EVENT_REMOVE
:
157 interface_set_available(iface
, new_state
);
161 interface_set_available(struct interface
*iface
, bool new_state
)
163 if (iface
->available
== new_state
)
166 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
167 iface
->available
= new_state
;
170 if (iface
->autostart
&& !config_init
)
171 interface_set_up(iface
);
173 __interface_set_down(iface
, true);
177 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
180 list_add(&dep
->list
, &iface
->users
);
181 if (iface
->state
== IFS_UP
)
182 dep
->cb(dep
, IFEV_UP
);
186 interface_remove_user(struct interface_user
*dep
)
188 list_del_init(&dep
->list
);
193 interface_claim_device(struct interface
*iface
)
197 if (iface
->proto_handler
&&
198 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
199 dev
= device_get(iface
->ifname
, true);
201 device_add_user(&iface
->main_dev
, dev
);
207 interface_cleanup(struct interface
*iface
)
209 struct interface_user
*dep
, *tmp
;
211 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
212 interface_remove_user(dep
);
214 interface_clear_dns(iface
);
215 interface_clear_errors(iface
);
216 if (iface
->main_dev
.dev
)
217 device_remove_user(&iface
->main_dev
);
218 iface
->l3_dev
= &iface
->main_dev
;
219 interface_set_proto_state(iface
, NULL
);
223 interface_do_free(struct interface
*iface
)
225 interface_cleanup(iface
);
227 netifd_ubus_remove_interface(iface
);
228 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
233 interface_do_reload(struct interface
*iface
)
235 interface_cleanup(iface
);
237 interface_claim_device(iface
);
238 proto_init_interface(iface
, iface
->config
);
240 if (iface
->autostart
&& !config_init
)
241 interface_set_up(iface
);
245 interface_handle_config_change(struct interface
*iface
)
247 switch(iface
->config_state
) {
251 interface_do_reload(iface
);
254 interface_do_free(iface
);
260 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
262 struct interface
*iface
= state
->iface
;
266 if (iface
->state
!= IFS_SETUP
)
269 system_flush_routes();
270 iface
->state
= IFS_UP
;
271 iface
->start_time
= system_get_rtime();
272 interface_event(iface
, IFEV_UP
);
273 interface_write_resolv_conf();
274 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
277 if (iface
->state
== IFS_DOWN
)
280 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
281 system_flush_routes();
282 mark_interface_down(iface
);
283 interface_handle_config_change(iface
);
284 if (iface
->l3_dev
->dev
)
285 device_release(iface
->l3_dev
);
286 if (iface
->autostart
&& iface
->available
)
287 __interface_set_up(iface
);
289 case IFPEV_LINK_LOST
:
290 if (iface
->state
!= IFS_UP
)
293 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
294 iface
->state
= IFS_SETUP
;
295 interface_event(iface
, IFEV_DOWN
);
300 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
303 iface
->proto
->free(iface
->proto
);
306 iface
->state
= IFS_DOWN
;
307 iface
->proto
= state
;
311 state
->proto_event
= interface_proto_cb
;
312 state
->iface
= iface
;
316 interface_init(struct interface
*iface
, const char *name
,
317 struct blob_attr
*config
)
319 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
320 struct blob_attr
*cur
;
321 const char *proto_name
= NULL
;
323 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
324 INIT_LIST_HEAD(&iface
->errors
);
325 INIT_LIST_HEAD(&iface
->users
);
326 INIT_LIST_HEAD(&iface
->hotplug_list
);
327 INIT_LIST_HEAD(&iface
->proto_dns_search
);
328 INIT_LIST_HEAD(&iface
->proto_dns_servers
);
330 iface
->main_dev
.cb
= interface_cb
;
331 iface
->l3_dev
= &iface
->main_dev
;
333 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
334 blob_data(config
), blob_len(config
));
336 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
337 proto_name
= blobmsg_data(cur
);
339 proto_attach_interface(iface
, proto_name
);
341 if ((cur
= tb
[IFACE_ATTR_AUTO
]))
342 iface
->autostart
= blobmsg_get_bool(cur
);
344 iface
->autostart
= true;
345 iface
->config_autostart
= iface
->autostart
;
349 interface_add(struct interface
*iface
, struct blob_attr
*config
)
351 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
352 struct blob_attr
*cur
;
354 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
355 blob_data(config
), blob_len(config
));
357 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
358 iface
->ifname
= blobmsg_data(cur
);
360 iface
->config
= config
;
361 vlist_add(&interfaces
, &iface
->node
);
365 interface_remove_link(struct interface
*iface
, struct device
*dev
)
367 struct device
*mdev
= iface
->main_dev
.dev
;
369 if (mdev
&& mdev
->hotplug_ops
) {
370 mdev
->hotplug_ops
->del(mdev
, dev
);
374 device_remove_user(&iface
->main_dev
);
378 interface_add_link(struct interface
*iface
, struct device
*dev
)
380 struct device
*mdev
= iface
->main_dev
.dev
;
382 if (mdev
&& mdev
->hotplug_ops
)
383 return mdev
->hotplug_ops
->add(mdev
, dev
);
385 if (iface
->main_dev
.dev
)
386 interface_remove_link(iface
, NULL
);
388 device_add_user(&iface
->main_dev
, dev
);
394 interface_set_up(struct interface
*iface
)
396 iface
->autostart
= true;
398 if (iface
->state
!= IFS_DOWN
)
401 interface_clear_errors(iface
);
402 if (!iface
->available
) {
403 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
407 return __interface_set_up(iface
);
411 interface_set_down(struct interface
*iface
)
414 vlist_for_each_element(&interfaces
, iface
, node
)
415 __interface_set_down(iface
, false);
417 iface
->autostart
= false;
418 __interface_set_down(iface
, false);
425 interface_start_pending(void)
427 struct interface
*iface
;
429 vlist_for_each_element(&interfaces
, iface
, node
) {
430 if (iface
->available
&& iface
->autostart
)
431 interface_set_up(iface
);
436 set_config_state(struct interface
*iface
, enum interface_config_state s
)
438 iface
->config_state
= s
;
439 if (iface
->state
== IFS_DOWN
)
440 interface_handle_config_change(iface
);
442 __interface_set_down(iface
, false);
446 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
448 struct blob_attr
*old_config
= if_old
->config
;
449 const char *old_ifname
= if_old
->ifname
;
450 const struct proto_handler
*proto
= if_old
->proto_handler
;
452 interface_clear_errors(if_old
);
453 if_old
->config
= if_new
->config
;
454 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
455 if_old
->autostart
= true;
457 if_old
->config_autostart
= if_new
->config_autostart
;
458 if_old
->ifname
= if_new
->ifname
;
459 if_old
->proto_handler
= if_new
->proto_handler
;
461 if (strcmp(old_ifname
, if_new
->ifname
) != 0 ||
462 proto
!= if_new
->proto_handler
) {
463 D(INTERFACE
, "Reload interface '%s' because of ifname/proto change\n",
468 if (!proto
->config_params
)
469 D(INTERFACE
, "No config parameters for interface '%s'\n",
471 else if (!config_check_equal(old_config
, if_new
->config
,
472 proto
->config_params
)) {
473 D(INTERFACE
, "Reload interface '%s because of config changes\n",
481 set_config_state(if_old
, IFC_RELOAD
);
488 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
489 struct vlist_node
*node_old
)
491 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
492 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
494 if (node_old
&& node_new
) {
495 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
496 interface_change_config(if_old
, if_new
);
497 } else if (node_old
) {
498 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
499 set_config_state(if_old
, IFC_REMOVE
);
500 } else if (node_new
) {
501 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
502 interface_claim_device(if_new
);
503 proto_init_interface(if_new
, if_new
->config
);
504 interface_ip_init(if_new
);
505 netifd_ubus_add_interface(if_new
);
511 interface_init_list(void)
513 vlist_init(&interfaces
, avl_strcmp
, interface_update
,
514 struct interface
, node
, name
);
515 interfaces
.keep_old
= true;
516 interfaces
.no_delete
= true;