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