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 if (((*pskb
)->nh
.iph
->tos
& IPT_ECN_IP_MASK
)
31 != (einfo
->ip_ect
& IPT_ECN_IP_MASK
)) {
34 if (!skb_ip_make_writable(pskb
, sizeof(struct iphdr
)))
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
,
44 (*pskb
)->nh
.iph
->check
46 (*pskb
)->nfcache
|= NFC_ALTERED
;
51 /* Return 0 if there was an error. */
53 set_ect_tcp(struct sk_buff
**pskb
, const struct ipt_ECN_info
*einfo
, int inward
)
55 struct tcphdr _tcph
, *tcph
;
58 /* Not enought header? */
59 tcph
= skb_header_pointer(*pskb
, (*pskb
)->nh
.iph
->ihl
*4,
60 sizeof(_tcph
), &_tcph
);
64 if (!(einfo
->operation
& IPT_ECN_OP_SET_ECE
65 || tcph
->ece
== einfo
->proto
.tcp
.ece
)
66 && (!(einfo
->operation
& IPT_ECN_OP_SET_CWR
67 || tcph
->cwr
== einfo
->proto
.tcp
.cwr
)))
70 if (!skb_ip_make_writable(pskb
, (*pskb
)->nh
.iph
->ihl
*4+sizeof(*tcph
)))
72 tcph
= (void *)(*pskb
)->nh
.iph
+ (*pskb
)->nh
.iph
->ihl
*4;
74 diffs
[0] = ((u_int16_t
*)tcph
)[6];
75 if (einfo
->operation
& IPT_ECN_OP_SET_ECE
)
76 tcph
->ece
= einfo
->proto
.tcp
.ece
;
77 if (einfo
->operation
& IPT_ECN_OP_SET_CWR
)
78 tcph
->cwr
= einfo
->proto
.tcp
.cwr
;
79 diffs
[1] = ((u_int16_t
*)tcph
)[6];
80 diffs
[0] = diffs
[0] ^ 0xFFFF;
82 if ((*pskb
)->ip_summed
!= CHECKSUM_HW
)
83 tcph
->check
= csum_fold(csum_partial((char *)diffs
,
87 if (skb_checksum_help(*pskb
, inward
))
89 (*pskb
)->nfcache
|= NFC_ALTERED
;
94 target(struct sk_buff
**pskb
,
95 const struct net_device
*in
,
96 const struct net_device
*out
,
101 const struct ipt_ECN_info
*einfo
= targinfo
;
103 if (einfo
->operation
& IPT_ECN_OP_SET_IP
)
104 if (!set_ect_ip(pskb
, einfo
))
107 if (einfo
->operation
& (IPT_ECN_OP_SET_ECE
| IPT_ECN_OP_SET_CWR
)
108 && (*pskb
)->nh
.iph
->protocol
== IPPROTO_TCP
)
109 if (!set_ect_tcp(pskb
, einfo
, (out
== NULL
)))
116 checkentry(const char *tablename
,
117 const struct ipt_entry
*e
,
119 unsigned int targinfosize
,
120 unsigned int hook_mask
)
122 const struct ipt_ECN_info
*einfo
= (struct ipt_ECN_info
*)targinfo
;
124 if (targinfosize
!= IPT_ALIGN(sizeof(struct ipt_ECN_info
))) {
125 printk(KERN_WARNING
"ECN: targinfosize %u != %Zu\n",
127 IPT_ALIGN(sizeof(struct ipt_ECN_info
)));
131 if (strcmp(tablename
, "mangle") != 0) {
132 printk(KERN_WARNING
"ECN: can only be called from \"mangle\" table, not \"%s\"\n", tablename
);
136 if (einfo
->operation
& IPT_ECN_OP_MASK
) {
137 printk(KERN_WARNING
"ECN: unsupported ECN operation %x\n",
141 if (einfo
->ip_ect
& ~IPT_ECN_IP_MASK
) {
142 printk(KERN_WARNING
"ECN: new ECT codepoint %x out of mask\n",
147 if ((einfo
->operation
& (IPT_ECN_OP_SET_ECE
|IPT_ECN_OP_SET_CWR
))
148 && (e
->ip
.proto
!= IPPROTO_TCP
|| (e
->ip
.invflags
& IPT_INV_PROTO
))) {
149 printk(KERN_WARNING
"ECN: cannot use TCP operations on a "
157 static struct ipt_target ipt_ecn_reg
= {
160 .checkentry
= checkentry
,
164 static int __init
init(void)
166 return ipt_register_target(&ipt_ecn_reg
);
169 static void __exit
fini(void)
171 ipt_unregister_target(&ipt_ecn_reg
);