in_pcb.c revision 1.83 1 1.83 itojun /* $NetBSD: in_pcb.c,v 1.83 2003/06/26 00:19:13 itojun Exp $ */
2 1.59 itojun
3 1.59 itojun /*
4 1.59 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 1.59 itojun * All rights reserved.
6 1.78 itojun *
7 1.59 itojun * Redistribution and use in source and binary forms, with or without
8 1.59 itojun * modification, are permitted provided that the following conditions
9 1.59 itojun * are met:
10 1.59 itojun * 1. Redistributions of source code must retain the above copyright
11 1.59 itojun * notice, this list of conditions and the following disclaimer.
12 1.59 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.59 itojun * notice, this list of conditions and the following disclaimer in the
14 1.59 itojun * documentation and/or other materials provided with the distribution.
15 1.59 itojun * 3. Neither the name of the project nor the names of its contributors
16 1.59 itojun * may be used to endorse or promote products derived from this software
17 1.59 itojun * without specific prior written permission.
18 1.78 itojun *
19 1.59 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.59 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.59 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.59 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.59 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.59 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.59 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.59 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.59 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.59 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.59 itojun * SUCH DAMAGE.
30 1.59 itojun */
31 1.57 thorpej
32 1.57 thorpej /*-
33 1.57 thorpej * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 1.57 thorpej * All rights reserved.
35 1.57 thorpej *
36 1.57 thorpej * This code is derived from software contributed to The NetBSD Foundation
37 1.57 thorpej * by Public Access Networks Corporation ("Panix"). It was developed under
38 1.57 thorpej * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39 1.57 thorpej *
40 1.57 thorpej * Redistribution and use in source and binary forms, with or without
41 1.57 thorpej * modification, are permitted provided that the following conditions
42 1.57 thorpej * are met:
43 1.57 thorpej * 1. Redistributions of source code must retain the above copyright
44 1.57 thorpej * notice, this list of conditions and the following disclaimer.
45 1.57 thorpej * 2. Redistributions in binary form must reproduce the above copyright
46 1.57 thorpej * notice, this list of conditions and the following disclaimer in the
47 1.57 thorpej * documentation and/or other materials provided with the distribution.
48 1.57 thorpej * 3. All advertising materials mentioning features or use of this software
49 1.57 thorpej * must display the following acknowledgement:
50 1.57 thorpej * This product includes software developed by the NetBSD
51 1.57 thorpej * Foundation, Inc. and its contributors.
52 1.57 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
53 1.57 thorpej * contributors may be used to endorse or promote products derived
54 1.57 thorpej * from this software without specific prior written permission.
55 1.57 thorpej *
56 1.57 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 1.57 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 1.57 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 1.57 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 1.57 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 1.57 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 1.57 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 1.57 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 1.57 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 1.57 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 1.57 thorpej * POSSIBILITY OF SUCH DAMAGE.
67 1.57 thorpej */
68 1.11 cgd
69 1.1 cgd /*
70 1.44 thorpej * Copyright (c) 1982, 1986, 1991, 1993, 1995
71 1.10 mycroft * The Regents of the University of California. All rights reserved.
72 1.1 cgd *
73 1.1 cgd * Redistribution and use in source and binary forms, with or without
74 1.1 cgd * modification, are permitted provided that the following conditions
75 1.1 cgd * are met:
76 1.1 cgd * 1. Redistributions of source code must retain the above copyright
77 1.1 cgd * notice, this list of conditions and the following disclaimer.
78 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
79 1.1 cgd * notice, this list of conditions and the following disclaimer in the
80 1.1 cgd * documentation and/or other materials provided with the distribution.
81 1.1 cgd * 3. All advertising materials mentioning features or use of this software
82 1.1 cgd * must display the following acknowledgement:
83 1.1 cgd * This product includes software developed by the University of
84 1.1 cgd * California, Berkeley and its contributors.
85 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
86 1.1 cgd * may be used to endorse or promote products derived from this software
87 1.1 cgd * without specific prior written permission.
88 1.1 cgd *
89 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
90 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
92 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
93 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
95 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
96 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
97 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
98 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99 1.1 cgd * SUCH DAMAGE.
100 1.1 cgd *
101 1.44 thorpej * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
102 1.1 cgd */
103 1.73 lukem
104 1.73 lukem #include <sys/cdefs.h>
105 1.83 itojun __KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.83 2003/06/26 00:19:13 itojun Exp $");
106 1.60 thorpej
107 1.60 thorpej #include "opt_ipsec.h"
108 1.1 cgd
109 1.7 mycroft #include <sys/param.h>
110 1.7 mycroft #include <sys/systm.h>
111 1.7 mycroft #include <sys/malloc.h>
112 1.7 mycroft #include <sys/mbuf.h>
113 1.7 mycroft #include <sys/protosw.h>
114 1.7 mycroft #include <sys/socket.h>
115 1.7 mycroft #include <sys/socketvar.h>
116 1.7 mycroft #include <sys/ioctl.h>
117 1.10 mycroft #include <sys/errno.h>
118 1.10 mycroft #include <sys/time.h>
119 1.52 thorpej #include <sys/pool.h>
120 1.10 mycroft #include <sys/proc.h>
121 1.1 cgd
122 1.7 mycroft #include <net/if.h>
123 1.7 mycroft #include <net/route.h>
124 1.1 cgd
125 1.7 mycroft #include <netinet/in.h>
126 1.7 mycroft #include <netinet/in_systm.h>
127 1.7 mycroft #include <netinet/ip.h>
128 1.7 mycroft #include <netinet/in_pcb.h>
129 1.7 mycroft #include <netinet/in_var.h>
130 1.7 mycroft #include <netinet/ip_var.h>
131 1.1 cgd
132 1.59 itojun #ifdef IPSEC
133 1.59 itojun #include <netinet6/ipsec.h>
134 1.59 itojun #include <netkey/key.h>
135 1.59 itojun #endif /* IPSEC */
136 1.59 itojun
137 1.1 cgd struct in_addr zeroin_addr;
138 1.1 cgd
139 1.33 mycroft #define INPCBHASH_BIND(table, laddr, lport) \
140 1.33 mycroft &(table)->inpt_bindhashtbl[ \
141 1.33 mycroft ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash]
142 1.33 mycroft #define INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
143 1.33 mycroft &(table)->inpt_connecthashtbl[ \
144 1.33 mycroft ((ntohl((faddr).s_addr) + ntohs(fport)) + \
145 1.33 mycroft (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash]
146 1.33 mycroft
147 1.33 mycroft struct inpcb *
148 1.33 mycroft in_pcblookup_port __P((struct inpcbtable *,
149 1.33 mycroft struct in_addr, u_int, int));
150 1.24 mycroft
151 1.43 lukem int anonportmin = IPPORT_ANONMIN;
152 1.43 lukem int anonportmax = IPPORT_ANONMAX;
153 1.67 tron int lowportmin = IPPORT_RESERVEDMIN;
154 1.67 tron int lowportmax = IPPORT_RESERVEDMAX;
155 1.43 lukem
156 1.52 thorpej struct pool inpcb_pool;
157 1.52 thorpej
158 1.18 mycroft void
159 1.33 mycroft in_pcbinit(table, bindhashsize, connecthashsize)
160 1.18 mycroft struct inpcbtable *table;
161 1.33 mycroft int bindhashsize, connecthashsize;
162 1.18 mycroft {
163 1.52 thorpej static int inpcb_pool_initialized;
164 1.52 thorpej
165 1.52 thorpej if (inpcb_pool_initialized == 0) {
166 1.52 thorpej pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 0, 0,
167 1.75 thorpej "inpcbpl", NULL);
168 1.52 thorpej inpcb_pool_initialized = 1;
169 1.52 thorpej }
170 1.18 mycroft
171 1.21 cgd CIRCLEQ_INIT(&table->inpt_queue);
172 1.68 ad table->inpt_bindhashtbl = hashinit(bindhashsize, HASH_LIST, M_PCB,
173 1.68 ad M_WAITOK, &table->inpt_bindhash);
174 1.68 ad table->inpt_connecthashtbl = hashinit(connecthashsize, HASH_LIST,
175 1.68 ad M_PCB, M_WAITOK, &table->inpt_connecthash);
176 1.47 lukem table->inpt_lastlow = IPPORT_RESERVEDMAX;
177 1.47 lukem table->inpt_lastport = (u_int16_t)anonportmax;
178 1.18 mycroft }
179 1.18 mycroft
180 1.10 mycroft int
181 1.25 christos in_pcballoc(so, v)
182 1.1 cgd struct socket *so;
183 1.25 christos void *v;
184 1.1 cgd {
185 1.25 christos struct inpcbtable *table = v;
186 1.64 augustss struct inpcb *inp;
187 1.24 mycroft int s;
188 1.70 itojun #ifdef IPSEC
189 1.70 itojun int error;
190 1.70 itojun #endif
191 1.1 cgd
192 1.52 thorpej inp = pool_get(&inpcb_pool, PR_NOWAIT);
193 1.10 mycroft if (inp == NULL)
194 1.1 cgd return (ENOBUFS);
195 1.10 mycroft bzero((caddr_t)inp, sizeof(*inp));
196 1.18 mycroft inp->inp_table = table;
197 1.1 cgd inp->inp_socket = so;
198 1.39 matt inp->inp_errormtu = -1;
199 1.70 itojun #ifdef IPSEC
200 1.79 itojun error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
201 1.70 itojun if (error != 0) {
202 1.70 itojun pool_put(&inpcb_pool, inp);
203 1.70 itojun return error;
204 1.70 itojun }
205 1.70 itojun #endif
206 1.33 mycroft so->so_pcb = inp;
207 1.24 mycroft s = splnet();
208 1.21 cgd CIRCLEQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue);
209 1.33 mycroft in_pcbstate(inp, INP_ATTACHED);
210 1.24 mycroft splx(s);
211 1.1 cgd return (0);
212 1.1 cgd }
213 1.8 mycroft
214 1.10 mycroft int
215 1.28 mycroft in_pcbbind(v, nam, p)
216 1.28 mycroft void *v;
217 1.1 cgd struct mbuf *nam;
218 1.28 mycroft struct proc *p;
219 1.1 cgd {
220 1.82 matt struct in_ifaddr *ia = NULL;
221 1.64 augustss struct inpcb *inp = v;
222 1.64 augustss struct socket *so = inp->inp_socket;
223 1.64 augustss struct inpcbtable *table = inp->inp_table;
224 1.64 augustss struct sockaddr_in *sin;
225 1.13 cgd u_int16_t lport = 0;
226 1.10 mycroft int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
227 1.1 cgd
228 1.72 matt if (TAILQ_FIRST(&in_ifaddr) == 0)
229 1.1 cgd return (EADDRNOTAVAIL);
230 1.32 mycroft if (inp->inp_lport || !in_nullhost(inp->inp_laddr))
231 1.1 cgd return (EINVAL);
232 1.55 lukem if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
233 1.54 lukem wild = 1;
234 1.28 mycroft if (nam == 0)
235 1.28 mycroft goto noname;
236 1.28 mycroft sin = mtod(nam, struct sockaddr_in *);
237 1.28 mycroft if (nam->m_len != sizeof (*sin))
238 1.28 mycroft return (EINVAL);
239 1.28 mycroft if (sin->sin_family != AF_INET)
240 1.28 mycroft return (EAFNOSUPPORT);
241 1.28 mycroft lport = sin->sin_port;
242 1.28 mycroft if (IN_MULTICAST(sin->sin_addr.s_addr)) {
243 1.10 mycroft /*
244 1.28 mycroft * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
245 1.28 mycroft * allow complete duplication of binding if
246 1.28 mycroft * SO_REUSEPORT is set, or if SO_REUSEADDR is set
247 1.28 mycroft * and a multicast address is bound on both
248 1.28 mycroft * new and duplicated sockets.
249 1.10 mycroft */
250 1.28 mycroft if (so->so_options & SO_REUSEADDR)
251 1.28 mycroft reuseport = SO_REUSEADDR|SO_REUSEPORT;
252 1.32 mycroft } else if (!in_nullhost(sin->sin_addr)) {
253 1.28 mycroft sin->sin_port = 0; /* yech... */
254 1.82 matt INADDR_TO_IA(sin->sin_addr, ia);
255 1.82 matt if (ia == NULL)
256 1.28 mycroft return (EADDRNOTAVAIL);
257 1.28 mycroft }
258 1.28 mycroft if (lport) {
259 1.28 mycroft struct inpcb *t;
260 1.31 perry #ifndef IPNOPRIVPORTS
261 1.28 mycroft /* GROSS */
262 1.28 mycroft if (ntohs(lport) < IPPORT_RESERVED &&
263 1.80 simonb (p == 0 || suser(p->p_ucred, &p->p_acflag)))
264 1.28 mycroft return (EACCES);
265 1.31 perry #endif
266 1.58 lukem if (so->so_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) {
267 1.58 lukem t = in_pcblookup_port(table, sin->sin_addr, lport, 1);
268 1.58 lukem /*
269 1.58 lukem * XXX: investigate ramifications of loosening this
270 1.58 lukem * restriction so that as long as both ports have
271 1.58 lukem * SO_REUSEPORT allow the bind
272 1.58 lukem */
273 1.58 lukem if (t &&
274 1.58 lukem (!in_nullhost(sin->sin_addr) ||
275 1.58 lukem !in_nullhost(t->inp_laddr) ||
276 1.58 lukem (t->inp_socket->so_options & SO_REUSEPORT) == 0)
277 1.58 lukem && (so->so_uid != t->inp_socket->so_uid)) {
278 1.58 lukem return (EADDRINUSE);
279 1.58 lukem }
280 1.58 lukem }
281 1.33 mycroft t = in_pcblookup_port(table, sin->sin_addr, lport, wild);
282 1.28 mycroft if (t && (reuseport & t->inp_socket->so_options) == 0)
283 1.28 mycroft return (EADDRINUSE);
284 1.1 cgd }
285 1.82 matt if (!in_nullhost(inp->inp_laddr)) {
286 1.82 matt KASSERT(inp->inp_ia != NULL);
287 1.82 matt LIST_REMOVE(inp, inp_ialink);
288 1.82 matt IFAFREE(&inp->inp_ia->ia_ifa);
289 1.82 matt inp->inp_ia = NULL;
290 1.82 matt }
291 1.28 mycroft inp->inp_laddr = sin->sin_addr;
292 1.82 matt if (ia != NULL) {
293 1.82 matt inp->inp_ia = ia;
294 1.82 matt LIST_INSERT_HEAD(&ia->ia_inpcbs, inp, inp_ialink);
295 1.82 matt IFAREF(&ia->ia_ifa);
296 1.82 matt }
297 1.82 matt
298 1.45 lukem
299 1.28 mycroft noname:
300 1.36 mycroft if (lport == 0) {
301 1.46 lukem int cnt;
302 1.45 lukem u_int16_t min, max;
303 1.45 lukem u_int16_t *lastport;
304 1.45 lukem
305 1.45 lukem if (inp->inp_flags & INP_LOWPORT) {
306 1.45 lukem #ifndef IPNOPRIVPORTS
307 1.80 simonb if (p == 0 || suser(p->p_ucred, &p->p_acflag))
308 1.45 lukem return (EACCES);
309 1.45 lukem #endif
310 1.67 tron min = lowportmin;
311 1.67 tron max = lowportmax;
312 1.45 lukem lastport = &table->inpt_lastlow;
313 1.45 lukem } else {
314 1.45 lukem min = anonportmin;
315 1.45 lukem max = anonportmax;
316 1.45 lukem lastport = &table->inpt_lastport;
317 1.45 lukem }
318 1.45 lukem if (min > max) { /* sanity check */
319 1.45 lukem u_int16_t swp;
320 1.45 lukem
321 1.45 lukem swp = min;
322 1.45 lukem min = max;
323 1.45 lukem max = swp;
324 1.45 lukem }
325 1.43 lukem
326 1.47 lukem lport = *lastport - 1;
327 1.47 lukem for (cnt = max - min + 1; cnt; cnt--, lport--) {
328 1.45 lukem if (lport < min || lport > max)
329 1.47 lukem lport = max;
330 1.36 mycroft if (!in_pcblookup_port(table, inp->inp_laddr,
331 1.54 lukem htons(lport), 1))
332 1.36 mycroft goto found;
333 1.42 lukem }
334 1.82 matt if (!in_nullhost(inp->inp_laddr)) {
335 1.82 matt if (inp->inp_ia != NULL) {
336 1.82 matt LIST_REMOVE(inp, inp_ialink);
337 1.82 matt IFAFREE(&inp->inp_ia->ia_ifa);
338 1.82 matt inp->inp_ia = NULL;
339 1.82 matt }
340 1.40 thorpej inp->inp_laddr.s_addr = INADDR_ANY;
341 1.82 matt }
342 1.36 mycroft return (EAGAIN);
343 1.36 mycroft found:
344 1.45 lukem inp->inp_flags |= INP_ANONPORT;
345 1.45 lukem *lastport = lport;
346 1.36 mycroft lport = htons(lport);
347 1.36 mycroft }
348 1.1 cgd inp->inp_lport = lport;
349 1.33 mycroft in_pcbstate(inp, INP_BOUND);
350 1.1 cgd return (0);
351 1.1 cgd }
352 1.1 cgd
353 1.1 cgd /*
354 1.1 cgd * Connect from a socket to a specified address.
355 1.1 cgd * Both address and port must be specified in argument sin.
356 1.1 cgd * If don't have a local address for this socket yet,
357 1.1 cgd * then pick one.
358 1.1 cgd */
359 1.10 mycroft int
360 1.25 christos in_pcbconnect(v, nam)
361 1.64 augustss void *v;
362 1.1 cgd struct mbuf *nam;
363 1.1 cgd {
364 1.64 augustss struct inpcb *inp = v;
365 1.82 matt struct in_ifaddr *ia = NULL;
366 1.25 christos struct sockaddr_in *ifaddr = NULL;
367 1.64 augustss struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
368 1.40 thorpej int error;
369 1.1 cgd
370 1.1 cgd if (nam->m_len != sizeof (*sin))
371 1.1 cgd return (EINVAL);
372 1.1 cgd if (sin->sin_family != AF_INET)
373 1.1 cgd return (EAFNOSUPPORT);
374 1.1 cgd if (sin->sin_port == 0)
375 1.1 cgd return (EADDRNOTAVAIL);
376 1.72 matt if (TAILQ_FIRST(&in_ifaddr) != 0) {
377 1.1 cgd /*
378 1.1 cgd * If the destination address is INADDR_ANY,
379 1.49 tls * use any local address (likely loopback).
380 1.1 cgd * If the supplied address is INADDR_BROADCAST,
381 1.49 tls * use the broadcast address of an interface
382 1.49 tls * which supports broadcast. (loopback does not)
383 1.1 cgd */
384 1.49 tls
385 1.72 matt if (in_nullhost(sin->sin_addr)) {
386 1.72 matt sin->sin_addr =
387 1.72 matt TAILQ_FIRST(&in_ifaddr)->ia_addr.sin_addr;
388 1.72 matt } else if (sin->sin_addr.s_addr == INADDR_BROADCAST) {
389 1.72 matt TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
390 1.72 matt if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
391 1.72 matt sin->sin_addr =
392 1.72 matt ia->ia_broadaddr.sin_addr;
393 1.72 matt break;
394 1.72 matt }
395 1.49 tls }
396 1.72 matt }
397 1.1 cgd }
398 1.32 mycroft /*
399 1.32 mycroft * If we haven't bound which network number to use as ours,
400 1.32 mycroft * we will use the number of the outgoing interface.
401 1.32 mycroft * This depends on having done a routing lookup, which
402 1.32 mycroft * we will probably have to do anyway, so we might
403 1.32 mycroft * as well do it now. On the other hand if we are
404 1.32 mycroft * sending to multiple destinations we may have already
405 1.32 mycroft * done the lookup, so see if we can use the route
406 1.32 mycroft * from before. In any case, we only
407 1.32 mycroft * chose a port number once, even if sending to multiple
408 1.32 mycroft * destinations.
409 1.32 mycroft */
410 1.32 mycroft if (in_nullhost(inp->inp_laddr)) {
411 1.59 itojun int error;
412 1.59 itojun ifaddr = in_selectsrc(sin, &inp->inp_route,
413 1.59 itojun inp->inp_socket->so_options, inp->inp_moptions, &error);
414 1.59 itojun if (ifaddr == NULL) {
415 1.59 itojun if (error == 0)
416 1.59 itojun error = EADDRNOTAVAIL;
417 1.59 itojun return error;
418 1.59 itojun }
419 1.82 matt INADDR_TO_IA(ifaddr->sin_addr, ia);
420 1.83 itojun if (ia == NULL)
421 1.83 itojun return (EADDRNOTAVAIL);
422 1.1 cgd }
423 1.33 mycroft if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
424 1.32 mycroft !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr,
425 1.24 mycroft inp->inp_lport) != 0)
426 1.1 cgd return (EADDRINUSE);
427 1.32 mycroft if (in_nullhost(inp->inp_laddr)) {
428 1.40 thorpej if (inp->inp_lport == 0) {
429 1.40 thorpej error = in_pcbbind(inp, (struct mbuf *)0,
430 1.28 mycroft (struct proc *)0);
431 1.40 thorpej /*
432 1.40 thorpej * This used to ignore the return value
433 1.40 thorpej * completely, but we need to check for
434 1.40 thorpej * ephemeral port shortage.
435 1.40 thorpej * XXX Should we check for other errors, too?
436 1.40 thorpej */
437 1.40 thorpej if (error == EAGAIN)
438 1.40 thorpej return (error);
439 1.40 thorpej }
440 1.82 matt KASSERT(inp->inp_ia == NULL);
441 1.82 matt inp->inp_laddr = ia->ia_addr.sin_addr;
442 1.82 matt inp->inp_ia = ia;
443 1.82 matt LIST_INSERT_HEAD(&ia->ia_inpcbs, inp, inp_ialink);
444 1.82 matt IFAREF(&ia->ia_ifa);
445 1.1 cgd inp->inp_laddr = ifaddr->sin_addr;
446 1.1 cgd }
447 1.1 cgd inp->inp_faddr = sin->sin_addr;
448 1.1 cgd inp->inp_fport = sin->sin_port;
449 1.33 mycroft in_pcbstate(inp, INP_CONNECTED);
450 1.71 itojun #ifdef IPSEC
451 1.71 itojun if (inp->inp_socket->so_type == SOCK_STREAM)
452 1.71 itojun ipsec_pcbconn(inp->inp_sp);
453 1.71 itojun #endif
454 1.1 cgd return (0);
455 1.1 cgd }
456 1.1 cgd
457 1.25 christos void
458 1.25 christos in_pcbdisconnect(v)
459 1.25 christos void *v;
460 1.1 cgd {
461 1.25 christos struct inpcb *inp = v;
462 1.1 cgd
463 1.32 mycroft inp->inp_faddr = zeroin_addr;
464 1.1 cgd inp->inp_fport = 0;
465 1.33 mycroft in_pcbstate(inp, INP_BOUND);
466 1.1 cgd if (inp->inp_socket->so_state & SS_NOFDREF)
467 1.1 cgd in_pcbdetach(inp);
468 1.71 itojun #ifdef IPSEC
469 1.71 itojun ipsec_pcbdisconn(inp->inp_sp);
470 1.71 itojun #endif
471 1.1 cgd }
472 1.1 cgd
473 1.25 christos void
474 1.25 christos in_pcbdetach(v)
475 1.25 christos void *v;
476 1.1 cgd {
477 1.25 christos struct inpcb *inp = v;
478 1.1 cgd struct socket *so = inp->inp_socket;
479 1.24 mycroft int s;
480 1.1 cgd
481 1.59 itojun #ifdef IPSEC
482 1.59 itojun ipsec4_delete_pcbpolicy(inp);
483 1.59 itojun #endif /*IPSEC*/
484 1.1 cgd so->so_pcb = 0;
485 1.1 cgd sofree(so);
486 1.1 cgd if (inp->inp_options)
487 1.1 cgd (void)m_free(inp->inp_options);
488 1.1 cgd if (inp->inp_route.ro_rt)
489 1.1 cgd rtfree(inp->inp_route.ro_rt);
490 1.6 hpeyerl ip_freemoptions(inp->inp_moptions);
491 1.82 matt if (inp->inp_ia != NULL) {
492 1.82 matt LIST_REMOVE(inp, inp_ialink);
493 1.82 matt IFAFREE(&inp->inp_ia->ia_ifa);
494 1.82 matt inp->inp_ia = NULL;
495 1.82 matt }
496 1.24 mycroft s = splnet();
497 1.33 mycroft in_pcbstate(inp, INP_ATTACHED);
498 1.21 cgd CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue);
499 1.24 mycroft splx(s);
500 1.52 thorpej pool_put(&inpcb_pool, inp);
501 1.1 cgd }
502 1.1 cgd
503 1.25 christos void
504 1.1 cgd in_setsockaddr(inp, nam)
505 1.64 augustss struct inpcb *inp;
506 1.1 cgd struct mbuf *nam;
507 1.1 cgd {
508 1.64 augustss struct sockaddr_in *sin;
509 1.78 itojun
510 1.1 cgd nam->m_len = sizeof (*sin);
511 1.1 cgd sin = mtod(nam, struct sockaddr_in *);
512 1.1 cgd bzero((caddr_t)sin, sizeof (*sin));
513 1.1 cgd sin->sin_family = AF_INET;
514 1.1 cgd sin->sin_len = sizeof(*sin);
515 1.1 cgd sin->sin_port = inp->inp_lport;
516 1.1 cgd sin->sin_addr = inp->inp_laddr;
517 1.1 cgd }
518 1.1 cgd
519 1.25 christos void
520 1.1 cgd in_setpeeraddr(inp, nam)
521 1.1 cgd struct inpcb *inp;
522 1.1 cgd struct mbuf *nam;
523 1.1 cgd {
524 1.64 augustss struct sockaddr_in *sin;
525 1.78 itojun
526 1.1 cgd nam->m_len = sizeof (*sin);
527 1.1 cgd sin = mtod(nam, struct sockaddr_in *);
528 1.1 cgd bzero((caddr_t)sin, sizeof (*sin));
529 1.1 cgd sin->sin_family = AF_INET;
530 1.1 cgd sin->sin_len = sizeof(*sin);
531 1.1 cgd sin->sin_port = inp->inp_fport;
532 1.1 cgd sin->sin_addr = inp->inp_faddr;
533 1.1 cgd }
534 1.1 cgd
535 1.1 cgd /*
536 1.1 cgd * Pass some notification to all connections of a protocol
537 1.1 cgd * associated with address dst. The local address and/or port numbers
538 1.1 cgd * may be specified to limit the search. The "usual action" will be
539 1.1 cgd * taken, depending on the ctlinput cmd. The caller must filter any
540 1.1 cgd * cmds that are uninteresting (e.g., no error in the map).
541 1.1 cgd * Call the protocol specific routine (if any) to report
542 1.1 cgd * any errors for each matching socket.
543 1.1 cgd *
544 1.22 mycroft * Must be called at splsoftnet.
545 1.1 cgd */
546 1.37 thorpej int
547 1.32 mycroft in_pcbnotify(table, faddr, fport_arg, laddr, lport_arg, errno, notify)
548 1.18 mycroft struct inpcbtable *table;
549 1.32 mycroft struct in_addr faddr, laddr;
550 1.10 mycroft u_int fport_arg, lport_arg;
551 1.19 mycroft int errno;
552 1.10 mycroft void (*notify) __P((struct inpcb *, int));
553 1.1 cgd {
554 1.33 mycroft struct inpcbhead *head;
555 1.64 augustss struct inpcb *inp, *ninp;
556 1.13 cgd u_int16_t fport = fport_arg, lport = lport_arg;
557 1.37 thorpej int nmatch;
558 1.1 cgd
559 1.33 mycroft if (in_nullhost(faddr) || notify == 0)
560 1.37 thorpej return (0);
561 1.1 cgd
562 1.37 thorpej nmatch = 0;
563 1.33 mycroft head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
564 1.72 matt for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
565 1.72 matt ninp = LIST_NEXT(inp, inp_hash);
566 1.33 mycroft if (in_hosteq(inp->inp_faddr, faddr) &&
567 1.33 mycroft inp->inp_fport == fport &&
568 1.33 mycroft inp->inp_lport == lport &&
569 1.37 thorpej in_hosteq(inp->inp_laddr, laddr)) {
570 1.33 mycroft (*notify)(inp, errno);
571 1.37 thorpej nmatch++;
572 1.37 thorpej }
573 1.1 cgd }
574 1.37 thorpej return (nmatch);
575 1.18 mycroft }
576 1.18 mycroft
577 1.20 mycroft void
578 1.32 mycroft in_pcbnotifyall(table, faddr, errno, notify)
579 1.18 mycroft struct inpcbtable *table;
580 1.32 mycroft struct in_addr faddr;
581 1.19 mycroft int errno;
582 1.18 mycroft void (*notify) __P((struct inpcb *, int));
583 1.18 mycroft {
584 1.64 augustss struct inpcb *inp, *ninp;
585 1.18 mycroft
586 1.33 mycroft if (in_nullhost(faddr) || notify == 0)
587 1.18 mycroft return;
588 1.18 mycroft
589 1.72 matt for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
590 1.72 matt inp != (void *)&table->inpt_queue;
591 1.33 mycroft inp = ninp) {
592 1.72 matt ninp = CIRCLEQ_NEXT(inp, inp_queue);
593 1.33 mycroft if (in_hosteq(inp->inp_faddr, faddr))
594 1.33 mycroft (*notify)(inp, errno);
595 1.63 thorpej }
596 1.63 thorpej }
597 1.63 thorpej
598 1.63 thorpej void
599 1.69 itojun in_pcbpurgeif0(table, ifp)
600 1.63 thorpej struct inpcbtable *table;
601 1.63 thorpej struct ifnet *ifp;
602 1.63 thorpej {
603 1.64 augustss struct inpcb *inp, *ninp;
604 1.65 enami struct ip_moptions *imo;
605 1.65 enami int i, gap;
606 1.63 thorpej
607 1.72 matt for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
608 1.72 matt inp != (void *)&table->inpt_queue;
609 1.63 thorpej inp = ninp) {
610 1.72 matt ninp = CIRCLEQ_NEXT(inp, inp_queue);
611 1.65 enami imo = inp->inp_moptions;
612 1.65 enami if (imo != NULL) {
613 1.65 enami /*
614 1.65 enami * Unselect the outgoing interface if it is being
615 1.65 enami * detached.
616 1.65 enami */
617 1.65 enami if (imo->imo_multicast_ifp == ifp)
618 1.65 enami imo->imo_multicast_ifp = NULL;
619 1.65 enami
620 1.65 enami /*
621 1.65 enami * Drop multicast group membership if we joined
622 1.65 enami * through the interface being detached.
623 1.65 enami */
624 1.65 enami for (i = 0, gap = 0; i < imo->imo_num_memberships;
625 1.65 enami i++) {
626 1.65 enami if (imo->imo_membership[i]->inm_ifp == ifp) {
627 1.65 enami in_delmulti(imo->imo_membership[i]);
628 1.65 enami gap++;
629 1.65 enami } else if (gap != 0)
630 1.65 enami imo->imo_membership[i - gap] =
631 1.65 enami imo->imo_membership[i];
632 1.65 enami }
633 1.65 enami imo->imo_num_memberships -= gap;
634 1.65 enami }
635 1.69 itojun }
636 1.69 itojun }
637 1.69 itojun
638 1.69 itojun void
639 1.69 itojun in_pcbpurgeif(table, ifp)
640 1.69 itojun struct inpcbtable *table;
641 1.69 itojun struct ifnet *ifp;
642 1.69 itojun {
643 1.69 itojun struct inpcb *inp, *ninp;
644 1.69 itojun
645 1.72 matt for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
646 1.72 matt inp != (void *)&table->inpt_queue;
647 1.69 itojun inp = ninp) {
648 1.72 matt ninp = CIRCLEQ_NEXT(inp, inp_queue);
649 1.69 itojun if (inp->inp_route.ro_rt != NULL &&
650 1.69 itojun inp->inp_route.ro_rt->rt_ifp == ifp)
651 1.69 itojun in_rtchange(inp, 0);
652 1.1 cgd }
653 1.1 cgd }
654 1.1 cgd
655 1.1 cgd /*
656 1.1 cgd * Check for alternatives when higher level complains
657 1.1 cgd * about service problems. For now, invalidate cached
658 1.1 cgd * routing information. If the route was created dynamically
659 1.1 cgd * (by a redirect), time to try a default gateway again.
660 1.1 cgd */
661 1.25 christos void
662 1.1 cgd in_losing(inp)
663 1.1 cgd struct inpcb *inp;
664 1.1 cgd {
665 1.64 augustss struct rtentry *rt;
666 1.10 mycroft struct rt_addrinfo info;
667 1.1 cgd
668 1.1 cgd if ((rt = inp->inp_route.ro_rt)) {
669 1.10 mycroft inp->inp_route.ro_rt = 0;
670 1.10 mycroft bzero((caddr_t)&info, sizeof(info));
671 1.17 mycroft info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst;
672 1.10 mycroft info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
673 1.10 mycroft info.rti_info[RTAX_NETMASK] = rt_mask(rt);
674 1.10 mycroft rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
675 1.1 cgd if (rt->rt_flags & RTF_DYNAMIC)
676 1.1 cgd (void) rtrequest(RTM_DELETE, rt_key(rt),
677 1.78 itojun rt->rt_gateway, rt_mask(rt), rt->rt_flags,
678 1.1 cgd (struct rtentry **)0);
679 1.78 itojun else
680 1.1 cgd /*
681 1.1 cgd * A new route can be allocated
682 1.1 cgd * the next time output is attempted.
683 1.1 cgd */
684 1.10 mycroft rtfree(rt);
685 1.1 cgd }
686 1.1 cgd }
687 1.1 cgd
688 1.1 cgd /*
689 1.1 cgd * After a routing change, flush old routing
690 1.1 cgd * and allocate a (hopefully) better one.
691 1.1 cgd */
692 1.10 mycroft void
693 1.10 mycroft in_rtchange(inp, errno)
694 1.64 augustss struct inpcb *inp;
695 1.10 mycroft int errno;
696 1.1 cgd {
697 1.32 mycroft
698 1.1 cgd if (inp->inp_route.ro_rt) {
699 1.1 cgd rtfree(inp->inp_route.ro_rt);
700 1.1 cgd inp->inp_route.ro_rt = 0;
701 1.1 cgd /*
702 1.1 cgd * A new route can be allocated the next time
703 1.1 cgd * output is attempted.
704 1.1 cgd */
705 1.1 cgd }
706 1.49 tls /* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
707 1.1 cgd }
708 1.1 cgd
709 1.1 cgd struct inpcb *
710 1.54 lukem in_pcblookup_port(table, laddr, lport_arg, lookup_wildcard)
711 1.18 mycroft struct inpcbtable *table;
712 1.33 mycroft struct in_addr laddr;
713 1.33 mycroft u_int lport_arg;
714 1.54 lukem int lookup_wildcard;
715 1.1 cgd {
716 1.64 augustss struct inpcb *inp, *match = 0;
717 1.1 cgd int matchwild = 3, wildcard;
718 1.33 mycroft u_int16_t lport = lport_arg;
719 1.1 cgd
720 1.72 matt CIRCLEQ_FOREACH(inp, &table->inpt_queue, inp_queue) {
721 1.1 cgd if (inp->inp_lport != lport)
722 1.1 cgd continue;
723 1.1 cgd wildcard = 0;
724 1.33 mycroft if (!in_nullhost(inp->inp_faddr))
725 1.33 mycroft wildcard++;
726 1.32 mycroft if (in_nullhost(inp->inp_laddr)) {
727 1.32 mycroft if (!in_nullhost(laddr))
728 1.1 cgd wildcard++;
729 1.1 cgd } else {
730 1.32 mycroft if (in_nullhost(laddr))
731 1.1 cgd wildcard++;
732 1.32 mycroft else {
733 1.32 mycroft if (!in_hosteq(inp->inp_laddr, laddr))
734 1.32 mycroft continue;
735 1.32 mycroft }
736 1.1 cgd }
737 1.54 lukem if (wildcard && !lookup_wildcard)
738 1.1 cgd continue;
739 1.1 cgd if (wildcard < matchwild) {
740 1.1 cgd match = inp;
741 1.1 cgd matchwild = wildcard;
742 1.1 cgd if (matchwild == 0)
743 1.1 cgd break;
744 1.1 cgd }
745 1.1 cgd }
746 1.1 cgd return (match);
747 1.24 mycroft }
748 1.24 mycroft
749 1.24 mycroft #ifdef DIAGNOSTIC
750 1.24 mycroft int in_pcbnotifymiss = 0;
751 1.24 mycroft #endif
752 1.24 mycroft
753 1.24 mycroft struct inpcb *
754 1.33 mycroft in_pcblookup_connect(table, faddr, fport_arg, laddr, lport_arg)
755 1.24 mycroft struct inpcbtable *table;
756 1.24 mycroft struct in_addr faddr, laddr;
757 1.24 mycroft u_int fport_arg, lport_arg;
758 1.24 mycroft {
759 1.24 mycroft struct inpcbhead *head;
760 1.64 augustss struct inpcb *inp;
761 1.24 mycroft u_int16_t fport = fport_arg, lport = lport_arg;
762 1.24 mycroft
763 1.33 mycroft head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
764 1.72 matt LIST_FOREACH(inp, head, inp_hash) {
765 1.33 mycroft if (in_hosteq(inp->inp_faddr, faddr) &&
766 1.33 mycroft inp->inp_fport == fport &&
767 1.33 mycroft inp->inp_lport == lport &&
768 1.33 mycroft in_hosteq(inp->inp_laddr, laddr))
769 1.33 mycroft goto out;
770 1.24 mycroft }
771 1.24 mycroft #ifdef DIAGNOSTIC
772 1.33 mycroft if (in_pcbnotifymiss) {
773 1.35 christos printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
774 1.24 mycroft ntohl(faddr.s_addr), ntohs(fport),
775 1.24 mycroft ntohl(laddr.s_addr), ntohs(lport));
776 1.24 mycroft }
777 1.24 mycroft #endif
778 1.33 mycroft return (0);
779 1.33 mycroft
780 1.33 mycroft out:
781 1.33 mycroft /* Move this PCB to the head of hash chain. */
782 1.72 matt if (inp != LIST_FIRST(head)) {
783 1.33 mycroft LIST_REMOVE(inp, inp_hash);
784 1.33 mycroft LIST_INSERT_HEAD(head, inp, inp_hash);
785 1.33 mycroft }
786 1.33 mycroft return (inp);
787 1.33 mycroft }
788 1.33 mycroft
789 1.33 mycroft struct inpcb *
790 1.33 mycroft in_pcblookup_bind(table, laddr, lport_arg)
791 1.33 mycroft struct inpcbtable *table;
792 1.33 mycroft struct in_addr laddr;
793 1.33 mycroft u_int lport_arg;
794 1.33 mycroft {
795 1.33 mycroft struct inpcbhead *head;
796 1.64 augustss struct inpcb *inp;
797 1.33 mycroft u_int16_t lport = lport_arg;
798 1.33 mycroft
799 1.33 mycroft head = INPCBHASH_BIND(table, laddr, lport);
800 1.72 matt LIST_FOREACH(inp, head, inp_hash) {
801 1.33 mycroft if (inp->inp_lport == lport &&
802 1.33 mycroft in_hosteq(inp->inp_laddr, laddr))
803 1.33 mycroft goto out;
804 1.33 mycroft }
805 1.33 mycroft head = INPCBHASH_BIND(table, zeroin_addr, lport);
806 1.72 matt LIST_FOREACH(inp, head, inp_hash) {
807 1.33 mycroft if (inp->inp_lport == lport &&
808 1.33 mycroft in_hosteq(inp->inp_laddr, zeroin_addr))
809 1.33 mycroft goto out;
810 1.33 mycroft }
811 1.33 mycroft #ifdef DIAGNOSTIC
812 1.33 mycroft if (in_pcbnotifymiss) {
813 1.35 christos printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
814 1.33 mycroft ntohl(laddr.s_addr), ntohs(lport));
815 1.33 mycroft }
816 1.33 mycroft #endif
817 1.33 mycroft return (0);
818 1.33 mycroft
819 1.33 mycroft out:
820 1.33 mycroft /* Move this PCB to the head of hash chain. */
821 1.72 matt if (inp != LIST_FIRST(head)) {
822 1.33 mycroft LIST_REMOVE(inp, inp_hash);
823 1.33 mycroft LIST_INSERT_HEAD(head, inp, inp_hash);
824 1.33 mycroft }
825 1.24 mycroft return (inp);
826 1.33 mycroft }
827 1.33 mycroft
828 1.33 mycroft void
829 1.33 mycroft in_pcbstate(inp, state)
830 1.33 mycroft struct inpcb *inp;
831 1.33 mycroft int state;
832 1.33 mycroft {
833 1.33 mycroft
834 1.33 mycroft if (inp->inp_state > INP_ATTACHED)
835 1.33 mycroft LIST_REMOVE(inp, inp_hash);
836 1.33 mycroft
837 1.33 mycroft switch (state) {
838 1.33 mycroft case INP_BOUND:
839 1.33 mycroft LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
840 1.33 mycroft inp->inp_laddr, inp->inp_lport), inp, inp_hash);
841 1.33 mycroft break;
842 1.33 mycroft case INP_CONNECTED:
843 1.33 mycroft LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
844 1.33 mycroft inp->inp_faddr, inp->inp_fport,
845 1.33 mycroft inp->inp_laddr, inp->inp_lport), inp, inp_hash);
846 1.33 mycroft break;
847 1.33 mycroft }
848 1.33 mycroft
849 1.33 mycroft inp->inp_state = state;
850 1.38 thorpej }
851 1.38 thorpej
852 1.38 thorpej struct rtentry *
853 1.38 thorpej in_pcbrtentry(inp)
854 1.38 thorpej struct inpcb *inp;
855 1.38 thorpej {
856 1.38 thorpej struct route *ro;
857 1.38 thorpej
858 1.38 thorpej ro = &inp->inp_route;
859 1.38 thorpej
860 1.77 itojun if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
861 1.77 itojun !in_hosteq(satosin(&ro->ro_dst)->sin_addr, inp->inp_faddr))) {
862 1.77 itojun RTFREE(ro->ro_rt);
863 1.77 itojun ro->ro_rt = (struct rtentry *)NULL;
864 1.77 itojun }
865 1.77 itojun if (ro->ro_rt == (struct rtentry *)NULL &&
866 1.77 itojun !in_nullhost(inp->inp_faddr)) {
867 1.77 itojun bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
868 1.77 itojun ro->ro_dst.sa_family = AF_INET;
869 1.77 itojun ro->ro_dst.sa_len = sizeof(ro->ro_dst);
870 1.77 itojun satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr;
871 1.77 itojun rtalloc(ro);
872 1.38 thorpej }
873 1.38 thorpej return (ro->ro_rt);
874 1.59 itojun }
875 1.59 itojun
876 1.59 itojun struct sockaddr_in *
877 1.59 itojun in_selectsrc(sin, ro, soopts, mopts, errorp)
878 1.59 itojun struct sockaddr_in *sin;
879 1.59 itojun struct route *ro;
880 1.59 itojun int soopts;
881 1.59 itojun struct ip_moptions *mopts;
882 1.59 itojun int *errorp;
883 1.59 itojun {
884 1.59 itojun struct in_ifaddr *ia;
885 1.59 itojun
886 1.59 itojun ia = (struct in_ifaddr *)0;
887 1.78 itojun /*
888 1.59 itojun * If route is known or can be allocated now,
889 1.59 itojun * our src addr is taken from the i/f, else punt.
890 1.74 itojun * Note that we should check the address family of the cached
891 1.74 itojun * destination, in case of sharing the cache with IPv6.
892 1.59 itojun */
893 1.59 itojun if (ro->ro_rt &&
894 1.74 itojun (ro->ro_dst.sa_family != AF_INET ||
895 1.74 itojun !in_hosteq(satosin(&ro->ro_dst)->sin_addr, sin->sin_addr) ||
896 1.59 itojun soopts & SO_DONTROUTE)) {
897 1.59 itojun RTFREE(ro->ro_rt);
898 1.59 itojun ro->ro_rt = (struct rtentry *)0;
899 1.59 itojun }
900 1.59 itojun if ((soopts & SO_DONTROUTE) == 0 && /*XXX*/
901 1.59 itojun (ro->ro_rt == (struct rtentry *)0 ||
902 1.74 itojun ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
903 1.59 itojun /* No route yet, so try to acquire one */
904 1.74 itojun bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
905 1.59 itojun ro->ro_dst.sa_family = AF_INET;
906 1.59 itojun ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
907 1.59 itojun satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
908 1.59 itojun rtalloc(ro);
909 1.59 itojun }
910 1.59 itojun /*
911 1.59 itojun * If we found a route, use the address
912 1.59 itojun * corresponding to the outgoing interface
913 1.59 itojun * unless it is the loopback (in case a route
914 1.59 itojun * to our address on another net goes to loopback).
915 1.78 itojun *
916 1.59 itojun * XXX Is this still true? Do we care?
917 1.59 itojun */
918 1.59 itojun if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
919 1.59 itojun ia = ifatoia(ro->ro_rt->rt_ifa);
920 1.62 thorpej if (ia == NULL) {
921 1.59 itojun u_int16_t fport = sin->sin_port;
922 1.59 itojun
923 1.59 itojun sin->sin_port = 0;
924 1.59 itojun ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
925 1.59 itojun sin->sin_port = fport;
926 1.59 itojun if (ia == 0) {
927 1.59 itojun /* Find 1st non-loopback AF_INET address */
928 1.72 matt TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
929 1.59 itojun if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
930 1.59 itojun break;
931 1.62 thorpej }
932 1.59 itojun }
933 1.62 thorpej if (ia == NULL) {
934 1.59 itojun *errorp = EADDRNOTAVAIL;
935 1.59 itojun return NULL;
936 1.59 itojun }
937 1.59 itojun }
938 1.59 itojun /*
939 1.59 itojun * If the destination address is multicast and an outgoing
940 1.59 itojun * interface has been set as a multicast option, use the
941 1.59 itojun * address of that interface as our source address.
942 1.59 itojun */
943 1.59 itojun if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
944 1.59 itojun struct ip_moptions *imo;
945 1.59 itojun struct ifnet *ifp;
946 1.59 itojun
947 1.59 itojun imo = mopts;
948 1.59 itojun if (imo->imo_multicast_ifp != NULL) {
949 1.59 itojun ifp = imo->imo_multicast_ifp;
950 1.59 itojun IFP_TO_IA(ifp, ia); /* XXX */
951 1.59 itojun if (ia == 0) {
952 1.59 itojun *errorp = EADDRNOTAVAIL;
953 1.59 itojun return NULL;
954 1.59 itojun }
955 1.59 itojun }
956 1.59 itojun }
957 1.59 itojun return satosin(&ia->ia_addr);
958 1.1 cgd }
959