FD.io VPP  v21.01.1
Vector Packet Processing
nat64_db.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef __included_nat64_db_h__
16 #define __included_nat64_db_h__
17 
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/fib/fib_source.h>
21 
22 #include <vppinfra/bihash_24_8.h>
23 #include <vppinfra/bihash_48_8.h>
24 
25 typedef struct
26 {
32 
33 typedef struct
34 {
35  union
36  {
37  struct
38  {
39  ip46_address_t addr;
44  };
45  u64 as_u64[3];
46  };
48 
49 /* *INDENT-OFF* */
50 typedef CLIB_PACKED(struct
51 {
52  ip6_address_t in_addr;
53  u16 in_port;
54  ip4_address_t out_addr;
55  u16 out_port;
56  u32 fib_index;
57  u32 ses_num;
58  u8 proto;
59  u8 is_static;
60 }) nat64_db_bib_entry_t;
61 /* *INDENT-ON* */
62 
63 typedef struct
64 {
65  /* BIBs */
66 /* *INDENT-OFF* */
67 #define _(N, i, n, s) \
68  nat64_db_bib_entry_t *_##n##_bib;
70 #undef _
71 /* *INDENT-ON* */
72  nat64_db_bib_entry_t *_unk_proto_bib;
73 
74  /* BIB lookup */
75  clib_bihash_24_8_t in2out;
76  clib_bihash_24_8_t out2in;
77 
81 
82 typedef struct
83 {
84  union
85  {
86  struct
87  {
88  ip46_address_t l_addr;
89  ip46_address_t r_addr;
94  u8 rsvd[7];
95  };
96  u64 as_u64[6];
97  };
99 
100 /* *INDENT-OFF* */
101 typedef CLIB_PACKED(struct
102 {
103  ip6_address_t in_r_addr;
104  ip4_address_t out_r_addr;
105  u16 r_port;
106  u32 bibe_index;
107  u32 expire;
108  u8 proto;
109  u8 tcp_state;
110 }) nat64_db_st_entry_t;
111 /* *INDENT-ON* */
112 
113 typedef struct
114 {
115  /* session tables */
116 /* *INDENT-OFF* */
117 #define _(N, i, n, s) \
118  nat64_db_st_entry_t *_##n##_st;
120 #undef _
121 /* *INDENT-ON* */
122  nat64_db_st_entry_t *_unk_proto_st;
123 
124  /* session lookup */
125  clib_bihash_48_8_t in2out;
126  clib_bihash_48_8_t out2in;
127 
130 } nat64_db_st_t;
131 
132 struct nat64_db_s;
133 
134 /**
135  * @brief Call back function to free NAT64 pool address and port when BIB
136  * entry is deleted.
137  */
138 typedef void (*nat64_db_free_addr_port_function_t) (struct nat64_db_s * db,
140  u16 port, u8 proto);
141 
142 typedef struct nat64_db_s
143 {
148 } nat64_db_t;
149 
150 /**
151  * @brief Initialize NAT64 DB.
152  *
153  * @param db NAT64 DB.
154  * @param c.bib_buckets Number of BIB hash buckets.
155  * @param c.bib_memory_size Memory size of BIB hash.
156  * @param c.st_buckets Number of session table hash buckets.
157  * @param c.st_memory_size Memory size of session table hash.
158  * @param free_addr_port_cb Call back function to free address and port.
159  *
160  * @returns 0 on success, non-zero value otherwise.
161  */
164 
165 /**
166  * @brief Free NAT64 DB.
167  *
168  * @param db NAT64 DB.
169  *
170  * @returns 0 on success, non-zero value otherwise.
171  */
172 int nat64_db_free (nat64_db_t * db);
173 
174 
175 /**
176  * @brief Create new NAT64 BIB entry.
177  *
178  * @param thread_index thread index.
179  * @param db NAT64 DB.
180  * @param in_addr Inside IPv6 address.
181  * @param out_addr Outside IPv4 address.
182  * @param in_port Inside port number.
183  * @param out_port Outside port number.
184  * @param fib_index FIB index.
185  * @param proto L4 protocol.
186  * @param is_static 1 if static, 0 if dynamic.
187  *
188  * @returns BIB entry on success, 0 otherwise.
189  */
190 nat64_db_bib_entry_t *nat64_db_bib_entry_create (u32 thread_index,
191  nat64_db_t * db,
192  ip6_address_t * in_addr,
193  ip4_address_t * out_addr,
194  u16 in_port, u16 out_port,
195  u32 fib_index, u8 proto,
196  u8 is_static);
197 
198 /**
199  * @brief Free NAT64 BIB entry.
200  *
201  * @param thread_index thread index.
202  * @param db NAT64 DB.
203  * @param bibe BIB entry.
204  */
205 void nat64_db_bib_entry_free (u32 thread_index, nat64_db_t * db,
206  nat64_db_bib_entry_t * bibe);
207 
208 /**
209  * @brief Call back function when walking NAT64 BIB, non-zero
210  * return value stop walk.
211  */
212 typedef int (*nat64_db_bib_walk_fn_t) (nat64_db_bib_entry_t * bibe,
213  void *ctx);
214 /**
215  * @brief Walk NAT64 BIB.
216  *
217  * @param db NAT64 DB.
218  * @param proto BIB L4 protocol:
219  * - 255 all BIBs
220  * - 6 TCP BIB
221  * - 17 UDP BIB
222  * - 1/58 ICMP BIB
223  *
224  * u - otherwise "unknown" protocol BIB
225  * @param fn The function to invoke on each entry visited.
226  * @param ctx A context passed in the visit function.
227  */
229  nat64_db_bib_walk_fn_t fn, void *ctx);
230 
231 /**
232  * @brief Find NAT64 BIB entry.
233  *
234  * @param db NAT64 DB.
235  * @param addr IP address.
236  * @param port Port number.
237  * @param proto L4 protocol.
238  * @param fib_index FIB index.
239  * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
240  *
241  * @return BIB entry if found.
242  */
243 nat64_db_bib_entry_t *nat64_db_bib_entry_find (nat64_db_t * db,
244  ip46_address_t * addr,
245  u16 port,
246  u8 proto,
247  u32 fib_index, u8 is_ip6);
248 
249 /**
250  * @brief Get BIB entry by index and protocol.
251  *
252  * @param db NAT64 DB.
253  * @param proto L4 protocol.
254  * @param bibe_index BIB entry index.
255  *
256  * @return BIB entry if found.
257  */
258 nat64_db_bib_entry_t *nat64_db_bib_entry_by_index (nat64_db_t * db,
259  u8 proto, u32 bibe_index);
260 /**
261  * @brief Create new NAT64 session table entry.
262  *
263  * @param thread_index thread index.
264  * @param db NAT64 DB.
265  * @param bibe Corresponding BIB entry.
266  * @param in_r_addr Inside IPv6 address of the remote host.
267  * @param out_r_addr Outside IPv4 address of the remote host.
268  * @param r_port Remote host port number.
269  *
270  * @returns BIB entry on success, 0 otherwise.
271  */
272 nat64_db_st_entry_t *nat64_db_st_entry_create (u32 thread_index,
273  nat64_db_t * db,
274  nat64_db_bib_entry_t * bibe,
275  ip6_address_t * in_r_addr,
276  ip4_address_t * out_r_addr,
277  u16 r_port);
278 
279 /**
280  * @brief Free NAT64 session table entry.
281  *
282  * @param thread_index thread index.
283  * @param db NAT64 DB.
284  * @param ste Session table entry.
285  */
286 void nat64_db_st_entry_free (u32 thread_index, nat64_db_t * db,
287  nat64_db_st_entry_t * ste);
288 
289 /**
290  * @brief Find NAT64 session table entry.
291  *
292  * @param db NAT64 DB.
293  * @param l_addr Local host address.
294  * @param r_addr Remote host address.
295  * @param l_port Local host port number.
296  * @param r_port Remote host port number.
297  * @param proto L4 protocol.
298  * @param fib_index FIB index.
299  * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
300  *
301  * @return BIB entry if found.
302  */
303 nat64_db_st_entry_t *nat64_db_st_entry_find (nat64_db_t * db,
304  ip46_address_t * l_addr,
305  ip46_address_t * r_addr,
306  u16 l_port, u16 r_port,
307  u8 proto,
308  u32 fib_index, u8 is_ip6);
309 
310 /**
311  * @brief Call back function when walking NAT64 session table, non-zero
312  * return value stop walk.
313  */
314 typedef int (*nat64_db_st_walk_fn_t) (nat64_db_st_entry_t * ste, void *ctx);
315 
316 /**
317  * @brief Walk NAT64 session table.
318  *
319  * @param db NAT64 DB.
320  * @param proto L4 protocol:
321  * - 255 all session tables
322  * - 6 TCP session table
323  * - 17 UDP session table
324  * - 1/58 ICMP session table
325  * - otherwise "unknown" protocol session table
326  * @param fn The function to invoke on each entry visited.
327  * @param ctx A context passed in the visit function.
328  */
329 void nat64_db_st_walk (nat64_db_t * db, u8 proto,
330  nat64_db_st_walk_fn_t fn, void *ctx);
331 
332 /**
333  * @brief Free expired session entries in session tables.
334  *
335  * @param thread_index thread index.
336  * @param db NAT64 DB.
337  * @param now Current time.
338  */
339 void nad64_db_st_free_expired (u32 thread_index, nat64_db_t * db, u32 now);
340 
341 /**
342  * @brief Free sessions using specific outside address.
343  *
344  * @param thread_index thread index.
345  * @param db NAT64 DB.
346  * @param out_addr Outside address to match.
347  */
348 void nat64_db_free_out_addr (u32 thread_index, nat64_db_t * db,
349  ip4_address_t * out_addr);
350 
351 /*
352  * @brief Get ST entry index.
353  *
354  * @param db NAT64 DB.
355  * @param ste ST entry.
356  *
357  * @return ST entry index on success, ~0 otherwise.
358  */
359 u32 nat64_db_st_entry_get_index (nat64_db_t * db, nat64_db_st_entry_t * ste);
360 
361 /**
362  * @brief Get ST entry by index and protocol.
363  *
364  * @param db NAT64 DB.
365  * @param proto L4 protocol.
366  * @param bibe_index ST entry index.
367  *
368  * @return BIB entry if found.
369  */
370 nat64_db_st_entry_t *nat64_db_st_entry_by_index (nat64_db_t * db,
371  u8 proto, u32 ste_index);
372 #endif /* __included_nat64_db_h__ */
373 
374 /*
375  * fd.io coding-style-patch-verification: ON
376  *
377  * Local Variables:
378  * eval: (c-set-style "gnu")
379  * End:
380  */
Definition: nat64_db.h:82
nat64_db_free_addr_port_function_t free_addr_port_cb
Definition: nat64_db.h:146
typedef CLIB_PACKED(struct { ip6_address_t in_addr;u16 in_port;ip4_address_t out_addr;u16 out_port;u32 fib_index;u32 ses_num;u8 proto;u8 is_static;}) nat64_db_bib_entry_t
u16 l_port
Definition: nat64_db.h:91
nat64_db_st_entry_t * nat64_db_st_entry_find(nat64_db_t *db, ip46_address_t *l_addr, ip46_address_t *r_addr, u16 l_port, u16 r_port, u8 proto, u32 fib_index, u8 is_ip6)
Find NAT64 session table entry.
Definition: nat64_db.c:571
u64 as_u64
Definition: bihash_doc.h:63
unsigned long u64
Definition: types.h:89
u32 st_buckets
Definition: nat64_db.h:29
Definition: nat64_db.h:33
nat64_db_st_entry_t * nat64_db_st_entry_by_index(nat64_db_t *db, u8 proto, u32 ste_index)
Get ST entry by index and protocol.
Definition: nat64_db.c:642
u32 fib_index
Definition: nat64_db.h:90
void nad64_db_st_free_expired(u32 thread_index, nat64_db_t *db, u32 now)
Free expired session entries in session tables.
Definition: nat64_db.c:665
nat64_db_bib_t bib
Definition: nat64_db.h:144
u32 st_memory_size
Definition: nat64_db.h:30
vhost_vring_addr_t addr
Definition: vhost_user.h:111
unsigned char u8
Definition: types.h:56
int(* nat64_db_bib_walk_fn_t)(nat64_db_bib_entry_t *bibe, void *ctx)
Call back function when walking NAT64 BIB, non-zero return value stop walk.
Definition: nat64_db.h:212
u32 st_entries_num
Definition: nat64_db.h:129
ip46_address_t l_addr
Definition: nat64_db.h:88
u16 r_port
Definition: nat64_db.h:92
clib_bihash_48_8_t in2out
Definition: nat64_db.h:125
u16 port
Definition: nat64_db.h:41
unsigned int u32
Definition: types.h:88
bool is_ip6
Definition: ip.api:43
nat64_db_bib_entry_t * nat64_db_bib_entry_find(nat64_db_t *db, ip46_address_t *addr, u16 port, u8 proto, u32 fib_index, u8 is_ip6)
Find NAT64 BIB entry.
Definition: nat64_db.c:225
clib_bihash_24_8_t in2out
Definition: nat64_db.h:75
vl_api_ip_proto_t proto
Definition: acl_types.api:51
long ctx[MAX_CONNS]
Definition: main.c:144
void nat64_db_bib_entry_free(u32 thread_index, nat64_db_t *db, nat64_db_bib_entry_t *bibe)
Free NAT64 BIB entry.
Definition: nat64_db.c:147
unsigned short u16
Definition: types.h:57
nat64_db_st_t st
Definition: nat64_db.h:145
u8 addr_free
Definition: nat64_db.h:147
nat64_db_bib_entry_t * nat64_db_bib_entry_create(u32 thread_index, nat64_db_t *db, ip6_address_t *in_addr, ip4_address_t *out_addr, u16 in_port, u16 out_port, u32 fib_index, u8 proto, u8 is_static)
Create new NAT64 BIB entry.
Definition: nat64_db.c:72
void nat64_db_st_walk(nat64_db_t *db, u8 proto, nat64_db_st_walk_fn_t fn, void *ctx)
Walk NAT64 session table.
Definition: nat64_db.c:341
u32 bib_buckets
Definition: nat64_db.h:27
svmdb_client_t * c
ip46_address_t addr
Definition: nat64_db.h:39
void nat64_db_free_out_addr(u32 thread_index, nat64_db_t *db, ip4_address_t *out_addr)
Free sessions using specific outside address.
Definition: nat64_db.c:699
void(* nat64_db_free_addr_port_function_t)(struct nat64_db_s *db, ip4_address_t *addr, u16 port, u8 proto)
Call back function to free NAT64 pool address and port when BIB entry is deleted. ...
Definition: nat64_db.h:138
u8 proto
Definition: nat64_db.h:93
int nat64_db_init(nat64_db_t *db, nat64_config_t c, nat64_db_free_addr_port_function_t free_addr_port_cb)
Initialize NAT64 DB.
Definition: nat64_db.c:23
u32 fib_index
Definition: nat64_db.h:40
u32 bib_memory_size
Definition: nat64_db.h:28
void nat64_db_st_entry_free(u32 thread_index, nat64_db_t *db, nat64_db_st_entry_t *ste)
Free NAT64 session table entry.
Definition: nat64_db.c:487
u32 bib_entries_num
Definition: nat64_db.h:79
u8 proto
Definition: nat64_db.h:42
clib_bihash_24_8_t out2in
Definition: nat64_db.h:76
u32 nat64_db_st_entry_get_index(nat64_db_t *db, nat64_db_st_entry_t *ste)
Definition: nat64_db.c:619
u16 port
Definition: lb_types.api:73
int nat64_db_free(nat64_db_t *db)
Free NAT64 DB.
Definition: nat64_db.c:49
int(* nat64_db_st_walk_fn_t)(nat64_db_st_entry_t *ste, void *ctx)
Call back function when walking NAT64 session table, non-zero return value stop walk.
Definition: nat64_db.h:314
nat64_db_st_entry_t * nat64_db_st_entry_create(u32 thread_index, nat64_db_t *db, nat64_db_bib_entry_t *bibe, ip6_address_t *in_r_addr, ip4_address_t *out_r_addr, u16 r_port)
Create new NAT64 session table entry.
Definition: nat64_db.c:392
u8 rsvd
Definition: nat64_db.h:43
nat64_db_bib_entry_t * nat64_db_bib_entry_by_index(nat64_db_t *db, u8 proto, u32 bibe_index)
Get BIB entry by index and protocol.
Definition: nat64_db.c:318
ip46_address_t r_addr
Definition: nat64_db.h:89
struct nat64_db_s nat64_db_t
clib_bihash_48_8_t out2in
Definition: nat64_db.h:126
void nat64_db_bib_walk(nat64_db_t *db, u8 proto, nat64_db_bib_walk_fn_t fn, void *ctx)
Walk NAT64 BIB.
Definition: nat64_db.c:267