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