libdhcp++.cc 10.7 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
//
// 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.

15
16
#include <boost/shared_array.hpp>
#include <boost/shared_ptr.hpp>
17
#include <util/buffer.h>
18
#include <exceptions/exceptions.h>
19
#include <dhcp/libdhcp++.h>
20
#include "config.h"
21
#include <dhcp/dhcp4.h>
22
23
24
25
#include <dhcp/dhcp6.h>
#include <dhcp/option.h>
#include <dhcp/option6_ia.h>
#include <dhcp/option6_iaaddr.h>
26
#include <dhcp/option_definition.h>
27
#include <dhcp/option6_int_array.h>
28

29
using namespace std;
30
using namespace isc::dhcp;
31
using namespace isc::util;
32

33
34
35
// static array with factories for options
std::map<unsigned short, Option::Factory*> LibDHCP::v4factories_;

36
// static array with factories for options
37
38
std::map<unsigned short, Option::Factory*> LibDHCP::v6factories_;

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Static container with DHCPv4 option definitions.
OptionDefContainer LibDHCP::v4option_defs_;

// Static container with DHCPv6 option definitions.
OptionDefContainer LibDHCP::v6option_defs_;

const OptionDefContainer&
LibDHCP::getOptionDefs(Option::Universe u) {
    switch (u) {
    case Option::V4:
        return (v4option_defs_);
    case Option::V6:
        return (v6option_defs_);
    default:
        isc_throw(isc::BadValue, "invalid universe " << u << " specified");
    }
}
56

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
OptionPtr
LibDHCP::optionFactory(Option::Universe u,
                       uint16_t type,
                       const OptionBuffer& buf) {
    FactoryMap::iterator it;
    if (u == Option::V4) {
        it = v4factories_.find(type);
        if (it == v4factories_.end()) {
            isc_throw(BadValue, "factory function not registered "
            "for DHCP v4 option type " << type);
        }
    } else if (u == Option::V6) {
        it = v6factories_.find(type);
        if (it == v6factories_.end()) {
            isc_throw(BadValue, "factory function not registered "
                      "for DHCPv6 option type " << type);
        }
    } else {
        isc_throw(BadValue, "invalid universe specified (expected "
                  "Option::V4 or Option::V6");
    }
78
    return (it->second(u, type, buf));
79
80
81
}


Tomek Mrugalski's avatar
Tomek Mrugalski committed
82
83
84
85
size_t LibDHCP::unpackOptions6(const OptionBuffer& buf,
                               isc::dhcp::Option::OptionCollection& options) {
    size_t offset = 0;
    size_t end = buf.size();
86

87
    while (offset +4 <= end) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
88
        uint16_t opt_type = buf[offset] * 256 + buf[offset + 1];
89
        offset += 2;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
90
        uint16_t opt_len = buf[offset] * 256 + buf[offset + 1];
91
92
        offset += 2;

93
        if (offset + opt_len > end) {
94
            // @todo: consider throwing exception here.
95
96
            return (offset);
        }
97
        OptionPtr opt;
98
99
100
        switch (opt_type) {
        case D6O_IA_NA:
        case D6O_IA_PD:
101
102
103
            opt = OptionPtr(new Option6IA(opt_type,
                                          buf.begin() + offset,
                                          buf.begin() + offset + opt_len));
104
105
            break;
        case D6O_IAADDR:
106
107
108
            opt = OptionPtr(new Option6IAAddr(opt_type,
                                              buf.begin() + offset,
                                              buf.begin() + offset + opt_len));
109
            break;
110
111
112
113
114
        case D6O_ORO:
            opt = OptionPtr(new Option6IntArray<uint16_t>(opt_type,
                                                          buf.begin() + offset,
                                                          buf.begin() + offset + opt_len));
            break;
115
        default:
116
117
118
119
            opt = OptionPtr(new Option(Option::V6,
                                       opt_type,
                                       buf.begin() + offset,
                                       buf.begin() + offset + opt_len));
120
121
            break;
        }
122
        // add option to options
123
        options.insert(std::make_pair(opt_type, opt));
124
125
126
127
128
129
        offset += opt_len;
    }

    return (offset);
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
130
size_t LibDHCP::unpackOptions4(const OptionBuffer& buf,
131
                                 isc::dhcp::Option::OptionCollection& options) {
132
133
    size_t offset = 0;

Tomek Mrugalski's avatar
Tomek Mrugalski committed
134
    // 2 byte - header of DHCPv4 option
135
    while (offset + 1 <= buf.size()) {
136
        uint8_t opt_type = buf[offset++];
137

Tomek Mrugalski's avatar
Tomek Mrugalski committed
138
        // DHO_END is a special, one octet long option
139
        if (opt_type == DHO_END)
140
            return (offset); // just return. Don't need to add DHO_END option
141

Tomek Mrugalski's avatar
Tomek Mrugalski committed
142
143
144
145
146
        // DHO_PAD is just a padding after DHO_END. Let's continue parsing
        // in case we receive a message without DHO_END.
        if (opt_type == DHO_PAD)
            continue;

147
        if (offset + 1 >= buf.size()) {
148
149
            isc_throw(OutOfRange, "Attempt to parse truncated option "
                      << opt_type);
150
151
        }

152
        uint8_t opt_len =  buf[offset++];
153
        if (offset + opt_len > buf.size()) {
154
155
156
157
158
            isc_throw(OutOfRange, "Option parse failed. Tried to parse "
                      << offset + opt_len << " bytes from " << buf.size()
                      << "-byte long buffer.");
        }

159
        OptionPtr opt;
160
161
        switch(opt_type) {
        default:
162
163
164
            opt = OptionPtr(new Option(Option::V4, opt_type,
                                       buf.begin()+offset,
                                       buf.begin()+offset+opt_len));
165
166
        }

167
        options.insert(std::make_pair(opt_type, opt));
168
169
        offset += opt_len;
    }
170
    return (offset);
171
172
}

173
174
void LibDHCP::packOptions6(isc::util::OutputBuffer &buf,
                           const isc::dhcp::Option::OptionCollection& options) {
175
176
177
    for (Option::OptionCollection::const_iterator it = options.begin();
         it != options.end(); ++it) {
        it->second->pack(buf);
178
    }
179
}
180

181
182
void
LibDHCP::packOptions(isc::util::OutputBuffer& buf,
183
184
                     const Option::OptionCollection& options) {
    for (Option::OptionCollection::const_iterator it = options.begin();
185
         it != options.end(); ++it) {
186
187
188
189
        it->second->pack4(buf);
    }
}

190
191
void LibDHCP::OptionFactoryRegister(Option::Universe u,
                                    uint16_t opt_type,
192
                                    Option::Factory* factory) {
193
194
    switch (u) {
    case Option::V6: {
195
        if (v6factories_.find(opt_type) != v6factories_.end()) {
196
197
198
199
            isc_throw(BadValue, "There is already DHCPv6 factory registered "
                     << "for option type "  << opt_type);
        }
        v6factories_[opt_type]=factory;
200
        return;
201
202
    }
    case Option::V4:
203
    {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
204
205
        // Option 0 is special (a one octet-long, equal 0) PAD option. It is never
        // instantiated as an Option object, but rather consumed during packet parsing.
206
207
208
        if (opt_type == 0) {
            isc_throw(BadValue, "Cannot redefine PAD option (code=0)");
        }
Tomek Mrugalski's avatar
Tomek Mrugalski committed
209
        // Option 255 is never instantiated as an option object. It is special
210
211
        // (a one-octet equal 255) option that is added at the end of all options
        // during packet assembly. It is also silently consumed during packet parsing.
212
213
214
215
216
217
218
219
220
        if (opt_type > 254) {
            isc_throw(BadValue, "Too big option type for DHCPv4, only 0-254 allowed.");
        }
        if (v4factories_.find(opt_type)!=v4factories_.end()) {
            isc_throw(BadValue, "There is already DHCPv4 factory registered "
                     << "for option type "  << opt_type);
        }
        v4factories_[opt_type]=factory;
        return;
221
    }
222
223
    default:
        isc_throw(BadValue, "Invalid universe type specified.");
224
225
    }

226
    return;
227
}
228
229

void
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
LibDHCP::initStdOptionDefs(Option::Universe u) {
    switch (u) {
    case Option::V4:
        initStdOptionDefs4();
        break;
    case Option::V6:
        initStdOptionDefs6();
        break;
    default:
        isc_throw(isc::BadValue, "invalid universe " << u << " specified");
    }
}

void
LibDHCP::initStdOptionDefs4() {
    isc_throw(isc::NotImplemented, "initStdOptionDefs4 is not implemented");
}

void
LibDHCP::initStdOptionDefs6() {
    v6option_defs_.clear();
251
252
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

    struct OptionParams {
        std::string name;
        uint16_t code;
        OptionDefinition::DataType type;
        bool array;
    };
    OptionParams params[] = {
        { "CLIENTID", D6O_CLIENTID, OptionDefinition::BINARY_TYPE, false },
        { "SERVERID", D6O_SERVERID, OptionDefinition::BINARY_TYPE, false },
        { "IA_NA", D6O_IA_NA, OptionDefinition::RECORD_TYPE, false },
        { "IAADDR", D6O_IAADDR, OptionDefinition::RECORD_TYPE, false },
        { "ORO", D6O_ORO, OptionDefinition::UINT16_TYPE, true },
        { "ELAPSED_TIME", D6O_ELAPSED_TIME, OptionDefinition::UINT16_TYPE, false },
        { "STATUS_CODE", D6O_STATUS_CODE, OptionDefinition::RECORD_TYPE, false },
        { "RAPID_COMMIT", D6O_RAPID_COMMIT, OptionDefinition::EMPTY_TYPE, false },
        { "DNS_SERVERS", D6O_NAME_SERVERS, OptionDefinition::IPV6_ADDRESS_TYPE, true }
    };
    const int params_size = sizeof(params) / sizeof(params[0]);

    for (int i = 0; i < params_size; ++i) {
        OptionDefinitionPtr definition(new OptionDefinition(params[i].name,
                                                            params[i].code,
                                                            params[i].type,
                                                            params[i].array));
        switch(params[i].code) {
        case D6O_IA_NA:
            for (int j = 0; j < 3; ++j) {
                definition->addRecordField(OptionDefinition::UINT32_TYPE);
            }
            break;
        case D6O_IAADDR:
            definition->addRecordField(OptionDefinition::IPV6_ADDRESS_TYPE);
            definition->addRecordField(OptionDefinition::UINT32_TYPE);
            definition->addRecordField(OptionDefinition::UINT32_TYPE);
            break;
        case D6O_STATUS_CODE:
288
            definition->addRecordField(OptionDefinition::UINT16_TYPE);
289
290
291
292
            definition->addRecordField(OptionDefinition::STRING_TYPE);
        default:
            break;
        }
293
294
295
296
297
298
299
300
        try {
            definition->validate();
        } catch (const Exception& ex) {
            isc_throw(isc::Unexpected, "internal server error: invalid definition of standard"
                      << " DHCPv6 option (with code " << params[i].code << "): "
                      << ex.what());
        }
        v6option_defs_.push_back(definition);
301
302
    }
}