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 
ipv6_hdr_get_tc
static uint8_t ipv6_hdr_get_tc(const ipv6_hdr_t *hdr)
Gets the value of the traffic class field of hdr.
Definition: hdr.h:195
ipv6_addr_t::u8
uint8_t u8[16]
divided by 16 8-bit words.
Definition: addr.h:75
ipv6_hdr_set_tc_dscp
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: hdr.h:180
be_uint32_t
A 32 bit integer in big endian aka network byte order.
Definition: byteorder.h:87
ipv6_hdr_t::len
network_uint16_t len
payload length of this packet.
Definition: hdr.h:89
ipv6_hdr_get_tc_dscp
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: hdr.h:235
byteorder.h
Functions to work with different byte orders.
be_uint16_t
A 16 bit integer in big endian aka network byte order.
Definition: byteorder.h:77
ipv6_addr_t
Data type to represent an IPv6 address.
Definition: addr.h:74
addr.h
Definitions for IPv6 addresses.
ipv6_hdr_set_version
static void ipv6_hdr_set_version(ipv6_hdr_t *hdr)
Sets the version field of hdr to 6.
Definition: hdr.h:101
ipv6_hdr_set_tc
static void ipv6_hdr_set_tc(ipv6_hdr_t *hdr, uint8_t tc)
Sets the traffic class field of hdr.
Definition: hdr.h:138
ipv6_hdr_is
static bool ipv6_hdr_is(const ipv6_hdr_t *hdr)
Checks if the version field is set to 6.
Definition: hdr.h:127
inet_csum
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
byteorder_ntohl
static uint32_t byteorder_ntohl(network_uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition: byteorder.h:463
ipv6_hdr_get_version
static uint8_t ipv6_hdr_get_version(const ipv6_hdr_t *hdr)
Gets the value of the version field of hdr.
Definition: hdr.h:114
ipv6_hdr_set_fl
static void ipv6_hdr_set_fl(ipv6_hdr_t *hdr, uint32_t fl)
Sets the flow label field of hdr.
Definition: hdr.h:247
ipv6_hdr_t::nh
uint8_t nh
type of next header in this packet.
Definition: hdr.h:90
byteorder_htonl
static network_uint32_t byteorder_htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition: byteorder.h:444
ipv6_hdr_get_fl
static uint32_t ipv6_hdr_get_fl(const ipv6_hdr_t *hdr)
Gets the value of the flow label field of hdr.
Definition: hdr.h:261
ipv6_hdr_t::hl
uint8_t hl
hop limit for this packet.
Definition: hdr.h:91
ipv6_hdr_t::src
ipv6_addr_t src
source address of this packet.
Definition: hdr.h:92
ipv6_hdr_t::dst
ipv6_addr_t dst
destination address of this packet.
Definition: hdr.h:93
be_uint32_t::u8
uint8_t u8[4]
8 bit representation
Definition: byteorder.h:89
ipv6_hdr_get_tc_ecn
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: hdr.h:215
ipv6_hdr_print
void ipv6_hdr_print(ipv6_hdr_t *hdr)
Outputs an IPv6 header to stdout.
be_uint32_t::u16
uint16_t u16[2]
16 bit representation
Definition: byteorder.h:90
ipv6_hdr_set_tc_ecn
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: hdr.h:160
ipv6_hdr_t
Data type to represent an IPv6 packet header.
Definition: hdr.h:66
inet_csum.h
Internet Checksum definitions.
ipv6_hdr_t::v_tc_fl
network_uint32_t v_tc_fl
Version, traffic class, and flow label.
Definition: hdr.h:88
ipv6_hdr_inet_csum
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: hdr.h:284