[NETFILTER]: replace open coded checksum updates
[linux-2.6] / net / ipv4 / netfilter / ipt_TOS.c
1 /* This is a module which is used for setting the TOS field of a packet. */
2
3 /* (C) 1999-2001 Paul `Rusty' Russell
4  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/ip.h>
14 #include <net/checksum.h>
15
16 #include <linux/netfilter_ipv4/ip_tables.h>
17 #include <linux/netfilter_ipv4/ipt_TOS.h>
18
19 MODULE_LICENSE("GPL");
20 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
21 MODULE_DESCRIPTION("iptables TOS mangling module");
22
23 static unsigned int
24 target(struct sk_buff **pskb,
25        const struct net_device *in,
26        const struct net_device *out,
27        unsigned int hooknum,
28        const struct xt_target *target,
29        const void *targinfo,
30        void *userinfo)
31 {
32         const struct ipt_tos_target_info *tosinfo = targinfo;
33         struct iphdr *iph = (*pskb)->nh.iph;
34         u_int16_t oldtos;
35
36         if ((iph->tos & IPTOS_TOS_MASK) != tosinfo->tos) {
37                 if (!skb_make_writable(pskb, sizeof(struct iphdr)))
38                         return NF_DROP;
39                 iph = (*pskb)->nh.iph;
40                 oldtos = iph->tos;
41                 iph->tos = (iph->tos & IPTOS_PREC_MASK) | tosinfo->tos;
42                 iph->check = nf_csum_update(oldtos ^ 0xFFFF, iph->tos,
43                                             iph->check);
44         }
45         return IPT_CONTINUE;
46 }
47
48 static int
49 checkentry(const char *tablename,
50            const void *e_void,
51            const struct xt_target *target,
52            void *targinfo,
53            unsigned int targinfosize,
54            unsigned int hook_mask)
55 {
56         const u_int8_t tos = ((struct ipt_tos_target_info *)targinfo)->tos;
57
58         if (tos != IPTOS_LOWDELAY
59             && tos != IPTOS_THROUGHPUT
60             && tos != IPTOS_RELIABILITY
61             && tos != IPTOS_MINCOST
62             && tos != IPTOS_NORMALSVC) {
63                 printk(KERN_WARNING "TOS: bad tos value %#x\n", tos);
64                 return 0;
65         }
66         return 1;
67 }
68
69 static struct ipt_target ipt_tos_reg = {
70         .name           = "TOS",
71         .target         = target,
72         .targetsize     = sizeof(struct ipt_tos_target_info),
73         .table          = "mangle",
74         .checkentry     = checkentry,
75         .me             = THIS_MODULE,
76 };
77
78 static int __init ipt_tos_init(void)
79 {
80         return ipt_register_target(&ipt_tos_reg);
81 }
82
83 static void __exit ipt_tos_fini(void)
84 {
85         ipt_unregister_target(&ipt_tos_reg);
86 }
87
88 module_init(ipt_tos_init);
89 module_exit(ipt_tos_fini);