dhcp4_srv.cc 9.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (C) 2011  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 <dhcp/dhcp4.h>
#include <dhcp/pkt4.h>
17
#include <dhcp/iface_mgr.h>
18 19
#include <dhcp4/dhcp4_srv.h>
#include <asiolink/io_address.h>
20
#include <dhcp/option4_addrlst.h>
21 22 23 24 25 26

using namespace std;
using namespace isc;
using namespace isc::dhcp;
using namespace isc::asiolink;

27 28
// These are hardcoded parameters. Currently this is a skeleton server that only
// grants those options and a single, fixed, hardcoded lease.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
29
const std::string HARDCODED_LEASE = "192.0.2.222"; // assigned lease
30 31
const std::string HARDCODED_NETMASK = "255.255.255.0";
const uint32_t    HARDCODED_LEASE_TIME = 60; // in seconds
Tomek Mrugalski's avatar
Tomek Mrugalski committed
32 33 34 35
const std::string HARDCODED_GATEWAY = "192.0.2.1";
const std::string HARDCODED_DNS_SERVER = "192.0.2.2";
const std::string HARDCODED_DOMAIN_NAME = "isc.example.com";
const std::string HARDCODED_SERVER_ID = "192.0.2.1";
36

37 38
Dhcpv4Srv::Dhcpv4Srv(uint16_t port) {
    cout << "Initialization: opening sockets on port " << port << endl;
39 40 41 42 43 44

    // first call to instance() will create IfaceMgr (it's a singleton)
    // it may throw something if things go wrong
    IfaceMgr::instance();

    /// @todo: instantiate LeaseMgr here once it is imlpemented.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
45 46 47
    IfaceMgr::instance().printIfaces();

    IfaceMgr::instance().openSockets4(port);
48 49 50

    setServerID();

Tomek Mrugalski's avatar
Tomek Mrugalski committed
51
    shutdown_ = false;
52 53 54 55
}

Dhcpv4Srv::~Dhcpv4Srv() {
    cout << "DHCPv4 server shutdown." << endl;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
56
    IfaceMgr::instance().closeSockets();
57 58 59 60
}

bool
Dhcpv4Srv::run() {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
61
    while (!shutdown_) {
62 63 64 65
        boost::shared_ptr<Pkt4> query; // client's message
        boost::shared_ptr<Pkt4> rsp;   // server's response

        query = IfaceMgr::instance().receive4();
66

67
        if (query) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
68 69 70 71
            try {
                query->unpack();
            } catch (const std::exception& e) {
                /// TODO: Printout reasons of failed parsing
72
                cout << "Failed to parse incoming packet " << endl;
73 74
                continue;
            }
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
            switch (query->getType()) {
            case DHCPDISCOVER:
                rsp = processDiscover(query);
                break;
            case DHCPREQUEST:
                rsp = processRequest(query);
                break;
            case DHCPRELEASE:
                processRelease(query);
                break;
            case DHCPDECLINE:
                processDecline(query);
                break;
            case DHCPINFORM:
                processInform(query);
                break;
            default:
                cout << "Unknown pkt type received:"
                     << query->getType() << endl;
            }

97
            cout << "Received message type " << int(query->getType()) << endl;
98

99 100
            // TODO: print out received packets only if verbose (or debug)
            // mode is enabled
101 102 103
            cout << query->toText();

            if (rsp) {
104 105 106 107 108 109 110 111 112
                if (rsp->getRemoteAddr().toText() == "0.0.0.0") {
                    rsp->setRemoteAddr(query->getRemoteAddr());
                }
                if (!rsp->getHops()) {
                    rsp->setRemotePort(DHCP4_CLIENT_PORT);
                } else {
                    rsp->setRemotePort(DHCP4_SERVER_PORT);
                }

113 114 115 116 117
                rsp->setLocalAddr(query->getLocalAddr());
                rsp->setLocalPort(DHCP4_SERVER_PORT);
                rsp->setIface(query->getIface());
                rsp->setIndex(query->getIndex());

Tomek Mrugalski's avatar
Tomek Mrugalski committed
118 119
                cout << "Replying with message type "
                     << static_cast<int>(rsp->getType()) << ":" << endl;
120 121 122
                cout << rsp->toText();
                cout << "----" << endl;
                if (rsp->pack()) {
123
                    cout << "Packet assembled correctly." << endl;
124
                }
125
                IfaceMgr::instance().send(rsp);
126 127 128 129 130 131 132 133 134 135 136 137
            }
        }

        // TODO add support for config session (see src/bin/auth/main.cc)
        //      so this daemon can be controlled from bob
    }

    return (true);
}

void
Dhcpv4Srv::setServerID() {
138 139 140
    /// TODO implement this for real once interface detection (ticket 1237)
    /// is done. Use hardcoded server-id for now.

141
#if 0
142
    // uncomment this once ticket 1350 is merged.
143 144 145 146 147 148
    IOAddress srvId("127.0.0.1");
    serverid_ = boost::shared_ptr<Option>(
      new Option4AddrLst(Option::V4, DHO_DHCP_SERVER_IDENTIFIER, srvId));
#endif
}

149

150 151 152 153 154
void Dhcpv4Srv::copyDefaultFields(const boost::shared_ptr<Pkt4>& question,
                                  boost::shared_ptr<Pkt4>& answer) {
    answer->setIface(question->getIface());
    answer->setIndex(question->getIndex());
    answer->setCiaddr(question->getCiaddr());
155

156 157
    answer->setSiaddr(IOAddress("0.0.0.0")); // explictly set this to 0
    answer->setHops(question->getHops());
158 159

    // copy MAC address
Tomek Mrugalski's avatar
Tomek Mrugalski committed
160 161
    vector<uint8_t> mac(question->getChaddr(),
                        question->getChaddr() + Pkt4::MAX_CHADDR_LEN);
162
    answer->setHWAddr(question->getHtype(), question->getHlen(), mac);
163 164

    // relay address
165 166 167
    answer->setGiaddr(question->getGiaddr());

    if (question->getGiaddr().toText() != "0.0.0.0") {
168
        // relayed traffic
169
        answer->setRemoteAddr(question->getGiaddr());
170 171
    } else {
        // direct traffic
172
        answer->setRemoteAddr(question->getRemoteAddr());
173 174
    }

175 176 177
}

void Dhcpv4Srv::appendDefaultOptions(boost::shared_ptr<Pkt4>& msg, uint8_t msg_type) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
178
    boost::shared_ptr<Option> opt;
179 180 181

    // add Message Type Option (type 53)
    std::vector<uint8_t> tmp;
182
    tmp.push_back(static_cast<uint8_t>(msg_type));
183
    opt = boost::shared_ptr<Option>(new Option(Option::V4, DHO_DHCP_MESSAGE_TYPE, tmp));
184 185
    msg->addOption(opt);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
186 187 188 189 190
    // DHCP Server Identifier (type 54)
    opt = boost::shared_ptr<Option>
        (new Option4AddrLst(DHO_DHCP_SERVER_IDENTIFIER, IOAddress(HARDCODED_SERVER_ID)));
    msg->addOption(opt);

191 192 193 194
    // more options will be added here later
}


Tomek Mrugalski's avatar
Tomek Mrugalski committed
195
void Dhcpv4Srv::appendRequestedOptions(boost::shared_ptr<Pkt4>& msg) {
196 197
    boost::shared_ptr<Option> opt;

Tomek Mrugalski's avatar
Tomek Mrugalski committed
198 199 200 201 202
    // Domain name (type 15)
    vector<uint8_t> domain(HARDCODED_DOMAIN_NAME.begin(), HARDCODED_DOMAIN_NAME.end());
    opt = boost::shared_ptr<Option>(new Option(Option::V4, DHO_DOMAIN_NAME, domain));
    msg->addOption(opt);
    // TODO: Add Option_String class
203

Tomek Mrugalski's avatar
Tomek Mrugalski committed
204 205 206 207 208
    // DNS servers (type 6)
    opt = boost::shared_ptr<Option>
        (new Option4AddrLst(DHO_DOMAIN_NAME_SERVERS, IOAddress(HARDCODED_DNS_SERVER)));
    msg->addOption(opt);
}
209

Tomek Mrugalski's avatar
Tomek Mrugalski committed
210
void Dhcpv4Srv::tryAssignLease(boost::shared_ptr<Pkt4>& msg) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
211
    boost::shared_ptr<Option> opt;
212

Tomek Mrugalski's avatar
Tomek Mrugalski committed
213 214
    // TODO: Implement actual lease assignment here
    msg->setYiaddr(IOAddress(HARDCODED_LEASE));
215 216

    // IP Address Lease time (type 51)
217 218
    opt = boost::shared_ptr<Option>(new Option(Option::V4, DHO_DHCP_LEASE_TIME));
    opt->setUint32(HARDCODED_LEASE_TIME);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
219
    msg->addOption(opt);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
220
    // TODO: create Option_IntArray that holds list of integers, similar to Option4_AddrLst
221 222

    // Subnet mask (type 1)
223 224
    opt = boost::shared_ptr<Option>
        (new Option4AddrLst(DHO_SUBNET_MASK, IOAddress(HARDCODED_NETMASK)));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
225
    msg->addOption(opt);
226 227

    // Router (type 3)
228 229
    opt = boost::shared_ptr<Option>
        (new Option4AddrLst(DHO_ROUTERS, IOAddress(HARDCODED_GATEWAY)));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
230 231
    msg->addOption(opt);
}
232

Tomek Mrugalski's avatar
Tomek Mrugalski committed
233 234 235 236
boost::shared_ptr<Pkt4>
Dhcpv4Srv::processDiscover(boost::shared_ptr<Pkt4>& discover) {
    boost::shared_ptr<Pkt4> offer = boost::shared_ptr<Pkt4>
        (new Pkt4(DHCPOFFER, discover->getTransid()));
237

Tomek Mrugalski's avatar
Tomek Mrugalski committed
238 239 240 241
    copyDefaultFields(discover, offer);
    appendDefaultOptions(offer, DHCPOFFER);
    appendRequestedOptions(offer);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
242
    tryAssignLease(offer);
243 244

    return (offer);
245 246 247
}

boost::shared_ptr<Pkt4>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
248
Dhcpv4Srv::processRequest(boost::shared_ptr<Pkt4>& request) {
249 250 251 252 253
    boost::shared_ptr<Pkt4> ack = boost::shared_ptr<Pkt4>
        (new Pkt4(DHCPACK, request->getTransid()));

    copyDefaultFields(request, ack);
    appendDefaultOptions(ack, DHCPACK);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
254
    appendRequestedOptions(ack);
255

Tomek Mrugalski's avatar
Tomek Mrugalski committed
256
    tryAssignLease(ack);
257 258

    return (ack);
259 260
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
261
void Dhcpv4Srv::processRelease(boost::shared_ptr<Pkt4>& release) {
262 263 264
    /// TODO: Implement this.
    cout << "Received RELEASE on " << release->getIface() << " interface." << endl;
}
265

Tomek Mrugalski's avatar
Tomek Mrugalski committed
266
void Dhcpv4Srv::processDecline(boost::shared_ptr<Pkt4>& decline) {
267 268 269 270
    /// TODO: Implement this.
    cout << "Received DECLINE on " << decline->getIface() << " interface." << endl;
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
271
boost::shared_ptr<Pkt4> Dhcpv4Srv::processInform(boost::shared_ptr<Pkt4>& inform) {
272
    /// TODO: Currently implemented echo mode. Implement this for real
273 274
    return (inform);
}