Commit | Line | Data |
---|---|---|
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 JK |
11 | #include <linux/init.h> |
12 | #include <linux/ip.h> | |
13 | #include <linux/udp.h> | |
14 | ||
15 | #include <linux/netfilter.h> | |
16 | #include <net/netfilter/nf_nat.h> | |
17 | #include <net/netfilter/nf_nat_core.h> | |
18 | #include <net/netfilter/nf_nat_rule.h> | |
19 | #include <net/netfilter/nf_nat_protocol.h> | |
20 | ||
937e0dfd | 21 | static u_int16_t udp_port_rover; |
5b1158e9 | 22 | |
f43dc98b | 23 | static void |
5b1158e9 | 24 | udp_unique_tuple(struct nf_conntrack_tuple *tuple, |
cbc9f2f4 | 25 | const struct nf_nat_ipv4_range *range, |
5b1158e9 JK |
26 | enum nf_nat_manip_type maniptype, |
27 | const struct nf_conn *ct) | |
28 | { | |
f43dc98b | 29 | nf_nat_proto_unique_tuple(tuple, range, maniptype, ct, &udp_port_rover); |
5b1158e9 JK |
30 | } |
31 | ||
f2ea825f | 32 | static bool |
3db05fea | 33 | udp_manip_pkt(struct sk_buff *skb, |
5b1158e9 JK |
34 | unsigned int iphdroff, |
35 | const struct nf_conntrack_tuple *tuple, | |
36 | enum nf_nat_manip_type maniptype) | |
37 | { | |
da3f13c9 | 38 | const struct iphdr *iph = (struct iphdr *)(skb->data + iphdroff); |
5b1158e9 JK |
39 | struct udphdr *hdr; |
40 | unsigned int hdroff = iphdroff + iph->ihl*4; | |
41 | __be32 oldip, newip; | |
42 | __be16 *portptr, newport; | |
43 | ||
3db05fea | 44 | if (!skb_make_writable(skb, hdroff + sizeof(*hdr))) |
f2ea825f | 45 | return false; |
5b1158e9 | 46 | |
3db05fea HX |
47 | iph = (struct iphdr *)(skb->data + iphdroff); |
48 | hdr = (struct udphdr *)(skb->data + hdroff); | |
5b1158e9 | 49 | |
cbc9f2f4 | 50 | if (maniptype == NF_NAT_MANIP_SRC) { |
5b1158e9 JK |
51 | /* Get rid of src ip and src pt */ |
52 | oldip = iph->saddr; | |
53 | newip = tuple->src.u3.ip; | |
54 | newport = tuple->src.u.udp.port; | |
55 | portptr = &hdr->source; | |
56 | } else { | |
57 | /* Get rid of dst ip and dst pt */ | |
58 | oldip = iph->daddr; | |
59 | newip = tuple->dst.u3.ip; | |
60 | newport = tuple->dst.u.udp.port; | |
61 | portptr = &hdr->dest; | |
62 | } | |
3db05fea | 63 | if (hdr->check || skb->ip_summed == CHECKSUM_PARTIAL) { |
be0ea7d5 PM |
64 | inet_proto_csum_replace4(&hdr->check, skb, oldip, newip, 1); |
65 | inet_proto_csum_replace2(&hdr->check, skb, *portptr, newport, | |
66 | 0); | |
5b1158e9 JK |
67 | if (!hdr->check) |
68 | hdr->check = CSUM_MANGLED_0; | |
69 | } | |
70 | *portptr = newport; | |
f2ea825f | 71 | return true; |
5b1158e9 JK |
72 | } |
73 | ||
2b628a08 | 74 | const struct nf_nat_protocol nf_nat_protocol_udp = { |
5b1158e9 | 75 | .protonum = IPPROTO_UDP, |
5b1158e9 | 76 | .manip_pkt = udp_manip_pkt, |
937e0dfd | 77 | .in_range = nf_nat_proto_in_range, |
5b1158e9 | 78 | .unique_tuple = udp_unique_tuple, |
e281db5c | 79 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
535b57c7 | 80 | .nlattr_to_range = nf_nat_proto_nlattr_to_range, |
5b1158e9 JK |
81 | #endif |
82 | }; |