ip6_flow.c revision 1.1 1 /* $NetBSD: ip6_flow.c,v 1.1 2007/03/07 22:20:04 liamjfoy Exp $ */
2
3 /*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by the 3am Software Foundry ("3am"). It was developed by Liam J. Foy
9 * <liamjfoy (at) netbsd.org> and Matt Thomas <matt (at) netbsd.org>.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 *
39 * IPv6 version was developed by Liam J. Foy. Original source existed in IPv4
40 * format developed by Matt Thomas. Thanks to Joerg Sonnenberger, Matt
41 * Thomas and Christos Zoulas.
42 *
43 * Thanks to Liverpool John Moores University, especially Dr. David Llewellyn-Jones
44 * for providing resources (to test) and Professor Madjid Merabti.
45 */
46
47 #include <sys/cdefs.h>
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/domain.h>
54 #include <sys/protosw.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/time.h>
58 #include <sys/kernel.h>
59 #include <sys/pool.h>
60 #include <sys/sysctl.h>
61
62 #include <net/if.h>
63 #include <net/if_dl.h>
64 #include <net/route.h>
65 #include <net/pfil.h>
66
67 #include <netinet/in.h>
68 #include <netinet/in_route.h>
69 #include <netinet6/in6_var.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip6.h>
72 #include <netinet6/ip6_var.h>
73
74 /*
75 * IPv6 Fast Forward caches/hashes flows from one source to destination.
76 *
77 * Upon a successful forward IPv6FF caches and hashes details such as the
78 * route, source and destination. Once another packet is received matching
79 * the source and destination the packet is forwarded straight onto if_output
80 * using the cached details.
81 *
82 * Example:
83 * ether/fddi_input -> ip6flow_fastfoward -> if_output
84 */
85
86 POOL_INIT(ip6flow_pool, sizeof(struct ip6flow), 0, 0, 0, "ip6flowpl", NULL);
87
88 LIST_HEAD(ip6flowhead, ip6flow);
89
90 /*
91 * We could use IPv4 defines (IPFLOW_HASHBITS) but we'll
92 * use our own (possibly for future expansion).
93 */
94 #define IP6FLOW_TIMER (5 * PR_SLOWHZ)
95 #define IP6FLOW_HASHSIZE (1 << IP6FLOW_HASHBITS)
96
97 static struct ip6flowhead ip6flowtable[IP6FLOW_HASHSIZE];
98 static struct ip6flowhead ip6flowlist;
99 static int ip6flow_inuse;
100
101 /*
102 * Insert an ip6flow into the list.
103 */
104 #define IP6FLOW_INSERT(bucket, ip6f) \
105 do { \
106 LIST_INSERT_HEAD((bucket), (ip6f), ip6f_hash); \
107 LIST_INSERT_HEAD(&ip6flowlist, (ip6f), ip6f_list); \
108 } while (/*CONSTCOND*/ 0)
109
110 /*
111 * Remove an ip6flow from the list.
112 */
113 #define IP6FLOW_REMOVE(ip6f) \
114 do { \
115 LIST_REMOVE((ip6f), ip6f_hash); \
116 LIST_REMOVE((ip6f), ip6f_list); \
117 } while (/*CONSTCOND*/ 0)
118
119 #ifndef IP6FLOW_DEFAULT
120 #define IP6FLOW_DEFAULT 256
121 #endif
122
123 int ip6_maxflows = IP6FLOW_DEFAULT;
124
125 /*
126 * Calculate hash table position.
127 */
128 static size_t
129 ip6flow_hash(struct ip6_hdr *ip6)
130 {
131 size_t hash;
132 uint32_t dst_sum, src_sum;
133 int idx;
134
135 src_sum = ip6->ip6_src.s6_addr32[0] + ip6->ip6_src.s6_addr32[1]
136 + ip6->ip6_src.s6_addr32[2] + ip6->ip6_src.s6_addr32[3];
137 dst_sum = ip6->ip6_dst.s6_addr32[0] + ip6->ip6_dst.s6_addr32[1]
138 + ip6->ip6_dst.s6_addr32[2] + ip6->ip6_dst.s6_addr32[3];
139
140 hash = ip6->ip6_flow;
141
142 for (idx = 0; idx < 32; idx += IP6FLOW_HASHBITS)
143 hash += (dst_sum >> (32 - idx)) + (src_sum >> idx);
144
145 return hash & (IP6FLOW_HASHSIZE-1);
146 }
147
148 /*
149 * Check to see if a flow already exists - if so return it.
150 */
151 static struct ip6flow *
152 ip6flow_lookup(struct ip6_hdr *ip6)
153 {
154 size_t hash;
155 struct ip6flow *ip6f;
156
157 hash = ip6flow_hash(ip6);
158
159 LIST_FOREACH(ip6f, &ip6flowlist, ip6f_list) {
160 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6f->ip6f_dst)
161 && IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6f->ip6f_src)
162 && ip6f->ip6f_flow == ip6->ip6_flow) {
163 /* A cached flow has been found. */
164 return ip6f;
165 }
166 }
167
168 return NULL;
169 }
170
171 /*
172 * Initalise lists.
173 */
174 void
175 ip6flow_init(void)
176 {
177 size_t i;
178
179 LIST_INIT(&ip6flowlist);
180 for (i = 0; i < IP6FLOW_HASHSIZE; i++)
181 LIST_INIT(&ip6flowtable[i]);
182 }
183
184 /*
185 * IPv6 Fast Forward routine. Attempt to forward the packet -
186 * if any problems are found return to the main IPv6 input
187 * routine to deal with.
188 */
189 int
190 ip6flow_fastforward(struct mbuf *m)
191 {
192 struct ip6flow *ip6f;
193 struct ip6_hdr *ip6;
194 struct rtentry *rt;
195 struct sockaddr_in6 *dst;
196 int error;
197
198 /*
199 * Are we forwarding packets and have flows?
200 */
201 if (!ip6_forwarding || ip6flow_inuse == 0)
202 return 0;
203
204 /*
205 * At least size of IPv6 Header?
206 */
207 if (m->m_len < sizeof(struct ip6_hdr))
208 return 0;
209 /*
210 * Was packet received as a link-level multicast or broadcast?
211 * If so, don't try to fast forward.
212 */
213 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
214 return 0;
215
216 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
217 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
218 (max_linkhdr + 3) & ~3)) == NULL) {
219 return 0;
220 }
221 } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
222 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
223 return 0;
224 }
225 }
226
227 ip6 = mtod(m, struct ip6_hdr *);
228
229 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
230 /* Bad version. */
231 return 0;
232 }
233
234 /*
235 * If we have a hop-by-hop extension we must process it.
236 * We just leave this up to ip6_input to deal with.
237 */
238 if (ip6->ip6_nxt == IPPROTO_HOPOPTS)
239 return 0;
240
241 /*
242 * Attempt to find a flow.
243 */
244 if ((ip6f = ip6flow_lookup(ip6)) == NULL) {
245 /* No flow found. */
246 return 0;
247 }
248
249 /*
250 * Route and interface still up?
251 */
252 rtcache_check((struct route *)&ip6f->ip6f_ro);
253 rt = ip6f->ip6f_ro.ro_rt;
254 if (rt == NULL || (rt->rt_ifp->if_flags & IFF_UP) == 0) {
255 /* Route or interface is down */
256 return 0;
257 }
258
259 /*
260 * Packet size greater than MTU?
261 */
262 if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
263 /* Return to main IPv6 input function. */
264 return 0;
265 }
266
267 if (ip6->ip6_hlim <= IPV6_HLIMDEC)
268 return 0;
269
270 /* Decrement hop limit (same as TTL) */
271 ip6->ip6_hlim -= IPV6_HLIMDEC;
272
273 if (rt->rt_flags & RTF_GATEWAY)
274 dst = (struct sockaddr_in6 *)rt->rt_gateway;
275 else
276 dst = &ip6f->ip6f_ro.ro_dst;
277
278 PRT_SLOW_ARM(ip6f->ip6f_timer, IP6FLOW_TIMER);
279
280 ip6f->ip6f_uses++;
281
282 /* Send on its way - straight to the interface output routine. */
283 if ((error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m,
284 (struct sockaddr *)dst, rt)) != 0) {
285 ip6f->ip6f_dropped++;
286 } else {
287 ip6f->ip6f_forwarded++;
288 }
289
290 return 1;
291 }
292
293 /*
294 * Add the IPv6 flow statistics to the main IPv6 statistics.
295 */
296 static void
297 ip6flow_addstats(struct ip6flow *ip6f)
298 {
299 rtcache_check((struct route *)&ip6f->ip6f_ro);
300 if (ip6f->ip6f_ro.ro_rt != NULL)
301 ip6f->ip6f_ro.ro_rt->rt_use += ip6f->ip6f_uses;
302 ip6stat.ip6s_fastforwardflows = ip6flow_inuse;
303 ip6stat.ip6s_cantforward += ip6f->ip6f_dropped;
304 ip6stat.ip6s_odropped += ip6f->ip6f_dropped;
305 ip6stat.ip6s_total += ip6f->ip6f_uses;
306 ip6stat.ip6s_forward += ip6f->ip6f_forwarded;
307 ip6stat.ip6s_fastforward += ip6f->ip6f_forwarded;
308 }
309
310 /*
311 * Add statistics and free the flow.
312 */
313 static void
314 ip6flow_free(struct ip6flow *ip6f)
315 {
316 int s;
317
318 /*
319 * Remove the flow from the hash table (at elevated IPL).
320 * Once it's off the list, we can deal with it at normal
321 * network IPL.
322 */
323 s = splnet();
324 IP6FLOW_REMOVE(ip6f);
325 splx(s);
326 ip6flow_inuse--;
327 ip6flow_addstats(ip6f);
328 rtcache_free((struct route *)&ip6f->ip6f_ro);
329 pool_put(&ip6flow_pool, ip6f);
330 }
331
332 /*
333 * Reap one or more flows - ip6flow_reap may remove
334 * multiple flows if net.inet6.ip6.maxflows is reduced.
335 */
336 struct ip6flow *
337 ip6flow_reap(int just_one)
338 {
339 while (just_one || ip6flow_inuse > ip6_maxflows) {
340 struct ip6flow *ip6f, *maybe_ip6f = NULL;
341 int s;
342
343 ip6f = LIST_FIRST(&ip6flowlist);
344 while (ip6f != NULL) {
345 /*
346 * If this no longer points to a valid route -
347 * reclaim it.
348 */
349 rtcache_check((struct route *)&ip6f->ip6f_ro);
350 if (ip6f->ip6f_ro.ro_rt == NULL)
351 goto done;
352 /*
353 * choose the one that's been least recently
354 * used or has had the least uses in the
355 * last 1.5 intervals.
356 */
357 if (maybe_ip6f == NULL ||
358 ip6f->ip6f_timer < maybe_ip6f->ip6f_timer ||
359 (ip6f->ip6f_timer == maybe_ip6f->ip6f_timer &&
360 ip6f->ip6f_last_uses + ip6f->ip6f_uses <
361 maybe_ip6f->ip6f_last_uses +
362 maybe_ip6f->ip6f_uses))
363 maybe_ip6f = ip6f;
364 ip6f = LIST_NEXT(ip6f, ip6f_list);
365 }
366 ip6f = maybe_ip6f;
367 done:
368 /*
369 * Remove the entry from the flow table
370 */
371 s = splnet();
372 IP6FLOW_REMOVE(ip6f);
373 splx(s);
374 rtcache_free((struct route *)&ip6f->ip6f_ro);
375 if (just_one) {
376 ip6flow_addstats(ip6f);
377 return ip6f;
378 }
379 ip6flow_inuse--;
380 ip6flow_addstats(ip6f);
381 pool_put(&ip6flow_pool, ip6f);
382 }
383 return NULL;
384 }
385
386 void
387 ip6flow_slowtimo(void)
388 {
389 struct ip6flow *ip6f, *next_ip6f;
390
391 for (ip6f = LIST_FIRST(&ip6flowlist); ip6f != NULL; ip6f = next_ip6f) {
392 next_ip6f = LIST_NEXT(ip6f, ip6f_list);
393 rtcache_check((struct route *)&ip6f->ip6f_ro);
394 if (PRT_SLOW_ISEXPIRED(ip6f->ip6f_timer) ||
395 ip6f->ip6f_ro.ro_rt == NULL) {
396 ip6flow_free(ip6f);
397 } else {
398 ip6f->ip6f_last_uses = ip6f->ip6f_uses;
399 ip6flow_addstats(ip6f);
400 ip6f->ip6f_uses = 0;
401 ip6f->ip6f_dropped = 0;
402 ip6f->ip6f_forwarded = 0;
403 }
404 }
405 }
406
407 /*
408 * We have successfully forwarded a packet using the normal
409 * IPv6 stack. Now create/update a flow.
410 */
411 void
412 ip6flow_create(const struct route_in6 *ro, struct mbuf *m)
413 {
414 struct ip6_hdr *ip6;
415 struct ip6flow *ip6f;
416 size_t hash;
417 int s;
418
419 ip6 = mtod(m, struct ip6_hdr *);
420
421 /*
422 * If IPv6 Fast Forward is disabled, don't create a flow.
423 * It can be disabled by setting net.inet6.ip6.maxflows to 0.
424 *
425 * Don't create a flow for ICMPv6 messages.
426 */
427 if (ip6_maxflows == 0 || ip6->ip6_nxt == IPPROTO_IPV6_ICMP)
428 return;
429
430 /*
431 * See if an existing flow exists. If so:
432 * - Remove the flow
433 * - Add flow statistics
434 * - Free the route
435 * - Reset statistics
436 *
437 * If a flow doesn't exist allocate a new one if
438 * ip6_maxflows hasn't reached its limit. If it has
439 * been reached, reap some flows.
440 */
441 ip6f = ip6flow_lookup(ip6);
442 if (ip6f == NULL) {
443 if (ip6flow_inuse >= ip6_maxflows) {
444 ip6f = ip6flow_reap(1);
445 } else {
446 ip6f = pool_get(&ip6flow_pool, PR_NOWAIT);
447 if (ip6f == NULL)
448 return;
449 ip6flow_inuse++;
450 }
451 memset(ip6f, 0, sizeof(*ip6f));
452 } else {
453 s = splnet();
454 IP6FLOW_REMOVE(ip6f);
455 splx(s);
456 ip6flow_addstats(ip6f);
457 rtcache_free((struct route *)&ip6f->ip6f_ro);
458 ip6f->ip6f_uses = 0;
459 ip6f->ip6f_last_uses = 0;
460 ip6f->ip6f_dropped = 0;
461 ip6f->ip6f_forwarded = 0;
462 }
463
464 /*
465 * Fill in the updated/new details.
466 */
467 rtcache_copy((struct route *)&ip6f->ip6f_ro, (const struct route *)ro,
468 sizeof(ip6f->ip6f_ro));
469 ip6f->ip6f_dst = ip6->ip6_dst;
470 ip6f->ip6f_src = ip6->ip6_src;
471 ip6f->ip6f_flow = ip6->ip6_flow;
472 PRT_SLOW_ARM(ip6f->ip6f_timer, IP6FLOW_TIMER);
473 ip6f->ip6f_start = time_uptime;
474
475 /*
476 * Insert into the approriate bucket of the flow table.
477 */
478 hash = ip6flow_hash(ip6);
479 s = splnet();
480 IP6FLOW_INSERT(&ip6flowtable[hash], ip6f);
481 splx(s);
482 }
483
484 /*
485 * Invalidate/remove all flows.
486 */
487 void
488 ip6flow_invalidate_all(void)
489 {
490 struct ip6flow *ip6f, *next_ip6f;
491 int s;
492
493 s = splnet();
494 for (ip6f = LIST_FIRST(&ip6flowlist); ip6f != NULL; ip6f = next_ip6f) {
495 next_ip6f = LIST_NEXT(ip6f, ip6f_list);
496 ip6flow_free(ip6f);
497 }
498 splx(s);
499 }
500