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