/* * Copyright (c) 2015 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * ip6/packet.h: ip6 packet format * * Copyright (c) 2008 Eliot Dresselhaus * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef included_ip6_packet_h #define included_ip6_packet_h #include #include typedef union { u8 as_u8[16]; u16 as_u16[8]; u32 as_u32[4]; u64 as_u64[2]; uword as_uword[16 / sizeof (uword)]; } ip6_address_t; /* Packed so that the mhash key doesn't include uninitialized pad bytes */ /* *INDENT-OFF* */ typedef CLIB_PACKED (struct { /* IP address must be first for ip_interface_address_get_address() to work */ ip6_address_t ip6_addr; u32 fib_index; }) ip6_address_fib_t; /* *INDENT-ON* */ /* *INDENT-OFF* */ typedef CLIB_PACKED (union { struct { u32 pad[3]; ip4_address_t ip4; }; ip6_address_t ip6; u8 as_u8[16]; u64 as_u64[2]; }) ip46_address_t; /* *INDENT-ON* */ #define ip46_address_is_ip4(ip46) (((ip46)->pad[0] | (ip46)->pad[1] | (ip46)->pad[2]) == 0) #define ip46_address_mask_ip4(ip46) ((ip46)->pad[0] = (ip46)->pad[1] = (ip46)->pad[2] = 0) #define ip46_address_set_ip4(ip46, ip) (ip46_address_mask_ip4(ip46), (ip46)->ip4 = (ip)[0]) #define ip46_address_reset(ip46) ((ip46)->as_u64[0] = (ip46)->as_u64[1] = 0) #define ip46_address_cmp(ip46_1, ip46_2) (memcmp(ip46_1, ip46_2, sizeof(*ip46_1))) #define ip46_address_is_zero(ip46) (((ip46)->as_u64[0] == 0) && ((ip46)->as_u64[1] == 0)) #define ip46_address_is_equal(a1, a2) (((a1)->as_u64[0] == (a2)->as_u64[0]) \ && ((a1)->as_u64[1] == (a2)->as_u64[1])) always_inline ip46_address_t to_ip46 (u32 is_ipv6, u8 * buf) { ip46_address_t ip; if (is_ipv6) ip.ip6 = *((ip6_address_t *) buf); else ip46_address_set_ip4 (&ip, (ip4_address_t *) buf); return ip; } always_inline void ip6_addr_fib_init (ip6_address_fib_t * addr_fib, ip6_address_t * address, u32 fib_index) { addr_fib->ip6_addr = *address; addr_fib->fib_index = fib_index; } /* Special addresses: unspecified ::/128 loopback ::1/128 global unicast 2000::/3 unique local unicast fc00::/7 link local unicast fe80::/10 multicast ff00::/8 ietf reserved everything else. */ #define foreach_ip6_multicast_address_scope \ _ (loopback, 0x1) \ _ (link_local, 0x2) \ _ (admin_local, 0x4) \ _ (site_local, 0x5) \ _ (organization_local, 0x8) \ _ (global, 0xe) #define foreach_ip6_multicast_link_local_group_id \ _ (all_hosts, 0x1) \ _ (all_routers, 0x2) \ _ (rip_routers, 0x9) \ _ (eigrp_routers, 0xa) \ _ (pim_routers, 0xd) \ _ (mldv2_routers, 0x16) typedef enum { #define _(f,n) IP6_MULTICAST_SCOPE_##f = n, foreach_ip6_multicast_address_scope #undef _ } ip6_multicast_address_scope_t; typedef enum { #define _(f,n) IP6_MULTICAST_GROUP_ID_##f = n, foreach_ip6_multicast_link_local_group_id #undef _ } ip6_multicast_link_local_group_id_t; always_inline uword ip6_address_is_multicast (ip6_address_t * a) { return a->as_u8[0] == 0xff; } always_inline uword ip46_address_is_multicast (ip46_address_t * a) { return ip46_address_is_ip4 (a) ? ip4_address_is_multicast (&a->ip4) : ip6_address_is_multicast (&a->ip6); } always_inline void ip6_set_reserved_multicast_address (ip6_address_t * a, ip6_multicast_address_scope_t scope, u16 id) { a->as_u64[0] = a->as_u64[1] = 0; a->as_u16[0] = clib_host_to_net_u16 (0xff00 | scope); a->as_u16[7] = clib_host_to_net_u16 (id); } always_inline void ip6_set_solicited_node_multicast_address (ip6_address_t * a, u32 id) { /* 0xff02::1:ffXX:XXXX. */ a->as_u64[0] = a->as_u64[1] = 0; a->as_u16[0] = clib_host_to_net_u16 (0xff02); a->as_u8[11] = 1; ASSERT ((id >> 24) == 0); id |= 0xff << 24; a->as_u32[3] = clib_host_to_net_u32 (id); } always_inline void ip6_link_local_address_from_ethernet_address (ip6_address_t * a, u8 * ethernet_address) { a->as_u64[0] = a->as_u64[1] = 0; a->as_u16[0] = clib_host_to_net_u16 (0xfe80); /* Always set locally administered bit (6). */ a->as_u8[0x8] = ethernet_address[0] | (1 << 6); a->as_u8[0x9] = ethernet_address[1]; a->as_u8[0xa] = ethernet_address[2]; a->as_u8[0xb] = 0xff; a->as_u8[0xc] = 0xfe; a->as_u8[0xd] = ethernet_address[3]; a->as_u8[0xe] = ethernet_address