Merge tag 'v3.10.65' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / nf_nat_proto_tcp.c
CommitLineData
5b1158e9
JK
1/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/types.h>
10#include <linux/init.h>
bc3b2d7f 11#include <linux/export.h>
5b1158e9
JK
12#include <linux/tcp.h>
13
14#include <linux/netfilter.h>
15#include <linux/netfilter/nfnetlink_conntrack.h>
16#include <net/netfilter/nf_nat.h>
c7232c99
PM
17#include <net/netfilter/nf_nat_l3proto.h>
18#include <net/netfilter/nf_nat_l4proto.h>
5b1158e9
JK
19#include <net/netfilter/nf_nat_core.h>
20
c7232c99 21static u16 tcp_port_rover;
5b1158e9 22
f43dc98b 23static void
c7232c99
PM
24tcp_unique_tuple(const struct nf_nat_l3proto *l3proto,
25 struct nf_conntrack_tuple *tuple,
26 const struct nf_nat_range *range,
5b1158e9
JK
27 enum nf_nat_manip_type maniptype,
28 const struct nf_conn *ct)
29{
c7232c99
PM
30 nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct,
31 &tcp_port_rover);
5b1158e9
JK
32}
33
f2ea825f 34static bool
3db05fea 35tcp_manip_pkt(struct sk_buff *skb,
c7232c99
PM
36 const struct nf_nat_l3proto *l3proto,
37 unsigned int iphdroff, unsigned int hdroff,
5b1158e9
JK
38 const struct nf_conntrack_tuple *tuple,
39 enum nf_nat_manip_type maniptype)
40{
5b1158e9 41 struct tcphdr *hdr;
5b1158e9
JK
42 __be16 *portptr, newport, oldport;
43 int hdrsize = 8; /* TCP connection tracking guarantees this much */
44
45 /* this could be a inner header returned in icmp packet; in such
46 cases we cannot update the checksum field since it is outside of
47 the 8 bytes of transport layer headers we are guaranteed */
3db05fea 48 if (skb->len >= hdroff + sizeof(struct tcphdr))
5b1158e9
JK
49 hdrsize = sizeof(struct tcphdr);
50
3db05fea 51 if (!skb_make_writable(skb, hdroff + hdrsize))
f2ea825f 52 return false;
5b1158e9 53
3db05fea 54 hdr = (struct tcphdr *)(skb->data + hdroff);
5b1158e9 55
cbc9f2f4 56 if (maniptype == NF_NAT_MANIP_SRC) {
c7232c99 57 /* Get rid of src port */
5b1158e9
JK
58 newport = tuple->src.u.tcp.port;
59 portptr = &hdr->source;
60 } else {
c7232c99 61 /* Get rid of dst port */
5b1158e9
JK
62 newport = tuple->dst.u.tcp.port;
63 portptr = &hdr->dest;
64 }
65
66 oldport = *portptr;
67 *portptr = newport;
68
69 if (hdrsize < sizeof(*hdr))
f2ea825f 70 return true;
5b1158e9 71
c7232c99 72 l3proto->csum_update(skb, iphdroff, &hdr->check, tuple, maniptype);
be0ea7d5 73 inet_proto_csum_replace2(&hdr->check, skb, oldport, newport, 0);
f2ea825f 74 return true;
5b1158e9
JK
75}
76
c7232c99
PM
77const struct nf_nat_l4proto nf_nat_l4proto_tcp = {
78 .l4proto = IPPROTO_TCP,
5b1158e9 79 .manip_pkt = tcp_manip_pkt,
c7232c99 80 .in_range = nf_nat_l4proto_in_range,
5b1158e9 81 .unique_tuple = tcp_unique_tuple,
e281db5c 82#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
c7232c99 83 .nlattr_to_range = nf_nat_l4proto_nlattr_to_range,
5b1158e9
JK
84#endif
85};