44bd42d4223d8d352d5d9b48cbbc8f9788c6b64f
[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_static_ip_settings(struct interface *iface, struct blob_attr *attr)
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, false, bcast.s_addr);
235
236 if ((cur = tb[OPT_IP6ADDR]))
237 n_v6 = parse_address_option(iface, cur, true,
238 netmask, false, 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 int
273 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
274 {
275 struct blob_attr *tb[__OPT_MAX];
276 struct blob_attr *cur;
277 const char *error;
278 int n_v4 = 0, n_v6 = 0;
279 struct in_addr bcast = {};
280
281 blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
282
283 if ((cur = tb[OPT_IPADDR]))
284 n_v4 = parse_address_option(iface, cur, false,
285 32, ext, bcast.s_addr);
286
287 if ((cur = tb[OPT_IP6ADDR]))
288 n_v6 = parse_address_option(iface, cur, true,
289 128, ext, 0);
290
291 if (!n_v4 && !n_v6) {
292 error = "NO_ADDRESS";
293 goto error;
294 }
295
296 if (n_v4 < 0 || n_v6 < 0)
297 goto out;
298
299 if ((cur = tb[OPT_GATEWAY])) {
300 if (n_v4 && !parse_gateway_option(iface, cur, false))
301 goto out;
302 }
303
304 if ((cur = tb[OPT_IP6GW])) {
305 if (n_v6 && !parse_gateway_option(iface, cur, true))
306 goto out;
307 }
308
309 if ((cur = tb[OPT_DNS]))
310 interface_add_dns_server_list(&iface->proto_ip, cur);
311
312 if ((cur = tb[OPT_DNS_SEARCH]))
313 interface_add_dns_search_list(&iface->proto_ip, cur);
314
315 return 0;
316
317 error:
318 interface_add_error(iface, "proto", error, NULL, 0);
319 out:
320 return -1;
321 }
322
323 void add_proto_handler(struct proto_handler *p)
324 {
325 if (!handlers.comp)
326 avl_init(&handlers, avl_strcmp, false, NULL);
327
328 if (p->avl.key)
329 return;
330
331 p->avl.key = p->name;
332 avl_insert(&handlers, &p->avl);
333 }
334
335 static void
336 default_proto_free(struct interface_proto_state *proto)
337 {
338 free(proto);
339 }
340
341 static int
342 invalid_proto_handler(struct interface_proto_state *proto,
343 enum interface_proto_cmd cmd, bool force)
344 {
345 return -1;
346 }
347
348 static int
349 no_proto_handler(struct interface_proto_state *proto,
350 enum interface_proto_cmd cmd, bool force)
351 {
352 return 0;
353 }
354
355 static struct interface_proto_state *
356 default_proto_attach(const struct proto_handler *h,
357 struct interface *iface, struct blob_attr *attr)
358 {
359 struct interface_proto_state *proto;
360
361 proto = calloc(1, sizeof(*proto));
362 proto->free = default_proto_free;
363 proto->cb = no_proto_handler;
364
365 return proto;
366 }
367
368 static const struct proto_handler no_proto = {
369 .name = "none",
370 .flags = PROTO_FLAG_IMMEDIATE,
371 .attach = default_proto_attach,
372 };
373
374 static const struct proto_handler *
375 get_proto_handler(const char *name)
376 {
377 struct proto_handler *proto;
378
379 if (!strcmp(name, "none"))
380 return &no_proto;
381
382 if (!handlers.comp)
383 return NULL;
384
385 return avl_find_element(&handlers, name, proto, avl);
386 }
387
388 void
389 proto_init_interface(struct interface *iface, struct blob_attr *attr)
390 {
391 const struct proto_handler *proto = iface->proto_handler;
392 struct interface_proto_state *state = NULL;
393
394 if (!proto)
395 proto = &no_proto;
396
397 state = proto->attach(proto, iface, attr);
398 if (!state) {
399 state = no_proto.attach(&no_proto, iface, attr);
400 state->cb = invalid_proto_handler;
401 }
402
403 state->handler = proto;
404 interface_set_proto_state(iface, state);
405 }
406
407 void
408 proto_attach_interface(struct interface *iface, const char *proto_name)
409 {
410 const struct proto_handler *proto = &no_proto;
411
412 if (proto_name) {
413 proto = get_proto_handler(proto_name);
414 if (!proto) {
415 interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
416 proto = &no_proto;
417 }
418 }
419
420 iface->proto_handler = proto;
421 }
422
423 int
424 interface_proto_event(struct interface_proto_state *proto,
425 enum interface_proto_cmd cmd, bool force)
426 {
427 enum interface_proto_event ev;
428 int ret;
429
430 ret = proto->cb(proto, cmd, force);
431 if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
432 goto out;
433
434 switch(cmd) {
435 case PROTO_CMD_SETUP:
436 ev = IFPEV_UP;
437 break;
438 case PROTO_CMD_TEARDOWN:
439 ev = IFPEV_DOWN;
440 break;
441 default:
442 return -EINVAL;
443 }
444 proto->proto_event(proto, ev);
445
446 out:
447 return ret;
448 }