]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_flow.c
98d19567cc62321037e244da8807d3749c290e5a
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1998 The NetBSD Foundation, Inc.
32 * All rights reserved.
34 * This code is derived from software contributed to The NetBSD Foundation
35 * by the 3am Software Foundry ("3am"). It was developed by Matt Thomas.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the NetBSD
48 * Foundation, Inc. and its contributors.
49 * 4. Neither the name of The NetBSD Foundation nor the names of its
50 * contributors may be used to endorse or promote products derived
51 * from this software without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
65 * $FreeBSD: src/sys/netinet/ip_flow.c,v 1.9.2.1 2001/08/08 08:20:35 ru Exp $
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/malloc.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/kernel.h>
76 #include <sys/sysctl.h>
79 #include <net/route.h>
81 #include <netinet/in.h>
82 #include <netinet/in_systm.h>
83 #include <netinet/ip.h>
84 #include <netinet/in_var.h>
85 #include <netinet/ip_var.h>
86 #include <netinet/ip_flow.h>
89 #define IPFLOW_TIMER (5 * PR_SLOWHZ)
90 #define IPFLOW_HASHBITS 6 /* should not be a multiple of 8 */
91 #define IPFLOW_HASHSIZE (1 << IPFLOW_HASHBITS)
92 static LIST_HEAD(ipflowhead
, ipflow
) ipflows
[IPFLOW_HASHSIZE
];
93 static int ipflow_inuse
;
94 #define IPFLOW_MAX 256
97 #define M_IPFLOW M_TEMP
100 static int ipflow_active
= 0;
101 SYSCTL_INT(_net_inet_ip
, IPCTL_FASTFORWARDING
, fastforwarding
, CTLFLAG_RW
,
102 &ipflow_active
, 0, "Enable flow-based IP forwarding");
105 static MALLOC_DEFINE(M_IPFLOW
, "ip_flow", "IP flow");
116 for (idx
= 0; idx
< 32; idx
+= IPFLOW_HASHBITS
)
117 hash
+= (dst
.s_addr
>> (32 - idx
)) + (src
.s_addr
>> idx
);
118 return hash
& (IPFLOW_HASHSIZE
-1);
121 static struct ipflow
*
128 hash
= ipflow_hash(ip
->ip_dst
, ip
->ip_src
, ip
->ip_tos
);
130 ipf
= LIST_FIRST(&ipflows
[hash
]);
131 while (ipf
!= NULL
) {
132 if (ip
->ip_dst
.s_addr
== ipf
->ipf_dst
.s_addr
133 && ip
->ip_src
.s_addr
== ipf
->ipf_src
.s_addr
134 && ip
->ip_tos
== ipf
->ipf_tos
)
136 ipf
= LIST_NEXT(ipf
, ipf_next
);
148 struct sockaddr
*dst
;
152 * Are we forwarding packets? Big enough for an IP packet?
154 if (!ipforwarding
|| !ipflow_active
|| m
->m_len
< sizeof(struct ip
))
157 * IP header with no option and valid version and length
159 ip
= mtod(m
, struct ip
*);
160 if (ip
->ip_v
!= IPVERSION
|| ip
->ip_hl
!= (sizeof(struct ip
) >> 2)
161 || ntohs(ip
->ip_len
) > m
->m_pkthdr
.len
)
166 if ((ipf
= ipflow_lookup(ip
)) == NULL
)
170 * Route and interface still up?
172 rt
= ipf
->ipf_ro
.ro_rt
;
173 if ((rt
->rt_flags
& RTF_UP
) == 0 || (rt
->rt_ifp
->if_flags
& IFF_UP
) == 0)
177 * Packet size OK? TTL?
179 if (m
->m_pkthdr
.len
> rt
->rt_ifp
->if_mtu
|| ip
->ip_ttl
<= IPTTLDEC
)
183 * Everything checks out and so we can forward this packet.
184 * Modify the TTL and incrementally change the checksum.
186 ip
->ip_ttl
-= IPTTLDEC
;
187 if (ip
->ip_sum
>= htons(0xffff - (IPTTLDEC
<< 8))) {
188 ip
->ip_sum
+= htons(IPTTLDEC
<< 8) + 1;
190 ip
->ip_sum
+= htons(IPTTLDEC
<< 8);
194 * Send the packet on its way. All we can get back is ENOBUFS
197 ipf
->ipf_timer
= IPFLOW_TIMER
;
199 if (rt
->rt_flags
& RTF_GATEWAY
)
200 dst
= rt
->rt_gateway
;
202 dst
= &ipf
->ipf_ro
.ro_dst
;
204 /* Not sure the rt_dlt is valid here !! XXX */
205 if ((error
= dlil_output(rt
->rt_ifp
, PF_INET
, m
, (caddr_t
) rt
, dst
, 0)) != 0) {
208 if ((error
= (*rt
->rt_ifp
->if_output
)(rt
->rt_ifp
, m
, dst
, rt
)) != 0) {
210 if (error
== ENOBUFS
)
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
;
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
239 LIST_REMOVE(ipf
, ipf_next
);
241 ipflow_addstats(ipf
);
242 rtfree(ipf
->ipf_ro
.ro_rt
);
247 static struct ipflow
*
251 struct ipflow
*ipf
, *maybe_ipf
= NULL
;
255 for (idx
= 0; idx
< IPFLOW_HASHSIZE
; idx
++) {
256 ipf
= LIST_FIRST(&ipflows
[idx
]);
257 while (ipf
!= NULL
) {
259 * If this no longer points to a valid route
262 if ((ipf
->ipf_ro
.ro_rt
->rt_flags
& RTF_UP
) == 0)
265 * choose the one that's been least recently used
266 * or has had the least uses in the last 1.5
269 if (maybe_ipf
== NULL
270 || ipf
->ipf_timer
< maybe_ipf
->ipf_timer
271 || (ipf
->ipf_timer
== maybe_ipf
->ipf_timer
272 && ipf
->ipf_last_uses
+ ipf
->ipf_uses
<
273 maybe_ipf
->ipf_last_uses
+
274 maybe_ipf
->ipf_uses
))
276 ipf
= LIST_NEXT(ipf
, ipf_next
);
282 * Remove the entry from the flow table.
285 LIST_REMOVE(ipf
, ipf_next
);
287 ipflow_addstats(ipf
);
288 rtfree(ipf
->ipf_ro
.ro_rt
);
299 for (idx
= 0; idx
< IPFLOW_HASHSIZE
; idx
++) {
300 ipf
= LIST_FIRST(&ipflows
[idx
]);
301 while (ipf
!= NULL
) {
302 struct ipflow
*next_ipf
= LIST_NEXT(ipf
, ipf_next
);
303 if (--ipf
->ipf_timer
== 0) {
306 ipf
->ipf_last_uses
= ipf
->ipf_uses
;
307 ipf
->ipf_ro
.ro_rt
->rt_use
+= ipf
->ipf_uses
;
308 ipstat
.ips_forward
+= ipf
->ipf_uses
;
309 ipstat
.ips_fastforward
+= ipf
->ipf_uses
;
319 const struct route
*ro
,
322 const struct ip
*const ip
= mtod(m
, struct ip
*);
328 * Don't create cache entries for ICMP messages.
330 if (!ipflow_active
|| ip
->ip_p
== IPPROTO_ICMP
)
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).
337 ipf
= ipflow_lookup(ip
);
339 if (ipflow_inuse
== IPFLOW_MAX
) {
342 ipf
= (struct ipflow
*) _MALLOC(sizeof(*ipf
), M_IPFLOW
,
348 bzero((caddr_t
) ipf
, sizeof(*ipf
));
351 LIST_REMOVE(ipf
, ipf_next
);
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;
360 * Fill in the updated information.
363 rtref(ro
->ro_rt
); //### LD 5/25/04 needs rt_mtx lock
364 ipf
->ipf_dst
= ip
->ip_dst
;
365 ipf
->ipf_src
= ip
->ip_src
;
366 ipf
->ipf_tos
= ip
->ip_tos
;
367 ipf
->ipf_timer
= IPFLOW_TIMER
;
369 * Insert into the approriate bucket of the flow table.
371 hash
= ipflow_hash(ip
->ip_dst
, ip
->ip_src
, ip
->ip_tos
);
373 LIST_INSERT_HEAD(&ipflows
[hash
], ipf
, ipf_next
);