netfilter: xtables: move extension arguments into compound structure (1/6)
[safe/jmp/linux-2.6] / net / netfilter / xt_dscp.c
1 /* IP tables module for matching the value of the IPv4/IPv6 DSCP field
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
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter/xt_dscp.h>
18 #include <linux/netfilter_ipv4/ipt_tos.h>
19
20 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
21 MODULE_DESCRIPTION("Xtables: DSCP/TOS field match");
22 MODULE_LICENSE("GPL");
23 MODULE_ALIAS("ipt_dscp");
24 MODULE_ALIAS("ip6t_dscp");
25 MODULE_ALIAS("ipt_tos");
26 MODULE_ALIAS("ip6t_tos");
27
28 static bool
29 dscp_mt(const struct sk_buff *skb, const struct xt_match_param *par)
30 {
31         const struct xt_dscp_info *info = par->matchinfo;
32         u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
33
34         return (dscp == info->dscp) ^ !!info->invert;
35 }
36
37 static bool
38 dscp_mt6(const struct sk_buff *skb, const struct xt_match_param *par)
39 {
40         const struct xt_dscp_info *info = par->matchinfo;
41         u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
42
43         return (dscp == info->dscp) ^ !!info->invert;
44 }
45
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)
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);
55                 return false;
56         }
57
58         return true;
59 }
60
61 static bool
62 tos_mt_v0(const struct sk_buff *skb, const struct xt_match_param *par)
63 {
64         const struct ipt_tos_info *info = par->matchinfo;
65
66         return (ip_hdr(skb)->tos == info->tos) ^ info->invert;
67 }
68
69 static bool tos_mt(const struct sk_buff *skb, const struct xt_match_param *par)
70 {
71         const struct xt_tos_match_info *info = par->matchinfo;
72
73         if (par->match->family == NFPROTO_IPV4)
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
81 static struct xt_match dscp_mt_reg[] __read_mostly = {
82         {
83                 .name           = "dscp",
84                 .family         = NFPROTO_IPV4,
85                 .checkentry     = dscp_mt_check,
86                 .match          = dscp_mt,
87                 .matchsize      = sizeof(struct xt_dscp_info),
88                 .me             = THIS_MODULE,
89         },
90         {
91                 .name           = "dscp",
92                 .family         = NFPROTO_IPV6,
93                 .checkentry     = dscp_mt_check,
94                 .match          = dscp_mt6,
95                 .matchsize      = sizeof(struct xt_dscp_info),
96                 .me             = THIS_MODULE,
97         },
98         {
99                 .name           = "tos",
100                 .revision       = 0,
101                 .family         = NFPROTO_IPV4,
102                 .match          = tos_mt_v0,
103                 .matchsize      = sizeof(struct ipt_tos_info),
104                 .me             = THIS_MODULE,
105         },
106         {
107                 .name           = "tos",
108                 .revision       = 1,
109                 .family         = NFPROTO_IPV4,
110                 .match          = tos_mt,
111                 .matchsize      = sizeof(struct xt_tos_match_info),
112                 .me             = THIS_MODULE,
113         },
114         {
115                 .name           = "tos",
116                 .revision       = 1,
117                 .family         = NFPROTO_IPV6,
118                 .match          = tos_mt,
119                 .matchsize      = sizeof(struct xt_tos_match_info),
120                 .me             = THIS_MODULE,
121         },
122 };
123
124 static int __init dscp_mt_init(void)
125 {
126         return xt_register_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
127 }
128
129 static void __exit dscp_mt_exit(void)
130 {
131         xt_unregister_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
132 }
133
134 module_init(dscp_mt_init);
135 module_exit(dscp_mt_exit);