pkt4.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_(-1),
  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. bool
  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. return (true);
  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. // fist use of readVector
  153. bufferIn.readVector(optsBuffer, opts_len);
  154. LibDHCP::unpackOptions4(optsBuffer, options_);
  155. return (true);
  156. }
  157. void Pkt4::check() {
  158. boost::shared_ptr<Option> typeOpt = getOption(DHO_DHCP_MESSAGE_TYPE);
  159. if (typeOpt) {
  160. uint8_t msg_type = typeOpt->getUint8();
  161. if (msg_type>DHCPLEASEACTIVE) {
  162. isc_throw(BadValue, "Invalid DHCP message type received:" << msg_type);
  163. }
  164. msg_type_ = msg_type;
  165. } else {
  166. isc_throw(Unexpected, "Missing DHCP Message Type option");
  167. }
  168. }
  169. void Pkt4::repack() {
  170. cout << "Convering RX packet to TX packet: " << data_.size() << " bytes." << endl;
  171. bufferOut_.writeData(&data_[0], data_.size());
  172. }
  173. std::string
  174. Pkt4::toText() {
  175. stringstream tmp;
  176. tmp << "localAddr=" << local_addr_.toText() << ":" << local_port_
  177. << " remoteAddr=" << remote_addr_.toText()
  178. << ":" << remote_port_ << ", msgtype=" << int(msg_type_)
  179. << ", transid=0x" << hex << transid_ << dec << endl;
  180. for (isc::dhcp::Option::OptionCollection::iterator opt=options_.begin();
  181. opt != options_.end();
  182. ++opt) {
  183. tmp << " " << opt->second->toText() << std::endl;
  184. }
  185. return tmp.str();
  186. }
  187. void
  188. Pkt4::setHWAddr(uint8_t hType, uint8_t hlen,
  189. const std::vector<uint8_t>& macAddr) {
  190. /// TODO Rewrite this once support for client-identifier option
  191. /// is implemented (ticket 1228?)
  192. if (hlen>MAX_CHADDR_LEN) {
  193. isc_throw(OutOfRange, "Hardware address (len=" << hlen
  194. << " too long. Max " << MAX_CHADDR_LEN << " supported.");
  195. }
  196. if ( (macAddr.size() == 0) && (hlen > 0) ) {
  197. isc_throw(OutOfRange, "Invalid HW Address specified");
  198. }
  199. htype_ = hType;
  200. hlen_ = hlen;
  201. memset(chaddr_, 0, MAX_CHADDR_LEN);
  202. memcpy(chaddr_, &macAddr[0], hlen);
  203. }
  204. void
  205. Pkt4::setSname(const uint8_t* sname, size_t snameLen /*= MAX_SNAME_LEN*/) {
  206. if (snameLen > MAX_SNAME_LEN) {
  207. isc_throw(OutOfRange, "sname field (len=" << snameLen
  208. << ") too long, Max " << MAX_SNAME_LEN << " supported.");
  209. }
  210. memset(sname_, 0, MAX_SNAME_LEN);
  211. memcpy(sname_, sname, snameLen);
  212. // no need to store snameLen as any empty space is filled with 0s
  213. }
  214. void
  215. Pkt4::setFile(const uint8_t* file, size_t fileLen /*= MAX_FILE_LEN*/) {
  216. if (fileLen > MAX_FILE_LEN) {
  217. isc_throw(OutOfRange, "file field (len=" << fileLen
  218. << ") too long, Max " << MAX_FILE_LEN << " supported.");
  219. }
  220. memset(file_, 0, MAX_FILE_LEN);
  221. memcpy(file_, file, fileLen);
  222. // no need to store fileLen as any empty space is filled with 0s
  223. }
  224. uint8_t
  225. Pkt4::DHCPTypeToBootpType(uint8_t dhcpType) {
  226. switch (dhcpType) {
  227. case DHCPDISCOVER:
  228. case DHCPREQUEST:
  229. case DHCPDECLINE:
  230. case DHCPRELEASE:
  231. case DHCPINFORM:
  232. case DHCPLEASEQUERY:
  233. return (BOOTREQUEST);
  234. case DHCPACK:
  235. case DHCPNAK:
  236. case DHCPOFFER:
  237. case DHCPLEASEUNASSIGNED:
  238. case DHCPLEASEUNKNOWN:
  239. case DHCPLEASEACTIVE:
  240. return (BOOTREPLY);
  241. default:
  242. isc_throw(OutOfRange, "Invalid message type: "
  243. << static_cast<int>(dhcpType) );
  244. }
  245. }
  246. void
  247. Pkt4::addOption(boost::shared_ptr<Option> opt) {
  248. // check for uniqueness (DHCPv4 options must be unique)
  249. if (getOption(opt->getType())) {
  250. isc_throw(BadValue, "Option " << opt->getType()
  251. << " already present in this message.");
  252. }
  253. options_.insert(pair<int, boost::shared_ptr<Option> >(opt->getType(), opt));
  254. }
  255. boost::shared_ptr<isc::dhcp::Option>
  256. Pkt4::getOption(uint8_t type) {
  257. Option::OptionCollection::const_iterator x = options_.find(type);
  258. if (x!=options_.end()) {
  259. return (*x).second;
  260. }
  261. return boost::shared_ptr<isc::dhcp::Option>(); // NULL
  262. }
  263. } // end of namespace isc::dhcp
  264. } // end of namespace isc