GNU Linux-libre 4.9.337-gnu1
[releases.git] / net / netfilter / nf_nat_proto_udplite.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
3  * (C) 2008 Patrick McHardy <kaber@trash.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/types.h>
11 #include <linux/init.h>
12 #include <linux/udp.h>
13
14 #include <linux/netfilter.h>
15 #include <linux/module.h>
16 #include <net/netfilter/nf_nat.h>
17 #include <net/netfilter/nf_nat_l3proto.h>
18 #include <net/netfilter/nf_nat_l4proto.h>
19
20 static void
21 udplite_unique_tuple(const struct nf_nat_l3proto *l3proto,
22                      struct nf_conntrack_tuple *tuple,
23                      const struct nf_nat_range *range,
24                      enum nf_nat_manip_type maniptype,
25                      const struct nf_conn *ct)
26 {
27         nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct);
28 }
29
30 static bool
31 udplite_manip_pkt(struct sk_buff *skb,
32                   const struct nf_nat_l3proto *l3proto,
33                   unsigned int iphdroff, unsigned int hdroff,
34                   const struct nf_conntrack_tuple *tuple,
35                   enum nf_nat_manip_type maniptype)
36 {
37         struct udphdr *hdr;
38         __be16 *portptr, newport;
39
40         if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
41                 return false;
42
43         hdr = (struct udphdr *)(skb->data + hdroff);
44
45         if (maniptype == NF_NAT_MANIP_SRC) {
46                 /* Get rid of source port */
47                 newport = tuple->src.u.udp.port;
48                 portptr = &hdr->source;
49         } else {
50                 /* Get rid of dst port */
51                 newport = tuple->dst.u.udp.port;
52                 portptr = &hdr->dest;
53         }
54
55         l3proto->csum_update(skb, iphdroff, &hdr->check, tuple, maniptype);
56         inet_proto_csum_replace2(&hdr->check, skb, *portptr, newport, false);
57         if (!hdr->check)
58                 hdr->check = CSUM_MANGLED_0;
59
60         *portptr = newport;
61         return true;
62 }
63
64 static const struct nf_nat_l4proto nf_nat_l4proto_udplite = {
65         .l4proto                = IPPROTO_UDPLITE,
66         .manip_pkt              = udplite_manip_pkt,
67         .in_range               = nf_nat_l4proto_in_range,
68         .unique_tuple           = udplite_unique_tuple,
69 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
70         .nlattr_to_range        = nf_nat_l4proto_nlattr_to_range,
71 #endif
72 };
73
74 static int __init nf_nat_proto_udplite_init(void)
75 {
76         int err;
77
78         err = nf_nat_l4proto_register(NFPROTO_IPV4, &nf_nat_l4proto_udplite);
79         if (err < 0)
80                 goto err1;
81         err = nf_nat_l4proto_register(NFPROTO_IPV6, &nf_nat_l4proto_udplite);
82         if (err < 0)
83                 goto err2;
84         return 0;
85
86 err2:
87         nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_udplite);
88 err1:
89         return err;
90 }
91
92 static void __exit nf_nat_proto_udplite_fini(void)
93 {
94         nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_udplite);
95         nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_udplite);
96 }
97
98 module_init(nf_nat_proto_udplite_init);
99 module_exit(nf_nat_proto_udplite_fini);
100
101 MODULE_LICENSE("GPL");
102 MODULE_DESCRIPTION("UDP-Lite NAT protocol helper");
103 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");