2005-04-16 22:20:36 +00:00
|
|
|
/* iptables module for the IPv4 and TCP ECN bits, Version 1.5
|
|
|
|
*
|
|
|
|
* (C) 2002 by Harald Welte <laforge@netfilter.org>
|
2007-02-09 14:24:47 +00:00
|
|
|
*
|
2005-04-16 22:20:36 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-02-09 14:24:47 +00:00
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
2005-04-16 22:20:36 +00:00
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
2007-02-07 23:11:19 +00:00
|
|
|
#include <linux/in.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/ip.h>
|
2007-03-12 23:09:15 +00:00
|
|
|
#include <net/ip.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/tcp.h>
|
|
|
|
#include <net/checksum.h>
|
|
|
|
|
2007-02-07 23:11:19 +00:00
|
|
|
#include <linux/netfilter/x_tables.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/netfilter_ipv4/ip_tables.h>
|
|
|
|
#include <linux/netfilter_ipv4/ipt_ECN.h>
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
|
|
|
|
MODULE_DESCRIPTION("iptables ECN modification module");
|
|
|
|
|
|
|
|
/* set ECT codepoint from IP header.
|
2007-07-08 05:16:26 +00:00
|
|
|
* return false if there was an error. */
|
|
|
|
static inline bool
|
2005-04-16 22:20:36 +00:00
|
|
|
set_ect_ip(struct sk_buff **pskb, const struct ipt_ECN_info *einfo)
|
|
|
|
{
|
2007-04-21 05:47:35 +00:00
|
|
|
struct iphdr *iph = ip_hdr(*pskb);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-08-22 07:33:09 +00:00
|
|
|
if ((iph->tos & IPT_ECN_IP_MASK) != (einfo->ip_ect & IPT_ECN_IP_MASK)) {
|
2006-11-15 05:43:23 +00:00
|
|
|
__u8 oldtos;
|
2005-08-10 02:37:23 +00:00
|
|
|
if (!skb_make_writable(pskb, sizeof(struct iphdr)))
|
2007-07-08 05:16:26 +00:00
|
|
|
return false;
|
2007-04-21 05:47:35 +00:00
|
|
|
iph = ip_hdr(*pskb);
|
2006-08-22 07:33:09 +00:00
|
|
|
oldtos = iph->tos;
|
|
|
|
iph->tos &= ~IPT_ECN_IP_MASK;
|
|
|
|
iph->tos |= (einfo->ip_ect & IPT_ECN_IP_MASK);
|
2006-11-15 05:43:23 +00:00
|
|
|
nf_csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));
|
2007-02-09 14:24:47 +00:00
|
|
|
}
|
2007-07-08 05:16:26 +00:00
|
|
|
return true;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-07-08 05:16:26 +00:00
|
|
|
/* Return false if there was an error. */
|
|
|
|
static inline bool
|
2006-08-29 23:44:56 +00:00
|
|
|
set_ect_tcp(struct sk_buff **pskb, const struct ipt_ECN_info *einfo)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct tcphdr _tcph, *tcph;
|
2006-09-28 21:22:24 +00:00
|
|
|
__be16 oldval;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* Not enought header? */
|
2007-03-12 23:09:15 +00:00
|
|
|
tcph = skb_header_pointer(*pskb, ip_hdrlen(*pskb),
|
2005-04-16 22:20:36 +00:00
|
|
|
sizeof(_tcph), &_tcph);
|
|
|
|
if (!tcph)
|
2007-07-08 05:16:26 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-08-21 00:38:40 +00:00
|
|
|
if ((!(einfo->operation & IPT_ECN_OP_SET_ECE) ||
|
|
|
|
tcph->ece == einfo->proto.tcp.ece) &&
|
2007-07-08 05:19:08 +00:00
|
|
|
(!(einfo->operation & IPT_ECN_OP_SET_CWR) ||
|
|
|
|
tcph->cwr == einfo->proto.tcp.cwr))
|
2007-07-08 05:16:26 +00:00
|
|
|
return true;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-03-12 23:09:15 +00:00
|
|
|
if (!skb_make_writable(pskb, ip_hdrlen(*pskb) + sizeof(*tcph)))
|
2007-07-08 05:16:26 +00:00
|
|
|
return false;
|
2007-04-21 05:47:35 +00:00
|
|
|
tcph = (void *)ip_hdr(*pskb) + ip_hdrlen(*pskb);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-09-28 21:22:24 +00:00
|
|
|
oldval = ((__be16 *)tcph)[6];
|
2005-04-16 22:20:36 +00:00
|
|
|
if (einfo->operation & IPT_ECN_OP_SET_ECE)
|
|
|
|
tcph->ece = einfo->proto.tcp.ece;
|
|
|
|
if (einfo->operation & IPT_ECN_OP_SET_CWR)
|
|
|
|
tcph->cwr = einfo->proto.tcp.cwr;
|
|
|
|
|
2006-11-15 05:43:23 +00:00
|
|
|
nf_proto_csum_replace2(&tcph->check, *pskb,
|
|
|
|
oldval, ((__be16 *)tcph)[6], 0);
|
2007-07-08 05:16:26 +00:00
|
|
|
return true;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
target(struct sk_buff **pskb,
|
|
|
|
const struct net_device *in,
|
|
|
|
const struct net_device *out,
|
|
|
|
unsigned int hooknum,
|
2006-03-21 02:02:56 +00:00
|
|
|
const struct xt_target *target,
|
2006-08-22 07:35:47 +00:00
|
|
|
const void *targinfo)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
const struct ipt_ECN_info *einfo = targinfo;
|
|
|
|
|
|
|
|
if (einfo->operation & IPT_ECN_OP_SET_IP)
|
|
|
|
if (!set_ect_ip(pskb, einfo))
|
|
|
|
return NF_DROP;
|
|
|
|
|
|
|
|
if (einfo->operation & (IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR)
|
2007-04-21 05:47:35 +00:00
|
|
|
&& ip_hdr(*pskb)->protocol == IPPROTO_TCP)
|
2006-08-29 23:44:56 +00:00
|
|
|
if (!set_ect_tcp(pskb, einfo))
|
2005-04-16 22:20:36 +00:00
|
|
|
return NF_DROP;
|
|
|
|
|
2007-02-07 23:11:19 +00:00
|
|
|
return XT_CONTINUE;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-07-08 05:16:26 +00:00
|
|
|
static bool
|
2005-04-16 22:20:36 +00:00
|
|
|
checkentry(const char *tablename,
|
[NETFILTER] x_tables: Abstraction layer for {ip,ip6,arp}_tables
This monster-patch tries to do the best job for unifying the data
structures and backend interfaces for the three evil clones ip_tables,
ip6_tables and arp_tables. In an ideal world we would never have
allowed this kind of copy+paste programming... but well, our world
isn't (yet?) ideal.
o introduce a new x_tables module
o {ip,arp,ip6}_tables depend on this x_tables module
o registration functions for tables, matches and targets are only
wrappers around x_tables provided functions
o all matches/targets that are used from ip_tables and ip6_tables
are now implemented as xt_FOOBAR.c files and provide module aliases
to ipt_FOOBAR and ip6t_FOOBAR
o header files for xt_matches are in include/linux/netfilter/,
include/linux/netfilter_{ipv4,ipv6} contains compatibility wrappers
around the xt_FOOBAR.h headers
Based on this patchset we're going to further unify the code,
gradually getting rid of all the layer 3 specific assumptions.
Signed-off-by: Harald Welte <laforge@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-01-12 21:30:04 +00:00
|
|
|
const void *e_void,
|
2006-03-21 02:02:56 +00:00
|
|
|
const struct xt_target *target,
|
2007-02-09 14:24:47 +00:00
|
|
|
void *targinfo,
|
|
|
|
unsigned int hook_mask)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
const struct ipt_ECN_info *einfo = (struct ipt_ECN_info *)targinfo;
|
[NETFILTER] x_tables: Abstraction layer for {ip,ip6,arp}_tables
This monster-patch tries to do the best job for unifying the data
structures and backend interfaces for the three evil clones ip_tables,
ip6_tables and arp_tables. In an ideal world we would never have
allowed this kind of copy+paste programming... but well, our world
isn't (yet?) ideal.
o introduce a new x_tables module
o {ip,arp,ip6}_tables depend on this x_tables module
o registration functions for tables, matches and targets are only
wrappers around x_tables provided functions
o all matches/targets that are used from ip_tables and ip6_tables
are now implemented as xt_FOOBAR.c files and provide module aliases
to ipt_FOOBAR and ip6t_FOOBAR
o header files for xt_matches are in include/linux/netfilter/,
include/linux/netfilter_{ipv4,ipv6} contains compatibility wrappers
around the xt_FOOBAR.h headers
Based on this patchset we're going to further unify the code,
gradually getting rid of all the layer 3 specific assumptions.
Signed-off-by: Harald Welte <laforge@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-01-12 21:30:04 +00:00
|
|
|
const struct ipt_entry *e = e_void;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (einfo->operation & IPT_ECN_OP_MASK) {
|
|
|
|
printk(KERN_WARNING "ECN: unsupported ECN operation %x\n",
|
|
|
|
einfo->operation);
|
2007-07-08 05:16:26 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
if (einfo->ip_ect & ~IPT_ECN_IP_MASK) {
|
|
|
|
printk(KERN_WARNING "ECN: new ECT codepoint %x out of mask\n",
|
|
|
|
einfo->ip_ect);
|
2007-07-08 05:16:26 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
if ((einfo->operation & (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR))
|
2007-02-07 23:11:19 +00:00
|
|
|
&& (e->ip.proto != IPPROTO_TCP || (e->ip.invflags & XT_INV_PROTO))) {
|
2005-04-16 22:20:36 +00:00
|
|
|
printk(KERN_WARNING "ECN: cannot use TCP operations on a "
|
|
|
|
"non-tcp rule\n");
|
2007-07-08 05:16:26 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2007-07-08 05:16:26 +00:00
|
|
|
return true;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-02-07 23:11:19 +00:00
|
|
|
static struct xt_target ipt_ecn_reg = {
|
2005-04-16 22:20:36 +00:00
|
|
|
.name = "ECN",
|
2007-02-07 23:11:19 +00:00
|
|
|
.family = AF_INET,
|
2005-04-16 22:20:36 +00:00
|
|
|
.target = target,
|
2006-03-21 02:01:14 +00:00
|
|
|
.targetsize = sizeof(struct ipt_ECN_info),
|
|
|
|
.table = "mangle",
|
2005-04-16 22:20:36 +00:00
|
|
|
.checkentry = checkentry,
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2006-03-29 00:37:06 +00:00
|
|
|
static int __init ipt_ecn_init(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-02-07 23:11:19 +00:00
|
|
|
return xt_register_target(&ipt_ecn_reg);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-03-29 00:37:06 +00:00
|
|
|
static void __exit ipt_ecn_fini(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-02-07 23:11:19 +00:00
|
|
|
xt_unregister_target(&ipt_ecn_reg);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-03-29 00:37:06 +00:00
|
|
|
module_init(ipt_ecn_init);
|
|
|
|
module_exit(ipt_ecn_fini);
|