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 *skb, const struct ipt_ECN_info *einfo)
31 struct iphdr *iph = ip_hdr(skb);
33 if ((iph->tos & IPT_ECN_IP_MASK) != (einfo->ip_ect & IPT_ECN_IP_MASK)) {
35 if (!skb_make_writable(skb, sizeof(struct iphdr)))
39 iph->tos &= ~IPT_ECN_IP_MASK;
40 iph->tos |= (einfo->ip_ect & IPT_ECN_IP_MASK);
41 csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));
46 /* Return false if there was an error. */
48 set_ect_tcp(struct sk_buff *skb, const struct ipt_ECN_info *einfo)
50 struct tcphdr _tcph, *tcph;
53 /* Not enought header? */
54 tcph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
58 if ((!(einfo->operation & IPT_ECN_OP_SET_ECE) ||
59 tcph->ece == einfo->proto.tcp.ece) &&
60 (!(einfo->operation & IPT_ECN_OP_SET_CWR) ||
61 tcph->cwr == einfo->proto.tcp.cwr))
64 if (!skb_make_writable(skb, ip_hdrlen(skb) + sizeof(*tcph)))
66 tcph = (void *)ip_hdr(skb) + ip_hdrlen(skb);
68 oldval = ((__be16 *)tcph)[6];
69 if (einfo->operation & IPT_ECN_OP_SET_ECE)
70 tcph->ece = einfo->proto.tcp.ece;
71 if (einfo->operation & IPT_ECN_OP_SET_CWR)
72 tcph->cwr = einfo->proto.tcp.cwr;
74 inet_proto_csum_replace2(&tcph->check, skb,
75 oldval, ((__be16 *)tcph)[6], 0);
80 target(struct sk_buff *skb,
81 const struct net_device *in,
82 const struct net_device *out,
84 const struct xt_target *target,
87 const struct ipt_ECN_info *einfo = targinfo;
89 if (einfo->operation & IPT_ECN_OP_SET_IP)
90 if (!set_ect_ip(skb, einfo))
93 if (einfo->operation & (IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR)
94 && ip_hdr(skb)->protocol == IPPROTO_TCP)
95 if (!set_ect_tcp(skb, einfo))
102 checkentry(const char *tablename,
104 const struct xt_target *target,
106 unsigned int hook_mask)
108 const struct ipt_ECN_info *einfo = (struct ipt_ECN_info *)targinfo;
109 const struct ipt_entry *e = e_void;
111 if (einfo->operation & IPT_ECN_OP_MASK) {
112 printk(KERN_WARNING "ECN: unsupported ECN operation %x\n",
116 if (einfo->ip_ect & ~IPT_ECN_IP_MASK) {
117 printk(KERN_WARNING "ECN: new ECT codepoint %x out of mask\n",
121 if ((einfo->operation & (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR))
122 && (e->ip.proto != IPPROTO_TCP || (e->ip.invflags & XT_INV_PROTO))) {
123 printk(KERN_WARNING "ECN: cannot use TCP operations on a "
130 static struct xt_target ipt_ecn_reg __read_mostly = {
134 .targetsize = sizeof(struct ipt_ECN_info),
136 .checkentry = checkentry,
140 static int __init ipt_ecn_init(void)
142 return xt_register_target(&ipt_ecn_reg);
145 static void __exit ipt_ecn_fini(void)
147 xt_unregister_target(&ipt_ecn_reg);
150 module_init(ipt_ecn_init);
151 module_exit(ipt_ecn_fini);