blob: b1e627227b6e2670fb6ce9d151e8965a4c8731c3 [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>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040010#include <linux/export.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080011#include <linux/init.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080012#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 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
Patrick McHardyc7232c92012-08-26 19:14:06 +020020static u16 udp_port_rover;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080021
Changli Gaof43dc982010-08-02 17:20:54 +020022static void
Patrick McHardyc7232c92012-08-26 19:14:06 +020023udp_unique_tuple(const struct nf_nat_l3proto *l3proto,
24 struct nf_conntrack_tuple *tuple,
25 const struct nf_nat_range *range,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080026 enum nf_nat_manip_type maniptype,
27 const struct nf_conn *ct)
28{
Patrick McHardyc7232c92012-08-26 19:14:06 +020029 nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct,
30 &udp_port_rover);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080031}
32
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020033static bool
Herbert Xu3db05fe2007-10-15 00:53:15 -070034udp_manip_pkt(struct sk_buff *skb,
Patrick McHardyc7232c92012-08-26 19:14:06 +020035 const struct nf_nat_l3proto *l3proto,
36 unsigned int iphdroff, unsigned int hdroff,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080037 const struct nf_conntrack_tuple *tuple,
38 enum nf_nat_manip_type maniptype)
39{
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080040 struct udphdr *hdr;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080041 __be16 *portptr, newport;
42
Herbert Xu3db05fe2007-10-15 00:53:15 -070043 if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020044 return false;
Herbert Xu3db05fe2007-10-15 00:53:15 -070045 hdr = (struct udphdr *)(skb->data + hdroff);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080046
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010047 if (maniptype == NF_NAT_MANIP_SRC) {
Patrick McHardyc7232c92012-08-26 19:14:06 +020048 /* Get rid of src port */
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080049 newport = tuple->src.u.udp.port;
50 portptr = &hdr->source;
51 } else {
Patrick McHardyc7232c92012-08-26 19:14:06 +020052 /* Get rid of dst port */
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080053 newport = tuple->dst.u.udp.port;
54 portptr = &hdr->dest;
55 }
Herbert Xu3db05fe2007-10-15 00:53:15 -070056 if (hdr->check || skb->ip_summed == CHECKSUM_PARTIAL) {
Patrick McHardyc7232c92012-08-26 19:14:06 +020057 l3proto->csum_update(skb, iphdroff, &hdr->check,
58 tuple, maniptype);
Patrick McHardybe0ea7d2007-11-30 01:17:11 +110059 inet_proto_csum_replace2(&hdr->check, skb, *portptr, newport,
Tom Herbert4b048d62015-08-17 13:42:25 -070060 false);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080061 if (!hdr->check)
62 hdr->check = CSUM_MANGLED_0;
63 }
64 *portptr = newport;
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020065 return true;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080066}
67
Patrick McHardyc7232c92012-08-26 19:14:06 +020068const struct nf_nat_l4proto nf_nat_l4proto_udp = {
69 .l4proto = IPPROTO_UDP,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080070 .manip_pkt = udp_manip_pkt,
Patrick McHardyc7232c92012-08-26 19:14:06 +020071 .in_range = nf_nat_l4proto_in_range,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080072 .unique_tuple = udp_unique_tuple,
Duan Jiong24de3d32014-06-30 09:19:32 +080073#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Patrick McHardyc7232c92012-08-26 19:14:06 +020074 .nlattr_to_range = nf_nat_l4proto_nlattr_to_range,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080075#endif
76};