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