in_pcb.c revision 1.82 1 1.82 matt /* $NetBSD: in_pcb.c,v 1.82 2003/06/15 02:49:33 matt 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.82 matt __KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.82 2003/06/15 02:49:33 matt 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.1 cgd }
421 1.33 mycroft if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
422 1.32 mycroft !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr,
423 1.24 mycroft inp->inp_lport) != 0)
424 1.1 cgd return (EADDRINUSE);
425 1.32 mycroft if (in_nullhost(inp->inp_laddr)) {
426 1.40 thorpej if (inp->inp_lport == 0) {
427 1.40 thorpej error = in_pcbbind(inp, (struct mbuf *)0,
428 1.28 mycroft (struct proc *)0);
429 1.40 thorpej /*
430 1.40 thorpej * This used to ignore the return value
431 1.40 thorpej * completely, but we need to check for
432 1.40 thorpej * ephemeral port shortage.
433 1.40 thorpej * XXX Should we check for other errors, too?
434 1.40 thorpej */
435 1.40 thorpej if (error == EAGAIN)
436 1.40 thorpej return (error);
437 1.40 thorpej }
438 1.82 matt KASSERT(inp->inp_ia == NULL);
439 1.82 matt inp->inp_laddr = ia->ia_addr.sin_addr;
440 1.82 matt inp->inp_ia = ia;
441 1.82 matt LIST_INSERT_HEAD(&ia->ia_inpcbs, inp, inp_ialink);
442 1.82 matt IFAREF(&ia->ia_ifa);
443 1.1 cgd inp->inp_laddr = ifaddr->sin_addr;
444 1.1 cgd }
445 1.1 cgd inp->inp_faddr = sin->sin_addr;
446 1.1 cgd inp->inp_fport = sin->sin_port;
447 1.33 mycroft in_pcbstate(inp, INP_CONNECTED);
448 1.71 itojun #ifdef IPSEC
449 1.71 itojun if (inp->inp_socket->so_type == SOCK_STREAM)
450 1.71 itojun ipsec_pcbconn(inp->inp_sp);
451 1.71 itojun #endif
452 1.1 cgd return (0);
453 1.1 cgd }
454 1.1 cgd
455 1.25 christos void
456 1.25 christos in_pcbdisconnect(v)
457 1.25 christos void *v;
458 1.1 cgd {
459 1.25 christos struct inpcb *inp = v;
460 1.1 cgd
461 1.32 mycroft inp->inp_faddr = zeroin_addr;
462 1.1 cgd inp->inp_fport = 0;
463 1.33 mycroft in_pcbstate(inp, INP_BOUND);
464 1.1 cgd if (inp->inp_socket->so_state & SS_NOFDREF)
465 1.1 cgd in_pcbdetach(inp);
466 1.71 itojun #ifdef IPSEC
467 1.71 itojun ipsec_pcbdisconn(inp->inp_sp);
468 1.71 itojun #endif
469 1.1 cgd }
470 1.1 cgd
471 1.25 christos void
472 1.25 christos in_pcbdetach(v)
473 1.25 christos void *v;
474 1.1 cgd {
475 1.25 christos struct inpcb *inp = v;
476 1.1 cgd struct socket *so = inp->inp_socket;
477 1.24 mycroft int s;
478 1.1 cgd
479 1.59 itojun #ifdef IPSEC
480 1.59 itojun ipsec4_delete_pcbpolicy(inp);
481 1.59 itojun #endif /*IPSEC*/
482 1.1 cgd so->so_pcb = 0;
483 1.1 cgd sofree(so);
484 1.1 cgd if (inp->inp_options)
485 1.1 cgd (void)m_free(inp->inp_options);
486 1.1 cgd if (inp->inp_route.ro_rt)
487 1.1 cgd rtfree(inp->inp_route.ro_rt);
488 1.6 hpeyerl ip_freemoptions(inp->inp_moptions);
489 1.82 matt if (inp->inp_ia != NULL) {
490 1.82 matt LIST_REMOVE(inp, inp_ialink);
491 1.82 matt IFAFREE(&inp->inp_ia->ia_ifa);
492 1.82 matt inp->inp_ia = NULL;
493 1.82 matt }
494 1.24 mycroft s = splnet();
495 1.33 mycroft in_pcbstate(inp, INP_ATTACHED);
496 1.21 cgd CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue);
497 1.24 mycroft splx(s);
498 1.52 thorpej pool_put(&inpcb_pool, inp);
499 1.1 cgd }
500 1.1 cgd
501 1.25 christos void
502 1.1 cgd in_setsockaddr(inp, nam)
503 1.64 augustss struct inpcb *inp;
504 1.1 cgd struct mbuf *nam;
505 1.1 cgd {
506 1.64 augustss struct sockaddr_in *sin;
507 1.78 itojun
508 1.1 cgd nam->m_len = sizeof (*sin);
509 1.1 cgd sin = mtod(nam, struct sockaddr_in *);
510 1.1 cgd bzero((caddr_t)sin, sizeof (*sin));
511 1.1 cgd sin->sin_family = AF_INET;
512 1.1 cgd sin->sin_len = sizeof(*sin);
513 1.1 cgd sin->sin_port = inp->inp_lport;
514 1.1 cgd sin->sin_addr = inp->inp_laddr;
515 1.1 cgd }
516 1.1 cgd
517 1.25 christos void
518 1.1 cgd in_setpeeraddr(inp, nam)
519 1.1 cgd struct inpcb *inp;
520 1.1 cgd struct mbuf *nam;
521 1.1 cgd {
522 1.64 augustss struct sockaddr_in *sin;
523 1.78 itojun
524 1.1 cgd nam->m_len = sizeof (*sin);
525 1.1 cgd sin = mtod(nam, struct sockaddr_in *);
526 1.1 cgd bzero((caddr_t)sin, sizeof (*sin));
527 1.1 cgd sin->sin_family = AF_INET;
528 1.1 cgd sin->sin_len = sizeof(*sin);
529 1.1 cgd sin->sin_port = inp->inp_fport;
530 1.1 cgd sin->sin_addr = inp->inp_faddr;
531 1.1 cgd }
532 1.1 cgd
533 1.1 cgd /*
534 1.1 cgd * Pass some notification to all connections of a protocol
535 1.1 cgd * associated with address dst. The local address and/or port numbers
536 1.1 cgd * may be specified to limit the search. The "usual action" will be
537 1.1 cgd * taken, depending on the ctlinput cmd. The caller must filter any
538 1.1 cgd * cmds that are uninteresting (e.g., no error in the map).
539 1.1 cgd * Call the protocol specific routine (if any) to report
540 1.1 cgd * any errors for each matching socket.
541 1.1 cgd *
542 1.22 mycroft * Must be called at splsoftnet.
543 1.1 cgd */
544 1.37 thorpej int
545 1.32 mycroft in_pcbnotify(table, faddr, fport_arg, laddr, lport_arg, errno, notify)
546 1.18 mycroft struct inpcbtable *table;
547 1.32 mycroft struct in_addr faddr, laddr;
548 1.10 mycroft u_int fport_arg, lport_arg;
549 1.19 mycroft int errno;
550 1.10 mycroft void (*notify) __P((struct inpcb *, int));
551 1.1 cgd {
552 1.33 mycroft struct inpcbhead *head;
553 1.64 augustss struct inpcb *inp, *ninp;
554 1.13 cgd u_int16_t fport = fport_arg, lport = lport_arg;
555 1.37 thorpej int nmatch;
556 1.1 cgd
557 1.33 mycroft if (in_nullhost(faddr) || notify == 0)
558 1.37 thorpej return (0);
559 1.1 cgd
560 1.37 thorpej nmatch = 0;
561 1.33 mycroft head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
562 1.72 matt for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
563 1.72 matt ninp = LIST_NEXT(inp, inp_hash);
564 1.33 mycroft if (in_hosteq(inp->inp_faddr, faddr) &&
565 1.33 mycroft inp->inp_fport == fport &&
566 1.33 mycroft inp->inp_lport == lport &&
567 1.37 thorpej in_hosteq(inp->inp_laddr, laddr)) {
568 1.33 mycroft (*notify)(inp, errno);
569 1.37 thorpej nmatch++;
570 1.37 thorpej }
571 1.1 cgd }
572 1.37 thorpej return (nmatch);
573 1.18 mycroft }
574 1.18 mycroft
575 1.20 mycroft void
576 1.32 mycroft in_pcbnotifyall(table, faddr, errno, notify)
577 1.18 mycroft struct inpcbtable *table;
578 1.32 mycroft struct in_addr faddr;
579 1.19 mycroft int errno;
580 1.18 mycroft void (*notify) __P((struct inpcb *, int));
581 1.18 mycroft {
582 1.64 augustss struct inpcb *inp, *ninp;
583 1.18 mycroft
584 1.33 mycroft if (in_nullhost(faddr) || notify == 0)
585 1.18 mycroft return;
586 1.18 mycroft
587 1.72 matt for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
588 1.72 matt inp != (void *)&table->inpt_queue;
589 1.33 mycroft inp = ninp) {
590 1.72 matt ninp = CIRCLEQ_NEXT(inp, inp_queue);
591 1.33 mycroft if (in_hosteq(inp->inp_faddr, faddr))
592 1.33 mycroft (*notify)(inp, errno);
593 1.63 thorpej }
594 1.63 thorpej }
595 1.63 thorpej
596 1.63 thorpej void
597 1.69 itojun in_pcbpurgeif0(table, ifp)
598 1.63 thorpej struct inpcbtable *table;
599 1.63 thorpej struct ifnet *ifp;
600 1.63 thorpej {
601 1.64 augustss struct inpcb *inp, *ninp;
602 1.65 enami struct ip_moptions *imo;
603 1.65 enami int i, gap;
604 1.63 thorpej
605 1.72 matt for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
606 1.72 matt inp != (void *)&table->inpt_queue;
607 1.63 thorpej inp = ninp) {
608 1.72 matt ninp = CIRCLEQ_NEXT(inp, inp_queue);
609 1.65 enami imo = inp->inp_moptions;
610 1.65 enami if (imo != NULL) {
611 1.65 enami /*
612 1.65 enami * Unselect the outgoing interface if it is being
613 1.65 enami * detached.
614 1.65 enami */
615 1.65 enami if (imo->imo_multicast_ifp == ifp)
616 1.65 enami imo->imo_multicast_ifp = NULL;
617 1.65 enami
618 1.65 enami /*
619 1.65 enami * Drop multicast group membership if we joined
620 1.65 enami * through the interface being detached.
621 1.65 enami */
622 1.65 enami for (i = 0, gap = 0; i < imo->imo_num_memberships;
623 1.65 enami i++) {
624 1.65 enami if (imo->imo_membership[i]->inm_ifp == ifp) {
625 1.65 enami in_delmulti(imo->imo_membership[i]);
626 1.65 enami gap++;
627 1.65 enami } else if (gap != 0)
628 1.65 enami imo->imo_membership[i - gap] =
629 1.65 enami imo->imo_membership[i];
630 1.65 enami }
631 1.65 enami imo->imo_num_memberships -= gap;
632 1.65 enami }
633 1.69 itojun }
634 1.69 itojun }
635 1.69 itojun
636 1.69 itojun void
637 1.69 itojun in_pcbpurgeif(table, ifp)
638 1.69 itojun struct inpcbtable *table;
639 1.69 itojun struct ifnet *ifp;
640 1.69 itojun {
641 1.69 itojun struct inpcb *inp, *ninp;
642 1.69 itojun
643 1.72 matt for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
644 1.72 matt inp != (void *)&table->inpt_queue;
645 1.69 itojun inp = ninp) {
646 1.72 matt ninp = CIRCLEQ_NEXT(inp, inp_queue);
647 1.69 itojun if (inp->inp_route.ro_rt != NULL &&
648 1.69 itojun inp->inp_route.ro_rt->rt_ifp == ifp)
649 1.69 itojun in_rtchange(inp, 0);
650 1.1 cgd }
651 1.1 cgd }
652 1.1 cgd
653 1.1 cgd /*
654 1.1 cgd * Check for alternatives when higher level complains
655 1.1 cgd * about service problems. For now, invalidate cached
656 1.1 cgd * routing information. If the route was created dynamically
657 1.1 cgd * (by a redirect), time to try a default gateway again.
658 1.1 cgd */
659 1.25 christos void
660 1.1 cgd in_losing(inp)
661 1.1 cgd struct inpcb *inp;
662 1.1 cgd {
663 1.64 augustss struct rtentry *rt;
664 1.10 mycroft struct rt_addrinfo info;
665 1.1 cgd
666 1.1 cgd if ((rt = inp->inp_route.ro_rt)) {
667 1.10 mycroft inp->inp_route.ro_rt = 0;
668 1.10 mycroft bzero((caddr_t)&info, sizeof(info));
669 1.17 mycroft info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst;
670 1.10 mycroft info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
671 1.10 mycroft info.rti_info[RTAX_NETMASK] = rt_mask(rt);
672 1.10 mycroft rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
673 1.1 cgd if (rt->rt_flags & RTF_DYNAMIC)
674 1.1 cgd (void) rtrequest(RTM_DELETE, rt_key(rt),
675 1.78 itojun rt->rt_gateway, rt_mask(rt), rt->rt_flags,
676 1.1 cgd (struct rtentry **)0);
677 1.78 itojun else
678 1.1 cgd /*
679 1.1 cgd * A new route can be allocated
680 1.1 cgd * the next time output is attempted.
681 1.1 cgd */
682 1.10 mycroft rtfree(rt);
683 1.1 cgd }
684 1.1 cgd }
685 1.1 cgd
686 1.1 cgd /*
687 1.1 cgd * After a routing change, flush old routing
688 1.1 cgd * and allocate a (hopefully) better one.
689 1.1 cgd */
690 1.10 mycroft void
691 1.10 mycroft in_rtchange(inp, errno)
692 1.64 augustss struct inpcb *inp;
693 1.10 mycroft int errno;
694 1.1 cgd {
695 1.32 mycroft
696 1.1 cgd if (inp->inp_route.ro_rt) {
697 1.1 cgd rtfree(inp->inp_route.ro_rt);
698 1.1 cgd inp->inp_route.ro_rt = 0;
699 1.1 cgd /*
700 1.1 cgd * A new route can be allocated the next time
701 1.1 cgd * output is attempted.
702 1.1 cgd */
703 1.1 cgd }
704 1.49 tls /* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
705 1.1 cgd }
706 1.1 cgd
707 1.1 cgd struct inpcb *
708 1.54 lukem in_pcblookup_port(table, laddr, lport_arg, lookup_wildcard)
709 1.18 mycroft struct inpcbtable *table;
710 1.33 mycroft struct in_addr laddr;
711 1.33 mycroft u_int lport_arg;
712 1.54 lukem int lookup_wildcard;
713 1.1 cgd {
714 1.64 augustss struct inpcb *inp, *match = 0;
715 1.1 cgd int matchwild = 3, wildcard;
716 1.33 mycroft u_int16_t lport = lport_arg;
717 1.1 cgd
718 1.72 matt CIRCLEQ_FOREACH(inp, &table->inpt_queue, inp_queue) {
719 1.1 cgd if (inp->inp_lport != lport)
720 1.1 cgd continue;
721 1.1 cgd wildcard = 0;
722 1.33 mycroft if (!in_nullhost(inp->inp_faddr))
723 1.33 mycroft wildcard++;
724 1.32 mycroft if (in_nullhost(inp->inp_laddr)) {
725 1.32 mycroft if (!in_nullhost(laddr))
726 1.1 cgd wildcard++;
727 1.1 cgd } else {
728 1.32 mycroft if (in_nullhost(laddr))
729 1.1 cgd wildcard++;
730 1.32 mycroft else {
731 1.32 mycroft if (!in_hosteq(inp->inp_laddr, laddr))
732 1.32 mycroft continue;
733 1.32 mycroft }
734 1.1 cgd }
735 1.54 lukem if (wildcard && !lookup_wildcard)
736 1.1 cgd continue;
737 1.1 cgd if (wildcard < matchwild) {
738 1.1 cgd match = inp;
739 1.1 cgd matchwild = wildcard;
740 1.1 cgd if (matchwild == 0)
741 1.1 cgd break;
742 1.1 cgd }
743 1.1 cgd }
744 1.1 cgd return (match);
745 1.24 mycroft }
746 1.24 mycroft
747 1.24 mycroft #ifdef DIAGNOSTIC
748 1.24 mycroft int in_pcbnotifymiss = 0;
749 1.24 mycroft #endif
750 1.24 mycroft
751 1.24 mycroft struct inpcb *
752 1.33 mycroft in_pcblookup_connect(table, faddr, fport_arg, laddr, lport_arg)
753 1.24 mycroft struct inpcbtable *table;
754 1.24 mycroft struct in_addr faddr, laddr;
755 1.24 mycroft u_int fport_arg, lport_arg;
756 1.24 mycroft {
757 1.24 mycroft struct inpcbhead *head;
758 1.64 augustss struct inpcb *inp;
759 1.24 mycroft u_int16_t fport = fport_arg, lport = lport_arg;
760 1.24 mycroft
761 1.33 mycroft head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
762 1.72 matt LIST_FOREACH(inp, head, inp_hash) {
763 1.33 mycroft if (in_hosteq(inp->inp_faddr, faddr) &&
764 1.33 mycroft inp->inp_fport == fport &&
765 1.33 mycroft inp->inp_lport == lport &&
766 1.33 mycroft in_hosteq(inp->inp_laddr, laddr))
767 1.33 mycroft goto out;
768 1.24 mycroft }
769 1.24 mycroft #ifdef DIAGNOSTIC
770 1.33 mycroft if (in_pcbnotifymiss) {
771 1.35 christos printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
772 1.24 mycroft ntohl(faddr.s_addr), ntohs(fport),
773 1.24 mycroft ntohl(laddr.s_addr), ntohs(lport));
774 1.24 mycroft }
775 1.24 mycroft #endif
776 1.33 mycroft return (0);
777 1.33 mycroft
778 1.33 mycroft out:
779 1.33 mycroft /* Move this PCB to the head of hash chain. */
780 1.72 matt if (inp != LIST_FIRST(head)) {
781 1.33 mycroft LIST_REMOVE(inp, inp_hash);
782 1.33 mycroft LIST_INSERT_HEAD(head, inp, inp_hash);
783 1.33 mycroft }
784 1.33 mycroft return (inp);
785 1.33 mycroft }
786 1.33 mycroft
787 1.33 mycroft struct inpcb *
788 1.33 mycroft in_pcblookup_bind(table, laddr, lport_arg)
789 1.33 mycroft struct inpcbtable *table;
790 1.33 mycroft struct in_addr laddr;
791 1.33 mycroft u_int lport_arg;
792 1.33 mycroft {
793 1.33 mycroft struct inpcbhead *head;
794 1.64 augustss struct inpcb *inp;
795 1.33 mycroft u_int16_t lport = lport_arg;
796 1.33 mycroft
797 1.33 mycroft head = INPCBHASH_BIND(table, laddr, lport);
798 1.72 matt LIST_FOREACH(inp, head, inp_hash) {
799 1.33 mycroft if (inp->inp_lport == lport &&
800 1.33 mycroft in_hosteq(inp->inp_laddr, laddr))
801 1.33 mycroft goto out;
802 1.33 mycroft }
803 1.33 mycroft head = INPCBHASH_BIND(table, zeroin_addr, lport);
804 1.72 matt LIST_FOREACH(inp, head, inp_hash) {
805 1.33 mycroft if (inp->inp_lport == lport &&
806 1.33 mycroft in_hosteq(inp->inp_laddr, zeroin_addr))
807 1.33 mycroft goto out;
808 1.33 mycroft }
809 1.33 mycroft #ifdef DIAGNOSTIC
810 1.33 mycroft if (in_pcbnotifymiss) {
811 1.35 christos printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
812 1.33 mycroft ntohl(laddr.s_addr), ntohs(lport));
813 1.33 mycroft }
814 1.33 mycroft #endif
815 1.33 mycroft return (0);
816 1.33 mycroft
817 1.33 mycroft out:
818 1.33 mycroft /* Move this PCB to the head of hash chain. */
819 1.72 matt if (inp != LIST_FIRST(head)) {
820 1.33 mycroft LIST_REMOVE(inp, inp_hash);
821 1.33 mycroft LIST_INSERT_HEAD(head, inp, inp_hash);
822 1.33 mycroft }
823 1.24 mycroft return (inp);
824 1.33 mycroft }
825 1.33 mycroft
826 1.33 mycroft void
827 1.33 mycroft in_pcbstate(inp, state)
828 1.33 mycroft struct inpcb *inp;
829 1.33 mycroft int state;
830 1.33 mycroft {
831 1.33 mycroft
832 1.33 mycroft if (inp->inp_state > INP_ATTACHED)
833 1.33 mycroft LIST_REMOVE(inp, inp_hash);
834 1.33 mycroft
835 1.33 mycroft switch (state) {
836 1.33 mycroft case INP_BOUND:
837 1.33 mycroft LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
838 1.33 mycroft inp->inp_laddr, inp->inp_lport), inp, inp_hash);
839 1.33 mycroft break;
840 1.33 mycroft case INP_CONNECTED:
841 1.33 mycroft LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
842 1.33 mycroft inp->inp_faddr, inp->inp_fport,
843 1.33 mycroft inp->inp_laddr, inp->inp_lport), inp, inp_hash);
844 1.33 mycroft break;
845 1.33 mycroft }
846 1.33 mycroft
847 1.33 mycroft inp->inp_state = state;
848 1.38 thorpej }
849 1.38 thorpej
850 1.38 thorpej struct rtentry *
851 1.38 thorpej in_pcbrtentry(inp)
852 1.38 thorpej struct inpcb *inp;
853 1.38 thorpej {
854 1.38 thorpej struct route *ro;
855 1.38 thorpej
856 1.38 thorpej ro = &inp->inp_route;
857 1.38 thorpej
858 1.77 itojun if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
859 1.77 itojun !in_hosteq(satosin(&ro->ro_dst)->sin_addr, inp->inp_faddr))) {
860 1.77 itojun RTFREE(ro->ro_rt);
861 1.77 itojun ro->ro_rt = (struct rtentry *)NULL;
862 1.77 itojun }
863 1.77 itojun if (ro->ro_rt == (struct rtentry *)NULL &&
864 1.77 itojun !in_nullhost(inp->inp_faddr)) {
865 1.77 itojun bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
866 1.77 itojun ro->ro_dst.sa_family = AF_INET;
867 1.77 itojun ro->ro_dst.sa_len = sizeof(ro->ro_dst);
868 1.77 itojun satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr;
869 1.77 itojun rtalloc(ro);
870 1.38 thorpej }
871 1.38 thorpej return (ro->ro_rt);
872 1.59 itojun }
873 1.59 itojun
874 1.59 itojun struct sockaddr_in *
875 1.59 itojun in_selectsrc(sin, ro, soopts, mopts, errorp)
876 1.59 itojun struct sockaddr_in *sin;
877 1.59 itojun struct route *ro;
878 1.59 itojun int soopts;
879 1.59 itojun struct ip_moptions *mopts;
880 1.59 itojun int *errorp;
881 1.59 itojun {
882 1.59 itojun struct in_ifaddr *ia;
883 1.59 itojun
884 1.59 itojun ia = (struct in_ifaddr *)0;
885 1.78 itojun /*
886 1.59 itojun * If route is known or can be allocated now,
887 1.59 itojun * our src addr is taken from the i/f, else punt.
888 1.74 itojun * Note that we should check the address family of the cached
889 1.74 itojun * destination, in case of sharing the cache with IPv6.
890 1.59 itojun */
891 1.59 itojun if (ro->ro_rt &&
892 1.74 itojun (ro->ro_dst.sa_family != AF_INET ||
893 1.74 itojun !in_hosteq(satosin(&ro->ro_dst)->sin_addr, sin->sin_addr) ||
894 1.59 itojun soopts & SO_DONTROUTE)) {
895 1.59 itojun RTFREE(ro->ro_rt);
896 1.59 itojun ro->ro_rt = (struct rtentry *)0;
897 1.59 itojun }
898 1.59 itojun if ((soopts & SO_DONTROUTE) == 0 && /*XXX*/
899 1.59 itojun (ro->ro_rt == (struct rtentry *)0 ||
900 1.74 itojun ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
901 1.59 itojun /* No route yet, so try to acquire one */
902 1.74 itojun bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
903 1.59 itojun ro->ro_dst.sa_family = AF_INET;
904 1.59 itojun ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
905 1.59 itojun satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
906 1.59 itojun rtalloc(ro);
907 1.59 itojun }
908 1.59 itojun /*
909 1.59 itojun * If we found a route, use the address
910 1.59 itojun * corresponding to the outgoing interface
911 1.59 itojun * unless it is the loopback (in case a route
912 1.59 itojun * to our address on another net goes to loopback).
913 1.78 itojun *
914 1.59 itojun * XXX Is this still true? Do we care?
915 1.59 itojun */
916 1.59 itojun if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
917 1.59 itojun ia = ifatoia(ro->ro_rt->rt_ifa);
918 1.62 thorpej if (ia == NULL) {
919 1.59 itojun u_int16_t fport = sin->sin_port;
920 1.59 itojun
921 1.59 itojun sin->sin_port = 0;
922 1.59 itojun ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
923 1.59 itojun sin->sin_port = fport;
924 1.59 itojun if (ia == 0) {
925 1.59 itojun /* Find 1st non-loopback AF_INET address */
926 1.72 matt TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
927 1.59 itojun if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
928 1.59 itojun break;
929 1.62 thorpej }
930 1.59 itojun }
931 1.62 thorpej if (ia == NULL) {
932 1.59 itojun *errorp = EADDRNOTAVAIL;
933 1.59 itojun return NULL;
934 1.59 itojun }
935 1.59 itojun }
936 1.59 itojun /*
937 1.59 itojun * If the destination address is multicast and an outgoing
938 1.59 itojun * interface has been set as a multicast option, use the
939 1.59 itojun * address of that interface as our source address.
940 1.59 itojun */
941 1.59 itojun if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
942 1.59 itojun struct ip_moptions *imo;
943 1.59 itojun struct ifnet *ifp;
944 1.59 itojun
945 1.59 itojun imo = mopts;
946 1.59 itojun if (imo->imo_multicast_ifp != NULL) {
947 1.59 itojun ifp = imo->imo_multicast_ifp;
948 1.59 itojun IFP_TO_IA(ifp, ia); /* XXX */
949 1.59 itojun if (ia == 0) {
950 1.59 itojun *errorp = EADDRNOTAVAIL;
951 1.59 itojun return NULL;
952 1.59 itojun }
953 1.59 itojun }
954 1.59 itojun }
955 1.59 itojun return satosin(&ia->ia_addr);
956 1.1 cgd }
957