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 | */ | |
9 | ||
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 JE |
17 | #include <linux/netfilter/xt_dscp.h> |
18 | #include <linux/netfilter_ipv4/ipt_tos.h> | |
9ba16276 YK |
19 | |
20 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); | |
2ae15b64 | 21 | MODULE_DESCRIPTION("Xtables: DSCP/TOS field match"); |
9ba16276 YK |
22 | MODULE_LICENSE("GPL"); |
23 | MODULE_ALIAS("ipt_dscp"); | |
24 | MODULE_ALIAS("ip6t_dscp"); | |
c3b33e6a | 25 | MODULE_ALIAS("ipt_tos"); |
f1095ab5 | 26 | MODULE_ALIAS("ip6t_tos"); |
9ba16276 | 27 | |
d3c5ee6d | 28 | static bool |
f7108a20 | 29 | dscp_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
1d93a9cb | 30 | { |
f7108a20 | 31 | const struct xt_dscp_info *info = par->matchinfo; |
1d93a9cb JE |
32 | u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT; |
33 | ||
34 | return (dscp == info->dscp) ^ !!info->invert; | |
35 | } | |
36 | ||
d3c5ee6d | 37 | static bool |
f7108a20 | 38 | dscp_mt6(const struct sk_buff *skb, const struct xt_match_param *par) |
9ba16276 | 39 | { |
f7108a20 | 40 | const struct xt_dscp_info *info = par->matchinfo; |
0660e03f | 41 | u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT; |
9ba16276 YK |
42 | |
43 | return (dscp == info->dscp) ^ !!info->invert; | |
44 | } | |
45 | ||
d3c5ee6d JE |
46 | static bool |
47 | dscp_mt_check(const char *tablename, const void *info, | |
48 | const struct xt_match *match, void *matchinfo, | |
49 | unsigned int hook_mask) | |
9ba16276 YK |
50 | { |
51 | const u_int8_t dscp = ((struct xt_dscp_info *)matchinfo)->dscp; | |
52 | ||
53 | if (dscp > XT_DSCP_MAX) { | |
54 | printk(KERN_ERR "xt_dscp: dscp %x out of range\n", dscp); | |
ccb79bdc | 55 | return false; |
9ba16276 YK |
56 | } |
57 | ||
ccb79bdc | 58 | return true; |
9ba16276 YK |
59 | } |
60 | ||
f7108a20 JE |
61 | static bool |
62 | tos_mt_v0(const struct sk_buff *skb, const struct xt_match_param *par) | |
c3b33e6a | 63 | { |
f7108a20 | 64 | const struct ipt_tos_info *info = par->matchinfo; |
c3b33e6a JE |
65 | |
66 | return (ip_hdr(skb)->tos == info->tos) ^ info->invert; | |
67 | } | |
68 | ||
f7108a20 | 69 | static bool tos_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
f1095ab5 | 70 | { |
f7108a20 | 71 | const struct xt_tos_match_info *info = par->matchinfo; |
f1095ab5 | 72 | |
f7108a20 | 73 | if (par->match->family == NFPROTO_IPV4) |
f1095ab5 JE |
74 | return ((ip_hdr(skb)->tos & info->tos_mask) == |
75 | info->tos_value) ^ !!info->invert; | |
76 | else | |
77 | return ((ipv6_get_dsfield(ipv6_hdr(skb)) & info->tos_mask) == | |
78 | info->tos_value) ^ !!info->invert; | |
79 | } | |
80 | ||
d3c5ee6d | 81 | static struct xt_match dscp_mt_reg[] __read_mostly = { |
4470bbc7 PM |
82 | { |
83 | .name = "dscp", | |
ee999d8b | 84 | .family = NFPROTO_IPV4, |
d3c5ee6d JE |
85 | .checkentry = dscp_mt_check, |
86 | .match = dscp_mt, | |
4470bbc7 PM |
87 | .matchsize = sizeof(struct xt_dscp_info), |
88 | .me = THIS_MODULE, | |
89 | }, | |
90 | { | |
91 | .name = "dscp", | |
ee999d8b | 92 | .family = NFPROTO_IPV6, |
d3c5ee6d JE |
93 | .checkentry = dscp_mt_check, |
94 | .match = dscp_mt6, | |
4470bbc7 PM |
95 | .matchsize = sizeof(struct xt_dscp_info), |
96 | .me = THIS_MODULE, | |
97 | }, | |
c3b33e6a JE |
98 | { |
99 | .name = "tos", | |
100 | .revision = 0, | |
ee999d8b | 101 | .family = NFPROTO_IPV4, |
c3b33e6a JE |
102 | .match = tos_mt_v0, |
103 | .matchsize = sizeof(struct ipt_tos_info), | |
104 | .me = THIS_MODULE, | |
105 | }, | |
f1095ab5 JE |
106 | { |
107 | .name = "tos", | |
108 | .revision = 1, | |
ee999d8b | 109 | .family = NFPROTO_IPV4, |
f1095ab5 JE |
110 | .match = tos_mt, |
111 | .matchsize = sizeof(struct xt_tos_match_info), | |
112 | .me = THIS_MODULE, | |
113 | }, | |
114 | { | |
115 | .name = "tos", | |
116 | .revision = 1, | |
ee999d8b | 117 | .family = NFPROTO_IPV6, |
f1095ab5 JE |
118 | .match = tos_mt, |
119 | .matchsize = sizeof(struct xt_tos_match_info), | |
120 | .me = THIS_MODULE, | |
121 | }, | |
9ba16276 YK |
122 | }; |
123 | ||
d3c5ee6d | 124 | static int __init dscp_mt_init(void) |
9ba16276 | 125 | { |
d3c5ee6d | 126 | return xt_register_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg)); |
9ba16276 YK |
127 | } |
128 | ||
d3c5ee6d | 129 | static void __exit dscp_mt_exit(void) |
9ba16276 | 130 | { |
d3c5ee6d | 131 | xt_unregister_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg)); |
9ba16276 YK |
132 | } |
133 | ||
d3c5ee6d JE |
134 | module_init(dscp_mt_init); |
135 | module_exit(dscp_mt_exit); |