vserver 1.9.3
[linux-2.6.git] / 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_ip_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                 (*pskb)->nfcache |= NFC_ALTERED;
47         } 
48         return 1;
49 }
50
51 /* Return 0 if there was an error. */
52 static inline int
53 set_ect_tcp(struct sk_buff **pskb, const struct ipt_ECN_info *einfo, int inward)
54 {
55         struct tcphdr _tcph, *th;
56         u_int16_t diffs[2];
57
58         /* Not enought header? */
59         th = skb_header_pointer(*pskb, (*pskb)->nh.iph->ihl*4,
60                                 sizeof(_tcph), &_tcph);
61         if (th == NULL)
62                 return 0;
63
64         diffs[0] = ((u_int16_t *)th)[6];
65         if (einfo->operation & IPT_ECN_OP_SET_ECE)
66                 th->ece = einfo->proto.tcp.ece;
67
68         if (einfo->operation & IPT_ECN_OP_SET_CWR)
69                 th->cwr = einfo->proto.tcp.cwr;
70         diffs[1] = ((u_int16_t *)&th)[6];
71
72         /* Only mangle if it's changed. */
73         if (diffs[0] != diffs[1]) {
74                 diffs[0] = diffs[0] ^ 0xFFFF;
75                 if (!skb_ip_make_writable(pskb,
76                                           (*pskb)->nh.iph->ihl*4+sizeof(_tcph)))
77                         return 0;
78
79                 if (th != &_tcph)
80                         memcpy(&_tcph, th, sizeof(_tcph));
81
82                 if ((*pskb)->ip_summed != CHECKSUM_HW)
83                         _tcph.check = csum_fold(csum_partial((char *)diffs,
84                                                              sizeof(diffs),
85                                                              _tcph.check^0xFFFF));
86                 memcpy((*pskb)->data + (*pskb)->nh.iph->ihl*4,
87                        &_tcph, sizeof(_tcph));
88                 if ((*pskb)->ip_summed == CHECKSUM_HW)
89                         if (skb_checksum_help(pskb, inward))
90                                 return 0;
91                 (*pskb)->nfcache |= NFC_ALTERED;
92         }
93         return 1;
94 }
95
96 static unsigned int
97 target(struct sk_buff **pskb,
98        const struct net_device *in,
99        const struct net_device *out,
100        unsigned int hooknum,
101        const void *targinfo,
102        void *userinfo)
103 {
104         const struct ipt_ECN_info *einfo = targinfo;
105
106         if (einfo->operation & IPT_ECN_OP_SET_IP)
107                 if (!set_ect_ip(pskb, einfo))
108                         return NF_DROP;
109
110         if (einfo->operation & (IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR)
111             && (*pskb)->nh.iph->protocol == IPPROTO_TCP)
112                 if (!set_ect_tcp(pskb, einfo, (out == NULL)))
113                         return NF_DROP;
114
115         return IPT_CONTINUE;
116 }
117
118 static int
119 checkentry(const char *tablename,
120            const struct ipt_entry *e,
121            void *targinfo,
122            unsigned int targinfosize,
123            unsigned int hook_mask)
124 {
125         const struct ipt_ECN_info *einfo = (struct ipt_ECN_info *)targinfo;
126
127         if (targinfosize != IPT_ALIGN(sizeof(struct ipt_ECN_info))) {
128                 printk(KERN_WARNING "ECN: targinfosize %u != %Zu\n",
129                        targinfosize,
130                        IPT_ALIGN(sizeof(struct ipt_ECN_info)));
131                 return 0;
132         }
133
134         if (strcmp(tablename, "mangle") != 0) {
135                 printk(KERN_WARNING "ECN: can only be called from \"mangle\" table, not \"%s\"\n", tablename);
136                 return 0;
137         }
138
139         if (einfo->operation & IPT_ECN_OP_MASK) {
140                 printk(KERN_WARNING "ECN: unsupported ECN operation %x\n",
141                         einfo->operation);
142                 return 0;
143         }
144         if (einfo->ip_ect & ~IPT_ECN_IP_MASK) {
145                 printk(KERN_WARNING "ECN: new ECT codepoint %x out of mask\n",
146                         einfo->ip_ect);
147                 return 0;
148         }
149
150         if ((einfo->operation & (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR))
151             && e->ip.proto != IPPROTO_TCP) {
152                 printk(KERN_WARNING "ECN: cannot use TCP operations on a "
153                        "non-tcp rule\n");
154                 return 0;
155         }
156
157         return 1;
158 }
159
160 static struct ipt_target ipt_ecn_reg = {
161         .name           = "ECN",
162         .target         = target,
163         .checkentry     = checkentry,
164         .me             = THIS_MODULE,
165 };
166
167 static int __init init(void)
168 {
169         return ipt_register_target(&ipt_ecn_reg);
170 }
171
172 static void __exit fini(void)
173 {
174         ipt_unregister_target(&ipt_ecn_reg);
175 }
176
177 module_init(init);
178 module_exit(fini);