dhcp6_srv.h 10.9 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
17
//
// 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.

#ifndef DHCPV6_SRV_H
#define DHCPV6_SRV_H

18
19
#include <iostream>

20
#include <boost/noncopyable.hpp>
21
#include <dhcp/alloc_engine.h>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
22
#include <dhcp/dhcp6.h>
23
#include <dhcp/duid.h>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
24
#include <dhcp/option.h>
25
#include <dhcp/option6_ia.h>
26
#include <dhcp/option_definition.h>
27
28
#include <dhcp/pkt6.h>
#include <dhcp/subnet.h>
29
30

namespace isc {
31

32
33
34
namespace dhcp {
/// @brief DHCPv6 server service.
///
Stephen Morris's avatar
Stephen Morris committed
35
/// This class represents DHCPv6 server. It contains all
36
37
38
39
40
/// top-level methods and routines necessary for server operation.
/// In particular, it instantiates IfaceMgr, loads or generates DUID
/// that is going to be used as server-identifier, receives incoming
/// packets, processes them, manages leases assignment and generates
/// appropriate responses.
Stephen Morris's avatar
Stephen Morris committed
41
42
43
44
///
/// @note Only one instance of this class is instantated as it encompasses
///       the whole operation of the server.  Nothing, however, enforces the
///       singleton status of the object.
45
46
47
class Dhcpv6Srv : public boost::noncopyable {

public:
Tomek Mrugalski's avatar
Tomek Mrugalski committed
48
49
50
51

    /// @brief Minimum length of a MAC address to be used in DUID generation.
    static const size_t MIN_MAC_LEN = 6;

52
53
54
55
56
57
    /// @brief Default constructor.
    ///
    /// Instantiates necessary services, required to run DHCPv6 server.
    /// In particular, creates IfaceMgr that will be responsible for
    /// network interaction. Will instantiate lease manager, and load
    /// old or create new DUID.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
58
59
60
    ///
    /// @param port port on will all sockets will listen
    Dhcpv6Srv(uint16_t port = DHCP6_SERVER_PORT);
61
62

    /// @brief Destructor. Used during DHCPv6 service shutdown.
63
    virtual ~Dhcpv6Srv();
64

Tomek Mrugalski's avatar
Tomek Mrugalski committed
65
    /// @brief Returns server-intentifier option.
66
67
    ///
    /// @return server-id option
68
    OptionPtr getServerID() { return serverid_; }
69
70
71
72
73
74
75
76
77
78
79

    /// @brief Main server processing loop.
    ///
    /// Main server processing loop. Receives incoming packets, verifies
    /// their correctness, generates appropriate answer (if needed) and
    /// transmits respones.
    ///
    /// @return true, if being shut down gracefully, fail if experienced
    ///         critical error.
    bool run();

80
81
    /// @brief Instructs the server to shut down.
    void shutdown();
82

Tomek Mrugalski's avatar
Tomek Mrugalski committed
83
    /// @brief Return textual type of packet received by server.
84
    ///
85
    /// Returns the name of valid packet received by the server (e.g. SOLICIT).
86
    /// If the packet is unknown - or if it is a valid DHCP packet but not one
87
88
    /// expected to be received by the server (such as an ADVERTISE), the string
    /// "UNKNOWN" is returned.  This method is used in debug messages.
89
90
91
92
93
94
95
96
97
98
99
    ///
    /// As the operation of the method does not depend on any server state, it
    /// is declared static.
    ///
    /// @param type DHCPv4 packet type
    ///
    /// @return Pointer to "const" string containing the packet name.
    ///         Note that this string is statically allocated and MUST NOT
    ///         be freed by the caller.
    static const char* serverReceivedPacketName(uint8_t type);

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
protected:
    /// @brief Processes incoming SOLICIT and returns response.
    ///
    /// Processes received SOLICIT message and verifies that its sender
    /// should be served. In particular IA, TA and PD options are populated
    /// with to-be assinged addresses, temporary addresses and delegated
    /// prefixes, respectively. In the usual 4 message exchange, server is
    /// expected to respond with ADVERTISE message. However, if client
    /// requests rapid-commit and server supports it, REPLY will be sent
    /// instead of ADVERTISE and requested leases will be assigned
    /// immediately.
    ///
    /// @param solicit SOLICIT message received from client
    ///
    /// @return ADVERTISE, REPLY message or NULL
115
    Pkt6Ptr processSolicit(const Pkt6Ptr& solicit);
116
117
118
119
120
121
122
123
124
125
126
127

    /// @brief Processes incoming REQUEST and returns REPLY response.
    ///
    /// Processes incoming REQUEST message and verifies that its sender
    /// should be served. In particular IA, TA and PD options are populated
    /// with assinged addresses, temporary addresses and delegated
    /// prefixes, respectively. Uses LeaseMgr to allocate or update existing
    /// leases.
    ///
    /// @param request a message received from client
    ///
    /// @return REPLY message or NULL
128
    Pkt6Ptr processRequest(const Pkt6Ptr& request);
129
130
131
132

    /// @brief Stub function that will handle incoming RENEW messages.
    ///
    /// @param renew message received from client
133
    Pkt6Ptr processRenew(const Pkt6Ptr& renew);
134
135
136
137

    /// @brief Stub function that will handle incoming REBIND messages.
    ///
    /// @param rebind message received from client
138
    Pkt6Ptr processRebind(const Pkt6Ptr& rebind);
139
140
141
142

    /// @brief Stub function that will handle incoming CONFIRM messages.
    ///
    /// @param confirm message received from client
143
    Pkt6Ptr processConfirm(const Pkt6Ptr& confirm);
144
145
146
147

    /// @brief Stub function that will handle incoming RELEASE messages.
    ///
    /// @param release message received from client
148
    Pkt6Ptr processRelease(const Pkt6Ptr& release);
149
150
151
152

    /// @brief Stub function that will handle incoming DECLINE messages.
    ///
    /// @param decline message received from client
153
    Pkt6Ptr processDecline(const Pkt6Ptr& decline);
154
155
156
157

    /// @brief Stub function that will handle incoming INF-REQUEST messages.
    ///
    /// @param infRequest message received from client
158
    Pkt6Ptr processInfRequest(const Pkt6Ptr& infRequest);
159

Tomek Mrugalski's avatar
Tomek Mrugalski committed
160
    /// @brief Creates status-code option.
161
162
163
164
165
166
    ///
    /// @param code status code value (see RFC3315)
    /// @param text textual explanation (will be sent in status code option)
    /// @return status-code option
    OptionPtr createStatusCode(uint16_t code, const std::string& text);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
167
    /// @brief Selects a subnet for a given client's packet.
168
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
169
    /// @param question client's message
170
    /// @return selected subnet (or NULL if no suitable subnet was found)
171
    isc::dhcp::Subnet6Ptr selectSubnet(const Pkt6Ptr& question);
172

Tomek Mrugalski's avatar
Tomek Mrugalski committed
173
    /// @brief Processes IA_NA option (and assigns addresses if necessary).
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    ///
    /// Generates response to IA_NA. This typically includes selecting (and
    /// allocating a lease in case of REQUEST) a lease and creating
    /// IAADDR option. In case of allocation failure, it may contain
    /// status code option with non-zero status, denoting cause of the
    /// allocation failure.
    ///
    /// @param subnet subnet the client is connected to
    /// @param duid client's duid
    /// @param question client's message (typically SOLICIT or REQUEST)
    /// @param ia pointer to client's IA_NA option (client's request)
    /// @return IA_NA option (server's response)
    OptionPtr handleIA_NA(const isc::dhcp::Subnet6Ptr& subnet,
                          const isc::dhcp::DuidPtr& duid,
                          isc::dhcp::Pkt6Ptr question,
                          boost::shared_ptr<Option6IA> ia);

Tomek Mrugalski's avatar
Tomek Mrugalski committed
191
    /// @brief Copies required options from client message to server answer.
192
193
194
195
196
197
198
    ///
    /// Copies options that must appear in any server response (ADVERTISE, REPLY)
    /// to client's messages (SOLICIT, REQUEST, RENEW, REBIND, DECLINE, RELEASE).
    /// One notable example is client-id. Other options may be copied as required.
    ///
    /// @param question client's message (options will be copied from here)
    /// @param answer server's message (options will be copied here)
199
    void copyDefaultOptions(const Pkt6Ptr& question, Pkt6Ptr& answer);
200
201
202
203
204
205
206
207
208

    /// @brief Appends default options to server's answer.
    ///
    /// Adds required options to server's answer. In particular, server-id
    /// is added. Possibly other mandatory options will be added, depending
    /// on type (or content) of client message.
    ///
    /// @param question client's message
    /// @param answer server's message (options will be added here)
209
    void appendDefaultOptions(const Pkt6Ptr& question, Pkt6Ptr& answer);
210
211
212
213
214
215
216

    /// @brief Appends requested options to server's answer.
    ///
    /// Appends options requested by client to the server's answer.
    ///
    /// @param question client's message
    /// @param answer server's message (options will be added here)
217
    void appendRequestedOptions(const Pkt6Ptr& question, Pkt6Ptr& answer);
218
219
220
221
222
223
224
225
226

    /// @brief Assigns leases.
    ///
    /// TODO: This method is currently a stub. It just appends one
    /// hardcoded lease. It supports addresses (IA_NA) only. It does NOT
    /// support temporary addresses (IA_TA) nor prefixes (IA_PD).
    ///
    /// @param question client's message (with requested IA_NA)
    /// @param answer server's message (IA_NA options will be added here)
227
    void assignLeases(const Pkt6Ptr& question, Pkt6Ptr& answer);
228
229
230
231
232
233
234
235
236
237
238
239
240

    /// @brief Sets server-identifier.
    ///
    /// This method attempts to set server-identifier DUID. It loads it
    /// from a file. If file load fails, it generates new DUID using
    /// interface link-layer addresses (EUI-64) + timestamp (DUID type
    /// duid-llt, see RFC3315, section 9.2). If there are no suitable
    /// interfaces present, exception it thrown
    ///
    /// @throws isc::Unexpected Failed to read DUID file and no suitable
    ///         interfaces for new DUID generation are detected.
    void setServerID();

241
242
243
244
245
246
247
248
249
250
251
    /// @brief Initializes option definitions for standard options.
    ///
    /// Each standard option's format is described by the
    /// dhcp::OptionDefinition object. This function creates such objects
    /// for each standard DHCPv6 option.
    ///
    /// @todo list thrown exceptions.
    /// @todo extend this function to cover all standard options. Currently
    /// it is limited to critical options only.
    void initStdOptionDefs();

252
private:
Tomek Mrugalski's avatar
Tomek Mrugalski committed
253
    /// @brief Allocation Engine.
254
255
256
    /// Pointer to the allocation engine that we are currently using
    /// It must be a pointer, because we will support changing engines
    /// during normal operation (e.g. to use different allocators)
Stephen Morris's avatar
Stephen Morris committed
257
258
259
260
    boost::shared_ptr<AllocEngine> alloc_engine_;

    /// Server DUID (to be sent in server-identifier option)
    boost::shared_ptr<isc::dhcp::Option> serverid_;
261

Tomek Mrugalski's avatar
Tomek Mrugalski committed
262
    /// Indicates if shutdown is in progress. Setting it to true will
263
    /// initiate server shutdown procedure.
264
    volatile bool shutdown_;
265
266
267
};

}; // namespace isc::dhcp
268
}; // namespace isc
269
270

#endif // DHCP6_SRV_H