blob: 7f03aa13a9554a0051600d1cfa31db97cc4777d7 [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
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070016#include <linux/netfilter/x_tables.h>
Jan Engelhardtc3b33e62007-12-04 23:37:54 -080017#include <linux/netfilter/xt_dscp.h>
18#include <linux/netfilter_ipv4/ipt_tos.h>
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070019
20MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080021MODULE_DESCRIPTION("Xtables: DSCP/TOS field match");
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070022MODULE_LICENSE("GPL");
23MODULE_ALIAS("ipt_dscp");
24MODULE_ALIAS("ip6t_dscp");
Jan Engelhardtc3b33e62007-12-04 23:37:54 -080025MODULE_ALIAS("ipt_tos");
Jan Engelhardtf1095ab2007-12-04 23:38:30 -080026MODULE_ALIAS("ip6t_tos");
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070027
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080028static bool
29dscp_mt(const struct sk_buff *skb, const struct net_device *in,
30 const struct net_device *out, const struct xt_match *match,
31 const void *matchinfo, int offset, unsigned int protoff, bool *hotdrop)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070032{
33 const struct xt_dscp_info *info = matchinfo;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070034 u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
35
36 return (dscp == info->dscp) ^ !!info->invert;
37}
38
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080039static bool
40dscp_mt6(const struct sk_buff *skb, const struct net_device *in,
41 const struct net_device *out, const struct xt_match *match,
42 const void *matchinfo, int offset, unsigned int protoff,
43 bool *hotdrop)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070044{
45 const struct xt_dscp_info *info = matchinfo;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070046 u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070047
48 return (dscp == info->dscp) ^ !!info->invert;
49}
50
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080051static bool
52dscp_mt_check(const char *tablename, const void *info,
53 const struct xt_match *match, void *matchinfo,
54 unsigned int hook_mask)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070055{
56 const u_int8_t dscp = ((struct xt_dscp_info *)matchinfo)->dscp;
57
58 if (dscp > XT_DSCP_MAX) {
59 printk(KERN_ERR "xt_dscp: dscp %x out of range\n", dscp);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070060 return false;
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070061 }
62
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070063 return true;
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070064}
65
Jan Engelhardtc3b33e62007-12-04 23:37:54 -080066static bool tos_mt_v0(const struct sk_buff *skb, const struct net_device *in,
67 const struct net_device *out,
68 const struct xt_match *match, const void *matchinfo,
69 int offset, unsigned int protoff, bool *hotdrop)
70{
71 const struct ipt_tos_info *info = matchinfo;
72
73 return (ip_hdr(skb)->tos == info->tos) ^ info->invert;
74}
75
Jan Engelhardtf1095ab2007-12-04 23:38:30 -080076static bool tos_mt(const struct sk_buff *skb, const struct net_device *in,
77 const struct net_device *out, const struct xt_match *match,
78 const void *matchinfo, int offset, unsigned int protoff,
79 bool *hotdrop)
80{
81 const struct xt_tos_match_info *info = matchinfo;
82
Jan Engelhardtee999d82008-10-08 11:35:01 +020083 if (match->family == NFPROTO_IPV4)
Jan Engelhardtf1095ab2007-12-04 23:38:30 -080084 return ((ip_hdr(skb)->tos & info->tos_mask) ==
85 info->tos_value) ^ !!info->invert;
86 else
87 return ((ipv6_get_dsfield(ipv6_hdr(skb)) & info->tos_mask) ==
88 info->tos_value) ^ !!info->invert;
89}
90
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080091static struct xt_match dscp_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070092 {
93 .name = "dscp",
Jan Engelhardtee999d82008-10-08 11:35:01 +020094 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080095 .checkentry = dscp_mt_check,
96 .match = dscp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070097 .matchsize = sizeof(struct xt_dscp_info),
98 .me = THIS_MODULE,
99 },
100 {
101 .name = "dscp",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200102 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800103 .checkentry = dscp_mt_check,
104 .match = dscp_mt6,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700105 .matchsize = sizeof(struct xt_dscp_info),
106 .me = THIS_MODULE,
107 },
Jan Engelhardtc3b33e62007-12-04 23:37:54 -0800108 {
109 .name = "tos",
110 .revision = 0,
Jan Engelhardtee999d82008-10-08 11:35:01 +0200111 .family = NFPROTO_IPV4,
Jan Engelhardtc3b33e62007-12-04 23:37:54 -0800112 .match = tos_mt_v0,
113 .matchsize = sizeof(struct ipt_tos_info),
114 .me = THIS_MODULE,
115 },
Jan Engelhardtf1095ab2007-12-04 23:38:30 -0800116 {
117 .name = "tos",
118 .revision = 1,
Jan Engelhardtee999d82008-10-08 11:35:01 +0200119 .family = NFPROTO_IPV4,
Jan Engelhardtf1095ab2007-12-04 23:38:30 -0800120 .match = tos_mt,
121 .matchsize = sizeof(struct xt_tos_match_info),
122 .me = THIS_MODULE,
123 },
124 {
125 .name = "tos",
126 .revision = 1,
Jan Engelhardtee999d82008-10-08 11:35:01 +0200127 .family = NFPROTO_IPV6,
Jan Engelhardtf1095ab2007-12-04 23:38:30 -0800128 .match = tos_mt,
129 .matchsize = sizeof(struct xt_tos_match_info),
130 .me = THIS_MODULE,
131 },
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -0700132};
133
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800134static int __init dscp_mt_init(void)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -0700135{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800136 return xt_register_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -0700137}
138
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800139static void __exit dscp_mt_exit(void)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -0700140{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800141 xt_unregister_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -0700142}
143
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800144module_init(dscp_mt_init);
145module_exit(dscp_mt_exit);