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 <snat/snat.h>
24 #include <vppinfra/hash.h>
25 #include <vppinfra/error.h>
26 #include <vppinfra/elog.h>
33 } snat_in2out_trace_t;
35 /* packet trace format function */
36 static u8 * format_snat_in2out_trace (u8 * s, va_list * args)
38 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
39 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
40 snat_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *);
43 tag = t->is_slow_path ? "SNAT_IN2OUT_SLOW_PATH" : "SNAT_IN2OUT_FAST_PATH";
45 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
46 t->sw_if_index, t->next_index, t->session_index);
51 static u8 * format_snat_in2out_fast_trace (u8 * s, va_list * args)
53 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
54 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
55 snat_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *);
57 s = format (s, "SANT_IN2OUT_FAST: sw_if_index %d, next index %d",
58 t->sw_if_index, t->next_index);
63 vlib_node_registration_t snat_in2out_node;
64 vlib_node_registration_t snat_in2out_slowpath_node;
65 vlib_node_registration_t snat_in2out_fast_node;
67 #define foreach_snat_in2out_error \
68 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
69 _(IN2OUT_PACKETS, "Good in2out packets processed") \
70 _(OUT_OF_PORTS, "Out of ports") \
71 _(BAD_OUTSIDE_FIB, "Outside VRF ID not found") \
72 _(BAD_ICMP_TYPE, "icmp type not echo-request") \
73 _(NO_TRANSLATION, "No translation")
76 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
77 foreach_snat_in2out_error
80 } snat_in2out_error_t;
82 static char * snat_in2out_error_strings[] = {
83 #define _(sym,string) string,
84 foreach_snat_in2out_error
89 SNAT_IN2OUT_NEXT_LOOKUP,
90 SNAT_IN2OUT_NEXT_DROP,
91 SNAT_IN2OUT_NEXT_SLOW_PATH,
96 static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
99 snat_session_key_t * key0,
100 snat_session_t ** sessionp,
101 vlib_node_runtime_t * node,
105 snat_user_key_t user_key;
107 clib_bihash_kv_8_8_t kv0, value0;
108 u32 oldest_per_user_translation_list_index;
109 dlist_elt_t * oldest_per_user_translation_list_elt;
110 dlist_elt_t * per_user_translation_list_elt;
111 dlist_elt_t * per_user_list_head_elt;
113 snat_session_key_t key1;
114 u32 address_index = ~0;
115 u32 outside_fib_index;
118 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id);
121 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_OUTSIDE_FIB];
122 return SNAT_IN2OUT_NEXT_DROP;
124 outside_fib_index = p[0];
126 user_key.addr = ip0->src_address;
127 user_key.fib_index = rx_fib_index0;
128 kv0.key = user_key.as_u64;
130 /* Ever heard of the "user" = src ip4 address before? */
131 if (clib_bihash_search_8_8 (&sm->user_hash, &kv0, &value0))
133 /* no, make a new one */
134 pool_get (sm->users, u);
135 memset (u, 0, sizeof (*u));
136 u->addr = ip0->src_address;
138 pool_get (sm->list_pool, per_user_list_head_elt);
140 u->sessions_per_user_list_head_index = per_user_list_head_elt -
143 clib_dlist_init (sm->list_pool, u->sessions_per_user_list_head_index);
145 kv0.value = u - sm->users;
148 clib_bihash_add_del_8_8 (&sm->user_hash, &kv0, 1 /* is_add */);
152 u = pool_elt_at_index (sm->users, value0.value);
155 /* Over quota? Recycle the least recently used dynamic translation */
156 if (u->nsessions >= sm->max_translations_per_user)
158 /* Remove the oldest dynamic translation */
160 oldest_per_user_translation_list_index =
161 clib_dlist_remove_head
162 (sm->list_pool, u->sessions_per_user_list_head_index);
164 ASSERT (oldest_per_user_translation_list_index != ~0);
166 /* add it back to the end of the LRU list */
167 clib_dlist_addtail (sm->list_pool,
168 u->sessions_per_user_list_head_index,
169 oldest_per_user_translation_list_index);
170 /* Get the list element */
171 oldest_per_user_translation_list_elt =
172 pool_elt_at_index (sm->list_pool,
173 oldest_per_user_translation_list_index);
175 /* Get the session index from the list element */
176 session_index = oldest_per_user_translation_list_elt->value;
178 /* Get the session */
179 s = pool_elt_at_index (sm->sessions, session_index);
180 } while (!snat_is_session_static (s));
182 /* Remove in2out, out2in keys */
183 kv0.key = s->in2out.as_u64;
184 if (clib_bihash_add_del_8_8 (&sm->in2out, &kv0, 0 /* is_add */))
185 clib_warning ("in2out key delete failed");
186 kv0.key = s->out2in.as_u64;
187 if (clib_bihash_add_del_8_8 (&sm->out2in, &kv0, 0 /* is_add */))
188 clib_warning ("out2in key delete failed");
190 snat_free_outside_address_and_port
191 (sm, &s->out2in, s->outside_address_index);
192 s->outside_address_index = ~0;
194 if (snat_alloc_outside_address_and_port (sm, &key1, &address_index))
198 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
199 return SNAT_IN2OUT_NEXT_DROP;
201 s->outside_address_index = address_index;
205 u8 static_mapping = 1;
207 /* First try to match static mapping by local address and port */
208 if (snat_static_mapping_match (sm, *key0, &key1, 0))
211 /* Try to create dynamic translation */
212 if (snat_alloc_outside_address_and_port (sm, &key1, &address_index))
214 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
215 return SNAT_IN2OUT_NEXT_DROP;
219 /* Create a new session */
220 pool_get (sm->sessions, s);
221 memset (s, 0, sizeof (*s));
223 s->outside_address_index = address_index;
227 u->nstaticsessions++;
228 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
235 /* Create list elts */
236 pool_get (sm->list_pool, per_user_translation_list_elt);
237 clib_dlist_init (sm->list_pool, per_user_translation_list_elt -
240 per_user_translation_list_elt->value = s - sm->sessions;
241 s->per_user_index = per_user_translation_list_elt - sm->list_pool;
242 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
244 clib_dlist_addtail (sm->list_pool, s->per_user_list_head_index,
245 per_user_translation_list_elt - sm->list_pool);
250 s->out2in.protocol = key0->protocol;
251 s->out2in.fib_index = outside_fib_index;
254 /* Add to translation hashes */
255 kv0.key = s->in2out.as_u64;
256 kv0.value = s - sm->sessions;
257 if (clib_bihash_add_del_8_8 (&sm->in2out, &kv0, 1 /* is_add */))
258 clib_warning ("in2out key add failed");
260 kv0.key = s->out2in.as_u64;
261 kv0.value = s - sm->sessions;
263 if (clib_bihash_add_del_8_8 (&sm->out2in, &kv0, 1 /* is_add */))
264 clib_warning ("out2in key add failed");
269 static inline u32 icmp_in2out_slow_path (snat_main_t *sm,
272 icmp46_header_t * icmp0,
275 vlib_node_runtime_t * node,
279 snat_session_key_t key0;
280 icmp_echo_header_t *echo0;
281 clib_bihash_kv_8_8_t kv0, value0;
283 u32 new_addr0, old_addr0;
284 u16 old_id0, new_id0;
286 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
288 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request))
290 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
291 return SNAT_IN2OUT_NEXT_DROP;
294 echo0 = (icmp_echo_header_t *)(icmp0+1);
296 key0.addr = ip0->src_address;
297 key0.port = echo0->identifier;
298 key0.protocol = SNAT_PROTOCOL_ICMP;
299 key0.fib_index = rx_fib_index0;
301 kv0.key = key0.as_u64;
303 if (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0))
305 ip4_address_t * first_int_addr;
307 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
310 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
311 0 /* just want the address */);
312 rt->cached_sw_if_index = sw_if_index0;
313 rt->cached_ip4_address = first_int_addr->as_u32;
316 /* Don't NAT packet aimed at the intfc address */
317 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
318 rt->cached_ip4_address))
321 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
324 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
328 s0 = pool_elt_at_index (sm->sessions, value0.value);
330 old_addr0 = ip0->src_address.as_u32;
331 ip0->src_address = s0->out2in.addr;
332 new_addr0 = ip0->src_address.as_u32;
333 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
335 sum0 = ip0->checksum;
336 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
338 src_address /* changed member */);
339 ip0->checksum = ip_csum_fold (sum0);
341 old_id0 = echo0->identifier;
342 new_id0 = s0->out2in.port;
343 echo0->identifier = new_id0;
345 sum0 = icmp0->checksum;
346 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
348 icmp0->checksum = ip_csum_fold (sum0);
351 s0->last_heard = now;
353 s0->total_bytes += vlib_buffer_length_in_chain (sm->vlib_main, b0);
354 /* Per-user LRU list maintenance for dynamic translations */
355 if (!snat_is_session_static (s0))
357 clib_dlist_remove (sm->list_pool, s0->per_user_index);
358 clib_dlist_addtail (sm->list_pool, s0->per_user_list_head_index,
366 snat_in2out_node_fn_inline (vlib_main_t * vm,
367 vlib_node_runtime_t * node,
368 vlib_frame_t * frame, int is_slow_path)
370 u32 n_left_from, * from, * to_next;
371 snat_in2out_next_t next_index;
372 u32 pkts_processed = 0;
373 snat_main_t * sm = &snat_main;
374 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
375 f64 now = vlib_time_now (vm);
376 u32 stats_node_index;
378 stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
379 snat_in2out_node.index;
381 from = vlib_frame_vector_args (frame);
382 n_left_from = frame->n_vectors;
383 next_index = node->cached_next_index;
385 while (n_left_from > 0)
389 vlib_get_next_frame (vm, node, next_index,
390 to_next, n_left_to_next);
392 while (n_left_from >= 4 && n_left_to_next >= 2)
395 vlib_buffer_t * b0, * b1;
397 u32 sw_if_index0, sw_if_index1;
398 ip4_header_t * ip0, * ip1;
399 ip_csum_t sum0, sum1;
400 u32 new_addr0, old_addr0, new_addr1, old_addr1;
401 u16 old_port0, new_port0, old_port1, new_port1;
402 udp_header_t * udp0, * udp1;
403 tcp_header_t * tcp0, * tcp1;
404 icmp46_header_t * icmp0, * icmp1;
405 snat_session_key_t key0, key1;
406 u32 rx_fib_index0, rx_fib_index1;
408 snat_session_t * s0 = 0, * s1 = 0;
409 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
411 /* Prefetch next iteration. */
413 vlib_buffer_t * p2, * p3;
415 p2 = vlib_get_buffer (vm, from[2]);
416 p3 = vlib_get_buffer (vm, from[3]);
418 vlib_prefetch_buffer_header (p2, LOAD);
419 vlib_prefetch_buffer_header (p3, LOAD);
421 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
422 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
425 /* speculatively enqueue b0 and b1 to the current next frame */
426 to_next[0] = bi0 = from[0];
427 to_next[1] = bi1 = from[1];
433 b0 = vlib_get_buffer (vm, bi0);
434 b1 = vlib_get_buffer (vm, bi1);
436 ip0 = vlib_buffer_get_current (b0);
437 udp0 = ip4_next_header (ip0);
438 tcp0 = (tcp_header_t *) udp0;
439 icmp0 = (icmp46_header_t *) udp0;
441 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
442 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
445 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
448 /* Formally correct, but we send to slowpath, lookup or drop */
449 vnet_get_config_data (&cm->config_main,
450 &b0->current_config_index,
452 0 /* sizeof config data */);
456 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
457 ? SNAT_PROTOCOL_UDP : proto0;
458 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
459 ? SNAT_PROTOCOL_TCP : proto0;
460 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
461 ? SNAT_PROTOCOL_ICMP : proto0;
463 /* Next configured feature, probably ip4-lookup */
466 if (PREDICT_FALSE (proto0 == ~0))
469 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
471 next0 = icmp_in2out_slow_path
472 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
479 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
481 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
486 key0.addr = ip0->src_address;
487 key0.port = udp0->src_port;
488 key0.protocol = proto0;
489 key0.fib_index = rx_fib_index0;
491 kv0.key = key0.as_u64;
493 if (PREDICT_FALSE (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0) != 0))
497 ip4_address_t * first_int_addr;
499 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
502 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
503 0 /* just want the address */);
504 rt->cached_sw_if_index = sw_if_index0;
505 rt->cached_ip4_address = first_int_addr->as_u32;
508 /* Don't NAT packet aimed at the intfc address */
509 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
510 rt->cached_ip4_address))
513 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
515 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
520 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
525 s0 = pool_elt_at_index (sm->sessions, value0.value);
527 old_addr0 = ip0->src_address.as_u32;
528 ip0->src_address = s0->out2in.addr;
529 new_addr0 = ip0->src_address.as_u32;
530 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
532 sum0 = ip0->checksum;
533 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
535 src_address /* changed member */);
536 ip0->checksum = ip_csum_fold (sum0);
538 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
540 old_port0 = tcp0->ports.src;
541 tcp0->ports.src = s0->out2in.port;
542 new_port0 = tcp0->ports.src;
544 sum0 = tcp0->checksum;
545 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
547 dst_address /* changed member */);
548 sum0 = ip_csum_update (sum0, old_port0, new_port0,
549 ip4_header_t /* cheat */,
550 length /* changed member */);
551 tcp0->checksum = ip_csum_fold(sum0);
555 old_port0 = udp0->src_port;
556 udp0->src_port = s0->out2in.port;
561 s0->last_heard = now;
563 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
564 /* Per-user LRU list maintenance for dynamic translation */
565 if (!snat_is_session_static (s0))
567 clib_dlist_remove (sm->list_pool, s0->per_user_index);
568 clib_dlist_addtail (sm->list_pool, s0->per_user_list_head_index,
573 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
574 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
576 snat_in2out_trace_t *t =
577 vlib_add_trace (vm, node, b0, sizeof (*t));
578 t->is_slow_path = is_slow_path;
579 t->sw_if_index = sw_if_index0;
580 t->next_index = next0;
581 t->session_index = ~0;
583 t->session_index = s0 - sm->sessions;
586 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
588 ip1 = vlib_buffer_get_current (b1);
589 udp1 = ip4_next_header (ip1);
590 tcp1 = (tcp_header_t *) udp1;
591 icmp1 = (icmp46_header_t *) udp1;
593 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
594 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
598 vnet_get_config_data (&cm->config_main,
599 &b1->current_config_index,
601 0 /* sizeof config data */);
605 proto1 = (ip1->protocol == IP_PROTOCOL_UDP)
606 ? SNAT_PROTOCOL_UDP : proto1;
607 proto1 = (ip1->protocol == IP_PROTOCOL_TCP)
608 ? SNAT_PROTOCOL_TCP : proto1;
609 proto1 = (ip1->protocol == IP_PROTOCOL_ICMP)
610 ? SNAT_PROTOCOL_ICMP : proto1;
612 /* Next configured feature, probably ip4-lookup */
615 if (PREDICT_FALSE (proto1 == ~0))
618 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
620 next1 = icmp_in2out_slow_path
621 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node, next1,
628 if (PREDICT_FALSE (proto1 == ~0 || proto1 == SNAT_PROTOCOL_ICMP))
630 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
635 key1.addr = ip1->src_address;
636 key1.port = udp1->src_port;
637 key1.protocol = proto1;
638 key1.fib_index = rx_fib_index1;
640 kv1.key = key1.as_u64;
642 if (PREDICT_FALSE(clib_bihash_search_8_8 (&sm->in2out, &kv1, &value1) != 0))
646 ip4_address_t * first_int_addr;
648 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index1))
651 ip4_interface_first_address (sm->ip4_main, sw_if_index1,
652 0 /* just want the address */);
653 rt->cached_sw_if_index = sw_if_index1;
654 rt->cached_ip4_address = first_int_addr->as_u32;
657 /* Don't NAT packet aimed at the intfc address */
658 if (PREDICT_FALSE(ip1->dst_address.as_u32 ==
659 rt->cached_ip4_address))
662 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
664 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
669 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
674 s1 = pool_elt_at_index (sm->sessions, value1.value);
676 old_addr1 = ip1->src_address.as_u32;
677 ip1->src_address = s1->out2in.addr;
678 new_addr1 = ip1->src_address.as_u32;
679 vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
681 sum1 = ip1->checksum;
682 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
684 src_address /* changed member */);
685 ip1->checksum = ip_csum_fold (sum1);
687 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
689 old_port1 = tcp1->ports.src;
690 tcp1->ports.src = s1->out2in.port;
691 new_port1 = tcp1->ports.src;
693 sum1 = tcp1->checksum;
694 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
696 dst_address /* changed member */);
697 sum1 = ip_csum_update (sum1, old_port1, new_port1,
698 ip4_header_t /* cheat */,
699 length /* changed member */);
700 tcp1->checksum = ip_csum_fold(sum1);
704 old_port1 = udp1->src_port;
705 udp1->src_port = s1->out2in.port;
710 s1->last_heard = now;
712 s1->total_bytes += vlib_buffer_length_in_chain (vm, b1);
713 /* Per-user LRU list maintenance for dynamic translation */
714 if (!snat_is_session_static (s1))
716 clib_dlist_remove (sm->list_pool, s1->per_user_index);
717 clib_dlist_addtail (sm->list_pool, s1->per_user_list_head_index,
722 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
723 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
725 snat_in2out_trace_t *t =
726 vlib_add_trace (vm, node, b1, sizeof (*t));
727 t->sw_if_index = sw_if_index1;
728 t->next_index = next1;
729 t->session_index = ~0;
731 t->session_index = s1 - sm->sessions;
734 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
736 /* verify speculative enqueues, maybe switch current next frame */
737 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
738 to_next, n_left_to_next,
739 bi0, bi1, next0, next1);
742 while (n_left_from > 0 && n_left_to_next > 0)
750 u32 new_addr0, old_addr0;
751 u16 old_port0, new_port0;
754 icmp46_header_t * icmp0;
755 snat_session_key_t key0;
758 snat_session_t * s0 = 0;
759 clib_bihash_kv_8_8_t kv0, value0;
761 /* speculatively enqueue b0 to the current next frame */
769 b0 = vlib_get_buffer (vm, bi0);
770 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
772 ip0 = vlib_buffer_get_current (b0);
773 udp0 = ip4_next_header (ip0);
774 tcp0 = (tcp_header_t *) udp0;
775 icmp0 = (icmp46_header_t *) udp0;
777 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
778 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
783 vnet_get_config_data (&cm->config_main,
784 &b0->current_config_index,
786 0 /* sizeof config data */);
790 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
791 ? SNAT_PROTOCOL_UDP : proto0;
792 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
793 ? SNAT_PROTOCOL_TCP : proto0;
794 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
795 ? SNAT_PROTOCOL_ICMP : proto0;
797 /* Next configured feature, probably ip4-lookup */
800 if (PREDICT_FALSE (proto0 == ~0))
803 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
805 next0 = icmp_in2out_slow_path
806 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node, next0,
813 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
815 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
820 key0.addr = ip0->src_address;
821 key0.port = udp0->src_port;
822 key0.protocol = proto0;
823 key0.fib_index = rx_fib_index0;
825 kv0.key = key0.as_u64;
827 if (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0))
831 ip4_address_t * first_int_addr;
833 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
836 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
837 0 /* just want the address */);
838 rt->cached_sw_if_index = sw_if_index0;
839 rt->cached_ip4_address = first_int_addr->as_u32;
842 /* Don't NAT packet aimed at the intfc address */
843 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
844 rt->cached_ip4_address))
847 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
849 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
854 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
859 s0 = pool_elt_at_index (sm->sessions, value0.value);
861 old_addr0 = ip0->src_address.as_u32;
862 ip0->src_address = s0->out2in.addr;
863 new_addr0 = ip0->src_address.as_u32;
864 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
866 sum0 = ip0->checksum;
867 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
869 src_address /* changed member */);
870 ip0->checksum = ip_csum_fold (sum0);
872 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
874 old_port0 = tcp0->ports.src;
875 tcp0->ports.src = s0->out2in.port;
876 new_port0 = tcp0->ports.src;
878 sum0 = tcp0->checksum;
879 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
881 dst_address /* changed member */);
882 sum0 = ip_csum_update (sum0, old_port0, new_port0,
883 ip4_header_t /* cheat */,
884 length /* changed member */);
885 tcp0->checksum = ip_csum_fold(sum0);
889 old_port0 = udp0->src_port;
890 udp0->src_port = s0->out2in.port;
895 s0->last_heard = now;
897 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
898 /* Per-user LRU list maintenance for dynamic translation */
899 if (!snat_is_session_static (s0))
901 clib_dlist_remove (sm->list_pool, s0->per_user_index);
902 clib_dlist_addtail (sm->list_pool, s0->per_user_list_head_index,
907 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
908 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
910 snat_in2out_trace_t *t =
911 vlib_add_trace (vm, node, b0, sizeof (*t));
912 t->is_slow_path = is_slow_path;
913 t->sw_if_index = sw_if_index0;
914 t->next_index = next0;
915 t->session_index = ~0;
917 t->session_index = s0 - sm->sessions;
920 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
922 /* verify speculative enqueue, maybe switch current next frame */
923 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
924 to_next, n_left_to_next,
928 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
931 vlib_node_increment_counter (vm, stats_node_index,
932 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
934 return frame->n_vectors;
938 snat_in2out_fast_path_fn (vlib_main_t * vm,
939 vlib_node_runtime_t * node,
940 vlib_frame_t * frame)
942 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */);
945 VLIB_REGISTER_NODE (snat_in2out_node) = {
946 .function = snat_in2out_fast_path_fn,
947 .name = "snat-in2out",
948 .vector_size = sizeof (u32),
949 .format_trace = format_snat_in2out_trace,
950 .type = VLIB_NODE_TYPE_INTERNAL,
952 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
953 .error_strings = snat_in2out_error_strings,
955 .runtime_data_bytes = sizeof (snat_runtime_t),
957 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
959 /* edit / add dispositions here */
961 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
962 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
963 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
967 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_node, snat_in2out_fast_path_fn);
970 snat_in2out_slow_path_fn (vlib_main_t * vm,
971 vlib_node_runtime_t * node,
972 vlib_frame_t * frame)
974 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */);
977 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
978 .function = snat_in2out_slow_path_fn,
979 .name = "snat-in2out-slowpath",
980 .vector_size = sizeof (u32),
981 .format_trace = format_snat_in2out_trace,
982 .type = VLIB_NODE_TYPE_INTERNAL,
984 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
985 .error_strings = snat_in2out_error_strings,
987 .runtime_data_bytes = sizeof (snat_runtime_t),
989 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
991 /* edit / add dispositions here */
993 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
994 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
995 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
999 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_slowpath_node, snat_in2out_slow_path_fn);
1001 static inline u32 icmp_in2out_static_map (snat_main_t *sm,
1004 icmp46_header_t * icmp0,
1006 vlib_node_runtime_t * node,
1009 snat_session_key_t key0, sm0;
1010 icmp_echo_header_t *echo0;
1011 u32 new_addr0, old_addr0;
1012 u16 old_id0, new_id0;
1014 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
1016 echo0 = (icmp_echo_header_t *)(icmp0+1);
1018 key0.addr = ip0->src_address;
1019 key0.port = echo0->identifier;
1021 if (snat_static_mapping_match(sm, key0, &sm0, 0))
1023 ip4_address_t * first_int_addr;
1025 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
1028 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
1029 0 /* just want the address */);
1030 rt->cached_sw_if_index = sw_if_index0;
1031 rt->cached_ip4_address = first_int_addr->as_u32;
1034 /* Don't NAT packet aimed at the intfc address */
1035 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
1036 rt->cached_ip4_address))
1039 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1040 return SNAT_IN2OUT_NEXT_DROP;
1043 new_addr0 = sm0.addr.as_u32;
1045 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1046 old_addr0 = ip0->src_address.as_u32;
1047 ip0->src_address.as_u32 = new_addr0;
1049 sum0 = ip0->checksum;
1050 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1052 src_address /* changed member */);
1053 ip0->checksum = ip_csum_fold (sum0);
1055 if (PREDICT_FALSE(new_id0 != echo0->identifier))
1057 old_id0 = echo0->identifier;
1058 echo0->identifier = new_id0;
1060 sum0 = icmp0->checksum;
1061 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
1063 icmp0->checksum = ip_csum_fold (sum0);
1070 snat_in2out_fast_static_map_fn (vlib_main_t * vm,
1071 vlib_node_runtime_t * node,
1072 vlib_frame_t * frame)
1074 u32 n_left_from, * from, * to_next;
1075 snat_in2out_next_t next_index;
1076 u32 pkts_processed = 0;
1077 snat_main_t * sm = &snat_main;
1078 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
1079 u32 stats_node_index;
1081 stats_node_index = snat_in2out_fast_node.index;
1083 from = vlib_frame_vector_args (frame);
1084 n_left_from = frame->n_vectors;
1085 next_index = node->cached_next_index;
1087 while (n_left_from > 0)
1091 vlib_get_next_frame (vm, node, next_index,
1092 to_next, n_left_to_next);
1094 while (n_left_from > 0 && n_left_to_next > 0)
1102 u32 new_addr0, old_addr0;
1103 u16 old_port0, new_port0;
1104 udp_header_t * udp0;
1105 tcp_header_t * tcp0;
1106 icmp46_header_t * icmp0;
1107 snat_session_key_t key0, sm0;
1110 /* speculatively enqueue b0 to the current next frame */
1116 n_left_to_next -= 1;
1118 b0 = vlib_get_buffer (vm, bi0);
1119 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1121 ip0 = vlib_buffer_get_current (b0);
1122 udp0 = ip4_next_header (ip0);
1123 tcp0 = (tcp_header_t *) udp0;
1124 icmp0 = (icmp46_header_t *) udp0;
1126 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1129 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
1130 ? SNAT_PROTOCOL_UDP : proto0;
1131 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
1132 ? SNAT_PROTOCOL_TCP : proto0;
1133 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
1134 ? SNAT_PROTOCOL_ICMP : proto0;
1136 if (PREDICT_FALSE (proto0 == ~0))
1139 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1141 ip4_address_t * first_int_addr;
1143 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
1146 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
1147 0 /* just want the address */);
1148 rt->cached_sw_if_index = sw_if_index0;
1149 rt->cached_ip4_address = first_int_addr->as_u32;
1152 /* Don't NAT packet aimed at the intfc address */
1153 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
1154 rt->cached_ip4_address))
1157 next0 = icmp_in2out_static_map
1158 (sm, b0, ip0, icmp0, sw_if_index0, node, next0);
1162 key0.addr = ip0->src_address;
1163 key0.port = udp0->src_port;
1165 if (snat_static_mapping_match(sm, key0, &sm0, 0))
1167 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1168 next0= SNAT_IN2OUT_NEXT_DROP;
1172 new_addr0 = sm0.addr.as_u32;
1173 new_port0 = sm0.port;
1174 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1175 old_addr0 = ip0->src_address.as_u32;
1176 ip0->src_address.as_u32 = new_addr0;
1178 sum0 = ip0->checksum;
1179 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1181 src_address /* changed member */);
1182 ip0->checksum = ip_csum_fold (sum0);
1184 if (PREDICT_FALSE(new_port0 != udp0->dst_port))
1186 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1188 old_port0 = tcp0->ports.src;
1189 tcp0->ports.src = new_port0;
1191 sum0 = tcp0->checksum;
1192 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1194 dst_address /* changed member */);
1195 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1196 ip4_header_t /* cheat */,
1197 length /* changed member */);
1198 tcp0->checksum = ip_csum_fold(sum0);
1202 old_port0 = udp0->src_port;
1203 udp0->src_port = new_port0;
1209 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1211 sum0 = tcp0->checksum;
1212 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1214 dst_address /* changed member */);
1215 tcp0->checksum = ip_csum_fold(sum0);
1220 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1221 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1223 snat_in2out_trace_t *t =
1224 vlib_add_trace (vm, node, b0, sizeof (*t));
1225 t->sw_if_index = sw_if_index0;
1226 t->next_index = next0;
1229 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1231 /* verify speculative enqueue, maybe switch current next frame */
1232 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1233 to_next, n_left_to_next,
1237 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1240 vlib_node_increment_counter (vm, stats_node_index,
1241 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1243 return frame->n_vectors;
1247 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1248 .function = snat_in2out_fast_static_map_fn,
1249 .name = "snat-in2out-fast",
1250 .vector_size = sizeof (u32),
1251 .format_trace = format_snat_in2out_fast_trace,
1252 .type = VLIB_NODE_TYPE_INTERNAL,
1254 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1255 .error_strings = snat_in2out_error_strings,
1257 .runtime_data_bytes = sizeof (snat_runtime_t),
1259 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1261 /* edit / add dispositions here */
1263 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1264 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1265 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
1269 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_fast_node, snat_in2out_fast_static_map_fn);