令人無比鬱悶的tcp Reset是如何產生的

前幾天“溫習”和“胡蘿蔔”一度在Google熱門詞彙裏面排在前兩位,因爲大家發現只要搜索一下這兩個詞,竟然Google網站就被Reset了。神奇的國度總是有些稀奇古怪的事情!

看看這個Reset是什麼來的吧,這裏的“Reset”其實是tcp的一種消息之一,用於重置連接,一般來說比如服務器端沒有客戶端請求的端口或者其他信息不符合時,系統的tcp協議棧就會給客戶端回覆一個reset通知消息。本來用於應對比如說服務器意外重啓等情況的。但很多防火牆系統也用來禁止客戶端的連接,如果防火牆發現網絡包的規則被觸發之後,就可以回一個reset消息讓客戶端的tcp連接被重置(也就是說斷開連接)。這樣比直接把網絡包丟棄要好,因爲直接丟棄的話,客戶端並不知道具體網絡狀況,由於tcp的重發和超時機制,客戶端就會在那裏不停的等待,客戶體驗很不好。而收到reset客戶端是可以知道網絡被斷開的就不用再等待了,瀏覽器也可以提示你連接被reset。很命令我們大名鼎鼎的GFW監控到http協議網絡包中的“胡蘿蔔”關鍵詞了,他就回復一個reset消息給我們了,而我們的客戶端卻分不清這個是服務器的回覆還是防火牆的虛假reset。

看看linux的iptable裏面是怎麼發送一個reset消息的吧。

http://lxr.linux.no/linux+v2.6.33/net/ipv4/netfilter/ipt_REJECT.c

/* Send RST reply */
35static void send_reset(struct sk_buff *oldskb, int hook)
36{
37        struct sk_buff *nskb;
38 const struct iphdr *oiph;
39        struct iphdr *niph;
40        const struct tcphdr *oth;
41        struct tcphdr _otcph, *tcph;
42        unsigned int addr_type;
43
44        /* IP header checks: fragment. */
45        if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
46                return;
47
48        oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
49                                 sizeof(_otcph), &_otcph);
50        if (oth == NULL)
51                return;
52
53        /* No RST for RST. */
54        if (oth->rst)
55                return;
56
57        /* Check checksum */
58        if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
59                return;
60        oiph = ip_hdr(oldskb);
61
62        nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
63                         LL_MAX_HEADER, GFP_ATOMIC); //新申請一個skb緩衝
64        if (!nskb)
65                return;
66
67        skb_reserve(nskb, LL_MAX_HEADER);
68
69        skb_reset_network_header(nskb);
70        niph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
71        niph->version   = 4;
72        niph->ihl       = sizeof(struct iphdr) / 4;
73        niph->tos       = 0;
74        niph->id        = 0;
75        niph->frag_off = htons(IP_DF);
76        niph->protocol = IPPROTO_TCP;
77        niph->check     = 0;
78        niph->saddr     = oiph->daddr;
79        niph->daddr     = oiph->saddr;
80
81        tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr));
82        memset(tcph, 0, sizeof(*tcph));
83        tcph->source    = oth->dest;              //根據需要reset的包反過來填寫接受地址和發送方地址
84        tcph->dest      = oth->source;
85        tcph->doff      = sizeof(struct tcphdr) / 4;
86
87        if (oth->ack)
88                tcph->seq = oth->ack_seq;     //
89        else {
90                tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
91                                      oldskb->len - ip_hdrlen(oldskb) -
92                                      (oth->doff << 2));
93                tcph->ack = 1;     //是不是迴應包,上面計算確認序號
94        }
95
96        tcph->rst       = 1;      //reset就是這樣來的 ?
97        tcph->check     = tcp_v4_check(sizeof(struct tcphdr),
98                                       niph->saddr, niph->daddr,
99                                       csum_partial(tcph,
100                                                    sizeof(struct tcphdr), 0));    //計算tcp校驗
101
102        addr_type = RTN_UNSPEC;
103        if (hook != NF_INET_FORWARD
104#ifdef CONFIG_BRIDGE_NETFILTER
105            || (nskb->nf_bridge && nskb->nf_bridge->mask & BRNF_BRIDGED)
106#endif
107           )
108                addr_type = RTN_LOCAL;
109
110        /* ip_route_me_harder expects skb->dst to be set */
111        skb_dst_set(nskb, dst_clone(skb_dst(oldskb)));
112
113        if (ip_route_me_harder(nskb, addr_type))
114                goto free_nskb;
115
116        niph->ttl       = dst_metric(skb_dst(nskb), RTAX_HOPLIMIT);
117        nskb->ip_summed = CHECKSUM_NONE;
118
119        /* "Never happens" */
120        if (nskb->len > dst_mtu(skb_dst(nskb)))
121                goto free_nskb;
122
123        nf_ct_attach(nskb, oldskb);
124
125        ip_local_out(nskb);   //把reset包發送出去
126        return;
127
128 free_nskb:
129        kfree_skb(nskb);
130}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章