3

如何更改 ip 标头中 tos 位的值?我尝试使用 iphdr 结构直接修改它。它是在内核级别使用 netfilter 完成的。我写的代码如下:

// Including the required header files

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/init.h>
#include <linux/kthread.h>
#include <linux/signal.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/types.h>
#include <linux/netdevice.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/in.h>
#include <linux/inet.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/socket.h>
#include <linux/net.h>
#include <net/sock.h>
#include <asm/processor.h>
#include <asm/uaccess.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/proc_fs.h>

// Defining the name of the module
#define MODULE_NAME "TOS_setter"

// Structure definition for processing outgoing packets
static struct nf_hook_ops nf_ops_out;

//===========================================================
// Function that modifies the TOS bits
unsigned int tos_setter(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;
unsigned char tosbits;

if (!skb)return NF_ACCEPT;

iph = ip_hdr(skb);

if (!iph)return NF_ACCEPT;
//if(iph->protocol==IPPROTO_TCP)return NF_ACCEPT;

if (iph->protocol==IPPROTO_TCP)
    { 
printk(KERN_ALERT " The total length is : %d\n" , (int)iph->tot_len);
iph->tos = tosbits | (unsigned char)2;
tosbits = iph->tos;
printk(KERN_ALERT " The tos bits are : %d\n" , (int)tosbits);
printk(KERN_ALERT " The total length is : %d\n" , (int)iph->tot_len);
}
return NF_ACCEPT;
}


//===========================================================
//Initialisation function
static int __init init(void){

   printk(KERN_ALERT " Initialization Started \n");

// Initialize Hook Functions
   nf_ops_out.hook = tos_setter;
   nf_ops_out.pf = PF_INET;
   nf_ops_out.hooknum =4;
   nf_ops_out.priority = NF_IP_PRI_FIRST; 
// Register the Hook functions   
   nf_register_hook(&nf_ops_out);
   printk(KERN_ALERT "hook functions registered\n");

   printk(KERN_ALERT " KSTAT Initialization Completed \n");
   return 0;
}

static void __exit cleanup(void){

  printk(KERN_ALERT "KSTAT Exit Started\n");

// Unregister the hook functions
  nf_unregister_hook(&nf_ops_out);
  printk("Unregistered the hooks\n");


  printk(KERN_ALERT "KSTAT Exit Completed\n");
}  

/* init and cleanup functions */
module_init(init);
module_exit(cleanup);

MODULE_LICENSE("GPL");

问题是,当我尝试更改 tos 位的值时,我的互联网停止工作。还有其他方法吗,或者如果有人可以指出我做错了什么,那将非常有帮助。

4

2 回答 2

0

更改标头中的任何内容后,您必须重新计算 IP 标头校验和。

请参阅 WP:http ://en.wikipedia.org/wiki/IPv4_header_checksum 代码在这里:如何计算 RFC 791 IP 标头校验和?

于 2012-12-02T16:29:57.607 回答
0

您可以使用增量IP校验和,即:由于IP校验和一个和,您可以减去旧值并添加新值。Linuxcsum_replace*()通过net/checksum.h. 做就是了:

csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));
于 2012-12-02T17:04:07.553 回答