|
|
@ -76,28 +76,20 @@ namespace llarp
|
|
|
|
return htons(sum);
|
|
|
|
return htons(sum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static std::map<
|
|
|
|
static void
|
|
|
|
byte_t,
|
|
|
|
checksumDstTCP(byte_t *pld, size_t psz, huint32_t oSrcIP, huint32_t oDstIP,
|
|
|
|
std::function< void(const ip_header *ohdr, byte_t *pld, size_t psz,
|
|
|
|
huint32_t nSrcIP, huint32_t nDstIP)
|
|
|
|
huint32_t oSrcIP, huint32_t oDstIP,
|
|
|
|
{
|
|
|
|
huint32_t nSrcIP, huint32_t nDstIP) > >
|
|
|
|
|
|
|
|
protoDstCheckSummer = {
|
|
|
|
|
|
|
|
// {RFC3022} says that IPv4 hdr isn't included in ICMP checksum calc
|
|
|
|
|
|
|
|
// and that we don't need to modify it
|
|
|
|
|
|
|
|
{// TCP
|
|
|
|
|
|
|
|
6,
|
|
|
|
|
|
|
|
[](const ip_header *ohdr, byte_t *pld, size_t psz,
|
|
|
|
|
|
|
|
huint32_t oSrcIP, huint32_t oDstIP, huint32_t nSrcIP,
|
|
|
|
|
|
|
|
huint32_t nDstIP) {
|
|
|
|
|
|
|
|
uint16_t *check = (uint16_t *)(pld + 16);
|
|
|
|
uint16_t *check = (uint16_t *)(pld + 16);
|
|
|
|
|
|
|
|
|
|
|
|
*check = deltachksum(*check, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
|
|
|
*check = deltachksum(*check, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
{// UDP
|
|
|
|
|
|
|
|
17,
|
|
|
|
static void
|
|
|
|
[](const ip_header *ohdr, byte_t *pld, size_t psz,
|
|
|
|
checksumDstUDP(const ip_header *ohdr, byte_t *pld, size_t psz,
|
|
|
|
huint32_t oSrcIP, huint32_t oDstIP, huint32_t nSrcIP,
|
|
|
|
huint32_t oSrcIP, huint32_t oDstIP, huint32_t nSrcIP,
|
|
|
|
huint32_t nDstIP) {
|
|
|
|
huint32_t nDstIP)
|
|
|
|
|
|
|
|
{
|
|
|
|
uint16_t *check = (uint16_t *)(pld + 6);
|
|
|
|
uint16_t *check = (uint16_t *)(pld + 6);
|
|
|
|
if(*check != 0xFFff)
|
|
|
|
if(*check != 0xFFff)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -117,8 +109,8 @@ namespace llarp
|
|
|
|
|
|
|
|
|
|
|
|
*check = 0; // zero checksum before calculation
|
|
|
|
*check = 0; // zero checksum before calculation
|
|
|
|
|
|
|
|
|
|
|
|
auto cs = ipchksum(
|
|
|
|
auto cs =
|
|
|
|
pld, psz,
|
|
|
|
ipchksum(pld, psz,
|
|
|
|
ipchksum_pseudoIPv4(nuint32_t{ohdr->saddr},
|
|
|
|
ipchksum_pseudoIPv4(nuint32_t{ohdr->saddr},
|
|
|
|
nuint32_t{ohdr->daddr}, 17, psz));
|
|
|
|
nuint32_t{ohdr->daddr}, 17, psz));
|
|
|
|
|
|
|
|
|
|
|
@ -135,9 +127,8 @@ namespace llarp
|
|
|
|
// put it in
|
|
|
|
// put it in
|
|
|
|
*check = new_cs;
|
|
|
|
*check = new_cs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
void
|
|
|
|
IPv4Packet::UpdatePacketOnDst(huint32_t nSrcIP, huint32_t nDstIP)
|
|
|
|
IPv4Packet::UpdatePacketOnDst(huint32_t nSrcIP, huint32_t nDstIP)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -151,11 +142,17 @@ namespace llarp
|
|
|
|
|
|
|
|
|
|
|
|
// L4 checksum
|
|
|
|
// L4 checksum
|
|
|
|
auto proto = hdr->protocol;
|
|
|
|
auto proto = hdr->protocol;
|
|
|
|
auto itr = protoDstCheckSummer.find(proto);
|
|
|
|
auto ihs = size_t(hdr->ihl * 4);
|
|
|
|
size_t ihs;
|
|
|
|
auto pld = buf + ihs;
|
|
|
|
if(itr != protoDstCheckSummer.end() && (ihs = size_t(hdr->ihl * 4)) <= sz)
|
|
|
|
auto psz = sz - ihs;
|
|
|
|
|
|
|
|
switch(proto)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
itr->second(hdr, buf + ihs, sz - ihs, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
|
|
|
case 6:
|
|
|
|
|
|
|
|
checksumDstTCP(pld, psz, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 17:
|
|
|
|
|
|
|
|
checksumDstUDP(hdr, pld, psz, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// write new IP addresses
|
|
|
|
// write new IP addresses
|
|
|
@ -163,32 +160,26 @@ namespace llarp
|
|
|
|
hdr->daddr = xhtonl(nDstIP).n;
|
|
|
|
hdr->daddr = xhtonl(nDstIP).n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static std::map<
|
|
|
|
static void
|
|
|
|
byte_t,
|
|
|
|
checksumSrcTCP(byte_t *pld, size_t psz, huint32_t oSrcIP, huint32_t oDstIP)
|
|
|
|
std::function< void(const ip_header *ohdr, byte_t *pld, size_t psz,
|
|
|
|
{
|
|
|
|
huint32_t oSrcIP, huint32_t oDstIP) > >
|
|
|
|
|
|
|
|
protoSrcCheckSummer = {
|
|
|
|
|
|
|
|
{// TCP
|
|
|
|
|
|
|
|
6,
|
|
|
|
|
|
|
|
[](const ip_header *ohdr, byte_t *pld, size_t psz,
|
|
|
|
|
|
|
|
huint32_t oSrcIP, huint32_t oDstIP) {
|
|
|
|
|
|
|
|
uint16_t *check = (uint16_t *)(pld + 16);
|
|
|
|
uint16_t *check = (uint16_t *)(pld + 16);
|
|
|
|
|
|
|
|
|
|
|
|
*check = deltachksum(*check, oSrcIP, oDstIP, huint32_t{0},
|
|
|
|
*check = deltachksum(*check, oSrcIP, oDstIP, huint32_t{0}, huint32_t{0});
|
|
|
|
huint32_t{0});
|
|
|
|
}
|
|
|
|
}},
|
|
|
|
|
|
|
|
{// UDP
|
|
|
|
static void
|
|
|
|
17,
|
|
|
|
checksumSrcUDP(const ip_header *ohdr, byte_t *pld, size_t psz,
|
|
|
|
[](const ip_header *ohdr, byte_t *pld, size_t psz,
|
|
|
|
huint32_t oSrcIP, huint32_t oDstIP)
|
|
|
|
huint32_t oSrcIP, huint32_t oDstIP) {
|
|
|
|
{
|
|
|
|
uint16_t *check = (uint16_t *)(pld + 6);
|
|
|
|
uint16_t *check = (uint16_t *)(pld + 6);
|
|
|
|
if(*check != 0xFFff)
|
|
|
|
if(*check != 0xFFff)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(*check == 0x0000)
|
|
|
|
if(*check == 0x0000)
|
|
|
|
return; // don't change zero
|
|
|
|
return; // don't change zero
|
|
|
|
|
|
|
|
|
|
|
|
*check = deltachksum(*check, oSrcIP, oDstIP, huint32_t{0},
|
|
|
|
*check =
|
|
|
|
huint32_t{0});
|
|
|
|
deltachksum(*check, oSrcIP, oDstIP, huint32_t{0}, huint32_t{0});
|
|
|
|
if(*check == 0x0000)
|
|
|
|
if(*check == 0x0000)
|
|
|
|
*check = 0xFFff;
|
|
|
|
*check = 0xFFff;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -201,13 +192,13 @@ namespace llarp
|
|
|
|
|
|
|
|
|
|
|
|
*check = 0; // zero checksum before calculation
|
|
|
|
*check = 0; // zero checksum before calculation
|
|
|
|
|
|
|
|
|
|
|
|
auto cs = ipchksum(
|
|
|
|
auto cs =
|
|
|
|
pld, psz,
|
|
|
|
ipchksum(pld, psz,
|
|
|
|
ipchksum_pseudoIPv4(nuint32_t{ohdr->saddr},
|
|
|
|
ipchksum_pseudoIPv4(nuint32_t{ohdr->saddr},
|
|
|
|
nuint32_t{ohdr->daddr}, 17, psz));
|
|
|
|
nuint32_t{ohdr->daddr}, 17, psz));
|
|
|
|
|
|
|
|
|
|
|
|
auto new_cs = deltachksum(cs, oSrcIP, oDstIP, huint32_t{0},
|
|
|
|
auto new_cs =
|
|
|
|
huint32_t{0});
|
|
|
|
deltachksum(cs, oSrcIP, oDstIP, huint32_t{0}, huint32_t{0});
|
|
|
|
|
|
|
|
|
|
|
|
if(cs != 0x0000 && cs != 0xFFff)
|
|
|
|
if(cs != 0x0000 && cs != 0xFFff)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -220,8 +211,8 @@ namespace llarp
|
|
|
|
// put it in
|
|
|
|
// put it in
|
|
|
|
*check = new_cs;
|
|
|
|
*check = new_cs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
void
|
|
|
|
IPv4Packet::UpdatePacketOnSrc()
|
|
|
|
IPv4Packet::UpdatePacketOnSrc()
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -232,11 +223,17 @@ namespace llarp
|
|
|
|
|
|
|
|
|
|
|
|
// L4
|
|
|
|
// L4
|
|
|
|
auto proto = hdr->protocol;
|
|
|
|
auto proto = hdr->protocol;
|
|
|
|
auto itr = protoSrcCheckSummer.find(proto);
|
|
|
|
auto ihs = size_t(hdr->ihl * 4);
|
|
|
|
size_t ihs;
|
|
|
|
auto pld = buf + ihs;
|
|
|
|
if(itr != protoSrcCheckSummer.end() && (ihs = size_t(hdr->ihl * 4)) <= sz)
|
|
|
|
auto psz = sz - ihs;
|
|
|
|
{
|
|
|
|
switch(proto)
|
|
|
|
itr->second(hdr, buf + ihs, sz - ihs, oSrcIP, oDstIP);
|
|
|
|
{
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
|
|
|
|
checksumSrcTCP(pld, psz, oSrcIP, oDstIP);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 17:
|
|
|
|
|
|
|
|
checksumSrcUDP(hdr, pld, psz, oSrcIP, oDstIP);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// IPv4
|
|
|
|
// IPv4
|
|
|
|