1 /* tcp_usrreq.c 1.45 81/12/21 */ 2 3 #include "../h/param.h" 4 #include "../h/systm.h" 5 #include "../h/mbuf.h" 6 #include "../h/socket.h" 7 #include "../h/socketvar.h" 8 #include "../h/protosw.h" 9 #include "../net/in.h" 10 #include "../net/in_pcb.h" 11 #include "../net/in_systm.h" 12 #include "../net/if.h" 13 #include "../net/ip.h" 14 #include "../net/ip_var.h" 15 #include "../net/tcp.h" 16 #include "../net/tcp_fsm.h" 17 #include "../net/tcp_seq.h" 18 #include "../net/tcp_timer.h" 19 #include "../net/tcp_var.h" 20 #include "../net/tcpip.h" 21 #include "../net/tcp_debug.h" 22 #include "../errno.h" 23 24 /* 25 * TCP protocol interface to socket abstraction. 26 */ 27 extern char *tcpstates[]; 28 struct tcpcb *tcp_newtcpcb(); 29 30 /* 31 * Process a TCP user request for TCP tb. If this is a send request 32 * then m is the mbuf chain of send data. If this is a timer expiration 33 * (called from the software clock routine), then timertype tells which timer. 34 */ 35 tcp_usrreq(so, req, m, addr) 36 struct socket *so; 37 int req; 38 struct mbuf *m; 39 caddr_t addr; 40 { 41 register struct inpcb *inp = sotoinpcb(so); 42 register struct tcpcb *tp; 43 int s = splnet(); 44 int error = 0; 45 int ostate; 46 COUNT(TCP_USRREQ); 47 48 /* 49 * When a TCP is attached to a socket, then there will be 50 * a (struct inpcb) pointed at by the socket, and this 51 * structure will point at a subsidary (struct tcpcb). 52 * The normal sequence of events is: 53 * PRU_ATTACH creating these structures 54 * PRU_CONNECT connecting to a remote peer 55 * (PRU_SEND|PRU_RCVD)* exchanging data 56 * PRU_DISCONNECT disconnecting from remote peer 57 * PRU_DETACH deleting the structures 58 * With the operations from PRU_CONNECT through PRU_DISCONNECT 59 * possible repeated several times. 60 * 61 * MULTIPLE CONNECTS ARE NOT YET IMPLEMENTED. 62 */ 63 if (inp == 0 && req != PRU_ATTACH) { 64 splx(s); 65 return (EINVAL); /* XXX */ 66 } 67 if (inp) { 68 tp = intotcpcb(inp); 69 #ifdef KPROF 70 tcp_acounts[tp->t_state][req]++; 71 #endif 72 ostate = tp->t_state; 73 } 74 switch (req) { 75 76 /* 77 * TCP attaches to socket via PRU_ATTACH, reserving space, 78 * and internet and TCP control blocks. 79 * If the socket is to receive connections, 80 * then the LISTEN state is entered. 81 */ 82 case PRU_ATTACH: 83 if (inp) { 84 error = EISCONN; 85 break; 86 } 87 error = tcp_attach(so, (struct sockaddr *)addr); 88 if (error) 89 break; 90 tp = sototcpcb(so); 91 break; 92 93 /* 94 * PRU_DETACH detaches the TCP protocol from the socket. 95 * If the protocol state is non-embryonic, then can't 96 * do this directly: have to initiate a PRU_DISCONNECT, 97 * which may finish later; embryonic TCB's can just 98 * be discarded here. 99 */ 100 case PRU_DETACH: 101 if (tp->t_state > TCPS_LISTEN) 102 tcp_disconnect(tp); 103 else { 104 tcp_close(tp); 105 tp = 0; 106 } 107 break; 108 109 /* 110 * Initiate connection to peer. 111 * Create a template for use in transmissions on this connection. 112 * Enter SYN_SENT state, and mark socket as connecting. 113 * Start keep-alive timer, and seed output sequence space. 114 * Send initial segment on connection. 115 */ 116 case PRU_CONNECT: 117 error = in_pcbconnect(inp, (struct sockaddr_in *)addr); 118 if (error) 119 break; 120 tp->t_template = tcp_template(tp); 121 if (tp->t_template == 0) { 122 in_pcbdisconnect(inp); 123 error = ENOBUFS; 124 break; 125 } 126 soisconnecting(so); 127 tp->t_state = TCPS_SYN_SENT; 128 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 129 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 130 tcp_sendseqinit(tp); 131 (void) tcp_output(tp); 132 break; 133 134 /* 135 * Initiate disconnect from peer. 136 * If connection never passed embryonic stage, just drop; 137 * else if don't need to let data drain, then can just drop anyways, 138 * else have to begin TCP shutdown process: mark socket disconnecting, 139 * drain unread data, state switch to reflect user close, and 140 * send segment (e.g. FIN) to peer. Socket will be really disconnected 141 * when peer sends FIN and acks ours. 142 * 143 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 144 */ 145 case PRU_DISCONNECT: 146 tcp_disconnect(tp); 147 break; 148 149 /* 150 * Accept a connection. Essentially all the work is 151 * done at higher levels; just return the address 152 * of the peer, storing through addr. 153 */ 154 case PRU_ACCEPT: 155 in_pcbconnaddr(inp, (struct sockaddr *)addr); 156 break; 157 158 /* 159 * Mark the connection as being incapable of further output. 160 */ 161 case PRU_SHUTDOWN: 162 socantsendmore(so); 163 tcp_usrclosed(tp); 164 (void) tcp_output(tp); 165 break; 166 167 /* 168 * After a receive, possibly send window update to peer. 169 */ 170 case PRU_RCVD: 171 (void) tcp_output(tp); 172 break; 173 174 /* 175 * Do a send by putting data in output queue and updating urgent 176 * marker if URG set. Possibly send more data. 177 */ 178 case PRU_SEND: 179 sbappend(&so->so_snd, m); 180 /* 181 if (tp->t_flags & TF_PUSH) 182 tp->snd_end = tp->snd_una + so->so_snd.sb_cc; 183 */ 184 if (tp->t_flags & TF_URG) 185 tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1; 186 (void) tcp_output(tp); 187 break; 188 189 /* 190 * Abort the TCP. 191 */ 192 case PRU_ABORT: 193 tcp_drop(tp, ECONNABORTED); 194 break; 195 196 /* SOME AS YET UNIMPLEMENTED HOOKS */ 197 case PRU_CONTROL: 198 error = EOPNOTSUPP; 199 break; 200 201 case PRU_SENSE: 202 error = EOPNOTSUPP; 203 break; 204 205 case PRU_RCVOOB: 206 error = EOPNOTSUPP; 207 break; 208 209 case PRU_SENDOOB: 210 error = EOPNOTSUPP; 211 break; 212 /* END UNIMPLEMENTED HOOKS */ 213 214 /* 215 * TCP slow timer went off; going through this 216 * routine for tracing's sake. 217 */ 218 case PRU_SLOWTIMO: 219 tcp_timers(tp, (int)addr); 220 req |= (int)addr << 8; /* for debug's sake */ 221 break; 222 223 default: 224 panic("tcp_usrreq"); 225 } 226 if (tp && (so->so_options & SO_DEBUG)) 227 tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 228 splx(s); 229 return (error); 230 } 231 232 /* 233 * Attach TCP protocol to socket, allocating 234 * internet protocol control block, tcp control block, 235 * bufer space, and entering LISTEN state if to accept connections. 236 */ 237 tcp_attach(so, sa) 238 struct socket *so; 239 struct sockaddr *sa; 240 { 241 register struct tcpcb *tp; 242 struct inpcb *inp; 243 int error; 244 245 error = in_pcbattach(so, &tcb, 2048, 2048, (struct sockaddr_in *)sa); 246 if (error) 247 return (error); 248 inp = (struct inpcb *)so->so_pcb; 249 tp = tcp_newtcpcb(inp); 250 if (so->so_options & SO_ACCEPTCONN) { 251 if (tp == 0) { 252 in_pcbdetach(inp); 253 return (ENOBUFS); 254 } 255 tp->t_state = TCPS_LISTEN; 256 } else 257 tp->t_state = TCPS_CLOSED; 258 return (0); 259 } 260 261 /* 262 * Initiate (or continue) disconnect. 263 * If embryonic state, just send reset (once). 264 * If not in ``let data drain'' option, just drop. 265 * Otherwise (hard), mark socket disconnecting and drop 266 * current input data; switch states based on user close, and 267 * send segment to peer (with FIN). 268 */ 269 tcp_disconnect(tp) 270 struct tcpcb *tp; 271 { 272 struct socket *so = tp->t_inpcb->inp_socket; 273 274 if (tp->t_state < TCPS_ESTABLISHED) 275 tcp_close(tp); 276 else if ((so->so_options & SO_LETDATADRAIN) == 0) 277 tcp_drop(tp, 0); 278 else { 279 soisdisconnecting(so); 280 sbflush(&so->so_rcv); 281 tcp_usrclosed(tp); 282 (void) tcp_output(tp); 283 } 284 } 285 286 /* 287 * User issued close, and wish to trail through shutdown states: 288 * if never received SYN, just forget it. If got a SYN from peer, 289 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 290 * If already got a FIN from peer, then almost done; go to LAST_ACK 291 * state. In all other cases, have already sent FIN to peer (e.g. 292 * after PRU_SHUTDOWN), and just have to play tedious game waiting 293 * for peer to send FIN or not respond to keep-alives, etc. 294 */ 295 tcp_usrclosed(tp) 296 struct tcpcb *tp; 297 { 298 299 switch (tp->t_state) { 300 301 case TCPS_LISTEN: 302 case TCPS_SYN_SENT: 303 tp->t_state = TCPS_CLOSED; 304 tcp_close(tp); 305 break; 306 307 case TCPS_SYN_RECEIVED: 308 case TCPS_ESTABLISHED: 309 tp->t_state = TCPS_FIN_WAIT_1; 310 break; 311 312 case TCPS_CLOSE_WAIT: 313 tp->t_state = TCPS_LAST_ACK; 314 break; 315 } 316 } 317