2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vppinfra/error.h>
17 #include <vnet/vnet.h>
18 #include <vnet/ip/ip.h>
19 #include <vlib/vlib.h>
20 #include <vnet/fib/fib_types.h>
21 #include <vnet/fib/ip4_fib.h>
22 #include <vnet/adj/adj.h>
23 #include <vnet/dpo/load_balance.h>
25 #include <vppinfra/lock.h>
26 #include <map/map.api_enum.h>
28 #define MAP_SKIP_IP6_LOOKUP 1
30 #define MAP_ERR_GOOD 0
31 #define MAP_ERR_BAD_POOL_SIZE -1
32 #define MAP_ERR_BAD_HT_RATIO -2
33 #define MAP_ERR_BAD_LIFETIME -3
34 #define MAP_ERR_BAD_BUFFERS -4
35 #define MAP_ERR_BAD_BUFFERS_TOO_LARGE -5
36 #define MAP_ERR_UNSUPPORTED -6
38 int map_create_domain (ip4_address_t * ip4_prefix, u8 ip4_prefix_len,
39 ip6_address_t * ip6_prefix, u8 ip6_prefix_len,
40 ip6_address_t * ip6_src, u8 ip6_src_len,
41 u8 ea_bits_len, u8 psid_offset, u8 psid_length,
42 u32 * map_domain_index, u16 mtu, u8 flags, u8 * tag);
43 int map_delete_domain (u32 map_domain_index);
44 int map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
46 int map_if_enable_disable (bool is_enable, u32 sw_if_index,
48 u8 *format_map_trace (u8 * s, va_list * args);
50 int map_param_set_fragmentation (bool inner, bool ignore_df);
51 int map_param_set_icmp (ip4_address_t * ip4_err_relay_src);
52 int map_param_set_icmp6 (u8 enable_unreachable);
53 void map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6, bool is_del);
54 int map_param_set_security_check (bool enable, bool fragments);
55 int map_param_set_traffic_class (bool copy, u8 tc);
56 int map_param_set_tcp (u16 tcp_mss);
61 MAP_DOMAIN_PREFIX = 1 << 0,
62 MAP_DOMAIN_TRANSLATION = 1 << 1, // The domain uses MAP-T
63 MAP_DOMAIN_RFC6052 = 1 << 2,
64 } __attribute__ ((__packed__)) map_domain_flags_e;
66 //#define IP6_MAP_T_OVERRIDE_TOS 0
69 * This structure _MUST_ be no larger than a single cache line (64 bytes).
70 * If more space is needed make a union of ip6_prefix and *rules, as
71 * those are mutually exclusive.
75 /* Required for pool_get_aligned */
76 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
77 ip6_address_t ip6_src;
78 ip6_address_t ip6_prefix;
81 ip4_address_t ip4_prefix;
84 map_domain_flags_e flags;
96 /* not used by forwarding */
100 STATIC_ASSERT ((sizeof (map_domain_t) <= CLIB_CACHE_LINE_BYTES),
101 "MAP domain fits in one cacheline");
104 * Extra data about a domain that doesn't need to be time/space critical.
105 * This structure is in a vector parallel to the main map_domain_t,
106 * and indexed by the same map-domain-index values.
110 u8 *tag; /* Probably a user-assigned domain name. */
111 } map_domain_extra_t;
113 #define MAP_REASS_INDEX_NONE ((u16)0xffff)
116 * MAP domain counters
120 /* Simple counters */
121 MAP_DOMAIN_IPV4_FRAGMENT = 0,
122 /* Combined counters */
123 MAP_DOMAIN_COUNTER_RX = 0,
124 MAP_DOMAIN_COUNTER_TX,
126 } map_domain_counter_t;
128 #ifdef MAP_SKIP_IP6_LOOKUP
130 * A pre-resolved next-hop
132 typedef struct map_main_pre_resolved_t_
135 * Linkage into the FIB graph
140 * The FIB entry index of the next-hop
142 fib_node_index_t fei;
145 * This object sibling index on the FIB entry's child dependency list
150 * The Load-balance object index to use to forward
153 } map_main_pre_resolved_t;
156 * Pre-resolved next hops for v4 and v6. Why these are global and not
157 * per-domain is beyond me.
159 extern map_main_pre_resolved_t pre_resolved[FIB_PROTOCOL_MAX];
164 /* pool of MAP domains */
165 map_domain_t *domains;
166 map_domain_extra_t *domain_extras;
168 /* MAP Domain packet/byte counters indexed by map domain index */
169 vlib_simple_counter_main_t *simple_domain_counters;
170 vlib_combined_counter_main_t *domain_counters;
171 volatile u32 *counter_lock;
173 /* API message id base */
176 /* Traffic class: zero, copy (~0) or fixed value */
180 bool sec_check; /* Inbound security check */
181 bool sec_check_frag; /* Inbound security check for (subsequent) fragments */
182 bool icmp6_enabled; /* Send destination unreachable for security check failure */
184 u16 tcp_mss; /* TCP MSS clamp value */
186 /* ICMPv6 -> ICMPv4 relay parameters */
187 ip4_address_t icmp4_src_address;
188 vlib_simple_counter_main_t icmp_relayed;
191 vlib_main_t *vlib_main;
192 vnet_main_t *vnet_main;
194 bool frag_inner; /* Inner or outer fragmentation */
195 bool frag_ignore_df; /* Fragment (outer) packet even if DF is set */
197 /* Graph node state */
198 uword *bm_trans_enabled_by_sw_if;
199 uword *bm_encap_enabled_by_sw_if;
202 lpm_t *ip4_prefix_tbl;
203 lpm_t *ip6_prefix_tbl;
204 lpm_t *ip6_src_prefix_tbl;
206 uword ip4_sv_reass_custom_next_index;
209 typedef vl_counter_map_enum_t map_error_t;
210 u64 map_error_counter_get (u32 node_index, map_error_t map_error);
214 u32 map_domain_index;
219 map_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
220 vlib_buffer_t * b, u32 map_domain_index, u16 port)
222 map_trace_t *tr = vlib_add_trace (vm, node, b, sizeof (*tr));
223 tr->map_domain_index = map_domain_index;
227 extern map_main_t map_main;
229 extern vlib_node_registration_t ip4_map_node;
230 extern vlib_node_registration_t ip6_map_node;
232 extern vlib_node_registration_t ip4_map_t_node;
233 extern vlib_node_registration_t ip4_map_t_fragmented_node;
234 extern vlib_node_registration_t ip4_map_t_tcp_udp_node;
235 extern vlib_node_registration_t ip4_map_t_icmp_node;
237 extern vlib_node_registration_t ip6_map_t_node;
238 extern vlib_node_registration_t ip6_map_t_fragmented_node;
239 extern vlib_node_registration_t ip6_map_t_tcp_udp_node;
240 extern vlib_node_registration_t ip6_map_t_icmp_node;
245 static_always_inline u64
246 map_get_pfx (map_domain_t * d, u32 addr, u16 port)
248 u16 psid = (port >> d->psid_shift) & d->psid_mask;
250 if (d->ea_bits_len == 0 && d->rules)
251 return clib_net_to_host_u64 (d->rules[psid].as_u64[0]);
253 u32 suffix = (addr >> d->suffix_shift) & d->suffix_mask;
255 d->ea_bits_len == 0 ? 0 : (((u64) suffix << d->psid_length)) | psid;
257 return clib_net_to_host_u64 (d->ip6_prefix.as_u64[0]) | ea << d->ea_shift;
260 static_always_inline u64
261 map_get_pfx_net (map_domain_t * d, u32 addr, u16 port)
263 return clib_host_to_net_u64 (map_get_pfx (d, clib_net_to_host_u32 (addr),
264 clib_net_to_host_u16 (port)));
270 static_always_inline u64
271 map_get_sfx (map_domain_t * d, u32 addr, u16 port)
273 u16 psid = (port >> d->psid_shift) & d->psid_mask;
275 /* Shared 1:1 mode. */
276 if (d->ea_bits_len == 0 && d->rules)
277 return clib_net_to_host_u64 (d->rules[psid].as_u64[1]);
278 if (d->ip6_prefix_len == 128)
279 return clib_net_to_host_u64 (d->ip6_prefix.as_u64[1]);
281 if (d->ip6_src_len == 96)
282 return (clib_net_to_host_u64 (d->ip6_prefix.as_u64[1]) | addr);
285 if (d->flags & MAP_DOMAIN_PREFIX)
286 return (u64) (addr & (0xFFFFFFFF << d->suffix_shift)) << 16;
288 /* Shared or full IPv4 address */
289 return ((u64) addr << 16) | psid;
292 static_always_inline u64
293 map_get_sfx_net (map_domain_t * d, u32 addr, u16 port)
295 return clib_host_to_net_u64 (map_get_sfx (d, clib_net_to_host_u32 (addr),
296 clib_net_to_host_u16 (port)));
299 static_always_inline u32
300 map_get_ip4 (ip6_address_t * addr, u16 prefix_len)
302 ASSERT (prefix_len == 64 || prefix_len == 96);
303 if (prefix_len == 96)
304 return clib_host_to_net_u32 (clib_net_to_host_u64 (addr->as_u64[1]));
306 return clib_host_to_net_u32 (clib_net_to_host_u64 (addr->as_u64[1]) >>
310 static_always_inline map_domain_t *
311 ip4_map_get_domain (ip4_address_t * addr, u32 * map_domain_index, u8 * error)
313 map_main_t *mm = &map_main;
315 u32 mdi = mm->ip4_prefix_tbl->lookup (mm->ip4_prefix_tbl, addr, 32);
318 *error = MAP_ERROR_NO_DOMAIN;
321 *map_domain_index = mdi;
322 return pool_elt_at_index (mm->domains, mdi);
326 * Get the MAP domain from an IPv6 address.
327 * If the IPv6 address or
328 * prefix is shared the IPv4 address must be used.
330 static_always_inline map_domain_t *
331 ip6_map_get_domain (ip6_address_t * addr, u32 * map_domain_index, u8 * error)
333 map_main_t *mm = &map_main;
335 mm->ip6_src_prefix_tbl->lookup (mm->ip6_src_prefix_tbl, addr, 128);
338 *error = MAP_ERROR_NO_DOMAIN;
342 *map_domain_index = mdi;
343 return pool_elt_at_index (mm->domains, mdi);
346 clib_error_t *map_plugin_api_hookup (vlib_main_t * vm);
348 void map_ip6_drop_pi (u32 pi);
351 * Supports prefix of 96 or 64 (with u-octet)
353 static_always_inline void
354 ip4_map_t_embedded_address (map_domain_t * d,
355 ip6_address_t * ip6, const ip4_address_t * ip4)
357 ASSERT (d->ip6_src_len == 96 || d->ip6_src_len == 64); //No support for other lengths for now
358 u8 offset = d->ip6_src_len == 64 ? 9 : 12;
359 ip6->as_u64[0] = d->ip6_src.as_u64[0];
360 ip6->as_u64[1] = d->ip6_src.as_u64[1];
361 clib_memcpy_fast (&ip6->as_u8[offset], ip4, 4);
364 static_always_inline u32
365 ip6_map_t_embedded_address (map_domain_t * d, ip6_address_t * addr)
367 ASSERT (d->ip6_src_len == 64 || d->ip6_src_len == 96);
369 u8 offset = d->ip6_src_len == 64 ? 9 : 12;
370 clib_memcpy (&x, &addr->as_u8[offset], 4);
375 map_domain_counter_lock (map_main_t * mm)
377 if (mm->counter_lock)
378 while (clib_atomic_test_and_set (mm->counter_lock))
383 map_domain_counter_unlock (map_main_t * mm)
385 if (mm->counter_lock)
386 clib_atomic_release (mm->counter_lock);
390 static_always_inline void
391 map_send_all_to_node (vlib_main_t * vm, u32 * pi_vector,
392 vlib_node_runtime_t * node, vlib_error_t * error,
395 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
396 //Deal with fragments that are ready
398 n_left_from = vec_len (pi_vector);
399 next_index = node->cached_next_index;
400 while (n_left_from > 0)
402 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
403 while (n_left_from > 0 && n_left_to_next > 0)
405 u32 pi0 = to_next[0] = from[0];
410 vlib_buffer_t *p0 = vlib_get_buffer (vm, pi0);
412 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
413 n_left_to_next, pi0, next);
415 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
419 static_always_inline void
420 map_mss_clamping (tcp_header_t * tcp, ip_csum_t * sum, u16 mss_clamping)
423 u8 opt_len, opts_len, kind;
425 u16 mss_value_net = clib_host_to_net_u16 (mss_clamping);
430 opts_len = (tcp_doff (tcp) << 2) - sizeof (tcp_header_t);
431 data = (u8 *) (tcp + 1);
432 for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
436 if (kind == TCP_OPTION_EOL)
438 else if (kind == TCP_OPTION_NOOP)
449 if (opt_len < 2 || opt_len > opts_len)
453 if (kind == TCP_OPTION_MSS)
455 mss = *(u16 *) (data + 2);
456 if (clib_net_to_host_u16 (mss) > mss_clamping)
459 ip_csum_update (*sum, mss, mss_value_net, ip4_header_t,
461 clib_memcpy (data + 2, &mss_value_net, 2);
468 static_always_inline bool
469 ip4_map_ip6_lookup_bypass (vlib_buffer_t * p0, ip4_header_t * ip)
471 #ifdef MAP_SKIP_IP6_LOOKUP
472 if (FIB_NODE_INDEX_INVALID != pre_resolved[FIB_PROTOCOL_IP6].fei)
474 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
475 pre_resolved[FIB_PROTOCOL_IP6].dpo.dpoi_index;
482 static_always_inline bool
483 ip6_map_ip4_lookup_bypass (vlib_buffer_t * p0, ip4_header_t * ip)
485 #ifdef MAP_SKIP_IP6_LOOKUP
486 if (FIB_NODE_INDEX_INVALID != pre_resolved[FIB_PROTOCOL_IP4].fei)
488 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
489 pre_resolved[FIB_PROTOCOL_IP4].dpo.dpoi_index;
497 * fd.io coding-style-patch-verification: ON
500 * eval: (c-set-style "gnu")