FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
nat66_in2out.c
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /**
16  * @file
17  * @brief NAT66 inside to outside network translation
18  */
19 
20 #include <nat/nat66/nat66.h>
21 #include <vnet/ip/ip6_to_ip4.h>
22 #include <vnet/fib/fib_table.h>
23 
24 typedef struct
25 {
29 
30 static u8 *
31 format_nat66_in2out_trace (u8 * s, va_list * args)
32 {
33  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
34  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
35  nat66_in2out_trace_t *t = va_arg (*args, nat66_in2out_trace_t *);
36 
37  s =
38  format (s, "NAT66-in2out: sw_if_index %d, next index %d", t->sw_if_index,
39  t->next_index);
40 
41  return s;
42 }
43 
44 #define foreach_nat66_in2out_error \
45 _(NO_TRANSLATION, "no translation") \
46 _(UNKNOWN, "unknown")
47 
48 typedef enum
49 {
50 #define _(sym,str) NAT66_IN2OUT_ERROR_##sym,
52 #undef _
55 
56 static char *nat66_in2out_error_strings[] = {
57 #define _(sym,string) string,
59 #undef _
60 };
61 
62 typedef enum
63 {
68 
69 static inline u8
70 nat66_not_translate (u32 rx_fib_index, ip6_address_t ip6_addr)
71 {
76  fib_prefix_t pfx = {
78  .fp_len = 128,
79  .fp_addr = {
80  .ip6 = ip6_addr,
81  },
82  };
83 
84  fei = fib_table_lookup (rx_fib_index, &pfx);
85  if (FIB_NODE_INDEX_INVALID == fei)
86  return 1;
88 
89  if (sw_if_index == ~0)
90  {
91  fei = fib_table_lookup (nm->outside_fib_index, &pfx);
92  if (FIB_NODE_INDEX_INVALID == fei)
93  return 1;
95  }
96 
97  /* *INDENT-OFF* */
99  {
100  /* NAT packet aimed at outside interface */
101  if (nat66_interface_is_outside (i) && sw_if_index == i->sw_if_index)
102  return 0;
103  }
104  /* *INDENT-ON* */
105 
106  return 1;
107 }
108 
112 {
113  u32 n_left_from, *from, *to_next;
117 
119  n_left_from = frame->n_vectors;
120  next_index = node->cached_next_index;
121 
122  while (n_left_from > 0)
123  {
124  u32 n_left_to_next;
125 
126  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
127 
128  while (n_left_from > 0 && n_left_to_next > 0)
129  {
130  u32 bi0;
131  vlib_buffer_t *b0;
133  ip6_header_t *ip60;
134  u16 l4_offset0, frag_offset0;
135  u8 l4_protocol0;
137  u32 sw_if_index0, fib_index0;
138  udp_header_t *udp0;
139  tcp_header_t *tcp0;
140  icmp46_header_t *icmp0;
141  u16 *checksum0 = 0;
142  ip_csum_t csum0;
143 
144  /* speculatively enqueue b0 to the current next frame */
145  bi0 = from[0];
146  to_next[0] = bi0;
147  from += 1;
148  to_next += 1;
149  n_left_from -= 1;
150  n_left_to_next -= 1;
151 
152  b0 = vlib_get_buffer (vm, bi0);
153  ip60 = vlib_buffer_get_current (b0);
154 
155  if (PREDICT_FALSE
156  (ip6_parse
157  (vm, b0, ip60, b0->current_length, &l4_protocol0, &l4_offset0,
158  &frag_offset0)))
159  {
160  next0 = NAT66_IN2OUT_NEXT_DROP;
161  b0->error = node->errors[NAT66_IN2OUT_ERROR_UNKNOWN];
162  goto trace0;
163  }
164 
165  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
166  fib_index0 =
168  sw_if_index0);
169 
170  if (nat66_not_translate (fib_index0, ip60->dst_address))
171  goto trace0;
172 
173  sm0 = nat66_static_mapping_get (&ip60->src_address, fib_index0, 1);
174  if (PREDICT_FALSE (!sm0))
175  {
176  goto trace0;
177  }
178 
179  if (l4_protocol0 == IP_PROTOCOL_UDP)
180  {
181  udp0 = (udp_header_t *) u8_ptr_add (ip60, l4_offset0);
182  checksum0 = &udp0->checksum;
183  }
184  else if (l4_protocol0 == IP_PROTOCOL_TCP)
185  {
186  tcp0 = (tcp_header_t *) u8_ptr_add (ip60, l4_offset0);
187  checksum0 = &tcp0->checksum;
188  }
189  else if (l4_protocol0 == IP_PROTOCOL_ICMP6)
190  {
191  icmp0 = (icmp46_header_t *) u8_ptr_add (ip60, l4_offset0);
192  checksum0 = &icmp0->checksum;
193  }
194  else
195  goto skip_csum0;
196 
197  csum0 = ip_csum_sub_even (*checksum0, ip60->src_address.as_u64[0]);
198  csum0 = ip_csum_sub_even (csum0, ip60->src_address.as_u64[1]);
199  csum0 = ip_csum_add_even (csum0, sm0->e_addr.as_u64[0]);
200  csum0 = ip_csum_add_even (csum0, sm0->e_addr.as_u64[1]);
201  *checksum0 = ip_csum_fold (csum0);
202 
203  skip_csum0:
204  ip60->src_address.as_u64[0] = sm0->e_addr.as_u64[0];
205  ip60->src_address.as_u64[1] = sm0->e_addr.as_u64[1];
206 
207  vlib_increment_combined_counter (&nm->session_counters,
208  thread_index, sm0 - nm->sm, 1,
210  b0));
211 
212  trace0:
213  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
214  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
215  {
217  vlib_add_trace (vm, node, b0, sizeof (*t));
218  t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
219  t->next_index = next0;
220  }
221 
222  if (next0 != NAT66_IN2OUT_NEXT_DROP)
223  {
224  vlib_increment_simple_counter (&nm->in2out_packets,
225  thread_index, sw_if_index0, 1);
226  }
227 
228  /* verify speculative enqueue, maybe switch current next frame */
230  n_left_to_next, bi0, next0);
231  }
232  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
233  }
234 
235  return frame->n_vectors;
236 }
237 
238 /* *INDENT-OFF* */
240  .name = "nat66-in2out",
241  .vector_size = sizeof (u32),
242  .format_trace = format_nat66_in2out_trace,
245  .error_strings = nat66_in2out_error_strings,
246  .n_next_nodes = NAT66_IN2OUT_N_NEXT,
247  /* edit / add dispositions here */
248  .next_nodes = {
249  [NAT66_IN2OUT_NEXT_DROP] = "error-drop",
250  [NAT66_IN2OUT_NEXT_IP6_LOOKUP] = "ip6-lookup",
251  },
252 };
253 /* *INDENT-ON* */
254 
255 /*
256  * fd.io coding-style-patch-verification: ON
257  *
258  * Local Variables:
259  * eval: (c-set-style "gnu")
260  * End:
261  */
nat66_static_mapping_get
nat66_static_mapping_t * nat66_static_mapping_get(ip6_address_t *addr, u32 fib_index, u8 is_local)
Definition: nat66.c:215
thread_index
u32 thread_index
Definition: nat44_ei_hairpinning.c:495
nat66_static_mapping_t::e_addr
ip6_address_t e_addr
Definition: nat66.h:29
u8_ptr_add
#define u8_ptr_add(ptr, index)
Definition: ip_types.h:56
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
next_index
nat44_ei_hairpin_src_next_t next_index
Definition: nat44_ei_hairpinning.c:412
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
tcp_header_t
struct _tcp_header tcp_header_t
nat44_ei_main_s::interfaces
nat44_ei_interface_t * interfaces
Definition: nat44_ei.h:340
VLIB_NODE_TYPE_INTERNAL
@ VLIB_NODE_TYPE_INTERNAL
Definition: node.h:72
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
FIB_NODE_INDEX_INVALID
#define FIB_NODE_INDEX_INVALID
Definition: fib_types.h:30
fib_table.h
nat66_in2out_error_t
nat66_in2out_error_t
Definition: nat66_in2out.c:48
u16
unsigned short u16
Definition: types.h:57
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
VLIB_RX
@ VLIB_RX
Definition: defs.h:46
fib_table_lookup
fib_node_index_t fib_table_lookup(u32 fib_index, const fib_prefix_t *prefix)
Perfom a longest prefix match in the non-forwarding table.
Definition: fib_table.c:68
vlib_frame_t
Definition: node.h:372
vlib_buffer_length_in_chain
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:433
udp_header_t
Definition: udp_packet.h:45
nat66_interface_is_outside
#define nat66_interface_is_outside(i)
Definition: nat66.h:56
NAT66_IN2OUT_N_NEXT
@ NAT66_IN2OUT_N_NEXT
Definition: nat66_in2out.c:66
nat66_in2out_next_t
nat66_in2out_next_t
Definition: nat66_in2out.c:62
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
vlib_increment_simple_counter
static void vlib_increment_simple_counter(vlib_simple_counter_main_t *cm, u32 thread_index, u32 index, u64 increment)
Increment a simple counter.
Definition: counter.h:74
ip6_parse
static_always_inline int ip6_parse(vlib_main_t *vm, vlib_buffer_t *b, const ip6_header_t *ip6, u32 buff_len, u8 *l4_protocol, u16 *l4_offset, u16 *frag_hdr_offset)
Parse some useful information from IPv6 header.
Definition: ip6_to_ip4.h:65
vlib_buffer_t::error
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
VLIB_NODE_FN
#define VLIB_NODE_FN(node)
Definition: node.h:202
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:441
VLIB_NODE_FLAG_TRACE
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:291
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
nat66_in2out_node
vlib_node_registration_t nat66_in2out_node
(constructor) VLIB_REGISTER_NODE (nat66_in2out_node)
Definition: nat66_in2out.c:239
fib_node_index_t
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:29
nat44_ei_main_s::outside_fib_index
u32 outside_fib_index
Definition: nat44_ei.h:354
vlib_main_t::thread_index
u32 thread_index
Definition: main.h:215
nat66_in2out_trace_t
Definition: nat66_in2out.c:24
ip6_header_t::dst_address
ip6_address_t dst_address
Definition: ip6_packet.h:310
nat66_main_t
Definition: nat66.h:58
ip6_to_ip4.h
IPv6 to IPv4 translation.
fib_entry_get_resolving_interface
u32 fib_entry_get_resolving_interface(fib_node_index_t entry_index)
Definition: fib_entry.c:1474
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
vlib_buffer_t::current_length
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:122
nat66_in2out_error_strings
static char * nat66_in2out_error_strings[]
Definition: nat66_in2out.c:56
udp_header_t::checksum
u16 checksum
Definition: udp_packet.h:55
nat66_not_translate
static u8 nat66_not_translate(u32 rx_fib_index, ip6_address_t ip6_addr)
Definition: nat66_in2out.c:70
ip_csum_sub_even
static ip_csum_t ip_csum_sub_even(ip_csum_t c, ip_csum_t x)
Definition: ip_packet.h:273
vlib_validate_buffer_enqueue_x1
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:224
NAT66_IN2OUT_NEXT_DROP
@ NAT66_IN2OUT_NEXT_DROP
Definition: nat66_in2out.c:65
format
description fragment has unexpected format
Definition: map.api:433
vlib_put_next_frame
vlib_put_next_frame(vm, node, next_index, 0)
fib_table_get_index_for_sw_if_index
u32 fib_table_get_index_for_sw_if_index(fib_protocol_t proto, u32 sw_if_index)
Get the index of the FIB bound to the interface.
Definition: fib_table.c:1003
u32
unsigned int u32
Definition: types.h:88
FIB_PROTOCOL_IP6
@ FIB_PROTOCOL_IP6
Definition: fib_types.h:37
nat66_main
nat66_main_t nat66_main
Definition: nat66.c:25
foreach_nat66_in2out_error
#define foreach_nat66_in2out_error
Definition: nat66_in2out.c:44
nm
nat44_ei_main_t * nm
Definition: nat44_ei_hairpinning.c:413
fib_prefix_t_::fp_proto
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:211
ip6_header_t
Definition: ip6_packet.h:294
nat66_in2out_trace_t::sw_if_index
u32 sw_if_index
Definition: nat66_in2out.c:26
ip6_header_t::src_address
ip6_address_t src_address
Definition: ip6_packet.h:310
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
u8
unsigned char u8
Definition: types.h:56
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
ip_csum_t
uword ip_csum_t
Definition: ip_packet.h:245
NAT66_IN2OUT_NEXT_IP6_LOOKUP
@ NAT66_IN2OUT_NEXT_IP6_LOOKUP
Definition: nat66_in2out.c:64
i
int i
Definition: flowhash_template.h:376
nat66_interface_t
Definition: nat66.h:47
nat66.h
NAT66 global declarations.
vlib_node_runtime_t
Definition: node.h:454
nat66_in2out_trace_t::next_index
u32 next_index
Definition: nat66_in2out.c:27
from
from
Definition: nat44_ei_hairpinning.c:415
ip_csum_add_even
static ip_csum_t ip_csum_add_even(ip_csum_t c, ip_csum_t x)
Definition: ip_packet.h:256
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
vlib_get_next_frame
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:395
format_nat66_in2out_trace
static u8 * format_nat66_in2out_trace(u8 *s, va_list *args)
Definition: nat66_in2out.c:31
ip_csum_fold
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:301
nat66_static_mapping_t
Definition: nat66.h:26
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
fib_prefix_t_
Aggregate type for a prefix.
Definition: fib_types.h:202
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vlib_increment_combined_counter
vlib_increment_combined_counter(ccm, ti, sw_if_index, n_buffers, n_bytes)
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
NAT66_IN2OUT_N_ERROR
@ NAT66_IN2OUT_N_ERROR
Definition: nat66_in2out.c:53
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169