]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/netfilter/nf_nat_proto_udp.c
Merge tag 'armsoc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[mirror_ubuntu-artful-kernel.git] / net / netfilter / nf_nat_proto_udp.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>
bc3b2d7f 10#include <linux/export.h>
5b1158e9 11#include <linux/init.h>
5b1158e9
JK
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>
c7232c99
PM
17#include <net/netfilter/nf_nat_l3proto.h>
18#include <net/netfilter/nf_nat_l4proto.h>
5b1158e9 19
c7232c99 20static u16 udp_port_rover;
5b1158e9 21
f43dc98b 22static void
c7232c99
PM
23udp_unique_tuple(const struct nf_nat_l3proto *l3proto,
24 struct nf_conntrack_tuple *tuple,
25 const struct nf_nat_range *range,
5b1158e9
JK
26 enum nf_nat_manip_type maniptype,
27 const struct nf_conn *ct)
28{
c7232c99
PM
29 nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct,
30 &udp_port_rover);
5b1158e9
JK
31}
32
9700ba80
FW
33static void
34__udp_manip_pkt(struct sk_buff *skb,
35 const struct nf_nat_l3proto *l3proto,
36 unsigned int iphdroff, struct udphdr *hdr,
37 const struct nf_conntrack_tuple *tuple,
38 enum nf_nat_manip_type maniptype, bool do_csum)
5b1158e9 39{
5b1158e9
JK
40 __be16 *portptr, newport;
41
cbc9f2f4 42 if (maniptype == NF_NAT_MANIP_SRC) {
c7232c99 43 /* Get rid of src port */
5b1158e9
JK
44 newport = tuple->src.u.udp.port;
45 portptr = &hdr->source;
46 } else {
c7232c99 47 /* Get rid of dst port */
5b1158e9
JK
48 newport = tuple->dst.u.udp.port;
49 portptr = &hdr->dest;
50 }
9700ba80 51 if (do_csum) {
c7232c99
PM
52 l3proto->csum_update(skb, iphdroff, &hdr->check,
53 tuple, maniptype);
be0ea7d5 54 inet_proto_csum_replace2(&hdr->check, skb, *portptr, newport,
4b048d6d 55 false);
5b1158e9
JK
56 if (!hdr->check)
57 hdr->check = CSUM_MANGLED_0;
58 }
59 *portptr = newport;
9700ba80
FW
60}
61
62static bool udp_manip_pkt(struct sk_buff *skb,
63 const struct nf_nat_l3proto *l3proto,
64 unsigned int iphdroff, unsigned int hdroff,
65 const struct nf_conntrack_tuple *tuple,
66 enum nf_nat_manip_type maniptype)
67{
68 struct udphdr *hdr;
69 bool do_csum;
70
71 if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
72 return false;
73
74 hdr = (struct udphdr *)(skb->data + hdroff);
75 do_csum = hdr->check || skb->ip_summed == CHECKSUM_PARTIAL;
76
77 __udp_manip_pkt(skb, l3proto, iphdroff, hdr, tuple, maniptype, do_csum);
78 return true;
79}
80
81#ifdef CONFIG_NF_NAT_PROTO_UDPLITE
82static u16 udplite_port_rover;
83
84static bool udplite_manip_pkt(struct sk_buff *skb,
85 const struct nf_nat_l3proto *l3proto,
86 unsigned int iphdroff, unsigned int hdroff,
87 const struct nf_conntrack_tuple *tuple,
88 enum nf_nat_manip_type maniptype)
89{
90 struct udphdr *hdr;
91
92 if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
93 return false;
94
95 hdr = (struct udphdr *)(skb->data + hdroff);
96 __udp_manip_pkt(skb, l3proto, iphdroff, hdr, tuple, maniptype, true);
f2ea825f 97 return true;
5b1158e9
JK
98}
99
9700ba80
FW
100static void
101udplite_unique_tuple(const struct nf_nat_l3proto *l3proto,
102 struct nf_conntrack_tuple *tuple,
103 const struct nf_nat_range *range,
104 enum nf_nat_manip_type maniptype,
105 const struct nf_conn *ct)
106{
107 nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct,
108 &udplite_port_rover);
109}
110
111const struct nf_nat_l4proto nf_nat_l4proto_udplite = {
112 .l4proto = IPPROTO_UDPLITE,
113 .manip_pkt = udplite_manip_pkt,
114 .in_range = nf_nat_l4proto_in_range,
115 .unique_tuple = udplite_unique_tuple,
116#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
117 .nlattr_to_range = nf_nat_l4proto_nlattr_to_range,
118#endif
119};
120#endif /* CONFIG_NF_NAT_PROTO_UDPLITE */
121
c7232c99
PM
122const struct nf_nat_l4proto nf_nat_l4proto_udp = {
123 .l4proto = IPPROTO_UDP,
5b1158e9 124 .manip_pkt = udp_manip_pkt,
c7232c99 125 .in_range = nf_nat_l4proto_in_range,
5b1158e9 126 .unique_tuple = udp_unique_tuple,
24de3d37 127#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
c7232c99 128 .nlattr_to_range = nf_nat_l4proto_nlattr_to_range,
5b1158e9
JK
129#endif
130};