/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1990 Mentat Inc. */
/*
* Copyright 2019 Joyent, Inc.
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
extern unsigned int ip_ocsum(ushort_t *address, int halfword_count,
unsigned int sum);
/*
* Checksum routine for Internet Protocol family headers.
* This routine is very heavily used in the network
* code and should be modified for each CPU to be as fast as possible.
*/
/*
* Even/Odd checks. Usually it is performed on pointers but may be
* used on integers as well. uintptr_t is long enough to hold both
* integer and pointer.
*/
#define IS_ODD(p) (((uintptr_t)(p) & 0x1) != 0)
#define IS_EVEN(p) (((uintptr_t)(p) & 0x1) == 0)
/*
*/
#define HAS_UIOSUM(mp) ((mp)->b_datap->db_struioflag & STRUIO_IP)
#ifdef _LITTLE_ENDIAN
#define FRAG(ptr) (*(ptr))
#else
#define FRAG(ptr) (*(ptr) << 8)
#endif
/*
* Give the compiler a hint to help optimize the code layout
*/
#define UNLIKELY(exp) __builtin_expect((exp), 0)
#define FOLD(val) (((val) & 0xFFFF) + ((val) >> 16))
/*
* Note: this does not ones-complement the result since it is used
* when computing partial checksums. It assumes mp->b_rptr + offset is
* 16 bit aligned and a valid offset in mp.
*/
unsigned int
ip_cksum(mblk_t *mp, int offset, uint_t initial_sum)
{
const uint_t sum_mask[2] = { 0, UINT_MAX };
uint64_t sum = initial_sum;
uint64_t total_len = 0;
uchar_t *w;
size_t mlen = MBLKL(mp);
uint_t msum, mask;
ASSERT3S(offset, >=, 0);
VERIFY(!HAS_UIOSUM(mp));
while (UNLIKELY(offset > mlen)) {
ASSERT3P(mp->b_cont, !=, NULL);
mp = mp->b_cont;
VERIFY(!HAS_UIOSUM(mp));
offset -= mlen;
mlen = MBLKL(mp);
}
/*
* Make sure we start with a folded sum. Since the initial sum
* is 32 bits, folding twice will always produce a sum <= 0xFFFF
*/
sum = FOLD(sum);
sum = FOLD(sum);
ASSERT3U(sum, <=, 0xFFFF);
while (mp != NULL) {
w = mp->b_rptr + offset;
mlen = mp->b_wptr - w;
offset = 0;
ASSERT3P(w, <=, mp->b_wptr);
VERIFY(!HAS_UIOSUM(mp));
if (UNLIKELY(mlen == 0)) {
mp = mp->b_cont;
continue;
}
/*
* ip_ocsum() currently requires a 16-bit aligned address.
* For unaligned buffers, we first sum the odd byte (and
* fold if necessary) before calling ip_ocsum(). ip_ocsum()
* also takes its length in units of 16-bit words. If
* we have an odd length, we must also manually add it after
* computing the main sum (and again fold if necessary).
*
* Since ip_ocsum() _should_ be a private per-platform
* optimized ip cksum implementation (with ip_cksum() being
* the less-private wrapper around it), a nice future
* optimization could be to modify ip_ocsum() for each
* platform to take a 64-bit sum. This would allow us to
* only have to fold exactly once before we return --
* the amount of data we'd need to checksum to overflow 64
* bits far exceeds the possible size of any mblk chain we
* could ever have.
*/
if (UNLIKELY(IS_ODD(w))) {
sum += FRAG(w);
w++;
--mlen;
total_len++;
if (UNLIKELY(mlen == 0)) {
mp = mp->b_cont;
continue;
}
}
/*
* ip_ocsum() takes the length as the number of half words
* (i.e. uint16_ts). It returns a result that is already
* folded (<= 0xFFFF).
*/
msum = ip_ocsum((ushort_t *)w, mlen / 2, 0);
ASSERT3U(msum, <=, 0xFFFF);
/*
* We mask the last byte based on the length of data.
* If the length is odd, we AND with UINT_MAX otherwise
* we AND with 0 (resulting in 0) and add the result to
* the mblk_t sum. This effectively gives us:
*
* if (IS_ODD(mlen))
* msum += FRAG(w + mlen - 1);
* else
* msum += 0;
*
* Without incurring a branch.
*/
mask = sum_mask[IS_ODD(mlen)];
msum += FRAG(w + mlen - 1) & mask;
/*
* If the data we are checksumming has been split
* between two mblk_ts along a non-16 bit boundary, that is
* we have something like:
* mblk_t 1: aa bb cc
* mblk_t 2: dd ee ff
* the result must be the same as if we checksummed a
* single mblk_t with 'aa bb cc dd ee ff'. As can be seen
* from the example, this situation causes the grouping of
* the data in the second mblk_t to be offset by a byte.
* The fix is to byteswap the mblk_t sum before adding it
* to the final sum. Again, we AND the mblk_t sum with a mask
* so that either the non-swapped or byteswapped sum is zeroed
* out and the other one is preserved (depending on the
* total bytes checksummed so far) and added to the sum.
*
* Effectively,
*
* if (IS_ODD(total_len))
* sum += BSWAP_32(msum);
* else
* sum += msum;
*/
mask = sum_mask[IS_ODD(total_len)];
sum += BSWAP_32(msum) & mask;
sum += msum & ~mask;
total_len += mlen;
mp = mp->b_cont;
}
/*
* To avoid unnecessary folding, we store the cumulative sum in
* a uint64_t. This means we can always checksum up to 2^56 bytes
* (2^(64-8)) without danger of overflowing. Since 2^56 is well past
* the petabyte range, and is far beyond the amount of data that
* could every be stored in a single mblk_t chain (for the forseeable
* future), this serves more as a sanity check than anything else.
*/
VERIFY3U(total_len, <=, (uint64_t)1 << 56);
/*
* For a 64-bit sum, we have to fold at most 4 times to
* produce a sum <= 0xFFFF.
*/
sum = FOLD(sum);
sum = FOLD(sum);
sum = FOLD(sum);
sum = FOLD(sum);
TRACE_3(TR_FAC_IP, TR_IP_CKSUM_END,
"ip_cksum_end:(%S) type %d (%X)", "ip_cksum", 1, sum);
return ((unsigned int)sum);
}
uint32_t
sctp_cksum(mblk_t *mp, int offset)
{
uint32_t crc32;
uchar_t *p = NULL;
crc32 = 0xFFFFFFFF;
p = mp->b_rptr + offset;
crc32 = sctp_crc32(crc32, p, mp->b_wptr - p);
for (mp = mp->b_cont; mp != NULL; mp = mp->b_cont) {
crc32 = sctp_crc32(crc32, mp->b_rptr, MBLKL(mp));
}
/* Complement the result */
crc32 = ~crc32;
return (crc32);
}
/*
* Routine to compute Internet checksum (16-bit 1's complement) of a given
* Multidata packet descriptor. As in the non-Multidata routine, this doesn't
* 1's complement the result, such that it may be used to compute partial
* checksums. Since it works on buffer spans rather than mblks, this routine
* does not handle existing partial checksum value as in the STRUIO_IP special
* mblk case (supporting this is rather trivial, but is perhaps of no use at
* the moment unless synchronous streams and delayed checksum calculation are
* revived.)
*
* Note also here that the given Multidata packet descriptor must refer to
* a header buffer, i.e. it must have a header fragment. In addition, the
* offset must lie within the boundary of the header fragment. For the
* outbound tcp (MDT) case, this will not be an issue because the stack
* ensures that such conditions are met, and that there is no need whatsoever
* to compute partial checksums on an arbitrary offset that is not part of
* the header fragment. We may need to revisit this routine to handle all
* cases of the inbound (MDR) case, especially when we need to perform partial
* checksum calculation due to padded bytes (non-zeroes) in the frame.
*/
uint_t
ip_md_cksum(pdesc_t *pd, int offset, uint_t sum)
{
pdescinfo_t *pdi = &pd->pd_pdi;
uchar_t *reg_start, *reg_end;
ssize_t mlen, i;
ushort_t *w;
boolean_t byteleft = B_FALSE;
ASSERT((pdi->flags & PDESC_HAS_REF) != 0);
ASSERT(pdi->hdr_rptr != NULL && pdi->hdr_wptr != NULL);
ASSERT(offset <= PDESC_HDRL(pdi));
for (i = 0; i < pdi->pld_cnt + 1; i++) {
if (i == 0) {
reg_start = pdi->hdr_rptr;
reg_end = pdi->hdr_wptr;
} else {
reg_start = pdi->pld_ary[i - 1].pld_rptr;
reg_end = pdi->pld_ary[i - 1].pld_wptr;
offset = 0;
}
w = (ushort_t *)(reg_start + offset);
mlen = reg_end - (uchar_t *)w;
if (mlen > 0 && byteleft) {
/*
* There is a byte left from the last
* segment; add it into the checksum.
* Don't have to worry about a carry-
* out here because we make sure that
* high part of (32 bit) sum is small
* below.
*/
#ifdef _LITTLE_ENDIAN
sum += *(uchar_t *)w << 8;
#else
sum += *(uchar_t *)w;
#endif
w = (ushort_t *)((char *)w + 1);
mlen--;
byteleft = B_FALSE;
}
if (mlen == 0)
continue;
if (IS_EVEN(w)) {
sum = ip_ocsum(w, mlen >> 1, sum);
w += mlen >> 1;
/*
* If we had an odd number of bytes,
* then the last byte goes in the high
* part of the sum, and we take the
* first byte to the low part of the sum
* the next time around the loop.
*/
if (IS_ODD(mlen)) {
#ifdef _LITTLE_ENDIAN
sum += *(uchar_t *)w;
#else
sum += *(uchar_t *)w << 8;
#endif
byteleft = B_TRUE;
}
} else {
ushort_t swsum;
#ifdef _LITTLE_ENDIAN
sum += *(uchar_t *)w;
#else
sum += *(uchar_t *)w << 8;
#endif
mlen--;
w = (ushort_t *)(1 + (uintptr_t)w);
/* Do a separate checksum and copy operation */
swsum = ip_ocsum(w, mlen >> 1, 0);
sum += ((swsum << 8) & 0xffff) | (swsum >> 8);
w += mlen >> 1;
/*
* If we had an even number of bytes,
* then the last byte goes in the low
* part of the sum. Otherwise we had an
* odd number of bytes and we take the first
* byte to the low part of the sum the
* next time around the loop.
*/
if (IS_ODD(mlen)) {
#ifdef _LITTLE_ENDIAN
sum += *(uchar_t *)w << 8;
#else
sum += *(uchar_t *)w;
#endif
} else {
byteleft = B_TRUE;
}
}
}
/*
* Add together high and low parts of sum and carry to get cksum.
* Have to be careful to not drop the last carry here.
*/
sum = (sum & 0xffff) + (sum >> 16);
sum = (sum & 0xffff) + (sum >> 16);
return (sum);
}
/* Return the IP checksum for the IP header at "iph". */
uint16_t
ip_csum_hdr(ipha_t *ipha)
{
uint16_t *uph;
uint32_t sum;
int opt_len;
opt_len = (ipha->ipha_version_and_hdr_length & 0xF) -
IP_SIMPLE_HDR_LENGTH_IN_WORDS;
uph = (uint16_t *)ipha;
sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] +
uph[5] + uph[6] + uph[7] + uph[8] + uph[9];
if (opt_len > 0) {
do {
sum += uph[10];
sum += uph[11];
uph += 2;
} while (--opt_len);
}
sum = (sum & 0xFFFF) + (sum >> 16);
sum = ~(sum + (sum >> 16)) & 0xFFFF;
if (sum == 0xffff)
sum = 0;
return ((uint16_t)sum);
}