Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 1 | /* IP tables module for matching the value of the IPv4/IPv6 DSCP field |
| 2 | * |
Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 3 | * (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 Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 16 | #include <linux/netfilter/x_tables.h> |
Jan Engelhardt | c3b33e6 | 2007-12-04 23:37:54 -0800 | [diff] [blame] | 17 | #include <linux/netfilter/xt_dscp.h> |
| 18 | #include <linux/netfilter_ipv4/ipt_tos.h> |
Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 19 | |
| 20 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 21 | MODULE_DESCRIPTION("Xtables: DSCP/TOS field match"); |
Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 22 | MODULE_LICENSE("GPL"); |
| 23 | MODULE_ALIAS("ipt_dscp"); |
| 24 | MODULE_ALIAS("ip6t_dscp"); |
Jan Engelhardt | c3b33e6 | 2007-12-04 23:37:54 -0800 | [diff] [blame] | 25 | MODULE_ALIAS("ipt_tos"); |
Jan Engelhardt | f1095ab | 2007-12-04 23:38:30 -0800 | [diff] [blame] | 26 | MODULE_ALIAS("ip6t_tos"); |
Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 27 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 28 | static bool |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 29 | dscp_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 30 | { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 31 | const struct xt_dscp_info *info = par->matchinfo; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 32 | u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT; |
| 33 | |
| 34 | return (dscp == info->dscp) ^ !!info->invert; |
| 35 | } |
| 36 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 37 | static bool |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 38 | dscp_mt6(const struct sk_buff *skb, const struct xt_match_param *par) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 39 | { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 40 | const struct xt_dscp_info *info = par->matchinfo; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 41 | u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT; |
Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 42 | |
| 43 | return (dscp == info->dscp) ^ !!info->invert; |
| 44 | } |
| 45 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 46 | static bool dscp_mt_check(const struct xt_mtchk_param *par) |
Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 47 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 48 | const struct xt_dscp_info *info = par->matchinfo; |
Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 49 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 50 | if (info->dscp > XT_DSCP_MAX) { |
| 51 | printk(KERN_ERR "xt_dscp: dscp %x out of range\n", info->dscp); |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 52 | return false; |
Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 55 | return true; |
Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 58 | static bool |
| 59 | tos_mt_v0(const struct sk_buff *skb, const struct xt_match_param *par) |
Jan Engelhardt | c3b33e6 | 2007-12-04 23:37:54 -0800 | [diff] [blame] | 60 | { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 61 | const struct ipt_tos_info *info = par->matchinfo; |
Jan Engelhardt | c3b33e6 | 2007-12-04 23:37:54 -0800 | [diff] [blame] | 62 | |
| 63 | return (ip_hdr(skb)->tos == info->tos) ^ info->invert; |
| 64 | } |
| 65 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 66 | static bool tos_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
Jan Engelhardt | f1095ab | 2007-12-04 23:38:30 -0800 | [diff] [blame] | 67 | { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 68 | const struct xt_tos_match_info *info = par->matchinfo; |
Jan Engelhardt | f1095ab | 2007-12-04 23:38:30 -0800 | [diff] [blame] | 69 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 70 | if (par->match->family == NFPROTO_IPV4) |
Jan Engelhardt | f1095ab | 2007-12-04 23:38:30 -0800 | [diff] [blame] | 71 | 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 Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 78 | static struct xt_match dscp_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 79 | { |
| 80 | .name = "dscp", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 81 | .family = NFPROTO_IPV4, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 82 | .checkentry = dscp_mt_check, |
| 83 | .match = dscp_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 84 | .matchsize = sizeof(struct xt_dscp_info), |
| 85 | .me = THIS_MODULE, |
| 86 | }, |
| 87 | { |
| 88 | .name = "dscp", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 89 | .family = NFPROTO_IPV6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 90 | .checkentry = dscp_mt_check, |
| 91 | .match = dscp_mt6, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 92 | .matchsize = sizeof(struct xt_dscp_info), |
| 93 | .me = THIS_MODULE, |
| 94 | }, |
Jan Engelhardt | c3b33e6 | 2007-12-04 23:37:54 -0800 | [diff] [blame] | 95 | { |
| 96 | .name = "tos", |
| 97 | .revision = 0, |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 98 | .family = NFPROTO_IPV4, |
Jan Engelhardt | c3b33e6 | 2007-12-04 23:37:54 -0800 | [diff] [blame] | 99 | .match = tos_mt_v0, |
| 100 | .matchsize = sizeof(struct ipt_tos_info), |
| 101 | .me = THIS_MODULE, |
| 102 | }, |
Jan Engelhardt | f1095ab | 2007-12-04 23:38:30 -0800 | [diff] [blame] | 103 | { |
| 104 | .name = "tos", |
| 105 | .revision = 1, |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 106 | .family = NFPROTO_IPV4, |
Jan Engelhardt | f1095ab | 2007-12-04 23:38:30 -0800 | [diff] [blame] | 107 | .match = tos_mt, |
| 108 | .matchsize = sizeof(struct xt_tos_match_info), |
| 109 | .me = THIS_MODULE, |
| 110 | }, |
| 111 | { |
| 112 | .name = "tos", |
| 113 | .revision = 1, |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 114 | .family = NFPROTO_IPV6, |
Jan Engelhardt | f1095ab | 2007-12-04 23:38:30 -0800 | [diff] [blame] | 115 | .match = tos_mt, |
| 116 | .matchsize = sizeof(struct xt_tos_match_info), |
| 117 | .me = THIS_MODULE, |
| 118 | }, |
Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 121 | static int __init dscp_mt_init(void) |
Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 122 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 123 | return xt_register_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg)); |
Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 126 | static void __exit dscp_mt_exit(void) |
Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 127 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 128 | xt_unregister_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg)); |
Yasuyuki Kozakai | 9ba1627 | 2006-08-22 00:29:37 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 131 | module_init(dscp_mt_init); |
| 132 | module_exit(dscp_mt_exit); |