1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33df8bae1dSRodney W. Grimes * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 347bc4aca7SDavid Greenman * $Id: in_pcb.c,v 1.9 1995/04/09 01:29:18 davidg Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 38df8bae1dSRodney W. Grimes #include <sys/systm.h> 39df8bae1dSRodney W. Grimes #include <sys/malloc.h> 40df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 41df8bae1dSRodney W. Grimes #include <sys/protosw.h> 42df8bae1dSRodney W. Grimes #include <sys/socket.h> 43df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 44df8bae1dSRodney W. Grimes #include <sys/ioctl.h> 45df8bae1dSRodney W. Grimes #include <sys/errno.h> 46df8bae1dSRodney W. Grimes #include <sys/time.h> 47df8bae1dSRodney W. Grimes #include <sys/proc.h> 4815bd2b43SDavid Greenman #include <sys/queue.h> 49df8bae1dSRodney W. Grimes 50df8bae1dSRodney W. Grimes #include <net/if.h> 51df8bae1dSRodney W. Grimes #include <net/route.h> 52df8bae1dSRodney W. Grimes 53df8bae1dSRodney W. Grimes #include <netinet/in.h> 54df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 55df8bae1dSRodney W. Grimes #include <netinet/ip.h> 56df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 57df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 58df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 59df8bae1dSRodney W. Grimes 60df8bae1dSRodney W. Grimes struct in_addr zeroin_addr; 61df8bae1dSRodney W. Grimes 62df8bae1dSRodney W. Grimes int 6315bd2b43SDavid Greenman in_pcballoc(so, pcbinfo) 64df8bae1dSRodney W. Grimes struct socket *so; 6515bd2b43SDavid Greenman struct inpcbinfo *pcbinfo; 66df8bae1dSRodney W. Grimes { 67df8bae1dSRodney W. Grimes register struct inpcb *inp; 687bc4aca7SDavid Greenman int s; 69df8bae1dSRodney W. Grimes 702c8fe19fSDavid Greenman MALLOC(inp, struct inpcb *, sizeof(*inp), M_PCB, M_NOWAIT); 71df8bae1dSRodney W. Grimes if (inp == NULL) 72df8bae1dSRodney W. Grimes return (ENOBUFS); 73df8bae1dSRodney W. Grimes bzero((caddr_t)inp, sizeof(*inp)); 7415bd2b43SDavid Greenman inp->inp_pcbinfo = pcbinfo; 75df8bae1dSRodney W. Grimes inp->inp_socket = so; 767bc4aca7SDavid Greenman s = splnet(); 7715bd2b43SDavid Greenman LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list); 7815bd2b43SDavid Greenman in_pcbinshash(inp); 797bc4aca7SDavid Greenman splx(s); 80df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t)inp; 81df8bae1dSRodney W. Grimes return (0); 82df8bae1dSRodney W. Grimes } 83df8bae1dSRodney W. Grimes 84df8bae1dSRodney W. Grimes int 85df8bae1dSRodney W. Grimes in_pcbbind(inp, nam) 86df8bae1dSRodney W. Grimes register struct inpcb *inp; 87df8bae1dSRodney W. Grimes struct mbuf *nam; 88df8bae1dSRodney W. Grimes { 89df8bae1dSRodney W. Grimes register struct socket *so = inp->inp_socket; 9015bd2b43SDavid Greenman struct inpcbhead *head = inp->inp_pcbinfo->listhead; 9115bd2b43SDavid Greenman unsigned short *lastport = &inp->inp_pcbinfo->lastport; 9215bd2b43SDavid Greenman struct sockaddr_in *sin; 93df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 94df8bae1dSRodney W. Grimes u_short lport = 0; 95df8bae1dSRodney W. Grimes int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 96df8bae1dSRodney W. Grimes int error; 97df8bae1dSRodney W. Grimes 98df8bae1dSRodney W. Grimes if (in_ifaddr == 0) 99df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 100df8bae1dSRodney W. Grimes if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY) 101df8bae1dSRodney W. Grimes return (EINVAL); 102df8bae1dSRodney W. Grimes if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 && 103df8bae1dSRodney W. Grimes ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 || 104df8bae1dSRodney W. Grimes (so->so_options & SO_ACCEPTCONN) == 0)) 105df8bae1dSRodney W. Grimes wild = INPLOOKUP_WILDCARD; 106df8bae1dSRodney W. Grimes if (nam) { 107df8bae1dSRodney W. Grimes sin = mtod(nam, struct sockaddr_in *); 108df8bae1dSRodney W. Grimes if (nam->m_len != sizeof (*sin)) 109df8bae1dSRodney W. Grimes return (EINVAL); 110df8bae1dSRodney W. Grimes #ifdef notdef 111df8bae1dSRodney W. Grimes /* 112df8bae1dSRodney W. Grimes * We should check the family, but old programs 113df8bae1dSRodney W. Grimes * incorrectly fail to initialize it. 114df8bae1dSRodney W. Grimes */ 115df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 116df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 117df8bae1dSRodney W. Grimes #endif 118df8bae1dSRodney W. Grimes lport = sin->sin_port; 119df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 120df8bae1dSRodney W. Grimes /* 121df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 122df8bae1dSRodney W. Grimes * allow complete duplication of binding if 123df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 124df8bae1dSRodney W. Grimes * and a multicast address is bound on both 125df8bae1dSRodney W. Grimes * new and duplicated sockets. 126df8bae1dSRodney W. Grimes */ 127df8bae1dSRodney W. Grimes if (so->so_options & SO_REUSEADDR) 128df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 129df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 130df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 131df8bae1dSRodney W. Grimes if (ifa_ifwithaddr((struct sockaddr *)sin) == 0) 132df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 133df8bae1dSRodney W. Grimes } 134df8bae1dSRodney W. Grimes if (lport) { 135df8bae1dSRodney W. Grimes struct inpcb *t; 136df8bae1dSRodney W. Grimes 137df8bae1dSRodney W. Grimes /* GROSS */ 138df8bae1dSRodney W. Grimes if (ntohs(lport) < IPPORT_RESERVED && 139df8bae1dSRodney W. Grimes (error = suser(p->p_ucred, &p->p_acflag))) 140df8bae1dSRodney W. Grimes return (error); 141df8bae1dSRodney W. Grimes t = in_pcblookup(head, zeroin_addr, 0, 142df8bae1dSRodney W. Grimes sin->sin_addr, lport, wild); 143df8bae1dSRodney W. Grimes if (t && (reuseport & t->inp_socket->so_options) == 0) 144df8bae1dSRodney W. Grimes return (EADDRINUSE); 145df8bae1dSRodney W. Grimes } 146df8bae1dSRodney W. Grimes inp->inp_laddr = sin->sin_addr; 147df8bae1dSRodney W. Grimes } 148df8bae1dSRodney W. Grimes if (lport == 0) 149df8bae1dSRodney W. Grimes do { 15015bd2b43SDavid Greenman ++*lastport; 15115bd2b43SDavid Greenman if (*lastport < IPPORT_RESERVED || 15215bd2b43SDavid Greenman *lastport > IPPORT_USERRESERVED) 15315bd2b43SDavid Greenman *lastport = IPPORT_RESERVED; 15415bd2b43SDavid Greenman lport = htons(*lastport); 155df8bae1dSRodney W. Grimes } while (in_pcblookup(head, 156df8bae1dSRodney W. Grimes zeroin_addr, 0, inp->inp_laddr, lport, wild)); 157df8bae1dSRodney W. Grimes inp->inp_lport = lport; 15815bd2b43SDavid Greenman in_pcbrehash(inp); 159df8bae1dSRodney W. Grimes return (0); 160df8bae1dSRodney W. Grimes } 161df8bae1dSRodney W. Grimes 162999f1343SGarrett Wollman /* 163999f1343SGarrett Wollman * Transform old in_pcbconnect() into an inner subroutine for new 164999f1343SGarrett Wollman * in_pcbconnect(): Do some validity-checking on the remote 165999f1343SGarrett Wollman * address (in mbuf 'nam') and then determine local host address 166999f1343SGarrett Wollman * (i.e., which interface) to use to access that remote host. 167999f1343SGarrett Wollman * 168999f1343SGarrett Wollman * This preserves definition of in_pcbconnect(), while supporting a 169999f1343SGarrett Wollman * slightly different version for T/TCP. (This is more than 170999f1343SGarrett Wollman * a bit of a kludge, but cleaning up the internal interfaces would 171999f1343SGarrett Wollman * have forced minor changes in every protocol). 172999f1343SGarrett Wollman */ 173999f1343SGarrett Wollman 174999f1343SGarrett Wollman int 175999f1343SGarrett Wollman in_pcbladdr(inp, nam, plocal_sin) 176999f1343SGarrett Wollman register struct inpcb *inp; 177999f1343SGarrett Wollman struct mbuf *nam; 178999f1343SGarrett Wollman struct sockaddr_in **plocal_sin; 179999f1343SGarrett Wollman { 180df8bae1dSRodney W. Grimes struct in_ifaddr *ia; 18126f9a767SRodney W. Grimes struct sockaddr_in *ifaddr = 0; 182df8bae1dSRodney W. Grimes register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 183df8bae1dSRodney W. Grimes 184df8bae1dSRodney W. Grimes if (nam->m_len != sizeof (*sin)) 185df8bae1dSRodney W. Grimes return (EINVAL); 186df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 187df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 188df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 189df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 190df8bae1dSRodney W. Grimes if (in_ifaddr) { 191df8bae1dSRodney W. Grimes /* 192df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 193df8bae1dSRodney W. Grimes * use the primary local address. 194df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 195df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 196df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 197df8bae1dSRodney W. Grimes */ 198df8bae1dSRodney W. Grimes #define satosin(sa) ((struct sockaddr_in *)(sa)) 199df8bae1dSRodney W. Grimes #define sintosa(sin) ((struct sockaddr *)(sin)) 200df8bae1dSRodney W. Grimes #define ifatoia(ifa) ((struct in_ifaddr *)(ifa)) 201df8bae1dSRodney W. Grimes if (sin->sin_addr.s_addr == INADDR_ANY) 202df8bae1dSRodney W. Grimes sin->sin_addr = IA_SIN(in_ifaddr)->sin_addr; 203df8bae1dSRodney W. Grimes else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST && 204df8bae1dSRodney W. Grimes (in_ifaddr->ia_ifp->if_flags & IFF_BROADCAST)) 205df8bae1dSRodney W. Grimes sin->sin_addr = satosin(&in_ifaddr->ia_broadaddr)->sin_addr; 206df8bae1dSRodney W. Grimes } 207df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr == INADDR_ANY) { 208df8bae1dSRodney W. Grimes register struct route *ro; 209df8bae1dSRodney W. Grimes 210df8bae1dSRodney W. Grimes ia = (struct in_ifaddr *)0; 211df8bae1dSRodney W. Grimes /* 212df8bae1dSRodney W. Grimes * If route is known or can be allocated now, 213df8bae1dSRodney W. Grimes * our src addr is taken from the i/f, else punt. 214df8bae1dSRodney W. Grimes */ 215df8bae1dSRodney W. Grimes ro = &inp->inp_route; 216df8bae1dSRodney W. Grimes if (ro->ro_rt && 217df8bae1dSRodney W. Grimes (satosin(&ro->ro_dst)->sin_addr.s_addr != 218df8bae1dSRodney W. Grimes sin->sin_addr.s_addr || 219df8bae1dSRodney W. Grimes inp->inp_socket->so_options & SO_DONTROUTE)) { 220df8bae1dSRodney W. Grimes RTFREE(ro->ro_rt); 221df8bae1dSRodney W. Grimes ro->ro_rt = (struct rtentry *)0; 222df8bae1dSRodney W. Grimes } 223df8bae1dSRodney W. Grimes if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/ 224df8bae1dSRodney W. Grimes (ro->ro_rt == (struct rtentry *)0 || 225df8bae1dSRodney W. Grimes ro->ro_rt->rt_ifp == (struct ifnet *)0)) { 226df8bae1dSRodney W. Grimes /* No route yet, so try to acquire one */ 227df8bae1dSRodney W. Grimes ro->ro_dst.sa_family = AF_INET; 228df8bae1dSRodney W. Grimes ro->ro_dst.sa_len = sizeof(struct sockaddr_in); 229df8bae1dSRodney W. Grimes ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 230df8bae1dSRodney W. Grimes sin->sin_addr; 231df8bae1dSRodney W. Grimes rtalloc(ro); 232df8bae1dSRodney W. Grimes } 233df8bae1dSRodney W. Grimes /* 234df8bae1dSRodney W. Grimes * If we found a route, use the address 235df8bae1dSRodney W. Grimes * corresponding to the outgoing interface 236df8bae1dSRodney W. Grimes * unless it is the loopback (in case a route 237df8bae1dSRodney W. Grimes * to our address on another net goes to loopback). 238df8bae1dSRodney W. Grimes */ 239df8bae1dSRodney W. Grimes if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) 240df8bae1dSRodney W. Grimes ia = ifatoia(ro->ro_rt->rt_ifa); 241df8bae1dSRodney W. Grimes if (ia == 0) { 242df8bae1dSRodney W. Grimes u_short fport = sin->sin_port; 243df8bae1dSRodney W. Grimes 244df8bae1dSRodney W. Grimes sin->sin_port = 0; 245df8bae1dSRodney W. Grimes ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin))); 246df8bae1dSRodney W. Grimes if (ia == 0) 247df8bae1dSRodney W. Grimes ia = ifatoia(ifa_ifwithnet(sintosa(sin))); 248df8bae1dSRodney W. Grimes sin->sin_port = fport; 249df8bae1dSRodney W. Grimes if (ia == 0) 250df8bae1dSRodney W. Grimes ia = in_ifaddr; 251df8bae1dSRodney W. Grimes if (ia == 0) 252df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 253df8bae1dSRodney W. Grimes } 254df8bae1dSRodney W. Grimes /* 255df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 256df8bae1dSRodney W. Grimes * interface has been set as a multicast option, use the 257df8bae1dSRodney W. Grimes * address of that interface as our source address. 258df8bae1dSRodney W. Grimes */ 259df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 260df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 261df8bae1dSRodney W. Grimes struct ip_moptions *imo; 262df8bae1dSRodney W. Grimes struct ifnet *ifp; 263df8bae1dSRodney W. Grimes 264df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 265df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 266df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 267df8bae1dSRodney W. Grimes for (ia = in_ifaddr; ia; ia = ia->ia_next) 268df8bae1dSRodney W. Grimes if (ia->ia_ifp == ifp) 269df8bae1dSRodney W. Grimes break; 270df8bae1dSRodney W. Grimes if (ia == 0) 271df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 272df8bae1dSRodney W. Grimes } 273df8bae1dSRodney W. Grimes } 274999f1343SGarrett Wollman /* 275999f1343SGarrett Wollman * Don't do pcblookup call here; return interface in plocal_sin 276999f1343SGarrett Wollman * and exit to caller, that will do the lookup. 277999f1343SGarrett Wollman */ 278999f1343SGarrett Wollman *plocal_sin = &ia->ia_addr; 279999f1343SGarrett Wollman 280999f1343SGarrett Wollman } 281999f1343SGarrett Wollman return(0); 282999f1343SGarrett Wollman } 283999f1343SGarrett Wollman 284999f1343SGarrett Wollman /* 285999f1343SGarrett Wollman * Outer subroutine: 286999f1343SGarrett Wollman * Connect from a socket to a specified address. 287999f1343SGarrett Wollman * Both address and port must be specified in argument sin. 288999f1343SGarrett Wollman * If don't have a local address for this socket yet, 289999f1343SGarrett Wollman * then pick one. 290999f1343SGarrett Wollman */ 291999f1343SGarrett Wollman int 292999f1343SGarrett Wollman in_pcbconnect(inp, nam) 293999f1343SGarrett Wollman register struct inpcb *inp; 294999f1343SGarrett Wollman struct mbuf *nam; 295999f1343SGarrett Wollman { 296999f1343SGarrett Wollman struct sockaddr_in *ifaddr; 297999f1343SGarrett Wollman register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 298999f1343SGarrett Wollman int error; 299999f1343SGarrett Wollman 300999f1343SGarrett Wollman /* 301999f1343SGarrett Wollman * Call inner routine, to assign local interface address. 302999f1343SGarrett Wollman */ 303999f1343SGarrett Wollman if (error = in_pcbladdr(inp, nam, &ifaddr)) 304999f1343SGarrett Wollman return(error); 305999f1343SGarrett Wollman 30615bd2b43SDavid Greenman if (in_pcblookup(inp->inp_pcbinfo->listhead, 307df8bae1dSRodney W. Grimes sin->sin_addr, 308df8bae1dSRodney W. Grimes sin->sin_port, 309df8bae1dSRodney W. Grimes inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr, 310df8bae1dSRodney W. Grimes inp->inp_lport, 311df8bae1dSRodney W. Grimes 0)) 312df8bae1dSRodney W. Grimes return (EADDRINUSE); 313df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr == INADDR_ANY) { 314df8bae1dSRodney W. Grimes if (inp->inp_lport == 0) 315df8bae1dSRodney W. Grimes (void)in_pcbbind(inp, (struct mbuf *)0); 316df8bae1dSRodney W. Grimes inp->inp_laddr = ifaddr->sin_addr; 317df8bae1dSRodney W. Grimes } 318df8bae1dSRodney W. Grimes inp->inp_faddr = sin->sin_addr; 319df8bae1dSRodney W. Grimes inp->inp_fport = sin->sin_port; 32015bd2b43SDavid Greenman in_pcbrehash(inp); 321df8bae1dSRodney W. Grimes return (0); 322df8bae1dSRodney W. Grimes } 323df8bae1dSRodney W. Grimes 32426f9a767SRodney W. Grimes void 325df8bae1dSRodney W. Grimes in_pcbdisconnect(inp) 326df8bae1dSRodney W. Grimes struct inpcb *inp; 327df8bae1dSRodney W. Grimes { 328df8bae1dSRodney W. Grimes 329df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 330df8bae1dSRodney W. Grimes inp->inp_fport = 0; 33115bd2b43SDavid Greenman in_pcbrehash(inp); 332df8bae1dSRodney W. Grimes if (inp->inp_socket->so_state & SS_NOFDREF) 333df8bae1dSRodney W. Grimes in_pcbdetach(inp); 334df8bae1dSRodney W. Grimes } 335df8bae1dSRodney W. Grimes 33626f9a767SRodney W. Grimes void 337df8bae1dSRodney W. Grimes in_pcbdetach(inp) 338df8bae1dSRodney W. Grimes struct inpcb *inp; 339df8bae1dSRodney W. Grimes { 340df8bae1dSRodney W. Grimes struct socket *so = inp->inp_socket; 3417bc4aca7SDavid Greenman int s; 342df8bae1dSRodney W. Grimes 343df8bae1dSRodney W. Grimes so->so_pcb = 0; 344df8bae1dSRodney W. Grimes sofree(so); 345df8bae1dSRodney W. Grimes if (inp->inp_options) 346df8bae1dSRodney W. Grimes (void)m_free(inp->inp_options); 347df8bae1dSRodney W. Grimes if (inp->inp_route.ro_rt) 348df8bae1dSRodney W. Grimes rtfree(inp->inp_route.ro_rt); 349df8bae1dSRodney W. Grimes ip_freemoptions(inp->inp_moptions); 3507bc4aca7SDavid Greenman s = splnet(); 35115bd2b43SDavid Greenman LIST_REMOVE(inp, inp_hash); 35215bd2b43SDavid Greenman LIST_REMOVE(inp, inp_list); 3537bc4aca7SDavid Greenman splx(s); 354df8bae1dSRodney W. Grimes FREE(inp, M_PCB); 355df8bae1dSRodney W. Grimes } 356df8bae1dSRodney W. Grimes 35726f9a767SRodney W. Grimes void 358df8bae1dSRodney W. Grimes in_setsockaddr(inp, nam) 359df8bae1dSRodney W. Grimes register struct inpcb *inp; 360df8bae1dSRodney W. Grimes struct mbuf *nam; 361df8bae1dSRodney W. Grimes { 362df8bae1dSRodney W. Grimes register struct sockaddr_in *sin; 363df8bae1dSRodney W. Grimes 364df8bae1dSRodney W. Grimes nam->m_len = sizeof (*sin); 365df8bae1dSRodney W. Grimes sin = mtod(nam, struct sockaddr_in *); 366df8bae1dSRodney W. Grimes bzero((caddr_t)sin, sizeof (*sin)); 367df8bae1dSRodney W. Grimes sin->sin_family = AF_INET; 368df8bae1dSRodney W. Grimes sin->sin_len = sizeof(*sin); 369df8bae1dSRodney W. Grimes sin->sin_port = inp->inp_lport; 370df8bae1dSRodney W. Grimes sin->sin_addr = inp->inp_laddr; 371df8bae1dSRodney W. Grimes } 372df8bae1dSRodney W. Grimes 37326f9a767SRodney W. Grimes void 374df8bae1dSRodney W. Grimes in_setpeeraddr(inp, nam) 375df8bae1dSRodney W. Grimes struct inpcb *inp; 376df8bae1dSRodney W. Grimes struct mbuf *nam; 377df8bae1dSRodney W. Grimes { 378df8bae1dSRodney W. Grimes register struct sockaddr_in *sin; 379df8bae1dSRodney W. Grimes 380df8bae1dSRodney W. Grimes nam->m_len = sizeof (*sin); 381df8bae1dSRodney W. Grimes sin = mtod(nam, struct sockaddr_in *); 382df8bae1dSRodney W. Grimes bzero((caddr_t)sin, sizeof (*sin)); 383df8bae1dSRodney W. Grimes sin->sin_family = AF_INET; 384df8bae1dSRodney W. Grimes sin->sin_len = sizeof(*sin); 385df8bae1dSRodney W. Grimes sin->sin_port = inp->inp_fport; 386df8bae1dSRodney W. Grimes sin->sin_addr = inp->inp_faddr; 387df8bae1dSRodney W. Grimes } 388df8bae1dSRodney W. Grimes 389df8bae1dSRodney W. Grimes /* 390df8bae1dSRodney W. Grimes * Pass some notification to all connections of a protocol 391df8bae1dSRodney W. Grimes * associated with address dst. The local address and/or port numbers 392df8bae1dSRodney W. Grimes * may be specified to limit the search. The "usual action" will be 393df8bae1dSRodney W. Grimes * taken, depending on the ctlinput cmd. The caller must filter any 394df8bae1dSRodney W. Grimes * cmds that are uninteresting (e.g., no error in the map). 395df8bae1dSRodney W. Grimes * Call the protocol specific routine (if any) to report 396df8bae1dSRodney W. Grimes * any errors for each matching socket. 397df8bae1dSRodney W. Grimes * 398df8bae1dSRodney W. Grimes * Must be called at splnet. 399df8bae1dSRodney W. Grimes */ 40026f9a767SRodney W. Grimes void 401df8bae1dSRodney W. Grimes in_pcbnotify(head, dst, fport_arg, laddr, lport_arg, cmd, notify) 40215bd2b43SDavid Greenman struct inpcbhead *head; 403df8bae1dSRodney W. Grimes struct sockaddr *dst; 404df8bae1dSRodney W. Grimes u_int fport_arg, lport_arg; 405df8bae1dSRodney W. Grimes struct in_addr laddr; 406df8bae1dSRodney W. Grimes int cmd; 407df8bae1dSRodney W. Grimes void (*notify) __P((struct inpcb *, int)); 408df8bae1dSRodney W. Grimes { 409df8bae1dSRodney W. Grimes register struct inpcb *inp, *oinp; 410df8bae1dSRodney W. Grimes struct in_addr faddr; 411df8bae1dSRodney W. Grimes u_short fport = fport_arg, lport = lport_arg; 4127bc4aca7SDavid Greenman int errno, s; 413df8bae1dSRodney W. Grimes 414df8bae1dSRodney W. Grimes if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET) 415df8bae1dSRodney W. Grimes return; 416df8bae1dSRodney W. Grimes faddr = ((struct sockaddr_in *)dst)->sin_addr; 417df8bae1dSRodney W. Grimes if (faddr.s_addr == INADDR_ANY) 418df8bae1dSRodney W. Grimes return; 419df8bae1dSRodney W. Grimes 420df8bae1dSRodney W. Grimes /* 421df8bae1dSRodney W. Grimes * Redirects go to all references to the destination, 422df8bae1dSRodney W. Grimes * and use in_rtchange to invalidate the route cache. 423df8bae1dSRodney W. Grimes * Dead host indications: notify all references to the destination. 424df8bae1dSRodney W. Grimes * Otherwise, if we have knowledge of the local port and address, 425df8bae1dSRodney W. Grimes * deliver only to that socket. 426df8bae1dSRodney W. Grimes */ 427df8bae1dSRodney W. Grimes if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 428df8bae1dSRodney W. Grimes fport = 0; 429df8bae1dSRodney W. Grimes lport = 0; 430df8bae1dSRodney W. Grimes laddr.s_addr = 0; 431df8bae1dSRodney W. Grimes if (cmd != PRC_HOSTDEAD) 432df8bae1dSRodney W. Grimes notify = in_rtchange; 433df8bae1dSRodney W. Grimes } 434df8bae1dSRodney W. Grimes errno = inetctlerrmap[cmd]; 4357bc4aca7SDavid Greenman s = splnet(); 43615bd2b43SDavid Greenman for (inp = head->lh_first; inp != NULL;) { 437df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != faddr.s_addr || 438df8bae1dSRodney W. Grimes inp->inp_socket == 0 || 439df8bae1dSRodney W. Grimes (lport && inp->inp_lport != lport) || 440df8bae1dSRodney W. Grimes (laddr.s_addr && inp->inp_laddr.s_addr != laddr.s_addr) || 441df8bae1dSRodney W. Grimes (fport && inp->inp_fport != fport)) { 44215bd2b43SDavid Greenman inp = inp->inp_list.le_next; 443df8bae1dSRodney W. Grimes continue; 444df8bae1dSRodney W. Grimes } 445df8bae1dSRodney W. Grimes oinp = inp; 44615bd2b43SDavid Greenman inp = inp->inp_list.le_next; 447df8bae1dSRodney W. Grimes if (notify) 448df8bae1dSRodney W. Grimes (*notify)(oinp, errno); 449df8bae1dSRodney W. Grimes } 4507bc4aca7SDavid Greenman splx(s); 451df8bae1dSRodney W. Grimes } 452df8bae1dSRodney W. Grimes 453df8bae1dSRodney W. Grimes /* 454df8bae1dSRodney W. Grimes * Check for alternatives when higher level complains 455df8bae1dSRodney W. Grimes * about service problems. For now, invalidate cached 456df8bae1dSRodney W. Grimes * routing information. If the route was created dynamically 457df8bae1dSRodney W. Grimes * (by a redirect), time to try a default gateway again. 458df8bae1dSRodney W. Grimes */ 45926f9a767SRodney W. Grimes void 460df8bae1dSRodney W. Grimes in_losing(inp) 461df8bae1dSRodney W. Grimes struct inpcb *inp; 462df8bae1dSRodney W. Grimes { 463df8bae1dSRodney W. Grimes register struct rtentry *rt; 464df8bae1dSRodney W. Grimes struct rt_addrinfo info; 465df8bae1dSRodney W. Grimes 466df8bae1dSRodney W. Grimes if ((rt = inp->inp_route.ro_rt)) { 467df8bae1dSRodney W. Grimes inp->inp_route.ro_rt = 0; 468df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 469df8bae1dSRodney W. Grimes info.rti_info[RTAX_DST] = 470df8bae1dSRodney W. Grimes (struct sockaddr *)&inp->inp_route.ro_dst; 471df8bae1dSRodney W. Grimes info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 472df8bae1dSRodney W. Grimes info.rti_info[RTAX_NETMASK] = rt_mask(rt); 473df8bae1dSRodney W. Grimes rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 474df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_DYNAMIC) 475df8bae1dSRodney W. Grimes (void) rtrequest(RTM_DELETE, rt_key(rt), 476df8bae1dSRodney W. Grimes rt->rt_gateway, rt_mask(rt), rt->rt_flags, 477df8bae1dSRodney W. Grimes (struct rtentry **)0); 478df8bae1dSRodney W. Grimes else 479df8bae1dSRodney W. Grimes /* 480df8bae1dSRodney W. Grimes * A new route can be allocated 481df8bae1dSRodney W. Grimes * the next time output is attempted. 482df8bae1dSRodney W. Grimes */ 483df8bae1dSRodney W. Grimes rtfree(rt); 484df8bae1dSRodney W. Grimes } 485df8bae1dSRodney W. Grimes } 486df8bae1dSRodney W. Grimes 487df8bae1dSRodney W. Grimes /* 488df8bae1dSRodney W. Grimes * After a routing change, flush old routing 489df8bae1dSRodney W. Grimes * and allocate a (hopefully) better one. 490df8bae1dSRodney W. Grimes */ 491df8bae1dSRodney W. Grimes void 492df8bae1dSRodney W. Grimes in_rtchange(inp, errno) 493df8bae1dSRodney W. Grimes register struct inpcb *inp; 494df8bae1dSRodney W. Grimes int errno; 495df8bae1dSRodney W. Grimes { 496df8bae1dSRodney W. Grimes if (inp->inp_route.ro_rt) { 497df8bae1dSRodney W. Grimes rtfree(inp->inp_route.ro_rt); 498df8bae1dSRodney W. Grimes inp->inp_route.ro_rt = 0; 499df8bae1dSRodney W. Grimes /* 500df8bae1dSRodney W. Grimes * A new route can be allocated the next time 501df8bae1dSRodney W. Grimes * output is attempted. 502df8bae1dSRodney W. Grimes */ 503df8bae1dSRodney W. Grimes } 504df8bae1dSRodney W. Grimes } 505df8bae1dSRodney W. Grimes 506df8bae1dSRodney W. Grimes struct inpcb * 507df8bae1dSRodney W. Grimes in_pcblookup(head, faddr, fport_arg, laddr, lport_arg, flags) 50815bd2b43SDavid Greenman struct inpcbhead *head; 509df8bae1dSRodney W. Grimes struct in_addr faddr, laddr; 510df8bae1dSRodney W. Grimes u_int fport_arg, lport_arg; 511df8bae1dSRodney W. Grimes int flags; 512df8bae1dSRodney W. Grimes { 5133dbdc25cSDavid Greenman register struct inpcb *inp, *match = NULL; 514df8bae1dSRodney W. Grimes int matchwild = 3, wildcard; 515df8bae1dSRodney W. Grimes u_short fport = fport_arg, lport = lport_arg; 5167bc4aca7SDavid Greenman int s; 5177bc4aca7SDavid Greenman 5187bc4aca7SDavid Greenman s = splnet(); 519df8bae1dSRodney W. Grimes 52015bd2b43SDavid Greenman for (inp = head->lh_first; inp != NULL; inp = inp->inp_list.le_next) { 521df8bae1dSRodney W. Grimes if (inp->inp_lport != lport) 522df8bae1dSRodney W. Grimes continue; 523df8bae1dSRodney W. Grimes wildcard = 0; 524df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 525df8bae1dSRodney W. Grimes if (faddr.s_addr == INADDR_ANY) 526df8bae1dSRodney W. Grimes wildcard++; 527df8bae1dSRodney W. Grimes else if (inp->inp_faddr.s_addr != faddr.s_addr || 528df8bae1dSRodney W. Grimes inp->inp_fport != fport) 529df8bae1dSRodney W. Grimes continue; 530df8bae1dSRodney W. Grimes } else { 531df8bae1dSRodney W. Grimes if (faddr.s_addr != INADDR_ANY) 532df8bae1dSRodney W. Grimes wildcard++; 533df8bae1dSRodney W. Grimes } 53415bd2b43SDavid Greenman if (inp->inp_laddr.s_addr != INADDR_ANY) { 53515bd2b43SDavid Greenman if (laddr.s_addr == INADDR_ANY) 53615bd2b43SDavid Greenman wildcard++; 53715bd2b43SDavid Greenman else if (inp->inp_laddr.s_addr != laddr.s_addr) 53815bd2b43SDavid Greenman continue; 53915bd2b43SDavid Greenman } else { 54015bd2b43SDavid Greenman if (laddr.s_addr != INADDR_ANY) 54115bd2b43SDavid Greenman wildcard++; 54215bd2b43SDavid Greenman } 543df8bae1dSRodney W. Grimes if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0) 544df8bae1dSRodney W. Grimes continue; 545df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 546df8bae1dSRodney W. Grimes match = inp; 547df8bae1dSRodney W. Grimes matchwild = wildcard; 5483dbdc25cSDavid Greenman if (matchwild == 0) { 549df8bae1dSRodney W. Grimes break; 550df8bae1dSRodney W. Grimes } 551df8bae1dSRodney W. Grimes } 5523dbdc25cSDavid Greenman } 5537bc4aca7SDavid Greenman splx(s); 554df8bae1dSRodney W. Grimes return (match); 555df8bae1dSRodney W. Grimes } 55615bd2b43SDavid Greenman 55715bd2b43SDavid Greenman /* 55815bd2b43SDavid Greenman * Lookup PCB in hash list. 55915bd2b43SDavid Greenman */ 56015bd2b43SDavid Greenman struct inpcb * 56115bd2b43SDavid Greenman in_pcblookuphash(pcbinfo, faddr, fport_arg, laddr, lport_arg) 56215bd2b43SDavid Greenman struct inpcbinfo *pcbinfo; 56315bd2b43SDavid Greenman struct in_addr faddr, laddr; 56415bd2b43SDavid Greenman u_int fport_arg, lport_arg; 56515bd2b43SDavid Greenman { 56615bd2b43SDavid Greenman struct inpcbhead *head; 56715bd2b43SDavid Greenman register struct inpcb *inp; 56815bd2b43SDavid Greenman u_short fport = fport_arg, lport = lport_arg; 5697bc4aca7SDavid Greenman int s; 57015bd2b43SDavid Greenman 5717bc4aca7SDavid Greenman s = splnet(); 57215bd2b43SDavid Greenman /* 57315bd2b43SDavid Greenman * First look for an exact match. 57415bd2b43SDavid Greenman */ 57515bd2b43SDavid Greenman head = &pcbinfo->hashbase[(faddr.s_addr + lport + fport) % pcbinfo->hashsize]; 57615bd2b43SDavid Greenman 57715bd2b43SDavid Greenman for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) { 57815bd2b43SDavid Greenman if (inp->inp_faddr.s_addr != faddr.s_addr || 57915bd2b43SDavid Greenman inp->inp_fport != fport || 58015bd2b43SDavid Greenman inp->inp_lport != lport || 58115bd2b43SDavid Greenman inp->inp_laddr.s_addr != laddr.s_addr) 58215bd2b43SDavid Greenman continue; 58315bd2b43SDavid Greenman /* 58415bd2b43SDavid Greenman * Move PCB to head of this hash chain so that it can be 58515bd2b43SDavid Greenman * found more quickly in the future. 58615bd2b43SDavid Greenman */ 58715bd2b43SDavid Greenman if (inp != head->lh_first) { 58815bd2b43SDavid Greenman LIST_REMOVE(inp, inp_hash); 58915bd2b43SDavid Greenman LIST_INSERT_HEAD(head, inp, inp_hash); 59015bd2b43SDavid Greenman } 5917bc4aca7SDavid Greenman splx(s); 59215bd2b43SDavid Greenman return (inp); 59315bd2b43SDavid Greenman } 5947bc4aca7SDavid Greenman splx(s); 59515bd2b43SDavid Greenman 59615bd2b43SDavid Greenman /* 59715bd2b43SDavid Greenman * Didn't find an exact match, so try again looking for a matching 59815bd2b43SDavid Greenman * wildcard PCB. 59915bd2b43SDavid Greenman */ 60015bd2b43SDavid Greenman return (in_pcblookup(pcbinfo->listhead, faddr, fport_arg, laddr, 60115bd2b43SDavid Greenman lport_arg, INPLOOKUP_WILDCARD)); 60215bd2b43SDavid Greenman } 60315bd2b43SDavid Greenman 6047bc4aca7SDavid Greenman /* 6057bc4aca7SDavid Greenman * Insert PCB into hash chain. Must be called at splnet. 6067bc4aca7SDavid Greenman */ 60715bd2b43SDavid Greenman void 60815bd2b43SDavid Greenman in_pcbinshash(inp) 60915bd2b43SDavid Greenman struct inpcb *inp; 61015bd2b43SDavid Greenman { 61115bd2b43SDavid Greenman struct inpcbhead *head; 61215bd2b43SDavid Greenman 61315bd2b43SDavid Greenman head = &inp->inp_pcbinfo->hashbase[(inp->inp_faddr.s_addr + 61415bd2b43SDavid Greenman inp->inp_lport + inp->inp_fport) % inp->inp_pcbinfo->hashsize]; 61515bd2b43SDavid Greenman 61615bd2b43SDavid Greenman LIST_INSERT_HEAD(head, inp, inp_hash); 61715bd2b43SDavid Greenman } 61815bd2b43SDavid Greenman 61915bd2b43SDavid Greenman void 62015bd2b43SDavid Greenman in_pcbrehash(inp) 62115bd2b43SDavid Greenman struct inpcb *inp; 62215bd2b43SDavid Greenman { 62315bd2b43SDavid Greenman struct inpcbhead *head; 6247bc4aca7SDavid Greenman int s; 62515bd2b43SDavid Greenman 6267bc4aca7SDavid Greenman s = splnet(); 62715bd2b43SDavid Greenman LIST_REMOVE(inp, inp_hash); 62815bd2b43SDavid Greenman 62915bd2b43SDavid Greenman head = &inp->inp_pcbinfo->hashbase[(inp->inp_faddr.s_addr + 63015bd2b43SDavid Greenman inp->inp_lport + inp->inp_fport) % inp->inp_pcbinfo->hashsize]; 63115bd2b43SDavid Greenman 63215bd2b43SDavid Greenman LIST_INSERT_HEAD(head, inp, inp_hash); 6337bc4aca7SDavid Greenman splx(s); 63415bd2b43SDavid Greenman } 635