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