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