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:
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 * Defines used for testing various optimisation schemes
20 #include <vnet/ip/ip_frag.h>
21 #include <vnet/ip/ip4_to_ip6.h>
25 IP4_MAP_NEXT_IP6_LOOKUP,
26 #ifdef MAP_SKIP_IP6_LOOKUP
27 IP4_MAP_NEXT_IP6_REWRITE,
29 IP4_MAP_NEXT_ICMP_ERROR,
34 static_always_inline u16
35 ip4_map_port_and_security_check (map_domain_t * d, vlib_buffer_t * b0,
39 if (d->psid_length > 0)
41 ip4_header_t *ip = vlib_buffer_get_current (b0);
44 ((ip->ip_version_and_header_length != 0x45)
45 || clib_host_to_net_u16 (ip->length) < 28))
50 port = vnet_buffer (b0)->ip.reass.l4_dst_port;
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))))
56 *error = MAP_ERROR_ENCAP_SEC_CHECK;
69 static_always_inline u32
70 ip4_map_vtcfl (ip4_header_t * ip4, vlib_buffer_t * p)
72 map_main_t *mm = &map_main;
73 u8 tc = mm->tc_copy ? ip4->tos : mm->tc;
74 u32 vtcfl = 0x6 << 28;
76 vtcfl |= vnet_buffer (p)->ip.flow_hash & 0x000fffff;
78 return (clib_host_to_net_u32 (vtcfl));
85 ip4_map_decrement_ttl (ip4_header_t * ip, u8 * error)
89 /* Input node should have reject packets with ttl 0. */
92 u32 checksum = ip->checksum + clib_host_to_net_u16 (0x0100);
93 checksum += checksum >= 0xffff;
94 ip->checksum = checksum;
97 *error = ttl <= 0 ? IP4_ERROR_TIME_EXPIRED : *error;
99 /* Verify checksum. */
100 ASSERT (ip->checksum == ip4_header_checksum (ip));
104 ip4_map_fragment (vlib_main_t * vm, u32 bi, u16 mtu, bool df, u32 ** buffers,
107 map_main_t *mm = &map_main;
108 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
112 /* IPv4 fragmented packets inside of IPv6 */
113 ip4_frag_do_fragment (vm, bi, mtu, sizeof (ip6_header_t), buffers);
117 vec_foreach (i, *buffers)
119 vlib_buffer_t *p = vlib_get_buffer (vm, *i);
120 ip6_header_t *ip6 = vlib_buffer_get_current (p);
121 ip6->payload_length =
122 clib_host_to_net_u16 (p->current_length - sizeof (ip6_header_t));
127 if (df && !mm->frag_ignore_df)
129 icmp4_error_set_vnet_buffer (b, ICMP4_destination_unreachable,
130 ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
132 vlib_buffer_advance (b, sizeof (ip6_header_t));
133 *error = MAP_ERROR_DF_SET;
134 return (IP4_MAP_NEXT_ICMP_ERROR);
137 /* Create IPv6 fragments here */
138 ip6_frag_do_fragment (vm, bi, mtu, 0, buffers);
140 return (IP4_MAP_NEXT_IP6_LOOKUP);
147 ip4_map (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
149 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
150 vlib_node_runtime_t *error_node =
151 vlib_node_get_runtime (vm, ip4_map_node.index);
152 from = vlib_frame_vector_args (frame);
153 n_left_from = frame->n_vectors;
154 next_index = node->cached_next_index;
155 map_main_t *mm = &map_main;
156 vlib_combined_counter_main_t *cm = mm->domain_counters;
157 u32 thread_index = vm->thread_index;
159 while (n_left_from > 0)
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)
167 u8 error0 = MAP_ERROR_NONE;
171 u32 next0 = IP4_MAP_NEXT_IP6_LOOKUP;
172 u32 map_domain_index0 = ~0;
174 bool free_original_buffer0 = false;
175 u32 *frag_from0, frag_left0;
177 pi0 = to_next[0] = from[0];
181 p0 = vlib_get_buffer (vm, pi0);
182 ip40 = vlib_buffer_get_current (p0);
185 ip4_map_get_domain (&ip40->dst_address, &map_domain_index0,
188 { /* Guess it wasn't for us */
189 vnet_feature_next (&next0, p0);
194 * Shared IPv4 address
196 port0 = ip4_map_port_and_security_check (d0, p0, &error0);
199 * Clamp TCP MSS value.
201 if (ip40->protocol == IP_PROTOCOL_TCP)
203 tcp_header_t *tcp = ip4_next_header (ip40);
204 if (mm->tcp_mss > 0 && tcp_syn (tcp))
206 ip_csum_t csum = tcp->checksum;
207 map_mss_clamping (tcp, &csum, mm->tcp_mss);
208 tcp->checksum = ip_csum_fold (csum);
212 /* Decrement IPv4 TTL */
213 ip4_map_decrement_ttl (ip40, &error0);
215 ip40->flags_and_fragment_offset &
216 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
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;
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;
231 ip6h0->ip_version_traffic_class_and_flow_label =
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);
241 * Determine next node. Can be one of:
242 * ip6-lookup, ip6-rewrite, error-drop
244 if (PREDICT_TRUE (error0 == MAP_ERROR_NONE))
248 && (clib_net_to_host_u16 (ip6h0->payload_length) +
249 sizeof (*ip6h0) > d0->mtu)))
252 ip4_map_fragment (vm, pi0, d0->mtu, df0, &buffer0,
255 if (error0 == MAP_ERROR_NONE)
257 free_original_buffer0 = true;
263 ip4_map_ip6_lookup_bypass (p0,
265 IP4_MAP_NEXT_IP6_REWRITE : next0;
266 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_TX,
268 map_domain_index0, 1,
270 (ip6h0->payload_length) +
276 next0 = IP4_MAP_NEXT_DROP;
279 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
281 map_add_trace (vm, node, p0, map_domain_index0, port0);
284 p0->error = error_node->errors[error0];
286 /* Send fragments that were added in the frame */
287 if (free_original_buffer0)
289 vlib_buffer_free_one (vm, pi0); /* Free original packet */
293 vec_add1 (buffer0, pi0);
296 frag_from0 = buffer0;
297 frag_left0 = vec_len (buffer0);
299 while (frag_left0 > 0)
301 while (frag_left0 > 0 && n_left_to_next > 0)
304 i0 = to_next[0] = frag_from0[0];
310 vlib_get_buffer (vm, i0)->error =
311 error_node->errors[error0];
312 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
313 to_next, n_left_to_next,
316 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
317 vlib_get_next_frame (vm, node, next_index, to_next,
320 vec_reset_length (buffer0);
322 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
325 return frame->n_vectors;
328 static char *map_error_strings[] = {
329 #define _(sym,string) string,
336 VNET_FEATURE_INIT (ip4_map_feature, static) =
338 .arc_name = "ip4-unicast",
339 .node_name = "ip4-map",
340 .runs_before = VNET_FEATURES ("ip4-flow-classify"),
341 .runs_after = VNET_FEATURES("ip4-sv-reassembly-feature"),
344 VLIB_REGISTER_NODE(ip4_map_node) = {
347 .vector_size = sizeof(u32),
348 .format_trace = format_map_trace,
349 .type = VLIB_NODE_TYPE_INTERNAL,
351 .n_errors = MAP_N_ERROR,
352 .error_strings = map_error_strings,
354 .n_next_nodes = IP4_MAP_N_NEXT,
356 [IP4_MAP_NEXT_IP6_LOOKUP] = "ip6-lookup",
357 #ifdef MAP_SKIP_IP6_LOOKUP
358 [IP4_MAP_NEXT_IP6_REWRITE] = "ip6-load-balance",
360 [IP4_MAP_NEXT_ICMP_ERROR] = "ip4-icmp-error",
361 [IP4_MAP_NEXT_DROP] = "error-drop",
367 * fd.io coding-style-patch-verification: ON
370 * eval: (c-set-style "gnu")