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>
28 vlib_node_registration_t snat_out2in_node;
34 } snat_out2in_trace_t;
36 /* packet trace format function */
37 static u8 * format_snat_out2in_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_out2in_trace_t * t = va_arg (*args, snat_out2in_trace_t *);
43 s = format (s, "SNAT_OUT2IN: sw_if_index %d, next index %d, session index %d",
44 t->sw_if_index, t->next_index, t->session_index);
48 vlib_node_registration_t snat_out2in_node;
50 #define foreach_snat_out2in_error \
51 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
52 _(OUT2IN_PACKETS, "Good out2in packets processed") \
53 _(BAD_ICMP_TYPE, "icmp type not echo-reply") \
54 _(NO_TRANSLATION, "No translation")
57 #define _(sym,str) SNAT_OUT2IN_ERROR_##sym,
58 foreach_snat_out2in_error
61 } snat_out2in_error_t;
63 static char * snat_out2in_error_strings[] = {
64 #define _(sym,string) string,
65 foreach_snat_out2in_error
70 SNAT_OUT2IN_NEXT_DROP,
74 static inline u32 icmp_out2in_slow_path (snat_main_t *sm,
77 icmp46_header_t * icmp0,
80 vlib_node_runtime_t * node,
83 snat_session_key_t key0;
84 icmp_echo_header_t *echo0;
85 clib_bihash_kv_8_8_t kv0, value0;
87 u32 new_addr0, old_addr0;
90 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
92 echo0 = (icmp_echo_header_t *)(icmp0+1);
94 key0.addr = ip0->dst_address;
95 key0.port = echo0->identifier;
96 key0.protocol = SNAT_PROTOCOL_ICMP;
97 key0.fib_index = rx_fib_index0;
99 kv0.key = key0.as_u64;
101 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
103 ip4_address_t * first_int_addr;
105 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
108 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
109 0 /* just want the address */);
110 rt->cached_sw_if_index = sw_if_index0;
111 rt->cached_ip4_address = first_int_addr->as_u32;
114 /* Don't NAT packet aimed at the intfc address */
115 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
116 rt->cached_ip4_address))
119 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
120 return SNAT_OUT2IN_NEXT_DROP;
123 s0 = pool_elt_at_index (sm->sessions, value0.value);
125 old_addr0 = ip0->dst_address.as_u32;
126 ip0->dst_address = s0->in2out.addr;
127 new_addr0 = ip0->dst_address.as_u32;
128 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
130 sum0 = ip0->checksum;
131 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
133 dst_address /* changed member */);
134 ip0->checksum = ip_csum_fold (sum0);
136 old_id0 = echo0->identifier;
137 new_id0 = s0->in2out.port;
138 echo0->identifier = new_id0;
140 sum0 = icmp0->checksum;
141 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
143 icmp0->checksum = ip_csum_fold (sum0);
145 /* Accounting, per-user LRU list maintenance */
146 s0->last_heard = now;
148 s0->total_bytes += vlib_buffer_length_in_chain (sm->vlib_main, b0);
149 clib_dlist_remove (sm->list_pool, s0->per_user_index);
150 clib_dlist_addtail (sm->list_pool, s0->per_user_list_head_index,
157 snat_out2in_node_fn (vlib_main_t * vm,
158 vlib_node_runtime_t * node,
159 vlib_frame_t * frame)
161 u32 n_left_from, * from, * to_next;
162 snat_out2in_next_t next_index;
163 u32 pkts_processed = 0;
164 snat_main_t * sm = &snat_main;
165 ip_lookup_main_t * lm = sm->ip4_lookup_main;
166 ip_config_main_t * cm = &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
167 f64 now = vlib_time_now (vm);
169 from = vlib_frame_vector_args (frame);
170 n_left_from = frame->n_vectors;
171 next_index = node->cached_next_index;
173 while (n_left_from > 0)
177 vlib_get_next_frame (vm, node, next_index,
178 to_next, n_left_to_next);
180 while (n_left_from >= 4 && n_left_to_next >= 2)
183 vlib_buffer_t * b0, * b1;
184 u32 next0 = SNAT_OUT2IN_NEXT_DROP;
185 u32 next1 = SNAT_OUT2IN_NEXT_DROP;
186 u32 sw_if_index0, sw_if_index1;
187 ip4_header_t * ip0, *ip1;
188 ip_csum_t sum0, sum1;
189 u32 new_addr0, old_addr0;
190 u16 new_port0, old_port0;
191 u32 new_addr1, old_addr1;
192 u16 new_port1, old_port1;
193 udp_header_t * udp0, * udp1;
194 tcp_header_t * tcp0, * tcp1;
195 icmp46_header_t * icmp0, * icmp1;
196 snat_session_key_t key0, key1;
197 u32 rx_fib_index0, rx_fib_index1;
199 snat_session_t * s0 = 0, * s1 = 0;
200 clib_bihash_kv_8_8_t kv0, kv1, value0, value1;
202 /* Prefetch next iteration. */
204 vlib_buffer_t * p2, * p3;
206 p2 = vlib_get_buffer (vm, from[2]);
207 p3 = vlib_get_buffer (vm, from[3]);
209 vlib_prefetch_buffer_header (p2, LOAD);
210 vlib_prefetch_buffer_header (p3, LOAD);
212 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
213 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
216 /* speculatively enqueue b0 and b1 to the current next frame */
217 to_next[0] = bi0 = from[0];
218 to_next[1] = bi1 = from[1];
224 b0 = vlib_get_buffer (vm, bi0);
225 b1 = vlib_get_buffer (vm, bi1);
227 ip0 = vlib_buffer_get_current (b0);
228 udp0 = ip4_next_header (ip0);
229 tcp0 = (tcp_header_t *) udp0;
230 icmp0 = (icmp46_header_t *) udp0;
232 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
233 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
236 vnet_get_config_data (&cm->config_main,
237 &b0->current_config_index,
239 0 /* sizeof config data */);
241 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
242 ? SNAT_PROTOCOL_UDP : proto0;
243 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
244 ? SNAT_PROTOCOL_TCP : proto0;
245 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
246 ? SNAT_PROTOCOL_ICMP : proto0;
248 if (PREDICT_FALSE (proto0 == ~0))
251 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
253 next0 = icmp_out2in_slow_path
254 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
259 key0.addr = ip0->dst_address;
260 key0.port = udp0->dst_port;
261 key0.protocol = proto0;
262 key0.fib_index = rx_fib_index0;
264 kv0.key = key0.as_u64;
266 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
269 s0 = pool_elt_at_index (sm->sessions, value0.value);
271 old_addr0 = ip0->dst_address.as_u32;
272 ip0->dst_address = s0->in2out.addr;
273 new_addr0 = ip0->dst_address.as_u32;
274 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
276 sum0 = ip0->checksum;
277 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
279 dst_address /* changed member */);
280 ip0->checksum = ip_csum_fold (sum0);
282 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
284 old_port0 = tcp0->ports.dst;
285 tcp0->ports.dst = s0->in2out.port;
286 new_port0 = tcp0->ports.dst;
288 sum0 = tcp0->checksum;
289 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
291 dst_address /* changed member */);
293 sum0 = ip_csum_update (sum0, old_port0, new_port0,
294 ip4_header_t /* cheat */,
295 length /* changed member */);
296 tcp0->checksum = ip_csum_fold(sum0);
300 old_port0 = udp0->dst_port;
301 udp0->dst_port = s0->in2out.port;
305 /* Accounting, per-user LRU list maintenance */
306 s0->last_heard = now;
308 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
309 clib_dlist_remove (sm->list_pool, s0->per_user_index);
310 clib_dlist_addtail (sm->list_pool, s0->per_user_list_head_index,
314 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
315 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
317 snat_out2in_trace_t *t =
318 vlib_add_trace (vm, node, b0, sizeof (*t));
319 t->sw_if_index = sw_if_index0;
320 t->next_index = next0;
321 t->session_index = ~0;
323 t->session_index = s0 - sm->sessions;
326 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
329 ip1 = vlib_buffer_get_current (b1);
330 udp1 = ip4_next_header (ip1);
331 tcp1 = (tcp_header_t *) udp1;
332 icmp1 = (icmp46_header_t *) udp1;
334 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
335 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
338 vnet_get_config_data (&cm->config_main,
339 &b1->current_config_index,
341 0 /* sizeof config data */);
343 proto1 = (ip1->protocol == IP_PROTOCOL_UDP)
344 ? SNAT_PROTOCOL_UDP : proto1;
345 proto1 = (ip1->protocol == IP_PROTOCOL_TCP)
346 ? SNAT_PROTOCOL_TCP : proto1;
347 proto1 = (ip1->protocol == IP_PROTOCOL_ICMP)
348 ? SNAT_PROTOCOL_ICMP : proto1;
350 if (PREDICT_FALSE (proto1 == ~0))
353 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
355 next1 = icmp_out2in_slow_path
356 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
361 key1.addr = ip1->dst_address;
362 key1.port = udp1->dst_port;
363 key1.protocol = proto1;
364 key1.fib_index = rx_fib_index1;
366 kv1.key = key1.as_u64;
368 if (clib_bihash_search_8_8 (&sm->out2in, &kv1, &value1))
371 s1 = pool_elt_at_index (sm->sessions, value1.value);
373 old_addr1 = ip1->dst_address.as_u32;
374 ip1->dst_address = s1->in2out.addr;
375 new_addr1 = ip1->dst_address.as_u32;
376 vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
378 sum1 = ip1->checksum;
379 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
381 dst_address /* changed member */);
382 ip1->checksum = ip_csum_fold (sum1);
384 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
386 old_port1 = tcp1->ports.dst;
387 tcp1->ports.dst = s1->in2out.port;
388 new_port1 = tcp1->ports.dst;
390 sum1 = tcp1->checksum;
391 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
393 dst_address /* changed member */);
395 sum1 = ip_csum_update (sum1, old_port1, new_port1,
396 ip4_header_t /* cheat */,
397 length /* changed member */);
398 tcp1->checksum = ip_csum_fold(sum1);
402 old_port1 = udp1->dst_port;
403 udp1->dst_port = s1->in2out.port;
407 /* Accounting, per-user LRU list maintenance */
408 s1->last_heard = now;
410 s1->total_bytes += vlib_buffer_length_in_chain (vm, b1);
411 clib_dlist_remove (sm->list_pool, s1->per_user_index);
412 clib_dlist_addtail (sm->list_pool, s1->per_user_list_head_index,
416 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
417 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
419 snat_out2in_trace_t *t =
420 vlib_add_trace (vm, node, b1, sizeof (*t));
421 t->sw_if_index = sw_if_index1;
422 t->next_index = next1;
423 t->session_index = ~0;
425 t->session_index = s1 - sm->sessions;
428 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
429 pkts_processed += next1 != SNAT_OUT2IN_NEXT_DROP;
431 /* verify speculative enqueues, maybe switch current next frame */
432 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
433 to_next, n_left_to_next,
434 bi0, bi1, next0, next1);
437 while (n_left_from > 0 && n_left_to_next > 0)
441 u32 next0 = SNAT_OUT2IN_NEXT_DROP;
445 u32 new_addr0, old_addr0;
446 u16 new_port0, old_port0;
449 icmp46_header_t * icmp0;
450 snat_session_key_t key0;
453 snat_session_t * s0 = 0;
454 clib_bihash_kv_8_8_t kv0, value0;
456 /* speculatively enqueue b0 to the current next frame */
464 b0 = vlib_get_buffer (vm, bi0);
466 ip0 = vlib_buffer_get_current (b0);
467 udp0 = ip4_next_header (ip0);
468 tcp0 = (tcp_header_t *) udp0;
469 icmp0 = (icmp46_header_t *) udp0;
471 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
472 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
475 vnet_get_config_data (&cm->config_main,
476 &b0->current_config_index,
478 0 /* sizeof config data */);
480 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
481 ? SNAT_PROTOCOL_UDP : proto0;
482 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
483 ? SNAT_PROTOCOL_TCP : proto0;
484 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
485 ? SNAT_PROTOCOL_ICMP : proto0;
487 if (PREDICT_FALSE (proto0 == ~0))
490 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
492 next0 = icmp_out2in_slow_path
493 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
498 key0.addr = ip0->dst_address;
499 key0.port = udp0->dst_port;
500 key0.protocol = proto0;
501 key0.fib_index = rx_fib_index0;
503 kv0.key = key0.as_u64;
505 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
508 s0 = pool_elt_at_index (sm->sessions, value0.value);
510 old_addr0 = ip0->dst_address.as_u32;
511 ip0->dst_address = s0->in2out.addr;
512 new_addr0 = ip0->dst_address.as_u32;
513 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
515 sum0 = ip0->checksum;
516 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
518 dst_address /* changed member */);
519 ip0->checksum = ip_csum_fold (sum0);
521 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
523 old_port0 = tcp0->ports.dst;
524 tcp0->ports.dst = s0->in2out.port;
525 new_port0 = tcp0->ports.dst;
527 sum0 = tcp0->checksum;
528 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
530 dst_address /* changed member */);
532 sum0 = ip_csum_update (sum0, old_port0, new_port0,
533 ip4_header_t /* cheat */,
534 length /* changed member */);
535 tcp0->checksum = ip_csum_fold(sum0);
539 old_port0 = udp0->dst_port;
540 udp0->dst_port = s0->in2out.port;
544 /* Accounting, per-user LRU list maintenance */
545 s0->last_heard = now;
547 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
548 clib_dlist_remove (sm->list_pool, s0->per_user_index);
549 clib_dlist_addtail (sm->list_pool, s0->per_user_list_head_index,
553 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
554 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
556 snat_out2in_trace_t *t =
557 vlib_add_trace (vm, node, b0, sizeof (*t));
558 t->sw_if_index = sw_if_index0;
559 t->next_index = next0;
560 t->session_index = ~0;
562 t->session_index = s0 - sm->sessions;
565 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
567 /* verify speculative enqueue, maybe switch current next frame */
568 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
569 to_next, n_left_to_next,
573 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
576 vlib_node_increment_counter (vm, snat_out2in_node.index,
577 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
579 return frame->n_vectors;
582 VLIB_REGISTER_NODE (snat_out2in_node) = {
583 .function = snat_out2in_node_fn,
584 .name = "snat-out2in",
585 .vector_size = sizeof (u32),
586 .format_trace = format_snat_out2in_trace,
587 .type = VLIB_NODE_TYPE_INTERNAL,
589 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
590 .error_strings = snat_out2in_error_strings,
592 .runtime_data_bytes = sizeof (snat_runtime_t),
594 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
596 /* edit / add dispositions here */
598 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
601 VLIB_NODE_FUNCTION_MULTIARCH (snat_out2in_node, snat_out2in_node_fn);