]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_flow.c
76649f14b48fbc200e675c850752ee4900c45d76
[apple/xnu.git] / bsd / netinet / ip_flow.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*-
24 * Copyright (c) 1998 The NetBSD Foundation, Inc.
25 * All rights reserved.
26 *
27 * This code is derived from software contributed to The NetBSD Foundation
28 * by the 3am Software Foundry ("3am"). It was developed by Matt Thomas.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the NetBSD
41 * Foundation, Inc. and its contributors.
42 * 4. Neither the name of The NetBSD Foundation nor the names of its
43 * contributors may be used to endorse or promote products derived
44 * from this software without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
47 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
50 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56 * POSSIBILITY OF SUCH DAMAGE.
57 *
58 * $FreeBSD: src/sys/netinet/ip_flow.c,v 1.9.2.1 2001/08/08 08:20:35 ru Exp $
59 */
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/malloc.h>
64 #include <sys/mbuf.h>
65 #include <sys/protosw.h>
66 #include <sys/socket.h>
67 #include <sys/kernel.h>
68
69 #include <sys/sysctl.h>
70
71 #include <net/if.h>
72 #include <net/route.h>
73
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/in_var.h>
78 #include <netinet/ip_var.h>
79 #include <netinet/ip_flow.h>
80 #include <net/dlil.h>
81
82 #define IPFLOW_TIMER (5 * PR_SLOWHZ)
83 #define IPFLOW_HASHBITS 6 /* should not be a multiple of 8 */
84 #define IPFLOW_HASHSIZE (1 << IPFLOW_HASHBITS)
85 static LIST_HEAD(ipflowhead, ipflow) ipflows[IPFLOW_HASHSIZE];
86 static int ipflow_inuse;
87 #define IPFLOW_MAX 256
88
89 #ifdef __APPLE__
90 #define M_IPFLOW M_TEMP
91 #endif
92
93 static int ipflow_active = 0;
94 SYSCTL_INT(_net_inet_ip, IPCTL_FASTFORWARDING, fastforwarding, CTLFLAG_RW,
95 &ipflow_active, 0, "Enable flow-based IP forwarding");
96
97 #ifndef __APPLE__
98 static MALLOC_DEFINE(M_IPFLOW, "ip_flow", "IP flow");
99 #endif
100
101 static unsigned
102 ipflow_hash(
103 struct in_addr dst,
104 struct in_addr src,
105 unsigned tos)
106 {
107 unsigned hash = tos;
108 int idx;
109 for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS)
110 hash += (dst.s_addr >> (32 - idx)) + (src.s_addr >> idx);
111 return hash & (IPFLOW_HASHSIZE-1);
112 }
113
114 static struct ipflow *
115 ipflow_lookup(
116 const struct ip *ip)
117 {
118 unsigned hash;
119 struct ipflow *ipf;
120
121 hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
122
123 ipf = LIST_FIRST(&ipflows[hash]);
124 while (ipf != NULL) {
125 if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr
126 && ip->ip_src.s_addr == ipf->ipf_src.s_addr
127 && ip->ip_tos == ipf->ipf_tos)
128 break;
129 ipf = LIST_NEXT(ipf, ipf_next);
130 }
131 return ipf;
132 }
133
134 int
135 ipflow_fastforward(
136 struct mbuf *m)
137 {
138 struct ip *ip;
139 struct ipflow *ipf;
140 struct rtentry *rt;
141 struct sockaddr *dst;
142 int error;
143
144 /*
145 * Are we forwarding packets? Big enough for an IP packet?
146 */
147 if (!ipforwarding || !ipflow_active || m->m_len < sizeof(struct ip))
148 return 0;
149 /*
150 * IP header with no option and valid version and length
151 */
152 ip = mtod(m, struct ip *);
153 if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2)
154 || ntohs(ip->ip_len) > m->m_pkthdr.len)
155 return 0;
156 /*
157 * Find a flow.
158 */
159 if ((ipf = ipflow_lookup(ip)) == NULL)
160 return 0;
161
162 /*
163 * Route and interface still up?
164 */
165 rt = ipf->ipf_ro.ro_rt;
166 if ((rt->rt_flags & RTF_UP) == 0 || (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 */
179 ip->ip_ttl -= IPTTLDEC;
180 if (ip->ip_sum >= htons(0xffff - (IPTTLDEC << 8))) {
181 ip->ip_sum += htons(IPTTLDEC << 8) + 1;
182 } else {
183 ip->ip_sum += htons(IPTTLDEC << 8);
184 }
185
186 /*
187 * Send the packet on its way. All we can get back is ENOBUFS
188 */
189 ipf->ipf_uses++;
190 ipf->ipf_timer = IPFLOW_TIMER;
191
192 if (rt->rt_flags & RTF_GATEWAY)
193 dst = rt->rt_gateway;
194 else
195 dst = &ipf->ipf_ro.ro_dst;
196 #ifdef __APPLE__
197 /* Not sure the rt_dlt is valid here !! XXX */
198 if ((error = dlil_output(rt->rt_ifp, PF_INET, m, (caddr_t) rt, dst, 0)) != 0) {
199
200 #else
201 if ((error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m, dst, rt)) != 0) {
202 #endif
203 if (error == ENOBUFS)
204 ipf->ipf_dropped++;
205 else
206 ipf->ipf_errors++;
207 }
208 return 1;
209 }
210 \f
211 static void
212 ipflow_addstats(
213 struct ipflow *ipf)
214 {
215 ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
216 ipstat.ips_cantforward += ipf->ipf_errors + ipf->ipf_dropped;
217 ipstat.ips_forward += ipf->ipf_uses;
218 ipstat.ips_fastforward += ipf->ipf_uses;
219 }
220
221 static void
222 ipflow_free(
223 struct ipflow *ipf)
224 {
225 int s;
226 /*
227 * Remove the flow from the hash table (at elevated IPL).
228 * Once it's off the list, we can deal with it at normal
229 * network IPL.
230 */
231 s = splimp();
232 LIST_REMOVE(ipf, ipf_next);
233 splx(s);
234 ipflow_addstats(ipf);
235 rtfree(ipf->ipf_ro.ro_rt);
236 ipflow_inuse--;
237 FREE(ipf, M_IPFLOW);
238 }
239
240 static struct ipflow *
241 ipflow_reap(
242 void)
243 {
244 struct ipflow *ipf, *maybe_ipf = NULL;
245 int idx;
246 int s;
247
248 for (idx = 0; idx < IPFLOW_HASHSIZE; idx++) {
249 ipf = LIST_FIRST(&ipflows[idx]);
250 while (ipf != NULL) {
251 /*
252 * If this no longer points to a valid route
253 * reclaim it.
254 */
255 if ((ipf->ipf_ro.ro_rt->rt_flags & RTF_UP) == 0)
256 goto done;
257 /*
258 * choose the one that's been least recently used
259 * or has had the least uses in the last 1.5
260 * intervals.
261 */
262 if (maybe_ipf == NULL
263 || ipf->ipf_timer < maybe_ipf->ipf_timer
264 || (ipf->ipf_timer == maybe_ipf->ipf_timer
265 && ipf->ipf_last_uses + ipf->ipf_uses <
266 maybe_ipf->ipf_last_uses +
267 maybe_ipf->ipf_uses))
268 maybe_ipf = ipf;
269 ipf = LIST_NEXT(ipf, ipf_next);
270 }
271 }
272 ipf = maybe_ipf;
273 done:
274 /*
275 * Remove the entry from the flow table.
276 */
277 s = splimp();
278 LIST_REMOVE(ipf, ipf_next);
279 splx(s);
280 ipflow_addstats(ipf);
281 rtfree(ipf->ipf_ro.ro_rt);
282 return ipf;
283 }
284
285 void
286 ipflow_slowtimo(
287 void)
288 {
289 struct ipflow *ipf;
290 int idx;
291
292 for (idx = 0; idx < IPFLOW_HASHSIZE; idx++) {
293 ipf = LIST_FIRST(&ipflows[idx]);
294 while (ipf != NULL) {
295 struct ipflow *next_ipf = LIST_NEXT(ipf, ipf_next);
296 if (--ipf->ipf_timer == 0) {
297 ipflow_free(ipf);
298 } else {
299 ipf->ipf_last_uses = ipf->ipf_uses;
300 ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
301 ipstat.ips_forward += ipf->ipf_uses;
302 ipstat.ips_fastforward += ipf->ipf_uses;
303 ipf->ipf_uses = 0;
304 }
305 ipf = next_ipf;
306 }
307 }
308 }
309
310 void
311 ipflow_create(
312 const struct route *ro,
313 struct mbuf *m)
314 {
315 const struct ip *const ip = mtod(m, struct ip *);
316 struct ipflow *ipf;
317 unsigned hash;
318 int s;
319
320 /*
321 * Don't create cache entries for ICMP messages.
322 */
323 if (!ipflow_active || ip->ip_p == IPPROTO_ICMP)
324 return;
325 /*
326 * See if an existing flow struct exists. If so remove it from it's
327 * list and free the old route. If not, try to malloc a new one
328 * (if we aren't at our limit).
329 */
330 ipf = ipflow_lookup(ip);
331 if (ipf == NULL) {
332 if (ipflow_inuse == IPFLOW_MAX) {
333 ipf = ipflow_reap();
334 } else {
335 ipf = (struct ipflow *) _MALLOC(sizeof(*ipf), M_IPFLOW,
336 M_NOWAIT);
337 if (ipf == NULL)
338 return;
339 ipflow_inuse++;
340 }
341 bzero((caddr_t) ipf, sizeof(*ipf));
342 } else {
343 s = splimp();
344 LIST_REMOVE(ipf, ipf_next);
345 splx(s);
346 ipflow_addstats(ipf);
347 rtfree(ipf->ipf_ro.ro_rt);
348 ipf->ipf_uses = ipf->ipf_last_uses = 0;
349 ipf->ipf_errors = ipf->ipf_dropped = 0;
350 }
351
352 /*
353 * Fill in the updated information.
354 */
355 ipf->ipf_ro = *ro;
356 rtref(ro->ro_rt); //### LD 5/25/04 needs rt_mtx lock
357 ipf->ipf_dst = ip->ip_dst;
358 ipf->ipf_src = ip->ip_src;
359 ipf->ipf_tos = ip->ip_tos;
360 ipf->ipf_timer = IPFLOW_TIMER;
361 /*
362 * Insert into the approriate bucket of the flow table.
363 */
364 hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
365 s = splimp();
366 LIST_INSERT_HEAD(&ipflows[hash], ipf, ipf_next);
367 splx(s);
368 }