title: how to change the tos bits in ip header without corrupting the header
body: <p>How to change the value of the tos bits in the ip header?
  I tried directly modifying it using the iphdr structure.
  It  is done at the kernel level using netfilter.
  The code I wrote is as follows:</p>
  
  <pre><code>// Including the required header files
  
  #include &lt;linux/kernel.h&gt;
  #include &lt;linux/module.h&gt;
  #include &lt;linux/netfilter.h&gt;
  #include &lt;linux/netfilter_ipv4.h&gt;
  #include &lt;linux/init.h&gt;
  #include &lt;linux/kthread.h&gt;
  #include &lt;linux/signal.h&gt;
  #include &lt;linux/errno.h&gt;
  #include &lt;linux/sched.h&gt;
  #include &lt;linux/wait.h&gt;
  #include &lt;linux/types.h&gt;
  #include &lt;linux/netdevice.h&gt;
  #include &lt;linux/ip.h&gt;
  #include &lt;linux/tcp.h&gt;
  #include &lt;linux/in.h&gt;
  #include &lt;linux/inet.h&gt;
  #include &lt;linux/delay.h&gt;
  #include &lt;linux/string.h&gt;
  #include &lt;linux/socket.h&gt;
  #include &lt;linux/net.h&gt;
  #include &lt;net/sock.h&gt;
  #include &lt;asm/processor.h&gt;
  #include &lt;asm/uaccess.h&gt;
  #include &lt;linux/file.h&gt;
  #include &lt;linux/fs.h&gt;
  #include &lt;linux/stat.h&gt;
  #include &lt;linux/proc_fs.h&gt;
  
  // 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-&gt;protocol==IPPROTO_TCP)return NF_ACCEPT;
  
  if (iph-&gt;protocol==IPPROTO_TCP)
      { 
  printk(KERN_ALERT " The total length is : %d\n" , (int)iph-&gt;tot_len);
  iph-&gt;tos = tosbits | (unsigned char)2;
  tosbits = iph-&gt;tos;
  printk(KERN_ALERT " The tos bits are : %d\n" , (int)tosbits);
  printk(KERN_ALERT " The total length is : %d\n" , (int)iph-&gt;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(&amp;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(&amp;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");
  </code></pre>
  
  <p>The problem is that when I try to change the value of the tos bits my internet stops working.
  Is there any other way or if anyone can point out what i am doing wrong it will be really helpful.</p>