2005-04-17 00:20:36 +02:00
|
|
|
/* iptables module for the IPv4 and TCP ECN bits, Version 1.5
|
|
|
|
*
|
|
|
|
* (C) 2002 by Harald Welte <laforge@netfilter.org>
|
2007-02-09 15:24:47 +01:00
|
|
|
*
|
2005-04-17 00:20:36 +02:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-02-09 15:24:47 +01:00
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
2005-04-17 00:20:36 +02:00
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
2007-02-08 00:11:19 +01:00
|
|
|
#include <linux/in.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/ip.h>
|
2007-03-13 00:09:15 +01:00
|
|
|
#include <net/ip.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <linux/tcp.h>
|
|
|
|
#include <net/checksum.h>
|
|
|
|
|
2007-02-08 00:11:19 +01:00
|
|
|
#include <linux/netfilter/x_tables.h>
|
2005-04-17 00:20:36 +02: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 07:16:26 +02:00
|
|
|
* return false if there was an error. */
|
|
|
|
static inline bool
|
2007-10-15 09:53:15 +02:00
|
|
|
set_ect_ip(struct sk_buff *skb, const struct ipt_ECN_info *einfo)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2007-10-15 09:53:15 +02:00
|
|
|
struct iphdr *iph = ip_hdr(skb);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-08-22 09:33:09 +02:00
|
|
|
if ((iph->tos & IPT_ECN_IP_MASK) != (einfo->ip_ect & IPT_ECN_IP_MASK)) {
|
2006-11-15 06:43:23 +01:00
|
|
|
__u8 oldtos;
|
2007-10-15 09:53:15 +02:00
|
|
|
if (!skb_make_writable(skb, sizeof(struct iphdr)))
|
2007-07-08 07:16:26 +02:00
|
|
|
return false;
|
2007-10-15 09:53:15 +02:00
|
|
|
iph = ip_hdr(skb);
|
2006-08-22 09:33:09 +02:00
|
|
|
oldtos = iph->tos;
|
|
|
|
iph->tos &= ~IPT_ECN_IP_MASK;
|
|
|
|
iph->tos |= (einfo->ip_ect & IPT_ECN_IP_MASK);
|
2006-11-15 06:43:23 +01:00
|
|
|
nf_csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));
|
2007-02-09 15:24:47 +01:00
|
|
|
}
|
2007-07-08 07:16:26 +02:00
|
|
|
return true;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2007-07-08 07:16:26 +02:00
|
|
|
/* Return false if there was an error. */
|
|
|
|
static inline bool
|
2007-10-15 09:53:15 +02:00
|
|
|
set_ect_tcp(struct sk_buff *skb, const struct ipt_ECN_info *einfo)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct tcphdr _tcph, *tcph;
|
2006-09-28 23:22:24 +02:00
|
|
|
__be16 oldval;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* Not enought header? */
|
2007-10-15 09:53:15 +02:00
|
|
|
tcph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!tcph)
|
2007-07-08 07:16:26 +02:00
|
|
|
return false;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2005-08-21 02:38:40 +02:00
|
|
|
if ((!(einfo->operation & IPT_ECN_OP_SET_ECE) ||
|
|
|
|
tcph->ece == einfo->proto.tcp.ece) &&
|
2007-07-08 07:19:08 +02:00
|
|
|
(!(einfo->operation & IPT_ECN_OP_SET_CWR) ||
|
|
|
|
tcph->cwr == einfo->proto.tcp.cwr))
|
2007-07-08 07:16:26 +02:00
|
|
|
return true;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-10-15 09:53:15 +02:00
|
|
|
if (!skb_make_writable(skb, ip_hdrlen(skb) + sizeof(*tcph)))
|
2007-07-08 07:16:26 +02:00
|
|
|
return false;
|
2007-10-15 09:53:15 +02:00
|
|
|
tcph = (void *)ip_hdr(skb) + ip_hdrlen(skb);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2006-09-28 23:22:24 +02:00
|
|
|
oldval = ((__be16 *)tcph)[6];
|
2005-04-17 00:20:36 +02: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;
|
|
|
|
|
2007-10-15 09:53:15 +02:00
|
|
|
nf_proto_csum_replace2(&tcph->check, skb,
|
2006-11-15 06:43:23 +01:00
|
|
|
oldval, ((__be16 *)tcph)[6], 0);
|
2007-07-08 07:16:26 +02:00
|
|
|
return true;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
2007-10-15 09:53:15 +02:00
|
|
|
target(struct sk_buff *skb,
|
2005-04-17 00:20:36 +02:00
|
|
|
const struct net_device *in,
|
|
|
|
const struct net_device *out,
|
|
|
|
unsigned int hooknum,
|
2006-03-21 03:02:56 +01:00
|
|
|
const struct xt_target *target,
|
2006-08-22 09:35:47 +02:00
|
|
|
const void *targinfo)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
const struct ipt_ECN_info *einfo = targinfo;
|
|
|
|
|
|
|
|
if (einfo->operation & IPT_ECN_OP_SET_IP)
|
2007-10-15 09:53:15 +02:00
|
|
|
if (!set_ect_ip(skb, einfo))
|
2005-04-17 00:20:36 +02:00
|
|
|
return NF_DROP;
|
|
|
|
|
|
|
|
if (einfo->operation & (IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR)
|
2007-10-15 09:53:15 +02:00
|
|
|
&& ip_hdr(skb)->protocol == IPPROTO_TCP)
|
|
|
|
if (!set_ect_tcp(skb, einfo))
|
2005-04-17 00:20:36 +02:00
|
|
|
return NF_DROP;
|
|
|
|
|
2007-02-08 00:11:19 +01:00
|
|
|
return XT_CONTINUE;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2007-07-08 07:16:26 +02:00
|
|
|
static bool
|
2005-04-17 00:20:36 +02: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 22:30:04 +01:00
|
|
|
const void *e_void,
|
2006-03-21 03:02:56 +01:00
|
|
|
const struct xt_target *target,
|
2007-02-09 15:24:47 +01:00
|
|
|
void *targinfo,
|
|
|
|
unsigned int hook_mask)
|
2005-04-17 00:20:36 +02: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 22:30:04 +01:00
|
|
|
const struct ipt_entry *e = e_void;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (einfo->operation & IPT_ECN_OP_MASK) {
|
|
|
|
printk(KERN_WARNING "ECN: unsupported ECN operation %x\n",
|
|
|
|
einfo->operation);
|
2007-07-08 07:16:26 +02:00
|
|
|
return false;
|
2005-04-17 00:20:36 +02: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 07:16:26 +02:00
|
|
|
return false;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
if ((einfo->operation & (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR))
|
2007-02-08 00:11:19 +01:00
|
|
|
&& (e->ip.proto != IPPROTO_TCP || (e->ip.invflags & XT_INV_PROTO))) {
|
2005-04-17 00:20:36 +02:00
|
|
|
printk(KERN_WARNING "ECN: cannot use TCP operations on a "
|
|
|
|
"non-tcp rule\n");
|
2007-07-08 07:16:26 +02:00
|
|
|
return false;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2007-07-08 07:16:26 +02:00
|
|
|
return true;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2007-07-08 07:22:02 +02:00
|
|
|
static struct xt_target ipt_ecn_reg __read_mostly = {
|
2005-04-17 00:20:36 +02:00
|
|
|
.name = "ECN",
|
2007-02-08 00:11:19 +01:00
|
|
|
.family = AF_INET,
|
2005-04-17 00:20:36 +02:00
|
|
|
.target = target,
|
2006-03-21 03:01:14 +01:00
|
|
|
.targetsize = sizeof(struct ipt_ECN_info),
|
|
|
|
.table = "mangle",
|
2005-04-17 00:20:36 +02:00
|
|
|
.checkentry = checkentry,
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2006-03-29 02:37:06 +02:00
|
|
|
static int __init ipt_ecn_init(void)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2007-02-08 00:11:19 +01:00
|
|
|
return xt_register_target(&ipt_ecn_reg);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2006-03-29 02:37:06 +02:00
|
|
|
static void __exit ipt_ecn_fini(void)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2007-02-08 00:11:19 +01:00
|
|
|
xt_unregister_target(&ipt_ecn_reg);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2006-03-29 02:37:06 +02:00
|
|
|
module_init(ipt_ecn_init);
|
|
|
|
module_exit(ipt_ecn_fini);
|