2 * A module for stripping a specific TCP option from TCP packets.
4 * Copyright (C) 2007 Sven Schnelle <svens@bitebene.org>
5 * Copyright © CC Computer Consultants GmbH, 2007
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.
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
15 #include <linux/ipv6.h>
16 #include <linux/tcp.h>
19 #include <linux/netfilter/x_tables.h>
20 #include <linux/netfilter/xt_TCPOPTSTRIP.h>
22 static inline unsigned int optlen(const u_int8_t
*opt
, unsigned int offset
)
24 /* Beware zero-length options: make finite progress */
25 if (opt
[offset
] <= TCPOPT_NOP
|| opt
[offset
+1] == 0)
32 tcpoptstrip_mangle_packet(struct sk_buff
*skb
,
33 const struct xt_action_param
*par
,
34 unsigned int tcphoff
, unsigned int minlen
)
36 const struct xt_tcpoptstrip_target_info
*info
= par
->targinfo
;
37 unsigned int optl
, i
, j
;
43 /* This is a fragment, no TCP header is available */
44 if (par
->fragoff
!= 0)
47 if (!skb_make_writable(skb
, skb
->len
))
50 len
= skb
->len
- tcphoff
;
51 if (len
< (int)sizeof(struct tcphdr
))
54 tcph
= (struct tcphdr
*)(skb_network_header(skb
) + tcphoff
);
55 if (tcph
->doff
* 4 > len
)
58 opt
= (u_int8_t
*)tcph
;
61 * Walk through all TCP options - if we find some option to remove,
62 * set all octets to %TCPOPT_NOP and adjust checksum.
64 for (i
= sizeof(struct tcphdr
); i
< tcp_hdrlen(skb
); i
+= optl
) {
65 optl
= optlen(opt
, i
);
67 if (i
+ optl
> tcp_hdrlen(skb
))
70 if (!tcpoptstrip_test_bit(info
->strip_bmap
, opt
[i
]))
73 for (j
= 0; j
< optl
; ++j
) {
76 if ((i
+ j
) % 2 == 0) {
80 inet_proto_csum_replace2(&tcph
->check
, skb
, htons(o
),
83 memset(opt
+ i
, TCPOPT_NOP
, optl
);
90 tcpoptstrip_tg4(struct sk_buff
*skb
, const struct xt_action_param
*par
)
92 return tcpoptstrip_mangle_packet(skb
, par
, ip_hdrlen(skb
),
93 sizeof(struct iphdr
) + sizeof(struct tcphdr
));
96 #if IS_ENABLED(CONFIG_IP6_NF_MANGLE)
98 tcpoptstrip_tg6(struct sk_buff
*skb
, const struct xt_action_param
*par
)
100 struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
105 nexthdr
= ipv6h
->nexthdr
;
106 tcphoff
= ipv6_skip_exthdr(skb
, sizeof(*ipv6h
), &nexthdr
, &frag_off
);
110 return tcpoptstrip_mangle_packet(skb
, par
, tcphoff
,
111 sizeof(*ipv6h
) + sizeof(struct tcphdr
));
115 static struct xt_target tcpoptstrip_tg_reg
[] __read_mostly
= {
117 .name
= "TCPOPTSTRIP",
118 .family
= NFPROTO_IPV4
,
120 .proto
= IPPROTO_TCP
,
121 .target
= tcpoptstrip_tg4
,
122 .targetsize
= sizeof(struct xt_tcpoptstrip_target_info
),
125 #if IS_ENABLED(CONFIG_IP6_NF_MANGLE)
127 .name
= "TCPOPTSTRIP",
128 .family
= NFPROTO_IPV6
,
130 .proto
= IPPROTO_TCP
,
131 .target
= tcpoptstrip_tg6
,
132 .targetsize
= sizeof(struct xt_tcpoptstrip_target_info
),
138 static int __init
tcpoptstrip_tg_init(void)
140 return xt_register_targets(tcpoptstrip_tg_reg
,
141 ARRAY_SIZE(tcpoptstrip_tg_reg
));
144 static void __exit
tcpoptstrip_tg_exit(void)
146 xt_unregister_targets(tcpoptstrip_tg_reg
,
147 ARRAY_SIZE(tcpoptstrip_tg_reg
));
150 module_init(tcpoptstrip_tg_init
);
151 module_exit(tcpoptstrip_tg_exit
);
152 MODULE_AUTHOR("Sven Schnelle <svens@bitebene.org>, Jan Engelhardt <jengelh@medozas.de>");
153 MODULE_DESCRIPTION("Xtables: TCP option stripping");
154 MODULE_LICENSE("GPL");
155 MODULE_ALIAS("ipt_TCPOPTSTRIP");
156 MODULE_ALIAS("ip6t_TCPOPTSTRIP");