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 calc_nat_key (ip4_address_t addr, u16 port, u32 fib_index, u8 proto)
29 ASSERT (fib_index <= (1 << 14) - 1);
30 ASSERT (proto <= (1 << 3) - 1);
31 return (u64) addr.as_u32 << 32 | (u64) port << 16 | fib_index << 3 |
36 split_nat_key (u64 key, ip4_address_t * addr, u16 * port,
37 u32 * fib_index, nat_protocol_t * proto)
41 addr->as_u32 = key >> 32;
45 *port = (key >> 16) & (u16) ~ 0;
49 *fib_index = key >> 3 & ((1 << 13) - 1);
58 init_nat_k (clib_bihash_kv_8_8_t * kv, ip4_address_t addr, u16 port,
59 u32 fib_index, nat_protocol_t proto)
61 kv->key = calc_nat_key (addr, port, fib_index, proto);
66 init_nat_kv (clib_bihash_kv_8_8_t * kv, ip4_address_t addr, u16 port,
67 u32 fib_index, nat_protocol_t proto, u64 value)
69 init_nat_k (kv, addr, port, fib_index, proto);
74 init_nat_i2o_k (clib_bihash_kv_8_8_t * kv, snat_session_t * s)
76 return init_nat_k (kv, s->in2out.addr, s->in2out.port, s->in2out.fib_index,
81 init_nat_i2o_kv (clib_bihash_kv_8_8_t * kv, snat_session_t * s, u64 value)
83 init_nat_k (kv, s->in2out.addr, s->in2out.port, s->in2out.fib_index,
89 init_nat_o2i_k (clib_bihash_kv_8_8_t * kv, snat_session_t * s)
91 return init_nat_k (kv, s->out2in.addr, s->out2in.port, s->out2in.fib_index,
96 init_nat_o2i_kv (clib_bihash_kv_8_8_t * kv, snat_session_t * s, u64 value)
98 init_nat_k (kv, s->out2in.addr, s->out2in.port, s->out2in.fib_index,
104 nat_pre_node_fn_inline (vlib_main_t * vm,
105 vlib_node_runtime_t * node,
106 vlib_frame_t * frame, u32 def_next)
108 u32 n_left_from, *from;
110 from = vlib_frame_vector_args (frame);
111 n_left_from = frame->n_vectors;
113 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
114 u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
115 vlib_get_buffers (vm, from, b, n_left_from);
117 while (n_left_from >= 2)
120 u32 arc_next0, arc_next1;
121 vlib_buffer_t *b0, *b1;
128 /* Prefetch next iteration. */
129 if (PREDICT_TRUE (n_left_from >= 4))
131 vlib_buffer_t *p2, *p3;
136 vlib_prefetch_buffer_header (p2, LOAD);
137 vlib_prefetch_buffer_header (p3, LOAD);
139 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
140 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
146 vnet_feature_next (&arc_next0, b0);
147 vnet_feature_next (&arc_next1, b1);
149 vnet_buffer2 (b0)->nat.arc_next = arc_next0;
150 vnet_buffer2 (b1)->nat.arc_next = arc_next1;
152 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
154 if (b0->flags & VLIB_BUFFER_IS_TRACED)
156 nat_pre_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
157 t->next_index = next0;
158 t->arc_next_index = arc_next0;
160 if (b1->flags & VLIB_BUFFER_IS_TRACED)
162 nat_pre_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
163 t->next_index = next1;
164 t->arc_next_index = arc_next1;
174 while (n_left_from > 0)
184 vnet_feature_next (&arc_next0, b0);
185 vnet_buffer2 (b0)->nat.arc_next = arc_next0;
187 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
188 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
190 nat_pre_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
191 t->next_index = next0;
192 t->arc_next_index = arc_next0;
199 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
202 return frame->n_vectors;
206 is_interface_addr (snat_main_t * sm, vlib_node_runtime_t * node,
207 u32 sw_if_index0, u32 ip4_addr)
209 snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
210 ip4_address_t *first_int_addr;
212 if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
215 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
216 0 /* just want the address */ );
217 rt->cached_sw_if_index = sw_if_index0;
219 rt->cached_ip4_address = first_int_addr->as_u32;
221 rt->cached_ip4_address = 0;
224 if (PREDICT_FALSE (ip4_addr == rt->cached_ip4_address))
231 maximum_sessions_exceeded (snat_main_t * sm, u32 thread_index)
233 if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
234 sm->max_translations_per_thread)
241 user_session_increment (snat_main_t * sm, snat_user_t * u, u8 is_static)
243 if (u->nsessions + u->nstaticsessions < sm->max_translations_per_user)
246 u->nstaticsessions++;
253 nat44_delete_user_with_no_session (snat_main_t * sm, snat_user_t * u,
256 clib_bihash_kv_8_8_t kv;
257 snat_user_key_t u_key;
258 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
261 if (u->nstaticsessions == 0 && u->nsessions == 0)
263 u_key.addr.as_u32 = u->addr.as_u32;
264 u_key.fib_index = u->fib_index;
265 kv.key = u_key.as_u64;
266 pool_put_index (tsm->list_pool, u->sessions_per_user_list_head_index);
267 pool_put (tsm->users, u);
268 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
269 vlib_set_simple_counter (&sm->total_users, thread_index, 0,
270 pool_elts (tsm->users));
275 nat44_delete_session (snat_main_t * sm, snat_session_t * ses,
278 snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
280 clib_bihash_kv_8_8_t kv, value;
282 const snat_user_key_t u_key = {
283 .addr = ses->in2out.addr,
284 .fib_index = ses->in2out.fib_index
286 const u8 u_static = snat_is_session_static (ses);
288 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
289 pool_put_index (tsm->list_pool, ses->per_user_index);
290 if (sm->endpoint_dependent)
292 clib_dlist_remove (tsm->lru_pool, ses->lru_index);
293 pool_put_index (tsm->lru_pool, ses->lru_index);
295 pool_put (tsm->sessions, ses);
296 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
297 pool_elts (tsm->sessions));
299 kv.key = u_key.as_u64;
300 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
302 u = pool_elt_at_index (tsm->users, value.value);
304 u->nstaticsessions--;
308 nat44_delete_user_with_no_session (sm, u, thread_index);
312 /** \brief Set TCP session state.
313 @return 1 if session was closed, otherwise 0
316 nat44_set_tcp_session_state_i2o (snat_main_t * sm, f64 now,
317 snat_session_t * ses, vlib_buffer_t * b,
320 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
321 u8 tcp_flags = vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags;
322 u32 tcp_ack_number = vnet_buffer (b)->ip.reass.tcp_ack_number;
323 u32 tcp_seq_number = vnet_buffer (b)->ip.reass.tcp_seq_number;
324 if ((ses->state == 0) && (tcp_flags & TCP_FLAG_RST))
325 ses->state = NAT44_SES_RST;
326 if ((ses->state == NAT44_SES_RST) && !(tcp_flags & TCP_FLAG_RST))
328 if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
329 (ses->state & NAT44_SES_O2I_SYN))
331 if (tcp_flags & TCP_FLAG_SYN)
332 ses->state |= NAT44_SES_I2O_SYN;
333 if (tcp_flags & TCP_FLAG_FIN)
335 ses->i2o_fin_seq = clib_net_to_host_u32 (tcp_seq_number);
336 ses->state |= NAT44_SES_I2O_FIN;
338 if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_O2I_FIN))
340 if (clib_net_to_host_u32 (tcp_ack_number) > ses->o2i_fin_seq)
342 ses->state |= NAT44_SES_O2I_FIN_ACK;
343 if (nat44_is_ses_closed (ses))
344 { // if session is now closed, save the timestamp
345 ses->tcp_closed_timestamp = now + sm->tcp_transitory_timeout;
346 ses->last_lru_update = now;
351 // move the session to proper LRU
354 ses->lru_head_index = tsm->tcp_trans_lru_head_index;
358 ses->lru_head_index = tsm->tcp_estab_lru_head_index;
360 clib_dlist_remove (tsm->lru_pool, ses->lru_index);
361 clib_dlist_addtail (tsm->lru_pool, ses->lru_head_index, ses->lru_index);
366 nat44_set_tcp_session_state_o2i (snat_main_t * sm, f64 now,
367 snat_session_t * ses, u8 tcp_flags,
368 u32 tcp_ack_number, u32 tcp_seq_number,
371 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
372 if ((ses->state == 0) && (tcp_flags & TCP_FLAG_RST))
373 ses->state = NAT44_SES_RST;
374 if ((ses->state == NAT44_SES_RST) && !(tcp_flags & TCP_FLAG_RST))
376 if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_SYN) &&
377 (ses->state & NAT44_SES_O2I_SYN))
379 if (tcp_flags & TCP_FLAG_SYN)
380 ses->state |= NAT44_SES_O2I_SYN;
381 if (tcp_flags & TCP_FLAG_FIN)
383 ses->o2i_fin_seq = clib_net_to_host_u32 (tcp_seq_number);
384 ses->state |= NAT44_SES_O2I_FIN;
386 if ((tcp_flags & TCP_FLAG_ACK) && (ses->state & NAT44_SES_I2O_FIN))
388 if (clib_net_to_host_u32 (tcp_ack_number) > ses->i2o_fin_seq)
389 ses->state |= NAT44_SES_I2O_FIN_ACK;
390 if (nat44_is_ses_closed (ses))
391 { // if session is now closed, save the timestamp
392 ses->tcp_closed_timestamp = now + sm->tcp_transitory_timeout;
393 ses->last_lru_update = now;
396 // move the session to proper LRU
399 ses->lru_head_index = tsm->tcp_trans_lru_head_index;
403 ses->lru_head_index = tsm->tcp_estab_lru_head_index;
405 clib_dlist_remove (tsm->lru_pool, ses->lru_index);
406 clib_dlist_addtail (tsm->lru_pool, ses->lru_head_index, ses->lru_index);
411 nat44_session_get_timeout (snat_main_t * sm, snat_session_t * s)
413 switch (s->nat_proto)
415 case NAT_PROTOCOL_ICMP:
416 return sm->icmp_timeout;
417 case NAT_PROTOCOL_UDP:
418 return sm->udp_timeout;
419 case NAT_PROTOCOL_TCP:
422 return sm->tcp_transitory_timeout;
424 return sm->tcp_established_timeout;
427 return sm->udp_timeout;
434 nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes,
439 s->total_bytes += bytes;
440 nat_ha_sref (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
441 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
442 s->total_pkts, s->total_bytes, thread_index,
443 &s->ha_last_refreshed, now);
446 /** \brief Per-user LRU list maintenance */
448 nat44_session_update_lru (snat_main_t * sm, snat_session_t * s,
451 /* don't update too often - timeout is in magnitude of seconds anyway */
452 if (s->last_heard > s->last_lru_update + 1)
454 if (!sm->endpoint_dependent)
456 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
458 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
459 s->per_user_list_head_index, s->per_user_index);
463 clib_dlist_remove (sm->per_thread_data[thread_index].lru_pool,
465 clib_dlist_addtail (sm->per_thread_data[thread_index].lru_pool,
466 s->lru_head_index, s->lru_index);
468 s->last_lru_update = s->last_heard;
473 init_ed_k (clib_bihash_kv_16_8_t * kv, ip4_address_t l_addr, u16 l_port,
474 ip4_address_t r_addr, u16 r_port, u32 fib_index, u8 proto)
476 kv->key[0] = (u64) r_addr.as_u32 << 32 | l_addr.as_u32;
478 (u64) r_port << 48 | (u64) l_port << 32 | fib_index << 8 | proto;
482 init_ed_kv (clib_bihash_kv_16_8_t * kv, ip4_address_t l_addr, u16 l_port,
483 ip4_address_t r_addr, u16 r_port, u32 fib_index, u8 proto,
484 u32 thread_index, u32 session_index)
486 init_ed_k (kv, l_addr, l_port, r_addr, r_port, fib_index, proto);
487 kv->value = (u64) thread_index << 32 | session_index;
491 ed_value_get_thread_index (clib_bihash_kv_16_8_t * value)
493 return value->value >> 32;
497 ed_value_get_session_index (clib_bihash_kv_16_8_t * value)
499 return value->value & ~(u32) 0;
503 split_ed_value (clib_bihash_kv_16_8_t * value, u32 * thread_index,
508 *thread_index = ed_value_get_thread_index (value);
512 *session_index = ed_value_get_session_index (value);
517 split_ed_kv (clib_bihash_kv_16_8_t * kv,
518 ip4_address_t * l_addr, ip4_address_t * r_addr, u8 * proto,
519 u32 * fib_index, u16 * l_port, u16 * r_port)
523 l_addr->as_u32 = kv->key[0] & (u32) ~ 0;
527 r_addr->as_u32 = kv->key[0] >> 32;
531 *r_port = kv->key[1] >> 48;
535 *l_port = (kv->key[1] >> 32) & (u16) ~ 0;
539 *fib_index = (kv->key[1] >> 8) & ((1 << 24) - 1);
543 *proto = kv->key[1] & (u8) ~ 0;
547 static_always_inline int
548 get_icmp_i2o_ed_key (vlib_buffer_t * b, ip4_header_t * ip0, u32 rx_fib_index,
549 u32 thread_index, u32 session_index,
550 nat_protocol_t * nat_proto, u16 * l_port, u16 * r_port,
551 clib_bihash_kv_16_8_t * kv)
554 u16 _l_port, _r_port;
555 ip4_address_t *l_addr, *r_addr;
557 icmp46_header_t *icmp0;
558 icmp_echo_header_t *echo0, *inner_echo0 = 0;
559 ip4_header_t *inner_ip0 = 0;
561 icmp46_header_t *inner_icmp0;
563 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
564 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
566 if (!icmp_type_is_error_message
567 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
569 proto = IP_PROTOCOL_ICMP;
570 l_addr = &ip0->src_address;
571 r_addr = &ip0->dst_address;
572 _l_port = vnet_buffer (b)->ip.reass.l4_src_port;
577 inner_ip0 = (ip4_header_t *) (echo0 + 1);
578 l4_header = ip4_next_header (inner_ip0);
579 proto = inner_ip0->protocol;
580 r_addr = &inner_ip0->src_address;
581 l_addr = &inner_ip0->dst_address;
582 switch (ip_proto_to_nat_proto (inner_ip0->protocol))
584 case NAT_PROTOCOL_ICMP:
585 inner_icmp0 = (icmp46_header_t *) l4_header;
586 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
588 _l_port = inner_echo0->identifier;
590 case NAT_PROTOCOL_UDP:
591 case NAT_PROTOCOL_TCP:
592 _l_port = ((tcp_udp_header_t *) l4_header)->dst_port;
593 _r_port = ((tcp_udp_header_t *) l4_header)->src_port;
596 return NAT_IN2OUT_ED_ERROR_UNSUPPORTED_PROTOCOL;
599 init_ed_kv (kv, *l_addr, _l_port, *r_addr, _r_port, rx_fib_index, proto,
600 thread_index, session_index);
603 *nat_proto = ip_proto_to_nat_proto (proto);
616 static_always_inline int
617 get_icmp_o2i_ed_key (vlib_buffer_t * b, ip4_header_t * ip0, u32 rx_fib_index,
618 u32 thread_index, u32 session_index,
619 nat_protocol_t * nat_proto, u16 * l_port, u16 * r_port,
620 clib_bihash_kv_16_8_t * kv)
622 icmp46_header_t *icmp0;
624 ip4_address_t *l_addr, *r_addr;
625 u16 _l_port, _r_port;
626 icmp_echo_header_t *echo0, *inner_echo0 = 0;
627 ip4_header_t *inner_ip0;
629 icmp46_header_t *inner_icmp0;
631 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
632 echo0 = (icmp_echo_header_t *) (icmp0 + 1);
634 if (!icmp_type_is_error_message
635 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
637 proto = IP_PROTOCOL_ICMP;
638 l_addr = &ip0->dst_address;
639 r_addr = &ip0->src_address;
640 _l_port = vnet_buffer (b)->ip.reass.l4_src_port;
645 inner_ip0 = (ip4_header_t *) (echo0 + 1);
646 l4_header = ip4_next_header (inner_ip0);
647 proto = inner_ip0->protocol;
648 l_addr = &inner_ip0->src_address;
649 r_addr = &inner_ip0->dst_address;
650 switch (ip_proto_to_nat_proto (inner_ip0->protocol))
652 case NAT_PROTOCOL_ICMP:
653 inner_icmp0 = (icmp46_header_t *) l4_header;
654 inner_echo0 = (icmp_echo_header_t *) (inner_icmp0 + 1);
655 _l_port = inner_echo0->identifier;
658 case NAT_PROTOCOL_UDP:
659 case NAT_PROTOCOL_TCP:
660 _l_port = ((tcp_udp_header_t *) l4_header)->src_port;
661 _r_port = ((tcp_udp_header_t *) l4_header)->dst_port;
667 init_ed_kv (kv, *l_addr, _l_port, *r_addr, _r_port, rx_fib_index, proto,
668 thread_index, session_index);
671 *nat_proto = ip_proto_to_nat_proto (proto);
685 * @brief Check if packet should be translated
687 * Packets aimed at outside interface and external address with active session
688 * should be translated.
691 * @param rt NAT runtime data
692 * @param sw_if_index0 index of the inside interface
693 * @param ip0 IPv4 header
694 * @param proto0 NAT protocol
695 * @param rx_fib_index0 RX FIB index
697 * @returns 0 if packet should be translated otherwise 1
700 snat_not_translate_fast (snat_main_t * sm, vlib_node_runtime_t * node,
701 u32 sw_if_index0, ip4_header_t * ip0, u32 proto0,
707 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
708 nat_outside_fib_t *outside_fib;
710 .fp_proto = FIB_PROTOCOL_IP4,
713 .ip4.as_u32 = ip0->dst_address.as_u32,
718 /* Don't NAT packet aimed at the intfc address */
719 if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index0,
720 ip0->dst_address.as_u32)))
723 fei = fib_table_lookup (rx_fib_index0, &pfx);
724 if (FIB_NODE_INDEX_INVALID != fei)
726 u32 sw_if_index = fib_entry_get_resolving_interface (fei);
727 if (sw_if_index == ~0)
729 vec_foreach (outside_fib, sm->outside_fibs)
731 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
732 if (FIB_NODE_INDEX_INVALID != fei)
734 sw_if_index = fib_entry_get_resolving_interface (fei);
735 if (sw_if_index != ~0)
740 if (sw_if_index == ~0)
745 pool_foreach (i, sm->interfaces, ({
746 /* NAT packet aimed at outside interface */
747 if ((nat_interface_is_outside (i)) && (sw_if_index == i->sw_if_index))
756 static_always_inline u16
757 snat_random_port (u16 min, u16 max)
759 snat_main_t *sm = &snat_main;
763 rwide = random_u32 (&sm->random_seed);
765 if (r >= min && r <= max)
768 return min + (rwide % (max - min + 1));
771 #endif /* __included_nat_inlines_h__ */
774 * fd.io coding-style-patch-verification: ON
777 * eval: (c-set-style "gnu")