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