FD.io VPP  v20.05-21-gb1500e9ff
Vector Packet Processing
nat_inlines.h
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  * @brief The NAT inline functions
17  */
18 
19 #ifndef __included_nat_inlines_h__
20 #define __included_nat_inlines_h__
21 
22 #include <vnet/fib/ip4_fib.h>
23 #include <nat/nat.h>
24 #include <nat/nat_ha.h>
25 
26 static inline uword
29  vlib_frame_t * frame, u32 def_next)
30 {
31  u32 n_left_from, *from, *to_next;
32  u16 next_index;
33 
34  from = vlib_frame_vector_args (frame);
35  n_left_from = frame->n_vectors;
36  next_index = node->cached_next_index;
37 
38  while (n_left_from > 0)
39  {
40  u32 n_left_to_next;
41 
42  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
43 
44  while (n_left_from >= 4 && n_left_to_next >= 2)
45  {
46  u32 next0, next1;
47  u32 arc_next0, arc_next1;
48  u32 bi0, bi1;
49  vlib_buffer_t *b0, *b1;
50 
51  /* Prefetch next iteration. */
52  {
53  vlib_buffer_t *p2, *p3;
54 
55  p2 = vlib_get_buffer (vm, from[2]);
56  p3 = vlib_get_buffer (vm, from[3]);
57 
58  vlib_prefetch_buffer_header (p2, LOAD);
59  vlib_prefetch_buffer_header (p3, LOAD);
60 
63  }
64 
65  /* speculatively enqueue b0 and b1 to the current next frame */
66  to_next[0] = bi0 = from[0];
67  to_next[1] = bi1 = from[1];
68  from += 2;
69  to_next += 2;
70  n_left_from -= 2;
71  n_left_to_next -= 2;
72 
73  b0 = vlib_get_buffer (vm, bi0);
74  b1 = vlib_get_buffer (vm, bi1);
75 
76  next0 = def_next;
77  next1 = def_next;
78 
79  vnet_feature_next (&arc_next0, b0);
80  vnet_feature_next (&arc_next1, b1);
81 
82  vnet_buffer2 (b0)->nat.arc_next = arc_next0;
83  vnet_buffer2 (b1)->nat.arc_next = arc_next1;
84 
86  {
87  if (b0->flags & VLIB_BUFFER_IS_TRACED)
88  {
89  nat_pre_trace_t *t =
90  vlib_add_trace (vm, node, b0, sizeof (*t));
91  t->next_index = next0;
92  t->arc_next_index = arc_next0;
93  }
94  if (b1->flags & VLIB_BUFFER_IS_TRACED)
95  {
96  nat_pre_trace_t *t =
97  vlib_add_trace (vm, node, b0, sizeof (*t));
98  t->next_index = next1;
99  t->arc_next_index = arc_next1;
100  }
101  }
102 
103  /* verify speculative enqueues, maybe switch current next frame */
104  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
105  to_next, n_left_to_next,
106  bi0, bi1, next0, next1);
107  }
108 
109  while (n_left_from > 0 && n_left_to_next > 0)
110  {
111  u32 next0;
112  u32 arc_next0;
113  u32 bi0;
114  vlib_buffer_t *b0;
115 
116  /* speculatively enqueue b0 to the current next frame */
117  bi0 = from[0];
118  to_next[0] = bi0;
119  from += 1;
120  to_next += 1;
121  n_left_from -= 1;
122  n_left_to_next -= 1;
123 
124  b0 = vlib_get_buffer (vm, bi0);
125  next0 = def_next;
126  vnet_feature_next (&arc_next0, b0);
127  vnet_buffer2 (b0)->nat.arc_next = arc_next0;
128 
130  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
131  {
132  nat_pre_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
133  t->next_index = next0;
134  t->arc_next_index = arc_next0;
135  }
136 
137  /* verify speculative enqueue, maybe switch current next frame */
138  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
139  to_next, n_left_to_next,
140  bi0, next0);
141  }
142 
143  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
144  }
145 
146  return frame->n_vectors;
147 }
148 
151  u32 sw_if_index0, u32 ip4_addr)
152 {
154  ip4_address_t *first_int_addr;
155 
156  if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
157  {
158  first_int_addr =
159  ip4_interface_first_address (sm->ip4_main, sw_if_index0,
160  0 /* just want the address */ );
161  rt->cached_sw_if_index = sw_if_index0;
162  if (first_int_addr)
163  rt->cached_ip4_address = first_int_addr->as_u32;
164  else
165  rt->cached_ip4_address = 0;
166  }
167 
168  if (PREDICT_FALSE (ip4_addr == rt->cached_ip4_address))
169  return 1;
170  else
171  return 0;
172 }
173 
176 {
177  if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
178  sm->max_translations)
179  return 1;
180 
181  return 0;
182 }
183 
184 always_inline void
187  u32 next)
188 {
189  u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
190 
191  from = bi_vector;
192  n_left_from = vec_len (bi_vector);
193  next_index = node->cached_next_index;
194  while (n_left_from > 0)
195  {
196  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
197  while (n_left_from > 0 && n_left_to_next > 0)
198  {
199  u32 bi0 = to_next[0] = from[0];
200  from += 1;
201  n_left_from -= 1;
202  to_next += 1;
203  n_left_to_next -= 1;
204  vlib_buffer_t *p0 = vlib_get_buffer (vm, bi0);
205  if (error)
206  p0->error = *error;
207  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
208  n_left_to_next, bi0, next);
209  }
210  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
211  }
212 }
213 
214 always_inline void
216 {
218  {
219  if (is_static)
220  u->nstaticsessions++;
221  else
222  u->nsessions++;
223  }
224 }
225 
226 always_inline void
228  u32 thread_index)
229 {
231  snat_user_key_t u_key;
233  thread_index);
234 
235  if (u->nstaticsessions == 0 && u->nsessions == 0)
236  {
237  u_key.addr.as_u32 = u->addr.as_u32;
238  u_key.fib_index = u->fib_index;
239  kv.key = u_key.as_u64;
241  pool_put (tsm->users, u);
242  clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
243  vlib_set_simple_counter (&sm->total_users, thread_index, 0,
244  pool_elts (tsm->users));
245  }
246 }
247 
248 always_inline void
249 nat44_delete_session (snat_main_t * sm, snat_session_t * ses,
250  u32 thread_index)
251 {
253  thread_index);
255  snat_user_t *u;
256  const snat_user_key_t u_key = {
257  .addr = ses->in2out.addr,
258  .fib_index = ses->in2out.fib_index
259  };
260  const u8 u_static = snat_is_session_static (ses);
261 
262  clib_dlist_remove (tsm->list_pool, ses->per_user_index);
263  pool_put_index (tsm->list_pool, ses->per_user_index);
264  if (sm->endpoint_dependent)
265  {
266  clib_dlist_remove (tsm->lru_pool, ses->lru_index);
267  pool_put_index (tsm->lru_pool, ses->lru_index);
268  }
269  pool_put (tsm->sessions, ses);
270  vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
271  pool_elts (tsm->sessions));
272 
273  kv.key = u_key.as_u64;
274  if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
275  {
276  u = pool_elt_at_index (tsm->users, value.value);
277  if (u_static)
278  u->nstaticsessions--;
279  else
280  u->nsessions--;
281 
282  nat44_delete_user_with_no_session (sm, u, thread_index);
283  }
284 }
285 
286 /** \brief Set TCP session state.
287  @return 1 if session was closed, otherwise 0
288 */
289 always_inline int
291  snat_session_t * ses, vlib_buffer_t * b,
292  u32 thread_index)
293 {
294  snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
295  u8 tcp_flags = vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags;
296  u32 tcp_ack_number = vnet_buffer (b)->ip.reass.tcp_ack_number;
297  u32 tcp_seq_number = vnet_buffer (b)->ip.reass.tcp_seq_number;
298  if ((ses->state == 0) && (tcp_flags & TCP_FLAG_RST))
299  ses->state = NAT44_SES_RST;
300  if ((ses->state == NAT44_SES_RST) && !(tcp_flags & TCP_FLAG_RST))
301  ses->state = 0;
302  if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
303  (ses->state & NAT44_SES_O2I_SYN))
304  ses->state = 0;
305  if (tcp_flags & TCP_FLAG_SYN)
306  ses->state |= NAT44_SES_I2O_SYN;
307  if (tcp_flags & TCP_FLAG_FIN)
308  {
309  ses->i2o_fin_seq = clib_net_to_host_u32 (tcp_seq_number);
310  ses->state |= NAT44_SES_I2O_FIN;
311  }
312  if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN))
313  {
314  if (clib_net_to_host_u32 (tcp_ack_number) > ses->o2i_fin_seq)
315  {
316  ses->state |= NAT44_SES_O2I_FIN_ACK;
317  if (nat44_is_ses_closed (ses))
318  { // if session is now closed, save the timestamp
319  ses->tcp_closed_timestamp = now + sm->tcp_transitory_timeout;
320  ses->last_lru_update = now;
321  }
322  }
323  }
324 
325  // move the session to proper LRU
326  if (ses->state)
327  {
328  ses->lru_head_index = tsm->tcp_trans_lru_head_index;
329  }
330  else
331  {
332  ses->lru_head_index = tsm->tcp_estab_lru_head_index;
333  }
334  clib_dlist_remove (tsm->lru_pool, ses->lru_index);
335  clib_dlist_addtail (tsm->lru_pool, ses->lru_head_index, ses->lru_index);
336  return 0;
337 }
338 
339 always_inline int
341  snat_session_t * ses, u8 tcp_flags,
342  u32 tcp_ack_number, u32 tcp_seq_number,
343  u32 thread_index)
344 {
345  snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
346  if ((ses->state == 0) && (tcp_flags & TCP_FLAG_RST))
347  ses->state = NAT44_SES_RST;
348  if ((ses->state == NAT44_SES_RST) && !(tcp_flags & TCP_FLAG_RST))
349  ses->state = 0;
350  if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
351  (ses->state & NAT44_SES_O2I_SYN))
352  ses->state = 0;
353  if (tcp_flags & TCP_FLAG_SYN)
354  ses->state |= NAT44_SES_O2I_SYN;
355  if (tcp_flags & TCP_FLAG_FIN)
356  {
357  ses->o2i_fin_seq = clib_net_to_host_u32 (tcp_seq_number);
358  ses->state |= NAT44_SES_O2I_FIN;
359  }
360  if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN))
361  {
362  if (clib_net_to_host_u32 (tcp_ack_number) > ses->i2o_fin_seq)
363  ses->state |= NAT44_SES_I2O_FIN_ACK;
364  if (nat44_is_ses_closed (ses))
365  { // if session is now closed, save the timestamp
366  ses->tcp_closed_timestamp = now + sm->tcp_transitory_timeout;
367  ses->last_lru_update = now;
368  }
369  }
370  // move the session to proper LRU
371  if (ses->state)
372  {
373  ses->lru_head_index = tsm->tcp_trans_lru_head_index;
374  }
375  else
376  {
377  ses->lru_head_index = tsm->tcp_estab_lru_head_index;
378  }
379  clib_dlist_remove (tsm->lru_pool, ses->lru_index);
380  clib_dlist_addtail (tsm->lru_pool, ses->lru_head_index, ses->lru_index);
381  return 0;
382 }
383 
385 nat44_session_get_timeout (snat_main_t * sm, snat_session_t * s)
386 {
387  switch (s->in2out.protocol)
388  {
389  case NAT_PROTOCOL_ICMP:
390  return sm->icmp_timeout;
391  case NAT_PROTOCOL_UDP:
392  return sm->udp_timeout;
393  case NAT_PROTOCOL_TCP:
394  {
395  if (s->state)
396  return sm->tcp_transitory_timeout;
397  else
398  return sm->tcp_established_timeout;
399  }
400  default:
401  return sm->udp_timeout;
402  }
403 
404  return 0;
405 }
406 
407 always_inline void
408 nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes,
409  u32 thread_index)
410 {
411  s->last_heard = now;
412  s->total_pkts++;
413  s->total_bytes += bytes;
414  nat_ha_sref (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
415  s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
416  s->total_pkts, s->total_bytes, thread_index,
417  &s->ha_last_refreshed, now);
418 }
419 
420 /** \brief Per-user LRU list maintenance */
421 always_inline void
422 nat44_session_update_lru (snat_main_t * sm, snat_session_t * s,
423  u32 thread_index)
424 {
425  /* don't update too often - timeout is in magnitude of seconds anyway */
426  if (s->last_heard > s->last_lru_update + 1)
427  {
428  if (!sm->endpoint_dependent)
429  {
430  clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
431  s->per_user_index);
432  clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
433  s->per_user_list_head_index, s->per_user_index);
434  }
435  else
436  {
437  clib_dlist_remove (sm->per_thread_data[thread_index].lru_pool,
438  s->lru_index);
439  clib_dlist_addtail (sm->per_thread_data[thread_index].lru_pool,
440  s->lru_head_index, s->lru_index);
441  }
442  s->last_lru_update = s->last_heard;
443  }
444 }
445 
446 always_inline void
448  u32 fib_index, u16 l_port, u16 r_port, u64 value,
450 {
451  kv->key[0] = (u64) r_addr->as_u32 << 32 | l_addr->as_u32;
452  kv->key[1] =
453  (u64) r_port << 48 | (u64) l_port << 32 | fib_index << 8 | proto;
454  kv->value = value;
455 }
456 
457 always_inline void
459  ip4_address_t * l_addr, ip4_address_t * r_addr, u8 * proto,
460  u32 * fib_index, u16 * l_port, u16 * r_port)
461 {
462  if (l_addr)
463  {
464  l_addr->as_u32 = kv->key[0] & (u32) ~ 0;
465  }
466  if (r_addr)
467  {
468  r_addr->as_u32 = kv->key[0] >> 32;
469  }
470  if (r_port)
471  {
472  *r_port = kv->key[1] >> 48;
473  }
474  if (l_port)
475  {
476  *l_port = (kv->key[1] >> 32) & (u16) ~ 0;
477  }
478  if (fib_index)
479  {
480  *fib_index = (kv->key[1] >> 8) & ((1 << 24) - 1);
481  }
482  if (proto)
483  {
484  *proto = kv->key[1] & (u8) ~ 0;
485  }
486 }
487 
488 always_inline void
490  u32 fib_index, u16 port)
491 {
492  kv->key = (u64) fib_index << 51 | (u64) proto << 48 | (u64) port << 32 |
493  addr->as_u32;
494 
495  kv->value = ~0ULL;
496 }
497 
500  u64 value, u8 * nat_proto, u16 * l_port, u16 * r_port,
502 {
503  u8 proto;
504  u16 _l_port, _r_port;
505  ip4_address_t *l_addr, *r_addr;
506 
507  icmp46_header_t *icmp0;
508  icmp_echo_header_t *echo0, *inner_echo0 = 0;
509  ip4_header_t *inner_ip0 = 0;
510  void *l4_header = 0;
511  icmp46_header_t *inner_icmp0;
512 
513  icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
514  echo0 = (icmp_echo_header_t *) (icmp0 + 1);
515 
517  (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
518  {
519  proto = IP_PROTOCOL_ICMP;
520  l_addr = &ip0->src_address;
521  r_addr = &ip0->dst_address;
522  _l_port = vnet_buffer (b)->ip.reass.l4_src_port; // TODO should this be src or dst?
523  _r_port = 0;
524  }
525  else
526  {
527  inner_ip0 = (ip4_header_t *) (echo0 + 1);
528  l4_header = ip4_next_header (inner_ip0);
529  proto = inner_ip0->protocol;
530  r_addr = &inner_ip0->src_address;
531  l_addr = &inner_ip0->dst_address;
532  switch (ip_proto_to_nat_proto (inner_ip0->protocol))
533  {
534  case NAT_PROTOCOL_ICMP:
535  inner_icmp0 = (icmp46_header_t *) l4_header;
536  inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
537  _r_port = 0;
538  _l_port = inner_echo0->identifier;
539  break;
540  case NAT_PROTOCOL_UDP:
541  case NAT_PROTOCOL_TCP:
542  _l_port = ((tcp_udp_header_t *) l4_header)->dst_port;
543  _r_port = ((tcp_udp_header_t *) l4_header)->src_port;
544  break;
545  default:
546  return NAT_IN2OUT_ED_ERROR_UNSUPPORTED_PROTOCOL;
547  }
548  }
549  make_ed_kv (l_addr, r_addr, proto, rx_fib_index, _l_port, _r_port, value,
550  kv);
551  if (nat_proto)
552  {
553  *nat_proto = ip_proto_to_nat_proto (proto);
554  }
555  if (l_port)
556  {
557  *l_port = _l_port;
558  }
559  if (r_port)
560  {
561  *r_port = _r_port;
562  }
563  return 0;
564 }
565 
566 
569  u64 value, u8 * nat_proto, u16 * l_port, u16 * r_port,
571 {
572  icmp46_header_t *icmp0;
573  u8 proto;
574  ip4_address_t *l_addr, *r_addr;
575  u16 _l_port, _r_port;
576  icmp_echo_header_t *echo0, *inner_echo0 = 0;
577  ip4_header_t *inner_ip0;
578  void *l4_header = 0;
579  icmp46_header_t *inner_icmp0;
580 
581  icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
582  echo0 = (icmp_echo_header_t *) (icmp0 + 1);
583 
585  (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
586  {
587  proto = IP_PROTOCOL_ICMP;
588  l_addr = &ip0->dst_address;
589  r_addr = &ip0->src_address;
590  _l_port = vnet_buffer (b)->ip.reass.l4_src_port; // TODO should this be src or dst?
591  _r_port = 0;
592  }
593  else
594  {
595  inner_ip0 = (ip4_header_t *) (echo0 + 1);
596  l4_header = ip4_next_header (inner_ip0);
597  proto = inner_ip0->protocol;
598  l_addr = &inner_ip0->src_address;
599  r_addr = &inner_ip0->dst_address;
600  switch (ip_proto_to_nat_proto (inner_ip0->protocol))
601  {
602  case NAT_PROTOCOL_ICMP:
603  inner_icmp0 = (icmp46_header_t *) l4_header;
604  inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
605  _l_port = inner_echo0->identifier;
606  _r_port = 0;
607  break;
608  case NAT_PROTOCOL_UDP:
609  case NAT_PROTOCOL_TCP:
610  _l_port = ((tcp_udp_header_t *) l4_header)->src_port;
611  _r_port = ((tcp_udp_header_t *) l4_header)->dst_port;
612  break;
613  default:
614  return -1;
615  }
616  }
617  make_ed_kv (l_addr, r_addr, proto, rx_fib_index, _l_port, _r_port, value,
618  kv);
619  if (nat_proto)
620  {
621  *nat_proto = ip_proto_to_nat_proto (proto);
622  }
623  if (l_port)
624  {
625  *l_port = _l_port;
626  }
627  if (r_port)
628  {
629  *r_port = _r_port;
630  }
631  return 0;
632 }
633 
634 always_inline void
636 {
637  u8 *data;
638  u8 opt_len, opts_len, kind;
639  u16 mss;
640 
641  if (!(sm->mss_clamping && tcp_syn (tcp)))
642  return;
643 
644  opts_len = (tcp_doff (tcp) << 2) - sizeof (tcp_header_t);
645  data = (u8 *) (tcp + 1);
646  for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
647  {
648  kind = data[0];
649 
650  if (kind == TCP_OPTION_EOL)
651  break;
652  else if (kind == TCP_OPTION_NOOP)
653  {
654  opt_len = 1;
655  continue;
656  }
657  else
658  {
659  if (opts_len < 2)
660  return;
661  opt_len = data[1];
662 
663  if (opt_len < 2 || opt_len > opts_len)
664  return;
665  }
666 
667  if (kind == TCP_OPTION_MSS)
668  {
669  mss = *(u16 *) (data + 2);
670  if (clib_net_to_host_u16 (mss) > sm->mss_clamping)
671  {
672  *sum =
673  ip_csum_update (*sum, mss, sm->mss_value_net, ip4_header_t,
674  length);
675  clib_memcpy_fast (data + 2, &sm->mss_value_net, 2);
676  }
677  return;
678  }
679  }
680 }
681 
682 /**
683  * @brief Check if packet should be translated
684  *
685  * Packets aimed at outside interface and external address with active session
686  * should be translated.
687  *
688  * @param sm NAT main
689  * @param rt NAT runtime data
690  * @param sw_if_index0 index of the inside interface
691  * @param ip0 IPv4 header
692  * @param proto0 NAT protocol
693  * @param rx_fib_index0 RX FIB index
694  *
695  * @returns 0 if packet should be translated otherwise 1
696  */
697 static inline int
699  u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
700  u32 rx_fib_index0)
701 {
702  if (sm->out2in_dpo)
703  return 0;
704 
706  nat_outside_fib_t *outside_fib;
707  fib_prefix_t pfx = {
709  .fp_len = 32,
710  .fp_addr = {
711  .ip4.as_u32 = ip0->dst_address.as_u32,
712  }
713  ,
714  };
715 
716  /* Don't NAT packet aimed at the intfc address */
717  if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
718  ip0->dst_address.as_u32)))
719  return 1;
720 
721  fei = fib_table_lookup (rx_fib_index0, &pfx);
722  if (FIB_NODE_INDEX_INVALID != fei)
723  {
725  if (sw_if_index == ~0)
726  {
727  vec_foreach (outside_fib, sm->outside_fibs)
728  {
729  fei = fib_table_lookup (outside_fib->fib_index, &pfx);
730  if (FIB_NODE_INDEX_INVALID != fei)
731  {
732  sw_if_index = fib_entry_get_resolving_interface (fei);
733  if (sw_if_index != ~0)
734  break;
735  }
736  }
737  }
738  if (sw_if_index == ~0)
739  return 1;
740 
742  /* *INDENT-OFF* */
743  pool_foreach (i, sm->interfaces, ({
744  /* NAT packet aimed at outside interface */
745  if ((nat_interface_is_outside (i)) && (sw_if_index == i->sw_if_index))
746  return 0;
747  }));
748  /* *INDENT-ON* */
749  }
750 
751  return 1;
752 }
753 
754 static inline void
756 {
757  u32 v;
758 
759  v = clib_net_to_host_u32 (a->as_u32) + 1;
760  a->as_u32 = clib_host_to_net_u32 (v);
761 }
762 
763 #endif /* __included_nat_inlines_h__ */
764 
765 /*
766  * fd.io coding-style-patch-verification: ON
767  *
768  * Local Variables:
769  * eval: (c-set-style "gnu")
770  * End:
771  */
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
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:212
nat_outside_fib_t * outside_fibs
Definition: nat.h:566
dlist_elt_t * lru_pool
Definition: nat.h:473
#define snat_is_session_static(s)
Check if SNAT session is created from static mapping.
Definition: nat.h:728
u32 sessions_per_user_list_head_index
Definition: nat.h:307
End of options.
Definition: tcp_packet.h:104
u32 arc_next_index
Definition: nat.h:68
#define NAT44_SES_I2O_FIN
Definition: nat.h:223
a
Definition: bitmap.h:538
u32 icmp_timeout
Definition: nat.h:652
u8 runtime_data[0]
Function dependent node-runtime data.
Definition: node.h:525
ip4_address_t src_address
Definition: ip4_packet.h:170
#define vnet_buffer2(b)
Definition: buffer.h:482
static u32 nat44_session_get_timeout(snat_main_t *sm, snat_session_t *s)
Definition: nat_inlines.h:385
#define TCP_FLAG_SYN
Definition: fa_node.h:13
u32 nsessions
Definition: nat.h:308
unsigned long u64
Definition: types.h:89
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
ip4_address_t * ip4_interface_first_address(ip4_main_t *im, u32 sw_if_index, ip_interface_address_t **result_ia)
Definition: ip4_forward.c:281
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:489
#define tcp_doff(_th)
Definition: tcp_packet.h:78
u32 nstaticsessions
Definition: nat.h:309
u16 vlib_error_t
Definition: error.h:43
uword ip_csum_t
Definition: ip_packet.h:244
u32 fib_index
Definition: nat.h:306
#define nat44_is_ses_closed(s)
Check if NAT44 endpoint-dependent TCP session is closed.
Definition: nat.h:782
#define NAT44_SES_O2I_FIN
Definition: nat.h:224
No operation.
Definition: tcp_packet.h:105
dlist_elt_t * list_pool
Definition: nat.h:470
struct _tcp_header tcp_header_t
vhost_vring_addr_t addr
Definition: vhost_user.h:254
static_always_inline int get_icmp_i2o_ed_key(vlib_buffer_t *b, ip4_header_t *ip0, u32 rx_fib_index, u64 value, u8 *nat_proto, u16 *l_port, u16 *r_port, clib_bihash_kv_16_8_t *kv)
Definition: nat_inlines.h:499
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
clib_bihash_8_8_t user_hash
Definition: nat.h:461
u32 cached_sw_if_index
Definition: nat.h:685
u32 next_index
Definition: nat.h:67
void nat_ha_sref(ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index, u32 total_pkts, u64 total_bytes, u32 thread_index, f64 *last_refreshed, f64 now)
Create session refresh HA event.
Definition: nat_ha.c:734
static int snat_not_translate_fast(snat_main_t *sm, vlib_node_runtime_t *node, u32 sw_if_index0, ip4_header_t *ip0, u32 proto0, u32 rx_fib_index0)
Check if packet should be translated.
Definition: nat_inlines.h:698
u32 max_translations_per_user
Definition: nat.h:640
Limit MSS.
Definition: tcp_packet.h:106
#define static_always_inline
Definition: clib.h:106
#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
vl_api_interface_index_t sw_if_index
Definition: gre.api:53
ip4_address_t dst_address
Definition: ip4_packet.h:170
#define TCP_FLAG_ACK
Definition: fa_node.h:16
ip4_address_t addr
Definition: nat.h:305
#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.
Aggregate type for a prefix.
Definition: fib_types.h:203
static_always_inline u8 icmp_type_is_error_message(u8 icmp_type)
Definition: inlines.h:53
static void split_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:458
ip4_main_t * ip4_main
Definition: nat.h:672
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:241
unsigned int u32
Definition: types.h:88
fib_node_index_t fib_table_lookup(u32 fib_index, const fib_prefix_t *prefix)
Perfom a longest prefix match in the non-forwarding table.
Definition: fib_table.c:68
#define NAT44_SES_RST
Definition: nat.h:229
static u8 maximum_sessions_exceeded(snat_main_t *sm, u32 thread_index)
Definition: nat_inlines.h:175
static void nat44_delete_session(snat_main_t *sm, snat_session_t *ses, u32 thread_index)
Definition: nat_inlines.h:249
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:136
u32 max_translations
Definition: nat.h:635
static void mss_clamping(snat_main_t *sm, tcp_header_t *tcp, ip_csum_t *sum)
Definition: nat_inlines.h:635
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
u32 fib_index
Definition: nat.h:328
u16 mss_value_net
Definition: nat.h:656
vl_api_ip_proto_t proto
Definition: acl_types.api:50
u64 key
the key
Definition: bihash_8_8.h:41
u16 mss_clamping
Definition: nat.h:655
static void clib_dlist_addtail(dlist_elt_t *pool, u32 head_index, u32 new_index)
Definition: dlist.h:43
unsigned short u16
Definition: types.h:57
u8 out2in_dpo
Definition: nat.h:630
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:302
u32 udp_timeout
Definition: nat.h:649
#define PREDICT_FALSE(x)
Definition: clib.h:118
#define always_inline
Definition: ipsec.h:28
#define TCP_FLAG_FIN
Definition: fa_node.h:12
#define NAT44_SES_O2I_SYN
Definition: nat.h:228
static void vlib_set_simple_counter(vlib_simple_counter_main_t *cm, u32 thread_index, u32 index, u64 value)
Set a simple counter.
Definition: counter.h:94
#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: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:338
vlib_main_t * vm
Definition: in2out_ed.c:1599
#define TCP_FLAG_RST
Definition: fa_node.h:14
u32 fib_entry_get_resolving_interface(fib_node_index_t entry_index)
Definition: fib_entry.c:1458
u64 value
the value
Definition: bihash_8_8.h:42
snat_user_t * users
Definition: nat.h:464
static int nat44_set_tcp_session_state_o2i(snat_main_t *sm, f64 now, snat_session_t *ses, u8 tcp_flags, u32 tcp_ack_number, u32 tcp_seq_number, u32 thread_index)
Definition: nat_inlines.h:340
static void nat44_delete_user_with_no_session(snat_main_t *sm, snat_user_t *u, u32 thread_index)
Definition: nat_inlines.h:227
u16 n_vectors
Definition: node.h:399
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
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
static void nat44_session_update_counters(snat_session_t *s, f64 now, uword bytes, u32 thread_index)
Definition: nat_inlines.h:408
u8 data[]
Packet data.
Definition: buffer.h:181
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:30
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
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
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1599
u32 tcp_transitory_timeout
Definition: nat.h:650
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:517
u8 value
Definition: qos.api:54
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:331
#define tcp_syn(_th)
Definition: tcp_packet.h:80
static int nat44_set_tcp_session_state_i2o(snat_main_t *sm, f64 now, snat_session_t *ses, vlib_buffer_t *b, u32 thread_index)
Set TCP session state.
Definition: nat_inlines.h:290
u8 data[128]
Definition: ipsec_types.api:89
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:422
u64 as_u64
Definition: nat.h:111
ip4_address_t addr
Definition: nat.h:108
static void clib_dlist_remove(dlist_elt_t *pool, u32 index)
Definition: dlist.h:99
#define NAT44_SES_I2O_SYN
Definition: nat.h:227
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
vlib_simple_counter_main_t total_users
Definition: nat.h:659
#define FIB_NODE_INDEX_INVALID
Definition: fib_types.h:31
static void user_session_increment(snat_main_t *sm, snat_user_t *u, u8 is_static)
Definition: nat_inlines.h:215
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
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:536
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:294
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:150
u32 fib_index
Definition: nat.h:109
u16 port
Definition: lb_types.api:72
static_always_inline int get_icmp_o2i_ed_key(vlib_buffer_t *b, ip4_header_t *ip0, u32 rx_fib_index, u64 value, u8 *nat_proto, u16 *l_port, u16 *r_port, clib_bihash_kv_16_8_t *kv)
Definition: nat_inlines.h:568
static void increment_v4_address(ip4_address_t *a)
Definition: nat_inlines.h:755
#define vnet_buffer(b)
Definition: buffer.h:417
#define NAT44_SES_I2O_FIN_ACK
Definition: nat.h:225
#define vec_foreach(var, vec)
Vector iterator.
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1600
u16 flags
Copy of main node flags.
Definition: node.h:511
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:185
u8 endpoint_dependent
Definition: nat.h:631
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:304
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
#define NAT44_SES_O2I_FIN_ACK
Definition: nat.h:226
vlib_simple_counter_main_t total_sessions
Definition: nat.h:660
snat_session_t * sessions
Definition: nat.h:467
u32 cached_ip4_address
Definition: nat.h:686
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 void make_ed_kv(ip4_address_t *l_addr, ip4_address_t *r_addr, u8 proto, u32 fib_index, u16 l_port, u16 r_port, u64 value, clib_bihash_kv_16_8_t *kv)
Definition: nat_inlines.h:447
snat_interface_t * interfaces
Definition: nat.h:548
NAT active-passive HA.
u32 tcp_established_timeout
Definition: nat.h:651
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128