blob: 1eb1a44bfd3d134452993dfb071aa15481857ad7 [file] [log] [blame]
Sven Schnelle338e8a72007-12-04 23:21:50 -08001/*
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
Sven Schnelle338e8a72007-12-04 23:21:50 -08006 *
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,
Pablo Neira Ayusobc6bcb52013-05-07 03:22:18 +020033 const struct xt_action_param *par,
Sven Schnelle338e8a72007-12-04 23:21:50 -080034 unsigned int tcphoff, unsigned int minlen)
35{
Pablo Neira Ayusobc6bcb52013-05-07 03:22:18 +020036 const struct xt_tcpoptstrip_target_info *info = par->targinfo;
Sven Schnelle338e8a72007-12-04 23:21:50 -080037 unsigned int optl, i, j;
38 struct tcphdr *tcph;
39 u_int16_t n, o;
40 u_int8_t *opt;
Pablo Neira Ayusobc6bcb52013-05-07 03:22:18 +020041 int len;
42
43 /* This is a fragment, no TCP header is available */
44 if (par->fragoff != 0)
45 return XT_CONTINUE;
Sven Schnelle338e8a72007-12-04 23:21:50 -080046
47 if (!skb_make_writable(skb, skb->len))
48 return NF_DROP;
49
Pablo Neira Ayusobc6bcb52013-05-07 03:22:18 +020050 len = skb->len - tcphoff;
51 if (len < (int)sizeof(struct tcphdr) ||
52 tcp_hdr(skb)->doff * 4 > len)
53 return NF_DROP;
54
Sven Schnelle338e8a72007-12-04 23:21:50 -080055 tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
56 opt = (u_int8_t *)tcph;
57
58 /*
59 * Walk through all TCP options - if we find some option to remove,
60 * set all octets to %TCPOPT_NOP and adjust checksum.
61 */
62 for (i = sizeof(struct tcphdr); i < tcp_hdrlen(skb); i += optl) {
63 optl = optlen(opt, i);
64
65 if (i + optl > tcp_hdrlen(skb))
66 break;
67
68 if (!tcpoptstrip_test_bit(info->strip_bmap, opt[i]))
69 continue;
70
71 for (j = 0; j < optl; ++j) {
72 o = opt[i+j];
73 n = TCPOPT_NOP;
74 if ((i + j) % 2 == 0) {
75 o <<= 8;
76 n <<= 8;
77 }
78 inet_proto_csum_replace2(&tcph->check, skb, htons(o),
79 htons(n), 0);
80 }
81 memset(opt + i, TCPOPT_NOP, optl);
82 }
83
84 return XT_CONTINUE;
85}
86
87static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020088tcpoptstrip_tg4(struct sk_buff *skb, const struct xt_action_param *par)
Sven Schnelle338e8a72007-12-04 23:21:50 -080089{
Pablo Neira Ayusobc6bcb52013-05-07 03:22:18 +020090 return tcpoptstrip_mangle_packet(skb, par, ip_hdrlen(skb),
Sven Schnelle338e8a72007-12-04 23:21:50 -080091 sizeof(struct iphdr) + sizeof(struct tcphdr));
92}
93
Igor Maravićc0cd1152011-12-12 02:58:24 +000094#if IS_ENABLED(CONFIG_IP6_NF_MANGLE)
Sven Schnelle338e8a72007-12-04 23:21:50 -080095static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020096tcpoptstrip_tg6(struct sk_buff *skb, const struct xt_action_param *par)
Sven Schnelle338e8a72007-12-04 23:21:50 -080097{
98 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Roel Kluinbe8d0d72008-04-29 03:15:10 -070099 int tcphoff;
Sven Schnelle338e8a72007-12-04 23:21:50 -0800100 u_int8_t nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800101 __be16 frag_off;
Sven Schnelle338e8a72007-12-04 23:21:50 -0800102
103 nexthdr = ipv6h->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800104 tcphoff = ipv6_skip_exthdr(skb, sizeof(*ipv6h), &nexthdr, &frag_off);
Sven Schnelle338e8a72007-12-04 23:21:50 -0800105 if (tcphoff < 0)
106 return NF_DROP;
107
Pablo Neira Ayusobc6bcb52013-05-07 03:22:18 +0200108 return tcpoptstrip_mangle_packet(skb, par, tcphoff,
Sven Schnelle338e8a72007-12-04 23:21:50 -0800109 sizeof(*ipv6h) + sizeof(struct tcphdr));
110}
111#endif
112
113static struct xt_target tcpoptstrip_tg_reg[] __read_mostly = {
114 {
115 .name = "TCPOPTSTRIP",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200116 .family = NFPROTO_IPV4,
Sven Schnelle338e8a72007-12-04 23:21:50 -0800117 .table = "mangle",
118 .proto = IPPROTO_TCP,
119 .target = tcpoptstrip_tg4,
120 .targetsize = sizeof(struct xt_tcpoptstrip_target_info),
121 .me = THIS_MODULE,
122 },
Igor Maravićc0cd1152011-12-12 02:58:24 +0000123#if IS_ENABLED(CONFIG_IP6_NF_MANGLE)
Sven Schnelle338e8a72007-12-04 23:21:50 -0800124 {
125 .name = "TCPOPTSTRIP",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200126 .family = NFPROTO_IPV6,
Sven Schnelle338e8a72007-12-04 23:21:50 -0800127 .table = "mangle",
128 .proto = IPPROTO_TCP,
129 .target = tcpoptstrip_tg6,
130 .targetsize = sizeof(struct xt_tcpoptstrip_target_info),
131 .me = THIS_MODULE,
132 },
133#endif
134};
135
136static int __init tcpoptstrip_tg_init(void)
137{
138 return xt_register_targets(tcpoptstrip_tg_reg,
139 ARRAY_SIZE(tcpoptstrip_tg_reg));
140}
141
142static void __exit tcpoptstrip_tg_exit(void)
143{
144 xt_unregister_targets(tcpoptstrip_tg_reg,
145 ARRAY_SIZE(tcpoptstrip_tg_reg));
146}
147
148module_init(tcpoptstrip_tg_init);
149module_exit(tcpoptstrip_tg_exit);
Jan Engelhardt408ffaa2010-02-28 23:19:52 +0100150MODULE_AUTHOR("Sven Schnelle <svens@bitebene.org>, Jan Engelhardt <jengelh@medozas.de>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -0800151MODULE_DESCRIPTION("Xtables: TCP option stripping");
Sven Schnelle338e8a72007-12-04 23:21:50 -0800152MODULE_LICENSE("GPL");
153MODULE_ALIAS("ipt_TCPOPTSTRIP");
154MODULE_ALIAS("ip6t_TCPOPTSTRIP");