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