FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
ip4_map.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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  * Defines used for testing various optimisation schemes
17  */
18 
19 #include "map.h"
20 #include <vnet/ip/ip_frag.h>
21 #include <vnet/ip/ip4_to_ip6.h>
22 
24 {
26 #ifdef MAP_SKIP_IP6_LOOKUP
28 #endif
32 };
33 
36  u8 * error)
37 {
38  u16 port;
39  if (d->psid_length > 0)
40  {
42 
43  if (PREDICT_FALSE
44  ((ip->ip_version_and_header_length != 0x45)
45  || clib_host_to_net_u16 (ip->length) < 28))
46  {
47  return 0;
48  }
49 
50  port = vnet_buffer (b0)->ip.reass.l4_dst_port;
51 
52  /* Verify that port is not among the well-known ports */
53  if ((d->psid_offset > 0)
54  && (clib_net_to_host_u16 (port) < (0x1 << (16 - d->psid_offset))))
55  {
56  *error = MAP_ERROR_ENCAP_SEC_CHECK;
57  }
58  else
59  {
60  return port;
61  }
62  }
63  return (0);
64 }
65 
66 /*
67  * ip4_map_vtcfl
68  */
71 {
72  map_main_t *mm = &map_main;
73  u8 tc = mm->tc_copy ? ip4->tos : mm->tc;
74  u32 vtcfl = 0x6 << 28;
75  vtcfl |= tc << 20;
76  vtcfl |= vnet_buffer (p)->ip.flow_hash & 0x000fffff;
77 
78  return (clib_host_to_net_u32 (vtcfl));
79 }
80 
81 /*
82  * ip4_map_ttl
83  */
84 static inline void
86 {
87  i32 ttl = ip->ttl;
88 
89  /* Input node should have reject packets with ttl 0. */
90  ASSERT (ip->ttl > 0);
91 
92  u32 checksum = ip->checksum + clib_host_to_net_u16 (0x0100);
93  checksum += checksum >= 0xffff;
94  ip->checksum = checksum;
95  ttl -= 1;
96  ip->ttl = ttl;
97  *error = ttl <= 0 ? IP4_ERROR_TIME_EXPIRED : *error;
98 
99  /* Verify checksum. */
101 }
102 
103 static u32
104 ip4_map_fragment (vlib_main_t * vm, u32 bi, u16 mtu, bool df, u32 ** buffers,
105  u8 * error)
106 {
107  map_main_t *mm = &map_main;
109 
110  if (mm->frag_inner)
111  {
112  /* IPv4 fragmented packets inside of IPv6 */
113  ip4_frag_do_fragment (vm, bi, mtu, sizeof (ip6_header_t), buffers);
114 
115  /* Fixup */
116  u32 *i;
117  vec_foreach (i, *buffers)
118  {
121  ip6->payload_length =
122  clib_host_to_net_u16 (p->current_length - sizeof (ip6_header_t));
123  }
124  }
125  else
126  {
127  if (df && !mm->frag_ignore_df)
128  {
129  icmp4_error_set_vnet_buffer (b, ICMP4_destination_unreachable,
130  ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
131  mtu);
132  vlib_buffer_advance (b, sizeof (ip6_header_t));
133  *error = MAP_ERROR_DF_SET;
134  return (IP4_MAP_NEXT_ICMP_ERROR);
135  }
136 
137  /* Create IPv6 fragments here */
138  ip6_frag_do_fragment (vm, bi, mtu, 0, buffers);
139  }
140  return (IP4_MAP_NEXT_IP6_LOOKUP);
141 }
142 
143 /*
144  * ip4_map
145  */
146 static uword
148 {
149  u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
150  vlib_node_runtime_t *error_node =
153  n_left_from = frame->n_vectors;
154  next_index = node->cached_next_index;
155  map_main_t *mm = &map_main;
158 
159  while (n_left_from > 0)
160  {
161  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
162  while (n_left_from > 0 && n_left_to_next > 0)
163  {
164  u32 pi0;
165  vlib_buffer_t *p0;
166  map_domain_t *d0;
167  u8 error0 = MAP_ERROR_NONE;
168  ip4_header_t *ip40;
169  u16 port0 = 0;
170  ip6_header_t *ip6h0;
172  u32 map_domain_index0 = ~0;
173  u32 *buffer0 = 0;
174  bool free_original_buffer0 = false;
175  u32 *frag_from0, frag_left0;
176 
177  pi0 = to_next[0] = from[0];
178  from += 1;
179  n_left_from -= 1;
180 
181  p0 = vlib_get_buffer (vm, pi0);
182  ip40 = vlib_buffer_get_current (p0);
183 
184  d0 =
185  ip4_map_get_domain (&ip40->dst_address, &map_domain_index0,
186  &error0);
187  if (!d0)
188  { /* Guess it wasn't for us */
189  vnet_feature_next (&next0, p0);
190  goto exit;
191  }
192 
193  /*
194  * Shared IPv4 address
195  */
196  port0 = ip4_map_port_and_security_check (d0, p0, &error0);
197 
198  /*
199  * Clamp TCP MSS value.
200  */
201  if (ip40->protocol == IP_PROTOCOL_TCP)
202  {
203  tcp_header_t *tcp = ip4_next_header (ip40);
204  if (mm->tcp_mss > 0 && tcp_syn (tcp))
205  {
206  ip_csum_t csum = tcp->checksum;
207  map_mss_clamping (tcp, &csum, mm->tcp_mss);
208  tcp->checksum = ip_csum_fold (csum);
209  }
210  }
211 
212  /* Decrement IPv4 TTL */
213  ip4_map_decrement_ttl (ip40, &error0);
214  bool df0 =
216  clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
217 
218  /* MAP calc */
219  u32 da40 = clib_net_to_host_u32 (ip40->dst_address.as_u32);
220  u16 dp40 = clib_net_to_host_u16 (port0);
221  u64 dal60 = map_get_pfx (d0, da40, dp40);
222  u64 dar60 = map_get_sfx (d0, da40, dp40);
223  if (dal60 == 0 && dar60 == 0 && error0 == MAP_ERROR_NONE)
224  error0 = MAP_ERROR_NO_BINDING;
225 
226  /* construct ipv6 header */
227  vlib_buffer_advance (p0, -(sizeof (ip6_header_t)));
228  ip6h0 = vlib_buffer_get_current (p0);
229  vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
230 
232  ip4_map_vtcfl (ip40, p0);
233  ip6h0->payload_length = ip40->length;
234  ip6h0->protocol = IP_PROTOCOL_IP_IN_IP;
235  ip6h0->hop_limit = 0x40;
236  ip6h0->src_address = d0->ip6_src;
237  ip6h0->dst_address.as_u64[0] = clib_host_to_net_u64 (dal60);
238  ip6h0->dst_address.as_u64[1] = clib_host_to_net_u64 (dar60);
239 
240  /*
241  * Determine next node. Can be one of:
242  * ip6-lookup, ip6-rewrite, error-drop
243  */
244  if (PREDICT_TRUE (error0 == MAP_ERROR_NONE))
245  {
246  if (PREDICT_FALSE
247  (d0->mtu
248  && (clib_net_to_host_u16 (ip6h0->payload_length) +
249  sizeof (*ip6h0) > d0->mtu)))
250  {
251  next0 =
252  ip4_map_fragment (vm, pi0, d0->mtu, df0, &buffer0,
253  &error0);
254 
255  if (error0 == MAP_ERROR_NONE)
256  {
257  free_original_buffer0 = true;
258  }
259  }
260  else
261  {
262  next0 =
264  ip40) ?
265  IP4_MAP_NEXT_IP6_REWRITE : next0;
267  thread_index,
268  map_domain_index0, 1,
269  clib_net_to_host_u16
270  (ip6h0->payload_length) +
271  40);
272  }
273  }
274  else
275  {
276  next0 = IP4_MAP_NEXT_DROP;
277  }
278 
279  if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
280  {
281  map_add_trace (vm, node, p0, map_domain_index0, port0);
282  }
283 
284  p0->error = error_node->errors[error0];
285  exit:
286  /* Send fragments that were added in the frame */
287  if (free_original_buffer0)
288  {
289  vlib_buffer_free_one (vm, pi0); /* Free original packet */
290  }
291  else
292  {
293  vec_add1 (buffer0, pi0);
294  }
295 
296  frag_from0 = buffer0;
297  frag_left0 = vec_len (buffer0);
298 
299  while (frag_left0 > 0)
300  {
301  while (frag_left0 > 0 && n_left_to_next > 0)
302  {
303  u32 i0;
304  i0 = to_next[0] = frag_from0[0];
305  frag_from0 += 1;
306  frag_left0 -= 1;
307  to_next += 1;
308  n_left_to_next -= 1;
309 
310  vlib_get_buffer (vm, i0)->error =
311  error_node->errors[error0];
313  to_next, n_left_to_next,
314  i0, next0);
315  }
316  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
318  n_left_to_next);
319  }
320  vec_reset_length (buffer0);
321  }
322  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
323  }
324 
325  return frame->n_vectors;
326 }
327 
328 /* *INDENT-OFF* */
329 VNET_FEATURE_INIT (ip4_map_feature, static) =
330 {
331  .arc_name = "ip4-unicast",
332  .node_name = "ip4-map",
333  .runs_before = VNET_FEATURES ("ip4-flow-classify"),
334  .runs_after = VNET_FEATURES("ip4-sv-reassembly-feature"),
335 };
336 
338  .function = ip4_map,
339  .name = "ip4-map",
340  .vector_size = sizeof(u32),
341  .format_trace = format_map_trace,
343 
344  .n_errors = MAP_N_ERROR,
345  .error_counters = map_error_counters,
346 
347  .n_next_nodes = IP4_MAP_N_NEXT,
348  .next_nodes = {
349  [IP4_MAP_NEXT_IP6_LOOKUP] = "ip6-lookup",
350 #ifdef MAP_SKIP_IP6_LOOKUP
351  [IP4_MAP_NEXT_IP6_REWRITE] = "ip6-load-balance",
352 #endif
353  [IP4_MAP_NEXT_ICMP_ERROR] = "ip4-icmp-error",
354  [IP4_MAP_NEXT_DROP] = "error-drop",
355  },
356 };
357 /* *INDENT-ON* */
358 
359 /*
360  * fd.io coding-style-patch-verification: ON
361  *
362  * Local Variables:
363  * eval: (c-set-style "gnu")
364  * End:
365  */
vec_reset_length
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
Definition: vec_bootstrap.h:194
IP4_MAP_NEXT_IP6_REWRITE
@ IP4_MAP_NEXT_IP6_REWRITE
Definition: ip4_map.c:27
IP4_MAP_NEXT_IP6_LOOKUP
@ IP4_MAP_NEXT_IP6_LOOKUP
Definition: ip4_map.c:25
thread_index
u32 thread_index
Definition: nat44_ei_hairpinning.c:495
IP4_HEADER_FLAG_DONT_FRAGMENT
#define IP4_HEADER_FLAG_DONT_FRAGMENT
Definition: ip4_packet.h:108
map_main_t::frag_inner
bool frag_inner
Definition: map.h:197
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
IP4_MAP_N_NEXT
@ IP4_MAP_N_NEXT
Definition: ip4_map.c:31
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
map_domain_t::mtu
u16 mtu
Definition: map.h:86
ip6_header_t::protocol
u8 protocol
Definition: ip6_packet.h:304
ttl
u8 ttl
Definition: fib_types.api:26
tcp_header_t
struct _tcp_header tcp_header_t
ip4_header_checksum_is_valid
static uword ip4_header_checksum_is_valid(ip4_header_t *i)
Definition: ip4_packet.h:396
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
ip6_header_t::hop_limit
u8 hop_limit
Definition: ip6_packet.h:307
ip4_address_t::as_u32
u32 as_u32
Definition: ip4_packet.h:57
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
port
u16 port
Definition: lb_types.api:73
vlib_frame_t
Definition: node.h:372
map_add_trace
static void map_add_trace(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t *b, u32 map_domain_index, u16 port)
Definition: map.h:222
ip4_map_vtcfl
static_always_inline u32 ip4_map_vtcfl(ip4_header_t *ip4, vlib_buffer_t *p)
Definition: ip4_map.c:70
map_main_t::tc_copy
bool tc_copy
Definition: map.h:181
ip4_header_t
Definition: ip4_packet.h:87
error
Definition: cJSON.c:88
map_main
map_main_t map_main
Definition: map.c:27
tcp_syn
#define tcp_syn(_th)
Definition: tcp_packet.h:80
ip4_header_t::length
u16 length
Definition: ip4_packet.h:99
i32
signed int i32
Definition: types.h:77
map_domain_t
Definition: map.h:76
map_mss_clamping
static_always_inline void map_mss_clamping(tcp_header_t *tcp, ip_csum_t *sum, u16 mss_clamping)
Definition: map.h:423
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
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
vlib_buffer_t::error
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
map_get_sfx
static_always_inline u64 map_get_sfx(map_domain_t *d, u32 addr, u16 port)
Definition: map.h:274
map_main_t::tcp_mss
u16 tcp_mss
Definition: map.h:187
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:441
map_main_t
Definition: map.h:165
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
vnet_feature_next
static_always_inline void vnet_feature_next(u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:322
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
map_domain_t::ip6_src
ip6_address_t ip6_src
Definition: map.h:80
map_domain_t::psid_offset
u8 psid_offset
Definition: map.h:91
static_always_inline
#define static_always_inline
Definition: clib.h:112
IP4_MAP_NEXT_DROP
@ IP4_MAP_NEXT_DROP
Definition: ip4_map.c:30
IP4_MAP_NEXT_ICMP_ERROR
@ IP4_MAP_NEXT_ICMP_ERROR
Definition: ip4_map.c:29
uword
u64 uword
Definition: types.h:112
ip4_map_decrement_ttl
static void ip4_map_decrement_ttl(ip4_header_t *ip, u8 *error)
Definition: ip4_map.c:85
ip4_map_get_domain
static_always_inline map_domain_t * ip4_map_get_domain(ip4_address_t *addr, u32 *map_domain_index, u8 *error)
Definition: map.h:314
vlib_main_t::thread_index
u32 thread_index
Definition: main.h:215
map_main_t::domain_counters
vlib_combined_counter_main_t * domain_counters
Definition: map.h:173
ip6_header_t::dst_address
ip6_address_t dst_address
Definition: ip6_packet.h:310
ip4_map_port_and_security_check
static_always_inline u16 ip4_map_port_and_security_check(map_domain_t *d, vlib_buffer_t *b0, u8 *error)
Definition: ip4_map.c:35
cm
vnet_feature_config_main_t * cm
Definition: nat44_ei_hairpinning.c:594
ip4_map_fragment
static u32 ip4_map_fragment(vlib_main_t *vm, u32 bi, u16 mtu, bool df, u32 **buffers, u8 *error)
Definition: ip4_map.c:104
ip4_map
static uword ip4_map(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: ip4_map.c:147
ip4_frag_do_fragment
ip_frag_error_t ip4_frag_do_fragment(vlib_main_t *vm, u32 from_bi, u16 mtu, u16 l2unfragmentablesize, u32 **buffer)
Definition: ip_frag.c:93
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
vlib_buffer_t::current_length
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:122
map.h
icmp4_error_set_vnet_buffer
static_always_inline void icmp4_error_set_vnet_buffer(vlib_buffer_t *b, u8 type, u8 code, u32 data)
Definition: icmp4.h:51
ip_frag.h
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
ip4_map_next_e
ip4_map_next_e
Definition: ip4_map.c:23
u64
unsigned long u64
Definition: types.h:89
map_get_pfx
static_always_inline u64 map_get_pfx(map_domain_t *d, u32 addr, u16 port)
Definition: map.h:249
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
vlib_combined_counter_main_t
A collection of combined counters.
Definition: counter.h:203
vlib_put_next_frame
vlib_put_next_frame(vm, node, next_index, 0)
map_main_t::frag_ignore_df
bool frag_ignore_df
Definition: map.h:198
u32
unsigned int u32
Definition: types.h:88
MAP_DOMAIN_COUNTER_TX
@ MAP_DOMAIN_COUNTER_TX
Definition: map.h:127
ip6
vl_api_ip6_address_t ip6
Definition: one.api:424
vec_foreach
#define vec_foreach(var, vec)
Vector iterator.
Definition: vec_bootstrap.h:213
ip4_map_node
vlib_node_registration_t ip4_map_node
(constructor) VLIB_REGISTER_NODE (ip4_map_node)
Definition: ip4_map.c:337
ip4_map_ip6_lookup_bypass
static_always_inline bool ip4_map_ip6_lookup_bypass(vlib_buffer_t *p0, ip4_header_t *ip)
Definition: map.h:472
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
ip6_header_t
Definition: ip6_packet.h:294
ip4_header_t::flags_and_fragment_offset
u16 flags_and_fragment_offset
Definition: ip4_packet.h:106
map_domain_t::psid_length
u8 psid_length
Definition: map.h:92
ip6_frag_do_fragment
ip_frag_error_t ip6_frag_do_fragment(vlib_main_t *vm, u32 from_bi, u16 mtu, u16 l2unfragmentablesize, u32 **buffer)
Definition: ip_frag.c:381
ip6_header_t::src_address
ip6_address_t src_address
Definition: ip6_packet.h:310
vlib_main_t
Definition: main.h:102
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
VNET_FEATURES
#define VNET_FEATURES(...)
Definition: feature.h:470
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
vl_api_address_t ip
Definition: l2.api:558
ip_csum_t
uword ip_csum_t
Definition: ip_packet.h:245
i
int i
Definition: flowhash_template.h:376
ip4_to_ip6.h
IPv4 to IPv6 translation.
vlib_buffer_free_one
static void vlib_buffer_free_one(vlib_main_t *vm, u32 buffer_index)
Free one buffer Shorthand to free a single buffer chain.
Definition: buffer_funcs.h:1012
ip6_header_t::payload_length
u16 payload_length
Definition: ip6_packet.h:301
vlib_node_runtime_t
Definition: node.h:454
VNET_FEATURE_INIT
VNET_FEATURE_INIT(ip4_map_feature, static)
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
VLIB_TX
@ VLIB_TX
Definition: defs.h:47
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
vlib_increment_combined_counter
vlib_increment_combined_counter(ccm, ti, sw_if_index, n_buffers, n_bytes)
format_map_trace
u8 * format_map_trace(u8 *s, va_list *args)
Definition: map.c:1204
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
map_main_t::tc
u8 tc
Definition: map.h:180
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169