1*5172Swnj /* in_pcb.c 4.12 81/12/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. 43*5172Swnj * 44*5172Swnj * TODO: 45*5172Swnj * use hashing 465161Swnj */ 475161Swnj 485161Swnj /* 494951Swnj * Allocate a protocol control block, space 504951Swnj * for send and receive data, and local host information. 514951Swnj * Return error. If no error make socket point at pcb. 524951Swnj */ 535161Swnj in_pcbattach(so, head, sndcc, rcvcc, sin) 544951Swnj struct socket *so; 554951Swnj struct inpcb *head; 564951Swnj int sndcc, rcvcc; 574951Swnj struct sockaddr_in *sin; 584905Swnj { 594905Swnj struct mbuf *m; 604983Swnj register struct inpcb *inp, *xp; 614951Swnj struct ifnet *ifp; 624951Swnj u_long lport; 634905Swnj 645161Swnj COUNT(IN_PCBATTACH); 654951Swnj if (sin) { 664951Swnj if (sin->sin_family != AF_INET) 674951Swnj return (EAFNOSUPPORT); 684951Swnj ifp = if_ifwithaddr(sin->sin_addr); 694951Swnj if (ifp == 0) 704951Swnj return (EADDRNOTAVAIL); 714951Swnj lport = sin->sin_port; 72*5172Swnj if (lport && 73*5172Swnj in_pcblookup(head, 0, 0, sin->sin_addr.s_addr, lport)) 74*5172Swnj return (EADDRINUSE); 754951Swnj } else { 764951Swnj ifp = if_ifwithaddr(ifnet->if_addr); 774951Swnj lport = 0; 784951Swnj } 79*5172Swnj m = m_getclr(0); 804951Swnj if (m == 0) 814983Swnj return (ENOBUFS); 824951Swnj if (sbreserve(&so->so_snd, sndcc) == 0) 834951Swnj goto bad; 844951Swnj if (sbreserve(&so->so_rcv, rcvcc) == 0) 854951Swnj goto bad2; 864951Swnj inp = mtod(m, struct inpcb *); 87*5172Swnj inp->inp_head = head; 884951Swnj inp->inp_laddr = ifp->if_addr; 89*5172Swnj if (lport == 0) 90*5172Swnj do { 91*5172Swnj if (head->inp_lport++ < 1024) 92*5172Swnj head->inp_lport = 1024; 93*5172Swnj lport = htons(head->inp_lport); 94*5172Swnj } while (in_pcblookup(head, 0, 0, inp->inp_laddr, lport)); 95*5172Swnj inp->inp_lport = lport; 964983Swnj inp->inp_socket = so; 974983Swnj insque(inp, head); 984951Swnj so->so_pcb = (caddr_t)inp; 994958Swnj sin = (struct sockaddr_in *)&so->so_addr; 1004958Swnj sin->sin_family = AF_INET; 1014958Swnj sin->sin_addr = inp->inp_laddr; 1024958Swnj sin->sin_port = inp->inp_lport; 1034951Swnj return (0); 1044951Swnj bad2: 1054951Swnj sbrelease(&so->so_snd); 1064951Swnj bad: 1074967Swnj (void) m_free(m); 1084951Swnj return (ENOBUFS); 1094905Swnj } 1104905Swnj 1115161Swnj in_pcbconnect(inp, sin) 1124951Swnj struct inpcb *inp; 1134951Swnj struct sockaddr_in *sin; 1144923Swnj { 115*5172Swnj struct inpcb *xp; 1164923Swnj 1175161Swnj COUNT(IN_PCBCONNECT); 1184951Swnj if (sin->sin_family != AF_INET) 1194951Swnj return (EAFNOSUPPORT); 1204951Swnj if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) 1214951Swnj return (EADDRNOTAVAIL); 122*5172Swnj xp = in_pcblookup(inp->inp_head, sin->sin_addr, sin->sin_port, inp->inp_laddr, inp->inp_lport); 123*5172Swnj if (xp->inp_faddr) 124*5172Swnj return (EADDRINUSE); 1254951Swnj inp->inp_faddr = sin->sin_addr; 1264951Swnj inp->inp_fport = sin->sin_port; 1274923Swnj return (0); 1284923Swnj } 1294923Swnj 1305161Swnj in_pcbdisconnect(inp) 1314905Swnj struct inpcb *inp; 1324905Swnj { 1335161Swnj 1345161Swnj COUNT(IN_PCBDISCONNECT); 1355161Swnj inp->inp_faddr.s_addr = 0; 1365161Swnj if (inp->inp_socket->so_state & SS_USERGONE) 1375161Swnj in_pcbdetach(inp); 1385161Swnj } 1395161Swnj 1405161Swnj in_pcbdetach(inp) 1415161Swnj struct inpcb *inp; 1425161Swnj { 1434905Swnj struct socket *so = inp->inp_socket; 1444905Swnj 1455009Swnj so->so_pcb = 0; 1465009Swnj sofree(so); 1474983Swnj remque(inp); 1484907Swnj (void) m_free(dtom(inp)); 1494905Swnj } 1504905Swnj 1515161Swnj /* 1525161Swnj * Look for a control block to accept a segment. 1535161Swnj * First choice is an exact address match. 1545161Swnj * Second choice is a match of local address, with 1555161Swnj * unspecified foreign address. 1565161Swnj */ 1574907Swnj struct inpcb * 1584951Swnj in_pcblookup(head, faddr, fport, laddr, lport) 1594905Swnj struct inpcb *head; 1604951Swnj struct in_addr faddr, laddr; 1614905Swnj u_short fport, lport; 1624905Swnj { 1634905Swnj register struct inpcb *inp; 1645161Swnj struct inpcb *match = 0; 1654905Swnj 1665161Swnj for (inp = head->inp_next; inp != head; inp = inp->inp_next) { 1675161Swnj if (inp->inp_laddr.s_addr != laddr.s_addr || 1685161Swnj inp->inp_lport != lport) 1695161Swnj continue; 1705161Swnj if (inp->inp_faddr.s_addr == 0) { 1715161Swnj match = inp; 1725161Swnj continue; 1735161Swnj } 1744951Swnj if (inp->inp_faddr.s_addr == faddr.s_addr && 1755161Swnj inp->inp_fport == fport) 1764905Swnj return (inp); 1775161Swnj } 1785161Swnj return (match); 1794905Swnj } 180