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