pkt4.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // Copyright (C) 2011-2012 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. :local_addr_(DEFAULT_ADDRESS),
  31. remote_addr_(DEFAULT_ADDRESS),
  32. iface_(""),
  33. ifindex_(0),
  34. local_port_(DHCP4_SERVER_PORT),
  35. remote_port_(DHCP4_CLIENT_PORT),
  36. op_(DHCPTypeToBootpType(msg_type)),
  37. hwaddr_(new HWAddr()),
  38. hops_(0),
  39. transid_(transid),
  40. secs_(0),
  41. flags_(0),
  42. ciaddr_(DEFAULT_ADDRESS),
  43. yiaddr_(DEFAULT_ADDRESS),
  44. siaddr_(DEFAULT_ADDRESS),
  45. giaddr_(DEFAULT_ADDRESS),
  46. bufferOut_(DHCPV4_PKT_HDR_LEN)
  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. :local_addr_(DEFAULT_ADDRESS),
  54. remote_addr_(DEFAULT_ADDRESS),
  55. iface_(""),
  56. ifindex_(0),
  57. local_port_(DHCP4_SERVER_PORT),
  58. remote_port_(DHCP4_CLIENT_PORT),
  59. op_(BOOTREQUEST),
  60. hwaddr_(new HWAddr()),
  61. transid_(0),
  62. secs_(0),
  63. flags_(0),
  64. ciaddr_(DEFAULT_ADDRESS),
  65. yiaddr_(DEFAULT_ADDRESS),
  66. siaddr_(DEFAULT_ADDRESS),
  67. giaddr_(DEFAULT_ADDRESS),
  68. bufferOut_(0) // not used, this is RX packet
  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 (Option::OptionCollection::const_iterator it = options_.begin();
  85. it != options_.end();
  86. ++it) {
  87. length += (*it).second->len();
  88. }
  89. return (length);
  90. }
  91. bool
  92. Pkt4::pack() {
  93. if (!hwaddr_) {
  94. isc_throw(InvalidOperation, "Can't build Pkt4 packet. HWAddr not set.");
  95. }
  96. size_t hw_len = hwaddr_->hwaddr_.size();
  97. bufferOut_.writeUint8(op_);
  98. bufferOut_.writeUint8(hwaddr_->htype_);
  99. bufferOut_.writeUint8(hw_len < MAX_CHADDR_LEN ? hw_len : MAX_CHADDR_LEN);
  100. bufferOut_.writeUint8(hops_);
  101. bufferOut_.writeUint32(transid_);
  102. bufferOut_.writeUint16(secs_);
  103. bufferOut_.writeUint16(flags_);
  104. bufferOut_.writeUint32(ciaddr_);
  105. bufferOut_.writeUint32(yiaddr_);
  106. bufferOut_.writeUint32(siaddr_);
  107. bufferOut_.writeUint32(giaddr_);
  108. if (hw_len <= MAX_CHADDR_LEN) {
  109. // write up to 16 bytes of the hardware address (CHADDR field is 16
  110. // bytes long in DHCPv4 message).
  111. bufferOut_.writeData(&hwaddr_->hwaddr_[0],
  112. (hw_len < MAX_CHADDR_LEN ? hw_len : MAX_CHADDR_LEN) );
  113. hw_len = MAX_CHADDR_LEN - hw_len;
  114. } else {
  115. hw_len = MAX_CHADDR_LEN;
  116. }
  117. // write (len) bytes of padding
  118. vector<uint8_t> zeros(hw_len, 0);
  119. bufferOut_.writeData(&zeros[0], hw_len);
  120. // bufferOut_.writeData(chaddr_, MAX_CHADDR_LEN);
  121. bufferOut_.writeData(sname_, MAX_SNAME_LEN);
  122. bufferOut_.writeData(file_, MAX_FILE_LEN);
  123. // write DHCP magic cookie
  124. bufferOut_.writeUint32(DHCP_OPTIONS_COOKIE);
  125. LibDHCP::packOptions(bufferOut_, options_);
  126. // add END option that indicates end of options
  127. // (End option is very simple, just a 255 octet)
  128. bufferOut_.writeUint8(DHO_END);
  129. return (true);
  130. }
  131. void
  132. Pkt4::unpack() {
  133. // input buffer (used during message reception)
  134. isc::util::InputBuffer bufferIn(&data_[0], data_.size());
  135. if (bufferIn.getLength() < DHCPV4_PKT_HDR_LEN) {
  136. isc_throw(OutOfRange, "Received truncated DHCPv4 packet (len="
  137. << bufferIn.getLength() << " received, at least "
  138. << DHCPV4_PKT_HDR_LEN << "is expected");
  139. }
  140. op_ = bufferIn.readUint8();
  141. uint8_t htype = bufferIn.readUint8();
  142. uint8_t hlen = bufferIn.readUint8();
  143. hops_ = bufferIn.readUint8();
  144. transid_ = bufferIn.readUint32();
  145. secs_ = bufferIn.readUint16();
  146. flags_ = bufferIn.readUint16();
  147. ciaddr_ = IOAddress(bufferIn.readUint32());
  148. yiaddr_ = IOAddress(bufferIn.readUint32());
  149. siaddr_ = IOAddress(bufferIn.readUint32());
  150. giaddr_ = IOAddress(bufferIn.readUint32());
  151. vector<uint8_t> hw_addr(MAX_CHADDR_LEN, 0);
  152. bufferIn.readVector(hw_addr, MAX_CHADDR_LEN);
  153. bufferIn.readData(sname_, MAX_SNAME_LEN);
  154. bufferIn.readData(file_, MAX_FILE_LEN);
  155. hw_addr.resize(hlen);
  156. hwaddr_ = HWAddrPtr(new HWAddr(hw_addr, htype));
  157. if (bufferIn.getLength() == bufferIn.getPosition()) {
  158. // this is *NOT* DHCP packet. It does not have any DHCPv4 options. In
  159. // particular, it does not have magic cookie, a 4 byte sequence that
  160. // differentiates between DHCP and BOOTP packets.
  161. isc_throw(InvalidOperation, "Received BOOTP packet. BOOTP is not supported.");
  162. }
  163. if (bufferIn.getLength() - bufferIn.getPosition() < 4) {
  164. // there is not enough data to hold magic DHCP cookie
  165. isc_throw(Unexpected, "Truncated or no DHCP packet.");
  166. }
  167. uint32_t magic = bufferIn.readUint32();
  168. if (magic != DHCP_OPTIONS_COOKIE) {
  169. isc_throw(Unexpected, "Invalid or missing DHCP magic cookie");
  170. }
  171. size_t opts_len = bufferIn.getLength() - bufferIn.getPosition();
  172. vector<uint8_t> optsBuffer;
  173. // First use of readVector.
  174. bufferIn.readVector(optsBuffer, opts_len);
  175. LibDHCP::unpackOptions4(optsBuffer, options_);
  176. // @todo check will need to be called separately, so hooks can be called
  177. // after the packet is parsed, but before its content is verified
  178. check();
  179. }
  180. void Pkt4::check() {
  181. uint8_t msg_type = getType();
  182. if (msg_type > DHCPLEASEACTIVE) {
  183. isc_throw(BadValue, "Invalid DHCP message type received: "
  184. << static_cast<int>(msg_type));
  185. }
  186. }
  187. uint8_t Pkt4::getType() const {
  188. OptionPtr generic = getOption(DHO_DHCP_MESSAGE_TYPE);
  189. if (!generic) {
  190. isc_throw(Unexpected, "Missing DHCP Message Type option");
  191. }
  192. // Check if Message Type is specified as OptionInt<uint8_t>
  193. boost::shared_ptr<OptionInt<uint8_t> > type_opt =
  194. boost::dynamic_pointer_cast<OptionInt<uint8_t> >(generic);
  195. if (type_opt) {
  196. return (type_opt->getValue());
  197. }
  198. // Try to use it as generic option
  199. return (generic->getUint8());
  200. }
  201. void Pkt4::setType(uint8_t dhcp_type) {
  202. OptionPtr opt = getOption(DHO_DHCP_MESSAGE_TYPE);
  203. if (opt) {
  204. // There is message type option already, update it
  205. opt->setUint8(dhcp_type);
  206. } else {
  207. // There is no message type option yet, add it
  208. std::vector<uint8_t> tmp(1, dhcp_type);
  209. opt = OptionPtr(new Option(Option::V4, DHO_DHCP_MESSAGE_TYPE, tmp));
  210. addOption(opt);
  211. }
  212. }
  213. void Pkt4::repack() {
  214. bufferOut_.writeData(&data_[0], data_.size());
  215. }
  216. std::string
  217. Pkt4::toText() {
  218. stringstream tmp;
  219. tmp << "localAddr=" << local_addr_.toText() << ":" << local_port_
  220. << " remoteAddr=" << remote_addr_.toText()
  221. << ":" << remote_port_ << ", msgtype=" << static_cast<int>(getType())
  222. << ", transid=0x" << hex << transid_ << dec << endl;
  223. for (isc::dhcp::Option::OptionCollection::iterator opt=options_.begin();
  224. opt != options_.end();
  225. ++opt) {
  226. tmp << " " << opt->second->toText() << std::endl;
  227. }
  228. return tmp.str();
  229. }
  230. void
  231. Pkt4::setHWAddr(uint8_t hType, uint8_t hlen,
  232. const std::vector<uint8_t>& mac_addr) {
  233. /// @todo Rewrite this once support for client-identifier option
  234. /// is implemented (ticket 1228?)
  235. if (hlen > MAX_CHADDR_LEN) {
  236. isc_throw(OutOfRange, "Hardware address (len=" << hlen
  237. << " too long. Max " << MAX_CHADDR_LEN << " supported.");
  238. } else if (mac_addr.empty() && (hlen > 0) ) {
  239. isc_throw(OutOfRange, "Invalid HW Address specified");
  240. }
  241. hwaddr_.reset(new HWAddr(mac_addr, hType));
  242. }
  243. void
  244. Pkt4::setHWAddr(const HWAddrPtr& addr) {
  245. if (!addr) {
  246. isc_throw(BadValue, "Setting hw address to NULL is forbidden");
  247. }
  248. hwaddr_ = addr;
  249. }
  250. void
  251. Pkt4::setSname(const uint8_t* sname, size_t snameLen /*= MAX_SNAME_LEN*/) {
  252. if (snameLen > MAX_SNAME_LEN) {
  253. isc_throw(OutOfRange, "sname field (len=" << snameLen
  254. << ") too long, Max " << MAX_SNAME_LEN << " supported.");
  255. } else if (sname == NULL) {
  256. isc_throw(InvalidParameter, "Invalid sname specified");
  257. }
  258. std::copy(&sname[0], &sname[snameLen], &sname_[0]);
  259. std::fill(&sname_[snameLen], &sname_[MAX_SNAME_LEN], 0);
  260. // No need to store snameLen as any empty space is filled with 0s
  261. }
  262. void
  263. Pkt4::setFile(const uint8_t* file, size_t fileLen /*= MAX_FILE_LEN*/) {
  264. if (fileLen > MAX_FILE_LEN) {
  265. isc_throw(OutOfRange, "file field (len=" << fileLen
  266. << ") too long, Max " << MAX_FILE_LEN << " supported.");
  267. } else if (file == NULL) {
  268. isc_throw(InvalidParameter, "Invalid file name specified");
  269. }
  270. std::copy(&file[0], &file[fileLen], &file_[0]);
  271. std::fill(&file_[fileLen], &file_[MAX_FILE_LEN], 0);
  272. // No need to store fileLen as any empty space is filled with 0s
  273. }
  274. uint8_t
  275. Pkt4::DHCPTypeToBootpType(uint8_t dhcpType) {
  276. switch (dhcpType) {
  277. case DHCPDISCOVER:
  278. case DHCPREQUEST:
  279. case DHCPDECLINE:
  280. case DHCPRELEASE:
  281. case DHCPINFORM:
  282. case DHCPLEASEQUERY:
  283. return (BOOTREQUEST);
  284. case DHCPACK:
  285. case DHCPNAK:
  286. case DHCPOFFER:
  287. case DHCPLEASEUNASSIGNED:
  288. case DHCPLEASEUNKNOWN:
  289. case DHCPLEASEACTIVE:
  290. return (BOOTREPLY);
  291. default:
  292. isc_throw(OutOfRange, "Invalid message type: "
  293. << static_cast<int>(dhcpType) );
  294. }
  295. }
  296. uint8_t
  297. Pkt4::getHtype() const {
  298. if (!hwaddr_) {
  299. isc_throw(InvalidOperation, "Can't get HType. HWAddr not defined");
  300. }
  301. return (hwaddr_->htype_);
  302. }
  303. uint8_t
  304. Pkt4::getHlen() const {
  305. if (!hwaddr_) {
  306. isc_throw(InvalidOperation, "Can't get HType. HWAddr not defined");
  307. }
  308. uint8_t len = hwaddr_->hwaddr_.size();
  309. return (len <= MAX_CHADDR_LEN ? len : MAX_CHADDR_LEN);
  310. }
  311. void
  312. Pkt4::addOption(boost::shared_ptr<Option> opt) {
  313. // Check for uniqueness (DHCPv4 options must be unique)
  314. if (getOption(opt->getType())) {
  315. isc_throw(BadValue, "Option " << opt->getType()
  316. << " already present in this message.");
  317. }
  318. options_.insert(pair<int, boost::shared_ptr<Option> >(opt->getType(), opt));
  319. }
  320. boost::shared_ptr<isc::dhcp::Option>
  321. Pkt4::getOption(uint8_t type) const {
  322. Option::OptionCollection::const_iterator x = options_.find(type);
  323. if (x != options_.end()) {
  324. return (*x).second;
  325. }
  326. return boost::shared_ptr<isc::dhcp::Option>(); // NULL
  327. }
  328. bool
  329. Pkt4::delOption(uint8_t type) {
  330. isc::dhcp::Option::OptionCollection::iterator x = options_.find(type);
  331. if (x != options_.end()) {
  332. options_.erase(x);
  333. return (true); // delete successful
  334. }
  335. return (false); // can't find option to be deleted
  336. }
  337. void
  338. Pkt4::updateTimestamp() {
  339. timestamp_ = boost::posix_time::microsec_clock::universal_time();
  340. }
  341. } // end of namespace isc::dhcp
  342. } // end of namespace isc