proto-shell: use calloc_a
[project/netifd.git] / proto.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17
18 #include <arpa/inet.h>
19 #include <netinet/in.h>
20
21 #include "netifd.h"
22 #include "interface.h"
23 #include "interface-ip.h"
24 #include "proto.h"
25
26 static struct avl_tree handlers;
27
28 enum {
29 OPT_IPADDR,
30 OPT_IP6ADDR,
31 OPT_NETMASK,
32 OPT_BROADCAST,
33 OPT_GATEWAY,
34 OPT_IP6GW,
35 __OPT_MAX,
36 };
37
38 static const struct blobmsg_policy proto_ip_attributes[__OPT_MAX] = {
39 [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
40 [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
41 [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
42 [OPT_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
43 [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
44 [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
45 };
46
47 static const union config_param_info proto_ip_attr_info[__OPT_MAX] = {
48 [OPT_IPADDR] = { .type = BLOBMSG_TYPE_STRING },
49 [OPT_IP6ADDR] = { .type = BLOBMSG_TYPE_STRING },
50 };
51
52 const struct config_param_list proto_ip_attr = {
53 .n_params = __OPT_MAX,
54 .params = proto_ip_attributes,
55 .info = proto_ip_attr_info,
56 };
57
58 enum {
59 ADDR_IPADDR,
60 ADDR_MASK,
61 ADDR_BROADCAST,
62 ADDR_PTP,
63 __ADDR_MAX
64 };
65
66 static const struct blobmsg_policy proto_ip_addr[__ADDR_MAX] = {
67 [ADDR_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING },
68 [ADDR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING },
69 [ADDR_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
70 [ADDR_PTP] = { .name = "ptp", .type = BLOBMSG_TYPE_STRING },
71 };
72
73 static struct device_addr *
74 alloc_device_addr(bool v6, bool ext)
75 {
76 struct device_addr *addr;
77
78 addr = calloc(1, sizeof(*addr));
79 addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
80 if (ext)
81 addr->flags |= DEVADDR_EXTERNAL;
82
83 return addr;
84 }
85
86 static bool
87 parse_addr(struct interface *iface, const char *str, bool v6, int mask,
88 bool ext, uint32_t broadcast)
89 {
90 struct device_addr *addr;
91 int af = v6 ? AF_INET6 : AF_INET;
92
93 addr = alloc_device_addr(v6, ext);
94 if (!addr)
95 return false;
96
97 addr->mask = mask;
98 if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
99 interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
100 free(addr);
101 return false;
102 }
103
104 if (broadcast)
105 addr->broadcast = broadcast;
106
107 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
108 return true;
109 }
110
111 static int
112 parse_static_address_option(struct interface *iface, struct blob_attr *attr,
113 bool v6, int netmask, bool ext, uint32_t broadcast)
114 {
115 struct blob_attr *cur;
116 int n_addr = 0;
117 int rem;
118
119 blobmsg_for_each_attr(cur, attr, rem) {
120 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
121 return -1;
122
123 n_addr++;
124 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
125 broadcast))
126 return -1;
127 }
128
129 return n_addr;
130 }
131
132 static struct device_addr *
133 parse_address_item(struct blob_attr *attr, bool v6, bool ext)
134 {
135 struct device_addr *addr;
136 struct blob_attr *tb[__ADDR_MAX];
137 struct blob_attr *cur;
138
139 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
140 return NULL;
141
142 addr = alloc_device_addr(v6, ext);
143 if (!addr)
144 return NULL;
145
146 blobmsg_parse(proto_ip_addr, __ADDR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
147
148 addr->mask = v6 ? 128 : 32;
149 if ((cur = tb[ADDR_MASK])) {
150 unsigned int new_mask;
151
152 new_mask = parse_netmask_string(blobmsg_data(cur), v6);
153 if (new_mask > addr->mask)
154 goto error;
155
156 addr->mask = new_mask;
157 }
158
159 cur = tb[ADDR_IPADDR];
160 if (!cur)
161 goto error;
162
163 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(cur), &addr->addr))
164 goto error;
165
166 if (!v6) {
167 if ((cur = tb[ADDR_BROADCAST]) &&
168 !inet_pton(AF_INET, blobmsg_data(cur), &addr->broadcast))
169 goto error;
170 if ((cur = tb[ADDR_PTP]) &&
171 !inet_pton(AF_INET, blobmsg_data(cur), &addr->point_to_point))
172 goto error;
173 }
174
175 return addr;
176
177 error:
178 free(addr);
179 return NULL;
180 }
181
182 static int
183 parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6,
184 bool ext)
185 {
186 struct device_addr *addr;
187 struct blob_attr *cur;
188 int n_addr = 0;
189 int rem;
190
191 blobmsg_for_each_attr(cur, attr, rem) {
192 addr = parse_address_item(cur, v6, ext);
193 if (!addr)
194 return -1;
195
196 n_addr++;
197 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
198 }
199
200 return n_addr;
201 }
202
203 static bool
204 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
205 {
206 struct device_route *route;
207 const char *str = blobmsg_data(attr);
208 int af = v6 ? AF_INET6 : AF_INET;
209
210 route = calloc(1, sizeof(*route));
211 if (!inet_pton(af, str, &route->nexthop)) {
212 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
213 free(route);
214 return false;
215 }
216
217 route->mask = 0;
218 route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
219 vlist_add(&iface->proto_ip.route, &route->node, &route->flags);
220
221 return true;
222 }
223
224 int
225 proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
226 {
227 struct blob_attr *tb[__OPT_MAX];
228 struct blob_attr *cur;
229 const char *error;
230 unsigned int netmask = 32;
231 int n_v4 = 0, n_v6 = 0;
232 struct in_addr bcast = {};
233
234 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
235
236 if ((cur = tb[OPT_NETMASK])) {
237 netmask = parse_netmask_string(blobmsg_data(cur), false);
238 if (netmask > 32) {
239 error = "INVALID_NETMASK";
240 goto error;
241 }
242 }
243
244 if ((cur = tb[OPT_BROADCAST])) {
245 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
246 error = "INVALID_BROADCAST";
247 goto error;
248 }
249 }
250
251 if ((cur = tb[OPT_IPADDR]))
252 n_v4 = parse_static_address_option(iface, cur, false,
253 netmask, false, bcast.s_addr);
254
255 if ((cur = tb[OPT_IP6ADDR]))
256 n_v6 = parse_static_address_option(iface, cur, true,
257 netmask, false, 0);
258
259 if (!n_v4 && !n_v6) {
260 error = "NO_ADDRESS";
261 goto error;
262 }
263
264 if (n_v4 < 0 || n_v6 < 0)
265 goto out;
266
267 if ((cur = tb[OPT_GATEWAY])) {
268 if (n_v4 && !parse_gateway_option(iface, cur, false))
269 goto out;
270 }
271
272 if ((cur = tb[OPT_IP6GW])) {
273 if (n_v6 && !parse_gateway_option(iface, cur, true))
274 goto out;
275 }
276
277 return 0;
278
279 error:
280 interface_add_error(iface, "proto", error, NULL, 0);
281 out:
282 return -1;
283 }
284
285 int
286 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
287 {
288 struct blob_attr *tb[__OPT_MAX];
289 struct blob_attr *cur;
290 const char *error;
291 int n_v4 = 0, n_v6 = 0;
292
293 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
294
295 if ((cur = tb[OPT_IPADDR]))
296 n_v4 = parse_address_list(iface, cur, false, ext);
297
298 if ((cur = tb[OPT_IP6ADDR]))
299 n_v6 = parse_address_list(iface, cur, true, ext);
300
301 if (!n_v4 && !n_v6) {
302 error = "NO_ADDRESS";
303 goto error;
304 }
305
306 if (n_v4 < 0 || n_v6 < 0)
307 goto out;
308
309 if ((cur = tb[OPT_GATEWAY])) {
310 if (n_v4 && !parse_gateway_option(iface, cur, false))
311 goto out;
312 }
313
314 if ((cur = tb[OPT_IP6GW])) {
315 if (n_v6 && !parse_gateway_option(iface, cur, true))
316 goto out;
317 }
318
319 return 0;
320
321 error:
322 interface_add_error(iface, "proto", error, NULL, 0);
323 out:
324 return -1;
325 }
326
327 void add_proto_handler(struct proto_handler *p)
328 {
329 if (!handlers.comp)
330 avl_init(&handlers, avl_strcmp, false, NULL);
331
332 if (p->avl.key)
333 return;
334
335 p->avl.key = p->name;
336 avl_insert(&handlers, &p->avl);
337 }
338
339 static void
340 default_proto_free(struct interface_proto_state *proto)
341 {
342 free(proto);
343 }
344
345 static int
346 invalid_proto_handler(struct interface_proto_state *proto,
347 enum interface_proto_cmd cmd, bool force)
348 {
349 return -1;
350 }
351
352 static int
353 no_proto_handler(struct interface_proto_state *proto,
354 enum interface_proto_cmd cmd, bool force)
355 {
356 return 0;
357 }
358
359 static struct interface_proto_state *
360 default_proto_attach(const struct proto_handler *h,
361 struct interface *iface, struct blob_attr *attr)
362 {
363 struct interface_proto_state *proto;
364
365 proto = calloc(1, sizeof(*proto));
366 proto->free = default_proto_free;
367 proto->cb = no_proto_handler;
368
369 return proto;
370 }
371
372 static const struct proto_handler no_proto = {
373 .name = "none",
374 .flags = PROTO_FLAG_IMMEDIATE,
375 .attach = default_proto_attach,
376 };
377
378 static const struct proto_handler *
379 get_proto_handler(const char *name)
380 {
381 struct proto_handler *proto;
382
383 if (!strcmp(name, "none"))
384 return &no_proto;
385
386 if (!handlers.comp)
387 return NULL;
388
389 return avl_find_element(&handlers, name, proto, avl);
390 }
391
392 void
393 proto_dump_handlers(struct blob_buf *b)
394 {
395 struct proto_handler *p;
396 void *c;
397
398 avl_for_each_element(&handlers, p, avl) {
399 c = blobmsg_open_table(b, p->name);
400 blobmsg_add_u8(b, "no_device", !!(p->flags & PROTO_FLAG_NODEV));
401 blobmsg_close_table(b, c);
402 }
403 }
404
405 void
406 proto_init_interface(struct interface *iface, struct blob_attr *attr)
407 {
408 const struct proto_handler *proto = iface->proto_handler;
409 struct interface_proto_state *state = NULL;
410
411 if (!proto)
412 proto = &no_proto;
413
414 state = proto->attach(proto, iface, attr);
415 if (!state) {
416 state = no_proto.attach(&no_proto, iface, attr);
417 state->cb = invalid_proto_handler;
418 }
419
420 state->handler = proto;
421 interface_set_proto_state(iface, state);
422 }
423
424 void
425 proto_attach_interface(struct interface *iface, const char *proto_name)
426 {
427 const struct proto_handler *proto = &no_proto;
428
429 if (proto_name) {
430 proto = get_proto_handler(proto_name);
431 if (!proto) {
432 interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
433 proto = &no_proto;
434 }
435 }
436
437 iface->proto_handler = proto;
438 }
439
440 int
441 interface_proto_event(struct interface_proto_state *proto,
442 enum interface_proto_cmd cmd, bool force)
443 {
444 enum interface_proto_event ev;
445 int ret;
446
447 ret = proto->cb(proto, cmd, force);
448 if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
449 goto out;
450
451 switch(cmd) {
452 case PROTO_CMD_SETUP:
453 ev = IFPEV_UP;
454 break;
455 case PROTO_CMD_TEARDOWN:
456 ev = IFPEV_DOWN;
457 break;
458 default:
459 return -EINVAL;
460 }
461 proto->proto_event(proto, ev);
462
463 out:
464 return ret;
465 }