- 論壇徽章:
- 0
|
本帖最后由 flash78910 于 2017-11-08 15:52 編輯
我在LOCAL_OUT點(diǎn)處對(duì)TCP數(shù)據(jù)包的數(shù)據(jù)段進(jìn)行擴(kuò)展,使用skb_put函數(shù)擴(kuò)展16個(gè)字節(jié),并插入16字節(jié)的數(shù)據(jù),同時(shí)更新了包的校驗(yàn)和。我在函數(shù)里只有一次memcpy動(dòng)作。為什么抓包顯示有多次重復(fù)的插入數(shù)據(jù)出現(xiàn)?以下是這個(gè)鉤子函數(shù),插入的內(nèi)容為insert,插入位置為TCP數(shù)據(jù)開(kāi)頭部分。謝謝各位,請(qǐng)不吝賜教!
- unsigned int hook_func(unsigned int hooknum, struct sk_buff *skb, const struct net_device *in, const struct net_device *out, int (*okfn) (struct sk_buff *))
- {
- struct iphdr *iph = NULL;
- unsigned char *tcp_ins = NULL; //tcp插入指針
- struct tcphdr *tcph = NULL; //tcp頭
- unsigned int tcp_len = 0; //tcp報(bào)文長(zhǎng)度
- unsigned int data_len = 0; //tcp數(shù)據(jù)部分長(zhǎng)度
- unsigned char insert[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; //插入數(shù)據(jù)
- if(skb == NULL)
- return NF_ACCEPT;
- if(is_ign_pkt(skb)==1) //過(guò)濾需要修改的數(shù)據(jù)包,此后都是要修改的數(shù)據(jù)包
- return NF_ACCEPT;
- if(skb_is_nonlinear(skb)) //線性化skb
- {
- if(skb_linearize(skb) != 0)
- return NF_ACCEPT;
- }
- iph = ip_hdr(skb); //skb的ip頭
- if(iph == NULL)
- return NF_ACCEPT;
- if(iph->protocol==TCP)
- {
- if(skb_tailroom(skb)<=16) //判斷tailroom空間
- {
- printk("TCP NOT Enough\n");
- }
- else
- {
- printk("TCP tail: 0x%u\n",skb->tail);
- skb_put(skb,16); //數(shù)據(jù)段空間擴(kuò)展
- printk("TCP new_tail: 0x%u\n",skb->tail);
- printk("TCP------tailroom: %u------\n",skb_tailroom(skb));
- tcph = tcp_hdr(skb);
- tcp_ins = (unsigned char *)tcph + (tcph->doff*4); //指向數(shù)據(jù)起始處
- iph->tot_len = htons(ntohs(iph->tot_len)+16);
- tcp_len = ntohs(iph->tot_len) - iph->ihl*4;
- data_len = tcp_len - (tcph->doff*4);
- memmove(tcp_ins+16, tcp_ins, data_len); //原始數(shù)據(jù)后移留出插入空間
- memcpy(tcp_ins, insert, 16); //拷貝插入數(shù)據(jù)
- tcph->check = 0;
- skb->csum = 0;
- iph->check = 0;
- skb->csum = skb_checksum(skb, iph->ihl*4,ntohs(iph->tot_len)-iph->ihl*4,0);
- tcph->check = csum_tcpudp_magic(iph->saddr, iph->daddr, tcp_len, iph->protocol, skb->csum);
- iph->check = ip_fast_csum(iph, iph->ihl);
- }
- return NF_ACCEPT;
- }
- return NF_ACCEPT;
- }
復(fù)制代碼
|
|