Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | #include "map.h" |
| 16 | |
| 17 | #include "../ip/ip_frag.h" |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 18 | #include <vnet/ip/ip4_to_ip6.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 19 | |
| 20 | #define IP4_MAP_T_DUAL_LOOP 1 |
| 21 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 22 | typedef enum |
| 23 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | IP4_MAPT_NEXT_MAPT_TCP_UDP, |
| 25 | IP4_MAPT_NEXT_MAPT_ICMP, |
| 26 | IP4_MAPT_NEXT_MAPT_FRAGMENTED, |
| 27 | IP4_MAPT_NEXT_DROP, |
| 28 | IP4_MAPT_N_NEXT |
| 29 | } ip4_mapt_next_t; |
| 30 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 31 | typedef enum |
| 32 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 33 | IP4_MAPT_ICMP_NEXT_IP6_LOOKUP, |
| 34 | IP4_MAPT_ICMP_NEXT_IP6_FRAG, |
| 35 | IP4_MAPT_ICMP_NEXT_DROP, |
| 36 | IP4_MAPT_ICMP_N_NEXT |
| 37 | } ip4_mapt_icmp_next_t; |
| 38 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 39 | typedef enum |
| 40 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | IP4_MAPT_TCP_UDP_NEXT_IP6_LOOKUP, |
| 42 | IP4_MAPT_TCP_UDP_NEXT_IP6_FRAG, |
| 43 | IP4_MAPT_TCP_UDP_NEXT_DROP, |
| 44 | IP4_MAPT_TCP_UDP_N_NEXT |
| 45 | } ip4_mapt_tcp_udp_next_t; |
| 46 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 47 | typedef enum |
| 48 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | IP4_MAPT_FRAGMENTED_NEXT_IP6_LOOKUP, |
| 50 | IP4_MAPT_FRAGMENTED_NEXT_IP6_FRAG, |
| 51 | IP4_MAPT_FRAGMENTED_NEXT_DROP, |
| 52 | IP4_MAPT_FRAGMENTED_N_NEXT |
| 53 | } ip4_mapt_fragmented_next_t; |
| 54 | |
| 55 | //This is used to pass information within the buffer data. |
| 56 | //Buffer structure being too small to contain big structures like this. |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 57 | /* *INDENT-OFF* */ |
| 58 | typedef CLIB_PACKED (struct { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | ip6_address_t daddr; |
| 60 | ip6_address_t saddr; |
| 61 | //IPv6 header + Fragmentation header will be here |
| 62 | //sizeof(ip6) + sizeof(ip_frag) - sizeof(ip4) |
| 63 | u8 unused[28]; |
| 64 | }) ip4_mapt_pseudo_header_t; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 65 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | |
| 68 | static_always_inline int |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 69 | ip4_map_fragment_cache (ip4_header_t * ip4, u16 port) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | { |
| 71 | u32 *ignore = NULL; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 72 | map_ip4_reass_lock (); |
| 73 | map_ip4_reass_t *r = |
| 74 | map_ip4_reass_get (ip4->src_address.as_u32, ip4->dst_address.as_u32, |
| 75 | ip4->fragment_id, |
| 76 | (ip4->protocol == |
| 77 | IP_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP6 : ip4->protocol, |
| 78 | &ignore); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | if (r) |
| 80 | r->port = port; |
| 81 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 82 | map_ip4_reass_unlock (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | return !r; |
| 84 | } |
| 85 | |
| 86 | static_always_inline i32 |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 87 | ip4_map_fragment_get_port (ip4_header_t * ip4) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | { |
| 89 | u32 *ignore = NULL; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 90 | map_ip4_reass_lock (); |
| 91 | map_ip4_reass_t *r = |
| 92 | map_ip4_reass_get (ip4->src_address.as_u32, ip4->dst_address.as_u32, |
| 93 | ip4->fragment_id, |
| 94 | (ip4->protocol == |
| 95 | IP_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP6 : ip4->protocol, |
| 96 | &ignore); |
| 97 | i32 ret = r ? r->port : -1; |
| 98 | map_ip4_reass_unlock (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | return ret; |
| 100 | } |
| 101 | |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 102 | typedef struct |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | { |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 104 | map_domain_t *d; |
| 105 | u16 recv_port; |
| 106 | } icmp_to_icmp6_ctx_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 108 | static int |
| 109 | ip4_to_ip6_set_icmp_cb (ip4_header_t * ip4, ip6_header_t * ip6, void *arg) |
| 110 | { |
| 111 | icmp_to_icmp6_ctx_t *ctx = arg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 113 | ip4_map_t_embedded_address (ctx->d, &ip6->src_address, &ip4->src_address); |
| 114 | ip6->dst_address.as_u64[0] = |
| 115 | map_get_pfx_net (ctx->d, ip4->dst_address.as_u32, ctx->recv_port); |
| 116 | ip6->dst_address.as_u64[1] = |
| 117 | map_get_sfx_net (ctx->d, ip4->dst_address.as_u32, ctx->recv_port); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 122 | static int |
| 123 | ip4_to_ip6_set_inner_icmp_cb (ip4_header_t * ip4, ip6_header_t * ip6, |
| 124 | void *arg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | { |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 126 | icmp_to_icmp6_ctx_t *ctx = arg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 128 | //Note that the source address is within the domain |
| 129 | //while the destination address is the one outside the domain |
| 130 | ip4_map_t_embedded_address (ctx->d, &ip6->dst_address, &ip4->dst_address); |
| 131 | ip6->src_address.as_u64[0] = |
| 132 | map_get_pfx_net (ctx->d, ip4->src_address.as_u32, ctx->recv_port); |
| 133 | ip6->src_address.as_u64[1] = |
| 134 | map_get_sfx_net (ctx->d, ip4->src_address.as_u32, ctx->recv_port); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 136 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static uword |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 140 | ip4_map_t_icmp (vlib_main_t * vm, |
| 141 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | { |
| 143 | u32 n_left_from, *from, next_index, *to_next, n_left_to_next; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 144 | vlib_node_runtime_t *error_node = |
| 145 | vlib_node_get_runtime (vm, ip4_map_t_icmp_node.index); |
| 146 | from = vlib_frame_vector_args (frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 147 | n_left_from = frame->n_vectors; |
| 148 | next_index = node->cached_next_index; |
| 149 | vlib_combined_counter_main_t *cm = map_main.domain_counters; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 150 | u32 thread_index = vlib_get_thread_index (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 152 | while (n_left_from > 0) |
| 153 | { |
| 154 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 156 | while (n_left_from > 0 && n_left_to_next > 0) |
| 157 | { |
| 158 | u32 pi0; |
| 159 | vlib_buffer_t *p0; |
| 160 | ip4_mapt_icmp_next_t next0; |
| 161 | u8 error0; |
| 162 | map_domain_t *d0; |
| 163 | u16 len0; |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 164 | icmp_to_icmp6_ctx_t ctx0; |
| 165 | ip4_header_t *ip40; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 167 | next0 = IP4_MAPT_ICMP_NEXT_IP6_LOOKUP; |
| 168 | pi0 = to_next[0] = from[0]; |
| 169 | from += 1; |
| 170 | n_left_from -= 1; |
| 171 | to_next += 1; |
| 172 | n_left_to_next -= 1; |
| 173 | error0 = MAP_ERROR_NONE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 175 | p0 = vlib_get_buffer (vm, pi0); |
| 176 | vlib_buffer_advance (p0, sizeof (ip4_mapt_pseudo_header_t)); //The pseudo-header is not used |
| 177 | len0 = |
| 178 | clib_net_to_host_u16 (((ip4_header_t *) |
| 179 | vlib_buffer_get_current (p0))->length); |
| 180 | d0 = |
| 181 | pool_elt_at_index (map_main.domains, |
| 182 | vnet_buffer (p0)->map_t.map_domain_index); |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 183 | |
| 184 | ip40 = vlib_buffer_get_current (p0); |
| 185 | ctx0.recv_port = ip4_get_port (ip40, 1); |
| 186 | ctx0.d = d0; |
| 187 | if (ctx0.recv_port == 0) |
| 188 | { |
| 189 | // In case of 1:1 mapping, we don't care about the port |
| 190 | if (!(d0->ea_bits_len == 0 && d0->rules)) |
| 191 | { |
| 192 | error0 = MAP_ERROR_ICMP; |
| 193 | goto err0; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (icmp_to_icmp6 |
| 198 | (p0, ip4_to_ip6_set_icmp_cb, &ctx0, |
| 199 | ip4_to_ip6_set_inner_icmp_cb, &ctx0)) |
| 200 | { |
| 201 | error0 = MAP_ERROR_ICMP; |
| 202 | goto err0; |
| 203 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 205 | if (vnet_buffer (p0)->map_t.mtu < p0->current_length) |
| 206 | { |
| 207 | vnet_buffer (p0)->ip_frag.header_offset = 0; |
| 208 | vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu; |
| 209 | vnet_buffer (p0)->ip_frag.next_index = IP6_FRAG_NEXT_IP6_LOOKUP; |
| 210 | next0 = IP4_MAPT_ICMP_NEXT_IP6_FRAG; |
| 211 | } |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 212 | err0: |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 213 | if (PREDICT_TRUE (error0 == MAP_ERROR_NONE)) |
| 214 | { |
| 215 | vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_TX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 216 | thread_index, |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 217 | vnet_buffer (p0)-> |
| 218 | map_t.map_domain_index, 1, |
| 219 | len0); |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 220 | } |
| 221 | else |
| 222 | { |
| 223 | next0 = IP4_MAPT_ICMP_NEXT_DROP; |
| 224 | } |
| 225 | p0->error = error_node->errors[error0]; |
| 226 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 227 | to_next, n_left_to_next, pi0, |
| 228 | next0); |
| 229 | } |
| 230 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | return frame->n_vectors; |
| 233 | } |
| 234 | |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 235 | static int |
| 236 | ip4_to_ip6_set_cb (ip4_header_t * ip4, ip6_header_t * ip6, void *ctx) |
| 237 | { |
| 238 | ip4_mapt_pseudo_header_t *pheader = ctx; |
| 239 | |
| 240 | ip6->dst_address.as_u64[0] = pheader->daddr.as_u64[0]; |
| 241 | ip6->dst_address.as_u64[1] = pheader->daddr.as_u64[1]; |
| 242 | ip6->src_address.as_u64[0] = pheader->saddr.as_u64[0]; |
| 243 | ip6->src_address.as_u64[1] = pheader->saddr.as_u64[1]; |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 248 | static uword |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 249 | ip4_map_t_fragmented (vlib_main_t * vm, |
| 250 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | { |
| 252 | u32 n_left_from, *from, next_index, *to_next, n_left_to_next; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 253 | from = vlib_frame_vector_args (frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | n_left_from = frame->n_vectors; |
| 255 | next_index = node->cached_next_index; |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 256 | vlib_node_runtime_t *error_node = |
| 257 | vlib_node_get_runtime (vm, ip4_map_t_fragmented_node.index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 259 | while (n_left_from > 0) |
| 260 | { |
| 261 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 263 | while (n_left_from > 0 && n_left_to_next > 0) |
| 264 | { |
| 265 | u32 pi0; |
| 266 | vlib_buffer_t *p0; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 267 | ip4_mapt_pseudo_header_t *pheader0; |
| 268 | ip4_mapt_fragmented_next_t next0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 269 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 270 | next0 = IP4_MAPT_FRAGMENTED_NEXT_IP6_LOOKUP; |
| 271 | pi0 = to_next[0] = from[0]; |
| 272 | from += 1; |
| 273 | n_left_from -= 1; |
| 274 | to_next += 1; |
| 275 | n_left_to_next -= 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 276 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 277 | p0 = vlib_get_buffer (vm, pi0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 278 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 279 | //Accessing pseudo header |
| 280 | pheader0 = vlib_buffer_get_current (p0); |
| 281 | vlib_buffer_advance (p0, sizeof (*pheader0)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 283 | if (ip4_to_ip6_fragmented (p0, ip4_to_ip6_set_cb, pheader0)) |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 284 | { |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 285 | p0->error = error_node->errors[MAP_ERROR_FRAGMENT_DROPPED]; |
| 286 | next0 = IP4_MAPT_FRAGMENTED_NEXT_DROP; |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | if (vnet_buffer (p0)->map_t.mtu < p0->current_length) |
| 291 | { |
| 292 | vnet_buffer (p0)->ip_frag.header_offset = 0; |
| 293 | vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu; |
| 294 | vnet_buffer (p0)->ip_frag.next_index = |
| 295 | IP6_FRAG_NEXT_IP6_LOOKUP; |
| 296 | next0 = IP4_MAPT_FRAGMENTED_NEXT_IP6_FRAG; |
| 297 | } |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 298 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 300 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 301 | to_next, n_left_to_next, pi0, |
| 302 | next0); |
| 303 | } |
| 304 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 305 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 306 | return frame->n_vectors; |
| 307 | } |
| 308 | |
| 309 | static uword |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 310 | ip4_map_t_tcp_udp (vlib_main_t * vm, |
| 311 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 312 | { |
| 313 | u32 n_left_from, *from, next_index, *to_next, n_left_to_next; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 314 | from = vlib_frame_vector_args (frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 315 | n_left_from = frame->n_vectors; |
| 316 | next_index = node->cached_next_index; |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 317 | vlib_node_runtime_t *error_node = |
| 318 | vlib_node_get_runtime (vm, ip4_map_t_tcp_udp_node.index); |
| 319 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 320 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 321 | while (n_left_from > 0) |
| 322 | { |
| 323 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 324 | |
| 325 | #ifdef IP4_MAP_T_DUAL_LOOP |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 326 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 327 | { |
| 328 | u32 pi0, pi1; |
| 329 | vlib_buffer_t *p0, *p1; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 330 | ip4_mapt_pseudo_header_t *pheader0, *pheader1; |
| 331 | ip4_mapt_tcp_udp_next_t next0, next1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 332 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 333 | pi0 = to_next[0] = from[0]; |
| 334 | pi1 = to_next[1] = from[1]; |
| 335 | from += 2; |
| 336 | n_left_from -= 2; |
| 337 | to_next += 2; |
| 338 | n_left_to_next -= 2; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 339 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 340 | next0 = IP4_MAPT_TCP_UDP_NEXT_IP6_LOOKUP; |
| 341 | next1 = IP4_MAPT_TCP_UDP_NEXT_IP6_LOOKUP; |
| 342 | p0 = vlib_get_buffer (vm, pi0); |
| 343 | p1 = vlib_get_buffer (vm, pi1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 344 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 345 | //Accessing pseudo header |
| 346 | pheader0 = vlib_buffer_get_current (p0); |
| 347 | pheader1 = vlib_buffer_get_current (p1); |
| 348 | vlib_buffer_advance (p0, sizeof (*pheader0)); |
| 349 | vlib_buffer_advance (p1, sizeof (*pheader1)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 351 | if (ip4_to_ip6_tcp_udp (p0, ip4_to_ip6_set_cb, pheader0)) |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 352 | { |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 353 | p0->error = error_node->errors[MAP_ERROR_UNKNOWN]; |
| 354 | next0 = IP4_MAPT_TCP_UDP_NEXT_DROP; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 355 | } |
| 356 | else |
| 357 | { |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 358 | if (vnet_buffer (p0)->map_t.mtu < p0->current_length) |
| 359 | { |
| 360 | //Send to fragmentation node if necessary |
| 361 | vnet_buffer (p0)->ip_frag.header_offset = 0; |
| 362 | vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu; |
| 363 | vnet_buffer (p0)->ip_frag.next_index = |
| 364 | IP6_FRAG_NEXT_IP6_LOOKUP; |
| 365 | next0 = IP4_MAPT_TCP_UDP_NEXT_IP6_FRAG; |
| 366 | } |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 367 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 368 | |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 369 | if (ip4_to_ip6_tcp_udp (p1, ip4_to_ip6_set_cb, pheader1)) |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 370 | { |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 371 | p1->error = error_node->errors[MAP_ERROR_UNKNOWN]; |
| 372 | next1 = IP4_MAPT_TCP_UDP_NEXT_DROP; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 373 | } |
| 374 | else |
| 375 | { |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 376 | if (vnet_buffer (p1)->map_t.mtu < p1->current_length) |
| 377 | { |
| 378 | //Send to fragmentation node if necessary |
| 379 | vnet_buffer (p1)->ip_frag.header_offset = 0; |
| 380 | vnet_buffer (p1)->ip_frag.mtu = vnet_buffer (p1)->map_t.mtu; |
| 381 | vnet_buffer (p1)->ip_frag.next_index = |
| 382 | IP6_FRAG_NEXT_IP6_LOOKUP; |
| 383 | next1 = IP4_MAPT_TCP_UDP_NEXT_IP6_FRAG; |
| 384 | } |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 385 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 387 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 388 | to_next, n_left_to_next, pi0, pi1, |
| 389 | next0, next1); |
| 390 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | #endif |
| 392 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 393 | while (n_left_from > 0 && n_left_to_next > 0) |
| 394 | { |
| 395 | u32 pi0; |
| 396 | vlib_buffer_t *p0; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 397 | ip4_mapt_pseudo_header_t *pheader0; |
| 398 | ip4_mapt_tcp_udp_next_t next0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 400 | pi0 = to_next[0] = from[0]; |
| 401 | from += 1; |
| 402 | n_left_from -= 1; |
| 403 | to_next += 1; |
| 404 | n_left_to_next -= 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 406 | next0 = IP4_MAPT_TCP_UDP_NEXT_IP6_LOOKUP; |
| 407 | p0 = vlib_get_buffer (vm, pi0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 408 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 409 | //Accessing pseudo header |
| 410 | pheader0 = vlib_buffer_get_current (p0); |
| 411 | vlib_buffer_advance (p0, sizeof (*pheader0)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 412 | |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 413 | if (ip4_to_ip6_tcp_udp (p0, ip4_to_ip6_set_cb, pheader0)) |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 414 | { |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 415 | p0->error = error_node->errors[MAP_ERROR_UNKNOWN]; |
| 416 | next0 = IP4_MAPT_TCP_UDP_NEXT_DROP; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 417 | } |
| 418 | else |
| 419 | { |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 420 | if (vnet_buffer (p0)->map_t.mtu < p0->current_length) |
| 421 | { |
| 422 | //Send to fragmentation node if necessary |
| 423 | vnet_buffer (p0)->ip_frag.header_offset = 0; |
| 424 | vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu; |
| 425 | vnet_buffer (p0)->ip_frag.next_index = |
| 426 | IP6_FRAG_NEXT_IP6_LOOKUP; |
| 427 | next0 = IP4_MAPT_TCP_UDP_NEXT_IP6_FRAG; |
| 428 | } |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 429 | } |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 430 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 431 | to_next, n_left_to_next, pi0, |
| 432 | next0); |
| 433 | } |
| 434 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 435 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 436 | |
| 437 | return frame->n_vectors; |
| 438 | } |
| 439 | |
| 440 | static_always_inline void |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 441 | ip4_map_t_classify (vlib_buffer_t * p0, map_domain_t * d0, |
| 442 | ip4_header_t * ip40, u16 ip4_len0, i32 * dst_port0, |
| 443 | u8 * error0, ip4_mapt_next_t * next0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 444 | { |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 445 | if (PREDICT_FALSE (ip4_get_fragment_offset (ip40))) |
| 446 | { |
| 447 | *next0 = IP4_MAPT_NEXT_MAPT_FRAGMENTED; |
| 448 | if (d0->ea_bits_len == 0 && d0->rules) |
| 449 | { |
| 450 | *dst_port0 = 0; |
| 451 | } |
| 452 | else |
| 453 | { |
| 454 | *dst_port0 = ip4_map_fragment_get_port (ip40); |
| 455 | *error0 = (*dst_port0 == -1) ? MAP_ERROR_FRAGMENT_MEMORY : *error0; |
| 456 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 457 | } |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 458 | else if (PREDICT_TRUE (ip40->protocol == IP_PROTOCOL_TCP)) |
| 459 | { |
| 460 | vnet_buffer (p0)->map_t.checksum_offset = 36; |
| 461 | *next0 = IP4_MAPT_NEXT_MAPT_TCP_UDP; |
| 462 | *error0 = ip4_len0 < 40 ? MAP_ERROR_MALFORMED : *error0; |
| 463 | *dst_port0 = (i32) * ((u16 *) u8_ptr_add (ip40, sizeof (*ip40) + 2)); |
| 464 | } |
| 465 | else if (PREDICT_TRUE (ip40->protocol == IP_PROTOCOL_UDP)) |
| 466 | { |
| 467 | vnet_buffer (p0)->map_t.checksum_offset = 26; |
| 468 | *next0 = IP4_MAPT_NEXT_MAPT_TCP_UDP; |
| 469 | *error0 = ip4_len0 < 28 ? MAP_ERROR_MALFORMED : *error0; |
| 470 | *dst_port0 = (i32) * ((u16 *) u8_ptr_add (ip40, sizeof (*ip40) + 2)); |
| 471 | } |
| 472 | else if (ip40->protocol == IP_PROTOCOL_ICMP) |
| 473 | { |
| 474 | *next0 = IP4_MAPT_NEXT_MAPT_ICMP; |
| 475 | if (d0->ea_bits_len == 0 && d0->rules) |
| 476 | *dst_port0 = 0; |
| 477 | else if (((icmp46_header_t *) u8_ptr_add (ip40, sizeof (*ip40)))->code |
| 478 | == ICMP4_echo_reply |
| 479 | || ((icmp46_header_t *) |
| 480 | u8_ptr_add (ip40, |
| 481 | sizeof (*ip40)))->code == ICMP4_echo_request) |
| 482 | *dst_port0 = (i32) * ((u16 *) u8_ptr_add (ip40, sizeof (*ip40) + 6)); |
| 483 | } |
| 484 | else |
| 485 | { |
| 486 | *error0 = MAP_ERROR_BAD_PROTOCOL; |
| 487 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static uword |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 491 | ip4_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 492 | { |
| 493 | u32 n_left_from, *from, next_index, *to_next, n_left_to_next; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 494 | vlib_node_runtime_t *error_node = |
| 495 | vlib_node_get_runtime (vm, ip4_map_t_node.index); |
| 496 | from = vlib_frame_vector_args (frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 497 | n_left_from = frame->n_vectors; |
| 498 | next_index = node->cached_next_index; |
| 499 | vlib_combined_counter_main_t *cm = map_main.domain_counters; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 500 | u32 thread_index = vlib_get_thread_index (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 501 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 502 | while (n_left_from > 0) |
| 503 | { |
| 504 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 505 | |
| 506 | #ifdef IP4_MAP_T_DUAL_LOOP |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 507 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 508 | { |
| 509 | u32 pi0, pi1; |
| 510 | vlib_buffer_t *p0, *p1; |
| 511 | ip4_header_t *ip40, *ip41; |
| 512 | map_domain_t *d0, *d1; |
| 513 | ip4_mapt_next_t next0 = 0, next1 = 0; |
| 514 | u16 ip4_len0, ip4_len1; |
| 515 | u8 error0, error1; |
| 516 | i32 dst_port0, dst_port1; |
| 517 | ip4_mapt_pseudo_header_t *pheader0, *pheader1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 518 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 519 | pi0 = to_next[0] = from[0]; |
| 520 | pi1 = to_next[1] = from[1]; |
| 521 | from += 2; |
| 522 | n_left_from -= 2; |
| 523 | to_next += 2; |
| 524 | n_left_to_next -= 2; |
| 525 | error0 = MAP_ERROR_NONE; |
| 526 | error1 = MAP_ERROR_NONE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 527 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 528 | p0 = vlib_get_buffer (vm, pi0); |
| 529 | p1 = vlib_get_buffer (vm, pi1); |
| 530 | ip40 = vlib_buffer_get_current (p0); |
| 531 | ip41 = vlib_buffer_get_current (p1); |
| 532 | ip4_len0 = clib_host_to_net_u16 (ip40->length); |
| 533 | ip4_len1 = clib_host_to_net_u16 (ip41->length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 534 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 535 | if (PREDICT_FALSE (p0->current_length < ip4_len0 || |
| 536 | ip40->ip_version_and_header_length != 0x45)) |
| 537 | { |
| 538 | error0 = MAP_ERROR_UNKNOWN; |
| 539 | next0 = IP4_MAPT_NEXT_DROP; |
| 540 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 541 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 542 | if (PREDICT_FALSE (p1->current_length < ip4_len1 || |
| 543 | ip41->ip_version_and_header_length != 0x45)) |
| 544 | { |
| 545 | error1 = MAP_ERROR_UNKNOWN; |
| 546 | next1 = IP4_MAPT_NEXT_DROP; |
| 547 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 548 | |
Neale Ranns | 9705c38 | 2017-02-20 20:29:41 -0800 | [diff] [blame] | 549 | vnet_buffer (p0)->map_t.map_domain_index = |
| 550 | vnet_buffer (p0)->ip.adj_index[VLIB_TX]; |
| 551 | d0 = ip4_map_get_domain (vnet_buffer (p0)->map_t.map_domain_index); |
| 552 | vnet_buffer (p1)->map_t.map_domain_index = |
| 553 | vnet_buffer (p1)->ip.adj_index[VLIB_TX]; |
| 554 | d1 = ip4_map_get_domain (vnet_buffer (p1)->map_t.map_domain_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 555 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 556 | vnet_buffer (p0)->map_t.mtu = d0->mtu ? d0->mtu : ~0; |
| 557 | vnet_buffer (p1)->map_t.mtu = d1->mtu ? d1->mtu : ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 558 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 559 | dst_port0 = -1; |
| 560 | dst_port1 = -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 561 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 562 | ip4_map_t_classify (p0, d0, ip40, ip4_len0, &dst_port0, &error0, |
| 563 | &next0); |
| 564 | ip4_map_t_classify (p1, d1, ip41, ip4_len1, &dst_port1, &error1, |
| 565 | &next1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 566 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 567 | //Add MAP-T pseudo header in front of the packet |
| 568 | vlib_buffer_advance (p0, -sizeof (*pheader0)); |
| 569 | vlib_buffer_advance (p1, -sizeof (*pheader1)); |
| 570 | pheader0 = vlib_buffer_get_current (p0); |
| 571 | pheader1 = vlib_buffer_get_current (p1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 572 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 573 | //Save addresses within the packet |
| 574 | ip4_map_t_embedded_address (d0, &pheader0->saddr, |
| 575 | &ip40->src_address); |
| 576 | ip4_map_t_embedded_address (d1, &pheader1->saddr, |
| 577 | &ip41->src_address); |
| 578 | pheader0->daddr.as_u64[0] = |
| 579 | map_get_pfx_net (d0, ip40->dst_address.as_u32, (u16) dst_port0); |
| 580 | pheader0->daddr.as_u64[1] = |
| 581 | map_get_sfx_net (d0, ip40->dst_address.as_u32, (u16) dst_port0); |
| 582 | pheader1->daddr.as_u64[0] = |
| 583 | map_get_pfx_net (d1, ip41->dst_address.as_u32, (u16) dst_port1); |
| 584 | pheader1->daddr.as_u64[1] = |
| 585 | map_get_sfx_net (d1, ip41->dst_address.as_u32, (u16) dst_port1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 586 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 587 | if (PREDICT_FALSE |
| 588 | (ip4_is_first_fragment (ip40) && (dst_port0 != -1) |
| 589 | && (d0->ea_bits_len != 0 || !d0->rules) |
| 590 | && ip4_map_fragment_cache (ip40, dst_port0))) |
| 591 | { |
| 592 | error0 = MAP_ERROR_FRAGMENT_MEMORY; |
| 593 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 594 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 595 | if (PREDICT_FALSE |
| 596 | (ip4_is_first_fragment (ip41) && (dst_port1 != -1) |
| 597 | && (d1->ea_bits_len != 0 || !d1->rules) |
| 598 | && ip4_map_fragment_cache (ip41, dst_port1))) |
| 599 | { |
| 600 | error1 = MAP_ERROR_FRAGMENT_MEMORY; |
| 601 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 602 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 603 | if (PREDICT_TRUE |
| 604 | (error0 == MAP_ERROR_NONE && next0 != IP4_MAPT_NEXT_MAPT_ICMP)) |
| 605 | { |
| 606 | vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_TX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 607 | thread_index, |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 608 | vnet_buffer (p0)-> |
| 609 | map_t.map_domain_index, 1, |
| 610 | clib_net_to_host_u16 |
| 611 | (ip40->length)); |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 612 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 613 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 614 | if (PREDICT_TRUE |
| 615 | (error1 == MAP_ERROR_NONE && next1 != IP4_MAPT_NEXT_MAPT_ICMP)) |
| 616 | { |
| 617 | vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_TX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 618 | thread_index, |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 619 | vnet_buffer (p1)-> |
| 620 | map_t.map_domain_index, 1, |
| 621 | clib_net_to_host_u16 |
| 622 | (ip41->length)); |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 623 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 624 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 625 | next0 = (error0 != MAP_ERROR_NONE) ? IP4_MAPT_NEXT_DROP : next0; |
| 626 | next1 = (error1 != MAP_ERROR_NONE) ? IP4_MAPT_NEXT_DROP : next1; |
| 627 | p0->error = error_node->errors[error0]; |
| 628 | p1->error = error_node->errors[error1]; |
| 629 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next, |
| 630 | n_left_to_next, pi0, pi1, next0, |
| 631 | next1); |
| 632 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 633 | #endif |
| 634 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 635 | while (n_left_from > 0 && n_left_to_next > 0) |
| 636 | { |
| 637 | u32 pi0; |
| 638 | vlib_buffer_t *p0; |
| 639 | ip4_header_t *ip40; |
| 640 | map_domain_t *d0; |
| 641 | ip4_mapt_next_t next0; |
| 642 | u16 ip4_len0; |
| 643 | u8 error0; |
| 644 | i32 dst_port0; |
| 645 | ip4_mapt_pseudo_header_t *pheader0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 646 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 647 | pi0 = to_next[0] = from[0]; |
| 648 | from += 1; |
| 649 | n_left_from -= 1; |
| 650 | to_next += 1; |
| 651 | n_left_to_next -= 1; |
| 652 | error0 = MAP_ERROR_NONE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 653 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 654 | p0 = vlib_get_buffer (vm, pi0); |
| 655 | ip40 = vlib_buffer_get_current (p0); |
| 656 | ip4_len0 = clib_host_to_net_u16 (ip40->length); |
| 657 | if (PREDICT_FALSE (p0->current_length < ip4_len0 || |
| 658 | ip40->ip_version_and_header_length != 0x45)) |
| 659 | { |
| 660 | error0 = MAP_ERROR_UNKNOWN; |
| 661 | next0 = IP4_MAPT_NEXT_DROP; |
| 662 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 663 | |
Neale Ranns | 9705c38 | 2017-02-20 20:29:41 -0800 | [diff] [blame] | 664 | vnet_buffer (p0)->map_t.map_domain_index = |
| 665 | vnet_buffer (p0)->ip.adj_index[VLIB_TX]; |
| 666 | d0 = ip4_map_get_domain (vnet_buffer (p0)->map_t.map_domain_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 667 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 668 | vnet_buffer (p0)->map_t.mtu = d0->mtu ? d0->mtu : ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 669 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 670 | dst_port0 = -1; |
| 671 | ip4_map_t_classify (p0, d0, ip40, ip4_len0, &dst_port0, &error0, |
| 672 | &next0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 673 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 674 | //Add MAP-T pseudo header in front of the packet |
| 675 | vlib_buffer_advance (p0, -sizeof (*pheader0)); |
| 676 | pheader0 = vlib_buffer_get_current (p0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 677 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 678 | //Save addresses within the packet |
| 679 | ip4_map_t_embedded_address (d0, &pheader0->saddr, |
| 680 | &ip40->src_address); |
| 681 | pheader0->daddr.as_u64[0] = |
| 682 | map_get_pfx_net (d0, ip40->dst_address.as_u32, (u16) dst_port0); |
| 683 | pheader0->daddr.as_u64[1] = |
| 684 | map_get_sfx_net (d0, ip40->dst_address.as_u32, (u16) dst_port0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 685 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 686 | //It is important to cache at this stage because the result might be necessary |
| 687 | //for packets within the same vector. |
| 688 | //Actually, this approach even provides some limited out-of-order fragments support |
| 689 | if (PREDICT_FALSE |
| 690 | (ip4_is_first_fragment (ip40) && (dst_port0 != -1) |
| 691 | && (d0->ea_bits_len != 0 || !d0->rules) |
| 692 | && ip4_map_fragment_cache (ip40, dst_port0))) |
| 693 | { |
| 694 | error0 = MAP_ERROR_UNKNOWN; |
| 695 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 696 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 697 | if (PREDICT_TRUE |
| 698 | (error0 == MAP_ERROR_NONE && next0 != IP4_MAPT_NEXT_MAPT_ICMP)) |
| 699 | { |
| 700 | vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_TX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 701 | thread_index, |
Matus Fabian | a774b53 | 2017-05-02 03:15:22 -0700 | [diff] [blame^] | 702 | vnet_buffer (p0)-> |
| 703 | map_t.map_domain_index, 1, |
| 704 | clib_net_to_host_u16 |
| 705 | (ip40->length)); |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 706 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 707 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 708 | next0 = (error0 != MAP_ERROR_NONE) ? IP4_MAPT_NEXT_DROP : next0; |
| 709 | p0->error = error_node->errors[error0]; |
| 710 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 711 | to_next, n_left_to_next, pi0, |
| 712 | next0); |
| 713 | } |
| 714 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 715 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 716 | return frame->n_vectors; |
| 717 | } |
| 718 | |
| 719 | static char *map_t_error_strings[] = { |
| 720 | #define _(sym,string) string, |
| 721 | foreach_map_error |
| 722 | #undef _ |
| 723 | }; |
| 724 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 725 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 726 | VLIB_REGISTER_NODE(ip4_map_t_fragmented_node) = { |
| 727 | .function = ip4_map_t_fragmented, |
| 728 | .name = "ip4-map-t-fragmented", |
| 729 | .vector_size = sizeof(u32), |
| 730 | .format_trace = format_map_trace, |
| 731 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 732 | |
| 733 | .n_errors = MAP_N_ERROR, |
| 734 | .error_strings = map_t_error_strings, |
| 735 | |
| 736 | .n_next_nodes = IP4_MAPT_FRAGMENTED_N_NEXT, |
| 737 | .next_nodes = { |
| 738 | [IP4_MAPT_FRAGMENTED_NEXT_IP6_LOOKUP] = "ip6-lookup", |
| 739 | [IP4_MAPT_FRAGMENTED_NEXT_IP6_FRAG] = IP6_FRAG_NODE_NAME, |
| 740 | [IP4_MAPT_FRAGMENTED_NEXT_DROP] = "error-drop", |
| 741 | }, |
| 742 | }; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 743 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 744 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 745 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 746 | VLIB_REGISTER_NODE(ip4_map_t_icmp_node) = { |
| 747 | .function = ip4_map_t_icmp, |
| 748 | .name = "ip4-map-t-icmp", |
| 749 | .vector_size = sizeof(u32), |
| 750 | .format_trace = format_map_trace, |
| 751 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 752 | |
| 753 | .n_errors = MAP_N_ERROR, |
| 754 | .error_strings = map_t_error_strings, |
| 755 | |
| 756 | .n_next_nodes = IP4_MAPT_ICMP_N_NEXT, |
| 757 | .next_nodes = { |
| 758 | [IP4_MAPT_ICMP_NEXT_IP6_LOOKUP] = "ip6-lookup", |
| 759 | [IP4_MAPT_ICMP_NEXT_IP6_FRAG] = IP6_FRAG_NODE_NAME, |
| 760 | [IP4_MAPT_ICMP_NEXT_DROP] = "error-drop", |
| 761 | }, |
| 762 | }; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 763 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 764 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 765 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 766 | VLIB_REGISTER_NODE(ip4_map_t_tcp_udp_node) = { |
| 767 | .function = ip4_map_t_tcp_udp, |
| 768 | .name = "ip4-map-t-tcp-udp", |
| 769 | .vector_size = sizeof(u32), |
| 770 | .format_trace = format_map_trace, |
| 771 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 772 | |
| 773 | .n_errors = MAP_N_ERROR, |
| 774 | .error_strings = map_t_error_strings, |
| 775 | |
| 776 | .n_next_nodes = IP4_MAPT_TCP_UDP_N_NEXT, |
| 777 | .next_nodes = { |
| 778 | [IP4_MAPT_TCP_UDP_NEXT_IP6_LOOKUP] = "ip6-lookup", |
| 779 | [IP4_MAPT_TCP_UDP_NEXT_IP6_FRAG] = IP6_FRAG_NODE_NAME, |
| 780 | [IP4_MAPT_TCP_UDP_NEXT_DROP] = "error-drop", |
| 781 | }, |
| 782 | }; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 783 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 784 | |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 785 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 786 | VLIB_REGISTER_NODE(ip4_map_t_node) = { |
| 787 | .function = ip4_map_t, |
| 788 | .name = "ip4-map-t", |
| 789 | .vector_size = sizeof(u32), |
| 790 | .format_trace = format_map_trace, |
| 791 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 792 | |
| 793 | .n_errors = MAP_N_ERROR, |
| 794 | .error_strings = map_t_error_strings, |
| 795 | |
| 796 | .n_next_nodes = IP4_MAPT_N_NEXT, |
| 797 | .next_nodes = { |
| 798 | [IP4_MAPT_NEXT_MAPT_TCP_UDP] = "ip4-map-t-tcp-udp", |
| 799 | [IP4_MAPT_NEXT_MAPT_ICMP] = "ip4-map-t-icmp", |
| 800 | [IP4_MAPT_NEXT_MAPT_FRAGMENTED] = "ip4-map-t-fragmented", |
| 801 | [IP4_MAPT_NEXT_DROP] = "error-drop", |
| 802 | }, |
| 803 | }; |
Keith Burns (alagalah) | 06e3d07 | 2016-08-07 08:43:18 -0700 | [diff] [blame] | 804 | /* *INDENT-ON* */ |
| 805 | |
| 806 | /* |
| 807 | * fd.io coding-style-patch-verification: ON |
| 808 | * |
| 809 | * Local Variables: |
| 810 | * eval: (c-set-style "gnu") |
| 811 | * End: |
| 812 | */ |