27 #ifndef ROHC_COMMON_IP_H 28 #define ROHC_COMMON_IP_H 108 static inline uint16_t
swab16(
const uint16_t value)
109 __attribute__((warn_unused_result,
const));
117 static inline uint16_t
swab16(
const uint16_t value)
119 return ((value & 0x00ff) << 8) | ((value & 0xff00) >> 8);
123 #if defined(__i386__) || defined(__x86_64__) 127 __attribute__((nonnull(1), warn_unused_result, pure));
143 uint32_t __ihl = ihl;
146 __asm__ __volatile__(
154 1: adcl 16(%1), %0 \n\ 169 :
"=r" (sum),
"=r" (iph),
"=r" (__ihl)
170 :
"1" (iph),
"2" (__ihl)
173 return (uint16_t) (sum & 0xffff);
179 static inline uint16_t
from32to16(
const uint32_t x)
180 __attribute__((warn_unused_result,
const));
186 y = (x & 0xffff) + (x >> 16);
188 y = (y & 0xffff) + (y >> 16);
192 static inline uint16_t
ip_fast_csum(
const uint8_t *
const iph,
194 __attribute__((nonnull(1), warn_unused_result, pure));
203 const uint8_t *buff = iph;
204 size_t len = ihl * 4;
213 odd = 1 & (uintptr_t) buff;
216 #ifdef __LITTLE_ENDIAN 219 result += (*buff << 8);
227 if(2 & (uintptr_t) buff)
229 result += *(uint16_t *) buff;
240 uint32_t word = *(uint32_t *) buff;
242 buff +=
sizeof(uint32_t);
245 carry = (word > result);
249 result = (result & 0xffff) + (result >> 16);
253 result += *(uint16_t *) buff;
259 #ifdef __LITTLE_ENDIAN 262 result += (*buff << 8);
268 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
278 # include <asm/checksum.h> 289 const uint8_t *
const packet,
291 __attribute__((nonnull(1, 2)));
294 __attribute__((nonnull(1, 2)));
297 __attribute__((warn_unused_result, nonnull(1), pure));
300 __attribute__((warn_unused_result, nonnull(1, 2)));
302 __attribute__((warn_unused_result, nonnull(1)));
305 __attribute__((warn_unused_result, nonnull(1, 2)));
308 __attribute__((warn_unused_result, nonnull(1, 2)));
311 __attribute__((warn_unused_result, nonnull(1), pure));
313 __attribute__((warn_unused_result, nonnull(1)));
316 __attribute__((warn_unused_result, nonnull(1), pure));
319 __attribute__((warn_unused_result, nonnull(1), pure));
321 __attribute__((warn_unused_result, nonnull(1), pure));
323 __attribute__((warn_unused_result, nonnull(1), pure));
325 __attribute__((warn_unused_result, nonnull(1), pure));
328 __attribute__((nonnull(1)));
330 __attribute__((nonnull(1)));
332 __attribute__((nonnull(1)));
334 __attribute__((nonnull(1)));
336 __attribute__((nonnull(1, 2)));
338 __attribute__((nonnull(1, 2)));
344 __attribute__((warn_unused_result, nonnull(1), pure));
346 __attribute__((warn_unused_result, nonnull(1), pure));
348 __attribute__((warn_unused_result, nonnull(1), pure));
350 __attribute__((warn_unused_result, nonnull(1), pure));
352 __attribute__((warn_unused_result, nonnull(1), pure));
354 __attribute__((warn_unused_result, nonnull(1), pure));
356 __attribute__((warn_unused_result, nonnull(1), pure));
359 __attribute__((nonnull(1)));
361 __attribute__((nonnull(1)));
367 __attribute__((warn_unused_result, nonnull(1),
const));
369 __attribute__((warn_unused_result, nonnull(1), pure));
371 __attribute__((warn_unused_result, nonnull(1), pure));
373 __attribute__((warn_unused_result, nonnull(1), pure));
375 __attribute__((warn_unused_result, nonnull(1), pure));
377 __attribute__((nonnull(1)));
379 __attribute__((warn_unused_result, nonnull(1), pure));
381 __attribute__((warn_unused_result, nonnull(1)));
unsigned int ip_get_tos(const struct ip_packet *const ip)
Get the IPv4 Type Of Service (TOS) or IPv6 Traffic Class (TC) of an IP packet.
Definition: ip.c:542
size_t size
The length (in bytes) of the whole IP data (header + payload)
Definition: ip.h:95
uint8_t * ip_get_next_layer(const struct ip_packet *const ip)
Get the next header (but skip IP extensions)
Definition: ip.c:248
uint16_t ipv4_get_id_nbo(const struct ip_packet *const ip, const unsigned int nbo)
Get the IP-ID of an IPv4 packet in Network Byte Order.
Definition: ip.c:762
struct ipv4_hdr v4
The IPv4 header.
Definition: ip.h:86
IP version 4 (malformed)
Definition: ip.h:58
not IP
Definition: ip.h:56
const struct ipv6_addr * ipv6_get_daddr(const struct ip_packet *const ip)
Get the destination address of an IPv6 packet.
Definition: ip.c:942
union ip_packet::@1 header
The IP header.
unsigned short ip_get_total_extension_size(const struct ip_packet *const ip)
Get the size of the extension list.
Definition: ip.c:353
const uint8_t * data
The whole IP data (header + payload) if not NULL.
Definition: ip.h:92
static uint16_t swab16(const uint16_t value)
In-place change the byte order in a two-byte value.
Definition: ip.h:117
void ip_create(struct ip_packet *const ip, const uint8_t *const packet, const size_t size)
Create an IP packet from raw data.
Definition: ip.c:67
ip_header_pos_t
Definition: ip.h:39
uint8_t ihl
Definition: ipv4.h:59
void ip_get_inner_packet(const struct ip_packet *const outer, struct ip_packet *const inner)
Get the inner IP packet (IP in IP)
Definition: ip.c:209
void ip_set_flow_label(struct ip_packet *const ip, const uint32_t value)
Set the flow label of an IPv6 packet.
Definition: ip.c:910
IP version 6.
Definition: ip.h:54
unsigned short ip_get_extension_size(const uint8_t *const ext)
Get the size of an IPv6 extension.
Definition: ip.c:336
static ip_version ip_get_version(const struct ip_packet *const ip)
Get the IP version of an IP packet.
Definition: ip.h:394
uint8_t ip_get_protocol(const struct ip_packet *const ip)
Get the protocol transported by an IP packet.
Definition: ip.c:497
static uint16_t ip_fast_csum(const uint8_t *const iph, const size_t ihl)
Definition: ip.h:200
ip_version version
The version of the IP packet.
Definition: ip.h:80
The IPv6 header.
Definition: ipv6.h:88
uint8_t proto
Definition: ip.h:67
uint32_t ip_get_flow_label(const struct ip_packet *const ip)
Get the flow label of an IPv6 packet.
Definition: ip.c:894
void ip_set_tos(struct ip_packet *const ip, const uint8_t value)
Set the IPv4 Type Of Service (TOS) or IPv6 Traffic Class (TC) of an IP packet.
Definition: ip.c:577
size_t len
Definition: ip.h:69
The IPv4 header.
Definition: ipv4.h:53
uint32_t ipv4_get_daddr(const struct ip_packet *const ip)
Get the destination address of an IPv4 packet.
Definition: ip.c:857
uint8_t * ip_get_next_ext_from_ip(const struct ip_packet *const ip, uint8_t *const type)
Get the next extension header of IPv6 packets from an IPv6 header.
Definition: ip.c:270
const struct ipv6_hdr * ipv6_get_header(const struct ip_packet *const ip)
Get the IPv6 header.
Definition: ip.c:878
int ipv4_get_df(const struct ip_packet *const ip)
Get the Don't Fragment (DF) bit of an IPv4 packet.
Definition: ip.c:809
void ip_set_daddr(struct ip_packet *const ip, const uint8_t *value)
Set the Destination Address of an IP packet.
Definition: ip.c:694
static size_t ipv6_get_hdrlen(const struct ip_packet *const ip)
Definition: ip.h:408
void ipv4_set_df(struct ip_packet *const ip, const int value)
Set the Don't Fragment (DF) bit of an IPv4 packet.
Definition: ip.c:825
void ip_set_saddr(struct ip_packet *const ip, const uint8_t *value)
Set the Source Address of an IP packet.
Definition: ip.c:667
void ip_set_ttl(struct ip_packet *const ip, const uint8_t value)
Set the IPv4 Time To Live (TTL) or IPv6 Hop Limit (HL) of an IP packet.
Definition: ip.c:640
const uint8_t * ip_get_raw_data(const struct ip_packet *const ip)
Get the IP raw data (header + payload)
Definition: ip.c:194
unsigned int ip_get_hdrlen(const struct ip_packet *const ip)
Get the length of an IP header.
Definition: ip.c:447
const struct ipv4_hdr * ipv4_get_header(const struct ip_packet *const ip)
Get the IPv4 header.
Definition: ip.c:726
void ipv4_set_id(struct ip_packet *const ip, const int value)
Set the IP-ID of an IPv4 packet.
Definition: ip.c:793
const struct ipv6_addr * ipv6_get_saddr(const struct ip_packet *const ip)
Get the source address of an IPv6 packet.
Definition: ip.c:926
IP version 6 (malformed)
Definition: ip.h:60
ip_version
IP version.
Definition: ip.h:49
static size_t ipv4_get_hdrlen(const struct ip_packet *const ip)
Definition: ip.h:401
Defines an IP-agnostic packet that can handle an IPv4 or IPv6 packet.
Definition: ip.h:77
IP version 4.
Definition: ip.h:52
uint8_t * ip_get_next_header(const struct ip_packet *const ip, uint8_t *const type)
Get the IP next header.
Definition: ip.c:227
unsigned int ip_get_ttl(const struct ip_packet *const ip)
Get the IPv4 Time To Live (TTL) or IPv6 Hop Limit (HL) of an IP packet.
Definition: ip.c:605
bool ip_is_fragment(const struct ip_packet *const ip)
Whether the IP packet is an IP fragment or not.
Definition: ip.c:383
void ip_set_protocol(struct ip_packet *const ip, const uint8_t value)
Set the protocol transported by an IP packet.
Definition: ip.c:512
The IPv6 address.
Definition: ipv6.h:65
void ip_set_version(struct ip_packet *const ip, const ip_version value)
Set the IP version of an IP packet.
Definition: ip.c:478
unsigned int ip_get_totlen(const struct ip_packet *const ip)
Get the total length of an IP packet.
Definition: ip.c:417
uint32_t ipv4_get_saddr(const struct ip_packet *const ip)
Get the source address of an IPv4 packet.
Definition: ip.c:841
uint8_t * data
Definition: ip.h:68
uint8_t * ip_get_next_ext_from_ext(const uint8_t *const ext, uint8_t *const type)
Get the next extension header of IPv6 packets from another extension.
Definition: ip.c:308
static uint16_t from32to16(const uint32_t x)
Definition: ip.h:182
uint16_t ipv4_get_id(const struct ip_packet *const ip)
Get the IP-ID of an IPv4 packet.
Definition: ip.c:745