pkt4.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. // Copyright (C) 2011-2013 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 <asiolink/io_address.h>
  15. #include <dhcp/dhcp4.h>
  16. #include <dhcp/libdhcp++.h>
  17. #include <dhcp/option_int.h>
  18. #include <dhcp/pkt4.h>
  19. #include <exceptions/exceptions.h>
  20. #include <algorithm>
  21. #include <iostream>
  22. #include <sstream>
  23. using namespace std;
  24. using namespace isc::dhcp;
  25. using namespace isc::asiolink;
  26. namespace isc {
  27. namespace dhcp {
  28. const IOAddress DEFAULT_ADDRESS("0.0.0.0");
  29. Pkt4::Pkt4(uint8_t msg_type, uint32_t transid)
  30. :buffer_out_(DHCPV4_PKT_HDR_LEN),
  31. local_addr_(DEFAULT_ADDRESS),
  32. remote_addr_(DEFAULT_ADDRESS),
  33. iface_(""),
  34. ifindex_(0),
  35. local_port_(DHCP4_SERVER_PORT),
  36. remote_port_(DHCP4_CLIENT_PORT),
  37. op_(DHCPTypeToBootpType(msg_type)),
  38. hwaddr_(new HWAddr()),
  39. hops_(0),
  40. transid_(transid),
  41. secs_(0),
  42. flags_(0),
  43. ciaddr_(DEFAULT_ADDRESS),
  44. yiaddr_(DEFAULT_ADDRESS),
  45. siaddr_(DEFAULT_ADDRESS),
  46. giaddr_(DEFAULT_ADDRESS)
  47. {
  48. memset(sname_, 0, MAX_SNAME_LEN);
  49. memset(file_, 0, MAX_FILE_LEN);
  50. setType(msg_type);
  51. }
  52. Pkt4::Pkt4(const uint8_t* data, size_t len)
  53. :buffer_out_(0), // not used, this is RX packet
  54. local_addr_(DEFAULT_ADDRESS),
  55. remote_addr_(DEFAULT_ADDRESS),
  56. iface_(""),
  57. ifindex_(0),
  58. local_port_(DHCP4_SERVER_PORT),
  59. remote_port_(DHCP4_CLIENT_PORT),
  60. op_(BOOTREQUEST),
  61. hwaddr_(new HWAddr()),
  62. transid_(0),
  63. secs_(0),
  64. flags_(0),
  65. ciaddr_(DEFAULT_ADDRESS),
  66. yiaddr_(DEFAULT_ADDRESS),
  67. siaddr_(DEFAULT_ADDRESS),
  68. giaddr_(DEFAULT_ADDRESS)
  69. {
  70. if (len < DHCPV4_PKT_HDR_LEN) {
  71. isc_throw(OutOfRange, "Truncated DHCPv4 packet (len=" << len
  72. << ") received, at least " << DHCPV4_PKT_HDR_LEN
  73. << " is expected.");
  74. } else if (data == NULL) {
  75. isc_throw(InvalidParameter, "data buffer passed to Pkt4 is NULL");
  76. }
  77. data_.resize(len);
  78. memcpy(&data_[0], data, len);
  79. }
  80. size_t
  81. Pkt4::len() {
  82. size_t length = DHCPV4_PKT_HDR_LEN; // DHCPv4 header
  83. // ... and sum of lengths of all options
  84. for (OptionCollection::const_iterator it = options_.begin();
  85. it != options_.end();
  86. ++it) {
  87. length += (*it).second->len();
  88. }
  89. return (length);
  90. }
  91. void
  92. Pkt4::pack() {
  93. if (!hwaddr_) {
  94. isc_throw(InvalidOperation, "Can't build Pkt4 packet. HWAddr not set.");
  95. }
  96. try {
  97. size_t hw_len = hwaddr_->hwaddr_.size();
  98. buffer_out_.writeUint8(op_);
  99. buffer_out_.writeUint8(hwaddr_->htype_);
  100. buffer_out_.writeUint8(hw_len < MAX_CHADDR_LEN ?
  101. hw_len : MAX_CHADDR_LEN);
  102. buffer_out_.writeUint8(hops_);
  103. buffer_out_.writeUint32(transid_);
  104. buffer_out_.writeUint16(secs_);
  105. buffer_out_.writeUint16(flags_);
  106. buffer_out_.writeUint32(ciaddr_);
  107. buffer_out_.writeUint32(yiaddr_);
  108. buffer_out_.writeUint32(siaddr_);
  109. buffer_out_.writeUint32(giaddr_);
  110. if (hw_len <= MAX_CHADDR_LEN) {
  111. // write up to 16 bytes of the hardware address (CHADDR field is 16
  112. // bytes long in DHCPv4 message).
  113. buffer_out_.writeData(&hwaddr_->hwaddr_[0],
  114. (hw_len < MAX_CHADDR_LEN ?
  115. hw_len : MAX_CHADDR_LEN) );
  116. hw_len = MAX_CHADDR_LEN - hw_len;
  117. } else {
  118. hw_len = MAX_CHADDR_LEN;
  119. }
  120. // write (len) bytes of padding
  121. vector<uint8_t> zeros(hw_len, 0);
  122. buffer_out_.writeData(&zeros[0], hw_len);
  123. // buffer_out_.writeData(chaddr_, MAX_CHADDR_LEN);
  124. buffer_out_.writeData(sname_, MAX_SNAME_LEN);
  125. buffer_out_.writeData(file_, MAX_FILE_LEN);
  126. // write DHCP magic cookie
  127. buffer_out_.writeUint32(DHCP_OPTIONS_COOKIE);
  128. LibDHCP::packOptions(buffer_out_, options_);
  129. // add END option that indicates end of options
  130. // (End option is very simple, just a 255 octet)
  131. buffer_out_.writeUint8(DHO_END);
  132. } catch(const Exception& e) {
  133. // An exception is thrown and message will be written to Logger
  134. isc_throw(InvalidOperation, e.what());
  135. }
  136. }
  137. void
  138. Pkt4::unpack() {
  139. // input buffer (used during message reception)
  140. isc::util::InputBuffer buffer_in(&data_[0], data_.size());
  141. if (buffer_in.getLength() < DHCPV4_PKT_HDR_LEN) {
  142. isc_throw(OutOfRange, "Received truncated DHCPv4 packet (len="
  143. << buffer_in.getLength() << " received, at least "
  144. << DHCPV4_PKT_HDR_LEN << "is expected");
  145. }
  146. op_ = buffer_in.readUint8();
  147. uint8_t htype = buffer_in.readUint8();
  148. uint8_t hlen = buffer_in.readUint8();
  149. hops_ = buffer_in.readUint8();
  150. transid_ = buffer_in.readUint32();
  151. secs_ = buffer_in.readUint16();
  152. flags_ = buffer_in.readUint16();
  153. ciaddr_ = IOAddress(buffer_in.readUint32());
  154. yiaddr_ = IOAddress(buffer_in.readUint32());
  155. siaddr_ = IOAddress(buffer_in.readUint32());
  156. giaddr_ = IOAddress(buffer_in.readUint32());
  157. vector<uint8_t> hw_addr(MAX_CHADDR_LEN, 0);
  158. buffer_in.readVector(hw_addr, MAX_CHADDR_LEN);
  159. buffer_in.readData(sname_, MAX_SNAME_LEN);
  160. buffer_in.readData(file_, MAX_FILE_LEN);
  161. hw_addr.resize(hlen);
  162. hwaddr_ = HWAddrPtr(new HWAddr(hw_addr, htype));
  163. if (buffer_in.getLength() == buffer_in.getPosition()) {
  164. // this is *NOT* DHCP packet. It does not have any DHCPv4 options. In
  165. // particular, it does not have magic cookie, a 4 byte sequence that
  166. // differentiates between DHCP and BOOTP packets.
  167. isc_throw(InvalidOperation, "Received BOOTP packet. BOOTP is not supported.");
  168. }
  169. if (buffer_in.getLength() - buffer_in.getPosition() < 4) {
  170. // there is not enough data to hold magic DHCP cookie
  171. isc_throw(Unexpected, "Truncated or no DHCP packet.");
  172. }
  173. uint32_t magic = buffer_in.readUint32();
  174. if (magic != DHCP_OPTIONS_COOKIE) {
  175. isc_throw(Unexpected, "Invalid or missing DHCP magic cookie");
  176. }
  177. size_t opts_len = buffer_in.getLength() - buffer_in.getPosition();
  178. vector<uint8_t> opts_buffer;
  179. // Use readVector because a function which parses option requires
  180. // a vector as an input.
  181. buffer_in.readVector(opts_buffer, opts_len);
  182. if (callback_.empty()) {
  183. LibDHCP::unpackOptions4(opts_buffer, options_);
  184. } else {
  185. // The last two arguments are set to NULL because they are
  186. // specific to DHCPv6 options parsing. They are unused for
  187. // DHCPv4 case. In DHCPv6 case they hold are the relay message
  188. // offset and length.
  189. callback_(opts_buffer, "dhcp4", options_, NULL, NULL);
  190. }
  191. // @todo check will need to be called separately, so hooks can be called
  192. // after the packet is parsed, but before its content is verified
  193. check();
  194. }
  195. void Pkt4::check() {
  196. uint8_t msg_type = getType();
  197. if (msg_type > DHCPLEASEACTIVE) {
  198. isc_throw(BadValue, "Invalid DHCP message type received: "
  199. << static_cast<int>(msg_type));
  200. }
  201. }
  202. uint8_t Pkt4::getType() const {
  203. OptionPtr generic = getOption(DHO_DHCP_MESSAGE_TYPE);
  204. if (!generic) {
  205. isc_throw(Unexpected, "Missing DHCP Message Type option");
  206. }
  207. // Check if Message Type is specified as OptionInt<uint8_t>
  208. boost::shared_ptr<OptionInt<uint8_t> > type_opt =
  209. boost::dynamic_pointer_cast<OptionInt<uint8_t> >(generic);
  210. if (type_opt) {
  211. return (type_opt->getValue());
  212. }
  213. // Try to use it as generic option
  214. return (generic->getUint8());
  215. }
  216. void Pkt4::setType(uint8_t dhcp_type) {
  217. OptionPtr opt = getOption(DHO_DHCP_MESSAGE_TYPE);
  218. if (opt) {
  219. // There is message type option already, update it
  220. opt->setUint8(dhcp_type);
  221. } else {
  222. // There is no message type option yet, add it
  223. std::vector<uint8_t> tmp(1, dhcp_type);
  224. opt = OptionPtr(new Option(Option::V4, DHO_DHCP_MESSAGE_TYPE, tmp));
  225. addOption(opt);
  226. }
  227. }
  228. void Pkt4::repack() {
  229. buffer_out_.writeData(&data_[0], data_.size());
  230. }
  231. std::string
  232. Pkt4::toText() {
  233. stringstream tmp;
  234. tmp << "localAddr=" << local_addr_.toText() << ":" << local_port_
  235. << " remoteAddr=" << remote_addr_.toText()
  236. << ":" << remote_port_ << ", msgtype=" << static_cast<int>(getType())
  237. << ", transid=0x" << hex << transid_ << dec << endl;
  238. for (isc::dhcp::OptionCollection::iterator opt=options_.begin();
  239. opt != options_.end();
  240. ++opt) {
  241. tmp << " " << opt->second->toText() << std::endl;
  242. }
  243. return tmp.str();
  244. }
  245. void
  246. Pkt4::setHWAddr(uint8_t htype, uint8_t hlen,
  247. const std::vector<uint8_t>& mac_addr) {
  248. setHWAddrMember(htype, hlen, mac_addr, hwaddr_);
  249. }
  250. void
  251. Pkt4::setHWAddr(const HWAddrPtr& addr) {
  252. if (!addr) {
  253. isc_throw(BadValue, "Setting DHCPv4 chaddr field to NULL"
  254. << " is forbidden");
  255. }
  256. hwaddr_ = addr;
  257. }
  258. void
  259. Pkt4::setHWAddrMember(const uint8_t htype, const uint8_t hlen,
  260. const std::vector<uint8_t>& mac_addr,
  261. HWAddrPtr& hw_addr) {
  262. /// @todo Rewrite this once support for client-identifier option
  263. /// is implemented (ticket 1228?)
  264. if (hlen > MAX_CHADDR_LEN) {
  265. isc_throw(OutOfRange, "Hardware address (len=" << hlen
  266. << " too long. Max " << MAX_CHADDR_LEN << " supported.");
  267. } else if (mac_addr.empty() && (hlen > 0) ) {
  268. isc_throw(OutOfRange, "Invalid HW Address specified");
  269. }
  270. hw_addr.reset(new HWAddr(mac_addr, htype));
  271. }
  272. void
  273. Pkt4::setLocalHWAddr(const uint8_t htype, const uint8_t hlen,
  274. const std::vector<uint8_t>& mac_addr) {
  275. setHWAddrMember(htype, hlen, mac_addr, local_hwaddr_);
  276. }
  277. void
  278. Pkt4::setLocalHWAddr(const HWAddrPtr& addr) {
  279. if (!addr) {
  280. isc_throw(BadValue, "Setting local HW address to NULL is"
  281. << " forbidden.");
  282. }
  283. local_hwaddr_ = addr;
  284. }
  285. void
  286. Pkt4::setRemoteHWAddr(const uint8_t htype, const uint8_t hlen,
  287. const std::vector<uint8_t>& mac_addr) {
  288. setHWAddrMember(htype, hlen, mac_addr, remote_hwaddr_);
  289. }
  290. void
  291. Pkt4::setRemoteHWAddr(const HWAddrPtr& addr) {
  292. if (!addr) {
  293. isc_throw(BadValue, "Setting remote HW address to NULL is"
  294. << " forbidden.");
  295. }
  296. remote_hwaddr_ = addr;
  297. }
  298. void
  299. Pkt4::setSname(const uint8_t* sname, size_t snameLen /*= MAX_SNAME_LEN*/) {
  300. if (snameLen > MAX_SNAME_LEN) {
  301. isc_throw(OutOfRange, "sname field (len=" << snameLen
  302. << ") too long, Max " << MAX_SNAME_LEN << " supported.");
  303. } else if (sname == NULL) {
  304. isc_throw(InvalidParameter, "Invalid sname specified");
  305. }
  306. std::copy(&sname[0], &sname[snameLen], &sname_[0]);
  307. std::fill(&sname_[snameLen], &sname_[MAX_SNAME_LEN], 0);
  308. // No need to store snameLen as any empty space is filled with 0s
  309. }
  310. void
  311. Pkt4::setFile(const uint8_t* file, size_t fileLen /*= MAX_FILE_LEN*/) {
  312. if (fileLen > MAX_FILE_LEN) {
  313. isc_throw(OutOfRange, "file field (len=" << fileLen
  314. << ") too long, Max " << MAX_FILE_LEN << " supported.");
  315. } else if (file == NULL) {
  316. isc_throw(InvalidParameter, "Invalid file name specified");
  317. }
  318. std::copy(&file[0], &file[fileLen], &file_[0]);
  319. std::fill(&file_[fileLen], &file_[MAX_FILE_LEN], 0);
  320. // No need to store fileLen as any empty space is filled with 0s
  321. }
  322. uint8_t
  323. Pkt4::DHCPTypeToBootpType(uint8_t dhcpType) {
  324. switch (dhcpType) {
  325. case DHCPDISCOVER:
  326. case DHCPREQUEST:
  327. case DHCPDECLINE:
  328. case DHCPRELEASE:
  329. case DHCPINFORM:
  330. case DHCPLEASEQUERY:
  331. return (BOOTREQUEST);
  332. case DHCPACK:
  333. case DHCPNAK:
  334. case DHCPOFFER:
  335. case DHCPLEASEUNASSIGNED:
  336. case DHCPLEASEUNKNOWN:
  337. case DHCPLEASEACTIVE:
  338. return (BOOTREPLY);
  339. default:
  340. isc_throw(OutOfRange, "Invalid message type: "
  341. << static_cast<int>(dhcpType) );
  342. }
  343. }
  344. uint8_t
  345. Pkt4::getHtype() const {
  346. if (!hwaddr_) {
  347. isc_throw(InvalidOperation, "Can't get HType. HWAddr not defined");
  348. }
  349. return (hwaddr_->htype_);
  350. }
  351. uint8_t
  352. Pkt4::getHlen() const {
  353. if (!hwaddr_) {
  354. isc_throw(InvalidOperation, "Can't get HType. HWAddr not defined");
  355. }
  356. uint8_t len = hwaddr_->hwaddr_.size();
  357. return (len <= MAX_CHADDR_LEN ? len : MAX_CHADDR_LEN);
  358. }
  359. void
  360. Pkt4::addOption(boost::shared_ptr<Option> opt) {
  361. // Check for uniqueness (DHCPv4 options must be unique)
  362. if (getOption(opt->getType())) {
  363. isc_throw(BadValue, "Option " << opt->getType()
  364. << " already present in this message.");
  365. }
  366. options_.insert(pair<int, boost::shared_ptr<Option> >(opt->getType(), opt));
  367. }
  368. boost::shared_ptr<isc::dhcp::Option>
  369. Pkt4::getOption(uint8_t type) const {
  370. OptionCollection::const_iterator x = options_.find(type);
  371. if (x != options_.end()) {
  372. return (*x).second;
  373. }
  374. return boost::shared_ptr<isc::dhcp::Option>(); // NULL
  375. }
  376. bool
  377. Pkt4::delOption(uint8_t type) {
  378. isc::dhcp::OptionCollection::iterator x = options_.find(type);
  379. if (x != options_.end()) {
  380. options_.erase(x);
  381. return (true); // delete successful
  382. }
  383. return (false); // can't find option to be deleted
  384. }
  385. void
  386. Pkt4::updateTimestamp() {
  387. timestamp_ = boost::posix_time::microsec_clock::universal_time();
  388. }
  389. } // end of namespace isc::dhcp
  390. } // end of namespace isc