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