• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
10 #include <linux/export.h>
11 #include <linux/init.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_l3proto.h>
18 #include <net/netfilter/nf_nat_l4proto.h>
19 
20 static u16 udp_port_rover;
21 
22 static void
udp_unique_tuple(const struct nf_nat_l3proto * l3proto,struct nf_conntrack_tuple * tuple,const struct nf_nat_range * range,enum nf_nat_manip_type maniptype,const struct nf_conn * ct)23 udp_unique_tuple(const struct nf_nat_l3proto *l3proto,
24 		 struct nf_conntrack_tuple *tuple,
25 		 const struct nf_nat_range *range,
26 		 enum nf_nat_manip_type maniptype,
27 		 const struct nf_conn *ct)
28 {
29 	nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct,
30 				    &udp_port_rover);
31 }
32 
33 static bool
udp_manip_pkt(struct sk_buff * skb,const struct nf_nat_l3proto * l3proto,unsigned int iphdroff,unsigned int hdroff,const struct nf_conntrack_tuple * tuple,enum nf_nat_manip_type maniptype)34 udp_manip_pkt(struct sk_buff *skb,
35 	      const struct nf_nat_l3proto *l3proto,
36 	      unsigned int iphdroff, unsigned int hdroff,
37 	      const struct nf_conntrack_tuple *tuple,
38 	      enum nf_nat_manip_type maniptype)
39 {
40 	struct udphdr *hdr;
41 	__be16 *portptr, newport;
42 
43 	if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
44 		return false;
45 	hdr = (struct udphdr *)(skb->data + hdroff);
46 
47 	if (maniptype == NF_NAT_MANIP_SRC) {
48 		/* Get rid of src port */
49 		newport = tuple->src.u.udp.port;
50 		portptr = &hdr->source;
51 	} else {
52 		/* Get rid of dst port */
53 		newport = tuple->dst.u.udp.port;
54 		portptr = &hdr->dest;
55 	}
56 	if (hdr->check || skb->ip_summed == CHECKSUM_PARTIAL) {
57 		l3proto->csum_update(skb, iphdroff, &hdr->check,
58 				     tuple, maniptype);
59 		inet_proto_csum_replace2(&hdr->check, skb, *portptr, newport,
60 					 0);
61 		if (!hdr->check)
62 			hdr->check = CSUM_MANGLED_0;
63 	}
64 	*portptr = newport;
65 	return true;
66 }
67 
68 const struct nf_nat_l4proto nf_nat_l4proto_udp = {
69 	.l4proto		= IPPROTO_UDP,
70 	.manip_pkt		= udp_manip_pkt,
71 	.in_range		= nf_nat_l4proto_in_range,
72 	.unique_tuple		= udp_unique_tuple,
73 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
74 	.nlattr_to_range	= nf_nat_l4proto_nlattr_to_range,
75 #endif
76 };
77