blob: c3f8085460d774feaaef8246e37bd596e880e650 [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
Jan Engelhardtf7108a22008-10-08 11:35:18 +020029dscp_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070030{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020031 const struct xt_dscp_info *info = par->matchinfo;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070032 u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
33
34 return (dscp == info->dscp) ^ !!info->invert;
35}
36
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080037static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +020038dscp_mt6(const struct sk_buff *skb, const struct xt_match_param *par)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070039{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020040 const struct xt_dscp_info *info = par->matchinfo;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070041 u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070042
43 return (dscp == info->dscp) ^ !!info->invert;
44}
45
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020046static bool dscp_mt_check(const struct xt_mtchk_param *par)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070047{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020048 const struct xt_dscp_info *info = par->matchinfo;
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070049
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020050 if (info->dscp > XT_DSCP_MAX) {
51 printk(KERN_ERR "xt_dscp: dscp %x out of range\n", info->dscp);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070052 return false;
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070053 }
54
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070055 return true;
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070056}
57
Jan Engelhardtf7108a22008-10-08 11:35:18 +020058static bool
59tos_mt_v0(const struct sk_buff *skb, const struct xt_match_param *par)
Jan Engelhardtc3b33e62007-12-04 23:37:54 -080060{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020061 const struct ipt_tos_info *info = par->matchinfo;
Jan Engelhardtc3b33e62007-12-04 23:37:54 -080062
63 return (ip_hdr(skb)->tos == info->tos) ^ info->invert;
64}
65
Jan Engelhardtf7108a22008-10-08 11:35:18 +020066static bool tos_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Jan Engelhardtf1095ab2007-12-04 23:38:30 -080067{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020068 const struct xt_tos_match_info *info = par->matchinfo;
Jan Engelhardtf1095ab2007-12-04 23:38:30 -080069
Jan Engelhardtf7108a22008-10-08 11:35:18 +020070 if (par->match->family == NFPROTO_IPV4)
Jan Engelhardtf1095ab2007-12-04 23:38:30 -080071 return ((ip_hdr(skb)->tos & info->tos_mask) ==
72 info->tos_value) ^ !!info->invert;
73 else
74 return ((ipv6_get_dsfield(ipv6_hdr(skb)) & info->tos_mask) ==
75 info->tos_value) ^ !!info->invert;
76}
77
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080078static struct xt_match dscp_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070079 {
80 .name = "dscp",
Jan Engelhardtee999d82008-10-08 11:35:01 +020081 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080082 .checkentry = dscp_mt_check,
83 .match = dscp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070084 .matchsize = sizeof(struct xt_dscp_info),
85 .me = THIS_MODULE,
86 },
87 {
88 .name = "dscp",
Jan Engelhardtee999d82008-10-08 11:35:01 +020089 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080090 .checkentry = dscp_mt_check,
91 .match = dscp_mt6,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070092 .matchsize = sizeof(struct xt_dscp_info),
93 .me = THIS_MODULE,
94 },
Jan Engelhardtc3b33e62007-12-04 23:37:54 -080095 {
96 .name = "tos",
97 .revision = 0,
Jan Engelhardtee999d82008-10-08 11:35:01 +020098 .family = NFPROTO_IPV4,
Jan Engelhardtc3b33e62007-12-04 23:37:54 -080099 .match = tos_mt_v0,
100 .matchsize = sizeof(struct ipt_tos_info),
101 .me = THIS_MODULE,
102 },
Jan Engelhardtf1095ab2007-12-04 23:38:30 -0800103 {
104 .name = "tos",
105 .revision = 1,
Jan Engelhardtee999d82008-10-08 11:35:01 +0200106 .family = NFPROTO_IPV4,
Jan Engelhardtf1095ab2007-12-04 23:38:30 -0800107 .match = tos_mt,
108 .matchsize = sizeof(struct xt_tos_match_info),
109 .me = THIS_MODULE,
110 },
111 {
112 .name = "tos",
113 .revision = 1,
Jan Engelhardtee999d82008-10-08 11:35:01 +0200114 .family = NFPROTO_IPV6,
Jan Engelhardtf1095ab2007-12-04 23:38:30 -0800115 .match = tos_mt,
116 .matchsize = sizeof(struct xt_tos_match_info),
117 .me = THIS_MODULE,
118 },
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -0700119};
120
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800121static int __init dscp_mt_init(void)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -0700122{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800123 return xt_register_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -0700124}
125
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800126static void __exit dscp_mt_exit(void)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -0700127{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800128 xt_unregister_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -0700129}
130
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800131module_init(dscp_mt_init);
132module_exit(dscp_mt_exit);