Add option to define target routing table for protocol routes.
[project/netifd.git] / proto.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2012 Steven Barth <steven@midlink.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18
19 #include <arpa/inet.h>
20 #include <netinet/in.h>
21
22 #include "netifd.h"
23 #include "system.h"
24 #include "interface.h"
25 #include "interface-ip.h"
26 #include "proto.h"
27
28 static struct avl_tree handlers;
29
30 enum {
31 OPT_IPADDR,
32 OPT_IP6ADDR,
33 OPT_NETMASK,
34 OPT_BROADCAST,
35 OPT_GATEWAY,
36 OPT_IP6GW,
37 OPT_IP6PREFIX,
38 __OPT_MAX,
39 };
40
41 static const struct blobmsg_policy proto_ip_attributes[__OPT_MAX] = {
42 [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
43 [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
44 [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
45 [OPT_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
46 [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
47 [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
48 [OPT_IP6PREFIX] = { .name = "ip6prefix", .type = BLOBMSG_TYPE_ARRAY },
49 };
50
51 static const union config_param_info proto_ip_attr_info[__OPT_MAX] = {
52 [OPT_IPADDR] = { .type = BLOBMSG_TYPE_STRING },
53 [OPT_IP6ADDR] = { .type = BLOBMSG_TYPE_STRING },
54 [OPT_IP6PREFIX] = { .type = BLOBMSG_TYPE_STRING },
55 };
56
57 const struct config_param_list proto_ip_attr = {
58 .n_params = __OPT_MAX,
59 .params = proto_ip_attributes,
60 .info = proto_ip_attr_info,
61 };
62
63 enum {
64 ADDR_IPADDR,
65 ADDR_MASK,
66 ADDR_BROADCAST,
67 ADDR_PTP,
68 ADDR_PREFERRED,
69 ADDR_VALID,
70 ADDR_OFFLINK,
71 __ADDR_MAX
72 };
73
74 static const struct blobmsg_policy proto_ip_addr[__ADDR_MAX] = {
75 [ADDR_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING },
76 [ADDR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING },
77 [ADDR_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
78 [ADDR_PTP] = { .name = "ptp", .type = BLOBMSG_TYPE_STRING },
79 [ADDR_PREFERRED] = { .name = "preferred", .type = BLOBMSG_TYPE_INT32 },
80 [ADDR_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
81 [ADDR_OFFLINK] = { .name = "offlink", .type = BLOBMSG_TYPE_BOOL },
82 };
83
84 static struct device_addr *
85 alloc_device_addr(bool v6, bool ext)
86 {
87 struct device_addr *addr;
88
89 addr = calloc(1, sizeof(*addr));
90 addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
91 if (ext)
92 addr->flags |= DEVADDR_EXTERNAL;
93
94 return addr;
95 }
96
97 static bool
98 parse_addr(struct interface *iface, const char *str, bool v6, int mask,
99 bool ext, uint32_t broadcast)
100 {
101 struct device_addr *addr;
102 int af = v6 ? AF_INET6 : AF_INET;
103
104 addr = alloc_device_addr(v6, ext);
105 if (!addr)
106 return false;
107
108 addr->mask = mask;
109 if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
110 interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
111 free(addr);
112 return false;
113 }
114
115 if (broadcast)
116 addr->broadcast = broadcast;
117
118 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
119 return true;
120 }
121
122 static int
123 parse_static_address_option(struct interface *iface, struct blob_attr *attr,
124 bool v6, int netmask, bool ext, uint32_t broadcast)
125 {
126 struct blob_attr *cur;
127 int n_addr = 0;
128 int rem;
129
130 blobmsg_for_each_attr(cur, attr, rem) {
131 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
132 return -1;
133
134 n_addr++;
135 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
136 broadcast))
137 return -1;
138 }
139
140 return n_addr;
141 }
142
143 static struct device_addr *
144 parse_address_item(struct blob_attr *attr, bool v6, bool ext)
145 {
146 struct device_addr *addr;
147 struct blob_attr *tb[__ADDR_MAX];
148 struct blob_attr *cur;
149
150 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
151 return NULL;
152
153 addr = alloc_device_addr(v6, ext);
154 if (!addr)
155 return NULL;
156
157 blobmsg_parse(proto_ip_addr, __ADDR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
158
159 addr->mask = v6 ? 128 : 32;
160 if ((cur = tb[ADDR_MASK])) {
161 unsigned int new_mask;
162
163 new_mask = parse_netmask_string(blobmsg_data(cur), v6);
164 if (new_mask > addr->mask)
165 goto error;
166
167 addr->mask = new_mask;
168 }
169
170 cur = tb[ADDR_IPADDR];
171 if (!cur)
172 goto error;
173
174 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(cur), &addr->addr))
175 goto error;
176
177 if ((cur = tb[ADDR_OFFLINK]) && blobmsg_get_bool(cur))
178 addr->flags |= DEVADDR_OFFLINK;
179
180 if (!v6) {
181 if ((cur = tb[ADDR_BROADCAST]) &&
182 !inet_pton(AF_INET, blobmsg_data(cur), &addr->broadcast))
183 goto error;
184 if ((cur = tb[ADDR_PTP]) &&
185 !inet_pton(AF_INET, blobmsg_data(cur), &addr->point_to_point))
186 goto error;
187 } else {
188 time_t now = system_get_rtime();
189 if ((cur = tb[ADDR_PREFERRED])) {
190 uint32_t preferred = blobmsg_get_u32(cur);
191 if (preferred < UINT32_MAX)
192 addr->preferred_until = now + preferred;
193 }
194
195 if ((cur = tb[ADDR_VALID])) {
196 uint32_t valid = blobmsg_get_u32(cur);
197 if (valid < UINT32_MAX)
198 addr->valid_until = now + valid;
199
200 }
201
202 if (addr->valid_until) {
203 if (!addr->preferred_until)
204 addr->preferred_until = addr->valid_until;
205 else if (addr->preferred_until > addr->valid_until)
206 goto error;
207 }
208 }
209
210 return addr;
211
212 error:
213 free(addr);
214 return NULL;
215 }
216
217 static int
218 parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6,
219 bool ext)
220 {
221 struct device_addr *addr;
222 struct blob_attr *cur;
223 int n_addr = 0;
224 int rem;
225
226 blobmsg_for_each_attr(cur, attr, rem) {
227 addr = parse_address_item(cur, v6, ext);
228 if (!addr)
229 return -1;
230
231 n_addr++;
232 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
233 }
234
235 return n_addr;
236 }
237
238 static bool
239 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
240 {
241 struct device_route *route;
242 const char *str = blobmsg_data(attr);
243 int af = v6 ? AF_INET6 : AF_INET;
244
245 route = calloc(1, sizeof(*route));
246 if (!inet_pton(af, str, &route->nexthop)) {
247 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
248 free(route);
249 return false;
250 }
251
252 route->mask = 0;
253 route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
254
255 unsigned int table = (v6) ? iface->ip6table : iface->ip4table;
256 if (table) {
257 route->table = table;
258 route->flags |= DEVROUTE_SRCTABLE;
259 }
260
261 vlist_add(&iface->proto_ip.route, &route->node, route);
262
263 return true;
264 }
265
266 static bool
267 parse_prefix_option(struct interface *iface, const char *str, size_t len)
268 {
269 char buf[128] = {0}, *saveptr;
270 if (len > sizeof(buf))
271 return false;
272
273 memcpy(buf, str, len);
274 char *addrstr = strtok_r(buf, "/", &saveptr);
275 if (!addrstr)
276 return false;
277
278 char *lengthstr = strtok_r(NULL, ",", &saveptr);
279 if (!lengthstr)
280 return false;
281
282 char *prefstr = strtok_r(NULL, ",", &saveptr);
283 char *validstr = (!prefstr) ? NULL : strtok_r(NULL, ",", &saveptr);
284 char *addstr = (!validstr) ? NULL : strtok_r(NULL, ",", &saveptr);
285
286 uint32_t pref = (!prefstr) ? 0 : strtoul(prefstr, NULL, 10);
287 uint32_t valid = (!validstr) ? 0 : strtoul(validstr, NULL, 10);
288
289 uint8_t length = strtoul(lengthstr, NULL, 10), excl_length = 0;
290 if (length < 1 || length > 64)
291 return false;
292
293 struct in6_addr addr, excluded, *excludedp = NULL;
294 if (inet_pton(AF_INET6, addrstr, &addr) < 1)
295 return false;
296
297 for (; addstr; addstr = strtok_r(NULL, ",", &saveptr)) {
298 char *key = NULL, *val = NULL, *addsaveptr;
299 if (!(key = strtok_r(addstr, "=", &addsaveptr)) ||
300 !(val = strtok_r(NULL, ",", &addsaveptr)))
301 continue;
302
303 if (!strcmp(key, "excluded")) {
304 char *sep = strchr(val, '/');
305 if (!sep)
306 return false;
307
308 *sep = 0;
309 excl_length = atoi(sep + 1);
310
311 if (inet_pton(AF_INET6, val, &excluded) < 1)
312 return false;
313
314 excludedp = &excluded;
315 }
316
317 }
318
319
320
321
322 time_t now = system_get_rtime();
323 time_t preferred_until = 0;
324 if (prefstr && pref != 0xffffffffU)
325 preferred_until = pref + now;
326
327 time_t valid_until = 0;
328 if (validstr && valid != 0xffffffffU)
329 valid_until = valid + now;
330
331 interface_ip_add_device_prefix(iface, &addr, length,
332 valid_until, preferred_until,
333 excludedp, excl_length);
334 return true;
335 }
336
337 static int
338 parse_prefix_list(struct interface *iface, struct blob_attr *attr)
339 {
340 struct blob_attr *cur;
341 int n_addr = 0;
342 int rem;
343
344 blobmsg_for_each_attr(cur, attr, rem) {
345 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
346 return -1;
347
348 n_addr++;
349 if (!parse_prefix_option(iface, blobmsg_data(cur),
350 blobmsg_data_len(cur)))
351 return -1;
352 }
353
354 return n_addr;
355 }
356
357 int
358 proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
359 {
360 struct blob_attr *tb[__OPT_MAX];
361 struct blob_attr *cur;
362 const char *error;
363 unsigned int netmask = 32;
364 int n_v4 = 0, n_v6 = 0;
365 struct in_addr bcast = {};
366
367 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
368
369 if ((cur = tb[OPT_NETMASK])) {
370 netmask = parse_netmask_string(blobmsg_data(cur), false);
371 if (netmask > 32) {
372 error = "INVALID_NETMASK";
373 goto error;
374 }
375 }
376
377 if ((cur = tb[OPT_BROADCAST])) {
378 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
379 error = "INVALID_BROADCAST";
380 goto error;
381 }
382 }
383
384 if ((cur = tb[OPT_IPADDR]))
385 n_v4 = parse_static_address_option(iface, cur, false,
386 netmask, false, bcast.s_addr);
387
388 if ((cur = tb[OPT_IP6ADDR]))
389 n_v6 = parse_static_address_option(iface, cur, true,
390 128, false, 0);
391
392 if ((cur = tb[OPT_IP6PREFIX]))
393 if (parse_prefix_list(iface, cur) < 0)
394 goto out;
395
396 if (n_v4 < 0 || n_v6 < 0)
397 goto out;
398
399 if ((cur = tb[OPT_GATEWAY])) {
400 if (n_v4 && !parse_gateway_option(iface, cur, false))
401 goto out;
402 }
403
404 if ((cur = tb[OPT_IP6GW])) {
405 if (n_v6 && !parse_gateway_option(iface, cur, true))
406 goto out;
407 }
408
409 return 0;
410
411 error:
412 interface_add_error(iface, "proto", error, NULL, 0);
413 out:
414 return -1;
415 }
416
417 int
418 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
419 {
420 struct blob_attr *tb[__OPT_MAX];
421 struct blob_attr *cur;
422 int n_v4 = 0, n_v6 = 0;
423
424 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
425
426 if ((cur = tb[OPT_IPADDR]))
427 n_v4 = parse_address_list(iface, cur, false, ext);
428
429 if ((cur = tb[OPT_IP6ADDR]))
430 n_v6 = parse_address_list(iface, cur, true, ext);
431
432 if ((cur = tb[OPT_IP6PREFIX]))
433 if (parse_prefix_list(iface, cur) < 0)
434 goto out;
435
436 if (n_v4 < 0 || n_v6 < 0)
437 goto out;
438
439 if ((cur = tb[OPT_GATEWAY])) {
440 if (n_v4 && !parse_gateway_option(iface, cur, false))
441 goto out;
442 }
443
444 if ((cur = tb[OPT_IP6GW])) {
445 if (n_v6 && !parse_gateway_option(iface, cur, true))
446 goto out;
447 }
448
449 return 0;
450
451 out:
452 return -1;
453 }
454
455 void add_proto_handler(struct proto_handler *p)
456 {
457 if (!handlers.comp)
458 avl_init(&handlers, avl_strcmp, false, NULL);
459
460 if (p->avl.key)
461 return;
462
463 p->avl.key = p->name;
464 avl_insert(&handlers, &p->avl);
465 }
466
467 static void
468 default_proto_free(struct interface_proto_state *proto)
469 {
470 free(proto);
471 }
472
473 static int
474 invalid_proto_handler(struct interface_proto_state *proto,
475 enum interface_proto_cmd cmd, bool force)
476 {
477 return -1;
478 }
479
480 static int
481 no_proto_handler(struct interface_proto_state *proto,
482 enum interface_proto_cmd cmd, bool force)
483 {
484 return 0;
485 }
486
487 static struct interface_proto_state *
488 default_proto_attach(const struct proto_handler *h,
489 struct interface *iface, struct blob_attr *attr)
490 {
491 struct interface_proto_state *proto;
492
493 proto = calloc(1, sizeof(*proto));
494 proto->free = default_proto_free;
495 proto->cb = no_proto_handler;
496
497 return proto;
498 }
499
500 static const struct proto_handler no_proto = {
501 .name = "none",
502 .flags = PROTO_FLAG_IMMEDIATE,
503 .attach = default_proto_attach,
504 };
505
506 static const struct proto_handler *
507 get_proto_handler(const char *name)
508 {
509 struct proto_handler *proto;
510
511 if (!strcmp(name, "none"))
512 return &no_proto;
513
514 if (!handlers.comp)
515 return NULL;
516
517 return avl_find_element(&handlers, name, proto, avl);
518 }
519
520 void
521 proto_dump_handlers(struct blob_buf *b)
522 {
523 struct proto_handler *p;
524 void *c;
525
526 avl_for_each_element(&handlers, p, avl) {
527 c = blobmsg_open_table(b, p->name);
528 blobmsg_add_u8(b, "no_device", !!(p->flags & PROTO_FLAG_NODEV));
529 blobmsg_close_table(b, c);
530 }
531 }
532
533 void
534 proto_init_interface(struct interface *iface, struct blob_attr *attr)
535 {
536 const struct proto_handler *proto = iface->proto_handler;
537 struct interface_proto_state *state = NULL;
538
539 if (!proto)
540 proto = &no_proto;
541
542 state = proto->attach(proto, iface, attr);
543 if (!state) {
544 state = no_proto.attach(&no_proto, iface, attr);
545 state->cb = invalid_proto_handler;
546 }
547
548 state->handler = proto;
549 interface_set_proto_state(iface, state);
550 }
551
552 void
553 proto_attach_interface(struct interface *iface, const char *proto_name)
554 {
555 const struct proto_handler *proto = &no_proto;
556
557 if (proto_name) {
558 proto = get_proto_handler(proto_name);
559 if (!proto) {
560 interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
561 proto = &no_proto;
562 }
563 }
564
565 iface->proto_handler = proto;
566 }
567
568 int
569 interface_proto_event(struct interface_proto_state *proto,
570 enum interface_proto_cmd cmd, bool force)
571 {
572 enum interface_proto_event ev;
573 int ret;
574
575 ret = proto->cb(proto, cmd, force);
576 if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
577 goto out;
578
579 switch(cmd) {
580 case PROTO_CMD_SETUP:
581 ev = IFPEV_UP;
582 break;
583 case PROTO_CMD_TEARDOWN:
584 ev = IFPEV_DOWN;
585 break;
586 default:
587 return -EINVAL;
588 }
589 proto->proto_event(proto, ev);
590
591 out:
592 return ret;
593 }