ROHC compression/decompression library
ipv6.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Didier Barvaux
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file ipv6.h
21  * @brief The IPv6 header
22  * @author Didier Barvaux <didier@barvaux.org>
23  */
24 
25 #ifndef ROHC_PROTOCOLS_IPV6_H
26 #define ROHC_PROTOCOLS_IPV6_H
27 
28 #include "rohc_utils.h"
29 
30 #include <stdint.h>
31 #include <stddef.h>
32 
33 #ifdef __KERNEL__
34 # include <endian.h>
35 #else
36 # include "config.h" /* for WORDS_BIGENDIAN */
37 #endif
38 
39 
40 /** The format to print an IPv6 address */
41 #define IPV6_ADDR_FORMAT \
42  "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
43 
44 
45 /** The data to print an IPv6 address in (struct ipv6_addr *) format */
46 #define IPV6_ADDR_IN6(x) \
47  IPV6_ADDR_RAW((x)->u8)
48 
49 
50 /** The data to print an IPv6 address in raw format */
51 #define IPV6_ADDR_RAW(x) \
52  (x)[0], (x)[1], (x)[2], (x)[3], (x)[4], (x)[5], (x)[6], (x)[7], \
53  (x)[8], (x)[9], (x)[10], (x)[11], (x)[12], (x)[13], (x)[14], (x)[15]
54 
55 
56 /**
57  * @brief The IPv6 address
58  */
59 struct ipv6_addr
60 {
61  union /* IPv6 address may be accessed by 8, 16 or 32-bit blocks */
62  {
63  uint8_t u8[16]; /**< The 16 8-bit blocks of the IPv6 address */
64  uint16_t u16[8]; /**< The 8 16-bit blocks of the IPv6 address */
65  uint32_t u32[4]; /**< The 4 32-bit blocks of the IPv6 address */
66  } __attribute__((packed));
67 } __attribute__((packed));
68 
69 
70 /* compiler sanity check for C11-compliant compilers and GCC >= 4.6 */
71 #if ((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
72  (defined(__GNUC__) && defined(__GNUC_MINOR__) && \
73  (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))))
74 _Static_assert(sizeof(struct ipv6_addr) == 16,
75  "IPv6 address should exactly 16-byte long");
76 #endif
77 
78 
79 /**
80  * @brief The IPv6 header
81  */
82 struct ipv6_hdr
83 {
84  union
85  {
86  uint32_t version_tc_flow; /**< The combined version, TC, Flow Label fields */
87 #define IPV6_VERSION_MASK 0xf0000000U /**< The mask for the Traffic Class (TC) field */
88 #define IPV6_TC_MASK 0x0ff00000U /**< The mask for the Traffic Class (TC) field */
89 #define IPV6_FLOW_MASK 0x000fffffU /**< The mask for the Flow Label field */
90  struct
91  {
92 #if WORDS_BIGENDIAN == 1
93  uint8_t version:4; /**< The IP version */
94  uint8_t tc1:4; /**< The Traffic Class (TC) (part 1) */
95  uint8_t tc2:4; /**< The Traffic Class (TC) (part 2) */
96  uint8_t flowl1:4; /**< The Flow Label (part 1) */
97 #else
98  uint8_t tc1:4;
99  uint8_t version:4;
100  uint8_t flow1:4;
101  uint8_t tc2:4;
102 #endif
103  uint16_t flow2; /**< The Flow Label (part 2) */
104  } __attribute__((packed));
105  struct
106  {
107 #if WORDS_BIGENDIAN == 1
108  uint8_t version_:4; /**< The IP version */
109  uint8_t dscp1:4; /**< The Differentiated Services Code Point (DSCP) (part 1) */
110  uint8_t dscp2:2; /**< The Differentiated Services Code Point (DSCP) (part 2) */
111  uint8_t ecn:2; /**< The Explicit Congestion Notification (ECN) */
112  uint8_t flowl1_:4; /**< The Flow Label (part 1) */
113 #else
114  uint8_t dscp1:4;
115  uint8_t version_:4;
116  uint8_t flowl1_:4;
117  uint8_t ecn:2;
118  uint8_t dscp2:2;
119 #endif
120  uint16_t flow2_; /**< The Flow Label (part 2) */
121  } __attribute__((packed));
122 
123  } __attribute__((packed));
124 
125  uint16_t plen; /**< The Payload Length */
126  uint8_t nh; /**< The protocol of the Next Header (NH) */
127  uint8_t hl; /**< The Hop Limit (HL) */
128  struct ipv6_addr saddr; /**< The source IP address */
129  struct ipv6_addr daddr; /**< The destination IP address */
130 
131 } __attribute__((packed));
132 
133 
134 /* compiler sanity check for C11-compliant compilers and GCC >= 4.6 */
135 #if ((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
136  (defined(__GNUC__) && defined(__GNUC_MINOR__) && \
137  (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))))
138 _Static_assert(sizeof(struct ipv6_hdr) == 40,
139  "IPv6 header should exactly 40-byte long");
140 #endif
141 
142 
143 /** The maximum value of the length field of one IPv6 extension header */
144 #define IPV6_OPT_HDR_LEN_FIELD_MAX_VAL 4U
145 /** The maximum length of one IPv6 extension header */
146 #define IPV6_OPT_HDR_LEN_MAX ((IPV6_OPT_HDR_LEN_FIELD_MAX_VAL + 1U) * 8U)
147 /** The maximum length for the IPv6 extension header context */
148 #define IPV6_OPT_CTXT_LEN_MAX (IPV6_OPT_HDR_LEN_MAX - 2U)
149 
150 
151 /** The IPv6 option header */
152 struct ipv6_opt
153 {
154  uint8_t next_header; /**< The protocol of the next header */
155  uint8_t length; /**< The length of the header in 8-byte units minus 1 */
156  uint8_t value[1]; /**< The start of the IPv6 option header */ /* TODO */
157 } __attribute__((packed));
158 
159 
160 /* compiler sanity check for C11-compliant compilers and GCC >= 4.6 */
161 #if ((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
162  (defined(__GNUC__) && defined(__GNUC_MINOR__) && \
163  (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))))
164 _Static_assert(sizeof(struct ipv6_opt) == 3,
165  "IPv6 option header should exactly 3-byte long");
166 #endif
167 
168 
169 static inline uint8_t ipv6_get_tc(const struct ipv6_hdr *const ipv6)
170  __attribute__((warn_unused_result, nonnull(1), pure));
171 static inline void ipv6_set_tc(struct ipv6_hdr *const ipv6, const uint8_t tc)
172  __attribute__((nonnull(1)));
173 
174 static inline void ipv6_set_dscp_ecn(struct ipv6_hdr *const ipv6,
175  const uint8_t dscp,
176  const uint8_t ecn)
177  __attribute__((nonnull(1)));
178 
179 static inline uint8_t ipv6_get_dscp(const struct ipv6_hdr *const ipv6)
180  __attribute__((warn_unused_result, nonnull(1), pure));
181 static inline void ipv6_set_dscp(struct ipv6_hdr *const ipv6, const uint8_t dscp)
182  __attribute__((nonnull(1)));
183 
184 static inline uint32_t ipv6_get_flow_label(const struct ipv6_hdr *const ipv6)
185  __attribute__((warn_unused_result, nonnull(1), pure));
186 static inline void ipv6_set_flow_label(struct ipv6_hdr *const ipv6,
187  const uint32_t flow_label)
188  __attribute__((nonnull(1)));
189 
190 static inline size_t ipv6_opt_get_length(const struct ipv6_opt *const opt)
191  __attribute__((warn_unused_result, nonnull(1), pure));
192 
193 
194 /**
195  * @brief Get the Traffic Class (TC) of the given IPv6 packet
196  *
197  * @param ipv6 The header of the IPv6 packet
198  * @return The 8-bit Traffic Class (TC)
199  */
200 static inline uint8_t ipv6_get_tc(const struct ipv6_hdr *const ipv6)
201 {
202  return ((ipv6->tc1 << 4) | ipv6->tc2);
203 }
204 
205 
206 /**
207  * @brief Set the Traffic Class (TC) of the given IPv6 packet
208  *
209  * @param[in,out] ipv6 The header of the IPv6 packet
210  * @param tc The 8-bit Traffic Class (TC)
211  */
212 static inline void ipv6_set_tc(struct ipv6_hdr *const ipv6, const uint8_t tc)
213 {
214  ipv6->tc1 = (tc >> 4) & 0x0f;
215  ipv6->tc2 = tc & 0x0f;
216 }
217 
218 
219 /**
220  * @brief Set the DSCP and ECN of the given IPv6 packet
221  *
222  * @param[in,out] ipv6 The header of the IPv6 packet
223  * @param dscp The 6-bit DSCP
224  * @param ecn The 2-bit ECN
225  */
226 static inline void ipv6_set_dscp_ecn(struct ipv6_hdr *const ipv6,
227  const uint8_t dscp,
228  const uint8_t ecn)
229 {
230  ipv6_set_tc(ipv6, ((dscp << 2) & 0xfc) | (ecn & 0x03));
231 }
232 
233 
234 /**
235  * @brief Get the Differentiated Services Code Point (DSCP) of the given IPv6 packet
236  *
237  * @param ipv6 The header of the IPv6 packet
238  * @return The 6-bit DSCP
239  */
240 static inline uint8_t ipv6_get_dscp(const struct ipv6_hdr *const ipv6)
241 {
242  return ((ipv6->dscp1 << 2) | ipv6->dscp2);
243 }
244 
245 
246 /**
247  * @brief Set the Differentiated Services Code Point (DSCP) of the given IPv6 packet
248  *
249  * @param[in,out] ipv6 The header of the IPv6 packet
250  * @param dscp The 6-bit DSCP
251  */
252 static inline void ipv6_set_dscp(struct ipv6_hdr *const ipv6, const uint8_t dscp)
253 {
254  ipv6->dscp1 = (dscp >> 2) & 0x0f;
255  ipv6->dscp2 = dscp & 0x03;
256 }
257 
258 
259 /**
260  * @brief Get the Flow Label of the given IPv6 packet
261  *
262  * @param ipv6 The header of the IPv6 packet
263  * @return The 20-bit Flow Label
264  */
265 static inline uint32_t ipv6_get_flow_label(const struct ipv6_hdr *const ipv6)
266 {
267  return (rohc_ntoh32(ipv6->version_tc_flow) & IPV6_FLOW_MASK);
268 }
269 
270 
271 /**
272  * @brief Set the Flow Label of the given IPv6 packet
273  *
274  * @param[in,out] ipv6 The header of the IPv6 packet
275  * @param flow_label The 20-bit Flow Label
276  */
277 static inline void ipv6_set_flow_label(struct ipv6_hdr *const ipv6,
278  const uint32_t flow_label)
279 {
280  ipv6->flow1 = (flow_label >> 16);
281  ipv6->flow2 = rohc_hton16(flow_label & 0xffff);
282 }
283 
284 
285 /**
286  * @brief get the length (in bytes) of the IPv6 option header
287  *
288  * @param opt The IPv6 option header
289  * @return The length (in bytes) of the IPv6 option header
290  */
291 static inline size_t ipv6_opt_get_length(const struct ipv6_opt *const opt)
292 {
293  return ((opt->length + 1) * 8);
294 }
295 
296 
297 #endif
298 
uint8_t value[1]
Definition: ipv6.h:156
uint16_t flow2_
Definition: ipv6.h:120
struct ipv6_addr daddr
Definition: ipv6.h:129
static uint32_t ipv6_get_flow_label(const struct ipv6_hdr *const ipv6)
Get the Flow Label of the given IPv6 packet.
Definition: ipv6.h:265
uint8_t next_header
Definition: ipv6.h:154
uint16_t flow2
Definition: ipv6.h:103
uint8_t hl
Definition: ipv6.h:127
uint8_t ecn
Definition: ipv6.h:117
Definition: ipv6.h:152
static void ipv6_set_flow_label(struct ipv6_hdr *const ipv6, const uint32_t flow_label)
Set the Flow Label of the given IPv6 packet.
Definition: ipv6.h:277
static size_t ipv6_opt_get_length(const struct ipv6_opt *const opt)
get the length (in bytes) of the IPv6 option header
Definition: ipv6.h:291
static void ipv6_set_dscp(struct ipv6_hdr *const ipv6, const uint8_t dscp)
Set the Differentiated Services Code Point (DSCP) of the given IPv6 packet.
Definition: ipv6.h:252
The IPv6 header.
Definition: ipv6.h:82
uint8_t dscp2
Definition: ipv6.h:118
Miscellaneous utils for ROHC libraries.
uint8_t tc2
Definition: ipv6.h:101
struct ipv6_addr saddr
Definition: ipv6.h:128
static void ipv6_set_dscp_ecn(struct ipv6_hdr *const ipv6, const uint8_t dscp, const uint8_t ecn)
Set the DSCP and ECN of the given IPv6 packet.
Definition: ipv6.h:226
uint8_t version
Definition: ipv6.h:99
bool nonnull(1)))
#define IPV6_FLOW_MASK
Definition: ipv6.h:89
uint8_t flowl1_
Definition: ipv6.h:116
static uint8_t ipv6_get_dscp(const struct ipv6_hdr *const ipv6)
Get the Differentiated Services Code Point (DSCP) of the given IPv6 packet.
Definition: ipv6.h:240
static uint8_t ipv6_get_tc(const struct ipv6_hdr *const ipv6)
Get the Traffic Class (TC) of the given IPv6 packet.
Definition: ipv6.h:200
uint16_t rohc_hton16(const uint16_t host16)
Convert a 16-bit short integer from host to network byte orders.
Definition: rohc_utils.c:104
uint32_t version_tc_flow
Definition: ipv6.h:86
uint8_t u8[16]
Definition: ipv6.h:63
uint8_t dscp1
Definition: ipv6.h:114
uint32_t u32[4]
Definition: ipv6.h:65
uint16_t plen
Definition: ipv6.h:125
uint8_t length
Definition: ipv6.h:155
uint32_t rohc_ntoh32(const uint32_t net32)
Convert a 32-bit long integer from network to host byte orders.
Definition: rohc_utils.c:55
static void ipv6_set_tc(struct ipv6_hdr *const ipv6, const uint8_t tc)
Set the Traffic Class (TC) of the given IPv6 packet.
Definition: ipv6.h:212
uint8_t version_
Definition: ipv6.h:115
uint8_t tc1
Definition: ipv6.h:98
The IPv6 address.
Definition: ipv6.h:59
uint8_t flow1
Definition: ipv6.h:100
uint8_t nh
Definition: ipv6.h:126
uint16_t u16[8]
Definition: ipv6.h:64