dp-packet: use dp_packet_reset_offsets during initialisation
[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;
90     struct in6_addr gw6;
91
92     in6_addr_set_mapped_ipv4(&ip6_dst, ip_dst);
93     if (ovs_router_lookup(&ip6_dst, output_bridge, &gw6)) {
94         *gw = in6_addr_get_mapped_ipv4(&gw6);
95         return true;
96     }
97     return route_table_fallback_lookup(ip_dst, output_bridge, gw);
98 }
99
100 static void
101 rt_entry_free(struct ovs_router_entry *p)
102 {
103     cls_rule_destroy(&p->cr);
104     free(p);
105 }
106
107 static void rt_init_match(struct match *match, const struct in6_addr *ip6_dst,
108                           uint8_t plen)
109 {
110     struct in6_addr dst;
111     struct in6_addr mask;
112
113     mask = ipv6_create_mask(plen);
114
115     dst = ipv6_addr_bitand(ip6_dst, &mask);
116     memset(match, 0, sizeof *match);
117     match->flow.ipv6_dst = dst;
118     match->wc.masks.ipv6_dst = mask;
119 }
120
121 static void
122 ovs_router_insert__(uint8_t priority, const struct in6_addr *ip6_dst,
123                     uint8_t plen, const char output_bridge[],
124                     const struct in6_addr *gw)
125 {
126     const struct cls_rule *cr;
127     struct ovs_router_entry *p;
128     struct match match;
129
130     rt_init_match(&match, ip6_dst, plen);
131
132     p = xzalloc(sizeof *p);
133     ovs_strlcpy(p->output_bridge, output_bridge, sizeof p->output_bridge);
134     if (ipv6_addr_is_set(gw)) {
135         p->gw = *gw;
136     }
137     p->nw_addr = match.flow.ipv6_dst;
138     p->plen = plen;
139     p->priority = priority;
140     /* Longest prefix matches first. */
141     cls_rule_init(&p->cr, &match, priority);
142
143     ovs_mutex_lock(&mutex);
144     cr = classifier_replace(&cls, &p->cr, CLS_MIN_VERSION, NULL, 0);
145     ovs_mutex_unlock(&mutex);
146
147     if (cr) {
148         /* An old rule with the same match was displaced. */
149         ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
150     }
151     tnl_port_map_insert_ipdev(output_bridge);
152     seq_change(tnl_conf_seq);
153 }
154
155 void
156 ovs_router_insert(const struct in6_addr *ip_dst, uint8_t plen,
157                   const char output_bridge[], const struct in6_addr *gw)
158 {
159     ovs_router_insert__(plen, ip_dst, plen, output_bridge, gw);
160 }
161
162
163 static bool
164 __rt_entry_delete(const struct cls_rule *cr)
165 {
166     struct ovs_router_entry *p = ovs_router_entry_cast(cr);
167
168     tnl_port_map_delete_ipdev(p->output_bridge);
169     /* Remove it. */
170     cr = classifier_remove(&cls, cr);
171     if (cr) {
172         ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
173         return true;
174     }
175     return false;
176 }
177
178 static bool
179 rt_entry_delete(uint8_t priority, const struct in6_addr *ip6_dst, uint8_t plen)
180 {
181     const struct cls_rule *cr;
182     struct cls_rule rule;
183     struct match match;
184     bool res = false;
185
186     rt_init_match(&match, ip6_dst, plen);
187
188     cls_rule_init(&rule, &match, priority);
189
190     /* Find the exact rule. */
191     cr = classifier_find_rule_exactly(&cls, &rule, CLS_MAX_VERSION);
192     if (cr) {
193         ovs_mutex_lock(&mutex);
194         res = __rt_entry_delete(cr);
195         ovs_mutex_unlock(&mutex);
196     }
197     return res;
198 }
199
200 static bool
201 scan_ipv6_route(const char *s, struct in6_addr *addr, unsigned int *plen)
202 {
203     struct in6_addr mask;
204     char *error;
205
206     error = ipv6_parse_masked(s, addr, &mask);
207     if (error) {
208         free(error);
209         return false;
210     }
211
212     if (!ipv6_is_cidr(&mask)) {
213         return false;
214     }
215
216     *plen = ipv6_count_cidr_bits(&mask);
217
218     return true;
219 }
220
221 static bool
222 scan_ipv4_route(const char *s, ovs_be32 *addr, unsigned int *plen)
223 {
224     int len, max_plen, n;
225     int slen = strlen(s);
226     uint8_t *ip = (uint8_t *)addr;
227
228     *addr = htonl(0);
229     if (!ovs_scan(s, "%"SCNu8"%n", &ip[0], &n)) {
230         return false;
231     }
232     len = n;
233     max_plen = 8;
234     for (int i = 1; i < 4; i++) {
235         if (ovs_scan(s + len, ".%"SCNu8"%n", &ip[i], &n)) {
236             len += n;
237             max_plen += 8;
238         } else {
239             break;
240         }
241     }
242     if (len == slen && max_plen == 32) {
243         *plen = 32;
244         return true;
245     }
246     if (ovs_scan(s + len, "/%u%n", plen, &n)
247         && len + n == slen && *plen <= max_plen) {
248         return true;
249     }
250     return false;
251 }
252
253 static void
254 ovs_router_add(struct unixctl_conn *conn, int argc,
255               const char *argv[], void *aux OVS_UNUSED)
256 {
257     ovs_be32 ip, gw;
258     unsigned int plen;
259     struct in6_addr ip6;
260     struct in6_addr gw6;
261
262     if (scan_ipv4_route(argv[1], &ip, &plen)) {
263         if (argc > 3) {
264             inet_pton(AF_INET, argv[3], (struct in_addr *)&gw);
265         } else {
266             gw = 0;
267         }
268         in6_addr_set_mapped_ipv4(&ip6, ip);
269         in6_addr_set_mapped_ipv4(&gw6, gw);
270         plen += 96;
271     } else if (scan_ipv6_route(argv[1], &ip6, &plen)) {
272         if (argc > 3) {
273             inet_pton(AF_INET6, argv[3], &gw6);
274         } else {
275             gw6 = in6addr_any;
276         }
277     } else {
278         unixctl_command_reply_error(conn, "Invalid parameters");
279         return;
280     }
281     ovs_router_insert__(plen + 32, &ip6, plen, argv[2], &gw6);
282     unixctl_command_reply(conn, "OK");
283 }
284
285 static void
286 ovs_router_del(struct unixctl_conn *conn, int argc OVS_UNUSED,
287               const char *argv[], void *aux OVS_UNUSED)
288 {
289     ovs_be32 ip;
290     unsigned int plen;
291     struct in6_addr ip6;
292
293     if (scan_ipv4_route(argv[1], &ip, &plen)) {
294         in6_addr_set_mapped_ipv4(&ip6, ip);
295         plen += 96;
296     } else if (!scan_ipv6_route(argv[1], &ip6, &plen)) {
297         unixctl_command_reply_error(conn, "Invalid parameters");
298         return;
299     }
300     if (rt_entry_delete(plen + 32, &ip6, plen)) {
301         unixctl_command_reply(conn, "OK");
302         seq_change(tnl_conf_seq);
303     } else {
304         unixctl_command_reply_error(conn, "Not found");
305     }
306 }
307
308 static void
309 ovs_router_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
310                const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
311 {
312     struct ovs_router_entry *rt;
313     struct ds ds = DS_EMPTY_INITIALIZER;
314
315     ds_put_format(&ds, "Route Table:\n");
316     CLS_FOR_EACH(rt, cr, &cls) {
317         uint8_t plen;
318         if (rt->priority == rt->plen) {
319             ds_put_format(&ds, "Cached: ");
320         } else {
321             ds_put_format(&ds, "User: ");
322         }
323         ipv6_format_mapped(&rt->nw_addr, &ds);
324         plen = rt->plen;
325         if (IN6_IS_ADDR_V4MAPPED(&rt->nw_addr)) {
326             plen -= 96;
327         }
328         ds_put_format(&ds, "/%"PRIu16" dev %s", plen, rt->output_bridge);
329         if (ipv6_addr_is_set(&rt->gw)) {
330             ds_put_format(&ds, " GW ");
331             ipv6_format_mapped(&rt->gw, &ds);
332         }
333         ds_put_format(&ds, "\n");
334     }
335     unixctl_command_reply(conn, ds_cstr(&ds));
336     ds_destroy(&ds);
337 }
338
339 static void
340 ovs_router_lookup_cmd(struct unixctl_conn *conn, int argc OVS_UNUSED,
341                       const char *argv[], void *aux OVS_UNUSED)
342 {
343     ovs_be32 ip;
344     struct in6_addr ip6;
345     unsigned int plen;
346     char iface[IFNAMSIZ];
347     struct in6_addr gw;
348
349     if (scan_ipv4_route(argv[1], &ip, &plen) && plen == 32) {
350         in6_addr_set_mapped_ipv4(&ip6, ip);
351     } else if (!(scan_ipv6_route(argv[1], &ip6, &plen) && plen == 128)) {
352         unixctl_command_reply_error(conn, "Invalid parameters");
353         return;
354     }
355
356     if (ovs_router_lookup(&ip6, iface, &gw)) {
357         struct ds ds = DS_EMPTY_INITIALIZER;
358         ds_put_format(&ds, "gateway ");
359         ipv6_format_mapped(&ip6, &ds);
360         ds_put_format(&ds, "\ndev %s\n", iface);
361         unixctl_command_reply(conn, ds_cstr(&ds));
362         ds_destroy(&ds);
363     } else {
364         unixctl_command_reply_error(conn, "Not found");
365     }
366 }
367
368 void
369 ovs_router_flush(void)
370 {
371     struct ovs_router_entry *rt;
372
373     ovs_mutex_lock(&mutex);
374     classifier_defer(&cls);
375     CLS_FOR_EACH(rt, cr, &cls) {
376         if (rt->priority == rt->plen) {
377             __rt_entry_delete(&rt->cr);
378         }
379     }
380     classifier_publish(&cls);
381     ovs_mutex_unlock(&mutex);
382     seq_change(tnl_conf_seq);
383 }
384
385 /* May not be called more than once. */
386 void
387 ovs_router_init(void)
388 {
389     classifier_init(&cls, NULL);
390     unixctl_command_register("ovs/route/add", "ip_addr/prefix_len out_br_name gw", 2, 3,
391                              ovs_router_add, NULL);
392     unixctl_command_register("ovs/route/show", "", 0, 0, ovs_router_show, NULL);
393     unixctl_command_register("ovs/route/del", "ip_addr/prefix_len", 1, 1, ovs_router_del,
394                              NULL);
395     unixctl_command_register("ovs/route/lookup", "ip_addr", 1, 1,
396                              ovs_router_lookup_cmd, NULL);
397 }