xref: /csrg-svn/sys/netinet/in_pcb.c (revision 6350)
1*6350Ssam /*	in_pcb.c	4.23	82/03/29	*/
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"
13*6350Ssam #include "../net/route.h"
145084Swnj #include "../net/in_pcb.h"
156116Swnj #include "../h/protosw.h"
164905Swnj 
174951Swnj /*
185161Swnj  * Routines to manage internet protocol control blocks.
195161Swnj  *
205161Swnj  * At PRU_ATTACH time a protocol control block is allocated in
215161Swnj  * in_pcballoc() and inserted on a doubly-linked list of such blocks
225161Swnj  * for the protocol.  A port address is either requested (and verified
235161Swnj  * to not be in use) or assigned at this time.  We also allocate
245161Swnj  * space in the socket sockbuf structures here, although this is
255161Swnj  * not a clearly correct place to put this function.
265161Swnj  *
275161Swnj  * A connectionless protocol will have its protocol control block
285161Swnj  * removed at PRU_DETACH time, when the socket will be freed (freeing
295161Swnj  * the space reserved) and the block will be removed from the list of
305161Swnj  * blocks for its protocol.
315161Swnj  *
325161Swnj  * A connection-based protocol may be connected to a remote peer at
335161Swnj  * PRU_CONNECT time through the routine in_pcbconnect().  In the normal
345161Swnj  * case a PRU_DISCONNECT occurs causing a in_pcbdisconnect().
355161Swnj  * It is also possible that higher-level routines will opt out of the
365161Swnj  * relationship with the connection before the connection shut down
375161Swnj  * is complete.  This often occurs in protocols like TCP where we must
385161Swnj  * hold on to the protocol control block for a unreasonably long time
395161Swnj  * after the connection is used up to avoid races in later connection
405161Swnj  * establishment.  To handle this we allow higher-level routines to
415161Swnj  * disassociate themselves from the socket, marking it SS_USERGONE while
425161Swnj  * the disconnect is in progress.  We notice that this has happened
435161Swnj  * when the disconnect is complete, and perform the PRU_DETACH operation,
445161Swnj  * freeing the socket.
455172Swnj  *
465172Swnj  * TODO:
475172Swnj  *	use hashing
485161Swnj  */
495240Sroot struct	in_addr zeroin_addr;
505161Swnj 
515161Swnj /*
524951Swnj  * Allocate a protocol control block, space
534951Swnj  * for send and receive data, and local host information.
544951Swnj  * Return error.  If no error make socket point at pcb.
554951Swnj  */
565161Swnj in_pcbattach(so, head, sndcc, rcvcc, sin)
574951Swnj 	struct socket *so;
584951Swnj 	struct inpcb *head;
594951Swnj 	int sndcc, rcvcc;
604951Swnj 	struct sockaddr_in *sin;
614905Swnj {
624905Swnj 	struct mbuf *m;
635240Sroot 	register struct inpcb *inp;
645994Swnj 	u_short lport = 0;
654905Swnj 
665161Swnj COUNT(IN_PCBATTACH);
675994Swnj 	if (ifnet == 0)
685994Swnj 		return (EADDRNOTAVAIL);
694951Swnj 	if (sin) {
704951Swnj 		if (sin->sin_family != AF_INET)
714951Swnj 			return (EAFNOSUPPORT);
72*6350Ssam 		if (sin->sin_addr.s_addr) {
73*6350Ssam 			int tport = sin->sin_port;
74*6350Ssam 
75*6350Ssam 			sin->sin_port = 0;		/* yech... */
76*6350Ssam 			if (if_ifwithaddr((struct sockaddr *)sin) == 0)
77*6350Ssam 				return (EADDRNOTAVAIL);
78*6350Ssam 			sin->sin_port = tport;
79*6350Ssam 		}
804951Swnj 		lport = sin->sin_port;
815994Swnj 		if (lport) {
825994Swnj 			u_short aport = lport;
836116Swnj 			int wild = 0;
845994Swnj #if vax
855994Swnj 			aport = htons(aport);
865994Swnj #endif
875994Swnj 			/* GROSS */
885994Swnj 			if (aport < IPPORT_RESERVED && u.u_uid != 0)
895994Swnj 				return (EPERM);
906116Swnj 			if ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
916116Swnj 			    (so->so_options & SO_ACCEPTCONN) == 0)
926116Swnj 				wild = INPLOOKUP_WILDCARD;
935994Swnj 			if (in_pcblookup(head,
946116Swnj 			    zeroin_addr, 0, sin->sin_addr, lport, wild))
955994Swnj 				return (EADDRINUSE);
965994Swnj 		}
974951Swnj 	}
985852Sroot 	m = m_getclr(M_DONTWAIT);
994951Swnj 	if (m == 0)
1004983Swnj 		return (ENOBUFS);
1014951Swnj 	if (sbreserve(&so->so_snd, sndcc) == 0)
1024951Swnj 		goto bad;
1034951Swnj 	if (sbreserve(&so->so_rcv, rcvcc) == 0)
1044951Swnj 		goto bad2;
1054951Swnj 	inp = mtod(m, struct inpcb *);
1065172Swnj 	inp->inp_head = head;
1075994Swnj 	if (sin)
1085994Swnj 		inp->inp_laddr = sin->sin_addr;
1095172Swnj 	if (lport == 0)
1105172Swnj 		do {
1115994Swnj 			if (head->inp_lport++ < IPPORT_RESERVED)
1125994Swnj 				head->inp_lport = IPPORT_RESERVED;
1135172Swnj 			lport = htons(head->inp_lport);
1145994Swnj 		} while (in_pcblookup(head,
1155994Swnj 			    zeroin_addr, 0, inp->inp_laddr, lport, 0));
1165172Swnj 	inp->inp_lport = lport;
1174983Swnj 	inp->inp_socket = so;
1184983Swnj 	insque(inp, head);
1194951Swnj 	so->so_pcb = (caddr_t)inp;
1205994Swnj 	in_setsockaddr(inp);
1214951Swnj 	return (0);
1224951Swnj bad2:
1234951Swnj 	sbrelease(&so->so_snd);
1244951Swnj bad:
1254967Swnj 	(void) m_free(m);
1264951Swnj 	return (ENOBUFS);
1274905Swnj }
1284905Swnj 
1296116Swnj /*
1306116Swnj  * Connect from a socket to a specified address.
1316116Swnj  * Both address and port must be specified in argument sin.
1326116Swnj  * If don't have a local address for this socket yet,
1336116Swnj  * then pick one.
1346116Swnj  */
1355161Swnj in_pcbconnect(inp, sin)
1364951Swnj 	struct inpcb *inp;
1374951Swnj 	struct sockaddr_in *sin;
1384923Swnj {
1395994Swnj 	struct ifnet *ifp;
1406338Ssam 	struct sockaddr_in *ifaddr;
1414923Swnj 
1425161Swnj COUNT(IN_PCBCONNECT);
1434951Swnj 	if (sin->sin_family != AF_INET)
1444951Swnj 		return (EAFNOSUPPORT);
1454951Swnj 	if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0)
1464951Swnj 		return (EADDRNOTAVAIL);
1475994Swnj 	if (inp->inp_laddr.s_addr == 0) {
1486338Ssam 		ifp = if_ifonnetof(sin->sin_addr.s_net);
1496338Ssam 		if (ifp == 0) {
1506338Ssam 			ifp = if_ifwithaf(AF_INET);
1516338Ssam 			if (ifp == 0)
1526338Ssam 				return (EADDRNOTAVAIL);		/* XXX */
1536338Ssam 		}
1546338Ssam 		ifaddr = (struct sockaddr_in *)&ifp->if_addr;
1555994Swnj 	}
1565994Swnj 	if (in_pcblookup(inp->inp_head,
1576116Swnj 	    sin->sin_addr,
1586116Swnj 	    sin->sin_port,
1596338Ssam 	    inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
1606116Swnj 	    inp->inp_lport,
1616116Swnj 	    0))
1625172Swnj 		return (EADDRINUSE);
1636297Swnj 	if (inp->inp_laddr.s_addr == 0) {
1646297Swnj 		struct sockaddr_in *sin2 =
1656297Swnj 		    (struct sockaddr_in *)&inp->inp_socket->so_addr;
1666297Swnj 
1676338Ssam 		inp->inp_laddr = ifaddr->sin_addr;
1686297Swnj 		sin2->sin_addr = inp->inp_laddr;
1696297Swnj 	}
1704951Swnj 	inp->inp_faddr = sin->sin_addr;
1714951Swnj 	inp->inp_fport = sin->sin_port;
1724923Swnj 	return (0);
1734923Swnj }
1744923Swnj 
1755994Swnj in_setsockaddr(inp)
1765277Sroot 	struct inpcb *inp;
1775277Sroot {
1785994Swnj 	register struct sockaddr_in *sin =
1795994Swnj 	    (struct sockaddr_in *)&inp->inp_socket->so_addr;
1805277Sroot 
1815277Sroot 	sin->sin_family = AF_INET;
1825994Swnj 	sin->sin_addr = inp->inp_laddr;
1835994Swnj 	sin->sin_port = inp->inp_lport;
1845277Sroot }
1855277Sroot 
1865161Swnj in_pcbdisconnect(inp)
1874905Swnj 	struct inpcb *inp;
1884905Swnj {
1895161Swnj 
1905161Swnj COUNT(IN_PCBDISCONNECT);
1915161Swnj 	inp->inp_faddr.s_addr = 0;
1926028Sroot 	inp->inp_fport = 0;
1935161Swnj 	if (inp->inp_socket->so_state & SS_USERGONE)
1945161Swnj 		in_pcbdetach(inp);
1955161Swnj }
1965161Swnj 
1975161Swnj in_pcbdetach(inp)
1985161Swnj 	struct inpcb *inp;
1995161Swnj {
2004905Swnj 	struct socket *so = inp->inp_socket;
2014905Swnj 
2025009Swnj 	so->so_pcb = 0;
2035009Swnj 	sofree(so);
204*6350Ssam 	if (inp->inp_route.ro_rt)
205*6350Ssam 		freeroute(inp->inp_route.ro_rt);
2064983Swnj 	remque(inp);
2074907Swnj 	(void) m_free(dtom(inp));
2084905Swnj }
2094905Swnj 
2105161Swnj /*
2115994Swnj  * SHOULD ALLOW MATCH ON MULTI-HOMING ONLY
2125161Swnj  */
2134907Swnj struct inpcb *
2146028Sroot in_pcblookup(head, faddr, fport, laddr, lport, flags)
2154905Swnj 	struct inpcb *head;
2164951Swnj 	struct in_addr faddr, laddr;
2174905Swnj 	u_short fport, lport;
2186028Sroot 	int flags;
2194905Swnj {
2205994Swnj 	register struct inpcb *inp, *match = 0;
2215994Swnj 	int matchwild = 3, wildcard;
2224905Swnj 
2235161Swnj 	for (inp = head->inp_next; inp != head; inp = inp->inp_next) {
2245994Swnj 		if (inp->inp_lport != lport)
2255161Swnj 			continue;
2265994Swnj 		wildcard = 0;
2275994Swnj 		if (inp->inp_laddr.s_addr != 0) {
2286116Swnj 			if (laddr.s_addr == 0)
2296116Swnj 				wildcard++;
2306116Swnj 			else if (inp->inp_laddr.s_addr != laddr.s_addr)
2315994Swnj 				continue;
2325994Swnj 		} else {
2335994Swnj 			if (laddr.s_addr != 0)
2345994Swnj 				wildcard++;
2355994Swnj 		}
2365994Swnj 		if (inp->inp_faddr.s_addr != 0) {
2376116Swnj 			if (faddr.s_addr == 0)
2386116Swnj 				wildcard++;
2396116Swnj 			else if (inp->inp_faddr.s_addr != faddr.s_addr ||
2406028Sroot 			    inp->inp_fport != fport)
2415994Swnj 				continue;
2425994Swnj 		} else {
2435994Swnj 			if (faddr.s_addr != 0)
2445994Swnj 				wildcard++;
2455994Swnj 		}
2466028Sroot 		if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
2475994Swnj 			continue;
2485994Swnj 		if (wildcard < matchwild) {
2495161Swnj 			match = inp;
2505994Swnj 			matchwild = wildcard;
2515994Swnj 			if (matchwild == 0)
2525994Swnj 				break;
2535161Swnj 		}
2545161Swnj 	}
2555161Swnj 	return (match);
2564905Swnj }
257