GBP Endpoint Updates
[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   uword as_uword[16 / sizeof (uword)];
53 }
54 ip6_address_t;
55
56 typedef struct
57 {
58   ip6_address_t addr, mask;
59 } ip6_address_and_mask_t;
60
61 /* Packed so that the mhash key doesn't include uninitialized pad bytes */
62 /* *INDENT-OFF* */
63 typedef CLIB_PACKED (struct {
64   /* IP address must be first for ip_interface_address_get_address() to work */
65   ip6_address_t ip6_addr;
66   u32 fib_index;
67 }) ip6_address_fib_t;
68 /* *INDENT-ON* */
69
70 typedef enum
71 {
72   IP46_TYPE_ANY,
73   IP46_TYPE_IP4,
74   IP46_TYPE_IP6
75 } ip46_type_t;
76
77 /* *INDENT-OFF* */
78 typedef CLIB_PACKED (union {
79   struct {
80     u32 pad[3];
81     ip4_address_t ip4;
82   };
83   ip6_address_t ip6;
84   u8 as_u8[16];
85   u64 as_u64[2];
86 }) ip46_address_t;
87 /* *INDENT-ON* */
88 #define ip46_address_is_ip4(ip46)       (((ip46)->pad[0] | (ip46)->pad[1] | (ip46)->pad[2]) == 0)
89 #define ip46_address_mask_ip4(ip46)     ((ip46)->pad[0] = (ip46)->pad[1] = (ip46)->pad[2] = 0)
90 #define ip46_address_set_ip4(ip46, ip)  (ip46_address_mask_ip4(ip46), (ip46)->ip4 = (ip)[0])
91 #define ip46_address_reset(ip46)        ((ip46)->as_u64[0] = (ip46)->as_u64[1] = 0)
92 #define ip46_address_cmp(ip46_1, ip46_2) (memcmp(ip46_1, ip46_2, sizeof(*ip46_1)))
93 #define ip46_address_is_zero(ip46)      (((ip46)->as_u64[0] == 0) && ((ip46)->as_u64[1] == 0))
94 #define ip46_address_is_equal(a1, a2)   (((a1)->as_u64[0] == (a2)->as_u64[0]) \
95                                          && ((a1)->as_u64[1] == (a2)->as_u64[1]))
96 static_always_inline void
97 ip46_address_copy (ip46_address_t * dst, const ip46_address_t * src)
98 {
99   dst->as_u64[0] = src->as_u64[0];
100   dst->as_u64[1] = src->as_u64[1];
101 }
102
103 #define ip46_address_initializer {{{ 0 }}}
104
105 always_inline ip46_address_t
106 to_ip46 (u32 is_ipv6, u8 * buf)
107 {
108   ip46_address_t ip;
109   if (is_ipv6)
110     ip.ip6 = *((ip6_address_t *) buf);
111   else
112     ip46_address_set_ip4 (&ip, (ip4_address_t *) buf);
113   return ip;
114 }
115
116
117 always_inline void
118 ip6_addr_fib_init (ip6_address_fib_t * addr_fib,
119                    const ip6_address_t * address, u32 fib_index)
120 {
121   addr_fib->ip6_addr = *address;
122   addr_fib->fib_index = fib_index;
123 }
124
125 /* Special addresses:
126    unspecified          ::/128
127    loopback             ::1/128
128    global unicast       2000::/3
129    unique local unicast fc00::/7
130    link local unicast   fe80::/10
131    multicast            ff00::/8
132    ietf reserved        everything else. */
133
134 #define foreach_ip6_multicast_address_scope     \
135   _ (loopback, 0x1)                             \
136   _ (link_local, 0x2)                           \
137   _ (admin_local, 0x4)                          \
138   _ (site_local, 0x5)                           \
139   _ (organization_local, 0x8)                   \
140   _ (global, 0xe)
141
142 #define foreach_ip6_multicast_link_local_group_id       \
143   _ (all_hosts, 0x1)                                    \
144   _ (all_routers, 0x2)                                  \
145   _ (rip_routers, 0x9)                                  \
146   _ (eigrp_routers, 0xa)                                \
147   _ (pim_routers, 0xd)                            \
148  _ (mldv2_routers, 0x16)
149
150 typedef enum
151 {
152 #define _(f,n) IP6_MULTICAST_SCOPE_##f = n,
153   foreach_ip6_multicast_address_scope
154 #undef _
155 } ip6_multicast_address_scope_t;
156
157 typedef enum
158 {
159 #define _(f,n) IP6_MULTICAST_GROUP_ID_##f = n,
160   foreach_ip6_multicast_link_local_group_id
161 #undef _
162 } ip6_multicast_link_local_group_id_t;
163
164 always_inline uword
165 ip6_address_is_multicast (const ip6_address_t * a)
166 {
167   return a->as_u8[0] == 0xff;
168 }
169
170 always_inline uword
171 ip46_address_is_multicast (const ip46_address_t * a)
172 {
173   return ip46_address_is_ip4 (a) ? ip4_address_is_multicast (&a->ip4) :
174     ip6_address_is_multicast (&a->ip6);
175 }
176
177 always_inline void
178 ip6_set_reserved_multicast_address (ip6_address_t * a,
179                                     ip6_multicast_address_scope_t scope,
180                                     u16 id)
181 {
182   a->as_u64[0] = a->as_u64[1] = 0;
183   a->as_u16[0] = clib_host_to_net_u16 (0xff00 | scope);
184   a->as_u16[7] = clib_host_to_net_u16 (id);
185 }
186
187 always_inline void
188 ip6_set_solicited_node_multicast_address (ip6_address_t * a, u32 id)
189 {
190   /* 0xff02::1:ffXX:XXXX. */
191   a->as_u64[0] = a->as_u64[1] = 0;
192   a->as_u16[0] = clib_host_to_net_u16 (0xff02);
193   a->as_u8[11] = 1;
194   ASSERT ((id >> 24) == 0);
195   id |= 0xff << 24;
196   a->as_u32[3] = clib_host_to_net_u32 (id);
197 }
198
199 always_inline void
200 ip6_link_local_address_from_ethernet_address (ip6_address_t * a,
201                                               const u8 * ethernet_address)
202 {
203   a->as_u64[0] = a->as_u64[1] = 0;
204   a->as_u16[0] = clib_host_to_net_u16 (0xfe80);
205   /* Always set locally administered bit (6). */
206   a->as_u8[0x8] = ethernet_address[0] | (1 << 6);
207   a->as_u8[0x9] = ethernet_address[1];
208   a->as_u8[0xa] = ethernet_address[2];
209   a->as_u8[0xb] = 0xff;
210   a->as_u8[0xc] = 0xfe;
211   a->as_u8[0xd] = ethernet_address[3];
212   a->as_u8[0xe] = ethernet_address[4];
213   a->as_u8[0xf] = ethernet_address[5];
214 }
215
216 always_inline void
217 ip6_multicast_ethernet_address (u8 * ethernet_address, u32 group_id)
218 {
219   ethernet_address[0] = 0x33;
220   ethernet_address[1] = 0x33;
221   ethernet_address[2] = ((group_id >> 24) & 0xff);
222   ethernet_address[3] = ((group_id >> 16) & 0xff);
223   ethernet_address[4] = ((group_id >> 8) & 0xff);
224   ethernet_address[5] = ((group_id >> 0) & 0xff);
225 }
226
227 always_inline uword
228 ip6_address_is_equal (const ip6_address_t * a, const ip6_address_t * b)
229 {
230   int i;
231   for (i = 0; i < ARRAY_LEN (a->as_uword); i++)
232     if (a->as_uword[i] != b->as_uword[i])
233       return 0;
234   return 1;
235 }
236
237 always_inline uword
238 ip6_address_is_equal_masked (const ip6_address_t * a,
239                              const ip6_address_t * b,
240                              const ip6_address_t * mask)
241 {
242   int i;
243   for (i = 0; i < ARRAY_LEN (a->as_uword); i++)
244     {
245       uword a_masked, b_masked;
246       a_masked = a->as_uword[i] & mask->as_uword[i];
247       b_masked = b->as_uword[i] & mask->as_uword[i];
248
249       if (a_masked != b_masked)
250         return 0;
251     }
252   return 1;
253 }
254
255 always_inline void
256 ip6_address_mask (ip6_address_t * a, const ip6_address_t * mask)
257 {
258   int i;
259   for (i = 0; i < ARRAY_LEN (a->as_uword); i++)
260     a->as_uword[i] &= mask->as_uword[i];
261 }
262
263 always_inline void
264 ip6_address_set_zero (ip6_address_t * a)
265 {
266   int i;
267   for (i = 0; i < ARRAY_LEN (a->as_uword); i++)
268     a->as_uword[i] = 0;
269 }
270
271 always_inline void
272 ip6_address_mask_from_width (ip6_address_t * a, u32 width)
273 {
274   int i, byte, bit, bitnum;
275   ASSERT (width <= 128);
276   memset (a, 0, sizeof (a[0]));
277   for (i = 0; i < width; i++)
278     {
279       bitnum = (7 - (i & 7));
280       byte = i / 8;
281       bit = 1 << bitnum;
282       a->as_u8[byte] |= bit;
283     }
284 }
285
286 always_inline uword
287 ip6_address_is_zero (const ip6_address_t * a)
288 {
289   int i;
290   for (i = 0; i < ARRAY_LEN (a->as_uword); i++)
291     if (a->as_uword[i] != 0)
292       return 0;
293   return 1;
294 }
295
296 /* Check for unspecified address ::0 */
297 always_inline uword
298 ip6_address_is_unspecified (const ip6_address_t * a)
299 {
300   return ip6_address_is_zero (a);
301 }
302
303 /* Check for loopback address ::1 */
304 always_inline uword
305 ip6_address_is_loopback (const ip6_address_t * a)
306 {
307   return (a->as_u64[0] == 0 &&
308           a->as_u32[2] == 0 &&
309           a->as_u16[6] == 0 && a->as_u8[14] == 0 && a->as_u8[15] == 1);
310 }
311
312 /* Check for link local unicast fe80::/10. */
313 always_inline uword
314 ip6_address_is_link_local_unicast (const ip6_address_t * a)
315 {
316   return a->as_u8[0] == 0xfe && (a->as_u8[1] & 0xc0) == 0x80;
317 }
318
319 /* Check for unique local unicast fc00::/7. */
320 always_inline uword
321 ip6_address_is_local_unicast (const ip6_address_t * a)
322 {
323   return (a->as_u8[0] & 0xfe) == 0xfc;
324 }
325
326 /* Check for unique global unicast 2000::/3. */
327 always_inline uword
328 ip6_address_is_global_unicast (const ip6_address_t * a)
329 {
330   return (a->as_u8[0] & 0xe0) == 0x20;
331 }
332
333 /* Check for solicited node multicast 0xff02::1:ff00:0/104 */
334 always_inline uword
335 ip6_is_solicited_node_multicast_address (const ip6_address_t * a)
336 {
337   return (a->as_u32[0] == clib_host_to_net_u32 (0xff020000)
338           && a->as_u32[1] == 0
339           && a->as_u32[2] == clib_host_to_net_u32 (1)
340           && a->as_u8[12] == 0xff);
341 }
342
343 typedef struct
344 {
345   /* 4 bit version, 8 bit traffic class and 20 bit flow label. */
346   u32 ip_version_traffic_class_and_flow_label;
347
348   /* Total packet length not including this header (but including
349      any extension headers if present). */
350   u16 payload_length;
351
352   /* Protocol for next header. */
353   u8 protocol;
354
355   /* Hop limit decremented by router at each hop. */
356   u8 hop_limit;
357
358   /* Source and destination address. */
359   ip6_address_t src_address, dst_address;
360 } ip6_header_t;
361
362 always_inline u8
363 ip6_traffic_class (const ip6_header_t * i)
364 {
365   return (i->ip_version_traffic_class_and_flow_label & 0x0FF00000) >> 20;
366 }
367
368 static_always_inline u8
369 ip6_traffic_class_network_order (const ip6_header_t * ip6)
370 {
371   return (clib_net_to_host_u32 (ip6->ip_version_traffic_class_and_flow_label)
372           & 0x0ff00000) >> 20;
373 }
374
375 static_always_inline void
376 ip6_set_traffic_class_network_order (ip6_header_t * ip6, u8 dscp)
377 {
378   u32 tmp =
379     clib_net_to_host_u32 (ip6->ip_version_traffic_class_and_flow_label);
380   tmp &= 0xf00fffff;
381   tmp |= (dscp << 20);
382   ip6->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (tmp);
383 }
384
385 always_inline void *
386 ip6_next_header (ip6_header_t * i)
387 {
388   return (void *) (i + 1);
389 }
390
391 always_inline void
392 ip6_copy_header (ip6_header_t * dst, const ip6_header_t * src)
393 {
394   dst->ip_version_traffic_class_and_flow_label =
395     src->ip_version_traffic_class_and_flow_label;
396   dst->payload_length = src->payload_length;
397   dst->protocol = src->protocol;
398   dst->hop_limit = src->hop_limit;
399
400   dst->src_address.as_uword[0] = src->src_address.as_uword[0];
401   dst->src_address.as_uword[1] = src->src_address.as_uword[1];
402   dst->dst_address.as_uword[0] = src->dst_address.as_uword[0];
403   dst->dst_address.as_uword[1] = src->dst_address.as_uword[1];
404 }
405
406 always_inline void
407 ip6_tcp_reply_x1 (ip6_header_t * ip0, tcp_header_t * tcp0)
408 {
409   {
410     ip6_address_t src0, dst0;
411
412     src0 = ip0->src_address;
413     dst0 = ip0->dst_address;
414     ip0->src_address = dst0;
415     ip0->dst_address = src0;
416   }
417
418   {
419     u16 src0, dst0;
420
421     src0 = tcp0->src;
422     dst0 = tcp0->dst;
423     tcp0->src = dst0;
424     tcp0->dst = src0;
425   }
426 }
427
428 always_inline void
429 ip6_tcp_reply_x2 (ip6_header_t * ip0, ip6_header_t * ip1,
430                   tcp_header_t * tcp0, tcp_header_t * tcp1)
431 {
432   {
433     ip6_address_t src0, dst0, src1, dst1;
434
435     src0 = ip0->src_address;
436     src1 = ip1->src_address;
437     dst0 = ip0->dst_address;
438     dst1 = ip1->dst_address;
439     ip0->src_address = dst0;
440     ip1->src_address = dst1;
441     ip0->dst_address = src0;
442     ip1->dst_address = src1;
443   }
444
445   {
446     u16 src0, dst0, src1, dst1;
447
448     src0 = tcp0->src;
449     src1 = tcp1->src;
450     dst0 = tcp0->dst;
451     dst1 = tcp1->dst;
452     tcp0->src = dst0;
453     tcp1->src = dst1;
454     tcp0->dst = src0;
455     tcp1->dst = src1;
456   }
457 }
458
459
460 /* *INDENT-OFF* */
461 typedef CLIB_PACKED (struct {
462   u8 data;
463 }) ip6_pad1_option_t;
464 /* *INDENT-ON* */
465
466 /* *INDENT-OFF* */
467 typedef CLIB_PACKED (struct {
468   u8 type;
469   u8 len;
470   u8 data[0];
471 }) ip6_padN_option_t;
472 /* *INDENT-ON* */
473
474 /* *INDENT-OFF* */
475 typedef CLIB_PACKED (struct {
476 #define IP6_MLDP_ALERT_TYPE  0x5
477   u8 type;
478   u8 len;
479   u16 value;
480 }) ip6_router_alert_option_t;
481 /* *INDENT-ON* */
482
483 /* *INDENT-OFF* */
484 typedef CLIB_PACKED (struct {
485   u8 next_hdr;
486   /* Length of this header plus option data in 8 byte units. */
487   u8 n_data_u64s;
488 }) ip6_ext_header_t;
489
490 always_inline u8 ip6_ext_hdr(u8 nexthdr)
491 {
492   /*
493    * find out if nexthdr is an extension header or a protocol
494    */
495   return   (nexthdr == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ||
496     (nexthdr == IP_PROTOCOL_IPV6_FRAGMENTATION)  ||
497     (nexthdr == IP_PROTOCOL_IPSEC_AH)      ||
498     (nexthdr == IP_PROTOCOL_IPV6_ROUTE)      ||
499     (nexthdr == IP_PROTOCOL_IP6_DESTINATION_OPTIONS);
500 }
501
502 #define ip6_ext_header_len(p)  ((((ip6_ext_header_t *)(p))->n_data_u64s+1) << 3)
503 #define ip6_ext_authhdr_len(p) ((((ip6_ext_header_t *)(p))->n_data_u64s+2) << 2)
504
505 always_inline void *
506 ip6_ext_next_header (ip6_ext_header_t *ext_hdr )
507 { return (void *)((u8 *) ext_hdr + ip6_ext_header_len(ext_hdr)); }
508
509 /*
510  * Macro to find the IPv6 ext header of type t
511  * I is the IPv6 header
512  * P is the previous IPv6 ext header (NULL if none)
513  * M is the matched IPv6 ext header of type t
514  */
515 #define ip6_ext_header_find_t(i, p, m, t)               \
516 if ((i)->protocol == t)                                 \
517 {                                                       \
518   (m) = (void *)((i)+1);                                \
519   (p) = NULL;                                           \
520 }                                                       \
521 else                                                    \
522 {                                                       \
523   (m) = NULL;                                           \
524   (p) = (void *)((i)+1);                                \
525   while (ip6_ext_hdr((p)->next_hdr) &&                  \
526     ((ip6_ext_header_t *)(p))->next_hdr != (t))         \
527   {                                                     \
528     (p) = ip6_ext_next_header((p));                     \
529   }                                                     \
530   if ( ((p)->next_hdr) == (t))                          \
531   {                                                     \
532     (m) = (void *)(ip6_ext_next_header((p)));           \
533   }                                                     \
534 }
535
536
537 typedef CLIB_PACKED (struct {
538   u8 next_hdr;
539   /* Length of this header plus option data in 8 byte units. */
540   u8 n_data_u64s;
541   u8 data[0];
542 }) ip6_hop_by_hop_ext_t;
543 /* *INDENT-ON* */
544
545 /* *INDENT-OFF* */
546 typedef CLIB_PACKED (struct {
547   u8 next_hdr;
548   u8 rsv;
549   u16 fragment_offset_and_more;
550   u32 identification;
551 }) ip6_frag_hdr_t;
552 /* *INDENT-ON* */
553
554 #define ip6_frag_hdr_offset(hdr) \
555   (clib_net_to_host_u16((hdr)->fragment_offset_and_more) >> 3)
556
557 #define ip6_frag_hdr_offset_bytes(hdr) \
558   (8 * ip6_frag_hdr_offset(hdr))
559
560 #define ip6_frag_hdr_more(hdr) \
561   (clib_net_to_host_u16((hdr)->fragment_offset_and_more) & 0x1)
562
563 #define ip6_frag_hdr_offset_and_more(offset, more) \
564   clib_host_to_net_u16(((offset) << 3) + !!(more))
565
566 #endif /* included_ip6_packet_h */
567
568 /*
569  * fd.io coding-style-patch-verification: ON
570  *
571  * Local Variables:
572  * eval: (c-set-style "gnu")
573  * End:
574  */