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 NET_IPV6_HDR_H
21 #define NET_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 
66 typedef struct __attribute__((packed)) {
90  uint8_t nh;
91  uint8_t hl;
94 } ipv6_hdr_t;
95 
101 static inline void ipv6_hdr_set_version(ipv6_hdr_t *hdr)
102 {
103  hdr->v_tc_fl.u8[0] &= 0x0f;
104  hdr->v_tc_fl.u8[0] |= 0x60;
105 }
106 
114 static inline uint8_t ipv6_hdr_get_version(const ipv6_hdr_t *hdr)
115 {
116  return ((hdr->v_tc_fl.u8[0]) >> 4);
117 }
118 
127 static inline bool ipv6_hdr_is(const ipv6_hdr_t *hdr)
128 {
129  return (((hdr->v_tc_fl.u8[0]) & 0xf0) == 0x60);
130 }
131 
138 static inline void ipv6_hdr_set_tc(ipv6_hdr_t *hdr, uint8_t tc)
139 {
140  hdr->v_tc_fl.u8[0] &= 0xf0;
141  hdr->v_tc_fl.u8[0] |= (0x0f & (tc >> 4));
142  hdr->v_tc_fl.u8[1] &= 0x0f;
143  hdr->v_tc_fl.u8[1] |= (0xf0 & (tc << 4));
144 }
145 
160 static inline void ipv6_hdr_set_tc_ecn(ipv6_hdr_t *hdr, uint8_t ecn)
161 {
162  hdr->v_tc_fl.u8[0] &= 0xf3;
163  hdr->v_tc_fl.u8[0] |= (0x0c & (ecn << 2));
164 }
165 
180 static inline void ipv6_hdr_set_tc_dscp(ipv6_hdr_t *hdr, uint8_t dscp)
181 {
182  hdr->v_tc_fl.u8[0] &= 0xfc;
183  hdr->v_tc_fl.u8[0] |= (0x03 & (dscp >> 4));
184  hdr->v_tc_fl.u8[1] &= 0x0f;
185  hdr->v_tc_fl.u8[1] |= (0xf0 & (dscp << 4));
186 }
187 
195 static inline uint8_t ipv6_hdr_get_tc(const ipv6_hdr_t *hdr)
196 {
197  return ((((hdr->v_tc_fl.u8[0]) & 0x0f) << 4) |
198  ((hdr->v_tc_fl.u8[1] & 0xf0) >> 4));
199 }
200 
215 static inline uint8_t ipv6_hdr_get_tc_ecn(const ipv6_hdr_t *hdr)
216 {
217  return (((hdr->v_tc_fl.u8[0]) & 0x0c) >> 2);
218 }
219 
220 
235 static inline uint8_t ipv6_hdr_get_tc_dscp(const ipv6_hdr_t *hdr)
236 {
237  return ((((hdr->v_tc_fl.u8[0]) & 0x03) << 4) |
238  ((hdr->v_tc_fl.u8[1] & 0xf0) >> 4));
239 }
240 
247 static inline void ipv6_hdr_set_fl(ipv6_hdr_t *hdr, uint32_t fl)
248 {
249  hdr->v_tc_fl.u8[1] &= 0xf0;
250  hdr->v_tc_fl.u8[1] |= (0x0f & (byteorder_htonl(fl).u8[1]));
251  hdr->v_tc_fl.u16[1] = byteorder_htonl(fl).u16[1];
252 }
253 
261 static inline uint32_t ipv6_hdr_get_fl(const ipv6_hdr_t *hdr)
262 {
263  return byteorder_ntohl(hdr->v_tc_fl) & 0x000fffff;
264 }
265 
284 static inline uint16_t ipv6_hdr_inet_csum(uint16_t sum, ipv6_hdr_t *hdr,
285  uint8_t prot_num, uint16_t len)
286 {
287  if (((uint32_t)sum + len + prot_num) > 0xffff) {
288  /* increment by one for overflow to keep it as 1's complement sum */
289  sum++;
290  }
291 
292  return inet_csum(sum + len + prot_num, hdr->src.u8,
293  (2 * sizeof(ipv6_addr_t)));
294 }
295 
301 void ipv6_hdr_print(ipv6_hdr_t *hdr);
302 
303 #ifdef __cplusplus
304 }
305 #endif
306 
307 #endif /* NET_IPV6_HDR_H */
308 
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:127
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:90
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:180
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:66
ipv6_addr_t src
source address of this packet.
Definition: ipv6/hdr.h:92
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:114
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:284
uint8_t hl
hop limit for this packet.
Definition: ipv6/hdr.h:91
ipv6_addr_t dst
destination address of this packet.
Definition: ipv6/hdr.h:93
network_uint32_t v_tc_fl
Version, traffic class, and flow label.
Definition: ipv6/hdr.h:88
static void ipv6_hdr_set_tc(ipv6_hdr_t *hdr, uint8_t tc)
Sets the traffic class field of hdr.
Definition: ipv6/hdr.h:138
static uint32_t byteorder_ntohl(network_uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition: byteorder.h:403
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:215
static network_uint32_t byteorder_htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition: byteorder.h:386
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:247
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:160
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:261
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:89
static void ipv6_hdr_set_version(ipv6_hdr_t *hdr)
Sets the version field of hdr to 6.
Definition: ipv6/hdr.h:101
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:195
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:235