ip_flow.c revision 1.6 1 /* $NetBSD: ip_flow.c,v 1.6 1998/06/10 00:47:57 sommerfe 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 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 int
127 ipflow_fastforward(
128 struct mbuf *m)
129 {
130 struct ip *ip;
131 struct ipflow *ipf;
132 struct rtentry *rt;
133 int error;
134 int iplen;
135
136 /*
137 * Are we forwarding packets? Big enough for an IP packet?
138 */
139 if (!ipforwarding || ipflow_inuse == 0 || m->m_len < sizeof(struct ip))
140 return 0;
141 /*
142 * IP header with no option and valid version and length
143 */
144 ip = mtod(m, struct ip *);
145 iplen = ntohs(ip->ip_len);
146 if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
147 iplen > m->m_pkthdr.len)
148 return 0;
149 /*
150 * Find a flow.
151 */
152 if ((ipf = ipflow_lookup(ip)) == NULL)
153 return 0;
154
155 /*
156 * Veryify the IP header checksum.
157 */
158 if (in_cksum(m, sizeof(struct ip)) != 0)
159 return 0;
160
161 /*
162 * Route and interface still up?
163 */
164 rt = ipf->ipf_ro.ro_rt;
165 if ((rt->rt_flags & RTF_UP) == 0 ||
166 (rt->rt_ifp->if_flags & IFF_UP) == 0)
167 return 0;
168
169 /*
170 * Packet size OK? TTL?
171 */
172 if (m->m_pkthdr.len > rt->rt_ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
173 return 0;
174
175 /*
176 * Everything checks out and so we can forward this packet.
177 * Modify the TTL and incrementally change the checksum.
178 * On little endian machine, the TTL is in LSB position
179 * (so we can simply add) while on big-endian it's in the
180 * MSB position (so we have to do two calculation; the first
181 * is the add and second is to wrap the results into 17 bits,
182 * 16 bits and a carry).
183 */
184 ip->ip_ttl -= IPTTLDEC;
185 if (ip->ip_sum >= htons(0xffff - (IPTTLDEC << 8))) {
186 ip->ip_sum += htons(IPTTLDEC << 8) + 1;
187 } else {
188 ip->ip_sum += htons(IPTTLDEC << 8);
189 }
190
191 /*
192 * Trim the packet in case it's too long..
193 */
194 if (m->m_pkthdr.len > iplen) {
195 if (m->m_len == m->m_pkthdr.len) {
196 m->m_len = iplen;
197 m->m_pkthdr.len = iplen;
198 } else
199 m_adj(m, iplen - m->m_pkthdr.len);
200 }
201
202 /*
203 * Send the packet on it's way. All we can get back is ENOBUFS
204 */
205 ipf->ipf_uses++;
206 PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
207 if ((error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m,
208 &ipf->ipf_ro.ro_dst, rt)) != 0) {
209 if (error == ENOBUFS)
210 ipf->ipf_dropped++;
211 else
212 ipf->ipf_errors++;
213 }
214 return 1;
215 }
216
217 static void
219 ipflow_addstats(
220 struct ipflow *ipf)
221 {
222 ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
223 ipstat.ips_cantforward += ipf->ipf_errors + ipf->ipf_dropped;
224 ipstat.ips_forward += ipf->ipf_uses;
225 ipstat.ips_fastforward += ipf->ipf_uses;
226 }
227
228 static void
229 ipflow_free(
230 struct ipflow *ipf)
231 {
232 int s;
233 /*
234 * Remove the flow from the hash table (at elevated IPL).
235 * Once it's off the list, we can deal with it at normal
236 * network IPL.
237 */
238 s = splimp();
239 IPFLOW_REMOVE(ipf);
240 splx(s);
241 ipflow_addstats(ipf);
242 RTFREE(ipf->ipf_ro.ro_rt);
243 ipflow_inuse--;
244 FREE(ipf, M_IPFLOW);
245 }
246
247 struct ipflow *
248 ipflow_reap(
249 int just_one)
250 {
251 while (just_one || ipflow_inuse > ip_maxflows) {
252 struct ipflow *ipf, *maybe_ipf = NULL;
253 int s;
254
255 ipf = LIST_FIRST(&ipflowlist);
256 while (ipf != NULL) {
257 /*
258 * If this no longer points to a valid route
259 * reclaim it.
260 */
261 if ((ipf->ipf_ro.ro_rt->rt_flags & RTF_UP) == 0)
262 goto done;
263 /*
264 * choose the one that's been least recently
265 * used or has had the least uses in the
266 * last 1.5 intervals.
267 */
268 if (maybe_ipf == NULL ||
269 ipf->ipf_timer < maybe_ipf->ipf_timer ||
270 (ipf->ipf_timer == maybe_ipf->ipf_timer &&
271 ipf->ipf_last_uses + ipf->ipf_uses <
272 maybe_ipf->ipf_last_uses +
273 maybe_ipf->ipf_uses))
274 maybe_ipf = ipf;
275 ipf = LIST_NEXT(ipf, ipf_list);
276 }
277 ipf = maybe_ipf;
278 done:
279 /*
280 * Remove the entry from the flow table.
281 */
282 s = splimp();
283 IPFLOW_REMOVE(ipf);
284 splx(s);
285 ipflow_addstats(ipf);
286 RTFREE(ipf->ipf_ro.ro_rt);
287 if (just_one)
288 return ipf;
289 FREE(ipf, M_IPFLOW);
290 ipflow_inuse--;
291 }
292 return NULL;
293 }
294
295 void
296 ipflow_slowtimo(
297 void)
298 {
299 struct ipflow *ipf, *next_ipf;
300
301 ipf = LIST_FIRST(&ipflowlist);
302 while (ipf != NULL) {
303 next_ipf = LIST_NEXT(ipf, ipf_list);
304 if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer)) {
305 ipflow_free(ipf);
306 } else {
307 ipf->ipf_last_uses = ipf->ipf_uses;
308 ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
309 ipstat.ips_forward += ipf->ipf_uses;
310 ipstat.ips_fastforward += ipf->ipf_uses;
311 ipf->ipf_uses = 0;
312 }
313 ipf = next_ipf;
314 }
315 }
316
317 void
318 ipflow_create(
319 const struct route *ro,
320 struct mbuf *m)
321 {
322 const struct ip *const ip = mtod(m, struct ip *);
323 struct ipflow *ipf;
324 unsigned hash;
325 int s;
326
327 /*
328 * Don't create cache entries for ICMP messages.
329 */
330 if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP)
331 return;
332 /*
333 * See if an existing flow struct exists. If so remove it from it's
334 * list and free the old route. If not, try to malloc a new one
335 * (if we aren't at our limit).
336 */
337 ipf = ipflow_lookup(ip);
338 if (ipf == NULL) {
339 if (ipflow_inuse >= ip_maxflows) {
340 ipf = ipflow_reap(1);
341 } else {
342 ipf = (struct ipflow *) malloc(sizeof(*ipf), M_IPFLOW,
343 M_NOWAIT);
344 if (ipf == NULL)
345 return;
346 ipflow_inuse++;
347 }
348 bzero((caddr_t) ipf, sizeof(*ipf));
349 } else {
350 s = splimp();
351 IPFLOW_REMOVE(ipf);
352 splx(s);
353 ipflow_addstats(ipf);
354 RTFREE(ipf->ipf_ro.ro_rt);
355 ipf->ipf_uses = ipf->ipf_last_uses = 0;
356 ipf->ipf_errors = ipf->ipf_dropped = 0;
357 }
358
359 /*
360 * Fill in the updated information.
361 */
362 ipf->ipf_ro = *ro;
363 ro->ro_rt->rt_refcnt++;
364 ipf->ipf_dst = ip->ip_dst;
365 ipf->ipf_src = ip->ip_src;
366 ipf->ipf_tos = ip->ip_tos;
367 PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
368 ipf->ipf_start = time.tv_sec;
369 /*
370 * Insert into the approriate bucket of the flow table.
371 */
372 hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
373 s = splimp();
374 IPFLOW_INSERT(&ipflowtable[hash], ipf);
375 splx(s);
376 }
377