blob: 9e61c79492e4beb0d02a56c0f1bb4b42226630b7 [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>
11#include <linux/ip.h>
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>
17#include <net/netfilter/nf_nat_rule.h>
18#include <net/netfilter/nf_nat_protocol.h>
19
Patrick McHardy937e0df2008-03-20 15:15:47 +010020static u_int16_t udp_port_rover;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080021
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020022static bool
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080023udp_unique_tuple(struct nf_conntrack_tuple *tuple,
24 const struct nf_nat_range *range,
25 enum nf_nat_manip_type maniptype,
26 const struct nf_conn *ct)
27{
Patrick McHardy937e0df2008-03-20 15:15:47 +010028 return nf_nat_proto_unique_tuple(tuple, range, maniptype, ct,
29 &udp_port_rover);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080030}
31
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020032static bool
Herbert Xu3db05fe2007-10-15 00:53:15 -070033udp_manip_pkt(struct sk_buff *skb,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080034 unsigned int iphdroff,
35 const struct nf_conntrack_tuple *tuple,
36 enum nf_nat_manip_type maniptype)
37{
Jan Engelhardtda3f13c2008-01-31 04:52:29 -080038 const struct iphdr *iph = (struct iphdr *)(skb->data + iphdroff);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080039 struct udphdr *hdr;
40 unsigned int hdroff = iphdroff + iph->ihl*4;
41 __be32 oldip, newip;
42 __be16 *portptr, newport;
43
Herbert Xu3db05fe2007-10-15 00:53:15 -070044 if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020045 return false;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080046
Herbert Xu3db05fe2007-10-15 00:53:15 -070047 iph = (struct iphdr *)(skb->data + iphdroff);
48 hdr = (struct udphdr *)(skb->data + hdroff);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080049
50 if (maniptype == IP_NAT_MANIP_SRC) {
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 }
Herbert Xu3db05fe2007-10-15 00:53:15 -070063 if (hdr->check || skb->ip_summed == CHECKSUM_PARTIAL) {
Patrick McHardybe0ea7d2007-11-30 01:17:11 +110064 inet_proto_csum_replace4(&hdr->check, skb, oldip, newip, 1);
65 inet_proto_csum_replace2(&hdr->check, skb, *portptr, newport,
66 0);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080067 if (!hdr->check)
68 hdr->check = CSUM_MANGLED_0;
69 }
70 *portptr = newport;
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020071 return true;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080072}
73
Patrick McHardy2b628a02007-12-17 22:37:36 -080074const struct nf_nat_protocol nf_nat_protocol_udp = {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080075 .protonum = IPPROTO_UDP,
76 .me = THIS_MODULE,
77 .manip_pkt = udp_manip_pkt,
Patrick McHardy937e0df2008-03-20 15:15:47 +010078 .in_range = nf_nat_proto_in_range,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080079 .unique_tuple = udp_unique_tuple,
Patrick McHardye281db5c2007-03-04 15:57:25 -080080#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardy535b57c2008-04-14 11:15:47 +020081 .range_to_nlattr = nf_nat_proto_range_to_nlattr,
82 .nlattr_to_range = nf_nat_proto_nlattr_to_range,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080083#endif
84};