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