The friendly Operating System for the Internet of Things
ipv6/hdr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
20 #ifndef IPV6_HDR_H
21 #define IPV6_HDR_H
22 
23 #include <stdint.h>
24 
25 #include "byteorder.h"
26 #include "net/inet_csum.h"
27 #include "net/ipv6/addr.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
68 typedef struct __attribute__((packed)) {
92  uint8_t nh;
93  uint8_t hl;
96 } ipv6_hdr_t;
97 
103 static inline void ipv6_hdr_set_version(ipv6_hdr_t *hdr)
104 {
105  hdr->v_tc_fl.u8[0] &= 0x0f;
106  hdr->v_tc_fl.u8[0] |= 0x60;
107 }
108 
116 static inline uint8_t ipv6_hdr_get_version(const ipv6_hdr_t *hdr)
117 {
118  return ((hdr->v_tc_fl.u8[0]) >> 4);
119 }
120 
129 static inline bool ipv6_hdr_is(const ipv6_hdr_t *hdr)
130 {
131  return (((hdr->v_tc_fl.u8[0]) & 0xf0) == 0x60);
132 }
133 
140 static inline void ipv6_hdr_set_tc(ipv6_hdr_t *hdr, uint8_t tc)
141 {
142  hdr->v_tc_fl.u8[0] &= 0xf0;
143  hdr->v_tc_fl.u8[0] |= (0x0f & (tc >> 4));
144  hdr->v_tc_fl.u8[1] &= 0x0f;
145  hdr->v_tc_fl.u8[1] |= (0xf0 & (tc << 4));
146 }
147 
162 static inline void ipv6_hdr_set_tc_ecn(ipv6_hdr_t *hdr, uint8_t ecn)
163 {
164  hdr->v_tc_fl.u8[0] &= 0xf3;
165  hdr->v_tc_fl.u8[0] |= (0x0c & (ecn << 2));
166 }
167 
182 static inline void ipv6_hdr_set_tc_dscp(ipv6_hdr_t *hdr, uint8_t dscp)
183 {
184  hdr->v_tc_fl.u8[0] &= 0xfc;
185  hdr->v_tc_fl.u8[0] |= (0x03 & (dscp >> 4));
186  hdr->v_tc_fl.u8[1] &= 0x0f;
187  hdr->v_tc_fl.u8[1] |= (0xf0 & (dscp << 4));
188 }
189 
197 static inline uint8_t ipv6_hdr_get_tc(const ipv6_hdr_t *hdr)
198 {
199  return ((((hdr->v_tc_fl.u8[0]) & 0x0f) << 4) |
200  ((hdr->v_tc_fl.u8[1] & 0xf0) >> 4));
201 }
202 
217 static inline uint8_t ipv6_hdr_get_tc_ecn(const ipv6_hdr_t *hdr)
218 {
219  return (((hdr->v_tc_fl.u8[0]) & 0x0c) >> 2);
220 }
221 
222 
237 static inline uint8_t ipv6_hdr_get_tc_dscp(const ipv6_hdr_t *hdr)
238 {
239  return ((((hdr->v_tc_fl.u8[0]) & 0x03) << 4) |
240  ((hdr->v_tc_fl.u8[1] & 0xf0) >> 4));
241 }
242 
249 static inline void ipv6_hdr_set_fl(ipv6_hdr_t *hdr, uint32_t fl)
250 {
251  hdr->v_tc_fl.u8[1] &= 0xf0;
252  hdr->v_tc_fl.u8[1] |= (0x0f & (byteorder_htonl(fl).u8[1]));
253  hdr->v_tc_fl.u16[1] = byteorder_htonl(fl).u16[1];
254 }
255 
263 static inline uint32_t ipv6_hdr_get_fl(const ipv6_hdr_t *hdr)
264 {
265  return byteorder_ntohl(hdr->v_tc_fl) & 0x000fffff;
266 }
267 
288 static inline uint16_t ipv6_hdr_inet_csum(uint16_t sum, ipv6_hdr_t *hdr,
289  uint8_t prot_num, uint16_t len)
290 {
291  if ((sum + len + prot_num) > 0xffff) {
292  /* increment by one for overflow to keep it as 1's complement sum */
293  sum++;
294  }
295 
296  return inet_csum(sum + len + prot_num, hdr->src.u8,
297  (2 * sizeof(ipv6_addr_t)));
298 }
299 
305 void ipv6_hdr_print(ipv6_hdr_t *hdr);
306 
307 #ifdef __cplusplus
308 }
309 #endif
310 
311 #endif /* IPV6_HDR_H */
312 
uint8_t u8[16]
divided by 16 8-bit words.
Definition: ipv6/addr.h:75
static bool ipv6_hdr_is(const ipv6_hdr_t *hdr)
Checks if the version field is set to 6.
Definition: ipv6/hdr.h:129
uint16_t u16[2]
16 bit representation
Definition: byteorder.h:89
Definitions for IPv6 addresses.
A 32 bit integer in big endian aka network byte order.
Definition: byteorder.h:86
uint8_t nh
type of next header in this packet.
Definition: ipv6/hdr.h:92
static void ipv6_hdr_set_tc_dscp(ipv6_hdr_t *hdr, uint8_t dscp)
Sets the value of the Differentiated Service Codepoint (DSCP) part of the traffic class field of hdr...
Definition: ipv6/hdr.h:182
uint8_t u8[4]
8 bit representation
Definition: byteorder.h:88
A 16 bit integer in big endian aka network byte order.
Definition: byteorder.h:76
Data type to represent an IPv6 packet header.
Definition: ipv6/hdr.h:68
ipv6_addr_t src
source address of this packet.
Definition: ipv6/hdr.h:94
static uint8_t ipv6_hdr_get_version(const ipv6_hdr_t *hdr)
Gets the value of the version field of hdr.
Definition: ipv6/hdr.h:116
static uint16_t ipv6_hdr_inet_csum(uint16_t sum, ipv6_hdr_t *hdr, uint8_t prot_num, uint16_t len)
Calculates the Internet Checksum for the IPv6 Pseudo Header.
Definition: ipv6/hdr.h:288
uint8_t hl
hop limit for this packet.
Definition: ipv6/hdr.h:93
ipv6_addr_t dst
destination address of this packet.
Definition: ipv6/hdr.h:95
network_uint32_t v_tc_fl
Version, traffic class, and flow label.
Definition: ipv6/hdr.h:90
static void ipv6_hdr_set_tc(ipv6_hdr_t *hdr, uint8_t tc)
Sets the traffic class field of hdr.
Definition: ipv6/hdr.h:140
static uint32_t byteorder_ntohl(network_uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition: byteorder.h:378
static uint8_t ipv6_hdr_get_tc_ecn(const ipv6_hdr_t *hdr)
Gets the value of the Explicit Congestion Notification (ECN) part of the traffic class field of hdr...
Definition: ipv6/hdr.h:217
static network_uint32_t byteorder_htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition: byteorder.h:361
Functions to work with different byte orders.
static void ipv6_hdr_set_fl(ipv6_hdr_t *hdr, uint32_t fl)
Sets the flow label field of hdr.
Definition: ipv6/hdr.h:249
static uint16_t inet_csum(uint16_t sum, const uint8_t *buf, uint16_t len)
Calculates the unnormalized Internet Checksum of buf, where the buffer provides a standalone domain f...
Definition: inet_csum.h:72
static void ipv6_hdr_set_tc_ecn(ipv6_hdr_t *hdr, uint8_t ecn)
Sets the value of the Explicit Congestion Notification (ECN) part of the traffic class field of hdr...
Definition: ipv6/hdr.h:162
static uint32_t ipv6_hdr_get_fl(const ipv6_hdr_t *hdr)
Gets the value of the flow label field of hdr.
Definition: ipv6/hdr.h:263
Data type to represent an IPv6 address.
Definition: ipv6/addr.h:74
void ipv6_hdr_print(ipv6_hdr_t *hdr)
Outputs an IPv6 header to stdout.
Internet Checksum definitions.
network_uint16_t len
payload length of this packet.
Definition: ipv6/hdr.h:91
static void ipv6_hdr_set_version(ipv6_hdr_t *hdr)
Sets the version field of hdr to 6.
Definition: ipv6/hdr.h:103
static uint8_t ipv6_hdr_get_tc(const ipv6_hdr_t *hdr)
Gets the value of the traffic class field of hdr.
Definition: ipv6/hdr.h:197
static uint8_t ipv6_hdr_get_tc_dscp(const ipv6_hdr_t *hdr)
Gets the value of the Differentiated Service Codepoint (DSCP) part of the traffic class field of hdr...
Definition: ipv6/hdr.h:237