proto-shell: refactor code to allow specifying per-address broadcast option for ipv4
[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_DNS,
36         OPT_DNS_SEARCH,
37         __OPT_MAX,
38 };
39
40 static const struct blobmsg_policy proto_ip_attributes[__OPT_MAX] = {
41         [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
42         [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
43         [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
44         [OPT_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
45         [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
46         [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
47         [OPT_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
48         [OPT_DNS_SEARCH] = { .name = "dns_search", .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_DNS] = { .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_MAX
68 };
69
70 static const struct blobmsg_policy proto_ip_addr[__ADDR_MAX] = {
71         [ADDR_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING },
72         [ADDR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING },
73         [ADDR_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
74 };
75
76 unsigned int
77 parse_netmask_string(const char *str, bool v6)
78 {
79         struct in_addr addr;
80         unsigned int ret;
81         char *err = NULL;
82
83         if (!strchr(str, '.')) {
84                 ret = strtoul(str, &err, 0);
85                 if (err && *err)
86                         goto error;
87
88                 return ret;
89         }
90
91         if (v6)
92                 goto error;
93
94         if (inet_aton(str, &addr) != 1)
95                 goto error;
96
97         return 32 - fls(~(ntohl(addr.s_addr)));
98
99 error:
100         return ~0;
101 }
102
103 static bool
104 split_netmask(char *str, unsigned int *netmask, bool v6)
105 {
106         char *delim = strchr(str, '/');
107
108         if (delim) {
109                 *(delim++) = 0;
110
111                 *netmask = parse_netmask_string(delim, v6);
112         }
113         return true;
114 }
115
116 static int
117 parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
118 {
119         char *astr = alloca(strlen(str) + 1);
120
121         strcpy(astr, str);
122         if (!split_netmask(astr, netmask, af == AF_INET6))
123                 return 0;
124
125         if (af == AF_INET6) {
126                 if (*netmask > 128)
127                         return 0;
128         } else {
129                 if (*netmask > 32)
130                         return 0;
131         }
132
133         return inet_pton(af, astr, addr);
134 }
135
136 static struct device_addr *
137 alloc_device_addr(bool v6, bool ext)
138 {
139         struct device_addr *addr;
140
141         addr = calloc(1, sizeof(*addr));
142         addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
143         if (ext)
144                 addr->flags |= DEVADDR_EXTERNAL;
145
146         return addr;
147 }
148
149 static bool
150 parse_addr(struct interface *iface, const char *str, bool v6, int mask,
151            bool ext, uint32_t broadcast)
152 {
153         struct device_addr *addr;
154         int af = v6 ? AF_INET6 : AF_INET;
155
156         addr = alloc_device_addr(v6, ext);
157         if (!addr)
158                 return false;
159
160         addr->mask = mask;
161         if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
162                 interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
163                 free(addr);
164                 return false;
165         }
166
167         if (broadcast)
168                 addr->broadcast = broadcast;
169
170         vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
171         return true;
172 }
173
174 static int
175 parse_static_address_option(struct interface *iface, struct blob_attr *attr,
176                             bool v6, int netmask, bool ext, uint32_t broadcast)
177 {
178         struct blob_attr *cur;
179         int n_addr = 0;
180         int rem;
181
182         blobmsg_for_each_attr(cur, attr, rem) {
183                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
184                         return -1;
185
186                 n_addr++;
187                 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
188                                 broadcast))
189                         return -1;
190         }
191
192         return n_addr;
193 }
194
195 static struct device_addr *
196 parse_address_item(struct blob_attr *attr, bool v6, bool ext)
197 {
198         struct device_addr *addr;
199         struct blob_attr *tb[__ADDR_MAX];
200         struct blob_attr *cur;
201
202         if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
203                 return NULL;
204
205         addr = alloc_device_addr(v6, ext);
206         if (!addr)
207                 return NULL;
208
209         blobmsg_parse(proto_ip_addr, __ADDR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
210
211         addr->mask = v6 ? 128 : 32;
212         if ((cur = tb[ADDR_MASK])) {
213                 unsigned int new_mask;
214
215                 new_mask = parse_netmask_string(blobmsg_data(cur), v6);
216                 if (new_mask > addr->mask)
217                         goto error;
218
219                 addr->mask = new_mask;
220         }
221
222         cur = tb[ADDR_IPADDR];
223         if (!cur)
224                 goto error;
225
226         if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(cur), &addr->addr))
227                 goto error;
228
229         if (!v6 && (cur = tb[ADDR_BROADCAST])) {
230                 if (!inet_pton(AF_INET, blobmsg_data(cur), &addr->broadcast))
231                         goto error;
232         }
233
234         return addr;
235
236 error:
237         free(addr);
238         return NULL;
239 }
240
241 static int
242 parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6,
243                    bool ext)
244 {
245         struct device_addr *addr;
246         struct blob_attr *cur;
247         int n_addr = 0;
248         int rem;
249
250         blobmsg_for_each_attr(cur, attr, rem) {
251                 addr = parse_address_item(cur, v6, ext);
252                 if (!addr)
253                         return -1;
254
255                 n_addr++;
256                 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
257         }
258
259         return n_addr;
260 }
261
262 static bool
263 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
264 {
265         struct device_route *route;
266         const char *str = blobmsg_data(attr);
267         int af = v6 ? AF_INET6 : AF_INET;
268
269         route = calloc(1, sizeof(*route));
270         if (!inet_pton(af, str, &route->nexthop)) {
271                 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
272                 free(route);
273                 return false;
274         }
275
276         route->mask = 0;
277         route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
278         vlist_add(&iface->proto_ip.route, &route->node, &route->flags);
279
280         return true;
281 }
282
283 int
284 proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
285 {
286         struct blob_attr *tb[__OPT_MAX];
287         struct blob_attr *cur;
288         const char *error;
289         unsigned int netmask = 32;
290         int n_v4 = 0, n_v6 = 0;
291         struct in_addr bcast = {};
292
293         blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
294
295         if ((cur = tb[OPT_NETMASK])) {
296                 netmask = parse_netmask_string(blobmsg_data(cur), false);
297                 if (netmask > 32) {
298                         error = "INVALID_NETMASK";
299                         goto error;
300                 }
301         }
302
303         if ((cur = tb[OPT_BROADCAST])) {
304                 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
305                         error = "INVALID_BROADCAST";
306                         goto error;
307                 }
308         }
309
310         if ((cur = tb[OPT_IPADDR]))
311                 n_v4 = parse_static_address_option(iface, cur, false,
312                         netmask, false, bcast.s_addr);
313
314         if ((cur = tb[OPT_IP6ADDR]))
315                 n_v6 = parse_static_address_option(iface, cur, true,
316                         netmask, false, 0);
317
318         if (!n_v4 && !n_v6) {
319                 error = "NO_ADDRESS";
320                 goto error;
321         }
322
323         if (n_v4 < 0 || n_v6 < 0)
324                 goto out;
325
326         if ((cur = tb[OPT_GATEWAY])) {
327                 if (n_v4 && !parse_gateway_option(iface, cur, false))
328                         goto out;
329         }
330
331         if ((cur = tb[OPT_IP6GW])) {
332                 if (n_v6 && !parse_gateway_option(iface, cur, true))
333                         goto out;
334         }
335
336         if ((cur = tb[OPT_DNS]))
337                 interface_add_dns_server_list(&iface->proto_ip, cur);
338
339         if ((cur = tb[OPT_DNS_SEARCH]))
340                 interface_add_dns_search_list(&iface->proto_ip, cur);
341
342         return 0;
343
344 error:
345         interface_add_error(iface, "proto", error, NULL, 0);
346 out:
347         return -1;
348 }
349
350 int
351 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
352 {
353         struct blob_attr *tb[__OPT_MAX];
354         struct blob_attr *cur;
355         const char *error;
356         int n_v4 = 0, n_v6 = 0;
357
358         blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
359
360         if ((cur = tb[OPT_IPADDR]))
361                 n_v4 = parse_address_list(iface, cur, false, ext);
362
363         if ((cur = tb[OPT_IP6ADDR]))
364                 n_v6 = parse_address_list(iface, cur, true, ext);
365
366         if (!n_v4 && !n_v6) {
367                 error = "NO_ADDRESS";
368                 goto error;
369         }
370
371         if (n_v4 < 0 || n_v6 < 0)
372                 goto out;
373
374         if ((cur = tb[OPT_GATEWAY])) {
375                 if (n_v4 && !parse_gateway_option(iface, cur, false))
376                         goto out;
377         }
378
379         if ((cur = tb[OPT_IP6GW])) {
380                 if (n_v6 && !parse_gateway_option(iface, cur, true))
381                         goto out;
382         }
383
384         if ((cur = tb[OPT_DNS]))
385                 interface_add_dns_server_list(&iface->proto_ip, cur);
386
387         if ((cur = tb[OPT_DNS_SEARCH]))
388                 interface_add_dns_search_list(&iface->proto_ip, cur);
389
390         return 0;
391
392 error:
393         interface_add_error(iface, "proto", error, NULL, 0);
394 out:
395         return -1;
396 }
397
398 void add_proto_handler(struct proto_handler *p)
399 {
400         if (!handlers.comp)
401                 avl_init(&handlers, avl_strcmp, false, NULL);
402
403         if (p->avl.key)
404                 return;
405
406         p->avl.key = p->name;
407         avl_insert(&handlers, &p->avl);
408 }
409
410 static void
411 default_proto_free(struct interface_proto_state *proto)
412 {
413         free(proto);
414 }
415
416 static int
417 invalid_proto_handler(struct interface_proto_state *proto,
418                       enum interface_proto_cmd cmd, bool force)
419 {
420         return -1;
421 }
422
423 static int
424 no_proto_handler(struct interface_proto_state *proto,
425                  enum interface_proto_cmd cmd, bool force)
426 {
427         return 0;
428 }
429
430 static struct interface_proto_state *
431 default_proto_attach(const struct proto_handler *h,
432                      struct interface *iface, struct blob_attr *attr)
433 {
434         struct interface_proto_state *proto;
435
436         proto = calloc(1, sizeof(*proto));
437         proto->free = default_proto_free;
438         proto->cb = no_proto_handler;
439
440         return proto;
441 }
442
443 static const struct proto_handler no_proto = {
444         .name = "none",
445         .flags = PROTO_FLAG_IMMEDIATE,
446         .attach = default_proto_attach,
447 };
448
449 static const struct proto_handler *
450 get_proto_handler(const char *name)
451 {
452         struct proto_handler *proto;
453
454         if (!strcmp(name, "none"))
455             return &no_proto;
456
457         if (!handlers.comp)
458                 return NULL;
459
460         return avl_find_element(&handlers, name, proto, avl);
461 }
462
463 void
464 proto_init_interface(struct interface *iface, struct blob_attr *attr)
465 {
466         const struct proto_handler *proto = iface->proto_handler;
467         struct interface_proto_state *state = NULL;
468
469         if (!proto)
470                 proto = &no_proto;
471
472         state = proto->attach(proto, iface, attr);
473         if (!state) {
474                 state = no_proto.attach(&no_proto, iface, attr);
475                 state->cb = invalid_proto_handler;
476         }
477
478         state->handler = proto;
479         interface_set_proto_state(iface, state);
480 }
481
482 void
483 proto_attach_interface(struct interface *iface, const char *proto_name)
484 {
485         const struct proto_handler *proto = &no_proto;
486
487         if (proto_name) {
488                 proto = get_proto_handler(proto_name);
489                 if (!proto) {
490                         interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
491                         proto = &no_proto;
492                 }
493         }
494
495         iface->proto_handler = proto;
496 }
497
498 int
499 interface_proto_event(struct interface_proto_state *proto,
500                       enum interface_proto_cmd cmd, bool force)
501 {
502         enum interface_proto_event ev;
503         int ret;
504
505         ret = proto->cb(proto, cmd, force);
506         if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
507                 goto out;
508
509         switch(cmd) {
510         case PROTO_CMD_SETUP:
511                 ev = IFPEV_UP;
512                 break;
513         case PROTO_CMD_TEARDOWN:
514                 ev = IFPEV_DOWN;
515                 break;
516         default:
517                 return -EINVAL;
518         }
519         proto->proto_event(proto, ev);
520
521 out:
522         return ret;
523 }