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