fix buffer overflow in interface data handling
[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
64 unsigned int
65 parse_netmask_string(const char *str, bool v6)
66 {
67         struct in_addr addr;
68         unsigned int ret;
69         char *err = NULL;
70
71         if (!strchr(str, '.')) {
72                 ret = strtoul(str, &err, 0);
73                 if (err && *err)
74                         goto error;
75
76                 return ret;
77         }
78
79         if (v6)
80                 goto error;
81
82         if (inet_aton(str, &addr) != 1)
83                 goto error;
84
85         return 32 - fls(~(ntohl(addr.s_addr)));
86
87 error:
88         return ~0;
89 }
90
91 static bool
92 split_netmask(char *str, unsigned int *netmask, bool v6)
93 {
94         char *delim = strchr(str, '/');
95
96         if (delim) {
97                 *(delim++) = 0;
98
99                 *netmask = parse_netmask_string(delim, v6);
100         }
101         return true;
102 }
103
104 static int
105 parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
106 {
107         char *astr = alloca(strlen(str) + 1);
108
109         strcpy(astr, str);
110         if (!split_netmask(astr, netmask, af == AF_INET6))
111                 return 0;
112
113         if (af == AF_INET6) {
114                 if (*netmask > 128)
115                         return 0;
116         } else {
117                 if (*netmask > 32)
118                         return 0;
119         }
120
121         return inet_pton(af, astr, addr);
122 }
123
124 static struct device_addr *
125 proto_parse_ip_addr_string(const char *str, bool v6, int mask)
126 {
127         struct device_addr *addr;
128         int af = v6 ? AF_INET6 : AF_INET;
129
130         addr = calloc(1, sizeof(*addr));
131         addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
132         addr->mask = mask;
133         if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
134                 free(addr);
135                 return NULL;
136         }
137         return addr;
138 }
139
140 static bool
141 parse_addr(struct interface *iface, const char *str, bool v6, int mask,
142            bool ext, uint32_t broadcast)
143 {
144         struct device_addr *addr;
145
146         addr = proto_parse_ip_addr_string(str, v6, mask);
147         if (!addr) {
148                 interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
149                 return false;
150         }
151
152         if (broadcast)
153                 addr->broadcast = broadcast;
154
155         if (ext)
156                 addr->flags |= DEVADDR_EXTERNAL;
157
158         vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
159         return true;
160 }
161
162 static int
163 parse_address_option(struct interface *iface, struct blob_attr *attr, bool v6,
164                      int netmask, bool ext, uint32_t broadcast)
165 {
166         struct blob_attr *cur;
167         int n_addr = 0;
168         int rem;
169
170         blobmsg_for_each_attr(cur, attr, rem) {
171                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
172                         return -1;
173
174                 n_addr++;
175                 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
176                                 broadcast))
177                         return -1;
178         }
179
180         return n_addr;
181 }
182
183
184 static bool
185 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
186 {
187         struct device_route *route;
188         const char *str = blobmsg_data(attr);
189         int af = v6 ? AF_INET6 : AF_INET;
190
191         route = calloc(1, sizeof(*route));
192         if (!inet_pton(af, str, &route->nexthop)) {
193                 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
194                 free(route);
195                 return false;
196         }
197
198         route->mask = 0;
199         route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
200         vlist_add(&iface->proto_ip.route, &route->node, &route->flags);
201
202         return true;
203 }
204
205 int
206 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
207 {
208         struct blob_attr *tb[__OPT_MAX];
209         struct blob_attr *cur;
210         const char *error;
211         unsigned int netmask = 32;
212         int n_v4 = 0, n_v6 = 0;
213         struct in_addr bcast = {};
214
215         blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
216
217         if ((cur = tb[OPT_NETMASK])) {
218                 netmask = parse_netmask_string(blobmsg_data(cur), false);
219                 if (netmask > 32) {
220                         error = "INVALID_NETMASK";
221                         goto error;
222                 }
223         }
224
225         if ((cur = tb[OPT_BROADCAST])) {
226                 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
227                         error = "INVALID_BROADCAST";
228                         goto error;
229                 }
230         }
231
232         if ((cur = tb[OPT_IPADDR]))
233                 n_v4 = parse_address_option(iface, cur, false,
234                         netmask, ext, bcast.s_addr);
235
236         if ((cur = tb[OPT_IP6ADDR]))
237                 n_v6 = parse_address_option(iface, cur, true,
238                         netmask, ext, 0);
239
240         if (!n_v4 && !n_v6) {
241                 error = "NO_ADDRESS";
242                 goto error;
243         }
244
245         if (n_v4 < 0 || n_v6 < 0)
246                 goto out;
247
248         if ((cur = tb[OPT_GATEWAY])) {
249                 if (n_v4 && !parse_gateway_option(iface, cur, false))
250                         goto out;
251         }
252
253         if ((cur = tb[OPT_IP6GW])) {
254                 if (n_v6 && !parse_gateway_option(iface, cur, true))
255                         goto out;
256         }
257
258         if ((cur = tb[OPT_DNS]))
259                 interface_add_dns_server_list(&iface->proto_ip, cur);
260
261         if ((cur = tb[OPT_DNS_SEARCH]))
262                 interface_add_dns_search_list(&iface->proto_ip, cur);
263
264         return 0;
265
266 error:
267         interface_add_error(iface, "proto", error, NULL, 0);
268 out:
269         return -1;
270 }
271
272 void add_proto_handler(struct proto_handler *p)
273 {
274         if (!handlers.comp)
275                 avl_init(&handlers, avl_strcmp, false, NULL);
276
277         if (p->avl.key)
278                 return;
279
280         p->avl.key = p->name;
281         avl_insert(&handlers, &p->avl);
282 }
283
284 static void
285 default_proto_free(struct interface_proto_state *proto)
286 {
287         free(proto);
288 }
289
290 static int
291 invalid_proto_handler(struct interface_proto_state *proto,
292                       enum interface_proto_cmd cmd, bool force)
293 {
294         return -1;
295 }
296
297 static int
298 no_proto_handler(struct interface_proto_state *proto,
299                  enum interface_proto_cmd cmd, bool force)
300 {
301         return 0;
302 }
303
304 static struct interface_proto_state *
305 default_proto_attach(const struct proto_handler *h,
306                      struct interface *iface, struct blob_attr *attr)
307 {
308         struct interface_proto_state *proto;
309
310         proto = calloc(1, sizeof(*proto));
311         proto->free = default_proto_free;
312         proto->cb = no_proto_handler;
313
314         return proto;
315 }
316
317 static const struct proto_handler no_proto = {
318         .name = "none",
319         .flags = PROTO_FLAG_IMMEDIATE,
320         .attach = default_proto_attach,
321 };
322
323 static const struct proto_handler *
324 get_proto_handler(const char *name)
325 {
326         struct proto_handler *proto;
327
328         if (!strcmp(name, "none"))
329             return &no_proto;
330
331         if (!handlers.comp)
332                 return NULL;
333
334         return avl_find_element(&handlers, name, proto, avl);
335 }
336
337 void
338 proto_init_interface(struct interface *iface, struct blob_attr *attr)
339 {
340         const struct proto_handler *proto = iface->proto_handler;
341         struct interface_proto_state *state = NULL;
342
343         if (!proto)
344                 proto = &no_proto;
345
346         state = proto->attach(proto, iface, attr);
347         if (!state) {
348                 state = no_proto.attach(&no_proto, iface, attr);
349                 state->cb = invalid_proto_handler;
350         }
351
352         state->handler = proto;
353         interface_set_proto_state(iface, state);
354 }
355
356 void
357 proto_attach_interface(struct interface *iface, const char *proto_name)
358 {
359         const struct proto_handler *proto = &no_proto;
360
361         if (proto_name) {
362                 proto = get_proto_handler(proto_name);
363                 if (!proto) {
364                         interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
365                         proto = &no_proto;
366                 }
367         }
368
369         iface->proto_handler = proto;
370 }
371
372 int
373 interface_proto_event(struct interface_proto_state *proto,
374                       enum interface_proto_cmd cmd, bool force)
375 {
376         enum interface_proto_event ev;
377         int ret;
378
379         ret = proto->cb(proto, cmd, force);
380         if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
381                 goto out;
382
383         switch(cmd) {
384         case PROTO_CMD_SETUP:
385                 ev = IFPEV_UP;
386                 break;
387         case PROTO_CMD_TEARDOWN:
388                 ev = IFPEV_DOWN;
389                 break;
390         default:
391                 return -EINVAL;
392         }
393         proto->proto_event(proto, ev);
394
395 out:
396         return ret;
397 }