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