blob: 1a20d7047329a3d370b21ac78fe0143d90ac6d54 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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#define MAP_ENCAP_DUAL 0
19
20#include "map.h"
21#include "../ip/ip_frag.h"
22
23vlib_node_registration_t ip4_map_reass_node;
24
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070025enum ip4_map_next_e
26{
Ed Warnickecb9cada2015-12-08 15:45:58 -070027 IP4_MAP_NEXT_IP6_LOOKUP,
28#ifdef MAP_SKIP_IP6_LOOKUP
29 IP4_MAP_NEXT_IP6_REWRITE,
30#endif
Ole Troan9fb87552016-01-13 22:30:43 +010031 IP4_MAP_NEXT_IP4_FRAGMENT,
32 IP4_MAP_NEXT_IP6_FRAGMENT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070033 IP4_MAP_NEXT_REASS,
Ole Troan9fb87552016-01-13 22:30:43 +010034 IP4_MAP_NEXT_ICMP_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070035 IP4_MAP_NEXT_DROP,
36 IP4_MAP_N_NEXT,
37};
38
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070039enum ip4_map_reass_next_t
40{
Ed Warnickecb9cada2015-12-08 15:45:58 -070041 IP4_MAP_REASS_NEXT_IP6_LOOKUP,
42 IP4_MAP_REASS_NEXT_IP4_FRAGMENT,
43 IP4_MAP_REASS_NEXT_DROP,
44 IP4_MAP_REASS_N_NEXT,
45};
46
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070047typedef struct
48{
Ed Warnickecb9cada2015-12-08 15:45:58 -070049 u32 map_domain_index;
50 u16 port;
51 u8 cached;
52} map_ip4_map_reass_trace_t;
53
54u8 *
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070055format_ip4_map_reass_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070056{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070057 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
58 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070059 map_ip4_map_reass_trace_t *t = va_arg (*args, map_ip4_map_reass_trace_t *);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070060 return format (s, "MAP domain index: %d L4 port: %u Status: %s",
61 t->map_domain_index, t->port,
62 t->cached ? "cached" : "forwarded");
Ed Warnickecb9cada2015-12-08 15:45:58 -070063}
64
65/*
66 * ip4_map_get_port
67 */
68u16
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070069ip4_map_get_port (ip4_header_t * ip, map_dir_e dir)
Ed Warnickecb9cada2015-12-08 15:45:58 -070070{
71 /* Find port information */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070072 if (PREDICT_TRUE ((ip->protocol == IP_PROTOCOL_TCP) ||
73 (ip->protocol == IP_PROTOCOL_UDP)))
74 {
75 udp_header_t *udp = (void *) (ip + 1);
76 return (dir == MAP_SENDER ? udp->src_port : udp->dst_port);
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070078 else if (ip->protocol == IP_PROTOCOL_ICMP)
79 {
80 /*
81 * 1) ICMP Echo request or Echo reply
82 * 2) ICMP Error with inner packet being UDP or TCP
83 * 3) ICMP Error with inner packet being ICMP Echo request or Echo reply
84 */
85 icmp46_header_t *icmp = (void *) (ip + 1);
86 if (icmp->type == ICMP4_echo_request || icmp->type == ICMP4_echo_reply)
87 {
88 return *((u16 *) (icmp + 1));
89 }
90 else if (clib_net_to_host_u16 (ip->length) >= 56)
91 { // IP + ICMP + IP + L4 header
92 ip4_header_t *icmp_ip = (ip4_header_t *) (icmp + 2);
93 if (PREDICT_TRUE ((icmp_ip->protocol == IP_PROTOCOL_TCP) ||
94 (icmp_ip->protocol == IP_PROTOCOL_UDP)))
95 {
96 udp_header_t *udp = (void *) (icmp_ip + 1);
97 return (dir == MAP_SENDER ? udp->dst_port : udp->src_port);
98 }
99 else if (icmp_ip->protocol == IP_PROTOCOL_ICMP)
100 {
101 icmp46_header_t *inner_icmp = (void *) (icmp_ip + 1);
102 if (inner_icmp->type == ICMP4_echo_request
103 || inner_icmp->type == ICMP4_echo_reply)
104 return (*((u16 *) (inner_icmp + 1)));
105 }
106 }
107 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108 return (0);
109}
110
111static_always_inline u16
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700112ip4_map_port_and_security_check (map_domain_t * d, ip4_header_t * ip,
113 u32 * next, u8 * error)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114{
115 u16 port = 0;
116
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700117 if (d->psid_length > 0)
118 {
119 if (ip4_get_fragment_offset (ip) == 0)
120 {
121 if (PREDICT_FALSE
122 ((ip->ip_version_and_header_length != 0x45)
123 || clib_host_to_net_u16 (ip->length) < 28))
124 {
125 return 0;
126 }
127 port = ip4_map_get_port (ip, MAP_RECEIVER);
128 if (port)
129 {
130 /* Verify that port is not among the well-known ports */
131 if ((d->psid_offset > 0)
132 && (clib_net_to_host_u16 (port) <
133 (0x1 << (16 - d->psid_offset))))
134 {
135 *error = MAP_ERROR_ENCAP_SEC_CHECK;
136 }
137 else
138 {
139 if (ip4_get_fragment_more (ip))
140 *next = IP4_MAP_NEXT_REASS;
141 return (port);
142 }
143 }
144 else
145 {
146 *error = MAP_ERROR_BAD_PROTOCOL;
147 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700149 else
150 {
151 *next = IP4_MAP_NEXT_REASS;
152 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 return (0);
155}
156
157/*
158 * ip4_map_vtcfl
159 */
160static_always_inline u32
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700161ip4_map_vtcfl (ip4_header_t * ip4, vlib_buffer_t * p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162{
163 map_main_t *mm = &map_main;
164 u8 tc = mm->tc_copy ? ip4->tos : mm->tc;
165 u32 vtcfl = 0x6 << 28;
166 vtcfl |= tc << 20;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700167 vtcfl |= vnet_buffer (p)->ip.flow_hash & 0x000fffff;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700169 return (clib_host_to_net_u32 (vtcfl));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170}
171
172static_always_inline bool
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700173ip4_map_ip6_lookup_bypass (vlib_buffer_t * p0, ip4_header_t * ip)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174{
175#ifdef MAP_SKIP_IP6_LOOKUP
Neale Ranns80823802017-02-20 18:23:41 -0800176 if (FIB_NODE_INDEX_INVALID != pre_resolved[FIB_PROTOCOL_IP6].fei)
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700177 {
Neale Ranns80823802017-02-20 18:23:41 -0800178 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
179 pre_resolved[FIB_PROTOCOL_IP6].dpo.dpoi_index;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700180 return (true);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182#endif
183 return (false);
184}
185
186/*
Ole Troan366ac6e2016-01-06 12:40:28 +0100187 * ip4_map_ttl
188 */
189static inline void
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700190ip4_map_decrement_ttl (ip4_header_t * ip, u8 * error)
Ole Troan366ac6e2016-01-06 12:40:28 +0100191{
192 i32 ttl = ip->ttl;
193
194 /* Input node should have reject packets with ttl 0. */
195 ASSERT (ip->ttl > 0);
196
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700197 u32 checksum = ip->checksum + clib_host_to_net_u16 (0x0100);
Ole Troan366ac6e2016-01-06 12:40:28 +0100198 checksum += checksum >= 0xffff;
199 ip->checksum = checksum;
200 ttl -= 1;
201 ip->ttl = ttl;
202 *error = ttl <= 0 ? IP4_ERROR_TIME_EXPIRED : *error;
203
204 /* Verify checksum. */
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700205 ASSERT (ip->checksum == ip4_header_checksum (ip));
Ole Troan366ac6e2016-01-06 12:40:28 +0100206}
207
Ole Troan9fb87552016-01-13 22:30:43 +0100208static u32
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700209ip4_map_fragment (vlib_buffer_t * b, u16 mtu, bool df, u8 * error)
Ole Troan9fb87552016-01-13 22:30:43 +0100210{
211 map_main_t *mm = &map_main;
212
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700213 if (mm->frag_inner)
214 {
215 ip_frag_set_vnet_buffer (b, sizeof (ip6_header_t), mtu,
216 IP4_FRAG_NEXT_IP6_LOOKUP,
217 IP_FRAG_FLAG_IP6_HEADER);
218 return (IP4_MAP_NEXT_IP4_FRAGMENT);
Ole Troan9fb87552016-01-13 22:30:43 +0100219 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700220 else
221 {
222 if (df && !mm->frag_ignore_df)
223 {
224 icmp4_error_set_vnet_buffer (b, ICMP4_destination_unreachable,
225 ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
226 mtu);
227 vlib_buffer_advance (b, sizeof (ip6_header_t));
228 *error = MAP_ERROR_DF_SET;
229 return (IP4_MAP_NEXT_ICMP_ERROR);
230 }
231 ip_frag_set_vnet_buffer (b, 0, mtu, IP6_FRAG_NEXT_IP6_LOOKUP,
232 IP_FRAG_FLAG_IP6_HEADER);
233 return (IP4_MAP_NEXT_IP6_FRAGMENT);
234 }
Ole Troan9fb87552016-01-13 22:30:43 +0100235}
236
Ole Troan366ac6e2016-01-06 12:40:28 +0100237/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238 * ip4_map
239 */
240static uword
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700241ip4_map (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242{
243 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700244 vlib_node_runtime_t *error_node =
245 vlib_node_get_runtime (vm, ip4_map_node.index);
246 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247 n_left_from = frame->n_vectors;
248 next_index = node->cached_next_index;
249 map_main_t *mm = &map_main;
250 vlib_combined_counter_main_t *cm = mm->domain_counters;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700251 u32 cpu_index = os_get_cpu_number ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700253 while (n_left_from > 0)
254 {
255 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700257 /* Dual loop */
258 while (n_left_from >= 4 && n_left_to_next >= 2)
259 {
260 u32 pi0, pi1;
261 vlib_buffer_t *p0, *p1;
262 map_domain_t *d0, *d1;
263 u8 error0 = MAP_ERROR_NONE, error1 = MAP_ERROR_NONE;
264 ip4_header_t *ip40, *ip41;
265 u16 port0 = 0, port1 = 0;
266 ip6_header_t *ip6h0, *ip6h1;
267 u32 map_domain_index0 = ~0, map_domain_index1 = ~0;
268 u32 next0 = IP4_MAP_NEXT_IP6_LOOKUP, next1 =
269 IP4_MAP_NEXT_IP6_LOOKUP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700271 /* Prefetch next iteration. */
272 {
273 vlib_buffer_t *p2, *p3;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700275 p2 = vlib_get_buffer (vm, from[2]);
276 p3 = vlib_get_buffer (vm, from[3]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700278 vlib_prefetch_buffer_header (p2, STORE);
279 vlib_prefetch_buffer_header (p3, STORE);
280 /* IPv4 + 8 = 28. possibly plus -40 */
281 CLIB_PREFETCH (p2->data - 40, 68, STORE);
282 CLIB_PREFETCH (p3->data - 40, 68, STORE);
283 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700285 pi0 = to_next[0] = from[0];
286 pi1 = to_next[1] = from[1];
287 from += 2;
288 n_left_from -= 2;
289 to_next += 2;
290 n_left_to_next -= 2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700292 p0 = vlib_get_buffer (vm, pi0);
293 p1 = vlib_get_buffer (vm, pi1);
294 ip40 = vlib_buffer_get_current (p0);
295 ip41 = vlib_buffer_get_current (p1);
Neale Ranns9705c382017-02-20 20:29:41 -0800296 map_domain_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
297 d0 = ip4_map_get_domain (map_domain_index0);
298 map_domain_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
299 d1 = ip4_map_get_domain (map_domain_index1);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700300 ASSERT (d0);
301 ASSERT (d1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700303 /*
304 * Shared IPv4 address
305 */
306 port0 = ip4_map_port_and_security_check (d0, ip40, &next0, &error0);
307 port1 = ip4_map_port_and_security_check (d1, ip41, &next1, &error1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700309 /* Decrement IPv4 TTL */
310 ip4_map_decrement_ttl (ip40, &error0);
311 ip4_map_decrement_ttl (ip41, &error1);
312 bool df0 =
Ed Warnicke853e7202016-08-12 11:42:26 -0700313 ip40->flags_and_fragment_offset &
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700314 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
315 bool df1 =
Ed Warnicke853e7202016-08-12 11:42:26 -0700316 ip41->flags_and_fragment_offset &
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700317 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
Ole Troan9fb87552016-01-13 22:30:43 +0100318
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700319 /* MAP calc */
320 u32 da40 = clib_net_to_host_u32 (ip40->dst_address.as_u32);
321 u32 da41 = clib_net_to_host_u32 (ip41->dst_address.as_u32);
322 u16 dp40 = clib_net_to_host_u16 (port0);
323 u16 dp41 = clib_net_to_host_u16 (port1);
324 u64 dal60 = map_get_pfx (d0, da40, dp40);
325 u64 dal61 = map_get_pfx (d1, da41, dp41);
326 u64 dar60 = map_get_sfx (d0, da40, dp40);
327 u64 dar61 = map_get_sfx (d1, da41, dp41);
328 if (dal60 == 0 && dar60 == 0 && error0 == MAP_ERROR_NONE
329 && next0 != IP4_MAP_NEXT_REASS)
330 error0 = MAP_ERROR_NO_BINDING;
331 if (dal61 == 0 && dar61 == 0 && error1 == MAP_ERROR_NONE
332 && next1 != IP4_MAP_NEXT_REASS)
333 error1 = MAP_ERROR_NO_BINDING;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700335 /* construct ipv6 header */
336 vlib_buffer_advance (p0, -sizeof (ip6_header_t));
337 vlib_buffer_advance (p1, -sizeof (ip6_header_t));
338 ip6h0 = vlib_buffer_get_current (p0);
339 ip6h1 = vlib_buffer_get_current (p1);
340 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
341 vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700343 ip6h0->ip_version_traffic_class_and_flow_label =
344 ip4_map_vtcfl (ip40, p0);
345 ip6h1->ip_version_traffic_class_and_flow_label =
346 ip4_map_vtcfl (ip41, p1);
347 ip6h0->payload_length = ip40->length;
348 ip6h1->payload_length = ip41->length;
349 ip6h0->protocol = IP_PROTOCOL_IP_IN_IP;
350 ip6h1->protocol = IP_PROTOCOL_IP_IN_IP;
351 ip6h0->hop_limit = 0x40;
352 ip6h1->hop_limit = 0x40;
353 ip6h0->src_address = d0->ip6_src;
354 ip6h1->src_address = d1->ip6_src;
355 ip6h0->dst_address.as_u64[0] = clib_host_to_net_u64 (dal60);
356 ip6h0->dst_address.as_u64[1] = clib_host_to_net_u64 (dar60);
357 ip6h1->dst_address.as_u64[0] = clib_host_to_net_u64 (dal61);
358 ip6h1->dst_address.as_u64[1] = clib_host_to_net_u64 (dar61);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700360 /*
361 * Determine next node. Can be one of:
362 * ip6-lookup, ip6-rewrite, ip4-fragment, ip4-virtreass, error-drop
363 */
364 if (PREDICT_TRUE (error0 == MAP_ERROR_NONE))
365 {
366 if (PREDICT_FALSE
367 (d0->mtu
368 && (clib_net_to_host_u16 (ip6h0->payload_length) +
369 sizeof (*ip6h0) > d0->mtu)))
370 {
371 next0 = ip4_map_fragment (p0, d0->mtu, df0, &error0);
372 }
373 else
374 {
375 next0 =
376 ip4_map_ip6_lookup_bypass (p0,
377 ip40) ?
378 IP4_MAP_NEXT_IP6_REWRITE : next0;
379 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_TX,
380 cpu_index,
381 map_domain_index0, 1,
382 clib_net_to_host_u16
383 (ip6h0->payload_length) +
384 40);
385 }
386 }
387 else
388 {
389 next0 = IP4_MAP_NEXT_DROP;
390 }
391
392 /*
393 * Determine next node. Can be one of:
394 * ip6-lookup, ip6-rewrite, ip4-fragment, ip4-virtreass, error-drop
395 */
396 if (PREDICT_TRUE (error1 == MAP_ERROR_NONE))
397 {
398 if (PREDICT_FALSE
399 (d1->mtu
400 && (clib_net_to_host_u16 (ip6h1->payload_length) +
401 sizeof (*ip6h1) > d1->mtu)))
402 {
403 next1 = ip4_map_fragment (p1, d1->mtu, df1, &error1);
404 }
405 else
406 {
407 next1 =
408 ip4_map_ip6_lookup_bypass (p1,
409 ip41) ?
410 IP4_MAP_NEXT_IP6_REWRITE : next1;
411 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_TX,
412 cpu_index,
413 map_domain_index1, 1,
414 clib_net_to_host_u16
415 (ip6h1->payload_length) +
416 40);
417 }
418 }
419 else
420 {
421 next1 = IP4_MAP_NEXT_DROP;
422 }
423
424 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
425 {
426 map_trace_t *tr = vlib_add_trace (vm, node, p0, sizeof (*tr));
427 tr->map_domain_index = map_domain_index0;
428 tr->port = port0;
429 }
430 if (PREDICT_FALSE (p1->flags & VLIB_BUFFER_IS_TRACED))
431 {
432 map_trace_t *tr = vlib_add_trace (vm, node, p1, sizeof (*tr));
433 tr->map_domain_index = map_domain_index1;
434 tr->port = port1;
435 }
436
437 p0->error = error_node->errors[error0];
438 p1->error = error_node->errors[error1];
439
440 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
441 n_left_to_next, pi0, pi1, next0,
442 next1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700445 while (n_left_from > 0 && n_left_to_next > 0)
446 {
447 u32 pi0;
448 vlib_buffer_t *p0;
449 map_domain_t *d0;
450 u8 error0 = MAP_ERROR_NONE;
451 ip4_header_t *ip40;
452 u16 port0 = 0;
453 ip6_header_t *ip6h0;
454 u32 next0 = IP4_MAP_NEXT_IP6_LOOKUP;
455 u32 map_domain_index0 = ~0;
456
457 pi0 = to_next[0] = from[0];
458 from += 1;
459 n_left_from -= 1;
460 to_next += 1;
461 n_left_to_next -= 1;
462
463 p0 = vlib_get_buffer (vm, pi0);
464 ip40 = vlib_buffer_get_current (p0);
Neale Ranns9705c382017-02-20 20:29:41 -0800465 map_domain_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
466 d0 = ip4_map_get_domain (map_domain_index0);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700467 ASSERT (d0);
468
469 /*
470 * Shared IPv4 address
471 */
472 port0 = ip4_map_port_and_security_check (d0, ip40, &next0, &error0);
473
474 /* Decrement IPv4 TTL */
475 ip4_map_decrement_ttl (ip40, &error0);
476 bool df0 =
Ed Warnicke853e7202016-08-12 11:42:26 -0700477 ip40->flags_and_fragment_offset &
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700478 clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
479
480 /* MAP calc */
481 u32 da40 = clib_net_to_host_u32 (ip40->dst_address.as_u32);
482 u16 dp40 = clib_net_to_host_u16 (port0);
483 u64 dal60 = map_get_pfx (d0, da40, dp40);
484 u64 dar60 = map_get_sfx (d0, da40, dp40);
485 if (dal60 == 0 && dar60 == 0 && error0 == MAP_ERROR_NONE
486 && next0 != IP4_MAP_NEXT_REASS)
487 error0 = MAP_ERROR_NO_BINDING;
488
489 /* construct ipv6 header */
490 vlib_buffer_advance (p0, -(sizeof (ip6_header_t)));
491 ip6h0 = vlib_buffer_get_current (p0);
492 vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
493
494 ip6h0->ip_version_traffic_class_and_flow_label =
495 ip4_map_vtcfl (ip40, p0);
496 ip6h0->payload_length = ip40->length;
497 ip6h0->protocol = IP_PROTOCOL_IP_IN_IP;
498 ip6h0->hop_limit = 0x40;
499 ip6h0->src_address = d0->ip6_src;
500 ip6h0->dst_address.as_u64[0] = clib_host_to_net_u64 (dal60);
501 ip6h0->dst_address.as_u64[1] = clib_host_to_net_u64 (dar60);
502
503 /*
504 * Determine next node. Can be one of:
505 * ip6-lookup, ip6-rewrite, ip4-fragment, ip4-virtreass, error-drop
506 */
507 if (PREDICT_TRUE (error0 == MAP_ERROR_NONE))
508 {
509 if (PREDICT_FALSE
510 (d0->mtu
511 && (clib_net_to_host_u16 (ip6h0->payload_length) +
512 sizeof (*ip6h0) > d0->mtu)))
513 {
514 next0 = ip4_map_fragment (p0, d0->mtu, df0, &error0);
515 }
516 else
517 {
518 next0 =
519 ip4_map_ip6_lookup_bypass (p0,
520 ip40) ?
521 IP4_MAP_NEXT_IP6_REWRITE : next0;
522 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_TX,
523 cpu_index,
524 map_domain_index0, 1,
525 clib_net_to_host_u16
526 (ip6h0->payload_length) +
527 40);
528 }
529 }
530 else
531 {
532 next0 = IP4_MAP_NEXT_DROP;
533 }
534
535 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
536 {
537 map_trace_t *tr = vlib_add_trace (vm, node, p0, sizeof (*tr));
538 tr->map_domain_index = map_domain_index0;
539 tr->port = port0;
540 }
541
542 p0->error = error_node->errors[error0];
543 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
544 n_left_to_next, pi0, next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700546 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 }
548
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549 return frame->n_vectors;
550}
551
552/*
553 * ip4_map_reass
554 */
555static uword
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700556ip4_map_reass (vlib_main_t * vm,
557 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558{
559 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700560 vlib_node_runtime_t *error_node =
561 vlib_node_get_runtime (vm, ip4_map_reass_node.index);
562 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563 n_left_from = frame->n_vectors;
564 next_index = node->cached_next_index;
565 map_main_t *mm = &map_main;
566 vlib_combined_counter_main_t *cm = mm->domain_counters;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700567 u32 cpu_index = os_get_cpu_number ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568 u32 *fragments_to_drop = NULL;
569 u32 *fragments_to_loopback = NULL;
570
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700571 while (n_left_from > 0)
572 {
573 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700574
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700575 while (n_left_from > 0 && n_left_to_next > 0)
576 {
577 u32 pi0;
578 vlib_buffer_t *p0;
579 map_domain_t *d0;
580 u8 error0 = MAP_ERROR_NONE;
581 ip4_header_t *ip40;
582 i32 port0 = 0;
583 ip6_header_t *ip60;
584 u32 next0 = IP4_MAP_REASS_NEXT_IP6_LOOKUP;
585 u32 map_domain_index0;
586 u8 cached = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700588 pi0 = to_next[0] = from[0];
589 from += 1;
590 n_left_from -= 1;
591 to_next += 1;
592 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700594 p0 = vlib_get_buffer (vm, pi0);
595 ip60 = vlib_buffer_get_current (p0);
596 ip40 = (ip4_header_t *) (ip60 + 1);
Neale Ranns9705c382017-02-20 20:29:41 -0800597 map_domain_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
598 d0 = ip4_map_get_domain (map_domain_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700600 map_ip4_reass_lock ();
Ed Warnicke853e7202016-08-12 11:42:26 -0700601 map_ip4_reass_t *r = map_ip4_reass_get (ip40->src_address.as_u32,
602 ip40->dst_address.as_u32,
603 ip40->fragment_id,
604 ip40->protocol,
605 &fragments_to_drop);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700606 if (PREDICT_FALSE (!r))
607 {
608 // Could not create a caching entry
609 error0 = MAP_ERROR_FRAGMENT_MEMORY;
610 }
611 else if (PREDICT_TRUE (ip4_get_fragment_offset (ip40)))
612 {
613 if (r->port >= 0)
614 {
615 // We know the port already
616 port0 = r->port;
617 }
618 else if (map_ip4_reass_add_fragment (r, pi0))
619 {
620 // Not enough space for caching
621 error0 = MAP_ERROR_FRAGMENT_MEMORY;
622 map_ip4_reass_free (r, &fragments_to_drop);
623 }
624 else
625 {
626 cached = 1;
627 }
628 }
629 else
630 if ((port0 =
631 ip4_get_port (ip40, MAP_RECEIVER, p0->current_length)) < 0)
632 {
633 // Could not find port. We'll free the reassembly.
634 error0 = MAP_ERROR_BAD_PROTOCOL;
635 port0 = 0;
636 map_ip4_reass_free (r, &fragments_to_drop);
637 }
638 else
639 {
640 r->port = port0;
641 map_ip4_reass_get_fragments (r, &fragments_to_loopback);
642 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643
644#ifdef MAP_IP4_REASS_COUNT_BYTES
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700645 if (!cached && r)
646 {
647 r->forwarded += clib_host_to_net_u16 (ip40->length) - 20;
648 if (!ip4_get_fragment_more (ip40))
649 r->expected_total =
650 ip4_get_fragment_offset (ip40) * 8 +
651 clib_host_to_net_u16 (ip40->length) - 20;
652 if (r->forwarded >= r->expected_total)
653 map_ip4_reass_free (r, &fragments_to_drop);
654 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655#endif
656
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700657 map_ip4_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700659 // NOTE: Most operations have already been performed by ip4_map
660 // All we need is the right destination address
661 ip60->dst_address.as_u64[0] =
662 map_get_pfx_net (d0, ip40->dst_address.as_u32, port0);
663 ip60->dst_address.as_u64[1] =
664 map_get_sfx_net (d0, ip40->dst_address.as_u32, port0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700666 if (PREDICT_FALSE
667 (d0->mtu
668 && (clib_net_to_host_u16 (ip60->payload_length) +
669 sizeof (*ip60) > d0->mtu)))
670 {
671 vnet_buffer (p0)->ip_frag.header_offset = sizeof (*ip60);
672 vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP6_LOOKUP;
673 vnet_buffer (p0)->ip_frag.mtu = d0->mtu;
674 vnet_buffer (p0)->ip_frag.flags = IP_FRAG_FLAG_IP6_HEADER;
675 next0 = IP4_MAP_REASS_NEXT_IP4_FRAGMENT;
676 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700678 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
679 {
680 map_ip4_map_reass_trace_t *tr =
681 vlib_add_trace (vm, node, p0, sizeof (*tr));
682 tr->map_domain_index = map_domain_index0;
683 tr->port = port0;
684 tr->cached = cached;
685 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700687 if (cached)
688 {
689 //Dequeue the packet
690 n_left_to_next++;
691 to_next--;
692 }
693 else
694 {
695 if (error0 == MAP_ERROR_NONE)
696 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_TX,
697 cpu_index, map_domain_index0,
698 1,
Ed Warnicke853e7202016-08-12 11:42:26 -0700699 clib_net_to_host_u16
700 (ip60->payload_length) + 40);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700701 next0 =
702 (error0 == MAP_ERROR_NONE) ? next0 : IP4_MAP_REASS_NEXT_DROP;
703 p0->error = error_node->errors[error0];
704 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
705 n_left_to_next, pi0, next0);
706 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700708 //Loopback when we reach the end of the inpu vector
709 if (n_left_from == 0 && vec_len (fragments_to_loopback))
710 {
711 from = vlib_frame_vector_args (frame);
712 u32 len = vec_len (fragments_to_loopback);
713 if (len <= VLIB_FRAME_SIZE)
714 {
715 clib_memcpy (from, fragments_to_loopback,
716 sizeof (u32) * len);
717 n_left_from = len;
718 vec_reset_length (fragments_to_loopback);
719 }
720 else
721 {
722 clib_memcpy (from,
723 fragments_to_loopback + (len -
724 VLIB_FRAME_SIZE),
725 sizeof (u32) * VLIB_FRAME_SIZE);
726 n_left_from = VLIB_FRAME_SIZE;
727 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
728 }
729 }
730 }
731 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700732 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700734 map_send_all_to_node (vm, fragments_to_drop, node,
735 &error_node->errors[MAP_ERROR_FRAGMENT_DROPPED],
736 IP4_MAP_REASS_NEXT_DROP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700738 vec_free (fragments_to_drop);
739 vec_free (fragments_to_loopback);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740 return frame->n_vectors;
741}
742
743static char *map_error_strings[] = {
744#define _(sym,string) string,
745 foreach_map_error
746#undef _
747};
748
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700749/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750VLIB_REGISTER_NODE(ip4_map_node) = {
751 .function = ip4_map,
752 .name = "ip4-map",
753 .vector_size = sizeof(u32),
754 .format_trace = format_map_trace,
755 .type = VLIB_NODE_TYPE_INTERNAL,
Damjan Marion607de1a2016-08-16 22:53:54 +0200756
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757 .n_errors = MAP_N_ERROR,
758 .error_strings = map_error_strings,
759
760 .n_next_nodes = IP4_MAP_N_NEXT,
761 .next_nodes = {
762 [IP4_MAP_NEXT_IP6_LOOKUP] = "ip6-lookup",
763#ifdef MAP_SKIP_IP6_LOOKUP
Neale Ranns80823802017-02-20 18:23:41 -0800764 [IP4_MAP_NEXT_IP6_REWRITE] = "ip6-load-balance",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765#endif
Ole Troan9fb87552016-01-13 22:30:43 +0100766 [IP4_MAP_NEXT_IP4_FRAGMENT] = "ip4-frag",
767 [IP4_MAP_NEXT_IP6_FRAGMENT] = "ip6-frag",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768 [IP4_MAP_NEXT_REASS] = "ip4-map-reass",
Ole Troan9fb87552016-01-13 22:30:43 +0100769 [IP4_MAP_NEXT_ICMP_ERROR] = "ip4-icmp-error",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770 [IP4_MAP_NEXT_DROP] = "error-drop",
771 },
772};
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700773/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700775/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700776VLIB_REGISTER_NODE(ip4_map_reass_node) = {
777 .function = ip4_map_reass,
778 .name = "ip4-map-reass",
779 .vector_size = sizeof(u32),
780 .format_trace = format_ip4_map_reass_trace,
781 .type = VLIB_NODE_TYPE_INTERNAL,
Damjan Marion607de1a2016-08-16 22:53:54 +0200782
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783 .n_errors = MAP_N_ERROR,
784 .error_strings = map_error_strings,
785
786 .n_next_nodes = IP4_MAP_REASS_N_NEXT,
787 .next_nodes = {
788 [IP4_MAP_REASS_NEXT_IP6_LOOKUP] = "ip6-lookup",
789 [IP4_MAP_REASS_NEXT_IP4_FRAGMENT] = "ip4-frag",
790 [IP4_MAP_REASS_NEXT_DROP] = "error-drop",
791 },
792};
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700793/* *INDENT-ON* */
794
795/*
796 * fd.io coding-style-patch-verification: ON
797 *
798 * Local Variables:
799 * eval: (c-set-style "gnu")
800 * End:
801 */