8 #include "interface-ip.h"
14 struct vlist_tree interfaces
;
20 IFACE_ATTR_DEFAULTROUTE
,
24 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
25 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
26 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
27 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
28 [IFACE_ATTR_DEFAULTROUTE
] = { .name
= "defaultroute", .type
= BLOBMSG_TYPE_BOOL
},
31 const struct config_param_list interface_attr_list
= {
32 .n_params
= IFACE_ATTR_MAX
,
33 .params
= iface_attrs
,
37 interface_clear_errors(struct interface
*iface
)
39 struct interface_error
*error
, *tmp
;
41 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
42 list_del(&error
->list
);
47 void interface_add_error(struct interface
*iface
, const char *subsystem
,
48 const char *code
, const char **data
, int n_data
)
50 struct interface_error
*error
;
56 len
= n_data
* sizeof(char *);
57 datalen
= alloca(len
);
58 for (i
= 0; i
< n_data
; i
++) {
59 datalen
[i
] = strlen(data
[i
]) + 1;
64 error
= calloc(1, sizeof(*error
) + sizeof(char *) + len
);
68 list_add_tail(&error
->list
, &iface
->errors
);
69 error
->subsystem
= subsystem
;
72 dest
= (char *) &error
->data
[n_data
+ 1];
73 for (i
= 0; i
< n_data
; i
++) {
74 error
->data
[i
] = dest
;
75 memcpy(dest
, data
[i
], datalen
[i
]);
78 error
->data
[n_data
] = NULL
;
82 interface_event(struct interface
*iface
, enum interface_event ev
)
84 struct interface_user
*dep
, *tmp
;
86 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
87 dep
->cb(dep
, IFEV_UP
);
89 interface_queue_event(iface
, ev
);
93 interface_flush_state(struct interface
*iface
)
95 interface_ip_flush(&iface
->proto_ip
);
96 if (iface
->main_dev
.dev
)
97 device_release(&iface
->main_dev
);
98 if (iface
->l3_dev
!= &iface
->main_dev
&& iface
->l3_dev
->dev
)
99 device_release(iface
->l3_dev
);
103 mark_interface_down(struct interface
*iface
)
105 if (iface
->state
== IFS_UP
)
106 interface_event(iface
, IFEV_DOWN
);
107 interface_flush_state(iface
);
108 iface
->state
= IFS_DOWN
;
112 __interface_set_down(struct interface
*iface
, bool force
)
114 interface_clear_errors(iface
);
116 if (iface
->state
== IFS_DOWN
||
117 iface
->state
== IFS_TEARDOWN
)
120 if (iface
->state
== IFS_UP
)
121 interface_event(iface
, IFEV_DOWN
);
122 iface
->state
= IFS_TEARDOWN
;
123 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
125 interface_flush_state(iface
);
129 interface_cb(struct device_user
*dep
, enum device_event ev
)
131 struct interface
*iface
;
134 iface
= container_of(dep
, struct interface
, main_dev
);
139 case DEV_EVENT_REMOVE
:
146 interface_set_available(iface
, new_state
);
150 interface_set_available(struct interface
*iface
, bool new_state
)
152 if (iface
->available
== new_state
)
155 D(INTERFACE
, "Interface '%s', available=%d\n", iface
->name
, new_state
);
156 iface
->available
= new_state
;
159 if (iface
->autostart
&& !config_init
)
160 interface_set_up(iface
);
162 __interface_set_down(iface
, true);
166 interface_add_user(struct interface_user
*dep
, struct interface
*iface
)
169 list_add(&dep
->list
, &iface
->users
);
170 if (iface
->state
== IFS_UP
)
171 dep
->cb(dep
, IFEV_UP
);
175 interface_remove_user(struct interface_user
*dep
)
177 list_del_init(&dep
->list
);
182 interface_claim_device(struct interface
*iface
)
187 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
188 dev
= device_get(iface
->ifname
, true);
190 device_add_user(&iface
->main_dev
, dev
);
192 if (iface
->proto_handler
->flags
& PROTO_FLAG_INIT_AVAILABLE
)
193 interface_set_available(iface
, true);
198 interface_cleanup(struct interface
*iface
)
200 struct interface_user
*dep
, *tmp
;
202 list_for_each_entry_safe(dep
, tmp
, &iface
->users
, list
)
203 interface_remove_user(dep
);
205 interface_ip_flush(&iface
->config_ip
);
206 interface_flush_state(iface
);
207 interface_clear_errors(iface
);
208 if (iface
->main_dev
.dev
)
209 device_remove_user(&iface
->main_dev
);
210 iface
->l3_dev
= &iface
->main_dev
;
211 interface_set_proto_state(iface
, NULL
);
215 interface_do_free(struct interface
*iface
)
217 interface_cleanup(iface
);
219 netifd_ubus_remove_interface(iface
);
220 avl_delete(&interfaces
.avl
, &iface
->node
.avl
);
225 interface_do_reload(struct interface
*iface
)
227 interface_cleanup(iface
);
228 proto_init_interface(iface
, iface
->config
);
229 interface_claim_device(iface
);
233 interface_handle_config_change(struct interface
*iface
)
235 switch(iface
->config_state
) {
239 interface_do_reload(iface
);
242 interface_do_free(iface
);
245 if (iface
->autostart
&& iface
->available
)
246 interface_set_up(iface
);
250 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
252 struct interface
*iface
= state
->iface
;
256 if (iface
->state
!= IFS_SETUP
)
259 interface_ip_set_enabled(&iface
->config_ip
, true);
260 system_flush_routes();
261 iface
->state
= IFS_UP
;
262 iface
->start_time
= system_get_rtime();
263 interface_event(iface
, IFEV_UP
);
264 interface_write_resolv_conf();
265 netifd_log_message(L_NOTICE
, "Interface '%s' is now up\n", iface
->name
);
268 if (iface
->state
== IFS_DOWN
)
271 netifd_log_message(L_NOTICE
, "Interface '%s' is now down\n", iface
->name
);
272 interface_ip_set_enabled(&iface
->config_ip
, false);
273 system_flush_routes();
274 mark_interface_down(iface
);
275 interface_handle_config_change(iface
);
277 case IFPEV_LINK_LOST
:
278 if (iface
->state
!= IFS_UP
)
281 netifd_log_message(L_NOTICE
, "Interface '%s' has lost the connection\n", iface
->name
);
282 if (iface
->state
== IFS_UP
)
283 interface_event(iface
, IFEV_DOWN
);
284 iface
->state
= IFS_SETUP
;
289 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
292 iface
->proto
->free(iface
->proto
);
295 iface
->state
= IFS_DOWN
;
296 iface
->proto
= state
;
300 state
->proto_event
= interface_proto_cb
;
301 state
->iface
= iface
;
305 interface_init(struct interface
*iface
, const char *name
,
306 struct blob_attr
*config
)
308 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
309 struct blob_attr
*cur
;
310 const char *proto_name
= NULL
;
312 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
313 INIT_LIST_HEAD(&iface
->errors
);
314 INIT_LIST_HEAD(&iface
->users
);
315 INIT_LIST_HEAD(&iface
->hotplug_list
);
316 interface_ip_init(&iface
->proto_ip
, iface
);
317 interface_ip_init(&iface
->config_ip
, iface
);
318 iface
->config_ip
.enabled
= false;
320 iface
->main_dev
.cb
= interface_cb
;
321 iface
->l3_dev
= &iface
->main_dev
;
323 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
324 blob_data(config
), blob_len(config
));
326 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
327 proto_name
= blobmsg_data(cur
);
329 proto_attach_interface(iface
, proto_name
);
331 iface
->autostart
= blobmsg_get_bool_default(tb
[IFACE_ATTR_AUTO
], true);
332 iface
->proto_ip
.no_defaultroute
=
333 !blobmsg_get_bool_default(tb
[IFACE_ATTR_DEFAULTROUTE
], true);
335 iface
->config_autostart
= iface
->autostart
;
339 interface_add(struct interface
*iface
, struct blob_attr
*config
)
341 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
342 struct blob_attr
*cur
;
344 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
345 blob_data(config
), blob_len(config
));
347 if ((cur
= tb
[IFACE_ATTR_IFNAME
]))
348 iface
->ifname
= blobmsg_data(cur
);
350 iface
->config
= config
;
351 vlist_add(&interfaces
, &iface
->node
);
355 interface_remove_link(struct interface
*iface
, struct device
*dev
)
357 struct device
*mdev
= iface
->main_dev
.dev
;
359 if (mdev
&& mdev
->hotplug_ops
) {
360 mdev
->hotplug_ops
->del(mdev
, dev
);
364 device_remove_user(&iface
->main_dev
);
368 interface_add_link(struct interface
*iface
, struct device
*dev
)
370 struct device
*mdev
= iface
->main_dev
.dev
;
372 if (mdev
&& mdev
->hotplug_ops
)
373 return mdev
->hotplug_ops
->add(mdev
, dev
);
375 if (iface
->main_dev
.dev
)
376 interface_remove_link(iface
, NULL
);
378 device_add_user(&iface
->main_dev
, dev
);
384 interface_set_up(struct interface
*iface
)
388 iface
->autostart
= true;
390 if (iface
->state
!= IFS_DOWN
)
393 interface_clear_errors(iface
);
394 if (!iface
->available
) {
395 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
399 if (iface
->main_dev
.dev
) {
400 ret
= device_claim(&iface
->main_dev
);
405 iface
->state
= IFS_SETUP
;
406 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
408 mark_interface_down(iface
);
416 interface_set_down(struct interface
*iface
)
419 vlist_for_each_element(&interfaces
, iface
, node
)
420 __interface_set_down(iface
, false);
422 iface
->autostart
= false;
423 __interface_set_down(iface
, false);
430 interface_start_pending(void)
432 struct interface
*iface
;
434 vlist_for_each_element(&interfaces
, iface
, node
) {
435 if (iface
->available
&& iface
->autostart
)
436 interface_set_up(iface
);
441 set_config_state(struct interface
*iface
, enum interface_config_state s
)
443 iface
->config_state
= s
;
444 if (iface
->state
== IFS_DOWN
)
445 interface_handle_config_change(iface
);
447 __interface_set_down(iface
, false);
451 interface_update_start(struct interface
*iface
)
453 interface_ip_update_start(&iface
->proto_ip
);
457 interface_update_complete(struct interface
*iface
)
459 struct device_route
*route
;
461 interface_ip_update_complete(&iface
->proto_ip
);
462 vlist_for_each_element(&iface
->config_ip
.route
, route
, node
) {
463 if (iface
->l3_dev
->dev
) {
464 system_add_route(iface
->l3_dev
->dev
, route
);
465 route
->enabled
= true;
471 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
473 struct blob_attr
*old_config
= if_old
->config
;
474 const char *old_ifname
= if_old
->ifname
;
475 const struct proto_handler
*proto
= if_old
->proto_handler
;
477 interface_clear_errors(if_old
);
478 if_old
->config
= if_new
->config
;
479 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
480 if_old
->autostart
= true;
482 if_old
->config_autostart
= if_new
->config_autostart
;
483 if_old
->ifname
= if_new
->ifname
;
484 if_old
->proto_handler
= if_new
->proto_handler
;
486 if ((!!old_ifname
!= !!if_new
->ifname
) ||
487 (old_ifname
&& strcmp(old_ifname
, if_new
->ifname
) != 0) ||
488 proto
!= if_new
->proto_handler
) {
489 D(INTERFACE
, "Reload interface '%s' because of ifname/proto change\n",
494 if (!proto
->config_params
)
495 D(INTERFACE
, "No config parameters for interface '%s'\n",
497 else if (!config_check_equal(old_config
, if_new
->config
,
498 proto
->config_params
)) {
499 D(INTERFACE
, "Reload interface '%s because of config changes\n",
504 if (if_old
->proto_ip
.no_defaultroute
!= if_new
->proto_ip
.no_defaultroute
) {
505 if_old
->proto_ip
.no_defaultroute
= if_new
->proto_ip
.no_defaultroute
;
506 interface_ip_set_enabled(&if_old
->proto_ip
, if_old
->proto_ip
.enabled
);
512 set_config_state(if_old
, IFC_RELOAD
);
519 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
520 struct vlist_node
*node_old
)
522 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
523 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
525 if (node_old
&& node_new
) {
526 D(INTERFACE
, "Update interface '%s'\n", if_new
->name
);
527 interface_change_config(if_old
, if_new
);
528 } else if (node_old
) {
529 D(INTERFACE
, "Remove interface '%s'\n", if_old
->name
);
530 set_config_state(if_old
, IFC_REMOVE
);
531 } else if (node_new
) {
532 D(INTERFACE
, "Create interface '%s'\n", if_new
->name
);
533 proto_init_interface(if_new
, if_new
->config
);
534 interface_claim_device(if_new
);
535 netifd_ubus_add_interface(if_new
);
541 interface_init_list(void)
543 vlist_init(&interfaces
, avl_strcmp
, interface_update
,
544 struct interface
, node
, name
);
545 interfaces
.keep_old
= true;
546 interfaces
.no_delete
= true;