Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1 | /* Kernel module to match TCP MSS values. */ |
| 2 | |
| 3 | /* Copyright (C) 2000 Marc Boucher <marc@mbsi.ca> |
| 4 | * Portions (C) 2005 by Harald Welte <laforge@netfilter.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/skbuff.h> |
| 13 | #include <net/tcp.h> |
| 14 | |
| 15 | #include <linux/netfilter/xt_tcpmss.h> |
| 16 | #include <linux/netfilter/x_tables.h> |
| 17 | |
| 18 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 19 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 20 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 21 | MODULE_LICENSE("GPL"); |
| 22 | MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 23 | MODULE_DESCRIPTION("Xtables: TCP MSS match"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 24 | MODULE_ALIAS("ipt_tcpmss"); |
Jan Engelhardt | 73aaf93 | 2007-10-11 14:36:40 -0700 | [diff] [blame] | 25 | MODULE_ALIAS("ip6t_tcpmss"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 26 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 27 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 28 | tcpmss_mt(const struct sk_buff *skb, struct xt_action_param *par) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 29 | { |
Jan Engelhardt | f7108a20 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 30 | const struct xt_tcpmss_match_info *info = par->matchinfo; |
Jan Engelhardt | 3cf93c9 | 2008-04-14 09:56:05 +0200 | [diff] [blame] | 31 | const struct tcphdr *th; |
| 32 | struct tcphdr _tcph; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 33 | /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */ |
Jan Engelhardt | 3cf93c9 | 2008-04-14 09:56:05 +0200 | [diff] [blame] | 34 | const u_int8_t *op; |
| 35 | u8 _opt[15 * 4 - sizeof(_tcph)]; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 36 | unsigned int i, optlen; |
| 37 | |
| 38 | /* If we don't have the whole header, drop packet. */ |
Jan Engelhardt | f7108a20 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 39 | th = skb_header_pointer(skb, par->thoff, sizeof(_tcph), &_tcph); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 40 | if (th == NULL) |
| 41 | goto dropit; |
| 42 | |
| 43 | /* Malformed. */ |
| 44 | if (th->doff*4 < sizeof(*th)) |
| 45 | goto dropit; |
| 46 | |
| 47 | optlen = th->doff*4 - sizeof(*th); |
| 48 | if (!optlen) |
| 49 | goto out; |
| 50 | |
| 51 | /* Truncated options. */ |
Jan Engelhardt | f7108a20 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 52 | op = skb_header_pointer(skb, par->thoff + sizeof(*th), optlen, _opt); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 53 | if (op == NULL) |
| 54 | goto dropit; |
| 55 | |
| 56 | for (i = 0; i < optlen; ) { |
| 57 | if (op[i] == TCPOPT_MSS |
| 58 | && (optlen - i) >= TCPOLEN_MSS |
| 59 | && op[i+1] == TCPOLEN_MSS) { |
| 60 | u_int16_t mssval; |
| 61 | |
| 62 | mssval = (op[i+2] << 8) | op[i+3]; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 63 | |
Patrick McHardy | ce556b3 | 2006-08-22 00:44:14 -0700 | [diff] [blame] | 64 | return (mssval >= info->mss_min && |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 65 | mssval <= info->mss_max) ^ info->invert; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 66 | } |
Patrick McHardy | ce556b3 | 2006-08-22 00:44:14 -0700 | [diff] [blame] | 67 | if (op[i] < 2) |
| 68 | i++; |
| 69 | else |
| 70 | i += op[i+1] ? : 1; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 71 | } |
| 72 | out: |
Patrick McHardy | ce556b3 | 2006-08-22 00:44:14 -0700 | [diff] [blame] | 73 | return info->invert; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 74 | |
Patrick McHardy | ce556b3 | 2006-08-22 00:44:14 -0700 | [diff] [blame] | 75 | dropit: |
Jan Engelhardt | b4ba261 | 2009-07-07 20:54:30 +0200 | [diff] [blame] | 76 | par->hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 77 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 80 | static struct xt_match tcpmss_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 81 | { |
| 82 | .name = "tcpmss", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 83 | .family = NFPROTO_IPV4, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 84 | .match = tcpmss_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 85 | .matchsize = sizeof(struct xt_tcpmss_match_info), |
| 86 | .proto = IPPROTO_TCP, |
| 87 | .me = THIS_MODULE, |
| 88 | }, |
| 89 | { |
| 90 | .name = "tcpmss", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 91 | .family = NFPROTO_IPV6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 92 | .match = tcpmss_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 93 | .matchsize = sizeof(struct xt_tcpmss_match_info), |
| 94 | .proto = IPPROTO_TCP, |
| 95 | .me = THIS_MODULE, |
| 96 | }, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 99 | static int __init tcpmss_mt_init(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 100 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 101 | return xt_register_matches(tcpmss_mt_reg, ARRAY_SIZE(tcpmss_mt_reg)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 104 | static void __exit tcpmss_mt_exit(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 105 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 106 | xt_unregister_matches(tcpmss_mt_reg, ARRAY_SIZE(tcpmss_mt_reg)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 109 | module_init(tcpmss_mt_init); |
| 110 | module_exit(tcpmss_mt_exit); |