📄 tcp_output.c
字号:
sigoff = optlen + 2; { unsigned int i; for (i = 0; i < 16; i++) *(bp++) = 0; } optlen += TCPOLEN_SIGNATURE; /* Pad options list to the next 32 bit boundary and * terminate it. */ *bp++ = TCPOPT_NOP; *bp++ = TCPOPT_EOL; optlen += 2; }#endif /* TCP_SIGNATURE */#ifdef TCP_SACK /* * Send SACKs if necessary. This should be the last option processed. * Only as many SACKs are sent as are permitted by the maximum options * size. No more than three SACKs are sent. */ if (!tp->sack_disable && tp->t_state == TCPS_ESTABLISHED && (tp->t_flags & (TF_SACK_PERMIT|TF_NOOPT)) == TF_SACK_PERMIT && tp->rcv_numsacks) { u_int32_t *lp = (u_int32_t *)(opt + optlen); u_int32_t *olp = lp++; int count = 0; /* actual number of SACKs inserted */ int maxsack = (MAX_TCPOPTLEN - (optlen + 4))/TCPOLEN_SACK; maxsack = min(maxsack, TCP_MAX_SACK); for (i = 0; (i < tp->rcv_numsacks && count < maxsack); i++) { struct sackblk sack = tp->sackblks[i]; if (sack.start == 0 && sack.end == 0) continue; *lp++ = htonl(sack.start); *lp++ = htonl(sack.end); count++; } *olp = htonl(TCPOPT_SACK_HDR|(TCPOLEN_SACK*count+2)); optlen += TCPOLEN_SACK*count + 4; /* including leading NOPs */ }#endif /* TCP_SACK */#ifdef DIAGNOSTIC if (optlen > MAX_TCPOPTLEN) panic("tcp_output: options too long");#endif /* DIAGNOSTIC */ hdrlen += optlen; /* * Adjust data length if insertion of options will * bump the packet length beyond the t_maxopd length. */ if (len > tp->t_maxopd - optlen) { len = tp->t_maxopd - optlen; sendalot = 1; flags &= ~TH_FIN; }#ifdef DIAGNOSTIC if (max_linkhdr + hdrlen > MCLBYTES) panic("tcphdr too big");#endif /* * Grab a header mbuf, attaching a copy of data to * be transmitted, and initialize the header from * the template for sends on this connection. */ if (len) { if (tp->t_force && len == 1) tcpstat.tcps_sndprobe++; else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { tcpstat.tcps_sndrexmitpack++; tcpstat.tcps_sndrexmitbyte += len; } else { tcpstat.tcps_sndpack++; tcpstat.tcps_sndbyte += len; }#ifdef notyet if ((m = m_copypack(so->so_snd.sb_mb, off, (int)len, max_linkhdr + hdrlen)) == 0) { error = ENOBUFS; goto out; } /* * m_copypack left space for our hdr; use it. */ m->m_len += hdrlen; m->m_data -= hdrlen;#else MGETHDR(m, M_DONTWAIT, MT_HEADER); if (m != NULL) { MCLGET(m, M_DONTWAIT); if ((m->m_flags & M_EXT) == 0) { m_freem(m); m = NULL; } } if (m == NULL) { error = ENOBUFS; goto out; } m->m_data += max_linkhdr; m->m_len = hdrlen; if (len <= MCLBYTES - hdrlen - max_linkhdr) { m_copydata(so->so_snd.sb_mb, off, (int) len, mtod(m, caddr_t) + hdrlen); m->m_len += len; } else { m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); if (m->m_next == 0) { (void) m_free(m); error = ENOBUFS; goto out; } }#endif /* * If we're sending everything we've got, set PUSH. * (This will keep happy those implementations which only * give data to the user when a buffer fills or * a PUSH comes in.) */ if (off + len == so->so_snd.sb_cc) flags |= TH_PUSH; } else { if (tp->t_flags & TF_ACKNOW) tcpstat.tcps_sndacks++; else if (flags & (TH_SYN|TH_FIN|TH_RST)) tcpstat.tcps_sndctrl++; else if (SEQ_GT(tp->snd_up, tp->snd_una)) tcpstat.tcps_sndurg++; else tcpstat.tcps_sndwinup++; MGETHDR(m, M_DONTWAIT, MT_HEADER); if (m != NULL) { MCLGET(m, M_DONTWAIT); if ((m->m_flags & M_EXT) == 0) { m_freem(m); m = NULL; } } if (m == NULL) { error = ENOBUFS; goto out; } m->m_data += max_linkhdr; m->m_len = hdrlen; } m->m_pkthdr.rcvif = (struct ifnet *)0; if (!tp->t_template) panic("tcp_output");#ifdef DIAGNOSTIC if (tp->t_template->m_len != hdrlen - optlen) panic("tcp_output: template len != hdrlen - optlen");#endif /* DIAGNOSTIC */ bcopy(mtod(tp->t_template, caddr_t), mtod(m, caddr_t), tp->t_template->m_len); th = (struct tcphdr *)(mtod(m, caddr_t) + tp->t_template->m_len - sizeof(struct tcphdr)); /* * Fill in fields, remembering maximum advertised * window for use in delaying messages about window sizes. * If resending a FIN, be sure not to use a new sequence number. */ if ((flags & TH_FIN) && (tp->t_flags & TF_SENTFIN) && (tp->snd_nxt == tp->snd_max)) tp->snd_nxt--; /* * If we are doing retransmissions, then snd_nxt will * not reflect the first unsent octet. For ACK only * packets, we do not want the sequence number of the * retransmitted packet, we want the sequence number * of the next unsent octet. So, if there is no data * (and no SYN or FIN), use snd_max instead of snd_nxt * when filling in ti_seq. But if we are in persist * state, snd_max might reflect one byte beyond the * right edge of the window, so use snd_nxt in that * case, since we know we aren't doing a retransmission. * (retransmit and persist are mutually exclusive...) */ if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST]) th->th_seq = htonl(tp->snd_nxt); else th->th_seq = htonl(tp->snd_max);#ifdef TCP_SACK if (sack_rxmit) { /* * If sendalot was turned on (due to option stuffing), turn it * off. Properly set th_seq field. Advance the ret'x pointer * by len. */ if (sendalot) sendalot = 0; th->th_seq = htonl(p->rxmit); p->rxmit += len;#if defined(TCP_SACK) && defined(TCP_FACK) tp->retran_data += len;#endif /* TCP_FACK */ }#endif /* TCP_SACK */ th->th_ack = htonl(tp->rcv_nxt); if (optlen) { bcopy((caddr_t)opt, (caddr_t)(th + 1), optlen); th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; } th->th_flags = flags; /* * Calculate receive window. Don't shrink window, * but avoid silly window syndrome. */ if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg) win = 0; if (win > (long)TCP_MAXWIN << tp->rcv_scale) win = (long)TCP_MAXWIN << tp->rcv_scale; if (win < (long)(tp->rcv_adv - tp->rcv_nxt)) win = (long)(tp->rcv_adv - tp->rcv_nxt); if (flags & TH_RST) win = 0; th->th_win = htons((u_int16_t) (win>>tp->rcv_scale)); if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { u_int32_t urp = tp->snd_up - tp->snd_nxt; if (urp > IP_MAXPACKET) urp = IP_MAXPACKET; th->th_urp = htons((u_int16_t)urp); th->th_flags |= TH_URG; } else /* * If no urgent pointer to send, then we pull * the urgent pointer to the left edge of the send window * so that it doesn't drift into the send window on sequence * number wraparound. */ tp->snd_up = tp->snd_una; /* drag it along */ /* Put TCP length in pseudo-header */ switch (tp->pf) { case 0: /*default to PF_INET*/#ifdef INET case AF_INET: if (len + optlen) mtod(m, struct ipovly *)->ih_len = htons((u_int16_t)( sizeof (struct tcphdr) + optlen + len)); break;#endif /* INET */#ifdef INET6 case AF_INET6: break;#endif /* INET6 */ }#ifdef TCP_SIGNATURE if (tp->t_flags & TF_SIGNATURE) { MD5_CTX ctx; union sockaddr_union sa; struct tdb *tdb; bzero(&sa, sizeof(union sockaddr_union)); switch (tp->pf) { case 0: /*default to PF_INET*/#ifdef INET case AF_INET: sa.sa.sa_len = sizeof(struct sockaddr_in); sa.sa.sa_family = AF_INET; sa.sin.sin_addr = mtod(m, struct ip *)->ip_dst; break;#endif /* INET */#ifdef INET6 case AF_INET6: sa.sa.sa_len = sizeof(struct sockaddr_in6); sa.sa.sa_family = AF_INET6; sa.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_dst; break;#endif /* INET6 */ } /* XXX gettdb() should really be called at spltdb(). */ /* XXX this is splsoftnet(), currently they are the same. */ tdb = gettdb(0, &sa, IPPROTO_TCP); if (tdb == NULL) return (EPERM); MD5Init(&ctx); switch (tp->pf) { case 0: /*default to PF_INET*/#ifdef INET case AF_INET: { struct ippseudo ippseudo; struct ipovly *ipovly; ipovly = mtod(m, struct ipovly *); ippseudo.ippseudo_src = ipovly->ih_src; ippseudo.ippseudo_dst = ipovly->ih_dst; ippseudo.ippseudo_pad = 0; ippseudo.ippseudo_p = IPPROTO_TCP; ippseudo.ippseudo_len = ipovly->ih_len; MD5Update(&ctx, (char *)&ippseudo, sizeof(struct ippseudo)); MD5Update(&ctx, mtod(m, caddr_t) + sizeof(struct ip), sizeof(struct tcphdr)); } break;#endif /* INET */#ifdef INET6 case AF_INET6: { static int printed = 0; if (!printed) { printf("error: TCP MD5 support for " "IPv6 not yet implemented.\n"); printed = 1; } } break;#endif /* INET6 */ } if (len && m_apply(m, hdrlen, len, tcp_signature_apply, (caddr_t)&ctx)) return (EINVAL); MD5Update(&ctx, tdb->tdb_amxkey, tdb->tdb_amxkeylen); MD5Final(mtod(m, caddr_t) + hdrlen - optlen + sigoff, &ctx); }#endif /* TCP_SIGNATURE */ /* * Put TCP length in extended header, and then * checksum extended header and data. */ switch (tp->pf) { case 0: /*default to PF_INET*/#ifdef INET case AF_INET: th->th_sum = in_cksum(m, (int)(hdrlen + len)); break;#endif /* INET */#ifdef INET6 case AF_INET6: m->m_pkthdr.len = hdrlen + len; th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), hdrlen - sizeof(struct ip6_hdr) + len); break;#endif /* INET6 */ } /* * In transmit state, time the transmission and arrange for * the retransmit. In persist state, just set snd_max. */ if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { tcp_seq startseq = tp->snd_nxt; /* * Advance snd_nxt over sequence space of this segment. */ if (flags & (TH_SYN|TH_FIN)) { if (flags & TH_SYN) tp->snd_nxt++; if (flags & TH_FIN) { tp->snd_nxt++; tp->t_flags |= TF_SENTFIN; } }#ifdef TCP_SACK if (!tp->sack_disable) { if (sack_rxmit && (p->rxmit != tp->snd_nxt)) { goto timer; } }#endif tp->snd_nxt += len; if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { tp->snd_max = tp->snd_nxt; /* * Time this transmission if not a retransmission and * not currently timing anything. */ if (tp->t_rtt == 0) { tp->t_rtt = 1; tp->t_rtseq = startseq; tcpstat.tcps_segstimed++; } } /* * Set retransmit timer if not currently set, * and not doing an ack or a keep-alive probe. * Initial value for retransmit timer is smoothed * round-trip time + 2 * round-trip time variance. * Initialize shift counter which is used for backoff * of retransmit time. */#ifdef TCP_SACK timer: if (!tp->sack_disable && sack_rxmit && tp->t_timer[TCPT_REXMT] == 0 && tp->snd_nxt != tp->snd_max) { tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; if (tp->t_timer[TCPT_PERSIST]) { tp->t_timer[TCPT_PERSIST] = 0; tp->t_rxtshift = 0; } }#endif if (tp->t_timer[TCPT_REXMT] == 0 && tp->snd_nxt != tp->snd_una) { tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; if (tp->t_timer[TCPT_PERSIST]) { tp->t_timer[TCPT_PERSIST] = 0; tp->t_rxtshift = 0; } } } else if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) tp->snd_max = tp->snd_nxt + len; /* * Trace. */ if (so->so_options & SO_DEBUG) tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, caddr_t), 0, len); /* * Fill in IP length and desired time to live and * send to IP level. There should be a better way * to handle ttl and tos; we could keep them in * the template, but need a way to checksum without them. */ m->m_pkthdr.len = hdrlen + len; switch (tp->pf) { case 0: /*default to PF_INET*/#ifdef INET case AF_INET: { struct ip *ip; ip = mtod(m, struct ip *); ip->ip_len = m->m_pkthdr.len; ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl; ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos; } error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, so->so_options & SO_DONTROUTE, 0, tp->t_inpcb); break;#endif /* INET */#ifdef INET6 case AF_INET6: { struct ip6_hdr *ipv6; ipv6 = mtod(m, struct ip6_hdr *); ipv6->ip6_plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); ipv6->ip6_nxt = IPPROTO_TCP; ipv6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL); } error = ip6_output(m, tp->t_inpcb->inp_outputopts6, &tp->t_inpcb->inp_route6, (so->so_options & SO_DONTROUTE), NULL, NULL); break;#endif /* INET6 */#ifdef TUBA case AF_ISO: if (tp->t_tuba_pcb) error = tuba_output(m, tp); break;#endif /* TUBA */ }#if defined(TCP_SACK) && defined(TCP_FACK) /* Update snd_awnd to reflect the new data that was sent. */ tp->snd_awnd = tcp_seq_subtract(tp->snd_max, tp->snd_fack) + tp->retran_data; #endif /* defined(TCP_SACK) && defined(TCP_FACK) */ if (error) {out: if (error == ENOBUFS) { tcp_quench(tp->t_inpcb, 0); return (0); } if ((error == EHOSTUNREACH || error == ENETDOWN) && TCPS_HAVERCVDSYN(tp->t_state)) { tp->t_softerror = error; return (0); } return (error); } tcpstat.tcps_sndtotal++; /* * Data sent (as far as we can tell). * If this advertises a larger window than any other segment, * then remember the size of the advertised window. * Any pending ACK has now been sent. */ if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) tp->rcv_adv = tp->rcv_nxt + win; tp->last_ack_sent = tp->rcv_nxt; tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);#if defined(TCP_SACK) if (sendalot && --maxburst)#else if (sendalot)#endif goto again; return (0);}voidtcp_setpersist(tp) register struct tcpcb *tp;{ register int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; if (tp->t_timer[TCPT_REXMT]) panic("tcp_output REXMT"); /* * Start/restart persistance timer. */ if (t < tp->t_rttmin) t = tp->t_rttmin; TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], t * tcp_backoff[tp->t_rxtshift], TCPTV_PERSMIN, TCPTV_PERSMAX); if (tp->t_rxtshift < TCP_MAXRXTSHIFT) tp->t_rxtshift++;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -