xref: /csrg-svn/sys/netinet/in_pcb.c (revision 6297)
1*6297Swnj /*	in_pcb.c	4.21	82/03/23	*/
24905Swnj 
34905Swnj #include "../h/param.h"
44951Swnj #include "../h/systm.h"
54951Swnj #include "../h/dir.h"
64951Swnj #include "../h/user.h"
74905Swnj #include "../h/mbuf.h"
84905Swnj #include "../h/socket.h"
94905Swnj #include "../h/socketvar.h"
105084Swnj #include "../net/in.h"
115084Swnj #include "../net/in_systm.h"
124951Swnj #include "../net/if.h"
135084Swnj #include "../net/in_pcb.h"
146116Swnj #include "../h/protosw.h"
154905Swnj 
164951Swnj /*
175161Swnj  * Routines to manage internet protocol control blocks.
185161Swnj  *
195161Swnj  * At PRU_ATTACH time a protocol control block is allocated in
205161Swnj  * in_pcballoc() and inserted on a doubly-linked list of such blocks
215161Swnj  * for the protocol.  A port address is either requested (and verified
225161Swnj  * to not be in use) or assigned at this time.  We also allocate
235161Swnj  * space in the socket sockbuf structures here, although this is
245161Swnj  * not a clearly correct place to put this function.
255161Swnj  *
265161Swnj  * A connectionless protocol will have its protocol control block
275161Swnj  * removed at PRU_DETACH time, when the socket will be freed (freeing
285161Swnj  * the space reserved) and the block will be removed from the list of
295161Swnj  * blocks for its protocol.
305161Swnj  *
315161Swnj  * A connection-based protocol may be connected to a remote peer at
325161Swnj  * PRU_CONNECT time through the routine in_pcbconnect().  In the normal
335161Swnj  * case a PRU_DISCONNECT occurs causing a in_pcbdisconnect().
345161Swnj  * It is also possible that higher-level routines will opt out of the
355161Swnj  * relationship with the connection before the connection shut down
365161Swnj  * is complete.  This often occurs in protocols like TCP where we must
375161Swnj  * hold on to the protocol control block for a unreasonably long time
385161Swnj  * after the connection is used up to avoid races in later connection
395161Swnj  * establishment.  To handle this we allow higher-level routines to
405161Swnj  * disassociate themselves from the socket, marking it SS_USERGONE while
415161Swnj  * the disconnect is in progress.  We notice that this has happened
425161Swnj  * when the disconnect is complete, and perform the PRU_DETACH operation,
435161Swnj  * freeing the socket.
445172Swnj  *
455172Swnj  * TODO:
465172Swnj  *	use hashing
475161Swnj  */
485240Sroot struct	in_addr zeroin_addr;
495161Swnj 
505161Swnj /*
514951Swnj  * Allocate a protocol control block, space
524951Swnj  * for send and receive data, and local host information.
534951Swnj  * Return error.  If no error make socket point at pcb.
544951Swnj  */
555161Swnj in_pcbattach(so, head, sndcc, rcvcc, sin)
564951Swnj 	struct socket *so;
574951Swnj 	struct inpcb *head;
584951Swnj 	int sndcc, rcvcc;
594951Swnj 	struct sockaddr_in *sin;
604905Swnj {
614905Swnj 	struct mbuf *m;
625240Sroot 	register struct inpcb *inp;
635994Swnj 	u_short lport = 0;
644905Swnj 
655161Swnj COUNT(IN_PCBATTACH);
665994Swnj 	if (ifnet == 0)
675994Swnj 		return (EADDRNOTAVAIL);
684951Swnj 	if (sin) {
694951Swnj 		if (sin->sin_family != AF_INET)
704951Swnj 			return (EAFNOSUPPORT);
715994Swnj 		if (sin->sin_addr.s_addr &&
726161Ssam 		    if_ifwithaddr(sin->sin_addr) == 0)
735994Swnj 			return (EADDRNOTAVAIL);
744951Swnj 		lport = sin->sin_port;
755994Swnj 		if (lport) {
765994Swnj 			u_short aport = lport;
776116Swnj 			int wild = 0;
785994Swnj #if vax
795994Swnj 			aport = htons(aport);
805994Swnj #endif
815994Swnj 			/* GROSS */
825994Swnj 			if (aport < IPPORT_RESERVED && u.u_uid != 0)
835994Swnj 				return (EPERM);
846116Swnj 			if ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
856116Swnj 			    (so->so_options & SO_ACCEPTCONN) == 0)
866116Swnj 				wild = INPLOOKUP_WILDCARD;
875994Swnj 			if (in_pcblookup(head,
886116Swnj 			    zeroin_addr, 0, sin->sin_addr, lport, wild))
895994Swnj 				return (EADDRINUSE);
905994Swnj 		}
914951Swnj 	}
925852Sroot 	m = m_getclr(M_DONTWAIT);
934951Swnj 	if (m == 0)
944983Swnj 		return (ENOBUFS);
954951Swnj 	if (sbreserve(&so->so_snd, sndcc) == 0)
964951Swnj 		goto bad;
974951Swnj 	if (sbreserve(&so->so_rcv, rcvcc) == 0)
984951Swnj 		goto bad2;
994951Swnj 	inp = mtod(m, struct inpcb *);
1005172Swnj 	inp->inp_head = head;
1015994Swnj 	if (sin)
1025994Swnj 		inp->inp_laddr = sin->sin_addr;
1035172Swnj 	if (lport == 0)
1045172Swnj 		do {
1055994Swnj 			if (head->inp_lport++ < IPPORT_RESERVED)
1065994Swnj 				head->inp_lport = IPPORT_RESERVED;
1075172Swnj 			lport = htons(head->inp_lport);
1085994Swnj 		} while (in_pcblookup(head,
1095994Swnj 			    zeroin_addr, 0, inp->inp_laddr, lport, 0));
1105172Swnj 	inp->inp_lport = lport;
1114983Swnj 	inp->inp_socket = so;
1124983Swnj 	insque(inp, head);
1134951Swnj 	so->so_pcb = (caddr_t)inp;
1145994Swnj 	in_setsockaddr(inp);
1154951Swnj 	return (0);
1164951Swnj bad2:
1174951Swnj 	sbrelease(&so->so_snd);
1184951Swnj bad:
1194967Swnj 	(void) m_free(m);
1204951Swnj 	return (ENOBUFS);
1214905Swnj }
1224905Swnj 
1236116Swnj /*
1246116Swnj  * Connect from a socket to a specified address.
1256116Swnj  * Both address and port must be specified in argument sin.
1266116Swnj  * If don't have a local address for this socket yet,
1276116Swnj  * then pick one.
1286116Swnj  */
1295161Swnj in_pcbconnect(inp, sin)
1304951Swnj 	struct inpcb *inp;
1314951Swnj 	struct sockaddr_in *sin;
1324923Swnj {
1335994Swnj 	struct ifnet *ifp;
1344923Swnj 
1355161Swnj COUNT(IN_PCBCONNECT);
1364951Swnj 	if (sin->sin_family != AF_INET)
1374951Swnj 		return (EAFNOSUPPORT);
1384951Swnj 	if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0)
1394951Swnj 		return (EADDRNOTAVAIL);
1405994Swnj 	if (inp->inp_laddr.s_addr == 0) {
1416161Ssam 		ifp = if_ifonnetof(sin->sin_addr);
1425994Swnj 		if (ifp == 0)
1435994Swnj 			ifp = ifnet;
1445994Swnj 	}
1455994Swnj 	if (in_pcblookup(inp->inp_head,
1466116Swnj 	    sin->sin_addr,
1476116Swnj 	    sin->sin_port,
1486116Swnj 	    inp->inp_laddr.s_addr ? inp->inp_laddr : ifp->if_addr,
1496116Swnj 	    inp->inp_lport,
1506116Swnj 	    0))
1515172Swnj 		return (EADDRINUSE);
152*6297Swnj 	if (inp->inp_laddr.s_addr == 0) {
153*6297Swnj 		struct sockaddr_in *sin2 =
154*6297Swnj 		    (struct sockaddr_in *)&inp->inp_socket->so_addr;
155*6297Swnj 
1566028Sroot 		inp->inp_laddr = ifp->if_addr;
157*6297Swnj 		sin2->sin_addr = inp->inp_laddr;
158*6297Swnj 	}
1594951Swnj 	inp->inp_faddr = sin->sin_addr;
1604951Swnj 	inp->inp_fport = sin->sin_port;
1614923Swnj 	return (0);
1624923Swnj }
1634923Swnj 
1645994Swnj in_setsockaddr(inp)
1655277Sroot 	struct inpcb *inp;
1665277Sroot {
1675994Swnj 	register struct sockaddr_in *sin =
1685994Swnj 	    (struct sockaddr_in *)&inp->inp_socket->so_addr;
1695277Sroot 
1705277Sroot 	sin->sin_family = AF_INET;
1715994Swnj 	sin->sin_addr = inp->inp_laddr;
1725994Swnj 	sin->sin_port = inp->inp_lport;
1735277Sroot }
1745277Sroot 
1755161Swnj in_pcbdisconnect(inp)
1764905Swnj 	struct inpcb *inp;
1774905Swnj {
1785161Swnj 
1795161Swnj COUNT(IN_PCBDISCONNECT);
1805161Swnj 	inp->inp_faddr.s_addr = 0;
1816028Sroot 	inp->inp_fport = 0;
1825161Swnj 	if (inp->inp_socket->so_state & SS_USERGONE)
1835161Swnj 		in_pcbdetach(inp);
1845161Swnj }
1855161Swnj 
1865161Swnj in_pcbdetach(inp)
1875161Swnj 	struct inpcb *inp;
1885161Swnj {
1894905Swnj 	struct socket *so = inp->inp_socket;
1904905Swnj 
1915009Swnj 	so->so_pcb = 0;
1925009Swnj 	sofree(so);
1934983Swnj 	remque(inp);
1944907Swnj 	(void) m_free(dtom(inp));
1954905Swnj }
1964905Swnj 
1975161Swnj /*
1985994Swnj  * SHOULD ALLOW MATCH ON MULTI-HOMING ONLY
1995161Swnj  */
2004907Swnj struct inpcb *
2016028Sroot in_pcblookup(head, faddr, fport, laddr, lport, flags)
2024905Swnj 	struct inpcb *head;
2034951Swnj 	struct in_addr faddr, laddr;
2044905Swnj 	u_short fport, lport;
2056028Sroot 	int flags;
2064905Swnj {
2075994Swnj 	register struct inpcb *inp, *match = 0;
2085994Swnj 	int matchwild = 3, wildcard;
2094905Swnj 
2105161Swnj 	for (inp = head->inp_next; inp != head; inp = inp->inp_next) {
2115994Swnj 		if (inp->inp_lport != lport)
2125161Swnj 			continue;
2135994Swnj 		wildcard = 0;
2145994Swnj 		if (inp->inp_laddr.s_addr != 0) {
2156116Swnj 			if (laddr.s_addr == 0)
2166116Swnj 				wildcard++;
2176116Swnj 			else if (inp->inp_laddr.s_addr != laddr.s_addr)
2185994Swnj 				continue;
2195994Swnj 		} else {
2205994Swnj 			if (laddr.s_addr != 0)
2215994Swnj 				wildcard++;
2225994Swnj 		}
2235994Swnj 		if (inp->inp_faddr.s_addr != 0) {
2246116Swnj 			if (faddr.s_addr == 0)
2256116Swnj 				wildcard++;
2266116Swnj 			else if (inp->inp_faddr.s_addr != faddr.s_addr ||
2276028Sroot 			    inp->inp_fport != fport)
2285994Swnj 				continue;
2295994Swnj 		} else {
2305994Swnj 			if (faddr.s_addr != 0)
2315994Swnj 				wildcard++;
2325994Swnj 		}
2336028Sroot 		if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
2345994Swnj 			continue;
2355994Swnj 		if (wildcard < matchwild) {
2365161Swnj 			match = inp;
2375994Swnj 			matchwild = wildcard;
2385994Swnj 			if (matchwild == 0)
2395994Swnj 				break;
2405161Swnj 		}
2415161Swnj 	}
2425161Swnj 	return (match);
2434905Swnj }
244