FD.io VPP  v18.01.2-1-g9b554f3
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;
72 
73 typedef struct
74 {
75  union
76  {
77  struct
78  {
79  ip46_address_t l_addr;
80  ip46_address_t r_addr;
85  u8 rsvd[7];
86  };
87  u64 as_u64[6];
88  };
90 
91 /* *INDENT-OFF* */
92 typedef CLIB_PACKED(struct
93 {
94  ip6_address_t in_r_addr;
95  ip4_address_t out_r_addr;
96  u16 r_port;
97  u32 bibe_index;
98  u32 expire;
99  u8 proto;
100  u8 tcp_state;
101 }) nat64_db_st_entry_t;
102 /* *INDENT-ON* */
103 
104 typedef struct
105 {
106  /* session tables */
107 /* *INDENT-OFF* */
108 #define _(N, i, n, s) \
109  nat64_db_st_entry_t *_##n##_st;
111 #undef _
112 /* *INDENT-ON* */
113  nat64_db_st_entry_t *_unk_proto_st;
114 
115  /* session lookup */
116  clib_bihash_48_8_t in2out;
117  clib_bihash_48_8_t out2in;
118 } nat64_db_st_t;
119 
120 struct nat64_db_s;
121 
122 /**
123  * @brief Call back function to free NAT64 pool address and port when BIB
124  * entry is deleted.
125  */
126 typedef void (*nat64_db_free_addr_port_function_t) (struct nat64_db_s * db,
128  u16 port, u8 proto);
129 
130 typedef struct nat64_db_s
131 {
135 } nat64_db_t;
136 
137 /**
138  * @brief Initialize NAT64 DB.
139  *
140  * @param db NAT64 DB.
141  * @param bib_buckets Number of BIB hash buckets.
142  * @param bib_memory_size Memory size of BIB hash.
143  * @param st_buckets Number of session table hash buckets.
144  * @param st_memory_size Memory size of session table hash.
145  * @param free_addr_port_cb Call back function to free address and port.
146  *
147  * @returns 0 on success, non-zero value otherwise.
148  */
149 int nat64_db_init (nat64_db_t * db, u32 bib_buckets, u32 bib_memory_size,
150  u32 st_buckets, u32 st_memory_size,
152 
153 /**
154  * @brief Create new NAT64 BIB entry.
155  *
156  * @param db NAT64 DB.
157  * @param in_addr Inside IPv6 address.
158  * @param out_addr Outside IPv4 address.
159  * @param in_port Inside port number.
160  * @param out_port Outside port number.
161  * @param fib_index FIB index.
162  * @param proto L4 protocol.
163  * @param is_static 1 if static, 0 if dynamic.
164  *
165  * @returns BIB entry on success, 0 otherwise.
166  */
167 nat64_db_bib_entry_t *nat64_db_bib_entry_create (nat64_db_t * db,
168  ip6_address_t * in_addr,
169  ip4_address_t * out_addr,
170  u16 in_port, u16 out_port,
171  u32 fib_index,
172  u8 proto, u8 is_static);
173 
174 /**
175  * @brief Free NAT64 BIB entry.
176  *
177  * @param db NAT64 DB.
178  * @param bibe BIB entry.
179  */
180 void nat64_db_bib_entry_free (nat64_db_t * db, nat64_db_bib_entry_t * bibe);
181 
182 /**
183  * @brief Call back function when walking NAT64 BIB, non-zero
184  * return value stop walk.
185  */
186 typedef int (*nat64_db_bib_walk_fn_t) (nat64_db_bib_entry_t * bibe,
187  void *ctx);
188 /**
189  * @brief Walk NAT64 BIB.
190  *
191  * @param db NAT64 DB.
192  * @param proto BIB L4 protocol:
193  * - 255 all BIBs
194  * - 6 TCP BIB
195  * - 17 UDP BIB
196  * - 1/58 ICMP BIB
197  * - otherwise "unknown" protocol BIB
198  * @param fn The function to invoke on each entry visited.
199  * @param ctx A context passed in the visit function.
200  */
201 void nat64_db_bib_walk (nat64_db_t * db, u8 proto,
202  nat64_db_bib_walk_fn_t fn, void *ctx);
203 
204 /**
205  * @brief Find NAT64 BIB entry.
206  *
207  * @param db NAT64 DB.
208  * @param addr IP address.
209  * @param port Port number.
210  * @param proto L4 protocol.
211  * @param fib_index FIB index.
212  * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
213  *
214  * @return BIB entry if found.
215  */
216 nat64_db_bib_entry_t *nat64_db_bib_entry_find (nat64_db_t * db,
217  ip46_address_t * addr,
218  u16 port,
219  u8 proto,
220  u32 fib_index, u8 is_ip6);
221 
222 /**
223  * @brief Get BIB entry by index and protocol.
224  *
225  * @param db NAT64 DB.
226  * @param proto L4 protocol.
227  * @param bibe_index BIB entry index.
228  *
229  * @return BIB entry if found.
230  */
231 nat64_db_bib_entry_t *nat64_db_bib_entry_by_index (nat64_db_t * db,
232  u8 proto, u32 bibe_index);
233 /**
234  * @brief Create new NAT64 session table entry.
235  *
236  * @param db NAT64 DB.
237  * @param bibe Corresponding BIB entry.
238  * @param in_r_addr Inside IPv6 address of the remote host.
239  * @param out_r_addr Outside IPv4 address of the remote host.
240  * @param r_port Remote host port number.
241  *
242  * @returns BIB entry on success, 0 otherwise.
243  */
244 nat64_db_st_entry_t *nat64_db_st_entry_create (nat64_db_t * db,
245  nat64_db_bib_entry_t * bibe,
246  ip6_address_t * in_r_addr,
247  ip4_address_t * out_r_addr,
248  u16 r_port);
249 
250 /**
251  * @brief Free NAT64 session table entry.
252  *
253  * @param db NAT64 DB.
254  * @param ste Session table entry.
255  */
256 void nat64_db_st_entry_free (nat64_db_t * db, nat64_db_st_entry_t * ste);
257 
258 /**
259  * @brief Find NAT64 session table entry.
260  *
261  * @param db NAT64 DB.
262  * @param l_addr Local host address.
263  * @param r_addr Remote host address.
264  * @param l_port Local host port number.
265  * @param r_port Remote host port number.
266  * @param proto L4 protocol.
267  * @param fib_index FIB index.
268  * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
269  *
270  * @return BIB entry if found.
271  */
272 nat64_db_st_entry_t *nat64_db_st_entry_find (nat64_db_t * db,
273  ip46_address_t * l_addr,
274  ip46_address_t * r_addr,
275  u16 l_port, u16 r_port,
276  u8 proto,
277  u32 fib_index, u8 is_ip6);
278 
279 /**
280  * @brief Call back function when walking NAT64 session table, non-zero
281  * return value stop walk.
282  */
283 typedef int (*nat64_db_st_walk_fn_t) (nat64_db_st_entry_t * ste, void *ctx);
284 
285 /**
286  * @brief Walk NAT64 session table.
287  *
288  * @param db NAT64 DB.
289  * @param proto L4 protocol:
290  * - 255 all session tables
291  * - 6 TCP session table
292  * - 17 UDP session table
293  * - 1/58 ICMP session table
294  * - otherwise "unknown" protocol session table
295  * @param fn The function to invoke on each entry visited.
296  * @param ctx A context passed in the visit function.
297  */
298 void nat64_db_st_walk (nat64_db_t * db, u8 proto,
299  nat64_db_st_walk_fn_t fn, void *ctx);
300 
301 /**
302  * @brief Free expired session entries in session tables.
303  *
304  * @param db NAT64 DB.
305  * @param now Current time.
306  */
307 void nad64_db_st_free_expired (nat64_db_t * db, u32 now);
308 
309 /**
310  * @brief Free sessions using specific outside address.
311  *
312  * @param db NAT64 DB.
313  * @param out_addr Outside address to match.
314  */
315 void nat64_db_free_out_addr (nat64_db_t * db, ip4_address_t * out_addr);
316 
317 /*
318  * @brief Get ST entry index.
319  *
320  * @param db NAT64 DB.
321  * @param ste ST entry.
322  *
323  * @return ST entry index on success, ~0 otherwise.
324  */
325 u32 nat64_db_st_entry_get_index (nat64_db_t * db, nat64_db_st_entry_t * ste);
326 
327 /**
328  * @brief Get ST entry by index and protocol.
329  *
330  * @param db NAT64 DB.
331  * @param proto L4 protocol.
332  * @param bibe_index ST entry index.
333  *
334  * @return BIB entry if found.
335  */
336 nat64_db_st_entry_t *nat64_db_st_entry_by_index (nat64_db_t * db,
337  u8 proto, u32 ste_index);
338 #endif /* __included_nat64_db_h__ */
339 
340 /*
341  * fd.io coding-style-patch-verification: ON
342  *
343  * Local Variables:
344  * eval: (c-set-style "gnu")
345  * End:
346  */
Definition: nat64_db.h:73
nat64_db_free_addr_port_function_t free_addr_port_cb
Definition: nat64_db.h:134
u16 l_port
Definition: nat64_db.h:82
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:484
u64 as_u64
Definition: bihash_doc.h:63
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:555
void nat64_db_free_out_addr(nat64_db_t *db, ip4_address_t *out_addr)
Free sessions using specific outside address.
Definition: nat64_db.c:610
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:22
u32 fib_index
Definition: nat64_db.h:81
nat64_db_bib_t bib
Definition: nat64_db.h:132
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
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:186
ip46_address_t l_addr
Definition: nat64_db.h:79
nat64_db_bib_entry_t * nat64_db_bib_entry_create(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:43
u16 r_port
Definition: nat64_db.h:83
clib_bihash_48_8_t in2out
Definition: nat64_db.h:116
unsigned long u64
Definition: types.h:89
nat64_db_st_entry_t * nat64_db_st_entry_create(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:339
u16 port
Definition: nat64_db.h:35
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:172
clib_bihash_24_8_t in2out
Definition: nat64_db.h:69
void nat64_db_st_entry_free(nat64_db_t *db, nat64_db_st_entry_t *ste)
Free NAT64 session table entry.
Definition: nat64_db.c:414
nat64_db_st_t st
Definition: nat64_db.h:133
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:288
ip46_address_t addr
Definition: nat64_db.h:33
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:126
u8 proto
Definition: nat64_db.h:84
void nad64_db_st_free_expired(nat64_db_t *db, u32 now)
Free expired session entries in session tables.
Definition: nat64_db.c:578
unsigned int u32
Definition: types.h:88
long ctx[MAX_CONNS]
Definition: main.c:122
void nat64_db_bib_entry_free(nat64_db_t *db, nat64_db_bib_entry_t *bibe)
Free NAT64 BIB entry.
Definition: nat64_db.c:104
u32 fib_index
Definition: nat64_db.h:34
u8 proto
Definition: nat64_db.h:36
unsigned short u16
Definition: types.h:57
clib_bihash_24_8_t out2in
Definition: nat64_db.h:70
unsigned char u8
Definition: types.h:56
u32 nat64_db_st_entry_get_index(nat64_db_t *db, nat64_db_st_entry_t *ste)
Definition: nat64_db.c:532
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:283
vhost_vring_addr_t addr
Definition: vhost-user.h:83
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:265
ip46_address_t r_addr
Definition: nat64_db.h:80
struct nat64_db_s nat64_db_t
clib_bihash_48_8_t out2in
Definition: nat64_db.h:117
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:214