move ip related functions and data structures to a separate header file
[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_ctx_addr(iface, NULL);
93 device_release(&iface->main_dev);
94 iface->state = IFS_DOWN;
95 }
96
97 static int
98 __interface_set_up(struct interface *iface)
99 {
100 int ret;
101
102 if (iface->state != IFS_DOWN)
103 return 0;
104
105 ret = device_claim(&iface->main_dev);
106 if (ret)
107 return ret;
108
109 iface->state = IFS_SETUP;
110 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
111 if (ret) {
112 mark_interface_down(iface);
113 return ret;
114 }
115
116 return 0;
117
118 }
119
120 static void
121 __interface_set_down(struct interface *iface, bool force)
122 {
123 clear_interface_errors(iface);
124
125 if (iface->state == IFS_DOWN ||
126 iface->state == IFS_TEARDOWN)
127 return;
128
129 iface->state = IFS_TEARDOWN;
130 interface_event(iface, IFEV_DOWN);
131
132 interface_del_all_routes(iface);
133 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
134 }
135
136 static void
137 interface_cb(struct device_user *dep, enum device_event ev)
138 {
139 struct interface *iface;
140 bool new_state;
141
142 iface = container_of(dep, struct interface, main_dev);
143 switch (ev) {
144 case DEV_EVENT_ADD:
145 new_state = true;
146 break;
147 case DEV_EVENT_REMOVE:
148 new_state = false;
149 break;
150 default:
151 return;
152 }
153
154 if (iface->active == new_state)
155 return;
156
157 iface->active = new_state;
158
159 if (new_state) {
160 if (iface->autostart && !config_init)
161 interface_set_up(iface);
162 } else
163 __interface_set_down(iface, true);
164 }
165
166 static void
167 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
168 {
169 struct interface *iface = state->iface;
170
171 switch (ev) {
172 case IFPEV_UP:
173 if (iface->state != IFS_SETUP)
174 return;
175
176 iface->state = IFS_UP;
177 interface_event(iface, IFEV_UP);
178 break;
179 case IFPEV_DOWN:
180 if (iface->state == IFS_DOWN)
181 return;
182
183 mark_interface_down(iface);
184 break;
185 }
186 }
187
188 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
189 {
190 if (iface->proto) {
191 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, true);
192 iface->proto->free(iface->proto);
193 iface->proto = NULL;
194 }
195 iface->state = IFS_DOWN;
196 iface->proto = state;
197 if (!state)
198 return;
199
200 state->proto_event = interface_proto_cb;
201 state->iface = iface;
202 }
203
204 struct interface *
205 interface_alloc(const char *name, struct blob_attr *attr)
206 {
207 struct interface *iface;
208 struct blob_attr *tb[IFACE_ATTR_MAX];
209 struct blob_attr *cur;
210 struct device *dev;
211 const char *proto_name = NULL;
212
213 iface = interface_get(name);
214 if (iface)
215 return iface;
216
217 iface = calloc(1, sizeof(*iface));
218 iface->main_dev.cb = interface_cb;
219 iface->l3_iface = &iface->main_dev;
220 strncpy(iface->name, name, sizeof(iface->name) - 1);
221 list_add_tail(&iface->list, &interfaces);
222 INIT_LIST_HEAD(&iface->errors);
223 INIT_LIST_HEAD(&iface->address);
224 INIT_LIST_HEAD(&iface->routes);
225
226 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
227 blob_data(attr), blob_len(attr));
228
229 if ((cur = tb[IFACE_ATTR_PROTO]))
230 proto_name = blobmsg_data(cur);
231
232 proto_attach_interface(iface, proto_name);
233
234 if ((cur = tb[IFACE_ATTR_IFNAME])) {
235 dev = device_get(blobmsg_data(cur), true);
236 if (dev)
237 device_add_user(&iface->main_dev, dev);
238 }
239
240 netifd_ubus_add_interface(iface);
241
242 return iface;
243 }
244
245 void
246 interface_free(struct interface *iface)
247 {
248 netifd_ubus_remove_interface(iface);
249 list_del(&iface->list);
250 if (iface->proto->free)
251 iface->proto->free(iface->proto);
252 free(iface);
253 }
254
255 struct interface *
256 interface_get(const char *name)
257 {
258 struct interface *iface;
259
260 list_for_each_entry(iface, &interfaces, list) {
261 if (!strcmp(iface->name, name))
262 return iface;
263 }
264 return NULL;
265 }
266
267 void
268 interface_remove_link(struct interface *iface, struct device *dev)
269 {
270 struct device *mdev = iface->main_dev.dev;
271
272 if (mdev && mdev->hotplug_ops) {
273 mdev->hotplug_ops->del(mdev, dev);
274 return;
275 }
276
277 device_remove_user(&iface->main_dev);
278 }
279
280 int
281 interface_add_link(struct interface *iface, struct device *dev)
282 {
283 struct device *mdev = iface->main_dev.dev;
284
285 if (mdev && mdev->hotplug_ops)
286 return mdev->hotplug_ops->add(mdev, dev);
287
288 if (iface->main_dev.dev)
289 interface_remove_link(iface, NULL);
290
291 device_add_user(&iface->main_dev, dev);
292
293 return 0;
294 }
295
296 int
297 interface_set_up(struct interface *iface)
298 {
299 iface->autostart = true;
300
301 if (!iface->active) {
302 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
303 return -1;
304 }
305
306 if (iface->state != IFS_DOWN)
307 return 0;
308
309 return __interface_set_up(iface);
310 }
311
312 int
313 interface_set_down(struct interface *iface)
314 {
315 iface->autostart = false;
316 __interface_set_down(iface, false);
317
318 return 0;
319 }
320
321 void
322 interface_start_pending(void)
323 {
324 struct interface *iface;
325
326 list_for_each_entry(iface, &interfaces, list) {
327 if (iface->active && iface->autostart)
328 interface_set_up(iface);
329 }
330 }