blob: a4c2b862f820af17ae9e36bab0e6cede0dc58cfc [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 */
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +01009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070010#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>
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070018
19MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080020MODULE_DESCRIPTION("Xtables: DSCP/TOS field match");
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070021MODULE_LICENSE("GPL");
22MODULE_ALIAS("ipt_dscp");
23MODULE_ALIAS("ip6t_dscp");
Jan Engelhardtc3b33e62007-12-04 23:37:54 -080024MODULE_ALIAS("ipt_tos");
Jan Engelhardtf1095ab2007-12-04 23:38:30 -080025MODULE_ALIAS("ip6t_tos");
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070026
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080027static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020028dscp_mt(const struct sk_buff *skb, struct xt_action_param *par)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070029{
Jan Engelhardtf7108a202008-10-08 11:35:18 +020030 const struct xt_dscp_info *info = par->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
Jan Engelhardt62fc8052009-07-07 20:42:08 +020037dscp_mt6(const struct sk_buff *skb, struct xt_action_param *par)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070038{
Jan Engelhardtf7108a202008-10-08 11:35:18 +020039 const struct xt_dscp_info *info = par->matchinfo;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070040 u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070041
42 return (dscp == info->dscp) ^ !!info->invert;
43}
44
Jan Engelhardtb0f38452010-03-19 17:16:42 +010045static int dscp_mt_check(const struct xt_mtchk_param *par)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070046{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020047 const struct xt_dscp_info *info = par->matchinfo;
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070048
Florian Westphal0cc95012018-02-09 15:51:59 +010049 if (info->dscp > XT_DSCP_MAX)
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010050 return -EDOM;
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070051
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010052 return 0;
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -070053}
54
Jan Engelhardt62fc8052009-07-07 20:42:08 +020055static bool tos_mt(const struct sk_buff *skb, struct xt_action_param *par)
Jan Engelhardtf1095ab2007-12-04 23:38:30 -080056{
Jan Engelhardtf7108a202008-10-08 11:35:18 +020057 const struct xt_tos_match_info *info = par->matchinfo;
Jan Engelhardtf1095ab2007-12-04 23:38:30 -080058
Pablo Neira Ayuso613dbd92016-11-03 10:56:21 +010059 if (xt_family(par) == NFPROTO_IPV4)
Jan Engelhardtf1095ab2007-12-04 23:38:30 -080060 return ((ip_hdr(skb)->tos & info->tos_mask) ==
61 info->tos_value) ^ !!info->invert;
62 else
63 return ((ipv6_get_dsfield(ipv6_hdr(skb)) & info->tos_mask) ==
64 info->tos_value) ^ !!info->invert;
65}
66
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080067static struct xt_match dscp_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070068 {
69 .name = "dscp",
Jan Engelhardtee999d82008-10-08 11:35:01 +020070 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080071 .checkentry = dscp_mt_check,
72 .match = dscp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070073 .matchsize = sizeof(struct xt_dscp_info),
74 .me = THIS_MODULE,
75 },
76 {
77 .name = "dscp",
Jan Engelhardtee999d82008-10-08 11:35:01 +020078 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080079 .checkentry = dscp_mt_check,
80 .match = dscp_mt6,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070081 .matchsize = sizeof(struct xt_dscp_info),
82 .me = THIS_MODULE,
83 },
Jan Engelhardtc3b33e62007-12-04 23:37:54 -080084 {
85 .name = "tos",
Jan Engelhardtf1095ab2007-12-04 23:38:30 -080086 .revision = 1,
Jan Engelhardtee999d82008-10-08 11:35:01 +020087 .family = NFPROTO_IPV4,
Jan Engelhardtf1095ab2007-12-04 23:38:30 -080088 .match = tos_mt,
89 .matchsize = sizeof(struct xt_tos_match_info),
90 .me = THIS_MODULE,
91 },
92 {
93 .name = "tos",
94 .revision = 1,
Jan Engelhardtee999d82008-10-08 11:35:01 +020095 .family = NFPROTO_IPV6,
Jan Engelhardtf1095ab2007-12-04 23:38:30 -080096 .match = tos_mt,
97 .matchsize = sizeof(struct xt_tos_match_info),
98 .me = THIS_MODULE,
99 },
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -0700100};
101
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800102static int __init dscp_mt_init(void)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -0700103{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800104 return xt_register_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -0700105}
106
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800107static void __exit dscp_mt_exit(void)
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -0700108{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800109 xt_unregister_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
Yasuyuki Kozakai9ba16272006-08-22 00:29:37 -0700110}
111
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800112module_init(dscp_mt_init);
113module_exit(dscp_mt_exit);