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