]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_flow.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1998 The NetBSD Foundation, Inc.
24 * All rights reserved.
26 * This code is derived from software contributed to The NetBSD Foundation
27 * by the 3am Software Foundry ("3am"). It was developed by Matt Thomas.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the NetBSD
40 * Foundation, Inc. and its contributors.
41 * 4. Neither the name of The NetBSD Foundation nor the names of its
42 * contributors may be used to endorse or promote products derived
43 * from this software without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
46 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
49 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55 * POSSIBILITY OF SUCH DAMAGE.
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/malloc.h>
63 #include <sys/protosw.h>
64 #include <sys/socket.h>
65 #include <sys/kernel.h>
67 #include <sys/sysctl.h>
70 #include <net/route.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/in_var.h>
76 #include <netinet/ip_var.h>
77 #include <netinet/ip_flow.h>
80 #define IPFLOW_TIMER (5 * PR_SLOWHZ)
81 #define IPFLOW_HASHBITS 6 /* should not be a multiple of 8 */
82 #define IPFLOW_HASHSIZE (1 << IPFLOW_HASHBITS)
83 static LIST_HEAD(ipflowhead
, ipflow
) ipflows
[IPFLOW_HASHSIZE
];
84 static int ipflow_inuse
;
85 #define IPFLOW_MAX 256
89 #define M_IPFLOW M_TEMP
92 static int ipflow_active
= 0;
93 SYSCTL_INT(_net_inet_ip
, IPCTL_FASTFORWARDING
, fastforwarding
, CTLFLAG_RW
,
94 &ipflow_active
, 0, "");
96 MALLOC_DEFINE(M_IPFLOW
, "ip_flow", "IP flow");
106 for (idx
= 0; idx
< 32; idx
+= IPFLOW_HASHBITS
)
107 hash
+= (dst
.s_addr
>> (32 - idx
)) + (src
.s_addr
>> idx
);
108 return hash
& (IPFLOW_HASHSIZE
-1);
111 static struct ipflow
*
118 hash
= ipflow_hash(ip
->ip_dst
, ip
->ip_src
, ip
->ip_tos
);
120 ipf
= LIST_FIRST(&ipflows
[hash
]);
121 while (ipf
!= NULL
) {
122 if (ip
->ip_dst
.s_addr
== ipf
->ipf_dst
.s_addr
123 && ip
->ip_src
.s_addr
== ipf
->ipf_src
.s_addr
124 && ip
->ip_tos
== ipf
->ipf_tos
)
126 ipf
= LIST_NEXT(ipf
, ipf_next
);
141 * Are we forwarding packets? Big enough for an IP packet?
143 if (!ipforwarding
|| !ipflow_active
|| m
->m_len
< sizeof(struct ip
))
146 * IP header with no option and valid version and length
148 ip
= mtod(m
, struct ip
*);
149 if (ip
->ip_v
!= IPVERSION
|| ip
->ip_hl
!= (sizeof(struct ip
) >> 2)
150 || ntohs(ip
->ip_len
) > m
->m_pkthdr
.len
)
155 if ((ipf
= ipflow_lookup(ip
)) == NULL
)
159 * Route and interface still up?
161 rt
= ipf
->ipf_ro
.ro_rt
;
162 if ((rt
->rt_flags
& RTF_UP
) == 0 || (rt
->rt_ifp
->if_flags
& IFF_UP
) == 0)
166 * Packet size OK? TTL?
168 if (m
->m_pkthdr
.len
> rt
->rt_ifp
->if_mtu
|| ip
->ip_ttl
<= IPTTLDEC
)
172 * Everything checks out and so we can forward this packet.
173 * Modify the TTL and incrementally change the checksum.
175 ip
->ip_ttl
-= IPTTLDEC
;
176 if (ip
->ip_sum
>= htons(0xffff - (IPTTLDEC
<< 8))) {
177 ip
->ip_sum
+= htons(IPTTLDEC
<< 8) + 1;
179 ip
->ip_sum
+= htons(IPTTLDEC
<< 8);
183 * Send the packet on its way. All we can get back is ENOBUFS
186 ipf
->ipf_timer
= IPFLOW_TIMER
;
188 /* Not sure the rt_dlt is valid here !! XXX */
189 if ((error
= dlil_output((u_long
)rt
->rt_dlt
, m
, (caddr_t
) rt
, &ipf
->ipf_ro
.ro_dst
, 0)) != 0) {
190 if (error
== ENOBUFS
)
202 ipf
->ipf_ro
.ro_rt
->rt_use
+= ipf
->ipf_uses
;
203 ipstat
.ips_cantforward
+= ipf
->ipf_errors
+ ipf
->ipf_dropped
;
204 ipstat
.ips_forward
+= ipf
->ipf_uses
;
205 ipstat
.ips_fastforward
+= ipf
->ipf_uses
;
214 * Remove the flow from the hash table (at elevated IPL).
215 * Once it's off the list, we can deal with it at normal
219 LIST_REMOVE(ipf
, ipf_next
);
221 ipflow_addstats(ipf
);
222 RTFREE(ipf
->ipf_ro
.ro_rt
);
227 static struct ipflow
*
231 struct ipflow
*ipf
, *maybe_ipf
= NULL
;
235 for (idx
= 0; idx
< IPFLOW_HASHSIZE
; idx
++) {
236 ipf
= LIST_FIRST(&ipflows
[idx
]);
237 while (ipf
!= NULL
) {
239 * If this no longer points to a valid route
242 if ((ipf
->ipf_ro
.ro_rt
->rt_flags
& RTF_UP
) == 0)
245 * choose the one that's been least recently used
246 * or has had the least uses in the last 1.5
249 if (maybe_ipf
== NULL
250 || ipf
->ipf_timer
< maybe_ipf
->ipf_timer
251 || (ipf
->ipf_timer
== maybe_ipf
->ipf_timer
252 && ipf
->ipf_last_uses
+ ipf
->ipf_uses
<
253 maybe_ipf
->ipf_last_uses
+
254 maybe_ipf
->ipf_uses
))
256 ipf
= LIST_NEXT(ipf
, ipf_next
);
262 * Remove the entry from the flow table.
265 LIST_REMOVE(ipf
, ipf_next
);
267 ipflow_addstats(ipf
);
268 RTFREE(ipf
->ipf_ro
.ro_rt
);
279 for (idx
= 0; idx
< IPFLOW_HASHSIZE
; idx
++) {
280 ipf
= LIST_FIRST(&ipflows
[idx
]);
281 while (ipf
!= NULL
) {
282 struct ipflow
*next_ipf
= LIST_NEXT(ipf
, ipf_next
);
283 if (--ipf
->ipf_timer
== 0) {
286 ipf
->ipf_last_uses
= ipf
->ipf_uses
;
287 ipf
->ipf_ro
.ro_rt
->rt_use
+= ipf
->ipf_uses
;
288 ipstat
.ips_forward
+= ipf
->ipf_uses
;
289 ipstat
.ips_fastforward
+= ipf
->ipf_uses
;
299 const struct route
*ro
,
302 const struct ip
*const ip
= mtod(m
, struct ip
*);
308 * Don't create cache entries for ICMP messages.
310 if (!ipflow_active
|| ip
->ip_p
== IPPROTO_ICMP
)
313 * See if an existing flow struct exists. If so remove it from it's
314 * list and free the old route. If not, try to malloc a new one
315 * (if we aren't at our limit).
317 ipf
= ipflow_lookup(ip
);
319 if (ipflow_inuse
== IPFLOW_MAX
) {
322 ipf
= (struct ipflow
*) _MALLOC(sizeof(*ipf
), M_IPFLOW
,
328 bzero((caddr_t
) ipf
, sizeof(*ipf
));
331 LIST_REMOVE(ipf
, ipf_next
);
333 ipflow_addstats(ipf
);
334 RTFREE(ipf
->ipf_ro
.ro_rt
);
335 ipf
->ipf_uses
= ipf
->ipf_last_uses
= 0;
336 ipf
->ipf_errors
= ipf
->ipf_dropped
= 0;
340 * Fill in the updated information.
343 ro
->ro_rt
->rt_refcnt
++;
344 ipf
->ipf_dst
= ip
->ip_dst
;
345 ipf
->ipf_src
= ip
->ip_src
;
346 ipf
->ipf_tos
= ip
->ip_tos
;
347 ipf
->ipf_timer
= IPFLOW_TIMER
;
349 * Insert into the approriate bucket of the flow table.
351 hash
= ipflow_hash(ip
->ip_dst
, ip
->ip_src
, ip
->ip_tos
);
353 LIST_INSERT_HEAD(&ipflows
[hash
], ipf
, ipf_next
);