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