pkt4.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // Copyright (C) 2011 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 <dhcp/pkt4.h>
  15. #include <dhcp/libdhcp++.h>
  16. #include <dhcp/dhcp4.h>
  17. #include <exceptions/exceptions.h>
  18. #include <asiolink/io_address.h>
  19. #include <iostream>
  20. #include <sstream>
  21. using namespace std;
  22. using namespace isc::dhcp;
  23. using namespace isc::asiolink;
  24. namespace isc {
  25. namespace dhcp {
  26. const IOAddress DEFAULT_ADDRESS("0.0.0.0");
  27. Pkt4::Pkt4(uint8_t msg_type, uint32_t transid)
  28. :local_addr_(DEFAULT_ADDRESS),
  29. remote_addr_(DEFAULT_ADDRESS),
  30. iface_(""),
  31. ifindex_(0),
  32. local_port_(DHCP4_SERVER_PORT),
  33. remote_port_(DHCP4_CLIENT_PORT),
  34. op_(DHCPTypeToBootpType(msg_type)),
  35. htype_(HTYPE_ETHER),
  36. hlen_(0),
  37. hops_(0),
  38. transid_(transid),
  39. secs_(0),
  40. flags_(0),
  41. ciaddr_(DEFAULT_ADDRESS),
  42. yiaddr_(DEFAULT_ADDRESS),
  43. siaddr_(DEFAULT_ADDRESS),
  44. giaddr_(DEFAULT_ADDRESS),
  45. bufferOut_(DHCPV4_PKT_HDR_LEN),
  46. msg_type_(msg_type)
  47. {
  48. memset(chaddr_, 0, MAX_CHADDR_LEN);
  49. memset(sname_, 0, MAX_SNAME_LEN);
  50. memset(file_, 0, MAX_FILE_LEN);
  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. transid_(0),
  61. secs_(0),
  62. flags_(0),
  63. ciaddr_(DEFAULT_ADDRESS),
  64. yiaddr_(DEFAULT_ADDRESS),
  65. siaddr_(DEFAULT_ADDRESS),
  66. giaddr_(DEFAULT_ADDRESS),
  67. bufferOut_(0), // not used, this is RX packet
  68. msg_type_(DHCPDISCOVER)
  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. }
  75. data_.resize(len);
  76. memcpy(&data_[0], data, len);
  77. }
  78. size_t
  79. Pkt4::len() {
  80. size_t length = DHCPV4_PKT_HDR_LEN; // DHCPv4 header
  81. // ... and sum of lengths of all options
  82. for (Option::OptionCollection::const_iterator it = options_.begin();
  83. it != options_.end();
  84. ++it) {
  85. length += (*it).second->len();
  86. }
  87. return (length);
  88. }
  89. bool
  90. Pkt4::pack() {
  91. bufferOut_.writeUint8(op_);
  92. bufferOut_.writeUint8(htype_);
  93. bufferOut_.writeUint8(hlen_);
  94. bufferOut_.writeUint8(hops_);
  95. bufferOut_.writeUint32(transid_);
  96. bufferOut_.writeUint16(secs_);
  97. bufferOut_.writeUint16(flags_);
  98. bufferOut_.writeUint32(ciaddr_);
  99. bufferOut_.writeUint32(yiaddr_);
  100. bufferOut_.writeUint32(siaddr_);
  101. bufferOut_.writeUint32(giaddr_);
  102. bufferOut_.writeData(chaddr_, MAX_CHADDR_LEN);
  103. bufferOut_.writeData(sname_, MAX_SNAME_LEN);
  104. bufferOut_.writeData(file_, MAX_FILE_LEN);
  105. // write DHCP magic cookie
  106. bufferOut_.writeUint32(DHCP_OPTIONS_COOKIE);
  107. LibDHCP::packOptions(bufferOut_, options_);
  108. // add END option that indicates end of options
  109. // (End option is very simple, just a 255 octet)
  110. bufferOut_.writeUint8(DHO_END);
  111. return (true);
  112. }
  113. void
  114. Pkt4::unpack() {
  115. // input buffer (used during message reception)
  116. isc::util::InputBuffer bufferIn(&data_[0], data_.size());
  117. if (bufferIn.getLength() < DHCPV4_PKT_HDR_LEN) {
  118. isc_throw(OutOfRange, "Received truncated DHCPv4 packet (len="
  119. << bufferIn.getLength() << " received, at least "
  120. << DHCPV4_PKT_HDR_LEN << "is expected");
  121. }
  122. op_ = bufferIn.readUint8();
  123. htype_ = bufferIn.readUint8();
  124. hlen_ = bufferIn.readUint8();
  125. hops_ = bufferIn.readUint8();
  126. transid_ = bufferIn.readUint32();
  127. secs_ = bufferIn.readUint16();
  128. flags_ = bufferIn.readUint16();
  129. ciaddr_ = IOAddress(bufferIn.readUint32());
  130. yiaddr_ = IOAddress(bufferIn.readUint32());
  131. siaddr_ = IOAddress(bufferIn.readUint32());
  132. giaddr_ = IOAddress(bufferIn.readUint32());
  133. bufferIn.readData(chaddr_, MAX_CHADDR_LEN);
  134. bufferIn.readData(sname_, MAX_SNAME_LEN);
  135. bufferIn.readData(file_, MAX_FILE_LEN);
  136. if (bufferIn.getLength() == bufferIn.getPosition()) {
  137. // this is *NOT* DHCP packet. It does not have any DHCPv4 options. In
  138. // particular, it does not have magic cookie, a 4 byte sequence that
  139. // differentiates between DHCP and BOOTP packets.
  140. isc_throw(InvalidOperation, "Recevied BOOTP packet. BOOTP is not supported.");
  141. }
  142. if (bufferIn.getLength() - bufferIn.getPosition() < 4) {
  143. // there is not enough data to hold magic DHCP cookie
  144. isc_throw(Unexpected, "Truncated or no DHCP packet.");
  145. }
  146. uint32_t magic = bufferIn.readUint32();
  147. if (magic != DHCP_OPTIONS_COOKIE) {
  148. isc_throw(Unexpected, "Invalid or missing DHCP magic cookie");
  149. }
  150. size_t opts_len = bufferIn.getLength() - bufferIn.getPosition();
  151. vector<uint8_t> optsBuffer;
  152. // First use of readVector.
  153. bufferIn.readVector(optsBuffer, opts_len);
  154. LibDHCP::unpackOptions4(optsBuffer, options_);
  155. // TODO: check will need to be called separately, so hooks can be called after
  156. // packet is parsed, but before its content is verified
  157. check();
  158. }
  159. void Pkt4::check() {
  160. boost::shared_ptr<Option> typeOpt = getOption(DHO_DHCP_MESSAGE_TYPE);
  161. if (typeOpt) {
  162. uint8_t msg_type = typeOpt->getUint8();
  163. if (msg_type>DHCPLEASEACTIVE) {
  164. isc_throw(BadValue, "Invalid DHCP message type received:" << msg_type);
  165. }
  166. msg_type_ = msg_type;
  167. } else {
  168. isc_throw(Unexpected, "Missing DHCP Message Type option");
  169. }
  170. }
  171. void Pkt4::repack() {
  172. bufferOut_.writeData(&data_[0], data_.size());
  173. }
  174. std::string
  175. Pkt4::toText() {
  176. stringstream tmp;
  177. tmp << "localAddr=" << local_addr_.toText() << ":" << local_port_
  178. << " remoteAddr=" << remote_addr_.toText()
  179. << ":" << remote_port_ << ", msgtype=" << int(msg_type_)
  180. << ", transid=0x" << hex << transid_ << dec << endl;
  181. for (isc::dhcp::Option::OptionCollection::iterator opt=options_.begin();
  182. opt != options_.end();
  183. ++opt) {
  184. tmp << " " << opt->second->toText() << std::endl;
  185. }
  186. return tmp.str();
  187. }
  188. void
  189. Pkt4::setHWAddr(uint8_t hType, uint8_t hlen,
  190. const std::vector<uint8_t>& macAddr) {
  191. /// TODO Rewrite this once support for client-identifier option
  192. /// is implemented (ticket 1228?)
  193. if (hlen>MAX_CHADDR_LEN) {
  194. isc_throw(OutOfRange, "Hardware address (len=" << hlen
  195. << " too long. Max " << MAX_CHADDR_LEN << " supported.");
  196. }
  197. if ( (macAddr.size() == 0) && (hlen > 0) ) {
  198. isc_throw(OutOfRange, "Invalid HW Address specified");
  199. }
  200. htype_ = hType;
  201. hlen_ = hlen;
  202. memset(chaddr_, 0, MAX_CHADDR_LEN);
  203. memcpy(chaddr_, &macAddr[0], hlen);
  204. }
  205. void
  206. Pkt4::setSname(const uint8_t* sname, size_t snameLen /*= MAX_SNAME_LEN*/) {
  207. if (snameLen > MAX_SNAME_LEN) {
  208. isc_throw(OutOfRange, "sname field (len=" << snameLen
  209. << ") too long, Max " << MAX_SNAME_LEN << " supported.");
  210. }
  211. memset(sname_, 0, MAX_SNAME_LEN);
  212. memcpy(sname_, sname, snameLen);
  213. // no need to store snameLen as any empty space is filled with 0s
  214. }
  215. void
  216. Pkt4::setFile(const uint8_t* file, size_t fileLen /*= MAX_FILE_LEN*/) {
  217. if (fileLen > MAX_FILE_LEN) {
  218. isc_throw(OutOfRange, "file field (len=" << fileLen
  219. << ") too long, Max " << MAX_FILE_LEN << " supported.");
  220. }
  221. memset(file_, 0, MAX_FILE_LEN);
  222. memcpy(file_, file, fileLen);
  223. // no need to store fileLen as any empty space is filled with 0s
  224. }
  225. uint8_t
  226. Pkt4::DHCPTypeToBootpType(uint8_t dhcpType) {
  227. switch (dhcpType) {
  228. case DHCPDISCOVER:
  229. case DHCPREQUEST:
  230. case DHCPDECLINE:
  231. case DHCPRELEASE:
  232. case DHCPINFORM:
  233. case DHCPLEASEQUERY:
  234. return (BOOTREQUEST);
  235. case DHCPACK:
  236. case DHCPNAK:
  237. case DHCPOFFER:
  238. case DHCPLEASEUNASSIGNED:
  239. case DHCPLEASEUNKNOWN:
  240. case DHCPLEASEACTIVE:
  241. return (BOOTREPLY);
  242. default:
  243. isc_throw(OutOfRange, "Invalid message type: "
  244. << static_cast<int>(dhcpType) );
  245. }
  246. }
  247. void
  248. Pkt4::addOption(boost::shared_ptr<Option> opt) {
  249. // check for uniqueness (DHCPv4 options must be unique)
  250. if (getOption(opt->getType())) {
  251. isc_throw(BadValue, "Option " << opt->getType()
  252. << " already present in this message.");
  253. }
  254. options_.insert(pair<int, boost::shared_ptr<Option> >(opt->getType(), opt));
  255. }
  256. boost::shared_ptr<isc::dhcp::Option>
  257. Pkt4::getOption(uint8_t type) {
  258. Option::OptionCollection::const_iterator x = options_.find(type);
  259. if (x!=options_.end()) {
  260. return (*x).second;
  261. }
  262. return boost::shared_ptr<isc::dhcp::Option>(); // NULL
  263. }
  264. void
  265. Pkt4::updateTimestamp() {
  266. timestamp_ = boost::posix_time::microsec_clock::universal_time();
  267. }
  268. } // end of namespace isc::dhcp
  269. } // end of namespace isc