[NETFILTER]: x_tables: pass registered match/target data to match/target functions
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / net / netfilter / xt_tcpmss.c
CommitLineData
1da177e4
LT
1/* Kernel module to match TCP MSS values. */
2
3/* Copyright (C) 2000 Marc Boucher <marc@mbsi.ca>
2e4e6a17 4 * Portions (C) 2005 by Harald Welte <laforge@netfilter.org>
1da177e4
LT
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
2e4e6a17
HW
15#include <linux/netfilter/xt_tcpmss.h>
16#include <linux/netfilter/x_tables.h>
17
1da177e4 18#include <linux/netfilter_ipv4/ip_tables.h>
2e4e6a17 19#include <linux/netfilter_ipv6/ip6_tables.h>
1da177e4
LT
20
21#define TH_SYN 0x02
22
23MODULE_LICENSE("GPL");
24MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
25MODULE_DESCRIPTION("iptables TCP MSS match module");
2e4e6a17 26MODULE_ALIAS("ipt_tcpmss");
1da177e4
LT
27
28/* Returns 1 if the mss option is set and matched by the range, 0 otherwise */
29static inline int
30mssoption_match(u_int16_t min, u_int16_t max,
31 const struct sk_buff *skb,
2e4e6a17 32 unsigned int protoff,
1da177e4
LT
33 int invert,
34 int *hotdrop)
35{
36 struct tcphdr _tcph, *th;
37 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
38 u8 _opt[15 * 4 - sizeof(_tcph)], *op;
39 unsigned int i, optlen;
40
41 /* If we don't have the whole header, drop packet. */
2e4e6a17 42 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
1da177e4
LT
43 if (th == NULL)
44 goto dropit;
45
46 /* Malformed. */
47 if (th->doff*4 < sizeof(*th))
48 goto dropit;
49
50 optlen = th->doff*4 - sizeof(*th);
51 if (!optlen)
52 goto out;
53
54 /* Truncated options. */
2e4e6a17 55 op = skb_header_pointer(skb, protoff + sizeof(*th), optlen, _opt);
1da177e4
LT
56 if (op == NULL)
57 goto dropit;
58
59 for (i = 0; i < optlen; ) {
60 if (op[i] == TCPOPT_MSS
61 && (optlen - i) >= TCPOLEN_MSS
62 && op[i+1] == TCPOLEN_MSS) {
63 u_int16_t mssval;
64
65 mssval = (op[i+2] << 8) | op[i+3];
66
67 return (mssval >= min && mssval <= max) ^ invert;
68 }
69 if (op[i] < 2) i++;
70 else i += op[i+1]?:1;
71 }
72out:
73 return invert;
74
75 dropit:
76 *hotdrop = 1;
77 return 0;
78}
79
80static int
81match(const struct sk_buff *skb,
82 const struct net_device *in,
83 const struct net_device *out,
84 const void *matchinfo,
85 int offset,
2e4e6a17 86 unsigned int protoff,
1da177e4
LT
87 int *hotdrop)
88{
2e4e6a17 89 const struct xt_tcpmss_match_info *info = matchinfo;
1da177e4 90
2e4e6a17 91 return mssoption_match(info->mss_min, info->mss_max, skb, protoff,
1da177e4
LT
92 info->invert, hotdrop);
93}
94
2e4e6a17 95static struct xt_match tcpmss_match = {
1da177e4 96 .name = "tcpmss",
5d04bff0
PM
97 .match = match,
98 .matchsize = sizeof(struct xt_tcpmss_match_info),
99 .proto = IPPROTO_TCP,
1da177e4
LT
100 .me = THIS_MODULE,
101};
102
2e4e6a17
HW
103static struct xt_match tcpmss6_match = {
104 .name = "tcpmss",
5d04bff0
PM
105 .match = match,
106 .matchsize = sizeof(struct xt_tcpmss_match_info),
107 .proto = IPPROTO_TCP,
2e4e6a17
HW
108 .me = THIS_MODULE,
109};
110
111
1da177e4
LT
112static int __init init(void)
113{
2e4e6a17
HW
114 int ret;
115 ret = xt_register_match(AF_INET, &tcpmss_match);
116 if (ret)
117 return ret;
118
119 ret = xt_register_match(AF_INET6, &tcpmss6_match);
120 if (ret)
121 xt_unregister_match(AF_INET, &tcpmss_match);
122
123 return ret;
1da177e4
LT
124}
125
126static void __exit fini(void)
127{
2e4e6a17
HW
128 xt_unregister_match(AF_INET6, &tcpmss6_match);
129 xt_unregister_match(AF_INET, &tcpmss_match);
1da177e4
LT
130}
131
132module_init(init);
133module_exit(fini);