dhcp4_srv.cc 8.49 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
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

Tomek Mrugalski's avatar
Tomek Mrugalski committed
63
64
65
        // client's message
        Pkt4Ptr query = IfaceMgr::instance().receive4();
        Pkt4Ptr rsp;   // server's response
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
    IOAddress srvId("127.0.0.1");
144
    serverid_ = OptionPtr(
145
146
147
148
      new Option4AddrLst(Option::V4, DHO_DHCP_SERVER_IDENTIFIER, srvId));
#endif
}

149

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

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

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

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

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

174
175
}

176
177
void Dhcpv4Srv::appendDefaultOptions(Pkt4Ptr& msg, uint8_t msg_type) {
    OptionPtr opt;
178
179
180

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

Tomek Mrugalski's avatar
Tomek Mrugalski committed
185
    // DHCP Server Identifier (type 54)
186
    opt = OptionPtr
Tomek Mrugalski's avatar
Tomek Mrugalski committed
187
188
189
        (new Option4AddrLst(DHO_DHCP_SERVER_IDENTIFIER, IOAddress(HARDCODED_SERVER_ID)));
    msg->addOption(opt);

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


194
195
void Dhcpv4Srv::appendRequestedOptions(Pkt4Ptr& msg) {
    OptionPtr opt;
196

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

Tomek Mrugalski's avatar
Tomek Mrugalski committed
203
    // DNS servers (type 6)
204
    opt = OptionPtr(new Option4AddrLst(DHO_DOMAIN_NAME_SERVERS, IOAddress(HARDCODED_DNS_SERVER)));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
205
206
    msg->addOption(opt);
}
207

208
209
void Dhcpv4Srv::tryAssignLease(Pkt4Ptr& msg) {
    OptionPtr opt;
210

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

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

    // Subnet mask (type 1)
221
    opt = OptionPtr(new Option4AddrLst(DHO_SUBNET_MASK, IOAddress(HARDCODED_NETMASK)));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
222
    msg->addOption(opt);
223
224

    // Router (type 3)
225
    opt = OptionPtr(new Option4AddrLst(DHO_ROUTERS, IOAddress(HARDCODED_GATEWAY)));
Tomek Mrugalski's avatar
Tomek Mrugalski committed
226
227
    msg->addOption(opt);
}
228

229
230
Pkt4Ptr Dhcpv4Srv::processDiscover(Pkt4Ptr& discover) {
    Pkt4Ptr offer = Pkt4Ptr
Tomek Mrugalski's avatar
Tomek Mrugalski committed
231
        (new Pkt4(DHCPOFFER, discover->getTransid()));
232

Tomek Mrugalski's avatar
Tomek Mrugalski committed
233
234
235
236
    copyDefaultFields(discover, offer);
    appendDefaultOptions(offer, DHCPOFFER);
    appendRequestedOptions(offer);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
237
    tryAssignLease(offer);
238
239

    return (offer);
240
241
}

242
243
Pkt4Ptr Dhcpv4Srv::processRequest(Pkt4Ptr& request) {
    Pkt4Ptr ack = Pkt4Ptr
244
245
246
247
        (new Pkt4(DHCPACK, request->getTransid()));

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

Tomek Mrugalski's avatar
Tomek Mrugalski committed
250
    tryAssignLease(ack);
251
252

    return (ack);
253
254
}

255
void Dhcpv4Srv::processRelease(Pkt4Ptr& release) {
256
257
258
    /// TODO: Implement this.
    cout << "Received RELEASE on " << release->getIface() << " interface." << endl;
}
259

260
void Dhcpv4Srv::processDecline(Pkt4Ptr& decline) {
261
262
263
264
    /// TODO: Implement this.
    cout << "Received DECLINE on " << decline->getIface() << " interface." << endl;
}

265
Pkt4Ptr Dhcpv4Srv::processInform(Pkt4Ptr& inform) {
266
    /// TODO: Currently implemented echo mode. Implement this for real
267
268
    return (inform);
}