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