1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
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.
9 #include <linux/types.h>
10 #include <linux/export.h>
11 #include <linux/init.h>
12 #include <linux/udp.h>
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>
20 static u16 udp_port_rover;
23 udp_unique_tuple(const struct nf_nat_l3proto *l3proto,
24 struct nf_conntrack_tuple *tuple,
25 const struct nf_nat_range2 *range,
26 enum nf_nat_manip_type maniptype,
27 const struct nf_conn *ct)
29 nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct,
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)
40 __be16 *portptr, newport;
42 if (maniptype == NF_NAT_MANIP_SRC) {
43 /* Get rid of src port */
44 newport = tuple->src.u.udp.port;
45 portptr = &hdr->source;
47 /* Get rid of dst port */
48 newport = tuple->dst.u.udp.port;
52 l3proto->csum_update(skb, iphdroff, &hdr->check,
54 inet_proto_csum_replace2(&hdr->check, skb, *portptr, newport,
57 hdr->check = CSUM_MANGLED_0;
62 static 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)
70 if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
73 hdr = (struct udphdr *)(skb->data + hdroff);
74 __udp_manip_pkt(skb, l3proto, iphdroff, hdr, tuple, maniptype,
80 #ifdef CONFIG_NF_NAT_PROTO_UDPLITE
81 static u16 udplite_port_rover;
83 static bool udplite_manip_pkt(struct sk_buff *skb,
84 const struct nf_nat_l3proto *l3proto,
85 unsigned int iphdroff, unsigned int hdroff,
86 const struct nf_conntrack_tuple *tuple,
87 enum nf_nat_manip_type maniptype)
91 if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
94 hdr = (struct udphdr *)(skb->data + hdroff);
95 __udp_manip_pkt(skb, l3proto, iphdroff, hdr, tuple, maniptype, true);
100 udplite_unique_tuple(const struct nf_nat_l3proto *l3proto,
101 struct nf_conntrack_tuple *tuple,
102 const struct nf_nat_range2 *range,
103 enum nf_nat_manip_type maniptype,
104 const struct nf_conn *ct)
106 nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct,
107 &udplite_port_rover);
110 const struct nf_nat_l4proto nf_nat_l4proto_udplite = {
111 .l4proto = IPPROTO_UDPLITE,
112 .manip_pkt = udplite_manip_pkt,
113 .in_range = nf_nat_l4proto_in_range,
114 .unique_tuple = udplite_unique_tuple,
115 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
116 .nlattr_to_range = nf_nat_l4proto_nlattr_to_range,
119 #endif /* CONFIG_NF_NAT_PROTO_UDPLITE */
121 const struct nf_nat_l4proto nf_nat_l4proto_udp = {
122 .l4proto = IPPROTO_UDP,
123 .manip_pkt = udp_manip_pkt,
124 .in_range = nf_nat_l4proto_in_range,
125 .unique_tuple = udp_unique_tuple,
126 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
127 .nlattr_to_range = nf_nat_l4proto_nlattr_to_range,