0001
0002
0003
0004
0005
0006 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0007 #include <linux/module.h>
0008 #include <linux/skbuff.h>
0009 #include <linux/ip.h>
0010 #include <linux/ipv6.h>
0011 #include <net/dsfield.h>
0012
0013 #include <linux/netfilter/x_tables.h>
0014 #include <linux/netfilter/xt_dscp.h>
0015
0016 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
0017 MODULE_DESCRIPTION("Xtables: DSCP/TOS field match");
0018 MODULE_LICENSE("GPL");
0019 MODULE_ALIAS("ipt_dscp");
0020 MODULE_ALIAS("ip6t_dscp");
0021 MODULE_ALIAS("ipt_tos");
0022 MODULE_ALIAS("ip6t_tos");
0023
0024 static bool
0025 dscp_mt(const struct sk_buff *skb, struct xt_action_param *par)
0026 {
0027 const struct xt_dscp_info *info = par->matchinfo;
0028 u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
0029
0030 return (dscp == info->dscp) ^ !!info->invert;
0031 }
0032
0033 static bool
0034 dscp_mt6(const struct sk_buff *skb, struct xt_action_param *par)
0035 {
0036 const struct xt_dscp_info *info = par->matchinfo;
0037 u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
0038
0039 return (dscp == info->dscp) ^ !!info->invert;
0040 }
0041
0042 static int dscp_mt_check(const struct xt_mtchk_param *par)
0043 {
0044 const struct xt_dscp_info *info = par->matchinfo;
0045
0046 if (info->dscp > XT_DSCP_MAX)
0047 return -EDOM;
0048
0049 return 0;
0050 }
0051
0052 static bool tos_mt(const struct sk_buff *skb, struct xt_action_param *par)
0053 {
0054 const struct xt_tos_match_info *info = par->matchinfo;
0055
0056 if (xt_family(par) == NFPROTO_IPV4)
0057 return ((ip_hdr(skb)->tos & info->tos_mask) ==
0058 info->tos_value) ^ !!info->invert;
0059 else
0060 return ((ipv6_get_dsfield(ipv6_hdr(skb)) & info->tos_mask) ==
0061 info->tos_value) ^ !!info->invert;
0062 }
0063
0064 static struct xt_match dscp_mt_reg[] __read_mostly = {
0065 {
0066 .name = "dscp",
0067 .family = NFPROTO_IPV4,
0068 .checkentry = dscp_mt_check,
0069 .match = dscp_mt,
0070 .matchsize = sizeof(struct xt_dscp_info),
0071 .me = THIS_MODULE,
0072 },
0073 {
0074 .name = "dscp",
0075 .family = NFPROTO_IPV6,
0076 .checkentry = dscp_mt_check,
0077 .match = dscp_mt6,
0078 .matchsize = sizeof(struct xt_dscp_info),
0079 .me = THIS_MODULE,
0080 },
0081 {
0082 .name = "tos",
0083 .revision = 1,
0084 .family = NFPROTO_IPV4,
0085 .match = tos_mt,
0086 .matchsize = sizeof(struct xt_tos_match_info),
0087 .me = THIS_MODULE,
0088 },
0089 {
0090 .name = "tos",
0091 .revision = 1,
0092 .family = NFPROTO_IPV6,
0093 .match = tos_mt,
0094 .matchsize = sizeof(struct xt_tos_match_info),
0095 .me = THIS_MODULE,
0096 },
0097 };
0098
0099 static int __init dscp_mt_init(void)
0100 {
0101 return xt_register_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
0102 }
0103
0104 static void __exit dscp_mt_exit(void)
0105 {
0106 xt_unregister_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
0107 }
0108
0109 module_init(dscp_mt_init);
0110 module_exit(dscp_mt_exit);