packets: Introduce in6_addr_mapped_ipv4() and use where appropriate.
[cascardo/ovs.git] / lib / ovs-router.c
1 /*
2  * Copyright (c) 2014, 2015 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "ovs-router.h"
20
21 #include <arpa/inet.h>
22 #include <errno.h>
23 #include <inttypes.h>
24 #include <sys/socket.h>
25 #include <net/if.h>
26 #include <netinet/in.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include "classifier.h"
33 #include "command-line.h"
34 #include "compiler.h"
35 #include "dpif.h"
36 #include "dynamic-string.h"
37 #include "netdev.h"
38 #include "packets.h"
39 #include "seq.h"
40 #include "ovs-thread.h"
41 #include "route-table.h"
42 #include "tnl-ports.h"
43 #include "unixctl.h"
44 #include "util.h"
45
46 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
47 static struct classifier cls;
48
49 struct ovs_router_entry {
50     struct cls_rule cr;
51     char output_bridge[IFNAMSIZ];
52     struct in6_addr gw;
53     struct in6_addr nw_addr;
54     uint8_t plen;
55     uint8_t priority;
56 };
57
58 static struct ovs_router_entry *
59 ovs_router_entry_cast(const struct cls_rule *cr)
60 {
61     if (offsetof(struct ovs_router_entry, cr) == 0) {
62         return CONTAINER_OF(cr, struct ovs_router_entry, cr);
63     } else {
64         return cr ? CONTAINER_OF(cr, struct ovs_router_entry, cr) : NULL;
65     }
66 }
67
68 bool
69 ovs_router_lookup(const struct in6_addr *ip6_dst, char output_bridge[],
70                   struct in6_addr *gw)
71 {
72     const struct cls_rule *cr;
73     struct flow flow = {.ipv6_dst = *ip6_dst};
74
75     cr = classifier_lookup(&cls, CLS_MAX_VERSION, &flow, NULL);
76     if (cr) {
77         struct ovs_router_entry *p = ovs_router_entry_cast(cr);
78
79         ovs_strlcpy(output_bridge, p->output_bridge, IFNAMSIZ);
80         *gw = p->gw;
81         return true;
82     }
83     return false;
84 }
85
86 bool
87 ovs_router_lookup4(ovs_be32 ip_dst, char output_bridge[], ovs_be32 *gw)
88 {
89     struct in6_addr ip6_dst = in6_addr_mapped_ipv4(ip_dst);
90     struct in6_addr gw6;
91
92     if (ovs_router_lookup(&ip6_dst, output_bridge, &gw6)) {
93         *gw = in6_addr_get_mapped_ipv4(&gw6);
94         return true;
95     }
96     return route_table_fallback_lookup(ip_dst, output_bridge, gw);
97 }
98
99 static void
100 rt_entry_free(struct ovs_router_entry *p)
101 {
102     cls_rule_destroy(&p->cr);
103     free(p);
104 }
105
106 static void rt_init_match(struct match *match, const struct in6_addr *ip6_dst,
107                           uint8_t plen)
108 {
109     struct in6_addr dst;
110     struct in6_addr mask;
111
112     mask = ipv6_create_mask(plen);
113
114     dst = ipv6_addr_bitand(ip6_dst, &mask);
115     memset(match, 0, sizeof *match);
116     match->flow.ipv6_dst = dst;
117     match->wc.masks.ipv6_dst = mask;
118 }
119
120 static void
121 ovs_router_insert__(uint8_t priority, const struct in6_addr *ip6_dst,
122                     uint8_t plen, const char output_bridge[],
123                     const struct in6_addr *gw)
124 {
125     const struct cls_rule *cr;
126     struct ovs_router_entry *p;
127     struct match match;
128
129     rt_init_match(&match, ip6_dst, plen);
130
131     p = xzalloc(sizeof *p);
132     ovs_strlcpy(p->output_bridge, output_bridge, sizeof p->output_bridge);
133     if (ipv6_addr_is_set(gw)) {
134         p->gw = *gw;
135     }
136     p->nw_addr = match.flow.ipv6_dst;
137     p->plen = plen;
138     p->priority = priority;
139     /* Longest prefix matches first. */
140     cls_rule_init(&p->cr, &match, priority);
141
142     ovs_mutex_lock(&mutex);
143     cr = classifier_replace(&cls, &p->cr, CLS_MIN_VERSION, NULL, 0);
144     ovs_mutex_unlock(&mutex);
145
146     if (cr) {
147         /* An old rule with the same match was displaced. */
148         ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
149     }
150     tnl_port_map_insert_ipdev(output_bridge);
151     seq_change(tnl_conf_seq);
152 }
153
154 void
155 ovs_router_insert(const struct in6_addr *ip_dst, uint8_t plen,
156                   const char output_bridge[], const struct in6_addr *gw)
157 {
158     ovs_router_insert__(plen, ip_dst, plen, output_bridge, gw);
159 }
160
161
162 static bool
163 __rt_entry_delete(const struct cls_rule *cr)
164 {
165     struct ovs_router_entry *p = ovs_router_entry_cast(cr);
166
167     tnl_port_map_delete_ipdev(p->output_bridge);
168     /* Remove it. */
169     cr = classifier_remove(&cls, cr);
170     if (cr) {
171         ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
172         return true;
173     }
174     return false;
175 }
176
177 static bool
178 rt_entry_delete(uint8_t priority, const struct in6_addr *ip6_dst, uint8_t plen)
179 {
180     const struct cls_rule *cr;
181     struct cls_rule rule;
182     struct match match;
183     bool res = false;
184
185     rt_init_match(&match, ip6_dst, plen);
186
187     cls_rule_init(&rule, &match, priority);
188
189     /* Find the exact rule. */
190     cr = classifier_find_rule_exactly(&cls, &rule, CLS_MAX_VERSION);
191     if (cr) {
192         ovs_mutex_lock(&mutex);
193         res = __rt_entry_delete(cr);
194         ovs_mutex_unlock(&mutex);
195     }
196     return res;
197 }
198
199 static bool
200 scan_ipv6_route(const char *s, struct in6_addr *addr, unsigned int *plen)
201 {
202     struct in6_addr mask;
203     char *error;
204
205     error = ipv6_parse_masked(s, addr, &mask);
206     if (error) {
207         free(error);
208         return false;
209     }
210
211     if (!ipv6_is_cidr(&mask)) {
212         return false;
213     }
214
215     *plen = ipv6_count_cidr_bits(&mask);
216
217     return true;
218 }
219
220 static bool
221 scan_ipv4_route(const char *s, ovs_be32 *addr, unsigned int *plen)
222 {
223     int len, max_plen, n;
224     int slen = strlen(s);
225     uint8_t *ip = (uint8_t *)addr;
226
227     *addr = htonl(0);
228     if (!ovs_scan(s, "%"SCNu8"%n", &ip[0], &n)) {
229         return false;
230     }
231     len = n;
232     max_plen = 8;
233     for (int i = 1; i < 4; i++) {
234         if (ovs_scan(s + len, ".%"SCNu8"%n", &ip[i], &n)) {
235             len += n;
236             max_plen += 8;
237         } else {
238             break;
239         }
240     }
241     if (len == slen && max_plen == 32) {
242         *plen = 32;
243         return true;
244     }
245     if (ovs_scan(s + len, "/%u%n", plen, &n)
246         && len + n == slen && *plen <= max_plen) {
247         return true;
248     }
249     return false;
250 }
251
252 static void
253 ovs_router_add(struct unixctl_conn *conn, int argc,
254               const char *argv[], void *aux OVS_UNUSED)
255 {
256     ovs_be32 ip, gw;
257     unsigned int plen;
258     struct in6_addr ip6;
259     struct in6_addr gw6;
260
261     if (scan_ipv4_route(argv[1], &ip, &plen)) {
262         if (argc > 3) {
263             inet_pton(AF_INET, argv[3], (struct in_addr *)&gw);
264         } else {
265             gw = 0;
266         }
267         in6_addr_set_mapped_ipv4(&ip6, ip);
268         in6_addr_set_mapped_ipv4(&gw6, gw);
269         plen += 96;
270     } else if (scan_ipv6_route(argv[1], &ip6, &plen)) {
271         if (argc > 3) {
272             inet_pton(AF_INET6, argv[3], &gw6);
273         } else {
274             gw6 = in6addr_any;
275         }
276     } else {
277         unixctl_command_reply_error(conn, "Invalid parameters");
278         return;
279     }
280     ovs_router_insert__(plen + 32, &ip6, plen, argv[2], &gw6);
281     unixctl_command_reply(conn, "OK");
282 }
283
284 static void
285 ovs_router_del(struct unixctl_conn *conn, int argc OVS_UNUSED,
286               const char *argv[], void *aux OVS_UNUSED)
287 {
288     ovs_be32 ip;
289     unsigned int plen;
290     struct in6_addr ip6;
291
292     if (scan_ipv4_route(argv[1], &ip, &plen)) {
293         in6_addr_set_mapped_ipv4(&ip6, ip);
294         plen += 96;
295     } else if (!scan_ipv6_route(argv[1], &ip6, &plen)) {
296         unixctl_command_reply_error(conn, "Invalid parameters");
297         return;
298     }
299     if (rt_entry_delete(plen + 32, &ip6, plen)) {
300         unixctl_command_reply(conn, "OK");
301         seq_change(tnl_conf_seq);
302     } else {
303         unixctl_command_reply_error(conn, "Not found");
304     }
305 }
306
307 static void
308 ovs_router_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
309                const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
310 {
311     struct ovs_router_entry *rt;
312     struct ds ds = DS_EMPTY_INITIALIZER;
313
314     ds_put_format(&ds, "Route Table:\n");
315     CLS_FOR_EACH(rt, cr, &cls) {
316         uint8_t plen;
317         if (rt->priority == rt->plen) {
318             ds_put_format(&ds, "Cached: ");
319         } else {
320             ds_put_format(&ds, "User: ");
321         }
322         ipv6_format_mapped(&rt->nw_addr, &ds);
323         plen = rt->plen;
324         if (IN6_IS_ADDR_V4MAPPED(&rt->nw_addr)) {
325             plen -= 96;
326         }
327         ds_put_format(&ds, "/%"PRIu16" dev %s", plen, rt->output_bridge);
328         if (ipv6_addr_is_set(&rt->gw)) {
329             ds_put_format(&ds, " GW ");
330             ipv6_format_mapped(&rt->gw, &ds);
331         }
332         ds_put_format(&ds, "\n");
333     }
334     unixctl_command_reply(conn, ds_cstr(&ds));
335     ds_destroy(&ds);
336 }
337
338 static void
339 ovs_router_lookup_cmd(struct unixctl_conn *conn, int argc OVS_UNUSED,
340                       const char *argv[], void *aux OVS_UNUSED)
341 {
342     ovs_be32 ip;
343     struct in6_addr ip6;
344     unsigned int plen;
345     char iface[IFNAMSIZ];
346     struct in6_addr gw;
347
348     if (scan_ipv4_route(argv[1], &ip, &plen) && plen == 32) {
349         in6_addr_set_mapped_ipv4(&ip6, ip);
350     } else if (!(scan_ipv6_route(argv[1], &ip6, &plen) && plen == 128)) {
351         unixctl_command_reply_error(conn, "Invalid parameters");
352         return;
353     }
354
355     if (ovs_router_lookup(&ip6, iface, &gw)) {
356         struct ds ds = DS_EMPTY_INITIALIZER;
357         ds_put_format(&ds, "gateway ");
358         ipv6_format_mapped(&ip6, &ds);
359         ds_put_format(&ds, "\ndev %s\n", iface);
360         unixctl_command_reply(conn, ds_cstr(&ds));
361         ds_destroy(&ds);
362     } else {
363         unixctl_command_reply_error(conn, "Not found");
364     }
365 }
366
367 void
368 ovs_router_flush(void)
369 {
370     struct ovs_router_entry *rt;
371
372     ovs_mutex_lock(&mutex);
373     classifier_defer(&cls);
374     CLS_FOR_EACH(rt, cr, &cls) {
375         if (rt->priority == rt->plen) {
376             __rt_entry_delete(&rt->cr);
377         }
378     }
379     classifier_publish(&cls);
380     ovs_mutex_unlock(&mutex);
381     seq_change(tnl_conf_seq);
382 }
383
384 /* May not be called more than once. */
385 void
386 ovs_router_init(void)
387 {
388     classifier_init(&cls, NULL);
389     unixctl_command_register("ovs/route/add", "ip_addr/prefix_len out_br_name gw", 2, 3,
390                              ovs_router_add, NULL);
391     unixctl_command_register("ovs/route/show", "", 0, 0, ovs_router_show, NULL);
392     unixctl_command_register("ovs/route/del", "ip_addr/prefix_len", 1, 1, ovs_router_del,
393                              NULL);
394     unixctl_command_register("ovs/route/lookup", "ip_addr", 1, 1,
395                              ovs_router_lookup_cmd, NULL);
396 }