blob: 8ff1532081b9725da5998a3789d9fbe185c56939 [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#include "map.h"
16
17#include "../ip/ip_frag.h"
Matus Fabiana774b532017-05-02 03:15:22 -070018#include <vnet/ip/ip6_to_ip4.h>
19#include <vnet/ip/ip4_to_ip6.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070020
21#define IP6_MAP_T_DUAL_LOOP
22
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070023typedef enum
24{
Ed Warnickecb9cada2015-12-08 15:45:58 -070025 IP6_MAPT_NEXT_MAPT_TCP_UDP,
26 IP6_MAPT_NEXT_MAPT_ICMP,
27 IP6_MAPT_NEXT_MAPT_FRAGMENTED,
28 IP6_MAPT_NEXT_DROP,
29 IP6_MAPT_N_NEXT
30} ip6_mapt_next_t;
31
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070032typedef enum
33{
Ed Warnickecb9cada2015-12-08 15:45:58 -070034 IP6_MAPT_ICMP_NEXT_IP4_LOOKUP,
35 IP6_MAPT_ICMP_NEXT_IP4_FRAG,
36 IP6_MAPT_ICMP_NEXT_DROP,
37 IP6_MAPT_ICMP_N_NEXT
38} ip6_mapt_icmp_next_t;
39
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070040typedef enum
41{
Ed Warnickecb9cada2015-12-08 15:45:58 -070042 IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP,
43 IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG,
44 IP6_MAPT_TCP_UDP_NEXT_DROP,
45 IP6_MAPT_TCP_UDP_N_NEXT
46} ip6_mapt_tcp_udp_next_t;
47
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070048typedef enum
49{
Ed Warnickecb9cada2015-12-08 15:45:58 -070050 IP6_MAPT_FRAGMENTED_NEXT_IP4_LOOKUP,
51 IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG,
52 IP6_MAPT_FRAGMENTED_NEXT_DROP,
53 IP6_MAPT_FRAGMENTED_N_NEXT
54} ip6_mapt_fragmented_next_t;
55
56static_always_inline int
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070057ip6_map_fragment_cache (ip6_header_t * ip6, ip6_frag_hdr_t * frag,
58 map_domain_t * d, u16 port)
Ed Warnickecb9cada2015-12-08 15:45:58 -070059{
60 u32 *ignore = NULL;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070061 map_ip4_reass_lock ();
62 map_ip4_reass_t *r = map_ip4_reass_get (map_get_ip4 (&ip6->src_address),
63 ip6_map_t_embedded_address (d,
64 &ip6->
65 dst_address),
66 frag_id_6to4 (frag->identification),
67 (ip6->protocol ==
68 IP_PROTOCOL_ICMP6) ?
69 IP_PROTOCOL_ICMP : ip6->protocol,
70 &ignore);
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 if (r)
72 r->port = port;
73
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070074 map_ip4_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -070075 return !r;
76}
77
78/* Returns the associated port or -1 */
79static_always_inline i32
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070080ip6_map_fragment_get (ip6_header_t * ip6, ip6_frag_hdr_t * frag,
81 map_domain_t * d)
Ed Warnickecb9cada2015-12-08 15:45:58 -070082{
83 u32 *ignore = NULL;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -070084 map_ip4_reass_lock ();
85 map_ip4_reass_t *r = map_ip4_reass_get (map_get_ip4 (&ip6->src_address),
86 ip6_map_t_embedded_address (d,
87 &ip6->
88 dst_address),
89 frag_id_6to4 (frag->identification),
90 (ip6->protocol ==
91 IP_PROTOCOL_ICMP6) ?
92 IP_PROTOCOL_ICMP : ip6->protocol,
93 &ignore);
94 i32 ret = r ? r->port : -1;
95 map_ip4_reass_unlock ();
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 return ret;
97}
98
Matus Fabiana774b532017-05-02 03:15:22 -070099typedef struct
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100{
Matus Fabiana774b532017-05-02 03:15:22 -0700101 map_domain_t *d;
102 u16 sender_port;
103} icmp6_to_icmp_ctx_t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104
Matus Fabiana774b532017-05-02 03:15:22 -0700105static int
106ip6_to_ip4_set_icmp_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107{
Matus Fabiana774b532017-05-02 03:15:22 -0700108 icmp6_to_icmp_ctx_t *ctx = arg;
109 u32 ip4_sadr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700111 //Security check
112 //Note that this prevents an intermediate IPv6 router from answering the request
113 ip4_sadr = map_get_ip4 (&ip6->src_address);
Matus Fabiana774b532017-05-02 03:15:22 -0700114 if (ip6->src_address.as_u64[0] !=
115 map_get_pfx_net (ctx->d, ip4_sadr, ctx->sender_port)
116 || ip6->src_address.as_u64[1] != map_get_sfx_net (ctx->d, ip4_sadr,
117 ctx->sender_port))
118 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119
Matus Fabiana774b532017-05-02 03:15:22 -0700120 ip4->dst_address.as_u32 =
121 ip6_map_t_embedded_address (ctx->d, &ip6->dst_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 ip4->src_address.as_u32 = ip4_sadr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123
Matus Fabiana774b532017-05-02 03:15:22 -0700124 return 0;
125}
126
127static int
128ip6_to_ip4_set_inner_icmp_cb (ip6_header_t * ip6, ip4_header_t * ip4,
129 void *arg)
130{
131 icmp6_to_icmp_ctx_t *ctx = arg;
132 u32 inner_ip4_dadr;
133
134 //Security check of inner packet
135 inner_ip4_dadr = map_get_ip4 (&ip6->dst_address);
136 if (ip6->dst_address.as_u64[0] !=
137 map_get_pfx_net (ctx->d, inner_ip4_dadr, ctx->sender_port)
138 || ip6->dst_address.as_u64[1] != map_get_sfx_net (ctx->d,
139 inner_ip4_dadr,
140 ctx->sender_port))
141 return -1;
142
143 ip4->dst_address.as_u32 = inner_ip4_dadr;
144 ip4->src_address.as_u32 =
145 ip6_map_t_embedded_address (ctx->d, &ip6->src_address);
146
147 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148}
149
150static uword
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700151ip6_map_t_icmp (vlib_main_t * vm,
152 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153{
154 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700155 vlib_node_runtime_t *error_node =
156 vlib_node_get_runtime (vm, ip6_map_t_icmp_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 from = vlib_frame_vector_args (frame);
158 n_left_from = frame->n_vectors;
159 next_index = node->cached_next_index;
160 vlib_combined_counter_main_t *cm = map_main.domain_counters;
Damjan Marion586afd72017-04-05 19:18:20 +0200161 u32 thread_index = vlib_get_thread_index ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700163 while (n_left_from > 0)
164 {
165 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700167 while (n_left_from > 0 && n_left_to_next > 0)
168 {
169 u32 pi0;
170 vlib_buffer_t *p0;
171 u8 error0;
172 ip6_mapt_icmp_next_t next0;
173 map_domain_t *d0;
174 u16 len0;
Matus Fabiana774b532017-05-02 03:15:22 -0700175 icmp6_to_icmp_ctx_t ctx0;
176 ip6_header_t *ip60;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700178 pi0 = to_next[0] = from[0];
179 from += 1;
180 n_left_from -= 1;
181 to_next += 1;
182 n_left_to_next -= 1;
183 error0 = MAP_ERROR_NONE;
184 next0 = IP6_MAPT_ICMP_NEXT_IP4_LOOKUP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700186 p0 = vlib_get_buffer (vm, pi0);
Matus Fabiana774b532017-05-02 03:15:22 -0700187 ip60 = vlib_buffer_get_current (p0);
188 len0 = clib_net_to_host_u16 (ip60->payload_length);
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700189 d0 =
190 pool_elt_at_index (map_main.domains,
191 vnet_buffer (p0)->map_t.map_domain_index);
Juraj Sloboda51e5edc2017-12-13 11:53:32 +0100192 ctx0.sender_port = ip6_get_port (ip60, 0, p0->current_length);
Matus Fabiana774b532017-05-02 03:15:22 -0700193 ctx0.d = d0;
194 if (ctx0.sender_port == 0)
195 {
196 // In case of 1:1 mapping, we don't care about the port
197 if (!(d0->ea_bits_len == 0 && d0->rules))
198 {
199 error0 = MAP_ERROR_ICMP;
200 goto err0;
201 }
202 }
203
204 if (icmp6_to_icmp
205 (p0, ip6_to_ip4_set_icmp_cb, d0, ip6_to_ip4_set_inner_icmp_cb,
206 d0))
207 {
208 error0 = MAP_ERROR_ICMP;
209 goto err0;
210 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700212 if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
213 {
214 //Send to fragmentation node if necessary
215 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
216 vnet_buffer (p0)->ip_frag.header_offset = 0;
217 vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
218 next0 = IP6_MAPT_ICMP_NEXT_IP4_FRAG;
219 }
Matus Fabiana774b532017-05-02 03:15:22 -0700220 err0:
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700221 if (PREDICT_TRUE (error0 == MAP_ERROR_NONE))
222 {
223 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200224 thread_index,
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700225 vnet_buffer (p0)->
226 map_t.map_domain_index, 1,
227 len0);
228 }
229 else
230 {
231 next0 = IP6_MAPT_ICMP_NEXT_DROP;
232 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700234 p0->error = error_node->errors[error0];
235 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
236 to_next, n_left_to_next, pi0,
237 next0);
238 }
239 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 return frame->n_vectors;
242}
243
Matus Fabiana774b532017-05-02 03:15:22 -0700244static int
245ip6_to_ip4_set_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *ctx)
246{
247 vlib_buffer_t *p = ctx;
248
249 ip4->dst_address.as_u32 = vnet_buffer (p)->map_t.v6.daddr;
250 ip4->src_address.as_u32 = vnet_buffer (p)->map_t.v6.saddr;
251
252 return 0;
253}
254
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255static uword
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700256ip6_map_t_fragmented (vlib_main_t * vm,
257 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258{
259 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700260 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261 n_left_from = frame->n_vectors;
262 next_index = node->cached_next_index;
Matus Fabiana774b532017-05-02 03:15:22 -0700263 vlib_node_runtime_t *error_node =
264 vlib_node_get_runtime (vm, ip6_map_t_fragmented_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700266 while (n_left_from > 0)
267 {
268 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269
270#ifdef IP6_MAP_T_DUAL_LOOP
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700271 while (n_left_from >= 4 && n_left_to_next >= 2)
272 {
273 u32 pi0, pi1;
274 vlib_buffer_t *p0, *p1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700275 u32 next0, next1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700277 pi0 = to_next[0] = from[0];
278 pi1 = to_next[1] = from[1];
279 from += 2;
280 n_left_from -= 2;
281 to_next += 2;
282 n_left_to_next -= 2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700284 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
285 next1 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
286 p0 = vlib_get_buffer (vm, pi0);
287 p1 = vlib_get_buffer (vm, pi1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
Matus Fabiana774b532017-05-02 03:15:22 -0700289 if (ip6_to_ip4_fragmented (p0, ip6_to_ip4_set_cb, p0))
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700290 {
Matus Fabiana774b532017-05-02 03:15:22 -0700291 p0->error = error_node->errors[MAP_ERROR_FRAGMENT_DROPPED];
292 next0 = IP6_MAPT_FRAGMENTED_NEXT_DROP;
293 }
294 else
295 {
296 if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
297 {
298 //Send to fragmentation node if necessary
299 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
300 vnet_buffer (p0)->ip_frag.header_offset = 0;
301 vnet_buffer (p0)->ip_frag.next_index =
302 IP4_FRAG_NEXT_IP4_LOOKUP;
303 next0 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
304 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700305 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306
Matus Fabiana774b532017-05-02 03:15:22 -0700307 if (ip6_to_ip4_fragmented (p1, ip6_to_ip4_set_cb, p1))
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700308 {
Matus Fabiana774b532017-05-02 03:15:22 -0700309 p1->error = error_node->errors[MAP_ERROR_FRAGMENT_DROPPED];
310 next1 = IP6_MAPT_FRAGMENTED_NEXT_DROP;
311 }
312 else
313 {
314 if (vnet_buffer (p1)->map_t.mtu < p1->current_length)
315 {
316 //Send to fragmentation node if necessary
317 vnet_buffer (p1)->ip_frag.mtu = vnet_buffer (p1)->map_t.mtu;
318 vnet_buffer (p1)->ip_frag.header_offset = 0;
319 vnet_buffer (p1)->ip_frag.next_index =
320 IP4_FRAG_NEXT_IP4_LOOKUP;
321 next1 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
322 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700323 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700325 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
326 to_next, n_left_to_next, pi0, pi1,
327 next0, next1);
328 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329#endif
330
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700331 while (n_left_from > 0 && n_left_to_next > 0)
332 {
333 u32 pi0;
334 vlib_buffer_t *p0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700335 u32 next0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700337 pi0 = to_next[0] = from[0];
338 from += 1;
339 n_left_from -= 1;
340 to_next += 1;
341 n_left_to_next -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700343 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
344 p0 = vlib_get_buffer (vm, pi0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
Matus Fabiana774b532017-05-02 03:15:22 -0700346 if (ip6_to_ip4_fragmented (p0, ip6_to_ip4_set_cb, p0))
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700347 {
Matus Fabiana774b532017-05-02 03:15:22 -0700348 p0->error = error_node->errors[MAP_ERROR_FRAGMENT_DROPPED];
349 next0 = IP6_MAPT_FRAGMENTED_NEXT_DROP;
350 }
351 else
352 {
353 if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
354 {
355 //Send to fragmentation node if necessary
356 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
357 vnet_buffer (p0)->ip_frag.header_offset = 0;
358 vnet_buffer (p0)->ip_frag.next_index =
359 IP4_FRAG_NEXT_IP4_LOOKUP;
360 next0 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
361 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700362 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700364 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
365 to_next, n_left_to_next, pi0,
366 next0);
367 }
368 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370 return frame->n_vectors;
371}
372
373static uword
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700374ip6_map_t_tcp_udp (vlib_main_t * vm,
375 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376{
377 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
Matus Fabiana774b532017-05-02 03:15:22 -0700378 vlib_node_runtime_t *error_node =
379 vlib_node_get_runtime (vm, ip6_map_t_tcp_udp_node.index);
380
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700381 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382 n_left_from = frame->n_vectors;
383 next_index = node->cached_next_index;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700384 while (n_left_from > 0)
385 {
386 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387
388#ifdef IP6_MAP_T_DUAL_LOOP
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700389 while (n_left_from >= 4 && n_left_to_next >= 2)
390 {
391 u32 pi0, pi1;
392 vlib_buffer_t *p0, *p1;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700393 ip6_mapt_tcp_udp_next_t next0, next1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700395 pi0 = to_next[0] = from[0];
396 pi1 = to_next[1] = from[1];
397 from += 2;
398 n_left_from -= 2;
399 to_next += 2;
400 n_left_to_next -= 2;
401 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
402 next1 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700404 p0 = vlib_get_buffer (vm, pi0);
405 p1 = vlib_get_buffer (vm, pi1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406
Matus Fabiana774b532017-05-02 03:15:22 -0700407 if (ip6_to_ip4_tcp_udp (p0, ip6_to_ip4_set_cb, p0, 1))
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700408 {
Matus Fabiana774b532017-05-02 03:15:22 -0700409 p0->error = error_node->errors[MAP_ERROR_UNKNOWN];
410 next0 = IP6_MAPT_TCP_UDP_NEXT_DROP;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700411 }
412 else
413 {
Matus Fabiana774b532017-05-02 03:15:22 -0700414 if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
415 {
416 //Send to fragmentation node if necessary
417 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
418 vnet_buffer (p0)->ip_frag.header_offset = 0;
419 vnet_buffer (p0)->ip_frag.next_index =
420 IP4_FRAG_NEXT_IP4_LOOKUP;
421 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
422 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700423 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
Matus Fabiana774b532017-05-02 03:15:22 -0700425 if (ip6_to_ip4_tcp_udp (p1, ip6_to_ip4_set_cb, p1, 1))
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700426 {
Matus Fabiana774b532017-05-02 03:15:22 -0700427 p1->error = error_node->errors[MAP_ERROR_UNKNOWN];
428 next1 = IP6_MAPT_TCP_UDP_NEXT_DROP;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700429 }
430 else
431 {
Matus Fabiana774b532017-05-02 03:15:22 -0700432 if (vnet_buffer (p1)->map_t.mtu < p1->current_length)
433 {
434 //Send to fragmentation node if necessary
435 vnet_buffer (p1)->ip_frag.mtu = vnet_buffer (p1)->map_t.mtu;
436 vnet_buffer (p1)->ip_frag.header_offset = 0;
437 vnet_buffer (p1)->ip_frag.next_index =
438 IP4_FRAG_NEXT_IP4_LOOKUP;
439 next1 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
440 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700441 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700443 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
444 n_left_to_next, pi0, pi1, next0,
445 next1);
446 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447#endif
448
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700449 while (n_left_from > 0 && n_left_to_next > 0)
450 {
451 u32 pi0;
452 vlib_buffer_t *p0;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700453 ip6_mapt_tcp_udp_next_t next0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700455 pi0 = to_next[0] = from[0];
456 from += 1;
457 n_left_from -= 1;
458 to_next += 1;
459 n_left_to_next -= 1;
460 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700462 p0 = vlib_get_buffer (vm, pi0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463
Matus Fabiana774b532017-05-02 03:15:22 -0700464 if (ip6_to_ip4_tcp_udp (p0, ip6_to_ip4_set_cb, p0, 1))
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700465 {
Matus Fabiana774b532017-05-02 03:15:22 -0700466 p0->error = error_node->errors[MAP_ERROR_UNKNOWN];
467 next0 = IP6_MAPT_TCP_UDP_NEXT_DROP;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700468 }
469 else
470 {
Matus Fabiana774b532017-05-02 03:15:22 -0700471 if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
472 {
473 //Send to fragmentation node if necessary
474 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
475 vnet_buffer (p0)->ip_frag.header_offset = 0;
476 vnet_buffer (p0)->ip_frag.next_index =
477 IP4_FRAG_NEXT_IP4_LOOKUP;
478 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
479 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700480 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700482 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
483 to_next, n_left_to_next, pi0,
484 next0);
485 }
486 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488 return frame->n_vectors;
489}
490
491static_always_inline void
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700492ip6_map_t_classify (vlib_buffer_t * p0, ip6_header_t * ip60,
493 map_domain_t * d0, i32 * src_port0,
494 u8 * error0, ip6_mapt_next_t * next0,
495 u32 l4_len0, ip6_frag_hdr_t * frag0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496{
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700497 if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset &&
498 ip6_frag_hdr_offset (frag0)))
499 {
500 *next0 = IP6_MAPT_NEXT_MAPT_FRAGMENTED;
501 if (d0->ea_bits_len == 0 && d0->rules)
502 {
503 *src_port0 = 0;
504 }
505 else
506 {
507 *src_port0 = ip6_map_fragment_get (ip60, frag0, d0);
508 *error0 = (*src_port0 != -1) ? *error0 : MAP_ERROR_FRAGMENT_DROPPED;
509 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700511 else
512 if (PREDICT_TRUE
513 (vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_TCP))
514 {
515 *error0 =
516 l4_len0 < sizeof (tcp_header_t) ? MAP_ERROR_MALFORMED : *error0;
517 vnet_buffer (p0)->map_t.checksum_offset =
518 vnet_buffer (p0)->map_t.v6.l4_offset + 16;
519 *next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
520 *src_port0 =
521 (i32) *
522 ((u16 *) u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 }
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700524 else
525 if (PREDICT_TRUE
526 (vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_UDP))
527 {
528 *error0 =
529 l4_len0 < sizeof (udp_header_t) ? MAP_ERROR_MALFORMED : *error0;
530 vnet_buffer (p0)->map_t.checksum_offset =
531 vnet_buffer (p0)->map_t.v6.l4_offset + 6;
532 *next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
533 *src_port0 =
534 (i32) *
535 ((u16 *) u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset));
536 }
537 else if (vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_ICMP6)
538 {
539 *error0 =
540 l4_len0 < sizeof (icmp46_header_t) ? MAP_ERROR_MALFORMED : *error0;
541 *next0 = IP6_MAPT_NEXT_MAPT_ICMP;
542 if (d0->ea_bits_len == 0 && d0->rules)
543 {
544 *src_port0 = 0;
545 }
546 else
547 if (((icmp46_header_t *)
548 u8_ptr_add (ip60,
549 vnet_buffer (p0)->map_t.v6.l4_offset))->code ==
550 ICMP6_echo_reply
551 || ((icmp46_header_t *)
552 u8_ptr_add (ip60,
553 vnet_buffer (p0)->map_t.v6.l4_offset))->code ==
554 ICMP6_echo_request)
555 {
556 *src_port0 =
557 (i32) *
558 ((u16 *)
559 u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset + 6));
560 }
561 }
562 else
563 {
564 //TODO: In case of 1:1 mapping, it might be possible to do something with those packets.
565 *error0 = MAP_ERROR_BAD_PROTOCOL;
566 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567}
568
569static uword
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700570ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571{
572 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700573 vlib_node_runtime_t *error_node =
574 vlib_node_get_runtime (vm, ip6_map_t_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575 vlib_combined_counter_main_t *cm = map_main.domain_counters;
Damjan Marion586afd72017-04-05 19:18:20 +0200576 u32 thread_index = vlib_get_thread_index ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700578 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579 n_left_from = frame->n_vectors;
580 next_index = node->cached_next_index;
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700581 while (n_left_from > 0)
582 {
583 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584
585#ifdef IP6_MAP_T_DUAL_LOOP
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700586 while (n_left_from >= 4 && n_left_to_next >= 2)
587 {
588 u32 pi0, pi1;
589 vlib_buffer_t *p0, *p1;
590 ip6_header_t *ip60, *ip61;
591 u8 error0, error1;
592 ip6_mapt_next_t next0, next1;
593 u32 l4_len0, l4_len1;
594 i32 src_port0, src_port1;
595 map_domain_t *d0, *d1;
596 ip6_frag_hdr_t *frag0, *frag1;
597 u32 saddr0, saddr1;
598 next0 = next1 = 0; //Because compiler whines
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700600 pi0 = to_next[0] = from[0];
601 pi1 = to_next[1] = from[1];
602 from += 2;
603 n_left_from -= 2;
604 to_next += 2;
605 n_left_to_next -= 2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700607 error0 = MAP_ERROR_NONE;
608 error1 = MAP_ERROR_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700610 p0 = vlib_get_buffer (vm, pi0);
611 p1 = vlib_get_buffer (vm, pi1);
612 ip60 = vlib_buffer_get_current (p0);
613 ip61 = vlib_buffer_get_current (p1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700615 saddr0 = map_get_ip4 (&ip60->src_address);
616 saddr1 = map_get_ip4 (&ip61->src_address);
617 d0 = ip6_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
618 (ip4_address_t *) & saddr0,
619 &vnet_buffer (p0)->map_t.map_domain_index,
620 &error0);
621 d1 =
622 ip6_map_get_domain (vnet_buffer (p1)->ip.adj_index[VLIB_TX],
623 (ip4_address_t *) & saddr1,
624 &vnet_buffer (p1)->map_t.map_domain_index,
625 &error1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700627 vnet_buffer (p0)->map_t.v6.saddr = saddr0;
628 vnet_buffer (p1)->map_t.v6.saddr = saddr1;
629 vnet_buffer (p0)->map_t.v6.daddr =
630 ip6_map_t_embedded_address (d0, &ip60->dst_address);
631 vnet_buffer (p1)->map_t.v6.daddr =
632 ip6_map_t_embedded_address (d1, &ip61->dst_address);
633 vnet_buffer (p0)->map_t.mtu = d0->mtu ? d0->mtu : ~0;
634 vnet_buffer (p1)->map_t.mtu = d1->mtu ? d1->mtu : ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700636 if (PREDICT_FALSE (ip6_parse (ip60, p0->current_length,
637 &(vnet_buffer (p0)->map_t.
638 v6.l4_protocol),
639 &(vnet_buffer (p0)->map_t.
640 v6.l4_offset),
641 &(vnet_buffer (p0)->map_t.
642 v6.frag_offset))))
643 {
644 error0 = MAP_ERROR_MALFORMED;
645 next0 = IP6_MAPT_NEXT_DROP;
646 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700648 if (PREDICT_FALSE (ip6_parse (ip61, p1->current_length,
649 &(vnet_buffer (p1)->map_t.
650 v6.l4_protocol),
651 &(vnet_buffer (p1)->map_t.
652 v6.l4_offset),
653 &(vnet_buffer (p1)->map_t.
654 v6.frag_offset))))
655 {
656 error1 = MAP_ERROR_MALFORMED;
657 next1 = IP6_MAPT_NEXT_DROP;
658 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700660 src_port0 = src_port1 = -1;
661 l4_len0 = (u32) clib_net_to_host_u16 (ip60->payload_length) +
662 sizeof (*ip60) - vnet_buffer (p0)->map_t.v6.l4_offset;
663 l4_len1 = (u32) clib_net_to_host_u16 (ip61->payload_length) +
664 sizeof (*ip60) - vnet_buffer (p1)->map_t.v6.l4_offset;
665 frag0 =
666 (ip6_frag_hdr_t *) u8_ptr_add (ip60,
667 vnet_buffer (p0)->map_t.
668 v6.frag_offset);
669 frag1 =
670 (ip6_frag_hdr_t *) u8_ptr_add (ip61,
671 vnet_buffer (p1)->map_t.
672 v6.frag_offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700674 ip6_map_t_classify (p0, ip60, d0, &src_port0, &error0, &next0,
675 l4_len0, frag0);
676 ip6_map_t_classify (p1, ip61, d1, &src_port1, &error1, &next1,
677 l4_len1, frag1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700679 if (PREDICT_FALSE
680 ((src_port0 != -1)
681 && (ip60->src_address.as_u64[0] !=
682 map_get_pfx_net (d0, vnet_buffer (p0)->map_t.v6.saddr,
683 src_port0)
684 || ip60->src_address.as_u64[1] != map_get_sfx_net (d0,
685 vnet_buffer
686 (p0)->map_t.v6.saddr,
687 src_port0))))
688 {
689 error0 = MAP_ERROR_SEC_CHECK;
690 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700692 if (PREDICT_FALSE
693 ((src_port1 != -1)
694 && (ip61->src_address.as_u64[0] !=
695 map_get_pfx_net (d1, vnet_buffer (p1)->map_t.v6.saddr,
696 src_port1)
697 || ip61->src_address.as_u64[1] != map_get_sfx_net (d1,
698 vnet_buffer
699 (p1)->map_t.v6.saddr,
700 src_port1))))
701 {
702 error1 = MAP_ERROR_SEC_CHECK;
703 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700705 if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset &&
706 !ip6_frag_hdr_offset ((ip6_frag_hdr_t *)
707 u8_ptr_add (ip60,
708 vnet_buffer
709 (p0)->map_t.
710 v6.frag_offset)))
711 && (src_port0 != -1) && (d0->ea_bits_len != 0 || !d0->rules)
712 && (error0 == MAP_ERROR_NONE))
713 {
714 ip6_map_fragment_cache (ip60,
715 (ip6_frag_hdr_t *) u8_ptr_add (ip60,
716 vnet_buffer
717 (p0)->map_t.
718 v6.frag_offset),
719 d0, src_port0);
720 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700722 if (PREDICT_FALSE (vnet_buffer (p1)->map_t.v6.frag_offset &&
723 !ip6_frag_hdr_offset ((ip6_frag_hdr_t *)
724 u8_ptr_add (ip61,
725 vnet_buffer
726 (p1)->map_t.
727 v6.frag_offset)))
728 && (src_port1 != -1) && (d1->ea_bits_len != 0 || !d1->rules)
729 && (error1 == MAP_ERROR_NONE))
730 {
731 ip6_map_fragment_cache (ip61,
732 (ip6_frag_hdr_t *) u8_ptr_add (ip61,
733 vnet_buffer
734 (p1)->map_t.
735 v6.frag_offset),
736 d1, src_port1);
737 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700738
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700739 if (PREDICT_TRUE
740 (error0 == MAP_ERROR_NONE && next0 != IP6_MAPT_NEXT_MAPT_ICMP))
741 {
742 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200743 thread_index,
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700744 vnet_buffer (p0)->
745 map_t.map_domain_index, 1,
746 clib_net_to_host_u16
747 (ip60->payload_length));
748 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700750 if (PREDICT_TRUE
751 (error1 == MAP_ERROR_NONE && next1 != IP6_MAPT_NEXT_MAPT_ICMP))
752 {
753 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200754 thread_index,
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700755 vnet_buffer (p1)->
756 map_t.map_domain_index, 1,
757 clib_net_to_host_u16
758 (ip61->payload_length));
759 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700761 next0 = (error0 != MAP_ERROR_NONE) ? IP6_MAPT_NEXT_DROP : next0;
762 next1 = (error1 != MAP_ERROR_NONE) ? IP6_MAPT_NEXT_DROP : next1;
763 p0->error = error_node->errors[error0];
764 p1->error = error_node->errors[error1];
765 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
766 n_left_to_next, pi0, pi1, next0,
767 next1);
768 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769#endif
770
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700771 while (n_left_from > 0 && n_left_to_next > 0)
772 {
773 u32 pi0;
774 vlib_buffer_t *p0;
775 ip6_header_t *ip60;
776 u8 error0;
777 u32 l4_len0;
778 i32 src_port0;
779 map_domain_t *d0;
780 ip6_frag_hdr_t *frag0;
781 ip6_mapt_next_t next0 = 0;
782 u32 saddr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700784 pi0 = to_next[0] = from[0];
785 from += 1;
786 n_left_from -= 1;
787 to_next += 1;
788 n_left_to_next -= 1;
789 error0 = MAP_ERROR_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700791 p0 = vlib_get_buffer (vm, pi0);
792 ip60 = vlib_buffer_get_current (p0);
793 //Save saddr in a different variable to not overwrite ip.adj_index
794 saddr = map_get_ip4 (&ip60->src_address);
795 d0 = ip6_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
796 (ip4_address_t *) & saddr,
797 &vnet_buffer (p0)->map_t.map_domain_index,
798 &error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700800 //FIXME: What if d0 is null
801 vnet_buffer (p0)->map_t.v6.saddr = saddr;
802 vnet_buffer (p0)->map_t.v6.daddr =
803 ip6_map_t_embedded_address (d0, &ip60->dst_address);
804 vnet_buffer (p0)->map_t.mtu = d0->mtu ? d0->mtu : ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700806 if (PREDICT_FALSE (ip6_parse (ip60, p0->current_length,
807 &(vnet_buffer (p0)->map_t.
808 v6.l4_protocol),
809 &(vnet_buffer (p0)->map_t.
810 v6.l4_offset),
811 &(vnet_buffer (p0)->map_t.
812 v6.frag_offset))))
813 {
814 error0 = MAP_ERROR_MALFORMED;
815 next0 = IP6_MAPT_NEXT_DROP;
816 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700818 src_port0 = -1;
819 l4_len0 = (u32) clib_net_to_host_u16 (ip60->payload_length) +
820 sizeof (*ip60) - vnet_buffer (p0)->map_t.v6.l4_offset;
821 frag0 =
822 (ip6_frag_hdr_t *) u8_ptr_add (ip60,
823 vnet_buffer (p0)->map_t.
824 v6.frag_offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700825
826
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700827 if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset &&
828 ip6_frag_hdr_offset (frag0)))
829 {
830 src_port0 = ip6_map_fragment_get (ip60, frag0, d0);
831 error0 = (src_port0 != -1) ? error0 : MAP_ERROR_FRAGMENT_MEMORY;
832 next0 = IP6_MAPT_NEXT_MAPT_FRAGMENTED;
833 }
834 else
835 if (PREDICT_TRUE
836 (vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_TCP))
837 {
838 error0 =
839 l4_len0 <
840 sizeof (tcp_header_t) ? MAP_ERROR_MALFORMED : error0;
841 vnet_buffer (p0)->map_t.checksum_offset =
842 vnet_buffer (p0)->map_t.v6.l4_offset + 16;
843 next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
844 src_port0 =
845 (i32) *
846 ((u16 *)
847 u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset));
848 }
849 else
850 if (PREDICT_TRUE
851 (vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_UDP))
852 {
853 error0 =
854 l4_len0 <
855 sizeof (udp_header_t) ? MAP_ERROR_MALFORMED : error0;
856 vnet_buffer (p0)->map_t.checksum_offset =
857 vnet_buffer (p0)->map_t.v6.l4_offset + 6;
858 next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
859 src_port0 =
860 (i32) *
861 ((u16 *)
862 u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset));
863 }
864 else if (vnet_buffer (p0)->map_t.v6.l4_protocol ==
865 IP_PROTOCOL_ICMP6)
866 {
867 error0 =
868 l4_len0 <
869 sizeof (icmp46_header_t) ? MAP_ERROR_MALFORMED : error0;
870 next0 = IP6_MAPT_NEXT_MAPT_ICMP;
871 if (((icmp46_header_t *)
872 u8_ptr_add (ip60,
873 vnet_buffer (p0)->map_t.v6.l4_offset))->code ==
874 ICMP6_echo_reply
875 || ((icmp46_header_t *)
876 u8_ptr_add (ip60,
877 vnet_buffer (p0)->map_t.v6.
878 l4_offset))->code == ICMP6_echo_request)
879 src_port0 =
880 (i32) *
881 ((u16 *)
882 u8_ptr_add (ip60,
883 vnet_buffer (p0)->map_t.v6.l4_offset + 6));
884 }
885 else
886 {
887 //TODO: In case of 1:1 mapping, it might be possible to do something with those packets.
888 error0 = MAP_ERROR_BAD_PROTOCOL;
889 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700891 //Security check
892 if (PREDICT_FALSE
893 ((src_port0 != -1)
894 && (ip60->src_address.as_u64[0] !=
895 map_get_pfx_net (d0, vnet_buffer (p0)->map_t.v6.saddr,
896 src_port0)
897 || ip60->src_address.as_u64[1] != map_get_sfx_net (d0,
898 vnet_buffer
899 (p0)->map_t.v6.saddr,
900 src_port0))))
901 {
902 //Security check when src_port0 is not zero (non-first fragment, UDP or TCP)
903 error0 = MAP_ERROR_SEC_CHECK;
904 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700905
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700906 //Fragmented first packet needs to be cached for following packets
907 if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset &&
908 !ip6_frag_hdr_offset ((ip6_frag_hdr_t *)
909 u8_ptr_add (ip60,
910 vnet_buffer
911 (p0)->map_t.
912 v6.frag_offset)))
913 && (src_port0 != -1) && (d0->ea_bits_len != 0 || !d0->rules)
914 && (error0 == MAP_ERROR_NONE))
915 {
916 ip6_map_fragment_cache (ip60,
917 (ip6_frag_hdr_t *) u8_ptr_add (ip60,
918 vnet_buffer
919 (p0)->map_t.
920 v6.frag_offset),
921 d0, src_port0);
922 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700923
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700924 if (PREDICT_TRUE
925 (error0 == MAP_ERROR_NONE && next0 != IP6_MAPT_NEXT_MAPT_ICMP))
926 {
927 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200928 thread_index,
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700929 vnet_buffer (p0)->
930 map_t.map_domain_index, 1,
931 clib_net_to_host_u16
932 (ip60->payload_length));
933 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700934
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700935 next0 = (error0 != MAP_ERROR_NONE) ? IP6_MAPT_NEXT_DROP : next0;
936 p0->error = error_node->errors[error0];
937 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
938 to_next, n_left_to_next, pi0,
939 next0);
940 }
941 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943 return frame->n_vectors;
944}
945
946static char *map_t_error_strings[] = {
947#define _(sym,string) string,
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700948 foreach_map_error
Ed Warnickecb9cada2015-12-08 15:45:58 -0700949#undef _
950};
951
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700952/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700953VLIB_REGISTER_NODE(ip6_map_t_fragmented_node) = {
954 .function = ip6_map_t_fragmented,
955 .name = "ip6-map-t-fragmented",
956 .vector_size = sizeof (u32),
957 .format_trace = format_map_trace,
958 .type = VLIB_NODE_TYPE_INTERNAL,
959
960 .n_errors = MAP_N_ERROR,
961 .error_strings = map_t_error_strings,
962
963 .n_next_nodes = IP6_MAPT_FRAGMENTED_N_NEXT,
964 .next_nodes = {
965 [IP6_MAPT_FRAGMENTED_NEXT_IP4_LOOKUP] = "ip4-lookup",
966 [IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
967 [IP6_MAPT_FRAGMENTED_NEXT_DROP] = "error-drop",
968 },
969};
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700970/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700972/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700973VLIB_REGISTER_NODE(ip6_map_t_icmp_node) = {
974 .function = ip6_map_t_icmp,
975 .name = "ip6-map-t-icmp",
976 .vector_size = sizeof (u32),
977 .format_trace = format_map_trace,
978 .type = VLIB_NODE_TYPE_INTERNAL,
979
980 .n_errors = MAP_N_ERROR,
981 .error_strings = map_t_error_strings,
982
983 .n_next_nodes = IP6_MAPT_ICMP_N_NEXT,
984 .next_nodes = {
985 [IP6_MAPT_ICMP_NEXT_IP4_LOOKUP] = "ip4-lookup",
986 [IP6_MAPT_ICMP_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
987 [IP6_MAPT_ICMP_NEXT_DROP] = "error-drop",
988 },
989};
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700990/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700991
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -0700992/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700993VLIB_REGISTER_NODE(ip6_map_t_tcp_udp_node) = {
994 .function = ip6_map_t_tcp_udp,
995 .name = "ip6-map-t-tcp-udp",
996 .vector_size = sizeof (u32),
997 .format_trace = format_map_trace,
998 .type = VLIB_NODE_TYPE_INTERNAL,
999
1000 .n_errors = MAP_N_ERROR,
1001 .error_strings = map_t_error_strings,
1002
1003 .n_next_nodes = IP6_MAPT_TCP_UDP_N_NEXT,
1004 .next_nodes = {
1005 [IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP] = "ip4-lookup",
1006 [IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
1007 [IP6_MAPT_TCP_UDP_NEXT_DROP] = "error-drop",
1008 },
1009};
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001010/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001012/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001013VLIB_REGISTER_NODE(ip6_map_t_node) = {
1014 .function = ip6_map_t,
1015 .name = "ip6-map-t",
1016 .vector_size = sizeof(u32),
1017 .format_trace = format_map_trace,
1018 .type = VLIB_NODE_TYPE_INTERNAL,
1019
1020 .n_errors = MAP_N_ERROR,
1021 .error_strings = map_t_error_strings,
1022
1023 .n_next_nodes = IP6_MAPT_N_NEXT,
1024 .next_nodes = {
1025 [IP6_MAPT_NEXT_MAPT_TCP_UDP] = "ip6-map-t-tcp-udp",
1026 [IP6_MAPT_NEXT_MAPT_ICMP] = "ip6-map-t-icmp",
1027 [IP6_MAPT_NEXT_MAPT_FRAGMENTED] = "ip6-map-t-fragmented",
1028 [IP6_MAPT_NEXT_DROP] = "error-drop",
1029 },
1030};
Keith Burns (alagalah)06e3d072016-08-07 08:43:18 -07001031/* *INDENT-ON* */
1032
1033/*
1034 * fd.io coding-style-patch-verification: ON
1035 *
1036 * Local Variables:
1037 * eval: (c-set-style "gnu")
1038 * End:
1039 */