Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
9ba16276 | 2 | /* IP tables module for matching the value of the IPv4/IPv6 DSCP field |
9ba16276 YK |
3 | * |
4 | * (C) 2002 by Harald Welte <laforge@netfilter.org> | |
9ba16276 | 5 | */ |
8bee4bad | 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
9ba16276 YK |
7 | #include <linux/module.h> |
8 | #include <linux/skbuff.h> | |
9 | #include <linux/ip.h> | |
10 | #include <linux/ipv6.h> | |
11 | #include <net/dsfield.h> | |
12 | ||
9ba16276 | 13 | #include <linux/netfilter/x_tables.h> |
c3b33e6a | 14 | #include <linux/netfilter/xt_dscp.h> |
9ba16276 YK |
15 | |
16 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); | |
2ae15b64 | 17 | MODULE_DESCRIPTION("Xtables: DSCP/TOS field match"); |
9ba16276 YK |
18 | MODULE_LICENSE("GPL"); |
19 | MODULE_ALIAS("ipt_dscp"); | |
20 | MODULE_ALIAS("ip6t_dscp"); | |
c3b33e6a | 21 | MODULE_ALIAS("ipt_tos"); |
f1095ab5 | 22 | MODULE_ALIAS("ip6t_tos"); |
9ba16276 | 23 | |
d3c5ee6d | 24 | static bool |
62fc8051 | 25 | dscp_mt(const struct sk_buff *skb, struct xt_action_param *par) |
1d93a9cb | 26 | { |
f7108a20 | 27 | const struct xt_dscp_info *info = par->matchinfo; |
1d93a9cb JE |
28 | u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT; |
29 | ||
30 | return (dscp == info->dscp) ^ !!info->invert; | |
31 | } | |
32 | ||
d3c5ee6d | 33 | static bool |
62fc8051 | 34 | dscp_mt6(const struct sk_buff *skb, struct xt_action_param *par) |
9ba16276 | 35 | { |
f7108a20 | 36 | const struct xt_dscp_info *info = par->matchinfo; |
0660e03f | 37 | u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT; |
9ba16276 YK |
38 | |
39 | return (dscp == info->dscp) ^ !!info->invert; | |
40 | } | |
41 | ||
b0f38452 | 42 | static int dscp_mt_check(const struct xt_mtchk_param *par) |
9ba16276 | 43 | { |
9b4fce7a | 44 | const struct xt_dscp_info *info = par->matchinfo; |
9ba16276 | 45 | |
0cc9501f | 46 | if (info->dscp > XT_DSCP_MAX) |
4a5a5c73 | 47 | return -EDOM; |
9ba16276 | 48 | |
bd414ee6 | 49 | return 0; |
9ba16276 YK |
50 | } |
51 | ||
62fc8051 | 52 | static bool tos_mt(const struct sk_buff *skb, struct xt_action_param *par) |
f1095ab5 | 53 | { |
f7108a20 | 54 | const struct xt_tos_match_info *info = par->matchinfo; |
f1095ab5 | 55 | |
613dbd95 | 56 | if (xt_family(par) == NFPROTO_IPV4) |
f1095ab5 JE |
57 | return ((ip_hdr(skb)->tos & info->tos_mask) == |
58 | info->tos_value) ^ !!info->invert; | |
59 | else | |
60 | return ((ipv6_get_dsfield(ipv6_hdr(skb)) & info->tos_mask) == | |
61 | info->tos_value) ^ !!info->invert; | |
62 | } | |
63 | ||
d3c5ee6d | 64 | static struct xt_match dscp_mt_reg[] __read_mostly = { |
4470bbc7 PM |
65 | { |
66 | .name = "dscp", | |
ee999d8b | 67 | .family = NFPROTO_IPV4, |
d3c5ee6d JE |
68 | .checkentry = dscp_mt_check, |
69 | .match = dscp_mt, | |
4470bbc7 PM |
70 | .matchsize = sizeof(struct xt_dscp_info), |
71 | .me = THIS_MODULE, | |
72 | }, | |
73 | { | |
74 | .name = "dscp", | |
ee999d8b | 75 | .family = NFPROTO_IPV6, |
d3c5ee6d JE |
76 | .checkentry = dscp_mt_check, |
77 | .match = dscp_mt6, | |
4470bbc7 PM |
78 | .matchsize = sizeof(struct xt_dscp_info), |
79 | .me = THIS_MODULE, | |
80 | }, | |
f1095ab5 JE |
81 | { |
82 | .name = "tos", | |
83 | .revision = 1, | |
ee999d8b | 84 | .family = NFPROTO_IPV4, |
f1095ab5 JE |
85 | .match = tos_mt, |
86 | .matchsize = sizeof(struct xt_tos_match_info), | |
87 | .me = THIS_MODULE, | |
88 | }, | |
89 | { | |
90 | .name = "tos", | |
91 | .revision = 1, | |
ee999d8b | 92 | .family = NFPROTO_IPV6, |
f1095ab5 JE |
93 | .match = tos_mt, |
94 | .matchsize = sizeof(struct xt_tos_match_info), | |
95 | .me = THIS_MODULE, | |
96 | }, | |
9ba16276 YK |
97 | }; |
98 | ||
d3c5ee6d | 99 | static int __init dscp_mt_init(void) |
9ba16276 | 100 | { |
d3c5ee6d | 101 | return xt_register_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg)); |
9ba16276 YK |
102 | } |
103 | ||
d3c5ee6d | 104 | static void __exit dscp_mt_exit(void) |
9ba16276 | 105 | { |
d3c5ee6d | 106 | xt_unregister_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg)); |
9ba16276 YK |
107 | } |
108 | ||
d3c5ee6d JE |
109 | module_init(dscp_mt_init); |
110 | module_exit(dscp_mt_exit); |