Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv4 / netfilter / ipt_tcpmss.c
blob4dc9b16ab4a31c91c26b0c2092fc902b286155d9
1 /* Kernel module to match TCP MSS values. */
3 /* Copyright (C) 2000 Marc Boucher <marc@mbsi.ca>
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 */
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <net/tcp.h>
14 #include <linux/netfilter_ipv4/ipt_tcpmss.h>
15 #include <linux/netfilter_ipv4/ip_tables.h>
17 #define TH_SYN 0x02
19 MODULE_LICENSE("GPL");
20 MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
21 MODULE_DESCRIPTION("iptables TCP MSS match module");
23 /* Returns 1 if the mss option is set and matched by the range, 0 otherwise */
24 static inline int
25 mssoption_match(u_int16_t min, u_int16_t max,
26 const struct sk_buff *skb,
27 int invert,
28 int *hotdrop)
30 struct tcphdr _tcph, *th;
31 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
32 u8 _opt[15 * 4 - sizeof(_tcph)], *op;
33 unsigned int i, optlen;
35 /* If we don't have the whole header, drop packet. */
36 th = skb_header_pointer(skb, skb->nh.iph->ihl * 4,
37 sizeof(_tcph), &_tcph);
38 if (th == NULL)
39 goto dropit;
41 /* Malformed. */
42 if (th->doff*4 < sizeof(*th))
43 goto dropit;
45 optlen = th->doff*4 - sizeof(*th);
46 if (!optlen)
47 goto out;
49 /* Truncated options. */
50 op = skb_header_pointer(skb, skb->nh.iph->ihl * 4 + sizeof(*th),
51 optlen, _opt);
52 if (op == NULL)
53 goto dropit;
55 for (i = 0; i < optlen; ) {
56 if (op[i] == TCPOPT_MSS
57 && (optlen - i) >= TCPOLEN_MSS
58 && op[i+1] == TCPOLEN_MSS) {
59 u_int16_t mssval;
61 mssval = (op[i+2] << 8) | op[i+3];
63 return (mssval >= min && mssval <= max) ^ invert;
65 if (op[i] < 2) i++;
66 else i += op[i+1]?:1;
68 out:
69 return invert;
71 dropit:
72 *hotdrop = 1;
73 return 0;
76 static int
77 match(const struct sk_buff *skb,
78 const struct net_device *in,
79 const struct net_device *out,
80 const void *matchinfo,
81 int offset,
82 int *hotdrop)
84 const struct ipt_tcpmss_match_info *info = matchinfo;
86 return mssoption_match(info->mss_min, info->mss_max, skb,
87 info->invert, hotdrop);
90 static int
91 checkentry(const char *tablename,
92 const struct ipt_ip *ip,
93 void *matchinfo,
94 unsigned int matchsize,
95 unsigned int hook_mask)
97 if (matchsize != IPT_ALIGN(sizeof(struct ipt_tcpmss_match_info)))
98 return 0;
100 /* Must specify -p tcp */
101 if (ip->proto != IPPROTO_TCP || (ip->invflags & IPT_INV_PROTO)) {
102 printk("tcpmss: Only works on TCP packets\n");
103 return 0;
106 return 1;
109 static struct ipt_match tcpmss_match = {
110 .name = "tcpmss",
111 .match = &match,
112 .checkentry = &checkentry,
113 .me = THIS_MODULE,
116 static int __init init(void)
118 return ipt_register_match(&tcpmss_match);
121 static void __exit fini(void)
123 ipt_unregister_match(&tcpmss_match);
126 module_init(init);
127 module_exit(fini);