]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_flow.c
76649f14b48fbc200e675c850752ee4900c45d76
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1998 The NetBSD Foundation, Inc.
25 * All rights reserved.
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.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
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.
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.
58 * $FreeBSD: src/sys/netinet/ip_flow.c,v 1.9.2.1 2001/08/08 08:20:35 ru Exp $
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/malloc.h>
65 #include <sys/protosw.h>
66 #include <sys/socket.h>
67 #include <sys/kernel.h>
69 #include <sys/sysctl.h>
72 #include <net/route.h>
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>
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
90 #define M_IPFLOW M_TEMP
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");
98 static MALLOC_DEFINE(M_IPFLOW
, "ip_flow", "IP flow");
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);
114 static struct ipflow
*
121 hash
= ipflow_hash(ip
->ip_dst
, ip
->ip_src
, ip
->ip_tos
);
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
)
129 ipf
= LIST_NEXT(ipf
, ipf_next
);
141 struct sockaddr
*dst
;
145 * Are we forwarding packets? Big enough for an IP packet?
147 if (!ipforwarding
|| !ipflow_active
|| m
->m_len
< sizeof(struct ip
))
150 * IP header with no option and valid version and length
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
)
159 if ((ipf
= ipflow_lookup(ip
)) == NULL
)
163 * Route and interface still up?
165 rt
= ipf
->ipf_ro
.ro_rt
;
166 if ((rt
->rt_flags
& RTF_UP
) == 0 || (rt
->rt_ifp
->if_flags
& IFF_UP
) == 0)
170 * Packet size OK? TTL?
172 if (m
->m_pkthdr
.len
> rt
->rt_ifp
->if_mtu
|| ip
->ip_ttl
<= IPTTLDEC
)
176 * Everything checks out and so we can forward this packet.
177 * Modify the TTL and incrementally change the checksum.
179 ip
->ip_ttl
-= IPTTLDEC
;
180 if (ip
->ip_sum
>= htons(0xffff - (IPTTLDEC
<< 8))) {
181 ip
->ip_sum
+= htons(IPTTLDEC
<< 8) + 1;
183 ip
->ip_sum
+= htons(IPTTLDEC
<< 8);
187 * Send the packet on its way. All we can get back is ENOBUFS
190 ipf
->ipf_timer
= IPFLOW_TIMER
;
192 if (rt
->rt_flags
& RTF_GATEWAY
)
193 dst
= rt
->rt_gateway
;
195 dst
= &ipf
->ipf_ro
.ro_dst
;
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) {
201 if ((error
= (*rt
->rt_ifp
->if_output
)(rt
->rt_ifp
, m
, dst
, rt
)) != 0) {
203 if (error
== ENOBUFS
)
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
;
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
232 LIST_REMOVE(ipf
, ipf_next
);
234 ipflow_addstats(ipf
);
235 rtfree(ipf
->ipf_ro
.ro_rt
);
240 static struct ipflow
*
244 struct ipflow
*ipf
, *maybe_ipf
= NULL
;
248 for (idx
= 0; idx
< IPFLOW_HASHSIZE
; idx
++) {
249 ipf
= LIST_FIRST(&ipflows
[idx
]);
250 while (ipf
!= NULL
) {
252 * If this no longer points to a valid route
255 if ((ipf
->ipf_ro
.ro_rt
->rt_flags
& RTF_UP
) == 0)
258 * choose the one that's been least recently used
259 * or has had the least uses in the last 1.5
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
))
269 ipf
= LIST_NEXT(ipf
, ipf_next
);
275 * Remove the entry from the flow table.
278 LIST_REMOVE(ipf
, ipf_next
);
280 ipflow_addstats(ipf
);
281 rtfree(ipf
->ipf_ro
.ro_rt
);
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) {
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
;
312 const struct route
*ro
,
315 const struct ip
*const ip
= mtod(m
, struct ip
*);
321 * Don't create cache entries for ICMP messages.
323 if (!ipflow_active
|| ip
->ip_p
== IPPROTO_ICMP
)
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).
330 ipf
= ipflow_lookup(ip
);
332 if (ipflow_inuse
== IPFLOW_MAX
) {
335 ipf
= (struct ipflow
*) _MALLOC(sizeof(*ipf
), M_IPFLOW
,
341 bzero((caddr_t
) ipf
, sizeof(*ipf
));
344 LIST_REMOVE(ipf
, ipf_next
);
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;
353 * Fill in the updated information.
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
;
362 * Insert into the approriate bucket of the flow table.
364 hash
= ipflow_hash(ip
->ip_dst
, ip
->ip_src
, ip
->ip_tos
);
366 LIST_INSERT_HEAD(&ipflows
[hash
], ipf
, ipf_next
);