FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
nat_det_out2in.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 Deterministic/CGN NAT44 outside to inside network translation
18  */
19 
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/fib/ip4_fib.h>
24 #include <vppinfra/error.h>
25 #include <vppinfra/elog.h>
26 #include <nat/nat.h>
27 #include <nat/nat_det.h>
28 #include <nat/nat_inlines.h>
29 
30 typedef enum
31 {
37 
38 typedef struct
39 {
44 
45 #define foreach_nat_det_out2in_error \
46 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
47 _(NO_TRANSLATION, "No translation") \
48 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
49 _(OUT2IN_PACKETS, "Good out2in packets processed")
50 
51 typedef enum
52 {
53 #define _(sym,str) NAT_DET_OUT2IN_ERROR_##sym,
55 #undef _
58 
59 static char *nat_det_out2in_error_strings[] = {
60 #define _(sym,string) string,
62 #undef _
63 };
64 
65 static u8 *
66 format_nat_det_out2in_trace (u8 * s, va_list * args)
67 {
68  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
69  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
70  nat_det_out2in_trace_t *t = va_arg (*args, nat_det_out2in_trace_t *);
71 
72  s =
73  format (s,
74  "NAT_DET_OUT2IN: sw_if_index %d, next index %d, session index %d",
76  return s;
77 }
78 
79 #ifndef CLIB_MARCH_VARIANT
80 /**
81  * Get address and port values to be used for ICMP packet translation
82  * and create session if needed
83  *
84  * @param[in,out] sm NAT main
85  * @param[in,out] node NAT node runtime
86  * @param[in] thread_index thread index
87  * @param[in,out] b0 buffer containing packet to be translated
88  * @param[out] p_proto protocol used for matching
89  * @param[out] p_value address and port after NAT translation
90  * @param[out] p_dont_translate if packet should not be translated
91  * @param d optional parameter
92  * @param e optional parameter
93  */
94 u32
96  u32 thread_index, vlib_buffer_t * b0,
97  ip4_header_t * ip0, u8 * p_proto,
98  snat_session_key_t * p_value,
99  u8 * p_dont_translate, void *d, void *e)
100 {
101  icmp46_header_t *icmp0;
102  u32 sw_if_index0;
103  u8 protocol;
104  snat_det_out_key_t key0;
105  u8 dont_translate = 0;
106  u32 next0 = ~0;
107  icmp_echo_header_t *echo0, *inner_echo0 = 0;
108  ip4_header_t *inner_ip0;
109  void *l4_header = 0;
110  icmp46_header_t *inner_icmp0;
111  snat_det_map_t *dm0 = 0;
112  ip4_address_t new_addr0 = { {0} };
113  snat_det_session_t *ses0 = 0;
114  ip4_address_t out_addr;
115 
116  icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
117  echo0 = (icmp_echo_header_t *) (icmp0 + 1);
118  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
119 
120  if (!icmp_is_error_message (icmp0))
121  {
122  protocol = SNAT_PROTOCOL_ICMP;
123  key0.ext_host_addr = ip0->src_address;
124  key0.ext_host_port = 0;
125  key0.out_port = echo0->identifier;
126  out_addr = ip0->dst_address;
127  }
128  else
129  {
130  inner_ip0 = (ip4_header_t *) (echo0 + 1);
131  l4_header = ip4_next_header (inner_ip0);
132  protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
133  key0.ext_host_addr = inner_ip0->dst_address;
134  out_addr = inner_ip0->src_address;
135  switch (protocol)
136  {
137  case SNAT_PROTOCOL_ICMP:
138  inner_icmp0 = (icmp46_header_t *) l4_header;
139  inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
140  key0.ext_host_port = 0;
141  key0.out_port = inner_echo0->identifier;
142  break;
143  case SNAT_PROTOCOL_UDP:
144  case SNAT_PROTOCOL_TCP:
145  key0.ext_host_port = ((tcp_udp_header_t *) l4_header)->dst_port;
146  key0.out_port = ((tcp_udp_header_t *) l4_header)->src_port;
147  break;
148  default:
149  b0->error = node->errors[NAT_DET_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
150  next0 = NAT_DET_OUT2IN_NEXT_DROP;
151  goto out;
152  }
153  }
154 
155  dm0 = snat_det_map_by_out (sm, &out_addr);
156  if (PREDICT_FALSE (!dm0))
157  {
158  /* Don't NAT packet aimed at the intfc address */
159  if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
160  ip0->dst_address.as_u32)))
161  {
162  dont_translate = 1;
163  goto out;
164  }
165  nat_log_info ("unknown dst address: %U",
167  goto out;
168  }
169 
170  snat_det_reverse (dm0, &ip0->dst_address,
171  clib_net_to_host_u16 (key0.out_port), &new_addr0);
172 
173  ses0 = snat_det_get_ses_by_out (dm0, &new_addr0, key0.as_u64);
174  if (PREDICT_FALSE (!ses0))
175  {
176  /* Don't NAT packet aimed at the intfc address */
177  if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
178  ip0->dst_address.as_u32)))
179  {
180  dont_translate = 1;
181  goto out;
182  }
183  nat_log_info ("no match src %U:%d dst %U:%d for user %U",
185  clib_net_to_host_u16 (key0.ext_host_port),
186  format_ip4_address, &out_addr,
187  clib_net_to_host_u16 (key0.out_port),
188  format_ip4_address, &new_addr0);
189  b0->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION];
190  next0 = NAT_DET_OUT2IN_NEXT_DROP;
191  goto out;
192  }
193 
194  if (PREDICT_FALSE (icmp0->type != ICMP4_echo_reply &&
195  !icmp_is_error_message (icmp0)))
196  {
197  b0->error = node->errors[NAT_DET_OUT2IN_ERROR_BAD_ICMP_TYPE];
198  next0 = NAT_DET_OUT2IN_NEXT_DROP;
199  goto out;
200  }
201 
202  goto out;
203 
204 out:
205  *p_proto = protocol;
206  if (ses0)
207  {
208  p_value->addr = new_addr0;
209  p_value->fib_index = sm->inside_fib_index;
210  p_value->port = ses0->in_port;
211  }
212  *p_dont_translate = dont_translate;
213  if (d)
214  *(snat_det_session_t **) d = ses0;
215  if (e)
216  *(snat_det_map_t **) e = dm0;
217  return next0;
218 }
219 #endif
220 
222  vlib_node_runtime_t * node,
223  vlib_frame_t * frame)
224 {
225  u32 n_left_from, *from, *to_next;
226  nat_det_out2in_next_t next_index;
227  u32 pkts_processed = 0;
228  snat_main_t *sm = &snat_main;
229  u32 thread_index = vm->thread_index;
230 
231  from = vlib_frame_vector_args (frame);
232  n_left_from = frame->n_vectors;
233  next_index = node->cached_next_index;
234 
235  while (n_left_from > 0)
236  {
237  u32 n_left_to_next;
238 
239  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
240 
241  while (n_left_from >= 4 && n_left_to_next >= 2)
242  {
243  u32 bi0, bi1;
244  vlib_buffer_t *b0, *b1;
247  u32 sw_if_index0, sw_if_index1;
248  ip4_header_t *ip0, *ip1;
249  ip_csum_t sum0, sum1;
250  ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
251  u16 new_port0, old_port0, old_port1, new_port1;
252  udp_header_t *udp0, *udp1;
253  tcp_header_t *tcp0, *tcp1;
254  u32 proto0, proto1;
255  snat_det_out_key_t key0, key1;
256  snat_det_map_t *dm0, *dm1;
257  snat_det_session_t *ses0 = 0, *ses1 = 0;
258  u32 rx_fib_index0, rx_fib_index1;
259  icmp46_header_t *icmp0, *icmp1;
260 
261  /* Prefetch next iteration. */
262  {
263  vlib_buffer_t *p2, *p3;
264 
265  p2 = vlib_get_buffer (vm, from[2]);
266  p3 = vlib_get_buffer (vm, from[3]);
267 
268  vlib_prefetch_buffer_header (p2, LOAD);
269  vlib_prefetch_buffer_header (p3, LOAD);
270 
273  }
274 
275  /* speculatively enqueue b0 and b1 to the current next frame */
276  to_next[0] = bi0 = from[0];
277  to_next[1] = bi1 = from[1];
278  from += 2;
279  to_next += 2;
280  n_left_from -= 2;
281  n_left_to_next -= 2;
282 
283  b0 = vlib_get_buffer (vm, bi0);
284  b1 = vlib_get_buffer (vm, bi1);
285 
286  ip0 = vlib_buffer_get_current (b0);
287  udp0 = ip4_next_header (ip0);
288  tcp0 = (tcp_header_t *) udp0;
289 
290  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
291 
292  if (PREDICT_FALSE (ip0->ttl == 1))
293  {
294  vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
295  icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
296  ICMP4_time_exceeded_ttl_exceeded_in_transit,
297  0);
299  goto trace0;
300  }
301 
302  proto0 = ip_proto_to_snat_proto (ip0->protocol);
303 
304  if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
305  {
306  rx_fib_index0 =
308  icmp0 = (icmp46_header_t *) udp0;
309 
310  next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
311  rx_fib_index0, node, next0, thread_index,
312  &ses0, &dm0);
313  goto trace0;
314  }
315 
316  key0.ext_host_addr = ip0->src_address;
317  key0.ext_host_port = tcp0->src;
318  key0.out_port = tcp0->dst;
319 
320  dm0 = snat_det_map_by_out (sm, &ip0->dst_address);
321  if (PREDICT_FALSE (!dm0))
322  {
323  nat_log_info ("unknown dst address: %U",
325  next0 = NAT_DET_OUT2IN_NEXT_DROP;
326  b0->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION];
327  goto trace0;
328  }
329 
330  snat_det_reverse (dm0, &ip0->dst_address,
331  clib_net_to_host_u16 (tcp0->dst), &new_addr0);
332 
333  ses0 = snat_det_get_ses_by_out (dm0, &new_addr0, key0.as_u64);
334  if (PREDICT_FALSE (!ses0))
335  {
336  nat_log_info ("no match src %U:%d dst %U:%d for user %U",
338  clib_net_to_host_u16 (tcp0->src),
340  clib_net_to_host_u16 (tcp0->dst),
341  format_ip4_address, &new_addr0);
342  next0 = NAT_DET_OUT2IN_NEXT_DROP;
343  b0->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION];
344  goto trace0;
345  }
346  old_port0 = udp0->dst_port;
347  udp0->dst_port = new_port0 = ses0->in_port;
348 
349  old_addr0 = ip0->dst_address;
350  ip0->dst_address = new_addr0;
351  vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->inside_fib_index;
352 
353  sum0 = ip0->checksum;
354  sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
355  ip4_header_t,
356  dst_address /* changed member */ );
357  ip0->checksum = ip_csum_fold (sum0);
358 
359  if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
360  {
361  if (tcp0->flags & TCP_FLAG_FIN
362  && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
363  ses0->state = SNAT_SESSION_TCP_CLOSE_WAIT;
364  else if (tcp0->flags & TCP_FLAG_ACK
365  && ses0->state == SNAT_SESSION_TCP_LAST_ACK)
366  snat_det_ses_close (dm0, ses0);
367 
368  sum0 = tcp0->checksum;
369  sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
370  ip4_header_t,
371  dst_address /* changed member */ );
372  sum0 = ip_csum_update (sum0, old_port0, new_port0,
373  ip4_header_t /* cheat */ ,
374  length /* changed member */ );
375  tcp0->checksum = ip_csum_fold (sum0);
376  }
377  else if (udp0->checksum)
378  {
379  sum0 = udp0->checksum;
380  sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
381  ip4_header_t,
382  dst_address /* changed member */ );
383  sum0 = ip_csum_update (sum0, old_port0, new_port0,
384  ip4_header_t /* cheat */ ,
385  length /* changed member */ );
386  udp0->checksum = ip_csum_fold (sum0);
387  }
388 
389  trace0:
390 
391  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
392  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
393  {
395  vlib_add_trace (vm, node, b0, sizeof (*t));
396  t->sw_if_index = sw_if_index0;
397  t->next_index = next0;
398  t->session_index = ~0;
399  if (ses0)
400  t->session_index = ses0 - dm0->sessions;
401  }
402 
403  pkts_processed += next0 != NAT_DET_OUT2IN_NEXT_DROP;
404 
405  b1 = vlib_get_buffer (vm, bi1);
406 
407  ip1 = vlib_buffer_get_current (b1);
408  udp1 = ip4_next_header (ip1);
409  tcp1 = (tcp_header_t *) udp1;
410 
411  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
412 
413  if (PREDICT_FALSE (ip1->ttl == 1))
414  {
415  vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
416  icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
417  ICMP4_time_exceeded_ttl_exceeded_in_transit,
418  0);
420  goto trace1;
421  }
422 
423  proto1 = ip_proto_to_snat_proto (ip1->protocol);
424 
425  if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
426  {
427  rx_fib_index1 =
429  icmp1 = (icmp46_header_t *) udp1;
430 
431  next1 = icmp_out2in (sm, b1, ip1, icmp1, sw_if_index1,
432  rx_fib_index1, node, next1, thread_index,
433  &ses1, &dm1);
434  goto trace1;
435  }
436 
437  key1.ext_host_addr = ip1->src_address;
438  key1.ext_host_port = tcp1->src;
439  key1.out_port = tcp1->dst;
440 
441  dm1 = snat_det_map_by_out (sm, &ip1->dst_address);
442  if (PREDICT_FALSE (!dm1))
443  {
444  nat_log_info ("unknown dst address: %U",
446  next1 = NAT_DET_OUT2IN_NEXT_DROP;
447  b1->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION];
448  goto trace1;
449  }
450 
451  snat_det_reverse (dm1, &ip1->dst_address,
452  clib_net_to_host_u16 (tcp1->dst), &new_addr1);
453 
454  ses1 = snat_det_get_ses_by_out (dm1, &new_addr1, key1.as_u64);
455  if (PREDICT_FALSE (!ses1))
456  {
457  nat_log_info ("no match src %U:%d dst %U:%d for user %U",
459  clib_net_to_host_u16 (tcp1->src),
461  clib_net_to_host_u16 (tcp1->dst),
462  format_ip4_address, &new_addr1);
463  next1 = NAT_DET_OUT2IN_NEXT_DROP;
464  b1->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION];
465  goto trace1;
466  }
467  old_port1 = udp1->dst_port;
468  udp1->dst_port = new_port1 = ses1->in_port;
469 
470  old_addr1 = ip1->dst_address;
471  ip1->dst_address = new_addr1;
472  vnet_buffer (b1)->sw_if_index[VLIB_TX] = sm->inside_fib_index;
473 
474  sum1 = ip1->checksum;
475  sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
476  ip4_header_t,
477  dst_address /* changed member */ );
478  ip1->checksum = ip_csum_fold (sum1);
479 
480  if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP))
481  {
482  if (tcp1->flags & TCP_FLAG_FIN
483  && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
484  ses1->state = SNAT_SESSION_TCP_CLOSE_WAIT;
485  else if (tcp1->flags & TCP_FLAG_ACK
486  && ses1->state == SNAT_SESSION_TCP_LAST_ACK)
487  snat_det_ses_close (dm1, ses1);
488 
489  sum1 = tcp1->checksum;
490  sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
491  ip4_header_t,
492  dst_address /* changed member */ );
493  sum1 = ip_csum_update (sum1, old_port1, new_port1,
494  ip4_header_t /* cheat */ ,
495  length /* changed member */ );
496  tcp1->checksum = ip_csum_fold (sum1);
497  }
498  else if (udp1->checksum)
499  {
500  sum1 = udp1->checksum;
501  sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
502  ip4_header_t,
503  dst_address /* changed member */ );
504  sum1 = ip_csum_update (sum1, old_port1, new_port1,
505  ip4_header_t /* cheat */ ,
506  length /* changed member */ );
507  udp1->checksum = ip_csum_fold (sum1);
508  }
509 
510  trace1:
511 
512  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
513  && (b1->flags & VLIB_BUFFER_IS_TRACED)))
514  {
516  vlib_add_trace (vm, node, b1, sizeof (*t));
517  t->sw_if_index = sw_if_index1;
518  t->next_index = next1;
519  t->session_index = ~0;
520  if (ses1)
521  t->session_index = ses1 - dm1->sessions;
522  }
523 
524  pkts_processed += next1 != NAT_DET_OUT2IN_NEXT_DROP;
525 
526  /* verify speculative enqueues, maybe switch current next frame */
527  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
528  to_next, n_left_to_next,
529  bi0, bi1, next0, next1);
530  }
531 
532  while (n_left_from > 0 && n_left_to_next > 0)
533  {
534  u32 bi0;
535  vlib_buffer_t *b0;
537  u32 sw_if_index0;
538  ip4_header_t *ip0;
539  ip_csum_t sum0;
540  ip4_address_t new_addr0, old_addr0;
541  u16 new_port0, old_port0;
542  udp_header_t *udp0;
543  tcp_header_t *tcp0;
544  u32 proto0;
545  snat_det_out_key_t key0;
546  snat_det_map_t *dm0;
547  snat_det_session_t *ses0 = 0;
548  u32 rx_fib_index0;
549  icmp46_header_t *icmp0;
550 
551  /* speculatively enqueue b0 to the current next frame */
552  bi0 = from[0];
553  to_next[0] = bi0;
554  from += 1;
555  to_next += 1;
556  n_left_from -= 1;
557  n_left_to_next -= 1;
558 
559  b0 = vlib_get_buffer (vm, bi0);
560 
561  ip0 = vlib_buffer_get_current (b0);
562  udp0 = ip4_next_header (ip0);
563  tcp0 = (tcp_header_t *) udp0;
564 
565  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
566 
567  if (PREDICT_FALSE (ip0->ttl == 1))
568  {
569  vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
570  icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
571  ICMP4_time_exceeded_ttl_exceeded_in_transit,
572  0);
574  goto trace00;
575  }
576 
577  proto0 = ip_proto_to_snat_proto (ip0->protocol);
578 
579  if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
580  {
581  rx_fib_index0 =
583  icmp0 = (icmp46_header_t *) udp0;
584 
585  next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0,
586  rx_fib_index0, node, next0, thread_index,
587  &ses0, &dm0);
588  goto trace00;
589  }
590 
591  key0.ext_host_addr = ip0->src_address;
592  key0.ext_host_port = tcp0->src;
593  key0.out_port = tcp0->dst;
594 
595  dm0 = snat_det_map_by_out (sm, &ip0->dst_address);
596  if (PREDICT_FALSE (!dm0))
597  {
598  nat_log_info ("unknown dst address: %U",
600  next0 = NAT_DET_OUT2IN_NEXT_DROP;
601  b0->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION];
602  goto trace00;
603  }
604 
605  snat_det_reverse (dm0, &ip0->dst_address,
606  clib_net_to_host_u16 (tcp0->dst), &new_addr0);
607 
608  ses0 = snat_det_get_ses_by_out (dm0, &new_addr0, key0.as_u64);
609  if (PREDICT_FALSE (!ses0))
610  {
611  nat_log_info ("no match src %U:%d dst %U:%d for user %U",
613  clib_net_to_host_u16 (tcp0->src),
615  clib_net_to_host_u16 (tcp0->dst),
616  format_ip4_address, &new_addr0);
617  next0 = NAT_DET_OUT2IN_NEXT_DROP;
618  b0->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION];
619  goto trace00;
620  }
621  old_port0 = udp0->dst_port;
622  udp0->dst_port = new_port0 = ses0->in_port;
623 
624  old_addr0 = ip0->dst_address;
625  ip0->dst_address = new_addr0;
626  vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm->inside_fib_index;
627 
628  sum0 = ip0->checksum;
629  sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
630  ip4_header_t,
631  dst_address /* changed member */ );
632  ip0->checksum = ip_csum_fold (sum0);
633 
634  if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
635  {
636  if (tcp0->flags & TCP_FLAG_FIN
637  && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
638  ses0->state = SNAT_SESSION_TCP_CLOSE_WAIT;
639  else if (tcp0->flags & TCP_FLAG_ACK
640  && ses0->state == SNAT_SESSION_TCP_LAST_ACK)
641  snat_det_ses_close (dm0, ses0);
642 
643  sum0 = tcp0->checksum;
644  sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
645  ip4_header_t,
646  dst_address /* changed member */ );
647  sum0 = ip_csum_update (sum0, old_port0, new_port0,
648  ip4_header_t /* cheat */ ,
649  length /* changed member */ );
650  tcp0->checksum = ip_csum_fold (sum0);
651  }
652  else if (udp0->checksum)
653  {
654  sum0 = udp0->checksum;
655  sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
656  ip4_header_t,
657  dst_address /* changed member */ );
658  sum0 = ip_csum_update (sum0, old_port0, new_port0,
659  ip4_header_t /* cheat */ ,
660  length /* changed member */ );
661  udp0->checksum = ip_csum_fold (sum0);
662  }
663 
664  trace00:
665 
666  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
667  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
668  {
670  vlib_add_trace (vm, node, b0, sizeof (*t));
671  t->sw_if_index = sw_if_index0;
672  t->next_index = next0;
673  t->session_index = ~0;
674  if (ses0)
675  t->session_index = ses0 - dm0->sessions;
676  }
677 
678  pkts_processed += next0 != NAT_DET_OUT2IN_NEXT_DROP;
679 
680  /* verify speculative enqueue, maybe switch current next frame */
681  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
682  to_next, n_left_to_next,
683  bi0, next0);
684  }
685 
686  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
687  }
688 
690  NAT_DET_OUT2IN_ERROR_OUT2IN_PACKETS,
691  pkts_processed);
692  return frame->n_vectors;
693 }
694 
695 /* *INDENT-OFF* */
697  .name = "nat44-det-out2in",
698  .vector_size = sizeof (u32),
699  .format_trace = format_nat_det_out2in_trace,
702  .error_strings = nat_det_out2in_error_strings,
703  .runtime_data_bytes = sizeof (snat_runtime_t),
704  .n_next_nodes = NAT_DET_OUT2IN_N_NEXT,
705  /* edit / add dispositions here */
706  .next_nodes = {
707  [NAT_DET_OUT2IN_NEXT_DROP] = "error-drop",
708  [NAT_DET_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
709  [NAT_DET_OUT2IN_NEXT_ICMP_ERROR] = "ip4-icmp-error",
710  },
711 };
712 /* *INDENT-ON* */
713 
714 /*
715  * fd.io coding-style-patch-verification: ON
716  *
717  * Local Variables:
718  * eval: (c-set-style "gnu")
719  * End:
720  */
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
u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, ip4_header_t *ip0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for ICMP packet translation and create session if needed...
#define nat_log_info(...)
Definition: nat.h:856
vlib_node_registration_t snat_det_out2in_node
(constructor) VLIB_REGISTER_NODE (snat_det_out2in_node)
#define CLIB_UNUSED(x)
Definition: clib.h:83
u16 ext_host_port
Definition: nat.h:125
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
ip4_address_t src_address
Definition: ip4_packet.h:170
#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
u32 thread_index
Definition: main.h:218
static void snat_det_ses_close(snat_det_map_t *dm, snat_det_session_t *ses)
Definition: nat_det.h:182
uword ip_csum_t
Definition: ip_packet.h:219
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define VLIB_NODE_FN(node)
Definition: node.h:202
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:470
struct _tcp_header tcp_header_t
static void snat_det_reverse(snat_det_map_t *dm, ip4_address_t *out_addr, u16 out_port, ip4_address_t *in_addr)
Definition: nat_det.h:90
unsigned char u8
Definition: types.h:56
u32 ip4_fib_table_get_index_for_sw_if_index(u32 sw_if_index)
Definition: ip4_fib.c:224
format_function_t format_ip4_address
Definition: format.h:75
ip4_address_t ext_host_addr
Definition: nat.h:124
ip4_address_t dst_address
Definition: ip4_packet.h:170
#define TCP_FLAG_ACK
Definition: fa_node.h:16
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:203
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:241
unsigned int u32
Definition: types.h:88
static char * nat_det_out2in_error_strings[]
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
snat_det_session_t * sessions
Definition: nat.h:403
static snat_det_map_t * snat_det_map_by_out(snat_main_t *sm, ip4_address_t *out_addr)
Definition: nat_det.h:60
vl_api_ip_proto_t protocol
Definition: punt.api:39
unsigned short u16
Definition: types.h:57
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
#define PREDICT_FALSE(x)
Definition: clib.h:112
#define TCP_FLAG_FIN
Definition: fa_node.h:12
#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 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
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1150
snat_main_t snat_main
Definition: nat.c:39
The fine-grained event logger allows lightweight, thread-safe event logging at minimum cost...
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
vlib_main_t * vm
Definition: buffer.c:323
static u8 * format_nat_det_out2in_trace(u8 *s, va_list *args)
deterministic NAT definitions
nat_det_out2in_next_t
u8 data[]
Packet data.
Definition: buffer.h:181
#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
u32 det_out2in_node_index
Definition: nat.h:642
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
#define foreach_nat_det_out2in_error
Definition: defs.h:47
static u32 ip_proto_to_snat_proto(u8 ip_proto)
Definition: nat_inlines.h:147
VLIB buffer representation.
Definition: buffer.h:102
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
#define ip_csum_update(sum, old, new, type, field)
Definition: ip_packet.h:269
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
static snat_det_session_t * snat_det_get_ses_by_out(snat_det_map_t *dm, ip4_address_t *in_addr, u64 out_key)
Definition: nat_det.h:112
#define vnet_buffer(b)
Definition: buffer.h:365
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:302
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
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
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:275
Definition: defs.h:46
u16 fib_index
Definition: nat.h:94
nat_det_out2in_error_t