FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
nat.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 NAT plugin global declarations
17  */
18 #ifndef __included_nat_h__
19 #define __included_nat_h__
20 
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/ip/icmp46_packet.h>
25 #include <vnet/api_errno.h>
26 #include <vnet/fib/fib_source.h>
27 #include <vppinfra/elog.h>
28 #include <vppinfra/bihash_8_8.h>
29 #include <vppinfra/bihash_16_8.h>
30 #include <vppinfra/dlist.h>
31 #include <vppinfra/error.h>
32 #include <vlibapi/api.h>
33 #include <vlib/log.h>
34 
35 /* default session timeouts */
36 #define SNAT_UDP_TIMEOUT 300
37 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
38 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
39 #define SNAT_ICMP_TIMEOUT 60
40 
41 /* number of worker handoff frame queue elements */
42 #define NAT_FQ_NELTS 64
43 
44 /* NAT buffer flags */
45 #define SNAT_FLAG_HAIRPINNING (1 << 0)
46 
47 typedef struct
48 {
51 
52 typedef enum
53 {
64 } nat_next_t;
65 
66 typedef struct
67 {
70 
71 #define nat_buffer_opaque(b) \
72  ((nat_buffer_opaque_t *)((vnet_buffer_opaque2_t *)b->opaque2)->__unused2)
73 
74 /*
75 STATIC_ASSERT (sizeof (nat_buffer_opaque_t) <=
76  STRUCT_SIZE_OF (vnet_buffer_opaque_t, unused),
77  "Custom meta-data too large for vnet_buffer_opaque_t");
78 
79 #define nat_buffer_opaque(b) \
80  ((nat_buffer_opaque_t *)((u8 *)((b)->opaque) + \
81  STRUCT_OFFSET_OF (vnet_buffer_opaque_t, unused)))*/
82 
83 /* session key (4-tuple) */
84 typedef struct
85 {
86  union
87  {
88  struct
89  {
92  u16 protocol:3, fib_index:13;
93  };
95  };
97 
98 /* endpoint-dependent session key (6-tuple) */
99 typedef struct
100 {
101  union
102  {
103  struct
104  {
107  u32 proto:8, fib_index:24;
110  };
112  };
114 
115 /* deterministic session outside key */
116 typedef struct
117 {
118  union
119  {
120  struct
121  {
125  };
127  };
129 
130 /* user (internal host) key */
131 typedef struct
132 {
133  union
134  {
135  struct
136  {
139  };
141  };
143 
144 typedef struct
145 {
150 
151 /* NAT API Configuration flags */
152 #define foreach_nat_config_flag \
153  _(0x01, IS_TWICE_NAT) \
154  _(0x02, IS_SELF_TWICE_NAT) \
155  _(0x04, IS_OUT2IN_ONLY) \
156  _(0x08, IS_ADDR_ONLY) \
157  _(0x10, IS_OUTSIDE) \
158  _(0x20, IS_INSIDE) \
159  _(0x40, IS_STATIC) \
160  _(0x80, IS_EXT_HOST_VALID) \
161 
163 {
164 #define _(n,f) NAT_API_##f = n,
166 #undef _
168 
169 /* External address and port allocation modes */
170 #define foreach_nat_addr_and_port_alloc_alg \
171  _(0, DEFAULT, "default") \
172  _(1, MAPE, "map-e") \
173  _(2, RANGE, "port-range")
174 
175 typedef enum
176 {
177 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
179 #undef _
181 
182 
183 /* Supported L4 protocols */
184 #define foreach_snat_protocol \
185  _(UDP, 0, udp, "udp") \
186  _(TCP, 1, tcp, "tcp") \
187  _(ICMP, 2, icmp, "icmp")
188 
189 typedef enum
190 {
191 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
193 #undef _
195 
196 
197 /* Session state */
198 #define foreach_snat_session_state \
199  _(0, UNKNOWN, "unknown") \
200  _(1, UDP_ACTIVE, "udp-active") \
201  _(2, TCP_SYN_SENT, "tcp-syn-sent") \
202  _(3, TCP_ESTABLISHED, "tcp-established") \
203  _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
204  _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
205  _(6, TCP_CLOSING, "tcp-closing") \
206  _(7, TCP_LAST_ACK, "tcp-last-ack") \
207  _(8, TCP_CLOSED, "tcp-closed") \
208  _(9, ICMP_ACTIVE, "icmp-active")
209 
210 typedef enum
211 {
212 #define _(v, N, s) SNAT_SESSION_##N = v,
214 #undef _
216 
217 #define foreach_nat_in2out_ed_error \
218 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
219 _(IN2OUT_PACKETS, "good in2out packets processed") \
220 _(OUT_OF_PORTS, "out of ports") \
221 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
222 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
223 _(DROP_FRAGMENT, "drop fragment") \
224 _(MAX_REASS, "maximum reassemblies exceeded") \
225 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
226 _(NON_SYN, "non-SYN packet try to create session") \
227 _(TCP_PACKETS, "TCP packets") \
228 _(UDP_PACKETS, "UDP packets") \
229 _(ICMP_PACKETS, "ICMP packets") \
230 _(OTHER_PACKETS, "other protocol packets") \
231 _(FRAGMENTS, "fragments") \
232 _(CACHED_FRAGMENTS, "cached fragments") \
233 _(PROCESSED_FRAGMENTS, "processed fragments")
234 
235 typedef enum
236 {
237 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
239 #undef _
242 
243 #define foreach_nat_out2in_ed_error \
244 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
245 _(OUT2IN_PACKETS, "good out2in packets processed") \
246 _(OUT_OF_PORTS, "out of ports") \
247 _(BAD_ICMP_TYPE, "unsupported ICMP type") \
248 _(NO_TRANSLATION, "no translation") \
249 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
250 _(DROP_FRAGMENT, "drop fragment") \
251 _(MAX_REASS, "maximum reassemblies exceeded") \
252 _(MAX_FRAG, "maximum fragments per reassembly exceeded")\
253 _(NON_SYN, "non-SYN packet try to create session") \
254 _(TCP_PACKETS, "TCP packets") \
255 _(UDP_PACKETS, "UDP packets") \
256 _(ICMP_PACKETS, "ICMP packets") \
257 _(OTHER_PACKETS, "other protocol packets") \
258 _(FRAGMENTS, "fragments") \
259 _(CACHED_FRAGMENTS, "cached fragments") \
260 _(PROCESSED_FRAGMENTS, "processed fragments")
261 
262 typedef enum
263 {
264 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
266 #undef _
269 
270 
271 /* Endpoint dependent TCP session state */
272 #define NAT44_SES_I2O_FIN 1
273 #define NAT44_SES_O2I_FIN 2
274 #define NAT44_SES_I2O_FIN_ACK 4
275 #define NAT44_SES_O2I_FIN_ACK 8
276 #define NAT44_SES_I2O_SYN 16
277 #define NAT44_SES_O2I_SYN 32
278 #define NAT44_SES_RST 64
279 
280 /* Session flags */
281 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
282 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO 2
283 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
284 #define SNAT_SESSION_FLAG_TWICE_NAT 8
285 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT 16
286 #define SNAT_SESSION_FLAG_FWD_BYPASS 32
287 #define SNAT_SESSION_FLAG_AFFINITY 64
288 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE 128
289 
290 /* NAT interface flags */
291 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
292 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
293 
294 /* Static mapping flags */
295 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY 1
296 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY 2
297 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
298 #define NAT_STATIC_MAPPING_FLAG_LB 8
299 
300 /* *INDENT-OFF* */
301 typedef CLIB_PACKED(struct
302 {
303  /* Outside network key */
304  snat_session_key_t out2in;
305 
306  /* Inside network key */
307  snat_session_key_t in2out;
308 
309  /* Flags */
310  u32 flags;
311 
312  /* Per-user translations */
313  u32 per_user_index;
314  u32 per_user_list_head_index;
315 
316  /* Last heard timer */
317  f64 last_heard;
318 
319  /* Last HA refresh */
320  f64 ha_last_refreshed;
321 
322  /* Counters */
323  u64 total_bytes;
324  u32 total_pkts;
325 
326  /* External host address and port */
327  ip4_address_t ext_host_addr;
328  u16 ext_host_port;
329 
330  /* External host address and port after translation */
331  ip4_address_t ext_host_nat_addr;
332  u16 ext_host_nat_port;
333 
334  /* TCP session state */
335  u8 state;
336  u32 i2o_fin_seq;
337  u32 o2i_fin_seq;
338 
339  /* user index */
340  u32 user_index;
341 }) snat_session_t;
342 /* *INDENT-ON* */
343 
344 
345 typedef struct
346 {
352 } snat_user_t;
353 
354 typedef struct
355 {
358 /* *INDENT-OFF* */
359 #define _(N, i, n, s) \
360  u16 busy_##n##_ports; \
361  u16 * busy_##n##_ports_per_thread; \
362  uword * busy_##n##_port_bitmap;
364 #undef _
365 /* *INDENT-ON* */
366 } snat_address_t;
367 
368 typedef struct
369 {
373 
374 typedef struct
375 {
376  /* Inside network port */
378  /* Outside network address and port */
380  /* Session state */
382  /* Expire timeout */
385 
386 typedef struct
387 {
388  /* inside IP address range */
391  /* outside IP address range */
394  /* inside IP addresses / outside IP addresses */
396  /* number of ports available to internal host */
398  /* session counter */
400  /* vector of sessions */
403 
404 typedef struct
405 {
406  /* backend IP address */
408  /* backend port number */
410  /* probability of the backend to be randomly matched */
413  /* backend FIB table */
417 
418 typedef enum
419 {
420  /* twice-nat disabled */
422  /* twice-nat enabled */
424  /* twice-nat only when src IP equals dst IP after translation */
427 
428 typedef enum
429 {
430  /* no load-balancing */
432  /* load-balancing */
434  /* load-balancing with affinity */
436 } lb_nat_type_t;
437 
438 typedef struct
439 {
440  /* local IP address */
442  /* external IP address */
444  /* local port */
446  /* external port */
448  /* is twice-nat */
449  twice_nat_type_t twice_nat;
450  /* local FIB table */
453  /* protocol */
454  snat_protocol_t proto;
455  /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
457  /* worker threads used by backends/local host */
459  /* opaque string tag */
460  u8 *tag;
461  /* backends for load-balancing mode */
463  /* affinity per service lis */
465  /* flags */
468 
469 typedef struct
470 {
474 
475 typedef struct
476 {
482  snat_protocol_t proto;
486  int is_add;
489  u8 *tag;
491 
492 typedef struct
493 {
494  /* Main lookup tables */
495  clib_bihash_8_8_t out2in;
496  clib_bihash_8_8_t in2out;
497 
498  /* Endpoint dependent sessions lookup tables */
499  clib_bihash_16_8_t out2in_ed;
500  clib_bihash_16_8_t in2out_ed;
501 
502  /* Find-a-user => src address lookup */
503  clib_bihash_8_8_t user_hash;
504 
505  /* User pool */
507 
508  /* Session pool */
509  snat_session_t *sessions;
510 
511  /* Pool of doubly-linked list elements */
513 
514  /* NAT thread index */
516 
517  /* real thread index */
520 
521 struct snat_main_s;
522 
523 /* ICMP session match function */
526  u32 thread_index,
527  vlib_buffer_t * b0,
528  ip4_header_t * ip0, u8 * p_proto,
529  snat_session_key_t * p_value,
530  u8 * p_dont_translate, void *d,
531  void *e);
532 
533 /* Return worker thread index for given packet */
535  u32 rx_fib_index,
536  u8 is_output);
537 
539  ip4_header_t * ip,
540  u32 rx_fib_index,
541  u8 is_output);
542 
543 /* NAT address and port allacotaion function */
544 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
545  addresses,
546  u32 fib_index,
547  u32 thread_index,
548  snat_session_key_t * k,
550  u32 snat_thread_index);
551 
552 typedef struct snat_main_s
553 {
554  /* ICMP session match functions */
557 
558  /* Thread settings */
566 
567  /* Per thread data */
569 
570  /* Find a static mapping by local */
571  clib_bihash_8_8_t static_mapping_by_local;
572 
573  /* Find a static mapping by external */
574  clib_bihash_8_8_t static_mapping_by_external;
575 
576  /* Static mapping pool */
578 
579  /* Interface pool */
582 
583  /* Vector of outside addresses */
584  snat_address_t *addresses;
585  /* Address and port allocation function */
587  /* Address and port allocation type */
588  nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
589  /* Port set parameters (MAP-E) */
593  /* Port range parameters */
596 
597  /* vector of outside fibs */
599 
600  /* Vector of twice NAT addresses for extenal hosts */
601  snat_address_t *twice_nat_addresses;
602 
603  /* sw_if_indices whose intfc addresses should be auto-added */
606 
607  /* vector of interface address static mappings to resolve. */
609 
610  /* Randomize port allocation order */
612 
613  /* Worker handoff frame-queue index */
617 
618  /* node indexes */
620 
621  /* handoff fq nodes */
625 
626  /* respect feature arc nodes */
629 
643 
650 
651 
652  /* Deterministic NAT mappings */
654 
655  /* If forwarding is enabled */
657 
658  /* Config parameters */
674 
675  /* values of various timeouts */
680 
681  /* TCP MSS clamping */
684 
685  /* counters/gauges */
688 
689  /* API message ID base */
691 
692  /* log class */
694  /* logging level */
696 
697  /* convenience */
703 } snat_main_t;
704 
705 typedef struct
706 {
710 
711 typedef struct
712 {
716 
717 extern snat_main_t snat_main;
718 
719 // nat pre ed next_node feature classification
723 
744 
747 
748 /* format functions */
758 /* unformat functions */
760 
761 /** \brief Check if SNAT session is created from static mapping.
762  @param s SNAT session
763  @return 1 if SNAT session is created from static mapping otherwise 0
764 */
765 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
766 
767 /** \brief Check if SNAT session for unknown protocol.
768  @param s SNAT session
769  @return 1 if SNAT session for unknown protocol otherwise 0
770 */
771 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
772 
773 /** \brief Check if NAT session is twice NAT.
774  @param s NAT session
775  @return 1 if NAT session is twice NAT
776 */
777 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
778 
779 /** \brief Check if NAT session is load-balancing.
780  @param s NAT session
781  @return 1 if NAT session is load-balancing
782 */
783 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
784 
785 /** \brief Check if NAT session is forwarding bypass.
786  @param s NAT session
787  @return 1 if NAT session is load-balancing
788 */
789 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
790 
791 /** \brief Check if NAT session is endpoint dependent.
792  @param s NAT session
793  @return 1 if NAT session is endpoint dependent
794 */
795 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
796 
797 /** \brief Check if NAT session has affinity record.
798  @param s NAT session
799  @return 1 if NAT session has affinity record
800 */
801 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
802 
803 /** \brief Check if NAT interface is inside.
804  @param i NAT interfce
805  @return 1 if inside interface
806 */
807 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
808 
809 /** \brief Check if NAT interface is outside.
810  @param i NAT interfce
811  @return 1 if outside interface
812 */
813 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
814 
815 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
816  @param s NAT session
817  @return 1 if session is closed
818 */
819 #define nat44_is_ses_closed(s) s->state == 0xf
820 
821 /** \brief Check if NAT static mapping is address only (1:1NAT).
822  @param sm NAT static mapping
823  @return 1 if 1:1NAT, 0 if 1:1NAPT
824 */
825 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
826 
827 /** \brief Check if NAT static mapping match only out2in direction.
828  @param sm NAT static mapping
829  @return 1 if rule match only out2in direction
830 */
831 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
832 
833 /** \brief Check if NAT static mapping is identity NAT.
834  @param sm NAT static mapping
835  @return 1 if identity NAT
836 */
837 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
838 
839 /** \brief Check if NAT static mapping is load-balancing.
840  @param sm NAT static mapping
841  @return 1 if load-balancing
842 */
843 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
844 
845 /** \brief Check if client initiating TCP connection (received SYN from client)
846  @param t TCP header
847  @return 1 if client initiating TCP connection
848 */
849 always_inline bool
851 {
852  return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
853 }
854 
855 /* logging */
856 #define nat_log_err(...) \
857  vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
858 #define nat_log_warn(...) \
859  vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
860 #define nat_log_notice(...) \
861  vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
862 #define nat_log_info(...) \
863  vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
864 #define nat_log_debug(...)\
865  vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
866 
867 /* NAT API Logging Levels */
868 #define foreach_nat_log_level \
869  _(0x00, LOG_NONE) \
870  _(0x01, LOG_ERROR) \
871  _(0x02, LOG_WARNING) \
872  _(0x03, LOG_NOTICE) \
873  _(0x04, LOG_INFO) \
874  _(0x05, LOG_DEBUG)
875 
876 typedef enum nat_log_level_t_
877 {
878 #define _(n,f) SNAT_##f = n,
880 #undef _
882 
883 #define nat_elog(_level, _str) \
884 do \
885  { \
886  snat_main_t *sm = &snat_main; \
887  if (PREDICT_FALSE (sm->log_level >= _level)) \
888  { \
889  ELOG_TYPE_DECLARE (e) = \
890  { \
891  .format = "nat-msg " _str, \
892  .format_args = "", \
893  }; \
894  ELOG_DATA (&sm->vlib_main->elog_main, e); \
895  } \
896  } while (0);
897 
898 #define nat_elog_addr(_level, _str, _addr) \
899 do \
900  { \
901  if (PREDICT_FALSE (sm->log_level >= _level)) \
902  { \
903  ELOG_TYPE_DECLARE (e) = \
904  { \
905  .format = "nat-msg " _str " %d.%d.%d.%d", \
906  .format_args = "i1i1i1i1", \
907  }; \
908  CLIB_PACKED(struct \
909  { \
910  u8 oct1; \
911  u8 oct2; \
912  u8 oct3; \
913  u8 oct4; \
914  }) *ed; \
915  ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
916  ed->oct4 = _addr >> 24; \
917  ed->oct3 = _addr >> 16; \
918  ed->oct2 = _addr >> 8; \
919  ed->oct1 = _addr; \
920  } \
921  } while (0);
922 
923 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst) \
924 do \
925  { \
926  if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
927  { \
928  ELOG_TYPE_DECLARE (e) = \
929  { \
930  .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
931  " tid from: %d to: %d fib: %d", \
932  .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
933  }; \
934  CLIB_PACKED(struct \
935  { \
936  u8 src_oct1; \
937  u8 src_oct2; \
938  u8 src_oct3; \
939  u8 src_oct4; \
940  u8 dst_oct1; \
941  u8 dst_oct2; \
942  u8 dst_oct3; \
943  u8 dst_oct4; \
944  u32 ftid; \
945  u32 ttid; \
946  u32 fib; \
947  }) *ed; \
948  ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
949  ed->src_oct1 = _src >> 24; \
950  ed->src_oct2 = _src >> 16; \
951  ed->src_oct3 = _src >> 8; \
952  ed->src_oct4 = _src; \
953  ed->dst_oct1 = _dst >> 24; \
954  ed->dst_oct2 = _dst >> 16; \
955  ed->dst_oct3 = _dst >> 8; \
956  ed->dst_oct4 = _dst; \
957  ed->ftid = vlib_get_thread_index (); \
958  ed->ttid = _tid; \
959  ed->fib = _fib; \
960  } \
961  } while (0);
962 
963 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst) \
964 do \
965  { \
966  if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG)) \
967  { \
968  ELOG_TYPE_DECLARE (e) = \
969  { \
970  .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
971  " tid:%d prt:%d fib:%d", \
972  .format_args = "i1i1i1i1i1i1i1i1i4i4i4", \
973  }; \
974  CLIB_PACKED(struct \
975  { \
976  u8 src_oct1; \
977  u8 src_oct2; \
978  u8 src_oct3; \
979  u8 src_oct4; \
980  u8 dst_oct1; \
981  u8 dst_oct2; \
982  u8 dst_oct3; \
983  u8 dst_oct4; \
984  u32 tid; \
985  u32 prt; \
986  u32 fib; \
987  }) *ed; \
988  ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
989  ed->src_oct1 = _src >> 24; \
990  ed->src_oct2 = _src >> 16; \
991  ed->src_oct3 = _src >> 8; \
992  ed->src_oct4 = _src; \
993  ed->dst_oct1 = _dst >> 24; \
994  ed->dst_oct2 = _dst >> 16; \
995  ed->dst_oct3 = _dst >> 8; \
996  ed->dst_oct4 = _dst; \
997  ed->tid = vlib_get_thread_index (); \
998  ed->prt = _prt; \
999  ed->fib = _fib; \
1000  } \
1001  } while (0);
1002 
1003 #define nat_elog_X1(_level, _fmt, _arg, _val1) \
1004 do \
1005  { \
1006  snat_main_t *sm = &snat_main; \
1007  if (PREDICT_FALSE (sm->log_level >= _level)) \
1008  { \
1009  ELOG_TYPE_DECLARE (e) = \
1010  { \
1011  .format = "nat-msg " _fmt, \
1012  .format_args = _arg, \
1013  }; \
1014  CLIB_PACKED(struct \
1015  { \
1016  typeof (_val1) val1; \
1017  }) *ed; \
1018  ed = ELOG_DATA (&sm->vlib_main->elog_main, e); \
1019  ed->val1 = _val1; \
1020  } \
1021  } while (0);
1022 
1023 #define nat_elog_notice(nat_elog_str) \
1024  nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
1025 #define nat_elog_warn(nat_elog_str) \
1026  nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
1027 #define nat_elog_err(nat_elog_str) \
1028  nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
1029 #define nat_elog_debug(nat_elog_str) \
1030  nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
1031 #define nat_elog_info(nat_elog_str) \
1032  nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1033 
1034 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1035  nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1036 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1037  nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1038 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1039  nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1040 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1041  nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1042 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1043  nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1044 
1045 /* ICMP session match functions */
1047  u32 thread_index, vlib_buffer_t * b0,
1048  ip4_header_t * ip0, u8 * p_proto,
1049  snat_session_key_t * p_value,
1050  u8 * p_dont_translate, void *d, void *e);
1052  u32 thread_index, vlib_buffer_t * b0,
1053  ip4_header_t * ip0, u8 * p_proto,
1054  snat_session_key_t * p_value,
1055  u8 * p_dont_translate, void *d, void *e);
1057  u32 thread_index, vlib_buffer_t * b0,
1058  ip4_header_t * ip0, u8 * p_proto,
1059  snat_session_key_t * p_value,
1060  u8 * p_dont_translate, void *d, void *e);
1062  u32 thread_index, vlib_buffer_t * b0,
1063  ip4_header_t * ip0, u8 * p_proto,
1064  snat_session_key_t * p_value,
1065  u8 * p_dont_translate, void *d, void *e);
1066 
1067 /* ICMP deterministic NAT session match functions */
1069  u32 thread_index, vlib_buffer_t * b0,
1070  ip4_header_t * ip0, u8 * p_proto,
1071  snat_session_key_t * p_value,
1072  u8 * p_dont_translate, void *d, void *e);
1074  u32 thread_index, vlib_buffer_t * b0,
1075  ip4_header_t * ip0, u8 * p_proto,
1076  snat_session_key_t * p_value,
1077  u8 * p_dont_translate, void *d, void *e);
1078 
1079 /* ICMP endpoint-dependent session match functions */
1081  u32 thread_index, vlib_buffer_t * b0,
1082  ip4_header_t * ip0, u8 * p_proto,
1083  snat_session_key_t * p_value,
1084  u8 * p_dont_translate, void *d, void *e);
1086  u32 thread_index, vlib_buffer_t * b0,
1087  ip4_header_t * ip0, u8 * p_proto,
1088  snat_session_key_t * p_value,
1089  u8 * p_dont_translate, void *d, void *e);
1090 
1092  icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1093  vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1094  void *d, void *e);
1095 
1097  icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1098  vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1099  void *d, void *e);
1100 
1101 /* hairpinning functions */
1103  ip4_header_t * ip0, icmp46_header_t * icmp0,
1104  int is_ed);
1106  ip4_header_t * ip);
1108  ip4_header_t * ip);
1110  ip4_header_t * ip0, udp_header_t * udp0,
1111  tcp_header_t * tcp0, u32 proto0, int is_ed);
1113  ip4_header_t * ip0, u16 sport, u16 dport,
1114  u32 proto0, int is_ed);
1115 
1116 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1121 
1122 /**
1123  * @brief Increment IPv4 address
1124  */
1126 
1127 /**
1128  * @brief Add external address to NAT44 pool
1129  *
1130  * @param addr IPv4 address
1131  * @param vrf_id VRF id of tenant, ~0 means independent of VRF
1132  * @param twice_nat 1 if twice NAT address
1133  *
1134  * @return 0 on success, non-zero value otherwise
1135  */
1137  u8 twice_nat);
1138 
1139 /**
1140  * @brief Delete external address from NAT44 pool
1141  *
1142  * @param addr IPv4 address
1143  * @param delete_sm 1 if delete static mapping using address
1144  * @param twice_nat 1 if twice NAT address
1145  *
1146  * @return 0 on success, non-zero value otherwise
1147  */
1148 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1149  u8 twice_nat);
1150 
1151 /**
1152  * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1153  *
1154  * @param addr IPv4 address
1155  * @param is_add 1 = add, 0 = delete
1156  *
1157  * @return 0 on success, non-zero value otherwise
1158  */
1160 
1161 /**
1162  * @brief Add/delete NAT44 static mapping
1163  *
1164  * @param l_addr local IPv4 address
1165  * @param e_addr external IPv4 address
1166  * @param l_port local port number
1167  * @param e_port external port number
1168  * @param vrf_id local VRF ID
1169  * @param addr_only 1 = 1:1NAT, 0 = 1:1NAPT
1170  * @param sw_if_index use interface address as external IPv4 address
1171  * @param proto L4 protocol
1172  * @param is_add 1 = add, 0 = delete
1173  * @param twice_nat twice-nat mode
1174  * @param out2in_only if 1 rule match only out2in direction
1175  * @param tag opaque string tag
1176  * @param identity_nat identity NAT
1177  *
1178  * @return 0 on success, non-zero value otherwise
1179  */
1181  u16 l_port, u16 e_port, u32 vrf_id,
1182  int addr_only, u32 sw_if_index,
1183  snat_protocol_t proto, int is_add,
1184  twice_nat_type_t twice_nat, u8 out2in_only,
1185  u8 * tag, u8 identity_nat);
1186 
1187 /**
1188  * @brief Add/delete static mapping with load-balancing (multiple backends)
1189  *
1190  * @param e_addr external IPv4 address
1191  * @param e_port external port number
1192  * @param proto L4 protocol
1193  * @param locals list of local backends
1194  * @param is_add 1 = add, 0 = delete
1195  * @param twice_nat twice-nat mode
1196  * @param out2in_only if 1 rule match only out2in direction
1197  * @param tag opaque string tag
1198  * @param affinity 0 = disabled, otherwise client IP affinity sticky time
1199  *
1200  * @return 0 on success, non-zero value otherwise
1201  */
1203  snat_protocol_t proto,
1204  nat44_lb_addr_port_t * locals, u8 is_add,
1205  twice_nat_type_t twice_nat,
1206  u8 out2in_only, u8 * tag, u32 affinity);
1207 
1209  ip4_address_t l_addr, u16 l_port,
1210  snat_protocol_t proto, u32 vrf_id,
1211  u8 probability, u8 is_add);
1212 
1214 
1215 /**
1216  * @brief Set NAT plugin workers
1217  *
1218  * @param bitmap NAT workers bitmap
1219  *
1220  * @return 0 on success, non-zero value otherwise
1221  */
1222 int snat_set_workers (uword * bitmap);
1223 
1224 /**
1225  * @brief Enable/disable NAT44 feature on the interface
1226  *
1227  * @param sw_if_index software index of the interface
1228  * @param is_inside 1 = inside, 0 = outside
1229  * @param is_del 1 = delete, 0 = add
1230  *
1231  * @return 0 on success, non-zero value otherwise
1232  */
1233 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1234 
1235 /**
1236  * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1237  *
1238  * @param sw_if_index software index of the interface
1239  * @param is_inside 1 = inside, 0 = outside
1240  * @param is_del 1 = delete, 0 = add
1241  *
1242  * @return 0 on success, non-zero value otherwise
1243  */
1245  int is_del);
1246 
1247 /**
1248  * @brief Add/delete NAT44 pool address from specific interfce
1249  *
1250  * @param sw_if_index software index of the interface
1251  * @param is_del 1 = delete, 0 = add
1252  * @param twice_nat 1 = twice NAT address for extenal hosts
1253  *
1254  * @return 0 on success, non-zero value otherwise
1255  */
1256 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1257  u8 twice_nat);
1258 
1259 /**
1260  * @brief Delete NAT44 session
1261  *
1262  * @param addr IPv4 address
1263  * @param port L4 port number
1264  * @param proto L4 protocol
1265  * @param vrf_id VRF ID
1266  * @param is_in 1 = inside network address and port pair, 0 = outside
1267  *
1268  * @return 0 on success, non-zero value otherwise
1269  */
1271  snat_protocol_t proto, u32 vrf_id, int is_in);
1272 
1273 /**
1274  * @brief Delete NAT44 endpoint-dependent session
1275  *
1276  * @param addr IPv4 address
1277  * @param port L4 port number
1278  * @param proto L4 protocol
1279  * @param vrf_id VRF ID
1280  * @param is_in 1 = inside network address and port pair, 0 = outside
1281  *
1282  * @return 0 on success, non-zero value otherwise
1283  */
1285  ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1286  u32 vrf_id, int is_in);
1287 
1288 /**
1289  * @brief Free NAT44 session data (lookup keys, external addrres port)
1290  *
1291  * @param s NAT session
1292  * @param thread_index thread index
1293  * @param is_ha is HA event
1294  */
1295 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1296  u32 thread_index, u8 is_ha);
1297 
1298 /**
1299  * @brief Find or create NAT user
1300  *
1301  * @param addr IPv4 address
1302  * @param fib_index FIB table index
1303  * @param thread_index thread index
1304  *
1305  * @return NAT user data structure on success otherwise zero value
1306  */
1308  u32 fib_index, u32 thread_index);
1309 
1310 /**
1311  * @brief Allocate new NAT session or recycle last used
1312  *
1313  * @param u NAT user
1314  * @param thread_index thread index
1315  *
1316  * @return session data structure on success otherwise zero value
1317  */
1318 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1319  snat_user_t * u,
1320  u32 thread_index, f64 now);
1321 
1322 /**
1323  * @brief Allocate NAT endpoint-dependent session
1324  *
1325  * @param u NAT user
1326  * @param thread_index thread index
1327  *
1328  * @return session data structure on success otherwise zero value
1329  */
1330 snat_session_t *nat_ed_session_alloc (snat_main_t * sm, snat_user_t * u,
1331  u32 thread_index, f64 now);
1332 
1333 /**
1334  * @brief Set address and port assignment algorithm for MAP-E CE
1335  *
1336  * @param psid Port Set Identifier value
1337  * @param psid_offset number of offset bits
1338  * @param psid_length length of PSID
1339  */
1341  u16 psid_length);
1342 
1343 /**
1344  * @brief Set address and port assignment algorithm for port range
1345  *
1346  * @param start_port beginning of the port range
1347  * @param end_port end of the port range
1348  */
1350 
1351 /**
1352  * @brief Set address and port assignment algorithm to default/standard
1353  */
1355 
1356 /**
1357  * @brief Free outside address and port pair
1358  *
1359  * @param addresses vector of outside addresses
1360  * @param thread_index thread index
1361  * @param k address, port and protocol
1362  */
1363 void snat_free_outside_address_and_port (snat_address_t * addresses,
1364  u32 thread_index,
1365  snat_session_key_t * k);
1366 
1367 /**
1368  * @brief Alloc outside address and port
1369  *
1370  * @param addresses vector of outside addresses
1371  * @param fib_index FIB table index
1372  * @param thread_index thread index
1373  * @param k allocated address and port pair
1374  * @param port_per_thread number of ports per threead
1375  * @param snat_thread_index NAT thread index
1376  *
1377  * @return 0 on success, non-zero value otherwise
1378  */
1379 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1380  u32 fib_index,
1381  u32 thread_index,
1382  snat_session_key_t * k,
1383  u16 port_per_thread,
1384  u32 snat_thread_index);
1385 
1386 /**
1387  * @brief Match NAT44 static mapping.
1388  *
1389  * @param match address and port to match
1390  * @param mapping external/local address and port of the matched mapping
1391  * @param by_external if 0 match by local address otherwise match by external
1392  * address
1393  * @param is_addr_only 1 if matched mapping is address only
1394  * @param twice_nat matched mapping is twice NAT type
1395  * @param lb 1 if matched mapping is load-balanced
1396  * @param ext_host_addr external host address
1397  *
1398  * @returns 0 if match found otherwise 1.
1399  */
1401  snat_session_key_t match,
1402  snat_session_key_t * mapping,
1403  u8 by_external,
1404  u8 * is_addr_only,
1405  twice_nat_type_t * twice_nat,
1406  lb_nat_type_t * lb,
1407  ip4_address_t * ext_host_addr,
1408  u8 * is_identity_nat);
1409 
1410 /**
1411  * @brief Add/del NAT address to FIB.
1412  *
1413  * Add the external NAT address to the FIB as receive entries. This ensures
1414  * that VPP will reply to ARP for this address and we don't need to enable
1415  * proxy ARP on the outside interface.
1416  *
1417  * @param addr IPv4 address
1418  * @param plen address prefix length
1419  * @param sw_if_index software index of the outside interface
1420  * @param is_add 0 = delete, 1 = add.
1421  */
1423  u8 p_len, u32 sw_if_index, int is_add);
1424 
1425 /*
1426  * Why is this here? Because we don't need to touch this layer to
1427  * simply reply to an icmp. We need to change id to a unique
1428  * value to NAT an echo request/reply.
1429  */
1430 
1431 typedef struct
1432 {
1436 
1437 typedef struct
1438 {
1441 
1442 #endif /* __included_nat_h__ */
1443 
1444 /*
1445  * fd.io coding-style-patch-verification: ON
1446  *
1447  * Local Variables:
1448  * eval: (c-set-style "gnu")
1449  * End:
1450  */
ip4_address_t external_addr
Definition: nat.h:443
int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm, u8 twice_nat)
Delete external address from NAT44 pool.
Definition: nat.c:1646
u32 user_memory_size
Definition: nat.h:668
clib_error_t * snat_api_init(vlib_main_t *vm, snat_main_t *sm)
Definition: nat_api.c:3429
#define foreach_nat_addr_and_port_alloc_alg
Definition: nat.h:170
nat_outside_fib_t * outside_fibs
Definition: nat.h:598
vlib_node_registration_t nat_default_node
(constructor) VLIB_REGISTER_NODE (nat_default_node)
Definition: nat.c:4354
vlib_node_registration_t snat_hairpin_src_node
(constructor) VLIB_REGISTER_NODE (snat_hairpin_src_node)
enum fib_source_t_ fib_source_t
The different sources that can create a route.
nat_addr_and_port_alloc_alg_t
Definition: nat.h:175
u32 sessions_per_user_list_head_index
Definition: nat.h:349
u8 proto
Definition: acl_types.api:47
int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del)
Enable/disable NAT44 feature on the interface.
Definition: nat.c:1760
u16 ext_host_port
Definition: nat.h:123
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:526
u32 hairpin_dst_node_index
Definition: nat.h:645
u32 ed_hairpinning_node_index
Definition: nat.h:647
a
Definition: bitmap.h:538
u32 icmp_timeout
Definition: nat.h:677
u32 fq_in2out_output_index
Definition: nat.h:615
u16 start_port
Definition: nat.h:594
u32 icmp_match_in2out_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 TCP_FLAG_SYN
Definition: fa_node.h:13
u32 ed_out2in_node_index
Definition: nat.h:639
u64 as_u64
Definition: bihash_doc.h:63
snat_session_t * nat_session_alloc_or_recycle(snat_main_t *sm, snat_user_t *u, u32 thread_index, f64 now)
Allocate new NAT session or recycle last used.
Definition: nat.c:375
u32 nsessions
Definition: nat.h:350
#define foreach_snat_session_state
Definition: nat.h:198
unsigned long u64
Definition: types.h:89
ip4_address_t addr
Definition: nat.h:407
int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del, u8 twice_nat)
Add/delete NAT44 pool address from specific interfce.
Definition: nat.c:4160
enum nat_log_level_t_ nat_log_level_t
snat_protocol_t proto
Definition: nat.h:454
u16 port_per_thread
Definition: nat.h:564
u32 nstaticsessions
Definition: nat.h:351
u32 ed_out2in_slowpath_node_index
Definition: nat.h:640
u32 icmp_match_out2in_slow(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...
Definition: out2in.c:328
vlib_node_registration_t nat44_ed_in2out_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_in2out_node)
Definition: in2out_ed.c:1727
format_function_t format_det_map_ses
Definition: nat.h:753
#define foreach_nat_in2out_ed_error
Definition: nat.h:217
vlib_node_registration_t snat_det_out2in_node
(constructor) VLIB_REGISTER_NODE (snat_det_out2in_node)
int nat44_i2o_ed_is_idle_session_cb(clib_bihash_kv_16_8_t *kv, void *arg)
Definition: in2out_ed.c:69
vlib_node_registration_t nat44_ed_hairpin_src_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_hairpin_src_node)
u32 fib_index
Definition: nat.h:348
format_function_t format_snat_static_mapping
Definition: nat.h:750
format_function_t format_snat_key
Definition: nat.h:754
snat_det_map_t * det_maps
Definition: nat.h:653
nat_log_level_t_
Definition: nat.h:876
nat_alloc_out_addr_and_port_function_t * alloc_addr_and_port
Definition: nat.h:586
u32 num_snat_thread
Definition: nat.h:565
#define foreach_snat_protocol
Definition: nat.h:184
dlist_elt_t * list_pool
Definition: nat.h:512
u8 in_plen
Definition: nat.h:390
struct _tcp_header tcp_header_t
int snat_hairpinning(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)
vhost_vring_addr_t addr
Definition: vhost_user.h:147
unsigned char u8
Definition: types.h:56
u8 deterministic
Definition: nat.h:661
u32 icmp_match_out2in_ed(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)
Definition: out2in_ed.c:441
u32 handoff_in2out_index
Definition: nat.h:623
u16 l_port
Definition: nat.h:108
nat44_lb_addr_port_t * locals
Definition: nat.h:462
int snat_set_workers(uword *bitmap)
Set NAT plugin workers.
Definition: nat.c:2223
u32 user_buckets
Definition: nat.h:667
double f64
Definition: types.h:142
clib_bihash_8_8_t user_hash
Definition: nat.h:503
u32 cached_sw_if_index
Definition: nat.h:713
u32 next_index
Definition: nat.h:68
void nat_set_alloc_addr_and_port_mape(u16 psid, u16 psid_offset, u16 psid_length)
Set address and port assignment algorithm for MAP-E CE.
Definition: nat.c:4315
u8 *() format_function_t(u8 *s, va_list *args)
Definition: format.h:48
u32 max_translations_per_user
Definition: nat.h:669
clib_bihash_8_8_t in2out
Definition: nat.h:496
Definition: nat.h:63
u32 in2out_output_node_index
Definition: nat.h:631
u32 ed_in2out_node_index
Definition: nat.h:635
u32 vlib_log_class_t
Definition: vlib.h:51
snat_get_worker_out2in_function_t * worker_out2in_cb
Definition: nat.h:563
vl_api_interface_index_t sw_if_index
Definition: gre.api:59
u16 r_port
Definition: nat.h:109
vlib_node_registration_t snat_out2in_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_node)
Definition: out2in.c:1384
ip4_address_t ext_host_addr
Definition: nat.h:122
u32 det_in2out_node_index
Definition: nat.h:641
#define TCP_FLAG_ACK
Definition: fa_node.h:16
fib_source_t nat_fib_src_hi
Definition: nat.c:41
u32 translation_buckets
Definition: nat.h:664
lb_nat_type_t
Definition: nat.h:428
ip4_address_t addr
Definition: nat.h:347
u8 probability
Definition: nat.api:820
int snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id, u8 twice_nat)
Add external address to NAT44 pool.
Definition: nat.c:558
ip4_main_t * ip4_main
Definition: nat.h:700
unsigned int u32
Definition: types.h:88
A collection of simple counters.
Definition: counter.h:57
vlib_node_registration_t snat_in2out_output_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_output_node)
Definition: in2out.c:1676
ip4_address_t local_addr
Definition: nat.h:441
vlib_node_registration_t snat_in2out_output_worker_handoff_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_output_worker_handoff_node)
snat_protocol_t proto
Definition: nat.h:482
fib_source_t nat_fib_src_low
Definition: nat.c:42
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...
u32() snat_get_worker_out2in_function_t(vlib_buffer_t *b, ip4_header_t *ip, u32 rx_fib_index, u8 is_output)
Definition: nat.h:538
vlib_node_registration_t snat_out2in_fast_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_fast_node)
Definition: out2in.c:1584
twice_nat_type_t twice_nat
Definition: nat.h:449
u32 max_translations
Definition: nat.h:666
u32 * auto_add_sw_if_indices_twice_nat
Definition: nat.h:605
vlib_node_registration_t nat44_ed_out2in_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_out2in_node)
Definition: out2in_ed.c:1644
vlib_node_registration_t snat_hairpin_dst_node
(constructor) VLIB_REGISTER_NODE (snat_hairpin_dst_node)
u32 fib_index
Definition: nat.h:370
u16 mss_value_net
Definition: nat.h:683
u32 handoff_in2out_output_index
Definition: nat.h:624
nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg
Definition: nat.h:588
#define foreach_nat_out2in_ed_error
Definition: nat.h:243
clib_bihash_16_8_t out2in_ed
Definition: nat.h:499
snat_det_session_t * sessions
Definition: nat.h:401
u16 mss_clamping
Definition: nat.h:682
vlib_main_t * vlib_main
Definition: nat.h:698
unsigned short u16
Definition: types.h:57
u32 pre_in2out_node_index
Definition: nat.h:628
u16 protocol
Definition: nat.h:92
u8 out2in_dpo
Definition: nat.h:662
snat_static_mapping_t * static_mappings
Definition: nat.h:577
u32 inside_fib_index
Definition: nat.h:673
u32 udp_timeout
Definition: nat.h:676
u8 static_mapping_only
Definition: nat.h:659
int nat44_del_ed_session(snat_main_t *sm, ip4_address_t *addr, u16 port, ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 vrf_id, int is_in)
Delete NAT44 endpoint-dependent session.
Definition: nat.c:4271
vlib_node_registration_t nat44_ed_out2in_worker_handoff_node
nat_next_t
Definition: nat.h:52
void nat_free_session_data(snat_main_t *sm, snat_session_t *s, u32 thread_index, u8 is_ha)
Free NAT44 session data (lookup keys, external addrres port)
Definition: nat.c:194
void nat_set_alloc_addr_and_port_default(void)
Set address and port assignment algorithm to default/standard.
Definition: nat.c:4338
int nat44_lb_static_mapping_add_del_local(ip4_address_t e_addr, u16 e_port, ip4_address_t l_addr, u16 l_port, snat_protocol_t proto, u32 vrf_id, u8 probability, u8 is_add)
Definition: nat.c:1462
#define always_inline
Definition: ipsec.h:28
void snat_add_del_addr_to_fib(ip4_address_t *addr, u8 p_len, u32 sw_if_index, int is_add)
Add/del NAT address to FIB.
Definition: nat.c:530
u32 hairpin_src_node_index
Definition: nat.h:646
clib_bihash_8_8_t static_mapping_by_external
Definition: nat.h:574
u32 in2out_slowpath_output_node_index
Definition: nat.h:634
u8 psid_offset
Definition: nat.h:590
struct snat_main_s snat_main_t
uword() unformat_function_t(unformat_input_t *input, va_list *args)
Definition: format.h:233
u32 handoff_out2in_index
Definition: nat.h:622
api_main_t * api_main
Definition: nat.h:702
vlib_main_t * vm
Definition: in2out_ed.c:1810
u8 out_plen
Definition: nat.h:393
u8 psid_length
Definition: nat.h:591
u32 refcount
Definition: nat.h:371
vnet_main_t * vnet_main
Definition: nat.h:699
u32 inside_vrf_id
Definition: nat.h:672
vlib_node_registration_t nat44_ed_in2out_worker_handoff_node
Definition: nat.h:433
u32 snat_icmp_hairpinning(snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0, icmp46_header_t *icmp0, int is_ed)
u8 log_level
Definition: nat.h:695
u32 fq_out2in_index
Definition: nat.h:616
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:225
int nat44_o2i_is_idle_session_cb(clib_bihash_kv_8_8_t *kv, void *arg)
Definition: out2in.c:115
snat_interface_t * output_feature_interfaces
Definition: nat.h:581
u16 src_port
Definition: nat.h:1439
u32 pre_out2in_node_index
Definition: nat.h:627
u32 ed_hairpin_src_node_index
Definition: nat.h:649
int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr, u16 l_port, u16 e_port, u32 vrf_id, int addr_only, u32 sw_if_index, snat_protocol_t proto, int is_add, twice_nat_type_t twice_nat, u8 out2in_only, u8 *tag, u8 identity_nat)
Add/delete NAT44 static mapping.
Definition: nat.c:694
The fine-grained event logger allows lightweight, thread-safe event logging at minimum cost...
snat_main_t snat_main
Definition: nat.c:39
vlib_node_registration_t snat_det_in2out_node
(constructor) VLIB_REGISTER_NODE (snat_det_in2out_node)
snat_session_state_t
Definition: nat.h:210
snat_user_t * users
Definition: nat.h:506
u32 random_seed
Definition: nat.h:611
u32 flags
Definition: vhost_user.h:141
clib_bihash_8_8_t out2in
Definition: nat.h:495
int nat44_o2i_ed_is_idle_session_cb(clib_bihash_kv_16_8_t *kv, void *arg)
Definition: out2in_ed.c:91
u32 outside_vrf_id
Definition: nat.h:670
ip4_address_t l_addr
Definition: nat.h:105
u8 static_mapping_connection_tracking
Definition: nat.h:660
u16 end_port
Definition: nat.h:595
format_function_t format_snat_static_map_to_resolve
Definition: nat.h:751
u32 ed_in2out_slowpath_node_index
Definition: nat.h:636
u32 error_node_index
Definition: nat.h:619
u32 sharing_ratio
Definition: nat.h:395
ip4_address_t out_addr
Definition: nat.h:392
u16 psid
Definition: nat.h:592
u32 outside_fib_index
Definition: nat.h:671
format_function_t format_nat_addr_and_port_alloc_alg
Definition: nat.h:757
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:2604
8 octet key, 8 octet key value pair
Definition: bihash_8_8.h:33
ip4_address_t addr
Definition: nat.h:90
u32 ed_hairpin_dst_node_index
Definition: nat.h:648
u32 sw_if_index
Definition: nat.h:471
#define foreach_nat_config_flag
Definition: nat.h:152
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:328
u32() snat_get_worker_in2out_function_t(ip4_header_t *ip, u32 rx_fib_index, u8 is_output)
Definition: nat.h:534
void increment_v4_address(ip4_address_t *a)
Increment IPv4 address.
Definition: nat.c:642
nat_out2in_ed_error_t
Definition: nat.h:262
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1810
u32 tcp_transitory_timeout
Definition: nat.h:678
ip4_address_t r_addr
Definition: nat.h:106
u32 * auto_add_sw_if_indices
Definition: nat.h:604
u32 in2out_fast_node_index
Definition: nat.h:632
void nat44_add_del_address_dpo(ip4_address_t addr, u8 is_add)
Add/delete external address to FIB DPO (out2in DPO mode)
Definition: nat.c:2962
u32 icmp_match_in2out_slow(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...
Definition: in2out.c:459
u32 num_workers
Definition: nat.h:559
Definition: nat.h:423
unformat_function_t unformat_snat_protocol
Definition: nat.h:759
u32 first_worker_index
Definition: nat.h:560
u32 det_out2in_node_index
Definition: nat.h:642
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:2530
ip4_address_t l_addr
Definition: nat.h:477
u32 in2out_slowpath_node_index
Definition: nat.h:633
snat_icmp_match_function_t * icmp_match_out2in_cb
Definition: nat.h:556
IPv4 main type.
Definition: ip4.h:105
vlib_log_class_t log_class
Definition: nat.h:693
u64 as_u64
Definition: nat.h:140
vlib_node_registration_t snat_out2in_worker_handoff_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_worker_handoff_node)
ip4_address_t addr
Definition: nat.h:137
ip4_address_t in_addr
Definition: nat.h:389
u32 fq_in2out_index
Definition: nat.h:614
u32 out2in_node_index
Definition: nat.h:637
format_function_t format_snat_protocol
Definition: nat.h:756
ip4_address_t addr
Definition: nat.h:356
snat_address_t * twice_nat_addresses
Definition: nat.h:601
u32 icmp_match_out2in_fast(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.
Definition: out2in.c:462
format_function_t format_snat_session
Definition: nat.h:752
struct _vlib_node_registration vlib_node_registration_t
Definition: nat.h:431
vlib_node_registration_t nat44_ed_hairpin_dst_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_hairpin_dst_node)
vlib_simple_counter_main_t total_users
Definition: nat.h:686
void nat_hairpinning_sm_unknown_proto(snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip)
vl_api_address_t ip
Definition: l2.api:490
format_function_t format_static_mapping_key
Definition: nat.h:755
twice_nat_type_t
Definition: nat.h:418
u32 hairpinning_node_index
Definition: nat.h:644
u16 msg_id_base
Definition: nat.h:690
vlib_node_registration_t nat_pre_in2out_node
(constructor) VLIB_REGISTER_NODE (nat_pre_in2out_node)
Definition: in2out_ed.c:1817
u16 ports_per_host
Definition: nat.h:397
VLIB buffer representation.
Definition: buffer.h:102
u32 * workers
Definition: nat.h:561
u64 uword
Definition: types.h:112
snat_main_per_thread_data_t * per_thread_data
Definition: nat.h:568
int nat44_i2o_is_idle_session_cb(clib_bihash_kv_8_8_t *kv, void *arg)
Definition: in2out.c:195
u32 icmp_match_in2out_ed(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)
Definition: in2out_ed.c:513
snat_protocol_t
Definition: nat.h:189
snat_det_out_key_t out
Definition: nat.h:379
u32 affinity_per_service_list_head_index
Definition: nat.h:464
u32 fib_index
Definition: nat.h:138
snat_address_t * addresses
Definition: nat.h:584
u16 port
Definition: lb_types.api:72
u32 out2in_fast_node_index
Definition: nat.h:638
void nat_set_alloc_addr_and_port_range(u16 start_port, u16 end_port)
Set address and port assignment algorithm for port range.
Definition: nat.c:4327
u32 in2out_node_index
Definition: nat.h:630
format_function_t format_snat_user
Definition: nat.h:749
snat_get_worker_in2out_function_t * worker_in2out_cb
Definition: nat.h:562
snat_static_map_resolve_t * to_resolve
Definition: nat.h:608
typedef CLIB_PACKED(struct { snat_session_key_t out2in;snat_session_key_t in2out;u32 flags;u32 per_user_index;u32 per_user_list_head_index;f64 last_heard;f64 ha_last_refreshed;u64 total_bytes;u32 total_pkts;ip4_address_t ext_host_addr;u16 ext_host_port;ip4_address_t ext_host_nat_addr;u16 ext_host_nat_port;u8 state;u32 i2o_fin_seq;u32 o2i_fin_seq;u32 user_index;}) snat_session_t
void nat44_ed_hairpinning_unknown_proto(snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip)
int nat44_add_del_lb_static_mapping(ip4_address_t e_addr, u16 e_port, snat_protocol_t proto, nat44_lb_addr_port_t *locals, u8 is_add, twice_nat_type_t twice_nat, u8 out2in_only, u8 *tag, u32 affinity)
Add/delete static mapping with load-balancing (multiple backends)
Definition: nat.c:1185
vl_api_dhcp_client_state_t state
Definition: dhcp.api:201
vlib_node_registration_t snat_in2out_worker_handoff_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node)
vlib_node_registration_t snat_in2out_fast_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_fast_node)
Definition: in2out.c:1950
nat_config_flags_t_
Definition: nat.h:162
u8 forwarding_enabled
Definition: nat.h:656
u32 translation_memory_size
Definition: nat.h:665
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:2763
int snat_interface_add_del_output_feature(u32 sw_if_index, u8 is_inside, int is_del)
Enable/disable NAT44 output feature on the interface (postrouting NAT)
Definition: nat.c:2025
u32 ses_num
Definition: nat.h:399
u32 icmp_in2out(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: in2out.c:655
enum nat_config_flags_t_ nat_config_flags_t
vlib_node_registration_t nat44_ed_in2out_output_worker_handoff_node
clib_bihash_16_8_t in2out_ed
Definition: nat.h:500
u32 vrf_id
Definition: nat.api:821
u8 endpoint_dependent
Definition: nat.h:663
u16 dst_port
Definition: udp.api:42
vlib_node_registration_t snat_in2out_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_node)
Definition: in2out.c:1644
vlib_node_registration_t nat44_ed_in2out_output_node
(constructor) VLIB_REGISTER_NODE (nat44_ed_in2out_output_node)
Definition: in2out_ed.c:1747
vlib_simple_counter_main_t total_sessions
Definition: nat.h:687
ip_lookup_main_t * ip4_lookup_main
Definition: nat.h:701
u32 icmp_match_in2out_fast(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.
Definition: in2out.c:582
snat_session_t * sessions
Definition: nat.h:509
u32 cached_ip4_address
Definition: nat.h:714
snat_icmp_match_function_t * icmp_match_in2out_cb
Definition: nat.h:555
#define foreach_nat_log_level
Definition: nat.h:868
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:450
u32() snat_icmp_match_function_t(struct snat_main_s *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)
Definition: nat.h:524
clib_bihash_8_8_t static_mapping_by_local
Definition: nat.h:571
int nat44_del_session(snat_main_t *sm, ip4_address_t *addr, u16 port, snat_protocol_t proto, u32 vrf_id, int is_in)
Delete NAT44 session.
Definition: nat.c:4228
u32 fib_index
Definition: nat.h:357
nat_in2out_ed_error_t
Definition: nat.h:235
void nat44_reass_hairpinning(snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0, u16 sport, u16 dport, u32 proto0, int is_ed)
snat_interface_t * interfaces
Definition: nat.h:580
vlib_node_registration_t nat_pre_out2in_node
(constructor) VLIB_REGISTER_NODE (nat_pre_out2in_node)
Definition: out2in_ed.c:1694
u32 tcp_established_timeout
Definition: nat.h:679
static bool tcp_flags_is_init(u8 f)
Check if client initiating TCP connection (received SYN from client)
Definition: nat.h:850
int() nat_alloc_out_addr_and_port_function_t(snat_address_t *addresses, u32 fib_index, u32 thread_index, snat_session_key_t *k, u16 port_per_thread, u32 snat_thread_index)
Definition: nat.h:544