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 34999f1343SGarrett Wollman * $Id: in_pcb.c,v 1.3 1994/08/02 07:48: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> 48df8bae1dSRodney W. Grimes 49df8bae1dSRodney W. Grimes #include <net/if.h> 50df8bae1dSRodney W. Grimes #include <net/route.h> 51df8bae1dSRodney W. Grimes 52df8bae1dSRodney W. Grimes #include <netinet/in.h> 53df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 54df8bae1dSRodney W. Grimes #include <netinet/ip.h> 55df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 56df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 57df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 58df8bae1dSRodney W. Grimes 59df8bae1dSRodney W. Grimes struct in_addr zeroin_addr; 60df8bae1dSRodney W. Grimes 61df8bae1dSRodney W. Grimes int 62df8bae1dSRodney W. Grimes in_pcballoc(so, head) 63df8bae1dSRodney W. Grimes struct socket *so; 64df8bae1dSRodney W. Grimes struct inpcb *head; 65df8bae1dSRodney W. Grimes { 66df8bae1dSRodney W. Grimes register struct inpcb *inp; 67df8bae1dSRodney W. Grimes 68df8bae1dSRodney W. Grimes MALLOC(inp, struct inpcb *, sizeof(*inp), M_PCB, M_WAITOK); 69df8bae1dSRodney W. Grimes if (inp == NULL) 70df8bae1dSRodney W. Grimes return (ENOBUFS); 71df8bae1dSRodney W. Grimes bzero((caddr_t)inp, sizeof(*inp)); 72df8bae1dSRodney W. Grimes inp->inp_head = head; 73df8bae1dSRodney W. Grimes inp->inp_socket = so; 74df8bae1dSRodney W. Grimes insque(inp, head); 75df8bae1dSRodney W. Grimes so->so_pcb = (caddr_t)inp; 76df8bae1dSRodney W. Grimes return (0); 77df8bae1dSRodney W. Grimes } 78df8bae1dSRodney W. Grimes 79df8bae1dSRodney W. Grimes int 80df8bae1dSRodney W. Grimes in_pcbbind(inp, nam) 81df8bae1dSRodney W. Grimes register struct inpcb *inp; 82df8bae1dSRodney W. Grimes struct mbuf *nam; 83df8bae1dSRodney W. Grimes { 84df8bae1dSRodney W. Grimes register struct socket *so = inp->inp_socket; 85df8bae1dSRodney W. Grimes register struct inpcb *head = inp->inp_head; 86df8bae1dSRodney W. Grimes register struct sockaddr_in *sin; 87df8bae1dSRodney W. Grimes struct proc *p = curproc; /* XXX */ 88df8bae1dSRodney W. Grimes u_short lport = 0; 89df8bae1dSRodney W. Grimes int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); 90df8bae1dSRodney W. Grimes int error; 91df8bae1dSRodney W. Grimes 92df8bae1dSRodney W. Grimes if (in_ifaddr == 0) 93df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 94df8bae1dSRodney W. Grimes if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY) 95df8bae1dSRodney W. Grimes return (EINVAL); 96df8bae1dSRodney W. Grimes if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 && 97df8bae1dSRodney W. Grimes ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 || 98df8bae1dSRodney W. Grimes (so->so_options & SO_ACCEPTCONN) == 0)) 99df8bae1dSRodney W. Grimes wild = INPLOOKUP_WILDCARD; 100df8bae1dSRodney W. Grimes if (nam) { 101df8bae1dSRodney W. Grimes sin = mtod(nam, struct sockaddr_in *); 102df8bae1dSRodney W. Grimes if (nam->m_len != sizeof (*sin)) 103df8bae1dSRodney W. Grimes return (EINVAL); 104df8bae1dSRodney W. Grimes #ifdef notdef 105df8bae1dSRodney W. Grimes /* 106df8bae1dSRodney W. Grimes * We should check the family, but old programs 107df8bae1dSRodney W. Grimes * incorrectly fail to initialize it. 108df8bae1dSRodney W. Grimes */ 109df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 110df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 111df8bae1dSRodney W. Grimes #endif 112df8bae1dSRodney W. Grimes lport = sin->sin_port; 113df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 114df8bae1dSRodney W. Grimes /* 115df8bae1dSRodney W. Grimes * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; 116df8bae1dSRodney W. Grimes * allow complete duplication of binding if 117df8bae1dSRodney W. Grimes * SO_REUSEPORT is set, or if SO_REUSEADDR is set 118df8bae1dSRodney W. Grimes * and a multicast address is bound on both 119df8bae1dSRodney W. Grimes * new and duplicated sockets. 120df8bae1dSRodney W. Grimes */ 121df8bae1dSRodney W. Grimes if (so->so_options & SO_REUSEADDR) 122df8bae1dSRodney W. Grimes reuseport = SO_REUSEADDR|SO_REUSEPORT; 123df8bae1dSRodney W. Grimes } else if (sin->sin_addr.s_addr != INADDR_ANY) { 124df8bae1dSRodney W. Grimes sin->sin_port = 0; /* yech... */ 125df8bae1dSRodney W. Grimes if (ifa_ifwithaddr((struct sockaddr *)sin) == 0) 126df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 127df8bae1dSRodney W. Grimes } 128df8bae1dSRodney W. Grimes if (lport) { 129df8bae1dSRodney W. Grimes struct inpcb *t; 130df8bae1dSRodney W. Grimes 131df8bae1dSRodney W. Grimes /* GROSS */ 132df8bae1dSRodney W. Grimes if (ntohs(lport) < IPPORT_RESERVED && 133df8bae1dSRodney W. Grimes (error = suser(p->p_ucred, &p->p_acflag))) 134df8bae1dSRodney W. Grimes return (error); 135df8bae1dSRodney W. Grimes t = in_pcblookup(head, zeroin_addr, 0, 136df8bae1dSRodney W. Grimes sin->sin_addr, lport, wild); 137df8bae1dSRodney W. Grimes if (t && (reuseport & t->inp_socket->so_options) == 0) 138df8bae1dSRodney W. Grimes return (EADDRINUSE); 139df8bae1dSRodney W. Grimes } 140df8bae1dSRodney W. Grimes inp->inp_laddr = sin->sin_addr; 141df8bae1dSRodney W. Grimes } 142df8bae1dSRodney W. Grimes if (lport == 0) 143df8bae1dSRodney W. Grimes do { 144df8bae1dSRodney W. Grimes if (head->inp_lport++ < IPPORT_RESERVED || 145df8bae1dSRodney W. Grimes head->inp_lport > IPPORT_USERRESERVED) 146df8bae1dSRodney W. Grimes head->inp_lport = IPPORT_RESERVED; 147df8bae1dSRodney W. Grimes lport = htons(head->inp_lport); 148df8bae1dSRodney W. Grimes } while (in_pcblookup(head, 149df8bae1dSRodney W. Grimes zeroin_addr, 0, inp->inp_laddr, lport, wild)); 150df8bae1dSRodney W. Grimes inp->inp_lport = lport; 151df8bae1dSRodney W. Grimes return (0); 152df8bae1dSRodney W. Grimes } 153df8bae1dSRodney W. Grimes 154999f1343SGarrett Wollman #ifdef TTCP 155999f1343SGarrett Wollman /* 156999f1343SGarrett Wollman * Transform old in_pcbconnect() into an inner subroutine for new 157999f1343SGarrett Wollman * in_pcbconnect(): Do some validity-checking on the remote 158999f1343SGarrett Wollman * address (in mbuf 'nam') and then determine local host address 159999f1343SGarrett Wollman * (i.e., which interface) to use to access that remote host. 160999f1343SGarrett Wollman * 161999f1343SGarrett Wollman * This preserves definition of in_pcbconnect(), while supporting a 162999f1343SGarrett Wollman * slightly different version for T/TCP. (This is more than 163999f1343SGarrett Wollman * a bit of a kludge, but cleaning up the internal interfaces would 164999f1343SGarrett Wollman * have forced minor changes in every protocol). 165999f1343SGarrett Wollman */ 166999f1343SGarrett Wollman 167999f1343SGarrett Wollman int 168999f1343SGarrett Wollman in_pcbladdr(inp, nam, plocal_sin) 169999f1343SGarrett Wollman register struct inpcb *inp; 170999f1343SGarrett Wollman struct mbuf *nam; 171999f1343SGarrett Wollman struct sockaddr_in **plocal_sin; 172999f1343SGarrett Wollman { 173999f1343SGarrett Wollman #else /* TTCP */ 174df8bae1dSRodney W. Grimes /* 175df8bae1dSRodney W. Grimes * Connect from a socket to a specified address. 176df8bae1dSRodney W. Grimes * Both address and port must be specified in argument sin. 177df8bae1dSRodney W. Grimes * If don't have a local address for this socket yet, 178df8bae1dSRodney W. Grimes * then pick one. 179df8bae1dSRodney W. Grimes */ 180999f1343SGarrett Wollman 181df8bae1dSRodney W. Grimes int 182df8bae1dSRodney W. Grimes in_pcbconnect(inp, nam) 183df8bae1dSRodney W. Grimes register struct inpcb *inp; 184df8bae1dSRodney W. Grimes struct mbuf *nam; 185df8bae1dSRodney W. Grimes { 186999f1343SGarrett Wollman #endif /* TTCP */ 187df8bae1dSRodney W. Grimes struct in_ifaddr *ia; 18826f9a767SRodney W. Grimes struct sockaddr_in *ifaddr = 0; 189df8bae1dSRodney W. Grimes register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 190df8bae1dSRodney W. Grimes 191df8bae1dSRodney W. Grimes if (nam->m_len != sizeof (*sin)) 192df8bae1dSRodney W. Grimes return (EINVAL); 193df8bae1dSRodney W. Grimes if (sin->sin_family != AF_INET) 194df8bae1dSRodney W. Grimes return (EAFNOSUPPORT); 195df8bae1dSRodney W. Grimes if (sin->sin_port == 0) 196df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 197df8bae1dSRodney W. Grimes if (in_ifaddr) { 198df8bae1dSRodney W. Grimes /* 199df8bae1dSRodney W. Grimes * If the destination address is INADDR_ANY, 200df8bae1dSRodney W. Grimes * use the primary local address. 201df8bae1dSRodney W. Grimes * If the supplied address is INADDR_BROADCAST, 202df8bae1dSRodney W. Grimes * and the primary interface supports broadcast, 203df8bae1dSRodney W. Grimes * choose the broadcast address for that interface. 204df8bae1dSRodney W. Grimes */ 205df8bae1dSRodney W. Grimes #define satosin(sa) ((struct sockaddr_in *)(sa)) 206df8bae1dSRodney W. Grimes #define sintosa(sin) ((struct sockaddr *)(sin)) 207df8bae1dSRodney W. Grimes #define ifatoia(ifa) ((struct in_ifaddr *)(ifa)) 208df8bae1dSRodney W. Grimes if (sin->sin_addr.s_addr == INADDR_ANY) 209df8bae1dSRodney W. Grimes sin->sin_addr = IA_SIN(in_ifaddr)->sin_addr; 210df8bae1dSRodney W. Grimes else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST && 211df8bae1dSRodney W. Grimes (in_ifaddr->ia_ifp->if_flags & IFF_BROADCAST)) 212df8bae1dSRodney W. Grimes sin->sin_addr = satosin(&in_ifaddr->ia_broadaddr)->sin_addr; 213df8bae1dSRodney W. Grimes } 214df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr == INADDR_ANY) { 215df8bae1dSRodney W. Grimes register struct route *ro; 216df8bae1dSRodney W. Grimes 217df8bae1dSRodney W. Grimes ia = (struct in_ifaddr *)0; 218df8bae1dSRodney W. Grimes /* 219df8bae1dSRodney W. Grimes * If route is known or can be allocated now, 220df8bae1dSRodney W. Grimes * our src addr is taken from the i/f, else punt. 221df8bae1dSRodney W. Grimes */ 222df8bae1dSRodney W. Grimes ro = &inp->inp_route; 223df8bae1dSRodney W. Grimes if (ro->ro_rt && 224df8bae1dSRodney W. Grimes (satosin(&ro->ro_dst)->sin_addr.s_addr != 225df8bae1dSRodney W. Grimes sin->sin_addr.s_addr || 226df8bae1dSRodney W. Grimes inp->inp_socket->so_options & SO_DONTROUTE)) { 227df8bae1dSRodney W. Grimes RTFREE(ro->ro_rt); 228df8bae1dSRodney W. Grimes ro->ro_rt = (struct rtentry *)0; 229df8bae1dSRodney W. Grimes } 230df8bae1dSRodney W. Grimes if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/ 231df8bae1dSRodney W. Grimes (ro->ro_rt == (struct rtentry *)0 || 232df8bae1dSRodney W. Grimes ro->ro_rt->rt_ifp == (struct ifnet *)0)) { 233df8bae1dSRodney W. Grimes /* No route yet, so try to acquire one */ 234df8bae1dSRodney W. Grimes ro->ro_dst.sa_family = AF_INET; 235df8bae1dSRodney W. Grimes ro->ro_dst.sa_len = sizeof(struct sockaddr_in); 236df8bae1dSRodney W. Grimes ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 237df8bae1dSRodney W. Grimes sin->sin_addr; 238df8bae1dSRodney W. Grimes rtalloc(ro); 239df8bae1dSRodney W. Grimes } 240df8bae1dSRodney W. Grimes /* 241df8bae1dSRodney W. Grimes * If we found a route, use the address 242df8bae1dSRodney W. Grimes * corresponding to the outgoing interface 243df8bae1dSRodney W. Grimes * unless it is the loopback (in case a route 244df8bae1dSRodney W. Grimes * to our address on another net goes to loopback). 245df8bae1dSRodney W. Grimes */ 246df8bae1dSRodney W. Grimes if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) 247df8bae1dSRodney W. Grimes ia = ifatoia(ro->ro_rt->rt_ifa); 248df8bae1dSRodney W. Grimes if (ia == 0) { 249df8bae1dSRodney W. Grimes u_short fport = sin->sin_port; 250df8bae1dSRodney W. Grimes 251df8bae1dSRodney W. Grimes sin->sin_port = 0; 252df8bae1dSRodney W. Grimes ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin))); 253df8bae1dSRodney W. Grimes if (ia == 0) 254df8bae1dSRodney W. Grimes ia = ifatoia(ifa_ifwithnet(sintosa(sin))); 255df8bae1dSRodney W. Grimes sin->sin_port = fport; 256df8bae1dSRodney W. Grimes if (ia == 0) 257df8bae1dSRodney W. Grimes ia = in_ifaddr; 258df8bae1dSRodney W. Grimes if (ia == 0) 259df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 260df8bae1dSRodney W. Grimes } 261df8bae1dSRodney W. Grimes /* 262df8bae1dSRodney W. Grimes * If the destination address is multicast and an outgoing 263df8bae1dSRodney W. Grimes * interface has been set as a multicast option, use the 264df8bae1dSRodney W. Grimes * address of that interface as our source address. 265df8bae1dSRodney W. Grimes */ 266df8bae1dSRodney W. Grimes if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) && 267df8bae1dSRodney W. Grimes inp->inp_moptions != NULL) { 268df8bae1dSRodney W. Grimes struct ip_moptions *imo; 269df8bae1dSRodney W. Grimes struct ifnet *ifp; 270df8bae1dSRodney W. Grimes 271df8bae1dSRodney W. Grimes imo = inp->inp_moptions; 272df8bae1dSRodney W. Grimes if (imo->imo_multicast_ifp != NULL) { 273df8bae1dSRodney W. Grimes ifp = imo->imo_multicast_ifp; 274df8bae1dSRodney W. Grimes for (ia = in_ifaddr; ia; ia = ia->ia_next) 275df8bae1dSRodney W. Grimes if (ia->ia_ifp == ifp) 276df8bae1dSRodney W. Grimes break; 277df8bae1dSRodney W. Grimes if (ia == 0) 278df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 279df8bae1dSRodney W. Grimes } 280df8bae1dSRodney W. Grimes } 281999f1343SGarrett Wollman #ifdef TTCP 282999f1343SGarrett Wollman /* 283999f1343SGarrett Wollman * Don't do pcblookup call here; return interface in plocal_sin 284999f1343SGarrett Wollman * and exit to caller, that will do the lookup. 285999f1343SGarrett Wollman */ 286999f1343SGarrett Wollman *plocal_sin = &ia->ia_addr; 287999f1343SGarrett Wollman 288999f1343SGarrett Wollman } 289999f1343SGarrett Wollman return(0); 290999f1343SGarrett Wollman } 291999f1343SGarrett Wollman 292999f1343SGarrett Wollman /* 293999f1343SGarrett Wollman * Outer subroutine: 294999f1343SGarrett Wollman * Connect from a socket to a specified address. 295999f1343SGarrett Wollman * Both address and port must be specified in argument sin. 296999f1343SGarrett Wollman * If don't have a local address for this socket yet, 297999f1343SGarrett Wollman * then pick one. 298999f1343SGarrett Wollman */ 299999f1343SGarrett Wollman int 300999f1343SGarrett Wollman in_pcbconnect(inp, nam) 301999f1343SGarrett Wollman register struct inpcb *inp; 302999f1343SGarrett Wollman struct mbuf *nam; 303999f1343SGarrett Wollman { 304999f1343SGarrett Wollman struct sockaddr_in *ifaddr; 305999f1343SGarrett Wollman register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 306999f1343SGarrett Wollman int error; 307999f1343SGarrett Wollman 308999f1343SGarrett Wollman /* 309999f1343SGarrett Wollman * Call inner routine, to assign local interface address. 310999f1343SGarrett Wollman */ 311999f1343SGarrett Wollman if (error = in_pcbladdr(inp, nam, &ifaddr)) 312999f1343SGarrett Wollman return(error); 313999f1343SGarrett Wollman 314999f1343SGarrett Wollman #else /* TTCP */ 315df8bae1dSRodney W. Grimes ifaddr = (struct sockaddr_in *)&ia->ia_addr; 316df8bae1dSRodney W. Grimes } 317999f1343SGarrett Wollman #endif /* TTCP */ 318df8bae1dSRodney W. Grimes if (in_pcblookup(inp->inp_head, 319df8bae1dSRodney W. Grimes sin->sin_addr, 320df8bae1dSRodney W. Grimes sin->sin_port, 321df8bae1dSRodney W. Grimes inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr, 322df8bae1dSRodney W. Grimes inp->inp_lport, 323df8bae1dSRodney W. Grimes 0)) 324df8bae1dSRodney W. Grimes return (EADDRINUSE); 325df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr == INADDR_ANY) { 326df8bae1dSRodney W. Grimes if (inp->inp_lport == 0) 327df8bae1dSRodney W. Grimes (void)in_pcbbind(inp, (struct mbuf *)0); 328df8bae1dSRodney W. Grimes inp->inp_laddr = ifaddr->sin_addr; 329df8bae1dSRodney W. Grimes } 330df8bae1dSRodney W. Grimes inp->inp_faddr = sin->sin_addr; 331df8bae1dSRodney W. Grimes inp->inp_fport = sin->sin_port; 332df8bae1dSRodney W. Grimes return (0); 333df8bae1dSRodney W. Grimes } 334df8bae1dSRodney W. Grimes 33526f9a767SRodney W. Grimes void 336df8bae1dSRodney W. Grimes in_pcbdisconnect(inp) 337df8bae1dSRodney W. Grimes struct inpcb *inp; 338df8bae1dSRodney W. Grimes { 339df8bae1dSRodney W. Grimes 340df8bae1dSRodney W. Grimes inp->inp_faddr.s_addr = INADDR_ANY; 341df8bae1dSRodney W. Grimes inp->inp_fport = 0; 342df8bae1dSRodney W. Grimes if (inp->inp_socket->so_state & SS_NOFDREF) 343df8bae1dSRodney W. Grimes in_pcbdetach(inp); 344df8bae1dSRodney W. Grimes } 345df8bae1dSRodney W. Grimes 34626f9a767SRodney W. Grimes void 347df8bae1dSRodney W. Grimes in_pcbdetach(inp) 348df8bae1dSRodney W. Grimes struct inpcb *inp; 349df8bae1dSRodney W. Grimes { 350df8bae1dSRodney W. Grimes struct socket *so = inp->inp_socket; 351df8bae1dSRodney W. Grimes 352df8bae1dSRodney W. Grimes so->so_pcb = 0; 353df8bae1dSRodney W. Grimes sofree(so); 354df8bae1dSRodney W. Grimes if (inp->inp_options) 355df8bae1dSRodney W. Grimes (void)m_free(inp->inp_options); 356df8bae1dSRodney W. Grimes if (inp->inp_route.ro_rt) 357df8bae1dSRodney W. Grimes rtfree(inp->inp_route.ro_rt); 358df8bae1dSRodney W. Grimes ip_freemoptions(inp->inp_moptions); 359df8bae1dSRodney W. Grimes remque(inp); 360df8bae1dSRodney W. Grimes FREE(inp, M_PCB); 361df8bae1dSRodney W. Grimes } 362df8bae1dSRodney W. Grimes 36326f9a767SRodney W. Grimes void 364df8bae1dSRodney W. Grimes in_setsockaddr(inp, nam) 365df8bae1dSRodney W. Grimes register struct inpcb *inp; 366df8bae1dSRodney W. Grimes struct mbuf *nam; 367df8bae1dSRodney W. Grimes { 368df8bae1dSRodney W. Grimes register struct sockaddr_in *sin; 369df8bae1dSRodney W. Grimes 370df8bae1dSRodney W. Grimes nam->m_len = sizeof (*sin); 371df8bae1dSRodney W. Grimes sin = mtod(nam, struct sockaddr_in *); 372df8bae1dSRodney W. Grimes bzero((caddr_t)sin, sizeof (*sin)); 373df8bae1dSRodney W. Grimes sin->sin_family = AF_INET; 374df8bae1dSRodney W. Grimes sin->sin_len = sizeof(*sin); 375df8bae1dSRodney W. Grimes sin->sin_port = inp->inp_lport; 376df8bae1dSRodney W. Grimes sin->sin_addr = inp->inp_laddr; 377df8bae1dSRodney W. Grimes } 378df8bae1dSRodney W. Grimes 37926f9a767SRodney W. Grimes void 380df8bae1dSRodney W. Grimes in_setpeeraddr(inp, nam) 381df8bae1dSRodney W. Grimes struct inpcb *inp; 382df8bae1dSRodney W. Grimes struct mbuf *nam; 383df8bae1dSRodney W. Grimes { 384df8bae1dSRodney W. Grimes register struct sockaddr_in *sin; 385df8bae1dSRodney W. Grimes 386df8bae1dSRodney W. Grimes nam->m_len = sizeof (*sin); 387df8bae1dSRodney W. Grimes sin = mtod(nam, struct sockaddr_in *); 388df8bae1dSRodney W. Grimes bzero((caddr_t)sin, sizeof (*sin)); 389df8bae1dSRodney W. Grimes sin->sin_family = AF_INET; 390df8bae1dSRodney W. Grimes sin->sin_len = sizeof(*sin); 391df8bae1dSRodney W. Grimes sin->sin_port = inp->inp_fport; 392df8bae1dSRodney W. Grimes sin->sin_addr = inp->inp_faddr; 393df8bae1dSRodney W. Grimes } 394df8bae1dSRodney W. Grimes 395df8bae1dSRodney W. Grimes /* 396df8bae1dSRodney W. Grimes * Pass some notification to all connections of a protocol 397df8bae1dSRodney W. Grimes * associated with address dst. The local address and/or port numbers 398df8bae1dSRodney W. Grimes * may be specified to limit the search. The "usual action" will be 399df8bae1dSRodney W. Grimes * taken, depending on the ctlinput cmd. The caller must filter any 400df8bae1dSRodney W. Grimes * cmds that are uninteresting (e.g., no error in the map). 401df8bae1dSRodney W. Grimes * Call the protocol specific routine (if any) to report 402df8bae1dSRodney W. Grimes * any errors for each matching socket. 403df8bae1dSRodney W. Grimes * 404df8bae1dSRodney W. Grimes * Must be called at splnet. 405df8bae1dSRodney W. Grimes */ 40626f9a767SRodney W. Grimes void 407df8bae1dSRodney W. Grimes in_pcbnotify(head, dst, fport_arg, laddr, lport_arg, cmd, notify) 408df8bae1dSRodney W. Grimes struct inpcb *head; 409df8bae1dSRodney W. Grimes struct sockaddr *dst; 410df8bae1dSRodney W. Grimes u_int fport_arg, lport_arg; 411df8bae1dSRodney W. Grimes struct in_addr laddr; 412df8bae1dSRodney W. Grimes int cmd; 413df8bae1dSRodney W. Grimes void (*notify) __P((struct inpcb *, int)); 414df8bae1dSRodney W. Grimes { 415df8bae1dSRodney W. Grimes extern u_char inetctlerrmap[]; 416df8bae1dSRodney W. Grimes register struct inpcb *inp, *oinp; 417df8bae1dSRodney W. Grimes struct in_addr faddr; 418df8bae1dSRodney W. Grimes u_short fport = fport_arg, lport = lport_arg; 419df8bae1dSRodney W. Grimes int errno; 420df8bae1dSRodney W. Grimes 421df8bae1dSRodney W. Grimes if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET) 422df8bae1dSRodney W. Grimes return; 423df8bae1dSRodney W. Grimes faddr = ((struct sockaddr_in *)dst)->sin_addr; 424df8bae1dSRodney W. Grimes if (faddr.s_addr == INADDR_ANY) 425df8bae1dSRodney W. Grimes return; 426df8bae1dSRodney W. Grimes 427df8bae1dSRodney W. Grimes /* 428df8bae1dSRodney W. Grimes * Redirects go to all references to the destination, 429df8bae1dSRodney W. Grimes * and use in_rtchange to invalidate the route cache. 430df8bae1dSRodney W. Grimes * Dead host indications: notify all references to the destination. 431df8bae1dSRodney W. Grimes * Otherwise, if we have knowledge of the local port and address, 432df8bae1dSRodney W. Grimes * deliver only to that socket. 433df8bae1dSRodney W. Grimes */ 434df8bae1dSRodney W. Grimes if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { 435df8bae1dSRodney W. Grimes fport = 0; 436df8bae1dSRodney W. Grimes lport = 0; 437df8bae1dSRodney W. Grimes laddr.s_addr = 0; 438df8bae1dSRodney W. Grimes if (cmd != PRC_HOSTDEAD) 439df8bae1dSRodney W. Grimes notify = in_rtchange; 440df8bae1dSRodney W. Grimes } 441df8bae1dSRodney W. Grimes errno = inetctlerrmap[cmd]; 442df8bae1dSRodney W. Grimes for (inp = head->inp_next; inp != head;) { 443df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != faddr.s_addr || 444df8bae1dSRodney W. Grimes inp->inp_socket == 0 || 445df8bae1dSRodney W. Grimes (lport && inp->inp_lport != lport) || 446df8bae1dSRodney W. Grimes (laddr.s_addr && inp->inp_laddr.s_addr != laddr.s_addr) || 447df8bae1dSRodney W. Grimes (fport && inp->inp_fport != fport)) { 448df8bae1dSRodney W. Grimes inp = inp->inp_next; 449df8bae1dSRodney W. Grimes continue; 450df8bae1dSRodney W. Grimes } 451df8bae1dSRodney W. Grimes oinp = inp; 452df8bae1dSRodney W. Grimes inp = inp->inp_next; 453df8bae1dSRodney W. Grimes if (notify) 454df8bae1dSRodney W. Grimes (*notify)(oinp, errno); 455df8bae1dSRodney W. Grimes } 456df8bae1dSRodney W. Grimes } 457df8bae1dSRodney W. Grimes 458df8bae1dSRodney W. Grimes /* 459df8bae1dSRodney W. Grimes * Check for alternatives when higher level complains 460df8bae1dSRodney W. Grimes * about service problems. For now, invalidate cached 461df8bae1dSRodney W. Grimes * routing information. If the route was created dynamically 462df8bae1dSRodney W. Grimes * (by a redirect), time to try a default gateway again. 463df8bae1dSRodney W. Grimes */ 46426f9a767SRodney W. Grimes void 465df8bae1dSRodney W. Grimes in_losing(inp) 466df8bae1dSRodney W. Grimes struct inpcb *inp; 467df8bae1dSRodney W. Grimes { 468df8bae1dSRodney W. Grimes register struct rtentry *rt; 469df8bae1dSRodney W. Grimes struct rt_addrinfo info; 470df8bae1dSRodney W. Grimes 471df8bae1dSRodney W. Grimes if ((rt = inp->inp_route.ro_rt)) { 472df8bae1dSRodney W. Grimes inp->inp_route.ro_rt = 0; 473df8bae1dSRodney W. Grimes bzero((caddr_t)&info, sizeof(info)); 474df8bae1dSRodney W. Grimes info.rti_info[RTAX_DST] = 475df8bae1dSRodney W. Grimes (struct sockaddr *)&inp->inp_route.ro_dst; 476df8bae1dSRodney W. Grimes info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 477df8bae1dSRodney W. Grimes info.rti_info[RTAX_NETMASK] = rt_mask(rt); 478df8bae1dSRodney W. Grimes rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); 479df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_DYNAMIC) 480df8bae1dSRodney W. Grimes (void) rtrequest(RTM_DELETE, rt_key(rt), 481df8bae1dSRodney W. Grimes rt->rt_gateway, rt_mask(rt), rt->rt_flags, 482df8bae1dSRodney W. Grimes (struct rtentry **)0); 483df8bae1dSRodney W. Grimes else 484df8bae1dSRodney W. Grimes /* 485df8bae1dSRodney W. Grimes * A new route can be allocated 486df8bae1dSRodney W. Grimes * the next time output is attempted. 487df8bae1dSRodney W. Grimes */ 488df8bae1dSRodney W. Grimes rtfree(rt); 489df8bae1dSRodney W. Grimes } 490df8bae1dSRodney W. Grimes } 491df8bae1dSRodney W. Grimes 492df8bae1dSRodney W. Grimes /* 493df8bae1dSRodney W. Grimes * After a routing change, flush old routing 494df8bae1dSRodney W. Grimes * and allocate a (hopefully) better one. 495df8bae1dSRodney W. Grimes */ 496df8bae1dSRodney W. Grimes void 497df8bae1dSRodney W. Grimes in_rtchange(inp, errno) 498df8bae1dSRodney W. Grimes register struct inpcb *inp; 499df8bae1dSRodney W. Grimes int errno; 500df8bae1dSRodney W. Grimes { 501df8bae1dSRodney W. Grimes if (inp->inp_route.ro_rt) { 502df8bae1dSRodney W. Grimes rtfree(inp->inp_route.ro_rt); 503df8bae1dSRodney W. Grimes inp->inp_route.ro_rt = 0; 504df8bae1dSRodney W. Grimes /* 505df8bae1dSRodney W. Grimes * A new route can be allocated the next time 506df8bae1dSRodney W. Grimes * output is attempted. 507df8bae1dSRodney W. Grimes */ 508df8bae1dSRodney W. Grimes } 509df8bae1dSRodney W. Grimes } 510df8bae1dSRodney W. Grimes 511df8bae1dSRodney W. Grimes struct inpcb * 512df8bae1dSRodney W. Grimes in_pcblookup(head, faddr, fport_arg, laddr, lport_arg, flags) 513df8bae1dSRodney W. Grimes struct inpcb *head; 514df8bae1dSRodney W. Grimes struct in_addr faddr, laddr; 515df8bae1dSRodney W. Grimes u_int fport_arg, lport_arg; 516df8bae1dSRodney W. Grimes int flags; 517df8bae1dSRodney W. Grimes { 518df8bae1dSRodney W. Grimes register struct inpcb *inp, *match = 0; 519df8bae1dSRodney W. Grimes int matchwild = 3, wildcard; 520df8bae1dSRodney W. Grimes u_short fport = fport_arg, lport = lport_arg; 521df8bae1dSRodney W. Grimes 522df8bae1dSRodney W. Grimes for (inp = head->inp_next; inp != head; inp = inp->inp_next) { 523df8bae1dSRodney W. Grimes if (inp->inp_lport != lport) 524df8bae1dSRodney W. Grimes continue; 525df8bae1dSRodney W. Grimes wildcard = 0; 526df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr != INADDR_ANY) { 527df8bae1dSRodney W. Grimes if (laddr.s_addr == INADDR_ANY) 528df8bae1dSRodney W. Grimes wildcard++; 529df8bae1dSRodney W. Grimes else if (inp->inp_laddr.s_addr != laddr.s_addr) 530df8bae1dSRodney W. Grimes continue; 531df8bae1dSRodney W. Grimes } else { 532df8bae1dSRodney W. Grimes if (laddr.s_addr != INADDR_ANY) 533df8bae1dSRodney W. Grimes wildcard++; 534df8bae1dSRodney W. Grimes } 535df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr != INADDR_ANY) { 536df8bae1dSRodney W. Grimes if (faddr.s_addr == INADDR_ANY) 537df8bae1dSRodney W. Grimes wildcard++; 538df8bae1dSRodney W. Grimes else if (inp->inp_faddr.s_addr != faddr.s_addr || 539df8bae1dSRodney W. Grimes inp->inp_fport != fport) 540df8bae1dSRodney W. Grimes continue; 541df8bae1dSRodney W. Grimes } else { 542df8bae1dSRodney W. Grimes if (faddr.s_addr != INADDR_ANY) 543df8bae1dSRodney W. Grimes wildcard++; 544df8bae1dSRodney W. Grimes } 545df8bae1dSRodney W. Grimes if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0) 546df8bae1dSRodney W. Grimes continue; 547df8bae1dSRodney W. Grimes if (wildcard < matchwild) { 548df8bae1dSRodney W. Grimes match = inp; 549df8bae1dSRodney W. Grimes matchwild = wildcard; 550df8bae1dSRodney W. Grimes if (matchwild == 0) 551df8bae1dSRodney W. Grimes break; 552df8bae1dSRodney W. Grimes } 553df8bae1dSRodney W. Grimes } 554df8bae1dSRodney W. Grimes return (match); 555df8bae1dSRodney W. Grimes } 556