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
);
191 void interface_set_proto_state(struct interface
*iface
, struct interface_proto_state
*state
)
194 interface_proto_event(iface
->proto
, PROTO_CMD_TEARDOWN
, true);
195 iface
->proto
->free(iface
->proto
);
198 iface
->state
= IFS_DOWN
;
199 iface
->proto
= state
;
203 state
->proto_event
= interface_proto_cb
;
204 state
->iface
= iface
;
208 interface_alloc(const char *name
, struct blob_attr
*attr
)
210 struct interface
*iface
;
211 struct blob_attr
*tb
[IFACE_ATTR_MAX
];
212 struct blob_attr
*cur
;
214 const char *proto_name
= NULL
;
216 iface
= interface_get(name
);
220 iface
= calloc(1, sizeof(*iface
));
221 iface
->main_dev
.cb
= interface_cb
;
222 iface
->l3_iface
= &iface
->main_dev
;
223 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
224 list_add_tail(&iface
->list
, &interfaces
);
225 INIT_LIST_HEAD(&iface
->errors
);
227 interface_ip_init(iface
);
229 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
,
230 blob_data(attr
), blob_len(attr
));
232 if ((cur
= tb
[IFACE_ATTR_PROTO
]))
233 proto_name
= blobmsg_data(cur
);
235 proto_attach_interface(iface
, proto_name
);
237 if (!(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
) &&
238 (cur
= tb
[IFACE_ATTR_IFNAME
])) {
239 dev
= device_get(blobmsg_data(cur
), true);
241 device_add_user(&iface
->main_dev
, dev
);
244 if ((cur
= tb
[IFACE_ATTR_AUTO
]))
245 iface
->autostart
= blobmsg_get_bool(cur
);
247 iface
->autostart
= true;
249 netifd_ubus_add_interface(iface
);
250 config_set_state(&iface
->config
, attr
);
256 interface_free(struct interface
*iface
)
258 netifd_ubus_remove_interface(iface
);
259 list_del(&iface
->list
);
260 if (iface
->proto
->free
)
261 iface
->proto
->free(iface
->proto
);
266 interface_get(const char *name
)
268 struct interface
*iface
;
270 list_for_each_entry(iface
, &interfaces
, list
) {
271 if (!strcmp(iface
->name
, name
))
278 interface_remove_link(struct interface
*iface
, struct device
*dev
)
280 struct device
*mdev
= iface
->main_dev
.dev
;
282 if (mdev
&& mdev
->hotplug_ops
) {
283 mdev
->hotplug_ops
->del(mdev
, dev
);
287 device_remove_user(&iface
->main_dev
);
291 interface_add_link(struct interface
*iface
, struct device
*dev
)
293 struct device
*mdev
= iface
->main_dev
.dev
;
295 if (mdev
&& mdev
->hotplug_ops
)
296 return mdev
->hotplug_ops
->add(mdev
, dev
);
298 if (iface
->main_dev
.dev
)
299 interface_remove_link(iface
, NULL
);
301 device_add_user(&iface
->main_dev
, dev
);
307 interface_set_up(struct interface
*iface
)
309 iface
->autostart
= true;
311 if (!iface
->available
) {
312 interface_add_error(iface
, "interface", "NO_DEVICE", NULL
, 0);
316 if (iface
->state
!= IFS_DOWN
)
319 return __interface_set_up(iface
);
323 interface_set_down(struct interface
*iface
)
326 list_for_each_entry(iface
, &interfaces
, list
)
327 __interface_set_down(iface
, false);
329 iface
->autostart
= false;
330 __interface_set_down(iface
, false);
337 interface_start_pending(void)
339 struct interface
*iface
;
341 list_for_each_entry(iface
, &interfaces
, list
) {
342 if (iface
->available
&& iface
->autostart
)
343 interface_set_up(iface
);