xref: /csrg-svn/sys/netinet/in_pcb.c (revision 6028)
1*6028Sroot /*	in_pcb.c	4.18	82/03/03	*/
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"
144905Swnj 
154951Swnj /*
165161Swnj  * Routines to manage internet protocol control blocks.
175161Swnj  *
185161Swnj  * At PRU_ATTACH time a protocol control block is allocated in
195161Swnj  * in_pcballoc() and inserted on a doubly-linked list of such blocks
205161Swnj  * for the protocol.  A port address is either requested (and verified
215161Swnj  * to not be in use) or assigned at this time.  We also allocate
225161Swnj  * space in the socket sockbuf structures here, although this is
235161Swnj  * not a clearly correct place to put this function.
245161Swnj  *
255161Swnj  * A connectionless protocol will have its protocol control block
265161Swnj  * removed at PRU_DETACH time, when the socket will be freed (freeing
275161Swnj  * the space reserved) and the block will be removed from the list of
285161Swnj  * blocks for its protocol.
295161Swnj  *
305161Swnj  * A connection-based protocol may be connected to a remote peer at
315161Swnj  * PRU_CONNECT time through the routine in_pcbconnect().  In the normal
325161Swnj  * case a PRU_DISCONNECT occurs causing a in_pcbdisconnect().
335161Swnj  * It is also possible that higher-level routines will opt out of the
345161Swnj  * relationship with the connection before the connection shut down
355161Swnj  * is complete.  This often occurs in protocols like TCP where we must
365161Swnj  * hold on to the protocol control block for a unreasonably long time
375161Swnj  * after the connection is used up to avoid races in later connection
385161Swnj  * establishment.  To handle this we allow higher-level routines to
395161Swnj  * disassociate themselves from the socket, marking it SS_USERGONE while
405161Swnj  * the disconnect is in progress.  We notice that this has happened
415161Swnj  * when the disconnect is complete, and perform the PRU_DETACH operation,
425161Swnj  * freeing the socket.
435172Swnj  *
445172Swnj  * TODO:
455172Swnj  *	use hashing
465161Swnj  */
475240Sroot struct	in_addr zeroin_addr;
485161Swnj 
495161Swnj /*
504951Swnj  * Allocate a protocol control block, space
514951Swnj  * for send and receive data, and local host information.
524951Swnj  * Return error.  If no error make socket point at pcb.
534951Swnj  */
545161Swnj in_pcbattach(so, head, sndcc, rcvcc, sin)
554951Swnj 	struct socket *so;
564951Swnj 	struct inpcb *head;
574951Swnj 	int sndcc, rcvcc;
584951Swnj 	struct sockaddr_in *sin;
594905Swnj {
604905Swnj 	struct mbuf *m;
615240Sroot 	register struct inpcb *inp;
625994Swnj 	u_short lport = 0;
634905Swnj 
645161Swnj COUNT(IN_PCBATTACH);
655994Swnj 	if (ifnet == 0)
665994Swnj 		return (EADDRNOTAVAIL);
674951Swnj 	if (sin) {
684951Swnj 		if (sin->sin_family != AF_INET)
694951Swnj 			return (EAFNOSUPPORT);
705994Swnj 		if (sin->sin_addr.s_addr &&
715994Swnj 		    if_ifwithaddr(sin->sin_addr.s_addr) == 0)
725994Swnj 			return (EADDRNOTAVAIL);
734951Swnj 		lport = sin->sin_port;
745994Swnj 		if (lport) {
755994Swnj 			u_short aport = lport;
765994Swnj #if vax
775994Swnj 			aport = htons(aport);
785994Swnj #endif
795994Swnj 			/* GROSS */
805994Swnj 			if (aport < IPPORT_RESERVED && u.u_uid != 0)
815994Swnj 				return (EPERM);
825994Swnj 			if (in_pcblookup(head,
835994Swnj 			    zeroin_addr, 0, sin->sin_addr, lport, 0))
845994Swnj 				return (EADDRINUSE);
855994Swnj 		}
864951Swnj 	}
875852Sroot 	m = m_getclr(M_DONTWAIT);
884951Swnj 	if (m == 0)
894983Swnj 		return (ENOBUFS);
904951Swnj 	if (sbreserve(&so->so_snd, sndcc) == 0)
914951Swnj 		goto bad;
924951Swnj 	if (sbreserve(&so->so_rcv, rcvcc) == 0)
934951Swnj 		goto bad2;
944951Swnj 	inp = mtod(m, struct inpcb *);
955172Swnj 	inp->inp_head = head;
965994Swnj 	if (sin)
975994Swnj 		inp->inp_laddr = sin->sin_addr;
985172Swnj 	if (lport == 0)
995172Swnj 		do {
1005994Swnj 			if (head->inp_lport++ < IPPORT_RESERVED)
1015994Swnj 				head->inp_lport = IPPORT_RESERVED;
1025172Swnj 			lport = htons(head->inp_lport);
1035994Swnj 		} while (in_pcblookup(head,
1045994Swnj 			    zeroin_addr, 0, inp->inp_laddr, lport, 0));
1055172Swnj 	inp->inp_lport = lport;
1064983Swnj 	inp->inp_socket = so;
1074983Swnj 	insque(inp, head);
1084951Swnj 	so->so_pcb = (caddr_t)inp;
1095994Swnj 	in_setsockaddr(inp);
1104951Swnj 	return (0);
1114951Swnj bad2:
1124951Swnj 	sbrelease(&so->so_snd);
1134951Swnj bad:
1144967Swnj 	(void) m_free(m);
1154951Swnj 	return (ENOBUFS);
1164905Swnj }
1174905Swnj 
1185161Swnj in_pcbconnect(inp, sin)
1194951Swnj 	struct inpcb *inp;
1204951Swnj 	struct sockaddr_in *sin;
1214923Swnj {
1225994Swnj 	struct ifnet *ifp;
1234923Swnj 
1245161Swnj COUNT(IN_PCBCONNECT);
1254951Swnj 	if (sin->sin_family != AF_INET)
1264951Swnj 		return (EAFNOSUPPORT);
1274951Swnj 	if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0)
1284951Swnj 		return (EADDRNOTAVAIL);
1295994Swnj 	if (inp->inp_laddr.s_addr == 0) {
1305994Swnj 		ifp = if_ifonnetof(sin->sin_addr.s_addr);
1315994Swnj 		if (ifp == 0)
1325994Swnj 			ifp = ifnet;
1335994Swnj 	}
1345994Swnj 	if (in_pcblookup(inp->inp_head,
1355994Swnj 	    sin->sin_addr, sin->sin_port, inp->inp_laddr, inp->inp_lport, 0))
1365172Swnj 		return (EADDRINUSE);
137*6028Sroot 	if (inp->inp_laddr.s_addr == 0)
138*6028Sroot 		inp->inp_laddr = ifp->if_addr;
1394951Swnj 	inp->inp_faddr = sin->sin_addr;
1404951Swnj 	inp->inp_fport = sin->sin_port;
1414923Swnj 	return (0);
1424923Swnj }
1434923Swnj 
1445994Swnj in_setsockaddr(inp)
1455277Sroot 	struct inpcb *inp;
1465277Sroot {
1475994Swnj 	register struct sockaddr_in *sin =
1485994Swnj 	    (struct sockaddr_in *)&inp->inp_socket->so_addr;
1495277Sroot 
1505277Sroot 	sin->sin_family = AF_INET;
1515994Swnj 	sin->sin_addr = inp->inp_laddr;
1525994Swnj 	sin->sin_port = inp->inp_lport;
1535277Sroot }
1545277Sroot 
1555161Swnj in_pcbdisconnect(inp)
1564905Swnj 	struct inpcb *inp;
1574905Swnj {
1585161Swnj 
1595161Swnj COUNT(IN_PCBDISCONNECT);
1605161Swnj 	inp->inp_faddr.s_addr = 0;
161*6028Sroot 	inp->inp_fport = 0;
1625161Swnj 	if (inp->inp_socket->so_state & SS_USERGONE)
1635161Swnj 		in_pcbdetach(inp);
1645161Swnj }
1655161Swnj 
1665161Swnj in_pcbdetach(inp)
1675161Swnj 	struct inpcb *inp;
1685161Swnj {
1694905Swnj 	struct socket *so = inp->inp_socket;
1704905Swnj 
1715009Swnj 	so->so_pcb = 0;
1725009Swnj 	sofree(so);
1734983Swnj 	remque(inp);
1744907Swnj 	(void) m_free(dtom(inp));
1754905Swnj }
1764905Swnj 
1775161Swnj /*
1785161Swnj  * Look for a control block to accept a segment.
1795161Swnj  * First choice is an exact address match.
1805994Swnj  * Second choice is a match with either the foreign or the local
1815994Swnj  * address specified.
1825994Swnj  *
1835994Swnj  * SHOULD ALLOW MATCH ON MULTI-HOMING ONLY
1845161Swnj  */
1854907Swnj struct inpcb *
186*6028Sroot in_pcblookup(head, faddr, fport, laddr, lport, flags)
1874905Swnj 	struct inpcb *head;
1884951Swnj 	struct in_addr faddr, laddr;
1894905Swnj 	u_short fport, lport;
190*6028Sroot 	int flags;
1914905Swnj {
1925994Swnj 	register struct inpcb *inp, *match = 0;
1935994Swnj 	int matchwild = 3, wildcard;
1944905Swnj 
1955161Swnj 	for (inp = head->inp_next; inp != head; inp = inp->inp_next) {
1965994Swnj 		if (inp->inp_lport != lport)
1975161Swnj 			continue;
1985994Swnj 		wildcard = 0;
1995994Swnj 		if (inp->inp_laddr.s_addr != 0) {
2005994Swnj 			if (inp->inp_laddr.s_addr != laddr.s_addr)
2015994Swnj 				continue;
2025994Swnj 		} else {
2035994Swnj 			if (laddr.s_addr != 0)
2045994Swnj 				wildcard++;
2055994Swnj 		}
2065994Swnj 		if (inp->inp_faddr.s_addr != 0) {
207*6028Sroot 			if (inp->inp_faddr.s_addr != faddr.s_addr ||
208*6028Sroot 			    inp->inp_fport != fport)
2095994Swnj 				continue;
2105994Swnj 		} else {
2115994Swnj 			if (faddr.s_addr != 0)
2125994Swnj 				wildcard++;
2135994Swnj 		}
214*6028Sroot 		if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
2155994Swnj 			continue;
2165994Swnj 		if (wildcard < matchwild) {
2175161Swnj 			match = inp;
2185994Swnj 			matchwild = wildcard;
2195994Swnj 			if (matchwild == 0)
2205994Swnj 				break;
2215161Swnj 		}
2225161Swnj 	}
2235161Swnj 	return (match);
2244905Swnj }
225