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
6 * Contact: Jan Engelhardt <jengelh@computergmbh.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
16 #include <linux/ipv6.h>
17 #include <linux/tcp.h>
20 #include <linux/netfilter/x_tables.h>
21 #include <linux/netfilter/xt_TCPOPTSTRIP.h>
23 static inline unsigned int optlen(const u_int8_t *opt, unsigned int offset)
25 /* Beware zero-length options: make finite progress */
26 if (opt[offset] <= TCPOPT_NOP || opt[offset+1] == 0)
33 tcpoptstrip_mangle_packet(struct sk_buff *skb,
34 const struct xt_tcpoptstrip_target_info *info,
35 unsigned int tcphoff, unsigned int minlen)
37 unsigned int optl, i, j;
42 if (!skb_make_writable(skb, skb->len))
45 tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
46 opt = (u_int8_t *)tcph;
49 * Walk through all TCP options - if we find some option to remove,
50 * set all octets to %TCPOPT_NOP and adjust checksum.
52 for (i = sizeof(struct tcphdr); i < tcp_hdrlen(skb); i += optl) {
53 optl = optlen(opt, i);
55 if (i + optl > tcp_hdrlen(skb))
58 if (!tcpoptstrip_test_bit(info->strip_bmap, opt[i]))
61 for (j = 0; j < optl; ++j) {
64 if ((i + j) % 2 == 0) {
68 inet_proto_csum_replace2(&tcph->check, skb, htons(o),
71 memset(opt + i, TCPOPT_NOP, optl);
78 tcpoptstrip_tg4(struct sk_buff *skb, const struct net_device *in,
79 const struct net_device *out, unsigned int hooknum,
80 const struct xt_target *target, const void *targinfo)
82 return tcpoptstrip_mangle_packet(skb, targinfo, ip_hdrlen(skb),
83 sizeof(struct iphdr) + sizeof(struct tcphdr));
86 #if defined(CONFIG_IP6_NF_MANGLE) || defined(CONFIG_IP6_NF_MANGLE_MODULE)
88 tcpoptstrip_tg6(struct sk_buff *skb, const struct net_device *in,
89 const struct net_device *out, unsigned int hooknum,
90 const struct xt_target *target, const void *targinfo)
92 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
96 nexthdr = ipv6h->nexthdr;
97 tcphoff = ipv6_skip_exthdr(skb, sizeof(*ipv6h), &nexthdr);
101 return tcpoptstrip_mangle_packet(skb, targinfo, tcphoff,
102 sizeof(*ipv6h) + sizeof(struct tcphdr));
106 static struct xt_target tcpoptstrip_tg_reg[] __read_mostly = {
108 .name = "TCPOPTSTRIP",
111 .proto = IPPROTO_TCP,
112 .target = tcpoptstrip_tg4,
113 .targetsize = sizeof(struct xt_tcpoptstrip_target_info),
116 #if defined(CONFIG_IP6_NF_MANGLE) || defined(CONFIG_IP6_NF_MANGLE_MODULE)
118 .name = "TCPOPTSTRIP",
121 .proto = IPPROTO_TCP,
122 .target = tcpoptstrip_tg6,
123 .targetsize = sizeof(struct xt_tcpoptstrip_target_info),
129 static int __init tcpoptstrip_tg_init(void)
131 return xt_register_targets(tcpoptstrip_tg_reg,
132 ARRAY_SIZE(tcpoptstrip_tg_reg));
135 static void __exit tcpoptstrip_tg_exit(void)
137 xt_unregister_targets(tcpoptstrip_tg_reg,
138 ARRAY_SIZE(tcpoptstrip_tg_reg));
141 module_init(tcpoptstrip_tg_init);
142 module_exit(tcpoptstrip_tg_exit);
143 MODULE_AUTHOR("Sven Schnelle <svens@bitebene.org>, Jan Engelhardt <jengelh@computergmbh.de>");
144 MODULE_DESCRIPTION("netfilter \"TCPOPTSTRIP\" target module");
145 MODULE_LICENSE("GPL");
146 MODULE_ALIAS("ipt_TCPOPTSTRIP");
147 MODULE_ALIAS("ip6t_TCPOPTSTRIP");