dhcp4_srv.cc 9.94 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
// #define ECHO_SERVER

29 30
// 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
31
const std::string HARDCODED_LEASE = "192.0.2.222"; // assigned lease
32 33
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
34 35 36 37
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";
38

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

    // 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
47 48
    IfaceMgr::instance().printIfaces();

49
    // uncomment this once #1238, #992 and #1239 are merged
Tomek Mrugalski's avatar
Tomek Mrugalski committed
50
    IfaceMgr::instance().openSockets4(port);
51 52 53

    setServerID();

Tomek Mrugalski's avatar
Tomek Mrugalski committed
54
    shutdown_ = false;
55 56 57 58
}

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

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

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

70
#if defined(ECHO_SERVER)
Tomek Mrugalski's avatar
Tomek Mrugalski committed
71
        query->repack();
72 73
        IfaceMgr::instance().send(query);
        continue;
74 75 76
#endif

        if (query) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
77 78 79 80
            try {
                query->unpack();
            } catch (const std::exception& e) {
                /// TODO: Printout reasons of failed parsing
81
                cout << "Failed to parse incoming packet " << endl;
82 83
                continue;
            }
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
            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;
            }

106
            cout << "Received message type " << int(query->getType()) << endl;
107

108 109
            // TODO: print out received packets only if verbose (or debug)
            // mode is enabled
110 111 112
            cout << query->toText();

            if (rsp) {
113 114 115 116 117 118 119 120 121
                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);
                }

122 123 124 125 126
                rsp->setLocalAddr(query->getLocalAddr());
                rsp->setLocalPort(DHCP4_SERVER_PORT);
                rsp->setIface(query->getIface());
                rsp->setIndex(query->getIndex());

Tomek Mrugalski's avatar
Tomek Mrugalski committed
127 128
                cout << "Replying with message type "
                     << static_cast<int>(rsp->getType()) << ":" << endl;
129 130 131
                cout << rsp->toText();
                cout << "----" << endl;
                if (rsp->pack()) {
132
                    cout << "Packet assembled correctly." << endl;
133
                }
134
#if 1
135
                // uncomment this once ticket 1240 is merged.
136
                IfaceMgr::instance().send(rsp);
137 138 139 140 141 142 143 144 145 146 147 148 149
#endif
            }
        }

        // 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() {
150 151 152
    /// TODO implement this for real once interface detection (ticket 1237)
    /// is done. Use hardcoded server-id for now.

153
#if 0
154
    // uncomment this once ticket 1350 is merged.
155 156 157 158 159 160
    IOAddress srvId("127.0.0.1");
    serverid_ = boost::shared_ptr<Option>(
      new Option4AddrLst(Option::V4, DHO_DHCP_SERVER_IDENTIFIER, srvId));
#endif
}

161

162 163 164 165 166
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());
167

168 169
    answer->setSiaddr(IOAddress("0.0.0.0")); // explictly set this to 0
    answer->setHops(question->getHops());
170 171 172 173

    // copy MAC address
    vector<uint8_t> mac;
    mac.resize(Pkt4::MAX_CHADDR_LEN);
174 175
    memcpy(&mac[0], question->getChaddr(), Pkt4::MAX_CHADDR_LEN);
    answer->setHWAddr(question->getHtype(), question->getHlen(), mac);
176 177

    // relay address
178 179 180
    answer->setGiaddr(question->getGiaddr());

    if (question->getGiaddr().toText() != "0.0.0.0") {
181
        // relayed traffic
182
        answer->setRemoteAddr(question->getGiaddr());
183 184
    } else {
        // direct traffic
185
        answer->setRemoteAddr(question->getRemoteAddr());
186 187
    }

188 189 190
}

void Dhcpv4Srv::appendDefaultOptions(boost::shared_ptr<Pkt4>& msg, uint8_t msg_type) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
191
    boost::shared_ptr<Option> opt;
192 193 194

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

Tomek Mrugalski's avatar
Tomek Mrugalski committed
199 200 201 202 203
    // DHCP Server Identifier (type 54)
    opt = boost::shared_ptr<Option>
        (new Option4AddrLst(DHO_DHCP_SERVER_IDENTIFIER, IOAddress(HARDCODED_SERVER_ID)));
    msg->addOption(opt);

204 205 206 207
    // more options will be added here later
}


Tomek Mrugalski's avatar
Tomek Mrugalski committed
208
void Dhcpv4Srv::appendRequestedOptions(boost::shared_ptr<Pkt4>& msg) {
209 210
    boost::shared_ptr<Option> opt;

Tomek Mrugalski's avatar
Tomek Mrugalski committed
211 212 213 214 215
    // 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
216

Tomek Mrugalski's avatar
Tomek Mrugalski committed
217 218 219 220 221
    // DNS servers (type 6)
    opt = boost::shared_ptr<Option>
        (new Option4AddrLst(DHO_DOMAIN_NAME_SERVERS, IOAddress(HARDCODED_DNS_SERVER)));
    msg->addOption(opt);
}
222

Tomek Mrugalski's avatar
Tomek Mrugalski committed
223 224
void Dhcpv4Srv::assignLease(boost::shared_ptr<Pkt4>& msg) {
    boost::shared_ptr<Option> opt;
225

Tomek Mrugalski's avatar
Tomek Mrugalski committed
226 227
    // TODO: Implement actual lease assignment here
    msg->setYiaddr(IOAddress(HARDCODED_LEASE));
228 229

    // IP Address Lease time (type 51)
230 231
    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
232
    msg->addOption(opt);
233 234

    // Subnet mask (type 1)
235 236
    opt = boost::shared_ptr<Option>
        (new Option4AddrLst(DHO_SUBNET_MASK, IOAddress(HARDCODED_NETMASK)));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
237
    msg->addOption(opt);
238 239

    // Router (type 3)
240 241
    opt = boost::shared_ptr<Option>
        (new Option4AddrLst(DHO_ROUTERS, IOAddress(HARDCODED_GATEWAY)));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
242 243
    msg->addOption(opt);
}
244

Tomek Mrugalski's avatar
Tomek Mrugalski committed
245 246 247 248
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()));
249

Tomek Mrugalski's avatar
Tomek Mrugalski committed
250 251 252 253 254 255 256
    boost::shared_ptr<Option> opt;

    copyDefaultFields(discover, offer);
    appendDefaultOptions(offer, DHCPOFFER);
    appendRequestedOptions(offer);

    assignLease(offer);
257 258

    return (offer);
259 260 261
}

boost::shared_ptr<Pkt4>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
262
Dhcpv4Srv::processRequest(boost::shared_ptr<Pkt4>& request) {
263 264 265 266 267 268 269
    boost::shared_ptr<Pkt4> ack = boost::shared_ptr<Pkt4>
        (new Pkt4(DHCPACK, request->getTransid()));

    boost::shared_ptr<Option> opt;

    copyDefaultFields(request, ack);
    appendDefaultOptions(ack, DHCPACK);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
270
    appendRequestedOptions(ack);
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

    // TODO: Implement actual lease assignment here
    ack->setYiaddr(IOAddress(HARDCODED_LEASE));

    // IP Address Lease time (type 51)
    opt = boost::shared_ptr<Option>(new Option(Option::V4, DHO_DHCP_LEASE_TIME));
    opt->setUint32(HARDCODED_LEASE_TIME);
    ack->addOption(opt);
    // TODO: create Option_IntArray that holds list of integers, similar to Option4_AddrLst

    // Subnet mask (type 1)
    opt = boost::shared_ptr<Option>
        (new Option4AddrLst(DHO_SUBNET_MASK, IOAddress(HARDCODED_NETMASK)));
    ack->addOption(opt);

    // Router (type 3)
    opt = boost::shared_ptr<Option>(new Option4AddrLst(DHO_ROUTERS, IOAddress(HARDCODED_GATEWAY)));
    ack->addOption(opt);

    return (ack);
291 292
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
293
void Dhcpv4Srv::processRelease(boost::shared_ptr<Pkt4>& release) {
294 295 296
    /// TODO: Implement this.
    cout << "Received RELEASE on " << release->getIface() << " interface." << endl;
}
297

Tomek Mrugalski's avatar
Tomek Mrugalski committed
298
void Dhcpv4Srv::processDecline(boost::shared_ptr<Pkt4>& decline) {
299 300 301 302
    /// TODO: Implement this.
    cout << "Received DECLINE on " << decline->getIface() << " interface." << endl;
}

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