FD.io VPP  v18.11-rc0-18-g2a3fb1a
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 db NAT64 DB.
164  * @param in_addr Inside IPv6 address.
165  * @param out_addr Outside IPv4 address.
166  * @param in_port Inside port number.
167  * @param out_port Outside port number.
168  * @param fib_index FIB index.
169  * @param proto L4 protocol.
170  * @param is_static 1 if static, 0 if dynamic.
171  *
172  * @returns BIB entry on success, 0 otherwise.
173  */
174 nat64_db_bib_entry_t *nat64_db_bib_entry_create (nat64_db_t * db,
175  ip6_address_t * in_addr,
176  ip4_address_t * out_addr,
177  u16 in_port, u16 out_port,
178  u32 fib_index,
179  u8 proto, u8 is_static);
180 
181 /**
182  * @brief Free NAT64 BIB entry.
183  *
184  * @param db NAT64 DB.
185  * @param bibe BIB entry.
186  */
187 void nat64_db_bib_entry_free (nat64_db_t * db, nat64_db_bib_entry_t * bibe);
188 
189 /**
190  * @brief Call back function when walking NAT64 BIB, non-zero
191  * return value stop walk.
192  */
193 typedef int (*nat64_db_bib_walk_fn_t) (nat64_db_bib_entry_t * bibe,
194  void *ctx);
195 /**
196  * @brief Walk NAT64 BIB.
197  *
198  * @param db NAT64 DB.
199  * @param proto BIB L4 protocol:
200  * - 255 all BIBs
201  * - 6 TCP BIB
202  * - 17 UDP BIB
203  * - 1/58 ICMP BIB
204  * - otherwise "unknown" protocol BIB
205  * @param fn The function to invoke on each entry visited.
206  * @param ctx A context passed in the visit function.
207  */
208 void nat64_db_bib_walk (nat64_db_t * db, u8 proto,
209  nat64_db_bib_walk_fn_t fn, void *ctx);
210 
211 /**
212  * @brief Find NAT64 BIB entry.
213  *
214  * @param db NAT64 DB.
215  * @param addr IP address.
216  * @param port Port number.
217  * @param proto L4 protocol.
218  * @param fib_index FIB index.
219  * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
220  *
221  * @return BIB entry if found.
222  */
223 nat64_db_bib_entry_t *nat64_db_bib_entry_find (nat64_db_t * db,
224  ip46_address_t * addr,
225  u16 port,
226  u8 proto,
227  u32 fib_index, u8 is_ip6);
228 
229 /**
230  * @brief Get BIB entry by index and protocol.
231  *
232  * @param db NAT64 DB.
233  * @param proto L4 protocol.
234  * @param bibe_index BIB entry index.
235  *
236  * @return BIB entry if found.
237  */
238 nat64_db_bib_entry_t *nat64_db_bib_entry_by_index (nat64_db_t * db,
239  u8 proto, u32 bibe_index);
240 /**
241  * @brief Create new NAT64 session table entry.
242  *
243  * @param db NAT64 DB.
244  * @param bibe Corresponding BIB entry.
245  * @param in_r_addr Inside IPv6 address of the remote host.
246  * @param out_r_addr Outside IPv4 address of the remote host.
247  * @param r_port Remote host port number.
248  *
249  * @returns BIB entry on success, 0 otherwise.
250  */
251 nat64_db_st_entry_t *nat64_db_st_entry_create (nat64_db_t * db,
252  nat64_db_bib_entry_t * bibe,
253  ip6_address_t * in_r_addr,
254  ip4_address_t * out_r_addr,
255  u16 r_port);
256 
257 /**
258  * @brief Free NAT64 session table entry.
259  *
260  * @param db NAT64 DB.
261  * @param ste Session table entry.
262  */
263 void nat64_db_st_entry_free (nat64_db_t * db, nat64_db_st_entry_t * ste);
264 
265 /**
266  * @brief Find NAT64 session table entry.
267  *
268  * @param db NAT64 DB.
269  * @param l_addr Local host address.
270  * @param r_addr Remote host address.
271  * @param l_port Local host port number.
272  * @param r_port Remote host port number.
273  * @param proto L4 protocol.
274  * @param fib_index FIB index.
275  * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
276  *
277  * @return BIB entry if found.
278  */
279 nat64_db_st_entry_t *nat64_db_st_entry_find (nat64_db_t * db,
280  ip46_address_t * l_addr,
281  ip46_address_t * r_addr,
282  u16 l_port, u16 r_port,
283  u8 proto,
284  u32 fib_index, u8 is_ip6);
285 
286 /**
287  * @brief Call back function when walking NAT64 session table, non-zero
288  * return value stop walk.
289  */
290 typedef int (*nat64_db_st_walk_fn_t) (nat64_db_st_entry_t * ste, void *ctx);
291 
292 /**
293  * @brief Walk NAT64 session table.
294  *
295  * @param db NAT64 DB.
296  * @param proto L4 protocol:
297  * - 255 all session tables
298  * - 6 TCP session table
299  * - 17 UDP session table
300  * - 1/58 ICMP session table
301  * - otherwise "unknown" protocol session table
302  * @param fn The function to invoke on each entry visited.
303  * @param ctx A context passed in the visit function.
304  */
305 void nat64_db_st_walk (nat64_db_t * db, u8 proto,
306  nat64_db_st_walk_fn_t fn, void *ctx);
307 
308 /**
309  * @brief Free expired session entries in session tables.
310  *
311  * @param db NAT64 DB.
312  * @param now Current time.
313  */
314 void nad64_db_st_free_expired (nat64_db_t * db, u32 now);
315 
316 /**
317  * @brief Free sessions using specific outside address.
318  *
319  * @param db NAT64 DB.
320  * @param out_addr Outside address to match.
321  */
322 void nat64_db_free_out_addr (nat64_db_t * db, ip4_address_t * out_addr);
323 
324 /*
325  * @brief Get ST entry index.
326  *
327  * @param db NAT64 DB.
328  * @param ste ST entry.
329  *
330  * @return ST entry index on success, ~0 otherwise.
331  */
332 u32 nat64_db_st_entry_get_index (nat64_db_t * db, nat64_db_st_entry_t * ste);
333 
334 /**
335  * @brief Get ST entry by index and protocol.
336  *
337  * @param db NAT64 DB.
338  * @param proto L4 protocol.
339  * @param bibe_index ST entry index.
340  *
341  * @return BIB entry if found.
342  */
343 nat64_db_st_entry_t *nat64_db_st_entry_by_index (nat64_db_t * db,
344  u8 proto, u32 ste_index);
345 #endif /* __included_nat64_db_h__ */
346 
347 /*
348  * fd.io coding-style-patch-verification: ON
349  *
350  * Local Variables:
351  * eval: (c-set-style "gnu")
352  * End:
353  */
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:544
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:615
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:670
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:25
u32 fib_index
Definition: nat64_db.h:84
nat64_db_bib_t bib
Definition: nat64_db.h:138
vhost_vring_addr_t addr
Definition: vhost_user.h:116
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:193
u32 st_entries_num
Definition: nat64_db.h:123
ip46_address_t l_addr
Definition: nat64_db.h:82
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:52
u16 r_port
Definition: nat64_db.h:86
clib_bihash_48_8_t in2out
Definition: nat64_db.h:119
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:372
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:205
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:464
unsigned short u16
Definition: types.h:57
nat64_db_st_t st
Definition: nat64_db.h:139
u8 addr_free
Definition: nat64_db.h:141
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:321
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:132
u8 proto
Definition: nat64_db.h:87
void nad64_db_st_free_expired(nat64_db_t *db, u32 now)
Free expired session entries in session tables.
Definition: nat64_db.c:638
long ctx[MAX_CONNS]
Definition: main.c:126
void nat64_db_bib_entry_free(nat64_db_t *db, nat64_db_bib_entry_t *bibe)
Free NAT64 BIB entry.
Definition: nat64_db.c:127
u32 fib_index
Definition: nat64_db.h:34
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:592
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:290
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:298
ip46_address_t r_addr
Definition: nat64_db.h:83
struct nat64_db_s nat64_db_t
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:247