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
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
15 #include <linux/tcp.h>
16 #include <net/checksum.h>
18 #include <linux/netfilter_ipv4/ip_tables.h>
19 #include <linux/netfilter_ipv4/ipt_ECN.h>
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
23 MODULE_DESCRIPTION("iptables ECN modification module");
25 /* set ECT codepoint from IP header.
26 * return 0 if there was an error. */
28 set_ect_ip(struct sk_buff
**pskb
, const struct ipt_ECN_info
*einfo
)
30 struct iphdr
*iph
= (*pskb
)->nh
.iph
;
33 if ((iph
->tos
& IPT_ECN_IP_MASK
) != (einfo
->ip_ect
& IPT_ECN_IP_MASK
)) {
34 if (!skb_make_writable(pskb
, sizeof(struct iphdr
)))
36 iph
= (*pskb
)->nh
.iph
;
38 iph
->tos
&= ~IPT_ECN_IP_MASK
;
39 iph
->tos
|= (einfo
->ip_ect
& IPT_ECN_IP_MASK
);
40 iph
->check
= nf_csum_update(oldtos
^ 0xFFFF, iph
->tos
,
46 /* Return 0 if there was an error. */
48 set_ect_tcp(struct sk_buff
**pskb
, const struct ipt_ECN_info
*einfo
)
50 struct tcphdr _tcph
, *tcph
;
53 /* Not enought header? */
54 tcph
= skb_header_pointer(*pskb
, (*pskb
)->nh
.iph
->ihl
*4,
55 sizeof(_tcph
), &_tcph
);
59 if ((!(einfo
->operation
& IPT_ECN_OP_SET_ECE
) ||
60 tcph
->ece
== einfo
->proto
.tcp
.ece
) &&
61 ((!(einfo
->operation
& IPT_ECN_OP_SET_CWR
) ||
62 tcph
->cwr
== einfo
->proto
.tcp
.cwr
)))
65 if (!skb_make_writable(pskb
, (*pskb
)->nh
.iph
->ihl
*4+sizeof(*tcph
)))
67 tcph
= (void *)(*pskb
)->nh
.iph
+ (*pskb
)->nh
.iph
->ihl
*4;
69 oldval
= ((u_int16_t
*)tcph
)[6];
70 if (einfo
->operation
& IPT_ECN_OP_SET_ECE
)
71 tcph
->ece
= einfo
->proto
.tcp
.ece
;
72 if (einfo
->operation
& IPT_ECN_OP_SET_CWR
)
73 tcph
->cwr
= einfo
->proto
.tcp
.cwr
;
75 tcph
->check
= nf_proto_csum_update((*pskb
),
77 ((u_int16_t
*)tcph
)[6],
83 target(struct sk_buff
**pskb
,
84 const struct net_device
*in
,
85 const struct net_device
*out
,
87 const struct xt_target
*target
,
90 const struct ipt_ECN_info
*einfo
= targinfo
;
92 if (einfo
->operation
& IPT_ECN_OP_SET_IP
)
93 if (!set_ect_ip(pskb
, einfo
))
96 if (einfo
->operation
& (IPT_ECN_OP_SET_ECE
| IPT_ECN_OP_SET_CWR
)
97 && (*pskb
)->nh
.iph
->protocol
== IPPROTO_TCP
)
98 if (!set_ect_tcp(pskb
, einfo
))
105 checkentry(const char *tablename
,
107 const struct xt_target
*target
,
109 unsigned int targinfosize
,
110 unsigned int hook_mask
)
112 const struct ipt_ECN_info
*einfo
= (struct ipt_ECN_info
*)targinfo
;
113 const struct ipt_entry
*e
= e_void
;
115 if (einfo
->operation
& IPT_ECN_OP_MASK
) {
116 printk(KERN_WARNING
"ECN: unsupported ECN operation %x\n",
120 if (einfo
->ip_ect
& ~IPT_ECN_IP_MASK
) {
121 printk(KERN_WARNING
"ECN: new ECT codepoint %x out of mask\n",
125 if ((einfo
->operation
& (IPT_ECN_OP_SET_ECE
|IPT_ECN_OP_SET_CWR
))
126 && (e
->ip
.proto
!= IPPROTO_TCP
|| (e
->ip
.invflags
& IPT_INV_PROTO
))) {
127 printk(KERN_WARNING
"ECN: cannot use TCP operations on a "
134 static struct ipt_target ipt_ecn_reg
= {
137 .targetsize
= sizeof(struct ipt_ECN_info
),
139 .checkentry
= checkentry
,
143 static int __init
ipt_ecn_init(void)
145 return ipt_register_target(&ipt_ecn_reg
);
148 static void __exit
ipt_ecn_fini(void)
150 ipt_unregister_target(&ipt_ecn_reg
);
153 module_init(ipt_ecn_init
);
154 module_exit(ipt_ecn_fini
);