ip_flow.c revision 1.45 1 /* $NetBSD: ip_flow.c,v 1.45 2007/04/05 18:11:47 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.45 2007/04/05 18:11:47 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 /*
70 * Similar code is very well commented in netinet6/ip6_flow.c
71 */
72
73 POOL_INIT(ipflow_pool, sizeof(struct ipflow), 0, 0, 0, "ipflowpl", NULL,
74 IPL_NET);
75
76 LIST_HEAD(ipflowhead, ipflow);
77
78 #define IPFLOW_TIMER (5 * PR_SLOWHZ)
79 #define IPFLOW_DEFAULT_HASHSIZE (1 << IPFLOW_HASHBITS)
80
81 static struct ipflowhead *ipflowtable = NULL;
82 static struct ipflowhead ipflowlist;
83 static int ipflow_inuse;
84
85 #define IPFLOW_INSERT(bucket, ipf) \
86 do { \
87 LIST_INSERT_HEAD((bucket), (ipf), ipf_hash); \
88 LIST_INSERT_HEAD(&ipflowlist, (ipf), ipf_list); \
89 } while (/*CONSTCOND*/ 0)
90
91 #define IPFLOW_REMOVE(ipf) \
92 do { \
93 LIST_REMOVE((ipf), ipf_hash); \
94 LIST_REMOVE((ipf), ipf_list); \
95 } while (/*CONSTCOND*/ 0)
96
97 #ifndef IPFLOW_MAX
98 #define IPFLOW_MAX 256
99 #endif
100 int ip_maxflows = IPFLOW_MAX;
101 int ip_hashsize = IPFLOW_DEFAULT_HASHSIZE;
102
103 static size_t
104 ipflow_hash(struct ip *ip)
105 {
106 size_t hash = ip->ip_tos;
107 size_t idx;
108
109 for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS) {
110 hash += (ip->ip_dst.s_addr >> (32 - idx)) +
111 (ip->ip_src.s_addr >> idx);
112 }
113
114 return hash & (ip_hashsize-1);
115 }
116
117 static struct ipflow *
118 ipflow_lookup(struct ip *ip)
119 {
120 size_t hash;
121 struct ipflow *ipf;
122
123 hash = ipflow_hash(ip);
124
125 LIST_FOREACH(ipf, &ipflowtable[hash], ipf_hash) {
126 if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr
127 && ip->ip_src.s_addr == ipf->ipf_src.s_addr
128 && ip->ip_tos == ipf->ipf_tos)
129 break;
130 }
131 return ipf;
132 }
133
134 int
135 ipflow_init(int table_size)
136 {
137 struct ipflowhead *new_table;
138 size_t i;
139
140 new_table = (struct ipflowhead *)malloc(sizeof(struct ipflowhead) *
141 table_size, M_RTABLE, M_NOWAIT);
142
143 if (new_table == NULL)
144 return 1;
145
146 if (ipflowtable != NULL)
147 free(ipflowtable, M_RTABLE);
148
149 ipflowtable = new_table;
150 ip_hashsize = table_size;
151
152 LIST_INIT(&ipflowlist);
153 for (i = 0; i < ip_hashsize; i++)
154 LIST_INIT(&ipflowtable[i]);
155
156 return 0;
157 }
158
159 int
160 ipflow_fastforward(struct mbuf *m)
161 {
162 struct ip *ip, ip_store;
163 struct ipflow *ipf;
164 struct rtentry *rt;
165 const struct sockaddr *dst;
166 int error;
167 int iplen;
168
169 /*
170 * Are we forwarding packets? Big enough for an IP packet?
171 */
172 if (!ipforwarding || ipflow_inuse == 0 || m->m_len < sizeof(struct ip))
173 return 0;
174
175 /*
176 * Was packet received as a link-level multicast or broadcast?
177 * If so, don't try to fast forward..
178 */
179 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
180 return 0;
181
182 /*
183 * IP header with no option and valid version and length
184 */
185 if (IP_HDR_ALIGNED_P(mtod(m, void *)))
186 ip = mtod(m, struct ip *);
187 else {
188 memcpy(&ip_store, mtod(m, void *), sizeof(ip_store));
189 ip = &ip_store;
190 }
191 iplen = ntohs(ip->ip_len);
192 if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
193 iplen < sizeof(struct ip) || iplen > m->m_pkthdr.len)
194 return 0;
195 /*
196 * Find a flow.
197 */
198 if ((ipf = ipflow_lookup(ip)) == NULL)
199 return 0;
200
201 /*
202 * Verify the IP header checksum.
203 */
204 switch (m->m_pkthdr.csum_flags &
205 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
206 M_CSUM_IPv4_BAD)) {
207 case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
208 return (0);
209
210 case M_CSUM_IPv4:
211 /* Checksum was okay. */
212 break;
213
214 default:
215 /* Must compute it ourselves. */
216 if (in_cksum(m, sizeof(struct ip)) != 0)
217 return (0);
218 break;
219 }
220
221 /*
222 * Route and interface still up?
223 */
224 rtcache_check(&ipf->ipf_ro);
225 rt = ipf->ipf_ro.ro_rt;
226 if (rt == NULL || (rt->rt_ifp->if_flags & IFF_UP) == 0)
227 return 0;
228
229 /*
230 * Packet size OK? TTL?
231 */
232 if (m->m_pkthdr.len > rt->rt_ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
233 return 0;
234
235 /*
236 * Clear any in-bound checksum flags for this packet.
237 */
238 m->m_pkthdr.csum_flags = 0;
239
240 /*
241 * Everything checks out and so we can forward this packet.
242 * Modify the TTL and incrementally change the checksum.
243 *
244 * This method of adding the checksum works on either endian CPU.
245 * If htons() is inlined, all the arithmetic is folded; otherwise
246 * the htons()s are combined by CSE due to the const attribute.
247 *
248 * Don't bother using HW checksumming here -- the incremental
249 * update is pretty fast.
250 */
251 ip->ip_ttl -= IPTTLDEC;
252 if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
253 ip->ip_sum -= ~htons(IPTTLDEC << 8);
254 else
255 ip->ip_sum += htons(IPTTLDEC << 8);
256
257 /*
258 * Done modifying the header; copy it back, if necessary.
259 */
260 if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0)
261 memcpy(mtod(m, void *), &ip_store, sizeof(ip_store));
262
263 /*
264 * Trim the packet in case it's too long..
265 */
266 if (m->m_pkthdr.len > iplen) {
267 if (m->m_len == m->m_pkthdr.len) {
268 m->m_len = iplen;
269 m->m_pkthdr.len = iplen;
270 } else
271 m_adj(m, iplen - m->m_pkthdr.len);
272 }
273
274 /*
275 * Send the packet on it's way. All we can get back is ENOBUFS
276 */
277 ipf->ipf_uses++;
278 PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
279
280 if (rt->rt_flags & RTF_GATEWAY)
281 dst = rt->rt_gateway;
282 else
283 dst = rtcache_getdst(&ipf->ipf_ro);
284
285 if ((error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m, dst, rt)) != 0) {
286 if (error == ENOBUFS)
287 ipf->ipf_dropped++;
288 else
289 ipf->ipf_errors++;
290 }
291 return 1;
292 }
293
294 static void
296 ipflow_addstats(struct ipflow *ipf)
297 {
298 rtcache_check(&ipf->ipf_ro);
299 if (ipf->ipf_ro.ro_rt != NULL)
300 ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
301 ipstat.ips_cantforward += ipf->ipf_errors + ipf->ipf_dropped;
302 ipstat.ips_total += ipf->ipf_uses;
303 ipstat.ips_forward += ipf->ipf_uses;
304 ipstat.ips_fastforward += ipf->ipf_uses;
305 }
306
307 static void
308 ipflow_free(struct ipflow *ipf)
309 {
310 int s;
311 /*
312 * Remove the flow from the hash table (at elevated IPL).
313 * Once it's off the list, we can deal with it at normal
314 * network IPL.
315 */
316 s = splnet();
317 IPFLOW_REMOVE(ipf);
318 splx(s);
319 ipflow_addstats(ipf);
320 rtcache_free(&ipf->ipf_ro);
321 ipflow_inuse--;
322 s = splnet();
323 pool_put(&ipflow_pool, ipf);
324 splx(s);
325 }
326
327 struct ipflow *
328 ipflow_reap(int just_one)
329 {
330 while (just_one || ipflow_inuse > ip_maxflows) {
331 struct ipflow *ipf, *maybe_ipf = NULL;
332 int s;
333
334 ipf = LIST_FIRST(&ipflowlist);
335 while (ipf != NULL) {
336 /*
337 * If this no longer points to a valid route
338 * reclaim it.
339 */
340 rtcache_check(&ipf->ipf_ro);
341 if (ipf->ipf_ro.ro_rt == NULL)
342 goto done;
343 /*
344 * choose the one that's been least recently
345 * used or has had the least uses in the
346 * last 1.5 intervals.
347 */
348 if (maybe_ipf == NULL ||
349 ipf->ipf_timer < maybe_ipf->ipf_timer ||
350 (ipf->ipf_timer == maybe_ipf->ipf_timer &&
351 ipf->ipf_last_uses + ipf->ipf_uses <
352 maybe_ipf->ipf_last_uses +
353 maybe_ipf->ipf_uses))
354 maybe_ipf = ipf;
355 ipf = LIST_NEXT(ipf, ipf_list);
356 }
357 ipf = maybe_ipf;
358 done:
359 /*
360 * Remove the entry from the flow table.
361 */
362 s = splnet();
363 IPFLOW_REMOVE(ipf);
364 splx(s);
365 ipflow_addstats(ipf);
366 rtcache_free(&ipf->ipf_ro);
367 if (just_one)
368 return ipf;
369 pool_put(&ipflow_pool, ipf);
370 ipflow_inuse--;
371 }
372 return NULL;
373 }
374
375 void
376 ipflow_slowtimo(void)
377 {
378 struct ipflow *ipf, *next_ipf;
379
380 for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
381 next_ipf = LIST_NEXT(ipf, ipf_list);
382 rtcache_check(&ipf->ipf_ro);
383 if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer) ||
384 ipf->ipf_ro.ro_rt == NULL) {
385 ipflow_free(ipf);
386 } else {
387 ipf->ipf_last_uses = ipf->ipf_uses;
388 ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
389 ipstat.ips_total += ipf->ipf_uses;
390 ipstat.ips_forward += ipf->ipf_uses;
391 ipstat.ips_fastforward += ipf->ipf_uses;
392 ipf->ipf_uses = 0;
393 }
394 }
395 }
396
397 void
398 ipflow_create(const struct route *ro, struct mbuf *m)
399 {
400 struct ip *const ip = mtod(m, struct ip *);
401 struct ipflow *ipf;
402 size_t hash;
403 int s;
404
405 /*
406 * Don't create cache entries for ICMP messages.
407 */
408 if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP)
409 return;
410 /*
411 * See if an existing flow struct exists. If so remove it from it's
412 * list and free the old route. If not, try to malloc a new one
413 * (if we aren't at our limit).
414 */
415 ipf = ipflow_lookup(ip);
416 if (ipf == NULL) {
417 if (ipflow_inuse >= ip_maxflows) {
418 ipf = ipflow_reap(1);
419 } else {
420 s = splnet();
421 ipf = pool_get(&ipflow_pool, PR_NOWAIT);
422 splx(s);
423 if (ipf == NULL)
424 return;
425 ipflow_inuse++;
426 }
427 memset(ipf, 0, sizeof(*ipf));
428 } else {
429 s = splnet();
430 IPFLOW_REMOVE(ipf);
431 splx(s);
432 ipflow_addstats(ipf);
433 rtcache_free(&ipf->ipf_ro);
434 ipf->ipf_uses = ipf->ipf_last_uses = 0;
435 ipf->ipf_errors = ipf->ipf_dropped = 0;
436 }
437
438 /*
439 * Fill in the updated information.
440 */
441 rtcache_copy(&ipf->ipf_ro, ro, sizeof(ipf->ipf_ro));
442 ipf->ipf_dst = ip->ip_dst;
443 ipf->ipf_src = ip->ip_src;
444 ipf->ipf_tos = ip->ip_tos;
445 PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
446 ipf->ipf_start = time_uptime;
447 /*
448 * Insert into the approriate bucket of the flow table.
449 */
450 hash = ipflow_hash(ip);
451 s = splnet();
452 IPFLOW_INSERT(&ipflowtable[hash], ipf);
453 splx(s);
454 }
455
456 int
457 ipflow_invalidate_all(int new_size)
458 {
459 struct ipflow *ipf, *next_ipf;
460 int s, error;
461
462 error = 0;
463 s = splnet();
464 for (ipf = LIST_FIRST(&ipflowlist); ipf != NULL; ipf = next_ipf) {
465 next_ipf = LIST_NEXT(ipf, ipf_list);
466 ipflow_free(ipf);
467 }
468
469 if (new_size)
470 error = ipflow_init(new_size);
471 splx(s);
472
473 return error;
474 }
475