Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-drm-fsl-dcu.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/in.h>
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/ip.h>
16 #include <linux/tcp.h>
17 #include <net/checksum.h>
18
19 #include <linux/netfilter/x_tables.h>
20 #include <linux/netfilter_ipv4/ip_tables.h>
21 #include <linux/netfilter_ipv4/ipt_ECN.h>
22
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
25 MODULE_DESCRIPTION("iptables ECN modification module");
26
27 /* set ECT codepoint from IP header.
28  *      return 0 if there was an error. */
29 static inline int
30 set_ect_ip(struct sk_buff **pskb, const struct ipt_ECN_info *einfo)
31 {
32         struct iphdr *iph = (*pskb)->nh.iph;
33
34         if ((iph->tos & IPT_ECN_IP_MASK) != (einfo->ip_ect & IPT_ECN_IP_MASK)) {
35                 __u8 oldtos;
36                 if (!skb_make_writable(pskb, sizeof(struct iphdr)))
37                         return 0;
38                 iph = (*pskb)->nh.iph;
39                 oldtos = iph->tos;
40                 iph->tos &= ~IPT_ECN_IP_MASK;
41                 iph->tos |= (einfo->ip_ect & IPT_ECN_IP_MASK);
42                 nf_csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));
43         }
44         return 1;
45 }
46
47 /* Return 0 if there was an error. */
48 static inline int
49 set_ect_tcp(struct sk_buff **pskb, const struct ipt_ECN_info *einfo)
50 {
51         struct tcphdr _tcph, *tcph;
52         __be16 oldval;
53
54         /* Not enought header? */
55         tcph = skb_header_pointer(*pskb, (*pskb)->nh.iph->ihl*4,
56                                   sizeof(_tcph), &_tcph);
57         if (!tcph)
58                 return 0;
59
60         if ((!(einfo->operation & IPT_ECN_OP_SET_ECE) ||
61              tcph->ece == einfo->proto.tcp.ece) &&
62             ((!(einfo->operation & IPT_ECN_OP_SET_CWR) ||
63              tcph->cwr == einfo->proto.tcp.cwr)))
64                 return 1;
65
66         if (!skb_make_writable(pskb, (*pskb)->nh.iph->ihl*4+sizeof(*tcph)))
67                 return 0;
68         tcph = (void *)(*pskb)->nh.iph + (*pskb)->nh.iph->ihl*4;
69
70         oldval = ((__be16 *)tcph)[6];
71         if (einfo->operation & IPT_ECN_OP_SET_ECE)
72                 tcph->ece = einfo->proto.tcp.ece;
73         if (einfo->operation & IPT_ECN_OP_SET_CWR)
74                 tcph->cwr = einfo->proto.tcp.cwr;
75
76         nf_proto_csum_replace2(&tcph->check, *pskb,
77                                 oldval, ((__be16 *)tcph)[6], 0);
78         return 1;
79 }
80
81 static unsigned int
82 target(struct sk_buff **pskb,
83        const struct net_device *in,
84        const struct net_device *out,
85        unsigned int hooknum,
86        const struct xt_target *target,
87        const void *targinfo)
88 {
89         const struct ipt_ECN_info *einfo = targinfo;
90
91         if (einfo->operation & IPT_ECN_OP_SET_IP)
92                 if (!set_ect_ip(pskb, einfo))
93                         return NF_DROP;
94
95         if (einfo->operation & (IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR)
96             && (*pskb)->nh.iph->protocol == IPPROTO_TCP)
97                 if (!set_ect_tcp(pskb, einfo))
98                         return NF_DROP;
99
100         return XT_CONTINUE;
101 }
102
103 static int
104 checkentry(const char *tablename,
105            const void *e_void,
106            const struct xt_target *target,
107            void *targinfo,
108            unsigned int hook_mask)
109 {
110         const struct ipt_ECN_info *einfo = (struct ipt_ECN_info *)targinfo;
111         const struct ipt_entry *e = e_void;
112
113         if (einfo->operation & IPT_ECN_OP_MASK) {
114                 printk(KERN_WARNING "ECN: unsupported ECN operation %x\n",
115                         einfo->operation);
116                 return 0;
117         }
118         if (einfo->ip_ect & ~IPT_ECN_IP_MASK) {
119                 printk(KERN_WARNING "ECN: new ECT codepoint %x out of mask\n",
120                         einfo->ip_ect);
121                 return 0;
122         }
123         if ((einfo->operation & (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR))
124             && (e->ip.proto != IPPROTO_TCP || (e->ip.invflags & XT_INV_PROTO))) {
125                 printk(KERN_WARNING "ECN: cannot use TCP operations on a "
126                        "non-tcp rule\n");
127                 return 0;
128         }
129         return 1;
130 }
131
132 static struct xt_target ipt_ecn_reg = {
133         .name           = "ECN",
134         .family         = AF_INET,
135         .target         = target,
136         .targetsize     = sizeof(struct ipt_ECN_info),
137         .table          = "mangle",
138         .checkentry     = checkentry,
139         .me             = THIS_MODULE,
140 };
141
142 static int __init ipt_ecn_init(void)
143 {
144         return xt_register_target(&ipt_ecn_reg);
145 }
146
147 static void __exit ipt_ecn_fini(void)
148 {
149         xt_unregister_target(&ipt_ecn_reg);
150 }
151
152 module_init(ipt_ecn_init);
153 module_exit(ipt_ecn_fini);