Loading...
Searching...
No Matches
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
30extern "C" {
31#endif
32
66typedef struct __attribute__((packed)) {
90 uint8_t nh;
91 uint8_t hl;
95
101static 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
114static inline uint8_t ipv6_hdr_get_version(const ipv6_hdr_t *hdr)
115{
116 return ((hdr->v_tc_fl.u8[0]) >> 4);
117}
118
127static inline bool ipv6_hdr_is(const ipv6_hdr_t *hdr)
128{
129 return (((hdr->v_tc_fl.u8[0]) & 0xf0) == 0x60);
130}
131
138static 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
160static 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
180static 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
195static 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
215static 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
234static inline uint8_t ipv6_hdr_get_tc_dscp(const ipv6_hdr_t *hdr)
235{
236 return ((((hdr->v_tc_fl.u8[0]) & 0x03) << 4) |
237 ((hdr->v_tc_fl.u8[1] & 0xf0) >> 4));
238}
239
246static inline void ipv6_hdr_set_fl(ipv6_hdr_t *hdr, uint32_t fl)
247{
248 hdr->v_tc_fl.u8[1] &= 0xf0;
249 hdr->v_tc_fl.u8[1] |= (0x0f & (byteorder_htonl(fl).u8[1]));
250 hdr->v_tc_fl.u16[1] = byteorder_htonl(fl).u16[1];
251}
252
260static inline uint32_t ipv6_hdr_get_fl(const ipv6_hdr_t *hdr)
261{
262 return byteorder_ntohl(hdr->v_tc_fl) & 0x000fffff;
263}
264
283static inline uint16_t ipv6_hdr_inet_csum(uint16_t sum, ipv6_hdr_t *hdr,
284 uint8_t prot_num, uint16_t len)
285{
286 if (((uint32_t)sum + len + prot_num) > 0xffff) {
287 /* increment by one for overflow to keep it as 1's complement sum */
288 sum++;
289 }
290
291 return inet_csum(sum + len + prot_num, hdr->src.u8,
292 (2 * sizeof(ipv6_addr_t)));
293}
294
301
302#ifdef __cplusplus
303}
304#endif
305
306#endif /* NET_IPV6_HDR_H */
Functions to work with different byte orders.
static uint32_t byteorder_ntohl(network_uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition byteorder.h:511
static network_uint32_t byteorder_htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition byteorder.h:492
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_fl(ipv6_hdr_t *hdr, uint32_t fl)
Sets the flow label field of hdr.
Definition hdr.h:246
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
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
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:260
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
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
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:234
static void ipv6_hdr_set_version(ipv6_hdr_t *hdr)
Sets the version field of hdr to 6.
Definition hdr.h:101
void ipv6_hdr_print(ipv6_hdr_t *hdr)
Outputs an IPv6 header to stdout.
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
static bool ipv6_hdr_is(const ipv6_hdr_t *hdr)
Checks if the version field is set to 6.
Definition hdr.h:127
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:283
static void ipv6_hdr_set_tc(ipv6_hdr_t *hdr, uint8_t tc)
Sets the traffic class field of hdr.
Definition hdr.h:138
Internet Checksum definitions.
Definitions for IPv6 addresses.
Data type to represent an IPv6 packet header.
Definition hdr.h:66
network_uint32_t v_tc_fl
Version, traffic class, and flow label.
Definition hdr.h:88
uint8_t nh
type of next header in this packet.
Definition hdr.h:90
uint8_t hl
hop limit for this packet.
Definition hdr.h:91
network_uint16_t len
payload length of this packet.
Definition hdr.h:89
ipv6_addr_t src
source address of this packet.
Definition hdr.h:92
ipv6_addr_t dst
destination address of this packet.
Definition hdr.h:93
A 16 bit integer in big endian aka network byte order.
Definition byteorder.h:74
A 32 bit integer in big endian aka network byte order.
Definition byteorder.h:84
uint16_t u16[2]
16 bit representation
Definition byteorder.h:87
uint8_t u8[4]
8 bit representation
Definition byteorder.h:86
Data type to represent an IPv6 address.
Definition addr.h:72
uint8_t u8[16]
divided by 16 8-bit words.
Definition addr.h:73