1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// Copyright (C) 2022-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

#include <config.h>

#include <dhcp/dhcp6.h>
#include <dhcp/libdhcp++.h>
#include <dhcp/pkt6.h>
#include <dhcp/option_custom.h>
#include <dhcp/option_int.h>
#include <dhcp/option6_addrlst.h>
#include <dhcp/option6_iaaddr.h>
#include <dhcp/option6_iaprefix.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/lease_mgr_factory.h>
#include <bulk_lease_query6.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <lease_query_impl_factory.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <lease_query_impl6.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <lease_query_log.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <blq_service.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stats/stats_mgr.h>
#include <util/encode/encode.h>

#include <boost/shared_ptr.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/pointer_cast.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc;
using namespace isc::asiolink;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::lease_query;
using namespace isc::log;
using namespace isc::stats;

std::string
BulkLeaseQuery6::leaseQueryLabel(const BlqMsgPtr& msg) {
    Pkt6Ptr pkt = boost::dynamic_pointer_cast<Pkt6>(msg->getPkt());
    if (!pkt) {
        return ("<none>");
    }
    return (LeaseQueryImpl6::leaseQueryLabel(pkt));
}

void
BulkLeaseQuery6::init() {
    // Take the page size from the MT LQ listener manager.
    auto service = BulkLeaseQueryService::instance();
    if (service) {
        page_size_ = service->getMaxLeasePerFetch();
    }

    query6_ = boost::dynamic_pointer_cast<Pkt6>(query_->getQuery());
    if (!query6_) {
        // Shouldn't happen
        isc_throw(BadValue, "BulkLeaseQuery6 has no DHCPv6 query");
    }

    // The query must have a client id option.
    DuidPtr req_clientid = query6_->getClientId();
    if (!req_clientid) {
        isc_throw(BadValue, "DHCPV6_LEASEQUERY must supply a D6O_CLIENTID");
    }

    // If the query has a server id option it should be for us.
    LeaseQueryImpl6::testServerId(query6_);

    // Get the lease query option.
    lq_option_ = boost::dynamic_pointer_cast<OptionCustom>
        (query6_->getOption(D6O_LQ_QUERY));
    if (!lq_option_) {
        isc_throw(BadValue, "DHCPV6_LEASEQUERY must supply a D6O_LQ_QUERY option");
    }

    // Extract the type and link fields.
    try {
        query_type_ = lq_option_->readInteger<uint8_t>(0);
        link_addr_ = lq_option_->readAddress(1);
    } catch (const std::exception& ex) {
        // unpack() should catch this?
        isc_throw(BadValue, "error reading query field(s): " << ex.what());
    }

    // Based on the query type, extract the requisite options.
    Option6StatusCodePtr status;
    OptionPtr opt;
    switch (query_type_) {
    case LQ6QT_BY_ADDRESS:
        opt = lq_option_->getOption(D6O_IAADDR);
        query_iaaddr_ = boost::dynamic_pointer_cast<Option6IAAddr>(opt);
        if (!query_iaaddr_) {
            status = LeaseQueryImpl6::makeStatusOption(STATUS_MalformedQuery,
                                                       "missing D6O_IAADDR");
            sendReply(status);
            return;
        }
        break;
    case LQ6QT_BY_CLIENTID:
        opt = lq_option_->getOption(D6O_CLIENTID);
        if (!opt) {
            status = LeaseQueryImpl6::makeStatusOption(STATUS_MalformedQuery,
                                                       "missing D6O_CLIENTID");
            sendReply(status);
            return;
        }
        try {
            query_client_id_.reset(new DUID(opt->getData()));
        } catch (const std::exception& ex) {
            status = LeaseQueryImpl6::makeStatusOption(STATUS_MalformedQuery,
                                                       "malformed D6O_CLIENTID");
            sendReply(status);
            return;
        }
        break;
    case LQ6QT_BY_RELAY_ID:
        opt = lq_option_->getOption(D6O_RELAY_ID);
        if (!opt) {
            status = LeaseQueryImpl6::makeStatusOption(STATUS_MalformedQuery,
                                                       "missing D6O_RELAY_ID");
            sendReply(status);
            return;
        }
        try {
            query_relay_id_.reset(new DUID(opt->getData()));
        } catch (const std::exception& ex) {
            status = LeaseQueryImpl6::makeStatusOption(STATUS_MalformedQuery,
                                                        "malformed D6O_RELAY_ID");
            sendReply(status);
            return;
        }
        break;
    case LQ6QT_BY_REMOTE_ID:
        query_remote_id_ = lq_option_->getOption(D6O_REMOTE_ID);
        if (!query_remote_id_) {
            status = LeaseQueryImpl6::makeStatusOption(STATUS_MalformedQuery,
                                                       "missing D6O_REMOTE_ID");
            sendReply(status);
            return;
        } else if (query_remote_id_->getData().empty()) {
            status = LeaseQueryImpl6::makeStatusOption(STATUS_MalformedQuery,
                                                       "empty D6O_REMOTE_ID");
            sendReply(status);
            return;
        }
        break;
    case LQ6QT_BY_LINK_ADDRESS:
        if (link_addr_.isV6Zero()) {
            status = LeaseQueryImpl6::makeStatusOption(STATUS_MalformedQuery,
                                                       "undefined link address");
            sendReply(status);
            return;
        }
        break;
    default:
        status = LeaseQueryImpl6::makeStatusOption(STATUS_UnknownQueryType,
                                                   "unknown query-type");
        sendReply(status);
        return;
    }
}

void
BulkLeaseQuery6::start() {
    if (started_) {
        isc_throw(InvalidOperation, "BulkLeaseQuery6 already in progress");
    }
    started_ = true;

    switch (query_type_) {
    case LQ6QT_BY_ADDRESS:
        bulkQueryByIpAddress();
        return;
    case LQ6QT_BY_CLIENTID:
        bulkQueryByClientId();
        return;
    case LQ6QT_BY_RELAY_ID:
        bulkQueryByRelayId();
        return;
    case LQ6QT_BY_REMOTE_ID:
        bulkQueryByRemoteId();
        return;
    case LQ6QT_BY_LINK_ADDRESS:
        bulkQueryByLinkAddress();
        return;
    default:
        isc_throw(InvalidOperation, "unknown query-type");
    }
}

void
BulkLeaseQuery6::bulkQueryByIpAddress() {
    if (!query_iaaddr_) {
        isc_throw(InvalidOperation, "no query ip address");
    }
    Lease6Collection leases;
    Option6StatusCodePtr status;

    const LeaseQueryImpl6& impl6 = dynamic_cast<const LeaseQueryImpl6&>(LeaseQueryImplFactory::getImpl());
    status = LeaseQueryImpl6::queryByIpAddress(query_iaaddr_->getAddress(),
                                               leases, impl6.getPrefixLengthList());
    if (leases.empty()) {
        // Error case.
        sendReply(status);
        return;
    }
    if (leases.size() > 1) {
        isc_throw(Unexpected, "more than one lease for an address");
    }

    // Construct and send the reply message.
    Pkt6Ptr reply = LeaseQueryImpl6::initReply(query6_);
    // Add the client option.
    reply->addOption(LeaseQueryImpl6::makeClientOption(leases.front()));
    // Add the status.
    reply->addOption(status);
    // Send the reply.
    send(reply);

    // Construct and send the done message.
    Pkt6Ptr done = LeaseQueryImpl6::initDone(query6_);
    send(done);

    // Done.
    setDone();
}

void
BulkLeaseQuery6::bulkQueryByClientId() {
    if (!query_client_id_) {
        isc_throw(InvalidOperation, "no query client id");
    }
    Lease6Collection leases;
    Option6StatusCodePtr status;
    status = LeaseQueryImpl6::queryByClientId(query_client_id_,
                                              link_addr_,
                                              leases);

    if (leases.empty()) {
        // Error case.
        sendReply(status);
        return;
    }

    bool first = true;
    for (auto const& lease : leases) {
        if (first) {
            first = false;
            // Create and send the reply message.
            Pkt6Ptr reply = LeaseQueryImpl6::initReply(query6_);
            // Add the client option.
            reply->addOption(LeaseQueryImpl6::makeClientOption(lease));
            // Add the status.
            reply->addOption(status);
            // Send the reply.
            send(reply);
            continue;
        }
        // Create and send the response.
        Pkt6Ptr response = LeaseQueryImpl6::initData(query6_);
        // Add the client option.
        response->addOption(LeaseQueryImpl6::makeClientOption(lease));
        // Send the response.
        send(response);
    }

    // Construct and send the done message.
    Pkt6Ptr done = LeaseQueryImpl6::initDone(query6_);
    send(done);

    // Done.
    setDone();
}

void
BulkLeaseQuery6::bulkQueryByRelayId() {
    if (!query_relay_id_) {
        isc_throw(InvalidOperation, "no query relay id");
    }
    Option6StatusCodePtr status;
    if (!LeaseMgrFactory::instance().getExtendedInfoTablesEnabled()) {
        status = LeaseQueryImpl6::makeStatusOption(STATUS_NotAllowed,
                                                   "extended info tables are disabled");
        sendReply(status);
        return;
    }

    Lease6Collection leases;
    status = LeaseQueryImpl6::queryByRelayIdStart(query_relay_id_,
                                                  start_addr_,
                                                  page_size_,
                                                  link_addr_,
                                                  links_,
                                                  leases);
    if (leases.empty()) {
        // Error case.
        sendReply(status);
        return;
    }

    bool first = true;
    for (auto const& lease : leases) {
        if (first) {
            first = false;
            // Create and send the reply message.
            Pkt6Ptr reply = LeaseQueryImpl6::initReply(query6_);
            // Add the client option.
            reply->addOption(LeaseQueryImpl6::makeClientOption(lease));
            // Add the status.
            reply->addOption(status);
            // Send the reply.
            send(reply);
            continue;
        }
        // Create and send the response.
        Pkt6Ptr response = LeaseQueryImpl6::initData(query6_);
        // Add the client option.
        response->addOption(LeaseQueryImpl6::makeClientOption(lease));
        // Send the response.
        send(response);
    }

    post_(std::bind(&BulkLeaseQuery6::doBulkQueryByRelayIdNext,
                    boost::static_pointer_cast<BulkLeaseQuery6>(shared_from_this())));
}

void
BulkLeaseQuery6::bulkQueryByRelayIdNext() {
    if (!query_relay_id_) {
        isc_throw(InvalidOperation, "no query relay id");
    }
    Lease6Collection leases;
    const IOAddress position = start_addr_;
    LeaseQueryImpl6::queryByRelayIdNext(query_relay_id_,
                                        start_addr_,
                                        page_size_,
                                        links_,
                                        leases);
    if (start_addr_ == position) {
        // Construct and send the done message.
        Pkt6Ptr done = LeaseQueryImpl6::initDone(query6_);
        send(done);

        // Done.
        setDone();

        return;
    }

    for (auto const& lease : leases) {
        // Create and send the response
        Pkt6Ptr response = LeaseQueryImpl6::initData(query6_);
        // Add the client option.
        response->addOption(LeaseQueryImpl6::makeClientOption(lease));
        // Send the response.
        send(response);
    }

    post_(std::bind(&BulkLeaseQuery6::doBulkQueryByRelayIdNext,
                    boost::static_pointer_cast<BulkLeaseQuery6>(shared_from_this())));
}

void
BulkLeaseQuery6::bulkQueryByRemoteId() {
    if (!query_remote_id_) {
        isc_throw(InvalidOperation, "no query remote id");
    }
    Option6StatusCodePtr status;
    if (!LeaseMgrFactory::instance().getExtendedInfoTablesEnabled()) {
        status = LeaseQueryImpl6::makeStatusOption(STATUS_NotAllowed,
                                                   "extended info tables are disabled");
        sendReply(status);
        return;
    }

    Lease6Collection leases;
    status = LeaseQueryImpl6::queryByRemoteIdStart(query_remote_id_->getData(),
                                                   start_addr_,
                                                   page_size_,
                                                   link_addr_,
                                                   links_,
                                                   leases);
    if (leases.empty()) {
        // Error case.
        sendReply(status);
        return;
    }

    bool first = true;
    for (auto const& lease : leases) {
        if (first) {
            first = false;
            // Create and send the reply message.
            Pkt6Ptr reply = LeaseQueryImpl6::initReply(query6_);
            // Add the client option.
            reply->addOption(LeaseQueryImpl6::makeClientOption(lease));
            // Add the status.
            reply->addOption(status);
            // Send the reply.
            send(reply);
            continue;
        }
        // Create and send the response.
        Pkt6Ptr response = LeaseQueryImpl6::initData(query6_);
        // Add the client option.
        response->addOption(LeaseQueryImpl6::makeClientOption(lease));
        // Send the response.
        send(response);
    }

    post_(std::bind(&BulkLeaseQuery6::doBulkQueryByRemoteIdNext,
                    boost::static_pointer_cast<BulkLeaseQuery6>(shared_from_this())));
}


void
BulkLeaseQuery6::bulkQueryByRemoteIdNext() {
    if (!query_remote_id_) {
        isc_throw(InvalidOperation, "no query remote id");
    }
    Lease6Collection leases;
    const IOAddress position = start_addr_;
    LeaseQueryImpl6::queryByRemoteIdNext(query_remote_id_->getData(),
                                         start_addr_,
                                         page_size_,
                                         links_,
                                         leases);
    if (start_addr_ == position) {
        // Construct and send the done message.
        Pkt6Ptr done = LeaseQueryImpl6::initDone(query6_);
        send(done);

        // Done.
        setDone();

        return;
    }

    for (auto const& lease : leases) {
        // Create and send the response
        Pkt6Ptr response = LeaseQueryImpl6::initData(query6_);
        // Add the client option.
        response->addOption(LeaseQueryImpl6::makeClientOption(lease));
        // Send the response.
        send(response);
    }

    post_(std::bind(&BulkLeaseQuery6::doBulkQueryByRemoteIdNext,
                    boost::static_pointer_cast<BulkLeaseQuery6>(shared_from_this())));
}

void
BulkLeaseQuery6::bulkQueryByLinkAddress() {
    // In fact by-link-address does not use extended info tables,
    // more it can be supported by SQL lease backends.
    Option6StatusCodePtr status;
    if (!LeaseMgrFactory::instance().getExtendedInfoTablesEnabled()) {
        status = LeaseQueryImpl6::makeStatusOption(STATUS_NotAllowed,
                                                   "extended info tables are disabled");
        sendReply(status);
        return;
    }

    Lease6Collection leases;
    status = LeaseQueryImpl6::queryByLinkStart(start_addr_,
                                               page_size_,
                                               link_addr_,
                                               links_,
                                               leases);
    if (leases.empty()) {
        // Error case.
        sendReply(status);
        return;
    }

    bool first = true;
    for (auto const& lease : leases) {
        if (first) {
            first = false;
            // Create and send the reply message.
            Pkt6Ptr reply = LeaseQueryImpl6::initReply(query6_);
            // Add the client option.
            reply->addOption(LeaseQueryImpl6::makeClientOption(lease));
            // Add the status.
            reply->addOption(status);
            // Send the reply.
            send(reply);
            continue;
        }
        // Create and send the response.
        Pkt6Ptr response = LeaseQueryImpl6::initData(query6_);
        // Add the client option.
        response->addOption(LeaseQueryImpl6::makeClientOption(lease));
        // Send the response.
        send(response);
    }

    post_(std::bind(&BulkLeaseQuery6::doBulkQueryByLinkAddressNext,
                    boost::static_pointer_cast<BulkLeaseQuery6>(shared_from_this())));
}

void
BulkLeaseQuery6::bulkQueryByLinkAddressNext() {
    Lease6Collection leases;
    const IOAddress position = start_addr_;
    LeaseQueryImpl6::queryByLinkNext(start_addr_,
                                     page_size_,
                                     links_,
                                     leases);
    if (start_addr_ == position) {
        // Construct and send the done message.
        Pkt6Ptr done = LeaseQueryImpl6::initDone(query6_);
        send(done);

        // Done.
        setDone();

        return;
    }

    for (auto const& lease : leases) {
        // Create and send the response
        Pkt6Ptr response = LeaseQueryImpl6::initData(query6_);
        // Add the client option.
        response->addOption(LeaseQueryImpl6::makeClientOption(lease));
        // Send the response.
        send(response);
    }

    post_(std::bind(&BulkLeaseQuery6::doBulkQueryByLinkAddressNext,
                    boost::static_pointer_cast<BulkLeaseQuery6>(shared_from_this())));
}

void
BulkLeaseQuery6::send(Pkt6Ptr response) const {
    CHECK_TERMINATED;
    BlqResponsePtr resp(new BlqResponse(response));
    if (!push_to_send_(resp)) {
        isc_throw(QueryTerminated, "connection closed");
    }
}

void
BulkLeaseQuery6::sendReply(OptionPtr status) {
    // Create and send reply message.
    Pkt6Ptr reply = LeaseQueryImpl6::initReply(query6_);
    // Add the status.
    reply->addOption(status);
    // Send the reply.
    send(reply);

    // It is final.
    setDone();
}