1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/*
* Copyright (c) 2020 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* @brief NAT port/address allocation lib
*/
#include <nat/lib/alloc.h>
static_always_inline void
nat_ip4_addr_increment (ip4_address_t * addr)
{
u32 v;
v = clib_net_to_host_u32 (addr->as_u32) + 1;
addr->as_u32 = clib_host_to_net_u32 (v);
}
int
nat_add_del_ip4_pool_addr (nat_ip4_pool_t * pool, ip4_address_t addr,
u8 is_add)
{
int i;
nat_ip4_pool_addr_t *a = 0;
vlib_thread_main_t *tm = vlib_get_thread_main ();
// lookup for the address
for (i = 0; i < vec_len (pool); i++)
{
if (pool->pool_addr[i].addr.as_u32 == addr.as_u32)
{
a = pool->pool_addr + 1;
break;
}
}
if (is_add)
{
if (a)
return NAT_ERROR_VALUE_EXIST;
vec_add2 (pool->pool_addr, a, 1);
a->addr = addr;
#define _(N, i, n, s) \
clib_bitmap_alloc (a->busy_##n##_port_bitmap, 65535); \
a->busy_##n##_ports = 0; \
vec_validate_init_empty (a->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
foreach_nat_protocol
#undef _
}
else
{
if (!a)
return NAT_ERROR_NO_SUCH_ENTRY;
#define _(N, id, n, s) \
clib_bitmap_free (a->busy_##n##_port_bitmap); \
vec_free (a->busy_##n##_ports_per_thread);
foreach_nat_protocol
#undef _
vec_del1 (pool->pool_addr, i);
}
return 0;
}
int
nat_add_del_ip4_pool_addrs (nat_ip4_pool_t * pool,
ip4_address_t addr, u32 count, u8 is_add,
void *opaque)
{
int i, rv;
for (i = 0; i < count; i++)
{
// TODO:
// a) consider if we could benefit from pre and post cb
// b) consider if we could benefit from add/del cb separation
// pre call:
// pool->add_del_pool_addr_pre_cb (&addr, is_add, opaque);
if ((rv = nat_add_del_ip4_pool_addr (pool, addr, is_add)) != 0)
return rv;
// post call:
// pool->add_del_pool_addr_post_cb (&addr, is_add, opaque);
pool->add_del_pool_addr_cb (addr, is_add, opaque);
nat_ip4_addr_increment (&addr);
}
return 0;
}
static_always_inline u16
nat_random_port (u32 random_seed, u16 min, u16 max)
{
return min + random_u32 (&random_seed) /
(random_u32_max () / (max - min + 1) + 1);
}
int
nat_alloc_ip4_addr_and_port_cb_default (nat_ip4_pool_t * pool,
u32 fib_index,
u32 thread_index,
u32 nat_thread_index,
u16 port_per_thread,
u16 protocol,
nat_ip4_addr_port_t * out)
{
nat_ip4_pool_addr_t *a, *ga = 0;
u32 i;
u32 portnum;
for (i = 0; i < vec_len (pool->pool_addr); i++)
{
a = pool->pool_addr + i;
switch (protocol)
{
#define _(N, j, n, s) \
case NAT_PROTOCOL_##N: \
if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
{ \
if (a->fib_index == fib_index) \
{ \
while (1) \
{ \
portnum = (port_per_thread * \
nat_thread_index) + \
nat_random_port(pool->random_seed, 1, port_per_thread) + 1024; \
if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
continue; \
clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
a->busy_##n##_ports_per_thread[thread_index]++; \
a->busy_##n##_ports++; \
out->addr = a->addr; \
out->port = clib_host_to_net_u16(portnum); \
return 0; \
} \
} \
else if (a->fib_index == ~0) \
{ \
ga = a; \
} \
} \
break;
foreach_nat_protocol
#undef _
default:
return NAT_ERROR_UNKNOWN_PROTOCOL;
}
}
if (ga)
{
a = ga;
switch (protocol)
{
#define _(N, j, n, s) \
case NAT_PROTOCOL_##N: \
while (1) \
{ \
portnum = (port_per_thread * \
nat_thread_index) + \
nat_random_port(pool->random_seed, 1, port_per_thread) + 1024; \
if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
continue; \
clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
a->busy_##n##_ports_per_thread[thread_index]++; \
a->busy_##n##_ports++; \
out->addr = a->addr; \
out->port = clib_host_to_net_u16(portnum); \
return 0; \
}
break;
foreach_nat_protocol
#undef _
default:
return NAT_ERROR_UNKNOWN_PROTOCOL;
}
}
return NAT_ERROR_OUT_OF_TRANSLATIONS;
}
int
nat_alloc_ip4_addr_and_port (nat_ip4_pool_t * pool,
u32 fib_index,
u32 thread_index,
u32 nat_thread_index,
u16 port_per_thread,
u16 protocol, nat_ip4_addr_port_t * out)
{
return pool->alloc_addr_and_port_cb (pool,
fib_index,
thread_index,
nat_thread_index,
port_per_thread, protocol, out);
}
// TODO: consider using standard u16 port and ip4_address_t as input ?
int
nat_free_ip4_addr_and_port (nat_ip4_pool_t * pool,
u32 thread_index,
u16 protocol, nat_ip4_addr_port_t * addr_port)
{
nat_ip4_pool_addr_t *a = 0;
u32 i;
u16 port = clib_net_to_host_u16 (addr_port->port);
for (i = 0; i < vec_len (pool->pool_addr); i++)
{
if (pool->pool_addr[i].addr.as_u32 == addr_port->addr.as_u32)
{
a = pool->pool_addr + i;
break;
}
}
if (!a)
{
return NAT_ERROR_NO_SUCH_ENTRY;
}
switch (protocol)
{
#define _(N, i, n, s) \
case NAT_PROTOCOL_##N: \
ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
port) == 1); \
clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
port, 0); \
a->busy_##n##_ports--; \
a->busy_##n##_ports_per_thread[thread_index]--; \
break;
foreach_nat_protocol
#undef _
default:
return NAT_ERROR_UNKNOWN_PROTOCOL;
}
return 0;
}
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/
|