]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/nf_nat_proto_common.c
x86/msr-index: Cleanup bit defines
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / nf_nat_proto_common.c
CommitLineData
937e0dfd
PM
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/random.h>
937e0dfd 12#include <linux/netfilter.h>
bc3b2d7f 13#include <linux/export.h>
c7232c99 14
937e0dfd
PM
15#include <net/netfilter/nf_nat.h>
16#include <net/netfilter/nf_nat_core.h>
c7232c99
PM
17#include <net/netfilter/nf_nat_l3proto.h>
18#include <net/netfilter/nf_nat_l4proto.h>
937e0dfd 19
c7232c99
PM
20bool nf_nat_l4proto_in_range(const struct nf_conntrack_tuple *tuple,
21 enum nf_nat_manip_type maniptype,
22 const union nf_conntrack_man_proto *min,
23 const union nf_conntrack_man_proto *max)
937e0dfd
PM
24{
25 __be16 port;
26
cbc9f2f4 27 if (maniptype == NF_NAT_MANIP_SRC)
937e0dfd
PM
28 port = tuple->src.u.all;
29 else
30 port = tuple->dst.u.all;
31
32 return ntohs(port) >= ntohs(min->all) &&
33 ntohs(port) <= ntohs(max->all);
34}
c7232c99 35EXPORT_SYMBOL_GPL(nf_nat_l4proto_in_range);
937e0dfd 36
c7232c99
PM
37void nf_nat_l4proto_unique_tuple(const struct nf_nat_l3proto *l3proto,
38 struct nf_conntrack_tuple *tuple,
39 const struct nf_nat_range *range,
40 enum nf_nat_manip_type maniptype,
41 const struct nf_conn *ct,
42 u16 *rover)
937e0dfd 43{
9bfb78a8 44 unsigned int range_size, min, max, i;
937e0dfd 45 __be16 *portptr;
5abd363f 46 u_int16_t off;
937e0dfd 47
cbc9f2f4 48 if (maniptype == NF_NAT_MANIP_SRC)
937e0dfd
PM
49 portptr = &tuple->src.u.all;
50 else
51 portptr = &tuple->dst.u.all;
52
53 /* If no range specified... */
cbc9f2f4 54 if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
937e0dfd 55 /* If it's dst rewrite, can't change port */
cbc9f2f4 56 if (maniptype == NF_NAT_MANIP_DST)
f43dc98b 57 return;
937e0dfd
PM
58
59 if (ntohs(*portptr) < 1024) {
60 /* Loose convention: >> 512 is credential passing */
61 if (ntohs(*portptr) < 512) {
62 min = 1;
63 range_size = 511 - min + 1;
64 } else {
65 min = 600;
66 range_size = 1023 - min + 1;
67 }
68 } else {
69 min = 1024;
70 range_size = 65535 - 1024 + 1;
71 }
72 } else {
c7232c99 73 min = ntohs(range->min_proto.all);
9bfb78a8
PA
74 max = ntohs(range->max_proto.all);
75 if (unlikely(max < min))
76 swap(max, min);
77 range_size = max - min + 1;
937e0dfd
PM
78 }
79
34ce3240 80 if (range->flags & NF_NAT_RANGE_PROTO_RANDOM) {
c7232c99
PM
81 off = l3proto->secure_port(tuple, maniptype == NF_NAT_MANIP_SRC
82 ? tuple->dst.u.all
83 : tuple->src.u.all);
34ce3240
DB
84 } else if (range->flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY) {
85 off = prandom_u32();
86 } else {
9f593653 87 off = *rover;
34ce3240 88 }
937e0dfd 89
2452a99d 90 for (i = 0; ; ++off) {
5abd363f 91 *portptr = htons(min + off % range_size);
2452a99d 92 if (++i != range_size && nf_nat_used_tuple(tuple, ct))
5abd363f 93 continue;
34ce3240 94 if (!(range->flags & NF_NAT_RANGE_PROTO_RANDOM_ALL))
5abd363f 95 *rover = off;
f43dc98b 96 return;
937e0dfd 97 }
937e0dfd 98}
c7232c99 99EXPORT_SYMBOL_GPL(nf_nat_l4proto_unique_tuple);
535b57c7 100
24de3d37 101#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
c7232c99
PM
102int nf_nat_l4proto_nlattr_to_range(struct nlattr *tb[],
103 struct nf_nat_range *range)
535b57c7 104{
535b57c7 105 if (tb[CTA_PROTONAT_PORT_MIN]) {
c7232c99
PM
106 range->min_proto.all = nla_get_be16(tb[CTA_PROTONAT_PORT_MIN]);
107 range->max_proto.all = range->min_proto.all;
cbc9f2f4 108 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
535b57c7 109 }
ca6a5074 110 if (tb[CTA_PROTONAT_PORT_MAX]) {
c7232c99 111 range->max_proto.all = nla_get_be16(tb[CTA_PROTONAT_PORT_MAX]);
cbc9f2f4 112 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
535b57c7 113 }
ca6a5074 114 return 0;
535b57c7 115}
c7232c99 116EXPORT_SYMBOL_GPL(nf_nat_l4proto_nlattr_to_range);
535b57c7 117#endif