1*5240Sroot /* in_pcb.c 4.13 81/12/11 */ 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 */ 47*5240Sroot 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; 61*5240Sroot register struct inpcb *inp; 624951Swnj struct ifnet *ifp; 63*5240Sroot u_short lport; 644905Swnj 655161Swnj COUNT(IN_PCBATTACH); 664951Swnj if (sin) { 674951Swnj if (sin->sin_family != AF_INET) 684951Swnj return (EAFNOSUPPORT); 694951Swnj ifp = if_ifwithaddr(sin->sin_addr); 704951Swnj if (ifp == 0) 714951Swnj return (EADDRNOTAVAIL); 724951Swnj lport = sin->sin_port; 735172Swnj if (lport && 74*5240Sroot in_pcblookup(head, zeroin_addr, 0, sin->sin_addr, lport)) 755172Swnj return (EADDRINUSE); 764951Swnj } else { 774951Swnj ifp = if_ifwithaddr(ifnet->if_addr); 784951Swnj lport = 0; 794951Swnj } 805172Swnj m = m_getclr(0); 814951Swnj if (m == 0) 824983Swnj return (ENOBUFS); 834951Swnj if (sbreserve(&so->so_snd, sndcc) == 0) 844951Swnj goto bad; 854951Swnj if (sbreserve(&so->so_rcv, rcvcc) == 0) 864951Swnj goto bad2; 874951Swnj inp = mtod(m, struct inpcb *); 885172Swnj inp->inp_head = head; 894951Swnj inp->inp_laddr = ifp->if_addr; 905172Swnj if (lport == 0) 915172Swnj do { 925172Swnj if (head->inp_lport++ < 1024) 935172Swnj head->inp_lport = 1024; 945172Swnj lport = htons(head->inp_lport); 95*5240Sroot } while (in_pcblookup(head, zeroin_addr, 0, inp->inp_laddr, lport)); 965172Swnj inp->inp_lport = lport; 974983Swnj inp->inp_socket = so; 984983Swnj insque(inp, head); 994951Swnj so->so_pcb = (caddr_t)inp; 1004958Swnj sin = (struct sockaddr_in *)&so->so_addr; 1014958Swnj sin->sin_family = AF_INET; 1024958Swnj sin->sin_addr = inp->inp_laddr; 1034958Swnj sin->sin_port = inp->inp_lport; 1044951Swnj return (0); 1054951Swnj bad2: 1064951Swnj sbrelease(&so->so_snd); 1074951Swnj bad: 1084967Swnj (void) m_free(m); 1094951Swnj return (ENOBUFS); 1104905Swnj } 1114905Swnj 1125161Swnj in_pcbconnect(inp, sin) 1134951Swnj struct inpcb *inp; 1144951Swnj struct sockaddr_in *sin; 1154923Swnj { 1165172Swnj struct inpcb *xp; 1174923Swnj 1185161Swnj COUNT(IN_PCBCONNECT); 1194951Swnj if (sin->sin_family != AF_INET) 1204951Swnj return (EAFNOSUPPORT); 1214951Swnj if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) 1224951Swnj return (EADDRNOTAVAIL); 1235172Swnj xp = in_pcblookup(inp->inp_head, sin->sin_addr, sin->sin_port, inp->inp_laddr, inp->inp_lport); 124*5240Sroot if (xp->inp_faddr.s_addr) 1255172Swnj return (EADDRINUSE); 1264951Swnj inp->inp_faddr = sin->sin_addr; 1274951Swnj inp->inp_fport = sin->sin_port; 1284923Swnj return (0); 1294923Swnj } 1304923Swnj 1315161Swnj in_pcbdisconnect(inp) 1324905Swnj struct inpcb *inp; 1334905Swnj { 1345161Swnj 1355161Swnj COUNT(IN_PCBDISCONNECT); 1365161Swnj inp->inp_faddr.s_addr = 0; 1375161Swnj if (inp->inp_socket->so_state & SS_USERGONE) 1385161Swnj in_pcbdetach(inp); 1395161Swnj } 1405161Swnj 1415161Swnj in_pcbdetach(inp) 1425161Swnj struct inpcb *inp; 1435161Swnj { 1444905Swnj struct socket *so = inp->inp_socket; 1454905Swnj 1465009Swnj so->so_pcb = 0; 1475009Swnj sofree(so); 1484983Swnj remque(inp); 1494907Swnj (void) m_free(dtom(inp)); 1504905Swnj } 1514905Swnj 1525161Swnj /* 1535161Swnj * Look for a control block to accept a segment. 1545161Swnj * First choice is an exact address match. 1555161Swnj * Second choice is a match of local address, with 1565161Swnj * unspecified foreign address. 1575161Swnj */ 1584907Swnj struct inpcb * 1594951Swnj in_pcblookup(head, faddr, fport, laddr, lport) 1604905Swnj struct inpcb *head; 1614951Swnj struct in_addr faddr, laddr; 1624905Swnj u_short fport, lport; 1634905Swnj { 1644905Swnj register struct inpcb *inp; 1655161Swnj struct inpcb *match = 0; 1664905Swnj 1675161Swnj for (inp = head->inp_next; inp != head; inp = inp->inp_next) { 1685161Swnj if (inp->inp_laddr.s_addr != laddr.s_addr || 1695161Swnj inp->inp_lport != lport) 1705161Swnj continue; 1715161Swnj if (inp->inp_faddr.s_addr == 0) { 1725161Swnj match = inp; 1735161Swnj continue; 1745161Swnj } 1754951Swnj if (inp->inp_faddr.s_addr == faddr.s_addr && 1765161Swnj inp->inp_fport == fport) 1774905Swnj return (inp); 1785161Swnj } 1795161Swnj return (match); 1804905Swnj } 181