1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Kernel module to match TCP MSS values. */
3
4 /* Copyright (C) 2000 Marc Boucher <marc@mbsi.ca>
5 * Portions (C) 2005 by Harald Welte <laforge@netfilter.org>
6 */
7
8 #include <linux/module.h>
9 #include <linux/skbuff.h>
10 #include <net/tcp.h>
11
12 #include <linux/netfilter/xt_tcpmss.h>
13 #include <linux/netfilter/x_tables.h>
14
15 #include <linux/netfilter_ipv4/ip_tables.h>
16 #include <linux/netfilter_ipv6/ip6_tables.h>
17
18 MODULE_LICENSE("GPL");
19 MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
20 MODULE_DESCRIPTION("Xtables: TCP MSS match");
21 MODULE_ALIAS("ipt_tcpmss");
22 MODULE_ALIAS("ip6t_tcpmss");
23
24 static bool
tcpmss_mt(const struct sk_buff * skb,struct xt_action_param * par)25 tcpmss_mt(const struct sk_buff *skb, struct xt_action_param *par)
26 {
27 const struct xt_tcpmss_match_info *info = par->matchinfo;
28 const struct tcphdr *th;
29 struct tcphdr _tcph;
30 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
31 const u_int8_t *op;
32 u8 _opt[15 * 4 - sizeof(_tcph)];
33 unsigned int i, optlen;
34
35 /* this is fine for IPv6 as xt_tcpmss enforces -p tcp */
36 if (par->fragoff)
37 return false;
38
39 /* If we don't have the whole header, drop packet. */
40 th = skb_header_pointer(skb, par->thoff, sizeof(_tcph), &_tcph);
41 if (th == NULL)
42 goto dropit;
43
44 /* Malformed. */
45 if (th->doff*4 < sizeof(*th))
46 goto dropit;
47
48 optlen = th->doff*4 - sizeof(*th);
49 if (!optlen)
50 goto out;
51
52 /* Truncated options. */
53 op = skb_header_pointer(skb, par->thoff + sizeof(*th), optlen, _opt);
54 if (op == NULL)
55 goto dropit;
56
57 for (i = 0; i < optlen; ) {
58 if (op[i] == TCPOPT_MSS
59 && (optlen - i) >= TCPOLEN_MSS
60 && op[i+1] == TCPOLEN_MSS) {
61 u_int16_t mssval;
62
63 mssval = (op[i+2] << 8) | op[i+3];
64
65 return (mssval >= info->mss_min &&
66 mssval <= info->mss_max) ^ info->invert;
67 }
68 if (op[i] < 2 || i == optlen - 1)
69 i++;
70 else
71 i += op[i+1] ? : 1;
72 }
73 out:
74 return info->invert;
75
76 dropit:
77 par->hotdrop = true;
78 return false;
79 }
80
tcpmss_mt_check(const struct xt_mtchk_param * par)81 static int tcpmss_mt_check(const struct xt_mtchk_param *par)
82 {
83 const struct xt_tcpmss_match_info *info = par->matchinfo;
84
85 if (info->mss_min > info->mss_max)
86 return -EINVAL;
87 if (info->invert > 1)
88 return -EINVAL;
89
90 return 0;
91 }
92
93 static struct xt_match tcpmss_mt_reg[] __read_mostly = {
94 {
95 .name = "tcpmss",
96 .family = NFPROTO_IPV4,
97 .checkentry = tcpmss_mt_check,
98 .match = tcpmss_mt,
99 .matchsize = sizeof(struct xt_tcpmss_match_info),
100 .proto = IPPROTO_TCP,
101 .me = THIS_MODULE,
102 },
103 {
104 .name = "tcpmss",
105 .family = NFPROTO_IPV6,
106 .checkentry = tcpmss_mt_check,
107 .match = tcpmss_mt,
108 .matchsize = sizeof(struct xt_tcpmss_match_info),
109 .proto = IPPROTO_TCP,
110 .me = THIS_MODULE,
111 },
112 };
113
tcpmss_mt_init(void)114 static int __init tcpmss_mt_init(void)
115 {
116 return xt_register_matches(tcpmss_mt_reg, ARRAY_SIZE(tcpmss_mt_reg));
117 }
118
tcpmss_mt_exit(void)119 static void __exit tcpmss_mt_exit(void)
120 {
121 xt_unregister_matches(tcpmss_mt_reg, ARRAY_SIZE(tcpmss_mt_reg));
122 }
123
124 module_init(tcpmss_mt_init);
125 module_exit(tcpmss_mt_exit);
126