8 #include "interface-ip.h"
13 static LIST_HEAD(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_proto_cb(struct interface_proto_state
*state
, enum interface_proto_event ev
)
172 struct interface
*iface
= state
->iface
;
176 if (iface
->state
!= IFS_SETUP
)
179 iface
->state
= IFS_UP
;
180 interface_event(iface
, IFEV_UP
);
183 if (iface
->state
== IFS_DOWN
)
186 mark_interface_down(iface
);
188 case IFPEV_LINK_LOST
:
189 if (iface
->state
!= IFS_UP
)
192 iface
->state
= IFS_SETUP
;
193 interface_event(iface
, IFEV_DOWN
);
198 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
201 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, true);
202 iface
->proto
->free(iface
->proto
);
205 iface
->state
= IFS_DOWN
;
206 iface
->proto
= state
;
210 state
->proto_event
= interface_proto_cb
;
211 state
->iface
= iface
;
215 interface_alloc(const char *name
, struct blob_attr
*attr
)
217 struct interface
*iface
;
218 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
219 struct blob_attr
*cur
;
221 const char *proto_name
= NULL
;
223 iface
= interface_get(name
);
227 iface
= calloc(1, sizeof(*iface
));
228 iface
->main_dev
.cb
= interface_cb
;
229 iface
->l3_dev
= &iface
->main_dev
;
230 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
231 list_add_tail(&iface
->list
, &interfaces
);
232 INIT_LIST_HEAD(&iface
->errors
);
234 interface_ip_init(iface
);
236 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
237 blob_data(attr
), blob_len(attr
));
239 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
240 proto_name
= blobmsg_data(cur
);
242 proto_attach_interface(iface
, proto_name
);
244 if (!(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
) &&
245 (cur
= tb
[IFACE_ATTR_IFNAME
])) {
246 dev
= device_get(blobmsg_data(cur
), true);
248 device_add_user(&iface
->main_dev
, dev
);
251 if ((cur
= tb
[IFACE_ATTR_AUTO
]))
252 iface
->autostart
= blobmsg_get_bool(cur
);
254 iface
->autostart
= true;
256 netifd_ubus_add_interface(iface
);
257 config_set_state(&iface
->config
, attr
);
263 interface_free(struct interface
*iface
)
265 netifd_ubus_remove_interface(iface
);
266 list_del(&iface
->list
);
267 if (iface
->proto
->free
)
268 iface
->proto
->free(iface
->proto
);
273 interface_get(const char *name
)
275 struct interface
*iface
;
277 list_for_each_entry(iface
, &interfaces
, list
) {
278 if (!strcmp(iface
->name
, name
))
285 interface_remove_link(struct interface
*iface
, struct device
*dev
)
287 struct device
*mdev
= iface
->main_dev
.dev
;
289 if (mdev
&& mdev
->hotplug_ops
) {
290 mdev
->hotplug_ops
->del(mdev
, dev
);
294 device_remove_user(&iface
->main_dev
);
298 interface_add_link(struct interface
*iface
, struct device
*dev
)
300 struct device
*mdev
= iface
->main_dev
.dev
;
302 if (mdev
&& mdev
->hotplug_ops
)
303 return mdev
->hotplug_ops
->add(mdev
, dev
);
305 if (iface
->main_dev
.dev
)
306 interface_remove_link(iface
, NULL
);
308 device_add_user(&iface
->main_dev
, dev
);
314 interface_set_up(struct interface
*iface
)
316 iface
->autostart
= true;
318 if (!iface
->available
) {
319 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
323 if (iface
->state
!= IFS_DOWN
)
326 return __interface_set_up(iface
);
330 interface_set_down(struct interface
*iface
)
333 list_for_each_entry(iface
, &interfaces
, list
)
334 __interface_set_down(iface
, false);
336 iface
->autostart
= false;
337 __interface_set_down(iface
, false);
344 interface_start_pending(void)
346 struct interface
*iface
;
348 list_for_each_entry(iface
, &interfaces
, list
) {
349 if (iface
->available
&& iface
->autostart
)
350 interface_set_up(iface
);