/*------------------------------------------------------------------------ * tcpreset - generate a reset in response to a bad packet *------------------------------------------------------------------------ */ int tcpreset(struct ep *pepin) { struct ep *pepout; struct ip *pipin = (struct ip *)pepin->ep_data, *pipout; struct tcp *ptcpin = (struct tcp *)pipin->ip_data, *ptcpout; int datalen; if (ptcpin->tcp_code & TCPF_RST) return OK; /* no RESETs on RESETs */ pepout = (struct ep *)getbuf(Net.netpool); if ((int)pepout == SYSERR) return SYSERR; pipout = (struct ip *)pepout->ep_data; pipout->ip_src = pipin->ip_dst; pipout->ip_dst = pipin->ip_src; ptcpout = (struct tcp *)pipout->ip_data; ptcpout->tcp_sport = ptcpin->tcp_dport; ptcpout->tcp_dport = ptcpin->tcp_sport; if (ptcpin->tcp_code & TCPF_ACK) { ptcpout->tcp_seq = ptcpin->tcp_ack; ptcpout->tcp_code = TCPF_RST; } else { ptcpout->tcp_seq = 0; ptcpout->tcp_code = TCPF_RST|TCPF_ACK; } datalen = pipin->ip_len - IP_HLEN(pipin) - TCP_HLEN(ptcpin); if (ptcpin->tcp_code & TCPF_SYN) datalen++; if (ptcpin->tcp_code & TCPF_FIN) datalen++; ptcpout->tcp_ack = ptcpin->tcp_seq + datalen; ptcpout->tcp_offset = TCPHOFFSET; ptcpout->tcp_window = ptcpout->tcp_urgptr = 0; tcph2net(ptcpout); ptcpout->tcp_cksum = 0; ptcpout->tcp_cksum = tcpcksum(pepout, TCPMHLEN); TcpOutSegs++; TcpOutRsts++; return ipsend(pipin->ip_src, pepout, TCPMHLEN, IPT_TCP, IPP_NORMAL, IP_TTL); }
/*------------------------------------------------------------------------ * tcpinp - handle TCP segment coming in from IP *------------------------------------------------------------------------ */ PROCESS tcpinp(void) { struct ep *pep; struct ip *pip; struct tcp *ptcp; struct tcb *ptcb; tcps_iport = pcreate(TCPQLEN); signal(Net.sema); while (TRUE) { pep = (struct ep *)preceive(tcps_iport); if ((int)pep == SYSERR) break; pip = (struct ip *)pep->ep_data; if (tcpcksum(pep, pip->ip_len - IP_HLEN(pip))) { ++TcpInErrs; freebuf(pep); continue; } ptcp = (struct tcp *)pip->ip_data; tcpnet2h(ptcp); /* convert all fields to host order */ pep->ep_order |= EPO_TCP; ptcb = tcpdemux(pep); if (ptcb == 0) { ++TcpInErrs; tcpreset(pep); freebuf(pep); continue; } if (!tcpok(ptcb, pep)) tcpackit(ptcb, pep); else { tcpopts(ptcb, pep); tcpswitch[ptcb->tcb_state](ptcb, pep); } if (ptcb->tcb_state != TCPS_FREE) signal(ptcb->tcb_mutex); freebuf(pep); } }
/*------------------------------------------------------------------------ * tcpackit - generate an ACK for a received TCP packet *------------------------------------------------------------------------ */ int tcpackit(struct tcb *ptcb, struct ep *pepin) { struct ep *pepout; struct ip *pipin = (struct ip *)pepin->ep_data, *pipout; struct tcp *ptcpin = (struct tcp *)pipin->ip_data, *ptcpout; if (ptcpin->tcp_code & TCPF_RST) return OK; if (pipin->ip_len <= IP_HLEN(pipin) + TCP_HLEN(ptcpin) && !(ptcpin->tcp_code & (TCPF_SYN|TCPF_FIN))) return OK; /* duplicate ACK */ pepout = (struct ep *)getbuf(Net.netpool); if ((int)pepout == SYSERR) return SYSERR; pepout->ep_order = ~0; pipout = (struct ip *)pepout->ep_data; pipout->ip_src = pipin->ip_dst; pipout->ip_dst = pipin->ip_src; ptcpout = (struct tcp *)pipout->ip_data; ptcpout->tcp_sport = ptcpin->tcp_dport; ptcpout->tcp_dport = ptcpin->tcp_sport; ptcpout->tcp_seq = ptcb->tcb_snext; ptcpout->tcp_ack = ptcb->tcb_rnext; ptcpout->tcp_code = TCPF_ACK; ptcpout->tcp_offset = TCPHOFFSET; ptcpout->tcp_window = tcprwindow(ptcb); ptcpout->tcp_urgptr = 0; ptcpout->tcp_cksum = 0; tcph2net(ptcpout); pepout->ep_order &= ~EPO_TCP; ptcpout->tcp_cksum = tcpcksum(pepout, TCPMHLEN); TcpOutSegs++; return ipsend(pipin->ip_src, pepout, TCPMHLEN, IPT_TCP, IPP_NORMAL, IP_TTL); }