FD.io VPP  v20.09-rc2-28-g3c5414029
Vector Packet Processing
nat44_hairpinning.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 hairpinning
18  */
19 
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/fib/ip4_fib.h>
23 #include <nat/nat.h>
24 #include <nat/nat_inlines.h>
25 
26 typedef enum
27 {
34 
35 typedef enum
36 {
41 
42 typedef struct
43 {
49 
50 static u8 *
51 format_nat_hairpin_trace (u8 * s, va_list * args)
52 {
53  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
54  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
55  nat_hairpin_trace_t *t = va_arg (*args, nat_hairpin_trace_t *);
56 
57  s =
58  format (s, "new dst addr %U port %u fib-index %u", format_ip4_address,
59  &t->addr, clib_net_to_host_u16 (t->port), t->fib_index);
60  if (~0 == t->session_index)
61  {
62  s = format (s, " is-static-mapping");
63  }
64  else
65  {
66  s = format (s, " session-index %u", t->session_index);
67  }
68 
69  return s;
70 }
71 
73 
76 {
77  snat_address_t *ap;
79 
80  /* *INDENT-OFF* */
81  vec_foreach (ap, sm->addresses)
82  {
83  if (ap->addr.as_u32 == dst_addr->as_u32)
84  return 1;
85  }
86  /* *INDENT-ON* */
87 
88  init_nat_k (&kv, *dst_addr, 0, 0, 0);
89  if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
90  return 1;
91 
92  return 0;
93 }
94 
95 #ifndef CLIB_MARCH_VARIANT
96 int
98  snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
99  udp_header_t * udp0, tcp_header_t * tcp0, u32 proto0,
100  int is_ed, int do_trace)
101 {
102  snat_session_t *s0 = NULL;
103  clib_bihash_kv_8_8_t kv0, value0;
104  ip_csum_t sum0;
105  u32 new_dst_addr0 = 0, old_dst_addr0, ti = 0, si = ~0;
106  u16 new_dst_port0 = ~0, old_dst_port0;
107  int rv;
108  ip4_address_t sm0_addr;
109  u16 sm0_port;
110  u32 sm0_fib_index;
111  /* Check if destination is static mappings */
113  (sm, ip0->dst_address, udp0->dst_port, sm->outside_fib_index, proto0,
114  &sm0_addr, &sm0_port, &sm0_fib_index, 1, 0, 0, 0, 0, 0, 0))
115  {
116  new_dst_addr0 = sm0_addr.as_u32;
117  new_dst_port0 = sm0_port;
118  vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0_fib_index;
119  }
120  /* or active session */
121  else
122  {
123  if (sm->num_workers > 1)
124  ti =
125  (clib_net_to_host_u16 (udp0->dst_port) -
126  1024) / sm->port_per_thread;
127  else
128  ti = sm->num_workers;
129 
130  if (is_ed)
131  {
132  clib_bihash_kv_16_8_t ed_kv, ed_value;
133  init_ed_k (&ed_kv, ip0->dst_address, udp0->dst_port,
134  ip0->src_address, udp0->src_port, sm->outside_fib_index,
135  ip0->protocol);
136  rv = clib_bihash_search_16_8 (&sm->out2in_ed, &ed_kv, &ed_value);
137  ASSERT (ti == ed_value_get_thread_index (&ed_value));
138  si = ed_value_get_session_index (&ed_value);
139  }
140  else
141  {
142 
143  init_nat_k (&kv0, ip0->dst_address, udp0->dst_port,
144  sm->outside_fib_index, proto0);
145  rv =
146  clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0,
147  &value0);
148  si = value0.value;
149  }
150  if (rv)
151  {
152  rv = 0;
153  goto trace;
154  }
155 
157  new_dst_addr0 = s0->in2out.addr.as_u32;
158  new_dst_port0 = s0->in2out.port;
159  vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
160  }
161 
162  /* Destination is behind the same NAT, use internal address and port */
163  if (new_dst_addr0)
164  {
165  old_dst_addr0 = ip0->dst_address.as_u32;
166  ip0->dst_address.as_u32 = new_dst_addr0;
167  sum0 = ip0->checksum;
168  sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
169  ip4_header_t, dst_address);
170  ip0->checksum = ip_csum_fold (sum0);
171 
172  old_dst_port0 = tcp0->dst;
173  if (PREDICT_TRUE (new_dst_port0 != old_dst_port0))
174  {
175  if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
176  {
177  tcp0->dst = new_dst_port0;
178  sum0 = tcp0->checksum;
179  sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
180  ip4_header_t, dst_address);
181  sum0 = ip_csum_update (sum0, old_dst_port0, new_dst_port0,
182  ip4_header_t /* cheat */ , length);
183  tcp0->checksum = ip_csum_fold (sum0);
184  }
185  else
186  {
187  udp0->dst_port = new_dst_port0;
188  udp0->checksum = 0;
189  }
190  }
191  else
192  {
193  if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
194  {
195  sum0 = tcp0->checksum;
196  sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
197  ip4_header_t, dst_address);
198  tcp0->checksum = ip_csum_fold (sum0);
199  }
200  }
201  rv = 1;
202  goto trace;
203  }
204  rv = 0;
205 trace:
206  if (do_trace && PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
207  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
208  {
209  nat_hairpin_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
210  t->addr.as_u32 = new_dst_addr0;
211  t->port = new_dst_port0;
212  t->fib_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
213  if (s0)
214  {
215  t->session_index = si;
216  }
217  else
218  {
219  t->session_index = ~0;
220  }
221  }
222  return rv;
223 }
224 #endif
225 
226 #ifndef CLIB_MARCH_VARIANT
227 u32
229  vlib_buffer_t * b0,
230  ip4_header_t * ip0, icmp46_header_t * icmp0, int is_ed)
231 {
232  clib_bihash_kv_8_8_t kv0, value0;
233  u32 old_dst_addr0, new_dst_addr0;
234  u32 old_addr0, new_addr0;
235  u16 old_port0, new_port0;
236  u16 old_checksum0, new_checksum0;
237  u32 si, ti = 0;
238  ip_csum_t sum0;
239  snat_session_t *s0;
241 
243  (vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags))
244  {
245  ip4_header_t *inner_ip0 = 0;
246  tcp_udp_header_t *l4_header = 0;
247 
248  inner_ip0 = (ip4_header_t *) ((icmp_echo_header_t *) (icmp0 + 1) + 1);
249  l4_header = ip4_next_header (inner_ip0);
251 
252  if (protocol != NAT_PROTOCOL_TCP && protocol != NAT_PROTOCOL_UDP)
253  return 1;
254 
255  if (is_ed)
256  {
257  clib_bihash_kv_16_8_t ed_kv, ed_value;
258  init_ed_k (&ed_kv, ip0->dst_address, l4_header->src_port,
259  ip0->src_address, l4_header->dst_port,
260  sm->outside_fib_index, inner_ip0->protocol);
261  if (clib_bihash_search_16_8 (&sm->out2in_ed, &ed_kv, &ed_value))
262  return 1;
263  ASSERT (ti == ed_value_get_thread_index (&ed_value));
264  si = ed_value_get_session_index (&ed_value);
265  }
266  else
267  {
268  init_nat_k (&kv0, ip0->dst_address, l4_header->src_port,
269  sm->outside_fib_index, protocol);
270  if (clib_bihash_search_8_8
271  (&sm->per_thread_data[ti].out2in, &kv0, &value0))
272  return 1;
273  si = value0.value;
274  }
275  s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
276  new_dst_addr0 = s0->in2out.addr.as_u32;
277  vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
278 
279  /* update inner source IP address */
280  old_addr0 = inner_ip0->src_address.as_u32;
281  inner_ip0->src_address.as_u32 = new_dst_addr0;
282  new_addr0 = inner_ip0->src_address.as_u32;
283  sum0 = icmp0->checksum;
284  sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
285  src_address);
286  icmp0->checksum = ip_csum_fold (sum0);
287 
288  /* update inner IP header checksum */
289  old_checksum0 = inner_ip0->checksum;
290  sum0 = inner_ip0->checksum;
291  sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
292  src_address);
293  inner_ip0->checksum = ip_csum_fold (sum0);
294  new_checksum0 = inner_ip0->checksum;
295  sum0 = icmp0->checksum;
296  sum0 = ip_csum_update (sum0, old_checksum0, new_checksum0, ip4_header_t,
297  checksum);
298  icmp0->checksum = ip_csum_fold (sum0);
299 
300  /* update inner source port */
301  old_port0 = l4_header->src_port;
302  l4_header->src_port = s0->in2out.port;
303  new_port0 = l4_header->src_port;
304  sum0 = icmp0->checksum;
305  sum0 = ip_csum_update (sum0, old_port0, new_port0, tcp_udp_header_t,
306  src_port);
307  icmp0->checksum = ip_csum_fold (sum0);
308  }
309  else
310  {
311  init_nat_k (&kv0, ip0->dst_address, 0, sm->outside_fib_index, 0);
312  if (clib_bihash_search_8_8
313  (&sm->static_mapping_by_external, &kv0, &value0))
314  {
315  if (!is_ed)
316  {
317  icmp_echo_header_t *echo0 = (icmp_echo_header_t *) (icmp0 + 1);
318  u16 icmp_id0 = echo0->identifier;
319  init_nat_k (&kv0, ip0->dst_address, icmp_id0,
320  sm->outside_fib_index, NAT_PROTOCOL_ICMP);
321  if (sm->num_workers > 1)
322  ti =
323  (clib_net_to_host_u16 (icmp_id0) -
324  1024) / sm->port_per_thread;
325  else
326  ti = sm->num_workers;
327  int rv =
328  clib_bihash_search_8_8 (&sm->per_thread_data[ti].out2in, &kv0,
329  &value0);
330  if (!rv)
331  {
332  si = value0.value;
333  s0 =
335  new_dst_addr0 = s0->in2out.addr.as_u32;
336  vnet_buffer (b0)->sw_if_index[VLIB_TX] =
337  s0->in2out.fib_index;
338  echo0->identifier = s0->in2out.port;
339  sum0 = icmp0->checksum;
340  sum0 = ip_csum_update (sum0, icmp_id0, s0->in2out.port,
341  icmp_echo_header_t, identifier);
342  icmp0->checksum = ip_csum_fold (sum0);
343  goto change_addr;
344  }
345  }
346 
347  return 1;
348  }
349 
350  m0 = pool_elt_at_index (sm->static_mappings, value0.value);
351 
352  new_dst_addr0 = m0->local_addr.as_u32;
353  if (vnet_buffer (b0)->sw_if_index[VLIB_TX] == ~0)
354  vnet_buffer (b0)->sw_if_index[VLIB_TX] = m0->fib_index;
355  }
356 change_addr:
357  /* Destination is behind the same NAT, use internal address and port */
358  if (new_dst_addr0)
359  {
360  old_dst_addr0 = ip0->dst_address.as_u32;
361  ip0->dst_address.as_u32 = new_dst_addr0;
362  sum0 = ip0->checksum;
363  sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
364  ip4_header_t, dst_address);
365  ip0->checksum = ip_csum_fold (sum0);
366  }
367  return 0;
368 }
369 #endif
370 
371 #ifndef CLIB_MARCH_VARIANT
372 void
375 {
378  u32 old_addr, new_addr;
379  ip_csum_t sum;
380 
381  init_nat_k (&kv, ip->dst_address, 0, 0, 0);
382  if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
383  return;
384 
385  m = pool_elt_at_index (sm->static_mappings, value.value);
386 
387  old_addr = ip->dst_address.as_u32;
388  new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
389  sum = ip->checksum;
390  sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
391  ip->checksum = ip_csum_fold (sum);
392 
393  if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
394  vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
395 }
396 #endif
397 
398 #ifndef CLIB_MARCH_VARIANT
399 void
402 {
403  u32 old_addr, new_addr = 0, ti = 0;
405  clib_bihash_kv_16_8_t s_kv, s_value;
407  ip_csum_t sum;
408  snat_session_t *s;
409 
410  if (sm->num_workers > 1)
411  ti = sm->worker_out2in_cb (b, ip, sm->outside_fib_index, 0);
412  else
413  ti = sm->num_workers;
414 
415  old_addr = ip->dst_address.as_u32;
416  init_ed_k (&s_kv, ip->dst_address, 0, ip->src_address, 0,
417  sm->outside_fib_index, ip->protocol);
418  if (clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
419  {
420  init_nat_k (&kv, ip->dst_address, 0, 0, 0);
421  if (clib_bihash_search_8_8
422  (&sm->static_mapping_by_external, &kv, &value))
423  return;
424 
425  m = pool_elt_at_index (sm->static_mappings, value.value);
426  if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
427  vnet_buffer (b)->sw_if_index[VLIB_TX] = m->fib_index;
428  new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
429  }
430  else
431  {
432  ASSERT (ti == ed_value_get_thread_index (&s_value));
433  s =
435  ed_value_get_session_index (&s_value));
436  if (vnet_buffer (b)->sw_if_index[VLIB_TX] == ~0)
437  vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
438  new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
439  }
440  sum = ip->checksum;
441  sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
442  ip->checksum = ip_csum_fold (sum);
443 }
444 #endif
445 
446 static inline uword
449  vlib_frame_t * frame, int is_ed)
450 {
451  u32 n_left_from, *from, *to_next;
452  nat_hairpin_next_t next_index;
453  snat_main_t *sm = &snat_main;
455  u8 arc_index = vnet_feat_arc_ip4_local.feature_arc_index;
457 
458  from = vlib_frame_vector_args (frame);
459  n_left_from = frame->n_vectors;
460  next_index = node->cached_next_index;
461 
462  while (n_left_from > 0)
463  {
464  u32 n_left_to_next;
465 
466  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
467 
468  while (n_left_from > 0 && n_left_to_next > 0)
469  {
470  u32 bi0;
471  vlib_buffer_t *b0;
472  u32 next0;
473  ip4_header_t *ip0;
474  u32 proto0;
475  udp_header_t *udp0;
476  tcp_header_t *tcp0;
477  u32 sw_if_index0;
478 
479  /* speculatively enqueue b0 to the current next frame */
480  bi0 = from[0];
481  to_next[0] = bi0;
482  from += 1;
483  to_next += 1;
484  n_left_from -= 1;
485  n_left_to_next -= 1;
486 
487  b0 = vlib_get_buffer (vm, bi0);
488  ip0 = vlib_buffer_get_current (b0);
489  udp0 = ip4_next_header (ip0);
490  tcp0 = (tcp_header_t *) udp0;
491  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
492 
493  proto0 = ip_proto_to_nat_proto (ip0->protocol);
494 
496  &next0, 0);
497 
498  if (snat_hairpinning
499  (vm, node, sm, b0, ip0, udp0, tcp0, proto0, is_ed,
500  1 /* do_trace */ ))
501  next0 = NAT_HAIRPIN_NEXT_LOOKUP;
502 
503  if (next0 != NAT_HAIRPIN_NEXT_DROP)
504  {
506  vm->thread_index, sw_if_index0,
507  1);
508  }
509 
510  /* verify speculative enqueue, maybe switch current next frame */
511  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
512  to_next, n_left_to_next,
513  bi0, next0);
514  }
515 
516  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
517  }
518 
519  return frame->n_vectors;
520 }
521 
525 {
526  return nat44_hairpinning_fn_inline (vm, node, frame, 0);
527 }
528 
529 /* *INDENT-OFF* */
531  .name = "nat44-hairpinning",
532  .vector_size = sizeof (u32),
534  .format_trace = format_nat_hairpin_trace,
535  .n_next_nodes = NAT_HAIRPIN_N_NEXT,
536  .next_nodes = {
537  [NAT_HAIRPIN_NEXT_DROP] = "error-drop",
538  [NAT_HAIRPIN_NEXT_LOOKUP] = "ip4-lookup",
539  },
540 };
541 /* *INDENT-ON* */
542 
546 {
547  return nat44_hairpinning_fn_inline (vm, node, frame, 1);
548 }
549 
550 /* *INDENT-OFF* */
552  .name = "nat44-ed-hairpinning",
553  .vector_size = sizeof (u32),
555  .format_trace = format_nat_hairpin_trace,
556  .n_next_nodes = NAT_HAIRPIN_N_NEXT,
557  .next_nodes = {
558  [NAT_HAIRPIN_NEXT_DROP] = "error-drop",
559  [NAT_HAIRPIN_NEXT_LOOKUP] = "ip4-lookup",
560  },
561 };
562 /* *INDENT-ON* */
563 
564 static inline uword
567  vlib_frame_t * frame, int is_ed)
568 {
569  u32 n_left_from, *from, *to_next;
570  nat_hairpin_next_t next_index;
571  snat_main_t *sm = &snat_main;
572 
573  from = vlib_frame_vector_args (frame);
574  n_left_from = frame->n_vectors;
575  next_index = node->cached_next_index;
576 
577  while (n_left_from > 0)
578  {
579  u32 n_left_to_next;
580 
581  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
582 
583  while (n_left_from > 0 && n_left_to_next > 0)
584  {
585  u32 bi0;
586  vlib_buffer_t *b0;
587  u32 next0;
588  ip4_header_t *ip0;
589  u32 proto0;
590  u32 sw_if_index0;
591 
592  /* speculatively enqueue b0 to the current next frame */
593  bi0 = from[0];
594  to_next[0] = bi0;
595  from += 1;
596  to_next += 1;
597  n_left_from -= 1;
598  n_left_to_next -= 1;
599 
600  b0 = vlib_get_buffer (vm, bi0);
601  next0 = NAT_HAIRPIN_NEXT_LOOKUP;
602  ip0 = vlib_buffer_get_current (b0);
603  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
604 
605  proto0 = ip_proto_to_nat_proto (ip0->protocol);
606 
607  vnet_buffer (b0)->snat.flags = 0;
608  if (PREDICT_FALSE (is_hairpinning (sm, &ip0->dst_address)))
609  {
610  if (proto0 == NAT_PROTOCOL_TCP || proto0 == NAT_PROTOCOL_UDP)
611  {
612  udp_header_t *udp0 = ip4_next_header (ip0);
613  tcp_header_t *tcp0 = (tcp_header_t *) udp0;
614 
615  snat_hairpinning (vm, node, sm, b0, ip0, udp0, tcp0, proto0,
616  is_ed, 1 /* do_trace */ );
617  }
618  else if (proto0 == NAT_PROTOCOL_ICMP)
619  {
620  icmp46_header_t *icmp0 = ip4_next_header (ip0);
621 
622  snat_icmp_hairpinning (sm, b0, ip0, icmp0, is_ed);
623  }
624  else
625  {
626  if (is_ed)
628  else
629  nat_hairpinning_sm_unknown_proto (sm, b0, ip0);
630  }
631 
632  vnet_buffer (b0)->snat.flags = SNAT_FLAG_HAIRPINNING;
633  }
634 
635 
636  if (next0 != NAT_HAIRPIN_NEXT_DROP)
637  {
639  vm->thread_index, sw_if_index0,
640  1);
641  }
642 
643  /* verify speculative enqueue, maybe switch current next frame */
644  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
645  to_next, n_left_to_next,
646  bi0, next0);
647  }
648 
649  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
650  }
651 
652  return frame->n_vectors;
653 }
654 
658 {
659  return snat_hairpin_dst_fn_inline (vm, node, frame, 0);
660 }
661 
662 /* *INDENT-OFF* */
664  .name = "nat44-hairpin-dst",
665  .vector_size = sizeof (u32),
667  .format_trace = format_nat_hairpin_trace,
668  .n_next_nodes = NAT_HAIRPIN_N_NEXT,
669  .next_nodes = {
670  [NAT_HAIRPIN_NEXT_DROP] = "error-drop",
671  [NAT_HAIRPIN_NEXT_LOOKUP] = "ip4-lookup",
672  },
673 };
674 /* *INDENT-ON* */
675 
679 {
680  return snat_hairpin_dst_fn_inline (vm, node, frame, 1);
681 }
682 
683 /* *INDENT-OFF* */
685  .name = "nat44-ed-hairpin-dst",
686  .vector_size = sizeof (u32),
688  .format_trace = format_nat_hairpin_trace,
689  .n_next_nodes = NAT_HAIRPIN_N_NEXT,
690  .next_nodes = {
691  [NAT_HAIRPIN_NEXT_DROP] = "error-drop",
692  [NAT_HAIRPIN_NEXT_LOOKUP] = "ip4-lookup",
693  },
694 };
695 /* *INDENT-ON* */
696 
697 static inline uword
700  vlib_frame_t * frame, int is_ed)
701 {
702  u32 n_left_from, *from, *to_next;
703  snat_hairpin_src_next_t next_index;
704  snat_main_t *sm = &snat_main;
705 
706  from = vlib_frame_vector_args (frame);
707  n_left_from = frame->n_vectors;
708  next_index = node->cached_next_index;
709 
710  while (n_left_from > 0)
711  {
712  u32 n_left_to_next;
713 
714  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
715 
716  while (n_left_from > 0 && n_left_to_next > 0)
717  {
718  u32 bi0;
719  vlib_buffer_t *b0;
720  u32 next0;
722  u32 sw_if_index0;
723 
724  /* speculatively enqueue b0 to the current next frame */
725  bi0 = from[0];
726  to_next[0] = bi0;
727  from += 1;
728  to_next += 1;
729  n_left_from -= 1;
730  n_left_to_next -= 1;
731 
732  b0 = vlib_get_buffer (vm, bi0);
733  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
734  vnet_feature_next (&next0, b0);
735 
736  /* *INDENT-OFF* */
738  ({
739  /* Only packets from NAT inside interface */
740  if ((nat_interface_is_inside(i)) && (sw_if_index0 == i->sw_if_index))
741  {
742  if (PREDICT_FALSE ((vnet_buffer (b0)->snat.flags) &
743  SNAT_FLAG_HAIRPINNING))
744  {
745  if (PREDICT_TRUE (sm->num_workers > 1))
746  next0 = SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH;
747  else
748  next0 = SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT;
749  }
750  break;
751  }
752  }));
753  /* *INDENT-ON* */
754 
755  if (next0 != SNAT_HAIRPIN_SRC_NEXT_DROP)
756  {
758  vm->thread_index, sw_if_index0,
759  1);
760  }
761 
762  /* verify speculative enqueue, maybe switch current next frame */
763  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
764  to_next, n_left_to_next,
765  bi0, next0);
766  }
767 
768  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
769  }
770 
771  return frame->n_vectors;
772 }
773 
777 {
778  return snat_hairpin_src_fn_inline (vm, node, frame, 0);
779 }
780 
781 /* *INDENT-OFF* */
783  .name = "nat44-hairpin-src",
784  .vector_size = sizeof (u32),
786  .n_next_nodes = SNAT_HAIRPIN_SRC_N_NEXT,
787  .next_nodes = {
788  [SNAT_HAIRPIN_SRC_NEXT_DROP] = "error-drop",
789  [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT] = "nat44-in2out-output",
790  [SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT] = "interface-output",
791  [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH] = "nat44-in2out-output-worker-handoff",
792  },
793 };
794 /* *INDENT-ON* */
795 
799 {
800  return snat_hairpin_src_fn_inline (vm, node, frame, 1);
801 }
802 
803 /* *INDENT-OFF* */
805  .name = "nat44-ed-hairpin-src",
806  .vector_size = sizeof (u32),
808  .n_next_nodes = SNAT_HAIRPIN_SRC_N_NEXT,
809  .next_nodes = {
810  [SNAT_HAIRPIN_SRC_NEXT_DROP] = "error-drop",
811  [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT] = "nat44-ed-in2out-output",
812  [SNAT_HAIRPIN_SRC_NEXT_INTERFACE_OUTPUT] = "interface-output",
813  [SNAT_HAIRPIN_SRC_NEXT_SNAT_IN2OUT_WH] = "nat44-in2out-output-worker-handoff",
814  },
815 };
816 /* *INDENT-ON* */
817 
818 /*
819  * fd.io coding-style-patch-verification: ON
820  *
821  * Local Variables:
822  * eval: (c-set-style "gnu")
823  * End:
824  */
vnet_config_main_t config_main
Definition: feature.h:82
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
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: vlib_api_cli.c:899
clib_bihash_16_8_t out2in_ed
Definition: nat.h:516
#define CLIB_UNUSED(x)
Definition: clib.h:87
vlib_node_registration_t snat_hairpin_src_node
(constructor) VLIB_REGISTER_NODE (snat_hairpin_src_node)
ip4_address_t src_address
Definition: ip4_packet.h:125
vlib_node_registration_t nat44_ed_hairpinning_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_hairpinning_node)
#define PREDICT_TRUE(x)
Definition: clib.h:121
vlib_node_registration_t nat44_ed_hairpin_dst_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_hairpin_dst_node)
vlib_node_registration_t nat44_ed_hairpin_src_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_hairpin_src_node)
u16 port_per_thread
Definition: nat.h:501
u32 thread_index
Definition: main.h:249
uword ip_csum_t
Definition: ip_packet.h:244
vlib_main_t * vm
Definition: in2out_ed.c:1582
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
static void vlib_increment_simple_counter(vlib_simple_counter_main_t *cm, u32 thread_index, u32 index, u64 increment)
Increment a simple counter.
Definition: counter.h:78
#define VLIB_NODE_FN(node)
Definition: node.h:202
static u8 * format_nat_hairpin_trace(u8 *s, va_list *args)
struct _tcp_header tcp_header_t
vlib_simple_counter_main_t hairpinning
Definition: nat.h:681
unsigned char u8
Definition: types.h:56
#define fm
vl_api_ip_proto_t protocol
Definition: lb_types.api:71
format_function_t format_ip4_address
Definition: format.h:73
snat_get_worker_out2in_function_t * worker_out2in_cb
Definition: nat.h:500
#define static_always_inline
Definition: clib.h:108
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:513
static nat_protocol_t ip_proto_to_nat_proto(u8 ip_proto)
Common NAT inline functions.
Definition: inlines.h:22
int snat_static_mapping_match(snat_main_t *sm, ip4_address_t match_addr, u16 match_port, u32 match_fib_index, nat_protocol_t match_protocol, ip4_address_t *mapping_addr, u16 *mapping_port, u32 *mapping_fib_index, 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, snat_static_mapping_t **out)
Match NAT44 static mapping.
Definition: nat.c:2815
ip4_address_t dst_address
Definition: ip4_packet.h:125
static_always_inline u8 icmp_type_is_error_message(u8 icmp_type)
Definition: inlines.h:53
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:196
unsigned int u32
Definition: types.h:88
ip4_address_t local_addr
Definition: nat.h:359
void nat_hairpinning_sm_unknown_proto(snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip)
vl_api_fib_path_type_t type
Definition: fib_types.api:123
static uword snat_hairpin_dst_fn_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_ed)
vnet_crypto_main_t * cm
Definition: quic_crypto.c:53
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
nat_hairpin_next_t
unsigned short u16
Definition: types.h:57
static u32 ed_value_get_session_index(clib_bihash_kv_16_8_t *value)
Definition: nat_inlines.h:497
snat_static_mapping_t * static_mappings
Definition: nat.h:513
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
struct snat_main_s::@91 counters
static void * vnet_get_config_data(vnet_config_main_t *cm, u32 *config_index, u32 *next_index, u32 n_data_bytes)
Definition: config.h:123
#define PREDICT_FALSE(x)
Definition: clib.h:120
clib_bihash_8_8_t static_mapping_by_external
Definition: nat.h:510
vl_api_address_union_t src_address
Definition: ip_types.api:111
#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:224
#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:391
static void init_nat_k(clib_bihash_kv_8_8_t *kv, ip4_address_t addr, u16 port, u32 fib_index, nat_protocol_t proto)
Definition: nat_inlines.h:58
snat_interface_t * output_feature_interfaces
Definition: nat.h:520
snat_main_t snat_main
Definition: nat.c:39
u16 src_port
Definition: det44.h:73
vl_api_ip_port_and_mask_t src_port
Definition: flow_types.api:91
u64 value
the value
Definition: bihash_8_8.h:42
vl_api_mac_address_t dst_addr
Definition: flow_types.api:65
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
u16 n_vectors
Definition: node.h:396
clib_bihash_8_8_t out2in
Definition: nat.h:415
vlib_node_registration_t snat_hairpin_dst_node
(constructor) VLIB_REGISTER_NODE (snat_hairpin_dst_node)
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
static_always_inline void vnet_feature_next(u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:322
u32 outside_fib_index
Definition: nat.h:613
int snat_hairpinning(vlib_main_t *vm, vlib_node_runtime_t *node, snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0, udp_header_t *udp0, tcp_header_t *tcp0, u32 proto0, int is_ed, int do_trace)
u32 current_config_index
Used by feature subgraph arcs to visit enabled feature nodes.
Definition: buffer.h:147
8 octet key, 8 octet key value pair
Definition: bihash_8_8.h:39
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:483
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1582
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:510
u8 value
Definition: qos.api:54
#define ASSERT(truth)
u32 num_workers
Definition: nat.h:496
snat_hairpin_src_next_t
ip4_address_t addr
Definition: nat.h:296
u16 dst_port
Definition: det44.h:73
Definition: defs.h:47
vl_api_address_t ip
Definition: l2.api:501
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1583
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:504
#define SNAT_FLAG_HAIRPINNING
Definition: nat.h:49
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:297
#define ip_csum_update(sum, old, new, type, field)
Definition: ip_packet.h:294
static uword nat44_hairpinning_fn_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_ed)
snat_address_t * addresses
Definition: nat.h:523
struct _vnet_feature_arc_registration vnet_feature_arc_registration_t
feature registration object
static void init_ed_k(clib_bihash_kv_16_8_t *kv, ip4_address_t l_addr, u16 l_port, ip4_address_t r_addr, u16 r_port, u32 fib_index, u8 proto)
Definition: nat_inlines.h:473
#define vnet_buffer(b)
Definition: buffer.h:417
static uword snat_hairpin_src_fn_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_ed)
#define vec_foreach(var, vec)
Vector iterator.
u16 flags
Copy of main node flags.
Definition: node.h:500
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:577
vlib_node_registration_t nat44_hairpinning_node
(constructor) VLIB_REGISTER_NODE (nat44_hairpinning_node)
u8 si
Definition: lisp_types.api:47
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:301
vnet_feature_config_main_t * feature_config_mains
feature config main objects
Definition: feature.h:100
vnet_feature_main_t feature_main
Definition: feature.c:18
vnet_feature_arc_registration_t vnet_feat_arc_ip4_local
snat_session_t * sessions
Definition: nat.h:428
static_always_inline int is_hairpinning(snat_main_t *sm, ip4_address_t *dst_addr)
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
static u32 ed_value_get_thread_index(clib_bihash_kv_16_8_t *value)
Definition: nat_inlines.h:491
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:300
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:33
Definition: defs.h:46
void nat44_ed_hairpinning_unknown_proto(snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip)
u32 snat_icmp_hairpinning(snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0, icmp46_header_t *icmp0, int is_ed)