Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 1 | /* ip_nat_helper.c - generic support functions for NAT helpers |
| 2 | * |
| 3 | * (C) 2000-2002 Harald Welte <laforge@netfilter.org> |
| 4 | * (C) 2003-2006 Netfilter Core Team <coreteam@netfilter.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/kmod.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/timer.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/tcp.h> |
| 16 | #include <linux/udp.h> |
| 17 | #include <net/checksum.h> |
| 18 | #include <net/tcp.h> |
KOVACS Krisztian | 1668e01 | 2008-10-01 07:33:10 -0700 | [diff] [blame] | 19 | #include <net/route.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 20 | |
| 21 | #include <linux/netfilter_ipv4.h> |
| 22 | #include <net/netfilter/nf_conntrack.h> |
| 23 | #include <net/netfilter/nf_conntrack_helper.h> |
Pablo Neira Ayuso | 13eae15 | 2007-12-17 22:28:00 -0800 | [diff] [blame] | 24 | #include <net/netfilter/nf_conntrack_ecache.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 25 | #include <net/netfilter/nf_conntrack_expect.h> |
| 26 | #include <net/netfilter/nf_nat.h> |
| 27 | #include <net/netfilter/nf_nat_protocol.h> |
| 28 | #include <net/netfilter/nf_nat_core.h> |
| 29 | #include <net/netfilter/nf_nat_helper.h> |
| 30 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 31 | #define DUMP_OFFSET(x) \ |
| 32 | pr_debug("offset_before=%d, offset_after=%d, correction_pos=%u\n", \ |
| 33 | x->offset_before, x->offset_after, x->correction_pos); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 34 | |
| 35 | static DEFINE_SPINLOCK(nf_nat_seqofs_lock); |
| 36 | |
| 37 | /* Setup TCP sequence correction given this change at this sequence */ |
| 38 | static inline void |
| 39 | adjust_tcp_sequence(u32 seq, |
| 40 | int sizediff, |
| 41 | struct nf_conn *ct, |
| 42 | enum ip_conntrack_info ctinfo) |
| 43 | { |
Hannes Eder | 76ac894 | 2009-11-05 15:51:19 +0100 | [diff] [blame] | 44 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 45 | struct nf_conn_nat *nat = nfct_nat(ct); |
Hannes Eder | 76ac894 | 2009-11-05 15:51:19 +0100 | [diff] [blame] | 46 | struct nf_nat_seq *this_way = &nat->seq[dir]; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 47 | |
Hannes Eder | 76ac894 | 2009-11-05 15:51:19 +0100 | [diff] [blame] | 48 | pr_debug("adjust_tcp_sequence: seq = %u, sizediff = %d\n", |
| 49 | seq, sizediff); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 50 | |
Hannes Eder | 76ac894 | 2009-11-05 15:51:19 +0100 | [diff] [blame] | 51 | pr_debug("adjust_tcp_sequence: Seq_offset before: "); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 52 | DUMP_OFFSET(this_way); |
| 53 | |
| 54 | spin_lock_bh(&nf_nat_seqofs_lock); |
| 55 | |
| 56 | /* SYN adjust. If it's uninitialized, or this is after last |
| 57 | * correction, record it: we don't handle more than one |
| 58 | * adjustment in the window, but do deal with common case of a |
| 59 | * retransmit */ |
| 60 | if (this_way->offset_before == this_way->offset_after || |
| 61 | before(this_way->correction_pos, seq)) { |
Hannes Eder | 76ac894 | 2009-11-05 15:51:19 +0100 | [diff] [blame] | 62 | this_way->correction_pos = seq; |
| 63 | this_way->offset_before = this_way->offset_after; |
| 64 | this_way->offset_after += sizediff; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 65 | } |
| 66 | spin_unlock_bh(&nf_nat_seqofs_lock); |
| 67 | |
Hannes Eder | 76ac894 | 2009-11-05 15:51:19 +0100 | [diff] [blame] | 68 | pr_debug("adjust_tcp_sequence: Seq_offset after: "); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 69 | DUMP_OFFSET(this_way); |
| 70 | } |
| 71 | |
Jozsef Kadlecsik | f9dd09c | 2009-11-06 00:43:42 -0800 | [diff] [blame] | 72 | /* Get the offset value, for conntrack */ |
| 73 | s16 nf_nat_get_offset(const struct nf_conn *ct, |
| 74 | enum ip_conntrack_dir dir, |
| 75 | u32 seq) |
| 76 | { |
| 77 | struct nf_conn_nat *nat = nfct_nat(ct); |
| 78 | struct nf_nat_seq *this_way; |
| 79 | s16 offset; |
| 80 | |
| 81 | if (!nat) |
| 82 | return 0; |
| 83 | |
| 84 | this_way = &nat->seq[dir]; |
| 85 | spin_lock_bh(&nf_nat_seqofs_lock); |
| 86 | offset = after(seq, this_way->correction_pos) |
| 87 | ? this_way->offset_after : this_way->offset_before; |
| 88 | spin_unlock_bh(&nf_nat_seqofs_lock); |
| 89 | |
| 90 | return offset; |
| 91 | } |
| 92 | EXPORT_SYMBOL_GPL(nf_nat_get_offset); |
| 93 | |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 94 | /* Frobs data inside this packet, which is linear. */ |
| 95 | static void mangle_contents(struct sk_buff *skb, |
| 96 | unsigned int dataoff, |
| 97 | unsigned int match_offset, |
| 98 | unsigned int match_len, |
| 99 | const char *rep_buffer, |
| 100 | unsigned int rep_len) |
| 101 | { |
| 102 | unsigned char *data; |
| 103 | |
| 104 | BUG_ON(skb_is_nonlinear(skb)); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 105 | data = skb_network_header(skb) + dataoff; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 106 | |
| 107 | /* move post-replacement */ |
| 108 | memmove(data + match_offset + rep_len, |
| 109 | data + match_offset + match_len, |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 110 | skb->tail - (skb->network_header + dataoff + |
| 111 | match_offset + match_len)); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 112 | |
| 113 | /* insert data from buffer */ |
| 114 | memcpy(data + match_offset, rep_buffer, rep_len); |
| 115 | |
| 116 | /* update skb info */ |
| 117 | if (rep_len > match_len) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 118 | pr_debug("nf_nat_mangle_packet: Extending packet by " |
| 119 | "%u from %u bytes\n", rep_len - match_len, skb->len); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 120 | skb_put(skb, rep_len - match_len); |
| 121 | } else { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 122 | pr_debug("nf_nat_mangle_packet: Shrinking packet from " |
| 123 | "%u from %u bytes\n", match_len - rep_len, skb->len); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 124 | __skb_trim(skb, skb->len + rep_len - match_len); |
| 125 | } |
| 126 | |
| 127 | /* fix IP hdr checksum information */ |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 128 | ip_hdr(skb)->tot_len = htons(skb->len); |
| 129 | ip_send_check(ip_hdr(skb)); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* Unusual, but possible case. */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 133 | static int enlarge_skb(struct sk_buff *skb, unsigned int extra) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 134 | { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 135 | if (skb->len + extra > 65535) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 136 | return 0; |
| 137 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 138 | if (pskb_expand_head(skb, 0, extra - skb_tailroom(skb), GFP_ATOMIC)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 139 | return 0; |
| 140 | |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | /* Generic function for mangling variable-length address changes inside |
| 145 | * NATed TCP connections (like the PORT XXX,XXX,XXX,XXX,XXX,XXX |
| 146 | * command in FTP). |
| 147 | * |
| 148 | * Takes care about all the nasty sequence number changes, checksumming, |
| 149 | * skb enlargement, ... |
| 150 | * |
| 151 | * */ |
| 152 | int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 153 | nf_nat_mangle_tcp_packet(struct sk_buff *skb, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 154 | struct nf_conn *ct, |
| 155 | enum ip_conntrack_info ctinfo, |
| 156 | unsigned int match_offset, |
| 157 | unsigned int match_len, |
| 158 | const char *rep_buffer, |
| 159 | unsigned int rep_len) |
| 160 | { |
Eric Dumazet | 511c3f9 | 2009-06-02 05:14:27 +0000 | [diff] [blame] | 161 | struct rtable *rt = skb_rtable(skb); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 162 | struct iphdr *iph; |
| 163 | struct tcphdr *tcph; |
| 164 | int oldlen, datalen; |
| 165 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 166 | if (!skb_make_writable(skb, skb->len)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 167 | return 0; |
| 168 | |
| 169 | if (rep_len > match_len && |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 170 | rep_len - match_len > skb_tailroom(skb) && |
| 171 | !enlarge_skb(skb, rep_len - match_len)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 172 | return 0; |
| 173 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 174 | SKB_LINEAR_ASSERT(skb); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 175 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 176 | iph = ip_hdr(skb); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 177 | tcph = (void *)iph + iph->ihl*4; |
| 178 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 179 | oldlen = skb->len - iph->ihl*4; |
| 180 | mangle_contents(skb, iph->ihl*4 + tcph->doff*4, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 181 | match_offset, match_len, rep_buffer, rep_len); |
| 182 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 183 | datalen = skb->len - iph->ihl*4; |
| 184 | if (skb->ip_summed != CHECKSUM_PARTIAL) { |
Patrick McHardy | fe6092e | 2007-04-12 22:15:50 -0700 | [diff] [blame] | 185 | if (!(rt->rt_flags & RTCF_LOCAL) && |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 186 | skb->dev->features & NETIF_F_V4_CSUM) { |
| 187 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 188 | skb->csum_start = skb_headroom(skb) + |
| 189 | skb_network_offset(skb) + |
| 190 | iph->ihl * 4; |
| 191 | skb->csum_offset = offsetof(struct tcphdr, check); |
Patrick McHardy | fe6092e | 2007-04-12 22:15:50 -0700 | [diff] [blame] | 192 | tcph->check = ~tcp_v4_check(datalen, |
| 193 | iph->saddr, iph->daddr, 0); |
| 194 | } else { |
| 195 | tcph->check = 0; |
| 196 | tcph->check = tcp_v4_check(datalen, |
| 197 | iph->saddr, iph->daddr, |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 198 | csum_partial(tcph, |
Patrick McHardy | fe6092e | 2007-04-12 22:15:50 -0700 | [diff] [blame] | 199 | datalen, 0)); |
| 200 | } |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 201 | } else |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 202 | inet_proto_csum_replace2(&tcph->check, skb, |
| 203 | htons(oldlen), htons(datalen), 1); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 204 | |
| 205 | if (rep_len != match_len) { |
| 206 | set_bit(IPS_SEQ_ADJUST_BIT, &ct->status); |
| 207 | adjust_tcp_sequence(ntohl(tcph->seq), |
| 208 | (int)rep_len - (int)match_len, |
| 209 | ct, ctinfo); |
Alexey Dobriyan | a71996f | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 210 | nf_conntrack_event_cache(IPCT_NATSEQADJ, ct); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 211 | } |
| 212 | return 1; |
| 213 | } |
| 214 | EXPORT_SYMBOL(nf_nat_mangle_tcp_packet); |
| 215 | |
| 216 | /* Generic function for mangling variable-length address changes inside |
| 217 | * NATed UDP connections (like the CONNECT DATA XXXXX MESG XXXXX INDEX XXXXX |
| 218 | * command in the Amanda protocol) |
| 219 | * |
| 220 | * Takes care about all the nasty sequence number changes, checksumming, |
| 221 | * skb enlargement, ... |
| 222 | * |
| 223 | * XXX - This function could be merged with nf_nat_mangle_tcp_packet which |
| 224 | * should be fairly easy to do. |
| 225 | */ |
| 226 | int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 227 | nf_nat_mangle_udp_packet(struct sk_buff *skb, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 228 | struct nf_conn *ct, |
| 229 | enum ip_conntrack_info ctinfo, |
| 230 | unsigned int match_offset, |
| 231 | unsigned int match_len, |
| 232 | const char *rep_buffer, |
| 233 | unsigned int rep_len) |
| 234 | { |
Eric Dumazet | 511c3f9 | 2009-06-02 05:14:27 +0000 | [diff] [blame] | 235 | struct rtable *rt = skb_rtable(skb); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 236 | struct iphdr *iph; |
| 237 | struct udphdr *udph; |
| 238 | int datalen, oldlen; |
| 239 | |
| 240 | /* UDP helpers might accidentally mangle the wrong packet */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 241 | iph = ip_hdr(skb); |
| 242 | if (skb->len < iph->ihl*4 + sizeof(*udph) + |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 243 | match_offset + match_len) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 244 | return 0; |
| 245 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 246 | if (!skb_make_writable(skb, skb->len)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 247 | return 0; |
| 248 | |
| 249 | if (rep_len > match_len && |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 250 | rep_len - match_len > skb_tailroom(skb) && |
| 251 | !enlarge_skb(skb, rep_len - match_len)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 252 | return 0; |
| 253 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 254 | iph = ip_hdr(skb); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 255 | udph = (void *)iph + iph->ihl*4; |
| 256 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 257 | oldlen = skb->len - iph->ihl*4; |
| 258 | mangle_contents(skb, iph->ihl*4 + sizeof(*udph), |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 259 | match_offset, match_len, rep_buffer, rep_len); |
| 260 | |
| 261 | /* update the length of the UDP packet */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 262 | datalen = skb->len - iph->ihl*4; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 263 | udph->len = htons(datalen); |
| 264 | |
| 265 | /* fix udp checksum if udp checksum was previously calculated */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 266 | if (!udph->check && skb->ip_summed != CHECKSUM_PARTIAL) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 267 | return 1; |
| 268 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 269 | if (skb->ip_summed != CHECKSUM_PARTIAL) { |
Patrick McHardy | fe6092e | 2007-04-12 22:15:50 -0700 | [diff] [blame] | 270 | if (!(rt->rt_flags & RTCF_LOCAL) && |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 271 | skb->dev->features & NETIF_F_V4_CSUM) { |
| 272 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 273 | skb->csum_start = skb_headroom(skb) + |
| 274 | skb_network_offset(skb) + |
| 275 | iph->ihl * 4; |
| 276 | skb->csum_offset = offsetof(struct udphdr, check); |
Patrick McHardy | fe6092e | 2007-04-12 22:15:50 -0700 | [diff] [blame] | 277 | udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, |
| 278 | datalen, IPPROTO_UDP, |
| 279 | 0); |
| 280 | } else { |
| 281 | udph->check = 0; |
| 282 | udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr, |
| 283 | datalen, IPPROTO_UDP, |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 284 | csum_partial(udph, |
Patrick McHardy | fe6092e | 2007-04-12 22:15:50 -0700 | [diff] [blame] | 285 | datalen, 0)); |
| 286 | if (!udph->check) |
| 287 | udph->check = CSUM_MANGLED_0; |
| 288 | } |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 289 | } else |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 290 | inet_proto_csum_replace2(&udph->check, skb, |
| 291 | htons(oldlen), htons(datalen), 1); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 292 | |
| 293 | return 1; |
| 294 | } |
| 295 | EXPORT_SYMBOL(nf_nat_mangle_udp_packet); |
| 296 | |
| 297 | /* Adjust one found SACK option including checksum correction */ |
| 298 | static void |
| 299 | sack_adjust(struct sk_buff *skb, |
| 300 | struct tcphdr *tcph, |
| 301 | unsigned int sackoff, |
| 302 | unsigned int sackend, |
| 303 | struct nf_nat_seq *natseq) |
| 304 | { |
| 305 | while (sackoff < sackend) { |
| 306 | struct tcp_sack_block_wire *sack; |
| 307 | __be32 new_start_seq, new_end_seq; |
| 308 | |
| 309 | sack = (void *)skb->data + sackoff; |
| 310 | if (after(ntohl(sack->start_seq) - natseq->offset_before, |
| 311 | natseq->correction_pos)) |
| 312 | new_start_seq = htonl(ntohl(sack->start_seq) |
| 313 | - natseq->offset_after); |
| 314 | else |
| 315 | new_start_seq = htonl(ntohl(sack->start_seq) |
| 316 | - natseq->offset_before); |
| 317 | |
| 318 | if (after(ntohl(sack->end_seq) - natseq->offset_before, |
| 319 | natseq->correction_pos)) |
| 320 | new_end_seq = htonl(ntohl(sack->end_seq) |
| 321 | - natseq->offset_after); |
| 322 | else |
| 323 | new_end_seq = htonl(ntohl(sack->end_seq) |
| 324 | - natseq->offset_before); |
| 325 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 326 | pr_debug("sack_adjust: start_seq: %d->%d, end_seq: %d->%d\n", |
| 327 | ntohl(sack->start_seq), new_start_seq, |
| 328 | ntohl(sack->end_seq), new_end_seq); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 329 | |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 330 | inet_proto_csum_replace4(&tcph->check, skb, |
| 331 | sack->start_seq, new_start_seq, 0); |
| 332 | inet_proto_csum_replace4(&tcph->check, skb, |
| 333 | sack->end_seq, new_end_seq, 0); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 334 | sack->start_seq = new_start_seq; |
| 335 | sack->end_seq = new_end_seq; |
| 336 | sackoff += sizeof(*sack); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /* TCP SACK sequence number adjustment */ |
| 341 | static inline unsigned int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 342 | nf_nat_sack_adjust(struct sk_buff *skb, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 343 | struct tcphdr *tcph, |
| 344 | struct nf_conn *ct, |
| 345 | enum ip_conntrack_info ctinfo) |
| 346 | { |
| 347 | unsigned int dir, optoff, optend; |
| 348 | struct nf_conn_nat *nat = nfct_nat(ct); |
| 349 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 350 | optoff = ip_hdrlen(skb) + sizeof(struct tcphdr); |
| 351 | optend = ip_hdrlen(skb) + tcph->doff * 4; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 352 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 353 | if (!skb_make_writable(skb, optend)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 354 | return 0; |
| 355 | |
| 356 | dir = CTINFO2DIR(ctinfo); |
| 357 | |
| 358 | while (optoff < optend) { |
| 359 | /* Usually: option, length. */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 360 | unsigned char *op = skb->data + optoff; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 361 | |
| 362 | switch (op[0]) { |
| 363 | case TCPOPT_EOL: |
| 364 | return 1; |
| 365 | case TCPOPT_NOP: |
| 366 | optoff++; |
| 367 | continue; |
| 368 | default: |
| 369 | /* no partial options */ |
| 370 | if (optoff + 1 == optend || |
| 371 | optoff + op[1] > optend || |
| 372 | op[1] < 2) |
| 373 | return 0; |
| 374 | if (op[0] == TCPOPT_SACK && |
| 375 | op[1] >= 2+TCPOLEN_SACK_PERBLOCK && |
| 376 | ((op[1] - 2) % TCPOLEN_SACK_PERBLOCK) == 0) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 377 | sack_adjust(skb, tcph, optoff+2, |
Yasuyuki Kozakai | b6b84d4 | 2007-07-07 22:26:35 -0700 | [diff] [blame] | 378 | optoff+op[1], &nat->seq[!dir]); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 379 | optoff += op[1]; |
| 380 | } |
| 381 | } |
| 382 | return 1; |
| 383 | } |
| 384 | |
| 385 | /* TCP sequence number adjustment. Returns 1 on success, 0 on failure */ |
| 386 | int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 387 | nf_nat_seq_adjust(struct sk_buff *skb, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 388 | struct nf_conn *ct, |
| 389 | enum ip_conntrack_info ctinfo) |
| 390 | { |
| 391 | struct tcphdr *tcph; |
| 392 | int dir; |
| 393 | __be32 newseq, newack; |
Patrick McHardy | a3a9f79 | 2009-06-29 14:07:56 +0200 | [diff] [blame] | 394 | s16 seqoff, ackoff; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 395 | struct nf_conn_nat *nat = nfct_nat(ct); |
| 396 | struct nf_nat_seq *this_way, *other_way; |
| 397 | |
| 398 | dir = CTINFO2DIR(ctinfo); |
| 399 | |
Yasuyuki Kozakai | b6b84d4 | 2007-07-07 22:26:35 -0700 | [diff] [blame] | 400 | this_way = &nat->seq[dir]; |
| 401 | other_way = &nat->seq[!dir]; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 402 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 403 | if (!skb_make_writable(skb, ip_hdrlen(skb) + sizeof(*tcph))) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 404 | return 0; |
| 405 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 406 | tcph = (void *)skb->data + ip_hdrlen(skb); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 407 | if (after(ntohl(tcph->seq), this_way->correction_pos)) |
Patrick McHardy | a3a9f79 | 2009-06-29 14:07:56 +0200 | [diff] [blame] | 408 | seqoff = this_way->offset_after; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 409 | else |
Patrick McHardy | a3a9f79 | 2009-06-29 14:07:56 +0200 | [diff] [blame] | 410 | seqoff = this_way->offset_before; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 411 | |
| 412 | if (after(ntohl(tcph->ack_seq) - other_way->offset_before, |
| 413 | other_way->correction_pos)) |
Patrick McHardy | a3a9f79 | 2009-06-29 14:07:56 +0200 | [diff] [blame] | 414 | ackoff = other_way->offset_after; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 415 | else |
Patrick McHardy | a3a9f79 | 2009-06-29 14:07:56 +0200 | [diff] [blame] | 416 | ackoff = other_way->offset_before; |
| 417 | |
| 418 | newseq = htonl(ntohl(tcph->seq) + seqoff); |
| 419 | newack = htonl(ntohl(tcph->ack_seq) - ackoff); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 420 | |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 421 | inet_proto_csum_replace4(&tcph->check, skb, tcph->seq, newseq, 0); |
| 422 | inet_proto_csum_replace4(&tcph->check, skb, tcph->ack_seq, newack, 0); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 423 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 424 | pr_debug("Adjusting sequence number from %u->%u, ack from %u->%u\n", |
| 425 | ntohl(tcph->seq), ntohl(newseq), ntohl(tcph->ack_seq), |
| 426 | ntohl(newack)); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 427 | |
| 428 | tcph->seq = newseq; |
| 429 | tcph->ack_seq = newack; |
| 430 | |
Jozsef Kadlecsik | f9dd09c | 2009-11-06 00:43:42 -0800 | [diff] [blame] | 431 | return nf_nat_sack_adjust(skb, tcph, ct, ctinfo); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 432 | } |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 433 | |
| 434 | /* Setup NAT on this expected conntrack so it follows master. */ |
| 435 | /* If we fail to get a free NAT slot, we'll get dropped on confirm */ |
| 436 | void nf_nat_follow_master(struct nf_conn *ct, |
| 437 | struct nf_conntrack_expect *exp) |
| 438 | { |
| 439 | struct nf_nat_range range; |
| 440 | |
| 441 | /* This must be a fresh one. */ |
| 442 | BUG_ON(ct->status & IPS_NAT_DONE_MASK); |
| 443 | |
| 444 | /* Change src to where master sends to */ |
| 445 | range.flags = IP_NAT_RANGE_MAP_IPS; |
| 446 | range.min_ip = range.max_ip |
| 447 | = ct->master->tuplehash[!exp->dir].tuple.dst.u3.ip; |
Patrick McHardy | cc01dcb | 2007-12-17 22:38:20 -0800 | [diff] [blame] | 448 | nf_nat_setup_info(ct, &range, IP_NAT_MANIP_SRC); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 449 | |
| 450 | /* For DST manip, map port here to where it's expected. */ |
| 451 | range.flags = (IP_NAT_RANGE_MAP_IPS | IP_NAT_RANGE_PROTO_SPECIFIED); |
| 452 | range.min = range.max = exp->saved_proto; |
| 453 | range.min_ip = range.max_ip |
| 454 | = ct->master->tuplehash[!exp->dir].tuple.src.u3.ip; |
Patrick McHardy | cc01dcb | 2007-12-17 22:38:20 -0800 | [diff] [blame] | 455 | nf_nat_setup_info(ct, &range, IP_NAT_MANIP_DST); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 456 | } |
| 457 | EXPORT_SYMBOL(nf_nat_follow_master); |