pkt6.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <dhcp/dhcp6.h>
  16. #include <dhcp/libdhcp++.h>
  17. #include <dhcp/option.h>
  18. #include <dhcp/option_vendor_class.h>
  19. #include <dhcp/option_vendor.h>
  20. #include <dhcp/pkt6.h>
  21. #include <dhcp/docsis3_option_defs.h>
  22. #include <util/io_utilities.h>
  23. #include <exceptions/exceptions.h>
  24. #include <dhcp/duid.h>
  25. #include <dhcp/iface_mgr.h>
  26. #include <iostream>
  27. #include <sstream>
  28. using namespace std;
  29. using namespace isc::asiolink;
  30. /// @brief Default address used in Pkt6 constructor
  31. const IOAddress DEFAULT_ADDRESS6("::");
  32. namespace isc {
  33. namespace dhcp {
  34. Pkt6::RelayInfo::RelayInfo()
  35. :msg_type_(0), hop_count_(0), linkaddr_(DEFAULT_ADDRESS6),
  36. peeraddr_(DEFAULT_ADDRESS6), relay_msg_len_(0) {
  37. }
  38. Pkt6::Pkt6(const uint8_t* buf, uint32_t buf_len, DHCPv6Proto proto /* = UDP */)
  39. :Pkt(buf, buf_len, DEFAULT_ADDRESS6, DEFAULT_ADDRESS6, 0, 0),
  40. proto_(proto), msg_type_(0) {
  41. }
  42. Pkt6::Pkt6(uint8_t msg_type, uint32_t transid, DHCPv6Proto proto /*= UDP*/)
  43. :Pkt(transid, DEFAULT_ADDRESS6, DEFAULT_ADDRESS6, 0, 0), proto_(proto),
  44. msg_type_(msg_type) {
  45. }
  46. size_t Pkt6::len() {
  47. if (relay_info_.empty()) {
  48. return (directLen());
  49. } else {
  50. // Unfortunately we need to re-calculate relay size every time, because
  51. // we need to make sure that once a new option is added, its extra size
  52. // is reflected in Pkt6::len().
  53. calculateRelaySizes();
  54. return (relay_info_[0].relay_msg_len_ + getRelayOverhead(relay_info_[0]));
  55. }
  56. }
  57. OptionPtr Pkt6::getAnyRelayOption(uint16_t opt_type, RelaySearchOrder order) {
  58. if (relay_info_.empty()) {
  59. // There's no relay info, this is a direct message
  60. return (OptionPtr());
  61. }
  62. int start = 0; // First relay to check
  63. int end = 0; // Last relay to check
  64. int direction = 0; // How we going to iterate: forward or backward?
  65. switch (order) {
  66. case RELAY_SEARCH_FROM_CLIENT:
  67. // Search backwards
  68. start = relay_info_.size() - 1;
  69. end = 0;
  70. direction = -1;
  71. break;
  72. case RELAY_SEARCH_FROM_SERVER:
  73. // Search forward
  74. start = 0;
  75. end = relay_info_.size() - 1;
  76. direction = 1;
  77. break;
  78. case RELAY_GET_FIRST:
  79. // Look at the innermost relay only
  80. start = relay_info_.size() - 1;
  81. end = start;
  82. direction = 1;
  83. break;
  84. case RELAY_GET_LAST:
  85. // Look at the outermost relay only
  86. start = 0;
  87. end = 0;
  88. direction = 1;
  89. }
  90. // This is a tricky loop. It must go from start to end, but it must work in
  91. // both directions (start > end; or start < end). We can't use regular
  92. // exit condition, because we don't know whether to use i <= end or i >= end.
  93. // That's why we check if in the next iteration we would go past the
  94. // list (end + direction). It is similar to STL concept of end pointing
  95. // to a place after the last element
  96. for (int i = start; i != end + direction; i += direction) {
  97. OptionPtr opt = getRelayOption(opt_type, i);
  98. if (opt) {
  99. return (opt);
  100. }
  101. }
  102. // We iterated over specified relays and haven't found what we were
  103. // looking for
  104. return (OptionPtr());
  105. }
  106. OptionPtr Pkt6::getRelayOption(uint16_t opt_type, uint8_t relay_level) {
  107. if (relay_level >= relay_info_.size()) {
  108. isc_throw(OutOfRange, "This message was relayed " << relay_info_.size() << " time(s)."
  109. << " There is no info about " << relay_level + 1 << " relay.");
  110. }
  111. for (OptionCollection::iterator it = relay_info_[relay_level].options_.begin();
  112. it != relay_info_[relay_level].options_.end(); ++it) {
  113. if ((*it).second->getType() == opt_type) {
  114. return (it->second);
  115. }
  116. }
  117. return (OptionPtr());
  118. }
  119. uint16_t Pkt6::getRelayOverhead(const RelayInfo& relay) const {
  120. uint16_t len = DHCPV6_RELAY_HDR_LEN // fixed header
  121. + Option::OPTION6_HDR_LEN; // header of the relay-msg option
  122. for (OptionCollection::const_iterator opt = relay.options_.begin();
  123. opt != relay.options_.end(); ++opt) {
  124. len += (opt->second)->len();
  125. }
  126. return (len);
  127. }
  128. uint16_t Pkt6::calculateRelaySizes() {
  129. uint16_t len = directLen(); // start with length of all options
  130. for (int relay_index = relay_info_.size(); relay_index > 0; --relay_index) {
  131. relay_info_[relay_index - 1].relay_msg_len_ = len;
  132. len += getRelayOverhead(relay_info_[relay_index - 1]);
  133. }
  134. return (len);
  135. }
  136. uint16_t Pkt6::directLen() const {
  137. uint16_t length = DHCPV6_PKT_HDR_LEN; // DHCPv6 header
  138. for (OptionCollection::const_iterator it = options_.begin();
  139. it != options_.end();
  140. ++it) {
  141. length += (*it).second->len();
  142. }
  143. return (length);
  144. }
  145. void
  146. Pkt6::pack() {
  147. switch (proto_) {
  148. case UDP:
  149. packUDP();
  150. break;
  151. case TCP:
  152. packTCP();
  153. break;
  154. default:
  155. isc_throw(BadValue, "Invalid protocol specified (non-TCP, non-UDP)");
  156. }
  157. }
  158. void
  159. Pkt6::packUDP() {
  160. try {
  161. // Make sure that the buffer is empty before we start writting to it.
  162. buffer_out_.clear();
  163. // is this a relayed packet?
  164. if (!relay_info_.empty()) {
  165. // calculate size needed for each relay (if there is only one relay,
  166. // then it will be equal to "regular" length + relay-forw header +
  167. // size of relay-msg option header + possibly size of interface-id
  168. // option (if present). If there is more than one relay, the whole
  169. // process is called iteratively for each relay.
  170. calculateRelaySizes();
  171. // Now for each relay, we need to...
  172. for (vector<RelayInfo>::iterator relay = relay_info_.begin();
  173. relay != relay_info_.end(); ++relay) {
  174. // build relay-forw/relay-repl header (see RFC3315, section 7)
  175. buffer_out_.writeUint8(relay->msg_type_);
  176. buffer_out_.writeUint8(relay->hop_count_);
  177. buffer_out_.writeData(&(relay->linkaddr_.toBytes()[0]),
  178. isc::asiolink::V6ADDRESS_LEN);
  179. buffer_out_.writeData(&relay->peeraddr_.toBytes()[0],
  180. isc::asiolink::V6ADDRESS_LEN);
  181. // store every option in this relay scope. Usually that will be
  182. // only interface-id, but occasionally other options may be
  183. // present here as well (vendor-opts for Cable modems,
  184. // subscriber-id, remote-id, options echoed back from Echo
  185. // Request Option, etc.)
  186. for (OptionCollection::const_iterator opt =
  187. relay->options_.begin();
  188. opt != relay->options_.end(); ++opt) {
  189. (opt->second)->pack(buffer_out_);
  190. }
  191. // and include header relay-msg option. Its payload will be
  192. // generated in the next iteration (if there are more relays)
  193. // or outside the loop (if there are no more relays and the
  194. // payload is a direct message)
  195. buffer_out_.writeUint16(D6O_RELAY_MSG);
  196. buffer_out_.writeUint16(relay->relay_msg_len_);
  197. }
  198. }
  199. // DHCPv6 header: message-type (1 octect) + transaction id (3 octets)
  200. buffer_out_.writeUint8(msg_type_);
  201. // store 3-octet transaction-id
  202. buffer_out_.writeUint8( (transid_ >> 16) & 0xff );
  203. buffer_out_.writeUint8( (transid_ >> 8) & 0xff );
  204. buffer_out_.writeUint8( (transid_) & 0xff );
  205. // the rest are options
  206. LibDHCP::packOptions(buffer_out_, options_);
  207. }
  208. catch (const Exception& e) {
  209. // An exception is thrown and message will be written to Logger
  210. isc_throw(InvalidOperation, e.what());
  211. }
  212. }
  213. void
  214. Pkt6::packTCP() {
  215. /// TODO Implement this function.
  216. isc_throw(NotImplemented, "DHCPv6 over TCP (bulk leasequery and failover)"
  217. "not implemented yet.");
  218. }
  219. void
  220. Pkt6::unpack() {
  221. switch (proto_) {
  222. case UDP:
  223. return unpackUDP();
  224. case TCP:
  225. return unpackTCP();
  226. default:
  227. isc_throw(BadValue, "Invalid protocol specified (non-TCP, non-UDP)");
  228. }
  229. }
  230. void
  231. Pkt6::unpackUDP() {
  232. if (data_.size() < 4) {
  233. isc_throw(BadValue, "Received truncated UDP DHCPv6 packet of size "
  234. << data_.size() << ", DHCPv6 header alone has 4 bytes.");
  235. }
  236. msg_type_ = data_[0];
  237. switch (msg_type_) {
  238. case DHCPV6_SOLICIT:
  239. case DHCPV6_ADVERTISE:
  240. case DHCPV6_REQUEST:
  241. case DHCPV6_CONFIRM:
  242. case DHCPV6_RENEW:
  243. case DHCPV6_REBIND:
  244. case DHCPV6_REPLY:
  245. case DHCPV6_DECLINE:
  246. case DHCPV6_RECONFIGURE:
  247. case DHCPV6_INFORMATION_REQUEST:
  248. default: // assume that uknown messages are not using relay format
  249. {
  250. return (unpackMsg(data_.begin(), data_.end()));
  251. }
  252. case DHCPV6_RELAY_FORW:
  253. case DHCPV6_RELAY_REPL:
  254. return (unpackRelayMsg());
  255. }
  256. }
  257. void
  258. Pkt6::unpackMsg(OptionBuffer::const_iterator begin,
  259. OptionBuffer::const_iterator end) {
  260. size_t size = std::distance(begin, end);
  261. if (size < 4) {
  262. // truncated message (less than 4 bytes)
  263. isc_throw(BadValue, "Received truncated UDP DHCPv6 packet of size "
  264. << data_.size() << ", DHCPv6 header alone has 4 bytes.");
  265. }
  266. msg_type_ = *begin++;
  267. transid_ = ( (*begin++) << 16 ) +
  268. ((*begin++) << 8) + (*begin++);
  269. transid_ = transid_ & 0xffffff;
  270. // See below about invoking Postel's law, as we aren't using
  271. // size we don't need to update it. If we do so in the future
  272. // perhaps for stats gathering we can uncomment this.
  273. // size -= sizeof(uint32_t); // We just parsed 4 bytes header
  274. OptionBuffer opt_buffer(begin, end);
  275. // If custom option parsing function has been set, use this function
  276. // to parse options. Otherwise, use standard function from libdhcp.
  277. size_t offset;
  278. if (callback_.empty()) {
  279. offset = LibDHCP::unpackOptions6(opt_buffer, "dhcp6", options_);
  280. } else {
  281. // The last two arguments hold the DHCPv6 Relay message offset and
  282. // length. Setting them to NULL because we are dealing with the
  283. // not-relayed message.
  284. offset = callback_(opt_buffer, "dhcp6", options_, NULL, NULL);
  285. }
  286. // If offset is not equal to the size, then something is wrong here. We
  287. // either parsed past input buffer (bug in our code) or we haven't parsed
  288. // everything (received trailing garbage or truncated option).
  289. //
  290. // Invoking Jon Postel's law here: be conservative in what you send, and be
  291. // liberal in what you accept. There's no easy way to log something from
  292. // libdhcp++ library, so we just choose to be silent about remaining
  293. // bytes. We also need to quell compiler warning about unused offset
  294. // variable.
  295. //
  296. // if (offset != size) {
  297. // isc_throw(BadValue, "Received DHCPv6 buffer of size " << size
  298. // << ", were able to parse " << offset << " bytes.");
  299. // }
  300. (void)offset;
  301. }
  302. void
  303. Pkt6::unpackRelayMsg() {
  304. // we use offset + bufsize, because we want to avoid creating unnecessary
  305. // copies. There may be up to 32 relays. While using InputBuffer would
  306. // be probably a bit cleaner, copying data up to 32 times is unacceptable
  307. // price here. Hence a single buffer with offets and lengths.
  308. size_t bufsize = data_.size();
  309. size_t offset = 0;
  310. while (bufsize >= DHCPV6_RELAY_HDR_LEN) {
  311. RelayInfo relay;
  312. size_t relay_msg_offset = 0;
  313. size_t relay_msg_len = 0;
  314. // parse fixed header first (first 34 bytes)
  315. relay.msg_type_ = data_[offset++];
  316. relay.hop_count_ = data_[offset++];
  317. relay.linkaddr_ = IOAddress::fromBytes(AF_INET6, &data_[offset]);
  318. offset += isc::asiolink::V6ADDRESS_LEN;
  319. relay.peeraddr_ = IOAddress::fromBytes(AF_INET6, &data_[offset]);
  320. offset += isc::asiolink::V6ADDRESS_LEN;
  321. bufsize -= DHCPV6_RELAY_HDR_LEN; // 34 bytes (1+1+16+16)
  322. // parse the rest as options
  323. OptionBuffer opt_buffer(&data_[offset], &data_[offset+bufsize]);
  324. // If custom option parsing function has been set, use this function
  325. // to parse options. Otherwise, use standard function from libdhcp.
  326. if (callback_.empty()) {
  327. LibDHCP::unpackOptions6(opt_buffer, "dhcp6", relay.options_,
  328. &relay_msg_offset, &relay_msg_len);
  329. } else {
  330. callback_(opt_buffer, "dhcp6", relay.options_,
  331. &relay_msg_offset, &relay_msg_len);
  332. }
  333. /// @todo: check that each option appears at most once
  334. //relay.interface_id_ = options->getOption(D6O_INTERFACE_ID);
  335. //relay.subscriber_id_ = options->getOption(D6O_SUBSCRIBER_ID);
  336. //relay.remote_id_ = options->getOption(D6O_REMOTE_ID);
  337. if (relay_msg_offset == 0 || relay_msg_len == 0) {
  338. isc_throw(BadValue, "Mandatory relay-msg option missing");
  339. }
  340. // store relay information parsed so far
  341. addRelayInfo(relay);
  342. /// @todo: implement ERO (Echo Request Option, RFC 4994) here
  343. if (relay_msg_len >= bufsize) {
  344. // length of the relay_msg option extends beyond end of the message
  345. isc_throw(Unexpected, "Relay-msg option is truncated.");
  346. }
  347. uint8_t inner_type = data_[offset + relay_msg_offset];
  348. offset += relay_msg_offset; // offset is relative
  349. bufsize = relay_msg_len; // length is absolute
  350. if ( (inner_type != DHCPV6_RELAY_FORW) &&
  351. (inner_type != DHCPV6_RELAY_REPL)) {
  352. // Ok, the inner message is not encapsulated, let's decode it
  353. // directly
  354. return (unpackMsg(data_.begin() + offset, data_.begin() + offset
  355. + relay_msg_len));
  356. }
  357. // Oh well, there's inner relay-forw or relay-repl inside. Let's
  358. // unpack it as well. The next loop iteration will take care
  359. // of that.
  360. }
  361. if ( (offset == data_.size()) && (bufsize == 0) ) {
  362. // message has been parsed completely
  363. return;
  364. }
  365. /// @todo: log here that there are additional unparsed bytes
  366. }
  367. void
  368. Pkt6::addRelayInfo(const RelayInfo& relay) {
  369. if (relay_info_.size() > 32) {
  370. isc_throw(BadValue, "Massage cannot be encapsulated more than 32 times");
  371. }
  372. /// @todo: Implement type checks here (e.g. we could receive relay-forw in relay-repl)
  373. relay_info_.push_back(relay);
  374. }
  375. void
  376. Pkt6::unpackTCP() {
  377. isc_throw(Unexpected, "DHCPv6 over TCP (bulk leasequery and failover) "
  378. "not implemented yet.");
  379. }
  380. HWAddrPtr
  381. Pkt6::getMACFromDUID() {
  382. OptionPtr opt_duid = getOption(D6O_CLIENTID);
  383. if (!opt_duid) {
  384. return (HWAddrPtr());
  385. }
  386. uint8_t hlen = opt_duid->getData().size();
  387. vector<uint8_t> hw_addr(hlen, 0);
  388. std::vector<unsigned char> duid_data = opt_duid->getData();
  389. // Read the first two bytes. That duid type.
  390. uint16_t duid_type = util::readUint16(&duid_data[0], duid_data.size());
  391. switch (duid_type) {
  392. case DUID::DUID_LL:
  393. {
  394. // 2 bytes of duid type, 2 bytes of hardware type and at least
  395. // 1 byte of actual identification
  396. if (duid_data.size() < 5) {
  397. // This duid is truncated. We can't extract anything from it.
  398. return (HWAddrPtr());
  399. }
  400. uint16_t hwtype = util::readUint16(&duid_data[2], duid_data.size() - 2);
  401. return (HWAddrPtr(new HWAddr(&duid_data[4], duid_data.size() - 4,
  402. hwtype)));
  403. }
  404. case DUID::DUID_LLT:
  405. {
  406. // 2 bytes of duid type, 2 bytes of hardware, 4 bytes for timestamp,
  407. // and at least 1 byte of actual identification
  408. if (duid_data.size() < 9) {
  409. // This duid is truncated. We can't extract anything from it.
  410. return (HWAddrPtr());
  411. }
  412. uint16_t hwtype = util::readUint16(&duid_data[2], duid_data.size() - 2);
  413. return (HWAddrPtr(new HWAddr(&duid_data[8], duid_data.size() - 8,
  414. hwtype)));
  415. }
  416. default:
  417. return (HWAddrPtr());
  418. }
  419. }
  420. std::string
  421. Pkt6::toText() {
  422. stringstream tmp;
  423. tmp << "localAddr=[" << local_addr_ << "]:" << local_port_
  424. << " remoteAddr=[" << remote_addr_
  425. << "]:" << remote_port_ << endl;
  426. tmp << "msgtype=" << static_cast<int>(msg_type_) << ", transid=0x" <<
  427. hex << transid_ << dec << endl;
  428. for (isc::dhcp::OptionCollection::iterator opt=options_.begin();
  429. opt != options_.end();
  430. ++opt) {
  431. tmp << opt->second->toText() << std::endl;
  432. }
  433. return tmp.str();
  434. }
  435. isc::dhcp::OptionCollection
  436. Pkt6::getOptions(uint16_t opt_type) {
  437. isc::dhcp::OptionCollection found;
  438. for (OptionCollection::const_iterator x = options_.begin();
  439. x != options_.end(); ++x) {
  440. if (x->first == opt_type) {
  441. found.insert(make_pair(opt_type, x->second));
  442. }
  443. }
  444. return (found);
  445. }
  446. const char*
  447. Pkt6::getName(uint8_t type) {
  448. static const char* CONFIRM = "CONFIRM";
  449. static const char* DECLINE = "DECLINE";
  450. static const char* INFORMATION_REQUEST = "INFORMATION_REQUEST";
  451. static const char* REBIND = "REBIND";
  452. static const char* RELEASE = "RELEASE";
  453. static const char* RENEW = "RENEW";
  454. static const char* REQUEST = "REQUEST";
  455. static const char* SOLICIT = "SOLICIT";
  456. static const char* UNKNOWN = "UNKNOWN";
  457. switch (type) {
  458. case DHCPV6_CONFIRM:
  459. return (CONFIRM);
  460. case DHCPV6_DECLINE:
  461. return (DECLINE);
  462. case DHCPV6_INFORMATION_REQUEST:
  463. return (INFORMATION_REQUEST);
  464. case DHCPV6_REBIND:
  465. return (REBIND);
  466. case DHCPV6_RELEASE:
  467. return (RELEASE);
  468. case DHCPV6_RENEW:
  469. return (RENEW);
  470. case DHCPV6_REQUEST:
  471. return (REQUEST);
  472. case DHCPV6_SOLICIT:
  473. return (SOLICIT);
  474. default:
  475. ;
  476. }
  477. return (UNKNOWN);
  478. }
  479. const char* Pkt6::getName() const {
  480. return (getName(getType()));
  481. }
  482. void Pkt6::copyRelayInfo(const Pkt6Ptr& question) {
  483. // We use index rather than iterator, because we need that as a parameter
  484. // passed to getRelayOption()
  485. for (int i = 0; i < question->relay_info_.size(); ++i) {
  486. RelayInfo info;
  487. info.msg_type_ = DHCPV6_RELAY_REPL;
  488. info.hop_count_ = question->relay_info_[i].hop_count_;
  489. info.linkaddr_ = question->relay_info_[i].linkaddr_;
  490. info.peeraddr_ = question->relay_info_[i].peeraddr_;
  491. // Is there an interface-id option in this nesting level?
  492. // If there is, we need to echo it back
  493. OptionPtr opt = question->getRelayOption(D6O_INTERFACE_ID, i);
  494. // taken from question->RelayInfo_[i].options_
  495. if (opt) {
  496. info.options_.insert(make_pair(opt->getType(), opt));
  497. }
  498. /// @todo: Implement support for ERO (Echo Request Option, RFC4994)
  499. // Add this relay-forw info (client's message) to our relay-repl
  500. // message (server's response)
  501. relay_info_.push_back(info);
  502. }
  503. }
  504. HWAddrPtr
  505. Pkt6::getMACFromSrcLinkLocalAddr() {
  506. if (relay_info_.empty()) {
  507. // This is a direct message, use source address
  508. return (getMACFromIPv6(remote_addr_));
  509. }
  510. // This is a relayed message, get the peer-addr from the first relay-forw
  511. return (getMACFromIPv6(relay_info_[relay_info_.size() - 1].peeraddr_));
  512. }
  513. HWAddrPtr
  514. Pkt6::getMACFromIPv6RelayOpt() {
  515. if (relay_info_.empty()) {
  516. // This is a direct message
  517. return (HWAddrPtr());
  518. }
  519. // RFC6969 Section 6: Look for the client_linklayer_addr option on the
  520. // relay agent closest to the client
  521. OptionPtr opt = getAnyRelayOption(D6O_CLIENT_LINKLAYER_ADDR, RELAY_GET_FIRST);
  522. if (opt) {
  523. const OptionBuffer data = opt->getData();
  524. if (data.size() < 3) {
  525. // This client link address option is truncated. It's supposed to be
  526. // 2 bytes of link-layer type followed by link-layer address.
  527. return (HWAddrPtr());
  528. }
  529. // +2, -2 means to skip the initial 2 bytes which are hwaddress type
  530. return (HWAddrPtr(new HWAddr(&data[0] + 2, data.size() - 2,
  531. opt->getUint16())));
  532. } else {
  533. return (HWAddrPtr());
  534. }
  535. }
  536. HWAddrPtr
  537. Pkt6::getMACFromDocsisModem() {
  538. OptionVendorPtr vendor = boost::dynamic_pointer_cast<
  539. OptionVendor>(getOption(D6O_VENDOR_OPTS));
  540. // Check if this is indeed DOCSIS3 environment
  541. if (!vendor || vendor->getVendorId() != VENDOR_ID_CABLE_LABS) {
  542. return (HWAddrPtr());
  543. }
  544. // If it is, try to get device-id option
  545. OptionPtr device_id = vendor->getOption(DOCSIS3_V6_DEVICE_ID);
  546. if (!device_id) {
  547. return (HWAddrPtr());
  548. }
  549. // If the option contains any data, use it as MAC address
  550. if (!device_id->getData().empty()) {
  551. return (HWAddrPtr(new HWAddr(device_id->getData(), HTYPE_DOCSIS)));
  552. } else {
  553. return (HWAddrPtr());
  554. }
  555. }
  556. HWAddrPtr
  557. Pkt6::getMACFromDocsisCMTS() {
  558. if (relay_info_.empty()) {
  559. // This message didn't pass through a CMTS, so there won't be any
  560. // CMTS-specific options in it.
  561. return (HWAddrPtr());
  562. }
  563. OptionVendorPtr vendor = boost::dynamic_pointer_cast<
  564. OptionVendor>(getAnyRelayOption(D6O_VENDOR_OPTS,
  565. RELAY_SEARCH_FROM_CLIENT));
  566. // Check if this is indeed DOCSIS3 environment
  567. if (!vendor || vendor->getVendorId() != VENDOR_ID_CABLE_LABS) {
  568. return (HWAddrPtr());
  569. }
  570. // If it is, try to get cable modem mac
  571. OptionPtr cm_mac = vendor->getOption(DOCSIS3_V6_CMTS_CM_MAC);
  572. if (!cm_mac) {
  573. return (HWAddrPtr());
  574. }
  575. // If the option contains any data, use it as MAC address
  576. if (!cm_mac->getData().empty()) {
  577. return (HWAddrPtr(new HWAddr(cm_mac->getData(), HTYPE_DOCSIS)));
  578. } else {
  579. return (HWAddrPtr());
  580. }
  581. }
  582. HWAddrPtr
  583. Pkt6::getMACFromRemoteIdRelayOption() {
  584. if (relay_info_.empty()) {
  585. // This is a direct message
  586. return (HWAddrPtr());
  587. }
  588. // Get remote-id option from a relay agent closest to the client
  589. OptionPtr opt = getAnyRelayOption(D6O_REMOTE_ID, RELAY_GET_FIRST);
  590. if (opt) {
  591. const OptionBuffer data = opt->getData();
  592. if (data.size() < 5) {
  593. // This remote-id option is truncated. It's supposed to be
  594. // 4 bytes of enterprise-number followed by remote-id.
  595. return (HWAddrPtr());
  596. }
  597. // Let's get the interface this packet was received on. We need it to get
  598. // the hardware type.
  599. IfacePtr iface = IfaceMgr::instance().getIface(iface_);
  600. uint16_t hwtype = 0; // not specified
  601. // If we get the interface HW type, great! If not, let's not panic.
  602. if (iface) {
  603. hwtype = iface->getHWType();
  604. }
  605. // Skip the initial 4 bytes which are enterprise-number.
  606. return (HWAddrPtr(new HWAddr(&data[0] + 4, data.size() - 4, hwtype)));
  607. } else {
  608. return (HWAddrPtr());
  609. }
  610. }
  611. } // end of isc::dhcp namespace
  612. } // end of isc namespace