1 // SPDX-License-Identifier: GPL-2.0-only
2 /* IP tables module for matching the value of the IPv4/IPv6 DSCP field
4 * (C) 2002 by Harald Welte <laforge@netfilter.org>
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/module.h>
8 #include <linux/skbuff.h>
10 #include <linux/ipv6.h>
11 #include <net/dsfield.h>
13 #include <linux/netfilter/x_tables.h>
14 #include <linux/netfilter/xt_dscp.h>
16 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
17 MODULE_DESCRIPTION("Xtables: DSCP/TOS field match");
18 MODULE_LICENSE("GPL");
19 MODULE_ALIAS("ipt_dscp");
20 MODULE_ALIAS("ip6t_dscp");
21 MODULE_ALIAS("ipt_tos");
22 MODULE_ALIAS("ip6t_tos");
25 dscp_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
27 const struct xt_dscp_info
*info
= par
->matchinfo
;
28 u_int8_t dscp
= ipv4_get_dsfield(ip_hdr(skb
)) >> XT_DSCP_SHIFT
;
30 return (dscp
== info
->dscp
) ^ !!info
->invert
;
34 dscp_mt6(const struct sk_buff
*skb
, struct xt_action_param
*par
)
36 const struct xt_dscp_info
*info
= par
->matchinfo
;
37 u_int8_t dscp
= ipv6_get_dsfield(ipv6_hdr(skb
)) >> XT_DSCP_SHIFT
;
39 return (dscp
== info
->dscp
) ^ !!info
->invert
;
42 static int dscp_mt_check(const struct xt_mtchk_param
*par
)
44 const struct xt_dscp_info
*info
= par
->matchinfo
;
46 if (info
->dscp
> XT_DSCP_MAX
)
52 static bool tos_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
54 const struct xt_tos_match_info
*info
= par
->matchinfo
;
56 if (xt_family(par
) == NFPROTO_IPV4
)
57 return ((ip_hdr(skb
)->tos
& info
->tos_mask
) ==
58 info
->tos_value
) ^ !!info
->invert
;
60 return ((ipv6_get_dsfield(ipv6_hdr(skb
)) & info
->tos_mask
) ==
61 info
->tos_value
) ^ !!info
->invert
;
64 static struct xt_match dscp_mt_reg
[] __read_mostly
= {
67 .family
= NFPROTO_IPV4
,
68 .checkentry
= dscp_mt_check
,
70 .matchsize
= sizeof(struct xt_dscp_info
),
75 .family
= NFPROTO_IPV6
,
76 .checkentry
= dscp_mt_check
,
78 .matchsize
= sizeof(struct xt_dscp_info
),
84 .family
= NFPROTO_IPV4
,
86 .matchsize
= sizeof(struct xt_tos_match_info
),
92 .family
= NFPROTO_IPV6
,
94 .matchsize
= sizeof(struct xt_tos_match_info
),
99 static int __init
dscp_mt_init(void)
101 return xt_register_matches(dscp_mt_reg
, ARRAY_SIZE(dscp_mt_reg
));
104 static void __exit
dscp_mt_exit(void)
106 xt_unregister_matches(dscp_mt_reg
, ARRAY_SIZE(dscp_mt_reg
));
109 module_init(dscp_mt_init
);
110 module_exit(dscp_mt_exit
);