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 | ||
9b4fce7a | 46 | static bool dscp_mt_check(const struct xt_mtchk_param *par) |
9ba16276 | 47 | { |
9b4fce7a | 48 | const struct xt_dscp_info *info = par->matchinfo; |
9ba16276 | 49 | |
9b4fce7a JE |
50 | if (info->dscp > XT_DSCP_MAX) { |
51 | printk(KERN_ERR "xt_dscp: dscp %x out of range\n", info->dscp); | |
ccb79bdc | 52 | return false; |
9ba16276 YK |
53 | } |
54 | ||
ccb79bdc | 55 | return true; |
9ba16276 YK |
56 | } |
57 | ||
f7108a20 JE |
58 | static bool |
59 | tos_mt_v0(const struct sk_buff *skb, const struct xt_match_param *par) | |
c3b33e6a | 60 | { |
f7108a20 | 61 | const struct ipt_tos_info *info = par->matchinfo; |
c3b33e6a JE |
62 | |
63 | return (ip_hdr(skb)->tos == info->tos) ^ info->invert; | |
64 | } | |
65 | ||
f7108a20 | 66 | static bool tos_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
f1095ab5 | 67 | { |
f7108a20 | 68 | const struct xt_tos_match_info *info = par->matchinfo; |
f1095ab5 | 69 | |
f7108a20 | 70 | if (par->match->family == NFPROTO_IPV4) |
f1095ab5 JE |
71 | return ((ip_hdr(skb)->tos & info->tos_mask) == |
72 | info->tos_value) ^ !!info->invert; | |
73 | else | |
74 | return ((ipv6_get_dsfield(ipv6_hdr(skb)) & info->tos_mask) == | |
75 | info->tos_value) ^ !!info->invert; | |
76 | } | |
77 | ||
d3c5ee6d | 78 | static struct xt_match dscp_mt_reg[] __read_mostly = { |
4470bbc7 PM |
79 | { |
80 | .name = "dscp", | |
ee999d8b | 81 | .family = NFPROTO_IPV4, |
d3c5ee6d JE |
82 | .checkentry = dscp_mt_check, |
83 | .match = dscp_mt, | |
4470bbc7 PM |
84 | .matchsize = sizeof(struct xt_dscp_info), |
85 | .me = THIS_MODULE, | |
86 | }, | |
87 | { | |
88 | .name = "dscp", | |
ee999d8b | 89 | .family = NFPROTO_IPV6, |
d3c5ee6d JE |
90 | .checkentry = dscp_mt_check, |
91 | .match = dscp_mt6, | |
4470bbc7 PM |
92 | .matchsize = sizeof(struct xt_dscp_info), |
93 | .me = THIS_MODULE, | |
94 | }, | |
c3b33e6a JE |
95 | { |
96 | .name = "tos", | |
97 | .revision = 0, | |
ee999d8b | 98 | .family = NFPROTO_IPV4, |
c3b33e6a JE |
99 | .match = tos_mt_v0, |
100 | .matchsize = sizeof(struct ipt_tos_info), | |
101 | .me = THIS_MODULE, | |
102 | }, | |
f1095ab5 JE |
103 | { |
104 | .name = "tos", | |
105 | .revision = 1, | |
ee999d8b | 106 | .family = NFPROTO_IPV4, |
f1095ab5 JE |
107 | .match = tos_mt, |
108 | .matchsize = sizeof(struct xt_tos_match_info), | |
109 | .me = THIS_MODULE, | |
110 | }, | |
111 | { | |
112 | .name = "tos", | |
113 | .revision = 1, | |
ee999d8b | 114 | .family = NFPROTO_IPV6, |
f1095ab5 JE |
115 | .match = tos_mt, |
116 | .matchsize = sizeof(struct xt_tos_match_info), | |
117 | .me = THIS_MODULE, | |
118 | }, | |
9ba16276 YK |
119 | }; |
120 | ||
d3c5ee6d | 121 | static int __init dscp_mt_init(void) |
9ba16276 | 122 | { |
d3c5ee6d | 123 | return xt_register_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg)); |
9ba16276 YK |
124 | } |
125 | ||
d3c5ee6d | 126 | static void __exit dscp_mt_exit(void) |
9ba16276 | 127 | { |
d3c5ee6d | 128 | xt_unregister_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg)); |
9ba16276 YK |
129 | } |
130 | ||
d3c5ee6d JE |
131 | module_init(dscp_mt_init); |
132 | module_exit(dscp_mt_exit); |