Loading...
Searching...
No Matches
hdr.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
19
20#include <stdint.h>
21
22#include "byteorder.h"
23#include "net/inet_csum.h"
24#include "net/ipv6/addr.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
63typedef struct __attribute__((packed)) {
87 uint8_t nh;
88 uint8_t hl;
92
98static inline void ipv6_hdr_set_version(ipv6_hdr_t *hdr)
99{
100 hdr->v_tc_fl.u8[0] &= 0x0f;
101 hdr->v_tc_fl.u8[0] |= 0x60;
102}
103
111static inline uint8_t ipv6_hdr_get_version(const ipv6_hdr_t *hdr)
112{
113 return ((hdr->v_tc_fl.u8[0]) >> 4);
114}
115
124static inline bool ipv6_hdr_is(const ipv6_hdr_t *hdr)
125{
126 return (((hdr->v_tc_fl.u8[0]) & 0xf0) == 0x60);
127}
128
135static inline void ipv6_hdr_set_tc(ipv6_hdr_t *hdr, uint8_t tc)
136{
137 hdr->v_tc_fl.u8[0] &= 0xf0;
138 hdr->v_tc_fl.u8[0] |= (0x0f & (tc >> 4));
139 hdr->v_tc_fl.u8[1] &= 0x0f;
140 hdr->v_tc_fl.u8[1] |= (0xf0 & (tc << 4));
141}
142
157static inline void ipv6_hdr_set_tc_ecn(ipv6_hdr_t *hdr, uint8_t ecn)
158{
159 hdr->v_tc_fl.u8[0] &= 0xf3;
160 hdr->v_tc_fl.u8[0] |= (0x0c & (ecn << 2));
161}
162
177static inline void ipv6_hdr_set_tc_dscp(ipv6_hdr_t *hdr, uint8_t dscp)
178{
179 hdr->v_tc_fl.u8[0] &= 0xfc;
180 hdr->v_tc_fl.u8[0] |= (0x03 & (dscp >> 4));
181 hdr->v_tc_fl.u8[1] &= 0x0f;
182 hdr->v_tc_fl.u8[1] |= (0xf0 & (dscp << 4));
183}
184
192static inline uint8_t ipv6_hdr_get_tc(const ipv6_hdr_t *hdr)
193{
194 return ((((hdr->v_tc_fl.u8[0]) & 0x0f) << 4) |
195 ((hdr->v_tc_fl.u8[1] & 0xf0) >> 4));
196}
197
212static inline uint8_t ipv6_hdr_get_tc_ecn(const ipv6_hdr_t *hdr)
213{
214 return (((hdr->v_tc_fl.u8[0]) & 0x0c) >> 2);
215}
216
231static inline uint8_t ipv6_hdr_get_tc_dscp(const ipv6_hdr_t *hdr)
232{
233 return ((((hdr->v_tc_fl.u8[0]) & 0x03) << 4) |
234 ((hdr->v_tc_fl.u8[1] & 0xf0) >> 4));
235}
236
243static inline void ipv6_hdr_set_fl(ipv6_hdr_t *hdr, uint32_t fl)
244{
245 hdr->v_tc_fl.u8[1] &= 0xf0;
246 hdr->v_tc_fl.u8[1] |= (0x0f & (byteorder_htonl(fl).u8[1]));
247 hdr->v_tc_fl.u16[1] = byteorder_htonl(fl).u16[1];
248}
249
257static inline uint32_t ipv6_hdr_get_fl(const ipv6_hdr_t *hdr)
258{
259 return byteorder_ntohl(hdr->v_tc_fl) & 0x000fffff;
260}
261
280static inline uint16_t ipv6_hdr_inet_csum(uint16_t sum, ipv6_hdr_t *hdr,
281 uint8_t prot_num, uint16_t len)
282{
283 if (((uint32_t)sum + len + prot_num) > 0xffff) {
284 /* increment by one for overflow to keep it as 1's complement sum */
285 sum++;
286 }
287
288 return inet_csum(sum + len + prot_num, hdr->src.u8,
289 (2 * sizeof(ipv6_addr_t)));
290}
291
298
299#ifdef __cplusplus
300}
301#endif
302
Functions to work with different byte orders.
be_uint32_t network_uint32_t
A 32 bit integer in network byte order.
Definition byteorder.h:109
static uint32_t byteorder_ntohl(network_uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition byteorder.h:507
be_uint16_t network_uint16_t
A 16 bit integer in network byte order.
Definition byteorder.h:104
static network_uint32_t byteorder_htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition byteorder.h:488
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:69
static void ipv6_hdr_set_fl(ipv6_hdr_t *hdr, uint32_t fl)
Sets the flow label field of hdr.
Definition hdr.h:243
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:192
static uint8_t ipv6_hdr_get_version(const ipv6_hdr_t *hdr)
Gets the value of the version field of hdr.
Definition hdr.h:111
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:257
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:157
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:212
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:231
static void ipv6_hdr_set_version(ipv6_hdr_t *hdr)
Sets the version field of hdr to 6.
Definition hdr.h:98
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:177
static bool ipv6_hdr_is(const ipv6_hdr_t *hdr)
Checks if the version field is set to 6.
Definition hdr.h:124
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:280
static void ipv6_hdr_set_tc(ipv6_hdr_t *hdr, uint8_t tc)
Sets the traffic class field of hdr.
Definition hdr.h:135
Internet Checksum definitions.
Definitions for IPv6 addresses.
Data type to represent an IPv6 packet header.
Definition hdr.h:63
network_uint32_t v_tc_fl
Version, traffic class, and flow label.
Definition hdr.h:85
uint8_t nh
type of next header in this packet.
Definition hdr.h:87
uint8_t hl
hop limit for this packet.
Definition hdr.h:88
network_uint16_t len
payload length of this packet.
Definition hdr.h:86
ipv6_addr_t src
source address of this packet.
Definition hdr.h:89
ipv6_addr_t dst
destination address of this packet.
Definition hdr.h:90
uint16_t u16[2]
16 bit representation
Definition byteorder.h:83
uint8_t u8[4]
8 bit representation
Definition byteorder.h:82
Data type to represent an IPv6 address.
Definition addr.h:64
uint8_t u8[16]
divided by 16 8-bit words.
Definition addr.h:65