19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <netinet/ip.h>
47 #define CNE_IPV4(a, b, c, d) \
48 ((uint32_t)(((a)&0xff) << 24) | (((b)&0xff) << 16) | (((c)&0xff) << 8) | ((d)&0xff))
51 #define CNE_IPV4_MAX_PKT_LEN 65535
54 #define CNE_IPV4_HDR_IHL_MASK (0x0f)
59 #define CNE_IPV4_IHL_MULTIPLIER (4)
62 #define CNE_IPV4_HDR_DSCP_MASK (0xfc)
63 #define CNE_IPV4_HDR_ECN_MASK (0x03)
64 #define CNE_IPV4_HDR_ECN_CE CNE_IPV4_HDR_ECN_MASK
67 #define CNE_IPV4_HDR_DF_SHIFT 14
68 #define CNE_IPV4_HDR_MF_SHIFT 13
69 #define CNE_IPV4_HDR_FO_SHIFT 3
71 #define CNE_IPV4_HDR_DF_FLAG (1 << CNE_IPV4_HDR_DF_SHIFT)
72 #define CNE_IPV4_HDR_MF_FLAG (1 << CNE_IPV4_HDR_MF_SHIFT)
74 #define CNE_IPV4_HDR_OFFSET_MASK ((1 << CNE_IPV4_HDR_MF_SHIFT) - 1)
76 #define CNE_IPV4_HDR_OFFSET_UNITS 8
81 #define CNE_IPV4_ANY ((uint32_t)0x00000000)
82 #define CNE_IPV4_LOOPBACK ((uint32_t)0x7f000001)
83 #define CNE_IPV4_BROADCAST ((uint32_t)0xe0000000)
84 #define CNE_IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001)
85 #define CNE_IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002)
86 #define CNE_IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff)
91 #define CNE_IPV4_MIN_MCAST CNE_IPV4(224, 0, 0, 0)
92 #define CNE_IPV4_MAX_MCAST \
93 CNE_IPV4(239, 255, 255, 255)
96 #define CNE_IS_IPV4_MCAST(x) ((x) >= CNE_IPV4_MIN_MCAST && (x) <= CNE_IPV4_MAX_MCAST)
100 #define CNE_IPV4_MIN_IHL (0x5)
101 #define CNE_IPV4_VHL_DEF ((IPVERSION << 4) | CNE_IPV4_MIN_IHL)
111 static inline uint8_t
130 static inline uint32_t
131 __cne_raw_cksum(
const void *buf,
size_t len, uint32_t sum)
134 uintptr_t ptr = (uintptr_t)buf;
135 typedef uint16_t __attribute__((__may_alias__)) u16_p;
136 const u16_p *u16_buf = (
const u16_p *)ptr;
138 while (len >= (
sizeof(*u16_buf) * 4)) {
143 len -=
sizeof(*u16_buf) * 4;
146 while (len >=
sizeof(*u16_buf)) {
148 len -=
sizeof(*u16_buf);
155 *(uint8_t *)&left = *(
const uint8_t *)u16_buf;
171 static inline uint16_t
172 __cne_raw_cksum_reduce(uint32_t sum)
174 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
175 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
176 return (uint16_t)sum;
189 static inline uint16_t
194 sum = __cne_raw_cksum(buf, len, 0);
195 return __cne_raw_cksum_reduce(sum);
210 static inline uint16_t
226 static inline uint16_t
231 return (uint16_t)~cksum;
252 static inline uint16_t
255 struct ipv4_psd_header {
264 psd_hdr.src_addr = ipv4_hdr->
src_addr;
265 psd_hdr.dst_addr = ipv4_hdr->
dst_addr;
292 static inline uint16_t
296 uint32_t l3_len, l4_len;
301 if (l3_len < ip_hdr_len)
304 l4_len = l3_len - ip_hdr_len;
309 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
311 return (uint16_t)cksum;
327 static inline uint16_t
381 #define CNE_IPV6_HDR_FL_SHIFT 0
382 #define CNE_IPV6_HDR_TC_SHIFT 20
383 #define CNE_IPV6_HDR_FL_MASK ((1u << CNE_IPV6_HDR_TC_SHIFT) - 1)
384 #define CNE_IPV6_HDR_TC_MASK (0xff << CNE_IPV6_HDR_TC_SHIFT)
385 #define CNE_IPV6_HDR_DSCP_MASK (0xfc << CNE_IPV6_HDR_TC_SHIFT)
386 #define CNE_IPV6_HDR_ECN_MASK (0x03 << CNE_IPV6_HDR_TC_SHIFT)
387 #define CNE_IPV6_HDR_ECN_CE CNE_IPV6_HDR_ECN_MASK
389 #define CNE_IPV6_MIN_MTU 1280
407 static inline uint16_t
416 psd_hdr.proto = (uint32_t)(ipv6_hdr->
proto << 24);
419 sum = __cne_raw_cksum(ipv6_hdr->
src_addr,
421 sum = __cne_raw_cksum(&psd_hdr,
sizeof(psd_hdr), sum);
422 return __cne_raw_cksum_reduce(sum);
438 static inline uint16_t
449 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
450 cksum = (~cksum) & 0xffff;
454 return (uint16_t)cksum;
458 #define CNE_IPV6_EHDR_MF_SHIFT 0
459 #define CNE_IPV6_EHDR_MF_MASK 1
460 #define CNE_IPV6_EHDR_FO_SHIFT 3
461 #define CNE_IPV6_EHDR_FO_MASK (~((1 << CNE_IPV6_EHDR_FO_SHIFT) - 1))
462 #define CNE_IPV6_EHDR_FO_ALIGN (1 << CNE_IPV6_EHDR_FO_SHIFT)
464 #define CNE_IPV6_FRAG_USED_MASK (CNE_IPV6_EHDR_MF_MASK | CNE_IPV6_EHDR_FO_MASK)
466 #define CNE_IPV6_GET_MF(x) ((x)&CNE_IPV6_EHDR_MF_MASK)
467 #define CNE_IPV6_GET_FO(x) ((x) >> CNE_IPV6_EHDR_FO_SHIFT)
469 #define CNE_IPV6_SET_FRAG_DATA(fo, mf) (((fo)&CNE_IPV6_EHDR_FO_MASK) | ((mf)&CNE_IPV6_EHDR_MF_MASK))
471 struct cne_ipv6_fragment_ext {
479 #define CNE_IPV6_FRAG_HDR_SIZE sizeof(struct cne_ipv6_fragment_ext)
506 *ext_len = (*p + 2) *
sizeof(uint32_t);
509 case IPPROTO_HOPOPTS:
510 case IPPROTO_ROUTING:
511 case IPPROTO_DSTOPTS:
513 *ext_len = (*p + 1) *
sizeof(uint64_t);
516 case IPPROTO_FRAGMENT:
518 *ext_len = CNE_IPV6_FRAG_HDR_SIZE;
static int cne_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
static uint16_t cne_ipv4_udptcp_cksum(const struct cne_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
#define CNE_IPV4_IHL_MULTIPLIER
static int cne_ipv4_udptcp_cksum_verify(const struct cne_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
static uint16_t cne_ipv4_phdr_cksum(const struct cne_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
static uint16_t cne_ipv4_cksum(const struct cne_ipv4_hdr *ipv4_hdr)
static uint16_t cne_ipv6_phdr_cksum(const struct cne_ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
static uint16_t __ipv4_udptcp_cksum(const struct cne_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
static uint16_t cne_raw_cksum(const void *buf, size_t len)
#define CNE_IPV4_HDR_IHL_MASK
static uint16_t cne_ipv6_udptcp_cksum(const struct cne_ipv6_hdr *ipv6_hdr, const void *l4_hdr)
static uint8_t cne_ipv4_hdr_len(const struct cne_ipv4_hdr *ipv4_hdr)
static uint16_t cne_raw_cksum_mbuf(const pktmbuf_t *m, uint32_t off, uint32_t len)
#define pktmbuf_mtod_offset(m, t, o)
#define CNE_MBUF_F_TX_TCP_SEG
cne_be16_t fragment_offset