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.
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
15 #include <linux/tcp.h>
16 #include <net/checksum.h>
18 #include <linux/netfilter/x_tables.h>
19 #include <linux/netfilter_ipv4/ip_tables.h>
20 #include <linux/netfilter_ipv4/ipt_ECN.h>
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
24 MODULE_DESCRIPTION("iptables ECN modification module");
26 /* set ECT codepoint from IP header.
27 * return false if there was an error. */
29 set_ect_ip(struct sk_buff **pskb, const struct ipt_ECN_info *einfo)
31 struct iphdr *iph = ip_hdr(*pskb);
33 if ((iph->tos & IPT_ECN_IP_MASK) != (einfo->ip_ect & IPT_ECN_IP_MASK)) {
35 if (!skb_make_writable(pskb, sizeof(struct iphdr)))
39 iph->tos &= ~IPT_ECN_IP_MASK;
40 iph->tos |= (einfo->ip_ect & IPT_ECN_IP_MASK);
41 nf_csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));
46 /* Return false 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, ip_hdrlen(*pskb),
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, ip_hdrlen(*pskb) + sizeof(*tcph)))
67 tcph = (void *)ip_hdr(*pskb) + ip_hdrlen(*pskb);
69 oldval = ((__be16 *)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 nf_proto_csum_replace2(&tcph->check, *pskb,
76 oldval, ((__be16 *)tcph)[6], 0);
81 target(struct sk_buff **pskb,
82 const struct net_device *in,
83 const struct net_device *out,
85 const struct xt_target *target,
88 const struct ipt_ECN_info *einfo = targinfo;
90 if (einfo->operation & IPT_ECN_OP_SET_IP)
91 if (!set_ect_ip(pskb, einfo))
94 if (einfo->operation & (IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR)
95 && ip_hdr(*pskb)->protocol == IPPROTO_TCP)
96 if (!set_ect_tcp(pskb, einfo))
103 checkentry(const char *tablename,
105 const struct xt_target *target,
107 unsigned int hook_mask)
109 const struct ipt_ECN_info *einfo = (struct ipt_ECN_info *)targinfo;
110 const struct ipt_entry *e = e_void;
112 if (einfo->operation & IPT_ECN_OP_MASK) {
113 printk(KERN_WARNING "ECN: unsupported ECN operation %x\n",
117 if (einfo->ip_ect & ~IPT_ECN_IP_MASK) {
118 printk(KERN_WARNING "ECN: new ECT codepoint %x out of mask\n",
122 if ((einfo->operation & (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR))
123 && (e->ip.proto != IPPROTO_TCP || (e->ip.invflags & XT_INV_PROTO))) {
124 printk(KERN_WARNING "ECN: cannot use TCP operations on a "
131 static struct xt_target ipt_ecn_reg __read_mostly = {
135 .targetsize = sizeof(struct ipt_ECN_info),
137 .checkentry = checkentry,
141 static int __init ipt_ecn_init(void)
143 return xt_register_target(&ipt_ecn_reg);
146 static void __exit ipt_ecn_fini(void)
148 xt_unregister_target(&ipt_ecn_reg);
151 module_init(ipt_ecn_init);
152 module_exit(ipt_ecn_fini);