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