netfilter: xt_TCPMSS: fix handling of malformed TCP header and options
[deliverable/linux.git] / net / netfilter / xt_TCPOPTSTRIP.c
CommitLineData
338e8a79
SS
1/*
2 * A module for stripping a specific TCP option from TCP packets.
3 *
4 * Copyright (C) 2007 Sven Schnelle <svens@bitebene.org>
5 * Copyright © CC Computer Consultants GmbH, 2007
338e8a79
SS
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/module.h>
13#include <linux/skbuff.h>
14#include <linux/ip.h>
15#include <linux/ipv6.h>
16#include <linux/tcp.h>
17#include <net/ipv6.h>
18#include <net/tcp.h>
19#include <linux/netfilter/x_tables.h>
20#include <linux/netfilter/xt_TCPOPTSTRIP.h>
21
22static inline unsigned int optlen(const u_int8_t *opt, unsigned int offset)
23{
24 /* Beware zero-length options: make finite progress */
25 if (opt[offset] <= TCPOPT_NOP || opt[offset+1] == 0)
26 return 1;
27 else
28 return opt[offset+1];
29}
30
31static unsigned int
32tcpoptstrip_mangle_packet(struct sk_buff *skb,
bc6bcb59 33 const struct xt_action_param *par,
338e8a79
SS
34 unsigned int tcphoff, unsigned int minlen)
35{
bc6bcb59 36 const struct xt_tcpoptstrip_target_info *info = par->targinfo;
338e8a79
SS
37 unsigned int optl, i, j;
38 struct tcphdr *tcph;
39 u_int16_t n, o;
40 u_int8_t *opt;
bc6bcb59
PNA
41 int len;
42
43 /* This is a fragment, no TCP header is available */
44 if (par->fragoff != 0)
45 return XT_CONTINUE;
338e8a79
SS
46
47 if (!skb_make_writable(skb, skb->len))
48 return NF_DROP;
49
bc6bcb59 50 len = skb->len - tcphoff;
ed82c437 51 if (len < (int)sizeof(struct tcphdr))
bc6bcb59
PNA
52 return NF_DROP;
53
338e8a79 54 tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
ed82c437
PNA
55 if (tcph->doff * 4 > len)
56 return NF_DROP;
57
338e8a79
SS
58 opt = (u_int8_t *)tcph;
59
60 /*
61 * Walk through all TCP options - if we find some option to remove,
62 * set all octets to %TCPOPT_NOP and adjust checksum.
63 */
64 for (i = sizeof(struct tcphdr); i < tcp_hdrlen(skb); i += optl) {
65 optl = optlen(opt, i);
66
67 if (i + optl > tcp_hdrlen(skb))
68 break;
69
70 if (!tcpoptstrip_test_bit(info->strip_bmap, opt[i]))
71 continue;
72
73 for (j = 0; j < optl; ++j) {
74 o = opt[i+j];
75 n = TCPOPT_NOP;
76 if ((i + j) % 2 == 0) {
77 o <<= 8;
78 n <<= 8;
79 }
80 inet_proto_csum_replace2(&tcph->check, skb, htons(o),
81 htons(n), 0);
82 }
83 memset(opt + i, TCPOPT_NOP, optl);
84 }
85
86 return XT_CONTINUE;
87}
88
89static unsigned int
4b560b44 90tcpoptstrip_tg4(struct sk_buff *skb, const struct xt_action_param *par)
338e8a79 91{
bc6bcb59 92 return tcpoptstrip_mangle_packet(skb, par, ip_hdrlen(skb),
338e8a79
SS
93 sizeof(struct iphdr) + sizeof(struct tcphdr));
94}
95
c0cd1156 96#if IS_ENABLED(CONFIG_IP6_NF_MANGLE)
338e8a79 97static unsigned int
4b560b44 98tcpoptstrip_tg6(struct sk_buff *skb, const struct xt_action_param *par)
338e8a79
SS
99{
100 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
be8d0d79 101 int tcphoff;
338e8a79 102 u_int8_t nexthdr;
75f2811c 103 __be16 frag_off;
338e8a79
SS
104
105 nexthdr = ipv6h->nexthdr;
75f2811c 106 tcphoff = ipv6_skip_exthdr(skb, sizeof(*ipv6h), &nexthdr, &frag_off);
338e8a79
SS
107 if (tcphoff < 0)
108 return NF_DROP;
109
bc6bcb59 110 return tcpoptstrip_mangle_packet(skb, par, tcphoff,
338e8a79
SS
111 sizeof(*ipv6h) + sizeof(struct tcphdr));
112}
113#endif
114
115static struct xt_target tcpoptstrip_tg_reg[] __read_mostly = {
116 {
117 .name = "TCPOPTSTRIP",
ee999d8b 118 .family = NFPROTO_IPV4,
338e8a79
SS
119 .table = "mangle",
120 .proto = IPPROTO_TCP,
121 .target = tcpoptstrip_tg4,
122 .targetsize = sizeof(struct xt_tcpoptstrip_target_info),
123 .me = THIS_MODULE,
124 },
c0cd1156 125#if IS_ENABLED(CONFIG_IP6_NF_MANGLE)
338e8a79
SS
126 {
127 .name = "TCPOPTSTRIP",
ee999d8b 128 .family = NFPROTO_IPV6,
338e8a79
SS
129 .table = "mangle",
130 .proto = IPPROTO_TCP,
131 .target = tcpoptstrip_tg6,
132 .targetsize = sizeof(struct xt_tcpoptstrip_target_info),
133 .me = THIS_MODULE,
134 },
135#endif
136};
137
138static int __init tcpoptstrip_tg_init(void)
139{
140 return xt_register_targets(tcpoptstrip_tg_reg,
141 ARRAY_SIZE(tcpoptstrip_tg_reg));
142}
143
144static void __exit tcpoptstrip_tg_exit(void)
145{
146 xt_unregister_targets(tcpoptstrip_tg_reg,
147 ARRAY_SIZE(tcpoptstrip_tg_reg));
148}
149
150module_init(tcpoptstrip_tg_init);
151module_exit(tcpoptstrip_tg_exit);
408ffaa4 152MODULE_AUTHOR("Sven Schnelle <svens@bitebene.org>, Jan Engelhardt <jengelh@medozas.de>");
2ae15b64 153MODULE_DESCRIPTION("Xtables: TCP option stripping");
338e8a79
SS
154MODULE_LICENSE("GPL");
155MODULE_ALIAS("ipt_TCPOPTSTRIP");
156MODULE_ALIAS("ip6t_TCPOPTSTRIP");
This page took 0.368205 seconds and 5 git commands to generate.