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;
|
2015-11-04 07:20:08 -06:00
|
|
|
const quint64 kBcastMac = 0xffffffffffffULL;
|
2016-01-01 08:47:54 -06:00
|
|
|
const quint16 kEthTypeIp4 = 0x0800;
|
|
|
|
const quint16 kEthTypeIp6 = 0x86dd;
|
|
|
|
const int kIp6HdrLen = 40;
|
|
|
|
const quint8 kIpProtoIcmp6 = 58;
|
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:
|
2015-09-20 07:19:15 -05:00
|
|
|
* 1. Device Key is (VLANS + 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)
|
|
|
|
*/
|
|
|
|
|
2016-01-01 08:47:54 -06:00
|
|
|
inline quint32 sumUInt128(UInt128 value)
|
|
|
|
{
|
|
|
|
quint8 *arr = value.toArray();
|
|
|
|
quint32 sum = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < 16; i += 2)
|
|
|
|
sum += qToBigEndian(*((quint16*)(arr + i)));
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2016-01-08 08:31:42 -06:00
|
|
|
inline bool isIp6Mcast(UInt128 ip)
|
|
|
|
{
|
|
|
|
return (ip.hi64() >> 56) == 0xff;
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
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);
|
|
|
|
key_[ofs] = vlan >> 8;
|
|
|
|
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);
|
|
|
|
memcpy(key_.data() + ofs, (char*)&mac_, sizeof(mac_));
|
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;
|
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;
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
int Device::encapSize()
|
|
|
|
{
|
|
|
|
// ethernet header + vlans
|
2015-11-14 05:36:43 -06:00
|
|
|
int size = 14 + 4*numVlanTags_;
|
2015-09-14 07:49:52 -05:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::encap(PacketBuffer *pktBuf, quint64 dstMac, quint16 type)
|
|
|
|
{
|
2015-09-20 07:19:15 -05:00
|
|
|
int ofs;
|
2015-11-14 05:36:43 -06:00
|
|
|
quint64 srcMac = mac_;
|
2015-09-14 07:49:52 -05:00
|
|
|
uchar *p = pktBuf->push(encapSize());
|
|
|
|
|
|
|
|
if (!p) {
|
|
|
|
qWarning("%s: failed to push %d bytes [0x%p, 0x%p]", __FUNCTION__,
|
|
|
|
encapSize(), pktBuf->head(), pktBuf->data());
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
*(quint32*)(p ) = qToBigEndian(quint32(dstMac >> 16));
|
|
|
|
*(quint16*)(p + 4) = qToBigEndian(quint16(dstMac & 0xffff));
|
|
|
|
*(quint32*)(p + 6) = qToBigEndian(quint32(srcMac >> 16));
|
|
|
|
*(quint16*)(p + 10) = qToBigEndian(quint16(srcMac & 0xffff));
|
2015-09-20 07:19:15 -05:00
|
|
|
ofs = 12;
|
|
|
|
for (int i = 0; i < numVlanTags_; i++) {
|
2015-12-20 08:03:02 -06:00
|
|
|
*(quint32*)(p + ofs) = qToBigEndian(vlan_[i]);
|
2015-09-20 07:19:15 -05:00
|
|
|
ofs += 4;
|
|
|
|
}
|
|
|
|
*(quint16*)(p + ofs) = qToBigEndian(type);
|
|
|
|
ofs += 2;
|
|
|
|
|
|
|
|
Q_ASSERT(ofs == encapSize());
|
2015-09-14 07:49:52 -05:00
|
|
|
|
|
|
|
_exit:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-04 07:20:08 -06:00
|
|
|
// We expect pktBuf to point to EthType on entry
|
|
|
|
void Device::receivePacket(PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
quint16 ethType = qFromBigEndian<quint16>(pktBuf->data());
|
|
|
|
pktBuf->pull(2);
|
|
|
|
|
|
|
|
qDebug("%s: ethType 0x%x", __PRETTY_FUNCTION__, ethType);
|
|
|
|
|
|
|
|
switch(ethType)
|
|
|
|
{
|
|
|
|
case 0x0806: // ARP
|
2016-01-01 00:29:31 -06:00
|
|
|
if (hasIp4_)
|
|
|
|
receiveArp(pktBuf);
|
2015-11-04 07:20:08 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x0800: // IPv4
|
2016-01-01 00:29:31 -06:00
|
|
|
if (hasIp4_)
|
|
|
|
receiveIp4(pktBuf);
|
2015-12-23 09:48:25 -06:00
|
|
|
break;
|
|
|
|
|
2015-11-04 07:20:08 -06:00
|
|
|
case 0x86dd: // IPv6
|
2016-01-06 06:40:28 -06:00
|
|
|
if (hasIp6_)
|
|
|
|
receiveIp6(pktBuf);
|
|
|
|
break;
|
|
|
|
|
2015-11-04 07:20:08 -06:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FIXME: temporary hack till DeviceManager clones pbufs
|
|
|
|
pktBuf->push(2);
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
if (hasIp4_)
|
|
|
|
sendArpRequest(ip4Gateway_);
|
|
|
|
|
|
|
|
if (hasIp6_)
|
|
|
|
sendNeighborSolicit(ip6Gateway_);
|
|
|
|
}
|
|
|
|
|
2016-03-17 09:42:13 -05:00
|
|
|
void Device::clearNeighbors(Device::NeighborSet set)
|
2015-11-04 07:20:08 -06:00
|
|
|
{
|
2016-03-17 09:42:13 -05:00
|
|
|
QMutableHashIterator<quint32, quint64> arpIter(arpTable_);
|
|
|
|
QMutableHashIterator<UInt128, quint64> ndpIter(ndpTable_);
|
|
|
|
|
|
|
|
switch (set) {
|
|
|
|
case kAllNeighbors:
|
|
|
|
arpTable_.clear();
|
|
|
|
ndpTable_.clear();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kUnresolvedNeighbors:
|
|
|
|
while (arpIter.hasNext()) {
|
|
|
|
arpIter.next();
|
|
|
|
if (arpIter.value() == 0)
|
|
|
|
arpIter.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ndpIter.hasNext()) {
|
|
|
|
ndpIter.next();
|
|
|
|
if (ndpIter.value() == 0)
|
|
|
|
ndpIter.remove();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Q_ASSERT(false); // Unreachable!
|
|
|
|
}
|
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)
|
|
|
|
{
|
2015-11-04 07:20:08 -06:00
|
|
|
case 0x0800: // IPv4
|
2016-01-01 00:29:31 -06:00
|
|
|
if (hasIp4_)
|
|
|
|
sendArpRequest(pktBuf);
|
2015-09-14 07:49:52 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x86dd: // 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
|
|
|
}
|
|
|
|
|
2015-11-04 07:20:08 -06:00
|
|
|
// Append this device's neighbors to the list
|
2015-11-10 23:22:29 -06:00
|
|
|
void Device::getNeighbors(OstEmul::DeviceNeighborList *neighbors)
|
2015-11-04 07:20:08 -06:00
|
|
|
{
|
2016-01-06 06:40:28 -06:00
|
|
|
QList<quint32> ip4List = arpTable_.keys();
|
|
|
|
QList<UInt128> ip6List = ndpTable_.keys();
|
|
|
|
QList<quint64> macList;
|
2015-11-04 07:20:08 -06:00
|
|
|
|
2016-01-06 06:40:28 -06:00
|
|
|
macList = arpTable_.values();
|
|
|
|
Q_ASSERT(ip4List.size() == macList.size());
|
2015-11-04 07:20:08 -06:00
|
|
|
|
2016-01-06 06:40:28 -06:00
|
|
|
for (int i = 0; i < ip4List.size(); i++) {
|
2015-11-04 07:20:08 -06:00
|
|
|
OstEmul::ArpEntry *arp = neighbors->add_arp();
|
2016-01-06 06:40:28 -06:00
|
|
|
arp->set_ip4(ip4List.at(i));
|
2015-11-04 07:20:08 -06:00
|
|
|
arp->set_mac(macList.at(i));
|
|
|
|
}
|
2016-01-06 06:40:28 -06:00
|
|
|
|
|
|
|
macList = ndpTable_.values();
|
|
|
|
Q_ASSERT(ip6List.size() == macList.size());
|
|
|
|
|
|
|
|
for (int i = 0; i < ip6List.size(); i++) {
|
|
|
|
OstEmul::NdpEntry *ndp = neighbors->add_ndp();
|
|
|
|
ndp->mutable_ip6()->set_hi(ip6List.at(i).hi64());
|
|
|
|
ndp->mutable_ip6()->set_lo(ip6List.at(i).lo64());
|
|
|
|
ndp->set_mac(macList.at(i));
|
|
|
|
}
|
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
|
2016-01-01 00:29:31 -06:00
|
|
|
if ((ethType == 0x0800) && 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)
|
|
|
|
{
|
|
|
|
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
|
2016-01-01 00:29:31 -06:00
|
|
|
if ((ethType == 0x0800) && hasIp4_) { // IPv4
|
2015-11-10 08:10:32 -06:00
|
|
|
int ipHdrLen = (pktData[0] & 0x0F) << 2;
|
|
|
|
quint32 dstIp, tgtIp, mask;
|
|
|
|
|
|
|
|
if (pktBuf->length() < ipHdrLen) {
|
|
|
|
qDebug("incomplete IPv4 header: expected %d, actual %d",
|
|
|
|
ipHdrLen, pktBuf->length());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dstIp = qFromBigEndian<quint32>(pktData + ipHdrLen - 4);
|
|
|
|
mask = ~0 << (32 - ip4PrefixLength_);
|
2016-01-13 09:45:47 -06:00
|
|
|
qDebug("dst %x mask %x self %x", dstIp, mask, ip4_);
|
2015-11-10 08:10:32 -06:00
|
|
|
tgtIp = ((dstIp & mask) == (ip4_ & mask)) ? dstIp : ip4Gateway_;
|
|
|
|
|
2016-01-01 08:47:54 -06:00
|
|
|
return arpTable_.value(tgtIp);
|
2015-11-10 08:10:32 -06:00
|
|
|
}
|
2016-01-13 09:45:47 -06:00
|
|
|
else if ((ethType == kEthTypeIp6) && hasIp6_) { // IPv6
|
|
|
|
UInt128 dstIp, tgtIp, mask;
|
|
|
|
|
|
|
|
if (pktBuf->length() < (kIp6HdrLen+2)) {
|
|
|
|
qDebug("incomplete IPv6 header: expected %d, actual %d",
|
|
|
|
kIp6HdrLen, pktBuf->length()-2);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dstIp = qFromBigEndian<UInt128>(pktData + 24);
|
|
|
|
mask = ~UInt128(0, 0) << (128 - ip6PrefixLength_);
|
|
|
|
qDebug("dst %s mask %s self %s",
|
|
|
|
qPrintable(QHostAddress(dstIp.toArray()).toString()),
|
|
|
|
qPrintable(QHostAddress(mask.toArray()).toString()),
|
|
|
|
qPrintable(QHostAddress(ip6_.toArray()).toString()));
|
|
|
|
tgtIp = ((dstIp & mask) == (ip6_ & mask)) ? dstIp : ip6Gateway_;
|
|
|
|
|
|
|
|
return ndpTable_.value(tgtIp);
|
|
|
|
}
|
2015-11-10 08:10:32 -06:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
//
|
|
|
|
// Private Methods
|
|
|
|
//
|
2016-01-01 08:47:54 -06:00
|
|
|
/*
|
|
|
|
* ---------------------------------------------------------
|
|
|
|
* IPv4 related private methods
|
|
|
|
* ---------------------------------------------------------
|
|
|
|
*/
|
2015-09-20 07:19:15 -05:00
|
|
|
void Device::receiveArp(PacketBuffer *pktBuf)
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
2015-09-20 07:19:15 -05:00
|
|
|
PacketBuffer *rspPkt;
|
2015-09-14 07:49:52 -05:00
|
|
|
uchar *pktData = pktBuf->data();
|
|
|
|
int offset = 0;
|
|
|
|
quint16 hwType, protoType;
|
|
|
|
quint8 hwAddrLen, protoAddrLen;
|
|
|
|
quint16 opCode;
|
|
|
|
quint64 srcMac, tgtMac;
|
|
|
|
quint32 srcIp, tgtIp;
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
// Extract tgtIp first to check quickly if this packet is for us or not
|
|
|
|
tgtIp = qFromBigEndian<quint32>(pktData + 24);
|
|
|
|
if (tgtIp != ip4_) {
|
2015-11-14 05:36:43 -06:00
|
|
|
qDebug("tgtIp %s is not me %s",
|
2015-09-20 07:19:15 -05:00
|
|
|
qPrintable(QHostAddress(tgtIp).toString()),
|
|
|
|
qPrintable(QHostAddress(ip4_).toString()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
// Extract annd verify ARP packet contents
|
|
|
|
hwType = qFromBigEndian<quint16>(pktData + offset);
|
|
|
|
offset += 2;
|
|
|
|
if (hwType != 1) // Mac
|
|
|
|
goto _invalid_exit;
|
|
|
|
|
|
|
|
protoType = qFromBigEndian<quint16>(pktData + offset);
|
|
|
|
offset += 2;
|
|
|
|
if (protoType != 0x0800) // IPv4
|
|
|
|
goto _invalid_exit;
|
|
|
|
|
|
|
|
hwAddrLen = pktData[offset];
|
|
|
|
offset += 1;
|
|
|
|
if (hwAddrLen != 6)
|
|
|
|
goto _invalid_exit;
|
|
|
|
|
|
|
|
protoAddrLen = pktData[offset];
|
|
|
|
offset += 1;
|
|
|
|
if (protoAddrLen != 4)
|
|
|
|
goto _invalid_exit;
|
|
|
|
|
|
|
|
opCode = qFromBigEndian<quint16>(pktData + offset);
|
|
|
|
offset += 2;
|
|
|
|
|
|
|
|
srcMac = qFromBigEndian<quint32>(pktData + offset);
|
|
|
|
offset += 4;
|
|
|
|
srcMac = (srcMac << 16) | qFromBigEndian<quint16>(pktData + offset);
|
|
|
|
offset += 2;
|
|
|
|
|
|
|
|
srcIp = qFromBigEndian<quint32>(pktData + offset);
|
|
|
|
offset += 4;
|
|
|
|
|
|
|
|
tgtMac = qFromBigEndian<quint32>(pktData + offset);
|
|
|
|
offset += 4;
|
|
|
|
tgtMac = (tgtMac << 16) | qFromBigEndian<quint16>(pktData + offset);
|
|
|
|
offset += 2;
|
|
|
|
|
|
|
|
switch (opCode)
|
|
|
|
{
|
2015-09-20 07:19:15 -05:00
|
|
|
case 1: // ARP Request
|
2016-01-01 08:47:54 -06:00
|
|
|
arpTable_.insert(srcIp, srcMac);
|
2015-11-04 07:20:08 -06:00
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
rspPkt = new PacketBuffer;
|
2015-11-14 05:36:43 -06:00
|
|
|
rspPkt->reserve(encapSize());
|
2015-09-20 07:19:15 -05:00
|
|
|
pktData = rspPkt->put(28);
|
|
|
|
if (pktData) {
|
|
|
|
// HTYP, PTYP
|
|
|
|
*(quint32*)(pktData ) = qToBigEndian(quint32(0x00010800));
|
|
|
|
// HLEN, PLEN, OPER
|
|
|
|
*(quint32*)(pktData+ 4) = qToBigEndian(quint32(0x06040002));
|
|
|
|
// Source H/W Addr, Proto Addr
|
|
|
|
*(quint32*)(pktData+ 8) = qToBigEndian(quint32(mac_ >> 16));
|
|
|
|
*(quint16*)(pktData+12) = qToBigEndian(quint16(mac_ & 0xffff));
|
|
|
|
*(quint32*)(pktData+14) = qToBigEndian(ip4_);
|
|
|
|
// Target H/W Addr, Proto Addr
|
|
|
|
*(quint32*)(pktData+18) = qToBigEndian(quint32(srcMac >> 16));
|
|
|
|
*(quint16*)(pktData+22) = qToBigEndian(quint16(srcMac & 0xffff));
|
|
|
|
*(quint32*)(pktData+24) = qToBigEndian(srcIp);
|
2015-09-14 07:49:52 -05:00
|
|
|
}
|
2015-09-20 07:19:15 -05:00
|
|
|
|
|
|
|
encap(rspPkt, srcMac, 0x0806);
|
|
|
|
transmitPacket(rspPkt);
|
|
|
|
|
|
|
|
qDebug("Sent ARP Reply for srcIp/tgtIp=%s/%s",
|
2015-11-14 05:36:43 -06:00
|
|
|
qPrintable(QHostAddress(srcIp).toString()),
|
2015-09-20 07:19:15 -05:00
|
|
|
qPrintable(QHostAddress(tgtIp).toString()));
|
2015-09-14 07:49:52 -05:00
|
|
|
break;
|
|
|
|
case 2: // ARP Response
|
2016-01-01 08:47:54 -06:00
|
|
|
arpTable_.insert(srcIp, srcMac);
|
2015-11-04 07:20:08 -06:00
|
|
|
break;
|
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
_invalid_exit:
|
|
|
|
qWarning("Invalid ARP content");
|
|
|
|
return;
|
|
|
|
}
|
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;
|
|
|
|
quint32 srcIp = ip4_, dstIp, mask, 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);
|
|
|
|
|
|
|
|
mask = ~0 << (32 - ip4PrefixLength_);
|
|
|
|
qDebug("dst %x src %x mask %x", dstIp, srcIp, mask);
|
|
|
|
tgtIp = ((dstIp & mask) == (srcIp & mask)) ? dstIp : ip4Gateway_;
|
|
|
|
|
2016-01-28 08:31:19 -06:00
|
|
|
sendArpRequest(tgtIp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::sendArpRequest(quint32 tgtIp)
|
|
|
|
{
|
|
|
|
quint32 srcIp = ip4_;
|
|
|
|
PacketBuffer *reqPkt;
|
|
|
|
uchar *pktData;
|
|
|
|
|
|
|
|
// Validate target IP
|
|
|
|
if (!tgtIp)
|
|
|
|
return;
|
|
|
|
|
2016-03-17 09:42:13 -05:00
|
|
|
// This function will be called once per unique stream - which
|
|
|
|
// may all have the same dst IP; even if dst IP are different the
|
|
|
|
// gateway for the different dst IP may all be same. However,
|
|
|
|
// we don't want to send duplicate ARP requests, so we check
|
|
|
|
// if the tgtIP is already in the cache (resolved or unresolved)
|
|
|
|
// and if so, we don't resend it
|
2016-01-01 08:47:54 -06:00
|
|
|
if (arpTable_.contains(tgtIp))
|
2015-11-10 08:10:32 -06:00
|
|
|
return;
|
|
|
|
|
2015-11-04 07:20:08 -06:00
|
|
|
reqPkt = new PacketBuffer;
|
|
|
|
reqPkt->reserve(encapSize());
|
|
|
|
pktData = reqPkt->put(28);
|
|
|
|
if (pktData) {
|
|
|
|
// HTYP, PTYP
|
|
|
|
*(quint32*)(pktData ) = qToBigEndian(quint32(0x00010800));
|
|
|
|
// HLEN, PLEN, OPER
|
|
|
|
*(quint32*)(pktData+ 4) = qToBigEndian(quint32(0x06040001));
|
|
|
|
// Source H/W Addr, Proto Addr
|
|
|
|
*(quint32*)(pktData+ 8) = qToBigEndian(quint32(mac_ >> 16));
|
|
|
|
*(quint16*)(pktData+12) = qToBigEndian(quint16(mac_ & 0xffff));
|
|
|
|
*(quint32*)(pktData+14) = qToBigEndian(srcIp);
|
|
|
|
// Target H/W Addr, Proto Addr
|
|
|
|
*(quint32*)(pktData+18) = qToBigEndian(quint32(0));
|
|
|
|
*(quint16*)(pktData+22) = qToBigEndian(quint16(0));
|
|
|
|
*(quint32*)(pktData+24) = qToBigEndian(tgtIp);
|
|
|
|
}
|
|
|
|
|
|
|
|
encap(reqPkt, kBcastMac, 0x0806);
|
|
|
|
transmitPacket(reqPkt);
|
2016-01-01 08:47:54 -06:00
|
|
|
arpTable_.insert(tgtIp, 0);
|
2015-11-04 07:20:08 -06:00
|
|
|
|
|
|
|
qDebug("Sent ARP Request for srcIp/tgtIp=%s/%s",
|
|
|
|
qPrintable(QHostAddress(srcIp).toString()),
|
|
|
|
qPrintable(QHostAddress(tgtIp).toString()));
|
|
|
|
}
|
2015-11-29 09:48:31 -06:00
|
|
|
|
2015-12-23 09:48:25 -06:00
|
|
|
void Device::receiveIp4(PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
uchar *pktData = pktBuf->data();
|
|
|
|
uchar ipProto;
|
|
|
|
quint32 dstIp;
|
|
|
|
|
|
|
|
if (pktData[0] != 0x45) {
|
|
|
|
qDebug("%s: Unsupported IP version or options (%02x) ", __FUNCTION__,
|
|
|
|
pktData[0]);
|
|
|
|
goto _invalid_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pktBuf->length() < 20) {
|
|
|
|
qDebug("incomplete IPv4 header: expected 20, actual %d",
|
|
|
|
pktBuf->length());
|
|
|
|
goto _invalid_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: We don't verify IP Header checksum
|
|
|
|
|
|
|
|
dstIp = qFromBigEndian<quint32>(pktData + 16);
|
|
|
|
if (dstIp != ip4_) {
|
|
|
|
qDebug("%s: dstIp %x is not me (%x)", __FUNCTION__, dstIp, ip4_);
|
|
|
|
goto _invalid_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipProto = pktData[9];
|
2016-01-08 08:31:42 -06:00
|
|
|
qDebug("%s: ipProto = %d", __FUNCTION__, ipProto);
|
2015-12-23 09:48:25 -06:00
|
|
|
switch (ipProto) {
|
|
|
|
case 1: // ICMP
|
|
|
|
pktBuf->pull(20);
|
|
|
|
receiveIcmp4(pktBuf);
|
|
|
|
break;
|
|
|
|
default:
|
2016-01-08 08:31:42 -06:00
|
|
|
qWarning("%s: Unsupported ipProto %d", __FUNCTION__, ipProto);
|
2015-12-23 09:48:25 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_invalid_exit:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function assumes we are replying back to the same IP
|
|
|
|
// that originally sent us the packet and therefore we can reuse the
|
|
|
|
// ingress packet for egress; in other words, it assumes the
|
|
|
|
// original IP header is intact and will just reuse it after
|
|
|
|
// minimal modifications
|
|
|
|
void Device::sendIp4Reply(PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
uchar *pktData = pktBuf->push(20);
|
|
|
|
uchar origTtl = pktData[8];
|
|
|
|
uchar ipProto = pktData[9];
|
|
|
|
quint32 srcIp, dstIp;
|
|
|
|
quint32 sum;
|
|
|
|
|
|
|
|
// Swap src/dst IP addresses
|
|
|
|
dstIp = qFromBigEndian<quint32>(pktData + 12); // srcIp in original pkt
|
|
|
|
srcIp = qFromBigEndian<quint32>(pktData + 16); // dstIp in original pkt
|
|
|
|
|
2016-01-12 09:32:19 -06:00
|
|
|
if (!arpTable_.contains(dstIp)) {
|
|
|
|
qWarning("%s: mac not found for %s; unable to send IPv4 packet",
|
|
|
|
__FUNCTION__, qPrintable(QHostAddress(dstIp).toString()));
|
2015-12-23 09:48:25 -06:00
|
|
|
return;
|
2016-01-12 09:32:19 -06:00
|
|
|
}
|
2015-12-23 09:48:25 -06:00
|
|
|
|
|
|
|
*(quint32*)(pktData + 12) = qToBigEndian(srcIp);
|
|
|
|
*(quint32*)(pktData + 16) = qToBigEndian(dstIp);
|
|
|
|
|
|
|
|
// Reset TTL
|
|
|
|
pktData[8] = 64;
|
|
|
|
|
|
|
|
// Incremental checksum update (RFC 1624 [Eqn.3])
|
|
|
|
// HC' = ~(~HC + ~m + m')
|
|
|
|
sum = quint16(~qFromBigEndian<quint16>(pktData + 10)); // old cksum
|
|
|
|
sum += quint16(~quint16(origTtl << 8 | ipProto)); // old value
|
|
|
|
sum += quint16(pktData[8] << 8 | ipProto); // new value
|
|
|
|
while(sum >> 16)
|
|
|
|
sum = (sum & 0xFFFF) + (sum >> 16);
|
|
|
|
*(quint16*)(pktData + 10) = qToBigEndian(quint16(~sum));
|
|
|
|
|
2016-01-01 08:47:54 -06:00
|
|
|
encap(pktBuf, arpTable_.value(dstIp), 0x0800);
|
2015-12-23 09:48:25 -06:00
|
|
|
transmitPacket(pktBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::receiveIcmp4(PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
uchar *pktData = pktBuf->data();
|
|
|
|
quint32 sum;
|
|
|
|
|
|
|
|
// XXX: We don't verify icmp checksum
|
|
|
|
|
|
|
|
// We handle only ping request
|
|
|
|
if (pktData[0] != 8) { // Echo Request
|
|
|
|
qDebug("%s: Ignoring non echo request (%d)", __FUNCTION__, pktData[0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pktData[0] = 0; // Echo Reply
|
|
|
|
|
|
|
|
// Incremental checksum update (RFC 1624 [Eqn.3])
|
|
|
|
// HC' = ~(~HC + ~m + m')
|
|
|
|
sum = quint16(~qFromBigEndian<quint16>(pktData + 2)); // old cksum
|
|
|
|
sum += quint16(~quint16(8 << 8 | pktData[1])); // old value
|
|
|
|
sum += quint16(0 << 8 | pktData[1]); // new value
|
|
|
|
while(sum >> 16)
|
|
|
|
sum = (sum & 0xFFFF) + (sum >> 16);
|
|
|
|
*(quint16*)(pktData + 2) = qToBigEndian(quint16(~sum));
|
|
|
|
|
|
|
|
sendIp4Reply(pktBuf);
|
|
|
|
qDebug("Sent ICMP Echo Reply");
|
|
|
|
}
|
|
|
|
|
2016-01-01 08:47:54 -06:00
|
|
|
/*
|
|
|
|
* ---------------------------------------------------------
|
|
|
|
* IPV6 related private methods
|
|
|
|
* ---------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2016-01-06 06:40:28 -06:00
|
|
|
void Device::receiveIp6(PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
uchar *pktData = pktBuf->data();
|
|
|
|
uchar ipProto;
|
|
|
|
UInt128 dstIp;
|
|
|
|
|
|
|
|
if ((pktData[0] & 0xF0) != 0x60) {
|
|
|
|
qDebug("%s: Unsupported IP version (%02x) ", __FUNCTION__,
|
|
|
|
pktData[0]);
|
|
|
|
goto _invalid_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pktBuf->length() < kIp6HdrLen) {
|
|
|
|
qDebug("incomplete IPv6 header: expected %d, actual %d",
|
|
|
|
kIp6HdrLen, pktBuf->length());
|
|
|
|
goto _invalid_exit;
|
|
|
|
}
|
|
|
|
|
2016-01-08 08:31:42 -06:00
|
|
|
// FIXME: check for specific mcast address(es) instead of any mcast?
|
2016-01-06 06:40:28 -06:00
|
|
|
dstIp = qFromBigEndian<UInt128>(pktData + 24);
|
2016-01-08 08:31:42 -06:00
|
|
|
if (!isIp6Mcast(dstIp) && (dstIp != ip6_)) {
|
2016-01-06 06:40:28 -06:00
|
|
|
qDebug("%s: dstIp %s is not me (%s)", __FUNCTION__,
|
|
|
|
qPrintable(QHostAddress(dstIp.toArray()).toString()),
|
|
|
|
qPrintable(QHostAddress(ip6_.toArray()).toString()));
|
|
|
|
goto _invalid_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipProto = pktData[6];
|
|
|
|
switch (ipProto) {
|
|
|
|
case kIpProtoIcmp6:
|
|
|
|
pktBuf->pull(kIp6HdrLen);
|
|
|
|
receiveIcmp6(pktBuf);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_invalid_exit:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-01 08:47:54 -06:00
|
|
|
// pktBuf should point to start of IP payload
|
|
|
|
bool Device::sendIp6(PacketBuffer *pktBuf, UInt128 dstIp, quint8 protocol)
|
|
|
|
{
|
|
|
|
int payloadLen = pktBuf->length();
|
|
|
|
uchar *p = pktBuf->push(kIp6HdrLen);
|
2016-01-12 09:32:19 -06:00
|
|
|
quint64 dstMac = ndpTable_.value(dstIp);
|
2016-01-01 08:47:54 -06:00
|
|
|
|
|
|
|
if (!p) {
|
|
|
|
qWarning("%s: failed to push %d bytes [0x%p, 0x%p]", __FUNCTION__,
|
|
|
|
kIp6HdrLen, pktBuf->head(), pktBuf->data());
|
|
|
|
goto _error_exit;
|
|
|
|
}
|
|
|
|
|
2016-01-12 09:32:19 -06:00
|
|
|
// In case of mcast, derive dstMac
|
|
|
|
if ((dstIp.hi64() >> 56) == 0xff)
|
|
|
|
dstMac = (quint64(0x3333) << 32) | (dstIp.lo64() & 0xffffffff);
|
|
|
|
|
|
|
|
if (!dstMac) {
|
|
|
|
qWarning("%s: mac not found for %s; unable to send IPv6 packet",
|
|
|
|
__FUNCTION__,
|
|
|
|
qPrintable(QHostAddress(dstIp.toArray()).toString()));
|
|
|
|
goto _error_exit;
|
|
|
|
}
|
|
|
|
|
2016-01-01 08:47:54 -06:00
|
|
|
// Ver(4), TrfClass(8), FlowLabel(8)
|
|
|
|
*(quint32*)(p ) = qToBigEndian(quint32(0x60000000));
|
|
|
|
*(quint16*)(p+ 4) = qToBigEndian(quint16(payloadLen));
|
|
|
|
p[6] = protocol;
|
|
|
|
p[7] = 255; // HopLimit
|
|
|
|
memcpy(p+ 8, ip6_.toArray(), 16); // Source IP
|
|
|
|
memcpy(p+24, dstIp.toArray(), 16); // Destination IP
|
|
|
|
|
|
|
|
// FIXME: both these functions should return success/failure
|
|
|
|
encap(pktBuf, dstMac, kEthTypeIp6);
|
|
|
|
transmitPacket(pktBuf);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
_error_exit:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-12 07:33:52 -06:00
|
|
|
// This function assumes we are replying back to the same IP
|
|
|
|
// that originally sent us the packet and therefore we can reuse the
|
|
|
|
// ingress packet for egress; in other words, it assumes the
|
|
|
|
// original IP header is intact and will just reuse it after
|
|
|
|
// minimal modifications
|
|
|
|
void Device::sendIp6Reply(PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
uchar *pktData = pktBuf->push(kIp6HdrLen);
|
|
|
|
UInt128 srcIp, dstIp;
|
|
|
|
|
|
|
|
// Swap src/dst IP addresses
|
|
|
|
dstIp = qFromBigEndian<UInt128>(pktData + 8); // srcIp in original pkt
|
|
|
|
srcIp = qFromBigEndian<UInt128>(pktData + 24); // dstIp in original pkt
|
|
|
|
|
2016-01-12 09:32:19 -06:00
|
|
|
if (!ndpTable_.contains(dstIp)) {
|
|
|
|
qWarning("%s: mac not found for %s; unable to send IPv6 packet",
|
|
|
|
__FUNCTION__,
|
|
|
|
qPrintable(QHostAddress(dstIp.toArray()).toString()));
|
2016-01-12 07:33:52 -06:00
|
|
|
return;
|
2016-01-12 09:32:19 -06:00
|
|
|
}
|
2016-01-12 07:33:52 -06:00
|
|
|
|
|
|
|
memcpy(pktData + 8, srcIp.toArray(), 16); // Source IP
|
|
|
|
memcpy(pktData + 24, dstIp.toArray(), 16); // Destination IP
|
|
|
|
|
|
|
|
// Reset TTL
|
|
|
|
pktData[7] = 64;
|
|
|
|
|
|
|
|
encap(pktBuf, ndpTable_.value(dstIp), 0x86dd);
|
|
|
|
transmitPacket(pktBuf);
|
|
|
|
}
|
|
|
|
|
2016-01-06 06:40:28 -06:00
|
|
|
void Device::receiveIcmp6(PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
uchar *pktData = pktBuf->data();
|
|
|
|
quint8 type = pktData[0];
|
2016-01-12 07:33:52 -06:00
|
|
|
quint32 sum;
|
2016-01-06 06:40:28 -06:00
|
|
|
|
|
|
|
// XXX: We don't verify icmp checksum
|
|
|
|
|
|
|
|
switch (type) {
|
2016-01-12 07:33:52 -06:00
|
|
|
case 128: // ICMPv6 Echo Request
|
|
|
|
pktData[0] = 129; // Echo Reply
|
|
|
|
|
|
|
|
// Incremental checksum update (RFC 1624 [Eqn.3])
|
|
|
|
// HC' = ~(~HC + ~m + m')
|
|
|
|
sum = quint16(~qFromBigEndian<quint16>(pktData + 2)); // old cksum
|
|
|
|
sum += quint16(~quint16(128 << 8 | pktData[1])); // old value
|
|
|
|
sum += quint16(129 << 8 | pktData[1]); // new value
|
|
|
|
while(sum >> 16)
|
|
|
|
sum = (sum & 0xFFFF) + (sum >> 16);
|
|
|
|
*(quint16*)(pktData + 2) = qToBigEndian(quint16(~sum));
|
|
|
|
|
|
|
|
sendIp6Reply(pktBuf);
|
|
|
|
qDebug("Sent ICMPv6 Echo Reply");
|
|
|
|
break;
|
|
|
|
|
2016-01-06 06:40:28 -06:00
|
|
|
case 135: // Neigh Solicit
|
|
|
|
case 136: // Neigh Advt
|
|
|
|
receiveNdp(pktBuf);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::receiveNdp(PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
uchar *pktData = pktBuf->data();
|
|
|
|
quint8 type = pktData[0];
|
2016-01-08 08:31:42 -06:00
|
|
|
int len = pktBuf->length();
|
|
|
|
int minLen = 24 + (type == 136 ? 8 : 0); // NA should have the Target TLV
|
2016-01-06 06:40:28 -06:00
|
|
|
|
2016-01-08 08:31:42 -06:00
|
|
|
if (len < minLen) {
|
|
|
|
qDebug("%s: incomplete NS/NA header: expected %d, actual %d",
|
|
|
|
__FUNCTION__, minLen, pktBuf->length());
|
2016-01-06 06:40:28 -06:00
|
|
|
goto _invalid_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case 135: { // Neigh Solicit
|
2016-01-08 08:31:42 -06:00
|
|
|
// TODO: Validation as per RFC 4861
|
|
|
|
sendNeighborAdvertisement(pktBuf);
|
2016-01-06 06:40:28 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 136: { // Neigh Advt
|
|
|
|
quint8 flags = pktData[4];
|
|
|
|
const quint8 kSFlag = 0x40;
|
|
|
|
const quint8 kOFlag = 0x20;
|
|
|
|
UInt128 tgtIp = qFromBigEndian<UInt128>(pktData + 8);
|
|
|
|
quint64 mac = ndpTable_.value(tgtIp);
|
|
|
|
|
|
|
|
// Update NDP table only for solicited responses
|
|
|
|
if (!(flags & kSFlag))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((flags & kOFlag) || (mac == 0)) {
|
|
|
|
// Check if we have a Target Link-Layer TLV
|
|
|
|
if ((pktData[24] != 2) || (pktData[25] != 1))
|
|
|
|
goto _invalid_exit;
|
|
|
|
mac = qFromBigEndian<quint32>(pktData + 26);
|
|
|
|
mac = (mac << 16) | qFromBigEndian<quint16>(pktData + 30);
|
|
|
|
ndpTable_.insert(tgtIp, mac);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_invalid_exit:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-01-28 08:31:19 -06:00
|
|
|
UInt128 srcIp = ip6_, dstIp, mask, 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);
|
|
|
|
|
|
|
|
mask = ~UInt128(0, 0) << (128 - ip6PrefixLength_);
|
|
|
|
qDebug("%s: dst %s src %s mask %s", __FUNCTION__,
|
|
|
|
qPrintable(QHostAddress(dstIp.toArray()).toString()),
|
|
|
|
qPrintable(QHostAddress(srcIp.toArray()).toString()),
|
|
|
|
qPrintable(QHostAddress(mask.toArray()).toString()));
|
|
|
|
tgtIp = ((dstIp & mask) == (srcIp & mask)) ? dstIp : ip6Gateway_;
|
|
|
|
|
2016-01-28 08:31:19 -06:00
|
|
|
sendNeighborSolicit(tgtIp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::sendNeighborSolicit(UInt128 tgtIp)
|
|
|
|
{
|
|
|
|
UInt128 dstIp, srcIp = ip6_;
|
|
|
|
PacketBuffer *reqPkt;
|
|
|
|
uchar *pktData;
|
|
|
|
|
|
|
|
// Validate target IP
|
|
|
|
if (tgtIp == UInt128(0, 0))
|
|
|
|
return;
|
|
|
|
|
2016-01-01 08:47:54 -06:00
|
|
|
// Do we already have a NDP entry (resolved or unresolved)?
|
2016-03-17 09:42:13 -05:00
|
|
|
// If so, don't resend (see note in sendArpRequest())
|
2016-01-01 08:47:54 -06:00
|
|
|
if (ndpTable_.contains(tgtIp))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Form the solicited node address to be used as dstIp
|
|
|
|
// ff02::1:ffXX:XXXX/104
|
|
|
|
dstIp = UInt128((quint64(0xff02) << 48),
|
|
|
|
(quint64(0x01ff) << 24) | (tgtIp.lo64() & 0xFFFFFF));
|
|
|
|
|
|
|
|
reqPkt = new PacketBuffer;
|
|
|
|
reqPkt->reserve(encapSize() + kIp6HdrLen);
|
|
|
|
pktData = reqPkt->put(32);
|
|
|
|
if (pktData) {
|
|
|
|
// Calculate checksum first -
|
|
|
|
// start with fixed fields in ICMP Header and IPv6 Pseudo Header ...
|
|
|
|
quint32 sum = 0x8700 + 0x0101 + 32 + kIpProtoIcmp6;
|
|
|
|
|
|
|
|
// then variable fields from ICMP header ...
|
|
|
|
sum += sumUInt128(tgtIp);
|
|
|
|
sum += (mac_ >> 32) + ((mac_ >> 16) & 0xffff) + (mac_ & 0xffff);
|
|
|
|
|
|
|
|
// and variable fields from IPv6 pseudo header
|
|
|
|
sum += sumUInt128(ip6_);
|
|
|
|
sum += sumUInt128(dstIp);
|
|
|
|
|
|
|
|
while(sum >> 16)
|
|
|
|
sum = (sum & 0xFFFF) + (sum >> 16);
|
|
|
|
|
|
|
|
// Type, Code
|
|
|
|
*(quint16*)(pktData ) = qToBigEndian(quint16(0x8700));
|
|
|
|
// Checksum
|
|
|
|
*(quint16*)(pktData+ 2) = qToBigEndian(quint16(~sum));
|
|
|
|
// Reserved
|
|
|
|
*(quint32*)(pktData+ 4) = qToBigEndian(quint32(0));
|
|
|
|
// Target IP
|
|
|
|
memcpy(pktData+ 8, tgtIp.toArray(), 16);
|
|
|
|
// Source Addr TLV + MacAddr
|
|
|
|
*(quint16*)(pktData+24) = qToBigEndian(quint16(0x0101));
|
|
|
|
*(quint32*)(pktData+26) = qToBigEndian(quint32(mac_ >> 16));
|
|
|
|
*(quint16*)(pktData+30) = qToBigEndian(quint16(mac_ & 0xffff));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sendIp6(reqPkt, dstIp , kIpProtoIcmp6))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ndpTable_.insert(tgtIp, 0);
|
|
|
|
|
|
|
|
qDebug("Sent NDP Request for srcIp/tgtIp=%s/%s",
|
|
|
|
qPrintable(QHostAddress(srcIp.toArray()).toString()),
|
|
|
|
qPrintable(QHostAddress(tgtIp.toArray()).toString()));
|
2015-12-31 08:47:56 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 08:31:42 -06:00
|
|
|
// Send NA for the NS packet in pktBuf
|
|
|
|
// pktBuf should point to start of ICMPv6 header
|
|
|
|
void Device::sendNeighborAdvertisement(PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
PacketBuffer *naPkt;
|
|
|
|
uchar *pktData = pktBuf->data();
|
|
|
|
quint16 flags = 0x6000; // solicit = 1; overide = 1
|
|
|
|
uchar *ip6Hdr;
|
|
|
|
UInt128 tgtIp, srcIp;
|
|
|
|
|
|
|
|
tgtIp = qFromBigEndian<UInt128>(pktData + 8);
|
|
|
|
if (tgtIp != ip6_) {
|
|
|
|
qDebug("%s: NS tgtIp %s is not us %s", __FUNCTION__,
|
|
|
|
qPrintable(QHostAddress(tgtIp.toArray()).toString()),
|
|
|
|
qPrintable(QHostAddress(ip6_.toArray()).toString()));
|
|
|
|
ip6Hdr = pktBuf->push(kIp6HdrLen);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ip6Hdr = pktBuf->push(kIp6HdrLen);
|
|
|
|
srcIp = qFromBigEndian<UInt128>(ip6Hdr + 8);
|
|
|
|
|
|
|
|
if (srcIp == UInt128(0, 0)) {
|
|
|
|
// reset solicit flag
|
|
|
|
flags &= ~0x4000;
|
|
|
|
// NA should be sent to All nodes address
|
|
|
|
srcIp = UInt128(quint64(0xff02) << 48, quint64(1));
|
|
|
|
}
|
2016-01-12 09:32:19 -06:00
|
|
|
else if (pktBuf->length() >= 32) { // have TLVs?
|
|
|
|
if ((pktData[24] == 0x01) && (pktData[25] == 0x01)) { // Source TLV
|
|
|
|
quint64 mac;
|
|
|
|
mac = qFromBigEndian<quint32>(pktData + 26);
|
|
|
|
mac = (mac << 16) | qFromBigEndian<quint16>(pktData + 30);
|
|
|
|
ndpTable_.insert(srcIp, mac);
|
|
|
|
}
|
|
|
|
}
|
2016-01-08 08:31:42 -06:00
|
|
|
|
|
|
|
naPkt = new PacketBuffer;
|
|
|
|
naPkt->reserve(encapSize() + kIp6HdrLen);
|
|
|
|
pktData = naPkt->put(32);
|
|
|
|
if (pktData) {
|
|
|
|
// Calculate checksum first -
|
|
|
|
// start with fixed fields in ICMP Header and IPv6 Pseudo Header ...
|
|
|
|
quint32 sum = (0x8800 + flags + 0x0201) + (32 + kIpProtoIcmp6);
|
|
|
|
|
|
|
|
// then variable fields from ICMP header ...
|
|
|
|
sum += sumUInt128(tgtIp);
|
|
|
|
sum += (mac_ >> 32) + ((mac_ >> 16) & 0xffff) + (mac_ & 0xffff);
|
|
|
|
|
|
|
|
// and variable fields from IPv6 pseudo header
|
|
|
|
sum += sumUInt128(ip6_);
|
|
|
|
sum += sumUInt128(srcIp);
|
|
|
|
|
|
|
|
while(sum >> 16)
|
|
|
|
sum = (sum & 0xFFFF) + (sum >> 16);
|
|
|
|
|
|
|
|
// Type, Code
|
|
|
|
*(quint16*)(pktData ) = qToBigEndian(quint16(0x8800));
|
|
|
|
// Checksum
|
|
|
|
*(quint16*)(pktData+ 2) = qToBigEndian(quint16(~sum));
|
|
|
|
// Flags-Reserved
|
|
|
|
*(quint32*)(pktData+ 4) = qToBigEndian(quint32(flags << 16));
|
|
|
|
// Target IP
|
|
|
|
memcpy(pktData+ 8, tgtIp.toArray(), 16);
|
|
|
|
// Target Addr TLV + MacAddr
|
|
|
|
*(quint16*)(pktData+24) = qToBigEndian(quint16(0x0201));
|
|
|
|
*(quint32*)(pktData+26) = qToBigEndian(quint32(mac_ >> 16));
|
|
|
|
*(quint16*)(pktData+30) = qToBigEndian(quint16(mac_ & 0xffff));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sendIp6(naPkt, srcIp , kIpProtoIcmp6))
|
|
|
|
return;
|
|
|
|
|
|
|
|
qDebug("Sent Neigh Advt to dstIp for tgtIp=%s/%s",
|
|
|
|
qPrintable(QHostAddress(srcIp.toArray()).toString()),
|
|
|
|
qPrintable(QHostAddress(tgtIp.toArray()).toString()));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|