Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* IP tables module for matching the value of the IPv4 and TCP ECN bits |
| 2 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * (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 Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 10 | #include <linux/in.h> |
| 11 | #include <linux/ip.h> |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 12 | #include <net/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/tcp.h> |
| 16 | |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 17 | #include <linux/netfilter/x_tables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 19 | #include <linux/netfilter_ipv4/ipt_ecn.h> |
| 20 | |
| 21 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame^] | 22 | MODULE_DESCRIPTION("Xtables: Explicit Congestion Notification (ECN) flag match for IPv4"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | MODULE_LICENSE("GPL"); |
| 24 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 25 | static inline bool match_ip(const struct sk_buff *skb, |
| 26 | const struct ipt_ecn_info *einfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 28 | return (ip_hdr(skb)->tos & IPT_ECN_IP_MASK) == einfo->ip_ect; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 31 | static inline bool match_tcp(const struct sk_buff *skb, |
| 32 | const struct ipt_ecn_info *einfo, |
| 33 | bool *hotdrop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 35 | struct tcphdr _tcph; |
| 36 | const struct tcphdr *th; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | /* In practice, TCP match does this, so can't fail. But let's |
| 39 | * be good citizens. |
| 40 | */ |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 41 | th = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | if (th == NULL) { |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 43 | *hotdrop = false; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 44 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } |
| 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 Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 50 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } else { |
| 52 | if (th->ece == 0) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 53 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 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 Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 60 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } else { |
| 62 | if (th->cwr == 0) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 63 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 67 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 70 | static bool |
| 71 | ecn_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
| 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 Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 79 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | if (info->operation & (IPT_ECN_OP_MATCH_ECE|IPT_ECN_OP_MATCH_CWR)) { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 82 | if (ip_hdr(skb)->protocol != IPPROTO_TCP) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 83 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | if (!match_tcp(skb, info, hotdrop)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 85 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 88 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 91 | static bool |
| 92 | ecn_mt_check(const char *tablename, const void *ip_void, |
| 93 | const struct xt_match *match, void *matchinfo, |
| 94 | unsigned int hook_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
| 96 | const struct ipt_ecn_info *info = matchinfo; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 97 | const struct ipt_ip *ip = ip_void; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | if (info->operation & IPT_ECN_OP_MATCH_MASK) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 100 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | if (info->invert & IPT_ECN_OP_MATCH_MASK) |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 103 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 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 Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 109 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 112 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 115 | static struct xt_match ecn_mt_reg __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | .name = "ecn", |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 117 | .family = AF_INET, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 118 | .match = ecn_mt, |
Patrick McHardy | 1d5cd90 | 2006-03-20 18:01:14 -0800 | [diff] [blame] | 119 | .matchsize = sizeof(struct ipt_ecn_info), |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 120 | .checkentry = ecn_mt_check, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | .me = THIS_MODULE, |
| 122 | }; |
| 123 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 124 | static int __init ecn_mt_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 126 | return xt_register_match(&ecn_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 129 | static void __exit ecn_mt_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 131 | xt_unregister_match(&ecn_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 134 | module_init(ecn_mt_init); |
| 135 | module_exit(ecn_mt_exit); |