FD.io VPP  v19.08.2-294-g37e99c22d
Vector Packet Processing
nat66_out2in.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 outside to inside network translation
18  */
19 
20 #include <nat/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_out2in_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_out2in_trace_t *t = va_arg (*args, nat66_out2in_trace_t *);
36 
37  s =
38  format (s, "NAT66-out2in: 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_out2in_error \
45 _(OUT2IN_PACKETS, "good out2in packets processed") \
46 _(NO_TRANSLATION, "no translation") \
47 _(UNKNOWN, "unknown")
48 
49 typedef enum
50 {
51 #define _(sym,str) NAT66_OUT2IN_ERROR_##sym,
53 #undef _
56 
57 static char *nat66_out2in_error_strings[] = {
58 #define _(sym,string) string,
60 #undef _
61 };
62 
63 typedef enum
64 {
69 
71  vlib_node_runtime_t * node,
72  vlib_frame_t * frame)
73 {
74  u32 n_left_from, *from, *to_next;
75  nat66_out2in_next_t next_index;
76  u32 pkts_processed = 0;
77  u32 thread_index = vm->thread_index;
78  nat66_main_t *nm = &nat66_main;
79 
80  from = vlib_frame_vector_args (frame);
81  n_left_from = frame->n_vectors;
82  next_index = node->cached_next_index;
83 
84  while (n_left_from > 0)
85  {
86  u32 n_left_to_next;
87 
88  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
89 
90  while (n_left_from > 0 && n_left_to_next > 0)
91  {
92  u32 bi0;
93  vlib_buffer_t *b0;
95  ip6_header_t *ip60;
96  u16 l4_offset0, frag_offset0;
97  u8 l4_protocol0;
99  u32 sw_if_index0, fib_index0;
100  udp_header_t *udp0;
101  tcp_header_t *tcp0;
102  icmp46_header_t *icmp0;
103  u16 *checksum0 = 0;
104  ip_csum_t csum0;
105 
106  /* speculatively enqueue b0 to the current next frame */
107  bi0 = from[0];
108  to_next[0] = bi0;
109  from += 1;
110  to_next += 1;
111  n_left_from -= 1;
112  n_left_to_next -= 1;
113 
114  b0 = vlib_get_buffer (vm, bi0);
115  ip60 = vlib_buffer_get_current (b0);
116 
117  if (PREDICT_FALSE
118  (ip6_parse
119  (ip60, b0->current_length, &l4_protocol0, &l4_offset0,
120  &frag_offset0)))
121  {
122  next0 = NAT66_OUT2IN_NEXT_DROP;
123  b0->error = node->errors[NAT66_OUT2IN_ERROR_UNKNOWN];
124  goto trace0;
125  }
126 
127  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
128  fib_index0 =
130  sw_if_index0);
131 
132  sm0 = nat66_static_mapping_get (&ip60->dst_address, fib_index0, 0);
133  if (PREDICT_FALSE (!sm0))
134  {
135  goto trace0;
136  }
137 
138  if (l4_protocol0 == IP_PROTOCOL_UDP)
139  {
140  udp0 = (udp_header_t *) u8_ptr_add (ip60, l4_offset0);
141  checksum0 = &udp0->checksum;
142  }
143  else if (l4_protocol0 == IP_PROTOCOL_TCP)
144  {
145  tcp0 = (tcp_header_t *) u8_ptr_add (ip60, l4_offset0);
146  checksum0 = &tcp0->checksum;
147  }
148  else if (l4_protocol0 == IP_PROTOCOL_ICMP6)
149  {
150  icmp0 = (icmp46_header_t *) u8_ptr_add (ip60, l4_offset0);
151  checksum0 = &icmp0->checksum;
152  }
153  else
154  goto skip_csum0;
155 
156  csum0 = ip_csum_sub_even (*checksum0, ip60->dst_address.as_u64[0]);
157  csum0 = ip_csum_sub_even (csum0, ip60->dst_address.as_u64[1]);
158  csum0 = ip_csum_add_even (csum0, sm0->l_addr.as_u64[0]);
159  csum0 = ip_csum_add_even (csum0, sm0->l_addr.as_u64[1]);
160  *checksum0 = ip_csum_fold (csum0);
161 
162  skip_csum0:
163  ip60->dst_address.as_u64[0] = sm0->l_addr.as_u64[0];
164  ip60->dst_address.as_u64[1] = sm0->l_addr.as_u64[1];
165  vnet_buffer (b0)->sw_if_index[VLIB_TX] = sm0->fib_index;
166 
168  thread_index, sm0 - nm->sm, 1,
170  b0));
171 
172  trace0:
173  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
174  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
175  {
177  vlib_add_trace (vm, node, b0, sizeof (*t));
178  t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
179  t->next_index = next0;
180  }
181 
182  pkts_processed += next0 != NAT66_OUT2IN_NEXT_DROP;
183 
184  /* verify speculative enqueue, maybe switch current next frame */
185  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
186  n_left_to_next, bi0, next0);
187  }
188  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
189  }
190 
192  NAT66_OUT2IN_ERROR_OUT2IN_PACKETS,
193  pkts_processed);
194  return frame->n_vectors;
195 }
196 
197 /* *INDENT-OFF* */
199  .name = "nat66-out2in",
200  .vector_size = sizeof (u32),
201  .format_trace = format_nat66_out2in_trace,
204  .error_strings = nat66_out2in_error_strings,
205  .n_next_nodes = NAT66_OUT2IN_N_NEXT,
206  /* edit / add dispositions here */
207  .next_nodes = {
208  [NAT66_OUT2IN_NEXT_DROP] = "error-drop",
209  [NAT66_OUT2IN_NEXT_IP6_LOOKUP] = "ip6-lookup",
210  },
211 };
212 /* *INDENT-ON* */
213 
214 /*
215  * fd.io coding-style-patch-verification: ON
216  *
217  * Local Variables:
218  * eval: (c-set-style "gnu")
219  * End:
220  */
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
#define CLIB_UNUSED(x)
Definition: clib.h:83
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 thread_index, u32 index, u64 n_packets, u64 n_bytes)
Increment a combined counter.
Definition: counter.h:220
u64 as_u64[2]
Definition: ip6_packet.h:51
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:972
u32 thread_index
Definition: main.h:218
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
uword ip_csum_t
Definition: ip_packet.h:219
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define VLIB_NODE_FN(node)
Definition: node.h:202
u32 out2in_node_index
Definition: nat66.h:60
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:366
struct _tcp_header tcp_header_t
unsigned char u8
Definition: types.h:56
static u8 * format_nat66_out2in_trace(u8 *s, va_list *args)
Definition: nat66_out2in.c:31
nat66_main_t nat66_main
Definition: nat66.c:23
#define u8_ptr_add(ptr, index)
Definition: ip_types.h:34
static_always_inline int ip6_parse(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:59
unsigned int u32
Definition: types.h:88
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:136
static char * nat66_out2in_error_strings[]
Definition: nat66_out2in.c:57
unsigned short u16
Definition: types.h:57
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
#define PREDICT_FALSE(x)
Definition: clib.h:112
#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:218
#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:338
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1150
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
vlib_main_t * vm
Definition: buffer.c:323
#define ARRAY_LEN(x)
Definition: clib.h:63
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:456
static ip_csum_t ip_csum_sub_even(ip_csum_t c, ip_csum_t x)
Definition: ip_packet.h:247
IPv6 to IPv4 translation.
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
ip6_address_t l_addr
Definition: nat66.h:27
Definition: defs.h:47
vlib_combined_counter_main_t session_counters
Session counters.
Definition: nat66.h:57
VLIB buffer representation.
Definition: buffer.h:102
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
#define foreach_nat66_out2in_error
Definition: nat66_out2in.c:44
#define vnet_buffer(b)
Definition: buffer.h:365
nat66_static_mapping_t * sm
Static mapping pool.
Definition: nat66.h:51
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:302
NAT66 global declarations.
nat66_static_mapping_t * nat66_static_mapping_get(ip6_address_t *addr, u32 fib_index, u8 is_local)
Definition: nat66.c:122
nat66_out2in_next_t
Definition: nat66_out2in.c:63
nat66_out2in_error_t
Definition: nat66_out2in.c:49
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
vlib_node_registration_t nat66_out2in_node
(constructor) VLIB_REGISTER_NODE (nat66_out2in_node)
Definition: nat66_out2in.c:198
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:275
Definition: defs.h:46
static ip_csum_t ip_csum_add_even(ip_csum_t c, ip_csum_t x)
Definition: ip_packet.h:230
ip6_address_t dst_address
Definition: ip6_packet.h:383