1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
/*
* Dibbler - a portable DHCPv6
*
* authors: Tomasz Mrugalski <thomson@klub.com.pl>
* Marek Senderski <msend@o2.pl>
*
* released under GNU GPL v2 only licence
*
*/
#define MAX_PACKET_LEN 1452
#define RELAY_FORW_MSG_LEN 36
#include <cstdlib>
#include <fstream>
#include <vector>
#include <string.h>
#include "RelTransMgr.h"
#include "RelCfgMgr.h"
#include "RelIfaceMgr.h"
#include "RelOptInterfaceID.h"
#include "RelOptEcho.h"
#include "RelOptGeneric.h"
#include "Logger.h"
#include "Portable.h"
TRelTransMgr * TRelTransMgr::Instance = 0; // singleton implementation
TRelTransMgr::TRelTransMgr(const std::string& xmlFile)
:XmlFile(xmlFile), IsDone(false)
{
// for each interface in CfgMgr, create socket (in IfaceMgr)
SPtr<TRelCfgIface> confIface;
RelCfgMgr().firstIface();
while (confIface=RelCfgMgr().getIface()) {
if (!this->openSocket(confIface)) {
this->IsDone = true;
break;
}
}
}
/*
* opens proper (multicast or unicast) socket on interface
*/
bool TRelTransMgr::openSocket(SPtr<TRelCfgIface> cfgIface) {
SPtr<TIfaceIface> iface = RelIfaceMgr().getIfaceByID(cfgIface->getID());
if (!iface) {
Log(Crit) << "Unable to find " << cfgIface->getName() << "/" << cfgIface->getID()
<< " interface in the IfaceMgr." << LogEnd;
return false;
}
SPtr<TIPv6Addr> srvUnicast = cfgIface->getServerUnicast();
SPtr<TIPv6Addr> clntUnicast = cfgIface->getClientUnicast();
SPtr<TIPv6Addr> addr;
if (cfgIface->getServerMulticast() || srvUnicast) {
iface->firstGlobalAddr();
addr = iface->getGlobalAddr();
if (!addr) {
Log(Warning) << "No global address defined on the " << iface->getFullName() << " interface."
<< " Trying to bind link local address, but expect troubles with relaying." << LogEnd;
iface->firstLLAddress();
addr = new TIPv6Addr(iface->getLLAddress());
}
Log(Notice) << "Creating srv unicast (" << addr->getPlain() << ") socket on the "
<< iface->getName() << "/" << iface->getID() << " interface." << LogEnd;
if (!iface->addSocket(addr, DHCPSERVER_PORT, true, false)) {
Log(Crit) << "Proper socket creation failed." << LogEnd;
return false;
}
}
if (cfgIface->getClientMulticast()) {
addr = new TIPv6Addr(ALL_DHCP_RELAY_AGENTS_AND_SERVERS, true);
Log(Notice) << "Creating clnt multicast (" << addr->getPlain() << ") socket on the "
<< iface->getName() << "/" << iface->getID() << " interface." << LogEnd;
if (!iface->addSocket(addr, DHCPSERVER_PORT, true, false)) {
Log(Crit) << "Proper socket creation failed." << LogEnd;
return false;
}
}
if (clntUnicast) {
addr = new TIPv6Addr(ALL_DHCP_RELAY_AGENTS_AND_SERVERS, true);
Log(Notice) << "Creating clnt unicast (" << clntUnicast->getPlain() << ") socket on the "
<< iface->getName() << "/" << iface->getID() << " interface." << LogEnd;
if (!iface->addSocket(clntUnicast, DHCPSERVER_PORT, true, false)) {
Log(Crit) << "Proper socket creation failed." << LogEnd;
return false;
}
}
return true;
}
/**
* relays normal (i.e. not server replies) messages to defined servers
*/
void TRelTransMgr::relayMsg(SPtr<TRelMsg> msg)
{
static char buf[MAX_PACKET_LEN];
int offset = 0;
int bufLen;
int hopCount = 0;
if (!msg->check()) {
Log(Warning) << "Invalid message received." << LogEnd;
return;
}
if (msg->getDestAddr()) {
this->relayMsgRepl(msg);
return;
}
if (msg->getType() == RELAY_FORW_MSG) {
hopCount = msg->getHopCount()+1;
}
// prepare message
SPtr<TIfaceIface> iface = RelIfaceMgr().getIfaceByID(msg->getIface());
SPtr<TIPv6Addr> addr;
// store header
buf[offset++] = RELAY_FORW_MSG;
buf[offset++] = hopCount;
// store link-addr
iface->firstGlobalAddr();
addr = iface->getGlobalAddr();
if (!addr) {
Log(Warning) << "Interface " << iface->getFullName() << " does not have global address." << LogEnd;
addr = new TIPv6Addr("::", true);
}
addr->storeSelf(buf+offset);
offset += 16;
// store peer-addr
addr = msg->getRemoteAddr();
addr->storeSelf(buf+offset);
offset += 16;
SPtr<TRelCfgIface> cfgIface;
cfgIface = RelCfgMgr().getIfaceByID(msg->getIface());
TRelOptInterfaceID ifaceID(cfgIface->getInterfaceID(), 0);
if (RelCfgMgr().getInterfaceIDOrder()==REL_IFACE_ID_ORDER_BEFORE)
{
// store InterfaceID option
ifaceID.storeSelf(buf + offset);
offset += ifaceID.getSize();
Log(Debug) << "Interface-id option added before relayed message." << LogEnd;
}
// store relay msg option
writeUint16((buf+offset), OPTION_RELAY_MSG);
offset += sizeof(uint16_t);
writeUint16((buf+offset), msg->getSize());
offset += sizeof(uint16_t);
bufLen = msg->storeSelf(buf+offset);
offset += bufLen;
if (RelCfgMgr().getInterfaceIDOrder()==REL_IFACE_ID_ORDER_AFTER)
{
// store InterfaceID option
ifaceID.storeSelf(buf + offset);
offset += ifaceID.getSize();
Log(Debug) << "Interface-id option added after relayed message." << LogEnd;
}
if (RelCfgMgr().getInterfaceIDOrder()==REL_IFACE_ID_ORDER_NONE)
{
Log(Warning) << "Interface-id option not added (interface-id-order omit used in relay.conf). "
<< "That is a debugging feature and violates RFC3315. Use with caution." << LogEnd;
}
SPtr<TOptVendorData> remoteID = RelCfgMgr().getRemoteID();
if (remoteID) {
remoteID->storeSelf(buf+offset);
offset += remoteID->getSize();
Log(Debug) << "Appended RemoteID with " << remoteID->getVendorDataLen()
<< "-byte long data (option length="
<< remoteID->getSize() << ")." << LogEnd;
}
SPtr<TOpt> relayID = RelCfgMgr().getRelayID();
if (relayID) {
relayID->storeSelf(buf + offset);
offset += relayID->getSize();
Log(Debug) << "Appended Relay-ID with " << relayID->getSize() << " bytes." << LogEnd;
}
if (RelCfgMgr().getClientLinkLayerAddress()) {
SPtr<TOpt> lladdr = getClientLinkLayerAddr(msg);
if (lladdr) {
Log(Debug) << "Appended client link-layer address option with "
<< lladdr->getSize() << " bytes." << LogEnd;
lladdr->storeSelf(buf + offset);
offset += lladdr->getSize();
}
}
SPtr<TRelOptEcho> echo = RelCfgMgr().getEcho();
if (echo) {
echo->storeSelf(buf+offset);
offset += echo->getSize();
Log(Debug) << "Appended EchoRequest option with ";
int i=0;
char tmpBuf[256];
for (i=0;i<255;i++)
tmpBuf[i] = 255-i;
for (int i=0; i<echo->count(); i++) {
int code = echo->getReqOpt(i);
SPtr<TRelOptGeneric> gen = new TRelOptGeneric(code, tmpBuf, 4, 0);
gen->storeSelf(buf+offset);
offset += gen->getSize();
Log(Cont) << code << " ";
}
Log(Cont) << " opt(s)." << LogEnd;
}
RelCfgMgr().firstIface();
while (cfgIface = RelCfgMgr().getIface()) {
if (cfgIface->getServerUnicast()) {
Log(Notice) << "Relaying encapsulated " << msg->getName() << " message on the "
<< cfgIface->getFullName() << " interface to unicast ("
<< cfgIface->getServerUnicast()->getPlain() << ") address, port "
<< DHCPSERVER_PORT << "." << LogEnd;
if (!RelIfaceMgr().send(cfgIface->getID(), buf, offset,
cfgIface->getServerUnicast(), DHCPSERVER_PORT)) {
Log(Error) << "Failed to send data to server unicast address." << LogEnd;
}
}
if (cfgIface->getServerMulticast()) {
addr = new TIPv6Addr(ALL_DHCP_SERVERS, true);
Log(Notice) << "Relaying encapsulated " << msg->getName() << " message on the "
<< cfgIface->getFullName() << " interface to multicast ("
<< addr->getPlain() << ") address, port " << DHCPSERVER_PORT
<< "." << LogEnd;
if (!RelIfaceMgr().send(cfgIface->getID(), buf, offset, addr, DHCPSERVER_PORT)) {
Log(Error) << "Failed to send data to server multicast address." << LogEnd;
}
}
}
// save DB state regardless of action taken
RelCfgMgr().dump();
}
void TRelTransMgr::relayMsgRepl(SPtr<TRelMsg> msg) {
int port;
SPtr<TRelCfgIface> cfgIface = RelCfgMgr().getIfaceByInterfaceID(msg->getDestIface());
if (!cfgIface) {
Log(Error) << "Unable to relay message: Invalid interfaceID value:"
<< msg->getDestIface() << LogEnd;
return;
}
SPtr<TIfaceIface> iface = RelIfaceMgr().getIfaceByID(cfgIface->getID());
SPtr<TIPv6Addr> addr = msg->getDestAddr();
static char buf[MAX_PACKET_LEN];
int bufLen;
if (!iface) {
Log(Warning) << "Unable to find interface with interfaceID=" << msg->getDestIface()
<< LogEnd;
return;
}
bufLen = msg->storeSelf(buf);
if (msg->getType() == RELAY_REPL_MSG)
port = DHCPSERVER_PORT;
else
port = DHCPCLIENT_PORT;
Log(Notice) << "Relaying decapsulated " << msg->getName() << " message on the "
<< iface->getFullName() << " interface to the " << addr->getPlain()
<< ", port " << port << "." << LogEnd;
if (!RelIfaceMgr().send(iface->getID(), buf, bufLen, addr, port)) {
Log(Error) << "Failed to decapsulated data." << LogEnd;
}
}
SPtr<TOpt> TRelTransMgr::getLinkAddrFromDuid(SPtr<TOpt> duid_opt) {
if (!duid_opt)
return TOptPtr(); // NULL
// We need at least option header and duid type
if (duid_opt->getSize() < 6)
return TOptPtr(); // NULL
// Create a vector and store whole DUID there.
std::vector<uint8_t> buffer(duid_opt->getSize(), 0);
duid_opt->storeSelf((char*)&buffer[0]);
char* buf = (char*)&buffer[0];
buf += sizeof(uint16_t); // skip first 2 bytes (option code)
uint16_t len = readUint16(buf); // read option length
buf += sizeof(uint16_t);
// stored length must be total option size, without the header
if (len + 4u != duid_opt->getSize())
return TOptPtr(); // NULL
uint16_t duid_type = readUint16(buf); // read duid type
buf += sizeof(uint16_t);
len -= sizeof(uint16_t);
std::vector<uint8_t> linkaddr;
switch (duid_type) {
case DUID_TYPE_LLT: {
// 7: hw type (16 bits), time (32 bits), at least 1 byte of MAC
if (len < 7) {
return TOptPtr(); // NULL
}
// Read hardware type
uint16_t hw_type = readUint16(buf);
buf += sizeof(uint16_t);
len -= sizeof(uint16_t);
// Skip duid creation time
buf += sizeof(uint32_t);
len -= sizeof(uint32_t);
// Now store hardware type + MAC
linkaddr.resize(len + sizeof(uint16_t));
char* out = (char*)&linkaddr[0];
out = writeUint16(out, hw_type); // hw type (2)
memcpy(out, buf, len);
return SPtr<TOpt>(new TOptGeneric(OPTION_CLIENT_LINKLAYER_ADDR,
(const char*)&linkaddr[0], linkaddr.size(),
NULL));
}
case DUID_TYPE_LL: {
// 3: hw type (16 bits), at least 1 byte of MAC
if (len < 3) {
return TOptPtr(); // NULL
}
// Read hardware type
uint16_t hw_type = readUint16(buf);
buf += sizeof(uint16_t);
len -= sizeof(uint16_t);
// Now store hardware type + MAC
linkaddr.resize(len + sizeof(uint16_t));
char* out = (char*)&linkaddr[0];
out = writeUint16(out, hw_type);
memcpy(out, buf, len);
return SPtr<TOpt>(new TOptGeneric(OPTION_CLIENT_LINKLAYER_ADDR,
(const char*)&linkaddr[0], linkaddr.size(),
NULL));
}
default:
return TOptPtr(); // NULL
}
}
SPtr<TOpt> TRelTransMgr::getLinkAddrFromSrcAddr(SPtr<TRelMsg> msg) {
SPtr<TIPv6Addr> srcAddr = msg->getRemoteAddr();
if (!srcAddr || !srcAddr->linkLocal())
return TOptPtr(); // NULL
std::vector<uint8_t> mac(8,0);
// store hardware type
SPtr<TIfaceIface> iface = RelIfaceMgr().getIfaceByID(msg->getIface());
if (!iface) {
// Should never happen
return TOptPtr(); // NULL
}
writeUint16((char*)&mac[0], iface->getHardwareType());
// now extract MAC address from the source address
char* addr = srcAddr->getAddr();
if ( (addr[11] != 0xff) || (addr[12] != 0xfe) ) {
return TOptPtr(); // NULL
}
memcpy(&mac[2], addr + 8, 3);
memcpy(&mac[5], addr + 13, 3);
// Ok, create the option and return it
return SPtr<TOpt>(new TOptGeneric(OPTION_CLIENT_LINKLAYER_ADDR,
(char*)&mac[0], 8, NULL));
}
SPtr<TOpt> TRelTransMgr::getClientLinkLayerAddr(SPtr<TRelMsg> msg) {
// Ignore messages that are not directly from client
if ( (msg->getType() == RELAY_FORW_MSG) ||
(msg->getType() == RELAY_REPL_MSG))
return TOptPtr(); // NULL
// Ok, this seems to be a message from a client. Let's try to
// extract its DUID
SPtr<TOpt> opt = msg->getOption(OPTION_CLIENTID);
if (opt) {
SPtr<TOpt> linkaddr = getLinkAddrFromDuid(opt);
if (linkaddr) {
return linkaddr;
}
}
// Ok, let's try to extract the MAC address from source link-local
// address.
return getLinkAddrFromSrcAddr(msg);
}
void TRelTransMgr::shutdown() {
IsDone = true;
}
bool TRelTransMgr::isDone() {
return this->IsDone;
}
bool TRelTransMgr::doDuties() {
return false;
}
char* TRelTransMgr::getCtrlAddr() {
return this->ctrlAddr;
}
int TRelTransMgr::getCtrlIface() {
return this->ctrlIface;
}
void TRelTransMgr::dump() {
std::ofstream xmlDump;
xmlDump.open(this->XmlFile.c_str());
xmlDump << *this;
xmlDump.close();
}
TRelTransMgr::~TRelTransMgr() {
Log(Debug) << "RelTransMgr cleanup." << LogEnd;
}
void TRelTransMgr::instanceCreate(const std::string& xmlFile)
{
if (Instance)
Log(Crit) << "RelTransMgr instance already created. Application error!" << LogEnd;
Instance = new TRelTransMgr(xmlFile);
}
TRelTransMgr& TRelTransMgr::instance()
{
if (!Instance) {
Log(Crit) << "RelTransMgr istance not created yet. Application error. Emergency shutdown." << LogEnd;
exit(EXIT_FAILURE);
}
return *Instance;
}
std::ostream & operator<<(std::ostream &s, TRelTransMgr &x)
{
s << "<TRelTransMgr>" << std::endl;
s << "<!-- RelTransMgr dumps are not implemented yet -->" << std::endl;
s << "</TRelTransMgr>" << std::endl;
return s;
}
// Stub definitions, added because they are called in TMsg::storeSelf()
extern "C" {
void *hmac_sha (const char *buffer, size_t len, char *key, size_t key_len, char *resbuf, int type) {
return NULL;
}
void *hmac_md5 (const char *buffer, size_t len, char *key, size_t key_len, char *resbuf) {
return NULL;
}
}
|