Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 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> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 10 | #include <linux/export.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 11 | #include <linux/init.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 12 | #include <linux/udp.h> |
| 13 | |
| 14 | #include <linux/netfilter.h> |
| 15 | #include <net/netfilter/nf_nat.h> |
| 16 | #include <net/netfilter/nf_nat_core.h> |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 17 | #include <net/netfilter/nf_nat_l3proto.h> |
| 18 | #include <net/netfilter/nf_nat_l4proto.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 19 | |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 20 | static u16 udp_port_rover; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 21 | |
Changli Gao | f43dc98 | 2010-08-02 17:20:54 +0200 | [diff] [blame] | 22 | static void |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 23 | udp_unique_tuple(const struct nf_nat_l3proto *l3proto, |
| 24 | struct nf_conntrack_tuple *tuple, |
| 25 | const struct nf_nat_range *range, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 26 | enum nf_nat_manip_type maniptype, |
| 27 | const struct nf_conn *ct) |
| 28 | { |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 29 | nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct, |
| 30 | &udp_port_rover); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 33 | static bool |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 34 | udp_manip_pkt(struct sk_buff *skb, |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 35 | const struct nf_nat_l3proto *l3proto, |
| 36 | unsigned int iphdroff, unsigned int hdroff, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 37 | const struct nf_conntrack_tuple *tuple, |
| 38 | enum nf_nat_manip_type maniptype) |
| 39 | { |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 40 | struct udphdr *hdr; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 41 | __be16 *portptr, newport; |
| 42 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 43 | if (!skb_make_writable(skb, hdroff + sizeof(*hdr))) |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 44 | return false; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 45 | hdr = (struct udphdr *)(skb->data + hdroff); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 46 | |
Patrick McHardy | cbc9f2f | 2011-12-23 13:59:49 +0100 | [diff] [blame] | 47 | if (maniptype == NF_NAT_MANIP_SRC) { |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 48 | /* Get rid of src port */ |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 49 | newport = tuple->src.u.udp.port; |
| 50 | portptr = &hdr->source; |
| 51 | } else { |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 52 | /* Get rid of dst port */ |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 53 | newport = tuple->dst.u.udp.port; |
| 54 | portptr = &hdr->dest; |
| 55 | } |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 56 | if (hdr->check || skb->ip_summed == CHECKSUM_PARTIAL) { |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 57 | l3proto->csum_update(skb, iphdroff, &hdr->check, |
| 58 | tuple, maniptype); |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 59 | inet_proto_csum_replace2(&hdr->check, skb, *portptr, newport, |
| 60 | 0); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 61 | if (!hdr->check) |
| 62 | hdr->check = CSUM_MANGLED_0; |
| 63 | } |
| 64 | *portptr = newport; |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 65 | return true; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 68 | const struct nf_nat_l4proto nf_nat_l4proto_udp = { |
| 69 | .l4proto = IPPROTO_UDP, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 70 | .manip_pkt = udp_manip_pkt, |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 71 | .in_range = nf_nat_l4proto_in_range, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 72 | .unique_tuple = udp_unique_tuple, |
Duan Jiong | 24de3d3 | 2014-06-30 09:19:32 +0800 | [diff] [blame^] | 73 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 74 | .nlattr_to_range = nf_nat_l4proto_nlattr_to_range, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 75 | #endif |
| 76 | }; |