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 vnet_buffer2 (b0)->nat.arc_next = arc_next0;
83 vnet_buffer2 (b1)->nat.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;
92 t->arc_next_index = arc_next0;
94 if (b1->flags & VLIB_BUFFER_IS_TRACED)
97 vlib_add_trace (vm, node, b0, sizeof (*t));
98 t->next_index = next1;
99 t->arc_next_index = arc_next1;
103 /* verify speculative enqueues, maybe switch current next frame */
104 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
105 to_next, n_left_to_next,
106 bi0, bi1, next0, next1);
109 while (n_left_from > 0 && n_left_to_next > 0)
116 /* speculatively enqueue b0 to the current next frame */
124 b0 = vlib_get_buffer (vm, bi0);
126 vnet_feature_next (&arc_next0, b0);
127 vnet_buffer2 (b0)->nat.arc_next = arc_next0;
129 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
130 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
132 nat_pre_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
133 t->next_index = next0;
134 t->arc_next_index = arc_next0;
137 /* verify speculative enqueue, maybe switch current next frame */
138 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
139 to_next, n_left_to_next,
143 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
146 return frame->n_vectors;
150 is_interface_addr (snat_main_t * sm, vlib_node_runtime_t * node,
151 u32 sw_if_index0, u32 ip4_addr)
153 snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
154 ip4_address_t *first_int_addr;
156 if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
159 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
160 0 /* just want the address */ );
161 rt->cached_sw_if_index = sw_if_index0;
163 rt->cached_ip4_address = first_int_addr->as_u32;
165 rt->cached_ip4_address = 0;
168 if (PREDICT_FALSE (ip4_addr == rt->cached_ip4_address))
175 maximum_sessions_exceeded (snat_main_t * sm, u32 thread_index)
177 if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
178 sm->max_translations)
185 nat_send_all_to_node (vlib_main_t * vm, u32 * bi_vector,
186 vlib_node_runtime_t * node, vlib_error_t * error,
189 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
192 n_left_from = vec_len (bi_vector);
193 next_index = node->cached_next_index;
194 while (n_left_from > 0)
196 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
197 while (n_left_from > 0 && n_left_to_next > 0)
199 u32 bi0 = to_next[0] = from[0];
204 vlib_buffer_t *p0 = vlib_get_buffer (vm, bi0);
207 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
208 n_left_to_next, bi0, next);
210 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
215 user_session_increment (snat_main_t * sm, snat_user_t * u, u8 is_static)
217 if (u->nsessions + u->nstaticsessions < sm->max_translations_per_user)
220 u->nstaticsessions++;
227 nat44_delete_user_with_no_session (snat_main_t * sm, snat_user_t * u,
230 clib_bihash_kv_8_8_t kv;
231 snat_user_key_t u_key;
232 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
235 if (u->nstaticsessions == 0 && u->nsessions == 0)
237 u_key.addr.as_u32 = u->addr.as_u32;
238 u_key.fib_index = u->fib_index;
239 kv.key = u_key.as_u64;
240 pool_put_index (tsm->list_pool, u->sessions_per_user_list_head_index);
241 pool_put (tsm->users, u);
242 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
243 vlib_set_simple_counter (&sm->total_users, thread_index, 0,
244 pool_elts (tsm->users));
249 nat44_delete_session (snat_main_t * sm, snat_session_t * ses,
252 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
254 clib_bihash_kv_8_8_t kv, value;
256 const snat_user_key_t u_key = {
257 .addr = ses->in2out.addr,
258 .fib_index = ses->in2out.fib_index
260 const u8 u_static = snat_is_session_static (ses);
262 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
263 pool_put_index (tsm->list_pool, ses->per_user_index);
264 if (sm->endpoint_dependent)
266 clib_dlist_remove (tsm->lru_pool, ses->lru_index);
267 pool_put_index (tsm->lru_pool, ses->lru_index);
269 pool_put (tsm->sessions, ses);
270 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
271 pool_elts (tsm->sessions));
273 kv.key = u_key.as_u64;
274 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
276 u = pool_elt_at_index (tsm->users, value.value);
278 u->nstaticsessions--;
282 nat44_delete_user_with_no_session (sm, u, thread_index);
286 /** \brief Set TCP session state.
287 @return 1 if session was closed, otherwise 0
290 nat44_set_tcp_session_state_i2o (snat_main_t * sm, f64 now,
291 snat_session_t * ses, vlib_buffer_t * b,
294 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
295 u8 tcp_flags = vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags;
296 u32 tcp_ack_number = vnet_buffer (b)->ip.reass.tcp_ack_number;
297 u32 tcp_seq_number = vnet_buffer (b)->ip.reass.tcp_seq_number;
298 if ((ses->state == 0) && (tcp_flags & TCP_FLAG_RST))
299 ses->state = NAT44_SES_RST;
300 if ((ses->state == NAT44_SES_RST) && !(tcp_flags & TCP_FLAG_RST))
302 if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
303 (ses->state & NAT44_SES_O2I_SYN))
305 if (tcp_flags & TCP_FLAG_SYN)
306 ses->state |= NAT44_SES_I2O_SYN;
307 if (tcp_flags & TCP_FLAG_FIN)
309 ses->i2o_fin_seq = clib_net_to_host_u32 (tcp_seq_number);
310 ses->state |= NAT44_SES_I2O_FIN;
312 if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN))
314 if (clib_net_to_host_u32 (tcp_ack_number) > ses->o2i_fin_seq)
316 ses->state |= NAT44_SES_O2I_FIN_ACK;
317 if (nat44_is_ses_closed (ses))
318 { // if session is now closed, save the timestamp
319 ses->tcp_closed_timestamp = now + sm->tcp_transitory_timeout;
320 ses->last_lru_update = now;
325 // move the session to proper LRU
328 ses->lru_head_index = tsm->tcp_trans_lru_head_index;
332 ses->lru_head_index = tsm->tcp_estab_lru_head_index;
334 clib_dlist_remove (tsm->lru_pool, ses->lru_index);
335 clib_dlist_addtail (tsm->lru_pool, ses->lru_head_index, ses->lru_index);
340 nat44_set_tcp_session_state_o2i (snat_main_t * sm, f64 now,
341 snat_session_t * ses, u8 tcp_flags,
342 u32 tcp_ack_number, u32 tcp_seq_number,
345 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
346 if ((ses->state == 0) && (tcp_flags & TCP_FLAG_RST))
347 ses->state = NAT44_SES_RST;
348 if ((ses->state == NAT44_SES_RST) && !(tcp_flags & TCP_FLAG_RST))
350 if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
351 (ses->state & NAT44_SES_O2I_SYN))
353 if (tcp_flags & TCP_FLAG_SYN)
354 ses->state |= NAT44_SES_O2I_SYN;
355 if (tcp_flags & TCP_FLAG_FIN)
357 ses->o2i_fin_seq = clib_net_to_host_u32 (tcp_seq_number);
358 ses->state |= NAT44_SES_O2I_FIN;
360 if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN))
362 if (clib_net_to_host_u32 (tcp_ack_number) > ses->i2o_fin_seq)
363 ses->state |= NAT44_SES_I2O_FIN_ACK;
364 if (nat44_is_ses_closed (ses))
365 { // if session is now closed, save the timestamp
366 ses->tcp_closed_timestamp = now + sm->tcp_transitory_timeout;
367 ses->last_lru_update = now;
370 // move the session to proper LRU
373 ses->lru_head_index = tsm->tcp_trans_lru_head_index;
377 ses->lru_head_index = tsm->tcp_estab_lru_head_index;
379 clib_dlist_remove (tsm->lru_pool, ses->lru_index);
380 clib_dlist_addtail (tsm->lru_pool, ses->lru_head_index, ses->lru_index);
385 nat44_session_get_timeout (snat_main_t * sm, snat_session_t * s)
387 switch (s->in2out.protocol)
389 case NAT_PROTOCOL_ICMP:
390 return sm->icmp_timeout;
391 case NAT_PROTOCOL_UDP:
392 return sm->udp_timeout;
393 case NAT_PROTOCOL_TCP:
396 return sm->tcp_transitory_timeout;
398 return sm->tcp_established_timeout;
401 return sm->udp_timeout;
408 nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes,
413 s->total_bytes += bytes;
414 nat_ha_sref (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
415 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
416 s->total_pkts, s->total_bytes, thread_index,
417 &s->ha_last_refreshed, now);
420 /** \brief Per-user LRU list maintenance */
422 nat44_session_update_lru (snat_main_t * sm, snat_session_t * s,
425 /* don't update too often - timeout is in magnitude of seconds anyway */
426 if (s->last_heard > s->last_lru_update + 1)
428 if (!sm->endpoint_dependent)
430 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
432 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
433 s->per_user_list_head_index, s->per_user_index);
437 clib_dlist_remove (sm->per_thread_data[thread_index].lru_pool,
439 clib_dlist_addtail (sm->per_thread_data[thread_index].lru_pool,
440 s->lru_head_index, s->lru_index);
442 s->last_lru_update = s->last_heard;
447 make_ed_kv (ip4_address_t * l_addr, ip4_address_t * r_addr, u8 proto,
448 u32 fib_index, u16 l_port, u16 r_port, u32 thread_index,
449 u32 session_index, clib_bihash_kv_16_8_t * kv)
451 kv->key[0] = (u64) r_addr->as_u32 << 32 | l_addr->as_u32;
453 (u64) r_port << 48 | (u64) l_port << 32 | fib_index << 8 | proto;
454 kv->value = (u64) thread_index << 32 | session_index;
458 ed_value_get_thread_index (clib_bihash_kv_16_8_t * value)
460 return value->value >> 32;
464 ed_value_get_session_index (clib_bihash_kv_16_8_t * value)
466 return value->value & ~(u32) 0;
470 split_ed_value (clib_bihash_kv_16_8_t * value, u32 * thread_index,
475 *thread_index = ed_value_get_thread_index (value);
479 *session_index = ed_value_get_session_index (value);
484 split_ed_kv (clib_bihash_kv_16_8_t * kv,
485 ip4_address_t * l_addr, ip4_address_t * r_addr, u8 * proto,
486 u32 * fib_index, u16 * l_port, u16 * r_port)
490 l_addr->as_u32 = kv->key[0] & (u32) ~ 0;
494 r_addr->as_u32 = kv->key[0] >> 32;
498 *r_port = kv->key[1] >> 48;
502 *l_port = (kv->key[1] >> 32) & (u16) ~ 0;
506 *fib_index = (kv->key[1] >> 8) & ((1 << 24) - 1);
510 *proto = kv->key[1] & (u8) ~ 0;
515 make_sm_kv (clib_bihash_kv_8_8_t * kv, ip4_address_t * addr, u8 proto,
516 u32 fib_index, u16 port)
518 kv->key = (u64) fib_index << 51 | (u64) proto << 48 | (u64) port << 32 |
524 static_always_inline int
525 get_icmp_i2o_ed_key (vlib_buffer_t * b, ip4_header_t * ip0, u32 rx_fib_index,
526 u32 thread_index, u32 session_index, u8 * nat_proto,
527 u16 * l_port, u16 * r_port, clib_bihash_kv_16_8_t * kv)
530 u16 _l_port, _r_port;
531 ip4_address_t *l_addr, *r_addr;
533 icmp46_header_t *icmp0;
534 icmp_echo_header_t *echo0, *inner_echo0 = 0;
535 ip4_header_t *inner_ip0 = 0;
537 icmp46_header_t *inner_icmp0;
539 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
540 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
542 if (!icmp_type_is_error_message
543 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
545 proto = IP_PROTOCOL_ICMP;
546 l_addr = &ip0->src_address;
547 r_addr = &ip0->dst_address;
548 _l_port = vnet_buffer (b)->ip.reass.l4_src_port; // TODO should this be src or dst?
553 inner_ip0 = (ip4_header_t *) (echo0 + 1);
554 l4_header = ip4_next_header (inner_ip0);
555 proto = inner_ip0->protocol;
556 r_addr = &inner_ip0->src_address;
557 l_addr = &inner_ip0->dst_address;
558 switch (ip_proto_to_nat_proto (inner_ip0->protocol))
560 case NAT_PROTOCOL_ICMP:
561 inner_icmp0 = (icmp46_header_t *) l4_header;
562 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
564 _l_port = inner_echo0->identifier;
566 case NAT_PROTOCOL_UDP:
567 case NAT_PROTOCOL_TCP:
568 _l_port = ((tcp_udp_header_t *) l4_header)->dst_port;
569 _r_port = ((tcp_udp_header_t *) l4_header)->src_port;
572 return NAT_IN2OUT_ED_ERROR_UNSUPPORTED_PROTOCOL;
575 make_ed_kv (l_addr, r_addr, proto, rx_fib_index, _l_port, _r_port,
576 thread_index, session_index, kv);
579 *nat_proto = ip_proto_to_nat_proto (proto);
593 static_always_inline int
594 get_icmp_o2i_ed_key (vlib_buffer_t * b, ip4_header_t * ip0, u32 rx_fib_index,
595 u32 thread_index, u32 session_index, u8 * nat_proto,
596 u16 * l_port, u16 * r_port, clib_bihash_kv_16_8_t * kv)
598 icmp46_header_t *icmp0;
600 ip4_address_t *l_addr, *r_addr;
601 u16 _l_port, _r_port;
602 icmp_echo_header_t *echo0, *inner_echo0 = 0;
603 ip4_header_t *inner_ip0;
605 icmp46_header_t *inner_icmp0;
607 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
608 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
610 if (!icmp_type_is_error_message
611 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
613 proto = IP_PROTOCOL_ICMP;
614 l_addr = &ip0->dst_address;
615 r_addr = &ip0->src_address;
616 _l_port = vnet_buffer (b)->ip.reass.l4_src_port; // TODO should this be src or dst?
621 inner_ip0 = (ip4_header_t *) (echo0 + 1);
622 l4_header = ip4_next_header (inner_ip0);
623 proto = inner_ip0->protocol;
624 l_addr = &inner_ip0->src_address;
625 r_addr = &inner_ip0->dst_address;
626 switch (ip_proto_to_nat_proto (inner_ip0->protocol))
628 case NAT_PROTOCOL_ICMP:
629 inner_icmp0 = (icmp46_header_t *) l4_header;
630 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
631 _l_port = inner_echo0->identifier;
634 case NAT_PROTOCOL_UDP:
635 case NAT_PROTOCOL_TCP:
636 _l_port = ((tcp_udp_header_t *) l4_header)->src_port;
637 _r_port = ((tcp_udp_header_t *) l4_header)->dst_port;
643 make_ed_kv (l_addr, r_addr, proto, rx_fib_index, _l_port, _r_port,
644 thread_index, session_index, kv);
647 *nat_proto = ip_proto_to_nat_proto (proto);
661 * @brief Check if packet should be translated
663 * Packets aimed at outside interface and external address with active session
664 * should be translated.
667 * @param rt NAT runtime data
668 * @param sw_if_index0 index of the inside interface
669 * @param ip0 IPv4 header
670 * @param proto0 NAT protocol
671 * @param rx_fib_index0 RX FIB index
673 * @returns 0 if packet should be translated otherwise 1
676 snat_not_translate_fast (snat_main_t * sm, vlib_node_runtime_t * node,
677 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
683 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
684 nat_outside_fib_t *outside_fib;
686 .fp_proto = FIB_PROTOCOL_IP4,
689 .ip4.as_u32 = ip0->dst_address.as_u32,
694 /* Don't NAT packet aimed at the intfc address */
695 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
696 ip0->dst_address.as_u32)))
699 fei = fib_table_lookup (rx_fib_index0, &pfx);
700 if (FIB_NODE_INDEX_INVALID != fei)
702 u32 sw_if_index = fib_entry_get_resolving_interface (fei);
703 if (sw_if_index == ~0)
705 vec_foreach (outside_fib, sm->outside_fibs)
707 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
708 if (FIB_NODE_INDEX_INVALID != fei)
710 sw_if_index = fib_entry_get_resolving_interface (fei);
711 if (sw_if_index != ~0)
716 if (sw_if_index == ~0)
721 pool_foreach (i, sm->interfaces, ({
722 /* NAT packet aimed at outside interface */
723 if ((nat_interface_is_outside (i)) && (sw_if_index == i->sw_if_index))
733 increment_v4_address (ip4_address_t * a)
737 v = clib_net_to_host_u32 (a->as_u32) + 1;
738 a->as_u32 = clib_host_to_net_u32 (v);
741 #endif /* __included_nat_inlines_h__ */
744 * fd.io coding-style-patch-verification: ON
747 * eval: (c-set-style "gnu")