dhcp4_srv.cc 10.3 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
31
32
33
34
35
36
37
38
// These are hardcoded parameters. Currently this is a skeleton server that only
// grants those options and a single, fixed, hardcoded lease.
const std::string HARDCODED_LEASE = "10.3.2.222"; // assigned lease
const std::string HARDCODED_NETMASK = "255.255.255.0";
const uint32_t    HARDCODED_LEASE_TIME = 60; // in seconds
const std::string HARDCODED_GATEWAY = "10.3.2.2";
const std::string HARDCODED_DNS_SERVER = "8.8.8.8";
const std::string HARDCODED_DOMAIN_NAME = "isc.example.org";
const std::string HARDCODED_SERVER_ID = "10.3.1.1";

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) {
191
192
193
194

    // add Message Type Option (type 53)
    boost::shared_ptr<Option> opt;
    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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    msg->addOption(opt);

    // more options will be added here later
}


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()));

    boost::shared_ptr<Option> opt;

    copyDefaultFields(discover, offer);

    appendDefaultOptions(offer, DHCPOFFER);

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

    // DHCP Server Identifier (type 54)
218
219
    opt = boost::shared_ptr<Option>
        (new Option4AddrLst(DHO_DHCP_SERVER_IDENTIFIER, IOAddress(HARDCODED_SERVER_ID)));
220
221
222
    offer->addOption(opt);

    // IP Address Lease time (type 51)
223
224
225
    opt = boost::shared_ptr<Option>(new Option(Option::V4, DHO_DHCP_LEASE_TIME));
    opt->setUint32(HARDCODED_LEASE_TIME);
    offer->addOption(opt);
226
227

    // Subnet mask (type 1)
228
229
    opt = boost::shared_ptr<Option>
        (new Option4AddrLst(DHO_SUBNET_MASK, IOAddress(HARDCODED_NETMASK)));
230
231
232
    offer->addOption(opt);

    // Router (type 3)
233
234
    opt = boost::shared_ptr<Option>
        (new Option4AddrLst(DHO_ROUTERS, IOAddress(HARDCODED_GATEWAY)));
235
236
237
    offer->addOption(opt);

    // Domain name (type 15)
238
239
240
241
    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));
    offer->addOption(opt);
    // TODO: Add Option_String class
242
243

    // DNS servers (type 6)
244
245
    opt = boost::shared_ptr<Option>
        (new Option4AddrLst(DHO_DOMAIN_NAME_SERVERS, IOAddress(HARDCODED_DNS_SERVER)));
246
247
248
    offer->addOption(opt);

    return (offer);
249
250
251
}

boost::shared_ptr<Pkt4>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
252
Dhcpv4Srv::processRequest(boost::shared_ptr<Pkt4>& request) {
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
    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);

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

    // DHCP Server Identifier (type 54)
    opt = boost::shared_ptr<Option>
        (new Option4AddrLst(DHO_DHCP_SERVER_IDENTIFIER, IOAddress(HARDCODED_SERVER_ID)));
    ack->addOption(opt);

    // 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);

    // 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));
    ack->addOption(opt);
    // TODO: Add Option_String class

    // DNS servers (type 6)
    opt = boost::shared_ptr<Option>
        (new Option4AddrLst(DHO_DOMAIN_NAME_SERVERS, IOAddress(HARDCODED_DNS_SERVER)));
    ack->addOption(opt);

    return (ack);
297
298
}

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

Tomek Mrugalski's avatar
Tomek Mrugalski committed
304
void Dhcpv4Srv::processDecline(boost::shared_ptr<Pkt4>& decline) {
305
306
307
308
    /// TODO: Implement this.
    cout << "Received DECLINE on " << decline->getIface() << " interface." << endl;
}

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