VirtualBox

Ignore:
Timestamp:
Apr 7, 2010 6:34:16 AM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
59731
Message:

iprt: ipv6 header and pseudo checksumming; tcp flags; tcp/udp checksumming additions.

Location:
trunk/src/VBox/Runtime/common/checksum
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/checksum/ipv4.cpp

    r25645 r28024  
    9999 * @param   cbPktMax    The max IP packet size, IP header and payload. This doesn't have
    100100 *                      to be mapped following pIpHdr.
    101  */
    102 RTDECL(bool) RTNetIPv4IsHdrValid(PCRTNETIPV4 pIpHdr, size_t cbHdrMax, size_t cbPktMax)
     101 * @param   fChecksum   Whether to validate the checksum (GSO).
     102 */
     103RTDECL(bool) RTNetIPv4IsHdrValid(PCRTNETIPV4 pIpHdr, size_t cbHdrMax, size_t cbPktMax, bool fChecksum)
    103104{
    104105    /*
     
    121122
    122123    /*
    123      * The header checksum.
     124     * The header checksum if requested.
    124125     */
    125     uint16_t u16Sum = RTNetIPv4HdrChecksum(pIpHdr);
    126     if (RT_UNLIKELY(pIpHdr->ip_sum != u16Sum))
    127         return false;
     126    if (fChecksum)
     127    {
     128        uint16_t u16Sum = RTNetIPv4HdrChecksum(pIpHdr);
     129        if (RT_UNLIKELY(pIpHdr->ip_sum != u16Sum))
     130            return false;
     131    }
    128132    return true;
    129133}
     
    386390
    387391/**
     392 * Calculates the checksum for the UDP header given the UDP header w/ payload
     393 * and the checksum of the pseudo header.
     394 *
     395 * @returns The checksum (network endian).
     396 * @param   u32Sum          The checksum of the pseudo header. See
     397 *                          RTNetIPv4PseudoChecksum and RTNetIPv6PseudoChecksum.
     398 * @param   pUdpHdr         Pointer to the UDP header and the payload, in
     399 *                          network endian (big).  We use the uh_ulen field to
     400 *                          figure out how much to checksum.
     401 */
     402RTDECL(uint16_t) RTNetUDPChecksum(uint32_t u32Sum, PCRTNETUDP pUdpHdr)
     403{
     404    u32Sum = rtNetIPv4AddUDPChecksum(pUdpHdr, u32Sum);
     405    bool fOdd = false;
     406    u32Sum = rtNetIPv4AddDataChecksum(pUdpHdr + 1, RT_BE2H_U16(pUdpHdr->uh_ulen) - sizeof(*pUdpHdr), u32Sum, &fOdd);
     407    return rtNetIPv4FinalizeChecksum(u32Sum);
     408}
     409RT_EXPORT_SYMBOL(RTNetUDPChecksum);
     410
     411
     412/**
    388413 * Calculates the checksum for the UDP header given the IP header,
    389414 * UDP header and payload.
     
    458483 *                          and that cbPktMax - sizeof(RTNETUDP) is mapped here.
    459484 * @param   cbPktMax        The max UDP packet size, UDP header and payload (data).
    460  */
    461 RTDECL(bool) RTNetIPv4IsUDPValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, void const *pvData, size_t cbPktMax)
     485 * @param   fChecksum       Whether to validate the checksum (GSO).
     486 */
     487RTDECL(bool) RTNetIPv4IsUDPValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, void const *pvData, size_t cbPktMax, bool fChecksum)
    462488{
    463489    if (RT_UNLIKELY(!rtNetIPv4IsUDPSizeValid(pIpHdr, pUdpHdr, cbPktMax)))
    464490        return false;
    465     if (pUdpHdr->uh_sum)
     491    if (fChecksum && pUdpHdr->uh_sum)
    466492    {
    467493        uint16_t u16Sum = RTNetIPv4UDPChecksum(pIpHdr, pUdpHdr, pvData);
     
    481507 * @param   pIpHdr          Pointer to the IPv4 header, in network endian (big).
    482508 * @param   pTcpHdr         Pointer to the TCP header, in network endian (big).
    483  * @param   pvData          Pointer to the TCP payload. The size is taken from the
    484  *                          TCP header and the caller is supposed to have validated
    485  *                          this before calling.
    486  *                          If NULL then we assume the data follows immediately after
    487  *                          the TCP header.
     509 * @param   pvData          Pointer to the TCP payload. The size is derived from
     510 *                          the two headers and the caller is supposed to have
     511 *                          validated this before calling.  If NULL, we assume
     512 *                          the data follows immediately after the TCP header.
    488513 */
    489514RTDECL(uint16_t) RTNetIPv4TCPChecksum(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, void const *pvData)
     
    498523}
    499524RT_EXPORT_SYMBOL(RTNetIPv4TCPChecksum);
     525
     526
     527/**
     528 * Calculates the checksum for the TCP header given the TCP header, payload and
     529 * the checksum of the pseudo header.
     530 *
     531 * This is not specific to IPv4.
     532 *
     533 * @returns The checksum (network endian).
     534 * @param   u32Sum          The checksum of the pseudo header. See
     535 *                          RTNetIPv4PseudoChecksum and RTNetIPv6PseudoChecksum.
     536 * @param   pTcpHdr         Pointer to the TCP header, in network endian (big).
     537 * @param   pvData          Pointer to the TCP payload.
     538 * @param   cbData          The size of the TCP payload.
     539 */
     540RTDECL(uint16_t) RTNetTCPChecksum(uint32_t u32Sum, PCRTNETTCP pTcpHdr, void const *pvData, size_t cbData)
     541{
     542    u32Sum = rtNetIPv4AddTCPChecksum(pTcpHdr, u32Sum);
     543    bool fOdd = false;
     544    u32Sum = rtNetIPv4AddDataChecksum(pvData, cbData, u32Sum, &fOdd);
     545    return rtNetIPv4FinalizeChecksum(u32Sum);
     546}
     547RT_EXPORT_SYMBOL(RTNetTCPChecksum);
    500548
    501549
     
    559607 *                          the TCP header.
    560608 * @param   cbPktMax        The max TCP packet size, TCP header and payload (data).
    561  */
    562 RTDECL(bool) RTNetIPv4IsTCPValid(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, size_t cbHdrMax, void const *pvData, size_t cbPktMax)
     609 * @param   fChecksum       Whether to validate the checksum (GSO).
     610 */
     611RTDECL(bool) RTNetIPv4IsTCPValid(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, size_t cbHdrMax, void const *pvData, size_t cbPktMax,
     612                                 bool fChecksum)
    563613{
    564614    if (RT_UNLIKELY(!rtNetIPv4IsTCPSizeValid(pIpHdr, pTcpHdr, cbHdrMax, cbPktMax)))
    565615        return false;
    566     uint16_t u16Sum = RTNetIPv4TCPChecksum(pIpHdr, pTcpHdr, pvData);
    567     if (RT_UNLIKELY(pTcpHdr->th_sum != u16Sum))
    568         return false;
     616    if (fChecksum)
     617    {
     618        uint16_t u16Sum = RTNetIPv4TCPChecksum(pIpHdr, pTcpHdr, pvData);
     619        if (RT_UNLIKELY(pTcpHdr->th_sum != u16Sum))
     620            return false;
     621    }
    569622    return true;
    570623}
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette