FD.io VPP  v19.01.2-3-gf61a1a8
Vector Packet Processing
nat64_db.c
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 #include <nat/nat64_db.h>
20 #include <nat/nat_ipfix_logging.h>
21 #include <nat/nat_inlines.h>
22 #include <nat/nat_syslog.h>
23 #include <vnet/fib/fib_table.h>
24 
25 int
26 nat64_db_init (nat64_db_t * db, u32 bib_buckets, u32 bib_memory_size,
27  u32 st_buckets, u32 st_memory_size,
28  nat64_db_free_addr_port_function_t free_addr_port_cb)
29 {
30  clib_bihash_init_24_8 (&db->bib.in2out, "bib-in2out", bib_buckets,
31  bib_memory_size);
32 
33  clib_bihash_init_24_8 (&db->bib.out2in, "bib-out2in", bib_buckets,
34  bib_memory_size);
35 
36  clib_bihash_init_48_8 (&db->st.in2out, "st-in2out", st_buckets,
37  st_memory_size);
38 
39  clib_bihash_init_48_8 (&db->st.out2in, "st-out2in", st_buckets,
40  st_memory_size);
41 
42  db->free_addr_port_cb = free_addr_port_cb;
43  db->bib.limit = 10 * bib_buckets;
44  db->bib.bib_entries_num = 0;
45  db->st.limit = 10 * st_buckets;
46  db->st.st_entries_num = 0;
47  db->addr_free = 0;
48 
49  return 0;
50 }
51 
52 nat64_db_bib_entry_t *
54  ip4_address_t * out_addr, u16 in_port,
55  u16 out_port, u32 fib_index, u8 proto,
56  u8 is_static)
57 {
58  nat64_db_bib_entry_t *bibe;
59  nat64_db_bib_entry_key_t bibe_key;
61  fib_table_t *fib;
62 
63  if (db->bib.bib_entries_num >= db->bib.limit)
64  {
65  db->free_addr_port_cb (db, out_addr, out_port, proto);
67  return 0;
68  }
69 
70  /* create pool entry */
71  switch (ip_proto_to_snat_proto (proto))
72  {
73 /* *INDENT-OFF* */
74 #define _(N, i, n, s) \
75  case SNAT_PROTOCOL_##N: \
76  pool_get (db->bib._##n##_bib, bibe); \
77  kv.value = bibe - db->bib._##n##_bib; \
78  break;
80 #undef _
81 /* *INDENT-ON* */
82  default:
83  pool_get (db->bib._unk_proto_bib, bibe);
84  kv.value = bibe - db->bib._unk_proto_bib;
85  break;
86  }
87 
88  db->bib.bib_entries_num++;
89 
90  clib_memset (bibe, 0, sizeof (*bibe));
91  bibe->in_addr.as_u64[0] = in_addr->as_u64[0];
92  bibe->in_addr.as_u64[1] = in_addr->as_u64[1];
93  bibe->in_port = in_port;
94  bibe->out_addr.as_u32 = out_addr->as_u32;
95  bibe->out_port = out_port;
96  bibe->fib_index = fib_index;
97  bibe->proto = proto;
98  bibe->is_static = is_static;
99 
100  /* create hash lookup */
101  bibe_key.addr.as_u64[0] = bibe->in_addr.as_u64[0];
102  bibe_key.addr.as_u64[1] = bibe->in_addr.as_u64[1];
103  bibe_key.fib_index = bibe->fib_index;
104  bibe_key.port = bibe->in_port;
105  bibe_key.proto = bibe->proto;
106  bibe_key.rsvd = 0;
107  kv.key[0] = bibe_key.as_u64[0];
108  kv.key[1] = bibe_key.as_u64[1];
109  kv.key[2] = bibe_key.as_u64[2];
110  clib_bihash_add_del_24_8 (&db->bib.in2out, &kv, 1);
111 
112  clib_memset (&bibe_key.addr, 0, sizeof (bibe_key.addr));
113  bibe_key.addr.ip4.as_u32 = bibe->out_addr.as_u32;
114  bibe_key.fib_index = 0;
115  bibe_key.port = bibe->out_port;
116  kv.key[0] = bibe_key.as_u64[0];
117  kv.key[1] = bibe_key.as_u64[1];
118  kv.key[2] = bibe_key.as_u64[2];
119  clib_bihash_add_del_24_8 (&db->bib.out2in, &kv, 1);
120 
121  fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
122  nat_ipfix_logging_nat64_bib (in_addr, out_addr, proto, in_port, out_port,
123  fib->ft_table_id, 1);
124  return bibe;
125 }
126 
127 void
128 nat64_db_bib_entry_free (nat64_db_t * db, nat64_db_bib_entry_t * bibe)
129 {
130  nat64_db_bib_entry_key_t bibe_key;
132  nat64_db_bib_entry_t *bib;
133  u32 *ste_to_be_free = 0, *ste_index, bibe_index;
134  nat64_db_st_entry_t *st, *ste;
135  fib_table_t *fib;
136 
137  switch (ip_proto_to_snat_proto (bibe->proto))
138  {
139 /* *INDENT-OFF* */
140 #define _(N, i, n, s) \
141  case SNAT_PROTOCOL_##N: \
142  bib = db->bib._##n##_bib; \
143  st = db->st._##n##_st; \
144  break;
146 #undef _
147 /* *INDENT-ON* */
148  default:
149  bib = db->bib._unk_proto_bib;
150  st = db->st._unk_proto_st;
151  break;
152  }
153 
154  db->bib.bib_entries_num--;
155 
156  bibe_index = bibe - bib;
157 
158  /* delete ST entries for static BIB entry */
159  if (bibe->is_static)
160  {
161  pool_foreach (ste, st, (
162  {
163  if (ste->bibe_index == bibe_index)
164  vec_add1 (ste_to_be_free, ste - st);}
165  ));
166  vec_foreach (ste_index, ste_to_be_free)
167  nat64_db_st_entry_free (db, pool_elt_at_index (st, ste_index[0]));
168  vec_free (ste_to_be_free);
169  }
170 
171  /* delete hash lookup */
172  bibe_key.addr.as_u64[0] = bibe->in_addr.as_u64[0];
173  bibe_key.addr.as_u64[1] = bibe->in_addr.as_u64[1];
174  bibe_key.fib_index = bibe->fib_index;
175  bibe_key.port = bibe->in_port;
176  bibe_key.proto = bibe->proto;
177  bibe_key.rsvd = 0;
178  kv.key[0] = bibe_key.as_u64[0];
179  kv.key[1] = bibe_key.as_u64[1];
180  kv.key[2] = bibe_key.as_u64[2];
181  clib_bihash_add_del_24_8 (&db->bib.in2out, &kv, 0);
182 
183  clib_memset (&bibe_key.addr, 0, sizeof (bibe_key.addr));
184  bibe_key.addr.ip4.as_u32 = bibe->out_addr.as_u32;
185  bibe_key.fib_index = 0;
186  bibe_key.port = bibe->out_port;
187  kv.key[0] = bibe_key.as_u64[0];
188  kv.key[1] = bibe_key.as_u64[1];
189  kv.key[2] = bibe_key.as_u64[2];
190  clib_bihash_add_del_24_8 (&db->bib.out2in, &kv, 0);
191 
192  if (!db->addr_free)
193  db->free_addr_port_cb (db, &bibe->out_addr, bibe->out_port, bibe->proto);
194 
195  fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
196  nat_ipfix_logging_nat64_bib (&bibe->in_addr, &bibe->out_addr, bibe->proto,
197  bibe->in_port, bibe->out_port,
198  fib->ft_table_id, 0);
199 
200  /* delete from pool */
201  pool_put (bib, bibe);
202 
203 }
204 
205 nat64_db_bib_entry_t *
206 nat64_db_bib_entry_find (nat64_db_t * db, ip46_address_t * addr, u16 port,
207  u8 proto, u32 fib_index, u8 is_ip6)
208 {
209  nat64_db_bib_entry_t *bibe = 0;
210  nat64_db_bib_entry_key_t bibe_key;
211  clib_bihash_kv_24_8_t kv, value;
212  nat64_db_bib_entry_t *bib;
213 
214  switch (ip_proto_to_snat_proto (proto))
215  {
216 /* *INDENT-OFF* */
217 #define _(N, i, n, s) \
218  case SNAT_PROTOCOL_##N: \
219  bib = db->bib._##n##_bib; \
220  break;
222 #undef _
223 /* *INDENT-ON* */
224  default:
225  bib = db->bib._unk_proto_bib;
226  break;
227  }
228 
229  bibe_key.addr.as_u64[0] = addr->as_u64[0];
230  bibe_key.addr.as_u64[1] = addr->as_u64[1];
231  bibe_key.fib_index = fib_index;
232  bibe_key.port = port;
233  bibe_key.proto = proto;
234  bibe_key.rsvd = 0;
235 
236  kv.key[0] = bibe_key.as_u64[0];
237  kv.key[1] = bibe_key.as_u64[1];
238  kv.key[2] = bibe_key.as_u64[2];
239 
240  if (!clib_bihash_search_24_8
241  (is_ip6 ? &db->bib.in2out : &db->bib.out2in, &kv, &value))
242  bibe = pool_elt_at_index (bib, value.value);
243 
244  return bibe;
245 }
246 
247 void
249  nat64_db_bib_walk_fn_t fn, void *ctx)
250 {
251  nat64_db_bib_entry_t *bib, *bibe;
252 
253  if (proto == 255)
254  {
255  /* *INDENT-OFF* */
256  #define _(N, i, n, s) \
257  bib = db->bib._##n##_bib; \
258  pool_foreach (bibe, bib, ({ \
259  if (fn (bibe, ctx)) \
260  return; \
261  }));
263  #undef _
264  bib = db->bib._unk_proto_bib;
265  pool_foreach (bibe, bib, ({
266  if (fn (bibe, ctx))
267  return;
268  }));
269  /* *INDENT-ON* */
270  }
271  else
272  {
273  switch (ip_proto_to_snat_proto (proto))
274  {
275  /* *INDENT-OFF* */
276  #define _(N, i, n, s) \
277  case SNAT_PROTOCOL_##N: \
278  bib = db->bib._##n##_bib; \
279  break;
281  #undef _
282  /* *INDENT-ON* */
283  default:
284  bib = db->bib._unk_proto_bib;
285  break;
286  }
287 
288  /* *INDENT-OFF* */
289  pool_foreach (bibe, bib,
290  ({
291  if (fn (bibe, ctx))
292  return;
293  }));
294  /* *INDENT-ON* */
295  }
296 }
297 
298 nat64_db_bib_entry_t *
299 nat64_db_bib_entry_by_index (nat64_db_t * db, u8 proto, u32 bibe_index)
300 {
301  nat64_db_bib_entry_t *bib;
302 
303  switch (ip_proto_to_snat_proto (proto))
304  {
305 /* *INDENT-OFF* */
306 #define _(N, i, n, s) \
307  case SNAT_PROTOCOL_##N: \
308  bib = db->bib._##n##_bib; \
309  break;
311 #undef _
312 /* *INDENT-ON* */
313  default:
314  bib = db->bib._unk_proto_bib;
315  break;
316  }
317 
318  return pool_elt_at_index (bib, bibe_index);
319 }
320 
321 void
323  nat64_db_st_walk_fn_t fn, void *ctx)
324 {
325  nat64_db_st_entry_t *st, *ste;
326 
327  if (proto == 255)
328  {
329  /* *INDENT-OFF* */
330  #define _(N, i, n, s) \
331  st = db->st._##n##_st; \
332  pool_foreach (ste, st, ({ \
333  if (fn (ste, ctx)) \
334  return; \
335  }));
337  #undef _
338  st = db->st._unk_proto_st;
339  pool_foreach (ste, st, ({
340  if (fn (ste, ctx))
341  return;
342  }));
343  /* *INDENT-ON* */
344  }
345  else
346  {
347  switch (ip_proto_to_snat_proto (proto))
348  {
349  /* *INDENT-OFF* */
350  #define _(N, i, n, s) \
351  case SNAT_PROTOCOL_##N: \
352  st = db->st._##n##_st; \
353  break;
355  #undef _
356  /* *INDENT-ON* */
357  default:
358  st = db->st._unk_proto_st;
359  break;
360  }
361 
362  /* *INDENT-OFF* */
363  pool_foreach (ste, st,
364  ({
365  if (fn (ste, ctx))
366  return;
367  }));
368  /* *INDENT-ON* */
369  }
370 }
371 
372 nat64_db_st_entry_t *
373 nat64_db_st_entry_create (nat64_db_t * db, nat64_db_bib_entry_t * bibe,
374  ip6_address_t * in_r_addr,
375  ip4_address_t * out_r_addr, u16 r_port)
376 {
377  nat64_db_st_entry_t *ste;
378  nat64_db_bib_entry_t *bib;
379  nat64_db_st_entry_key_t ste_key;
381  fib_table_t *fib;
382 
383  if (db->st.st_entries_num >= db->st.limit)
384  {
386  return 0;
387  }
388 
389  /* create pool entry */
390  switch (ip_proto_to_snat_proto (bibe->proto))
391  {
392 /* *INDENT-OFF* */
393 #define _(N, i, n, s) \
394  case SNAT_PROTOCOL_##N: \
395  pool_get (db->st._##n##_st, ste); \
396  kv.value = ste - db->st._##n##_st; \
397  bib = db->bib._##n##_bib; \
398  break;
400 #undef _
401 /* *INDENT-ON* */
402  default:
403  pool_get (db->st._unk_proto_st, ste);
404  kv.value = ste - db->st._unk_proto_st;
405  bib = db->bib._unk_proto_bib;
406  break;
407  }
408 
409  db->st.st_entries_num++;
410 
411  clib_memset (ste, 0, sizeof (*ste));
412  ste->in_r_addr.as_u64[0] = in_r_addr->as_u64[0];
413  ste->in_r_addr.as_u64[1] = in_r_addr->as_u64[1];
414  ste->out_r_addr.as_u32 = out_r_addr->as_u32;
415  ste->r_port = r_port;
416  ste->bibe_index = bibe - bib;
417  ste->proto = bibe->proto;
418 
419  /* increment session number for BIB entry */
420  bibe->ses_num++;
421 
422  /* create hash lookup */
423  clib_memset (&ste_key, 0, sizeof (ste_key));
424  ste_key.l_addr.as_u64[0] = bibe->in_addr.as_u64[0];
425  ste_key.l_addr.as_u64[1] = bibe->in_addr.as_u64[1];
426  ste_key.r_addr.as_u64[0] = ste->in_r_addr.as_u64[0];
427  ste_key.r_addr.as_u64[1] = ste->in_r_addr.as_u64[1];
428  ste_key.fib_index = bibe->fib_index;
429  ste_key.l_port = bibe->in_port;
430  ste_key.r_port = ste->r_port;
431  ste_key.proto = ste->proto;
432  kv.key[0] = ste_key.as_u64[0];
433  kv.key[1] = ste_key.as_u64[1];
434  kv.key[2] = ste_key.as_u64[2];
435  kv.key[3] = ste_key.as_u64[3];
436  kv.key[4] = ste_key.as_u64[4];
437  kv.key[5] = ste_key.as_u64[5];
438  clib_bihash_add_del_48_8 (&db->st.in2out, &kv, 1);
439 
440  clib_memset (&ste_key, 0, sizeof (ste_key));
441  ste_key.l_addr.ip4.as_u32 = bibe->out_addr.as_u32;
442  ste_key.r_addr.ip4.as_u32 = ste->out_r_addr.as_u32;
443  ste_key.l_port = bibe->out_port;
444  ste_key.r_port = ste->r_port;
445  ste_key.proto = ste->proto;
446  kv.key[0] = ste_key.as_u64[0];
447  kv.key[1] = ste_key.as_u64[1];
448  kv.key[2] = ste_key.as_u64[2];
449  kv.key[3] = ste_key.as_u64[3];
450  kv.key[4] = ste_key.as_u64[4];
451  kv.key[5] = ste_key.as_u64[5];
452  clib_bihash_add_del_48_8 (&db->st.out2in, &kv, 1);
453 
454  fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
455  nat_ipfix_logging_nat64_session (&bibe->in_addr, &bibe->out_addr,
456  bibe->proto, bibe->in_port, bibe->out_port,
457  &ste->in_r_addr, &ste->out_r_addr,
458  ste->r_port, ste->r_port, fib->ft_table_id,
459  1);
460  nat_syslog_nat64_sadd (bibe->fib_index, &bibe->in_addr, bibe->in_port,
461  &bibe->out_addr, bibe->out_port, &ste->out_r_addr,
462  ste->r_port, bibe->proto);
463  return ste;
464 }
465 
466 void
467 nat64_db_st_entry_free (nat64_db_t * db, nat64_db_st_entry_t * ste)
468 {
469  nat64_db_st_entry_t *st;
470  nat64_db_bib_entry_t *bib, *bibe;
471  nat64_db_st_entry_key_t ste_key;
473  fib_table_t *fib;
474 
475  switch (ip_proto_to_snat_proto (ste->proto))
476  {
477 /* *INDENT-OFF* */
478 #define _(N, i, n, s) \
479  case SNAT_PROTOCOL_##N: \
480  st = db->st._##n##_st; \
481  bib = db->bib._##n##_bib; \
482  break;
484 #undef _
485 /* *INDENT-ON* */
486  default:
487  st = db->st._unk_proto_st;
488  bib = db->bib._unk_proto_bib;
489  break;
490  }
491 
492  bibe = pool_elt_at_index (bib, ste->bibe_index);
493 
494  db->st.st_entries_num--;
495 
496  /* delete hash lookup */
497  clib_memset (&ste_key, 0, sizeof (ste_key));
498  ste_key.l_addr.as_u64[0] = bibe->in_addr.as_u64[0];
499  ste_key.l_addr.as_u64[1] = bibe->in_addr.as_u64[1];
500  ste_key.r_addr.as_u64[0] = ste->in_r_addr.as_u64[0];
501  ste_key.r_addr.as_u64[1] = ste->in_r_addr.as_u64[1];
502  ste_key.fib_index = bibe->fib_index;
503  ste_key.l_port = bibe->in_port;
504  ste_key.r_port = ste->r_port;
505  ste_key.proto = ste->proto;
506  kv.key[0] = ste_key.as_u64[0];
507  kv.key[1] = ste_key.as_u64[1];
508  kv.key[2] = ste_key.as_u64[2];
509  kv.key[3] = ste_key.as_u64[3];
510  kv.key[4] = ste_key.as_u64[4];
511  kv.key[5] = ste_key.as_u64[5];
512  clib_bihash_add_del_48_8 (&db->st.in2out, &kv, 0);
513 
514  clib_memset (&ste_key, 0, sizeof (ste_key));
515  ste_key.l_addr.ip4.as_u32 = bibe->out_addr.as_u32;
516  ste_key.r_addr.ip4.as_u32 = ste->out_r_addr.as_u32;
517  ste_key.l_port = bibe->out_port;
518  ste_key.r_port = ste->r_port;
519  ste_key.proto = ste->proto;
520  kv.key[0] = ste_key.as_u64[0];
521  kv.key[1] = ste_key.as_u64[1];
522  kv.key[2] = ste_key.as_u64[2];
523  kv.key[3] = ste_key.as_u64[3];
524  kv.key[4] = ste_key.as_u64[4];
525  kv.key[5] = ste_key.as_u64[5];
526  clib_bihash_add_del_48_8 (&db->st.out2in, &kv, 0);
527 
528  fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
529  nat_ipfix_logging_nat64_session (&bibe->in_addr, &bibe->out_addr,
530  bibe->proto, bibe->in_port, bibe->out_port,
531  &ste->in_r_addr, &ste->out_r_addr,
532  ste->r_port, ste->r_port, fib->ft_table_id,
533  0);
534  nat_syslog_nat64_sdel (bibe->fib_index, &bibe->in_addr, bibe->in_port,
535  &bibe->out_addr, bibe->out_port, &ste->out_r_addr,
536  ste->r_port, bibe->proto);
537 
538  /* delete from pool */
539  pool_put (st, ste);
540 
541  /* decrement session number for BIB entry */
542  bibe->ses_num--;
543 
544  /* delete BIB entry if last session and dynamic */
545  if (!bibe->is_static && !bibe->ses_num)
546  nat64_db_bib_entry_free (db, bibe);
547 }
548 
549 nat64_db_st_entry_t *
550 nat64_db_st_entry_find (nat64_db_t * db, ip46_address_t * l_addr,
551  ip46_address_t * r_addr, u16 l_port, u16 r_port,
552  u8 proto, u32 fib_index, u8 is_ip6)
553 {
554  nat64_db_st_entry_t *ste = 0;
555  nat64_db_st_entry_t *st;
556  nat64_db_st_entry_key_t ste_key;
557  clib_bihash_kv_48_8_t kv, value;
558 
559  switch (ip_proto_to_snat_proto (proto))
560  {
561 /* *INDENT-OFF* */
562 #define _(N, i, n, s) \
563  case SNAT_PROTOCOL_##N: \
564  st = db->st._##n##_st; \
565  break;
567 #undef _
568 /* *INDENT-ON* */
569  default:
570  st = db->st._unk_proto_st;
571  break;
572  }
573 
574  clib_memset (&ste_key, 0, sizeof (ste_key));
575  ste_key.l_addr.as_u64[0] = l_addr->as_u64[0];
576  ste_key.l_addr.as_u64[1] = l_addr->as_u64[1];
577  ste_key.r_addr.as_u64[0] = r_addr->as_u64[0];
578  ste_key.r_addr.as_u64[1] = r_addr->as_u64[1];
579  ste_key.fib_index = fib_index;
580  ste_key.l_port = l_port;
581  ste_key.r_port = r_port;
582  ste_key.proto = proto;
583  kv.key[0] = ste_key.as_u64[0];
584  kv.key[1] = ste_key.as_u64[1];
585  kv.key[2] = ste_key.as_u64[2];
586  kv.key[3] = ste_key.as_u64[3];
587  kv.key[4] = ste_key.as_u64[4];
588  kv.key[5] = ste_key.as_u64[5];
589 
590  if (!clib_bihash_search_48_8
591  (is_ip6 ? &db->st.in2out : &db->st.out2in, &kv, &value))
592  ste = pool_elt_at_index (st, value.value);
593 
594  return ste;
595 }
596 
597 u32
598 nat64_db_st_entry_get_index (nat64_db_t * db, nat64_db_st_entry_t * ste)
599 {
600  nat64_db_st_entry_t *st;
601 
602  switch (ip_proto_to_snat_proto (ste->proto))
603  {
604 /* *INDENT-OFF* */
605 #define _(N, i, n, s) \
606  case SNAT_PROTOCOL_##N: \
607  st = db->st._##n##_st; \
608  break;
610 #undef _
611 /* *INDENT-ON* */
612  default:
613  st = db->st._unk_proto_st;
614  return (u32) ~ 0;
615  }
616 
617  return ste - st;
618 }
619 
620 nat64_db_st_entry_t *
621 nat64_db_st_entry_by_index (nat64_db_t * db, u8 proto, u32 ste_index)
622 {
623  nat64_db_st_entry_t *st;
624 
625  switch (ip_proto_to_snat_proto (proto))
626  {
627 /* *INDENT-OFF* */
628 #define _(N, i, n, s) \
629  case SNAT_PROTOCOL_##N: \
630  st = db->st._##n##_st; \
631  break;
633 #undef _
634 /* *INDENT-ON* */
635  default:
636  st = db->st._unk_proto_st;
637  break;
638  }
639 
640  return pool_elt_at_index (st, ste_index);
641 }
642 
643 void
645 {
646  u32 *ste_to_be_free = 0, *ste_index;
647  nat64_db_st_entry_t *st, *ste;
648 
649 /* *INDENT-OFF* */
650 #define _(N, i, n, s) \
651  st = db->st._##n##_st; \
652  pool_foreach (ste, st, ({\
653  if (i == SNAT_PROTOCOL_TCP && !ste->tcp_state) \
654  continue; \
655  if (ste->expire < now) \
656  vec_add1 (ste_to_be_free, ste - st); \
657  })); \
658  vec_foreach (ste_index, ste_to_be_free) \
659  nat64_db_st_entry_free (db, pool_elt_at_index(st, ste_index[0])); \
660  vec_free (ste_to_be_free); \
661  ste_to_be_free = 0;
663 #undef _
664  st = db->st._unk_proto_st;
665  pool_foreach (ste, st, ({
666  if (ste->expire < now)
667  vec_add1 (ste_to_be_free, ste - st);
668  }));
669  vec_foreach (ste_index, ste_to_be_free)
670  nat64_db_st_entry_free (db, pool_elt_at_index(st, ste_index[0]));
671  vec_free (ste_to_be_free);
672 /* *INDENT-ON* */
673 }
674 
675 void
677 {
678  u32 *ste_to_be_free = 0, *ste_index;
679  nat64_db_st_entry_t *st, *ste;
680  nat64_db_bib_entry_t *bibe;
681 
682  db->addr_free = 1;
683 /* *INDENT-OFF* */
684 #define _(N, i, n, s) \
685  st = db->st._##n##_st; \
686  pool_foreach (ste, st, ({ \
687  bibe = pool_elt_at_index (db->bib._##n##_bib, ste->bibe_index); \
688  if (bibe->out_addr.as_u32 == out_addr->as_u32) \
689  vec_add1 (ste_to_be_free, ste - st); \
690  })); \
691  vec_foreach (ste_index, ste_to_be_free) \
692  nat64_db_st_entry_free (db, pool_elt_at_index(st, ste_index[0])); \
693  vec_free (ste_to_be_free); \
694  ste_to_be_free = 0;
696 #undef _
697  st = db->st._unk_proto_st;
698  pool_foreach (ste, st, ({
699  bibe = pool_elt_at_index (db->bib._unk_proto_bib, ste->bibe_index);
700  if (bibe->out_addr.as_u32 == out_addr->as_u32)
701  vec_add1 (ste_to_be_free, ste - st);
702  }));
703  vec_foreach (ste_index, ste_to_be_free)
704  nat64_db_st_entry_free (db, pool_elt_at_index(st, ste_index[0]));
705  vec_free (ste_to_be_free);
706  db->addr_free = 0;
707 /* *INDENT-ON* */
708 }
709 
710 /*
711  * fd.io coding-style-patch-verification: ON
712  *
713  * Local Variables:
714  * eval: (c-set-style "gnu")
715  * End:
716  */
void nat_ipfix_logging_max_sessions(u32 limit)
Generate maximum session entries exceeded event.
Definition: nat64_db.h:76
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:26
nat64_db_free_addr_port_function_t free_addr_port_cb
Definition: nat64_db.h:140
u16 l_port
Definition: nat64_db.h:85
u64 as_u64[2]
Definition: ip6_packet.h:51
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:373
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:53
Definition: nat64_db.h:27
void nat_syslog_nat64_sdel(u32 sfibix, ip6_address_t *isaddr, u16 isport, ip4_address_t *xsaddr, u16 xsport, ip4_address_t *xdaddr, u16 xdport, snat_protocol_t proto)
Definition: nat_syslog.c:275
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:525
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:206
u32 fib_index
Definition: nat64_db.h:84
clib_memset(h->entries, 0, sizeof(h->entries[0])*entries)
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:621
nat64_db_bib_t bib
Definition: nat64_db.h:138
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
vhost_vring_addr_t addr
Definition: vhost_user.h:121
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
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:490
void nat_ipfix_logging_nat64_bib(ip6_address_t *src_ip, ip4_address_t *nat_src_ip, u8 proto, u16 src_port, u16 nat_src_port, u32 vrf_id, u8 is_create)
Generate NAT64 BIB create and delete events.
u16 r_port
Definition: nat64_db.h:86
clib_bihash_48_8_t in2out
Definition: nat64_db.h:119
u16 port
Definition: nat64_db.h:35
unsigned int u32
Definition: types.h:88
u64 as_u64[6]
Definition: nat64_db.h:90
clib_bihash_24_8_t in2out
Definition: nat64_db.h:69
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:511
void nad64_db_st_free_expired(nat64_db_t *db, u32 now)
Free expired session entries in session tables.
Definition: nat64_db.c:644
long ctx[MAX_CONNS]
Definition: main.c:144
unsigned short u16
Definition: types.h:57
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
void nat64_db_bib_entry_free(nat64_db_t *db, nat64_db_bib_entry_t *bibe)
Free NAT64 BIB entry.
Definition: nat64_db.c:128
void nat_syslog_nat64_sadd(u32 sfibix, ip6_address_t *isaddr, u16 isport, ip4_address_t *xsaddr, u16 xsport, ip4_address_t *xdaddr, u16 xdport, snat_protocol_t proto)
Definition: nat_syslog.c:265
nat64_db_st_t st
Definition: nat64_db.h:139
u8 addr_free
Definition: nat64_db.h:141
void nat_ipfix_logging_max_bibs(u32 limit)
Generate maximum BIB entries exceeded event.
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:89
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
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
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:550
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:322
u32 nat64_db_st_entry_get_index(nat64_db_t *db, nat64_db_st_entry_t *ste)
Definition: nat64_db.c:598
u32 fib_index
Definition: nat64_db.h:34
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:299
u32 bib_entries_num
Definition: nat64_db.h:73
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:676
u8 proto
Definition: nat64_db.h:36
clib_bihash_24_8_t out2in
Definition: nat64_db.h:70
static u32 ip_proto_to_snat_proto(u8 ip_proto)
The NAT inline functions.
Definition: nat_inlines.h:26
NAT syslog logging.
fib_table_t * fib_table_get(fib_node_index_t index, fib_protocol_t proto)
Get a pointer to a FIB table.
Definition: fib_table.c:27
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:467
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
NAT64 DB.
#define vec_foreach(var, vec)
Vector iterator.
u8 rsvd
Definition: nat64_db.h:37
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:248
u64 as_u64[3]
Definition: nat64_db.h:39
ip46_address_t r_addr
Definition: nat64_db.h:83
A protocol Independent FIB table.
Definition: fib_table.h:69
clib_bihash_48_8_t out2in
Definition: nat64_db.h:120
void nat_ipfix_logging_nat64_session(ip6_address_t *src_ip, ip4_address_t *nat_src_ip, u8 proto, u16 src_port, u16 nat_src_port, ip6_address_t *dst_ip, ip4_address_t *nat_dst_ip, u16 dst_port, u16 nat_dst_port, u32 vrf_id, u8 is_create)
Generate NAT64 session create and delete events.