2015-09-14 07:49:52 -05:00
|
|
|
/*
|
|
|
|
Copyright (C) 2015 Srivats P.
|
|
|
|
|
|
|
|
This file is part of "Ostinato"
|
|
|
|
|
|
|
|
This is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "device.h"
|
|
|
|
|
|
|
|
#include "../common/emulproto.pb.h"
|
|
|
|
#include "devicemanager.h"
|
|
|
|
#include "packetbuffer.h"
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
#include <QHostAddress>
|
2015-09-14 07:49:52 -05:00
|
|
|
#include <qendian.h>
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
const int kBaseHex = 16;
|
2016-01-01 08:47:54 -06:00
|
|
|
const quint16 kEthTypeIp4 = 0x0800;
|
|
|
|
const quint16 kEthTypeIp6 = 0x86dd;
|
|
|
|
const int kIp6HdrLen = 40;
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
/*
|
2015-11-14 05:36:43 -06:00
|
|
|
* NOTE:
|
2020-01-26 07:31:37 -06:00
|
|
|
* 1. Device Key is (VLANS [without prio/cfi] + MAC)
|
|
|
|
* - is assumed to be unique for a device
|
2015-11-14 05:36:43 -06:00
|
|
|
* 2. Device clients/users (viz. DeviceManager) should take care when
|
2015-09-20 07:19:15 -05:00
|
|
|
* setting params that change the key, if the key is used elsewhere
|
|
|
|
* (e.g. in a hash)
|
|
|
|
*/
|
|
|
|
|
|
|
|
Device::Device(DeviceManager *deviceManager)
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
|
|
|
deviceManager_ = deviceManager;
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
for (int i = 0; i < kMaxVlan; i++)
|
|
|
|
vlan_[i] = 0;
|
|
|
|
numVlanTags_ = 0;
|
|
|
|
mac_ = 0;
|
2015-12-31 08:47:56 -06:00
|
|
|
|
2016-01-01 00:29:31 -06:00
|
|
|
hasIp4_ = false;
|
|
|
|
hasIp6_ = false;
|
2015-12-31 08:47:56 -06:00
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
clearKey();
|
2015-09-14 07:49:52 -05:00
|
|
|
}
|
|
|
|
|
2015-12-20 08:03:02 -06:00
|
|
|
void Device::setVlan(int index, quint16 vlan, quint16 tpid)
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
2015-09-20 07:19:15 -05:00
|
|
|
int ofs;
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
if ((index < 0) || (index >= kMaxVlan)) {
|
|
|
|
qWarning("%s: vlan index %d out of range (0 - %d)", __FUNCTION__,
|
|
|
|
index, kMaxVlan - 1);
|
|
|
|
return;
|
|
|
|
}
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2015-12-20 08:03:02 -06:00
|
|
|
vlan_[index] = (tpid << 16) | vlan;
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
ofs = index * sizeof(quint16);
|
2020-01-26 07:31:37 -06:00
|
|
|
key_[ofs] = (vlan >> 8) & 0x0f; // Vlan prio/cfi should not be part of key
|
2015-09-20 07:19:15 -05:00
|
|
|
key_[ofs+1] = vlan & 0xff;
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
if (index >= numVlanTags_)
|
|
|
|
numVlanTags_ = index + 1;
|
|
|
|
}
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2015-11-10 08:10:32 -06:00
|
|
|
quint64 Device::mac()
|
|
|
|
{
|
|
|
|
return mac_;
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
void Device::setMac(quint64 mac)
|
|
|
|
{
|
|
|
|
int ofs = kMaxVlan * sizeof(quint16);
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2016-01-01 08:47:54 -06:00
|
|
|
mac_ = mac & ~(0xffffULL << 48);
|
2020-03-23 10:07:47 -05:00
|
|
|
qToBigEndian(mac_, (uchar*)key_.data()+ofs);
|
2015-09-14 07:49:52 -05:00
|
|
|
}
|
|
|
|
|
2015-11-04 07:20:08 -06:00
|
|
|
void Device::setIp4(quint32 address, int prefixLength, quint32 gateway)
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
2015-09-20 07:19:15 -05:00
|
|
|
ip4_ = address;
|
|
|
|
ip4PrefixLength_ = prefixLength;
|
2015-11-04 07:20:08 -06:00
|
|
|
ip4Gateway_ = gateway;
|
2016-01-01 00:29:31 -06:00
|
|
|
hasIp4_ = true;
|
2016-09-17 04:04:44 -05:00
|
|
|
|
|
|
|
// Precalculate our mask 'n subnet to avoid doing so at pkt rx/tx time
|
|
|
|
ip4Mask_ = ~0 << (32 - ip4PrefixLength_);
|
|
|
|
ip4Subnet_ = ip4_ & ip4Mask_;
|
2015-09-14 07:49:52 -05:00
|
|
|
}
|
|
|
|
|
2015-12-31 08:47:56 -06:00
|
|
|
void Device::setIp6(UInt128 address, int prefixLength, UInt128 gateway)
|
|
|
|
{
|
|
|
|
ip6_ = address;
|
|
|
|
ip6PrefixLength_ = prefixLength;
|
|
|
|
ip6Gateway_ = gateway;
|
2016-01-01 00:29:31 -06:00
|
|
|
hasIp6_ = true;
|
2016-09-17 04:04:44 -05:00
|
|
|
|
|
|
|
// Precalculate our mask 'n subnet to avoid doing so at pkt rx/tx time
|
|
|
|
ip6Mask_ = ~UInt128(0, 0) << (128 - ip6PrefixLength_);
|
|
|
|
ip6Subnet_ = ip6_ & ip6Mask_;
|
2015-12-31 08:47:56 -06:00
|
|
|
}
|
|
|
|
|
2015-11-11 01:35:15 -06:00
|
|
|
void Device::getConfig(OstEmul::Device *deviceConfig)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < numVlanTags_; i++)
|
|
|
|
deviceConfig->add_vlan(vlan_[i]);
|
|
|
|
deviceConfig->set_mac(mac_);
|
2015-12-31 08:47:56 -06:00
|
|
|
|
2016-01-01 00:29:31 -06:00
|
|
|
if (hasIp4_) {
|
|
|
|
deviceConfig->set_ip4(ip4_);
|
|
|
|
deviceConfig->set_ip4_prefix_length(ip4PrefixLength_);
|
|
|
|
deviceConfig->set_ip4_default_gateway(ip4Gateway_);
|
|
|
|
}
|
2015-12-31 08:47:56 -06:00
|
|
|
|
2016-01-01 00:29:31 -06:00
|
|
|
if (hasIp6_) {
|
|
|
|
deviceConfig->mutable_ip6()->set_hi(ip6_.hi64());
|
|
|
|
deviceConfig->mutable_ip6()->set_lo(ip6_.lo64());
|
|
|
|
deviceConfig->set_ip6_prefix_length(ip6PrefixLength_);
|
|
|
|
deviceConfig->mutable_ip6_default_gateway()->set_hi(ip6Gateway_.hi64());
|
|
|
|
deviceConfig->mutable_ip6_default_gateway()->set_lo(ip6Gateway_.lo64());
|
|
|
|
}
|
2015-11-11 01:35:15 -06:00
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
QString Device::config()
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
2016-01-01 00:29:31 -06:00
|
|
|
QString config;
|
|
|
|
|
|
|
|
for (int i = 0; i < numVlanTags_; i++) {
|
|
|
|
config.append(i == 0 ? "vlans=" : "|");
|
|
|
|
config.append(
|
|
|
|
(vlan_[i] >> 16) != kVlanTpid ?
|
2015-12-20 08:03:02 -06:00
|
|
|
QString("0x%1-%2")
|
2016-01-01 00:29:31 -06:00
|
|
|
.arg(vlan_[i] >> 16, 4, kBaseHex, QChar('0'))
|
|
|
|
.arg(vlan_[i] & 0xFFFF) :
|
2015-12-20 08:03:02 -06:00
|
|
|
QString("%1")
|
2016-01-01 00:29:31 -06:00
|
|
|
.arg(vlan_[i] & 0xFFFF));
|
|
|
|
}
|
|
|
|
|
|
|
|
config.append(QString(" mac=%1")
|
|
|
|
.arg(mac_, 12, kBaseHex, QChar('0')));
|
|
|
|
if (hasIp4_)
|
|
|
|
config.append(QString(" ip4=%1/%2")
|
|
|
|
.arg(QHostAddress(ip4_).toString())
|
|
|
|
.arg(ip4PrefixLength_));
|
|
|
|
if (hasIp6_)
|
|
|
|
config.append(QString(" ip6=%1/%2")
|
|
|
|
.arg(QHostAddress(ip6_.toArray()).toString())
|
|
|
|
.arg(ip6PrefixLength_));
|
|
|
|
|
|
|
|
return config;
|
2015-09-20 07:19:15 -05:00
|
|
|
}
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
DeviceKey Device::key()
|
|
|
|
{
|
|
|
|
return key_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::clearKey()
|
2015-11-14 05:36:43 -06:00
|
|
|
{
|
2015-09-20 07:19:15 -05:00
|
|
|
key_.fill(0, kMaxVlan * sizeof(quint16) + sizeof(quint64));
|
|
|
|
}
|
|
|
|
|
2018-07-19 08:24:01 -05:00
|
|
|
/*
|
2015-11-04 07:20:08 -06:00
|
|
|
void Device::receivePacket(PacketBuffer *pktBuf)
|
|
|
|
{
|
2018-07-19 08:24:01 -05:00
|
|
|
// XXX: Pure Virtual: Subclass should implement
|
|
|
|
// We expect pktBuf to point to EthType on entry
|
2015-11-04 07:20:08 -06:00
|
|
|
}
|
2018-07-19 08:24:01 -05:00
|
|
|
*/
|
2015-11-04 07:20:08 -06:00
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
void Device::transmitPacket(PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
deviceManager_->transmitPacket(pktBuf);
|
|
|
|
}
|
|
|
|
|
2016-01-28 08:31:19 -06:00
|
|
|
void Device::resolveGateway()
|
|
|
|
{
|
2020-06-08 08:12:38 -05:00
|
|
|
if (hasIp4_ && ip4Gateway_ && !isResolved(ip4Gateway_))
|
2016-01-28 08:31:19 -06:00
|
|
|
sendArpRequest(ip4Gateway_);
|
|
|
|
|
2020-06-08 08:12:38 -05:00
|
|
|
if (hasIp6_ && (ip6Gateway_ != 0) && !isResolved(ip6Gateway_))
|
2016-01-28 08:31:19 -06:00
|
|
|
sendNeighborSolicit(ip6Gateway_);
|
|
|
|
}
|
|
|
|
|
2015-11-04 07:20:08 -06:00
|
|
|
// Resolve the Neighbor IP address for this to-be-transmitted pktBuf
|
2015-09-20 07:19:15 -05:00
|
|
|
// We expect pktBuf to point to EthType on entry
|
2015-11-04 07:20:08 -06:00
|
|
|
void Device::resolveNeighbor(PacketBuffer *pktBuf)
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
2015-09-20 07:19:15 -05:00
|
|
|
quint16 ethType = qFromBigEndian<quint16>(pktBuf->data());
|
|
|
|
pktBuf->pull(2);
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
qDebug("%s: ethType 0x%x", __PRETTY_FUNCTION__, ethType);
|
2015-09-14 07:49:52 -05:00
|
|
|
|
|
|
|
switch(ethType)
|
|
|
|
{
|
2018-05-22 11:18:21 -05:00
|
|
|
case kEthTypeIp4: // IPv4
|
2016-01-01 00:29:31 -06:00
|
|
|
if (hasIp4_)
|
|
|
|
sendArpRequest(pktBuf);
|
2015-09-14 07:49:52 -05:00
|
|
|
break;
|
|
|
|
|
2018-05-22 11:18:21 -05:00
|
|
|
case kEthTypeIp6: // IPv6
|
2016-01-01 00:29:31 -06:00
|
|
|
if (hasIp6_)
|
|
|
|
sendNeighborSolicit(pktBuf);
|
2015-12-31 08:47:56 -06:00
|
|
|
break;
|
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-10-03 02:48:44 -05:00
|
|
|
// FIXME: temporary hack till DeviceManager clones pbufs
|
|
|
|
pktBuf->push(2);
|
2015-09-14 07:49:52 -05:00
|
|
|
}
|
|
|
|
|
2018-07-19 08:24:01 -05:00
|
|
|
/*
|
|
|
|
void Device::clearNeighbors(Device::NeighborSet set)
|
2015-11-04 07:20:08 -06:00
|
|
|
{
|
2018-07-19 08:24:01 -05:00
|
|
|
// XXX: Pure virtual: Subclass should implement
|
|
|
|
}
|
2016-01-06 06:40:28 -06:00
|
|
|
|
2018-07-19 08:24:01 -05:00
|
|
|
// Append this device's neighbors to the list
|
|
|
|
void Device::getNeighbors(OstEmul::DeviceNeighborList* neighbors)
|
|
|
|
{
|
|
|
|
// XXX: Subclass should implement
|
2015-11-04 07:20:08 -06:00
|
|
|
}
|
2018-07-19 08:24:01 -05:00
|
|
|
*/
|
2015-11-04 07:20:08 -06:00
|
|
|
|
2016-01-01 00:29:31 -06:00
|
|
|
// Are we the source of the given packet?
|
2015-11-10 08:10:32 -06:00
|
|
|
// We expect pktBuf to point to EthType on entry
|
|
|
|
bool Device::isOrigin(const PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
const uchar *pktData = pktBuf->data();
|
|
|
|
quint16 ethType = qFromBigEndian<quint16>(pktData);
|
|
|
|
|
|
|
|
qDebug("%s: ethType 0x%x", __PRETTY_FUNCTION__, ethType);
|
|
|
|
pktData += 2;
|
|
|
|
|
2016-01-01 08:47:54 -06:00
|
|
|
// We know only about IP packets - adjust for ethType length (2 bytes)
|
|
|
|
// when checking that we have a complete IP header
|
2018-05-22 11:18:21 -05:00
|
|
|
if ((ethType == kEthTypeIp4) && hasIp4_) { // IPv4
|
2015-11-10 08:10:32 -06:00
|
|
|
int ipHdrLen = (pktData[0] & 0x0F) << 2;
|
|
|
|
quint32 srcIp;
|
|
|
|
|
2016-01-01 08:47:54 -06:00
|
|
|
if (pktBuf->length() < (ipHdrLen+2)) {
|
2015-11-10 08:10:32 -06:00
|
|
|
qDebug("incomplete IPv4 header: expected %d, actual %d",
|
|
|
|
ipHdrLen, pktBuf->length());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
srcIp = qFromBigEndian<quint32>(pktData + ipHdrLen - 8);
|
|
|
|
qDebug("%s: pktSrcIp/selfIp = 0x%x/0x%x", __FUNCTION__, srcIp, ip4_);
|
|
|
|
return (srcIp == ip4_);
|
|
|
|
}
|
2016-01-01 08:47:54 -06:00
|
|
|
else if ((ethType == kEthTypeIp6) && hasIp6_) { // IPv6
|
|
|
|
UInt128 srcIp;
|
|
|
|
if (pktBuf->length() < (kIp6HdrLen+2)) {
|
|
|
|
qDebug("incomplete IPv6 header: expected %d, actual %d",
|
|
|
|
kIp6HdrLen, pktBuf->length()-2);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
srcIp = qFromBigEndian<UInt128>(pktData + 8);
|
|
|
|
qDebug("%s: pktSrcIp6/selfIp6 = %llx-%llx/%llx-%llx", __FUNCTION__,
|
|
|
|
srcIp.hi64(), srcIp.lo64(), ip6_.hi64(), ip6_.lo64());
|
|
|
|
return (srcIp == ip6_);
|
|
|
|
}
|
2015-11-10 08:10:32 -06:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-01 00:29:31 -06:00
|
|
|
// Return the mac address corresponding to the dstIp of the given packet
|
2015-11-10 08:10:32 -06:00
|
|
|
// We expect pktBuf to point to EthType on entry
|
|
|
|
quint64 Device::neighborMac(const PacketBuffer *pktBuf)
|
|
|
|
{
|
2018-09-02 09:15:08 -05:00
|
|
|
quint64 mac = 0;
|
2015-11-10 08:10:32 -06:00
|
|
|
const uchar *pktData = pktBuf->data();
|
|
|
|
quint16 ethType = qFromBigEndian<quint16>(pktData);
|
|
|
|
|
|
|
|
qDebug("%s: ethType 0x%x", __PRETTY_FUNCTION__, ethType);
|
|
|
|
pktData += 2;
|
|
|
|
|
|
|
|
// We know only about IP packets
|
2018-05-22 11:18:21 -05:00
|
|
|
if ((ethType == kEthTypeIp4) && hasIp4_) { // IPv4
|
2015-11-10 08:10:32 -06:00
|
|
|
int ipHdrLen = (pktData[0] & 0x0F) << 2;
|
2016-09-17 04:04:44 -05:00
|
|
|
quint32 dstIp, tgtIp;
|
2015-11-10 08:10:32 -06:00
|
|
|
|
|
|
|
if (pktBuf->length() < ipHdrLen) {
|
|
|
|
qDebug("incomplete IPv4 header: expected %d, actual %d",
|
|
|
|
ipHdrLen, pktBuf->length());
|
2018-09-02 09:15:08 -05:00
|
|
|
return mac;
|
2015-11-10 08:10:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
dstIp = qFromBigEndian<quint32>(pktData + ipHdrLen - 4);
|
2016-08-15 10:50:19 -05:00
|
|
|
if ((dstIp & 0xF0000000) == 0xE0000000) { // Mcast IP?
|
2018-09-02 09:15:08 -05:00
|
|
|
mac = (quint64(0x01005e) << 24) | (dstIp & 0x7FFFFF);
|
|
|
|
qDebug("mcast dst %08x, mac: %012llx", dstIp, mac);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tgtIp = ((dstIp & ip4Mask_) == ip4Subnet_) ? dstIp : ip4Gateway_;
|
|
|
|
mac = arpLookup(tgtIp);
|
|
|
|
qDebug("tgtIp: %08x, mac: %012llx", tgtIp, mac);
|
2016-08-15 10:50:19 -05:00
|
|
|
}
|
2015-11-10 08:10:32 -06:00
|
|
|
}
|
2016-01-13 09:45:47 -06:00
|
|
|
else if ((ethType == kEthTypeIp6) && hasIp6_) { // IPv6
|
2016-09-17 04:04:44 -05:00
|
|
|
UInt128 dstIp, tgtIp;
|
2016-01-13 09:45:47 -06:00
|
|
|
|
|
|
|
if (pktBuf->length() < (kIp6HdrLen+2)) {
|
|
|
|
qDebug("incomplete IPv6 header: expected %d, actual %d",
|
|
|
|
kIp6HdrLen, pktBuf->length()-2);
|
2018-09-02 09:15:08 -05:00
|
|
|
return mac;
|
2016-01-13 09:45:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
dstIp = qFromBigEndian<UInt128>(pktData + 24);
|
2016-08-15 10:50:19 -05:00
|
|
|
if (dstIp.toArray()[0] == 0xFF) { // Mcast IP?
|
2018-09-02 09:15:08 -05:00
|
|
|
mac = (quint64(0x3333) << 32) | (dstIp.lo64() & 0xFFFFFFFF);
|
|
|
|
qDebug("mcast dst %s, mac: %012llx",
|
|
|
|
qPrintable(QHostAddress(dstIp.toArray()).toString()),
|
|
|
|
mac);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tgtIp = ((dstIp & ip6Mask_) == ip6Subnet_) ? dstIp : ip6Gateway_;
|
|
|
|
mac = ndpLookup(tgtIp);
|
|
|
|
qDebug("tgtIp %s, mac: %012llx",
|
|
|
|
qPrintable(QHostAddress(dstIp.toArray()).toString()),
|
|
|
|
mac);
|
2016-08-15 10:50:19 -05:00
|
|
|
}
|
2016-01-13 09:45:47 -06:00
|
|
|
}
|
2015-11-10 08:10:32 -06:00
|
|
|
|
2018-09-02 09:15:08 -05:00
|
|
|
return mac;
|
2015-11-10 08:10:32 -06:00
|
|
|
}
|
|
|
|
|
2016-01-01 08:47:54 -06:00
|
|
|
/*
|
2018-07-19 08:24:01 -05:00
|
|
|
quint64 Device::arpLookup(quint32 ip)
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
2018-07-19 08:24:01 -05:00
|
|
|
// XXX: Pure virtual: Subclass should implement
|
|
|
|
}
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2018-07-19 08:24:01 -05:00
|
|
|
quint64 Device::ndpLookup(UInt128 ip)
|
|
|
|
{
|
|
|
|
// XXX: Pure virtual: Subclass should implement
|
2015-09-14 07:49:52 -05:00
|
|
|
}
|
2018-07-19 08:24:01 -05:00
|
|
|
*/
|
2015-11-04 07:20:08 -06:00
|
|
|
|
|
|
|
// Send ARP request for the IPv4 packet in pktBuf
|
|
|
|
// pktBuf points to start of IP header
|
|
|
|
void Device::sendArpRequest(PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
uchar *pktData = pktBuf->data();
|
2016-01-28 08:31:19 -06:00
|
|
|
int ipHdrLen = (pktData[0] & 0x0F) << 2;
|
2016-09-17 04:04:44 -05:00
|
|
|
quint32 dstIp, tgtIp;
|
2015-11-04 07:20:08 -06:00
|
|
|
|
|
|
|
if (pktBuf->length() < ipHdrLen) {
|
|
|
|
qDebug("incomplete IPv4 header: expected %d, actual %d",
|
|
|
|
ipHdrLen, pktBuf->length());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dstIp = qFromBigEndian<quint32>(pktData + ipHdrLen - 4);
|
|
|
|
|
2016-09-17 04:04:44 -05:00
|
|
|
tgtIp = ((dstIp & ip4Mask_) == ip4Subnet_) ? dstIp : ip4Gateway_;
|
2015-11-04 07:20:08 -06:00
|
|
|
|
2019-03-11 07:55:07 -05:00
|
|
|
if (!isResolved(tgtIp))
|
|
|
|
sendArpRequest(tgtIp);
|
2016-01-28 08:31:19 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-31 08:47:56 -06:00
|
|
|
// Send NS for the IPv6 packet in pktBuf
|
2016-01-01 08:47:54 -06:00
|
|
|
// caller is responsible to check that pktBuf originates from this device
|
|
|
|
// pktBuf should point to start of IP header
|
2015-12-31 08:47:56 -06:00
|
|
|
void Device::sendNeighborSolicit(PacketBuffer *pktBuf)
|
|
|
|
{
|
2016-01-01 08:47:54 -06:00
|
|
|
uchar *pktData = pktBuf->data();
|
2016-09-17 04:04:44 -05:00
|
|
|
UInt128 dstIp, tgtIp;
|
2016-01-01 08:47:54 -06:00
|
|
|
|
|
|
|
if (pktBuf->length() < kIp6HdrLen) {
|
|
|
|
qDebug("incomplete IPv6 header: expected %d, actual %d",
|
|
|
|
kIp6HdrLen, pktBuf->length());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dstIp = qFromBigEndian<UInt128>(pktData + 24);
|
|
|
|
|
2016-09-17 04:04:44 -05:00
|
|
|
tgtIp = ((dstIp & ip6Mask_) == ip6Subnet_) ? dstIp : ip6Gateway_;
|
2016-01-01 08:47:54 -06:00
|
|
|
|
2019-03-11 07:55:07 -05:00
|
|
|
if (!isResolved(tgtIp))
|
|
|
|
sendNeighborSolicit(tgtIp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Device::isResolved(quint32 ip)
|
|
|
|
{
|
|
|
|
return arpLookup(ip) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Device::isResolved(UInt128 ip)
|
|
|
|
{
|
|
|
|
return ndpLookup(ip) > 0;
|
2016-01-28 08:31:19 -06:00
|
|
|
}
|
|
|
|
|
2018-07-19 08:24:01 -05:00
|
|
|
//
|
|
|
|
// Protected Methods
|
|
|
|
//
|
|
|
|
/*
|
|
|
|
void Device::sendArpRequest(quint32 tgtIp)
|
2016-01-28 08:31:19 -06:00
|
|
|
{
|
2018-07-19 08:24:01 -05:00
|
|
|
// XXX: Pure virtual: Subclass should implement
|
2015-12-31 08:47:56 -06:00
|
|
|
}
|
|
|
|
|
2018-07-19 08:24:01 -05:00
|
|
|
void Device::sendNeighborSolicit(UInt128 tgtIp)
|
2016-01-08 08:31:42 -06:00
|
|
|
{
|
2018-07-19 08:24:01 -05:00
|
|
|
// XXX: Pure virtual: Subclass should implement
|
2016-01-08 08:31:42 -06:00
|
|
|
}
|
2018-07-19 08:24:01 -05:00
|
|
|
*/
|
2016-01-08 08:31:42 -06:00
|
|
|
|
2015-11-29 09:48:31 -06:00
|
|
|
bool operator<(const DeviceKey &a1, const DeviceKey &a2)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (i < a1.size()) {
|
|
|
|
if (uchar(a1.at(i)) < uchar(a2.at(i)))
|
|
|
|
return true;
|
|
|
|
if (uchar(a1.at(i)) > uchar(a2.at(i)))
|
|
|
|
return false;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|