1 /*
2  * Copyright (c) 2012-2014, 2017 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef _IPV6__H_
20 #define _IPV6__H_
21 
22 #if defined(ATH_TARGET)
23 #include <osapi.h>              /* A_UINT8 */
24 #else
25 #include <a_types.h>            /* A_UINT8 */
26 #endif
27 
28 /* utilities for converting between network byte order and native endianness */
29 #ifndef BYTESWAP32
30 #define BYTESWAP32(x) \
31 	((((x) & 0x000000ff) << 24) /* byte 0 -> byte 3 */ | \
32 	 (((x) & 0x0000ff00) <<  8) /* byte 1 -> byte 2 */ | \
33 	 (((x) & 0x00ff0000) >>  8) /* byte 2 -> byte 1 */ | \
34 	 (((x) & 0xff000000) >> 24) /* byte 3 -> byte 0 */)
35 #endif /* BYTESWAP32 */
36 
37 #ifndef BE_TO_CPU32
38 #if defined(ATH_TARGET)
39 /* assume target is little-endian */
40 #define BE_TO_CPU32(x) BYTESWAP32(x)
41 #else
42 #ifdef BIG_ENDIAN_HOST
43 #define BE_TO_CPU32(x) (x)
44 #else
45 #define BE_TO_CPU32(x) BYTESWAP32(x)
46 #endif
47 #endif
48 #endif /* BE_TO_CPU32 */
49 
50 /* IPv6 header definition */
51 
52 #define IPV6_ADDR_LEN 4         /* bytes */
53 struct ipv6_hdr_t {
54 	/* version, traffic class, and flow label */
55 	A_UINT32 ver_tclass_flowlabel;
56 	A_UINT8 pyld_len[2];    /* payload length */
57 	A_UINT8 next_hdr;
58 	A_UINT8 hop_limit;
59 	A_UINT8 src_addr[IPV6_ADDR_LEN];
60 	A_UINT8 dst_addr[IPV6_ADDR_LEN];
61 };
62 
63 #define IPV6_HDR_LEN (sizeof(struct ipv6_hdr_t))
64 #define IPV6_HDR_OFFSET_NEXT_HDR (offsetof(struct ipv6_hdr_t, next_hdr))
65 #define IPV6_HDR_OFFSET_DST_ADDR (offsetof(struct ipv6_hdr_t, dst_addr[0]))
66 
67 /* IPv6 header field access macros */
68 
69 #define IPV6_HDR_VERSION_M       0xF0000000
70 #define IPV6_HDR_VERSION_S       28
71 
72 #define IPV6_HDR_TRAFFIC_CLASS_M 0x0FF00000
73 #define IPV6_HDR_TRAFFIC_CLASS_S 20
74 
75 #define IPV6_HDR_FLOW_LABEL_M    0x000FFFFF
76 #define IPV6_HDR_FLOW_LABEL_S    0
77 
ipv6_version(struct ipv6_hdr_t * ipv6_hdr)78 static inline A_UINT8 ipv6_version(struct ipv6_hdr_t *ipv6_hdr)
79 {
80 	return (BE_TO_CPU32(ipv6_hdr->ver_tclass_flowlabel) &
81 	       IPV6_HDR_VERSION_M) >> IPV6_HDR_VERSION_S;
82 }
83 
ipv6_traffic_class(struct ipv6_hdr_t * ipv6_hdr)84 static inline A_UINT8 ipv6_traffic_class(struct ipv6_hdr_t *ipv6_hdr)
85 {
86 	return (A_UINT8)((BE_TO_CPU32(ipv6_hdr->ver_tclass_flowlabel) &
87 	       IPV6_HDR_TRAFFIC_CLASS_M) >> IPV6_HDR_TRAFFIC_CLASS_S);
88 }
89 
ipv6_flow_label(struct ipv6_hdr_t * ipv6_hdr)90 static inline A_UINT32 ipv6_flow_label(struct ipv6_hdr_t *ipv6_hdr)
91 {
92 	return (BE_TO_CPU32(ipv6_hdr->ver_tclass_flowlabel) &
93 	       IPV6_HDR_FLOW_LABEL_M) >> IPV6_HDR_FLOW_LABEL_S;
94 }
95 
96 #endif /* _IPV6__H_ */
97