387d3491dc602118f7c761e5563810a12c59e253
[project/netifd.git] / interface.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include "netifd.h"
6 #include "device.h"
7 #include "interface.h"
8 #include "interface-ip.h"
9 #include "proto.h"
10 #include "ubus.h"
11 #include "config.h"
12
13 static LIST_HEAD(interfaces);
14
15 enum {
16 IFACE_ATTR_IFNAME,
17 IFACE_ATTR_PROTO,
18 IFACE_ATTR_AUTO,
19 IFACE_ATTR_MAX
20 };
21
22 static const union config_param_info iface_attr_info[IFACE_ATTR_MAX] = {
23 [IFACE_ATTR_IFNAME].type = BLOBMSG_TYPE_STRING,
24 };
25
26 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
27 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
28 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
29 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
30 };
31
32 const struct config_param_list interface_attr_list = {
33 .n_params = IFACE_ATTR_MAX,
34 .params = iface_attrs,
35 .info = iface_attr_info,
36 };
37
38 static void
39 clear_interface_errors(struct interface *iface)
40 {
41 struct interface_error *error, *tmp;
42
43 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
44 list_del(&error->list);
45 free(error);
46 }
47 }
48
49 void interface_add_error(struct interface *iface, const char *subsystem,
50 const char *code, const char **data, int n_data)
51 {
52 struct interface_error *error;
53 int i, len = 0;
54 int *datalen = NULL;
55 char *dest;
56
57 if (n_data) {
58 len = n_data * sizeof(char *);
59 datalen = alloca(len);
60 for (i = 0; i < n_data; i++) {
61 datalen[i] = strlen(data[i]) + 1;
62 len += datalen[i];
63 }
64 }
65
66 error = calloc(1, sizeof(*error) + sizeof(char *) + len);
67 if (!error)
68 return;
69
70 list_add_tail(&error->list, &iface->errors);
71 error->subsystem = subsystem;
72 error->code = code;
73
74 dest = (char *) &error->data[n_data + 1];
75 for (i = 0; i < n_data; i++) {
76 error->data[i] = dest;
77 memcpy(dest, data[i], datalen[i]);
78 dest += datalen[i];
79 }
80 error->data[n_data] = NULL;
81 }
82
83 static void
84 interface_event(struct interface *iface, enum interface_event ev)
85 {
86 /* TODO */
87 }
88
89 static void
90 mark_interface_down(struct interface *iface)
91 {
92 interface_del_all_routes(iface);
93 interface_del_ctx_addr(iface, NULL);
94 device_release(&iface->main_dev);
95 iface->state = IFS_DOWN;
96 }
97
98 static int
99 __interface_set_up(struct interface *iface)
100 {
101 int ret;
102
103 if (iface->state != IFS_DOWN)
104 return 0;
105
106 ret = device_claim(&iface->main_dev);
107 if (ret)
108 return ret;
109
110 iface->state = IFS_SETUP;
111 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
112 if (ret) {
113 mark_interface_down(iface);
114 return ret;
115 }
116
117 return 0;
118
119 }
120
121 static void
122 __interface_set_down(struct interface *iface, bool force)
123 {
124 clear_interface_errors(iface);
125
126 if (iface->state == IFS_DOWN ||
127 iface->state == IFS_TEARDOWN)
128 return;
129
130 iface->state = IFS_TEARDOWN;
131 interface_event(iface, IFEV_DOWN);
132
133 interface_del_all_routes(iface);
134 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
135 }
136
137 static void
138 interface_cb(struct device_user *dep, enum device_event ev)
139 {
140 struct interface *iface;
141 bool new_state;
142
143 iface = container_of(dep, struct interface, main_dev);
144 switch (ev) {
145 case DEV_EVENT_ADD:
146 new_state = true;
147 break;
148 case DEV_EVENT_REMOVE:
149 new_state = false;
150 break;
151 default:
152 return;
153 }
154
155 if (iface->active == new_state)
156 return;
157
158 iface->active = new_state;
159
160 if (new_state) {
161 if (iface->autostart && !config_init)
162 interface_set_up(iface);
163 } else
164 __interface_set_down(iface, true);
165 }
166
167 static void
168 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
169 {
170 struct interface *iface = state->iface;
171
172 switch (ev) {
173 case IFPEV_UP:
174 if (iface->state != IFS_SETUP)
175 return;
176
177 iface->state = IFS_UP;
178 interface_event(iface, IFEV_UP);
179 break;
180 case IFPEV_DOWN:
181 if (iface->state == IFS_DOWN)
182 return;
183
184 mark_interface_down(iface);
185 break;
186 }
187 }
188
189 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
190 {
191 if (iface->proto) {
192 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, true);
193 iface->proto->free(iface->proto);
194 iface->proto = NULL;
195 }
196 iface->state = IFS_DOWN;
197 iface->proto = state;
198 if (!state)
199 return;
200
201 state->proto_event = interface_proto_cb;
202 state->iface = iface;
203 }
204
205 struct interface *
206 interface_alloc(const char *name, struct blob_attr *attr)
207 {
208 struct interface *iface;
209 struct blob_attr *tb[IFACE_ATTR_MAX];
210 struct blob_attr *cur;
211 struct device *dev;
212 const char *proto_name = NULL;
213
214 iface = interface_get(name);
215 if (iface)
216 return iface;
217
218 iface = calloc(1, sizeof(*iface));
219 iface->main_dev.cb = interface_cb;
220 iface->l3_iface = &iface->main_dev;
221 strncpy(iface->name, name, sizeof(iface->name) - 1);
222 list_add_tail(&iface->list, &interfaces);
223 INIT_LIST_HEAD(&iface->errors);
224 INIT_LIST_HEAD(&iface->address);
225 INIT_LIST_HEAD(&iface->routes);
226
227 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
228 blob_data(attr), blob_len(attr));
229
230 if ((cur = tb[IFACE_ATTR_PROTO]))
231 proto_name = blobmsg_data(cur);
232
233 proto_attach_interface(iface, proto_name);
234
235 if ((cur = tb[IFACE_ATTR_IFNAME])) {
236 dev = device_get(blobmsg_data(cur), true);
237 if (dev)
238 device_add_user(&iface->main_dev, dev);
239 }
240
241 if ((cur = tb[IFACE_ATTR_AUTO]))
242 iface->autostart = blobmsg_get_bool(cur);
243 else
244 iface->autostart = true;
245
246 netifd_ubus_add_interface(iface);
247 config_set_state(&iface->config, attr);
248
249 return iface;
250 }
251
252 void
253 interface_free(struct interface *iface)
254 {
255 netifd_ubus_remove_interface(iface);
256 list_del(&iface->list);
257 if (iface->proto->free)
258 iface->proto->free(iface->proto);
259 free(iface);
260 }
261
262 struct interface *
263 interface_get(const char *name)
264 {
265 struct interface *iface;
266
267 list_for_each_entry(iface, &interfaces, list) {
268 if (!strcmp(iface->name, name))
269 return iface;
270 }
271 return NULL;
272 }
273
274 void
275 interface_remove_link(struct interface *iface, struct device *dev)
276 {
277 struct device *mdev = iface->main_dev.dev;
278
279 if (mdev && mdev->hotplug_ops) {
280 mdev->hotplug_ops->del(mdev, dev);
281 return;
282 }
283
284 device_remove_user(&iface->main_dev);
285 }
286
287 int
288 interface_add_link(struct interface *iface, struct device *dev)
289 {
290 struct device *mdev = iface->main_dev.dev;
291
292 if (mdev && mdev->hotplug_ops)
293 return mdev->hotplug_ops->add(mdev, dev);
294
295 if (iface->main_dev.dev)
296 interface_remove_link(iface, NULL);
297
298 device_add_user(&iface->main_dev, dev);
299
300 return 0;
301 }
302
303 int
304 interface_set_up(struct interface *iface)
305 {
306 iface->autostart = true;
307
308 if (!iface->active) {
309 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
310 return -1;
311 }
312
313 if (iface->state != IFS_DOWN)
314 return 0;
315
316 return __interface_set_up(iface);
317 }
318
319 int
320 interface_set_down(struct interface *iface)
321 {
322 iface->autostart = false;
323 __interface_set_down(iface, false);
324
325 return 0;
326 }
327
328 void
329 interface_start_pending(void)
330 {
331 struct interface *iface;
332
333 list_for_each_entry(iface, &interfaces, list) {
334 if (iface->active && iface->autostart)
335 interface_set_up(iface);
336 }
337 }