711b6bf6b0352b5d5711b5b27727e8fa7d966105
[vpp.git] / src / plugins / nat / nat64 / nat64_db.h
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 {
27   u32 bib_buckets;
28   u32 bib_memory_size;
29   u32 st_buckets;
30   u32 st_memory_size;
31 } nat64_config_t;
32
33 typedef struct
34 {
35   union
36   {
37     struct
38     {
39       ip46_address_t addr;
40       u32 fib_index;
41       u16 port;
42       u8 proto;
43       u8 rsvd;
44     };
45     u64 as_u64[3];
46   };
47 } nat64_db_bib_entry_key_t;
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;
69   foreach_nat_protocol
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
78   u32 limit;
79   u32 bib_entries_num;
80 } nat64_db_bib_t;
81
82 typedef struct
83 {
84   union
85   {
86     struct
87     {
88       ip46_address_t l_addr;
89       ip46_address_t r_addr;
90       u32 fib_index;
91       u16 l_port;
92       u16 r_port;
93       u8 proto;
94       u8 rsvd[7];
95     };
96     u64 as_u64[6];
97   };
98 } nat64_db_st_entry_key_t;
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;
119   foreach_nat_protocol
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
128   u32 limit;
129   u32 st_entries_num;
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,
139                                                     ip4_address_t * addr,
140                                                     u16 port, u8 proto);
141
142 typedef struct nat64_db_s
143 {
144   nat64_db_bib_t bib;
145   nat64_db_st_t st;
146   nat64_db_free_addr_port_function_t free_addr_port_cb;
147   u8 addr_free;
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  */
162 int nat64_db_init (nat64_db_t * db, nat64_config_t c,
163                    nat64_db_free_addr_port_function_t free_addr_port_cb);
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  */
228 void nat64_db_bib_walk (nat64_db_t * db, u8 proto,
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  */