]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/netfilter/xt_dscp.c
netfilter: xtables: make use of caller family rather than match family
[net-next-2.6.git] / net / netfilter / xt_dscp.c
CommitLineData
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 17#include <linux/netfilter/xt_dscp.h>
9ba16276
YK
18
19MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
2ae15b64 20MODULE_DESCRIPTION("Xtables: DSCP/TOS field match");
9ba16276
YK
21MODULE_LICENSE("GPL");
22MODULE_ALIAS("ipt_dscp");
23MODULE_ALIAS("ip6t_dscp");
c3b33e6a 24MODULE_ALIAS("ipt_tos");
f1095ab5 25MODULE_ALIAS("ip6t_tos");
9ba16276 26
d3c5ee6d 27static bool
f7108a20 28dscp_mt(const struct sk_buff *skb, const struct xt_match_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 36static bool
f7108a20 37dscp_mt6(const struct sk_buff *skb, const struct xt_match_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
9b4fce7a 45static bool dscp_mt_check(const struct xt_mtchk_param *par)
9ba16276 46{
9b4fce7a 47 const struct xt_dscp_info *info = par->matchinfo;
9ba16276 48
9b4fce7a
JE
49 if (info->dscp > XT_DSCP_MAX) {
50 printk(KERN_ERR "xt_dscp: dscp %x out of range\n", info->dscp);
ccb79bdc 51 return false;
9ba16276
YK
52 }
53
ccb79bdc 54 return true;
9ba16276
YK
55}
56
f7108a20 57static bool tos_mt(const struct sk_buff *skb, const struct xt_match_param *par)
f1095ab5 58{
f7108a20 59 const struct xt_tos_match_info *info = par->matchinfo;
f1095ab5 60
aa5fa318 61 if (par->family == NFPROTO_IPV4)
f1095ab5
JE
62 return ((ip_hdr(skb)->tos & info->tos_mask) ==
63 info->tos_value) ^ !!info->invert;
64 else
65 return ((ipv6_get_dsfield(ipv6_hdr(skb)) & info->tos_mask) ==
66 info->tos_value) ^ !!info->invert;
67}
68
d3c5ee6d 69static struct xt_match dscp_mt_reg[] __read_mostly = {
4470bbc7
PM
70 {
71 .name = "dscp",
ee999d8b 72 .family = NFPROTO_IPV4,
d3c5ee6d
JE
73 .checkentry = dscp_mt_check,
74 .match = dscp_mt,
4470bbc7
PM
75 .matchsize = sizeof(struct xt_dscp_info),
76 .me = THIS_MODULE,
77 },
78 {
79 .name = "dscp",
ee999d8b 80 .family = NFPROTO_IPV6,
d3c5ee6d
JE
81 .checkentry = dscp_mt_check,
82 .match = dscp_mt6,
4470bbc7
PM
83 .matchsize = sizeof(struct xt_dscp_info),
84 .me = THIS_MODULE,
85 },
f1095ab5
JE
86 {
87 .name = "tos",
88 .revision = 1,
ee999d8b 89 .family = NFPROTO_IPV4,
f1095ab5
JE
90 .match = tos_mt,
91 .matchsize = sizeof(struct xt_tos_match_info),
92 .me = THIS_MODULE,
93 },
94 {
95 .name = "tos",
96 .revision = 1,
ee999d8b 97 .family = NFPROTO_IPV6,
f1095ab5
JE
98 .match = tos_mt,
99 .matchsize = sizeof(struct xt_tos_match_info),
100 .me = THIS_MODULE,
101 },
9ba16276
YK
102};
103
d3c5ee6d 104static int __init dscp_mt_init(void)
9ba16276 105{
d3c5ee6d 106 return xt_register_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
9ba16276
YK
107}
108
d3c5ee6d 109static void __exit dscp_mt_exit(void)
9ba16276 110{
d3c5ee6d 111 xt_unregister_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
9ba16276
YK
112}
113
d3c5ee6d
JE
114module_init(dscp_mt_init);
115module_exit(dscp_mt_exit);