ipsec: Tunnel SA DSCP behaviour
[vpp.git] / src / vnet / ip / ip6_packet.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 /*
16  * ip6/packet.h: ip6 packet format
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #ifndef included_ip6_packet_h
41 #define included_ip6_packet_h
42
43 #include <vnet/tcp/tcp_packet.h>
44 #include <vnet/ip/ip4_packet.h>
45
46 typedef union
47 {
48   u8 as_u8[16];
49   u16 as_u16[8];
50   u32 as_u32[4];
51   u64 as_u64[2];
52   u64x2 as_u128;
53   uword as_uword[16 / sizeof (uword)];
54 }
55 __clib_packed ip6_address_t;
56
57 STATIC_ASSERT_SIZEOF (ip6_address_t, 16);
58
59 typedef struct
60 {
61   ip6_address_t addr, mask;
62 } ip6_address_and_mask_t;
63
64 /* Packed so that the mhash key doesn't include uninitialized pad bytes */
65 /* *INDENT-OFF* */
66 typedef CLIB_PACKED (struct {
67   /* IP address must be first for ip_interface_address_get_address() to work */
68   ip6_address_t ip6_addr;
69   u32 fib_index;
70 }) ip6_address_fib_t;
71 /* *INDENT-ON* */
72
73 always_inline void
74 ip6_addr_fib_init (ip6_address_fib_t * addr_fib,
75                    const ip6_address_t * address, u32 fib_index)
76 {
77   addr_fib->ip6_addr = *address;
78   addr_fib->fib_index = fib_index;
79 }
80
81 /* Special addresses:
82    unspecified          ::/128
83    loopback             ::1/128
84    global unicast       2000::/3
85    unique local unicast fc00::/7
86    link local unicast   fe80::/10
87    multicast            ff00::/8
88    ietf reserved        everything else. */
89
90 #define foreach_ip6_multicast_address_scope     \
91   _ (loopback, 0x1)                             \
92   _ (link_local, 0x2)                           \
93   _ (admin_local, 0x4)                          \
94   _ (site_local, 0x5)                           \
95   _ (organization_local, 0x8)                   \
96   _ (global, 0xe)
97
98 #define foreach_ip6_multicast_link_local_group_id       \
99   _ (all_hosts, 0x1)                                    \
100   _ (all_routers, 0x2)                                  \
101   _ (rip_routers, 0x9)                                  \
102   _ (eigrp_routers, 0xa)                                \
103   _ (pim_routers, 0xd)                            \
104  _ (mldv2_routers, 0x16)
105
106 typedef enum
107 {
108 #define _(f,n) IP6_MULTICAST_SCOPE_##f = n,
109   foreach_ip6_multicast_address_scope
110 #undef _
111 } ip6_multicast_address_scope_t;
112
113 typedef enum
114 {
115 #define _(f,n) IP6_MULTICAST_GROUP_ID_##f = n,
116   foreach_ip6_multicast_link_local_group_id
117 #undef _
118 } ip6_multicast_link_local_group_id_t;
119
120 always_inline uword
121 ip6_address_is_multicast (const ip6_address_t * a)
122 {
123   return a->as_u8[0] == 0xff;
124 }
125
126 always_inline void
127 ip6_address_copy (ip6_address_t * dst, const ip6_address_t * src)
128 {
129   dst->as_u64[0] = src->as_u64[0];
130   dst->as_u64[1] = src->as_u64[1];
131 }
132
133 always_inline void
134 ip6_set_reserved_multicast_address (ip6_address_t * a,
135                                     ip6_multicast_address_scope_t scope,
136                                     u16 id)
137 {
138   a->as_u64[0] = a->as_u64[1] = 0;
139   a->as_u16[0] = clib_host_to_net_u16 (0xff00 | scope);
140   a->as_u16[7] = clib_host_to_net_u16 (id);
141 }
142
143 always_inline void
144 ip6_set_solicited_node_multicast_address (ip6_address_t * a, u32 id)
145 {
146   /* 0xff02::1:ffXX:XXXX. */
147   a->as_u64[0] = a->as_u64[1] = 0;
148   a->as_u16[0] = clib_host_to_net_u16 (0xff02);
149   a->as_u8[11] = 1;
150   ASSERT ((id >> 24) == 0);
151   id |= 0xff << 24;
152   a->as_u32[3] = clib_host_to_net_u32 (id);
153 }
154
155 always_inline void
156 ip6_multicast_ethernet_address (u8 * ethernet_address, u32 group_id)
157 {
158   ethernet_address[0] = 0x33;
159   ethernet_address[1] = 0x33;
160   ethernet_address[2] = ((group_id >> 24) & 0xff);
161   ethernet_address[3] = ((group_id >> 16) & 0xff);
162   ethernet_address[4] = ((group_id >> 8) & 0xff);
163   ethernet_address[5] = ((group_id >> 0) & 0xff);
164 }
165
166 always_inline uword
167 ip6_address_is_equal (const ip6_address_t * a, const ip6_address_t * b)
168 {
169   int i;
170   for (i = 0; i < ARRAY_LEN (a->as_uword); i++)
171     if (a->as_uword[i] != b->as_uword[i])
172       return 0;
173   return 1;
174 }
175
176 always_inline uword
177 ip6_address_is_equal_masked (const ip6_address_t * a,
178                              const ip6_address_t * b,
179                              const ip6_address_t * mask)
180 {
181   int i;
182   for (i = 0; i < ARRAY_LEN (a->as_uword); i++)
183     {
184       uword a_masked, b_masked;
185       a_masked = a->as_uword[i] & mask->as_uword[i];
186       b_masked = b->as_uword[i] & mask->as_uword[i];
187
188       if (a_masked != b_masked)
189         return 0;
190     }
191   return 1;
192 }
193
194 always_inline void
195 ip6_address_mask (ip6_address_t * a, const ip6_address_t * mask)
196 {
197   int i;
198   for (i = 0; i < ARRAY_LEN (a->as_uword); i++)
199     a->as_uword[i] &= mask->as_uword[i];
200 }
201
202 always_inline void
203 ip6_address_set_zero (ip6_address_t * a)
204 {
205   int i;
206   for (i = 0; i < ARRAY_LEN (a->as_uword); i++)
207     a->as_uword[i] = 0;
208 }
209
210 always_inline void
211 ip6_address_mask_from_width (ip6_address_t * a, u32 width)
212 {
213   int i, byte, bit, bitnum;
214   ASSERT (width <= 128);
215   clib_memset (a, 0, sizeof (a[0]));
216   for (i = 0; i < width; i++)
217     {
218       bitnum = (7 - (i & 7));
219       byte = i / 8;
220       bit = 1 << bitnum;
221       a->as_u8[byte] |= bit;
222     }
223 }
224
225 always_inline uword
226 ip6_address_is_zero (const ip6_address_t * a)
227 {
228   int i;
229   for (i = 0; i < ARRAY_LEN (a->as_uword); i++)
230     if (a->as_uword[i] != 0)
231       return 0;
232   return 1;
233 }
234
235 /* Check for unspecified address ::0 */
236 always_inline uword
237 ip6_address_is_unspecified (const ip6_address_t * a)
238 {
239   return ip6_address_is_zero (a);
240 }
241
242 /* Check for loopback address ::1 */
243 always_inline uword
244 ip6_address_is_loopback (const ip6_address_t * a)
245 {
246   return (a->as_u64[0] == 0 &&
247           a->as_u32[2] == 0 &&
248           a->as_u16[6] == 0 && a->as_u8[14] == 0 && a->as_u8[15] == 1);
249 }
250
251 /* Check for link local unicast fe80::/10. */
252 always_inline uword
253 ip6_address_is_link_local_unicast (const ip6_address_t * a)
254 {
255   return a->as_u8[0] == 0xfe && (a->as_u8[1] & 0xc0) == 0x80;
256 }
257
258 /* Check for unique local unicast fc00::/7. */
259 always_inline uword
260 ip6_address_is_local_unicast (const ip6_address_t * a)
261 {
262   return (a->as_u8[0] & 0xfe) == 0xfc;
263 }
264
265 /* Check for unique global unicast 2000::/3. */
266 always_inline uword
267 ip6_address_is_global_unicast (const ip6_address_t * a)
268 {
269   return (a->as_u8[0] & 0xe0) == 0x20;
270 }
271
272 /* Check for solicited node multicast 0xff02::1:ff00:0/104 */
273 always_inline uword
274 ip6_is_solicited_node_multicast_address (const ip6_address_t * a)
275 {
276   return (a->as_u32[0] == clib_host_to_net_u32 (0xff020000)
277           && a->as_u32[1] == 0
278           && a->as_u32[2] == clib_host_to_net_u32 (1)
279           && a->as_u8[12] == 0xff);
280 }
281
282 always_inline u32
283 ip6_address_hash_to_u32 (const ip6_address_t * a)
284 {
285   return (a->as_u32[0] ^ a->as_u32[1] ^ a->as_u32[2] ^ a->as_u32[3]);
286 }
287
288 always_inline u64
289 ip6_address_hash_to_u64 (const ip6_address_t * a)
290 {
291   return (a->as_u64[0] ^ a->as_u64[1]);
292 }
293
294 typedef struct
295 {
296   /* 4 bit version, 8 bit traffic class and 20 bit flow label. */
297   u32 ip_version_traffic_class_and_flow_label;
298
299   /* Total packet length not including this header (but including
300      any extension headers if present). */
301   u16 payload_length;
302
303   /* Protocol for next header. */
304   u8 protocol;
305
306   /* Hop limit decremented by router at each hop. */
307   u8 hop_limit;
308
309   /* Source and destination address. */
310   ip6_address_t src_address, dst_address;
311 } ip6_header_t;
312
313 #define IP6_PACKET_TC_MASK 0x0FF00000
314 #define IP6_PACKET_DSCP_MASK 0x0FC00000
315 #define IP6_PACKET_ECN_MASK 0x00300000
316
317 always_inline ip_dscp_t
318 ip6_traffic_class (const ip6_header_t * i)
319 {
320   return (i->ip_version_traffic_class_and_flow_label & IP6_PACKET_TC_MASK) >>
321     20;
322 }
323
324 static_always_inline ip_dscp_t
325 ip6_traffic_class_network_order (const ip6_header_t * ip6)
326 {
327   return (clib_net_to_host_u32 (ip6->ip_version_traffic_class_and_flow_label)
328           & IP6_PACKET_TC_MASK) >> 20;
329 }
330
331 static_always_inline ip_dscp_t
332 ip6_dscp_network_order (const ip6_header_t * ip6)
333 {
334   return (clib_net_to_host_u32 (ip6->ip_version_traffic_class_and_flow_label)
335           & IP6_PACKET_DSCP_MASK) >> 22;
336 }
337
338 static_always_inline ip_ecn_t
339 ip6_ecn_network_order (const ip6_header_t * ip6)
340 {
341   return (clib_net_to_host_u32 (ip6->ip_version_traffic_class_and_flow_label)
342           & IP6_PACKET_ECN_MASK) >> 20;
343 }
344
345 static_always_inline void
346 ip6_set_traffic_class_network_order (ip6_header_t * ip6, ip_dscp_t dscp)
347 {
348   u32 tmp =
349     clib_net_to_host_u32 (ip6->ip_version_traffic_class_and_flow_label);
350   tmp &= 0xf00fffff;
351   tmp |= (dscp << 20);
352   ip6->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (tmp);
353 }
354
355 static_always_inline void
356 ip6_set_dscp_network_order (ip6_header_t * ip6, ip_dscp_t dscp)
357 {
358   u32 tmp =
359     clib_net_to_host_u32 (ip6->ip_version_traffic_class_and_flow_label);
360   tmp &= 0xf03fffff;
361   tmp |= (dscp << 22);
362   ip6->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (tmp);
363 }
364
365 static_always_inline void
366 ip6_set_ecn_network_order (ip6_header_t * ip6, ip_ecn_t ecn)
367 {
368   u32 tmp =
369     clib_net_to_host_u32 (ip6->ip_version_traffic_class_and_flow_label);
370   tmp &= 0xffcfffff;
371   tmp |= (ecn << 20);
372   ip6->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (tmp);
373 }
374
375 always_inline void *
376 ip6_next_header (ip6_header_t * i)
377 {
378   return (void *) (i + 1);
379 }
380
381 always_inline void
382 ip6_copy_header (ip6_header_t * dst, const ip6_header_t * src)
383 {
384   dst->ip_version_traffic_class_and_flow_label =
385     src->ip_version_traffic_class_and_flow_label;
386   dst->payload_length = src->payload_length;
387   dst->protocol = src->protocol;
388   dst->hop_limit = src->hop_limit;
389
390   dst->src_address.as_uword[0] = src->src_address.as_uword[0];
391   dst->src_address.as_uword[1] = src->src_address.as_uword[1];
392   dst->dst_address.as_uword[0] = src->dst_address.as_uword[0];
393   dst->dst_address.as_uword[1] = src->dst_address.as_uword[1];
394 }
395
396 always_inline void
397 ip6_tcp_reply_x1 (ip6_header_t * ip0, tcp_header_t * tcp0)
398 {
399   {
400     ip6_address_t src0, dst0;
401
402     src0 = ip0->src_address;
403     dst0 = ip0->dst_address;
404     ip0->src_address = dst0;
405     ip0->dst_address = src0;
406   }
407
408   {
409     u16 src0, dst0;
410
411     src0 = tcp0->src;
412     dst0 = tcp0->dst;
413     tcp0->src = dst0;
414     tcp0->dst = src0;
415   }
416 }
417
418 always_inline void
419 ip6_tcp_reply_x2 (ip6_header_t * ip0, ip6_header_t * ip1,
420                   tcp_header_t * tcp0, tcp_header_t * tcp1)
421 {
422   {
423     ip6_address_t src0, dst0, src1, dst1;
424
425     src0 = ip0->src_address;
426     src1 = ip1->src_address;
427     dst0 = ip0->dst_address;
428     dst1 = ip1->dst_address;
429     ip0->src_address = dst0;
430     ip1->src_address = dst1;
431     ip0->dst_address = src0;
432     ip1->dst_address = src1;
433   }
434
435   {
436     u16 src0, dst0, src1, dst1;
437
438     src0 = tcp0->src;
439     src1 = tcp1->src;
440     dst0 = tcp0->dst;
441     dst1 = tcp1->dst;
442     tcp0->src = dst0;
443     tcp1->src = dst1;
444     tcp0->dst = src0;
445     tcp1->dst = src1;
446   }
447 }
448
449
450 /* *INDENT-OFF* */
451 typedef CLIB_PACKED (struct {
452   u8 data;
453 }) ip6_pad1_option_t;
454 /* *INDENT-ON* */
455
456 /* *INDENT-OFF* */
457 typedef CLIB_PACKED (struct {
458   u8 type;
459   u8 len;
460   u8 data[0];
461 }) ip6_padN_option_t;
462 /* *INDENT-ON* */
463
464 /* *INDENT-OFF* */
465 typedef CLIB_PACKED (struct {
466 #define IP6_MLDP_ALERT_TYPE  0x5
467   u8 type;
468   u8 len;
469   u16 value;
470 }) ip6_router_alert_option_t;
471 /* *INDENT-ON* */
472
473 /* *INDENT-OFF* */
474 typedef CLIB_PACKED (struct {
475   u8 next_hdr;
476   /* Length of this header plus option data in 8 byte units. */
477   u8 n_data_u64s;
478 }) ip6_ext_header_t;
479 /* *INDENT-ON* */
480
481 #define foreach_ext_hdr_type \
482   _(IP6_HOP_BY_HOP_OPTIONS) \
483   _(IPV6_ROUTE) \
484   _(IPV6_FRAGMENTATION) \
485   _(IPSEC_ESP) \
486   _(IPSEC_AH) \
487   _(IP6_DESTINATION_OPTIONS) \
488   _(MOBILITY) \
489   _(HIP) \
490   _(SHIM6)
491
492 always_inline u8
493 ip6_ext_hdr (u8 nexthdr)
494 {
495 #ifdef CLIB_HAVE_VEC128
496   static const u8x16 ext_hdr_types = {
497 #define _(x) IP_PROTOCOL_##x,
498     foreach_ext_hdr_type
499 #undef _
500   };
501
502   return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr));
503 #else
504   /*
505    * find out if nexthdr is an extension header or a protocol
506    */
507   return 0
508 #define _(x) || (nexthdr == IP_PROTOCOL_##x)
509     foreach_ext_hdr_type;
510 #undef _
511 #endif
512 }
513
514 #define ip6_ext_header_len(p)  ((((ip6_ext_header_t *)(p))->n_data_u64s+1) << 3)
515 #define ip6_ext_authhdr_len(p) ((((ip6_ext_header_t *)(p))->n_data_u64s+2) << 2)
516
517 always_inline void *
518 ip6_ext_next_header (ip6_ext_header_t * ext_hdr)
519 {
520   return (void *) ((u8 *) ext_hdr + ip6_ext_header_len (ext_hdr));
521 }
522
523 always_inline int
524 vlib_object_within_buffer_data (vlib_main_t * vm, vlib_buffer_t * b,
525                                 void *obj, size_t len)
526 {
527   u8 *o = obj;
528   if (o < b->data ||
529       o + len > b->data + vlib_buffer_get_default_data_size (vm))
530     return 0;
531   return 1;
532 }
533
534 /*
535  * find ipv6 extension header within ipv6 header within buffer b
536  *
537  * @param vm
538  * @param b buffer to limit search to
539  * @param ip6_header ipv6 header
540  * @param header_type extension header type to search for
541  * @param[out] prev_ext_header address of header preceding found header
542  */
543 always_inline void *
544 ip6_ext_header_find (vlib_main_t * vm, vlib_buffer_t * b,
545                      ip6_header_t * ip6_header, u8 header_type,
546                      ip6_ext_header_t ** prev_ext_header)
547 {
548   ip6_ext_header_t *prev = NULL;
549   ip6_ext_header_t *result = NULL;
550   if ((ip6_header)->protocol == header_type)
551     {
552       result = (void *) (ip6_header + 1);
553       if (!vlib_object_within_buffer_data (vm, b, result,
554                                            ip6_ext_header_len (result)))
555         {
556           result = NULL;
557         }
558     }
559   else
560     {
561       result = NULL;
562       prev = (void *) (ip6_header + 1);
563       while (ip6_ext_hdr (prev->next_hdr) && prev->next_hdr != header_type)
564         {
565           prev = ip6_ext_next_header (prev);
566           if (!vlib_object_within_buffer_data (vm, b, prev,
567                                                ip6_ext_header_len (prev)))
568             {
569               prev = NULL;
570               break;
571             }
572         }
573       if (prev && (prev->next_hdr == header_type))
574         {
575           result = ip6_ext_next_header (prev);
576           if (!vlib_object_within_buffer_data (vm, b, result,
577                                                ip6_ext_header_len (result)))
578             {
579               result = NULL;
580             }
581         }
582     }
583   if (prev_ext_header)
584     {
585       *prev_ext_header = prev;
586     }
587   return result;
588 }
589
590 /*
591  * walk extension headers, looking for a specific extension header and last
592  * extension header, calculating length of all extension headers
593  *
594  * @param vm
595  * @param b buffer to limit search to
596  * @param ip6_header ipv6 header
597  * @param find_hdr extension header to look for (ignored if ext_hdr is NULL)
598  * @param length[out] length of all extension headers
599  * @param ext_hdr[out] extension header of type find_hdr (may be NULL)
600  * @param last_ext_hdr[out] last extension header (may be NULL)
601  *
602  * @return 0 on success, -1 on failure (ext headers crossing buffer boundary)
603  */
604 always_inline int
605 ip6_walk_ext_hdr (vlib_main_t * vm, vlib_buffer_t * b,
606                   const ip6_header_t * ip6_header, u8 find_hdr, u32 * length,
607                   ip6_ext_header_t ** ext_hdr,
608                   ip6_ext_header_t ** last_ext_hdr)
609 {
610   if (!ip6_ext_hdr (ip6_header->protocol))
611     {
612       *length = 0;
613       *ext_hdr = NULL;
614       *last_ext_hdr = NULL;
615       return 0;
616     }
617   *length = 0;
618   ip6_ext_header_t *h = (void *) (ip6_header + 1);
619   if (!vlib_object_within_buffer_data (vm, b, h, ip6_ext_header_len (h)))
620     {
621       return -1;
622     }
623   *length += ip6_ext_header_len (h);
624   *last_ext_hdr = h;
625   *ext_hdr = NULL;
626   if (ip6_header->protocol == find_hdr)
627     {
628       *ext_hdr = h;
629     }
630   while (ip6_ext_hdr (h->next_hdr))
631     {
632       if (h->next_hdr == find_hdr)
633         {
634           h = ip6_ext_next_header (h);
635           *ext_hdr = h;
636         }
637       else
638         {
639           h = ip6_ext_next_header (h);
640         }
641       if (!vlib_object_within_buffer_data (vm, b, h, ip6_ext_header_len (h)))
642         {
643           return -1;
644         }
645       *length += ip6_ext_header_len (h);
646       *last_ext_hdr = h;
647     }
648   return 0;
649 }
650
651 /* *INDENT-OFF* */
652 typedef CLIB_PACKED (struct {
653   u8 next_hdr;
654   /* Length of this header plus option data in 8 byte units. */
655   u8 n_data_u64s;
656   u8 data[0];
657 }) ip6_hop_by_hop_ext_t;
658 /* *INDENT-ON* */
659
660 /* *INDENT-OFF* */
661 typedef CLIB_PACKED (struct {
662   u8 next_hdr;
663   u8 rsv;
664   u16 fragment_offset_and_more;
665   u32 identification;
666 }) ip6_frag_hdr_t;
667 /* *INDENT-ON* */
668
669 #define ip6_frag_hdr_offset(hdr) \
670   (clib_net_to_host_u16((hdr)->fragment_offset_and_more) >> 3)
671
672 #define ip6_frag_hdr_offset_bytes(hdr) \
673   (8 * ip6_frag_hdr_offset(hdr))
674
675 #define ip6_frag_hdr_more(hdr) \
676   (clib_net_to_host_u16((hdr)->fragment_offset_and_more) & 0x1)
677
678 #define ip6_frag_hdr_offset_and_more(offset, more) \
679   clib_host_to_net_u16(((offset) << 3) + !!(more))
680
681 #endif /* included_ip6_packet_h */
682
683 /*
684  * fd.io coding-style-patch-verification: ON
685  *
686  * Local Variables:
687  * eval: (c-set-style "gnu")
688  * End:
689  */