blob: 3c831a8efebc65840af55347c880cd188a465435 [file] [log] [blame]
Patrick McHardyaf0d29c2011-06-09 15:20:27 +02001/*
2 * Xtables module for matching the value of the IPv4/IPv6 and TCP ECN bits
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * (C) 2002 by Harald Welte <laforge@gnumonks.org>
Patrick McHardyaf0d29c2011-06-09 15:20:27 +02005 * (C) 2011 Patrick McHardy <kaber@trash.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080012#include <linux/in.h>
13#include <linux/ip.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030014#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
16#include <linux/skbuff.h>
17#include <linux/tcp.h>
18
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080019#include <linux/netfilter/x_tables.h>
Jan Engelhardta4c6f9d2011-06-09 21:15:37 +020020#include <linux/netfilter/xt_ecn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/netfilter_ipv4/ip_tables.h>
Patrick McHardyaf0d29c2011-06-09 15:20:27 +020022#include <linux/netfilter_ipv6/ip6_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Patrick McHardyaf0d29c2011-06-09 15:20:27 +020025MODULE_DESCRIPTION("Xtables: Explicit Congestion Notification (ECN) flag match");
Linus Torvalds1da177e2005-04-16 15:20:36 -070026MODULE_LICENSE("GPL");
Jan Engelhardtd446a8202011-06-09 21:03:07 +020027MODULE_ALIAS("ipt_ecn");
Patrick McHardyaf0d29c2011-06-09 15:20:27 +020028MODULE_ALIAS("ip6t_ecn");
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Patrick McHardyaf0d29c2011-06-09 15:20:27 +020030static bool match_tcp(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Patrick McHardyaf0d29c2011-06-09 15:20:27 +020032 const struct xt_ecn_info *einfo = par->matchinfo;
Jan Engelhardta47362a2007-07-07 22:16:55 -070033 struct tcphdr _tcph;
34 const struct tcphdr *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36 /* In practice, TCP match does this, so can't fail. But let's
37 * be good citizens.
38 */
Patrick McHardyaf0d29c2011-06-09 15:20:27 +020039 th = skb_header_pointer(skb, par->thoff, sizeof(_tcph), &_tcph);
Jan Engelhardt42c344a2011-06-09 22:16:50 +020040 if (th == NULL)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070041 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jan Engelhardta4c6f9d2011-06-09 21:15:37 +020043 if (einfo->operation & XT_ECN_OP_MATCH_ECE) {
44 if (einfo->invert & XT_ECN_OP_MATCH_ECE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (th->ece == 1)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070046 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 } else {
48 if (th->ece == 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070049 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 }
51 }
52
Jan Engelhardta4c6f9d2011-06-09 21:15:37 +020053 if (einfo->operation & XT_ECN_OP_MATCH_CWR) {
54 if (einfo->invert & XT_ECN_OP_MATCH_CWR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (th->cwr == 1)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070056 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 } else {
58 if (th->cwr == 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070059 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
61 }
62
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070063 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Patrick McHardyaf0d29c2011-06-09 15:20:27 +020066static inline bool match_ip(const struct sk_buff *skb,
67 const struct xt_ecn_info *einfo)
68{
69 return ((ip_hdr(skb)->tos & XT_ECN_IP_MASK) == einfo->ip_ect) ^
70 !!(einfo->invert & XT_ECN_OP_MATCH_IP);
71}
72
73static bool ecn_mt4(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Jan Engelhardta4c6f9d2011-06-09 21:15:37 +020075 const struct xt_ecn_info *info = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Jan Engelhardt42c344a2011-06-09 22:16:50 +020077 if (info->operation & XT_ECN_OP_MATCH_IP && !match_ip(skb, info))
78 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Jan Engelhardt42c344a2011-06-09 22:16:50 +020080 if (info->operation & (XT_ECN_OP_MATCH_ECE | XT_ECN_OP_MATCH_CWR) &&
81 !match_tcp(skb, par))
82 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070084 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Patrick McHardyaf0d29c2011-06-09 15:20:27 +020087static int ecn_mt_check4(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Jan Engelhardta4c6f9d2011-06-09 21:15:37 +020089 const struct xt_ecn_info *info = par->matchinfo;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020090 const struct ipt_ip *ip = par->entryinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Jan Engelhardta4c6f9d2011-06-09 21:15:37 +020092 if (info->operation & XT_ECN_OP_MATCH_MASK)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010093 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Jan Engelhardta4c6f9d2011-06-09 21:15:37 +020095 if (info->invert & XT_ECN_OP_MATCH_MASK)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010096 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jan Engelhardta4c6f9d2011-06-09 21:15:37 +020098 if (info->operation & (XT_ECN_OP_MATCH_ECE | XT_ECN_OP_MATCH_CWR) &&
Patrick McHardy58d5a022011-06-16 17:24:17 +020099 (ip->proto != IPPROTO_TCP || ip->invflags & IPT_INV_PROTO)) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100100 pr_info("cannot match TCP bits in rule for non-tcp packets\n");
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100101 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100104 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Patrick McHardyaf0d29c2011-06-09 15:20:27 +0200107static inline bool match_ipv6(const struct sk_buff *skb,
108 const struct xt_ecn_info *einfo)
109{
110 return (((ipv6_hdr(skb)->flow_lbl[0] >> 4) & XT_ECN_IP_MASK) ==
111 einfo->ip_ect) ^
112 !!(einfo->invert & XT_ECN_OP_MATCH_IP);
113}
114
115static bool ecn_mt6(const struct sk_buff *skb, struct xt_action_param *par)
116{
117 const struct xt_ecn_info *info = par->matchinfo;
118
119 if (info->operation & XT_ECN_OP_MATCH_IP && !match_ipv6(skb, info))
120 return false;
121
122 if (info->operation & (XT_ECN_OP_MATCH_ECE | XT_ECN_OP_MATCH_CWR) &&
123 !match_tcp(skb, par))
124 return false;
125
126 return true;
127}
128
129static int ecn_mt_check6(const struct xt_mtchk_param *par)
130{
131 const struct xt_ecn_info *info = par->matchinfo;
132 const struct ip6t_ip6 *ip = par->entryinfo;
133
134 if (info->operation & XT_ECN_OP_MATCH_MASK)
135 return -EINVAL;
136
137 if (info->invert & XT_ECN_OP_MATCH_MASK)
138 return -EINVAL;
139
140 if (info->operation & (XT_ECN_OP_MATCH_ECE | XT_ECN_OP_MATCH_CWR) &&
141 (ip->proto != IPPROTO_TCP || ip->invflags & IP6T_INV_PROTO)) {
142 pr_info("cannot match TCP bits in rule for non-tcp packets\n");
143 return -EINVAL;
144 }
145
146 return 0;
147}
148
149static struct xt_match ecn_mt_reg[] __read_mostly = {
150 {
151 .name = "ecn",
152 .family = NFPROTO_IPV4,
153 .match = ecn_mt4,
154 .matchsize = sizeof(struct xt_ecn_info),
155 .checkentry = ecn_mt_check4,
156 .me = THIS_MODULE,
157 },
158 {
159 .name = "ecn",
160 .family = NFPROTO_IPV6,
161 .match = ecn_mt6,
162 .matchsize = sizeof(struct xt_ecn_info),
163 .checkentry = ecn_mt_check6,
164 .me = THIS_MODULE,
165 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166};
167
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800168static int __init ecn_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Patrick McHardyaf0d29c2011-06-09 15:20:27 +0200170 return xt_register_matches(ecn_mt_reg, ARRAY_SIZE(ecn_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800173static void __exit ecn_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Patrick McHardyaf0d29c2011-06-09 15:20:27 +0200175 xt_unregister_matches(ecn_mt_reg, ARRAY_SIZE(ecn_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800178module_init(ecn_mt_init);
179module_exit(ecn_mt_exit);