1 /* 2 * Copyright (c) 1982, 1986 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that this notice is preserved and that due credit is given 7 * to the University of California at Berkeley. The name of the University 8 * may not be used to endorse or promote products derived from this 9 * software without specific prior written permission. This software 10 * is provided ``as is'' without express or implied warranty. 11 * 12 * @(#)tcp_usrreq.c 7.9 (Berkeley) 05/25/88 13 */ 14 15 #include "param.h" 16 #include "systm.h" 17 #include "mbuf.h" 18 #include "socket.h" 19 #include "socketvar.h" 20 #include "protosw.h" 21 #include "errno.h" 22 #include "stat.h" 23 24 #include "../net/if.h" 25 #include "../net/route.h" 26 27 #include "in.h" 28 #include "in_pcb.h" 29 #include "in_systm.h" 30 #include "ip.h" 31 #include "ip_var.h" 32 #include "tcp.h" 33 #include "tcp_fsm.h" 34 #include "tcp_seq.h" 35 #include "tcp_timer.h" 36 #include "tcp_var.h" 37 #include "tcpip.h" 38 #include "tcp_debug.h" 39 40 /* 41 * TCP protocol interface to socket abstraction. 42 */ 43 extern char *tcpstates[]; 44 struct tcpcb *tcp_newtcpcb(); 45 46 /* 47 * Process a TCP user request for TCP tb. If this is a send request 48 * then m is the mbuf chain of send data. If this is a timer expiration 49 * (called from the software clock routine), then timertype tells which timer. 50 */ 51 /*ARGSUSED*/ 52 tcp_usrreq(so, req, m, nam, rights) 53 struct socket *so; 54 int req; 55 struct mbuf *m, *nam, *rights; 56 { 57 register struct inpcb *inp; 58 register struct tcpcb *tp; 59 int s; 60 int error = 0; 61 int ostate; 62 63 if (req == PRU_CONTROL) 64 return (in_control(so, (int)m, (caddr_t)nam, 65 (struct ifnet *)rights)); 66 if (rights && rights->m_len) 67 return (EINVAL); 68 69 s = splnet(); 70 inp = sotoinpcb(so); 71 /* 72 * When a TCP is attached to a socket, then there will be 73 * a (struct inpcb) pointed at by the socket, and this 74 * structure will point at a subsidary (struct tcpcb). 75 */ 76 if (inp == 0 && req != PRU_ATTACH) { 77 splx(s); 78 return (EINVAL); /* XXX */ 79 } 80 if (inp) { 81 tp = intotcpcb(inp); 82 /* WHAT IF TP IS 0? */ 83 #ifdef KPROF 84 tcp_acounts[tp->t_state][req]++; 85 #endif 86 ostate = tp->t_state; 87 } else 88 ostate = 0; 89 switch (req) { 90 91 /* 92 * TCP attaches to socket via PRU_ATTACH, reserving space, 93 * and an internet control block. 94 */ 95 case PRU_ATTACH: 96 if (inp) { 97 error = EISCONN; 98 break; 99 } 100 error = tcp_attach(so); 101 if (error) 102 break; 103 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 104 so->so_linger = TCP_LINGERTIME; 105 tp = sototcpcb(so); 106 break; 107 108 /* 109 * PRU_DETACH detaches the TCP protocol from the socket. 110 * If the protocol state is non-embryonic, then can't 111 * do this directly: have to initiate a PRU_DISCONNECT, 112 * which may finish later; embryonic TCB's can just 113 * be discarded here. 114 */ 115 case PRU_DETACH: 116 if (tp->t_state > TCPS_LISTEN) 117 tp = tcp_disconnect(tp); 118 else 119 tp = tcp_close(tp); 120 break; 121 122 /* 123 * Give the socket an address. 124 */ 125 case PRU_BIND: 126 error = in_pcbbind(inp, nam); 127 if (error) 128 break; 129 break; 130 131 /* 132 * Prepare to accept connections. 133 */ 134 case PRU_LISTEN: 135 if (inp->inp_lport == 0) 136 error = in_pcbbind(inp, (struct mbuf *)0); 137 if (error == 0) 138 tp->t_state = TCPS_LISTEN; 139 break; 140 141 /* 142 * Initiate connection to peer. 143 * Create a template for use in transmissions on this connection. 144 * Enter SYN_SENT state, and mark socket as connecting. 145 * Start keep-alive timer, and seed output sequence space. 146 * Send initial segment on connection. 147 */ 148 case PRU_CONNECT: 149 if (inp->inp_lport == 0) { 150 error = in_pcbbind(inp, (struct mbuf *)0); 151 if (error) 152 break; 153 } 154 error = in_pcbconnect(inp, nam); 155 if (error) 156 break; 157 tp->t_template = tcp_template(tp); 158 if (tp->t_template == 0) { 159 in_pcbdisconnect(inp); 160 error = ENOBUFS; 161 break; 162 } 163 soisconnecting(so); 164 tcpstat.tcps_connattempt++; 165 tp->t_state = TCPS_SYN_SENT; 166 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 167 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 168 tcp_sendseqinit(tp); 169 error = tcp_output(tp); 170 break; 171 172 /* 173 * Create a TCP connection between two sockets. 174 */ 175 case PRU_CONNECT2: 176 error = EOPNOTSUPP; 177 break; 178 179 /* 180 * Initiate disconnect from peer. 181 * If connection never passed embryonic stage, just drop; 182 * else if don't need to let data drain, then can just drop anyways, 183 * else have to begin TCP shutdown process: mark socket disconnecting, 184 * drain unread data, state switch to reflect user close, and 185 * send segment (e.g. FIN) to peer. Socket will be really disconnected 186 * when peer sends FIN and acks ours. 187 * 188 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 189 */ 190 case PRU_DISCONNECT: 191 tp = tcp_disconnect(tp); 192 break; 193 194 /* 195 * Accept a connection. Essentially all the work is 196 * done at higher levels; just return the address 197 * of the peer, storing through addr. 198 */ 199 case PRU_ACCEPT: { 200 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 201 202 nam->m_len = sizeof (struct sockaddr_in); 203 sin->sin_family = AF_INET; 204 sin->sin_port = inp->inp_fport; 205 sin->sin_addr = inp->inp_faddr; 206 break; 207 } 208 209 /* 210 * Mark the connection as being incapable of further output. 211 */ 212 case PRU_SHUTDOWN: 213 socantsendmore(so); 214 tp = tcp_usrclosed(tp); 215 if (tp) 216 error = tcp_output(tp); 217 break; 218 219 /* 220 * After a receive, possibly send window update to peer. 221 */ 222 case PRU_RCVD: 223 (void) tcp_output(tp); 224 break; 225 226 /* 227 * Do a send by putting data in output queue and updating urgent 228 * marker if URG set. Possibly send more data. 229 */ 230 case PRU_SEND: 231 sbappend(&so->so_snd, m); 232 error = tcp_output(tp); 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 u_long tcp_sendspace = 1024*4; 371 u_long 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 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 385 error = soreserve(so, tcp_sendspace, tcp_recvspace); 386 if (error) 387 return (error); 388 } 389 error = in_pcballoc(so, &tcb); 390 if (error) 391 return (error); 392 inp = sotoinpcb(so); 393 tp = tcp_newtcpcb(inp); 394 if (tp == 0) { 395 int nofd = so->so_state & SS_NOFDREF; /* XXX */ 396 397 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 398 in_pcbdetach(inp); 399 so->so_state |= nofd; 400 return (ENOBUFS); 401 } 402 tp->t_state = TCPS_CLOSED; 403 return (0); 404 } 405 406 /* 407 * Initiate (or continue) disconnect. 408 * If embryonic state, just send reset (once). 409 * If in ``let data drain'' option and linger null, just drop. 410 * Otherwise (hard), mark socket disconnecting and drop 411 * current input data; switch states based on user close, and 412 * send segment to peer (with FIN). 413 */ 414 struct tcpcb * 415 tcp_disconnect(tp) 416 register struct tcpcb *tp; 417 { 418 struct socket *so = tp->t_inpcb->inp_socket; 419 420 if (tp->t_state < TCPS_ESTABLISHED) 421 tp = tcp_close(tp); 422 else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 423 tp = tcp_drop(tp, 0); 424 else { 425 soisdisconnecting(so); 426 sbflush(&so->so_rcv); 427 tp = tcp_usrclosed(tp); 428 if (tp) 429 (void) tcp_output(tp); 430 } 431 return (tp); 432 } 433 434 /* 435 * User issued close, and wish to trail through shutdown states: 436 * if never received SYN, just forget it. If got a SYN from peer, 437 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 438 * If already got a FIN from peer, then almost done; go to LAST_ACK 439 * state. In all other cases, have already sent FIN to peer (e.g. 440 * after PRU_SHUTDOWN), and just have to play tedious game waiting 441 * for peer to send FIN or not respond to keep-alives, etc. 442 * We can let the user exit from the close as soon as the FIN is acked. 443 */ 444 struct tcpcb * 445 tcp_usrclosed(tp) 446 register struct tcpcb *tp; 447 { 448 449 switch (tp->t_state) { 450 451 case TCPS_CLOSED: 452 case TCPS_LISTEN: 453 case TCPS_SYN_SENT: 454 tp->t_state = TCPS_CLOSED; 455 tp = tcp_close(tp); 456 break; 457 458 case TCPS_SYN_RECEIVED: 459 case TCPS_ESTABLISHED: 460 tp->t_state = TCPS_FIN_WAIT_1; 461 break; 462 463 case TCPS_CLOSE_WAIT: 464 tp->t_state = TCPS_LAST_ACK; 465 break; 466 } 467 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) 468 soisdisconnected(tp->t_inpcb->inp_socket); 469 return (tp); 470 } 471