blob: 4f8820fc514804d775274330f590fe0d1dbab54f [file] [log] [blame]
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001/* (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>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040011#include <linux/export.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080012#include <linux/tcp.h>
13
14#include <linux/netfilter.h>
15#include <linux/netfilter/nfnetlink_conntrack.h>
16#include <net/netfilter/nf_nat.h>
Patrick McHardyc7232c92012-08-26 19:14:06 +020017#include <net/netfilter/nf_nat_l3proto.h>
18#include <net/netfilter/nf_nat_l4proto.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080019#include <net/netfilter/nf_nat_core.h>
20
Patrick McHardyc7232c92012-08-26 19:14:06 +020021static u16 tcp_port_rover;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080022
Changli Gaof43dc982010-08-02 17:20:54 +020023static void
Patrick McHardyc7232c92012-08-26 19:14:06 +020024tcp_unique_tuple(const struct nf_nat_l3proto *l3proto,
25 struct nf_conntrack_tuple *tuple,
26 const struct nf_nat_range *range,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080027 enum nf_nat_manip_type maniptype,
28 const struct nf_conn *ct)
29{
Patrick McHardyc7232c92012-08-26 19:14:06 +020030 nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct,
31 &tcp_port_rover);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080032}
33
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020034static bool
Herbert Xu3db05fe2007-10-15 00:53:15 -070035tcp_manip_pkt(struct sk_buff *skb,
Patrick McHardyc7232c92012-08-26 19:14:06 +020036 const struct nf_nat_l3proto *l3proto,
37 unsigned int iphdroff, unsigned int hdroff,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080038 const struct nf_conntrack_tuple *tuple,
39 enum nf_nat_manip_type maniptype)
40{
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080041 struct tcphdr *hdr;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080042 __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 */
Herbert Xu3db05fe2007-10-15 00:53:15 -070048 if (skb->len >= hdroff + sizeof(struct tcphdr))
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080049 hdrsize = sizeof(struct tcphdr);
50
Herbert Xu3db05fe2007-10-15 00:53:15 -070051 if (!skb_make_writable(skb, hdroff + hdrsize))
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020052 return false;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080053
Herbert Xu3db05fe2007-10-15 00:53:15 -070054 hdr = (struct tcphdr *)(skb->data + hdroff);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080055
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010056 if (maniptype == NF_NAT_MANIP_SRC) {
Patrick McHardyc7232c92012-08-26 19:14:06 +020057 /* Get rid of src port */
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080058 newport = tuple->src.u.tcp.port;
59 portptr = &hdr->source;
60 } else {
Patrick McHardyc7232c92012-08-26 19:14:06 +020061 /* Get rid of dst port */
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080062 newport = tuple->dst.u.tcp.port;
63 portptr = &hdr->dest;
64 }
65
66 oldport = *portptr;
67 *portptr = newport;
68
69 if (hdrsize < sizeof(*hdr))
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020070 return true;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080071
Patrick McHardyc7232c92012-08-26 19:14:06 +020072 l3proto->csum_update(skb, iphdroff, &hdr->check, tuple, maniptype);
Tom Herbert4b048d62015-08-17 13:42:25 -070073 inet_proto_csum_replace2(&hdr->check, skb, oldport, newport, false);
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020074 return true;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080075}
76
Patrick McHardyc7232c92012-08-26 19:14:06 +020077const struct nf_nat_l4proto nf_nat_l4proto_tcp = {
78 .l4proto = IPPROTO_TCP,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080079 .manip_pkt = tcp_manip_pkt,
Patrick McHardyc7232c92012-08-26 19:14:06 +020080 .in_range = nf_nat_l4proto_in_range,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080081 .unique_tuple = tcp_unique_tuple,
Duan Jiong24de3d32014-06-30 09:19:32 +080082#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Patrick McHardyc7232c92012-08-26 19:14:06 +020083 .nlattr_to_range = nf_nat_l4proto_nlattr_to_range,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080084#endif
85};