http: fix client receiving large data
[vpp.git] / src / plugins / map / map.h
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 #ifndef included_map_h
16 #define included_map_h
17
18 #include <stdbool.h>
19 #include <vppinfra/error.h>
20 #include <vnet/vnet.h>
21 #include <vnet/ip/ip.h>
22 #include <vlib/vlib.h>
23 #include <vnet/fib/fib_types.h>
24 #include <vnet/fib/ip4_fib.h>
25 #include <vnet/adj/adj.h>
26 #include <vnet/dpo/load_balance.h>
27 #include "lpm.h"
28 #include <vppinfra/lock.h>
29 #include <map/map.api_enum.h>
30
31 #define MAP_SKIP_IP6_LOOKUP 1
32
33 #define MAP_ERR_GOOD                    0
34 #define MAP_ERR_BAD_POOL_SIZE           -1
35 #define MAP_ERR_BAD_HT_RATIO            -2
36 #define MAP_ERR_BAD_LIFETIME            -3
37 #define MAP_ERR_BAD_BUFFERS             -4
38 #define MAP_ERR_BAD_BUFFERS_TOO_LARGE   -5
39 #define MAP_ERR_UNSUPPORTED             -6
40
41 int map_create_domain (ip4_address_t * ip4_prefix, u8 ip4_prefix_len,
42                        ip6_address_t * ip6_prefix, u8 ip6_prefix_len,
43                        ip6_address_t * ip6_src, u8 ip6_src_len,
44                        u8 ea_bits_len, u8 psid_offset, u8 psid_length,
45                        u32 * map_domain_index, u16 mtu, u8 flags, u8 * tag);
46 int map_delete_domain (u32 map_domain_index);
47 int map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
48                       bool is_add);
49 int map_if_enable_disable (bool is_enable, u32 sw_if_index,
50                            bool is_translation);
51 u8 *format_map_trace (u8 * s, va_list * args);
52
53 int map_param_set_fragmentation (bool inner, bool ignore_df);
54 int map_param_set_icmp (ip4_address_t * ip4_err_relay_src);
55 int map_param_set_icmp6 (u8 enable_unreachable);
56 void map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6, bool is_del);
57 int map_param_set_security_check (bool enable, bool fragments);
58 int map_param_set_traffic_class (bool copy, u8 tc);
59 int map_param_set_tcp (u16 tcp_mss);
60
61
62 typedef enum
63 {
64   MAP_DOMAIN_PREFIX = 1 << 0,
65   MAP_DOMAIN_TRANSLATION = 1 << 1,      // The domain uses MAP-T
66   MAP_DOMAIN_RFC6052 = 1 << 2,
67 } __attribute__ ((__packed__)) map_domain_flags_e;
68
69 //#define IP6_MAP_T_OVERRIDE_TOS 0
70
71 /*
72  * This structure _MUST_ be no larger than a single cache line (64 bytes).
73  * If more space is needed make a union of ip6_prefix and *rules, as
74  * those are mutually exclusive.
75  */
76 typedef struct
77 {
78   /* Required for pool_get_aligned */
79   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
80   ip6_address_t ip6_src;
81   ip6_address_t ip6_prefix;
82   ip6_address_t *rules;
83   u32 suffix_mask;
84   ip4_address_t ip4_prefix;
85   u16 psid_mask;
86   u16 mtu;
87   map_domain_flags_e flags;
88   u8 ip6_prefix_len;
89   u8 ip6_src_len;
90   u8 ea_bits_len;
91   u8 psid_offset;
92   u8 psid_length;
93
94   /* helpers */
95   u8 psid_shift;
96   u8 suffix_shift;
97   u8 ea_shift;
98
99   /* not used by forwarding */
100   u8 ip4_prefix_len;
101 } map_domain_t;
102
103 STATIC_ASSERT ((sizeof (map_domain_t) <= CLIB_CACHE_LINE_BYTES),
104                "MAP domain fits in one cacheline");
105
106 /*
107  * Extra data about a domain that doesn't need to be time/space critical.
108  * This structure is in a vector parallel to the main map_domain_t,
109  * and indexed by the same map-domain-index values.
110  */
111 typedef struct
112 {
113   u8 *tag;                      /* Probably a user-assigned domain name. */
114 } map_domain_extra_t;
115
116 #define MAP_REASS_INDEX_NONE ((u16)0xffff)
117
118 /*
119  * MAP domain counters
120  */
121 typedef enum
122 {
123   /* Simple counters */
124   MAP_DOMAIN_IPV4_FRAGMENT = 0,
125   /* Combined counters */
126   MAP_DOMAIN_COUNTER_RX = 0,
127   MAP_DOMAIN_COUNTER_TX,
128   MAP_N_DOMAIN_COUNTER
129 } map_domain_counter_t;
130
131 #ifdef MAP_SKIP_IP6_LOOKUP
132 /**
133  * A pre-resolved next-hop
134  */
135 typedef struct map_main_pre_resolved_t_
136 {
137   /**
138    * Linkage into the FIB graph
139    */
140   fib_node_t node;
141
142   /**
143    * The FIB entry index of the next-hop
144    */
145   fib_node_index_t fei;
146
147   /**
148    * This object sibling index on the FIB entry's child dependency list
149    */
150   u32 sibling;
151
152   /**
153    * The Load-balance object index to use to forward
154    */
155   dpo_id_t dpo;
156 } map_main_pre_resolved_t;
157
158 /**
159  * Pre-resolved next hops for v4 and v6. Why these are global and not
160  * per-domain is beyond me.
161  */
162 extern map_main_pre_resolved_t pre_resolved[FIB_PROTOCOL_MAX];
163 #endif
164
165 typedef struct
166 {
167   /* pool of MAP domains */
168   map_domain_t *domains;
169   map_domain_extra_t *domain_extras;
170
171   /* MAP Domain packet/byte counters indexed by map domain index */
172   vlib_simple_counter_main_t *simple_domain_counters;
173   vlib_combined_counter_main_t *domain_counters;
174   volatile u32 *counter_lock;
175
176   /* API message id base */
177   u16 msg_id_base;
178
179   /* Traffic class: zero, copy (~0) or fixed value */
180   u8 tc;
181   bool tc_copy;
182
183   bool sec_check;               /* Inbound security check */
184   bool sec_check_frag;          /* Inbound security check for (subsequent) fragments */
185   bool icmp6_enabled;           /* Send destination unreachable for security check failure */
186
187   u16 tcp_mss;                  /* TCP MSS clamp value */
188
189   /* ICMPv6 -> ICMPv4 relay parameters */
190   ip4_address_t icmp4_src_address;
191   vlib_simple_counter_main_t icmp_relayed;
192
193   /* convenience */
194   vlib_main_t *vlib_main;
195   vnet_main_t *vnet_main;
196
197   bool frag_inner;              /* Inner or outer fragmentation */
198   bool frag_ignore_df;          /* Fragment (outer) packet even if DF is set */
199
200   /* Graph node state */
201   uword *bm_trans_enabled_by_sw_if;
202   uword *bm_encap_enabled_by_sw_if;
203
204   /* Lookup tables */
205   lpm_t *ip4_prefix_tbl;
206   lpm_t *ip6_prefix_tbl;
207   lpm_t *ip6_src_prefix_tbl;
208
209   uword ip4_sv_reass_custom_next_index;
210 } map_main_t;
211
212 typedef vl_counter_map_enum_t map_error_t;
213 u64 map_error_counter_get (u32 node_index, map_error_t map_error);
214
215 typedef struct
216 {
217   u32 map_domain_index;
218   u16 port;
219 } map_trace_t;
220
221 always_inline void
222 map_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
223                vlib_buffer_t * b, u32 map_domain_index, u16 port)
224 {
225   map_trace_t *tr = vlib_add_trace (vm, node, b, sizeof (*tr));
226   tr->map_domain_index = map_domain_index;
227   tr->port = port;
228 }
229
230 extern map_main_t map_main;
231
232 extern vlib_node_registration_t ip4_map_node;
233 extern vlib_node_registration_t ip6_map_node;
234
235 extern vlib_node_registration_t ip4_map_t_node;
236 extern vlib_node_registration_t ip4_map_t_fragmented_node;
237 extern vlib_node_registration_t ip4_map_t_tcp_udp_node;
238 extern vlib_node_registration_t ip4_map_t_icmp_node;
239
240 extern vlib_node_registration_t ip6_map_t_node;
241 extern vlib_node_registration_t ip6_map_t_fragmented_node;
242 extern vlib_node_registration_t ip6_map_t_tcp_udp_node;
243 extern vlib_node_registration_t ip6_map_t_icmp_node;
244
245 /*
246  * map_get_pfx
247  */
248 static_always_inline u64
249 map_get_pfx (map_domain_t * d, u32 addr, u16 port)
250 {
251   u16 psid = (port >> d->psid_shift) & d->psid_mask;
252
253   if (d->ea_bits_len == 0 && d->rules)
254     return clib_net_to_host_u64 (d->rules[psid].as_u64[0]);
255
256   u32 suffix = (addr >> d->suffix_shift) & d->suffix_mask;
257   u64 ea =
258     d->ea_bits_len == 0 ? 0 : (((u64) suffix << d->psid_length)) | psid;
259
260   return clib_net_to_host_u64 (d->ip6_prefix.as_u64[0]) | ea << d->ea_shift;
261 }
262
263 static_always_inline u64
264 map_get_pfx_net (map_domain_t * d, u32 addr, u16 port)
265 {
266   return clib_host_to_net_u64 (map_get_pfx (d, clib_net_to_host_u32 (addr),
267                                             clib_net_to_host_u16 (port)));
268 }
269
270 /*
271  * map_get_sfx
272  */
273 static_always_inline u64
274 map_get_sfx (map_domain_t * d, u32 addr, u16 port)
275 {
276   u16 psid = (port >> d->psid_shift) & d->psid_mask;
277
278   /* Shared 1:1 mode. */
279   if (d->ea_bits_len == 0 && d->rules)
280     return clib_net_to_host_u64 (d->rules[psid].as_u64[1]);
281   if (d->ip6_prefix_len == 128)
282     return clib_net_to_host_u64 (d->ip6_prefix.as_u64[1]);
283
284   if (d->ip6_src_len == 96)
285     return (clib_net_to_host_u64 (d->ip6_prefix.as_u64[1]) | addr);
286
287   /* IPv4 prefix */
288   if (d->flags & MAP_DOMAIN_PREFIX)
289     return (u64) (addr & (0xFFFFFFFF << d->suffix_shift)) << 16;
290
291   /* Shared or full IPv4 address */
292   return ((u64) addr << 16) | psid;
293 }
294
295 static_always_inline u64
296 map_get_sfx_net (map_domain_t * d, u32 addr, u16 port)
297 {
298   return clib_host_to_net_u64 (map_get_sfx (d, clib_net_to_host_u32 (addr),
299                                             clib_net_to_host_u16 (port)));
300 }
301
302 static_always_inline u32
303 map_get_ip4 (ip6_address_t * addr, u16 prefix_len)
304 {
305   ASSERT (prefix_len == 64 || prefix_len == 96);
306   if (prefix_len == 96)
307     return clib_host_to_net_u32 (clib_net_to_host_u64 (addr->as_u64[1]));
308   else
309     return clib_host_to_net_u32 (clib_net_to_host_u64 (addr->as_u64[1]) >>
310                                  16);
311 }
312
313 static_always_inline map_domain_t *
314 ip4_map_get_domain (ip4_address_t * addr, u32 * map_domain_index, u8 * error)
315 {
316   map_main_t *mm = &map_main;
317
318   u32 mdi = mm->ip4_prefix_tbl->lookup (mm->ip4_prefix_tbl, addr, 32);
319   if (mdi == ~0)
320     {
321       *error = MAP_ERROR_NO_DOMAIN;
322       return 0;
323     }
324   *map_domain_index = mdi;
325   return pool_elt_at_index (mm->domains, mdi);
326 }
327
328 /*
329  * Get the MAP domain from an IPv6 address.
330  * If the IPv6 address or
331  * prefix is shared the IPv4 address must be used.
332  */
333 static_always_inline map_domain_t *
334 ip6_map_get_domain (ip6_address_t * addr, u32 * map_domain_index, u8 * error)
335 {
336   map_main_t *mm = &map_main;
337   u32 mdi =
338     /* This is the old src (ip6 destination) hash lookup [dgeist]
339      *
340      * mm->ip6_src_prefix_tbl->lookup (mm->ip6_src_prefix_tbl, addr, 128);
341      */
342     mm->ip6_prefix_tbl->lookup (mm->ip6_prefix_tbl, addr, 128);
343   if (mdi == ~0)
344     {
345       *error = MAP_ERROR_NO_DOMAIN;
346       return 0;
347     }
348
349   *map_domain_index = mdi;
350   return pool_elt_at_index (mm->domains, mdi);
351 }
352
353 clib_error_t *map_plugin_api_hookup (vlib_main_t * vm);
354
355 void map_ip6_drop_pi (u32 pi);
356
357 /*
358  * Supports prefix of 96 or 64 (with u-octet)
359  */
360 static_always_inline void
361 ip4_map_t_embedded_address (map_domain_t * d,
362                             ip6_address_t * ip6, const ip4_address_t * ip4)
363 {
364   ASSERT (d->ip6_src_len == 96 || d->ip6_src_len == 64);        //No support for other lengths for now
365   u8 offset = d->ip6_src_len == 64 ? 9 : 12;
366   ip6->as_u64[0] = d->ip6_src.as_u64[0];
367   ip6->as_u64[1] = d->ip6_src.as_u64[1];
368   clib_memcpy_fast (&ip6->as_u8[offset], ip4, 4);
369 }
370
371 static_always_inline u32
372 ip6_map_t_embedded_address (map_domain_t * d, ip6_address_t * addr)
373 {
374   ASSERT (d->ip6_src_len == 64 || d->ip6_src_len == 96);
375   u32 x;
376   u8 offset = d->ip6_src_len == 64 ? 9 : 12;
377   clib_memcpy (&x, &addr->as_u8[offset], 4);
378   return x;
379 }
380
381 static inline void
382 map_domain_counter_lock (map_main_t * mm)
383 {
384   if (mm->counter_lock)
385     while (clib_atomic_test_and_set (mm->counter_lock))
386       /* zzzz */ ;
387 }
388
389 static inline void
390 map_domain_counter_unlock (map_main_t * mm)
391 {
392   if (mm->counter_lock)
393     clib_atomic_release (mm->counter_lock);
394 }
395
396
397 static_always_inline void
398 map_send_all_to_node (vlib_main_t * vm, u32 * pi_vector,
399                       vlib_node_runtime_t * node, vlib_error_t * error,
400                       u32 next)
401 {
402   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
403   //Deal with fragments that are ready
404   from = pi_vector;
405   n_left_from = vec_len (pi_vector);
406   next_index = node->cached_next_index;
407   while (n_left_from > 0)
408     {
409       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
410       while (n_left_from > 0 && n_left_to_next > 0)
411         {
412           u32 pi0 = to_next[0] = from[0];
413           from += 1;
414           n_left_from -= 1;
415           to_next += 1;
416           n_left_to_next -= 1;
417           vlib_buffer_t *p0 = vlib_get_buffer (vm, pi0);
418           p0->error = *error;
419           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
420                                            n_left_to_next, pi0, next);
421         }
422       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
423     }
424 }
425
426 static_always_inline void
427 map_mss_clamping (tcp_header_t * tcp, ip_csum_t * sum, u16 mss_clamping)
428 {
429   u8 *data;
430   u8 opt_len, opts_len, kind;
431   u16 mss;
432   u16 mss_value_net = clib_host_to_net_u16 (mss_clamping);
433
434   if (!tcp_syn (tcp))
435     return;
436
437   opts_len = (tcp_doff (tcp) << 2) - sizeof (tcp_header_t);
438   data = (u8 *) (tcp + 1);
439   for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
440     {
441       kind = data[0];
442
443       if (kind == TCP_OPTION_EOL)
444         break;
445       else if (kind == TCP_OPTION_NOOP)
446         {
447           opt_len = 1;
448           continue;
449         }
450       else
451         {
452           if (opts_len < 2)
453             return;
454           opt_len = data[1];
455
456           if (opt_len < 2 || opt_len > opts_len)
457             return;
458         }
459
460       if (kind == TCP_OPTION_MSS)
461         {
462           mss = *(u16 *) (data + 2);
463           if (clib_net_to_host_u16 (mss) > mss_clamping)
464             {
465               *sum =
466                 ip_csum_update (*sum, mss, mss_value_net, ip4_header_t,
467                                 length);
468               clib_memcpy (data + 2, &mss_value_net, 2);
469             }
470           return;
471         }
472     }
473 }
474
475 static_always_inline bool
476 ip4_map_ip6_lookup_bypass (vlib_buffer_t * p0, ip4_header_t * ip)
477 {
478 #ifdef MAP_SKIP_IP6_LOOKUP
479   if (FIB_NODE_INDEX_INVALID != pre_resolved[FIB_PROTOCOL_IP6].fei)
480     {
481       vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
482         pre_resolved[FIB_PROTOCOL_IP6].dpo.dpoi_index;
483       return (true);
484     }
485 #endif
486   return (false);
487 }
488
489 static_always_inline bool
490 ip6_map_ip4_lookup_bypass (vlib_buffer_t * p0, ip4_header_t * ip)
491 {
492 #ifdef MAP_SKIP_IP6_LOOKUP
493   if (FIB_NODE_INDEX_INVALID != pre_resolved[FIB_PROTOCOL_IP4].fei)
494     {
495       vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
496         pre_resolved[FIB_PROTOCOL_IP4].dpo.dpoi_index;
497       return (true);
498     }
499 #endif
500   return (false);
501 }
502
503 #endif
504 /*
505  * fd.io coding-style-patch-verification: ON
506  *
507  * Local Variables:
508  * eval: (c-set-style "gnu")
509  * End:
510  */