27 #ifndef ROHC_COMMON_IP_H
28 #define ROHC_COMMON_IP_H
36 # include <linux/types.h>
112 static inline uint16_t
swab16(
const uint16_t value)
113 __attribute__((warn_unused_result,
const));
121 static inline uint16_t
swab16(
const uint16_t value)
123 return ((value & 0x00ff) << 8) | ((value & 0xff00) >> 8);
127 #if defined(__i386__) || defined(__x86_64__)
131 __attribute__((nonnull(1), warn_unused_result, pure));
147 uint32_t __ihl = ihl;
150 __asm__ __volatile__(
158 1: adcl 16(%1), %0 \n\
173 :
"=r" (sum),
"=r" (iph),
"=r" (__ihl)
174 :
"1" (iph),
"2" (__ihl)
177 return (uint16_t) (sum & 0xffff);
183 static inline uint16_t
from32to16(
const uint32_t x)
184 __attribute__((warn_unused_result,
const));
190 y = (x & 0xffff) + (x >> 16);
192 y = (y & 0xffff) + (y >> 16);
196 static inline uint16_t
ip_fast_csum(
const uint8_t *
const iph,
198 __attribute__((nonnull(1), warn_unused_result, pure));
207 const uint8_t *buff = iph;
208 size_t len = ihl * 4;
217 odd = 1 & (uintptr_t) buff;
220 #ifdef __LITTLE_ENDIAN
223 result += (*buff << 8);
231 if(2 & (uintptr_t) buff)
233 result += *(uint16_t *) buff;
244 uint32_t word = *(uint32_t *) buff;
246 buff +=
sizeof(uint32_t);
249 carry = (word > result);
253 result = (result & 0xffff) + (result >> 16);
257 result += *(uint16_t *) buff;
263 #ifdef __LITTLE_ENDIAN
266 result += (*buff << 8);
272 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
282 # include <asm/checksum.h>
293 const uint8_t *
const packet,
295 __attribute__((warn_unused_result, nonnull(1, 2)));
298 __attribute__((warn_unused_result, nonnull(1, 2)));
301 __attribute__((warn_unused_result, nonnull(1), pure));
304 __attribute__((warn_unused_result, nonnull(1, 2)));
306 __attribute__((warn_unused_result, nonnull(1)));
309 __attribute__((warn_unused_result, nonnull(1, 2)));
312 __attribute__((warn_unused_result, nonnull(1, 2)));
315 __attribute__((warn_unused_result, nonnull(1), pure));
317 __attribute__((warn_unused_result, nonnull(1)));
320 __attribute__((warn_unused_result, nonnull(1), pure));
322 __attribute__((warn_unused_result, nonnull(1), pure));
324 __attribute__((warn_unused_result, nonnull(1), pure));
326 __attribute__((warn_unused_result, nonnull(1), pure));
328 __attribute__((warn_unused_result, nonnull(1), pure));
331 __attribute__((nonnull(1)));
333 __attribute__((nonnull(1)));
335 __attribute__((nonnull(1)));
337 __attribute__((nonnull(1)));
339 __attribute__((nonnull(1, 2)));
341 __attribute__((nonnull(1, 2)));
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)));
366 __attribute__((warn_unused_result, nonnull(1), pure));
368 __attribute__((warn_unused_result, nonnull(1), pure));
370 __attribute__((warn_unused_result, nonnull(1), pure));
372 __attribute__((warn_unused_result, nonnull(1), pure));
374 __attribute__((nonnull(1)));
376 __attribute__((warn_unused_result, nonnull(1), pure));
378 __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:567
size_t size
The length (in bytes) of the whole IP data (header + payload)
Definition: ip.h:99
uint8_t * ip_get_next_layer(const struct ip_packet *const ip)
Get the next header (but skip IP extensions)
Definition: ip.c:258
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:787
struct ipv4_hdr v4
The IPv4 header.
Definition: ip.h:90
IP version 4 (malformed)
Definition: ip.h:62
not IP
Definition: ip.h:60
const struct ipv6_addr * ipv6_get_daddr(const struct ip_packet *const ip)
Get the destination address of an IPv6 packet.
Definition: ip.c:967
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:363
const uint8_t * data
The whole IP data (header + payload) if not NULL.
Definition: ip.h:96
static uint16_t swab16(const uint16_t value)
In-place change the byte order in a two-byte value.
Definition: ip.h:121
ip_header_pos_t
Definition: ip.h:43
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:935
IP version 6.
Definition: ip.h:58
unsigned short ip_get_extension_size(const uint8_t *const ext)
Get the size of an IPv6 extension.
Definition: ip.c:346
uint8_t ip_get_protocol(const struct ip_packet *const ip)
Get the protocol transported by an IP packet.
Definition: ip.c:522
static uint16_t ip_fast_csum(const uint8_t *const iph, const size_t ihl)
Definition: ip.h:204
ip_version version
The version of the IP packet.
Definition: ip.h:84
The IPv6 header.
Definition: ipv6.h:88
bool 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:70
uint8_t proto
Definition: ip.h:71
uint32_t ip_get_flow_label(const struct ip_packet *const ip)
Get the flow label of an IPv6 packet.
Definition: ip.c:919
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:602
size_t len
Definition: ip.h:73
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:882
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:280
const struct ipv6_hdr * ipv6_get_header(const struct ip_packet *const ip)
Get the IPv6 header.
Definition: ip.c:903
int ipv4_get_df(const struct ip_packet *const ip)
Get the Don't Fragment (DF) bit of an IPv4 packet.
Definition: ip.c:834
void ip_set_daddr(struct ip_packet *const ip, const uint8_t *value)
Set the Destination Address of an IP packet.
Definition: ip.c:719
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:850
void ip_set_saddr(struct ip_packet *const ip, const uint8_t *value)
Set the Source Address of an IP packet.
Definition: ip.c:692
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:665
const uint8_t * ip_get_raw_data(const struct ip_packet *const ip)
Get the IP raw data (header + payload)
Definition: ip.c:203
unsigned int ip_get_hdrlen(const struct ip_packet *const ip)
Get the length of an IP header.
Definition: ip.c:457
struct net_hdr nh
Definition: ip.h:101
const struct ipv4_hdr * ipv4_get_header(const struct ip_packet *const ip)
Get the IPv4 header.
Definition: ip.c:751
void ipv4_set_id(struct ip_packet *const ip, const int value)
Set the IP-ID of an IPv4 packet.
Definition: ip.c:818
const struct ipv6_addr * ipv6_get_saddr(const struct ip_packet *const ip)
Get the source address of an IPv6 packet.
Definition: ip.c:951
IP version 6 (malformed)
Definition: ip.h:64
bool 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:219
ip_version
IP version.
Definition: ip.h:53
Defines an IP-agnostic packet that can handle an IPv4 or IPv6 packet.
Definition: ip.h:81
IP version 4.
Definition: ip.h:56
uint8_t * ip_get_next_header(const struct ip_packet *const ip, uint8_t *const type)
Get the IP next header.
Definition: ip.c:237
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:630
bool ip_is_fragment(const struct ip_packet *const ip)
Whether the IP packet is an IP fragment or not.
Definition: ip.c:393
void ip_set_protocol(struct ip_packet *const ip, const uint8_t value)
Set the protocol transported by an IP packet.
Definition: ip.c:537
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:503
unsigned int ip_get_totlen(const struct ip_packet *const ip)
Get the total length of an IP packet.
Definition: ip.c:427
uint32_t ipv4_get_saddr(const struct ip_packet *const ip)
Get the source address of an IPv4 packet.
Definition: ip.c:866
uint8_t * data
Definition: ip.h:72
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:318
struct ipv6_hdr v6
The IPv6 header.
Definition: ip.h:92
static uint16_t from32to16(const uint32_t x)
Definition: ip.h:186
uint16_t ipv4_get_id(const struct ip_packet *const ip)
Get the IP-ID of an IPv4 packet.
Definition: ip.c:770
struct net_hdr nl
Definition: ip.h:102
ip_version ip_get_version(const struct ip_packet *const ip)
Get the IP version of an IP packet.
Definition: ip.c:491