dhcp4_srv.cc 8.87 KB
Newer Older
1
// Copyright (C) 2011-2012 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
// 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
    try {
        // first call to instance() will create IfaceMgr (it's a singleton)
        // it may throw something if things go wrong
        IfaceMgr::instance();
44

45
        /// @todo: instantiate LeaseMgr here once it is imlpemented.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
46

47
        IfaceMgr::instance().openSockets4(port);
48

49 50 51 52 53 54
        setServerID();
    } catch (const std::exception &e) {
        cerr << "Error during DHCPv4 server startup: " << e.what() << endl;
        shutdown_ = true;
        return;
    }
55

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

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

64 65 66 67 68
void Dhcpv4Srv::shutdown() {
    cout << "b10-dhcp4: DHCPv4 server shutdown." << endl;
    shutdown_ = true;
}

69 70
bool
Dhcpv4Srv::run() {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
71
    while (!shutdown_) {
72 73 74
        /// @todo: calculate actual timeout once we have lease database
        int timeout = 1000;

Tomek Mrugalski's avatar
Tomek Mrugalski committed
75
        // client's message and server's response
76
        Pkt4Ptr query = IfaceMgr::instance().receive4(timeout);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
77
        Pkt4Ptr rsp;
78

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

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

109
            cout << "Received message type " << int(query->getType()) << endl;
110

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

            if (rsp) {
116 117 118 119 120 121 122 123 124
                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);
                }

125 126 127 128 129
                rsp->setLocalAddr(query->getLocalAddr());
                rsp->setLocalPort(DHCP4_SERVER_PORT);
                rsp->setIface(query->getIface());
                rsp->setIndex(query->getIndex());

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

        // 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
    IOAddress srvId("127.0.0.1");
156
    serverid_ = OptionPtr(
157 158 159 160
      new Option4AddrLst(Option::V4, DHO_DHCP_SERVER_IDENTIFIER, srvId));
#endif
}

161

162
void Dhcpv4Srv::copyDefaultFields(const Pkt4Ptr& question, Pkt4Ptr& answer) {
163 164 165
    answer->setIface(question->getIface());
    answer->setIndex(question->getIndex());
    answer->setCiaddr(question->getCiaddr());
166

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

    // copy MAC address
Tomek Mrugalski's avatar
Tomek Mrugalski committed
171 172
    vector<uint8_t> mac(question->getChaddr(),
                        question->getChaddr() + Pkt4::MAX_CHADDR_LEN);
173
    answer->setHWAddr(question->getHtype(), question->getHlen(), mac);
174 175

    // relay address
176 177 178
    answer->setGiaddr(question->getGiaddr());

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

186 187
}

188 189
void Dhcpv4Srv::appendDefaultOptions(Pkt4Ptr& msg, uint8_t msg_type) {
    OptionPtr opt;
190 191 192

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

Tomek Mrugalski's avatar
Tomek Mrugalski committed
197
    // DHCP Server Identifier (type 54)
198
    opt = OptionPtr
Tomek Mrugalski's avatar
Tomek Mrugalski committed
199 200 201
        (new Option4AddrLst(DHO_DHCP_SERVER_IDENTIFIER, IOAddress(HARDCODED_SERVER_ID)));
    msg->addOption(opt);

202 203 204 205
    // more options will be added here later
}


206 207
void Dhcpv4Srv::appendRequestedOptions(Pkt4Ptr& msg) {
    OptionPtr opt;
208

Tomek Mrugalski's avatar
Tomek Mrugalski committed
209 210
    // Domain name (type 15)
    vector<uint8_t> domain(HARDCODED_DOMAIN_NAME.begin(), HARDCODED_DOMAIN_NAME.end());
211
    opt = OptionPtr(new Option(Option::V4, DHO_DOMAIN_NAME, domain));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
212 213
    msg->addOption(opt);
    // TODO: Add Option_String class
214

Tomek Mrugalski's avatar
Tomek Mrugalski committed
215
    // DNS servers (type 6)
216
    opt = OptionPtr(new Option4AddrLst(DHO_DOMAIN_NAME_SERVERS, IOAddress(HARDCODED_DNS_SERVER)));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
217 218
    msg->addOption(opt);
}
219

220 221
void Dhcpv4Srv::tryAssignLease(Pkt4Ptr& msg) {
    OptionPtr opt;
222

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

    // IP Address Lease time (type 51)
227
    opt = OptionPtr(new Option(Option::V4, DHO_DHCP_LEASE_TIME));
228
    opt->setUint32(HARDCODED_LEASE_TIME);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
229
    msg->addOption(opt);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
230
    // TODO: create Option_IntArray that holds list of integers, similar to Option4_AddrLst
231 232

    // Subnet mask (type 1)
233
    opt = OptionPtr(new Option4AddrLst(DHO_SUBNET_MASK, IOAddress(HARDCODED_NETMASK)));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
234
    msg->addOption(opt);
235 236

    // Router (type 3)
237
    opt = OptionPtr(new Option4AddrLst(DHO_ROUTERS, IOAddress(HARDCODED_GATEWAY)));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
238 239
    msg->addOption(opt);
}
240

241 242
Pkt4Ptr Dhcpv4Srv::processDiscover(Pkt4Ptr& discover) {
    Pkt4Ptr offer = Pkt4Ptr
Tomek Mrugalski's avatar
Tomek Mrugalski committed
243
        (new Pkt4(DHCPOFFER, discover->getTransid()));
244

Tomek Mrugalski's avatar
Tomek Mrugalski committed
245 246 247 248
    copyDefaultFields(discover, offer);
    appendDefaultOptions(offer, DHCPOFFER);
    appendRequestedOptions(offer);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
249
    tryAssignLease(offer);
250 251

    return (offer);
252 253
}

254 255
Pkt4Ptr Dhcpv4Srv::processRequest(Pkt4Ptr& request) {
    Pkt4Ptr ack = Pkt4Ptr
256 257 258 259
        (new Pkt4(DHCPACK, request->getTransid()));

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

Tomek Mrugalski's avatar
Tomek Mrugalski committed
262
    tryAssignLease(ack);
263 264

    return (ack);
265 266
}

267
void Dhcpv4Srv::processRelease(Pkt4Ptr& release) {
268 269 270
    /// TODO: Implement this.
    cout << "Received RELEASE on " << release->getIface() << " interface." << endl;
}
271

272
void Dhcpv4Srv::processDecline(Pkt4Ptr& decline) {
273 274 275 276
    /// TODO: Implement this.
    cout << "Received DECLINE on " << decline->getIface() << " interface." << endl;
}

277
Pkt4Ptr Dhcpv4Srv::processInform(Pkt4Ptr& inform) {
278
    /// TODO: Currently implemented echo mode. Implement this for real
279 280
    return (inform);
}