blob: 749de8284ce53fb61cc17c56e7b584fa1cd77031 [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 */
9
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 Engelhardtd3c5ee62007-12-04 23:24:03 -080070static bool
71ecn_mt(const struct sk_buff *skb, const struct net_device *in,
72 const struct net_device *out, const struct xt_match *match,
73 const void *matchinfo, int offset, unsigned int protoff, bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 const struct ipt_ecn_info *info = matchinfo;
76
77 if (info->operation & IPT_ECN_OP_MATCH_IP)
78 if (!match_ip(skb, info))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070079 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070082 if (ip_hdr(skb)->protocol != IPPROTO_TCP)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070083 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (!match_tcp(skb, info, hotdrop))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070085 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070088 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080091static bool
92ecn_mt_check(const char *tablename, const void *ip_void,
93 const struct xt_match *match, void *matchinfo,
94 unsigned int hook_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 const struct ipt_ecn_info *info = matchinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -080097 const struct ipt_ip *ip = ip_void;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (info->operation & IPT_ECN_OP_MATCH_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700100 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 if (info->invert & IPT_ECN_OP_MATCH_MASK)
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700103 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)
106 && ip->proto != IPPROTO_TCP) {
107 printk(KERN_WARNING "ipt_ecn: can't match TCP bits in rule for"
108 " non-tcp packets\n");
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700109 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700112 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800115static struct xt_match ecn_mt_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .name = "ecn",
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800117 .family = AF_INET,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800118 .match = ecn_mt,
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800119 .matchsize = sizeof(struct ipt_ecn_info),
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800120 .checkentry = ecn_mt_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .me = THIS_MODULE,
122};
123
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800124static int __init ecn_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800126 return xt_register_match(&ecn_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800129static void __exit ecn_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800131 xt_unregister_match(&ecn_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800134module_init(ecn_mt_init);
135module_exit(ecn_mt_exit);