8 #include "interface-ip.h"
13 struct vlist_tree interfaces
;
22 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
23 [IFACE_ATTR_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
24 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
25 [IFACE_ATTR_AUTO
] = { .name
= "auto", .type
= BLOBMSG_TYPE_BOOL
},
28 const struct config_param_list interface_attr_list
= {
29 .n_params
= IFACE_ATTR_MAX
,
30 .params
= iface_attrs
,
34 clear_interface_errors(struct interface
*iface
)
36 struct interface_error
*error
, *tmp
;
38 list_for_each_entry_safe(error
, tmp
, &iface
->errors
, list
) {
39 list_del(&error
->list
);
44 void interface_add_error(struct interface
*iface
, const char *subsystem
,
45 const char *code
, const char **data
, int n_data
)
47 struct interface_error
*error
;
53 len
= n_data
* sizeof(char *);
54 datalen
= alloca(len
);
55 for (i
= 0; i
< n_data
; i
++) {
56 datalen
[i
] = strlen(data
[i
]) + 1;
61 error
= calloc(1, sizeof(*error
) + sizeof(char *) + len
);
65 list_add_tail(&error
->list
, &iface
->errors
);
66 error
->subsystem
= subsystem
;
69 dest
= (char *) &error
->data
[n_data
+ 1];
70 for (i
= 0; i
< n_data
; i
++) {
71 error
->data
[i
] = dest
;
72 memcpy(dest
, data
[i
], datalen
[i
]);
75 error
->data
[n_data
] = NULL
;
79 interface_event(struct interface
*iface
, enum interface_event ev
)
85 mark_interface_down(struct interface
*iface
)
87 vlist_flush_all(&iface
->proto_addr
);
88 vlist_flush_all(&iface
->proto_route
);
89 if (iface
->main_dev
.dev
)
90 device_release(&iface
->main_dev
);
91 iface
->state
= IFS_DOWN
;
95 __interface_set_up(struct interface
*iface
)
99 if (iface
->state
!= IFS_DOWN
)
102 if (iface
->main_dev
.dev
) {
103 ret
= device_claim(&iface
->main_dev
);
108 iface
->state
= IFS_SETUP
;
109 ret
= interface_proto_event(iface
->proto
, PROTO_CMD_SETUP
, false);
111 mark_interface_down(iface
);
120 __interface_set_down(struct interface
*iface
, bool force
)
122 clear_interface_errors(iface
);
124 if (iface
->state
== IFS_DOWN
||
125 iface
->state
== IFS_TEARDOWN
)
128 iface
->state
= IFS_TEARDOWN
;
129 interface_event(iface
, IFEV_DOWN
);
130 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, force
);
134 interface_cb(struct device_user
*dep
, enum device_event ev
)
136 struct interface
*iface
;
139 iface
= container_of(dep
, struct interface
, main_dev
);
144 case DEV_EVENT_REMOVE
:
151 interface_set_available(iface
, new_state
);
155 interface_set_available(struct interface
*iface
, bool new_state
)
157 if (iface
->available
== new_state
)
160 iface
->available
= new_state
;
163 if (iface
->autostart
&& !config_init
)
164 interface_set_up(iface
);
166 __interface_set_down(iface
, true);
170 interface_do_free(struct interface
*iface
)
172 interface_set_proto_state(iface
, NULL
);
174 netifd_ubus_remove_interface(iface
);
179 interface_handle_config_change(struct interface
*iface
)
181 switch(iface
->config_state
) {
185 if (iface
->autostart
)
186 interface_set_up(iface
);
189 interface_do_free(iface
);
195 interface_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
197 struct interface
*iface
= state
->iface
;
201 if (iface
->state
!= IFS_SETUP
)
204 iface
->state
= IFS_UP
;
205 interface_event(iface
, IFEV_UP
);
208 if (iface
->state
== IFS_DOWN
)
211 mark_interface_down(iface
);
212 interface_handle_config_change(iface
);
214 case IFPEV_LINK_LOST
:
215 if (iface
->state
!= IFS_UP
)
218 iface
->state
= IFS_SETUP
;
219 interface_event(iface
, IFEV_DOWN
);
224 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
227 iface
->proto
->free(iface
->proto
);
230 iface
->state
= IFS_DOWN
;
231 iface
->proto
= state
;
235 state
->proto_event
= interface_proto_cb
;
236 state
->iface
= iface
;
240 interface_init(struct interface
*iface
, const char *name
,
241 struct blob_attr
*config
)
243 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
244 struct blob_attr
*cur
;
245 const char *proto_name
= NULL
;
247 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
248 INIT_LIST_HEAD(&iface
->errors
);
250 iface
->main_dev
.cb
= interface_cb
;
251 iface
->l3_dev
= &iface
->main_dev
;
253 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
254 blob_data(config
), blob_len(config
));
256 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
257 proto_name
= blobmsg_data(cur
);
259 proto_attach_interface(iface
, proto_name
);
261 if ((cur
= tb
[IFACE_ATTR_AUTO
]))
262 iface
->autostart
= blobmsg_get_bool(cur
);
264 iface
->autostart
= true;
265 iface
->config_autostart
= iface
->autostart
;
269 interface_add(struct interface
*iface
, struct blob_attr
*config
)
271 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
272 struct blob_attr
*cur
;
275 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
276 blob_data(config
), blob_len(config
));
278 if ((cur
= tb
[IFACE_ATTR_IFNAME
])) {
279 iface
->ifname
= blobmsg_data(cur
);
281 if (iface
->proto_handler
&&
282 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
)) {
283 dev
= device_get(iface
->ifname
, true);
285 device_add_user(&iface
->main_dev
, dev
);
289 iface
->config
= config
;
290 vlist_add(&interfaces
, &iface
->node
);
294 interface_remove_link(struct interface
*iface
, struct device
*dev
)
296 struct device
*mdev
= iface
->main_dev
.dev
;
298 if (mdev
&& mdev
->hotplug_ops
) {
299 mdev
->hotplug_ops
->del(mdev
, dev
);
303 device_remove_user(&iface
->main_dev
);
307 interface_add_link(struct interface
*iface
, struct device
*dev
)
309 struct device
*mdev
= iface
->main_dev
.dev
;
311 if (mdev
&& mdev
->hotplug_ops
)
312 return mdev
->hotplug_ops
->add(mdev
, dev
);
314 if (iface
->main_dev
.dev
)
315 interface_remove_link(iface
, NULL
);
317 device_add_user(&iface
->main_dev
, dev
);
323 interface_set_up(struct interface
*iface
)
325 iface
->autostart
= true;
327 if (!iface
->available
) {
328 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
332 if (iface
->state
!= IFS_DOWN
)
335 return __interface_set_up(iface
);
339 interface_set_down(struct interface
*iface
)
342 vlist_for_each_element(&interfaces
, iface
, node
)
343 __interface_set_down(iface
, false);
345 iface
->autostart
= false;
346 __interface_set_down(iface
, false);
353 interface_start_pending(void)
355 struct interface
*iface
;
357 vlist_for_each_element(&interfaces
, iface
, node
) {
358 if (iface
->available
&& iface
->autostart
)
359 interface_set_up(iface
);
364 set_config_state(struct interface
*iface
, enum interface_config_state s
)
366 iface
->config_state
= s
;
367 if (iface
->state
== IFS_DOWN
)
368 interface_handle_config_change(iface
);
370 interface_set_down(iface
);
374 interface_change_config(struct interface
*if_old
, struct interface
*if_new
)
376 struct blob_attr
*old_config
= if_old
->config
;
378 if_old
->config
= if_new
->config
;
379 if (!if_old
->config_autostart
&& if_new
->config_autostart
)
380 if_old
->autostart
= true;
382 if_old
->config_autostart
= if_new
->config_autostart
;
383 if_old
->ifname
= if_new
->ifname
;
385 set_config_state(if_old
, IFC_RELOAD
);
391 interface_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
392 struct vlist_node
*node_old
)
394 struct interface
*if_old
= container_of(node_old
, struct interface
, node
);
395 struct interface
*if_new
= container_of(node_new
, struct interface
, node
);
397 if (node_old
&& node_new
)
398 interface_change_config(if_old
, if_new
);
400 set_config_state(if_old
, IFC_REMOVE
);
402 proto_init_interface(if_new
, if_new
->config
);
403 interface_ip_init(if_new
);
404 netifd_ubus_add_interface(if_new
);
410 interface_init_list(void)
412 vlist_init(&interfaces
, avl_strcmp
, interface_update
,
413 struct interface
, node
, name
);
414 interfaces
.keep_old
= true;
415 interfaces
.no_delete
= true;