2019-06-04 10:11:33 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2006-08-22 09:29:37 +02:00
|
|
|
/* IP tables module for matching the value of the IPv4/IPv6 DSCP field
|
|
|
|
*
|
|
|
|
* (C) 2002 by Harald Welte <laforge@netfilter.org>
|
|
|
|
*/
|
2010-03-17 16:04:40 +01:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
2006-08-22 09:29:37 +02:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/ip.h>
|
|
|
|
#include <linux/ipv6.h>
|
|
|
|
#include <net/dsfield.h>
|
|
|
|
|
|
|
|
#include <linux/netfilter/x_tables.h>
|
2007-12-05 08:37:54 +01:00
|
|
|
#include <linux/netfilter/xt_dscp.h>
|
2006-08-22 09:29:37 +02:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
|
2008-01-15 08:42:28 +01:00
|
|
|
MODULE_DESCRIPTION("Xtables: DSCP/TOS field match");
|
2006-08-22 09:29:37 +02:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS("ipt_dscp");
|
|
|
|
MODULE_ALIAS("ip6t_dscp");
|
2007-12-05 08:37:54 +01:00
|
|
|
MODULE_ALIAS("ipt_tos");
|
2007-12-05 08:38:30 +01:00
|
|
|
MODULE_ALIAS("ip6t_tos");
|
2006-08-22 09:29:37 +02:00
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static bool
|
2009-07-07 20:42:08 +02:00
|
|
|
dscp_mt(const struct sk_buff *skb, struct xt_action_param *par)
|
2007-07-08 07:15:35 +02:00
|
|
|
{
|
2008-10-08 11:35:18 +02:00
|
|
|
const struct xt_dscp_info *info = par->matchinfo;
|
2007-07-08 07:15:35 +02:00
|
|
|
u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
|
|
|
|
|
|
|
|
return (dscp == info->dscp) ^ !!info->invert;
|
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static bool
|
2009-07-07 20:42:08 +02:00
|
|
|
dscp_mt6(const struct sk_buff *skb, struct xt_action_param *par)
|
2006-08-22 09:29:37 +02:00
|
|
|
{
|
2008-10-08 11:35:18 +02:00
|
|
|
const struct xt_dscp_info *info = par->matchinfo;
|
2007-04-26 02:54:47 +02:00
|
|
|
u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
|
2006-08-22 09:29:37 +02:00
|
|
|
|
|
|
|
return (dscp == info->dscp) ^ !!info->invert;
|
|
|
|
}
|
|
|
|
|
2010-03-19 17:16:42 +01:00
|
|
|
static int dscp_mt_check(const struct xt_mtchk_param *par)
|
2006-08-22 09:29:37 +02:00
|
|
|
{
|
2008-10-08 11:35:18 +02:00
|
|
|
const struct xt_dscp_info *info = par->matchinfo;
|
2006-08-22 09:29:37 +02:00
|
|
|
|
2018-02-09 15:51:59 +01:00
|
|
|
if (info->dscp > XT_DSCP_MAX)
|
2010-03-19 17:32:59 +01:00
|
|
|
return -EDOM;
|
2006-08-22 09:29:37 +02:00
|
|
|
|
2010-03-23 16:35:56 +01:00
|
|
|
return 0;
|
2006-08-22 09:29:37 +02:00
|
|
|
}
|
|
|
|
|
2009-07-07 20:42:08 +02:00
|
|
|
static bool tos_mt(const struct sk_buff *skb, struct xt_action_param *par)
|
2007-12-05 08:38:30 +01:00
|
|
|
{
|
2008-10-08 11:35:18 +02:00
|
|
|
const struct xt_tos_match_info *info = par->matchinfo;
|
2007-12-05 08:38:30 +01:00
|
|
|
|
2016-11-03 10:56:21 +01:00
|
|
|
if (xt_family(par) == NFPROTO_IPV4)
|
2007-12-05 08:38:30 +01:00
|
|
|
return ((ip_hdr(skb)->tos & info->tos_mask) ==
|
|
|
|
info->tos_value) ^ !!info->invert;
|
|
|
|
else
|
|
|
|
return ((ipv6_get_dsfield(ipv6_hdr(skb)) & info->tos_mask) ==
|
|
|
|
info->tos_value) ^ !!info->invert;
|
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static struct xt_match dscp_mt_reg[] __read_mostly = {
|
2006-08-22 09:34:04 +02:00
|
|
|
{
|
|
|
|
.name = "dscp",
|
2008-10-08 11:35:01 +02:00
|
|
|
.family = NFPROTO_IPV4,
|
2007-12-05 08:24:03 +01:00
|
|
|
.checkentry = dscp_mt_check,
|
|
|
|
.match = dscp_mt,
|
2006-08-22 09:34:04 +02:00
|
|
|
.matchsize = sizeof(struct xt_dscp_info),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "dscp",
|
2008-10-08 11:35:01 +02:00
|
|
|
.family = NFPROTO_IPV6,
|
2007-12-05 08:24:03 +01:00
|
|
|
.checkentry = dscp_mt_check,
|
|
|
|
.match = dscp_mt6,
|
2006-08-22 09:34:04 +02:00
|
|
|
.matchsize = sizeof(struct xt_dscp_info),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
2007-12-05 08:38:30 +01:00
|
|
|
{
|
|
|
|
.name = "tos",
|
|
|
|
.revision = 1,
|
2008-10-08 11:35:01 +02:00
|
|
|
.family = NFPROTO_IPV4,
|
2007-12-05 08:38:30 +01:00
|
|
|
.match = tos_mt,
|
|
|
|
.matchsize = sizeof(struct xt_tos_match_info),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "tos",
|
|
|
|
.revision = 1,
|
2008-10-08 11:35:01 +02:00
|
|
|
.family = NFPROTO_IPV6,
|
2007-12-05 08:38:30 +01:00
|
|
|
.match = tos_mt,
|
|
|
|
.matchsize = sizeof(struct xt_tos_match_info),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
2006-08-22 09:29:37 +02:00
|
|
|
};
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static int __init dscp_mt_init(void)
|
2006-08-22 09:29:37 +02:00
|
|
|
{
|
2007-12-05 08:24:03 +01:00
|
|
|
return xt_register_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
|
2006-08-22 09:29:37 +02:00
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static void __exit dscp_mt_exit(void)
|
2006-08-22 09:29:37 +02:00
|
|
|
{
|
2007-12-05 08:24:03 +01:00
|
|
|
xt_unregister_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
|
2006-08-22 09:29:37 +02:00
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
module_init(dscp_mt_init);
|
|
|
|
module_exit(dscp_mt_exit);
|