1 /* tcp_usrreq.c 6.4 84/08/29 */ 2 3 #include "param.h" 4 #include "systm.h" 5 #include "mbuf.h" 6 #include "socket.h" 7 #include "socketvar.h" 8 #include "protosw.h" 9 #include "errno.h" 10 #include "stat.h" 11 12 #include "../net/if.h" 13 #include "../net/route.h" 14 15 #include "in.h" 16 #include "in_pcb.h" 17 #include "in_systm.h" 18 #include "ip.h" 19 #include "ip_var.h" 20 #include "tcp.h" 21 #include "tcp_fsm.h" 22 #include "tcp_seq.h" 23 #include "tcp_timer.h" 24 #include "tcp_var.h" 25 #include "tcpip.h" 26 #include "tcp_debug.h" 27 28 /* 29 * TCP protocol interface to socket abstraction. 30 */ 31 extern char *tcpstates[]; 32 struct tcpcb *tcp_newtcpcb(); 33 int tcpsenderrors; 34 35 /* 36 * Process a TCP user request for TCP tb. If this is a send request 37 * then m is the mbuf chain of send data. If this is a timer expiration 38 * (called from the software clock routine), then timertype tells which timer. 39 */ 40 /*ARGSUSED*/ 41 tcp_usrreq(so, req, m, nam, rights) 42 struct socket *so; 43 int req; 44 struct mbuf *m, *nam, *rights; 45 { 46 register struct inpcb *inp = sotoinpcb(so); 47 register struct tcpcb *tp; 48 int s = splnet(); 49 int error = 0; 50 int ostate; 51 52 if (rights && rights->m_len) { 53 splx(s); 54 return (EINVAL); 55 } 56 /* 57 * When a TCP is attached to a socket, then there will be 58 * a (struct inpcb) pointed at by the socket, and this 59 * structure will point at a subsidary (struct tcpcb). 60 */ 61 if (inp == 0 && req != PRU_ATTACH) { 62 splx(s); 63 return (EINVAL); /* XXX */ 64 } 65 if (inp) { 66 tp = intotcpcb(inp); 67 /* WHAT IF TP IS 0? */ 68 #ifdef KPROF 69 tcp_acounts[tp->t_state][req]++; 70 #endif 71 ostate = tp->t_state; 72 } else 73 ostate = 0; 74 switch (req) { 75 76 /* 77 * TCP attaches to socket via PRU_ATTACH, reserving space, 78 * and an internet control block. 79 */ 80 case PRU_ATTACH: 81 if (inp) { 82 error = EISCONN; 83 break; 84 } 85 error = tcp_attach(so); 86 if (error) 87 break; 88 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 89 so->so_linger = TCP_LINGERTIME; 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 tp = tcp_disconnect(tp); 103 else 104 tp = tcp_close(tp); 105 break; 106 107 /* 108 * Give the socket an address. 109 */ 110 case PRU_BIND: 111 error = in_pcbbind(inp, nam); 112 if (error) 113 break; 114 break; 115 116 /* 117 * Prepare to accept connections. 118 */ 119 case PRU_LISTEN: 120 if (inp->inp_lport == 0) 121 error = in_pcbbind(inp, (struct mbuf *)0); 122 if (error == 0) 123 tp->t_state = TCPS_LISTEN; 124 break; 125 126 /* 127 * Initiate connection to peer. 128 * Create a template for use in transmissions on this connection. 129 * Enter SYN_SENT state, and mark socket as connecting. 130 * Start keep-alive timer, and seed output sequence space. 131 * Send initial segment on connection. 132 */ 133 case PRU_CONNECT: 134 if (inp->inp_lport == 0) { 135 error = in_pcbbind(inp, (struct mbuf *)0); 136 if (error) 137 break; 138 } 139 error = in_pcbconnect(inp, nam); 140 if (error) 141 break; 142 tp->t_template = tcp_template(tp); 143 if (tp->t_template == 0) { 144 in_pcbdisconnect(inp); 145 error = ENOBUFS; 146 break; 147 } 148 soisconnecting(so); 149 tp->t_state = TCPS_SYN_SENT; 150 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 151 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 152 tcp_sendseqinit(tp); 153 error = tcp_output(tp); 154 break; 155 156 /* 157 * Create a TCP connection between two sockets. 158 */ 159 case PRU_CONNECT2: 160 error = EOPNOTSUPP; 161 break; 162 163 /* 164 * Initiate disconnect from peer. 165 * If connection never passed embryonic stage, just drop; 166 * else if don't need to let data drain, then can just drop anyways, 167 * else have to begin TCP shutdown process: mark socket disconnecting, 168 * drain unread data, state switch to reflect user close, and 169 * send segment (e.g. FIN) to peer. Socket will be really disconnected 170 * when peer sends FIN and acks ours. 171 * 172 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 173 */ 174 case PRU_DISCONNECT: 175 tp = tcp_disconnect(tp); 176 break; 177 178 /* 179 * Accept a connection. Essentially all the work is 180 * done at higher levels; just return the address 181 * of the peer, storing through addr. 182 */ 183 case PRU_ACCEPT: { 184 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 185 186 nam->m_len = sizeof (struct sockaddr_in); 187 sin->sin_family = AF_INET; 188 sin->sin_port = inp->inp_fport; 189 sin->sin_addr = inp->inp_faddr; 190 break; 191 } 192 193 /* 194 * Mark the connection as being incapable of further output. 195 */ 196 case PRU_SHUTDOWN: 197 socantsendmore(so); 198 tp = tcp_usrclosed(tp); 199 if (tp) 200 error = tcp_output(tp); 201 break; 202 203 /* 204 * After a receive, possibly send window update to peer. 205 */ 206 case PRU_RCVD: 207 (void) tcp_output(tp); 208 break; 209 210 /* 211 * Do a send by putting data in output queue and updating urgent 212 * marker if URG set. Possibly send more data. 213 */ 214 case PRU_SEND: 215 sbappend(&so->so_snd, m); 216 #ifdef notdef 217 if (tp->t_flags & TF_PUSH) 218 tp->snd_end = tp->snd_una + so->so_snd.sb_cc; 219 #endif 220 error = tcp_output(tp); 221 if (error) { /* XXX fix to use other path */ 222 if (error == ENOBUFS) /* XXX */ 223 error = 0; /* XXX */ 224 tcpsenderrors++; 225 } 226 break; 227 228 /* 229 * Abort the TCP. 230 */ 231 case PRU_ABORT: 232 tp = tcp_drop(tp, ECONNABORTED); 233 break; 234 235 /* SOME AS YET UNIMPLEMENTED HOOKS */ 236 case PRU_CONTROL: 237 error = EOPNOTSUPP; 238 break; 239 /* END UNIMPLEMENTED HOOKS */ 240 241 case PRU_SENSE: 242 ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 243 return (0); 244 245 case PRU_RCVOOB: 246 if (so->so_oobmark == 0 && 247 (so->so_state & SS_RCVATMARK) == 0) { 248 error = EINVAL; 249 break; 250 } 251 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 252 error = EWOULDBLOCK; 253 break; 254 } 255 m->m_len = 1; 256 *mtod(m, caddr_t) = tp->t_iobc; 257 break; 258 259 case PRU_SENDOOB: 260 if (sbspace(&so->so_snd) < -512) { 261 m_freem(m); 262 error = ENOBUFS; 263 break; 264 } 265 tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1; 266 sbappend(&so->so_snd, m); 267 tp->t_force = 1; 268 error = tcp_output(tp); 269 tp->t_force = 0; 270 break; 271 272 case PRU_SOCKADDR: 273 in_setsockaddr(inp, nam); 274 break; 275 276 case PRU_PEERADDR: 277 in_setpeeraddr(inp, nam); 278 break; 279 280 /* 281 * TCP slow timer went off; going through this 282 * routine for tracing's sake. 283 */ 284 case PRU_SLOWTIMO: 285 tp = tcp_timers(tp, (int)nam); 286 req |= (int)nam << 8; /* for debug's sake */ 287 break; 288 289 default: 290 panic("tcp_usrreq"); 291 } 292 if (tp && (so->so_options & SO_DEBUG)) 293 tcp_trace(TA_USER, ostate, tp, (struct tcpiphdr *)0, req); 294 splx(s); 295 return (error); 296 } 297 298 int tcp_sendspace = 1024*2; 299 int tcp_recvspace = 1024*2; 300 /* 301 * Attach TCP protocol to socket, allocating 302 * internet protocol control block, tcp control block, 303 * bufer space, and entering LISTEN state if to accept connections. 304 */ 305 tcp_attach(so) 306 struct socket *so; 307 { 308 register struct tcpcb *tp; 309 struct inpcb *inp; 310 int error; 311 312 error = soreserve(so, tcp_sendspace, tcp_recvspace); 313 if (error) 314 return (error); 315 error = in_pcballoc(so, &tcb); 316 if (error) 317 return (error); 318 inp = sotoinpcb(so); 319 tp = tcp_newtcpcb(inp); 320 if (tp == 0) { 321 int nofd = so->so_state & SS_NOFDREF; /* XXX */ 322 323 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 324 in_pcbdetach(inp); 325 so->so_state |= nofd; 326 return (ENOBUFS); 327 } 328 tp->t_state = TCPS_CLOSED; 329 return (0); 330 } 331 332 /* 333 * Initiate (or continue) disconnect. 334 * If embryonic state, just send reset (once). 335 * If in ``let data drain'' option and linger null, just drop. 336 * Otherwise (hard), mark socket disconnecting and drop 337 * current input data; switch states based on user close, and 338 * send segment to peer (with FIN). 339 */ 340 struct tcpcb * 341 tcp_disconnect(tp) 342 register struct tcpcb *tp; 343 { 344 struct socket *so = tp->t_inpcb->inp_socket; 345 346 if (tp->t_state < TCPS_ESTABLISHED) 347 tp = tcp_close(tp); 348 else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 349 tp = tcp_drop(tp, 0); 350 else { 351 soisdisconnecting(so); 352 sbflush(&so->so_rcv); 353 tp = tcp_usrclosed(tp); 354 if (tp) 355 (void) tcp_output(tp); 356 } 357 return (tp); 358 } 359 360 /* 361 * User issued close, and wish to trail through shutdown states: 362 * if never received SYN, just forget it. If got a SYN from peer, 363 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 364 * If already got a FIN from peer, then almost done; go to LAST_ACK 365 * state. In all other cases, have already sent FIN to peer (e.g. 366 * after PRU_SHUTDOWN), and just have to play tedious game waiting 367 * for peer to send FIN or not respond to keep-alives, etc. 368 * We can let the user exit from the close as soon as the FIN is acked. 369 */ 370 struct tcpcb * 371 tcp_usrclosed(tp) 372 register struct tcpcb *tp; 373 { 374 375 switch (tp->t_state) { 376 377 case TCPS_CLOSED: 378 case TCPS_LISTEN: 379 case TCPS_SYN_SENT: 380 tp->t_state = TCPS_CLOSED; 381 tp = tcp_close(tp); 382 break; 383 384 case TCPS_SYN_RECEIVED: 385 case TCPS_ESTABLISHED: 386 tp->t_state = TCPS_FIN_WAIT_1; 387 break; 388 389 case TCPS_CLOSE_WAIT: 390 tp->t_state = TCPS_LAST_ACK; 391 break; 392 } 393 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) 394 soisdisconnected(tp->t_inpcb->inp_socket); 395 return (tp); 396 } 397