2005-04-16 16:20:36 -06: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 16:11:19 -07:00
|
|
|
#include <linux/in.h>
|
|
|
|
#include <linux/ip.h>
|
2007-03-12 17:09:15 -06:00
|
|
|
#include <net/ip.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/tcp.h>
|
|
|
|
|
2007-02-07 16:11:19 -07:00
|
|
|
#include <linux/netfilter/x_tables.h>
|
2005-04-16 16:20:36 -06: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 00:42:28 -07:00
|
|
|
MODULE_DESCRIPTION("Xtables: Explicit Congestion Notification (ECN) flag match for IPv4");
|
2005-04-16 16:20:36 -06:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
2007-07-07 23:15:35 -06:00
|
|
|
static inline bool match_ip(const struct sk_buff *skb,
|
|
|
|
const struct ipt_ecn_info *einfo)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2007-04-20 23:47:35 -06:00
|
|
|
return (ip_hdr(skb)->tos & IPT_ECN_IP_MASK) == einfo->ip_ect;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-07-07 23:15:35 -06:00
|
|
|
static inline bool match_tcp(const struct sk_buff *skb,
|
|
|
|
const struct ipt_ecn_info *einfo,
|
|
|
|
bool *hotdrop)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2007-07-07 23:16:55 -06:00
|
|
|
struct tcphdr _tcph;
|
|
|
|
const struct tcphdr *th;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* In practice, TCP match does this, so can't fail. But let's
|
|
|
|
* be good citizens.
|
|
|
|
*/
|
2007-03-12 17:09:15 -06:00
|
|
|
th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (th == NULL) {
|
2007-07-07 23:15:12 -06:00
|
|
|
*hotdrop = false;
|
2007-07-07 23:15:35 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (einfo->operation & IPT_ECN_OP_MATCH_ECE) {
|
|
|
|
if (einfo->invert & IPT_ECN_OP_MATCH_ECE) {
|
|
|
|
if (th->ece == 1)
|
2007-07-07 23:15:35 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
} else {
|
|
|
|
if (th->ece == 0)
|
2007-07-07 23:15:35 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (einfo->operation & IPT_ECN_OP_MATCH_CWR) {
|
|
|
|
if (einfo->invert & IPT_ECN_OP_MATCH_CWR) {
|
|
|
|
if (th->cwr == 1)
|
2007-07-07 23:15:35 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
} else {
|
|
|
|
if (th->cwr == 0)
|
2007-07-07 23:15:35 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-07 23:15:35 -06:00
|
|
|
return true;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2008-10-08 03:35:18 -06:00
|
|
|
static bool ecn_mt(const struct sk_buff *skb, const struct xt_match_param *par)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-08 03:35:18 -06:00
|
|
|
const struct ipt_ecn_info *info = par->matchinfo;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (info->operation & IPT_ECN_OP_MATCH_IP)
|
|
|
|
if (!match_ip(skb, info))
|
2007-07-07 23:15:35 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)) {
|
2007-04-20 23:47:35 -06:00
|
|
|
if (ip_hdr(skb)->protocol != IPPROTO_TCP)
|
2007-07-07 23:15:35 -06:00
|
|
|
return false;
|
2008-10-08 03:35:18 -06:00
|
|
|
if (!match_tcp(skb, info, par->hotdrop))
|
2007-07-07 23:15:35 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-07-07 23:15:35 -06:00
|
|
|
return true;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2008-10-08 03:35:18 -06:00
|
|
|
static bool ecn_mt_check(const struct xt_mtchk_param *par)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-08 03:35:18 -06:00
|
|
|
const struct ipt_ecn_info *info = par->matchinfo;
|
|
|
|
const struct ipt_ip *ip = par->entryinfo;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (info->operation & IPT_ECN_OP_MATCH_MASK)
|
2007-07-07 23:16:00 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (info->invert & IPT_ECN_OP_MATCH_MASK)
|
2007-07-07 23:16:00 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)
|
|
|
|
&& ip->proto != IPPROTO_TCP) {
|
|
|
|
printk(KERN_WARNING "ipt_ecn: can't match TCP bits in rule for"
|
|
|
|
" non-tcp packets\n");
|
2007-07-07 23:16:00 -06:00
|
|
|
return false;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-07-07 23:16:00 -06:00
|
|
|
return true;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
static struct xt_match ecn_mt_reg __read_mostly = {
|
2005-04-16 16:20:36 -06:00
|
|
|
.name = "ecn",
|
2008-10-08 03:35:01 -06:00
|
|
|
.family = NFPROTO_IPV4,
|
2007-12-05 00:24:03 -07:00
|
|
|
.match = ecn_mt,
|
2006-03-20 19:01:14 -07:00
|
|
|
.matchsize = sizeof(struct ipt_ecn_info),
|
2007-12-05 00:24:03 -07:00
|
|
|
.checkentry = ecn_mt_check,
|
2005-04-16 16:20:36 -06:00
|
|
|
.me = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
static int __init ecn_mt_init(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2007-12-05 00:24:03 -07:00
|
|
|
return xt_register_match(&ecn_mt_reg);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
static void __exit ecn_mt_exit(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2007-12-05 00:24:03 -07:00
|
|
|
xt_unregister_match(&ecn_mt_reg);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
module_init(ecn_mt_init);
|
|
|
|
module_exit(ecn_mt_exit);
|