proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[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
34 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.26 2003/01/24 05:11:33 sam Exp $
dadab5e9 35 * $DragonFly: src/sys/netinet/in_pcb.c,v 1.4 2003/06/25 03:56:04 dillon 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>
73#include <netkey/key.h>
74#endif /* IPSEC */
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
86struct in_addr zeroin_addr;
87
88/*
89 * These configure the range of local port addresses assigned to
90 * "unspecified" outgoing connections/packets/whatever.
91 */
92int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
93int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
94int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
95int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
96int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
97int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
98
99#define RANGECHK(var, min, max) \
100 if ((var) < (min)) { (var) = (min); } \
101 else if ((var) > (max)) { (var) = (max); }
102
103static int
104sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
105{
106 int error = sysctl_handle_int(oidp,
107 oidp->oid_arg1, oidp->oid_arg2, req);
108 if (!error) {
109 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
110 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
111 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
112 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
113 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
114 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
115 }
116 return error;
117}
118
119#undef RANGECHK
120
121SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
122
123SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
124 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
125SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
126 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
127SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
128 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
129SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
130 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
131SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
132 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
133SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
134 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
135
136/*
137 * in_pcb.c: manage the Protocol Control Blocks.
138 *
139 * NOTE: It is assumed that most of these functions will be called at
140 * splnet(). XXX - There are, unfortunately, a few exceptions to this
141 * rule that should be fixed.
142 */
143
144/*
145 * Allocate a PCB and associate it with the socket.
146 */
147int
dadab5e9 148in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo, struct thread *td)
984263bc 149{
dadab5e9 150 struct inpcb *inp;
984263bc
MD
151#ifdef IPSEC
152 int error;
153#endif
154
155 inp = zalloci(pcbinfo->ipi_zone);
156 if (inp == NULL)
157 return (ENOBUFS);
158 bzero((caddr_t)inp, sizeof(*inp));
159 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
160 inp->inp_pcbinfo = pcbinfo;
161 inp->inp_socket = so;
162#ifdef IPSEC
163 error = ipsec_init_policy(so, &inp->inp_sp);
164 if (error != 0) {
165 zfreei(pcbinfo->ipi_zone, inp);
166 return error;
167 }
168#endif /*IPSEC*/
169#if defined(INET6)
170 if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
171 inp->inp_flags |= IN6P_IPV6_V6ONLY;
172#endif
173 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
174 pcbinfo->ipi_count++;
175 so->so_pcb = (caddr_t)inp;
176#ifdef INET6
177 if (ip6_auto_flowlabel)
178 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
179#endif
180 return (0);
181}
182
183int
dadab5e9 184in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
984263bc 185{
dadab5e9
MD
186 struct socket *so = inp->inp_socket;
187 struct proc *p = td->td_proc;
984263bc
MD
188 unsigned short *lastport;
189 struct sockaddr_in *sin;
190 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
191 u_short lport = 0;
192 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
193 int error, prison = 0;
194
dadab5e9
MD
195 KKASSERT(p);
196
984263bc
MD
197 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
198 return (EADDRNOTAVAIL);
199 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
200 return (EINVAL);
201 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
202 wild = 1;
203 if (nam) {
204 sin = (struct sockaddr_in *)nam;
205 if (nam->sa_len != sizeof (*sin))
206 return (EINVAL);
207#ifdef notdef
208 /*
209 * We should check the family, but old programs
210 * incorrectly fail to initialize it.
211 */
212 if (sin->sin_family != AF_INET)
213 return (EAFNOSUPPORT);
214#endif
215 if (sin->sin_addr.s_addr != INADDR_ANY)
dadab5e9 216 if (prison_ip(td, 0, &sin->sin_addr.s_addr))
984263bc
MD
217 return(EINVAL);
218 lport = sin->sin_port;
219 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
220 /*
221 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
222 * allow complete duplication of binding if
223 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
224 * and a multicast address is bound on both
225 * new and duplicated sockets.
226 */
227 if (so->so_options & SO_REUSEADDR)
228 reuseport = SO_REUSEADDR|SO_REUSEPORT;
229 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
230 sin->sin_port = 0; /* yech... */
231 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
232 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
233 return (EADDRNOTAVAIL);
234 }
235 if (lport) {
236 struct inpcb *t;
237
238 /* GROSS */
239 if (ntohs(lport) < IPPORT_RESERVED && p &&
dadab5e9 240 suser_cred(p->p_ucred, PRISON_ROOT))
984263bc 241 return (EACCES);
41c20dac 242 if (p && p->p_ucred->cr_prison)
984263bc
MD
243 prison = 1;
244 if (so->so_cred->cr_uid != 0 &&
245 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
246 t = in_pcblookup_local(inp->inp_pcbinfo,
247 sin->sin_addr, lport,
248 prison ? 0 : INPLOOKUP_WILDCARD);
249 if (t &&
250 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
251 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
252 (t->inp_socket->so_options &
253 SO_REUSEPORT) == 0) &&
254 (so->so_cred->cr_uid !=
255 t->inp_socket->so_cred->cr_uid)) {
256#if defined(INET6)
257 if (ntohl(sin->sin_addr.s_addr) !=
258 INADDR_ANY ||
259 ntohl(t->inp_laddr.s_addr) !=
260 INADDR_ANY ||
261 INP_SOCKAF(so) ==
262 INP_SOCKAF(t->inp_socket))
263#endif /* defined(INET6) */
264 return (EADDRINUSE);
265 }
266 }
267 if (prison &&
dadab5e9 268 prison_ip(td, 0, &sin->sin_addr.s_addr))
984263bc
MD
269 return (EADDRNOTAVAIL);
270 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
271 lport, prison ? 0 : wild);
272 if (t &&
273 (reuseport & t->inp_socket->so_options) == 0) {
274#if defined(INET6)
275 if (ntohl(sin->sin_addr.s_addr) !=
276 INADDR_ANY ||
277 ntohl(t->inp_laddr.s_addr) !=
278 INADDR_ANY ||
279 INP_SOCKAF(so) ==
280 INP_SOCKAF(t->inp_socket))
281#endif /* defined(INET6) */
282 return (EADDRINUSE);
283 }
284 }
285 inp->inp_laddr = sin->sin_addr;
286 }
287 if (lport == 0) {
288 ushort first, last;
289 int count;
290
291 if (inp->inp_laddr.s_addr != INADDR_ANY)
dadab5e9 292 if (prison_ip(td, 0, &inp->inp_laddr.s_addr )) {
984263bc
MD
293 inp->inp_laddr.s_addr = INADDR_ANY;
294 return (EINVAL);
295 }
296 inp->inp_flags |= INP_ANONPORT;
297
298 if (inp->inp_flags & INP_HIGHPORT) {
299 first = ipport_hifirstauto; /* sysctl */
300 last = ipport_hilastauto;
301 lastport = &pcbinfo->lasthi;
302 } else if (inp->inp_flags & INP_LOWPORT) {
dadab5e9 303 if (p && (error = suser_cred(p->p_ucred, PRISON_ROOT))) {
984263bc
MD
304 inp->inp_laddr.s_addr = INADDR_ANY;
305 return error;
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 }
364 if (in_pcbinshash(inp) != 0) {
365 inp->inp_laddr.s_addr = INADDR_ANY;
366 inp->inp_lport = 0;
367 return (EAGAIN);
368 }
369 return (0);
370}
371
372/*
373 * Transform old in_pcbconnect() into an inner subroutine for new
374 * in_pcbconnect(): Do some validity-checking on the remote
375 * address (in mbuf 'nam') and then determine local host address
376 * (i.e., which interface) to use to access that remote host.
377 *
378 * This preserves definition of in_pcbconnect(), while supporting a
379 * slightly different version for T/TCP. (This is more than
380 * a bit of a kludge, but cleaning up the internal interfaces would
381 * have forced minor changes in every protocol).
382 */
383
384int
385in_pcbladdr(inp, nam, plocal_sin)
386 register struct inpcb *inp;
387 struct sockaddr *nam;
388 struct sockaddr_in **plocal_sin;
389{
390 struct in_ifaddr *ia;
391 register struct sockaddr_in *sin = (struct sockaddr_in *)nam;
392
393 if (nam->sa_len != sizeof (*sin))
394 return (EINVAL);
395 if (sin->sin_family != AF_INET)
396 return (EAFNOSUPPORT);
397 if (sin->sin_port == 0)
398 return (EADDRNOTAVAIL);
399 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
400 /*
401 * If the destination address is INADDR_ANY,
402 * use the primary local address.
403 * If the supplied address is INADDR_BROADCAST,
404 * and the primary interface supports broadcast,
405 * choose the broadcast address for that interface.
406 */
407 if (sin->sin_addr.s_addr == INADDR_ANY)
408 sin->sin_addr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
409 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
410 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & IFF_BROADCAST))
411 sin->sin_addr = satosin(&TAILQ_FIRST(&in_ifaddrhead)->ia_broadaddr)->sin_addr;
412 }
413 if (inp->inp_laddr.s_addr == INADDR_ANY) {
414 register struct route *ro;
415
416 ia = (struct in_ifaddr *)0;
417 /*
418 * If route is known or can be allocated now,
419 * our src addr is taken from the i/f, else punt.
420 * Note that we should check the address family of the cached
421 * destination, in case of sharing the cache with IPv6.
422 */
423 ro = &inp->inp_route;
424 if (ro->ro_rt &&
425 (ro->ro_dst.sa_family != AF_INET ||
426 satosin(&ro->ro_dst)->sin_addr.s_addr !=
427 sin->sin_addr.s_addr ||
428 inp->inp_socket->so_options & SO_DONTROUTE)) {
429 RTFREE(ro->ro_rt);
430 ro->ro_rt = (struct rtentry *)0;
431 }
432 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
433 (ro->ro_rt == (struct rtentry *)0 ||
434 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
435 /* No route yet, so try to acquire one */
436 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
437 ro->ro_dst.sa_family = AF_INET;
438 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
439 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
440 sin->sin_addr;
441 rtalloc(ro);
442 }
443 /*
444 * If we found a route, use the address
445 * corresponding to the outgoing interface
446 * unless it is the loopback (in case a route
447 * to our address on another net goes to loopback).
448 */
449 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
450 ia = ifatoia(ro->ro_rt->rt_ifa);
451 if (ia == 0) {
452 u_short fport = sin->sin_port;
453
454 sin->sin_port = 0;
455 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
456 if (ia == 0)
457 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
458 sin->sin_port = fport;
459 if (ia == 0)
460 ia = TAILQ_FIRST(&in_ifaddrhead);
461 if (ia == 0)
462 return (EADDRNOTAVAIL);
463 }
464 /*
465 * If the destination address is multicast and an outgoing
466 * interface has been set as a multicast option, use the
467 * address of that interface as our source address.
468 */
469 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
470 inp->inp_moptions != NULL) {
471 struct ip_moptions *imo;
472 struct ifnet *ifp;
473
474 imo = inp->inp_moptions;
475 if (imo->imo_multicast_ifp != NULL) {
476 ifp = imo->imo_multicast_ifp;
477 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
478 if (ia->ia_ifp == ifp)
479 break;
480 if (ia == 0)
481 return (EADDRNOTAVAIL);
482 }
483 }
484 /*
485 * Don't do pcblookup call here; return interface in plocal_sin
486 * and exit to caller, that will do the lookup.
487 */
488 *plocal_sin = &ia->ia_addr;
489
490 }
491 return(0);
492}
493
494/*
495 * Outer subroutine:
496 * Connect from a socket to a specified address.
497 * Both address and port must be specified in argument sin.
498 * If don't have a local address for this socket yet,
499 * then pick one.
500 */
501int
dadab5e9 502in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
984263bc
MD
503{
504 struct sockaddr_in *ifaddr;
505 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
506 struct sockaddr_in sa;
dadab5e9 507 struct ucred *cr = td->td_proc ? td->td_proc->p_ucred : NULL;
984263bc
MD
508 int error;
509
dadab5e9 510 if (inp->inp_laddr.s_addr == INADDR_ANY && cr && cr->cr_prison != NULL) {
984263bc 511 bzero(&sa, sizeof (sa));
dadab5e9 512 sa.sin_addr.s_addr = htonl(cr->cr_prison->pr_ip);
984263bc
MD
513 sa.sin_len=sizeof (sa);
514 sa.sin_family = AF_INET;
dadab5e9 515 error = in_pcbbind(inp, (struct sockaddr *)&sa, td);
984263bc
MD
516 if (error)
517 return (error);
518 }
519 /*
520 * Call inner routine, to assign local interface address.
521 */
522 if ((error = in_pcbladdr(inp, nam, &ifaddr)) != 0)
523 return(error);
524
525 if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
526 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
527 inp->inp_lport, 0, NULL) != NULL) {
528 return (EADDRINUSE);
529 }
530 if (inp->inp_laddr.s_addr == INADDR_ANY) {
531 if (inp->inp_lport == 0) {
dadab5e9 532 error = in_pcbbind(inp, (struct sockaddr *)0, td);
984263bc
MD
533 if (error)
534 return (error);
535 }
536 inp->inp_laddr = ifaddr->sin_addr;
537 }
538 inp->inp_faddr = sin->sin_addr;
539 inp->inp_fport = sin->sin_port;
540 in_pcbrehash(inp);
541 return (0);
542}
543
544void
545in_pcbdisconnect(inp)
546 struct inpcb *inp;
547{
548
549 inp->inp_faddr.s_addr = INADDR_ANY;
550 inp->inp_fport = 0;
551 in_pcbrehash(inp);
552 if (inp->inp_socket->so_state & SS_NOFDREF)
553 in_pcbdetach(inp);
554}
555
556void
557in_pcbdetach(inp)
558 struct inpcb *inp;
559{
560 struct socket *so = inp->inp_socket;
561 struct inpcbinfo *ipi = inp->inp_pcbinfo;
562
563#ifdef IPSEC
564 ipsec4_delete_pcbpolicy(inp);
565#endif /*IPSEC*/
566 inp->inp_gencnt = ++ipi->ipi_gencnt;
567 in_pcbremlists(inp);
568 so->so_pcb = 0;
569 sofree(so);
570 if (inp->inp_options)
571 (void)m_free(inp->inp_options);
572 if (inp->inp_route.ro_rt)
573 rtfree(inp->inp_route.ro_rt);
574 ip_freemoptions(inp->inp_moptions);
575 inp->inp_vflag = 0;
576 zfreei(ipi->ipi_zone, inp);
577}
578
579/*
580 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
581 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
582 * in struct pr_usrreqs, so that protocols can just reference then directly
583 * without the need for a wrapper function. The socket must have a valid
584 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
585 * except through a kernel programming error, so it is acceptable to panic
586 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
587 * because there actually /is/ a programming error somewhere... XXX)
588 */
589int
590in_setsockaddr(so, nam)
591 struct socket *so;
592 struct sockaddr **nam;
593{
594 int s;
595 register struct inpcb *inp;
596 register struct sockaddr_in *sin;
597
598 /*
599 * Do the malloc first in case it blocks.
600 */
601 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
602 M_WAITOK | M_ZERO);
603 sin->sin_family = AF_INET;
604 sin->sin_len = sizeof(*sin);
605
606 s = splnet();
607 inp = sotoinpcb(so);
608 if (!inp) {
609 splx(s);
610 free(sin, M_SONAME);
611 return ECONNRESET;
612 }
613 sin->sin_port = inp->inp_lport;
614 sin->sin_addr = inp->inp_laddr;
615 splx(s);
616
617 *nam = (struct sockaddr *)sin;
618 return 0;
619}
620
621int
622in_setpeeraddr(so, nam)
623 struct socket *so;
624 struct sockaddr **nam;
625{
626 int s;
627 struct inpcb *inp;
628 register struct sockaddr_in *sin;
629
630 /*
631 * Do the malloc first in case it blocks.
632 */
633 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
634 M_WAITOK | M_ZERO);
635 sin->sin_family = AF_INET;
636 sin->sin_len = sizeof(*sin);
637
638 s = splnet();
639 inp = sotoinpcb(so);
640 if (!inp) {
641 splx(s);
642 free(sin, M_SONAME);
643 return ECONNRESET;
644 }
645 sin->sin_port = inp->inp_fport;
646 sin->sin_addr = inp->inp_faddr;
647 splx(s);
648
649 *nam = (struct sockaddr *)sin;
650 return 0;
651}
652
653void
654in_pcbnotifyall(head, faddr, errno, notify)
655 struct inpcbhead *head;
656 struct in_addr faddr;
657 void (*notify) __P((struct inpcb *, int));
658{
659 struct inpcb *inp, *ninp;
660 int s;
661
662 s = splnet();
663 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
664 ninp = LIST_NEXT(inp, inp_list);
665#ifdef INET6
666 if ((inp->inp_vflag & INP_IPV4) == 0)
667 continue;
668#endif
669 if (inp->inp_faddr.s_addr != faddr.s_addr ||
670 inp->inp_socket == NULL)
671 continue;
672 (*notify)(inp, errno);
673 }
674 splx(s);
675}
676
677void
678in_pcbpurgeif0(head, ifp)
679 struct inpcb *head;
680 struct ifnet *ifp;
681{
682 struct inpcb *inp;
683 struct ip_moptions *imo;
684 int i, gap;
685
686 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
687 imo = inp->inp_moptions;
688 if ((inp->inp_vflag & INP_IPV4) &&
689 imo != NULL) {
690 /*
691 * Unselect the outgoing interface if it is being
692 * detached.
693 */
694 if (imo->imo_multicast_ifp == ifp)
695 imo->imo_multicast_ifp = NULL;
696
697 /*
698 * Drop multicast group membership if we joined
699 * through the interface being detached.
700 */
701 for (i = 0, gap = 0; i < imo->imo_num_memberships;
702 i++) {
703 if (imo->imo_membership[i]->inm_ifp == ifp) {
704 in_delmulti(imo->imo_membership[i]);
705 gap++;
706 } else if (gap != 0)
707 imo->imo_membership[i - gap] =
708 imo->imo_membership[i];
709 }
710 imo->imo_num_memberships -= gap;
711 }
712 }
713}
714
715/*
716 * Check for alternatives when higher level complains
717 * about service problems. For now, invalidate cached
718 * routing information. If the route was created dynamically
719 * (by a redirect), time to try a default gateway again.
720 */
721void
722in_losing(inp)
723 struct inpcb *inp;
724{
725 register struct rtentry *rt;
726 struct rt_addrinfo info;
727
728 if ((rt = inp->inp_route.ro_rt)) {
729 bzero((caddr_t)&info, sizeof(info));
730 info.rti_flags = rt->rt_flags;
731 info.rti_info[RTAX_DST] = rt_key(rt);
732 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
733 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
734 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
735 if (rt->rt_flags & RTF_DYNAMIC)
736 (void) rtrequest1(RTM_DELETE, &info, NULL);
737 inp->inp_route.ro_rt = NULL;
738 rtfree(rt);
739 /*
740 * A new route can be allocated
741 * the next time output is attempted.
742 */
743 }
744}
745
746/*
747 * After a routing change, flush old routing
748 * and allocate a (hopefully) better one.
749 */
750void
751in_rtchange(inp, errno)
752 register struct inpcb *inp;
753 int errno;
754{
755 if (inp->inp_route.ro_rt) {
756 rtfree(inp->inp_route.ro_rt);
757 inp->inp_route.ro_rt = 0;
758 /*
759 * A new route can be allocated the next time
760 * output is attempted.
761 */
762 }
763}
764
765/*
766 * Lookup a PCB based on the local address and port.
767 */
768struct inpcb *
769in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
770 struct inpcbinfo *pcbinfo;
771 struct in_addr laddr;
772 u_int lport_arg;
773 int wild_okay;
774{
775 register struct inpcb *inp;
776 int matchwild = 3, wildcard;
777 u_short lport = lport_arg;
778
779 if (!wild_okay) {
780 struct inpcbhead *head;
781 /*
782 * Look for an unconnected (wildcard foreign addr) PCB that
783 * matches the local address and port we're looking for.
784 */
785 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
786 LIST_FOREACH(inp, head, inp_hash) {
787#ifdef INET6
788 if ((inp->inp_vflag & INP_IPV4) == 0)
789 continue;
790#endif
791 if (inp->inp_faddr.s_addr == INADDR_ANY &&
792 inp->inp_laddr.s_addr == laddr.s_addr &&
793 inp->inp_lport == lport) {
794 /*
795 * Found.
796 */
797 return (inp);
798 }
799 }
800 /*
801 * Not found.
802 */
803 return (NULL);
804 } else {
805 struct inpcbporthead *porthash;
806 struct inpcbport *phd;
807 struct inpcb *match = NULL;
808 /*
809 * Best fit PCB lookup.
810 *
811 * First see if this local port is in use by looking on the
812 * port hash list.
813 */
814 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
815 pcbinfo->porthashmask)];
816 LIST_FOREACH(phd, porthash, phd_hash) {
817 if (phd->phd_port == lport)
818 break;
819 }
820 if (phd != NULL) {
821 /*
822 * Port is in use by one or more PCBs. Look for best
823 * fit.
824 */
825 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
826 wildcard = 0;
827#ifdef INET6
828 if ((inp->inp_vflag & INP_IPV4) == 0)
829 continue;
830#endif
831 if (inp->inp_faddr.s_addr != INADDR_ANY)
832 wildcard++;
833 if (inp->inp_laddr.s_addr != INADDR_ANY) {
834 if (laddr.s_addr == INADDR_ANY)
835 wildcard++;
836 else if (inp->inp_laddr.s_addr != laddr.s_addr)
837 continue;
838 } else {
839 if (laddr.s_addr != INADDR_ANY)
840 wildcard++;
841 }
842 if (wildcard < matchwild) {
843 match = inp;
844 matchwild = wildcard;
845 if (matchwild == 0) {
846 break;
847 }
848 }
849 }
850 }
851 return (match);
852 }
853}
854
855/*
856 * Lookup PCB in hash list.
857 */
858struct inpcb *
859in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard,
860 ifp)
861 struct inpcbinfo *pcbinfo;
862 struct in_addr faddr, laddr;
863 u_int fport_arg, lport_arg;
864 int wildcard;
865 struct ifnet *ifp;
866{
867 struct inpcbhead *head;
868 register struct inpcb *inp;
869 u_short fport = fport_arg, lport = lport_arg;
870
871 /*
872 * First look for an exact match.
873 */
874 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
875 LIST_FOREACH(inp, head, inp_hash) {
876#ifdef INET6
877 if ((inp->inp_vflag & INP_IPV4) == 0)
878 continue;
879#endif
880 if (inp->inp_faddr.s_addr == faddr.s_addr &&
881 inp->inp_laddr.s_addr == laddr.s_addr &&
882 inp->inp_fport == fport &&
883 inp->inp_lport == lport) {
884 /*
885 * Found.
886 */
887 return (inp);
888 }
889 }
890 if (wildcard) {
891 struct inpcb *local_wild = NULL;
892#if defined(INET6)
893 struct inpcb *local_wild_mapped = NULL;
894#endif /* defined(INET6) */
895
896 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
897 LIST_FOREACH(inp, head, inp_hash) {
898#ifdef INET6
899 if ((inp->inp_vflag & INP_IPV4) == 0)
900 continue;
901#endif
902 if (inp->inp_faddr.s_addr == INADDR_ANY &&
903 inp->inp_lport == lport) {
904 if (ifp && ifp->if_type == IFT_FAITH &&
905 (inp->inp_flags & INP_FAITH) == 0)
906 continue;
907 if (inp->inp_laddr.s_addr == laddr.s_addr)
908 return (inp);
909 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
910#if defined(INET6)
911 if (INP_CHECK_SOCKAF(inp->inp_socket,
912 AF_INET6))
913 local_wild_mapped = inp;
914 else
915#endif /* defined(INET6) */
916 local_wild = inp;
917 }
918 }
919 }
920#if defined(INET6)
921 if (local_wild == NULL)
922 return (local_wild_mapped);
923#endif /* defined(INET6) */
924 return (local_wild);
925 }
926
927 /*
928 * Not found.
929 */
930 return (NULL);
931}
932
933/*
934 * Insert PCB onto various hash lists.
935 */
936int
937in_pcbinshash(inp)
938 struct inpcb *inp;
939{
940 struct inpcbhead *pcbhash;
941 struct inpcbporthead *pcbporthash;
942 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
943 struct inpcbport *phd;
944 u_int32_t hashkey_faddr;
945
946#ifdef INET6
947 if (inp->inp_vflag & INP_IPV6)
948 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
949 else
950#endif /* INET6 */
951 hashkey_faddr = inp->inp_faddr.s_addr;
952
953 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
954 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
955
956 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
957 pcbinfo->porthashmask)];
958
959 /*
960 * Go through port list and look for a head for this lport.
961 */
962 LIST_FOREACH(phd, pcbporthash, phd_hash) {
963 if (phd->phd_port == inp->inp_lport)
964 break;
965 }
966 /*
967 * If none exists, malloc one and tack it on.
968 */
969 if (phd == NULL) {
970 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
971 if (phd == NULL) {
972 return (ENOBUFS); /* XXX */
973 }
974 phd->phd_port = inp->inp_lport;
975 LIST_INIT(&phd->phd_pcblist);
976 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
977 }
978 inp->inp_phd = phd;
979 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
980 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
981 return (0);
982}
983
984/*
985 * Move PCB to the proper hash bucket when { faddr, fport } have been
986 * changed. NOTE: This does not handle the case of the lport changing (the
987 * hashed port list would have to be updated as well), so the lport must
988 * not change after in_pcbinshash() has been called.
989 */
990void
991in_pcbrehash(inp)
992 struct inpcb *inp;
993{
994 struct inpcbhead *head;
995 u_int32_t hashkey_faddr;
996
997#ifdef INET6
998 if (inp->inp_vflag & INP_IPV6)
999 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1000 else
1001#endif /* INET6 */
1002 hashkey_faddr = inp->inp_faddr.s_addr;
1003
1004 head = &inp->inp_pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1005 inp->inp_lport, inp->inp_fport, inp->inp_pcbinfo->hashmask)];
1006
1007 LIST_REMOVE(inp, inp_hash);
1008 LIST_INSERT_HEAD(head, inp, inp_hash);
1009}
1010
1011/*
1012 * Remove PCB from various lists.
1013 */
1014void
1015in_pcbremlists(inp)
1016 struct inpcb *inp;
1017{
1018 inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt;
1019 if (inp->inp_lport) {
1020 struct inpcbport *phd = inp->inp_phd;
1021
1022 LIST_REMOVE(inp, inp_hash);
1023 LIST_REMOVE(inp, inp_portlist);
1024 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1025 LIST_REMOVE(phd, phd_hash);
1026 free(phd, M_PCB);
1027 }
1028 }
1029 LIST_REMOVE(inp, inp_list);
1030 inp->inp_pcbinfo->ipi_count--;
1031}
1032
1033int
dadab5e9 1034prison_xinpcb(struct thread *td, struct inpcb *inp)
984263bc 1035{
dadab5e9
MD
1036 struct ucred *cr;
1037
1038 if (td->td_proc == NULL)
1039 return (0);
1040 cr = td->td_proc->p_ucred;
1041 if (cr->cr_prison == NULL)
984263bc 1042 return (0);
dadab5e9 1043 if (ntohl(inp->inp_laddr.s_addr) == cr->cr_prison->pr_ip)
984263bc
MD
1044 return (0);
1045 return (1);
1046}
dadab5e9 1047