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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vnet/pg/pg.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/fib/ip4_fib.h>
23 #include <snat/snat.h>
25 #include <vppinfra/hash.h>
26 #include <vppinfra/error.h>
27 #include <vppinfra/elog.h>
34 } snat_in2out_trace_t;
36 /* packet trace format function */
37 static u8 * format_snat_in2out_trace (u8 * s, va_list * args)
39 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
40 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
41 snat_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *);
44 tag = t->is_slow_path ? "SNAT_IN2OUT_SLOW_PATH" : "SNAT_IN2OUT_FAST_PATH";
46 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
47 t->sw_if_index, t->next_index, t->session_index);
52 static u8 * format_snat_in2out_fast_trace (u8 * s, va_list * args)
54 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
55 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
56 snat_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *);
58 s = format (s, "SANT_IN2OUT_FAST: sw_if_index %d, next index %d",
59 t->sw_if_index, t->next_index);
64 vlib_node_registration_t snat_in2out_node;
65 vlib_node_registration_t snat_in2out_slowpath_node;
66 vlib_node_registration_t snat_in2out_fast_node;
68 #define foreach_snat_in2out_error \
69 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
70 _(IN2OUT_PACKETS, "Good in2out packets processed") \
71 _(OUT_OF_PORTS, "Out of ports") \
72 _(BAD_OUTSIDE_FIB, "Outside VRF ID not found") \
73 _(BAD_ICMP_TYPE, "icmp type not echo-request") \
74 _(NO_TRANSLATION, "No translation")
77 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
78 foreach_snat_in2out_error
81 } snat_in2out_error_t;
83 static char * snat_in2out_error_strings[] = {
84 #define _(sym,string) string,
85 foreach_snat_in2out_error
90 SNAT_IN2OUT_NEXT_LOOKUP,
91 SNAT_IN2OUT_NEXT_DROP,
92 SNAT_IN2OUT_NEXT_SLOW_PATH,
97 static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
100 snat_session_key_t * key0,
101 snat_session_t ** sessionp,
102 vlib_node_runtime_t * node,
106 snat_user_key_t user_key;
108 clib_bihash_kv_8_8_t kv0, value0;
109 u32 oldest_per_user_translation_list_index;
110 dlist_elt_t * oldest_per_user_translation_list_elt;
111 dlist_elt_t * per_user_translation_list_elt;
112 dlist_elt_t * per_user_list_head_elt;
114 snat_session_key_t key1;
115 u32 address_index = ~0;
116 u32 outside_fib_index;
119 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id);
122 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_OUTSIDE_FIB];
123 return SNAT_IN2OUT_NEXT_DROP;
125 outside_fib_index = p[0];
127 user_key.addr = ip0->src_address;
128 user_key.fib_index = rx_fib_index0;
129 kv0.key = user_key.as_u64;
131 /* Ever heard of the "user" = src ip4 address before? */
132 if (clib_bihash_search_8_8 (&sm->user_hash, &kv0, &value0))
134 /* no, make a new one */
135 pool_get (sm->users, u);
136 memset (u, 0, sizeof (*u));
137 u->addr = ip0->src_address;
139 pool_get (sm->list_pool, per_user_list_head_elt);
141 u->sessions_per_user_list_head_index = per_user_list_head_elt -
144 clib_dlist_init (sm->list_pool, u->sessions_per_user_list_head_index);
146 kv0.value = u - sm->users;
149 clib_bihash_add_del_8_8 (&sm->user_hash, &kv0, 1 /* is_add */);
153 u = pool_elt_at_index (sm->users, value0.value);
156 /* Over quota? Recycle the least recently used dynamic translation */
157 if (u->nsessions >= sm->max_translations_per_user)
159 /* Remove the oldest dynamic translation */
161 oldest_per_user_translation_list_index =
162 clib_dlist_remove_head
163 (sm->list_pool, u->sessions_per_user_list_head_index);
165 ASSERT (oldest_per_user_translation_list_index != ~0);
167 /* add it back to the end of the LRU list */
168 clib_dlist_addtail (sm->list_pool,
169 u->sessions_per_user_list_head_index,
170 oldest_per_user_translation_list_index);
171 /* Get the list element */
172 oldest_per_user_translation_list_elt =
173 pool_elt_at_index (sm->list_pool,
174 oldest_per_user_translation_list_index);
176 /* Get the session index from the list element */
177 session_index = oldest_per_user_translation_list_elt->value;
179 /* Get the session */
180 s = pool_elt_at_index (sm->sessions, session_index);
181 } while (!snat_is_session_static (s));
183 /* Remove in2out, out2in keys */
184 kv0.key = s->in2out.as_u64;
185 if (clib_bihash_add_del_8_8 (&sm->in2out, &kv0, 0 /* is_add */))
186 clib_warning ("in2out key delete failed");
187 kv0.key = s->out2in.as_u64;
188 if (clib_bihash_add_del_8_8 (&sm->out2in, &kv0, 0 /* is_add */))
189 clib_warning ("out2in key delete failed");
191 snat_free_outside_address_and_port
192 (sm, &s->out2in, s->outside_address_index);
193 s->outside_address_index = ~0;
195 if (snat_alloc_outside_address_and_port (sm, &key1, &address_index))
199 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
200 return SNAT_IN2OUT_NEXT_DROP;
202 s->outside_address_index = address_index;
206 u8 static_mapping = 1;
208 /* First try to match static mapping by local address and port */
209 if (snat_static_mapping_match (sm, *key0, &key1, 0))
212 /* Try to create dynamic translation */
213 if (snat_alloc_outside_address_and_port (sm, &key1, &address_index))
215 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
216 return SNAT_IN2OUT_NEXT_DROP;
220 /* Create a new session */
221 pool_get (sm->sessions, s);
222 memset (s, 0, sizeof (*s));
224 s->outside_address_index = address_index;
228 u->nstaticsessions++;
229 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
236 /* Create list elts */
237 pool_get (sm->list_pool, per_user_translation_list_elt);
238 clib_dlist_init (sm->list_pool, per_user_translation_list_elt -
241 per_user_translation_list_elt->value = s - sm->sessions;
242 s->per_user_index = per_user_translation_list_elt - sm->list_pool;
243 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
245 clib_dlist_addtail (sm->list_pool, s->per_user_list_head_index,
246 per_user_translation_list_elt - sm->list_pool);
251 s->out2in.protocol = key0->protocol;
252 s->out2in.fib_index = outside_fib_index;
255 /* Add to translation hashes */
256 kv0.key = s->in2out.as_u64;
257 kv0.value = s - sm->sessions;
258 if (clib_bihash_add_del_8_8 (&sm->in2out, &kv0, 1 /* is_add */))
259 clib_warning ("in2out key add failed");
261 kv0.key = s->out2in.as_u64;
262 kv0.value = s - sm->sessions;
264 if (clib_bihash_add_del_8_8 (&sm->out2in, &kv0, 1 /* is_add */))
265 clib_warning ("out2in key add failed");
270 static inline u32 icmp_in2out_slow_path (snat_main_t *sm,
273 icmp46_header_t * icmp0,
276 vlib_node_runtime_t * node,
280 snat_session_key_t key0;
281 icmp_echo_header_t *echo0;
282 clib_bihash_kv_8_8_t kv0, value0;
284 u32 new_addr0, old_addr0;
285 u16 old_id0, new_id0;
287 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
289 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request))
291 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
292 return SNAT_IN2OUT_NEXT_DROP;
295 echo0 = (icmp_echo_header_t *)(icmp0+1);
297 key0.addr = ip0->src_address;
298 key0.port = echo0->identifier;
299 key0.protocol = SNAT_PROTOCOL_ICMP;
300 key0.fib_index = rx_fib_index0;
302 kv0.key = key0.as_u64;
304 if (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0))
306 ip4_address_t * first_int_addr;
308 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
311 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
312 0 /* just want the address */);
313 rt->cached_sw_if_index = sw_if_index0;
314 rt->cached_ip4_address = first_int_addr->as_u32;
317 /* Don't NAT packet aimed at the intfc address */
318 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
319 rt->cached_ip4_address))
322 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
325 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
329 s0 = pool_elt_at_index (sm->sessions, value0.value);
331 old_addr0 = ip0->src_address.as_u32;
332 ip0->src_address = s0->out2in.addr;
333 new_addr0 = ip0->src_address.as_u32;
334 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
336 sum0 = ip0->checksum;
337 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
339 src_address /* changed member */);
340 ip0->checksum = ip_csum_fold (sum0);
342 old_id0 = echo0->identifier;
343 new_id0 = s0->out2in.port;
344 echo0->identifier = new_id0;
346 sum0 = icmp0->checksum;
347 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
349 icmp0->checksum = ip_csum_fold (sum0);
352 s0->last_heard = now;
354 s0->total_bytes += vlib_buffer_length_in_chain (sm->vlib_main, b0);
355 /* Per-user LRU list maintenance for dynamic translations */
356 if (!snat_is_session_static (s0))
358 clib_dlist_remove (sm->list_pool, s0->per_user_index);
359 clib_dlist_addtail (sm->list_pool, s0->per_user_list_head_index,
367 snat_in2out_node_fn_inline (vlib_main_t * vm,
368 vlib_node_runtime_t * node,
369 vlib_frame_t * frame, int is_slow_path)
371 u32 n_left_from, * from, * to_next;
372 snat_in2out_next_t next_index;
373 u32 pkts_processed = 0;
374 snat_main_t * sm = &snat_main;
375 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
376 f64 now = vlib_time_now (vm);
377 u32 stats_node_index;
379 stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
380 snat_in2out_node.index;
382 from = vlib_frame_vector_args (frame);
383 n_left_from = frame->n_vectors;
384 next_index = node->cached_next_index;
386 while (n_left_from > 0)
390 vlib_get_next_frame (vm, node, next_index,
391 to_next, n_left_to_next);
393 while (n_left_from >= 4 && n_left_to_next >= 2)
396 vlib_buffer_t * b0, * b1;
398 u32 sw_if_index0, sw_if_index1;
399 ip4_header_t * ip0, * ip1;
400 ip_csum_t sum0, sum1;
401 u32 new_addr0, old_addr0, new_addr1, old_addr1;
402 u16 old_port0, new_port0, old_port1, new_port1;
403 udp_header_t * udp0, * udp1;
404 tcp_header_t * tcp0, * tcp1;
405 icmp46_header_t * icmp0, * icmp1;
406 snat_session_key_t key0, key1;
407 u32 rx_fib_index0, rx_fib_index1;
409 snat_session_t * s0 = 0, * s1 = 0;
410 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
412 /* Prefetch next iteration. */
414 vlib_buffer_t * p2, * p3;
416 p2 = vlib_get_buffer (vm, from[2]);
417 p3 = vlib_get_buffer (vm, from[3]);
419 vlib_prefetch_buffer_header (p2, LOAD);
420 vlib_prefetch_buffer_header (p3, LOAD);
422 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
423 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
426 /* speculatively enqueue b0 and b1 to the current next frame */
427 to_next[0] = bi0 = from[0];
428 to_next[1] = bi1 = from[1];
434 b0 = vlib_get_buffer (vm, bi0);
435 b1 = vlib_get_buffer (vm, bi1);
437 ip0 = vlib_buffer_get_current (b0);
438 udp0 = ip4_next_header (ip0);
439 tcp0 = (tcp_header_t *) udp0;
440 icmp0 = (icmp46_header_t *) udp0;
442 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
443 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
446 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
449 /* Formally correct, but we send to slowpath, lookup or drop */
450 vnet_get_config_data (&cm->config_main,
451 &b0->current_config_index,
453 0 /* sizeof config data */);
457 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
458 ? SNAT_PROTOCOL_UDP : proto0;
459 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
460 ? SNAT_PROTOCOL_TCP : proto0;
461 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
462 ? SNAT_PROTOCOL_ICMP : proto0;
464 /* Next configured feature, probably ip4-lookup */
467 if (PREDICT_FALSE (proto0 == ~0))
470 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
472 next0 = icmp_in2out_slow_path
473 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
480 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
482 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
487 key0.addr = ip0->src_address;
488 key0.port = udp0->src_port;
489 key0.protocol = proto0;
490 key0.fib_index = rx_fib_index0;
492 kv0.key = key0.as_u64;
494 if (PREDICT_FALSE (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0) != 0))
498 ip4_address_t * first_int_addr;
500 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
503 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
504 0 /* just want the address */);
505 rt->cached_sw_if_index = sw_if_index0;
506 rt->cached_ip4_address = first_int_addr->as_u32;
509 /* Don't NAT packet aimed at the intfc address */
510 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
511 rt->cached_ip4_address))
514 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
516 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
521 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
526 s0 = pool_elt_at_index (sm->sessions, value0.value);
528 old_addr0 = ip0->src_address.as_u32;
529 ip0->src_address = s0->out2in.addr;
530 new_addr0 = ip0->src_address.as_u32;
531 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
533 sum0 = ip0->checksum;
534 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
536 src_address /* changed member */);
537 ip0->checksum = ip_csum_fold (sum0);
539 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
541 old_port0 = tcp0->ports.src;
542 tcp0->ports.src = s0->out2in.port;
543 new_port0 = tcp0->ports.src;
545 sum0 = tcp0->checksum;
546 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
548 dst_address /* changed member */);
549 sum0 = ip_csum_update (sum0, old_port0, new_port0,
550 ip4_header_t /* cheat */,
551 length /* changed member */);
552 tcp0->checksum = ip_csum_fold(sum0);
556 old_port0 = udp0->src_port;
557 udp0->src_port = s0->out2in.port;
562 s0->last_heard = now;
564 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
565 /* Per-user LRU list maintenance for dynamic translation */
566 if (!snat_is_session_static (s0))
568 clib_dlist_remove (sm->list_pool, s0->per_user_index);
569 clib_dlist_addtail (sm->list_pool, s0->per_user_list_head_index,
574 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
575 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
577 snat_in2out_trace_t *t =
578 vlib_add_trace (vm, node, b0, sizeof (*t));
579 t->is_slow_path = is_slow_path;
580 t->sw_if_index = sw_if_index0;
581 t->next_index = next0;
582 t->session_index = ~0;
584 t->session_index = s0 - sm->sessions;
587 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
589 ip1 = vlib_buffer_get_current (b1);
590 udp1 = ip4_next_header (ip1);
591 tcp1 = (tcp_header_t *) udp1;
592 icmp1 = (icmp46_header_t *) udp1;
594 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
595 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
599 vnet_get_config_data (&cm->config_main,
600 &b1->current_config_index,
602 0 /* sizeof config data */);
606 proto1 = (ip1->protocol == IP_PROTOCOL_UDP)
607 ? SNAT_PROTOCOL_UDP : proto1;
608 proto1 = (ip1->protocol == IP_PROTOCOL_TCP)
609 ? SNAT_PROTOCOL_TCP : proto1;
610 proto1 = (ip1->protocol == IP_PROTOCOL_ICMP)
611 ? SNAT_PROTOCOL_ICMP : proto1;
613 /* Next configured feature, probably ip4-lookup */
616 if (PREDICT_FALSE (proto1 == ~0))
619 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
621 next1 = icmp_in2out_slow_path
622 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node, next1,
629 if (PREDICT_FALSE (proto1 == ~0 || proto1 == SNAT_PROTOCOL_ICMP))
631 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
636 key1.addr = ip1->src_address;
637 key1.port = udp1->src_port;
638 key1.protocol = proto1;
639 key1.fib_index = rx_fib_index1;
641 kv1.key = key1.as_u64;
643 if (PREDICT_FALSE(clib_bihash_search_8_8 (&sm->in2out, &kv1, &value1) != 0))
647 ip4_address_t * first_int_addr;
649 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index1))
652 ip4_interface_first_address (sm->ip4_main, sw_if_index1,
653 0 /* just want the address */);
654 rt->cached_sw_if_index = sw_if_index1;
655 rt->cached_ip4_address = first_int_addr->as_u32;
658 /* Don't NAT packet aimed at the intfc address */
659 if (PREDICT_FALSE(ip1->dst_address.as_u32 ==
660 rt->cached_ip4_address))
663 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
665 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
670 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
675 s1 = pool_elt_at_index (sm->sessions, value1.value);
677 old_addr1 = ip1->src_address.as_u32;
678 ip1->src_address = s1->out2in.addr;
679 new_addr1 = ip1->src_address.as_u32;
680 vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
682 sum1 = ip1->checksum;
683 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
685 src_address /* changed member */);
686 ip1->checksum = ip_csum_fold (sum1);
688 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
690 old_port1 = tcp1->ports.src;
691 tcp1->ports.src = s1->out2in.port;
692 new_port1 = tcp1->ports.src;
694 sum1 = tcp1->checksum;
695 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
697 dst_address /* changed member */);
698 sum1 = ip_csum_update (sum1, old_port1, new_port1,
699 ip4_header_t /* cheat */,
700 length /* changed member */);
701 tcp1->checksum = ip_csum_fold(sum1);
705 old_port1 = udp1->src_port;
706 udp1->src_port = s1->out2in.port;
711 s1->last_heard = now;
713 s1->total_bytes += vlib_buffer_length_in_chain (vm, b1);
714 /* Per-user LRU list maintenance for dynamic translation */
715 if (!snat_is_session_static (s1))
717 clib_dlist_remove (sm->list_pool, s1->per_user_index);
718 clib_dlist_addtail (sm->list_pool, s1->per_user_list_head_index,
723 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
724 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
726 snat_in2out_trace_t *t =
727 vlib_add_trace (vm, node, b1, sizeof (*t));
728 t->sw_if_index = sw_if_index1;
729 t->next_index = next1;
730 t->session_index = ~0;
732 t->session_index = s1 - sm->sessions;
735 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
737 /* verify speculative enqueues, maybe switch current next frame */
738 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
739 to_next, n_left_to_next,
740 bi0, bi1, next0, next1);
743 while (n_left_from > 0 && n_left_to_next > 0)
751 u32 new_addr0, old_addr0;
752 u16 old_port0, new_port0;
755 icmp46_header_t * icmp0;
756 snat_session_key_t key0;
759 snat_session_t * s0 = 0;
760 clib_bihash_kv_8_8_t kv0, value0;
762 /* speculatively enqueue b0 to the current next frame */
770 b0 = vlib_get_buffer (vm, bi0);
771 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
773 ip0 = vlib_buffer_get_current (b0);
774 udp0 = ip4_next_header (ip0);
775 tcp0 = (tcp_header_t *) udp0;
776 icmp0 = (icmp46_header_t *) udp0;
778 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
779 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
784 vnet_get_config_data (&cm->config_main,
785 &b0->current_config_index,
787 0 /* sizeof config data */);
791 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
792 ? SNAT_PROTOCOL_UDP : proto0;
793 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
794 ? SNAT_PROTOCOL_TCP : proto0;
795 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
796 ? SNAT_PROTOCOL_ICMP : proto0;
798 /* Next configured feature, probably ip4-lookup */
801 if (PREDICT_FALSE (proto0 == ~0))
804 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
806 next0 = icmp_in2out_slow_path
807 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node, next0,
814 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
816 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
821 key0.addr = ip0->src_address;
822 key0.port = udp0->src_port;
823 key0.protocol = proto0;
824 key0.fib_index = rx_fib_index0;
826 kv0.key = key0.as_u64;
828 if (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0))
832 ip4_address_t * first_int_addr;
834 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
837 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
838 0 /* just want the address */);
839 rt->cached_sw_if_index = sw_if_index0;
840 rt->cached_ip4_address = first_int_addr->as_u32;
843 /* Don't NAT packet aimed at the intfc address */
844 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
845 rt->cached_ip4_address))
848 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
850 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
855 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
860 s0 = pool_elt_at_index (sm->sessions, value0.value);
862 old_addr0 = ip0->src_address.as_u32;
863 ip0->src_address = s0->out2in.addr;
864 new_addr0 = ip0->src_address.as_u32;
865 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
867 sum0 = ip0->checksum;
868 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
870 src_address /* changed member */);
871 ip0->checksum = ip_csum_fold (sum0);
873 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
875 old_port0 = tcp0->ports.src;
876 tcp0->ports.src = s0->out2in.port;
877 new_port0 = tcp0->ports.src;
879 sum0 = tcp0->checksum;
880 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
882 dst_address /* changed member */);
883 sum0 = ip_csum_update (sum0, old_port0, new_port0,
884 ip4_header_t /* cheat */,
885 length /* changed member */);
886 tcp0->checksum = ip_csum_fold(sum0);
890 old_port0 = udp0->src_port;
891 udp0->src_port = s0->out2in.port;
896 s0->last_heard = now;
898 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
899 /* Per-user LRU list maintenance for dynamic translation */
900 if (!snat_is_session_static (s0))
902 clib_dlist_remove (sm->list_pool, s0->per_user_index);
903 clib_dlist_addtail (sm->list_pool, s0->per_user_list_head_index,
908 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
909 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
911 snat_in2out_trace_t *t =
912 vlib_add_trace (vm, node, b0, sizeof (*t));
913 t->is_slow_path = is_slow_path;
914 t->sw_if_index = sw_if_index0;
915 t->next_index = next0;
916 t->session_index = ~0;
918 t->session_index = s0 - sm->sessions;
921 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
923 /* verify speculative enqueue, maybe switch current next frame */
924 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
925 to_next, n_left_to_next,
929 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
932 vlib_node_increment_counter (vm, stats_node_index,
933 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
935 return frame->n_vectors;
939 snat_in2out_fast_path_fn (vlib_main_t * vm,
940 vlib_node_runtime_t * node,
941 vlib_frame_t * frame)
943 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */);
946 VLIB_REGISTER_NODE (snat_in2out_node) = {
947 .function = snat_in2out_fast_path_fn,
948 .name = "snat-in2out",
949 .vector_size = sizeof (u32),
950 .format_trace = format_snat_in2out_trace,
951 .type = VLIB_NODE_TYPE_INTERNAL,
953 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
954 .error_strings = snat_in2out_error_strings,
956 .runtime_data_bytes = sizeof (snat_runtime_t),
958 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
960 /* edit / add dispositions here */
962 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
963 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
964 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
968 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_node, snat_in2out_fast_path_fn);
971 snat_in2out_slow_path_fn (vlib_main_t * vm,
972 vlib_node_runtime_t * node,
973 vlib_frame_t * frame)
975 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */);
978 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
979 .function = snat_in2out_slow_path_fn,
980 .name = "snat-in2out-slowpath",
981 .vector_size = sizeof (u32),
982 .format_trace = format_snat_in2out_trace,
983 .type = VLIB_NODE_TYPE_INTERNAL,
985 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
986 .error_strings = snat_in2out_error_strings,
988 .runtime_data_bytes = sizeof (snat_runtime_t),
990 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
992 /* edit / add dispositions here */
994 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
995 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
996 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
1000 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_slowpath_node, snat_in2out_slow_path_fn);
1002 static inline u32 icmp_in2out_static_map (snat_main_t *sm,
1005 icmp46_header_t * icmp0,
1007 vlib_node_runtime_t * node,
1011 snat_session_key_t key0, sm0;
1012 icmp_echo_header_t *echo0;
1013 u32 new_addr0, old_addr0;
1014 u16 old_id0, new_id0;
1016 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
1018 echo0 = (icmp_echo_header_t *)(icmp0+1);
1020 key0.addr = ip0->src_address;
1021 key0.port = echo0->identifier;
1022 key0.fib_index = rx_fib_index0;
1024 if (snat_static_mapping_match(sm, key0, &sm0, 0))
1026 ip4_address_t * first_int_addr;
1028 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
1031 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
1032 0 /* just want the address */);
1033 rt->cached_sw_if_index = sw_if_index0;
1034 rt->cached_ip4_address = first_int_addr->as_u32;
1037 /* Don't NAT packet aimed at the intfc address */
1038 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
1039 rt->cached_ip4_address))
1042 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1043 return SNAT_IN2OUT_NEXT_DROP;
1046 new_addr0 = sm0.addr.as_u32;
1048 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1049 old_addr0 = ip0->src_address.as_u32;
1050 ip0->src_address.as_u32 = new_addr0;
1052 sum0 = ip0->checksum;
1053 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1055 src_address /* changed member */);
1056 ip0->checksum = ip_csum_fold (sum0);
1058 if (PREDICT_FALSE(new_id0 != echo0->identifier))
1060 old_id0 = echo0->identifier;
1061 echo0->identifier = new_id0;
1063 sum0 = icmp0->checksum;
1064 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
1066 icmp0->checksum = ip_csum_fold (sum0);
1073 snat_in2out_fast_static_map_fn (vlib_main_t * vm,
1074 vlib_node_runtime_t * node,
1075 vlib_frame_t * frame)
1077 u32 n_left_from, * from, * to_next;
1078 snat_in2out_next_t next_index;
1079 u32 pkts_processed = 0;
1080 snat_main_t * sm = &snat_main;
1081 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
1082 u32 stats_node_index;
1084 stats_node_index = snat_in2out_fast_node.index;
1086 from = vlib_frame_vector_args (frame);
1087 n_left_from = frame->n_vectors;
1088 next_index = node->cached_next_index;
1090 while (n_left_from > 0)
1094 vlib_get_next_frame (vm, node, next_index,
1095 to_next, n_left_to_next);
1097 while (n_left_from > 0 && n_left_to_next > 0)
1105 u32 new_addr0, old_addr0;
1106 u16 old_port0, new_port0;
1107 udp_header_t * udp0;
1108 tcp_header_t * tcp0;
1109 icmp46_header_t * icmp0;
1110 snat_session_key_t key0, sm0;
1114 /* speculatively enqueue b0 to the current next frame */
1120 n_left_to_next -= 1;
1122 b0 = vlib_get_buffer (vm, bi0);
1123 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1125 ip0 = vlib_buffer_get_current (b0);
1126 udp0 = ip4_next_header (ip0);
1127 tcp0 = (tcp_header_t *) udp0;
1128 icmp0 = (icmp46_header_t *) udp0;
1130 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1131 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
1134 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
1135 ? SNAT_PROTOCOL_UDP : proto0;
1136 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
1137 ? SNAT_PROTOCOL_TCP : proto0;
1138 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
1139 ? SNAT_PROTOCOL_ICMP : proto0;
1141 if (PREDICT_FALSE (proto0 == ~0))
1144 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1146 ip4_address_t * first_int_addr;
1148 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
1151 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
1152 0 /* just want the address */);
1153 rt->cached_sw_if_index = sw_if_index0;
1154 rt->cached_ip4_address = first_int_addr->as_u32;
1157 /* Don't NAT packet aimed at the intfc address */
1158 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
1159 rt->cached_ip4_address))
1162 next0 = icmp_in2out_static_map
1163 (sm, b0, ip0, icmp0, sw_if_index0, node, next0, rx_fib_index0);
1167 key0.addr = ip0->src_address;
1168 key0.port = udp0->src_port;
1169 key0.fib_index = rx_fib_index0;
1171 if (snat_static_mapping_match(sm, key0, &sm0, 0))
1173 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1174 next0= SNAT_IN2OUT_NEXT_DROP;
1178 new_addr0 = sm0.addr.as_u32;
1179 new_port0 = sm0.port;
1180 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1181 old_addr0 = ip0->src_address.as_u32;
1182 ip0->src_address.as_u32 = new_addr0;
1184 sum0 = ip0->checksum;
1185 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1187 src_address /* changed member */);
1188 ip0->checksum = ip_csum_fold (sum0);
1190 if (PREDICT_FALSE(new_port0 != udp0->dst_port))
1192 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1194 old_port0 = tcp0->ports.src;
1195 tcp0->ports.src = new_port0;
1197 sum0 = tcp0->checksum;
1198 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1200 dst_address /* changed member */);
1201 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1202 ip4_header_t /* cheat */,
1203 length /* changed member */);
1204 tcp0->checksum = ip_csum_fold(sum0);
1208 old_port0 = udp0->src_port;
1209 udp0->src_port = new_port0;
1215 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1217 sum0 = tcp0->checksum;
1218 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1220 dst_address /* changed member */);
1221 tcp0->checksum = ip_csum_fold(sum0);
1226 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1227 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1229 snat_in2out_trace_t *t =
1230 vlib_add_trace (vm, node, b0, sizeof (*t));
1231 t->sw_if_index = sw_if_index0;
1232 t->next_index = next0;
1235 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1237 /* verify speculative enqueue, maybe switch current next frame */
1238 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1239 to_next, n_left_to_next,
1243 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1246 vlib_node_increment_counter (vm, stats_node_index,
1247 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1249 return frame->n_vectors;
1253 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1254 .function = snat_in2out_fast_static_map_fn,
1255 .name = "snat-in2out-fast",
1256 .vector_size = sizeof (u32),
1257 .format_trace = format_snat_in2out_fast_trace,
1258 .type = VLIB_NODE_TYPE_INTERNAL,
1260 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1261 .error_strings = snat_in2out_error_strings,
1263 .runtime_data_bytes = sizeof (snat_runtime_t),
1265 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1267 /* edit / add dispositions here */
1269 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1270 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1271 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
1275 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_fast_node, snat_in2out_fast_static_map_fn);