]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/netfilter/xt_dscp.c
[NETFILTER]: Merge ipt_tos into xt_dscp
[linux-2.6-omap-h63xx.git] / 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("x_tables DSCP/tos matching module");
22 MODULE_LICENSE("GPL");
23 MODULE_ALIAS("ipt_dscp");
24 MODULE_ALIAS("ip6t_dscp");
25 MODULE_ALIAS("ipt_tos");
26
27 static bool
28 dscp_mt(const struct sk_buff *skb, const struct net_device *in,
29         const struct net_device *out, const struct xt_match *match,
30         const void *matchinfo, int offset, unsigned int protoff, bool *hotdrop)
31 {
32         const struct xt_dscp_info *info = matchinfo;
33         u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
34
35         return (dscp == info->dscp) ^ !!info->invert;
36 }
37
38 static bool
39 dscp_mt6(const struct sk_buff *skb, const struct net_device *in,
40          const struct net_device *out, const struct xt_match *match,
41          const void *matchinfo, int offset, unsigned int protoff,
42          bool *hotdrop)
43 {
44         const struct xt_dscp_info *info = matchinfo;
45         u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
46
47         return (dscp == info->dscp) ^ !!info->invert;
48 }
49
50 static bool
51 dscp_mt_check(const char *tablename, const void *info,
52               const struct xt_match *match, void *matchinfo,
53               unsigned int hook_mask)
54 {
55         const u_int8_t dscp = ((struct xt_dscp_info *)matchinfo)->dscp;
56
57         if (dscp > XT_DSCP_MAX) {
58                 printk(KERN_ERR "xt_dscp: dscp %x out of range\n", dscp);
59                 return false;
60         }
61
62         return true;
63 }
64
65 static bool tos_mt_v0(const struct sk_buff *skb, const struct net_device *in,
66                       const struct net_device *out,
67                       const struct xt_match *match, const void *matchinfo,
68                       int offset, unsigned int protoff, bool *hotdrop)
69 {
70         const struct ipt_tos_info *info = matchinfo;
71
72         return (ip_hdr(skb)->tos == info->tos) ^ info->invert;
73 }
74
75 static struct xt_match dscp_mt_reg[] __read_mostly = {
76         {
77                 .name           = "dscp",
78                 .family         = AF_INET,
79                 .checkentry     = dscp_mt_check,
80                 .match          = dscp_mt,
81                 .matchsize      = sizeof(struct xt_dscp_info),
82                 .me             = THIS_MODULE,
83         },
84         {
85                 .name           = "dscp",
86                 .family         = AF_INET6,
87                 .checkentry     = dscp_mt_check,
88                 .match          = dscp_mt6,
89                 .matchsize      = sizeof(struct xt_dscp_info),
90                 .me             = THIS_MODULE,
91         },
92         {
93                 .name           = "tos",
94                 .revision       = 0,
95                 .family         = AF_INET,
96                 .match          = tos_mt_v0,
97                 .matchsize      = sizeof(struct ipt_tos_info),
98                 .me             = THIS_MODULE,
99         },
100 };
101
102 static int __init dscp_mt_init(void)
103 {
104         return xt_register_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
105 }
106
107 static void __exit dscp_mt_exit(void)
108 {
109         xt_unregister_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
110 }
111
112 module_init(dscp_mt_init);
113 module_exit(dscp_mt_exit);