ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / ipv4 / netfilter / ipt_tcpmss.c
1 /* Kernel module to match TCP MSS values. */
2
3 /* Copyright (C) 2000 Marc Boucher <marc@mbsi.ca>
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
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <net/tcp.h>
13
14 #include <linux/netfilter_ipv4/ipt_tcpmss.h>
15 #include <linux/netfilter_ipv4/ip_tables.h>
16
17 #define TH_SYN 0x02
18
19 MODULE_LICENSE("GPL");
20 MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
21 MODULE_DESCRIPTION("iptables TCP MSS match module");
22
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)
29 {
30         struct tcphdr tcph;
31         /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
32         u8 opt[15 * 4 - sizeof(tcph)];
33         unsigned int i, optlen;
34
35         /* If we don't have the whole header, drop packet. */
36         if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
37                 goto dropit;
38
39         /* Malformed. */
40         if (tcph.doff*4 < sizeof(tcph))
41                 goto dropit;
42
43         optlen = tcph.doff*4 - sizeof(tcph);
44         /* Truncated options. */
45         if (skb_copy_bits(skb, skb->nh.iph->ihl*4+sizeof(tcph), opt, optlen)<0)
46                 goto dropit;
47
48         for (i = 0; i < optlen; ) {
49                 if (opt[i] == TCPOPT_MSS
50                     && (optlen - i) >= TCPOLEN_MSS
51                     && opt[i+1] == TCPOLEN_MSS) {
52                         u_int16_t mssval;
53
54                         mssval = (opt[i+2] << 8) | opt[i+3];
55                         
56                         return (mssval >= min && mssval <= max) ^ invert;
57                 }
58                 if (opt[i] < 2) i++;
59                 else i += opt[i+1]?:1;
60         }
61         return invert;
62
63  dropit:
64         *hotdrop = 1;
65         return 0;
66 }
67
68 static int
69 match(const struct sk_buff *skb,
70       const struct net_device *in,
71       const struct net_device *out,
72       const void *matchinfo,
73       int offset,
74       int *hotdrop)
75 {
76         const struct ipt_tcpmss_match_info *info = matchinfo;
77
78         return mssoption_match(info->mss_min, info->mss_max, skb,
79                                info->invert, hotdrop);
80 }
81
82 static inline int find_syn_match(const struct ipt_entry_match *m)
83 {
84         const struct ipt_tcp *tcpinfo = (const struct ipt_tcp *)m->data;
85
86         if (strcmp(m->u.kernel.match->name, "tcp") == 0
87             && (tcpinfo->flg_cmp & TH_SYN)
88             && !(tcpinfo->invflags & IPT_TCP_INV_FLAGS))
89                 return 1;
90
91         return 0;
92 }
93
94 static int
95 checkentry(const char *tablename,
96            const struct ipt_ip *ip,
97            void *matchinfo,
98            unsigned int matchsize,
99            unsigned int hook_mask)
100 {
101         if (matchsize != IPT_ALIGN(sizeof(struct ipt_tcpmss_match_info)))
102                 return 0;
103
104         /* Must specify -p tcp */
105         if (ip->proto != IPPROTO_TCP || (ip->invflags & IPT_INV_PROTO)) {
106                 printk("tcpmss: Only works on TCP packets\n");
107                 return 0;
108         }
109
110         return 1;
111 }
112
113 static struct ipt_match tcpmss_match = {
114         .name           = "tcpmss",
115         .match          = &match,
116         .checkentry     = &checkentry,
117         .me             = THIS_MODULE,
118 };
119
120 static int __init init(void)
121 {
122         return ipt_register_match(&tcpmss_match);
123 }
124
125 static void __exit fini(void)
126 {
127         ipt_unregister_match(&tcpmss_match);
128 }
129
130 module_init(init);
131 module_exit(fini);