acl-plugin: bihash-based ACL lookup
[vpp.git] / src / plugins / snat / nat64_db.h
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;
34       u32 fib_index;
35       u16 port;
36       u8 proto;
37       u8 rsvd;
38     };
39     u64 as_u64[3];
40   };
41 } nat64_db_bib_entry_key_t;
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;
63   foreach_snat_protocol
64 #undef _
65 /* *INDENT-ON* */
66
67   /* BIB lookup */
68   clib_bihash_24_8_t in2out;
69   clib_bihash_24_8_t out2in;
70 } nat64_db_bib_t;
71
72 typedef struct
73 {
74   union
75   {
76     struct
77     {
78       ip46_address_t l_addr;
79       ip46_address_t r_addr;
80       u32 fib_index;
81       u16 l_port;
82       u16 r_port;
83       u8 proto;
84       u8 rsvd[7];
85     };
86     u64 as_u64[6];
87   };
88 } nat64_db_st_entry_key_t;
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;
109   foreach_snat_protocol
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 {
120   nat64_db_bib_t bib;
121   nat64_db_st_t st;
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  */
177 void nat64_db_bib_walk (nat64_db_t * db, snat_protocol_t proto,
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  */
270 void nat64_db_st_walk (nat64_db_t * db, snat_protocol_t proto,
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  */