[NETFILTER]: Get rid of HW checksum invalidation
[linux-2.6] / net / ipv4 / netfilter / ipt_ECN.c
1 /* iptables module for the IPv4 and TCP ECN bits, Version 1.5
2  *
3  * (C) 2002 by Harald Welte <laforge@netfilter.org>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as 
7  * published by the Free Software Foundation.
8  *
9  * ipt_ECN.c,v 1.5 2002/08/18 19:36:51 laforge Exp
10 */
11
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/ip.h>
15 #include <linux/tcp.h>
16 #include <net/checksum.h>
17
18 #include <linux/netfilter_ipv4/ip_tables.h>
19 #include <linux/netfilter_ipv4/ipt_ECN.h>
20
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
23 MODULE_DESCRIPTION("iptables ECN modification module");
24
25 /* set ECT codepoint from IP header.
26  *      return 0 if there was an error. */
27 static inline int
28 set_ect_ip(struct sk_buff **pskb, const struct ipt_ECN_info *einfo)
29 {
30         if (((*pskb)->nh.iph->tos & IPT_ECN_IP_MASK)
31             != (einfo->ip_ect & IPT_ECN_IP_MASK)) {
32                 u_int16_t diffs[2];
33
34                 if (!skb_make_writable(pskb, sizeof(struct iphdr)))
35                         return 0;
36
37                 diffs[0] = htons((*pskb)->nh.iph->tos) ^ 0xFFFF;
38                 (*pskb)->nh.iph->tos &= ~IPT_ECN_IP_MASK;
39                 (*pskb)->nh.iph->tos |= (einfo->ip_ect & IPT_ECN_IP_MASK);
40                 diffs[1] = htons((*pskb)->nh.iph->tos);
41                 (*pskb)->nh.iph->check
42                         = csum_fold(csum_partial((char *)diffs,
43                                                  sizeof(diffs),
44                                                  (*pskb)->nh.iph->check
45                                                  ^0xFFFF));
46         } 
47         return 1;
48 }
49
50 /* Return 0 if there was an error. */
51 static inline int
52 set_ect_tcp(struct sk_buff **pskb, const struct ipt_ECN_info *einfo)
53 {
54         struct tcphdr _tcph, *tcph;
55         u_int16_t oldval;
56
57         /* Not enought header? */
58         tcph = skb_header_pointer(*pskb, (*pskb)->nh.iph->ihl*4,
59                                   sizeof(_tcph), &_tcph);
60         if (!tcph)
61                 return 0;
62
63         if ((!(einfo->operation & IPT_ECN_OP_SET_ECE) ||
64              tcph->ece == einfo->proto.tcp.ece) &&
65             ((!(einfo->operation & IPT_ECN_OP_SET_CWR) ||
66              tcph->cwr == einfo->proto.tcp.cwr)))
67                 return 1;
68
69         if (!skb_make_writable(pskb, (*pskb)->nh.iph->ihl*4+sizeof(*tcph)))
70                 return 0;
71         tcph = (void *)(*pskb)->nh.iph + (*pskb)->nh.iph->ihl*4;
72
73         oldval = ((u_int16_t *)tcph)[6];
74         if (einfo->operation & IPT_ECN_OP_SET_ECE)
75                 tcph->ece = einfo->proto.tcp.ece;
76         if (einfo->operation & IPT_ECN_OP_SET_CWR)
77                 tcph->cwr = einfo->proto.tcp.cwr;
78
79         tcph->check = nf_proto_csum_update((*pskb),
80                                            oldval ^ 0xFFFF,
81                                            ((u_int16_t *)tcph)[6],
82                                            tcph->check, 0);
83         return 1;
84 }
85
86 static unsigned int
87 target(struct sk_buff **pskb,
88        const struct net_device *in,
89        const struct net_device *out,
90        unsigned int hooknum,
91        const struct xt_target *target,
92        const void *targinfo,
93        void *userinfo)
94 {
95         const struct ipt_ECN_info *einfo = targinfo;
96
97         if (einfo->operation & IPT_ECN_OP_SET_IP)
98                 if (!set_ect_ip(pskb, einfo))
99                         return NF_DROP;
100
101         if (einfo->operation & (IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR)
102             && (*pskb)->nh.iph->protocol == IPPROTO_TCP)
103                 if (!set_ect_tcp(pskb, einfo))
104                         return NF_DROP;
105
106         return IPT_CONTINUE;
107 }
108
109 static int
110 checkentry(const char *tablename,
111            const void *e_void,
112            const struct xt_target *target,
113            void *targinfo,
114            unsigned int targinfosize,
115            unsigned int hook_mask)
116 {
117         const struct ipt_ECN_info *einfo = (struct ipt_ECN_info *)targinfo;
118         const struct ipt_entry *e = e_void;
119
120         if (einfo->operation & IPT_ECN_OP_MASK) {
121                 printk(KERN_WARNING "ECN: unsupported ECN operation %x\n",
122                         einfo->operation);
123                 return 0;
124         }
125         if (einfo->ip_ect & ~IPT_ECN_IP_MASK) {
126                 printk(KERN_WARNING "ECN: new ECT codepoint %x out of mask\n",
127                         einfo->ip_ect);
128                 return 0;
129         }
130         if ((einfo->operation & (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR))
131             && (e->ip.proto != IPPROTO_TCP || (e->ip.invflags & IPT_INV_PROTO))) {
132                 printk(KERN_WARNING "ECN: cannot use TCP operations on a "
133                        "non-tcp rule\n");
134                 return 0;
135         }
136         return 1;
137 }
138
139 static struct ipt_target ipt_ecn_reg = {
140         .name           = "ECN",
141         .target         = target,
142         .targetsize     = sizeof(struct ipt_ECN_info),
143         .table          = "mangle",
144         .checkentry     = checkentry,
145         .me             = THIS_MODULE,
146 };
147
148 static int __init ipt_ecn_init(void)
149 {
150         return ipt_register_target(&ipt_ecn_reg);
151 }
152
153 static void __exit ipt_ecn_fini(void)
154 {
155         ipt_unregister_target(&ipt_ecn_reg);
156 }
157
158 module_init(ipt_ecn_init);
159 module_exit(ipt_ecn_fini);