]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_hostcache.c
0ff6a40705055a512ebc6c0f61c3b176c68b58be
[apple/xnu.git] / bsd / netinet / in_hostcache.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright 1997 Massachusetts Institute of Technology
24 *
25 * Permission to use, copy, modify, and distribute this software and
26 * its documentation for any purpose and without fee is hereby
27 * granted, provided that both the above copyright notice and this
28 * permission notice appear in all copies, that both the above
29 * copyright notice and this permission notice appear in all
30 * supporting documentation, and that the name of M.I.T. not be used
31 * in advertising or publicity pertaining to distribution of the
32 * software without specific, written prior permission. M.I.T. makes
33 * no representations about the suitability of this software for any
34 * purpose. It is provided "as is" without express or implied
35 * warranty.
36 *
37 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
38 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
39 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
41 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 *
50 */
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/malloc.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57
58 #include <net/hostcache.h>
59 #include <net/if.h>
60 #include <net/if_var.h>
61 #include <net/route.h>
62
63 #include <netinet/in.h>
64 #include <netinet/in_hostcache.h>
65 #include <netinet/tcp.h>
66 #include <netinet/tcp_timer.h>
67 #include <netinet/tcp_var.h>
68
69 /*
70 * Manage the IP per-host cache (really a thin veneer over the generic
71 * per-host cache code).
72 */
73
74 /* Look up an entry -- can be called from interrupt context. */
75 struct in_hcentry *
76 inhc_lookup(struct sockaddr_in *sin)
77 {
78 struct hcentry *hc;
79
80 hc = hc_get((struct sockaddr *)sin);
81 return ((struct in_hcentry *)hc);
82 }
83
84 /* Look up and possibly create an entry -- must be called from user mode. */
85 struct in_hcentry *
86 inhc_alloc(struct sockaddr_in *sin)
87 {
88 struct in_hcentry *inhc;
89 struct rtentry *rt;
90 int error;
91 /* xxx mutual exclusion for smp */
92
93 inhc = inhc_lookup(sin);
94 if (inhc != 0)
95 return inhc;
96
97 rt = rtalloc1(inhc->inhc_hc.hc_host, 1, 0);
98 if (rt == 0)
99 return 0;
100
101 MALLOC(inhc, struct in_hcentry *, sizeof *inhc, M_HOSTCACHE, M_WAITOK);
102 if (inhc == NULL)
103 retturn (ENOMEM);
104 bzero(inhc, sizeof *inhc);
105 inhc->inhc_hc.hc_host = dup_sockaddr((struct sockaddr *)sin, 1);
106 if (in_broadcast(sin->sin_addr, rt->rt_ifp))
107 inhc->inhc_flags |= INHC_BROADCAST;
108 else if (((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->sin_addr.s_addr
109 == sin->sin_addr.s_addr)
110 inhc->inhc_flags |= INHC_LOCAL;
111 else if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
112 inhc->inhc_flags |= INHC_MULTICAST;
113 inhc->inhc_pmtu = rt->rt_rmx.rmx_mtu;
114 inhc->inhc_recvpipe = rt->rt_rmx.rmx_recvpipe;
115 inhc->inhc_sendpipe = rt->rt_rmx.rmx_sendpipe;
116 inhc->inhc_ssthresh = rt->rt_rmx.rmx_ssthresh;
117 if (rt->rt_rmx.rmx_locks & RTV_RTT)
118 inhc->inhc_rttmin = rt->rt_rmx.rmx_rtt
119 / (RTM_RTTUNIT / TCP_RTT_SCALE);
120 inhc->inhc_hc.hc_rt = rt;
121 error = hc_insert(&inhc->inhc_hc);
122 if (error != 0) {
123 RTFREE(rt);
124 FREE(inhc, M_HOSTCACHE);
125 return 0;
126 }
127 /*
128 * We don't return the structure directly because hc_get() needs
129 * to be allowed to do its own processing.
130 */
131 return (inhc_lookup(sin));
132 }
133
134 /*
135 * This is Van Jacobson's hash function for IPv4 addresses.
136 * It is designed to work with a power-of-two-sized hash table.
137 */
138 static u_long
139 inhc_hash(struct sockaddr *sa, u_long nbuckets)
140 {
141 u_long ip;
142
143 ip = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
144 return ((ip ^ (ip >> 23) ^ (ip >> 17)) & ~(nbuckets - 1));
145 }
146
147 /*
148 * We don't need to do any special work... if there are no references,
149 * as the caller has already ensured, then it's OK to kill.
150 */
151 static int
152 inhc_delete(struct hcentry *hc)
153 {
154 return 0;
155 }
156
157 /*
158 * Return the next increment for the number of buckets in the hash table.
159 * Zero means ``do not bump''.
160 */
161 static u_long
162 inhc_bump(u_long oldsize)
163 {
164 if (oldsize < 512)
165 return (oldsize << 1);
166 return 0;
167 }
168
169 static struct hccallback inhc_cb = {
170 inhc_hash, inhc_delete, inhc_bump
171 };
172
173 int
174 inhc_init(void)
175 {
176
177 return (hc_init(AF_INET, &inhc_cb, 128, 0));
178 }
179