2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
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 * @brief The NAT inline functions
19 #ifndef __included_nat_inlines_h__
20 #define __included_nat_inlines_h__
22 #include <vnet/fib/ip4_fib.h>
24 #include <nat/nat_ha.h>
27 nat_pre_node_fn_inline (vlib_main_t * vm,
28 vlib_node_runtime_t * node,
29 vlib_frame_t * frame, u32 def_next)
31 u32 n_left_from, *from, *to_next;
34 from = vlib_frame_vector_args (frame);
35 n_left_from = frame->n_vectors;
36 next_index = node->cached_next_index;
38 while (n_left_from > 0)
42 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
44 while (n_left_from >= 4 && n_left_to_next >= 2)
47 u32 arc_next0, arc_next1;
49 vlib_buffer_t *b0, *b1;
51 /* Prefetch next iteration. */
53 vlib_buffer_t *p2, *p3;
55 p2 = vlib_get_buffer (vm, from[2]);
56 p3 = vlib_get_buffer (vm, from[3]);
58 vlib_prefetch_buffer_header (p2, LOAD);
59 vlib_prefetch_buffer_header (p3, LOAD);
61 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
62 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
65 /* speculatively enqueue b0 and b1 to the current next frame */
66 to_next[0] = bi0 = from[0];
67 to_next[1] = bi1 = from[1];
73 b0 = vlib_get_buffer (vm, bi0);
74 b1 = vlib_get_buffer (vm, bi1);
79 vnet_feature_next (&arc_next0, b0);
80 vnet_feature_next (&arc_next1, b1);
82 nat_buffer_opaque (b0)->arc_next = arc_next0;
83 nat_buffer_opaque (b1)->arc_next = arc_next1;
85 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
87 if (b0->flags & VLIB_BUFFER_IS_TRACED)
90 vlib_add_trace (vm, node, b0, sizeof (*t));
91 t->next_index = next0;
93 if (b1->flags & VLIB_BUFFER_IS_TRACED)
96 vlib_add_trace (vm, node, b0, sizeof (*t));
97 t->next_index = next0;
101 /* verify speculative enqueues, maybe switch current next frame */
102 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
103 to_next, n_left_to_next,
104 bi0, bi1, next0, next1);
107 while (n_left_from > 0 && n_left_to_next > 0)
114 /* speculatively enqueue b0 to the current next frame */
122 b0 = vlib_get_buffer (vm, bi0);
124 vnet_feature_next (&arc_next0, b0);
125 nat_buffer_opaque (b0)->arc_next = arc_next0;
127 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
128 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
130 nat_pre_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
131 t->next_index = next0;
134 /* verify speculative enqueue, maybe switch current next frame */
135 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
136 to_next, n_left_to_next,
140 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
143 return frame->n_vectors;
147 ip_proto_to_snat_proto (u8 ip_proto)
151 snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
152 snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
154 (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
156 (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
162 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
166 ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
167 ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
168 ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
173 static_always_inline u8
174 icmp_type_is_error_message (u8 icmp_type)
178 case ICMP4_destination_unreachable:
179 case ICMP4_time_exceeded:
180 case ICMP4_parameter_problem:
181 case ICMP4_source_quench:
183 case ICMP4_alternate_host_address:
190 is_interface_addr (snat_main_t * sm, vlib_node_runtime_t * node,
191 u32 sw_if_index0, u32 ip4_addr)
193 snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
194 ip4_address_t *first_int_addr;
196 if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
199 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
200 0 /* just want the address */ );
201 rt->cached_sw_if_index = sw_if_index0;
203 rt->cached_ip4_address = first_int_addr->as_u32;
205 rt->cached_ip4_address = 0;
208 if (PREDICT_FALSE (ip4_addr == rt->cached_ip4_address))
215 maximum_sessions_exceeded (snat_main_t * sm, u32 thread_index)
217 if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
218 sm->max_translations)
225 nat_send_all_to_node (vlib_main_t * vm, u32 * bi_vector,
226 vlib_node_runtime_t * node, vlib_error_t * error,
229 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
232 n_left_from = vec_len (bi_vector);
233 next_index = node->cached_next_index;
234 while (n_left_from > 0)
236 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
237 while (n_left_from > 0 && n_left_to_next > 0)
239 u32 bi0 = to_next[0] = from[0];
244 vlib_buffer_t *p0 = vlib_get_buffer (vm, bi0);
247 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
248 n_left_to_next, bi0, next);
250 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
255 user_session_increment (snat_main_t * sm, snat_user_t * u, u8 is_static)
257 if (u->nsessions + u->nstaticsessions < sm->max_translations_per_user)
260 u->nstaticsessions++;
267 nat44_delete_user_with_no_session (snat_main_t * sm, snat_user_t * u,
270 clib_bihash_kv_8_8_t kv;
271 snat_user_key_t u_key;
272 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
275 if (u->nstaticsessions == 0 && u->nsessions == 0)
277 u_key.addr.as_u32 = u->addr.as_u32;
278 u_key.fib_index = u->fib_index;
279 kv.key = u_key.as_u64;
280 pool_put_index (tsm->list_pool, u->sessions_per_user_list_head_index);
281 pool_put (tsm->users, u);
282 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
283 vlib_set_simple_counter (&sm->total_users, thread_index, 0,
284 pool_elts (tsm->users));
289 nat44_delete_session_internal (snat_main_t * sm, snat_session_t * ses,
290 u32 thread_index, int global_lru_delete
291 /* delete from global LRU list */ )
293 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
295 clib_bihash_kv_8_8_t kv, value;
297 const snat_user_key_t u_key = {
298 .addr = ses->in2out.addr,
299 .fib_index = ses->in2out.fib_index
301 const u8 u_static = snat_is_session_static (ses);
303 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
304 pool_put_index (tsm->list_pool, ses->per_user_index);
305 if (global_lru_delete)
307 clib_dlist_remove (tsm->global_lru_pool, ses->global_lru_index);
309 pool_put_index (tsm->global_lru_pool, ses->global_lru_index);
310 pool_put (tsm->sessions, ses);
311 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
312 pool_elts (tsm->sessions));
314 kv.key = u_key.as_u64;
315 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
317 u = pool_elt_at_index (tsm->users, value.value);
319 u->nstaticsessions--;
323 nat44_delete_user_with_no_session (sm, u, thread_index);
328 nat44_delete_session (snat_main_t * sm, snat_session_t * ses,
331 return nat44_delete_session_internal (sm, ses, thread_index, 1);
335 nat44_ed_delete_session (snat_main_t * sm, snat_session_t * ses,
336 u32 thread_index, int global_lru_delete
337 /* delete from global LRU list */ )
339 return nat44_delete_session_internal (sm, ses, thread_index,
343 /** \brief Set TCP session state.
344 @return 1 if session was closed, otherwise 0
347 nat44_set_tcp_session_state_i2o (snat_main_t * sm, f64 now,
348 snat_session_t * ses, vlib_buffer_t * b,
351 u8 tcp_flags = vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags;
352 u32 tcp_ack_number = vnet_buffer (b)->ip.reass.tcp_ack_number;
353 u32 tcp_seq_number = vnet_buffer (b)->ip.reass.tcp_seq_number;
354 if ((ses->state == 0) && (tcp_flags & TCP_FLAG_RST))
355 ses->state = NAT44_SES_RST;
356 if ((ses->state == NAT44_SES_RST) && !(tcp_flags & TCP_FLAG_RST))
358 if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
359 (ses->state & NAT44_SES_O2I_SYN))
361 if (tcp_flags & TCP_FLAG_SYN)
362 ses->state |= NAT44_SES_I2O_SYN;
363 if (tcp_flags & TCP_FLAG_FIN)
365 ses->i2o_fin_seq = clib_net_to_host_u32 (tcp_seq_number);
366 ses->state |= NAT44_SES_I2O_FIN;
368 if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN))
370 if (clib_net_to_host_u32 (tcp_ack_number) > ses->o2i_fin_seq)
372 ses->state |= NAT44_SES_O2I_FIN_ACK;
373 if (nat44_is_ses_closed (ses))
374 { // if session is now closed, save the timestamp
375 ses->tcp_close_timestamp = now + sm->tcp_transitory_timeout;
383 nat44_set_tcp_session_state_o2i (snat_main_t * sm, f64 now,
384 snat_session_t * ses, u8 tcp_flags,
385 u32 tcp_ack_number, u32 tcp_seq_number,
388 if ((ses->state == 0) && (tcp_flags & TCP_FLAG_RST))
389 ses->state = NAT44_SES_RST;
390 if ((ses->state == NAT44_SES_RST) && !(tcp_flags & TCP_FLAG_RST))
392 if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
393 (ses->state & NAT44_SES_O2I_SYN))
395 if (tcp_flags & TCP_FLAG_SYN)
396 ses->state |= NAT44_SES_O2I_SYN;
397 if (tcp_flags & TCP_FLAG_FIN)
399 ses->o2i_fin_seq = clib_net_to_host_u32 (tcp_seq_number);
400 ses->state |= NAT44_SES_O2I_FIN;
402 if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN))
404 if (clib_net_to_host_u32 (tcp_ack_number) > ses->i2o_fin_seq)
405 ses->state |= NAT44_SES_I2O_FIN_ACK;
406 if (nat44_is_ses_closed (ses))
407 { // if session is now closed, save the timestamp
408 ses->tcp_close_timestamp = now + sm->tcp_transitory_timeout;
415 nat44_session_get_timeout (snat_main_t * sm, snat_session_t * s)
417 switch (s->in2out.protocol)
419 case SNAT_PROTOCOL_ICMP:
420 return sm->icmp_timeout;
421 case SNAT_PROTOCOL_UDP:
422 return sm->udp_timeout;
423 case SNAT_PROTOCOL_TCP:
426 return sm->tcp_transitory_timeout;
428 return sm->tcp_established_timeout;
431 return sm->udp_timeout;
438 nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes,
443 s->total_bytes += bytes;
444 nat_ha_sref (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
445 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
446 s->total_pkts, s->total_bytes, thread_index,
447 &s->ha_last_refreshed, now);
450 /** \brief Per-user LRU list maintenance */
452 nat44_session_update_lru (snat_main_t * sm, snat_session_t * s,
455 /* don't update too often - timeout is in a magnitude of seconds anyway */
456 if (s->last_heard > s->last_lru_update + 1)
458 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
460 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
461 s->per_user_list_head_index, s->per_user_index);
463 clib_dlist_remove (sm->per_thread_data[thread_index].global_lru_pool,
464 s->global_lru_index);
465 clib_dlist_addtail (sm->per_thread_data[thread_index].global_lru_pool,
467 per_thread_data[thread_index].global_lru_head_index,
468 s->global_lru_index);
469 s->last_lru_update = s->last_heard;
474 make_ed_kv (clib_bihash_kv_16_8_t * kv, ip4_address_t * l_addr,
475 ip4_address_t * r_addr, u8 proto, u32 fib_index, u16 l_port,
478 nat_ed_ses_key_t *key = (nat_ed_ses_key_t *) kv->key;
480 key->l_addr.as_u32 = l_addr->as_u32;
481 key->r_addr.as_u32 = r_addr->as_u32;
482 key->fib_index = fib_index;
484 key->l_port = l_port;
485 key->r_port = r_port;
491 make_sm_kv (clib_bihash_kv_8_8_t * kv, ip4_address_t * addr, u8 proto,
492 u32 fib_index, u16 port)
494 snat_session_key_t key;
496 key.addr.as_u32 = addr->as_u32;
498 key.protocol = proto;
499 key.fib_index = fib_index;
501 kv->key = key.as_u64;
505 static_always_inline int
506 get_icmp_i2o_ed_key (vlib_buffer_t * b, ip4_header_t * ip0,
507 nat_ed_ses_key_t * p_key0)
509 icmp46_header_t *icmp0;
510 nat_ed_ses_key_t key0;
511 icmp_echo_header_t *echo0, *inner_echo0 = 0;
512 ip4_header_t *inner_ip0 = 0;
514 icmp46_header_t *inner_icmp0;
516 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
517 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
519 if (!icmp_type_is_error_message
520 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
522 key0.proto = IP_PROTOCOL_ICMP;
523 key0.l_addr = ip0->src_address;
524 key0.r_addr = ip0->dst_address;
525 key0.l_port = vnet_buffer (b)->ip.reass.l4_src_port; // TODO should this be src or dst?
530 inner_ip0 = (ip4_header_t *) (echo0 + 1);
531 l4_header = ip4_next_header (inner_ip0);
532 key0.proto = inner_ip0->protocol;
533 key0.r_addr = inner_ip0->src_address;
534 key0.l_addr = inner_ip0->dst_address;
535 switch (ip_proto_to_snat_proto (inner_ip0->protocol))
537 case SNAT_PROTOCOL_ICMP:
538 inner_icmp0 = (icmp46_header_t *) l4_header;
539 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
541 key0.l_port = inner_echo0->identifier;
543 case SNAT_PROTOCOL_UDP:
544 case SNAT_PROTOCOL_TCP:
545 key0.l_port = ((tcp_udp_header_t *) l4_header)->dst_port;
546 key0.r_port = ((tcp_udp_header_t *) l4_header)->src_port;
549 return NAT_IN2OUT_ED_ERROR_UNSUPPORTED_PROTOCOL;
557 static_always_inline int
558 get_icmp_o2i_ed_key (vlib_buffer_t * b, ip4_header_t * ip0,
559 nat_ed_ses_key_t * p_key0)
561 icmp46_header_t *icmp0;
562 nat_ed_ses_key_t key0;
563 icmp_echo_header_t *echo0, *inner_echo0 = 0;
564 ip4_header_t *inner_ip0;
566 icmp46_header_t *inner_icmp0;
568 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
569 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
571 if (!icmp_type_is_error_message
572 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
574 key0.proto = IP_PROTOCOL_ICMP;
575 key0.l_addr = ip0->dst_address;
576 key0.r_addr = ip0->src_address;
577 key0.l_port = vnet_buffer (b)->ip.reass.l4_src_port; // TODO should this be src or dst?
582 inner_ip0 = (ip4_header_t *) (echo0 + 1);
583 l4_header = ip4_next_header (inner_ip0);
584 key0.proto = inner_ip0->protocol;
585 key0.l_addr = inner_ip0->src_address;
586 key0.r_addr = inner_ip0->dst_address;
587 switch (ip_proto_to_snat_proto (inner_ip0->protocol))
589 case SNAT_PROTOCOL_ICMP:
590 inner_icmp0 = (icmp46_header_t *) l4_header;
591 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
592 key0.l_port = inner_echo0->identifier;
595 case SNAT_PROTOCOL_UDP:
596 case SNAT_PROTOCOL_TCP:
597 key0.l_port = ((tcp_udp_header_t *) l4_header)->src_port;
598 key0.r_port = ((tcp_udp_header_t *) l4_header)->dst_port;
609 mss_clamping (snat_main_t * sm, tcp_header_t * tcp, ip_csum_t * sum)
612 u8 opt_len, opts_len, kind;
615 if (!(sm->mss_clamping && tcp_syn (tcp)))
618 opts_len = (tcp_doff (tcp) << 2) - sizeof (tcp_header_t);
619 data = (u8 *) (tcp + 1);
620 for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
624 if (kind == TCP_OPTION_EOL)
626 else if (kind == TCP_OPTION_NOOP)
637 if (opt_len < 2 || opt_len > opts_len)
641 if (kind == TCP_OPTION_MSS)
643 mss = *(u16 *) (data + 2);
644 if (clib_net_to_host_u16 (mss) > sm->mss_clamping)
647 ip_csum_update (*sum, mss, sm->mss_value_net, ip4_header_t,
649 clib_memcpy_fast (data + 2, &sm->mss_value_net, 2);
657 * @brief Check if packet should be translated
659 * Packets aimed at outside interface and external address with active session
660 * should be translated.
663 * @param rt NAT runtime data
664 * @param sw_if_index0 index of the inside interface
665 * @param ip0 IPv4 header
666 * @param proto0 NAT protocol
667 * @param rx_fib_index0 RX FIB index
669 * @returns 0 if packet should be translated otherwise 1
672 snat_not_translate_fast (snat_main_t * sm, vlib_node_runtime_t * node,
673 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
679 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
680 nat_outside_fib_t *outside_fib;
682 .fp_proto = FIB_PROTOCOL_IP4,
685 .ip4.as_u32 = ip0->dst_address.as_u32,
690 /* Don't NAT packet aimed at the intfc address */
691 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
692 ip0->dst_address.as_u32)))
695 fei = fib_table_lookup (rx_fib_index0, &pfx);
696 if (FIB_NODE_INDEX_INVALID != fei)
698 u32 sw_if_index = fib_entry_get_resolving_interface (fei);
699 if (sw_if_index == ~0)
701 vec_foreach (outside_fib, sm->outside_fibs)
703 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
704 if (FIB_NODE_INDEX_INVALID != fei)
706 sw_if_index = fib_entry_get_resolving_interface (fei);
707 if (sw_if_index != ~0)
712 if (sw_if_index == ~0)
717 pool_foreach (i, sm->interfaces, ({
718 /* NAT packet aimed at outside interface */
719 if ((nat_interface_is_outside (i)) && (sw_if_index == i->sw_if_index))
729 increment_v4_address (ip4_address_t * a)
733 v = clib_net_to_host_u32 (a->as_u32) + 1;
734 a->as_u32 = clib_host_to_net_u32 (v);
737 #endif /* __included_nat_inlines_h__ */
740 * fd.io coding-style-patch-verification: ON
743 * eval: (c-set-style "gnu")