1 /* iptables module for the IPv4 and TCP ECN bits, Version 1.5
3 * (C) 2002 by Harald Welte <laforge@netfilter.org>
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.
9 * ipt_ECN.c,v 1.5 2002/08/18 19:36:51 laforge Exp
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
16 #include <linux/tcp.h>
17 #include <net/checksum.h>
19 #include <linux/netfilter/x_tables.h>
20 #include <linux/netfilter_ipv4/ip_tables.h>
21 #include <linux/netfilter_ipv4/ipt_ECN.h>
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
25 MODULE_DESCRIPTION("iptables ECN modification module");
27 /* set ECT codepoint from IP header.
28 * return 0 if there was an error. */
30 set_ect_ip(struct sk_buff
**pskb
, const struct ipt_ECN_info
*einfo
)
32 struct iphdr
*iph
= (*pskb
)->nh
.iph
;
34 if ((iph
->tos
& IPT_ECN_IP_MASK
) != (einfo
->ip_ect
& IPT_ECN_IP_MASK
)) {
36 if (!skb_make_writable(pskb
, sizeof(struct iphdr
)))
38 iph
= (*pskb
)->nh
.iph
;
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
));
47 /* Return 0 if there was an error. */
49 set_ect_tcp(struct sk_buff
**pskb
, const struct ipt_ECN_info
*einfo
)
51 struct tcphdr _tcph
, *tcph
;
54 /* Not enought header? */
55 tcph
= skb_header_pointer(*pskb
, (*pskb
)->nh
.iph
->ihl
*4,
56 sizeof(_tcph
), &_tcph
);
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
)))
66 if (!skb_make_writable(pskb
, (*pskb
)->nh
.iph
->ihl
*4+sizeof(*tcph
)))
68 tcph
= (void *)(*pskb
)->nh
.iph
+ (*pskb
)->nh
.iph
->ihl
*4;
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
;
76 nf_proto_csum_replace2(&tcph
->check
, *pskb
,
77 oldval
, ((__be16
*)tcph
)[6], 0);
82 target(struct sk_buff
**pskb
,
83 const struct net_device
*in
,
84 const struct net_device
*out
,
86 const struct xt_target
*target
,
89 const struct ipt_ECN_info
*einfo
= targinfo
;
91 if (einfo
->operation
& IPT_ECN_OP_SET_IP
)
92 if (!set_ect_ip(pskb
, einfo
))
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
))
104 checkentry(const char *tablename
,
106 const struct xt_target
*target
,
108 unsigned int hook_mask
)
110 const struct ipt_ECN_info
*einfo
= (struct ipt_ECN_info
*)targinfo
;
111 const struct ipt_entry
*e
= e_void
;
113 if (einfo
->operation
& IPT_ECN_OP_MASK
) {
114 printk(KERN_WARNING
"ECN: unsupported ECN operation %x\n",
118 if (einfo
->ip_ect
& ~IPT_ECN_IP_MASK
) {
119 printk(KERN_WARNING
"ECN: new ECT codepoint %x out of mask\n",
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 "
132 static struct xt_target ipt_ecn_reg
= {
136 .targetsize
= sizeof(struct ipt_ECN_info
),
138 .checkentry
= checkentry
,
142 static int __init
ipt_ecn_init(void)
144 return xt_register_target(&ipt_ecn_reg
);
147 static void __exit
ipt_ecn_fini(void)
149 xt_unregister_target(&ipt_ecn_reg
);
152 module_init(ipt_ecn_init
);
153 module_exit(ipt_ecn_fini
);