FD.io VPP  v17.07.01-10-g3be13f0
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 <snat/snat.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 
67  /* BIB lookup */
68  clib_bihash_24_8_t in2out;
69  clib_bihash_24_8_t out2in;
71 
72 typedef struct
73 {
74  union
75  {
76  struct
77  {
78  ip46_address_t l_addr;
79  ip46_address_t r_addr;
84  u8 rsvd[7];
85  };
86  u64 as_u64[6];
87  };
89 
90 /* *INDENT-OFF* */
91 typedef CLIB_PACKED(struct
92 {
93  ip6_address_t in_r_addr;
94  ip4_address_t out_r_addr;
95  u16 r_port;
96  u32 bibe_index;
97  u32 expire;
98  u8 proto;
99  u8 tcp_state;
100 }) nat64_db_st_entry_t;
101 /* *INDENT-ON* */
102 
103 typedef struct
104 {
105  /* session tables */
106 /* *INDENT-OFF* */
107 #define _(N, i, n, s) \
108  nat64_db_st_entry_t *_##n##_st;
110 #undef _
111 /* *INDENT-ON* */
112 
113  /* session lookup */
114  clib_bihash_48_8_t in2out;
115  clib_bihash_48_8_t out2in;
116 } nat64_db_st_t;
117 
118 typedef struct
119 {
122 } nat64_db_t;
123 
124 /**
125  * @brief Initialize NAT64 DB.
126  *
127  * @param db NAT64 DB.
128  *
129  * @returns 0 on success, non-zero value otherwise.
130  */
131 int nat64_db_init (nat64_db_t * db);
132 
133 /**
134  * @brief Create new NAT64 BIB entry.
135  *
136  * @param db NAT64 DB.
137  * @param in_addr Inside IPv6 address.
138  * @param out_addr Outside IPv4 address.
139  * @param in_port Inside port number.
140  * @param out_port Outside port number.
141  * @param fib_index FIB index.
142  * @param proto L4 protocol.
143  * @param is_static 1 if static, 0 if dynamic.
144  *
145  * @returns BIB entry on success, 0 otherwise.
146  */
147 nat64_db_bib_entry_t *nat64_db_bib_entry_create (nat64_db_t * db,
148  ip6_address_t * in_addr,
149  ip4_address_t * out_addr,
150  u16 in_port, u16 out_port,
151  u32 fib_index,
152  snat_protocol_t proto,
153  u8 is_static);
154 
155 /**
156  * @brief Free NAT64 BIB entry.
157  *
158  * @param db NAT64 DB.
159  * @param bibe BIB entry.
160  */
161 void nat64_db_bib_entry_free (nat64_db_t * db, nat64_db_bib_entry_t * bibe);
162 
163 /**
164  * @brief Call back function when walking NAT64 BIB, non-zero
165  * return value stop walk.
166  */
167 typedef int (*nat64_db_bib_walk_fn_t) (nat64_db_bib_entry_t * bibe,
168  void *ctx);
169 /**
170  * @brief Walk NAT64 BIB.
171  *
172  * @param db NAT64 DB.
173  * @param proto BIB protocol (TCP/UDP/ICMP).
174  * @param fn The function to invoke on each entry visited.
175  * @param ctx A context passed in the visit function.
176  */
178  nat64_db_bib_walk_fn_t fn, void *ctx);
179 
180 /**
181  * @brief Find NAT64 BIB entry.
182  *
183  * @param db NAT64 DB.
184  * @param addr IP address.
185  * @param port Port number.
186  * @param proto L4 protocol.
187  * @param fib_index FIB index.
188  * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
189  *
190  * @return BIB entry if found.
191  */
192 nat64_db_bib_entry_t *nat64_db_bib_entry_find (nat64_db_t * db,
193  ip46_address_t * addr,
194  u16 port,
195  snat_protocol_t proto,
196  u32 fib_index, u8 is_ip6);
197 
198 /**
199  * @brief Get BIB entry by index and protocol.
200  *
201  * @param db NAT64 DB.
202  * @param proto L4 protocol.
203  * @param bibe_index BIB entry index.
204  *
205  * @return BIB entry if found.
206  */
207 nat64_db_bib_entry_t *nat64_db_bib_entry_by_index (nat64_db_t * db,
208  snat_protocol_t proto,
209  u32 bibe_index);
210 /**
211  * @brief Create new NAT64 session table entry.
212  *
213  * @param db NAT64 DB.
214  * @param bibe Corresponding BIB entry.
215  * @param in_r_addr Inside IPv6 address of the remote host.
216  * @param out_r_addr Outside IPv4 address of the remote host.
217  * @param r_port Remote host port number.
218  *
219  * @returns BIB entry on success, 0 otherwise.
220  */
221 nat64_db_st_entry_t *nat64_db_st_entry_create (nat64_db_t * db,
222  nat64_db_bib_entry_t * bibe,
223  ip6_address_t * in_r_addr,
224  ip4_address_t * out_r_addr,
225  u16 r_port);
226 
227 /**
228  * @brief Free NAT64 session table entry.
229  *
230  * @param db NAT64 DB.
231  * @param ste Session table entry.
232  */
233 void nat64_db_st_entry_free (nat64_db_t * db, nat64_db_st_entry_t * ste);
234 
235 /**
236  * @brief Find NAT64 session table entry.
237  *
238  * @param db NAT64 DB.
239  * @param l_addr Local host address.
240  * @param r_addr Remote host address.
241  * @param l_port Local host port number.
242  * @param r_port Remote host port number.
243  * @param proto L4 protocol.
244  * @param fib_index FIB index.
245  * @param is_ip6 1 if find by IPv6 (inside) address, 0 by IPv4 (outside).
246  *
247  * @return BIB entry if found.
248  */
249 nat64_db_st_entry_t *nat64_db_st_entry_find (nat64_db_t * db,
250  ip46_address_t * l_addr,
251  ip46_address_t * r_addr,
252  u16 l_port, u16 r_port,
253  snat_protocol_t proto,
254  u32 fib_index, u8 is_ip6);
255 
256 /**
257  * @brief Call back function when walking NAT64 session table, non-zero
258  * return value stop walk.
259  */
260 typedef int (*nat64_db_st_walk_fn_t) (nat64_db_st_entry_t * ste, void *ctx);
261 
262 /**
263  * @brief Walk NAT64 session table.
264  *
265  * @param db NAT64 DB.
266  * @param proto Session table protocol (TCP/UDP/ICMP).
267  * @param fn The function to invoke on each entry visited.
268  * @param ctx A context passed in the visit function.
269  */
271  nat64_db_st_walk_fn_t fn, void *ctx);
272 
273 /**
274  * @brief Free expired session entries in session tables.
275  *
276  * @param db NAT64 DB.
277  * @param now Current time.
278  */
279 void nad64_db_st_free_expired (nat64_db_t * db, u32 now);
280 
281 #endif /* __included_nat64_db_h__ */
282 
283 /*
284  * fd.io coding-style-patch-verification: ON
285  *
286  * Local Variables:
287  * eval: (c-set-style "gnu")
288  * End:
289  */
Definition: nat64_db.h:72
nat64_db_bib_t bib
Definition: nat64_db.h:120
u16 l_port
Definition: nat64_db.h:81
nat64_db_bib_entry_t * nat64_db_bib_entry_find(nat64_db_t *db, ip46_address_t *addr, u16 port, snat_protocol_t proto, u32 fib_index, u8 is_ip6)
Find NAT64 BIB entry.
Definition: nat64_db.c:171
u64 as_u64
Definition: bihash_doc.h:63
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, snat_protocol_t proto, u32 fib_index, u8 is_ip6)
Find NAT64 session table entry.
Definition: nat64_db.c:439
Definition: nat64_db.h:27
u32 fib_index
Definition: nat64_db.h:80
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:167
ip46_address_t l_addr
Definition: nat64_db.h:78
foreach_snat_protocol clib_bihash_48_8_t in2out
Definition: nat64_db.h:114
u16 r_port
Definition: nat64_db.h:82
void nat64_db_st_walk(nat64_db_t *db, snat_protocol_t proto, nat64_db_st_walk_fn_t fn, void *ctx)
Walk NAT64 session table.
Definition: nat64_db.c:267
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:297
u16 port
Definition: nat64_db.h:35
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:370
foreach_snat_protocol clib_bihash_24_8_t in2out
Definition: nat64_db.h:68
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, snat_protocol_t proto, u8 is_static)
Create new NAT64 BIB entry.
Definition: nat64_db.c:45
nat64_db_bib_entry_t * nat64_db_bib_entry_by_index(nat64_db_t *db, snat_protocol_t proto, u32 bibe_index)
Get BIB entry by index and protocol.
Definition: nat64_db.c:243
ip46_address_t addr
Definition: nat64_db.h:33
snat_protocol_t
Definition: snat.h:98
u8 proto
Definition: nat64_db.h:83
void nad64_db_st_free_expired(nat64_db_t *db, u32 now)
Free expired session entries in session tables.
Definition: nat64_db.c:487
unsigned int u32
Definition: types.h:88
void nat64_db_bib_walk(nat64_db_t *db, snat_protocol_t proto, nat64_db_bib_walk_fn_t fn, void *ctx)
Walk NAT64 BIB.
Definition: nat64_db.c:213
void nat64_db_bib_entry_free(nat64_db_t *db, nat64_db_bib_entry_t *bibe)
Free NAT64 BIB entry.
Definition: nat64_db.c:105
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:69
unsigned char u8
Definition: types.h:56
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:260
nat64_db_st_t st
Definition: nat64_db.h:121
vhost_vring_addr_t addr
Definition: vhost-user.h:82
u8 rsvd
Definition: nat64_db.h:37
ip46_address_t r_addr
Definition: nat64_db.h:79
int nat64_db_init(nat64_db_t *db)
Initialize NAT64 DB.
Definition: nat64_db.c:22
clib_bihash_48_8_t out2in
Definition: nat64_db.h:115