1 /* IP tables module for matching the value of the IPv4/IPv6 DSCP field
3 * (C) 2002 by Harald Welte <laforge@netfilter.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
13 #include <linux/ipv6.h>
14 #include <net/dsfield.h>
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter/xt_dscp.h>
19 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
20 MODULE_DESCRIPTION("Xtables: DSCP/TOS field match");
21 MODULE_LICENSE("GPL");
22 MODULE_ALIAS("ipt_dscp");
23 MODULE_ALIAS("ip6t_dscp");
24 MODULE_ALIAS("ipt_tos");
25 MODULE_ALIAS("ip6t_tos");
28 dscp_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
30 const struct xt_dscp_info
*info
= par
->matchinfo
;
31 u_int8_t dscp
= ipv4_get_dsfield(ip_hdr(skb
)) >> XT_DSCP_SHIFT
;
33 return (dscp
== info
->dscp
) ^ !!info
->invert
;
37 dscp_mt6(const struct sk_buff
*skb
, struct xt_action_param
*par
)
39 const struct xt_dscp_info
*info
= par
->matchinfo
;
40 u_int8_t dscp
= ipv6_get_dsfield(ipv6_hdr(skb
)) >> XT_DSCP_SHIFT
;
42 return (dscp
== info
->dscp
) ^ !!info
->invert
;
45 static int dscp_mt_check(const struct xt_mtchk_param
*par
)
47 const struct xt_dscp_info
*info
= par
->matchinfo
;
49 if (info
->dscp
> XT_DSCP_MAX
) {
50 pr_info("dscp %x out of range\n", info
->dscp
);
57 static bool tos_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
59 const struct xt_tos_match_info
*info
= par
->matchinfo
;
61 if (xt_family(par
) == NFPROTO_IPV4
)
62 return ((ip_hdr(skb
)->tos
& info
->tos_mask
) ==
63 info
->tos_value
) ^ !!info
->invert
;
65 return ((ipv6_get_dsfield(ipv6_hdr(skb
)) & info
->tos_mask
) ==
66 info
->tos_value
) ^ !!info
->invert
;
69 static struct xt_match dscp_mt_reg
[] __read_mostly
= {
72 .family
= NFPROTO_IPV4
,
73 .checkentry
= dscp_mt_check
,
75 .matchsize
= sizeof(struct xt_dscp_info
),
80 .family
= NFPROTO_IPV6
,
81 .checkentry
= dscp_mt_check
,
83 .matchsize
= sizeof(struct xt_dscp_info
),
89 .family
= NFPROTO_IPV4
,
91 .matchsize
= sizeof(struct xt_tos_match_info
),
97 .family
= NFPROTO_IPV6
,
99 .matchsize
= sizeof(struct xt_tos_match_info
),
104 static int __init
dscp_mt_init(void)
106 return xt_register_matches(dscp_mt_reg
, ARRAY_SIZE(dscp_mt_reg
));
109 static void __exit
dscp_mt_exit(void)
111 xt_unregister_matches(dscp_mt_reg
, ARRAY_SIZE(dscp_mt_reg
));
114 module_init(dscp_mt_init
);
115 module_exit(dscp_mt_exit
);