pkt6.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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::packOptions6(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. case DHCPV6_DHCPV4_QUERY:
  249. case DHCPV6_DHCPV4_RESPONSE:
  250. default: // assume that uknown messages are not using relay format
  251. {
  252. return (unpackMsg(data_.begin(), data_.end()));
  253. }
  254. case DHCPV6_RELAY_FORW:
  255. case DHCPV6_RELAY_REPL:
  256. return (unpackRelayMsg());
  257. }
  258. }
  259. void
  260. Pkt6::unpackMsg(OptionBuffer::const_iterator begin,
  261. OptionBuffer::const_iterator end) {
  262. size_t size = std::distance(begin, end);
  263. if (size < 4) {
  264. // truncated message (less than 4 bytes)
  265. isc_throw(BadValue, "Received truncated UDP DHCPv6 packet of size "
  266. << data_.size() << ", DHCPv6 header alone has 4 bytes.");
  267. }
  268. msg_type_ = *begin++;
  269. transid_ = ( (*begin++) << 16 ) +
  270. ((*begin++) << 8) + (*begin++);
  271. transid_ = transid_ & 0xffffff;
  272. // See below about invoking Postel's law, as we aren't using
  273. // size we don't need to update it. If we do so in the future
  274. // perhaps for stats gathering we can uncomment this.
  275. // size -= sizeof(uint32_t); // We just parsed 4 bytes header
  276. OptionBuffer opt_buffer(begin, end);
  277. // If custom option parsing function has been set, use this function
  278. // to parse options. Otherwise, use standard function from libdhcp.
  279. size_t offset;
  280. if (callback_.empty()) {
  281. offset = LibDHCP::unpackOptions6(opt_buffer, "dhcp6", options_);
  282. } else {
  283. // The last two arguments hold the DHCPv6 Relay message offset and
  284. // length. Setting them to NULL because we are dealing with the
  285. // not-relayed message.
  286. offset = callback_(opt_buffer, "dhcp6", options_, NULL, NULL);
  287. }
  288. // If offset is not equal to the size, then something is wrong here. We
  289. // either parsed past input buffer (bug in our code) or we haven't parsed
  290. // everything (received trailing garbage or truncated option).
  291. //
  292. // Invoking Jon Postel's law here: be conservative in what you send, and be
  293. // liberal in what you accept. There's no easy way to log something from
  294. // libdhcp++ library, so we just choose to be silent about remaining
  295. // bytes. We also need to quell compiler warning about unused offset
  296. // variable.
  297. //
  298. // if (offset != size) {
  299. // isc_throw(BadValue, "Received DHCPv6 buffer of size " << size
  300. // << ", were able to parse " << offset << " bytes.");
  301. // }
  302. (void)offset;
  303. }
  304. void
  305. Pkt6::unpackRelayMsg() {
  306. // we use offset + bufsize, because we want to avoid creating unnecessary
  307. // copies. There may be up to 32 relays. While using InputBuffer would
  308. // be probably a bit cleaner, copying data up to 32 times is unacceptable
  309. // price here. Hence a single buffer with offets and lengths.
  310. size_t bufsize = data_.size();
  311. size_t offset = 0;
  312. while (bufsize >= DHCPV6_RELAY_HDR_LEN) {
  313. RelayInfo relay;
  314. size_t relay_msg_offset = 0;
  315. size_t relay_msg_len = 0;
  316. // parse fixed header first (first 34 bytes)
  317. relay.msg_type_ = data_[offset++];
  318. relay.hop_count_ = data_[offset++];
  319. relay.linkaddr_ = IOAddress::fromBytes(AF_INET6, &data_[offset]);
  320. offset += isc::asiolink::V6ADDRESS_LEN;
  321. relay.peeraddr_ = IOAddress::fromBytes(AF_INET6, &data_[offset]);
  322. offset += isc::asiolink::V6ADDRESS_LEN;
  323. bufsize -= DHCPV6_RELAY_HDR_LEN; // 34 bytes (1+1+16+16)
  324. // parse the rest as options
  325. OptionBuffer opt_buffer(&data_[offset], &data_[offset] + bufsize);
  326. // If custom option parsing function has been set, use this function
  327. // to parse options. Otherwise, use standard function from libdhcp.
  328. if (callback_.empty()) {
  329. LibDHCP::unpackOptions6(opt_buffer, "dhcp6", relay.options_,
  330. &relay_msg_offset, &relay_msg_len);
  331. } else {
  332. callback_(opt_buffer, "dhcp6", relay.options_,
  333. &relay_msg_offset, &relay_msg_len);
  334. }
  335. /// @todo: check that each option appears at most once
  336. //relay.interface_id_ = options->getOption(D6O_INTERFACE_ID);
  337. //relay.subscriber_id_ = options->getOption(D6O_SUBSCRIBER_ID);
  338. //relay.remote_id_ = options->getOption(D6O_REMOTE_ID);
  339. if (relay_msg_offset == 0 || relay_msg_len == 0) {
  340. isc_throw(BadValue, "Mandatory relay-msg option missing");
  341. }
  342. // store relay information parsed so far
  343. addRelayInfo(relay);
  344. /// @todo: implement ERO (Echo Request Option, RFC 4994) here
  345. if (relay_msg_len >= bufsize) {
  346. // length of the relay_msg option extends beyond end of the message
  347. isc_throw(Unexpected, "Relay-msg option is truncated.");
  348. }
  349. uint8_t inner_type = data_[offset + relay_msg_offset];
  350. offset += relay_msg_offset; // offset is relative
  351. bufsize = relay_msg_len; // length is absolute
  352. if ( (inner_type != DHCPV6_RELAY_FORW) &&
  353. (inner_type != DHCPV6_RELAY_REPL)) {
  354. // Ok, the inner message is not encapsulated, let's decode it
  355. // directly
  356. return (unpackMsg(data_.begin() + offset, data_.begin() + offset
  357. + relay_msg_len));
  358. }
  359. // Oh well, there's inner relay-forw or relay-repl inside. Let's
  360. // unpack it as well. The next loop iteration will take care
  361. // of that.
  362. }
  363. if ( (offset == data_.size()) && (bufsize == 0) ) {
  364. // message has been parsed completely
  365. return;
  366. }
  367. /// @todo: log here that there are additional unparsed bytes
  368. }
  369. void
  370. Pkt6::addRelayInfo(const RelayInfo& relay) {
  371. if (relay_info_.size() > 32) {
  372. isc_throw(BadValue, "Massage cannot be encapsulated more than 32 times");
  373. }
  374. /// @todo: Implement type checks here (e.g. we could receive relay-forw in relay-repl)
  375. relay_info_.push_back(relay);
  376. }
  377. void
  378. Pkt6::unpackTCP() {
  379. isc_throw(Unexpected, "DHCPv6 over TCP (bulk leasequery and failover) "
  380. "not implemented yet.");
  381. }
  382. HWAddrPtr
  383. Pkt6::getMACFromDUID() {
  384. OptionPtr opt_duid = getOption(D6O_CLIENTID);
  385. if (!opt_duid) {
  386. return (HWAddrPtr());
  387. }
  388. uint8_t hlen = opt_duid->getData().size();
  389. vector<uint8_t> hw_addr(hlen, 0);
  390. std::vector<unsigned char> duid_data = opt_duid->getData();
  391. // Read the first two bytes. That duid type.
  392. uint16_t duid_type = util::readUint16(&duid_data[0], duid_data.size());
  393. switch (duid_type) {
  394. case DUID::DUID_LL:
  395. {
  396. // 2 bytes of duid type, 2 bytes of hardware type and at least
  397. // 1 byte of actual identification
  398. if (duid_data.size() < 5) {
  399. // This duid is truncated. We can't extract anything from it.
  400. return (HWAddrPtr());
  401. }
  402. uint16_t hwtype = util::readUint16(&duid_data[2], duid_data.size() - 2);
  403. return (HWAddrPtr(new HWAddr(&duid_data[4], duid_data.size() - 4,
  404. hwtype)));
  405. }
  406. case DUID::DUID_LLT:
  407. {
  408. // 2 bytes of duid type, 2 bytes of hardware, 4 bytes for timestamp,
  409. // and at least 1 byte of actual identification
  410. if (duid_data.size() < 9) {
  411. // This duid is truncated. We can't extract anything from it.
  412. return (HWAddrPtr());
  413. }
  414. uint16_t hwtype = util::readUint16(&duid_data[2], duid_data.size() - 2);
  415. return (HWAddrPtr(new HWAddr(&duid_data[8], duid_data.size() - 8,
  416. hwtype)));
  417. }
  418. default:
  419. return (HWAddrPtr());
  420. }
  421. }
  422. std::string
  423. Pkt6::makeLabel(const DuidPtr duid, const uint32_t transid,
  424. const HWAddrPtr& hwaddr) {
  425. // Create label with DUID and HW address.
  426. std::stringstream label;
  427. label << makeLabel(duid, hwaddr);
  428. // Append transaction id.
  429. label << ", tid=0x" << std::hex << transid << std::dec;
  430. return (label.str());
  431. }
  432. std::string
  433. Pkt6::makeLabel(const DuidPtr duid, const HWAddrPtr& hwaddr) {
  434. std::stringstream label;
  435. // DUID should be present at all times, so explicitly inform when
  436. // it is no present (no info).
  437. label << "duid=[" << (duid ? duid->toText() : "no info")
  438. << "]";
  439. // HW address is typically not carried in the DHCPv6 mmessages
  440. // and can be extracted using various, but not fully reliable,
  441. // techniques. If it is not present, don't print anything.
  442. if (hwaddr) {
  443. label << ", [" << hwaddr->toText() << "]";
  444. }
  445. return (label.str());
  446. }
  447. std::string
  448. Pkt6::getLabel() const {
  449. /// @todo Do not print HW address as it is unclear how it should
  450. /// be retrieved if there is no access to user configuration which
  451. /// specifies the order of various techniques to be used to retrieve
  452. /// it.
  453. return (makeLabel(getClientId(), getTransid(), HWAddrPtr()));}
  454. std::string
  455. Pkt6::toText() const {
  456. stringstream tmp;
  457. tmp << "localAddr=[" << local_addr_ << "]:" << local_port_
  458. << " remoteAddr=[" << remote_addr_
  459. << "]:" << remote_port_ << endl;
  460. tmp << "msgtype=" << static_cast<int>(msg_type_) << ", transid=0x" <<
  461. hex << transid_ << dec << endl;
  462. for (isc::dhcp::OptionCollection::const_iterator opt=options_.begin();
  463. opt != options_.end();
  464. ++opt) {
  465. tmp << opt->second->toText() << std::endl;
  466. }
  467. return tmp.str();
  468. }
  469. DuidPtr
  470. Pkt6::getClientId() const {
  471. OptionPtr opt_duid = getOption(D6O_CLIENTID);
  472. try {
  473. // This will throw if the DUID length is larger than 128 bytes
  474. // or is too short.
  475. return (opt_duid ? DuidPtr(new DUID(opt_duid->getData())) : DuidPtr());
  476. } catch (...) {
  477. // Do nothing. This method is used only by getLabel(), which is
  478. // used for logging purposes. We should not throw, but rather
  479. // report no DUID. We should not log anything, as we're in the
  480. // process of logging something for this packet. So the only
  481. // choice left is to return an empty pointer.
  482. }
  483. return DuidPtr();
  484. }
  485. isc::dhcp::OptionCollection
  486. Pkt6::getOptions(uint16_t opt_type) {
  487. isc::dhcp::OptionCollection found;
  488. for (OptionCollection::const_iterator x = options_.begin();
  489. x != options_.end(); ++x) {
  490. if (x->first == opt_type) {
  491. found.insert(make_pair(opt_type, x->second));
  492. }
  493. }
  494. return (found);
  495. }
  496. const char*
  497. Pkt6::getName(const uint8_t type) {
  498. static const char* ADVERTISE = "ADVERTISE";
  499. static const char* CONFIRM = "CONFIRM";
  500. static const char* DECLINE = "DECLINE";
  501. static const char* INFORMATION_REQUEST = "INFORMATION_REQUEST";
  502. static const char* LEASEQUERY = "LEASEQUERY";
  503. static const char* LEASEQUERY_REPLY = "LEASEQUERY_REPLY";
  504. static const char* REBIND = "REBIND";
  505. static const char* RECONFIGURE = "RECONFIGURE";
  506. static const char* RELAY_FORW = "RELAY_FORWARD";
  507. static const char* RELAY_REPL = "RELAY_REPLY";
  508. static const char* RELEASE = "RELEASE";
  509. static const char* RENEW = "RENEW";
  510. static const char* REPLY = "REPLY";
  511. static const char* REQUEST = "REQUEST";
  512. static const char* SOLICIT = "SOLICIT";
  513. static const char* DHCPV4_QUERY = "DHCPV4_QUERY";
  514. static const char* DHCPV4_RESPONSE = "DHCPV4_RESPONSE";
  515. static const char* UNKNOWN = "UNKNOWN";
  516. switch (type) {
  517. case DHCPV6_ADVERTISE:
  518. return (ADVERTISE);
  519. case DHCPV6_CONFIRM:
  520. return (CONFIRM);
  521. case DHCPV6_DECLINE:
  522. return (DECLINE);
  523. case DHCPV6_INFORMATION_REQUEST:
  524. return (INFORMATION_REQUEST);
  525. case DHCPV6_LEASEQUERY:
  526. return (LEASEQUERY);
  527. case DHCPV6_LEASEQUERY_REPLY:
  528. return (LEASEQUERY_REPLY);
  529. case DHCPV6_REBIND:
  530. return (REBIND);
  531. case DHCPV6_RECONFIGURE:
  532. return (RECONFIGURE);
  533. case DHCPV6_RELAY_FORW:
  534. return (RELAY_FORW);
  535. case DHCPV6_RELAY_REPL:
  536. return (RELAY_REPL);
  537. case DHCPV6_RELEASE:
  538. return (RELEASE);
  539. case DHCPV6_RENEW:
  540. return (RENEW);
  541. case DHCPV6_REPLY:
  542. return (REPLY);
  543. case DHCPV6_REQUEST:
  544. return (REQUEST);
  545. case DHCPV6_SOLICIT:
  546. return (SOLICIT);
  547. case DHCPV6_DHCPV4_QUERY:
  548. return (DHCPV4_QUERY);
  549. case DHCPV6_DHCPV4_RESPONSE:
  550. return (DHCPV4_RESPONSE);
  551. default:
  552. ;
  553. }
  554. return (UNKNOWN);
  555. }
  556. const char* Pkt6::getName() const {
  557. return (getName(getType()));
  558. }
  559. void Pkt6::copyRelayInfo(const Pkt6Ptr& question) {
  560. // We use index rather than iterator, because we need that as a parameter
  561. // passed to getRelayOption()
  562. for (size_t i = 0; i < question->relay_info_.size(); ++i) {
  563. RelayInfo info;
  564. info.msg_type_ = DHCPV6_RELAY_REPL;
  565. info.hop_count_ = question->relay_info_[i].hop_count_;
  566. info.linkaddr_ = question->relay_info_[i].linkaddr_;
  567. info.peeraddr_ = question->relay_info_[i].peeraddr_;
  568. // Is there an interface-id option in this nesting level?
  569. // If there is, we need to echo it back
  570. OptionPtr opt = question->getRelayOption(D6O_INTERFACE_ID, i);
  571. // taken from question->RelayInfo_[i].options_
  572. if (opt) {
  573. info.options_.insert(make_pair(opt->getType(), opt));
  574. }
  575. /// @todo: Implement support for ERO (Echo Request Option, RFC4994)
  576. // Add this relay-forw info (client's message) to our relay-repl
  577. // message (server's response)
  578. relay_info_.push_back(info);
  579. }
  580. }
  581. HWAddrPtr
  582. Pkt6::getMACFromSrcLinkLocalAddr() {
  583. if (relay_info_.empty()) {
  584. // This is a direct message, use source address
  585. return (getMACFromIPv6(remote_addr_));
  586. }
  587. // This is a relayed message, get the peer-addr from the first relay-forw
  588. return (getMACFromIPv6(relay_info_[relay_info_.size() - 1].peeraddr_));
  589. }
  590. HWAddrPtr
  591. Pkt6::getMACFromIPv6RelayOpt() {
  592. if (relay_info_.empty()) {
  593. // This is a direct message
  594. return (HWAddrPtr());
  595. }
  596. // RFC6969 Section 6: Look for the client_linklayer_addr option on the
  597. // relay agent closest to the client
  598. OptionPtr opt = getAnyRelayOption(D6O_CLIENT_LINKLAYER_ADDR, RELAY_GET_FIRST);
  599. if (opt) {
  600. const OptionBuffer data = opt->getData();
  601. if (data.size() < 3) {
  602. // This client link address option is truncated. It's supposed to be
  603. // 2 bytes of link-layer type followed by link-layer address.
  604. return (HWAddrPtr());
  605. }
  606. // +2, -2 means to skip the initial 2 bytes which are hwaddress type
  607. return (HWAddrPtr(new HWAddr(&data[0] + 2, data.size() - 2,
  608. opt->getUint16())));
  609. } else {
  610. return (HWAddrPtr());
  611. }
  612. }
  613. HWAddrPtr
  614. Pkt6::getMACFromDocsisModem() {
  615. OptionVendorPtr vendor = boost::dynamic_pointer_cast<
  616. OptionVendor>(getOption(D6O_VENDOR_OPTS));
  617. // Check if this is indeed DOCSIS3 environment
  618. if (!vendor || vendor->getVendorId() != VENDOR_ID_CABLE_LABS) {
  619. return (HWAddrPtr());
  620. }
  621. // If it is, try to get device-id option
  622. OptionPtr device_id = vendor->getOption(DOCSIS3_V6_DEVICE_ID);
  623. if (!device_id) {
  624. return (HWAddrPtr());
  625. }
  626. // If the option contains any data, use it as MAC address
  627. if (!device_id->getData().empty()) {
  628. return (HWAddrPtr(new HWAddr(device_id->getData(), HTYPE_DOCSIS)));
  629. } else {
  630. return (HWAddrPtr());
  631. }
  632. }
  633. HWAddrPtr
  634. Pkt6::getMACFromDocsisCMTS() {
  635. if (relay_info_.empty()) {
  636. // This message didn't pass through a CMTS, so there won't be any
  637. // CMTS-specific options in it.
  638. return (HWAddrPtr());
  639. }
  640. OptionVendorPtr vendor = boost::dynamic_pointer_cast<
  641. OptionVendor>(getAnyRelayOption(D6O_VENDOR_OPTS,
  642. RELAY_SEARCH_FROM_CLIENT));
  643. // Check if this is indeed DOCSIS3 environment
  644. if (!vendor || vendor->getVendorId() != VENDOR_ID_CABLE_LABS) {
  645. return (HWAddrPtr());
  646. }
  647. // If it is, try to get cable modem mac
  648. OptionPtr cm_mac = vendor->getOption(DOCSIS3_V6_CMTS_CM_MAC);
  649. if (!cm_mac) {
  650. return (HWAddrPtr());
  651. }
  652. // If the option contains any data, use it as MAC address
  653. if (!cm_mac->getData().empty()) {
  654. return (HWAddrPtr(new HWAddr(cm_mac->getData(), HTYPE_DOCSIS)));
  655. } else {
  656. return (HWAddrPtr());
  657. }
  658. }
  659. HWAddrPtr
  660. Pkt6::getMACFromRemoteIdRelayOption() {
  661. if (relay_info_.empty()) {
  662. // This is a direct message
  663. return (HWAddrPtr());
  664. }
  665. // Get remote-id option from a relay agent closest to the client
  666. OptionPtr opt = getAnyRelayOption(D6O_REMOTE_ID, RELAY_GET_FIRST);
  667. if (opt) {
  668. const OptionBuffer data = opt->getData();
  669. if (data.size() < 5) {
  670. // This remote-id option is truncated. It's supposed to be
  671. // 4 bytes of enterprise-number followed by remote-id.
  672. return (HWAddrPtr());
  673. }
  674. // Let's get the interface this packet was received on. We need it to get
  675. // the hardware type.
  676. IfacePtr iface = IfaceMgr::instance().getIface(iface_);
  677. uint16_t hwtype = 0; // not specified
  678. // If we get the interface HW type, great! If not, let's not panic.
  679. if (iface) {
  680. hwtype = iface->getHWType();
  681. }
  682. // Skip the initial 4 bytes which are enterprise-number.
  683. return (HWAddrPtr(new HWAddr(&data[0] + 4, data.size() - 4, hwtype)));
  684. } else {
  685. return (HWAddrPtr());
  686. }
  687. }
  688. } // end of isc::dhcp namespace
  689. } // end of isc namespace