123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953 |
- // Copyright (C) 2011-2013 Internet Systems Consortium, Inc. ("ISC")
- //
- // Permission to use, copy, modify, and/or distribute this software for any
- // purpose with or without fee is hereby granted, provided that the above
- // copyright notice and this permission notice appear in all copies.
- //
- // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
- // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
- // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- // PERFORMANCE OF THIS SOFTWARE.
- #include <config.h>
- #include <asiolink/io_address.h>
- #include <dhcp/dhcp6.h>
- #include <dhcp/duid.h>
- #include <dhcp/iface_mgr.h>
- #include <dhcp/libdhcp++.h>
- #include <dhcp/option6_addrlst.h>
- #include <dhcp/option6_ia.h>
- #include <dhcp/option6_iaaddr.h>
- #include <dhcp/option6_iaaddr.h>
- #include <dhcp/option_custom.h>
- #include <dhcp/option_int_array.h>
- #include <dhcp/pkt6.h>
- #include <dhcp6/dhcp6_log.h>
- #include <dhcp6/dhcp6_srv.h>
- #include <dhcpsrv/cfgmgr.h>
- #include <dhcpsrv/lease_mgr.h>
- #include <dhcpsrv/lease_mgr_factory.h>
- #include <dhcpsrv/subnet.h>
- #include <dhcpsrv/utils.h>
- #include <exceptions/exceptions.h>
- #include <util/io_utilities.h>
- #include <util/range_utilities.h>
- #include <boost/foreach.hpp>
- #include <stdlib.h>
- #include <time.h>
- using namespace isc;
- using namespace isc::asiolink;
- using namespace isc::dhcp;
- using namespace isc::util;
- using namespace std;
- namespace isc {
- namespace dhcp {
- Dhcpv6Srv::Dhcpv6Srv(uint16_t port, const char* dbconfig)
- : alloc_engine_(), serverid_(), shutdown_(true) {
- LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_OPEN_SOCKET).arg(port);
- // Initialize objects required for DHCP server operation.
- try {
- // Port 0 is used for testing purposes. It means that the server should
- // not open any sockets at all. Some tests, e.g. configuration parser,
- // require Dhcpv6Srv object, but they don't really need it to do
- // anything. This speed up and simplifies the tests.
- if (port > 0) {
- if (IfaceMgr::instance().countIfaces() == 0) {
- LOG_ERROR(dhcp6_logger, DHCP6_NO_INTERFACES);
- return;
- }
- IfaceMgr::instance().openSockets6(port);
- }
- setServerID();
- // Instantiate LeaseMgr
- LeaseMgrFactory::create(dbconfig);
- LOG_INFO(dhcp6_logger, DHCP6_DB_BACKEND_STARTED)
- .arg(LeaseMgrFactory::instance().getType())
- .arg(LeaseMgrFactory::instance().getName());
- // Instantiate allocation engine
- alloc_engine_.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100));
- } catch (const std::exception &e) {
- LOG_ERROR(dhcp6_logger, DHCP6_SRV_CONSTRUCT_ERROR).arg(e.what());
- return;
- }
- // All done, so can proceed
- shutdown_ = false;
- }
- Dhcpv6Srv::~Dhcpv6Srv() {
- IfaceMgr::instance().closeSockets();
- LeaseMgrFactory::destroy();
- }
- void Dhcpv6Srv::shutdown() {
- LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_SHUTDOWN_REQUEST);
- shutdown_ = true;
- }
- bool Dhcpv6Srv::run() {
- while (!shutdown_) {
- /// @todo: calculate actual timeout to the next event (e.g. lease
- /// expiration) once we have lease database. The idea here is that
- /// it is possible to do everything in a single process/thread.
- /// For now, we are just calling select for 1000 seconds. There
- /// were some issues reported on some systems when calling select()
- /// with too large values. Unfortunately, I don't recall the details.
- int timeout = 1000;
- // client's message and server's response
- Pkt6Ptr query;
- Pkt6Ptr rsp;
- try {
- query = IfaceMgr::instance().receive6(timeout);
- } catch (const std::exception& e) {
- LOG_ERROR(dhcp6_logger, DHCP6_PACKET_RECEIVE_FAIL).arg(e.what());
- }
- if (query) {
- if (!query->unpack()) {
- LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL,
- DHCP6_PACKET_PARSE_FAIL);
- continue;
- }
- LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_PACKET_RECEIVED)
- .arg(query->getName());
- LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_QUERY_DATA)
- .arg(static_cast<int>(query->getType()))
- .arg(query->getBuffer().getLength())
- .arg(query->toText());
- try {
- switch (query->getType()) {
- case DHCPV6_SOLICIT:
- rsp = processSolicit(query);
- break;
- case DHCPV6_REQUEST:
- rsp = processRequest(query);
- break;
- case DHCPV6_RENEW:
- rsp = processRenew(query);
- break;
- case DHCPV6_REBIND:
- rsp = processRebind(query);
- break;
- case DHCPV6_CONFIRM:
- rsp = processConfirm(query);
- break;
- case DHCPV6_RELEASE:
- rsp = processRelease(query);
- break;
- case DHCPV6_DECLINE:
- rsp = processDecline(query);
- break;
- case DHCPV6_INFORMATION_REQUEST:
- rsp = processInfRequest(query);
- break;
- default:
- // Only action is to output a message if debug is enabled,
- // and that will be covered by the debug statement before
- // the "switch" statement.
- ;
- }
- } catch (const RFCViolation& e) {
- LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_REQUIRED_OPTIONS_CHECK_FAIL)
- .arg(query->getName())
- .arg(query->getRemoteAddr())
- .arg(e.what());
- }
- if (rsp) {
- rsp->setRemoteAddr(query->getRemoteAddr());
- rsp->setLocalAddr(query->getLocalAddr());
- rsp->setRemotePort(DHCP6_CLIENT_PORT);
- rsp->setLocalPort(DHCP6_SERVER_PORT);
- rsp->setIndex(query->getIndex());
- rsp->setIface(query->getIface());
- LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA,
- DHCP6_RESPONSE_DATA)
- .arg(rsp->getType()).arg(rsp->toText());
- if (rsp->pack()) {
- try {
- IfaceMgr::instance().send(rsp);
- } catch (const std::exception& e) {
- LOG_ERROR(dhcp6_logger, DHCP6_PACKET_SEND_FAIL).arg(e.what());
- }
- } else {
- LOG_ERROR(dhcp6_logger, DHCP6_PACK_FAIL);
- }
- }
- }
- }
- return (true);
- }
- void Dhcpv6Srv::setServerID() {
- /// @todo: DUID should be generated once and then stored, rather
- /// than generated each time
- /// @todo: This code implements support for DUID-LLT (the recommended one).
- /// We should eventually add support for other DUID types: DUID-LL, DUID-EN
- /// and DUID-UUID
- const IfaceMgr::IfaceCollection& ifaces = IfaceMgr::instance().getIfaces();
- // Let's find suitable interface.
- for (IfaceMgr::IfaceCollection::const_iterator iface = ifaces.begin();
- iface != ifaces.end(); ++iface) {
- // All the following checks could be merged into one multi-condition
- // statement, but let's keep them separated as perhaps one day
- // we will grow knobs to selectively turn them on or off. Also,
- // this code is used only *once* during first start on a new machine
- // and then server-id is stored. (or at least it will be once
- // DUID storage is implemente
- // I wish there was a this_is_a_real_physical_interface flag...
- // MAC address should be at least 6 bytes. Although there is no such
- // requirement in any RFC, all decent physical interfaces (Ethernet,
- // WiFi, Infiniband, etc.) have 6 bytes long MAC address. We want to
- // base our DUID on real hardware address, rather than virtual
- // interface that pretends that underlying IP address is its MAC.
- if (iface->getMacLen() < MIN_MAC_LEN) {
- continue;
- }
- // Let's don't use loopback.
- if (iface->flag_loopback_) {
- continue;
- }
- // Let's skip downed interfaces. It is better to use working ones.
- if (!iface->flag_up_) {
- continue;
- }
- // Some interfaces (like lo on Linux) report 6-bytes long
- // MAC adress 00:00:00:00:00:00. Let's not use such weird interfaces
- // to generate DUID.
- if (isRangeZero(iface->getMac(), iface->getMac() + iface->getMacLen())) {
- continue;
- }
- // Ok, we have useful MAC. Let's generate DUID-LLT based on
- // it. See RFC3315, Section 9.2 for details.
- // DUID uses seconds since midnight of 01-01-2000, time() returns
- // seconds since 01-01-1970. DUID_TIME_EPOCH substution corrects that.
- time_t seconds = time(NULL);
- seconds -= DUID_TIME_EPOCH;
- OptionBuffer srvid(8 + iface->getMacLen());
- writeUint16(DUID::DUID_LLT, &srvid[0]);
- writeUint16(HWTYPE_ETHERNET, &srvid[2]);
- writeUint32(static_cast<uint32_t>(seconds), &srvid[4]);
- memcpy(&srvid[0] + 8, iface->getMac(), iface->getMacLen());
- serverid_ = OptionPtr(new Option(Option::V6, D6O_SERVERID,
- srvid.begin(), srvid.end()));
- return;
- }
- // If we reached here, there are no suitable interfaces found.
- // Either interface detection is not supported on this platform or
- // this is really weird box. Let's use DUID-EN instead.
- // See Section 9.3 of RFC3315 for details.
- OptionBuffer srvid(12);
- writeUint16(DUID::DUID_EN, &srvid[0]);
- writeUint32(ENTERPRISE_ID_ISC, &srvid[2]);
- // Length of the identifier is company specific. I hereby declare
- // ISC "standard" of 6 bytes long pseudo-random numbers.
- srandom(time(NULL));
- fillRandom(&srvid[6], &srvid[12]);
- serverid_ = OptionPtr(new Option(Option::V6, D6O_SERVERID,
- srvid.begin(), srvid.end()));
- }
- void Dhcpv6Srv::copyDefaultOptions(const Pkt6Ptr& question, Pkt6Ptr& answer) {
- // Add client-id.
- OptionPtr clientid = question->getOption(D6O_CLIENTID);
- if (clientid) {
- answer->addOption(clientid);
- }
- // TODO: Should throw if there is no client-id (except anonymous INF-REQUEST)
- }
- void Dhcpv6Srv::appendDefaultOptions(const Pkt6Ptr& question, Pkt6Ptr& answer) {
- // add server-id
- answer->addOption(getServerID());
- // Get the subnet object. It holds options to be sent to the client
- // that belongs to the particular subnet.
- Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(question->getRemoteAddr());
- // Warn if subnet is not supported and quit.
- if (!subnet) {
- LOG_WARN(dhcp6_logger, DHCP6_NO_SUBNET_DEF_OPT)
- .arg(question->getRemoteAddr().toText());
- return;
- }
- }
- void Dhcpv6Srv::appendRequestedOptions(const Pkt6Ptr& question, Pkt6Ptr& answer) {
- // Get the subnet for a particular address.
- Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(question->getRemoteAddr());
- if (!subnet) {
- LOG_WARN(dhcp6_logger, DHCP6_NO_SUBNET_REQ_OPT)
- .arg(question->getRemoteAddr().toText());
- return;
- }
- // Client requests some options using ORO option. Try to
- // get this option from client's message.
- boost::shared_ptr<OptionIntArray<uint16_t> > option_oro =
- boost::dynamic_pointer_cast<OptionIntArray<uint16_t> >(question->getOption(D6O_ORO));
- // Option ORO not found. Don't do anything then.
- if (!option_oro) {
- return;
- }
- // Get the list of options that client requested.
- const std::vector<uint16_t>& requested_opts = option_oro->getValues();
- // Get the list of options configured for a subnet.
- Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp6");
- const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
- // Try to match requested options with those configured for a subnet.
- // If match is found, append configured option to the answer message.
- BOOST_FOREACH(uint16_t opt, requested_opts) {
- const Subnet::OptionContainerTypeRange& range = idx.equal_range(opt);
- BOOST_FOREACH(Subnet::OptionDescriptor desc, range) {
- answer->addOption(desc.option);
- }
- }
- }
- OptionPtr Dhcpv6Srv::createStatusCode(uint16_t code, const std::string& text) {
- // @todo This function uses OptionCustom class to manage contents
- // of the data fields. Since this this option is frequently used
- // it may be good to implement dedicated class to avoid performance
- // impact.
- // Get the definition of the option holding status code.
- OptionDefinitionPtr status_code_def =
- LibDHCP::getOptionDef(Option::V6, D6O_STATUS_CODE);
- // This definition is assumed to be initialized in LibDHCP.
- assert(status_code_def);
- // As there is no dedicated class to represent Status Code
- // the OptionCustom class should be returned here.
- boost::shared_ptr<OptionCustom> option_status =
- boost::dynamic_pointer_cast<
- OptionCustom>(status_code_def->optionFactory(Option::V6, D6O_STATUS_CODE));
- assert(option_status);
- // Set status code to 'code' (0 - means data field #0).
- option_status->writeInteger(code, 0);
- // Set a message (1 - means data field #1).
- option_status->writeString(text, 1);
- return (option_status);
- }
- void Dhcpv6Srv::sanityCheck(const Pkt6Ptr& pkt, RequirementLevel clientid,
- RequirementLevel serverid) {
- Option::OptionCollection client_ids = pkt->getOptions(D6O_CLIENTID);
- switch (clientid) {
- case MANDATORY:
- if (client_ids.size() != 1) {
- isc_throw(RFCViolation, "Exactly 1 client-id option expected in "
- << pkt->getName() << ", but " << client_ids.size()
- << " received");
- }
- break;
- case OPTIONAL:
- if (client_ids.size() > 1) {
- isc_throw(RFCViolation, "Too many (" << client_ids.size()
- << ") client-id options received in " << pkt->getName());
- }
- break;
- case FORBIDDEN:
- // doesn't make sense - client-id is always allowed
- break;
- }
- Option::OptionCollection server_ids = pkt->getOptions(D6O_SERVERID);
- switch (serverid) {
- case FORBIDDEN:
- if (!server_ids.empty()) {
- isc_throw(RFCViolation, "Server-id option was not expected, but "
- << server_ids.size() << " received in " << pkt->getName());
- }
- break;
- case MANDATORY:
- if (server_ids.size() != 1) {
- isc_throw(RFCViolation, "Invalid number of server-id options received ("
- << server_ids.size() << "), exactly 1 expected in message "
- << pkt->getName());
- }
- break;
- case OPTIONAL:
- if (server_ids.size() > 1) {
- isc_throw(RFCViolation, "Too many (" << server_ids.size()
- << ") server-id options received in " << pkt->getName());
- }
- }
- }
- Subnet6Ptr Dhcpv6Srv::selectSubnet(const Pkt6Ptr& question) {
- Subnet6Ptr subnet = CfgMgr::instance().getSubnet6(question->getRemoteAddr());
- return (subnet);
- }
- void Dhcpv6Srv::assignLeases(const Pkt6Ptr& question, Pkt6Ptr& answer) {
- // We need to allocate addresses for all IA_NA options in the client's
- // question (i.e. SOLICIT or REQUEST) message.
- // @todo add support for IA_TA
- // @todo add support for IA_PD
- // We need to select a subnet the client is connected in.
- Subnet6Ptr subnet = selectSubnet(question);
- if (!subnet) {
- // This particular client is out of luck today. We do not have
- // information about the subnet he is connected to. This likely means
- // misconfiguration of the server (or some relays). We will continue to
- // process this message, but our response will be almost useless: no
- // addresses or prefixes, no subnet specific configuration etc. The only
- // thing this client can get is some global information (like DNS
- // servers).
- // perhaps this should be logged on some higher level? This is most likely
- // configuration bug.
- LOG_ERROR(dhcp6_logger, DHCP6_SUBNET_SELECTION_FAILED)
- .arg(question->getRemoteAddr().toText())
- .arg(question->getName());
- } else {
- LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_SUBNET_SELECTED)
- .arg(subnet->toText());
- }
- // @todo: We should implement Option6Duid some day, but we can do without it
- // just fine for now
- // Let's find client's DUID. Client is supposed to include its client-id
- // option almost all the time (the only exception is an anonymous inf-request,
- // but that is mostly a theoretical case). Our allocation engine needs DUID
- // and will refuse to allocate anything to anonymous clients.
- DuidPtr duid;
- OptionPtr opt_duid = question->getOption(D6O_CLIENTID);
- if (opt_duid) {
- duid = DuidPtr(new DUID(opt_duid->getData()));
- } else {
- LOG_DEBUG(dhcp6_logger, DBG_DHCP6_BASIC, DHCP6_CLIENTID_MISSING);
- // Let's drop the message. This client is not sane.
- isc_throw(RFCViolation, "Mandatory client-id is missing in received message");
- }
- // Now that we have all information about the client, let's iterate over all
- // received options and handle IA_NA options one by one and store our
- // responses in answer message (ADVERTISE or REPLY).
- //
- // @todo: expand this to cover IA_PD and IA_TA once we implement support for
- // prefix delegation and temporary addresses.
- for (Option::OptionCollection::iterator opt = question->options_.begin();
- opt != question->options_.end(); ++opt) {
- switch (opt->second->getType()) {
- case D6O_IA_NA: {
- OptionPtr answer_opt = assignIA_NA(subnet, duid, question,
- boost::dynamic_pointer_cast<Option6IA>(opt->second));
- if (answer_opt) {
- answer->addOption(answer_opt);
- }
- break;
- }
- default:
- break;
- }
- }
- }
- OptionPtr Dhcpv6Srv::assignIA_NA(const Subnet6Ptr& subnet, const DuidPtr& duid,
- Pkt6Ptr question, boost::shared_ptr<Option6IA> ia) {
- // If there is no subnet selected for handling this IA_NA, the only thing to do left is
- // to say that we are sorry, but the user won't get an address. As a convenience, we
- // use a different status text to indicate that (compare to the same status code,
- // but different wording below)
- if (!subnet) {
- // Create empty IA_NA option with IAID matching the request.
- // Note that we don't use OptionDefinition class to create this option.
- // This is because we prefer using a constructor of Option6IA that
- // initializes IAID. Otherwise we would have to use setIAID() after
- // creation of the option which has some performance implications.
- boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
- // Insert status code NoAddrsAvail.
- ia_rsp->addOption(createStatusCode(STATUS_NoAddrsAvail, "Sorry, no subnet available."));
- return (ia_rsp);
- }
- // Check if the client sent us a hint in his IA_NA. Clients may send an
- // address in their IA_NA options as a suggestion (e.g. the last address
- // they used before).
- boost::shared_ptr<Option6IAAddr> hintOpt = boost::dynamic_pointer_cast<Option6IAAddr>
- (ia->getOption(D6O_IAADDR));
- IOAddress hint("::");
- if (hintOpt) {
- hint = hintOpt->getAddress();
- }
- LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_PROCESS_IA_NA_REQUEST)
- .arg(duid?duid->toText():"(no-duid)").arg(ia->getIAID())
- .arg(hintOpt?hint.toText():"(no hint)");
- // "Fake" allocation is processing of SOLICIT message. We pretend to do an
- // allocation, but we do not put the lease in the database. That is ok,
- // because we do not guarantee that the user will get that exact lease. If
- // the user selects this server to do actual allocation (i.e. sends REQUEST)
- // it should include this hint. That will help us during the actual lease
- // allocation.
- bool fake_allocation = false;
- if (question->getType() == DHCPV6_SOLICIT) {
- /// @todo: Check if we support rapid commit
- fake_allocation = true;
- }
- // Use allocation engine to pick a lease for this client. Allocation engine
- // will try to honour the hint, but it is just a hint - some other address
- // may be used instead. If fake_allocation is set to false, the lease will
- // be inserted into the LeaseMgr as well.
- Lease6Ptr lease = alloc_engine_->allocateAddress6(subnet, duid, ia->getIAID(),
- hint, fake_allocation);
- // Create IA_NA that we will put in the response.
- // Do not use OptionDefinition to create option's instance so
- // as we can initialize IAID using a constructor.
- boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
- if (lease) {
- // We have a lease! Let's wrap its content into IA_NA option
- // with IAADDR suboption.
- LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, fake_allocation?
- DHCP6_LEASE_ADVERT:DHCP6_LEASE_ALLOC)
- .arg(lease->addr_.toText())
- .arg(duid?duid->toText():"(no-duid)")
- .arg(ia->getIAID());
- ia_rsp->setT1(subnet->getT1());
- ia_rsp->setT2(subnet->getT2());
- boost::shared_ptr<Option6IAAddr>
- addr(new Option6IAAddr(D6O_IAADDR,
- lease->addr_,
- lease->preferred_lft_,
- lease->valid_lft_));
- ia_rsp->addOption(addr);
- // It would be possible to insert status code=0(success) as well,
- // but this is considered waste of bandwidth as absence of status
- // code is considered a success.
- } else {
- // Allocation engine did not allocate a lease. The engine logged
- // cause of that failure. The only thing left is to insert
- // status code to pass the sad news to the client.
- LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, fake_allocation?
- DHCP6_LEASE_ADVERT_FAIL:DHCP6_LEASE_ALLOC_FAIL)
- .arg(duid?duid->toText():"(no-duid)")
- .arg(ia->getIAID())
- .arg(subnet->toText());
- ia_rsp->addOption(createStatusCode(STATUS_NoAddrsAvail,
- "Sorry, no address could be allocated."));
- }
- return (ia_rsp);
- }
- OptionPtr Dhcpv6Srv::renewIA_NA(const Subnet6Ptr& subnet, const DuidPtr& duid,
- Pkt6Ptr question, boost::shared_ptr<Option6IA> ia) {
- Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(*duid, ia->getIAID(),
- subnet->getID());
- if (!lease) {
- // client renewing a lease that we don't know about.
- // Create empty IA_NA option with IAID matching the request.
- boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
- // Insert status code NoAddrsAvail.
- ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
- "Sorry, no known leases for this duid/iaid."));
- LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_UNKNOWN_RENEW)
- .arg(duid->toText())
- .arg(ia->getIAID())
- .arg(subnet->toText());
- return (ia_rsp);
- }
- lease->preferred_lft_ = subnet->getPreferred();
- lease->valid_lft_ = subnet->getValid();
- lease->t1_ = subnet->getT1();
- lease->t2_ = subnet->getT2();
- lease->cltt_ = time(NULL);
- LeaseMgrFactory::instance().updateLease6(lease);
- // Create empty IA_NA option with IAID matching the request.
- boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
- ia_rsp->setT1(subnet->getT1());
- ia_rsp->setT2(subnet->getT2());
- boost::shared_ptr<Option6IAAddr> addr(new Option6IAAddr(D6O_IAADDR,
- lease->addr_, lease->preferred_lft_,
- lease->valid_lft_));
- ia_rsp->addOption(addr);
- return (ia_rsp);
- }
- void Dhcpv6Srv::renewLeases(const Pkt6Ptr& renew, Pkt6Ptr& reply) {
- // We need to renew addresses for all IA_NA options in the client's
- // RENEW message.
- // @todo add support for IA_TA
- // @todo add support for IA_PD
- // We need to select a subnet the client is connected in.
- Subnet6Ptr subnet = selectSubnet(renew);
- if (!subnet) {
- // This particular client is out of luck today. We do not have
- // information about the subnet he is connected to. This likely means
- // misconfiguration of the server (or some relays). We will continue to
- // process this message, but our response will be almost useless: no
- // addresses or prefixes, no subnet specific configuration etc. The only
- // thing this client can get is some global information (like DNS
- // servers).
- // perhaps this should be logged on some higher level? This is most likely
- // configuration bug.
- LOG_ERROR(dhcp6_logger, DHCP6_SUBNET_SELECTION_FAILED);
- } else {
- LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL_DATA, DHCP6_SUBNET_SELECTED)
- .arg(subnet->toText());
- }
- // Let's find client's DUID. Client is supposed to include its client-id
- // option almost all the time (the only exception is an anonymous inf-request,
- // but that is mostly a theoretical case). Our allocation engine needs DUID
- // and will refuse to allocate anything to anonymous clients.
- OptionPtr opt_duid = renew->getOption(D6O_CLIENTID);
- if (!opt_duid) {
- // This should not happen. We have checked this before.
- reply->addOption(createStatusCode(STATUS_UnspecFail,
- "You did not include mandatory client-id"));
- return;
- }
- DuidPtr duid(new DUID(opt_duid->getData()));
- for (Option::OptionCollection::iterator opt = renew->options_.begin();
- opt != renew->options_.end(); ++opt) {
- switch (opt->second->getType()) {
- case D6O_IA_NA: {
- OptionPtr answer_opt = renewIA_NA(subnet, duid, renew,
- boost::dynamic_pointer_cast<Option6IA>(opt->second));
- if (answer_opt) {
- reply->addOption(answer_opt);
- }
- break;
- }
- default:
- break;
- }
- }
- }
- void Dhcpv6Srv::releaseLeases(const Pkt6Ptr& release, Pkt6Ptr& reply) {
- // We need to release addresses for all IA_NA options in the client's
- // RELEASE message.
- // @todo Add support for IA_TA
- // @todo Add support for IA_PD
- // @todo Consider supporting more than one address in a single IA_NA.
- // That was envisaged by RFC3315, but it never happened. The only
- // software that supports that is Dibbler, but its author seriously doubts
- // if anyone is really using it. Clients that want more than one address
- // just include more instances of IA_NA options.
- // Let's find client's DUID. Client is supposed to include its client-id
- // option almost all the time (the only exception is an anonymous inf-request,
- // but that is mostly a theoretical case). Our allocation engine needs DUID
- // and will refuse to allocate anything to anonymous clients.
- OptionPtr opt_duid = release->getOption(D6O_CLIENTID);
- if (!opt_duid) {
- // This should not happen. We have checked this before.
- // see sanityCheck() called from processRelease()
- LOG_WARN(dhcp6_logger, DHCP6_RELEASE_MISSING_CLIENTID)
- .arg(release->getRemoteAddr().toText());
- reply->addOption(createStatusCode(STATUS_UnspecFail,
- "You did not include mandatory client-id"));
- return;
- }
- DuidPtr duid(new DUID(opt_duid->getData()));
- int general_status = STATUS_Success;
- for (Option::OptionCollection::iterator opt = release->options_.begin();
- opt != release->options_.end(); ++opt) {
- switch (opt->second->getType()) {
- case D6O_IA_NA: {
- OptionPtr answer_opt = releaseIA_NA(duid, release, general_status,
- boost::dynamic_pointer_cast<Option6IA>(opt->second));
- if (answer_opt) {
- reply->addOption(answer_opt);
- }
- break;
- }
- // @todo: add support for IA_PD
- // @todo: add support for IA_TA
- default:
- // remaining options are stateless and thus ignored in this context
- ;
- }
- }
- // To be pedantic, we should also include status code in the top-level
- // scope, not just in each IA_NA. See RFC3315, section 18.2.6.
- // This behavior will likely go away in RFC3315bis.
- reply->addOption(createStatusCode(general_status,
- "Summary status for all processed IA_NAs"));
- }
- OptionPtr Dhcpv6Srv::releaseIA_NA(const DuidPtr& duid, Pkt6Ptr question,
- int& general_status,
- boost::shared_ptr<Option6IA> ia) {
- // Release can be done in one of two ways:
- // Approach 1: extract address from client's IA_NA and see if it belongs
- // to this particular client.
- // Approach 2: find a subnet for this client, get a lease for
- // this subnet/duid/iaid and check if its content matches to what the
- // client is asking us to release.
- //
- // This method implements approach 1.
- // That's our response
- boost::shared_ptr<Option6IA> ia_rsp(new Option6IA(D6O_IA_NA, ia->getIAID()));
- boost::shared_ptr<Option6IAAddr> release_addr = boost::dynamic_pointer_cast<Option6IAAddr>
- (ia->getOption(D6O_IAADDR));
- if (!release_addr) {
- ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
- "You did not include address in your RELEASE"));
- general_status = STATUS_NoBinding;
- return (ia_rsp);
- }
- Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(release_addr->getAddress());
- if (!lease) {
- // client releasing a lease that we don't know about.
- // Insert status code NoAddrsAvail.
- ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
- "Sorry, no known leases for this duid/iaid, can't release."));
- general_status = STATUS_NoBinding;
- LOG_INFO(dhcp6_logger, DHCP6_UNKNOWN_RELEASE)
- .arg(duid->toText())
- .arg(ia->getIAID());
- return (ia_rsp);
- }
- if (!lease->duid_) {
- // Something is gravely wrong here. We do have a lease, but it does not
- // have mandatory DUID information attached. Someone was messing with our
- // database.
- LOG_ERROR(dhcp6_logger, DHCP6_LEASE_WITHOUT_DUID)
- .arg(release_addr->getAddress().toText());
- general_status = STATUS_UnspecFail;
- ia_rsp->addOption(createStatusCode(STATUS_UnspecFail,
- "Database consistency check failed when trying to RELEASE"));
- return (ia_rsp);
- }
- if (*duid != *(lease->duid_)) {
- // Sorry, it's not your address. You can't release it.
- LOG_INFO(dhcp6_logger, DHCP6_RELEASE_FAIL_WRONG_DUID)
- .arg(duid->toText())
- .arg(release_addr->getAddress().toText())
- .arg(lease->duid_->toText());
- general_status = STATUS_NoBinding;
- ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
- "This address does not belong to you, you can't release it"));
- return (ia_rsp);
- }
- if (ia->getIAID() != lease->iaid_) {
- // This address belongs to this client, but to a different IA
- LOG_WARN(dhcp6_logger, DHCP6_RELEASE_FAIL_WRONG_IAID)
- .arg(duid->toText())
- .arg(release_addr->getAddress().toText())
- .arg(lease->iaid_)
- .arg(ia->getIAID());
- ia_rsp->addOption(createStatusCode(STATUS_NoBinding,
- "This is your address, but you used wrong IAID"));
- general_status = STATUS_NoBinding;
- return (ia_rsp);
- }
- // It is not necessary to check if the address matches as we used
- // getLease6(addr) method that is supposed to return a proper lease.
- // Ok, we've passed all checks. Let's release this address.
- if (!LeaseMgrFactory::instance().deleteLease(lease->addr_)) {
- ia_rsp->addOption(createStatusCode(STATUS_UnspecFail,
- "Server failed to release a lease"));
- LOG_ERROR(dhcp6_logger, DHCP6_RELEASE_FAIL)
- .arg(lease->addr_.toText())
- .arg(duid->toText())
- .arg(lease->iaid_);
- general_status = STATUS_UnspecFail;
- return (ia_rsp);
- } else {
- LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_RELEASE)
- .arg(lease->addr_.toText())
- .arg(duid->toText())
- .arg(lease->iaid_);
- ia_rsp->addOption(createStatusCode(STATUS_Success,
- "Lease released. Thank you, please come again."));
- return (ia_rsp);
- }
- }
- Pkt6Ptr Dhcpv6Srv::processSolicit(const Pkt6Ptr& solicit) {
- sanityCheck(solicit, MANDATORY, FORBIDDEN);
- Pkt6Ptr advertise(new Pkt6(DHCPV6_ADVERTISE, solicit->getTransid()));
- copyDefaultOptions(solicit, advertise);
- appendDefaultOptions(solicit, advertise);
- appendRequestedOptions(solicit, advertise);
- assignLeases(solicit, advertise);
- return (advertise);
- }
- Pkt6Ptr Dhcpv6Srv::processRequest(const Pkt6Ptr& request) {
- sanityCheck(request, MANDATORY, MANDATORY);
- Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, request->getTransid()));
- copyDefaultOptions(request, reply);
- appendDefaultOptions(request, reply);
- appendRequestedOptions(request, reply);
- assignLeases(request, reply);
- return (reply);
- }
- Pkt6Ptr Dhcpv6Srv::processRenew(const Pkt6Ptr& renew) {
- sanityCheck(renew, MANDATORY, MANDATORY);
- Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, renew->getTransid()));
- copyDefaultOptions(renew, reply);
- appendDefaultOptions(renew, reply);
- appendRequestedOptions(renew, reply);
- renewLeases(renew, reply);
- return reply;
- }
- Pkt6Ptr Dhcpv6Srv::processRebind(const Pkt6Ptr& rebind) {
- /// @todo: Implement this
- Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, rebind->getTransid()));
- return reply;
- }
- Pkt6Ptr Dhcpv6Srv::processConfirm(const Pkt6Ptr& confirm) {
- /// @todo: Implement this
- Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, confirm->getTransid()));
- return reply;
- }
- Pkt6Ptr Dhcpv6Srv::processRelease(const Pkt6Ptr& release) {
- sanityCheck(release, MANDATORY, MANDATORY);
- Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, release->getTransid()));
- copyDefaultOptions(release, reply);
- appendDefaultOptions(release, reply);
- releaseLeases(release, reply);
- return reply;
- }
- Pkt6Ptr Dhcpv6Srv::processDecline(const Pkt6Ptr& decline) {
- /// @todo: Implement this
- Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, decline->getTransid()));
- return reply;
- }
- Pkt6Ptr Dhcpv6Srv::processInfRequest(const Pkt6Ptr& infRequest) {
- /// @todo: Implement this
- Pkt6Ptr reply(new Pkt6(DHCPV6_REPLY, infRequest->getTransid()));
- return reply;
- }
- };
- };
|