ip_flow.c revision 1.43 1 /* $NetBSD: ip_flow.c,v 1.43 2007/03/25 20:12:20 liamjfoy Exp $ */
2
3 /*-
4 * Copyright (c) 1998 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 Matt Thomas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: ip_flow.c,v 1.43 2007/03/25 20:12:20 liamjfoy Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/errno.h>
51 #include <sys/time.h>
52 #include <sys/kernel.h>
53 #include <sys/pool.h>
54 #include <sys/sysctl.h>
55
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/route.h>
59 #include <net/pfil.h>
60
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/in_route.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip_var.h>
68
69 POOL_INIT(ipflow_pool, sizeof(struct ipflow), 0, 0, 0, "ipflowpl", NULL,
70 IPL_NET);
71
72 LIST_HEAD(ipflowhead, ipflow);
73
74 #define IPFLOW_TIMER (5 * PR_SLOWHZ)
75 #define IPFLOW_DEFAULT_HASHSIZE (1 << IPFLOW_HASHBITS)
76
77 static struct ipflowhead *ipflowtable = NULL;
78 static struct ipflowhead ipflowlist;
79 static int ipflow_inuse;
80
81 #define IPFLOW_INSERT(bucket, ipf) \
82 do { \
83 LIST_INSERT_HEAD((bucket), (ipf), ipf_hash); \
84 LIST_INSERT_HEAD(&ipflowlist, (ipf), ipf_list); \
85 } while (/*CONSTCOND*/ 0)
86
87 #define IPFLOW_REMOVE(ipf) \
88 do { \
89 LIST_REMOVE((ipf), ipf_hash); \
90 LIST_REMOVE((ipf), ipf_list); \
91 } while (/*CONSTCOND*/ 0)
92
93 #ifndef IPFLOW_MAX
94 #define IPFLOW_MAX 256
95 #endif
96 int ip_maxflows = IPFLOW_MAX;
97 int ip_hashsize = IPFLOW_DEFAULT_HASHSIZE;
98
99 static unsigned
100 ipflow_hash(struct in_addr dst, struct in_addr src, unsigned tos)
101 {
102 unsigned hash = tos;
103 int idx;
104 for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS)
105 hash += (dst.s_addr >> (32 - idx)) + (src.s_addr >> idx);
106 return hash & (ip_hashsize-1);
107 }
108
109 static struct ipflow *
110 ipflow_lookup(const struct ip *ip)
111 {
112 unsigned hash;
113 struct ipflow *ipf;
114
115 hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
116
117 LIST_FOREACH(ipf, &ipflowtable[hash], ipf_hash) {
118 if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr
119 && ip->ip_src.s_addr == ipf->ipf_src.s_addr
120 && ip->ip_tos == ipf->ipf_tos)
121 break;
122 }
123 return ipf;
124 }
125
126 int
127 ipflow_init(int table_size)
128 {
129 struct ipflowhead *new_table;
130 int i;
131
132 new_table = (struct ipflowhead *)malloc(sizeof(struct ipflowhead) *
133 table_size, M_RTABLE, M_NOWAIT);
134
135 if (new_table == NULL)
136 return 1;
137
138 if (ipflowtable != NULL)
139 free(ipflowtable, M_RTABLE);
140
141 ipflowtable = new_table;
142 ip_hashsize = table_size;
143
144 LIST_INIT(&ipflowlist);
145 for (i = 0; i < ip_hashsize; i++)
146 LIST_INIT(&ipflowtable[i]);
147
148 return 0;
149 }
150
151 int
152 ipflow_fastforward(struct mbuf *m)
153 {
154 struct ip *ip, ip_store;
155 struct ipflow *ipf;
156 struct rtentry *rt;
157 const struct sockaddr *dst;
158 int error;
159 int iplen;
160
161 /*
162 * Are we forwarding packets? Big enough for an IP packet?
163 */
164 if (!ipforwarding || ipflow_inuse == 0 || m->m_len < sizeof(struct ip))
165 return 0;
166
167 /*
168 * Was packet received as a link-level multicast or broadcast?
169 * If so, don't try to fast forward..
170 */
171 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
172 return 0;
173
174 /*
175 * IP header with no option and valid version and length
176 */
177 if (IP_HDR_ALIGNED_P(mtod(m, void *)))
178 ip = mtod(m, struct ip *);
179 else {
180 memcpy(&ip_store, mtod(m, void *), sizeof(ip_store));
181 ip = &ip_store;
182 }
183 iplen = ntohs(ip->ip_len);
184 if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
185 iplen < sizeof(struct ip) || iplen > m->m_pkthdr.len)
186 return 0;
187 /*
188 * Find a flow.
189 */
190 if ((ipf = ipflow_lookup(ip)) == NULL)
191 return 0;
192
193 /*
194 * Verify the IP header checksum.
195 */
196 switch (m->m_pkthdr.csum_flags &
197 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
198 M_CSUM_IPv4_BAD)) {
199 case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
200 return (0);
201
202 case M_CSUM_IPv4:
203 /* Checksum was okay. */
204 break;
205
206 default:
207 /* Must compute it ourselves. */
208 if (in_cksum(m, sizeof(struct ip)) != 0)
209 return (0);
210 break;
211 }
212
213 /*
214 * Route and interface still up?
215 */
216 rtcache_check(&ipf->ipf_ro);
217 rt = ipf->ipf_ro.ro_rt;
218 if (rt == NULL || (rt->rt_ifp->if_flags & IFF_UP) == 0)
219 return 0;
220
221 /*
222 * Packet size OK? TTL?
223 */
224 if (m->m_pkthdr.len > rt->rt_ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
225 return 0;
226
227 /*
228 * Clear any in-bound checksum flags for this packet.
229 */
230 m->m_pkthdr.csum_flags = 0;
231
232 /*
233 * Everything checks out and so we can forward this packet.
234 * Modify the TTL and incrementally change the checksum.
235 *
236 * This method of adding the checksum works on either endian CPU.
237 * If htons() is inlined, all the arithmetic is folded; otherwise
238 * the htons()s are combined by CSE due to the const attribute.
239 *
240 * Don't bother using HW checksumming here -- the incremental
241 * update is pretty fast.
242 */
243 ip->ip_ttl -= IPTTLDEC;
244 if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
245 ip->ip_sum -= ~htons(IPTTLDEC << 8);
246 else
247 ip->ip_sum += htons(IPTTLDEC << 8);
248
249 /*
250 * Done modifying the header; copy it back, if necessary.
251 */
252 if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0)
253 memcpy(mtod(m, void *), &ip_store, sizeof(ip_store));
254
255 /*
256 * Trim the packet in case it's too long..
257 */
258 if (m->m_pkthdr.len > iplen) {
259 if (m->m_len == m->m_pkthdr.len) {
260 m->m_len = iplen;
261 m->m_pkthdr.len = iplen;
262 } else
263 m_adj(m, iplen - m->m_pkthdr.len);
264 }
265
266 /*
267 * Send the packet on it's way. All we can get back is ENOBUFS
268 */
269 ipf->ipf_uses++;
270 PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
271
272 if (rt->rt_flags & RTF_GATEWAY)
273 dst = rt->rt_gateway;
274 else
275 dst = rtcache_getdst(&ipf->ipf_ro);
276
277 if ((error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m, dst, rt)) != 0) {
278 if (error == ENOBUFS)
279 ipf->ipf_dropped++;
280 else
281 ipf->ipf_errors++;
282 }
283 return 1;
284 }
285
286 static void
288 ipflow_addstats(struct ipflow *ipf)
289 {
290 rtcache_check(&ipf->ipf_ro);
291 if (ipf->ipf_ro.ro_rt != NULL)
292 ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
293 ipstat.ips_cantforward += ipf->ipf_errors + ipf->ipf_dropped;
294 ipstat.ips_total += ipf->ipf_uses;
295 ipstat.ips_forward += ipf->ipf_uses;
296 ipstat.ips_fastforward += ipf->ipf_uses;
297 }
298
299 static void
300 ipflow_free(struct ipflow *ipf)
301 {
302 int s;
303 /*
304 * Remove the flow from the hash table (at elevated IPL).
305 * Once it's off the list, we can deal with it at normal
306 * network IPL.
307 */
308 s = splnet();
309 IPFLOW_REMOVE(ipf);
310 splx(s);
311 ipflow_addstats(ipf);
312 rtcache_free(&ipf->ipf_ro);
313 ipflow_inuse--;
314 s = splnet();
315 pool_put(&ipflow_pool, ipf);
316 splx(s);
317 }
318
319 struct ipflow *
320 ipflow_reap(int just_one)
321 {
322 while (just_one || ipflow_inuse > ip_maxflows) {
323 struct ipflow *ipf, *maybe_ipf = NULL;
324 int s;
325
326 ipf = LIST_FIRST(&ipflowlist);
327 while (ipf != NULL) {
328 /*
329 * If this no longer points to a valid route
330 * reclaim it.
331 */
332 rtcache_check(&ipf->ipf_ro);
333 if (ipf->ipf_ro.ro_rt == NULL)
334 goto done;
335 /*
336 * choose the one that's been least recently
337 * used or has had the least uses in the
338 * last 1.5 intervals.
339 */
340 if (maybe_ipf == NULL ||
341 ipf->ipf_timer < maybe_ipf->ipf_timer ||
342 (ipf->ipf_timer == maybe_ipf->ipf_timer &&
343 ipf->ipf_last_uses + ipf->ipf_uses <
344 maybe_ipf->ipf_last_uses +
345 maybe_ipf->ipf_uses))
346 maybe_ipf = ipf;
347 ipf = LIST_NEXT(ipf, ipf_list);
348 }
349 ipf = maybe_ipf;
350 done:
351 /*
352 * Remove the entry from the flow table.
353 */
354 s = splnet();
355 IPFLOW_REMOVE(ipf);
356 splx(s);
357 ipflow_addstats(ipf);
358 rtcache_free(&ipf->ipf_ro);
359 if (just_one)
360 return ipf;
361 pool_put(&ipflow_pool, ipf);
362 ipflow_inuse--;
363 }
364 return NULL;
365 }
366
367 void
368 ipflow_slowtimo(void)
369 {
370 struct ipflow *ipf, *next_ipf;
371
372 for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
373 next_ipf = LIST_NEXT(ipf, ipf_list);
374 rtcache_check(&ipf->ipf_ro);
375 if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer) ||
376 ipf->ipf_ro.ro_rt == NULL) {
377 ipflow_free(ipf);
378 } else {
379 ipf->ipf_last_uses = ipf->ipf_uses;
380 ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
381 ipstat.ips_total += ipf->ipf_uses;
382 ipstat.ips_forward += ipf->ipf_uses;
383 ipstat.ips_fastforward += ipf->ipf_uses;
384 ipf->ipf_uses = 0;
385 }
386 }
387 }
388
389 void
390 ipflow_create(const struct route *ro, struct mbuf *m)
391 {
392 const struct ip *const ip = mtod(m, struct ip *);
393 struct ipflow *ipf;
394 unsigned hash;
395 int s;
396
397 /*
398 * Don't create cache entries for ICMP messages.
399 */
400 if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP)
401 return;
402 /*
403 * See if an existing flow struct exists. If so remove it from it's
404 * list and free the old route. If not, try to malloc a new one
405 * (if we aren't at our limit).
406 */
407 ipf = ipflow_lookup(ip);
408 if (ipf == NULL) {
409 if (ipflow_inuse >= ip_maxflows) {
410 ipf = ipflow_reap(1);
411 } else {
412 s = splnet();
413 ipf = pool_get(&ipflow_pool, PR_NOWAIT);
414 splx(s);
415 if (ipf == NULL)
416 return;
417 ipflow_inuse++;
418 }
419 memset(ipf, 0, sizeof(*ipf));
420 } else {
421 s = splnet();
422 IPFLOW_REMOVE(ipf);
423 splx(s);
424 ipflow_addstats(ipf);
425 rtcache_free(&ipf->ipf_ro);
426 ipf->ipf_uses = ipf->ipf_last_uses = 0;
427 ipf->ipf_errors = ipf->ipf_dropped = 0;
428 }
429
430 /*
431 * Fill in the updated information.
432 */
433 rtcache_copy(&ipf->ipf_ro, ro, sizeof(ipf->ipf_ro));
434 ipf->ipf_dst = ip->ip_dst;
435 ipf->ipf_src = ip->ip_src;
436 ipf->ipf_tos = ip->ip_tos;
437 PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
438 ipf->ipf_start = time_uptime;
439 /*
440 * Insert into the approriate bucket of the flow table.
441 */
442 hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
443 s = splnet();
444 IPFLOW_INSERT(&ipflowtable[hash], ipf);
445 splx(s);
446 }
447
448 int
449 ipflow_invalidate_all(int new_size)
450 {
451 struct ipflow *ipf, *next_ipf;
452 int s, error;
453
454 error = 0;
455 s = splnet();
456 for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
457 next_ipf = LIST_NEXT(ipf, ipf_list);
458 ipflow_free(ipf);
459 }
460
461 if (new_size)
462 error = ipflow_init(new_size);
463 splx(s);
464
465 return error;
466 }
467