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.
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
14 #include <linux/netfilter_ipv4/ipt_tcpmss.h>
15 #include <linux/netfilter_ipv4/ip_tables.h>
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 */
25 mssoption_match(u_int16_t min, u_int16_t max,
26 const struct sk_buff *skb,
31 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
32 u8 opt[15 * 4 - sizeof(tcph)];
33 unsigned int i, optlen;
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)
40 if (tcph.doff*4 < sizeof(tcph))
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)
48 for (i = 0; i < optlen; ) {
49 if (opt[i] == TCPOPT_MSS
50 && (optlen - i) >= TCPOLEN_MSS
51 && opt[i+1] == TCPOLEN_MSS) {
54 mssval = (opt[i+2] << 8) | opt[i+3];
56 return (mssval >= min && mssval <= max) ^ invert;
59 else i += opt[i+1]?:1;
69 match(const struct sk_buff *skb,
70 const struct net_device *in,
71 const struct net_device *out,
72 const void *matchinfo,
76 const struct ipt_tcpmss_match_info *info = matchinfo;
78 return mssoption_match(info->mss_min, info->mss_max, skb,
79 info->invert, hotdrop);
82 static inline int find_syn_match(const struct ipt_entry_match *m)
84 const struct ipt_tcp *tcpinfo = (const struct ipt_tcp *)m->data;
86 if (strcmp(m->u.kernel.match->name, "tcp") == 0
87 && (tcpinfo->flg_cmp & TH_SYN)
88 && !(tcpinfo->invflags & IPT_TCP_INV_FLAGS))
95 checkentry(const char *tablename,
96 const struct ipt_ip *ip,
98 unsigned int matchsize,
99 unsigned int hook_mask)
101 if (matchsize != IPT_ALIGN(sizeof(struct ipt_tcpmss_match_info)))
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");
113 static struct ipt_match tcpmss_match = {
116 .checkentry = &checkentry,
120 static int __init init(void)
122 return ipt_register_match(&tcpmss_match);
125 static void __exit fini(void)
127 ipt_unregister_match(&tcpmss_match);