system-linux: fix GRE ikey/okey endianness
[project/netifd.git] / iprule.c
1 /*
2  * netifd - network interface daemon
3  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4  * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18
19 #include <arpa/inet.h>
20
21 #include "netifd.h"
22 #include "device.h"
23 #include "interface.h"
24 #include "iprule.h"
25 #include "proto.h"
26 #include "ubus.h"
27 #include "system.h"
28
29 struct vlist_tree iprules;
30 static bool iprules_flushed = false;
31 static unsigned int iprules_counter[2];
32
33 enum {
34         RULE_INTERFACE_IN,
35         RULE_INTERFACE_OUT,
36         RULE_INVERT,
37         RULE_SRC,
38         RULE_DEST,
39         RULE_PRIORITY,
40         RULE_TOS,
41         RULE_FWMARK,
42         RULE_LOOKUP,
43         RULE_ACTION,
44         RULE_GOTO,
45         RULE_SUP_PREFIXLEN,
46         __RULE_MAX
47 };
48
49 static const struct blobmsg_policy rule_attr[__RULE_MAX] = {
50         [RULE_INTERFACE_IN] = { .name = "in", .type = BLOBMSG_TYPE_STRING },
51         [RULE_INTERFACE_OUT] = { .name = "out", .type = BLOBMSG_TYPE_STRING },
52         [RULE_INVERT] = { .name = "invert", .type = BLOBMSG_TYPE_BOOL },
53         [RULE_SRC] = { .name = "src", .type = BLOBMSG_TYPE_STRING },
54         [RULE_DEST] = { .name = "dest", .type = BLOBMSG_TYPE_STRING },
55         [RULE_PRIORITY] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
56         [RULE_TOS] = { .name = "tos", .type = BLOBMSG_TYPE_INT32 },
57         [RULE_FWMARK] = { .name = "mark", .type = BLOBMSG_TYPE_STRING },
58         [RULE_LOOKUP] = { .name = "lookup", .type = BLOBMSG_TYPE_STRING },
59         [RULE_SUP_PREFIXLEN] = { .name = "suppress_prefixlength", .type = BLOBMSG_TYPE_INT32 },
60         [RULE_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
61         [RULE_GOTO]   = { .name = "goto", .type = BLOBMSG_TYPE_INT32 },
62 };
63
64 const struct uci_blob_param_list rule_attr_list = {
65         .n_params = __RULE_MAX,
66         .params = rule_attr,
67 };
68
69
70 static bool
71 iprule_parse_mark(const char *mark, struct iprule *rule)
72 {
73         char *s, *e;
74         unsigned int n;
75
76         if ((s = strchr(mark, '/')) != NULL)
77                 *s++ = 0;
78
79         n = strtoul(mark, &e, 0);
80
81         if (e == mark || *e)
82                 return false;
83
84         rule->fwmark = n;
85         rule->flags |= IPRULE_FWMARK;
86
87         if (s) {
88                 n = strtoul(s, &e, 0);
89
90                 if (e == s || *e)
91                         return false;
92
93                 rule->fwmask = n;
94                 rule->flags |= IPRULE_FWMASK;
95         }
96
97         return true;
98 }
99
100 void
101 iprule_add(struct blob_attr *attr, bool v6)
102 {
103         struct interface *iif = NULL, *oif = NULL;
104         struct blob_attr *tb[__RULE_MAX], *cur;
105         struct interface *iface;
106         struct iprule *rule;
107         int af = v6 ? AF_INET6 : AF_INET;
108
109         blobmsg_parse(rule_attr, __RULE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
110
111         rule = calloc(1, sizeof(*rule));
112         if (!rule)
113                 return;
114
115         rule->flags = v6 ? IPRULE_INET6 : IPRULE_INET4;
116         rule->order = iprules_counter[rule->flags]++;
117
118         if ((cur = tb[RULE_INVERT]) != NULL)
119                 rule->invert = blobmsg_get_bool(cur);
120
121         if ((cur = tb[RULE_INTERFACE_IN]) != NULL) {
122                 iif = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
123
124                 if (!iif || !iif->l3_dev.dev) {
125                         DPRINTF("Failed to resolve device of network: %s\n", (char *) blobmsg_data(cur));
126                         goto error;
127                 }
128
129                 memcpy(rule->in_dev, iif->l3_dev.dev->ifname, sizeof(rule->in_dev));
130                 rule->flags |= IPRULE_IN;
131         }
132
133         if ((cur = tb[RULE_INTERFACE_OUT]) != NULL) {
134                 oif = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
135
136                 if (!oif || !oif->l3_dev.dev) {
137                         DPRINTF("Failed to resolve device of network: %s\n", (char *) blobmsg_data(cur));
138                         goto error;
139                 }
140
141                 memcpy(rule->out_dev, oif->l3_dev.dev->ifname, sizeof(rule->out_dev));
142                 rule->flags |= IPRULE_OUT;
143         }
144
145         if ((cur = tb[RULE_SRC]) != NULL) {
146                 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &rule->src_addr, &rule->src_mask)) {
147                         DPRINTF("Failed to parse rule source: %s\n", (char *) blobmsg_data(cur));
148                         goto error;
149                 }
150                 rule->flags |= IPRULE_SRC;
151         }
152
153         if ((cur = tb[RULE_DEST]) != NULL) {
154                 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &rule->dest_addr, &rule->dest_mask)) {
155                         DPRINTF("Failed to parse rule destination: %s\n", (char *) blobmsg_data(cur));
156                         goto error;
157                 }
158                 rule->flags |= IPRULE_DEST;
159         }
160
161         if ((cur = tb[RULE_PRIORITY]) != NULL) {
162                 rule->priority = blobmsg_get_u32(cur);
163                 rule->flags |= IPRULE_PRIORITY;
164         }
165
166         if ((cur = tb[RULE_TOS]) != NULL) {
167                 if ((rule->tos = blobmsg_get_u32(cur)) > 255) {
168                         DPRINTF("Invalid TOS value: %u\n", blobmsg_get_u32(cur));
169                         goto error;
170                 }
171                 rule->flags |= IPRULE_TOS;
172         }
173
174         if ((cur = tb[RULE_FWMARK]) != NULL) {
175                 if (!iprule_parse_mark(blobmsg_data(cur), rule)) {
176                         DPRINTF("Failed to parse rule fwmark: %s\n", (char *) blobmsg_data(cur));
177                         goto error;
178                 }
179                 /* flags set by iprule_parse_mark() */
180         }
181
182         if ((cur = tb[RULE_LOOKUP]) != NULL) {
183                 if (!system_resolve_rt_table(blobmsg_data(cur), &rule->lookup)) {
184                         DPRINTF("Failed to parse rule lookup table: %s\n", (char *) blobmsg_data(cur));
185                         goto error;
186                 }
187                 rule->flags |= IPRULE_LOOKUP;
188         }
189
190         if ((cur = tb[RULE_SUP_PREFIXLEN]) != NULL) {
191                 rule->sup_prefixlen = blobmsg_get_u32(cur);
192                 rule->flags |= IPRULE_SUP_PREFIXLEN;
193         }
194
195         if ((cur = tb[RULE_ACTION]) != NULL) {
196                 if (!system_resolve_iprule_action(blobmsg_data(cur), &rule->action)) {
197                         DPRINTF("Failed to parse rule action: %s\n", (char *) blobmsg_data(cur));
198                         goto error;
199                 }
200                 rule->flags |= IPRULE_ACTION;
201         }
202
203         if ((cur = tb[RULE_GOTO]) != NULL) {
204                 rule->gotoid = blobmsg_get_u32(cur);
205                 rule->flags |= IPRULE_GOTO;
206         }
207
208         vlist_add(&iprules, &rule->node, &rule->flags);
209         return;
210
211 error:
212         free(rule);
213 }
214
215 void
216 iprule_update_start(void)
217 {
218         if (!iprules_flushed) {
219                 system_flush_iprules();
220                 iprules_flushed = true;
221         }
222
223         iprules_counter[0] = 1;
224         iprules_counter[1] = 1;
225         vlist_update(&iprules);
226 }
227
228 void
229 iprule_update_complete(void)
230 {
231         vlist_flush(&iprules);
232 }
233
234
235 static int
236 rule_cmp(const void *k1, const void *k2, void *ptr)
237 {
238         return memcmp(k1, k2, sizeof(struct iprule)-offsetof(struct iprule, flags));
239 }
240
241 static void
242 iprule_update_rule(struct vlist_tree *tree,
243                         struct vlist_node *node_new, struct vlist_node *node_old)
244 {
245         struct iprule *rule_old, *rule_new;
246
247         rule_old = container_of(node_old, struct iprule, node);
248         rule_new = container_of(node_new, struct iprule, node);
249
250         if (node_old) {
251                 system_del_iprule(rule_old);
252                 free(rule_old);
253         }
254
255         if (node_new)
256                 system_add_iprule(rule_new);
257 }
258
259 static void __init
260 iprule_init_list(void)
261 {
262         vlist_init(&iprules, rule_cmp, iprule_update_rule);
263 }