0ff02ded72f8691080d08ceb7b7c31beed9fe546
[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 #include <unistd.h>
19
20 #include <arpa/inet.h>
21
22 #include "netifd.h"
23 #include "device.h"
24 #include "interface.h"
25 #include "iprule.h"
26 #include "proto.h"
27 #include "ubus.h"
28 #include "system.h"
29
30 struct vlist_tree iprules;
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_MAX
46 };
47
48 static const struct blobmsg_policy rule_attr[__RULE_MAX] = {
49         [RULE_INTERFACE_IN] = { .name = "in", .type = BLOBMSG_TYPE_STRING },
50         [RULE_INTERFACE_OUT] = { .name = "out", .type = BLOBMSG_TYPE_STRING },
51         [RULE_INVERT] = { .name = "invert", .type = BLOBMSG_TYPE_BOOL },
52         [RULE_SRC] = { .name = "src", .type = BLOBMSG_TYPE_STRING },
53         [RULE_DEST] = { .name = "dest", .type = BLOBMSG_TYPE_STRING },
54         [RULE_PRIORITY] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
55         [RULE_TOS] = { .name = "tos", .type = BLOBMSG_TYPE_INT32 },
56         [RULE_FWMARK] = { .name = "mark", .type = BLOBMSG_TYPE_STRING },
57         [RULE_LOOKUP] = { .name = "lookup", .type = BLOBMSG_TYPE_STRING },
58         [RULE_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_STRING },
59         [RULE_GOTO]   = { .name = "goto", .type = BLOBMSG_TYPE_INT32 },
60 };
61
62 const struct config_param_list rule_attr_list = {
63         .n_params = __RULE_MAX,
64         .params = rule_attr,
65 };
66
67
68 static bool
69 iprule_parse_mark(const char *mark, struct iprule *rule)
70 {
71         char *s, *e;
72         unsigned int n;
73
74         if ((s = strchr(mark, '/')) != NULL)
75                 *s++ = 0;
76
77         n = strtoul(mark, &e, 0);
78
79         if (e == mark || *e)
80                 return false;
81
82         rule->fwmark = n;
83         rule->flags |= IPRULE_FWMARK;
84
85         if (s) {
86                 n = strtoul(s, &e, 0);
87
88                 if (e == s || *e)
89                         return false;
90
91                 rule->fwmask = n;
92                 rule->flags |= IPRULE_FWMASK;
93         }
94
95         return true;
96 }
97
98 void
99 iprule_add(struct blob_attr *attr, bool v6)
100 {
101         struct interface *iif = NULL, *oif = NULL;
102         struct blob_attr *tb[__RULE_MAX], *cur;
103         struct interface *iface;
104         struct iprule *rule;
105         int af = v6 ? AF_INET6 : AF_INET;
106
107         blobmsg_parse(rule_attr, __RULE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
108
109         rule = calloc(1, sizeof(*rule));
110         if (!rule)
111                 return;
112
113         rule->flags = v6 ? IPRULE_INET6 : IPRULE_INET4;
114         rule->order = iprules_counter[rule->flags]++;
115
116         if ((cur = tb[RULE_INVERT]) != NULL)
117                 rule->invert = blobmsg_get_bool(cur);
118
119         if ((cur = tb[RULE_INTERFACE_IN]) != NULL) {
120                 iif = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
121
122                 if (!iif || !iif->l3_dev.dev) {
123                         DPRINTF("Failed to resolve device of network: %s\n", (char *) blobmsg_data(cur));
124                         goto error;
125                 }
126
127                 memcpy(rule->in_dev, iif->l3_dev.dev->ifname, sizeof(rule->in_dev));
128                 rule->flags |= IPRULE_IN;
129         }
130
131         if ((cur = tb[RULE_INTERFACE_OUT]) != NULL) {
132                 oif = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
133
134                 if (!oif || !oif->l3_dev.dev) {
135                         DPRINTF("Failed to resolve device of network: %s\n", (char *) blobmsg_data(cur));
136                         goto error;
137                 }
138
139                 memcpy(rule->out_dev, oif->l3_dev.dev->ifname, sizeof(rule->out_dev));
140                 rule->flags |= IPRULE_OUT;
141         }
142
143         if ((cur = tb[RULE_SRC]) != NULL) {
144                 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &rule->src_addr, &rule->src_mask)) {
145                         DPRINTF("Failed to parse rule source: %s\n", (char *) blobmsg_data(cur));
146                         goto error;
147                 }
148                 rule->flags |= IPRULE_SRC;
149         }
150
151         if ((cur = tb[RULE_DEST]) != NULL) {
152                 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &rule->dest_addr, &rule->dest_mask)) {
153                         DPRINTF("Failed to parse rule destination: %s\n", (char *) blobmsg_data(cur));
154                         goto error;
155                 }
156                 rule->flags |= IPRULE_DEST;
157         }
158
159         if ((cur = tb[RULE_PRIORITY]) != NULL) {
160                 rule->priority = blobmsg_get_u32(cur);
161                 rule->flags |= IPRULE_PRIORITY;
162         }
163
164         if ((cur = tb[RULE_TOS]) != NULL) {
165                 if ((rule->tos = blobmsg_get_u32(cur)) > 255) {
166                         DPRINTF("Invalid TOS value: %u\n", blobmsg_get_u32(cur));
167                         goto error;
168                 }
169                 rule->flags |= IPRULE_TOS;
170         }
171
172         if ((cur = tb[RULE_FWMARK]) != NULL) {
173                 if (!iprule_parse_mark(blobmsg_data(cur), rule)) {
174                         DPRINTF("Failed to parse rule fwmark: %s\n", (char *) blobmsg_data(cur));
175                         goto error;
176                 }
177                 /* flags set by iprule_parse_mark() */
178         }
179
180         if ((cur = tb[RULE_LOOKUP]) != NULL) {
181                 if (!system_resolve_rt_table(blobmsg_data(cur), &rule->lookup)) {
182                         DPRINTF("Failed to parse rule lookup table: %s\n", (char *) blobmsg_data(cur));
183                         goto error;
184                 }
185                 rule->flags |= IPRULE_LOOKUP;
186         }
187
188         if ((cur = tb[RULE_ACTION]) != NULL) {
189                 if (!system_resolve_iprule_action(blobmsg_data(cur), &rule->action)) {
190                         DPRINTF("Failed to parse rule action: %s\n", (char *) blobmsg_data(cur));
191                         goto error;
192                 }
193                 rule->flags |= IPRULE_ACTION;
194         }
195
196         if ((cur = tb[RULE_GOTO]) != NULL) {
197                 rule->gotoid = blobmsg_get_u32(cur);
198                 rule->flags |= IPRULE_GOTO;
199         }
200
201         vlist_add(&iprules, &rule->node, &rule->flags);
202         return;
203
204 error:
205         free(rule);
206 }
207
208 void
209 iprule_update_start(void)
210 {
211         iprules_counter[0] = 1;
212         iprules_counter[1] = 1;
213         vlist_update(&iprules);
214 }
215
216 void
217 iprule_update_complete(void)
218 {
219         vlist_flush(&iprules);
220 }
221
222
223 static int
224 rule_cmp(const void *k1, const void *k2, void *ptr)
225 {
226         return memcmp(k1, k2, sizeof(struct iprule)-offsetof(struct iprule, flags));
227 }
228
229 static void
230 iprule_update_rule(struct vlist_tree *tree,
231                    struct vlist_node *node_new, struct vlist_node *node_old)
232 {
233         struct iprule *rule_old, *rule_new;
234
235         rule_old = container_of(node_old, struct iprule, node);
236         rule_new = container_of(node_new, struct iprule, node);
237
238         if (node_old) {
239                 system_del_iprule(rule_old);
240                 free(rule_old);
241         }
242
243         if (node_new)
244                 system_add_iprule(rule_new);
245 }
246
247 static void __init
248 iprule_init_list(void)
249 {
250         system_flush_iprules();
251         vlist_init(&iprules, rule_cmp, iprule_update_rule);
252 }