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