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