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