blob: e661108c73f18de89abdd07073fe5f18a55eb72c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* IP tables module for matching the value of the IPv4 and TCP ECN bits
2 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * (C) 2002 by Harald Welte <laforge@gnumonks.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 Engelhardtff67e4e2010-03-19 21:08:16 +01009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080010#include <linux/in.h>
11#include <linux/ip.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030012#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/skbuff.h>
15#include <linux/tcp.h>
16
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080017#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/netfilter_ipv4/ip_tables.h>
19#include <linux/netfilter_ipv4/ipt_ecn.h>
20
21MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080022MODULE_DESCRIPTION("Xtables: Explicit Congestion Notification (ECN) flag match for IPv4");
Linus Torvalds1da177e2005-04-16 15:20:36 -070023MODULE_LICENSE("GPL");
24
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070025static inline bool match_ip(const struct sk_buff *skb,
26 const struct ipt_ecn_info *einfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070028 return (ip_hdr(skb)->tos & IPT_ECN_IP_MASK) == einfo->ip_ect;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029}
30
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070031static inline bool match_tcp(const struct sk_buff *skb,
32 const struct ipt_ecn_info *einfo,
33 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Jan Engelhardta47362a2007-07-07 22:16:55 -070035 struct tcphdr _tcph;
36 const struct tcphdr *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 /* In practice, TCP match does this, so can't fail. But let's
39 * be good citizens.
40 */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030041 th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 if (th == NULL) {
Jan Engelhardtcff533a2007-07-07 22:15:12 -070043 *hotdrop = false;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070044 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 }
46
47 if (einfo->operation & IPT_ECN_OP_MATCH_ECE) {
48 if (einfo->invert & IPT_ECN_OP_MATCH_ECE) {
49 if (th->ece == 1)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070050 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 } else {
52 if (th->ece == 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070053 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 }
55 }
56
57 if (einfo->operation & IPT_ECN_OP_MATCH_CWR) {
58 if (einfo->invert & IPT_ECN_OP_MATCH_CWR) {
59 if (th->cwr == 1)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070060 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 } else {
62 if (th->cwr == 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070063 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
65 }
66
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070067 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Jan Engelhardtf7108a22008-10-08 11:35:18 +020070static bool ecn_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020072 const struct ipt_ecn_info *info = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 if (info->operation & IPT_ECN_OP_MATCH_IP)
75 if (!match_ip(skb, info))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070076 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070079 if (ip_hdr(skb)->protocol != IPPROTO_TCP)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070080 return false;
Jan Engelhardtf7108a22008-10-08 11:35:18 +020081 if (!match_tcp(skb, info, par->hotdrop))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070082 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070085 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020088static bool ecn_mt_check(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020090 const struct ipt_ecn_info *info = par->matchinfo;
91 const struct ipt_ip *ip = par->entryinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (info->operation & IPT_ECN_OP_MATCH_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070094 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 if (info->invert & IPT_ECN_OP_MATCH_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070097 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Joe Perches3666ed12009-11-23 23:17:06 +010099 if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR) &&
100 ip->proto != IPPROTO_TCP) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100101 pr_info("cannot match TCP bits in rule for non-tcp packets\n");
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700102 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700105 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800108static struct xt_match ecn_mt_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .name = "ecn",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200110 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800111 .match = ecn_mt,
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800112 .matchsize = sizeof(struct ipt_ecn_info),
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800113 .checkentry = ecn_mt_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .me = THIS_MODULE,
115};
116
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800117static int __init ecn_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800119 return xt_register_match(&ecn_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800122static void __exit ecn_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800124 xt_unregister_match(&ecn_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800127module_init(ecn_mt_init);
128module_exit(ecn_mt_exit);