2005-04-16 22:20:36 +00:00
|
|
|
/* IP tables module for matching the value of the IPv4 and TCP ECN bits
|
|
|
|
*
|
|
|
|
* (C) 2002 by Harald Welte <laforge@gnumonks.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
2007-02-07 23:11:19 +00:00
|
|
|
#include <linux/in.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/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/tcp.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_AUTHOR("Harald Welte <laforge@netfilter.org>");
|
2008-01-15 07:42:28 +00:00
|
|
|
MODULE_DESCRIPTION("Xtables: Explicit Congestion Notification (ECN) flag match for IPv4");
|
2005-04-16 22:20:36 +00:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
2007-07-08 05:15:35 +00:00
|
|
|
static inline bool match_ip(const struct sk_buff *skb,
|
|
|
|
const struct ipt_ecn_info *einfo)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-04-21 05:47:35 +00:00
|
|
|
return (ip_hdr(skb)->tos & IPT_ECN_IP_MASK) == einfo->ip_ect;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-07-08 05:15:35 +00:00
|
|
|
static inline bool match_tcp(const struct sk_buff *skb,
|
|
|
|
const struct ipt_ecn_info *einfo,
|
|
|
|
bool *hotdrop)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-07-08 05:16:55 +00:00
|
|
|
struct tcphdr _tcph;
|
|
|
|
const struct tcphdr *th;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* In practice, TCP match does this, so can't fail. But let's
|
|
|
|
* be good citizens.
|
|
|
|
*/
|
2007-03-12 23:09:15 +00:00
|
|
|
th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (th == NULL) {
|
2007-07-08 05:15:12 +00:00
|
|
|
*hotdrop = false;
|
2007-07-08 05:15:35 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (einfo->operation & IPT_ECN_OP_MATCH_ECE) {
|
|
|
|
if (einfo->invert & IPT_ECN_OP_MATCH_ECE) {
|
|
|
|
if (th->ece == 1)
|
2007-07-08 05:15:35 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
|
|
|
if (th->ece == 0)
|
2007-07-08 05:15:35 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (einfo->operation & IPT_ECN_OP_MATCH_CWR) {
|
|
|
|
if (einfo->invert & IPT_ECN_OP_MATCH_CWR) {
|
|
|
|
if (th->cwr == 1)
|
2007-07-08 05:15:35 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
|
|
|
if (th->cwr == 0)
|
2007-07-08 05:15:35 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-08 05:15:35 +00:00
|
|
|
return true;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-10-08 09:35:18 +00:00
|
|
|
static bool ecn_mt(const struct sk_buff *skb, const struct xt_match_param *par)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-10-08 09:35:18 +00:00
|
|
|
const struct ipt_ecn_info *info = par->matchinfo;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (info->operation & IPT_ECN_OP_MATCH_IP)
|
|
|
|
if (!match_ip(skb, info))
|
2007-07-08 05:15:35 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)) {
|
2007-04-21 05:47:35 +00:00
|
|
|
if (ip_hdr(skb)->protocol != IPPROTO_TCP)
|
2007-07-08 05:15:35 +00:00
|
|
|
return false;
|
2008-10-08 09:35:18 +00:00
|
|
|
if (!match_tcp(skb, info, par->hotdrop))
|
2007-07-08 05:15:35 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-07-08 05:15:35 +00:00
|
|
|
return true;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-10-08 09:35:18 +00:00
|
|
|
static bool ecn_mt_check(const struct xt_mtchk_param *par)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-10-08 09:35:18 +00:00
|
|
|
const struct ipt_ecn_info *info = par->matchinfo;
|
|
|
|
const struct ipt_ip *ip = par->entryinfo;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (info->operation & IPT_ECN_OP_MATCH_MASK)
|
2007-07-08 05:16:00 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (info->invert & IPT_ECN_OP_MATCH_MASK)
|
2007-07-08 05:16:00 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2009-11-23 22:17:06 +00:00
|
|
|
if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR) &&
|
|
|
|
ip->proto != IPPROTO_TCP) {
|
2005-04-16 22:20:36 +00:00
|
|
|
printk(KERN_WARNING "ipt_ecn: can't match TCP bits in rule for"
|
|
|
|
" non-tcp packets\n");
|
2007-07-08 05:16:00 +00:00
|
|
|
return false;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-07-08 05:16:00 +00:00
|
|
|
return true;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static struct xt_match ecn_mt_reg __read_mostly = {
|
2005-04-16 22:20:36 +00:00
|
|
|
.name = "ecn",
|
2008-10-08 09:35:01 +00:00
|
|
|
.family = NFPROTO_IPV4,
|
2007-12-05 07:24:03 +00:00
|
|
|
.match = ecn_mt,
|
2006-03-21 02:01:14 +00:00
|
|
|
.matchsize = sizeof(struct ipt_ecn_info),
|
2007-12-05 07:24:03 +00:00
|
|
|
.checkentry = ecn_mt_check,
|
2005-04-16 22:20:36 +00:00
|
|
|
.me = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static int __init ecn_mt_init(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-12-05 07:24:03 +00:00
|
|
|
return xt_register_match(&ecn_mt_reg);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static void __exit ecn_mt_exit(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-12-05 07:24:03 +00:00
|
|
|
xt_unregister_match(&ecn_mt_reg);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
module_init(ecn_mt_init);
|
|
|
|
module_exit(ecn_mt_exit);
|