1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 1982, 1986, 1990, 1993 3 * The Regents of the University of California. 4 * Copyright(c) 2010-2014 Intel Corporation. 5 * Copyright(c) 2014 6WIND S.A. 6 * All rights reserved. 7 */ 8 9 #ifndef _RTE_IP_H_ 10 #define _RTE_IP_H_ 11 12 /** 13 * @file 14 * 15 * IP-related defines 16 */ 17 18 #include <stdint.h> 19 20 #ifdef RTE_EXEC_ENV_WINDOWS 21 #include <ws2tcpip.h> 22 #else 23 #include <sys/socket.h> 24 #include <sys/types.h> 25 #include <netinet/in.h> 26 #include <arpa/inet.h> 27 #include <netinet/ip.h> 28 #include <netinet/ip6.h> 29 #endif 30 31 #include <rte_byteorder.h> 32 #include <rte_mbuf.h> 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 /** 39 * IPv4 Header 40 */ 41 struct rte_ipv4_hdr { 42 __extension__ 43 union { 44 uint8_t version_ihl; /**< version and header length */ 45 struct { 46 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN 47 uint8_t ihl:4; /**< header length */ 48 uint8_t version:4; /**< version */ 49 #elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN 50 uint8_t version:4; /**< version */ 51 uint8_t ihl:4; /**< header length */ 52 #endif 53 }; 54 }; 55 uint8_t type_of_service; /**< type of service */ 56 rte_be16_t total_length; /**< length of packet */ 57 rte_be16_t packet_id; /**< packet ID */ 58 rte_be16_t fragment_offset; /**< fragmentation offset */ 59 uint8_t time_to_live; /**< time to live */ 60 uint8_t next_proto_id; /**< protocol ID */ 61 rte_be16_t hdr_checksum; /**< header checksum */ 62 rte_be32_t src_addr; /**< source address */ 63 rte_be32_t dst_addr; /**< destination address */ 64 } __rte_packed; 65 66 /** Create IPv4 address */ 67 #define RTE_IPV4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \ 68 (((b) & 0xff) << 16) | \ 69 (((c) & 0xff) << 8) | \ 70 ((d) & 0xff)) 71 72 /** Maximal IPv4 packet length (including a header) */ 73 #define RTE_IPV4_MAX_PKT_LEN 65535 74 75 /** Internet header length mask for version_ihl field */ 76 #define RTE_IPV4_HDR_IHL_MASK (0x0f) 77 /** 78 * Internet header length field multiplier (IHL field specifies overall header 79 * length in number of 4-byte words) 80 */ 81 #define RTE_IPV4_IHL_MULTIPLIER (4) 82 83 /* Type of Service fields */ 84 #define RTE_IPV4_HDR_DSCP_MASK (0xfc) 85 #define RTE_IPV4_HDR_ECN_MASK (0x03) 86 #define RTE_IPV4_HDR_ECN_CE RTE_IPV4_HDR_ECN_MASK 87 88 /* Fragment Offset * Flags. */ 89 #define RTE_IPV4_HDR_DF_SHIFT 14 90 #define RTE_IPV4_HDR_MF_SHIFT 13 91 #define RTE_IPV4_HDR_FO_SHIFT 3 92 93 #define RTE_IPV4_HDR_DF_FLAG (1 << RTE_IPV4_HDR_DF_SHIFT) 94 #define RTE_IPV4_HDR_MF_FLAG (1 << RTE_IPV4_HDR_MF_SHIFT) 95 96 #define RTE_IPV4_HDR_OFFSET_MASK ((1 << RTE_IPV4_HDR_MF_SHIFT) - 1) 97 98 #define RTE_IPV4_HDR_OFFSET_UNITS 8 99 100 /* IPv4 options */ 101 #define RTE_IPV4_HDR_OPT_EOL 0 102 #define RTE_IPV4_HDR_OPT_NOP 1 103 #define RTE_IPV4_HDR_OPT_COPIED(v) ((v) & 0x80) 104 #define RTE_IPV4_HDR_OPT_MAX_LEN 40 105 106 /* 107 * IPv4 address types 108 */ 109 #define RTE_IPV4_ANY ((uint32_t)0x00000000) /**< 0.0.0.0 */ 110 #define RTE_IPV4_LOOPBACK ((uint32_t)0x7f000001) /**< 127.0.0.1 */ 111 #define RTE_IPV4_BROADCAST ((uint32_t)0xe0000000) /**< 224.0.0.0 */ 112 #define RTE_IPV4_ALLHOSTS_GROUP ((uint32_t)0xe0000001) /**< 224.0.0.1 */ 113 #define RTE_IPV4_ALLRTRS_GROUP ((uint32_t)0xe0000002) /**< 224.0.0.2 */ 114 #define RTE_IPV4_MAX_LOCAL_GROUP ((uint32_t)0xe00000ff) /**< 224.0.0.255 */ 115 116 /* 117 * IPv4 Multicast-related macros 118 */ 119 #define RTE_IPV4_MIN_MCAST \ 120 RTE_IPV4(224, 0, 0, 0) /**< Minimal IPv4-multicast address */ 121 #define RTE_IPV4_MAX_MCAST \ 122 RTE_IPV4(239, 255, 255, 255) /**< Maximum IPv4 multicast address */ 123 124 #define RTE_IS_IPV4_MCAST(x) \ 125 ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST) 126 /**< check if IPv4 address is multicast */ 127 128 /* IPv4 default fields values */ 129 #define RTE_IPV4_MIN_IHL (0x5) 130 #define RTE_IPV4_VHL_DEF ((IPVERSION << 4) | RTE_IPV4_MIN_IHL) 131 132 /** 133 * Get the length of an IPv4 header. 134 * 135 * @param ipv4_hdr 136 * Pointer to the IPv4 header. 137 * @return 138 * The length of the IPv4 header (with options if present) in bytes. 139 */ 140 static inline uint8_t 141 rte_ipv4_hdr_len(const struct rte_ipv4_hdr *ipv4_hdr) 142 { 143 return (uint8_t)((ipv4_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK) * 144 RTE_IPV4_IHL_MULTIPLIER); 145 } 146 147 /** 148 * @internal Calculate a sum of all words in the buffer. 149 * Helper routine for the rte_raw_cksum(). 150 * 151 * @param buf 152 * Pointer to the buffer. 153 * @param len 154 * Length of the buffer. 155 * @param sum 156 * Initial value of the sum. 157 * @return 158 * sum += Sum of all words in the buffer. 159 */ 160 static inline uint32_t 161 __rte_raw_cksum(const void *buf, size_t len, uint32_t sum) 162 { 163 const void *end; 164 165 for (end = RTE_PTR_ADD(buf, RTE_ALIGN_FLOOR(len, sizeof(uint16_t))); 166 buf != end; buf = RTE_PTR_ADD(buf, sizeof(uint16_t))) { 167 uint16_t v; 168 169 memcpy(&v, buf, sizeof(uint16_t)); 170 sum += v; 171 } 172 173 /* if length is odd, keeping it byte order independent */ 174 if (unlikely(len % 2)) { 175 uint16_t left = 0; 176 177 memcpy(&left, end, 1); 178 sum += left; 179 } 180 181 return sum; 182 } 183 184 /** 185 * @internal Reduce a sum to the non-complemented checksum. 186 * Helper routine for the rte_raw_cksum(). 187 * 188 * @param sum 189 * Value of the sum. 190 * @return 191 * The non-complemented checksum. 192 */ 193 static inline uint16_t 194 __rte_raw_cksum_reduce(uint32_t sum) 195 { 196 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff); 197 sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff); 198 return (uint16_t)sum; 199 } 200 201 /** 202 * Process the non-complemented checksum of a buffer. 203 * 204 * @param buf 205 * Pointer to the buffer. 206 * @param len 207 * Length of the buffer. 208 * @return 209 * The non-complemented checksum. 210 */ 211 static inline uint16_t 212 rte_raw_cksum(const void *buf, size_t len) 213 { 214 uint32_t sum; 215 216 sum = __rte_raw_cksum(buf, len, 0); 217 return __rte_raw_cksum_reduce(sum); 218 } 219 220 /** 221 * Compute the raw (non complemented) checksum of a packet. 222 * 223 * @param m 224 * The pointer to the mbuf. 225 * @param off 226 * The offset in bytes to start the checksum. 227 * @param len 228 * The length in bytes of the data to checksum. 229 * @param cksum 230 * A pointer to the checksum, filled on success. 231 * @return 232 * 0 on success, -1 on error (bad length or offset). 233 */ 234 static inline int 235 rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len, 236 uint16_t *cksum) 237 { 238 const struct rte_mbuf *seg; 239 const char *buf; 240 uint32_t sum, tmp; 241 uint32_t seglen, done; 242 243 /* easy case: all data in the first segment */ 244 if (off + len <= rte_pktmbuf_data_len(m)) { 245 *cksum = rte_raw_cksum(rte_pktmbuf_mtod_offset(m, 246 const char *, off), len); 247 return 0; 248 } 249 250 if (unlikely(off + len > rte_pktmbuf_pkt_len(m))) 251 return -1; /* invalid params, return a dummy value */ 252 253 /* else browse the segment to find offset */ 254 seglen = 0; 255 for (seg = m; seg != NULL; seg = seg->next) { 256 seglen = rte_pktmbuf_data_len(seg); 257 if (off < seglen) 258 break; 259 off -= seglen; 260 } 261 RTE_ASSERT(seg != NULL); 262 if (seg == NULL) 263 return -1; 264 seglen -= off; 265 buf = rte_pktmbuf_mtod_offset(seg, const char *, off); 266 if (seglen >= len) { 267 /* all in one segment */ 268 *cksum = rte_raw_cksum(buf, len); 269 return 0; 270 } 271 272 /* hard case: process checksum of several segments */ 273 sum = 0; 274 done = 0; 275 for (;;) { 276 tmp = __rte_raw_cksum(buf, seglen, 0); 277 if (done & 1) 278 tmp = rte_bswap16((uint16_t)tmp); 279 sum += tmp; 280 done += seglen; 281 if (done == len) 282 break; 283 seg = seg->next; 284 buf = rte_pktmbuf_mtod(seg, const char *); 285 seglen = rte_pktmbuf_data_len(seg); 286 if (seglen > len - done) 287 seglen = len - done; 288 } 289 290 *cksum = __rte_raw_cksum_reduce(sum); 291 return 0; 292 } 293 294 /** 295 * Process the IPv4 checksum of an IPv4 header. 296 * 297 * The checksum field must be set to 0 by the caller. 298 * 299 * @param ipv4_hdr 300 * The pointer to the contiguous IPv4 header. 301 * @return 302 * The complemented checksum to set in the IP packet. 303 */ 304 static inline uint16_t 305 rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr) 306 { 307 uint16_t cksum; 308 cksum = rte_raw_cksum(ipv4_hdr, rte_ipv4_hdr_len(ipv4_hdr)); 309 return (uint16_t)~cksum; 310 } 311 312 /** 313 * Process the pseudo-header checksum of an IPv4 header. 314 * 315 * The checksum field must be set to 0 by the caller. 316 * 317 * Depending on the ol_flags, the pseudo-header checksum expected by the 318 * drivers is not the same. For instance, when TSO is enabled, the IP 319 * payload length must not be included in the packet. 320 * 321 * When ol_flags is 0, it computes the standard pseudo-header checksum. 322 * 323 * @param ipv4_hdr 324 * The pointer to the contiguous IPv4 header. 325 * @param ol_flags 326 * The ol_flags of the associated mbuf. 327 * @return 328 * The non-complemented checksum to set in the L4 header. 329 */ 330 static inline uint16_t 331 rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags) 332 { 333 struct ipv4_psd_header { 334 uint32_t src_addr; /* IP address of source host. */ 335 uint32_t dst_addr; /* IP address of destination host. */ 336 uint8_t zero; /* zero. */ 337 uint8_t proto; /* L4 protocol type. */ 338 uint16_t len; /* L4 length. */ 339 } psd_hdr; 340 341 uint32_t l3_len; 342 343 psd_hdr.src_addr = ipv4_hdr->src_addr; 344 psd_hdr.dst_addr = ipv4_hdr->dst_addr; 345 psd_hdr.zero = 0; 346 psd_hdr.proto = ipv4_hdr->next_proto_id; 347 if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) { 348 psd_hdr.len = 0; 349 } else { 350 l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length); 351 psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len - 352 rte_ipv4_hdr_len(ipv4_hdr))); 353 } 354 return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr)); 355 } 356 357 /** 358 * @internal Calculate the non-complemented IPv4 L4 checksum 359 */ 360 static inline uint16_t 361 __rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) 362 { 363 uint32_t cksum; 364 uint32_t l3_len, l4_len; 365 uint8_t ip_hdr_len; 366 367 ip_hdr_len = rte_ipv4_hdr_len(ipv4_hdr); 368 l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length); 369 if (l3_len < ip_hdr_len) 370 return 0; 371 372 l4_len = l3_len - ip_hdr_len; 373 374 cksum = rte_raw_cksum(l4_hdr, l4_len); 375 cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0); 376 377 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff); 378 379 return (uint16_t)cksum; 380 } 381 382 /** 383 * Process the IPv4 UDP or TCP checksum. 384 * 385 * The layer 4 checksum must be set to 0 in the L4 header by the caller. 386 * 387 * @param ipv4_hdr 388 * The pointer to the contiguous IPv4 header. 389 * @param l4_hdr 390 * The pointer to the beginning of the L4 header. 391 * @return 392 * The complemented checksum to set in the L4 header. 393 */ 394 static inline uint16_t 395 rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr) 396 { 397 uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr); 398 399 cksum = ~cksum; 400 401 /* 402 * Per RFC 768: If the computed checksum is zero for UDP, 403 * it is transmitted as all ones 404 * (the equivalent in one's complement arithmetic). 405 */ 406 if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP) 407 cksum = 0xffff; 408 409 return cksum; 410 } 411 412 /** 413 * @internal Calculate the non-complemented IPv4 L4 checksum of a packet 414 */ 415 static inline uint16_t 416 __rte_ipv4_udptcp_cksum_mbuf(const struct rte_mbuf *m, 417 const struct rte_ipv4_hdr *ipv4_hdr, 418 uint16_t l4_off) 419 { 420 uint16_t raw_cksum; 421 uint32_t cksum; 422 423 if (l4_off > m->pkt_len) 424 return 0; 425 426 if (rte_raw_cksum_mbuf(m, l4_off, m->pkt_len - l4_off, &raw_cksum)) 427 return 0; 428 429 cksum = raw_cksum + rte_ipv4_phdr_cksum(ipv4_hdr, 0); 430 431 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff); 432 433 return (uint16_t)cksum; 434 } 435 436 /** 437 * @warning 438 * @b EXPERIMENTAL: this API may change without prior notice. 439 * 440 * Compute the IPv4 UDP/TCP checksum of a packet. 441 * 442 * @param m 443 * The pointer to the mbuf. 444 * @param ipv4_hdr 445 * The pointer to the contiguous IPv4 header. 446 * @param l4_off 447 * The offset in bytes to start L4 checksum. 448 * @return 449 * The complemented checksum to set in the L4 header. 450 */ 451 __rte_experimental 452 static inline uint16_t 453 rte_ipv4_udptcp_cksum_mbuf(const struct rte_mbuf *m, 454 const struct rte_ipv4_hdr *ipv4_hdr, uint16_t l4_off) 455 { 456 uint16_t cksum = __rte_ipv4_udptcp_cksum_mbuf(m, ipv4_hdr, l4_off); 457 458 cksum = ~cksum; 459 460 /* 461 * Per RFC 768: If the computed checksum is zero for UDP, 462 * it is transmitted as all ones 463 * (the equivalent in one's complement arithmetic). 464 */ 465 if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP) 466 cksum = 0xffff; 467 468 return cksum; 469 } 470 471 /** 472 * Validate the IPv4 UDP or TCP checksum. 473 * 474 * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0 475 * (i.e. no checksum). 476 * 477 * @param ipv4_hdr 478 * The pointer to the contiguous IPv4 header. 479 * @param l4_hdr 480 * The pointer to the beginning of the L4 header. 481 * @return 482 * Return 0 if the checksum is correct, else -1. 483 */ 484 __rte_experimental 485 static inline int 486 rte_ipv4_udptcp_cksum_verify(const struct rte_ipv4_hdr *ipv4_hdr, 487 const void *l4_hdr) 488 { 489 uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr); 490 491 if (cksum != 0xffff) 492 return -1; 493 494 return 0; 495 } 496 497 /** 498 * @warning 499 * @b EXPERIMENTAL: this API may change without prior notice. 500 * 501 * Verify the IPv4 UDP/TCP checksum of a packet. 502 * 503 * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0 504 * (i.e. no checksum). 505 * 506 * @param m 507 * The pointer to the mbuf. 508 * @param ipv4_hdr 509 * The pointer to the contiguous IPv4 header. 510 * @param l4_off 511 * The offset in bytes to start L4 checksum. 512 * @return 513 * Return 0 if the checksum is correct, else -1. 514 */ 515 __rte_experimental 516 static inline uint16_t 517 rte_ipv4_udptcp_cksum_mbuf_verify(const struct rte_mbuf *m, 518 const struct rte_ipv4_hdr *ipv4_hdr, 519 uint16_t l4_off) 520 { 521 uint16_t cksum = __rte_ipv4_udptcp_cksum_mbuf(m, ipv4_hdr, l4_off); 522 523 if (cksum != 0xffff) 524 return -1; 525 526 return 0; 527 } 528 529 /** 530 * IPv6 Header 531 */ 532 struct rte_ipv6_hdr { 533 rte_be32_t vtc_flow; /**< IP version, traffic class & flow label. */ 534 rte_be16_t payload_len; /**< IP payload size, including ext. headers */ 535 uint8_t proto; /**< Protocol, next header. */ 536 uint8_t hop_limits; /**< Hop limits. */ 537 uint8_t src_addr[16]; /**< IP address of source host. */ 538 uint8_t dst_addr[16]; /**< IP address of destination host(s). */ 539 } __rte_packed; 540 541 /* IPv6 vtc_flow: IPv / TC / flow_label */ 542 #define RTE_IPV6_HDR_FL_SHIFT 0 543 #define RTE_IPV6_HDR_TC_SHIFT 20 544 #define RTE_IPV6_HDR_FL_MASK ((1u << RTE_IPV6_HDR_TC_SHIFT) - 1) 545 #define RTE_IPV6_HDR_TC_MASK (0xff << RTE_IPV6_HDR_TC_SHIFT) 546 #define RTE_IPV6_HDR_DSCP_MASK (0xfc << RTE_IPV6_HDR_TC_SHIFT) 547 #define RTE_IPV6_HDR_ECN_MASK (0x03 << RTE_IPV6_HDR_TC_SHIFT) 548 #define RTE_IPV6_HDR_ECN_CE RTE_IPV6_HDR_ECN_MASK 549 550 #define RTE_IPV6_MIN_MTU 1280 /**< Minimum MTU for IPv6, see RFC 8200. */ 551 552 /** 553 * Process the pseudo-header checksum of an IPv6 header. 554 * 555 * Depending on the ol_flags, the pseudo-header checksum expected by the 556 * drivers is not the same. For instance, when TSO is enabled, the IPv6 557 * payload length must not be included in the packet. 558 * 559 * When ol_flags is 0, it computes the standard pseudo-header checksum. 560 * 561 * @param ipv6_hdr 562 * The pointer to the contiguous IPv6 header. 563 * @param ol_flags 564 * The ol_flags of the associated mbuf. 565 * @return 566 * The non-complemented checksum to set in the L4 header. 567 */ 568 static inline uint16_t 569 rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr *ipv6_hdr, uint64_t ol_flags) 570 { 571 uint32_t sum; 572 struct { 573 rte_be32_t len; /* L4 length. */ 574 rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */ 575 } psd_hdr; 576 577 psd_hdr.proto = (uint32_t)(ipv6_hdr->proto << 24); 578 if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) { 579 psd_hdr.len = 0; 580 } else { 581 psd_hdr.len = ipv6_hdr->payload_len; 582 } 583 584 sum = __rte_raw_cksum(ipv6_hdr->src_addr, 585 sizeof(ipv6_hdr->src_addr) + sizeof(ipv6_hdr->dst_addr), 586 0); 587 sum = __rte_raw_cksum(&psd_hdr, sizeof(psd_hdr), sum); 588 return __rte_raw_cksum_reduce(sum); 589 } 590 591 /** 592 * @internal Calculate the non-complemented IPv6 L4 checksum 593 */ 594 static inline uint16_t 595 __rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr) 596 { 597 uint32_t cksum; 598 uint32_t l4_len; 599 600 l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len); 601 602 cksum = rte_raw_cksum(l4_hdr, l4_len); 603 cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0); 604 605 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff); 606 607 return (uint16_t)cksum; 608 } 609 610 /** 611 * Process the IPv6 UDP or TCP checksum. 612 * 613 * The IPv6 header must not be followed by extension headers. The layer 4 614 * checksum must be set to 0 in the L4 header by the caller. 615 * 616 * @param ipv6_hdr 617 * The pointer to the contiguous IPv6 header. 618 * @param l4_hdr 619 * The pointer to the beginning of the L4 header. 620 * @return 621 * The complemented checksum to set in the L4 header. 622 */ 623 static inline uint16_t 624 rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void *l4_hdr) 625 { 626 uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr); 627 628 cksum = ~cksum; 629 630 /* 631 * Per RFC 768: If the computed checksum is zero for UDP, 632 * it is transmitted as all ones 633 * (the equivalent in one's complement arithmetic). 634 */ 635 if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP) 636 cksum = 0xffff; 637 638 return cksum; 639 } 640 641 /** 642 * @internal Calculate the non-complemented IPv6 L4 checksum of a packet 643 */ 644 static inline uint16_t 645 __rte_ipv6_udptcp_cksum_mbuf(const struct rte_mbuf *m, 646 const struct rte_ipv6_hdr *ipv6_hdr, 647 uint16_t l4_off) 648 { 649 uint16_t raw_cksum; 650 uint32_t cksum; 651 652 if (l4_off > m->pkt_len) 653 return 0; 654 655 if (rte_raw_cksum_mbuf(m, l4_off, m->pkt_len - l4_off, &raw_cksum)) 656 return 0; 657 658 cksum = raw_cksum + rte_ipv6_phdr_cksum(ipv6_hdr, 0); 659 660 cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff); 661 662 return (uint16_t)cksum; 663 } 664 665 /** 666 * @warning 667 * @b EXPERIMENTAL: this API may change without prior notice. 668 * 669 * Process the IPv6 UDP or TCP checksum of a packet. 670 * 671 * The IPv6 header must not be followed by extension headers. The layer 4 672 * checksum must be set to 0 in the L4 header by the caller. 673 * 674 * @param m 675 * The pointer to the mbuf. 676 * @param ipv6_hdr 677 * The pointer to the contiguous IPv6 header. 678 * @param l4_off 679 * The offset in bytes to start L4 checksum. 680 * @return 681 * The complemented checksum to set in the L4 header. 682 */ 683 __rte_experimental 684 static inline uint16_t 685 rte_ipv6_udptcp_cksum_mbuf(const struct rte_mbuf *m, 686 const struct rte_ipv6_hdr *ipv6_hdr, uint16_t l4_off) 687 { 688 uint16_t cksum = __rte_ipv6_udptcp_cksum_mbuf(m, ipv6_hdr, l4_off); 689 690 cksum = ~cksum; 691 692 /* 693 * Per RFC 768: If the computed checksum is zero for UDP, 694 * it is transmitted as all ones 695 * (the equivalent in one's complement arithmetic). 696 */ 697 if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP) 698 cksum = 0xffff; 699 700 return cksum; 701 } 702 703 /** 704 * Validate the IPv6 UDP or TCP checksum. 705 * 706 * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0: 707 * this is either invalid or means no checksum in some situations. See 8.1 708 * (Upper-Layer Checksums) in RFC 8200. 709 * 710 * @param ipv6_hdr 711 * The pointer to the contiguous IPv6 header. 712 * @param l4_hdr 713 * The pointer to the beginning of the L4 header. 714 * @return 715 * Return 0 if the checksum is correct, else -1. 716 */ 717 __rte_experimental 718 static inline int 719 rte_ipv6_udptcp_cksum_verify(const struct rte_ipv6_hdr *ipv6_hdr, 720 const void *l4_hdr) 721 { 722 uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr); 723 724 if (cksum != 0xffff) 725 return -1; 726 727 return 0; 728 } 729 730 /** 731 * @warning 732 * @b EXPERIMENTAL: this API may change without prior notice. 733 * 734 * Validate the IPv6 UDP or TCP checksum of a packet. 735 * 736 * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0: 737 * this is either invalid or means no checksum in some situations. See 8.1 738 * (Upper-Layer Checksums) in RFC 8200. 739 * 740 * @param m 741 * The pointer to the mbuf. 742 * @param ipv6_hdr 743 * The pointer to the contiguous IPv6 header. 744 * @param l4_off 745 * The offset in bytes to start L4 checksum. 746 * @return 747 * Return 0 if the checksum is correct, else -1. 748 */ 749 __rte_experimental 750 static inline int 751 rte_ipv6_udptcp_cksum_mbuf_verify(const struct rte_mbuf *m, 752 const struct rte_ipv6_hdr *ipv6_hdr, 753 uint16_t l4_off) 754 { 755 uint16_t cksum = __rte_ipv6_udptcp_cksum_mbuf(m, ipv6_hdr, l4_off); 756 757 if (cksum != 0xffff) 758 return -1; 759 760 return 0; 761 } 762 763 /** IPv6 fragment extension header. */ 764 #define RTE_IPV6_EHDR_MF_SHIFT 0 765 #define RTE_IPV6_EHDR_MF_MASK 1 766 #define RTE_IPV6_EHDR_FO_SHIFT 3 767 #define RTE_IPV6_EHDR_FO_MASK (~((1 << RTE_IPV6_EHDR_FO_SHIFT) - 1)) 768 #define RTE_IPV6_EHDR_FO_ALIGN (1 << RTE_IPV6_EHDR_FO_SHIFT) 769 770 #define RTE_IPV6_FRAG_USED_MASK (RTE_IPV6_EHDR_MF_MASK | RTE_IPV6_EHDR_FO_MASK) 771 772 #define RTE_IPV6_GET_MF(x) ((x) & RTE_IPV6_EHDR_MF_MASK) 773 #define RTE_IPV6_GET_FO(x) ((x) >> RTE_IPV6_EHDR_FO_SHIFT) 774 775 #define RTE_IPV6_SET_FRAG_DATA(fo, mf) \ 776 (((fo) & RTE_IPV6_EHDR_FO_MASK) | ((mf) & RTE_IPV6_EHDR_MF_MASK)) 777 778 struct rte_ipv6_fragment_ext { 779 uint8_t next_header; /**< Next header type */ 780 uint8_t reserved; /**< Reserved */ 781 rte_be16_t frag_data; /**< All fragmentation data */ 782 rte_be32_t id; /**< Packet ID */ 783 } __rte_packed; 784 785 /* IPv6 fragment extension header size */ 786 #define RTE_IPV6_FRAG_HDR_SIZE sizeof(struct rte_ipv6_fragment_ext) 787 788 /** 789 * Parse next IPv6 header extension 790 * 791 * This function checks if proto number is an IPv6 extensions and parses its 792 * data if so, providing information on next header and extension length. 793 * 794 * @param p 795 * Pointer to an extension raw data. 796 * @param proto 797 * Protocol number extracted from the "next header" field from 798 * the IPv6 header or the previous extension. 799 * @param ext_len 800 * Extension data length. 801 * @return 802 * next protocol number if proto is an IPv6 extension, -EINVAL otherwise 803 */ 804 __rte_experimental 805 static inline int 806 rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len) 807 { 808 int next_proto; 809 810 switch (proto) { 811 case IPPROTO_AH: 812 next_proto = *p++; 813 *ext_len = (*p + 2) * sizeof(uint32_t); 814 break; 815 816 case IPPROTO_HOPOPTS: 817 case IPPROTO_ROUTING: 818 case IPPROTO_DSTOPTS: 819 next_proto = *p++; 820 *ext_len = (*p + 1) * sizeof(uint64_t); 821 break; 822 823 case IPPROTO_FRAGMENT: 824 next_proto = *p; 825 *ext_len = RTE_IPV6_FRAG_HDR_SIZE; 826 break; 827 828 default: 829 return -EINVAL; 830 } 831 832 return next_proto; 833 } 834 835 #ifdef __cplusplus 836 } 837 #endif 838 839 #endif /* _RTE_IP_H_ */ 840