dhcp4_srv.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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/iface_mgr.h>
  17. #include <dhcp/option4_addrlst.h>
  18. #include <dhcp/option_int.h>
  19. #include <dhcp/option_int_array.h>
  20. #include <dhcp/pkt4.h>
  21. #include <dhcp/duid.h>
  22. #include <dhcp/hwaddr.h>
  23. #include <dhcp4/dhcp4_log.h>
  24. #include <dhcp4/dhcp4_srv.h>
  25. #include <dhcpsrv/utils.h>
  26. #include <dhcpsrv/cfgmgr.h>
  27. #include <dhcpsrv/lease_mgr.h>
  28. #include <dhcpsrv/lease_mgr_factory.h>
  29. #include <dhcpsrv/subnet.h>
  30. #include <dhcpsrv/utils.h>
  31. #include <dhcpsrv/addr_utilities.h>
  32. #include <boost/algorithm/string/erase.hpp>
  33. #include <iomanip>
  34. #include <fstream>
  35. using namespace isc;
  36. using namespace isc::asiolink;
  37. using namespace isc::dhcp;
  38. using namespace isc::log;
  39. using namespace std;
  40. namespace isc {
  41. namespace dhcp {
  42. /// @brief file name of a server-id file
  43. ///
  44. /// Server must store its server identifier in persistent storage that must not
  45. /// change between restarts. This is name of the file that is created in dataDir
  46. /// (see isc::dhcp::CfgMgr::getDataDir()). It is a text file that uses
  47. /// regular IPv4 address, e.g. 192.0.2.1. Server will create it during
  48. /// first run and then use it afterwards.
  49. static const char* SERVER_ID_FILE = "b10-dhcp4-serverid";
  50. // These are hardcoded parameters. Currently this is a skeleton server that only
  51. // grants those options and a single, fixed, hardcoded lease.
  52. Dhcpv4Srv::Dhcpv4Srv(uint16_t port, const char* dbconfig) {
  53. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_OPEN_SOCKET).arg(port);
  54. try {
  55. // First call to instance() will create IfaceMgr (it's a singleton)
  56. // it may throw something if things go wrong
  57. IfaceMgr::instance();
  58. if (port) {
  59. // open sockets only if port is non-zero. Port 0 is used
  60. // for non-socket related testing.
  61. IfaceMgr::instance().openSockets4(port);
  62. }
  63. string srvid_file = CfgMgr::instance().getDataDir() + "/" + string(SERVER_ID_FILE);
  64. if (loadServerID(srvid_file)) {
  65. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_SERVERID_LOADED)
  66. .arg(srvidToString(getServerID()))
  67. .arg(srvid_file);
  68. } else {
  69. generateServerID();
  70. LOG_INFO(dhcp4_logger, DHCP4_SERVERID_GENERATED)
  71. .arg(srvidToString(getServerID()))
  72. .arg(srvid_file);
  73. if (!writeServerID(srvid_file)) {
  74. LOG_WARN(dhcp4_logger, DHCP4_SERVERID_WRITE_FAIL)
  75. .arg(srvid_file);
  76. }
  77. }
  78. // Instantiate LeaseMgr
  79. LeaseMgrFactory::create(dbconfig);
  80. LOG_INFO(dhcp4_logger, DHCP4_DB_BACKEND_STARTED)
  81. .arg(LeaseMgrFactory::instance().getType())
  82. .arg(LeaseMgrFactory::instance().getName());
  83. // Instantiate allocation engine
  84. alloc_engine_.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100));
  85. } catch (const std::exception &e) {
  86. LOG_ERROR(dhcp4_logger, DHCP4_SRV_CONSTRUCT_ERROR).arg(e.what());
  87. shutdown_ = true;
  88. return;
  89. }
  90. shutdown_ = false;
  91. }
  92. Dhcpv4Srv::~Dhcpv4Srv() {
  93. IfaceMgr::instance().closeSockets();
  94. }
  95. void
  96. Dhcpv4Srv::shutdown() {
  97. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_SHUTDOWN_REQUEST);
  98. shutdown_ = true;
  99. }
  100. bool
  101. Dhcpv4Srv::run() {
  102. while (!shutdown_) {
  103. /// @todo: calculate actual timeout once we have lease database
  104. int timeout = 1000;
  105. // client's message and server's response
  106. Pkt4Ptr query;
  107. Pkt4Ptr rsp;
  108. try {
  109. query = IfaceMgr::instance().receive4(timeout);
  110. } catch (const std::exception& e) {
  111. LOG_ERROR(dhcp4_logger, DHCP4_PACKET_RECEIVE_FAIL).arg(e.what());
  112. }
  113. if (query) {
  114. try {
  115. query->unpack();
  116. } catch (const std::exception& e) {
  117. // Failed to parse the packet.
  118. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL,
  119. DHCP4_PACKET_PARSE_FAIL).arg(e.what());
  120. continue;
  121. }
  122. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_PACKET_RECEIVED)
  123. .arg(serverReceivedPacketName(query->getType()))
  124. .arg(query->getType())
  125. .arg(query->getIface());
  126. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_QUERY_DATA)
  127. .arg(query->toText());
  128. switch (query->getType()) {
  129. case DHCPDISCOVER:
  130. rsp = processDiscover(query);
  131. break;
  132. case DHCPREQUEST:
  133. rsp = processRequest(query);
  134. break;
  135. case DHCPRELEASE:
  136. processRelease(query);
  137. break;
  138. case DHCPDECLINE:
  139. processDecline(query);
  140. break;
  141. case DHCPINFORM:
  142. processInform(query);
  143. break;
  144. default:
  145. // Only action is to output a message if debug is enabled,
  146. // and that will be covered by the debug statement before
  147. // the "switch" statement.
  148. ;
  149. }
  150. if (rsp) {
  151. if (rsp->getRemoteAddr().toText() == "0.0.0.0") {
  152. rsp->setRemoteAddr(query->getRemoteAddr());
  153. }
  154. if (!rsp->getHops()) {
  155. rsp->setRemotePort(DHCP4_CLIENT_PORT);
  156. } else {
  157. rsp->setRemotePort(DHCP4_SERVER_PORT);
  158. }
  159. rsp->setLocalAddr(query->getLocalAddr());
  160. rsp->setLocalPort(DHCP4_SERVER_PORT);
  161. rsp->setIface(query->getIface());
  162. rsp->setIndex(query->getIndex());
  163. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL_DATA,
  164. DHCP4_RESPONSE_DATA)
  165. .arg(rsp->getType()).arg(rsp->toText());
  166. if (rsp->pack()) {
  167. try {
  168. IfaceMgr::instance().send(rsp);
  169. } catch (const std::exception& e) {
  170. LOG_ERROR(dhcp4_logger, DHCP4_PACKET_SEND_FAIL).arg(e.what());
  171. }
  172. } else {
  173. LOG_ERROR(dhcp4_logger, DHCP4_PACK_FAIL);
  174. }
  175. }
  176. }
  177. }
  178. return (true);
  179. }
  180. bool
  181. Dhcpv4Srv::loadServerID(const std::string& file_name) {
  182. // load content of the file into a string
  183. fstream f(file_name.c_str(), ios::in);
  184. if (!f.is_open()) {
  185. return (false);
  186. }
  187. string hex_string;
  188. f >> hex_string;
  189. f.close();
  190. // remove any spaces
  191. boost::algorithm::erase_all(hex_string, " ");
  192. try {
  193. IOAddress addr(hex_string);
  194. if (!addr.isV4()) {
  195. return (false);
  196. }
  197. // Now create server-id option
  198. serverid_.reset(new Option4AddrLst(DHO_DHCP_SERVER_IDENTIFIER, addr));
  199. } catch(...) {
  200. // any kind of malformed input (empty string, IPv6 address, complete
  201. // garbate etc.)
  202. return (false);
  203. }
  204. return (true);
  205. }
  206. void
  207. Dhcpv4Srv::generateServerID() {
  208. const IfaceMgr::IfaceCollection& ifaces = IfaceMgr::instance().getIfaces();
  209. // Let's find suitable interface.
  210. for (IfaceMgr::IfaceCollection::const_iterator iface = ifaces.begin();
  211. iface != ifaces.end(); ++iface) {
  212. // Let's don't use loopback.
  213. if (iface->flag_loopback_) {
  214. continue;
  215. }
  216. // Let's skip downed interfaces. It is better to use working ones.
  217. if (!iface->flag_up_) {
  218. continue;
  219. }
  220. const IfaceMgr::AddressCollection addrs = iface->getAddresses();
  221. for (IfaceMgr::AddressCollection::const_iterator addr = addrs.begin();
  222. addr != addrs.end(); ++addr) {
  223. if (addr->getFamily() != AF_INET) {
  224. continue;
  225. }
  226. serverid_ = OptionPtr(new Option4AddrLst(DHO_DHCP_SERVER_IDENTIFIER,
  227. *addr));
  228. return;
  229. }
  230. }
  231. isc_throw(BadValue, "No suitable interfaces for server-identifier found");
  232. }
  233. bool
  234. Dhcpv4Srv::writeServerID(const std::string& file_name) {
  235. fstream f(file_name.c_str(), ios::out | ios::trunc);
  236. if (!f.good()) {
  237. return (false);
  238. }
  239. f << srvidToString(getServerID());
  240. f.close();
  241. return (true);
  242. }
  243. string
  244. Dhcpv4Srv::srvidToString(const OptionPtr& srvid) {
  245. if (!srvid) {
  246. isc_throw(BadValue, "NULL pointer passed to srvidToString()");
  247. }
  248. boost::shared_ptr<Option4AddrLst> generated =
  249. boost::dynamic_pointer_cast<Option4AddrLst>(srvid);
  250. if (!srvid) {
  251. isc_throw(BadValue, "Pointer to invalid option passed to srvidToString()");
  252. }
  253. Option4AddrLst::AddressContainer addrs = generated->getAddresses();
  254. if (addrs.size() != 1) {
  255. isc_throw(BadValue, "Malformed option passed to srvidToString(). "
  256. << "Expected to contain a single IPv4 address.");
  257. }
  258. return (addrs[0].toText());
  259. }
  260. void
  261. Dhcpv4Srv::copyDefaultFields(const Pkt4Ptr& question, Pkt4Ptr& answer) {
  262. answer->setIface(question->getIface());
  263. answer->setIndex(question->getIndex());
  264. answer->setCiaddr(question->getCiaddr());
  265. answer->setSiaddr(IOAddress("0.0.0.0")); // explictly set this to 0
  266. answer->setHops(question->getHops());
  267. // copy MAC address
  268. answer->setHWAddr(question->getHWAddr());
  269. // relay address
  270. answer->setGiaddr(question->getGiaddr());
  271. if (question->getGiaddr().toText() != "0.0.0.0") {
  272. // relayed traffic
  273. answer->setRemoteAddr(question->getGiaddr());
  274. } else {
  275. // direct traffic
  276. answer->setRemoteAddr(question->getRemoteAddr());
  277. }
  278. // Let's copy client-id to response. See RFC6842.
  279. OptionPtr client_id = question->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
  280. if (client_id) {
  281. answer->addOption(client_id);
  282. }
  283. }
  284. void
  285. Dhcpv4Srv::appendDefaultOptions(Pkt4Ptr& msg, uint8_t msg_type) {
  286. OptionPtr opt;
  287. // add Message Type Option (type 53)
  288. msg->setType(msg_type);
  289. // DHCP Server Identifier (type 54)
  290. msg->addOption(getServerID());
  291. // more options will be added here later
  292. }
  293. void
  294. Dhcpv4Srv::appendRequestedOptions(const Pkt4Ptr& question, Pkt4Ptr& msg) {
  295. // Get the subnet relevant for the client. We will need it
  296. // to get the options associated with it.
  297. Subnet4Ptr subnet = selectSubnet(question);
  298. // If we can't find the subnet for the client there is no way
  299. // to get the options to be sent to a client. We don't log an
  300. // error because it will be logged by the assignLease method
  301. // anyway.
  302. if (!subnet) {
  303. return;
  304. }
  305. // try to get the 'Parameter Request List' option which holds the
  306. // codes of requested options.
  307. OptionUint8ArrayPtr option_prl = boost::dynamic_pointer_cast<
  308. OptionUint8Array>(question->getOption(DHO_DHCP_PARAMETER_REQUEST_LIST));
  309. // If there is no PRL option in the message from the client then
  310. // there is nothing to do.
  311. if (!option_prl) {
  312. return;
  313. }
  314. // Get the codes of requested options.
  315. const std::vector<uint8_t>& requested_opts = option_prl->getValues();
  316. // For each requested option code get the instance of the option
  317. // to be returned to the client.
  318. for (std::vector<uint8_t>::const_iterator opt = requested_opts.begin();
  319. opt != requested_opts.end(); ++opt) {
  320. Subnet::OptionDescriptor desc =
  321. subnet->getOptionDescriptor("dhcp4", *opt);
  322. if (desc.option) {
  323. msg->addOption(desc.option);
  324. }
  325. }
  326. }
  327. void
  328. Dhcpv4Srv::appendBasicOptions(const Pkt4Ptr& question, Pkt4Ptr& msg) {
  329. // Identify options that we always want to send to the
  330. // client (if they are configured).
  331. static const uint16_t required_options[] = {
  332. DHO_SUBNET_MASK,
  333. DHO_ROUTERS,
  334. DHO_DOMAIN_NAME_SERVERS,
  335. DHO_DOMAIN_NAME };
  336. static size_t required_options_size =
  337. sizeof(required_options) / sizeof(required_options[0]);
  338. // Get the subnet.
  339. Subnet4Ptr subnet = selectSubnet(question);
  340. if (!subnet) {
  341. return;
  342. }
  343. // Try to find all 'required' options in the outgoing
  344. // message. Those that are not present will be added.
  345. for (int i = 0; i < required_options_size; ++i) {
  346. OptionPtr opt = msg->getOption(required_options[i]);
  347. if (!opt) {
  348. // Check whether option has been configured.
  349. Subnet::OptionDescriptor desc =
  350. subnet->getOptionDescriptor("dhcp4", required_options[i]);
  351. if (desc.option) {
  352. msg->addOption(desc.option);
  353. }
  354. }
  355. }
  356. }
  357. void
  358. Dhcpv4Srv::assignLease(const Pkt4Ptr& question, Pkt4Ptr& answer) {
  359. // We need to select a subnet the client is connected in.
  360. Subnet4Ptr subnet = selectSubnet(question);
  361. if (!subnet) {
  362. // This particular client is out of luck today. We do not have
  363. // information about the subnet he is connected to. This likely means
  364. // misconfiguration of the server (or some relays). We will continue to
  365. // process this message, but our response will be almost useless: no
  366. // addresses or prefixes, no subnet specific configuration etc. The only
  367. // thing this client can get is some global information (like DNS
  368. // servers).
  369. // perhaps this should be logged on some higher level? This is most likely
  370. // configuration bug.
  371. LOG_ERROR(dhcp4_logger, DHCP4_SUBNET_SELECTION_FAILED)
  372. .arg(question->getRemoteAddr().toText())
  373. .arg(serverReceivedPacketName(question->getType()));
  374. answer->setType(DHCPNAK);
  375. answer->setYiaddr(IOAddress("0.0.0.0"));
  376. return;
  377. }
  378. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL_DATA, DHCP4_SUBNET_SELECTED)
  379. .arg(subnet->toText());
  380. // Get client-id option
  381. ClientIdPtr client_id;
  382. OptionPtr opt = question->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
  383. if (opt) {
  384. client_id = ClientIdPtr(new ClientId(opt->getData()));
  385. }
  386. // client-id is not mandatory in DHCPv4
  387. IOAddress hint = question->getYiaddr();
  388. HWAddrPtr hwaddr = question->getHWAddr();
  389. // "Fake" allocation is processing of DISCOVER message. We pretend to do an
  390. // allocation, but we do not put the lease in the database. That is ok,
  391. // because we do not guarantee that the user will get that exact lease. If
  392. // the user selects this server to do actual allocation (i.e. sends REQUEST)
  393. // it should include this hint. That will help us during the actual lease
  394. // allocation.
  395. bool fake_allocation = (question->getType() == DHCPDISCOVER);
  396. // Use allocation engine to pick a lease for this client. Allocation engine
  397. // will try to honour the hint, but it is just a hint - some other address
  398. // may be used instead. If fake_allocation is set to false, the lease will
  399. // be inserted into the LeaseMgr as well.
  400. Lease4Ptr lease = alloc_engine_->allocateAddress4(subnet, client_id, hwaddr,
  401. hint, fake_allocation);
  402. if (lease) {
  403. // We have a lease! Let's set it in the packet and send it back to
  404. // the client.
  405. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, fake_allocation?
  406. DHCP4_LEASE_ADVERT:DHCP4_LEASE_ALLOC)
  407. .arg(lease->addr_.toText())
  408. .arg(client_id?client_id->toText():"(no client-id)")
  409. .arg(hwaddr?hwaddr->toText():"(no hwaddr info)");
  410. answer->setYiaddr(lease->addr_);
  411. // IP Address Lease time (type 51)
  412. opt = OptionPtr(new Option(Option::V4, DHO_DHCP_LEASE_TIME));
  413. opt->setUint32(lease->valid_lft_);
  414. answer->addOption(opt);
  415. // Router (type 3)
  416. Subnet::OptionDescriptor opt_routers =
  417. subnet->getOptionDescriptor("dhcp4", DHO_ROUTERS);
  418. if (opt_routers.option) {
  419. answer->addOption(opt_routers.option);
  420. }
  421. // Subnet mask (type 1)
  422. answer->addOption(getNetmaskOption(subnet));
  423. // @todo: send renew timer option (T1, option 58)
  424. // @todo: send rebind timer option (T2, option 59)
  425. } else {
  426. // Allocation engine did not allocate a lease. The engine logged
  427. // cause of that failure. The only thing left is to insert
  428. // status code to pass the sad news to the client.
  429. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, fake_allocation?
  430. DHCP4_LEASE_ADVERT_FAIL:DHCP4_LEASE_ALLOC_FAIL)
  431. .arg(client_id?client_id->toText():"(no client-id)")
  432. .arg(hwaddr?hwaddr->toText():"(no hwaddr info)")
  433. .arg(hint.toText());
  434. answer->setType(DHCPNAK);
  435. answer->setYiaddr(IOAddress("0.0.0.0"));
  436. }
  437. }
  438. OptionPtr
  439. Dhcpv4Srv::getNetmaskOption(const Subnet4Ptr& subnet) {
  440. uint32_t netmask = getNetmask4(subnet->get().second);
  441. OptionPtr opt(new OptionInt<uint32_t>(Option::V4,
  442. DHO_SUBNET_MASK, netmask));
  443. return (opt);
  444. }
  445. Pkt4Ptr
  446. Dhcpv4Srv::processDiscover(Pkt4Ptr& discover) {
  447. Pkt4Ptr offer = Pkt4Ptr
  448. (new Pkt4(DHCPOFFER, discover->getTransid()));
  449. copyDefaultFields(discover, offer);
  450. appendDefaultOptions(offer, DHCPOFFER);
  451. appendRequestedOptions(discover, offer);
  452. assignLease(discover, offer);
  453. // There are a few basic options that we always want to
  454. // include in the response. If client did not request
  455. // them we append them for him.
  456. appendBasicOptions(discover, offer);
  457. return (offer);
  458. }
  459. Pkt4Ptr
  460. Dhcpv4Srv::processRequest(Pkt4Ptr& request) {
  461. Pkt4Ptr ack = Pkt4Ptr
  462. (new Pkt4(DHCPACK, request->getTransid()));
  463. copyDefaultFields(request, ack);
  464. appendDefaultOptions(ack, DHCPACK);
  465. appendRequestedOptions(request, ack);
  466. assignLease(request, ack);
  467. // There are a few basic options that we always want to
  468. // include in the response. If client did not request
  469. // them we append them for him.
  470. appendBasicOptions(request, ack);
  471. return (ack);
  472. }
  473. void
  474. Dhcpv4Srv::processRelease(Pkt4Ptr& release) {
  475. // Try to find client-id
  476. ClientIdPtr client_id;
  477. OptionPtr opt = release->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
  478. if (opt) {
  479. client_id = ClientIdPtr(new ClientId(opt->getData()));
  480. }
  481. try {
  482. // Do we have a lease for that particular address?
  483. Lease4Ptr lease = LeaseMgrFactory::instance().getLease4(release->getYiaddr());
  484. if (!lease) {
  485. // No such lease - bogus release
  486. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_RELEASE_FAIL_NO_LEASE)
  487. .arg(release->getYiaddr().toText())
  488. .arg(release->getHWAddr()->toText())
  489. .arg(client_id ? client_id->toText() : "(no client-id)");
  490. return;
  491. }
  492. // Does the hardware address match? We don't want one client releasing
  493. // second client's leases.
  494. if (lease->hwaddr_ != release->getHWAddr()->hwaddr_) {
  495. // @todo: Print hwaddr from lease as part of ticket #2589
  496. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_RELEASE_FAIL_WRONG_HWADDR)
  497. .arg(release->getYiaddr().toText())
  498. .arg(client_id ? client_id->toText() : "(no client-id)")
  499. .arg(release->getHWAddr()->toText());
  500. return;
  501. }
  502. // Does the lease have client-id info? If it has, then check it with what
  503. // the client sent us.
  504. if (lease->client_id_ && client_id && *lease->client_id_ != *client_id) {
  505. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_RELEASE_FAIL_WRONG_CLIENT_ID)
  506. .arg(release->getYiaddr().toText())
  507. .arg(client_id->toText())
  508. .arg(lease->client_id_->toText());
  509. return;
  510. }
  511. // Ok, hw and client-id match - let's release the lease.
  512. if (LeaseMgrFactory::instance().deleteLease(lease->addr_)) {
  513. // Release successful - we're done here
  514. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_RELEASE)
  515. .arg(lease->addr_.toText())
  516. .arg(client_id ? client_id->toText() : "(no client-id)")
  517. .arg(release->getHWAddr()->toText());
  518. } else {
  519. // Release failed -
  520. LOG_ERROR(dhcp4_logger, DHCP4_RELEASE_FAIL)
  521. .arg(lease->addr_.toText())
  522. .arg(client_id ? client_id->toText() : "(no client-id)")
  523. .arg(release->getHWAddr()->toText());
  524. }
  525. } catch (const isc::Exception& ex) {
  526. // Rethrow the exception with a bit more data.
  527. LOG_ERROR(dhcp4_logger, DHCP4_RELEASE_EXCEPTION)
  528. .arg(ex.what())
  529. .arg(release->getYiaddr());
  530. }
  531. }
  532. void
  533. Dhcpv4Srv::processDecline(Pkt4Ptr& /* decline */) {
  534. /// TODO: Implement this.
  535. }
  536. Pkt4Ptr
  537. Dhcpv4Srv::processInform(Pkt4Ptr& inform) {
  538. /// TODO: Currently implemented echo mode. Implement this for real
  539. return (inform);
  540. }
  541. const char*
  542. Dhcpv4Srv::serverReceivedPacketName(uint8_t type) {
  543. static const char* DISCOVER = "DISCOVER";
  544. static const char* REQUEST = "REQUEST";
  545. static const char* RELEASE = "RELEASE";
  546. static const char* DECLINE = "DECLINE";
  547. static const char* INFORM = "INFORM";
  548. static const char* UNKNOWN = "UNKNOWN";
  549. switch (type) {
  550. case DHCPDISCOVER:
  551. return (DISCOVER);
  552. case DHCPREQUEST:
  553. return (REQUEST);
  554. case DHCPRELEASE:
  555. return (RELEASE);
  556. case DHCPDECLINE:
  557. return (DECLINE);
  558. case DHCPINFORM:
  559. return (INFORM);
  560. default:
  561. ;
  562. }
  563. return (UNKNOWN);
  564. }
  565. Subnet4Ptr
  566. Dhcpv4Srv::selectSubnet(const Pkt4Ptr& question) {
  567. // Is this relayed message?
  568. IOAddress relay = question->getGiaddr();
  569. if (relay.toText() == "0.0.0.0") {
  570. // Yes: Use relay address to select subnet
  571. return (CfgMgr::instance().getSubnet4(relay));
  572. } else {
  573. // No: Use client's address to select subnet
  574. return (CfgMgr::instance().getSubnet4(question->getRemoteAddr()));
  575. }
  576. }
  577. void
  578. Dhcpv4Srv::sanityCheck(const Pkt4Ptr& pkt, RequirementLevel serverid) {
  579. OptionPtr server_id = pkt->getOption(DHO_DHCP_SERVER_IDENTIFIER);
  580. switch (serverid) {
  581. case FORBIDDEN:
  582. if (server_id) {
  583. isc_throw(RFCViolation, "Server-id option was not expected, but "
  584. << "received in " << serverReceivedPacketName(pkt->getType()));
  585. }
  586. break;
  587. case MANDATORY:
  588. if (!server_id) {
  589. isc_throw(RFCViolation, "Server-id option was expected, but not "
  590. " received in message "
  591. << serverReceivedPacketName(pkt->getType()));
  592. }
  593. break;
  594. case OPTIONAL:
  595. // do nothing here
  596. ;
  597. }
  598. }
  599. } // namespace dhcp
  600. } // namespace isc