blob: 63f7354ca9aa0be95d25a8ac6720c8363b0a8089 [file] [log] [blame]
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -07001/* IP tables module for matching the value of the IPv4/IPv6 DSCP field
2 *
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -07003 * (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/xt_dscp.h>
17#include <linux/netfilter/x_tables.h>
18
19MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
20MODULE_DESCRIPTION("x_tables DSCP matching module");
21MODULE_LICENSE("GPL");
22MODULE_ALIAS("ipt_dscp");
23MODULE_ALIAS("ip6t_dscp");
24
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080025static bool
26dscp_mt(const struct sk_buff *skb, const struct net_device *in,
27 const struct net_device *out, const struct xt_match *match,
28 const void *matchinfo, int offset, unsigned int protoff, bool *hotdrop)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070029{
30 const struct xt_dscp_info *info = matchinfo;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070031 u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
32
33 return (dscp == info->dscp) ^ !!info->invert;
34}
35
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080036static bool
37dscp_mt6(const struct sk_buff *skb, const struct net_device *in,
38 const struct net_device *out, const struct xt_match *match,
39 const void *matchinfo, int offset, unsigned int protoff,
40 bool *hotdrop)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070041{
42 const struct xt_dscp_info *info = matchinfo;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070043 u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070044
45 return (dscp == info->dscp) ^ !!info->invert;
46}
47
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080048static bool
49dscp_mt_check(const char *tablename, const void *info,
50 const struct xt_match *match, void *matchinfo,
51 unsigned int hook_mask)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070052{
53 const u_int8_t dscp = ((struct xt_dscp_info *)matchinfo)->dscp;
54
55 if (dscp > XT_DSCP_MAX) {
56 printk(KERN_ERR "xt_dscp: dscp %x out of range\n", dscp);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070057 return false;
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070058 }
59
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070060 return true;
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070061}
62
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080063static struct xt_match dscp_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070064 {
65 .name = "dscp",
66 .family = AF_INET,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080067 .checkentry = dscp_mt_check,
68 .match = dscp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070069 .matchsize = sizeof(struct xt_dscp_info),
70 .me = THIS_MODULE,
71 },
72 {
73 .name = "dscp",
74 .family = AF_INET6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080075 .checkentry = dscp_mt_check,
76 .match = dscp_mt6,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070077 .matchsize = sizeof(struct xt_dscp_info),
78 .me = THIS_MODULE,
79 },
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070080};
81
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080082static int __init dscp_mt_init(void)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070083{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080084 return xt_register_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070085}
86
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080087static void __exit dscp_mt_exit(void)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070088{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080089 xt_unregister_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070090}
91
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080092module_init(dscp_mt_init);
93module_exit(dscp_mt_exit);