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
|
/*
* Dibbler - a portable DHCPv6
*
* authors: Tomasz Mrugalski <thomson@klub.com.pl>
* Marek Senderski <msend@o2.pl>
* changes: Krzysztof Wnuk <keczi@poczta.onet.pl>
* Nguyen Vinh Nghiem
*
* released under GNU GPL v2 only licence
*
*/
#ifdef WIN32
#include <winsock2.h>
#endif
#ifdef LINUX
#include <netinet/in.h>
#endif
#include <sstream>
#include "SrvOptIA_PD.h"
#include "SrvOptIAPrefix.h"
#include "SrvOptStatusCode.h"
#include "SrvOptRapidCommit.h"
#include "Logger.h"
#include "AddrClient.h"
#include "DHCPConst.h"
TSrvOptIA_PD::TSrvOptIA_PD( long IAID, long T1, long T2, TMsg* parent)
:TOptIA_PD(IAID,T1,T2, parent) {
this->IAID=IAID;
this->T1=T1;
this->T2=T2;
}
TSrvOptIA_PD::TSrvOptIA_PD( long IAID, long T1, long T2, int Code, string Text, TMsg* parent)
:TOptIA_PD(IAID,T1,T2, parent) {
SubOptions.append(new TSrvOptStatusCode(Code, Text, parent));
}
/*
* Create IA_PD option based on receive buffer
*/
TSrvOptIA_PD::TSrvOptIA_PD( char * buf, int bufsize, TMsg* parent)
:TOptIA_PD(buf,bufsize, parent) {
int pos=0;
while(pos<bufsize)
{
int code=buf[pos]*256+buf[pos+1];
pos+=2;
int length=buf[pos]*256+buf[pos+1];
pos+=2;
if ((code>0)&&(code<=26)) // was 24
{
if(allowOptInOpt(parent->getType(),OPTION_IA_PD,code)) {
SmartPtr<TOpt> opt;
opt = SmartPtr<TOpt>(); /* NULL */
switch (code)
{
case OPTION_IAPREFIX:
opt = (Ptr*)SmartPtr<TSrvOptIAPrefix>
(new TSrvOptIAPrefix(buf+pos,length,this->Parent));
break;
case OPTION_STATUS_CODE:
opt = (Ptr*)SmartPtr<TSrvOptStatusCode>
(new TSrvOptStatusCode(buf+pos,length,this->Parent));
break;
default:
Log(Warning) <<"Option " << code<< "not supported "
<<" in message (type=" << parent->getType()
<<") in this version of server." << LogEnd;
break;
}
if((opt)&&(opt->isValid()))
SubOptions.append(opt);
}
else {
Log(Warning) << "Illegal option received (type=" << code
<< ") in an IA_PD option." << LogEnd;
}
}
else {
Log(Warning) << "Unknown option received (type=" << code
<< ") in an IA_PD option." << LogEnd;
};
pos+=length;
}
}
void TSrvOptIA_PD::releaseAllPrefixes(bool quiet) {
SmartPtr<TOpt> opt;
SmartPtr<TIPv6Addr> prefix;
SmartPtr<TOptIAPrefix> optPrefix;
this->firstOption();
while ( opt = this->getOption() ) {
if (opt->getOptType() != OPTION_IAPREFIX)
continue;
optPrefix = (Ptr*) opt;
prefix = optPrefix->getPrefix();
//AddrMgr->delClntAddr(this->ClntDuid, this->IAID, prefix, quiet); // not sure about that
CfgMgr->decrPrefixCount(this->Iface, prefix);
}
}
/**
* gets one (or more) prefix for requested
*
* @param hint
* @param length
* @param pref
* @param valid
* @param fake
*
* @return status, if it was possible to assign something (STATUSCODE_SUCCESS)
*/
int TSrvOptIA_PD::assignPrefix(SmartPtr<TIPv6Addr> hint, bool fake) {
SmartPtr<TIPv6Addr> prefix;
SmartPtr<TSrvOptIAPrefix> optPrefix;
SmartPtr<TSrvCfgPD> ptrPD;
List(TIPv6Addr) prefixLst;
// get address
prefixLst.clear();
prefixLst = this->getFreePrefixes(hint);
ostringstream buf;
bool alreadyIncreased = false;
prefixLst.first();
while (prefix = prefixLst.get()) {
buf << prefix->getPlain() << "/" << this->PDLength << " ";
optPrefix = new TSrvOptIAPrefix(prefix, this->PDLength, this->Prefered, this->Valid, this->Parent);
SubOptions.append((Ptr*)optPrefix);
if (!fake) {
// every prefix has to be remembered in AddrMgr, e.g. when there are 2 pools defined,
// prefixLst contains entries from each pool, so 2 prefixes has to be remembered
AddrMgr->addPrefix(this->ClntDuid, this->ClntAddr, this->Iface, this->IAID, this->T1, this->T2,
prefix, this->Prefered, this->Valid, this->PDLength, false);
if (!alreadyIncreased) {
// but CfgMgr has to increase usage only once. Don't ask my why :)
CfgMgr->incrPrefixCount(Iface, prefix);
alreadyIncreased = true;
}
}
}
Log(Info) << "PD:" << (fake?"(would be)":"") << " assigned prefix(es):" << buf.str() << LogEnd;
if (prefixLst.count()) {
return STATUSCODE_SUCCESS;
} else
return STATUSCODE_NOPREFIXAVAIL;
}
// so far it is enough here
// constructor used only in RENEW, REBIND, DECLINE and RELEASE
TSrvOptIA_PD::TSrvOptIA_PD( SmartPtr<TSrvCfgMgr> cfgMgr,
SmartPtr<TSrvAddrMgr> addrMgr,
SmartPtr<TSrvOptIA_PD> queryOpt,
SmartPtr<TIPv6Addr> clntAddr, SmartPtr<TDUID> clntDuid,
int iface, int msgType , TMsg* parent)
:TOptIA_PD(queryOpt->getIAID(), 0x7fffffff, 0x7fffffff, parent)
{
this->AddrMgr = addrMgr;
this->CfgMgr = cfgMgr;
this->ClntDuid = clntDuid;
this->ClntAddr = clntAddr;
this->Iface = iface;
SPtr<TSrvCfgIface> ptrIface = CfgMgr->getIfaceByID(Iface);
if (!ptrIface) {
Log(Error) << "Unable to find interface with ifindex=" << Iface << ". Something is wrong, VERY wrong." << LogEnd;
return;
}
// is the prefix delegation supported?
if ( !ptrIface->supportPrefixDelegation() ) {
SmartPtr<TSrvOptStatusCode> ptrStatus;
ptrStatus = new TSrvOptStatusCode(STATUSCODE_NOPREFIXAVAIL,
"Server support for prefix delegation is not enabled. Sorry buddy.",this->Parent);
this->SubOptions.append((Ptr*)ptrStatus);
return;
}
bool fake = false; // is this assignment for real?
if (msgType == SOLICIT_MSG)
fake = true;
if (parent->getOption(OPTION_RAPID_COMMIT))
fake = false;
switch (msgType) {
case SOLICIT_MSG:
this->solicitRequest(queryOpt, ptrIface, fake);
break;
case REQUEST_MSG:
this->solicitRequest(queryOpt, ptrIface, fake);
break;
case RENEW_MSG:
this->renew(queryOpt, ptrIface);
break;
case REBIND_MSG:
this->rebind(queryOpt, ptrIface);
break;
case RELEASE_MSG:
this->release(queryOpt, ptrIface);
break;
case CONFIRM_MSG:
this->confirm(queryOpt, ptrIface);
break;
case DECLINE_MSG:
this->decline(queryOpt, ptrIface);
break;
default: {
Log(Warning) << "Unknown message type (" << msgType
<< "). Cannot generate OPTION_PD."<< LogEnd;
SubOptions.append(new TSrvOptStatusCode(STATUSCODE_UNSPECFAIL,
"Unknown message type.",this->Parent));
break;
}
}
}
/**
* this method is used to prepare response to IA_PD received in SOLICIT and REQUEST messages
* (i.e. it tries to assign prefix as requested by client)
*
* @param queryOpt
* @param ptrIface
* @param fake
*/
void TSrvOptIA_PD::solicitRequest(SPtr<TSrvOptIA_PD> queryOpt, SPtr<TSrvCfgIface> ptrIface, bool fake) {
// --- Is this PD without IAPREFIX options? ---
SPtr<TIPv6Addr> hint = 0;
if (!queryOpt->countPrefixes()) {
Log(Notice) << "PD option (with IAPREFIX suboptions missing) received. " << LogEnd;
hint = new TIPv6Addr(); /* :: - any address */
this->Prefered = DHCPV6_INFINITY;
this->Valid = DHCPV6_INFINITY;
} else {
SPtr<TSrvOptIAPrefix> hintPrefix = (Ptr*) queryOpt->getOption(OPTION_IAPREFIX);
hint = hintPrefix->getPrefix();
Log(Info) << "PD: PD option with " << hint->getPlain() << " as a hint received." << LogEnd;
this->Prefered = hintPrefix->getPref();
this->Valid = hintPrefix->getValid();
}
// assign prefixes
int status = this->assignPrefix(hint, fake);
// include status code
SmartPtr<TSrvOptStatusCode> ptrStatus;
if (status==STATUSCODE_SUCCESS) {
ostringstream tmp;
tmp << countPrefixes() << " prefix(es) granted.";
ptrStatus = new TSrvOptStatusCode(STATUSCODE_SUCCESS, tmp.str(), this->Parent);
} else {
ptrStatus = new TSrvOptStatusCode(status,
"Unable to provide any prefixes. Sorry.", this->Parent);
}
this->SubOptions.append((Ptr*)ptrStatus);
return;
}
/**
* generate OPTION_PD based on OPTION_PD received in RENEW message
*
* @param queryOpt - IA_PD option in the RENEW message
* @param addrCount
*/
void TSrvOptIA_PD::renew(SPtr<TSrvOptIA_PD> queryOpt, SPtr<TSrvCfgIface> iface) {
SmartPtr <TAddrClient> ptrClient;
ptrClient = this->AddrMgr->getClient(this->ClntDuid);
if (!ptrClient) {
SubOptions.append(new TSrvOptStatusCode(STATUSCODE_NOBINDING,"Who are you? Do I know you?",
this->Parent));
return;
}
// find that IA
SmartPtr <TAddrIA> ptrIA;
ptrIA = ptrClient->getPD(this->IAID);
if (!ptrIA) {
SubOptions.append(new TSrvOptStatusCode(STATUSCODE_NOBINDING,"I see this IAID first time.",
this->Parent ));
return;
}
// everything seems ok, update data in addrdb
ptrIA->setTimestamp();
this->T1 = ptrIA->getT1();
this->T2 = ptrIA->getT2();
// send addr info to client
SmartPtr<TAddrPrefix> prefix;
ptrIA->firstPrefix();
while ( prefix = ptrIA->getPrefix() ) {
SmartPtr<TSrvOptIAPrefix> optPrefix;
prefix->setTimestamp();
optPrefix = new TSrvOptIAPrefix(prefix->get(), prefix->getLength(), prefix->getPref(),
prefix->getValid(), this->Parent);
SubOptions.append( (Ptr*)optPrefix );
}
// finally send greetings and happy OK status code
SmartPtr<TSrvOptStatusCode> ptrStatus;
ptrStatus = new TSrvOptStatusCode(STATUSCODE_SUCCESS,"Prefix(es) renewed.", this->Parent);
SubOptions.append( (Ptr*)ptrStatus );
}
void TSrvOptIA_PD::rebind(SPtr<TSrvOptIA_PD> queryOpt, SPtr<TSrvCfgIface> iface) {
// FIXME: implement PD support in REBIND message
}
void TSrvOptIA_PD::release(SPtr<TSrvOptIA_PD> queryOpt, SPtr<TSrvCfgIface> iface) {
// FIXME: implement PD support in RELEASE message
}
void TSrvOptIA_PD::confirm(SPtr<TSrvOptIA_PD> queryOpt, SPtr<TSrvCfgIface> iface) {
// FIXME: implement PD support in CONFIRM message
}
void TSrvOptIA_PD::decline(SPtr<TSrvOptIA_PD> queryOpt, SPtr<TSrvCfgIface> iface) {
// FIXME: implement PD support in DECLINE message
}
bool TSrvOptIA_PD::doDuties() {
return true;
}
/**
* return free prefixes for a client. There are several ways that method may work:
* 1 - client didn't provide any hints:
* => one prefix from each pool will be granted
* 2 - client has provided hint and that is valid (supported and unused):
* => requested prefix will be granted
* 3 - client has provided hint, which belongs to supported pool, but this prefix is used:
* => other prefix from that pool will be asigned
* 4 - client has provided hint, but it is invalid (not beloninging to a supported pool,
* multicast or link-local):
* => see 1
*
* @param hint - hint provided by client (or ::)
*
* @return - list of prefixes
*/
List(TIPv6Addr) TSrvOptIA_PD::getFreePrefixes(SmartPtr<TIPv6Addr> hint) {
SmartPtr<TSrvCfgIface> ptrIface;
SmartPtr<TIPv6Addr> prefix;
SmartPtr<TSrvCfgPD> ptrPD;
bool validHint = true;
List(TIPv6Addr) lst;
lst.clear();
ptrIface = this->CfgMgr->getIfaceByID(this->Iface);
if (!ptrIface) {
Log(Error) << "PD: Trying to find free prefix on non-existent interface (ifindex=" << this->Iface << ")." << LogEnd;
return lst; // empty list
}
if (!ptrIface->supportPrefixDelegation()) {
// this method should not be called anyway
Log(Error) << "PD: Prefix delegation is not supported on the " << ptrIface->getFullName() << "." << LogEnd;
return lst; // empty list
}
ptrIface->firstPD();
ptrPD = ptrIface->getPD();
if (ptrPD->getAssignedCount() >= ptrPD->getTotalCount()) { // should be ==, asigned>total should never happen
Log(Error) << "PD: Unable to grant any prefixes: Already asigned " << ptrPD->getAssignedCount()
<< " out of " << ptrPD->getTotalCount() << "." << LogEnd;
return lst; // empty list
}
// check if this prefix is ok
// is it anyaddress (::)?
SmartPtr<TIPv6Addr> anyaddr = new TIPv6Addr();
if (*anyaddr==*hint) {
Log(Debug) << "PD: Client requested unspecified (" << *hint
<< ") prefix. Hint ignored." << LogEnd;
validHint = false;
}
// is it multicast address (ff...)?
if ((*(hint->getAddr()))==0xff) {
Log(Debug) << "PD: Client requested multicast (" << *hint
<< ") prefix. Hint ignored." << LogEnd;
validHint = false;
}
// is it link-local address (fe80::...)?
char linklocal[]={0xfe, 0x80};
if (!memcmp(hint->getAddr(),linklocal,2)) {
Log(Debug) << "PD: Client requested link-local (" << *hint << ") prefix. Hint ignored." << LogEnd;
validHint = false;
}
// Get the request message from client
SmartPtr<TSrvTransMgr> srvTrans = ((TSrvMsg*)Parent)->SrvTransMgr;
SmartPtr<TSrvMsg> requestMsg = (Ptr*)(srvTrans->requestMsg);
if ( validHint ) {
// hint is valid, try to use it
ptrPD = this->CfgMgr->getClassByPrefix(this->Iface, hint);
// if the PD allow the hint, based on DUID, Addr, and Msg from client
if (ptrPD && ptrPD->clntSupported(ClntDuid, ClntAddr, requestMsg ))
{
// case 2: address belongs to supported class, and is free
if ( ptrPD && AddrMgr->prefixIsFree(hint) ) {
Log(Debug) << "PD: Requested prefix (" << *hint << ") is free, great!" << LogEnd;
this->PDLength = ptrPD->getPD_Length();
this->Prefered = ptrPD->getPrefered(this->Prefered);
this->Valid = ptrPD->getValid(this->Valid);
this->T1 = ptrPD->getT1(this->T1);
this->T2 = ptrPD->getT2(this->T2);
lst.append(hint);
return lst;
}
// case 3: hint is used, but we can assign another prefix from the same pool
if (ptrPD) {
do {
prefix=ptrPD->getRandomPrefix();
} while (!this->AddrMgr->prefixIsFree(prefix));
lst.append(prefix);
this->PDLength = ptrPD->getPD_Length();
this->Prefered = ptrPD->getPrefered(this->Prefered);
this->Valid = ptrPD->getValid(this->Valid);
this->T1 = ptrPD->getT1(this->T1);
this->T2 = ptrPD->getT2(this->T2);
return lst;
} // if hint is used
} // if the PD support hint, based on Client Class
} // if valid hint
// case 1: no hint provided, assign one prefix from each pool
// case 4: provided hint does not belong to supported class or is useless (multicast,link-local, ::)
ptrIface->firstPD();
while ( ptrPD = ptrIface->getPD())
{
if (!ptrPD->clntSupported(ClntDuid, ClntAddr, requestMsg ))
continue;
break;
}
if (!ptrPD)
{
Log(Warning) << "Unable to find any PD for this client." << LogEnd;
return lst; // return empty list
}
while (true) {
List(TIPv6Addr) lst;
lst = ptrPD->getRandomList();
lst.first();
bool allFree = true;
while (prefix = lst.get()) {
if (!AddrMgr->prefixIsFree(prefix)) {
allFree = false;
}
}
if (allFree) {
this->PDLength = ptrPD->getPD_Length();
this->Prefered = ptrPD->getPrefered(this->Prefered);
this->Valid = ptrPD->getValid(this->Valid);
this->T1 = ptrPD->getT1(this->T1);
this->T2 = ptrPD->getT2(this->T2);
return lst;
}
};
}
|