FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
dslite_out2in.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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 #include <nat/dslite/dslite.h>
16 
17 typedef enum
18 {
24 
25 static char *dslite_out2in_error_strings[] = {
26 #define _(sym,string) string,
28 #undef _
29 };
30 
31 static inline u32
33  dslite_session_t ** sp, u32 next, u8 * error,
35 {
36  dslite_session_t *s = 0;
37  icmp46_header_t *icmp = ip4_next_header (ip4);
40  u32 n = next;
41  echo_header_t *echo;
42  u32 new_addr, old_addr;
43  u16 old_id, new_id;
44  ip_csum_t sum;
45 
46  echo = (echo_header_t *) (icmp + 1);
47 
49  || (icmp->type != ICMP4_echo_reply))
50  {
52  *error = DSLITE_ERROR_BAD_ICMP_TYPE;
53  goto done;
54  }
55 
56  key.addr = ip4->dst_address;
57  key.port = echo->identifier;
58  key.protocol = NAT_PROTOCOL_ICMP;
59  key.fib_index = 0;
60  kv.key = key.as_u64;
61 
62  if (clib_bihash_search_8_8
64  {
66  *error = DSLITE_ERROR_NO_TRANSLATION;
67  goto done;
68  }
69  else
70  {
71  s =
73  value.value);
74  }
75 
76  old_id = echo->identifier;
77  echo->identifier = new_id = s->in2out.port;
78  sum = icmp->checksum;
79  sum = ip_csum_update (sum, old_id, new_id, echo_header_t, identifier);
80  icmp->checksum = ip_csum_fold (sum);
81 
82  old_addr = ip4->dst_address.as_u32;
83  ip4->dst_address = s->in2out.addr;
84  new_addr = ip4->dst_address.as_u32;
85 
86  sum = ip4->checksum;
87  sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
88  ip4->checksum = ip_csum_fold (sum);
89 
90 done:
91  *sp = s;
92  return n;
93 }
94 
98 {
99  u32 n_left_from, *from, *to_next;
101  vlib_node_runtime_t *error_node;
103  f64 now = vlib_time_now (vm);
104  dslite_main_t *dm = &dslite_main;
105 
107 
109  n_left_from = frame->n_vectors;
110  next_index = node->cached_next_index;
111 
112 
113  while (n_left_from > 0)
114  {
115  u32 n_left_to_next;
116 
117  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
118 
119  while (n_left_from > 0 && n_left_to_next > 0)
120  {
121  u32 bi0;
122  vlib_buffer_t *b0;
124  u8 error0 = DSLITE_ERROR_OUT2IN;
125  ip4_header_t *ip40;
126  ip6_header_t *ip60;
127  u32 proto0;
128  udp_header_t *udp0;
129  tcp_header_t *tcp0;
130  clib_bihash_kv_8_8_t kv0, value0;
131  nat_session_key_t key0;
132  dslite_session_t *s0 = 0;
133  ip_csum_t sum0;
134  u32 new_addr0, old_addr0;
135  u16 new_port0, old_port0;
136 
137  /* speculatively enqueue b0 to the current next frame */
138  bi0 = from[0];
139  to_next[0] = bi0;
140  from += 1;
141  to_next += 1;
142  n_left_from -= 1;
143  n_left_to_next -= 1;
144 
145  b0 = vlib_get_buffer (vm, bi0);
146  ip40 = vlib_buffer_get_current (b0);
147  proto0 = ip_proto_to_nat_proto (ip40->protocol);
148 
149  if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
150  {
151  error0 = DSLITE_ERROR_UNSUPPORTED_PROTOCOL;
152  next0 = DSLITE_OUT2IN_NEXT_DROP;
153  goto trace0;
154  }
155 
156  if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_ICMP))
157  {
158  next0 =
159  dslite_icmp_out2in (dm, ip40, &s0, next0, &error0,
160  thread_index);
161  if (PREDICT_FALSE (next0 == DSLITE_OUT2IN_NEXT_DROP))
162  goto trace0;
163 
164  goto encap0;
165  }
166 
167  udp0 = ip4_next_header (ip40);
168  tcp0 = (tcp_header_t *) udp0;
169 
170  key0.addr = ip40->dst_address;
171  key0.port = udp0->dst_port;
172  key0.protocol = proto0;
173  key0.fib_index = 0;
174  kv0.key = key0.as_u64;
175 
176  if (clib_bihash_search_8_8
177  (&dm->per_thread_data[thread_index].out2in, &kv0, &value0))
178  {
179  next0 = DSLITE_OUT2IN_NEXT_DROP;
180  error0 = DSLITE_ERROR_NO_TRANSLATION;
181  goto trace0;
182  }
183  else
184  {
185  s0 =
187  value0.value);
188  }
189 
190  old_addr0 = ip40->dst_address.as_u32;
191  ip40->dst_address = s0->in2out.addr;
192  new_addr0 = ip40->dst_address.as_u32;
193 
194  sum0 = ip40->checksum;
195  sum0 =
196  ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
197  dst_address);
198  ip40->checksum = ip_csum_fold (sum0);
199 
200  if (PREDICT_TRUE (proto0 == NAT_PROTOCOL_TCP))
201  {
202  old_port0 = tcp0->dst_port;
203  tcp0->dst_port = s0->in2out.port;
204  new_port0 = tcp0->dst_port;
205 
206  sum0 = tcp0->checksum;
207  sum0 =
208  ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
209  dst_address);
210  sum0 =
211  ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
212  length);
213  tcp0->checksum = ip_csum_fold (sum0);
214  }
215  else
216  {
217  old_port0 = udp0->dst_port;
218  udp0->dst_port = s0->in2out.port;
219  udp0->checksum = 0;
220  }
221 
222  encap0:
223  /* Construct IPv6 header */
224  vlib_buffer_advance (b0, -(sizeof (ip6_header_t)));
225  ip60 = vlib_buffer_get_current (b0);
227  clib_host_to_net_u32 ((6 << 28) + (ip40->tos << 20));
228  ip60->payload_length = ip40->length;
229  ip60->protocol = IP_PROTOCOL_IP_IN_IP;
230  ip60->hop_limit = ip40->ttl;
231  ip60->src_address.as_u64[0] = dm->aftr_ip6_addr.as_u64[0];
232  ip60->src_address.as_u64[1] = dm->aftr_ip6_addr.as_u64[1];
233  ip60->dst_address.as_u64[0] = s0->in2out.softwire_id.as_u64[0];
234  ip60->dst_address.as_u64[1] = s0->in2out.softwire_id.as_u64[1];
235 
236  /* Accounting */
237  s0->last_heard = now;
238  s0->total_pkts++;
239  s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
240  /* Per-B4 LRU list maintenance */
242  s0->per_b4_index);
244  s0->per_b4_list_head_index, s0->per_b4_index);
245  trace0:
246  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
247  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
248  {
249  dslite_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
250  t->next_index = next0;
251  t->session_index = ~0;
252  if (s0)
253  t->session_index =
255  }
256 
257  b0->error = error_node->errors[error0];
258 
259  /* verify speculative enqueue, maybe switch current next frame */
261  n_left_to_next, bi0, next0);
262  }
263  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
264  }
265 
266  return frame->n_vectors;
267 }
268 
269 /* *INDENT-OFF* */
271  .name = "dslite-out2in",
272  .vector_size = sizeof (u32),
273  .format_trace = format_dslite_trace,
276  .error_strings = dslite_out2in_error_strings,
277  .n_next_nodes = DSLITE_OUT2IN_N_NEXT,
278  /* edit / add dispositions here */
279  .next_nodes = {
280  [DSLITE_OUT2IN_NEXT_DROP] = "error-drop",
281  [DSLITE_OUT2IN_NEXT_IP4_LOOKUP] = "ip4-lookup",
282  [DSLITE_OUT2IN_NEXT_IP6_LOOKUP] = "ip6-lookup",
283  },
284 };
285 /* *INDENT-ON* */
286 
287 /*
288  * fd.io coding-style-patch-verification: ON
289  *
290  * Local Variables:
291  * eval: (c-set-style "gnu")
292  * End:
293  */
dslite_out2in_node
vlib_node_registration_t dslite_out2in_node
(constructor) VLIB_REGISTER_NODE (dslite_out2in_node)
Definition: dslite_out2in.c:270
nat_session_key_t::port
u16 port
Definition: dslite.h:41
foreach_dslite_error
#define foreach_dslite_error
Definition: dslite.h:145
thread_index
u32 thread_index
Definition: nat44_ei_hairpinning.c:492
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
dslite.h
nat_session_key_t::addr
ip4_address_t addr
Definition: dslite.h:40
dslite_main_t
Definition: dslite.h:103
dslite_main_t::per_thread_data
dslite_per_thread_data_t * per_thread_data
Definition: dslite.h:109
ip4
vl_api_ip4_address_t ip4
Definition: one.api:376
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
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:553
ip6_header_t::protocol
u8 protocol
Definition: ip6_packet.h:304
DSLITE_OUT2IN_NEXT_DROP
@ DSLITE_OUT2IN_NEXT_DROP
Definition: dslite_out2in.c:21
dslite_per_thread_data_t::sessions
dslite_session_t * sessions
Definition: dslite.h:97
tcp_header_t
struct _tcp_header tcp_header_t
ip_proto_to_nat_proto
static nat_protocol_t ip_proto_to_nat_proto(u8 ip_proto)
Common NAT inline functions.
Definition: inlines.h:24
nat_session_key_t::protocol
u16 protocol
Definition: dslite.h:42
next
u16 * next
Definition: nat44_ei_out2in.c:718
echo_header_t
Definition: dslite.h:27
VLIB_NODE_TYPE_INTERNAL
@ VLIB_NODE_TYPE_INTERNAL
Definition: node.h:72
dslite_per_thread_data_t::out2in
clib_bihash_8_8_t out2in
Definition: dslite.h:87
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
ip6_header_t::hop_limit
u8 hop_limit
Definition: ip6_packet.h:307
clib_bihash_kv_8_8_t::value
u64 value
the value
Definition: bihash_8_8.h:44
dslite_out2in_next_t
dslite_out2in_next_t
Definition: dslite_out2in.c:17
ip4_address_t::as_u32
u32 as_u32
Definition: ip4_packet.h:57
clib_bihash_kv_8_8_t::key
u64 key
the key
Definition: bihash_8_8.h:43
u16
unsigned short u16
Definition: types.h:57
nat_session_key_t::as_u64
u64 as_u64
Definition: dslite.h:44
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
dslite_main_t::aftr_ip6_addr
ip6_address_t aftr_ip6_addr
Definition: dslite.h:105
vlib_frame_t
Definition: node.h:372
icmp_type_is_error_message
static_always_inline u8 icmp_type_is_error_message(u8 icmp_type)
Definition: cnat_node.h:109
DSLITE_OUT2IN_NEXT_IP4_LOOKUP
@ DSLITE_OUT2IN_NEXT_IP4_LOOKUP
Definition: dslite_out2in.c:19
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
ip4_header_t
Definition: ip4_packet.h:87
ip4_header_t::tos
ip_dscp_t tos
Definition: ip4_packet.h:96
error
Definition: cJSON.c:88
ip4_header_t::length
u16 length
Definition: ip4_packet.h:99
key
typedef key
Definition: ipsec_types.api:88
vlib_node_runtime_t::errors
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:460
vlib_buffer_advance
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:276
vlib_buffer_t::error
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
dslite_trace_t::session_index
u32 session_index
Definition: dslite.h:137
VLIB_NODE_FN
#define VLIB_NODE_FN(node)
Definition: node.h:202
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
format_dslite_trace
u8 * format_dslite_trace(u8 *s, va_list *args)
Definition: dslite.c:258
nat_session_key_t::fib_index
u16 fib_index
Definition: dslite.h:42
vlib_main_t::thread_index
u32 thread_index
Definition: main.h:213
clib_dlist_remove
static void clib_dlist_remove(dlist_elt_t *pool, u32 index)
Definition: dlist.h:99
f64
double f64
Definition: types.h:142
ip6_header_t::dst_address
ip6_address_t dst_address
Definition: ip6_packet.h:310
ip4_header_t::checksum
u16 checksum
Definition: ip4_packet.h:118
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
ip4_header_t::dst_address
ip4_address_t dst_address
Definition: ip4_packet.h:125
udp_header_t::checksum
u16 checksum
Definition: udp_packet.h:55
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
icmp
icmp
Definition: map.api:387
dslite_icmp_out2in
static u32 dslite_icmp_out2in(dslite_main_t *dm, ip4_header_t *ip4, dslite_session_t **sp, u32 next, u8 *error, u32 thread_index)
Definition: dslite_out2in.c:32
dslite_per_thread_data_t::list_pool
dlist_elt_t * list_pool
Definition: dslite.h:100
vlib_put_next_frame
vlib_put_next_frame(vm, node, next_index, 0)
u32
unsigned int u32
Definition: types.h:88
udp_header_t::dst_port
u16 dst_port
Definition: udp_packet.h:48
ip4_header_t::ttl
u8 ttl
Definition: ip4_packet.h:112
dslite_trace_t
Definition: dslite.h:134
dslite_main_t::dslite_out2in_node_index
u32 dslite_out2in_node_index
Definition: dslite.h:124
clib_bihash_kv_8_8_t
8 octet key, 8 octet key value pair
Definition: bihash_8_8.h:41
vlib_node_get_runtime
static vlib_node_runtime_t * vlib_node_get_runtime(vlib_main_t *vm, u32 node_index)
Get node runtime by node index.
Definition: node_funcs.h:116
value
u8 value
Definition: qos.api:54
ip6_header_t
Definition: ip6_packet.h:294
now
f64 now
Definition: nat44_ei_out2in.c:710
length
char const int length
Definition: cJSON.h:163
ip6_header_t::src_address
ip6_address_t src_address
Definition: ip6_packet.h:310
echo_header_t::identifier
u16 identifier
Definition: dslite.h:29
vlib_main_t
Definition: main.h:102
ip_csum_update
#define ip_csum_update(sum, old, new, type, field)
Definition: ip_packet.h:295
dslite_out2in_error_strings
static char * dslite_out2in_error_strings[]
Definition: dslite_out2in.c:25
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
DSLITE_OUT2IN_NEXT_IP6_LOOKUP
@ DSLITE_OUT2IN_NEXT_IP6_LOOKUP
Definition: dslite_out2in.c:20
ip_csum_t
uword ip_csum_t
Definition: ip_packet.h:245
clib_dlist_addtail
static void clib_dlist_addtail(dlist_elt_t *pool, u32 head_index, u32 new_index)
Definition: dlist.h:43
DSLITE_OUT2IN_N_NEXT
@ DSLITE_OUT2IN_N_NEXT
Definition: dslite_out2in.c:22
dslite_trace_t::next_index
u32 next_index
Definition: dslite.h:136
nat_session_key_t
Definition: dslite.h:34
dslite_main
dslite_main_t dslite_main
Definition: dslite.c:21
vlib_time_now
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:325
ip6_header_t::payload_length
u16 payload_length
Definition: ip6_packet.h:301
vlib_node_runtime_t
Definition: node.h:454
from
from
Definition: nat44_ei_hairpinning.c:415
PREDICT_TRUE
#define PREDICT_TRUE(x)
Definition: clib.h:125
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
ip_csum_fold
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:301
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
ip6_header_t::ip_version_traffic_class_and_flow_label
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:297
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
ip4_header_t::protocol
u8 protocol
Definition: ip4_packet.h:115
ip4_next_header
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:196
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
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169