Once we distribute socket protocol processing requests to different
[dragonfly.git] / sys / netinet / in_pcb.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1991, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
73bc1178 34 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.27 2004/01/02 04:06:42 ambrisko Exp $
e4700d00 35 * $DragonFly: src/sys/netinet/in_pcb.c,v 1.12 2004/03/05 16:57:15 hsu Exp $
984263bc
MD
36 */
37
38#include "opt_ipsec.h"
39#include "opt_inet6.h"
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/domain.h>
46#include <sys/protosw.h>
47#include <sys/socket.h>
48#include <sys/socketvar.h>
49#include <sys/proc.h>
50#include <sys/jail.h>
51#include <sys/kernel.h>
52#include <sys/sysctl.h>
53
54#include <machine/limits.h>
55
56#include <vm/vm_zone.h>
57
58#include <net/if.h>
59#include <net/if_types.h>
60#include <net/route.h>
61
62#include <netinet/in.h>
63#include <netinet/in_pcb.h>
64#include <netinet/in_var.h>
65#include <netinet/ip_var.h>
66#ifdef INET6
67#include <netinet/ip6.h>
68#include <netinet6/ip6_var.h>
69#endif /* INET6 */
70
71#ifdef IPSEC
72#include <netinet6/ipsec.h>
d2438d69 73#include <netproto/key/key.h>
80dff869 74#endif
984263bc
MD
75
76#ifdef FAST_IPSEC
77#if defined(IPSEC) || defined(IPSEC_ESP)
78#error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
79#endif
80
81#include <netipsec/ipsec.h>
82#include <netipsec/key.h>
83#define IPSEC
84#endif /* FAST_IPSEC */
85
80dff869 86struct in_addr zeroin_addr;
984263bc
MD
87
88/*
89 * These configure the range of local port addresses assigned to
90 * "unspecified" outgoing connections/packets/whatever.
91 */
80dff869
JH
92int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
93int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
94
95int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
96int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
984263bc 97
80dff869
JH
98int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
99int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
100
101static __inline void
102RANGECHK(int var, int min, int max)
103{
104 if (var < min)
105 var = min;
106 else if (var > max)
107 var = max;
108}
984263bc
MD
109
110static int
111sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
112{
80dff869
JH
113 int error;
114
115 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
984263bc
MD
116 if (!error) {
117 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
118 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
13d8907a 119
984263bc
MD
120 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
121 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
13d8907a 122
984263bc
MD
123 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
124 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
125 }
80dff869 126 return (error);
984263bc
MD
127}
128
984263bc
MD
129SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
130
131SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
132 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
133SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
134 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
135SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
136 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
137SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
138 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
139SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
140 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
141SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
142 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
143
144/*
145 * in_pcb.c: manage the Protocol Control Blocks.
146 *
147 * NOTE: It is assumed that most of these functions will be called at
148 * splnet(). XXX - There are, unfortunately, a few exceptions to this
149 * rule that should be fixed.
150 */
151
152/*
153 * Allocate a PCB and associate it with the socket.
154 */
155int
e4700d00 156in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
984263bc 157{
dadab5e9 158 struct inpcb *inp;
984263bc
MD
159#ifdef IPSEC
160 int error;
161#endif
162
8a8d5d85 163 inp = zalloc(pcbinfo->ipi_zone);
984263bc
MD
164 if (inp == NULL)
165 return (ENOBUFS);
80dff869 166 bzero((caddr_t)inp, sizeof *inp);
984263bc
MD
167 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
168 inp->inp_pcbinfo = pcbinfo;
169 inp->inp_socket = so;
170#ifdef IPSEC
171 error = ipsec_init_policy(so, &inp->inp_sp);
172 if (error != 0) {
8a8d5d85 173 zfree(pcbinfo->ipi_zone, inp);
80dff869 174 return (error);
984263bc 175 }
80dff869
JH
176#endif
177#ifdef INET6
984263bc
MD
178 if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
179 inp->inp_flags |= IN6P_IPV6_V6ONLY;
80dff869
JH
180 if (ip6_auto_flowlabel)
181 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
984263bc 182#endif
13d8907a 183 so->so_pcb = (caddr_t)inp;
984263bc
MD
184 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
185 pcbinfo->ipi_count++;
984263bc
MD
186 return (0);
187}
188
189int
dadab5e9 190in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
984263bc 191{
dadab5e9
MD
192 struct socket *so = inp->inp_socket;
193 struct proc *p = td->td_proc;
984263bc
MD
194 unsigned short *lastport;
195 struct sockaddr_in *sin;
196 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
197 u_short lport = 0;
198 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
199 int error, prison = 0;
200
dadab5e9
MD
201 KKASSERT(p);
202
984263bc
MD
203 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
204 return (EADDRNOTAVAIL);
13d8907a
JH
205 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
206 return (EINVAL); /* already bound */
80dff869 207 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
13d8907a
JH
208 wild = 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
209 if (nam != NULL) {
984263bc 210 sin = (struct sockaddr_in *)nam;
80dff869 211 if (nam->sa_len != sizeof *sin)
984263bc
MD
212 return (EINVAL);
213#ifdef notdef
214 /*
215 * We should check the family, but old programs
216 * incorrectly fail to initialize it.
217 */
218 if (sin->sin_family != AF_INET)
219 return (EAFNOSUPPORT);
220#endif
13d8907a
JH
221 if (sin->sin_addr.s_addr != INADDR_ANY &&
222 prison_ip(td, 0, &sin->sin_addr.s_addr))
80dff869 223 return (EINVAL);
984263bc
MD
224 lport = sin->sin_port;
225 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
226 /*
227 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
228 * allow complete duplication of binding if
229 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
230 * and a multicast address is bound on both
231 * new and duplicated sockets.
232 */
233 if (so->so_options & SO_REUSEADDR)
13d8907a 234 reuseport = SO_REUSEADDR | SO_REUSEPORT;
984263bc
MD
235 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
236 sin->sin_port = 0; /* yech... */
80dff869
JH
237 bzero(&sin->sin_zero, sizeof sin->sin_zero);
238 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL)
984263bc
MD
239 return (EADDRNOTAVAIL);
240 }
13d8907a 241 if (lport != 0) {
984263bc
MD
242 struct inpcb *t;
243
244 /* GROSS */
13d8907a
JH
245 if (ntohs(lport) < IPPORT_RESERVED &&
246 p && suser_cred(p->p_ucred, PRISON_ROOT))
984263bc 247 return (EACCES);
41c20dac 248 if (p && p->p_ucred->cr_prison)
984263bc
MD
249 prison = 1;
250 if (so->so_cred->cr_uid != 0 &&
251 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
252 t = in_pcblookup_local(inp->inp_pcbinfo,
253 sin->sin_addr, lport,
80dff869 254 prison ? 0 : INPLOOKUP_WILDCARD);
984263bc 255 if (t &&
80dff869
JH
256 (!in_nullhost(sin->sin_addr) ||
257 !in_nullhost(t->inp_laddr) ||
984263bc
MD
258 (t->inp_socket->so_options &
259 SO_REUSEPORT) == 0) &&
260 (so->so_cred->cr_uid !=
261 t->inp_socket->so_cred->cr_uid)) {
80dff869
JH
262#ifdef INET6
263 if (!in_nullhost(sin->sin_addr) ||
264 !in_nullhost(t->inp_laddr) ||
984263bc
MD
265 INP_SOCKAF(so) ==
266 INP_SOCKAF(t->inp_socket))
80dff869 267#endif
984263bc
MD
268 return (EADDRINUSE);
269 }
270 }
80dff869 271 if (prison && prison_ip(td, 0, &sin->sin_addr.s_addr))
984263bc
MD
272 return (EADDRNOTAVAIL);
273 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
274 lport, prison ? 0 : wild);
80dff869
JH
275 if (t && !(reuseport & t->inp_socket->so_options)) {
276#ifdef INET6
277 if (!in_nullhost(sin->sin_addr) ||
278 !in_nullhost(t->inp_laddr) ||
279 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
280#endif
984263bc
MD
281 return (EADDRINUSE);
282 }
283 }
284 inp->inp_laddr = sin->sin_addr;
285 }
286 if (lport == 0) {
287 ushort first, last;
288 int count;
289
13d8907a
JH
290 if (inp->inp_laddr.s_addr != INADDR_ANY &&
291 prison_ip(td, 0, &inp->inp_laddr.s_addr )) {
292 inp->inp_laddr.s_addr = INADDR_ANY;
293 return (EINVAL);
294 }
984263bc
MD
295 inp->inp_flags |= INP_ANONPORT;
296
297 if (inp->inp_flags & INP_HIGHPORT) {
298 first = ipport_hifirstauto; /* sysctl */
299 last = ipport_hilastauto;
300 lastport = &pcbinfo->lasthi;
301 } else if (inp->inp_flags & INP_LOWPORT) {
80dff869
JH
302 if (p &&
303 (error = suser_cred(p->p_ucred, PRISON_ROOT))) {
984263bc 304 inp->inp_laddr.s_addr = INADDR_ANY;
80dff869 305 return (error);
984263bc
MD
306 }
307 first = ipport_lowfirstauto; /* 1023 */
308 last = ipport_lowlastauto; /* 600 */
309 lastport = &pcbinfo->lastlow;
310 } else {
311 first = ipport_firstauto; /* sysctl */
312 last = ipport_lastauto;
313 lastport = &pcbinfo->lastport;
314 }
315 /*
316 * Simple check to ensure all ports are not used up causing
317 * a deadlock here.
318 *
319 * We split the two cases (up and down) so that the direction
320 * is not being tested on each round of the loop.
321 */
322 if (first > last) {
323 /*
324 * counting down
325 */
326 count = first - last;
327
328 do {
329 if (count-- < 0) { /* completely used? */
330 inp->inp_laddr.s_addr = INADDR_ANY;
331 return (EADDRNOTAVAIL);
332 }
333 --*lastport;
334 if (*lastport > first || *lastport < last)
335 *lastport = first;
336 lport = htons(*lastport);
337 } while (in_pcblookup_local(pcbinfo,
338 inp->inp_laddr, lport, wild));
339 } else {
340 /*
341 * counting up
342 */
343 count = last - first;
344
345 do {
346 if (count-- < 0) { /* completely used? */
347 inp->inp_laddr.s_addr = INADDR_ANY;
348 return (EADDRNOTAVAIL);
349 }
350 ++*lastport;
351 if (*lastport < first || *lastport > last)
352 *lastport = first;
353 lport = htons(*lastport);
354 } while (in_pcblookup_local(pcbinfo,
355 inp->inp_laddr, lport, wild));
356 }
357 }
358 inp->inp_lport = lport;
dadab5e9 359 if (prison_ip(td, 0, &inp->inp_laddr.s_addr)) {
984263bc
MD
360 inp->inp_laddr.s_addr = INADDR_ANY;
361 inp->inp_lport = 0;
362 return (EINVAL);
363 }
13d8907a 364 if (in_pcbinsporthash(inp) != 0) {
984263bc
MD
365 inp->inp_laddr.s_addr = INADDR_ANY;
366 inp->inp_lport = 0;
367 return (EAGAIN);
368 }
13d8907a 369 in_pcbinsbindhash(inp);
984263bc
MD
370 return (0);
371}
372
373/*
374 * Transform old in_pcbconnect() into an inner subroutine for new
375 * in_pcbconnect(): Do some validity-checking on the remote
376 * address (in mbuf 'nam') and then determine local host address
377 * (i.e., which interface) to use to access that remote host.
378 *
379 * This preserves definition of in_pcbconnect(), while supporting a
380 * slightly different version for T/TCP. (This is more than
381 * a bit of a kludge, but cleaning up the internal interfaces would
382 * have forced minor changes in every protocol).
383 */
384
385int
386in_pcbladdr(inp, nam, plocal_sin)
2256ba69 387 struct inpcb *inp;
984263bc
MD
388 struct sockaddr *nam;
389 struct sockaddr_in **plocal_sin;
390{
391 struct in_ifaddr *ia;
2256ba69 392 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
984263bc 393
80dff869 394 if (nam->sa_len != sizeof *sin)
984263bc
MD
395 return (EINVAL);
396 if (sin->sin_family != AF_INET)
397 return (EAFNOSUPPORT);
398 if (sin->sin_port == 0)
399 return (EADDRNOTAVAIL);
400 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
80dff869 401 ia = TAILQ_FIRST(&in_ifaddrhead);
984263bc
MD
402 /*
403 * If the destination address is INADDR_ANY,
404 * use the primary local address.
405 * If the supplied address is INADDR_BROADCAST,
406 * and the primary interface supports broadcast,
407 * choose the broadcast address for that interface.
408 */
409 if (sin->sin_addr.s_addr == INADDR_ANY)
80dff869 410 sin->sin_addr = IA_SIN(ia)->sin_addr;
984263bc 411 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
80dff869
JH
412 (ia->ia_ifp->if_flags & IFF_BROADCAST))
413 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
984263bc
MD
414 }
415 if (inp->inp_laddr.s_addr == INADDR_ANY) {
2256ba69 416 struct route *ro;
984263bc 417
80dff869 418 ia = (struct in_ifaddr *)NULL;
984263bc
MD
419 /*
420 * If route is known or can be allocated now,
421 * our src addr is taken from the i/f, else punt.
422 * Note that we should check the address family of the cached
423 * destination, in case of sharing the cache with IPv6.
424 */
425 ro = &inp->inp_route;
80dff869
JH
426 if (ro->ro_rt &&
427 (!(ro->ro_rt->rt_flags & RTF_UP) ||
428 ro->ro_dst.sa_family != AF_INET ||
429 satosin(&ro->ro_dst)->sin_addr.s_addr !=
430 sin->sin_addr.s_addr ||
431 inp->inp_socket->so_options & SO_DONTROUTE)) {
984263bc 432 RTFREE(ro->ro_rt);
80dff869 433 ro->ro_rt = (struct rtentry *)NULL;
984263bc 434 }
80dff869
JH
435 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
436 (ro->ro_rt == (struct rtentry *)NULL ||
437 ro->ro_rt->rt_ifp == (struct ifnet *)NULL)) {
984263bc
MD
438 /* No route yet, so try to acquire one */
439 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
440 ro->ro_dst.sa_family = AF_INET;
441 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
442 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
443 sin->sin_addr;
444 rtalloc(ro);
445 }
446 /*
447 * If we found a route, use the address
448 * corresponding to the outgoing interface
449 * unless it is the loopback (in case a route
450 * to our address on another net goes to loopback).
451 */
452 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
453 ia = ifatoia(ro->ro_rt->rt_ifa);
80dff869 454 if (ia == NULL) {
984263bc
MD
455 u_short fport = sin->sin_port;
456
457 sin->sin_port = 0;
458 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
80dff869 459 if (ia == NULL)
984263bc
MD
460 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
461 sin->sin_port = fport;
80dff869 462 if (ia == NULL)
984263bc 463 ia = TAILQ_FIRST(&in_ifaddrhead);
80dff869 464 if (ia == NULL)
984263bc
MD
465 return (EADDRNOTAVAIL);
466 }
467 /*
468 * If the destination address is multicast and an outgoing
469 * interface has been set as a multicast option, use the
470 * address of that interface as our source address.
471 */
472 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
473 inp->inp_moptions != NULL) {
474 struct ip_moptions *imo;
475 struct ifnet *ifp;
476
477 imo = inp->inp_moptions;
478 if (imo->imo_multicast_ifp != NULL) {
479 ifp = imo->imo_multicast_ifp;
480 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
481 if (ia->ia_ifp == ifp)
482 break;
80dff869 483 if (ia == NULL)
984263bc
MD
484 return (EADDRNOTAVAIL);
485 }
486 }
80dff869
JH
487 /*
488 * Don't do pcblookup call here; return interface in plocal_sin
489 * and exit to caller, that will do the lookup.
490 */
984263bc
MD
491 *plocal_sin = &ia->ia_addr;
492
493 }
80dff869 494 return (0);
984263bc
MD
495}
496
497/*
498 * Outer subroutine:
499 * Connect from a socket to a specified address.
500 * Both address and port must be specified in argument sin.
501 * If don't have a local address for this socket yet,
502 * then pick one.
503 */
504int
dadab5e9 505in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
984263bc 506{
13d8907a 507 struct sockaddr_in *if_sin;
984263bc
MD
508 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
509 struct sockaddr_in sa;
dadab5e9 510 struct ucred *cr = td->td_proc ? td->td_proc->p_ucred : NULL;
984263bc
MD
511 int error;
512
13d8907a 513 if (cr && cr->cr_prison != NULL && in_nullhost(inp->inp_laddr)) {
80dff869 514 bzero(&sa, sizeof sa);
dadab5e9 515 sa.sin_addr.s_addr = htonl(cr->cr_prison->pr_ip);
80dff869 516 sa.sin_len = sizeof sa;
984263bc 517 sa.sin_family = AF_INET;
dadab5e9 518 error = in_pcbbind(inp, (struct sockaddr *)&sa, td);
984263bc
MD
519 if (error)
520 return (error);
521 }
80dff869
JH
522
523 /* Call inner routine to assign local interface address. */
13d8907a 524 if ((error = in_pcbladdr(inp, nam, &if_sin)) != 0)
80dff869 525 return (error);
984263bc
MD
526
527 if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
13d8907a 528 inp->inp_laddr.s_addr ? inp->inp_laddr : if_sin->sin_addr,
80dff869 529 inp->inp_lport, FALSE, NULL) != NULL) {
984263bc
MD
530 return (EADDRINUSE);
531 }
532 if (inp->inp_laddr.s_addr == INADDR_ANY) {
533 if (inp->inp_lport == 0) {
13d8907a 534 error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
984263bc 535 if (error)
80dff869 536 return (error);
984263bc 537 }
13d8907a 538 inp->inp_laddr = if_sin->sin_addr;
984263bc
MD
539 }
540 inp->inp_faddr = sin->sin_addr;
541 inp->inp_fport = sin->sin_port;
13d8907a 542 in_pcbrehash(inp, INP_CONNECTED);
984263bc
MD
543 return (0);
544}
545
546void
547in_pcbdisconnect(inp)
548 struct inpcb *inp;
549{
550
551 inp->inp_faddr.s_addr = INADDR_ANY;
552 inp->inp_fport = 0;
13d8907a 553 in_pcbremconnhash(inp);
984263bc
MD
554 if (inp->inp_socket->so_state & SS_NOFDREF)
555 in_pcbdetach(inp);
556}
557
558void
559in_pcbdetach(inp)
560 struct inpcb *inp;
561{
562 struct socket *so = inp->inp_socket;
563 struct inpcbinfo *ipi = inp->inp_pcbinfo;
564
565#ifdef IPSEC
566 ipsec4_delete_pcbpolicy(inp);
567#endif /*IPSEC*/
568 inp->inp_gencnt = ++ipi->ipi_gencnt;
569 in_pcbremlists(inp);
570 so->so_pcb = 0;
571 sofree(so);
572 if (inp->inp_options)
573 (void)m_free(inp->inp_options);
574 if (inp->inp_route.ro_rt)
575 rtfree(inp->inp_route.ro_rt);
576 ip_freemoptions(inp->inp_moptions);
577 inp->inp_vflag = 0;
8a8d5d85 578 zfree(ipi->ipi_zone, inp);
984263bc
MD
579}
580
581/*
582 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
583 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
584 * in struct pr_usrreqs, so that protocols can just reference then directly
585 * without the need for a wrapper function. The socket must have a valid
586 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
587 * except through a kernel programming error, so it is acceptable to panic
588 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
589 * because there actually /is/ a programming error somewhere... XXX)
590 */
591int
592in_setsockaddr(so, nam)
593 struct socket *so;
594 struct sockaddr **nam;
595{
596 int s;
2256ba69
RG
597 struct inpcb *inp;
598 struct sockaddr_in *sin;
984263bc
MD
599
600 /*
601 * Do the malloc first in case it blocks.
602 */
603 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
604 M_WAITOK | M_ZERO);
605 sin->sin_family = AF_INET;
80dff869 606 sin->sin_len = sizeof *sin;
984263bc
MD
607
608 s = splnet();
609 inp = sotoinpcb(so);
610 if (!inp) {
611 splx(s);
612 free(sin, M_SONAME);
80dff869 613 return (ECONNRESET);
984263bc
MD
614 }
615 sin->sin_port = inp->inp_lport;
616 sin->sin_addr = inp->inp_laddr;
617 splx(s);
618
619 *nam = (struct sockaddr *)sin;
80dff869 620 return (0);
984263bc
MD
621}
622
623int
624in_setpeeraddr(so, nam)
625 struct socket *so;
626 struct sockaddr **nam;
627{
628 int s;
629 struct inpcb *inp;
2256ba69 630 struct sockaddr_in *sin;
984263bc
MD
631
632 /*
633 * Do the malloc first in case it blocks.
634 */
635 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
636 M_WAITOK | M_ZERO);
637 sin->sin_family = AF_INET;
80dff869 638 sin->sin_len = sizeof *sin;
984263bc
MD
639
640 s = splnet();
641 inp = sotoinpcb(so);
642 if (!inp) {
643 splx(s);
644 free(sin, M_SONAME);
80dff869 645 return (ECONNRESET);
984263bc
MD
646 }
647 sin->sin_port = inp->inp_fport;
648 sin->sin_addr = inp->inp_faddr;
649 splx(s);
650
651 *nam = (struct sockaddr *)sin;
80dff869 652 return (0);
984263bc
MD
653}
654
655void
656in_pcbnotifyall(head, faddr, errno, notify)
657 struct inpcbhead *head;
658 struct in_addr faddr;
42a7fc75 659 void (*notify) (struct inpcb *, int);
984263bc
MD
660{
661 struct inpcb *inp, *ninp;
662 int s;
663
664 s = splnet();
665 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
666 ninp = LIST_NEXT(inp, inp_list);
667#ifdef INET6
80dff869 668 if (!(inp->inp_vflag & INP_IPV4))
984263bc
MD
669 continue;
670#endif
671 if (inp->inp_faddr.s_addr != faddr.s_addr ||
672 inp->inp_socket == NULL)
80dff869 673 continue;
984263bc
MD
674 (*notify)(inp, errno);
675 }
676 splx(s);
677}
678
679void
680in_pcbpurgeif0(head, ifp)
681 struct inpcb *head;
682 struct ifnet *ifp;
683{
684 struct inpcb *inp;
685 struct ip_moptions *imo;
686 int i, gap;
687
688 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
689 imo = inp->inp_moptions;
80dff869 690 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
984263bc
MD
691 /*
692 * Unselect the outgoing interface if it is being
693 * detached.
694 */
695 if (imo->imo_multicast_ifp == ifp)
696 imo->imo_multicast_ifp = NULL;
697
698 /*
699 * Drop multicast group membership if we joined
700 * through the interface being detached.
701 */
702 for (i = 0, gap = 0; i < imo->imo_num_memberships;
703 i++) {
704 if (imo->imo_membership[i]->inm_ifp == ifp) {
705 in_delmulti(imo->imo_membership[i]);
706 gap++;
707 } else if (gap != 0)
708 imo->imo_membership[i - gap] =
709 imo->imo_membership[i];
710 }
711 imo->imo_num_memberships -= gap;
712 }
713 }
714}
715
716/*
717 * Check for alternatives when higher level complains
718 * about service problems. For now, invalidate cached
719 * routing information. If the route was created dynamically
720 * (by a redirect), time to try a default gateway again.
721 */
722void
723in_losing(inp)
724 struct inpcb *inp;
725{
2256ba69 726 struct rtentry *rt;
984263bc
MD
727 struct rt_addrinfo info;
728
729 if ((rt = inp->inp_route.ro_rt)) {
80dff869 730 bzero((caddr_t)&info, sizeof info);
984263bc
MD
731 info.rti_flags = rt->rt_flags;
732 info.rti_info[RTAX_DST] = rt_key(rt);
733 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
734 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
735 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
736 if (rt->rt_flags & RTF_DYNAMIC)
737 (void) rtrequest1(RTM_DELETE, &info, NULL);
738 inp->inp_route.ro_rt = NULL;
739 rtfree(rt);
740 /*
741 * A new route can be allocated
742 * the next time output is attempted.
743 */
744 }
745}
746
747/*
748 * After a routing change, flush old routing
749 * and allocate a (hopefully) better one.
750 */
751void
752in_rtchange(inp, errno)
2256ba69 753 struct inpcb *inp;
984263bc
MD
754 int errno;
755{
756 if (inp->inp_route.ro_rt) {
757 rtfree(inp->inp_route.ro_rt);
758 inp->inp_route.ro_rt = 0;
759 /*
760 * A new route can be allocated the next time
761 * output is attempted.
762 */
763 }
764}
765
766/*
767 * Lookup a PCB based on the local address and port.
768 */
769struct inpcb *
770in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
771 struct inpcbinfo *pcbinfo;
772 struct in_addr laddr;
773 u_int lport_arg;
774 int wild_okay;
775{
2256ba69 776 struct inpcb *inp;
984263bc
MD
777 int matchwild = 3, wildcard;
778 u_short lport = lport_arg;
779
13d8907a
JH
780 struct inpcbporthead *porthash;
781 struct inpcbport *phd;
782 struct inpcb *match = NULL;
783
784 /*
785 * Best fit PCB lookup.
786 *
787 * First see if this local port is in use by looking on the
788 * port hash list.
789 */
790 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
791 pcbinfo->porthashmask)];
792 LIST_FOREACH(phd, porthash, phd_hash) {
793 if (phd->phd_port == lport)
794 break;
795 }
796 if (phd != NULL) {
984263bc 797 /*
13d8907a
JH
798 * Port is in use by one or more PCBs. Look for best
799 * fit.
984263bc 800 */
13d8907a
JH
801 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
802 wildcard = 0;
984263bc
MD
803#ifdef INET6
804 if ((inp->inp_vflag & INP_IPV4) == 0)
805 continue;
806#endif
13d8907a
JH
807 if (inp->inp_faddr.s_addr != INADDR_ANY)
808 wildcard++;
809 if (inp->inp_laddr.s_addr != INADDR_ANY) {
810 if (laddr.s_addr == INADDR_ANY)
811 wildcard++;
812 else if (inp->inp_laddr.s_addr != laddr.s_addr)
984263bc 813 continue;
13d8907a
JH
814 } else {
815 if (laddr.s_addr != INADDR_ANY)
984263bc 816 wildcard++;
13d8907a
JH
817 }
818 if (wildcard && !wild_okay)
819 continue;
820 if (wildcard < matchwild) {
821 match = inp;
822 matchwild = wildcard;
823 if (matchwild == 0) {
824 break;
984263bc
MD
825 }
826 }
827 }
984263bc 828 }
13d8907a 829 return (match);
984263bc
MD
830}
831
832/*
833 * Lookup PCB in hash list.
834 */
835struct inpcb *
80dff869 836in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
984263bc
MD
837 struct inpcbinfo *pcbinfo;
838 struct in_addr faddr, laddr;
839 u_int fport_arg, lport_arg;
80dff869 840 boolean_t wildcard;
984263bc
MD
841 struct ifnet *ifp;
842{
843 struct inpcbhead *head;
2256ba69 844 struct inpcb *inp;
984263bc
MD
845 u_short fport = fport_arg, lport = lport_arg;
846
847 /*
848 * First look for an exact match.
849 */
13d8907a
JH
850 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
851 laddr.s_addr, lport, pcbinfo->hashmask)];
984263bc
MD
852 LIST_FOREACH(inp, head, inp_hash) {
853#ifdef INET6
80dff869 854 if (!(inp->inp_vflag & INP_IPV4))
984263bc
MD
855 continue;
856#endif
80dff869
JH
857 if (in_hosteq(inp->inp_faddr, faddr) &&
858 in_hosteq(inp->inp_laddr, laddr) &&
859 inp->inp_fport == fport && inp->inp_lport == lport) {
860 /* found */
984263bc
MD
861 return (inp);
862 }
863 }
80dff869 864
984263bc
MD
865 if (wildcard) {
866 struct inpcb *local_wild = NULL;
80dff869 867#ifdef INET6
984263bc 868 struct inpcb *local_wild_mapped = NULL;
80dff869 869#endif
984263bc 870
13d8907a
JH
871 head = &pcbinfo->bindhashbase[INP_PCBBINDHASH(lport,
872 pcbinfo->bindhashmask)];
984263bc
MD
873 LIST_FOREACH(inp, head, inp_hash) {
874#ifdef INET6
80dff869 875 if (!(inp->inp_vflag & INP_IPV4))
984263bc
MD
876 continue;
877#endif
13d8907a 878 if (inp->inp_lport == lport) {
984263bc 879 if (ifp && ifp->if_type == IFT_FAITH &&
80dff869 880 !(inp->inp_flags & INP_FAITH))
984263bc
MD
881 continue;
882 if (inp->inp_laddr.s_addr == laddr.s_addr)
883 return (inp);
13d8907a 884 if (inp->inp_laddr.s_addr == INADDR_ANY) {
80dff869 885#ifdef INET6
984263bc
MD
886 if (INP_CHECK_SOCKAF(inp->inp_socket,
887 AF_INET6))
888 local_wild_mapped = inp;
889 else
80dff869
JH
890#endif
891 local_wild = inp;
984263bc
MD
892 }
893 }
894 }
80dff869 895#ifdef INET6
984263bc
MD
896 if (local_wild == NULL)
897 return (local_wild_mapped);
80dff869 898#endif
984263bc
MD
899 return (local_wild);
900 }
901
902 /*
903 * Not found.
904 */
905 return (NULL);
906}
907
908/*
13d8907a 909 * Insert PCB into connection hash table.
984263bc 910 */
13d8907a
JH
911void
912in_pcbinsconnhash(struct inpcb *inp)
984263bc 913{
984263bc 914 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
13d8907a
JH
915 struct inpcbhead *bucket;
916 u_int32_t hashkey_faddr, hashkey_laddr;
984263bc
MD
917
918#ifdef INET6
13d8907a
JH
919 if (inp->inp_vflag & INP_IPV6) {
920 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
921 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
922 } else {
80dff869
JH
923#endif
924 hashkey_faddr = inp->inp_faddr.s_addr;
13d8907a
JH
925 hashkey_laddr = inp->inp_laddr.s_addr;
926#ifdef INET6
927 }
928#endif
984263bc 929
13d8907a
JH
930 inp->inp_flags |= INP_CONNECTED;
931
932 /*
933 * Insert into the connection hash table.
934 */
935 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
936 inp->inp_fport, hashkey_laddr,
937 inp->inp_lport, pcbinfo->hashmask)];
938 LIST_INSERT_HEAD(bucket, inp, inp_hash);
939}
940
941/*
942 * Remove PCB from connection hash table.
943 */
944void
945in_pcbremconnhash(struct inpcb *inp)
946{
947 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
948 LIST_REMOVE(inp, inp_hash);
949 inp->inp_flags &= ~INP_CONNECTED;
950}
951
952/*
953 * Insert PCB into port hash table.
954 */
955int
956in_pcbinsporthash(struct inpcb *inp)
957{
958 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
959 struct inpcbporthead *pcbporthash;
960 struct inpcbport *phd;
961
962 /*
963 * Insert into the port hash table.
964 */
965 pcbporthash = &pcbinfo->porthashbase[
966 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->porthashmask)];
984263bc 967
80dff869
JH
968 /* Go through port list and look for a head for this lport. */
969 LIST_FOREACH(phd, pcbporthash, phd_hash)
984263bc
MD
970 if (phd->phd_port == inp->inp_lport)
971 break;
80dff869
JH
972
973 /* If none exists, malloc one and tack it on. */
984263bc 974 if (phd == NULL) {
80dff869
JH
975 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport),
976 M_PCB, M_NOWAIT);
977 if (phd == NULL)
984263bc 978 return (ENOBUFS); /* XXX */
984263bc
MD
979 phd->phd_port = inp->inp_lport;
980 LIST_INIT(&phd->phd_pcblist);
981 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
982 }
80dff869 983
984263bc
MD
984 inp->inp_phd = phd;
985 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
13d8907a 986
984263bc
MD
987 return (0);
988}
989
990/*
13d8907a 991 * Insert PCB into bind hash table.
984263bc
MD
992 */
993void
13d8907a 994in_pcbinsbindhash(struct inpcb *inp)
984263bc 995{
13d8907a 996 struct inpcbhead *bucket;
984263bc 997
13d8907a
JH
998 bucket = &inp->inp_pcbinfo->bindhashbase[INP_PCBBINDHASH(inp->inp_lport,
999 inp->inp_pcbinfo->porthashmask)];
984263bc 1000
13d8907a
JH
1001 inp->inp_flags |= INP_BOUND;
1002 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1003}
984263bc 1004
13d8907a
JH
1005/*
1006 * Remove PCB from bind hash table.
1007 */
1008void
1009in_pcbrembindhash(struct inpcb *inp)
1010{
1011 KASSERT(inp->inp_flags & INP_BOUND, ("inp not bound"));
984263bc 1012 LIST_REMOVE(inp, inp_hash);
13d8907a
JH
1013 inp->inp_flags &= ~INP_BOUND;
1014}
1015
1016static void
1017in_pcbremhash(struct inpcb *inp)
1018{
1019 if (inp->inp_flags & (INP_BOUND | INP_CONNECTED)) {
1020 LIST_REMOVE(inp, inp_hash);
1021 inp->inp_flags &= ~(INP_BOUND | INP_CONNECTED);
1022 }
1023}
1024
1025void
1026in_pcbrehash(struct inpcb *inp, int state)
1027{
1028 in_pcbremhash(inp);
1029 if (state == INP_BOUND)
1030 in_pcbinsbindhash(inp);
1031 else
1032 in_pcbinsconnhash(inp);
984263bc
MD
1033}
1034
1035/*
1036 * Remove PCB from various lists.
1037 */
1038void
1039in_pcbremlists(inp)
1040 struct inpcb *inp;
1041{
1042 inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt;
1043 if (inp->inp_lport) {
1044 struct inpcbport *phd = inp->inp_phd;
1045
984263bc
MD
1046 LIST_REMOVE(inp, inp_portlist);
1047 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1048 LIST_REMOVE(phd, phd_hash);
1049 free(phd, M_PCB);
1050 }
1051 }
13d8907a 1052 in_pcbremhash(inp);
984263bc
MD
1053 LIST_REMOVE(inp, inp_list);
1054 inp->inp_pcbinfo->ipi_count--;
1055}
1056
1057int
dadab5e9 1058prison_xinpcb(struct thread *td, struct inpcb *inp)
984263bc 1059{
dadab5e9
MD
1060 struct ucred *cr;
1061
1062 if (td->td_proc == NULL)
1063 return (0);
1064 cr = td->td_proc->p_ucred;
1065 if (cr->cr_prison == NULL)
984263bc 1066 return (0);
dadab5e9 1067 if (ntohl(inp->inp_laddr.s_addr) == cr->cr_prison->pr_ip)
984263bc
MD
1068 return (0);
1069 return (1);
1070}