FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
out2in_ed.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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 NAT44 endpoint-dependent outside to inside network translation
18  */
19 
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/pg/pg.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/fib/ip4_fib.h>
26 #include <vnet/udp/udp.h>
27 #include <vppinfra/error.h>
28 #include <nat/nat.h>
29 #include <nat/nat_ipfix_logging.h>
30 #include <nat/nat_reass.h>
31 #include <nat/nat_inlines.h>
32 #include <nat/nat_syslog.h>
33 #include <nat/nat_ha.h>
34 
35 static char *nat_out2in_ed_error_strings[] = {
36 #define _(sym,string) string,
38 #undef _
39 };
40 
41 typedef struct
42 {
48 
49 static u8 *
50 format_nat44_ed_out2in_trace (u8 * s, va_list * args)
51 {
52  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54  nat44_ed_out2in_trace_t *t = va_arg (*args, nat44_ed_out2in_trace_t *);
55  char *tag;
56 
57  tag =
58  t->is_slow_path ? "NAT44_OUT2IN_ED_SLOW_PATH" :
59  "NAT44_OUT2IN_ED_FAST_PATH";
60 
61  s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
63 
64  return s;
65 }
66 
67 static inline u32
69  ip4_header_t * ip0, icmp46_header_t * icmp0,
70  u32 sw_if_index0, u32 rx_fib_index0,
71  vlib_node_runtime_t * node, u32 next0, f64 now,
72  u32 thread_index, snat_session_t ** p_s0)
73 {
74  next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
75  next0, thread_index, p_s0, 0);
76  snat_session_t *s0 = *p_s0;
77  if (PREDICT_TRUE (next0 != NAT_NEXT_DROP && s0))
78  {
79  /* Accounting */
82  (sm->vlib_main, b0), thread_index);
83  /* Per-user LRU list maintenance */
84  nat44_session_update_lru (sm, s0, thread_index);
85  }
86  return next0;
87 }
88 
89 #ifndef CLIB_MARCH_VARIANT
90 int
92 {
93  snat_main_t *sm = &snat_main;
95  snat_session_t *s;
96  u64 sess_timeout_time;
97  nat_ed_ses_key_t ed_key;
99  int i;
100  snat_address_t *a;
103  ctx->thread_index);
104 
105  s = pool_elt_at_index (tsm->sessions, kv->value);
106  sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
107  if (ctx->now >= sess_timeout_time)
108  {
109  ed_key.l_addr = s->in2out.addr;
110  ed_key.r_addr = s->ext_host_addr;
111  ed_key.fib_index = s->in2out.fib_index;
113  {
114  ed_key.proto = s->in2out.port;
115  ed_key.r_port = 0;
116  ed_key.l_port = 0;
117  }
118  else
119  {
120  ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
121  ed_key.l_port = s->in2out.port;
122  ed_key.r_port = s->ext_host_port;
123  }
124  if (is_twice_nat_session (s))
125  {
126  ed_key.r_addr = s->ext_host_nat_addr;
127  ed_key.r_port = s->ext_host_nat_port;
128  }
129  ed_kv.key[0] = ed_key.as_u64[0];
130  ed_kv.key[1] = ed_key.as_u64[1];
131  if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
132  nat_elog_warn ("in2out_ed key del failed");
133 
135  goto delete;
136 
138  s->in2out.addr.as_u32,
139  s->out2in.addr.as_u32,
140  s->in2out.protocol,
141  s->in2out.port,
142  s->out2in.port,
143  s->in2out.fib_index);
144 
145  nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
146  &s->in2out.addr, s->in2out.port,
147  &s->ext_host_nat_addr, s->ext_host_nat_port,
148  &s->out2in.addr, s->out2in.port,
149  &s->ext_host_addr, s->ext_host_port,
150  s->in2out.protocol, is_twice_nat_session (s));
151 
152  nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
153  s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
154  ctx->thread_index);
155 
156  if (is_twice_nat_session (s))
157  {
158  for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
159  {
160  key.protocol = s->in2out.protocol;
161  key.port = s->ext_host_nat_port;
162  a = sm->twice_nat_addresses + i;
163  if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
164  {
166  ctx->thread_index,
167  &key);
168  break;
169  }
170  }
171  }
172 
173  if (snat_is_session_static (s))
174  goto delete;
175 
177  &s->out2in);
178  delete:
179  nat44_delete_session (sm, s, ctx->thread_index);
180  return 1;
181  }
182 
183  return 0;
184 }
185 #endif
186 
187 static snat_session_t *
189  vlib_buffer_t * b,
190  snat_session_key_t l_key,
191  snat_session_key_t e_key,
192  vlib_node_runtime_t * node,
193  u32 thread_index,
194  twice_nat_type_t twice_nat,
195  lb_nat_type_t lb_nat, f64 now)
196 {
197  snat_session_t *s;
198  snat_user_t *u;
199  ip4_header_t *ip;
200  udp_header_t *udp;
201  snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
203  snat_session_key_t eh_key;
205 
206  if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
207  {
208  b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
209  nat_elog_notice ("maximum sessions exceeded");
210  return 0;
211  }
212 
213  u = nat_user_get_or_create (sm, &l_key.addr, l_key.fib_index, thread_index);
214  if (!u)
215  {
216  nat_elog_warn ("create NAT user failed");
217  return 0;
218  }
219 
220  s = nat_ed_session_alloc (sm, u, thread_index, now);
221  if (!s)
222  {
223  nat44_delete_user_with_no_session (sm, u, thread_index);
224  nat_elog_warn ("create NAT session failed");
225  return 0;
226  }
227 
228  ip = vlib_buffer_get_current (b);
229  udp = ip4_next_header (ip);
230 
231  s->ext_host_addr.as_u32 = ip->src_address.as_u32;
232  s->ext_host_port = e_key.protocol == SNAT_PROTOCOL_ICMP ? 0 : udp->src_port;
234  if (lb_nat)
236  if (lb_nat == AFFINITY_LB_NAT)
237  s->flags |= SNAT_SESSION_FLAG_AFFINITY;
239  s->out2in = e_key;
240  s->in2out = l_key;
241  s->in2out.protocol = s->out2in.protocol;
242  user_session_increment (sm, u, 1);
243 
244  /* Add to lookup tables */
245  make_ed_kv (&kv, &e_key.addr, &s->ext_host_addr, ip->protocol,
246  e_key.fib_index, e_key.port, s->ext_host_port);
247  kv.value = s - tsm->sessions;
248  ctx.now = now;
249  ctx.thread_index = thread_index;
250  if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, &kv,
252  &ctx))
253  nat_elog_notice ("out2in-ed key add failed");
254 
255  if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
256  ip->src_address.as_u32 == l_key.addr.as_u32))
257  {
258  eh_key.protocol = e_key.protocol;
260  thread_index, &eh_key,
261  sm->port_per_thread,
262  tsm->snat_thread_index))
263  {
264  b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
265  nat44_delete_session (sm, s, thread_index);
266  if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 0))
267  nat_elog_notice ("out2in-ed key del failed");
268  return 0;
269  }
270  s->ext_host_nat_addr.as_u32 = eh_key.addr.as_u32;
271  s->ext_host_nat_port = eh_key.port;
272  s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
273  make_ed_kv (&kv, &l_key.addr, &s->ext_host_nat_addr, ip->protocol,
274  l_key.fib_index, l_key.port, s->ext_host_nat_port);
275  }
276  else
277  {
278  make_ed_kv (&kv, &l_key.addr, &s->ext_host_addr, ip->protocol,
279  l_key.fib_index, l_key.port, s->ext_host_port);
280  }
281  kv.value = s - tsm->sessions;
282  if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
284  &ctx))
285  nat_elog_notice ("in2out-ed key add failed");
286 
288  s->in2out.addr.as_u32,
289  s->out2in.addr.as_u32,
290  s->in2out.protocol,
291  s->in2out.port,
292  s->out2in.port, s->in2out.fib_index);
293 
294  nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
295  &s->in2out.addr, s->in2out.port,
296  &s->ext_host_nat_addr, s->ext_host_nat_port,
297  &s->out2in.addr, s->out2in.port,
298  &s->ext_host_addr, s->ext_host_port,
299  s->in2out.protocol, is_twice_nat_session (s));
300 
301  nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
302  s->out2in.port, &s->ext_host_addr, s->ext_host_port,
303  &s->ext_host_nat_addr, s->ext_host_nat_port,
304  s->in2out.protocol, s->in2out.fib_index, s->flags,
305  thread_index, 0);
306 
307  return s;
308 }
309 
310 static int
312  u16 dst_port, u32 thread_index, u32 rx_fib_index)
313 {
315  snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
316 
317  make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto,
318  rx_fib_index, src_port, dst_port);
319  if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
320  return 1;
321 
322  return 0;
323 }
324 
325 static void
327  u32 thread_index)
328 {
331  udp_header_t *udp;
332  snat_user_t *u;
333  snat_session_t *s = 0;
334  snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
335  f64 now = vlib_time_now (sm->vlib_main);
336 
337  if (ip->protocol == IP_PROTOCOL_ICMP)
338  {
339  if (get_icmp_o2i_ed_key (ip, &key))
340  return;
341  }
342  else if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
343  {
344  udp = ip4_next_header (ip);
345  key.r_addr = ip->src_address;
346  key.l_addr = ip->dst_address;
347  key.proto = ip->protocol;
348  key.l_port = udp->dst_port;
349  key.r_port = udp->src_port;
350  }
351  else
352  {
353  key.r_addr = ip->src_address;
354  key.l_addr = ip->dst_address;
355  key.proto = ip->protocol;
356  key.l_port = key.r_port = 0;
357  }
358  key.fib_index = 0;
359  kv.key[0] = key.as_u64[0];
360  kv.key[1] = key.as_u64[1];
361 
362  if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
363  {
364  s = pool_elt_at_index (tsm->sessions, value.value);
365  }
366  else
367  {
368  u32 proto;
369 
370  if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
371  return;
372 
374  thread_index);
375  if (!u)
376  {
377  nat_elog_warn ("create NAT user failed");
378  return;
379  }
380 
381  s = nat_ed_session_alloc (sm, u, thread_index, now);
382  if (!s)
383  {
384  nat44_delete_user_with_no_session (sm, u, thread_index);
385  nat_elog_warn ("create NAT session failed");
386  return;
387  }
388 
389  proto = ip_proto_to_snat_proto (key.proto);
390 
391  s->ext_host_addr = key.r_addr;
392  s->ext_host_port = key.r_port;
393  s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
394  s->out2in.addr = key.l_addr;
395  s->out2in.port = key.l_port;
396  s->out2in.protocol = proto;
397  if (proto == ~0)
398  {
400  s->out2in.port = ip->protocol;
401  }
402  s->out2in.fib_index = 0;
403  s->in2out = s->out2in;
404  user_session_increment (sm, u, 0);
405 
406  kv.value = s - tsm->sessions;
407  if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
408  nat_elog_notice ("in2out_ed key add failed");
409  }
410 
411  if (ip->protocol == IP_PROTOCOL_TCP)
412  {
413  tcp_header_t *tcp = ip4_next_header (ip);
414  if (nat44_set_tcp_session_state_o2i (sm, s, tcp, thread_index))
415  return;
416  }
417 
418  /* Accounting */
419  nat44_session_update_counters (s, now, 0, thread_index);
420  /* Per-user LRU list maintenance */
421  nat44_session_update_lru (sm, s, thread_index);
422 }
423 
424 static inline void
426  u32 rx_fib_index)
427 {
428  ip4_header_t ip_wkr = {
429  .src_address = ip->dst_address,
430  };
431  u32 thread_index = sm->worker_in2out_cb (&ip_wkr, rx_fib_index, 0);
432 
433  create_bypass_for_fwd (sm, ip, rx_fib_index, thread_index);
434 }
435 
436 #ifndef CLIB_MARCH_VARIANT
437 u32
439  u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
440  u8 * p_proto, snat_session_key_t * p_value,
441  u8 * p_dont_translate, void *d, void *e)
442 {
443  u32 next = ~0, sw_if_index, rx_fib_index;
444  icmp46_header_t *icmp;
447  snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
448  snat_session_t *s = 0;
449  u8 dont_translate = 0, is_addr_only, identity_nat;
450  snat_session_key_t e_key, l_key;
451 
452  icmp = (icmp46_header_t *) ip4_next_header (ip);
453  sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
455 
456  if (get_icmp_o2i_ed_key (ip, &key))
457  {
458  b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
459  next = NAT_NEXT_DROP;
460  goto out;
461  }
462  key.fib_index = rx_fib_index;
463  kv.key[0] = key.as_u64[0];
464  kv.key[1] = key.as_u64[1];
465 
466  if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
467  {
468  /* Try to match static mapping */
469  e_key.addr = ip->dst_address;
470  e_key.port = key.l_port;
471  e_key.protocol = ip_proto_to_snat_proto (key.proto);
472  e_key.fib_index = rx_fib_index;
474  (sm, e_key, &l_key, 1, &is_addr_only, 0, 0, 0, &identity_nat))
475  {
476  if (!sm->forwarding_enabled)
477  {
478  /* Don't NAT packet aimed at the intfc address */
480  ip->dst_address.as_u32)))
481  {
482  dont_translate = 1;
483  goto out;
484  }
485  b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
486  next = NAT_NEXT_DROP;
487  goto out;
488  }
489  else
490  {
491  dont_translate = 1;
492  if (next_src_nat (sm, ip, key.proto, key.l_port, key.r_port,
493  thread_index, rx_fib_index))
494  {
496  goto out;
497  }
498  if (sm->num_workers > 1)
499  create_bypass_for_fwd_worker (sm, ip, rx_fib_index);
500  else
501  create_bypass_for_fwd (sm, ip, rx_fib_index, thread_index);
502  goto out;
503  }
504  }
505 
506  if (PREDICT_FALSE (icmp->type != ICMP4_echo_reply &&
507  (icmp->type != ICMP4_echo_request || !is_addr_only)))
508  {
509  b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
510  next = NAT_NEXT_DROP;
511  goto out;
512  }
513 
514  if (PREDICT_FALSE (identity_nat))
515  {
516  dont_translate = 1;
517  goto out;
518  }
519 
520  /* Create session initiated by host from external network */
521  s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
522  thread_index, 0, 0,
524  (sm->vlib_main));
525 
526  if (!s)
527  {
528  next = NAT_NEXT_DROP;
529  goto out;
530  }
531  }
532  else
533  {
534  if (PREDICT_FALSE (icmp->type != ICMP4_echo_reply &&
535  icmp->type != ICMP4_echo_request &&
536  !icmp_is_error_message (icmp)))
537  {
538  b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
539  next = NAT_NEXT_DROP;
540  goto out;
541  }
542 
543  s = pool_elt_at_index (tsm->sessions, value.value);
544  }
545 
546  *p_proto = ip_proto_to_snat_proto (key.proto);
547 out:
548  if (s)
549  *p_value = s->in2out;
550  *p_dont_translate = dont_translate;
551  if (d)
552  *(snat_session_t **) d = s;
553  return next;
554 }
555 #endif
556 
557 static snat_session_t *
559  vlib_buffer_t * b,
560  ip4_header_t * ip,
561  u32 rx_fib_index,
562  u32 thread_index,
563  f64 now,
564  vlib_main_t * vm, vlib_node_runtime_t * node)
565 {
567  clib_bihash_kv_16_8_t s_kv, s_value;
569  u32 old_addr, new_addr;
570  ip_csum_t sum;
571  snat_session_t *s;
572  snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
573  snat_user_t *u;
574 
575  old_addr = ip->dst_address.as_u32;
576 
577  make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
578  rx_fib_index, 0, 0);
579 
580  if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
581  {
582  s = pool_elt_at_index (tsm->sessions, s_value.value);
583  new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
584  }
585  else
586  {
587  if (PREDICT_FALSE (maximum_sessions_exceeded (sm, thread_index)))
588  {
589  b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
590  nat_elog_notice ("maximum sessions exceeded");
591  return 0;
592  }
593 
594  make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
595  if (clib_bihash_search_8_8
596  (&sm->static_mapping_by_external, &kv, &value))
597  {
598  b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
599  return 0;
600  }
601 
602  m = pool_elt_at_index (sm->static_mappings, value.value);
603 
604  new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
605 
607  thread_index);
608  if (!u)
609  {
610  nat_elog_warn ("create NAT user failed");
611  return 0;
612  }
613 
614  /* Create a new session */
615  s = nat_ed_session_alloc (sm, u, thread_index, now);
616  if (!s)
617  {
618  nat44_delete_user_with_no_session (sm, u, thread_index);
619  nat_elog_warn ("create NAT session failed");
620  return 0;
621  }
622 
623  s->ext_host_addr.as_u32 = ip->src_address.as_u32;
627  s->out2in.addr.as_u32 = old_addr;
628  s->out2in.fib_index = rx_fib_index;
629  s->in2out.addr.as_u32 = new_addr;
630  s->in2out.fib_index = m->fib_index;
631  s->in2out.port = s->out2in.port = ip->protocol;
632  user_session_increment (sm, u, 1);
633 
634  /* Add to lookup tables */
635  s_kv.value = s - tsm->sessions;
636  if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
637  nat_elog_notice ("out2in key add failed");
638 
639  make_ed_kv (&s_kv, &ip->dst_address, &ip->src_address, ip->protocol,
640  m->fib_index, 0, 0);
641  s_kv.value = s - tsm->sessions;
642  if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
643  nat_elog_notice ("in2out key add failed");
644  }
645 
646  /* Update IP checksum */
647  sum = ip->checksum;
648  sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
649  ip->checksum = ip_csum_fold (sum);
650 
651  vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
652 
653  /* Accounting */
655  thread_index);
656  /* Per-user LRU list maintenance */
657  nat44_session_update_lru (sm, s, thread_index);
658 
659  return s;
660 }
661 
662 static inline uword
664  vlib_node_runtime_t * node,
665  vlib_frame_t * frame, int is_slow_path)
666 {
667  u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
668  nat_next_t next_index;
669  snat_main_t *sm = &snat_main;
670  f64 now = vlib_time_now (vm);
671  u32 thread_index = vm->thread_index;
672  snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
673  u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
674  0, fragments = 0;
675 
676  stats_node_index = is_slow_path ? sm->ed_out2in_slowpath_node_index :
678 
679  from = vlib_frame_vector_args (frame);
680  n_left_from = frame->n_vectors;
681  next_index = node->cached_next_index;
682 
683  while (n_left_from > 0)
684  {
685  u32 n_left_to_next;
686 
687  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
688 
689  while (n_left_from >= 4 && n_left_to_next >= 2)
690  {
691  u32 bi0, bi1;
692  vlib_buffer_t *b0, *b1;
693  u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
694  new_addr0;
695  u32 next1, sw_if_index1, rx_fib_index1, proto1, old_addr1,
696  new_addr1;
697  u16 old_port0, new_port0, old_port1, new_port1;
698  ip4_header_t *ip0, *ip1;
699  udp_header_t *udp0, *udp1;
700  tcp_header_t *tcp0, *tcp1;
701  icmp46_header_t *icmp0, *icmp1;
702  snat_session_t *s0 = 0, *s1 = 0;
703  clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
704  ip_csum_t sum0, sum1;
705  snat_session_key_t e_key0, l_key0, e_key1, l_key1;
706  lb_nat_type_t lb_nat0, lb_nat1;
707  twice_nat_type_t twice_nat0, twice_nat1;
708  u8 identity_nat0, identity_nat1;
709 
710  /* Prefetch next iteration. */
711  {
712  vlib_buffer_t *p2, *p3;
713 
714  p2 = vlib_get_buffer (vm, from[2]);
715  p3 = vlib_get_buffer (vm, from[3]);
716 
717  vlib_prefetch_buffer_header (p2, LOAD);
718  vlib_prefetch_buffer_header (p3, LOAD);
719 
722  }
723 
724  /* speculatively enqueue b0 and b1 to the current next frame */
725  to_next[0] = bi0 = from[0];
726  to_next[1] = bi1 = from[1];
727  from += 2;
728  to_next += 2;
729  n_left_from -= 2;
730  n_left_to_next -= 2;
731 
732  b0 = vlib_get_buffer (vm, bi0);
733  b1 = vlib_get_buffer (vm, bi1);
734 
735  next0 = nat_buffer_opaque (b0)->arc_next;
736  next1 = nat_buffer_opaque (b1)->arc_next;
737 
738  vnet_buffer (b0)->snat.flags = 0;
739  ip0 = vlib_buffer_get_current (b0);
740 
741  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
742  rx_fib_index0 =
744  sw_if_index0);
745 
746  if (PREDICT_FALSE (ip0->ttl == 1))
747  {
748  vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
749  icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
750  ICMP4_time_exceeded_ttl_exceeded_in_transit,
751  0);
752  next0 = NAT_NEXT_ICMP_ERROR;
753  goto trace00;
754  }
755 
756  udp0 = ip4_next_header (ip0);
757  tcp0 = (tcp_header_t *) udp0;
758  icmp0 = (icmp46_header_t *) udp0;
759  proto0 = ip_proto_to_snat_proto (ip0->protocol);
760 
761  if (is_slow_path)
762  {
763  if (PREDICT_FALSE (proto0 == ~0))
764  {
765  s0 =
766  nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
767  thread_index, now, vm,
768  node);
769  other_packets++;
770  if (!sm->forwarding_enabled)
771  {
772  if (!s0)
773  next0 = NAT_NEXT_DROP;
774  goto trace00;
775  }
776  }
777 
778  if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
779  {
781  (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
782  next0, now, thread_index, &s0);
783  icmp_packets++;
784  goto trace00;
785  }
786  }
787  else
788  {
789  if (PREDICT_FALSE (proto0 == ~0))
790  {
792  goto trace00;
793  }
794 
795  if (ip4_is_fragment (ip0))
796  {
797  next0 = NAT_NEXT_OUT2IN_ED_REASS;
798  fragments++;
799  goto trace00;
800  }
801 
802  if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
803  {
805  goto trace00;
806  }
807  }
808 
809  make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
810  ip0->protocol, rx_fib_index0, udp0->dst_port,
811  udp0->src_port);
812 
813  if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
814  {
815  if (is_slow_path)
816  {
817  /* Try to match static mapping by external address and port,
818  destination address and port in packet */
819  e_key0.addr = ip0->dst_address;
820  e_key0.port = udp0->dst_port;
821  e_key0.protocol = proto0;
822  e_key0.fib_index = rx_fib_index0;
823  if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
824  &twice_nat0, &lb_nat0,
825  &ip0->src_address,
826  &identity_nat0))
827  {
828  /*
829  * Send DHCP packets to the ipv4 stack, or we won't
830  * be able to use dhcp client on the outside interface
831  */
832  if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
833  && (udp0->dst_port ==
834  clib_host_to_net_u16
835  (UDP_DST_PORT_dhcp_to_client))))
836  {
837  goto trace00;
838  }
839 
840  if (!sm->forwarding_enabled)
841  {
842  b0->error =
843  node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
844  next0 = NAT_NEXT_DROP;
845  }
846  else
847  {
848  if (next_src_nat (sm, ip0, ip0->protocol,
849  udp0->src_port, udp0->dst_port,
850  thread_index, rx_fib_index0))
851  {
853  goto trace00;
854  }
855  if (sm->num_workers > 1)
857  rx_fib_index0);
858  else
859  create_bypass_for_fwd (sm, ip0, rx_fib_index0,
860  thread_index);
861  }
862  goto trace00;
863  }
864 
865  if (PREDICT_FALSE (identity_nat0))
866  goto trace00;
867 
868  if ((proto0 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp0))
869  {
870  b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
871  next0 = NAT_NEXT_DROP;
872  goto trace00;
873  }
874 
875  /* Create session initiated by host from external network */
876  s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
877  e_key0, node,
878  thread_index,
879  twice_nat0,
880  lb_nat0, now);
881 
882  if (!s0)
883  {
884  next0 = NAT_NEXT_DROP;
885  goto trace00;
886  }
887  }
888  else
889  {
891  goto trace00;
892  }
893  }
894  else
895  {
896  s0 = pool_elt_at_index (tsm->sessions, value0.value);
897  }
898 
899  old_addr0 = ip0->dst_address.as_u32;
900  new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
901  vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
902 
903  sum0 = ip0->checksum;
904  sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
905  dst_address);
907  sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
908  s0->ext_host_nat_addr.as_u32, ip4_header_t,
909  src_address);
910  ip0->checksum = ip_csum_fold (sum0);
911 
912  old_port0 = udp0->dst_port;
913  new_port0 = udp0->dst_port = s0->in2out.port;
914 
915  if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
916  {
917  sum0 = tcp0->checksum;
918  sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
919  dst_address);
920  sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
921  length);
922  if (is_twice_nat_session (s0))
923  {
924  sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
925  s0->ext_host_nat_addr.as_u32,
926  ip4_header_t, dst_address);
927  sum0 = ip_csum_update (sum0, tcp0->src_port,
928  s0->ext_host_nat_port, ip4_header_t,
929  length);
930  tcp0->src_port = s0->ext_host_nat_port;
931  ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
932  }
933  tcp0->checksum = ip_csum_fold (sum0);
934  tcp_packets++;
936  (sm, s0, tcp0, thread_index))
937  goto trace00;
938  }
939  else if (udp0->checksum)
940  {
941  sum0 = udp0->checksum;
942  sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
943  dst_address);
944  sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
945  length);
947  {
948  sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
949  s0->ext_host_nat_addr.as_u32,
950  ip4_header_t, dst_address);
951  sum0 = ip_csum_update (sum0, udp0->src_port,
952  s0->ext_host_nat_port, ip4_header_t,
953  length);
954  udp0->src_port = s0->ext_host_nat_port;
955  ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
956  }
957  udp0->checksum = ip_csum_fold (sum0);
958  udp_packets++;
959  }
960  else
961  {
963  {
964  udp0->src_port = s0->ext_host_nat_port;
965  ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
966  }
967  udp_packets++;
968  }
969 
970  /* Accounting */
973  thread_index);
974  /* Per-user LRU list maintenance */
975  nat44_session_update_lru (sm, s0, thread_index);
976 
977  trace00:
979  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
980  {
982  vlib_add_trace (vm, node, b0, sizeof (*t));
983  t->is_slow_path = is_slow_path;
984  t->sw_if_index = sw_if_index0;
985  t->next_index = next0;
986  t->session_index = ~0;
987  if (s0)
988  t->session_index = s0 - tsm->sessions;
989  }
990 
991  pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
992 
993  vnet_buffer (b1)->snat.flags = 0;
994  ip1 = vlib_buffer_get_current (b1);
995 
996  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
997  rx_fib_index1 =
999  sw_if_index1);
1000 
1001  if (PREDICT_FALSE (ip1->ttl == 1))
1002  {
1003  vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1004  icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1005  ICMP4_time_exceeded_ttl_exceeded_in_transit,
1006  0);
1007  next1 = NAT_NEXT_ICMP_ERROR;
1008  goto trace01;
1009  }
1010 
1011  udp1 = ip4_next_header (ip1);
1012  tcp1 = (tcp_header_t *) udp1;
1013  icmp1 = (icmp46_header_t *) udp1;
1014  proto1 = ip_proto_to_snat_proto (ip1->protocol);
1015 
1016  if (is_slow_path)
1017  {
1018  if (PREDICT_FALSE (proto1 == ~0))
1019  {
1020  s1 =
1021  nat44_ed_out2in_unknown_proto (sm, b1, ip1, rx_fib_index1,
1022  thread_index, now, vm,
1023  node);
1024  other_packets++;
1025  if (!sm->forwarding_enabled)
1026  {
1027  if (!s1)
1028  next1 = NAT_NEXT_DROP;
1029  goto trace01;
1030  }
1031  }
1032 
1033  if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1034  {
1035  next1 = icmp_out2in_ed_slow_path
1036  (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1037  next1, now, thread_index, &s1);
1038  icmp_packets++;
1039  goto trace01;
1040  }
1041  }
1042  else
1043  {
1044  if (PREDICT_FALSE (proto1 == ~0))
1045  {
1047  goto trace01;
1048  }
1049 
1050  if (ip4_is_fragment (ip1))
1051  {
1052  next1 = NAT_NEXT_OUT2IN_ED_REASS;
1053  fragments++;
1054  goto trace01;
1055  }
1056 
1057  if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1058  {
1060  goto trace01;
1061  }
1062  }
1063 
1064  make_ed_kv (&kv1, &ip1->dst_address, &ip1->src_address,
1065  ip1->protocol, rx_fib_index1, udp1->dst_port,
1066  udp1->src_port);
1067 
1068  if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv1, &value1))
1069  {
1070  if (is_slow_path)
1071  {
1072  /* Try to match static mapping by external address and port,
1073  destination address and port in packet */
1074  e_key1.addr = ip1->dst_address;
1075  e_key1.port = udp1->dst_port;
1076  e_key1.protocol = proto1;
1077  e_key1.fib_index = rx_fib_index1;
1078  if (snat_static_mapping_match (sm, e_key1, &l_key1, 1, 0,
1079  &twice_nat1, &lb_nat1,
1080  &ip1->src_address,
1081  &identity_nat1))
1082  {
1083  /*
1084  * Send DHCP packets to the ipv4 stack, or we won't
1085  * be able to use dhcp client on the outside interface
1086  */
1087  if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_UDP
1088  && (udp1->dst_port ==
1089  clib_host_to_net_u16
1090  (UDP_DST_PORT_dhcp_to_client))))
1091  {
1092  goto trace01;
1093  }
1094 
1095  if (!sm->forwarding_enabled)
1096  {
1097  b1->error =
1098  node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1099  next1 = NAT_NEXT_DROP;
1100  }
1101  else
1102  {
1103  if (next_src_nat (sm, ip1, ip1->protocol,
1104  udp1->src_port, udp1->dst_port,
1105  thread_index, rx_fib_index1))
1106  {
1108  goto trace01;
1109  }
1110  if (sm->num_workers > 1)
1112  rx_fib_index1);
1113  else
1114  create_bypass_for_fwd (sm, ip1, rx_fib_index1,
1115  thread_index);
1116  }
1117  goto trace01;
1118  }
1119 
1120  if (PREDICT_FALSE (identity_nat1))
1121  goto trace01;
1122 
1123  if ((proto1 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp1))
1124  {
1125  b1->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1126  next1 = NAT_NEXT_DROP;
1127  goto trace01;
1128  }
1129 
1130  /* Create session initiated by host from external network */
1131  s1 = create_session_for_static_mapping_ed (sm, b1, l_key1,
1132  e_key1, node,
1133  thread_index,
1134  twice_nat1,
1135  lb_nat1, now);
1136 
1137  if (!s1)
1138  {
1139  next1 = NAT_NEXT_DROP;
1140  goto trace01;
1141  }
1142  }
1143  else
1144  {
1146  goto trace01;
1147  }
1148  }
1149  else
1150  {
1151  s1 = pool_elt_at_index (tsm->sessions, value1.value);
1152  }
1153 
1154  old_addr1 = ip1->dst_address.as_u32;
1155  new_addr1 = ip1->dst_address.as_u32 = s1->in2out.addr.as_u32;
1156  vnet_buffer (b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
1157 
1158  sum1 = ip1->checksum;
1159  sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1160  dst_address);
1162  sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1163  s1->ext_host_nat_addr.as_u32, ip4_header_t,
1164  src_address);
1165  ip1->checksum = ip_csum_fold (sum1);
1166 
1167  old_port1 = udp1->dst_port;
1168  new_port1 = udp1->dst_port = s1->in2out.port;
1169 
1170  if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
1171  {
1172  sum1 = tcp1->checksum;
1173  sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1174  dst_address);
1175  sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1176  length);
1177  if (is_twice_nat_session (s1))
1178  {
1179  sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1180  s1->ext_host_nat_addr.as_u32,
1181  ip4_header_t, dst_address);
1182  sum1 = ip_csum_update (sum1, tcp1->src_port,
1183  s1->ext_host_nat_port, ip4_header_t,
1184  length);
1185  tcp1->src_port = s1->ext_host_nat_port;
1186  ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1187  }
1188  tcp1->checksum = ip_csum_fold (sum1);
1189  tcp_packets++;
1191  (sm, s1, tcp1, thread_index))
1192  goto trace01;
1193  }
1194  else if (udp1->checksum)
1195  {
1196  sum1 = udp1->checksum;
1197  sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t,
1198  dst_address);
1199  sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t,
1200  length);
1202  {
1203  sum1 = ip_csum_update (sum1, ip1->src_address.as_u32,
1204  s1->ext_host_nat_addr.as_u32,
1205  ip4_header_t, dst_address);
1206  sum1 = ip_csum_update (sum1, udp1->src_port,
1207  s1->ext_host_nat_port, ip4_header_t,
1208  length);
1209  udp1->src_port = s1->ext_host_nat_port;
1210  ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1211  }
1212  udp1->checksum = ip_csum_fold (sum1);
1213  udp_packets++;
1214  }
1215  else
1216  {
1218  {
1219  udp1->src_port = s1->ext_host_nat_port;
1220  ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32;
1221  }
1222  udp_packets++;
1223  }
1224 
1225  /* Accounting */
1227  vlib_buffer_length_in_chain (vm, b1),
1228  thread_index);
1229  /* Per-user LRU list maintenance */
1230  nat44_session_update_lru (sm, s1, thread_index);
1231 
1232  trace01:
1234  && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1235  {
1237  vlib_add_trace (vm, node, b1, sizeof (*t));
1238  t->is_slow_path = is_slow_path;
1239  t->sw_if_index = sw_if_index1;
1240  t->next_index = next1;
1241  t->session_index = ~0;
1242  if (s1)
1243  t->session_index = s1 - tsm->sessions;
1244  }
1245 
1246  pkts_processed += next1 == nat_buffer_opaque (b1)->arc_next;
1247 
1248  /* verify speculative enqueues, maybe switch current next frame */
1249  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1250  to_next, n_left_to_next,
1251  bi0, bi1, next0, next1);
1252  }
1253 
1254  while (n_left_from > 0 && n_left_to_next > 0)
1255  {
1256  u32 bi0;
1257  vlib_buffer_t *b0;
1258  u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
1259  new_addr0;
1260  u16 old_port0, new_port0;
1261  ip4_header_t *ip0;
1262  udp_header_t *udp0;
1263  tcp_header_t *tcp0;
1264  icmp46_header_t *icmp0;
1265  snat_session_t *s0 = 0;
1266  clib_bihash_kv_16_8_t kv0, value0;
1267  ip_csum_t sum0;
1268  snat_session_key_t e_key0, l_key0;
1269  lb_nat_type_t lb_nat0;
1270  twice_nat_type_t twice_nat0;
1271  u8 identity_nat0;
1272 
1273  /* speculatively enqueue b0 to the current next frame */
1274  bi0 = from[0];
1275  to_next[0] = bi0;
1276  from += 1;
1277  to_next += 1;
1278  n_left_from -= 1;
1279  n_left_to_next -= 1;
1280 
1281  b0 = vlib_get_buffer (vm, bi0);
1282  next0 = nat_buffer_opaque (b0)->arc_next;
1283 
1284  vnet_buffer (b0)->snat.flags = 0;
1285  ip0 = vlib_buffer_get_current (b0);
1286 
1287  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1288  rx_fib_index0 =
1290  sw_if_index0);
1291 
1292  if (PREDICT_FALSE (ip0->ttl == 1))
1293  {
1294  vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1295  icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1296  ICMP4_time_exceeded_ttl_exceeded_in_transit,
1297  0);
1298  next0 = NAT_NEXT_ICMP_ERROR;
1299  goto trace0;
1300  }
1301 
1302  udp0 = ip4_next_header (ip0);
1303  tcp0 = (tcp_header_t *) udp0;
1304  icmp0 = (icmp46_header_t *) udp0;
1305  proto0 = ip_proto_to_snat_proto (ip0->protocol);
1306 
1307  if (is_slow_path)
1308  {
1309  if (PREDICT_FALSE (proto0 == ~0))
1310  {
1311  s0 =
1312  nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
1313  thread_index, now, vm,
1314  node);
1315  other_packets++;
1316  if (!sm->forwarding_enabled)
1317  {
1318  if (!s0)
1319  next0 = NAT_NEXT_DROP;
1320  goto trace0;
1321  }
1322  }
1323 
1324  if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1325  {
1326  next0 = icmp_out2in_ed_slow_path
1327  (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1328  next0, now, thread_index, &s0);
1329  icmp_packets++;
1330  goto trace0;
1331  }
1332  }
1333  else
1334  {
1335  if (PREDICT_FALSE (proto0 == ~0))
1336  {
1338  goto trace0;
1339  }
1340 
1341  if (ip4_is_fragment (ip0))
1342  {
1343  next0 = NAT_NEXT_OUT2IN_ED_REASS;
1344  fragments++;
1345  goto trace0;
1346  }
1347 
1348  if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1349  {
1351  goto trace0;
1352  }
1353  }
1354 
1355  make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1356  ip0->protocol, rx_fib_index0, udp0->dst_port,
1357  udp0->src_port);
1358 
1359  if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1360  {
1361  if (is_slow_path)
1362  {
1363  /* Try to match static mapping by external address and port,
1364  destination address and port in packet */
1365  e_key0.addr = ip0->dst_address;
1366  e_key0.port = udp0->dst_port;
1367  e_key0.protocol = proto0;
1368  e_key0.fib_index = rx_fib_index0;
1369  if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1370  &twice_nat0, &lb_nat0,
1371  &ip0->src_address,
1372  &identity_nat0))
1373  {
1374  /*
1375  * Send DHCP packets to the ipv4 stack, or we won't
1376  * be able to use dhcp client on the outside interface
1377  */
1378  if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1379  && (udp0->dst_port ==
1380  clib_host_to_net_u16
1381  (UDP_DST_PORT_dhcp_to_client))))
1382  {
1383  goto trace0;
1384  }
1385 
1386  if (!sm->forwarding_enabled)
1387  {
1388  b0->error =
1389  node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1390  next0 = NAT_NEXT_DROP;
1391  }
1392  else
1393  {
1394  if (next_src_nat (sm, ip0, ip0->protocol,
1395  udp0->src_port, udp0->dst_port,
1396  thread_index, rx_fib_index0))
1397  {
1399  goto trace0;
1400  }
1401  if (sm->num_workers > 1)
1403  rx_fib_index0);
1404  else
1405  create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1406  thread_index);
1407  }
1408  goto trace0;
1409  }
1410 
1411  if (PREDICT_FALSE (identity_nat0))
1412  goto trace0;
1413 
1414  if ((proto0 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp0))
1415  {
1416  b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1417  next0 = NAT_NEXT_DROP;
1418  goto trace0;
1419  }
1420 
1421  /* Create session initiated by host from external network */
1422  s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1423  e_key0, node,
1424  thread_index,
1425  twice_nat0,
1426  lb_nat0, now);
1427 
1428  if (!s0)
1429  {
1430  next0 = NAT_NEXT_DROP;
1431  goto trace0;
1432  }
1433  }
1434  else
1435  {
1437  goto trace0;
1438  }
1439  }
1440  else
1441  {
1442  s0 = pool_elt_at_index (tsm->sessions, value0.value);
1443  }
1444 
1445  old_addr0 = ip0->dst_address.as_u32;
1446  new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1447  vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1448 
1449  sum0 = ip0->checksum;
1450  sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1451  dst_address);
1453  sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1454  s0->ext_host_nat_addr.as_u32, ip4_header_t,
1455  src_address);
1456  ip0->checksum = ip_csum_fold (sum0);
1457 
1458  old_port0 = udp0->dst_port;
1459  new_port0 = udp0->dst_port = s0->in2out.port;
1460 
1461  if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1462  {
1463  sum0 = tcp0->checksum;
1464  sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1465  dst_address);
1466  sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1467  length);
1468  if (is_twice_nat_session (s0))
1469  {
1470  sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1471  s0->ext_host_nat_addr.as_u32,
1472  ip4_header_t, dst_address);
1473  sum0 = ip_csum_update (sum0, tcp0->src_port,
1474  s0->ext_host_nat_port, ip4_header_t,
1475  length);
1476  tcp0->src_port = s0->ext_host_nat_port;
1477  ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1478  }
1479  tcp0->checksum = ip_csum_fold (sum0);
1480  tcp_packets++;
1482  (sm, s0, tcp0, thread_index))
1483  goto trace0;
1484  }
1485  else if (udp0->checksum)
1486  {
1487  sum0 = udp0->checksum;
1488  sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1489  dst_address);
1490  sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1491  length);
1493  {
1494  sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1495  s0->ext_host_nat_addr.as_u32,
1496  ip4_header_t, dst_address);
1497  sum0 = ip_csum_update (sum0, udp0->src_port,
1498  s0->ext_host_nat_port, ip4_header_t,
1499  length);
1500  udp0->src_port = s0->ext_host_nat_port;
1501  ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1502  }
1503  udp0->checksum = ip_csum_fold (sum0);
1504  udp_packets++;
1505  }
1506  else
1507  {
1509  {
1510  udp0->src_port = s0->ext_host_nat_port;
1511  ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1512  }
1513  udp_packets++;
1514  }
1515 
1516  /* Accounting */
1518  vlib_buffer_length_in_chain (vm, b0),
1519  thread_index);
1520  /* Per-user LRU list maintenance */
1521  nat44_session_update_lru (sm, s0, thread_index);
1522 
1523  trace0:
1525  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1526  {
1528  vlib_add_trace (vm, node, b0, sizeof (*t));
1529  t->is_slow_path = is_slow_path;
1530  t->sw_if_index = sw_if_index0;
1531  t->next_index = next0;
1532  t->session_index = ~0;
1533  if (s0)
1534  t->session_index = s0 - tsm->sessions;
1535  }
1536 
1537  pkts_processed += next0 == nat_buffer_opaque (b0)->arc_next;
1538  /* verify speculative enqueue, maybe switch current next frame */
1539  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1540  to_next, n_left_to_next,
1541  bi0, next0);
1542  }
1543 
1544  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1545  }
1546 
1547  vlib_node_increment_counter (vm, stats_node_index,
1548  NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1549  pkts_processed);
1550  vlib_node_increment_counter (vm, stats_node_index,
1551  NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
1552  vlib_node_increment_counter (vm, stats_node_index,
1553  NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
1554  vlib_node_increment_counter (vm, stats_node_index,
1555  NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
1556  icmp_packets);
1557  vlib_node_increment_counter (vm, stats_node_index,
1558  NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
1559  other_packets);
1560  vlib_node_increment_counter (vm, stats_node_index,
1561  NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
1562  return frame->n_vectors;
1563 }
1564 
1565 static inline uword
1567  vlib_node_runtime_t * node,
1568  vlib_frame_t * frame)
1569 {
1570  u32 n_left_from, *from, *to_next;
1571  nat_next_t next_index;
1572  u32 pkts_processed = 0;
1573  snat_main_t *sm = &snat_main;
1574  f64 now = vlib_time_now (vm);
1575  u32 thread_index = vm->thread_index;
1577  &sm->per_thread_data[thread_index];
1578  u32 *fragments_to_drop = 0;
1579  u32 *fragments_to_loopback = 0;
1580 
1581  from = vlib_frame_vector_args (frame);
1582  n_left_from = frame->n_vectors;
1583  next_index = node->cached_next_index;
1584 
1585  while (n_left_from > 0)
1586  {
1587  u32 n_left_to_next;
1588 
1589  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1590 
1591  while (n_left_from > 0 && n_left_to_next > 0)
1592  {
1593  u32 bi0, sw_if_index0, proto0, rx_fib_index0, new_addr0, old_addr0;
1594  vlib_buffer_t *b0;
1595  u32 next0;
1596  u8 cached0 = 0;
1597  ip4_header_t *ip0;
1598  nat_reass_ip4_t *reass0;
1599  udp_header_t *udp0;
1600  tcp_header_t *tcp0;
1601  icmp46_header_t *icmp0;
1602  clib_bihash_kv_16_8_t kv0, value0;
1603  snat_session_t *s0 = 0;
1604  u16 old_port0, new_port0;
1605  ip_csum_t sum0;
1606  snat_session_key_t e_key0, l_key0;
1607  lb_nat_type_t lb0;
1608  twice_nat_type_t twice_nat0;
1609  u8 identity_nat0;
1610 
1611  /* speculatively enqueue b0 to the current next frame */
1612  bi0 = from[0];
1613  to_next[0] = bi0;
1614  from += 1;
1615  to_next += 1;
1616  n_left_from -= 1;
1617  n_left_to_next -= 1;
1618 
1619  b0 = vlib_get_buffer (vm, bi0);
1620  next0 = nat_buffer_opaque (b0)->arc_next;
1621 
1622  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1623  rx_fib_index0 =
1625  sw_if_index0);
1626 
1628  {
1629  next0 = NAT_NEXT_DROP;
1630  b0->error = node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT];
1631  goto trace0;
1632  }
1633 
1634  ip0 = (ip4_header_t *) vlib_buffer_get_current (b0);
1635  udp0 = ip4_next_header (ip0);
1636  tcp0 = (tcp_header_t *) udp0;
1637  icmp0 = (icmp46_header_t *) udp0;
1638  proto0 = ip_proto_to_snat_proto (ip0->protocol);
1639 
1641  ip0->dst_address,
1642  ip0->fragment_id,
1643  ip0->protocol,
1644  1, &fragments_to_drop);
1645 
1646  if (PREDICT_FALSE (!reass0))
1647  {
1648  next0 = NAT_NEXT_DROP;
1649  b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_REASS];
1650  nat_elog_notice ("maximum reassemblies exceeded");
1651  goto trace0;
1652  }
1653 
1655  {
1656  if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1657  {
1658  next0 = icmp_out2in_ed_slow_path
1659  (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1660  next0, now, thread_index, &s0);
1661 
1662  if (PREDICT_TRUE (next0 != NAT_NEXT_DROP))
1663  {
1664  if (s0)
1665  reass0->sess_index = s0 - per_thread_data->sessions;
1666  else
1667  reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1668  reass0->thread_index = thread_index;
1669  nat_ip4_reass_get_frags (reass0,
1670  &fragments_to_loopback);
1671  }
1672 
1673  goto trace0;
1674  }
1675 
1676  make_ed_kv (&kv0, &ip0->dst_address, &ip0->src_address,
1677  ip0->protocol, rx_fib_index0, udp0->dst_port,
1678  udp0->src_port);
1679 
1680  if (clib_bihash_search_16_8
1681  (&per_thread_data->out2in_ed, &kv0, &value0))
1682  {
1683  /* Try to match static mapping by external address and port,
1684  destination address and port in packet */
1685  e_key0.addr = ip0->dst_address;
1686  e_key0.port = udp0->dst_port;
1687  e_key0.protocol = proto0;
1688  e_key0.fib_index = rx_fib_index0;
1689  if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1690  &twice_nat0, &lb0, 0,
1691  &identity_nat0))
1692  {
1693  /*
1694  * Send DHCP packets to the ipv4 stack, or we won't
1695  * be able to use dhcp client on the outside interface
1696  */
1697  if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1698  && (udp0->dst_port
1699  ==
1700  clib_host_to_net_u16
1701  (UDP_DST_PORT_dhcp_to_client))))
1702  {
1703  goto trace0;
1704  }
1705 
1706  if (!sm->forwarding_enabled)
1707  {
1708  b0->error =
1709  node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1710  next0 = NAT_NEXT_DROP;
1711  }
1712  else
1713  {
1714  if (next_src_nat (sm, ip0, ip0->protocol,
1715  udp0->src_port, udp0->dst_port,
1716  thread_index, rx_fib_index0))
1717  {
1719  goto trace0;
1720  }
1721  if (sm->num_workers > 1)
1723  rx_fib_index0);
1724  else
1725  create_bypass_for_fwd (sm, ip0, rx_fib_index0,
1726  thread_index);
1727  reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1728  nat_ip4_reass_get_frags (reass0,
1729  &fragments_to_loopback);
1730  }
1731  goto trace0;
1732  }
1733 
1734  if (PREDICT_FALSE (identity_nat0))
1735  {
1736  reass0->flags |= NAT_REASS_FLAG_ED_DONT_TRANSLATE;
1737  goto trace0;
1738  }
1739 
1740  if ((proto0 == SNAT_PROTOCOL_TCP) && !tcp_is_init (tcp0))
1741  {
1742  b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1743  next0 = NAT_NEXT_DROP;
1744  goto trace0;
1745  }
1746 
1747  /* Create session initiated by host from external network */
1748  s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1749  e_key0, node,
1750  thread_index,
1751  twice_nat0, lb0,
1752  now);
1753  if (!s0)
1754  {
1755  b0->error =
1756  node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1757  next0 = NAT_NEXT_DROP;
1758  goto trace0;
1759  }
1760  reass0->sess_index = s0 - per_thread_data->sessions;
1761  reass0->thread_index = thread_index;
1762  }
1763  else
1764  {
1765  s0 = pool_elt_at_index (per_thread_data->sessions,
1766  value0.value);
1767  reass0->sess_index = value0.value;
1768  }
1769  nat_ip4_reass_get_frags (reass0, &fragments_to_loopback);
1770  }
1771  else
1772  {
1773  if (reass0->flags & NAT_REASS_FLAG_ED_DONT_TRANSLATE)
1774  goto trace0;
1775  if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1776  {
1778  (thread_index, reass0, bi0, &fragments_to_drop))
1779  {
1780  b0->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_FRAG];
1782  ("maximum fragments per reassembly exceeded");
1783  next0 = NAT_NEXT_DROP;
1784  goto trace0;
1785  }
1786  cached0 = 1;
1787  goto trace0;
1788  }
1789  s0 = pool_elt_at_index (per_thread_data->sessions,
1790  reass0->sess_index);
1791  }
1792 
1793  old_addr0 = ip0->dst_address.as_u32;
1794  ip0->dst_address = s0->in2out.addr;
1795  new_addr0 = ip0->dst_address.as_u32;
1796  vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1797 
1798  sum0 = ip0->checksum;
1799  sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1800  ip4_header_t,
1801  dst_address /* changed member */ );
1803  sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1804  s0->ext_host_nat_addr.as_u32, ip4_header_t,
1805  src_address);
1806  ip0->checksum = ip_csum_fold (sum0);
1807 
1809  {
1810  old_port0 = udp0->dst_port;
1811  new_port0 = udp0->dst_port = s0->in2out.port;
1812 
1813  if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1814  {
1815  sum0 = tcp0->checksum;
1816  sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1817  ip4_header_t,
1818  dst_address /* changed member */ );
1819 
1820  sum0 = ip_csum_update (sum0, old_port0, new_port0,
1821  ip4_header_t /* cheat */ ,
1822  length /* changed member */ );
1823  if (is_twice_nat_session (s0))
1824  {
1825  sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1826  s0->ext_host_nat_addr.as_u32,
1827  ip4_header_t, dst_address);
1828  sum0 = ip_csum_update (sum0, tcp0->src_port,
1829  s0->ext_host_nat_port,
1830  ip4_header_t, length);
1831  tcp0->src_port = s0->ext_host_nat_port;
1832  ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1833  }
1834  tcp0->checksum = ip_csum_fold (sum0);
1835  }
1836  else if (udp0->checksum)
1837  {
1838  sum0 = udp0->checksum;
1839  sum0 =
1840  ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1841  dst_address);
1842  sum0 =
1843  ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1844  length);
1846  {
1847  sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1848  s0->ext_host_nat_addr.as_u32,
1849  ip4_header_t, dst_address);
1850  sum0 = ip_csum_update (sum0, udp0->src_port,
1851  s0->ext_host_nat_port,
1852  ip4_header_t, length);
1853  udp0->src_port = s0->ext_host_nat_port;
1854  ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1855  }
1856  udp0->checksum = ip_csum_fold (sum0);
1857  }
1858  else
1859  {
1861  {
1862  udp0->src_port = s0->ext_host_nat_port;
1863  ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1864  }
1865  }
1866  }
1867 
1868  /* Accounting */
1870  vlib_buffer_length_in_chain (vm, b0),
1871  thread_index);
1872  /* Per-user LRU list maintenance */
1873  nat44_session_update_lru (sm, s0, thread_index);
1874 
1875  trace0:
1877  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1878  {
1879  nat44_reass_trace_t *t =
1880  vlib_add_trace (vm, node, b0, sizeof (*t));
1881  t->cached = cached0;
1882  t->sw_if_index = sw_if_index0;
1883  t->next_index = next0;
1884  }
1885 
1886  if (cached0)
1887  {
1888  n_left_to_next++;
1889  to_next--;
1890  }
1891  else
1892  {
1893  pkts_processed += next0 != NAT_NEXT_DROP;
1894 
1895  /* verify speculative enqueue, maybe switch current next frame */
1896  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1897  to_next, n_left_to_next,
1898  bi0, next0);
1899  }
1900 
1901  if (n_left_from == 0 && vec_len (fragments_to_loopback))
1902  {
1903  from = vlib_frame_vector_args (frame);
1904  u32 len = vec_len (fragments_to_loopback);
1905  if (len <= VLIB_FRAME_SIZE)
1906  {
1907  clib_memcpy_fast (from, fragments_to_loopback,
1908  sizeof (u32) * len);
1909  n_left_from = len;
1910  vec_reset_length (fragments_to_loopback);
1911  }
1912  else
1913  {
1914  clib_memcpy_fast (from, fragments_to_loopback +
1915  (len - VLIB_FRAME_SIZE),
1916  sizeof (u32) * VLIB_FRAME_SIZE);
1917  n_left_from = VLIB_FRAME_SIZE;
1918  _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1919  }
1920  }
1921  }
1922 
1923  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1924  }
1925 
1927  NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1928  pkts_processed);
1929 
1930  nat_send_all_to_node (vm, fragments_to_drop, node,
1931  &node->errors[NAT_OUT2IN_ED_ERROR_DROP_FRAGMENT],
1932  NAT_NEXT_DROP);
1933 
1934  vec_free (fragments_to_drop);
1935  vec_free (fragments_to_loopback);
1936  return frame->n_vectors;
1937 }
1938 
1940  vlib_node_runtime_t * node,
1941  vlib_frame_t * frame)
1942 {
1943  return nat44_ed_out2in_node_fn_inline (vm, node, frame, 0);
1944 }
1945 
1946 /* *INDENT-OFF* */
1948  .name = "nat44-ed-out2in",
1949  .vector_size = sizeof (u32),
1950  .sibling_of = "nat-default",
1951  .format_trace = format_nat44_ed_out2in_trace,
1954  .error_strings = nat_out2in_ed_error_strings,
1955  .runtime_data_bytes = sizeof (snat_runtime_t),
1956 };
1957 /* *INDENT-ON* */
1958 
1960  vlib_node_runtime_t * node,
1961  vlib_frame_t * frame)
1962 {
1963  return nat44_ed_out2in_node_fn_inline (vm, node, frame, 1);
1964 }
1965 
1966 /* *INDENT-OFF* */
1968  .name = "nat44-ed-out2in-slowpath",
1969  .vector_size = sizeof (u32),
1970  .sibling_of = "nat-default",
1971  .format_trace = format_nat44_ed_out2in_trace,
1974  .error_strings = nat_out2in_ed_error_strings,
1975  .runtime_data_bytes = sizeof (snat_runtime_t),
1976 };
1977 /* *INDENT-ON* */
1978 
1980  vlib_node_runtime_t * node,
1981  vlib_frame_t * frame)
1982 {
1983  return nat44_ed_out2in_reass_node_fn_inline (vm, node, frame);
1984 }
1985 
1986 /* *INDENT-OFF* */
1988  .name = "nat44-ed-out2in-reass",
1989  .vector_size = sizeof (u32),
1990  .sibling_of = "nat-default",
1991  .format_trace = format_nat44_reass_trace,
1994  .error_strings = nat_out2in_ed_error_strings,
1995 };
1996 /* *INDENT-ON* */
1997 
1998 static u8 *
1999 format_nat_pre_trace (u8 * s, va_list * args)
2000 {
2001  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2002  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2003  nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
2004  return format (s, "out2in next_index %d", t->next_index);
2005 }
2006 
2008  vlib_node_runtime_t * node,
2009  vlib_frame_t * frame)
2010 {
2011  return nat_pre_node_fn_inline (vm, node, frame,
2013 }
2014 
2015 /* *INDENT-OFF* */
2017  .name = "nat-pre-out2in",
2018  .vector_size = sizeof (u32),
2019  .sibling_of = "nat-default",
2020  .format_trace = format_nat_pre_trace,
2022  .n_errors = 0,
2023  };
2024 /* *INDENT-ON* */
2025 
2026 /*
2027  * fd.io coding-style-patch-verification: ON
2028  *
2029  * Local Variables:
2030  * eval: (c-set-style "gnu")
2031  * End:
2032  */
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
#define nat_buffer_opaque(b)
Definition: nat.h:73
static uword nat44_ed_out2in_reass_node_fn_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: out2in_ed.c:1566
#define snat_is_session_static(s)
Check if SNAT session is created from static mapping.
Definition: nat.h:763
int nat_ip4_reass_add_fragment(u32 thread_index, nat_reass_ip4_t *reass, u32 bi, u32 **bi_to_drop)
Cache fragment.
Definition: nat_reass.c:392
#define CLIB_UNUSED(x)
Definition: clib.h:83
u32 icmp_out2in(snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0, icmp46_header_t *icmp0, u32 sw_if_index0, u32 rx_fib_index0, vlib_node_runtime_t *node, u32 next0, u32 thread_index, void *d, void *e)
Definition: out2in.c:518
static int next_src_nat(snat_main_t *sm, ip4_header_t *ip, u8 proto, u16 src_port, u16 dst_port, u32 thread_index, u32 rx_fib_index)
Definition: out2in_ed.c:311
a
Definition: bitmap.h:538
ip4_address_t src_address
Definition: ip4_packet.h:170
static u32 nat44_session_get_timeout(snat_main_t *sm, snat_session_t *s)
Definition: nat_inlines.h:390
u32 ed_out2in_node_index
Definition: nat.h:638
#define nat_elog_notice(nat_elog_str)
Definition: nat.h:1017
#define PREDICT_TRUE(x)
Definition: clib.h:113
static_always_inline u8 icmp_is_error_message(icmp46_header_t *icmp)
Definition: nat_inlines.h:174
unsigned long u64
Definition: types.h:89
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:279
u32 fib_table_get_index_for_sw_if_index(fib_protocol_t proto, u32 sw_if_index)
Get the index of the FIB bound to the interface.
Definition: fib_table.c:972
static void make_sm_kv(clib_bihash_kv_8_8_t *kv, ip4_address_t *addr, u8 proto, u32 fib_index, u16 port)
Definition: nat_inlines.h:454
u16 port_per_thread
Definition: nat.h:560
u32 thread_index
Definition: main.h:218
u32 ed_out2in_slowpath_node_index
Definition: nat.h:639
int i
void nat_syslog_nat44_sdel(u32 ssubix, u32 sfibix, ip4_address_t *isaddr, u16 isport, ip4_address_t *idaddr, u16 idport, ip4_address_t *xsaddr, u16 xsport, ip4_address_t *xdaddr, u16 xdport, snat_protocol_t proto, u8 is_twicenat)
Definition: nat_syslog.c:211
uword ip_csum_t
Definition: ip_packet.h:219
#define nat_elog_warn(nat_elog_str)
Definition: nat.h:1019
void nat_ha_sadd(ip4_address_t *in_addr, u16 in_port, ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, ip4_address_t *ehn_addr, u16 ehn_port, u8 proto, u32 fib_index, u16 flags, u32 thread_index, u8 is_resync)
Create session add HA event.
Definition: nat_ha.c:689
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
vlib_node_registration_t nat44_ed_out2in_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_out2in_node)
Definition: out2in_ed.c:1947
#define VLIB_NODE_FN(node)
Definition: node.h:202
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:470
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:366
struct _tcp_header tcp_header_t
#define snat_is_unk_proto_session(s)
Check if SNAT session for unknown protocol.
Definition: nat.h:769
unsigned char u8
Definition: types.h:56
static int nat44_set_tcp_session_state_o2i(snat_main_t *sm, snat_session_t *ses, tcp_header_t *tcp, u32 thread_index)
Definition: nat_inlines.h:358
u16 l_port
Definition: nat.h:110
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
static int ip4_is_fragment(const ip4_header_t *i)
Definition: ip4_packet.h:213
u32 next_index
Definition: nat.h:70
u16 src_port
Definition: udp.api:41
static u32 icmp_out2in_ed_slow_path(snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0, icmp46_header_t *icmp0, u32 sw_if_index0, u32 rx_fib_index0, vlib_node_runtime_t *node, u32 next0, f64 now, u32 thread_index, snat_session_t **p_s0)
Definition: out2in_ed.c:68
u32 ip4_fib_table_get_index_for_sw_if_index(u32 sw_if_index)
Definition: ip4_fib.c:224
#define tcp_is_init(t)
Check if client initiating TCP connection (received SYN from client)
Definition: nat.h:847
static char * nat_out2in_ed_error_strings[]
Definition: out2in_ed.c:35
static_always_inline int get_icmp_o2i_ed_key(ip4_header_t *ip0, nat_ed_ses_key_t *p_key0)
Definition: nat_inlines.h:519
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
u16 r_port
Definition: nat.h:111
ip4_address_t dst_address
Definition: ip4_packet.h:170
lb_nat_type_t
Definition: nat.h:430
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:203
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define NAT_REASS_FLAG_ED_DONT_TRANSLATE
Definition: nat_reass.h:35
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:241
unsigned int u32
Definition: types.h:88
ip4_address_t local_addr
Definition: nat.h:443
#define VLIB_FRAME_SIZE
Definition: node.h:378
u64 as_u64[2]
Definition: nat.h:113
static u8 maximum_sessions_exceeded(snat_main_t *sm, u32 thread_index)
Definition: nat_inlines.h:215
int snat_static_mapping_match(snat_main_t *sm, snat_session_key_t match, snat_session_key_t *mapping, u8 by_external, u8 *is_addr_only, twice_nat_type_t *twice_nat, lb_nat_type_t *lb, ip4_address_t *ext_host_addr, u8 *is_identity_nat)
Match NAT44 static mapping.
Definition: nat.c:2548
void snat_free_outside_address_and_port(snat_address_t *addresses, u32 thread_index, snat_session_key_t *k)
Free outside address and port pair.
Definition: nat.c:2474
static void nat44_delete_session(snat_main_t *sm, snat_session_t *ses, u32 thread_index)
Definition: nat_inlines.h:289
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:136
int nat44_o2i_ed_is_idle_session_cb(clib_bihash_kv_16_8_t *kv, void *arg)
Definition: out2in_ed.c:91
int snat_alloc_outside_address_and_port(snat_address_t *addresses, u32 fib_index, u32 thread_index, snat_session_key_t *k, u16 port_per_thread, u32 snat_thread_index)
Alloc outside address and port.
Definition: nat.c:2707
u32 fib_index
Definition: nat.h:109
snat_user_t * nat_user_get_or_create(snat_main_t *sm, ip4_address_t *addr, u32 fib_index, u32 thread_index)
Find or create NAT user.
Definition: nat.c:323
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
#define foreach_nat_out2in_ed_error
Definition: nat.h:245
clib_bihash_16_8_t out2in_ed
Definition: nat.h:501
long ctx[MAX_CONNS]
Definition: main.c:144
vlib_main_t * vlib_main
Definition: nat.h:698
unsigned short u16
Definition: types.h:57
u16 protocol
Definition: nat.h:94
snat_session_t * nat_ed_session_alloc(snat_main_t *sm, snat_user_t *u, u32 thread_index, f64 now)
Allocate NAT endpoint-dependent session.
Definition: nat.c:445
snat_static_mapping_t * static_mappings
Definition: nat.h:573
u32 inside_fib_index
Definition: nat.h:673
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
void nat_syslog_nat44_sadd(u32 ssubix, u32 sfibix, ip4_address_t *isaddr, u16 isport, ip4_address_t *idaddr, u16 idport, ip4_address_t *xsaddr, u16 xsport, ip4_address_t *xdaddr, u16 xdport, snat_protocol_t proto, u8 is_twicenat)
Definition: nat_syslog.c:199
vlib_node_registration_t nat_pre_out2in_node
(constructor) VLIB_REGISTER_NODE (nat_pre_out2in_node)
Definition: out2in_ed.c:2016
nat_next_t
Definition: nat.h:51
#define PREDICT_FALSE(x)
Definition: clib.h:112
clib_bihash_8_8_t static_mapping_by_external
Definition: nat.h:570
vl_api_address_union_t src_address
Definition: ip_types.api:97
static void create_bypass_for_fwd(snat_main_t *sm, ip4_header_t *ip, u32 rx_fib_index, u32 thread_index)
Definition: out2in_ed.c:326
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Finish enqueueing two buffers forward in the graph.
Definition: buffer_node.h:70
#define SNAT_SESSION_FLAG_UNKNOWN_PROTO
Definition: nat.h:284
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:338
vlib_node_registration_t nat44_ed_out2in_reass_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_out2in_reass_node)
Definition: out2in_ed.c:1987
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1150
u8 len
Definition: ip_types.api:90
snat_main_t snat_main
Definition: nat.c:39
void snat_ipfix_logging_nat44_ses_delete(u32 thread_index, u32 src_ip, u32 nat_src_ip, snat_protocol_t snat_proto, u16 src_port, u16 nat_src_port, u32 vrf_id)
Generate NAT44 session delete event.
u64 value
the value
Definition: bihash_8_8.h:36
static void nat44_delete_user_with_no_session(snat_main_t *sm, snat_user_t *u, u32 thread_index)
Definition: nat_inlines.h:267
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
u32 ed_out2in_reass_node_index
Definition: nat.h:640
static u8 snat_proto_to_ip_proto(snat_protocol_t snat_proto)
Definition: nat_inlines.h:162
u16 n_vectors
Definition: node.h:397
u8 nat_reass_is_drop_frag(u8 is_ip6)
Get status of virtual fragmentation reassembly.
Definition: nat_reass.c:168
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
vlib_main_t * vm
Definition: buffer.c:323
ip4_address_t l_addr
Definition: nat.h:107
snat_get_worker_function_t * worker_in2out_cb
Definition: nat.h:558
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
static void nat44_session_update_counters(snat_session_t *s, f64 now, uword bytes, u32 thread_index)
Definition: nat_inlines.h:413
static u8 * format_nat44_ed_out2in_trace(u8 *s, va_list *args)
Definition: out2in_ed.c:50
static uword nat44_ed_out2in_node_fn_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_slow_path)
Definition: out2in_ed.c:663
u8 data[]
Packet data.
Definition: buffer.h:181
static u8 * format_nat_pre_trace(u8 *s, va_list *args)
Definition: out2in_ed.c:1999
8 octet key, 8 octet key value pair
Definition: bihash_8_8.h:33
#define ARRAY_LEN(x)
Definition: clib.h:63
ip4_address_t addr
Definition: nat.h:92
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:456
vlib_node_registration_t nat44_ed_out2in_slowpath_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node)
Definition: out2in_ed.c:1967
static uword nat_pre_node_fn_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, u32 def_next)
The NAT inline functions.
Definition: nat_inlines.h:27
static void make_ed_kv(clib_bihash_kv_16_8_t *kv, ip4_address_t *l_addr, ip4_address_t *r_addr, u8 proto, u32 fib_index, u16 l_port, u16 r_port)
Definition: nat_inlines.h:437
static snat_session_t * nat44_ed_out2in_unknown_proto(snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip, u32 rx_fib_index, u32 thread_index, f64 now, vlib_main_t *vm, vlib_node_runtime_t *node)
Definition: out2in_ed.c:558
ip4_address_t r_addr
Definition: nat.h:108
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:515
u8 value
Definition: qos.api:53
u32 num_workers
Definition: nat.h:555
Definition: nat.h:425
#define SNAT_SESSION_FLAG_TWICE_NAT
Definition: nat.h:286
static void nat44_session_update_lru(snat_main_t *sm, snat_session_t *s, u32 thread_index)
Per-user LRU list maintenance.
Definition: nat_inlines.h:427
static snat_session_t * create_session_for_static_mapping_ed(snat_main_t *sm, vlib_buffer_t *b, snat_session_key_t l_key, snat_session_key_t e_key, vlib_node_runtime_t *node, u32 thread_index, twice_nat_type_t twice_nat, lb_nat_type_t lb_nat, f64 now)
Definition: out2in_ed.c:188
#define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT
Definition: nat.h:287
ip4_address_t addr
Definition: nat.h:358
snat_address_t * twice_nat_addresses
Definition: nat.h:597
void nat_ha_sdel(ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index, u32 thread_index)
Create session delete HA event.
Definition: nat_ha.c:715
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
Definition: defs.h:47
format_function_t format_nat44_reass_trace
Definition: nat.h:755
static int ip4_is_first_fragment(const ip4_header_t *i)
Definition: ip4_packet.h:220
vl_api_address_t ip
Definition: l2.api:489
#define SNAT_SESSION_FLAG_FWD_BYPASS
Definition: nat.h:288
static u32 ip_proto_to_snat_proto(u8 ip_proto)
Definition: nat_inlines.h:147
int nat44_i2o_ed_is_idle_session_cb(clib_bihash_kv_16_8_t *kv, void *arg)
Definition: in2out_ed.c:68
static void user_session_increment(snat_main_t *sm, snat_user_t *u, u8 is_static)
Definition: nat_inlines.h:255
u32 icmp_match_out2in_ed(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b, ip4_header_t *ip, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Definition: out2in_ed.c:438
twice_nat_type_t
Definition: nat.h:420
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define SNAT_SESSION_FLAG_AFFINITY
Definition: nat.h:289
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
snat_main_per_thread_data_t * per_thread_data
Definition: nat.h:564
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
typedef key
Definition: ipsec.api:247
#define ip_csum_update(sum, old, new, type, field)
Definition: ip_packet.h:269
NAT syslog logging.
static u8 is_interface_addr(snat_main_t *sm, vlib_node_runtime_t *node, u32 sw_if_index0, u32 ip4_addr)
Definition: nat_inlines.h:190
nat_reass_ip4_t * nat_ip4_reass_find_or_create(ip4_address_t src, ip4_address_t dst, u16 frag_id, u8 proto, u8 reset_timeout, u32 **bi_to_drop)
Find or create reassembly.
Definition: nat_reass.c:274
snat_address_t * addresses
Definition: nat.h:580
#define vnet_buffer(b)
Definition: buffer.h:365
#define SNAT_SESSION_FLAG_STATIC_MAPPING
Definition: nat.h:283
u8 forwarding_enabled
Definition: nat.h:656
u16 flags
Copy of main node flags.
Definition: node.h:509
static void nat_send_all_to_node(vlib_main_t *vm, u32 *bi_vector, vlib_node_runtime_t *node, vlib_error_t *error, u32 next)
Definition: nat_inlines.h:225
#define is_twice_nat_session(s)
Check if NAT session is twice NAT.
Definition: nat.h:775
clib_bihash_16_8_t in2out_ed
Definition: nat.h:502
u16 dst_port
Definition: udp.api:42
void nat_ip4_reass_get_frags(nat_reass_ip4_t *reass, u32 **bi)
Get cached fragments.
Definition: nat_reass.c:424
NAT plugin virtual fragmentation reassembly.
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:302
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
snat_session_t * sessions
Definition: nat.h:511
static_always_inline void icmp4_error_set_vnet_buffer(vlib_buffer_t *b, u8 type, u8 code, u32 data)
Definition: icmp4.h:51
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
#define SNAT_SESSION_FLAG_LOAD_BALANCING
Definition: nat.h:285
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:275
Definition: defs.h:46
NAT active-passive HA.
static void create_bypass_for_fwd_worker(snat_main_t *sm, ip4_header_t *ip, u32 rx_fib_index)
Definition: out2in_ed.c:425
u16 fib_index
Definition: nat.h:94
vl_api_fib_path_nh_proto_t proto
Definition: fib_types.api:125
void snat_ipfix_logging_nat44_ses_create(u32 thread_index, u32 src_ip, u32 nat_src_ip, snat_protocol_t snat_proto, u16 src_port, u16 nat_src_port, u32 vrf_id)
Generate NAT44 session create event.