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