ip_flow.c revision 1.3 1 /* $NetBSD: ip_flow.c,v 1.3 1998/05/04 19:24:53 matt 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/proc.h>
51
52 #include <vm/vm.h>
53 #include <sys/sysctl.h>
54
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/route.h>
58 #include <net/pfil.h>
59
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/in_pcb.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip_var.h>
66
67 #define IPFLOW_TIMER (5 * PR_SLOWHZ)
68 #define IPFLOW_HASHSIZE (1 << IPFLOW_HASHBITS)
69 static LIST_HEAD(ipflowhead, ipflow) ipflows[IPFLOW_HASHSIZE];
70 static int ipflow_inuse;
71 #ifndef IPFLOW_MAX
72 #define IPFLOW_MAX 256
73 #endif
74 int ip_maxflows = IPFLOW_MAX;
75
76 static unsigned
77 ipflow_hash(
78 struct in_addr dst,
79 struct in_addr src,
80 unsigned tos)
81 {
82 unsigned hash = tos;
83 int idx;
84 for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS)
85 hash += (dst.s_addr >> (32 - idx)) + (src.s_addr >> idx);
86 return hash & (IPFLOW_HASHSIZE-1);
87 }
88
89 static struct ipflow *
90 ipflow_lookup(
91 const struct ip *ip)
92 {
93 unsigned hash;
94 struct ipflow *ipf;
95
96 hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
97
98 ipf = LIST_FIRST(&ipflows[hash]);
99 while (ipf != NULL) {
100 if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr
101 && ip->ip_src.s_addr == ipf->ipf_src.s_addr
102 && ip->ip_tos == ipf->ipf_tos)
103 break;
104 ipf = LIST_NEXT(ipf, ipf_next);
105 }
106 return ipf;
107 }
108
109 int
110 ipflow_fastforward(
111 struct mbuf *m)
112 {
113 struct ip *ip;
114 struct ipflow *ipf;
115 struct rtentry *rt;
116 int error;
117
118 /*
119 * Are we forwarding packets? Big enough for an IP packet?
120 */
121 if (!ipforwarding || ipflow_inuse == 0 || m->m_len < sizeof(struct ip))
122 return 0;
123 /*
124 * IP header with no option and valid version and length
125 */
126 ip = mtod(m, struct ip *);
127 if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2)
128 || ntohs(ip->ip_len) > m->m_pkthdr.len)
129 return 0;
130 /*
131 * Find a flow.
132 */
133 if ((ipf = ipflow_lookup(ip)) == NULL)
134 return 0;
135
136 /*
137 * Veryify the IP header checksum.
138 */
139 if (in_cksum(m, sizeof(struct ip)) != 0)
140 return 0;
141
142 /*
143 * Route and interface still up?
144 */
145 rt = ipf->ipf_ro.ro_rt;
146 if ((rt->rt_flags & RTF_UP) == 0 || (rt->rt_ifp->if_flags & IFF_UP) == 0)
147 return 0;
148
149 /*
150 * Packet size OK? TTL?
151 */
152 if (m->m_pkthdr.len > rt->rt_ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
153 return 0;
154
155 /*
156 * Everything checks out and so we can forward this packet.
157 * Modify the TTL and incrementally change the checksum.
158 * On little endian machine, the TTL is in LSB position
159 * (so we can simply add) while on big-endian it's in the
160 * MSB position (so we have to do two calculation; the first
161 * is the add and second is to wrap the results into 17 bits,
162 * 16 bits and a carry).
163 */
164 ip->ip_ttl -= IPTTLDEC;
165 if (ip->ip_sum >= htons(0xffff - (IPTTLDEC << 8))) {
166 ip->ip_sum += htons(IPTTLDEC << 8) + 1;
167 } else {
168 ip->ip_sum += htons(IPTTLDEC << 8);
169 }
170
171 /*
172 * Send the packet on it's way. All we can get back is ENOBUFS
173 */
174 ipf->ipf_uses++;
175 ipf->ipf_timer = IPFLOW_TIMER;
176 if ((error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m, &ipf->ipf_ro.ro_dst, rt)) != 0) {
177 if (error == ENOBUFS)
178 ipf->ipf_dropped++;
179 else
180 ipf->ipf_errors++;
181 }
182 return 1;
183 }
184
185 static void
187 ipflow_addstats(
188 struct ipflow *ipf)
189 {
190 ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
191 ipstat.ips_cantforward += ipf->ipf_errors + ipf->ipf_dropped;
192 ipstat.ips_forward += ipf->ipf_uses;
193 ipstat.ips_fastforward += ipf->ipf_uses;
194 }
195
196 static void
197 ipflow_free(
198 struct ipflow *ipf)
199 {
200 int s;
201 /*
202 * Remove the flow from the hash table (at elevated IPL).
203 * Once it's off the list, we can deal with it at normal
204 * network IPL.
205 */
206 s = splimp();
207 LIST_REMOVE(ipf, ipf_next);
208 splx(s);
209 ipflow_addstats(ipf);
210 RTFREE(ipf->ipf_ro.ro_rt);
211 ipflow_inuse--;
212 FREE(ipf, M_IPFLOW);
213 }
214
215 struct ipflow *
216 ipflow_reap(
217 int just_one)
218 {
219 while (just_one || ipflow_inuse > ip_maxflows) {
220 struct ipflow *ipf, *maybe_ipf = NULL;
221 int idx;
222 int s;
223
224 for (idx = 0; idx < IPFLOW_HASHSIZE; idx++) {
225 ipf = LIST_FIRST(&ipflows[idx]);
226 while (ipf != NULL) {
227 /*
228 * If this no longer points to a valid route
229 * reclaim it.
230 */
231 if ((ipf->ipf_ro.ro_rt->rt_flags & RTF_UP) == 0)
232 goto done;
233 /*
234 * choose the one that's been least recently
235 * used or has had the least uses in the
236 * last 1.5 intervals.
237 */
238 if (ipf == NULL
239 || ipf->ipf_timer < maybe_ipf->ipf_timer
240 || (ipf->ipf_timer == maybe_ipf->ipf_timer
241 && ipf->ipf_last_uses + ipf->ipf_uses <
242 maybe_ipf->ipf_last_uses +
243 maybe_ipf->ipf_uses))
244 maybe_ipf = ipf;
245 ipf = LIST_NEXT(ipf, ipf_next);
246 }
247 }
248 ipf = maybe_ipf;
249 done:
250 /*
251 * Remove the entry from the flow table.
252 */
253 s = splimp();
254 LIST_REMOVE(ipf, ipf_next);
255 splx(s);
256 ipflow_addstats(ipf);
257 RTFREE(ipf->ipf_ro.ro_rt);
258 if (just_one)
259 return ipf;
260 FREE(ipf, M_IPFLOW);
261 ipflow_inuse--;
262 }
263 return NULL;
264 }
265
266 void
267 ipflow_slowtimo(
268 void)
269 {
270 struct ipflow *ipf;
271 int idx;
272 int inuse = ipflow_inuse;
273
274 /*
275 * Save ourselves some work if we know there aren't any flows.
276 */
277 for (idx = 0; inuse > 0 && idx < IPFLOW_HASHSIZE; idx++) {
278 ipf = LIST_FIRST(&ipflows[idx]);
279 while (ipf != NULL) {
280 struct ipflow *next_ipf = LIST_NEXT(ipf, ipf_next);
281 if (--ipf->ipf_timer == 0) {
282 ipflow_free(ipf);
283 } else {
284 ipf->ipf_last_uses = ipf->ipf_uses;
285 ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
286 ipstat.ips_forward += ipf->ipf_uses;
287 ipstat.ips_fastforward += ipf->ipf_uses;
288 ipf->ipf_uses = 0;
289 inuse--;
290 }
291 ipf = next_ipf;
292 }
293 }
294 }
295
296 void
297 ipflow_create(
298 const struct route *ro,
299 struct mbuf *m)
300 {
301 const struct ip *const ip = mtod(m, struct ip *);
302 struct ipflow *ipf;
303 unsigned hash;
304 int s;
305
306 /*
307 * Don't create cache entries for ICMP messages.
308 */
309 if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP)
310 return;
311 /*
312 * See if an existing flow struct exists. If so remove it from it's
313 * list and free the old route. If not, try to malloc a new one
314 * (if we aren't at our limit).
315 */
316 ipf = ipflow_lookup(ip);
317 if (ipf == NULL) {
318 if (ipflow_inuse >= ip_maxflows) {
319 ipf = ipflow_reap(1);
320 } else {
321 ipf = (struct ipflow *) malloc(sizeof(*ipf), M_IPFLOW,
322 M_NOWAIT);
323 if (ipf == NULL)
324 return;
325 ipflow_inuse++;
326 }
327 bzero((caddr_t) ipf, sizeof(*ipf));
328 } else {
329 s = splimp();
330 LIST_REMOVE(ipf, ipf_next);
331 splx(s);
332 ipflow_addstats(ipf);
333 RTFREE(ipf->ipf_ro.ro_rt);
334 ipf->ipf_uses = ipf->ipf_last_uses = 0;
335 ipf->ipf_errors = ipf->ipf_dropped = 0;
336 }
337
338 /*
339 * Fill in the updated information.
340 */
341 ipf->ipf_ro = *ro;
342 ro->ro_rt->rt_refcnt++;
343 ipf->ipf_dst = ip->ip_dst;
344 ipf->ipf_src = ip->ip_src;
345 ipf->ipf_tos = ip->ip_tos;
346 ipf->ipf_timer = IPFLOW_TIMER;
347 ipf->ipf_start = time.tv_sec;
348 /*
349 * Insert into the approriate bucket of the flow table.
350 */
351 hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
352 s = splimp();
353 LIST_INSERT_HEAD(&ipflows[hash], ipf, ipf_next);
354 splx(s);
355 }
356