如何在不破坏IP头的情况下更改IP头中的TOS位

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位的值时,我的互联网就停止工作了。 是否有其他方法或者有人能指出我做错了什么将非常有帮助。
2个回答

0

您可以使用增量IP校验和,即:由于IP校验和一个总和,因此您可以减去旧值并加上新值。 Linux通过net/checksum.h中的csum_replace*()函数支持该功能。只需执行以下操作:

csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接