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
// Copyright (C) 2020-2023 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <dhcp4/client_handler.h>
#include <dhcp4/dhcp4_log.h>
#include <exceptions/exceptions.h>
#include <stats/stats_mgr.h>
#include <util/multi_threading_mgr.h>

using namespace std;
using namespace isc::util;
using namespace isc::log;

namespace isc {
namespace dhcp {

ClientHandler::Client::Client(Pkt4Ptr query, ClientIdPtr client_id,
                              HWAddrPtr hwaddr)
    : query_(query), htype_(HTYPE_ETHER), thread_(this_thread::get_id()) {
    // Sanity checks.
    if (!query) {
        isc_throw(InvalidParameter, "null query in ClientHandler");
    }
    if (!client_id && (!hwaddr || hwaddr->hwaddr_.empty())) {
        isc_throw(InvalidParameter,
                  "null client-id and hwaddr in ClientHandler");
    }

    if (client_id) {
        client_id_ = client_id->getClientId();
    }
    if (hwaddr && !hwaddr->hwaddr_.empty()) {
        htype_ = hwaddr->htype_;
        hwaddr_ = hwaddr->hwaddr_;
    }
}

mutex ClientHandler::mutex_;

ClientHandler::ClientByIdContainer ClientHandler::clients_client_id_;

ClientHandler::ClientByHWAddrContainer ClientHandler::clients_hwaddr_;

ClientHandler::ClientPtr
ClientHandler::lookup(const ClientIdPtr& client_id) {
    // Sanity check.
    if (!client_id) {
        isc_throw(InvalidParameter, "null client-id in ClientHandler::lookup");
    }

    auto it = clients_client_id_.find(client_id->getClientId());
    if (it == clients_client_id_.end()) {
        return (ClientPtr());
    }
    return (*it);
}

ClientHandler::ClientPtr
ClientHandler::lookup(const HWAddrPtr& hwaddr) {
    // Sanity checks.
    if (!hwaddr) {
        isc_throw(InvalidParameter, "null hwaddr in ClientHandler::lookup");
    }
    if (hwaddr->hwaddr_.empty()) {
        isc_throw(InvalidParameter, "empty hwaddr in ClientHandler::lookup");
    }

    auto key = boost::make_tuple(hwaddr->htype_, hwaddr->hwaddr_);
    auto it = clients_hwaddr_.find(key);
    if (it == clients_hwaddr_.end()) {
        return (ClientPtr());
    }
    return (*it);
}

void
ClientHandler::addById(const ClientPtr& client) {
    // Sanity check.
    if (!client) {
        isc_throw(InvalidParameter, "null client in ClientHandler::addById");
    }

    // Assume insert will never fail so not checking its result.
    clients_client_id_.insert(client);
}

void
ClientHandler::addByHWAddr(const ClientPtr& client) {
    // Sanity check.
    if (!client) {
        isc_throw(InvalidParameter,
                  "null client in ClientHandler::addByHWAddr");
    }

    // Assume insert will never fail so not checking its result.
    clients_hwaddr_.insert(client);
}

void
ClientHandler::del(const ClientIdPtr& client_id) {
    // Sanity check.
    if (!client_id) {
        isc_throw(InvalidParameter, "null duid in ClientHandler::del");
    }

    // Assume erase will never fail so not checking its result.
    clients_client_id_.erase(client_id->getClientId());
}

void
ClientHandler::del(const HWAddrPtr& hwaddr) {
    // Sanity checks.
    if (!hwaddr) {
        isc_throw(InvalidParameter, "null hwaddr in ClientHandler::del");
    }
    if (hwaddr->hwaddr_.empty()) {
        isc_throw(InvalidParameter, "empty hwaddr in ClientHandler::del");
    }

    auto key = boost::make_tuple(hwaddr->htype_, hwaddr->hwaddr_);
    // Assume erase will never fail so not checking its result.
    auto it = clients_hwaddr_.find(key);
    if (it == clients_hwaddr_.end()) {
        // Should not happen.
        return;
    }
    clients_hwaddr_.erase(it);
}

ClientHandler::ClientHandler()
    : client_(), locked_client_id_(), locked_hwaddr_() {
}

ClientHandler::~ClientHandler() {
    bool unlocked = false;
    lock_guard<mutex> lk(mutex_);
    if (locked_client_id_) {
        unlocked = true;
        unLockById();<--- Exception thrown in function declared not to throw exceptions.
    }
    if (locked_hwaddr_) {
        unlocked = true;
        unLockByHWAddr();
    }
    if (!unlocked || !client_ || !client_->cont_) {
        return;
    }
    // Try to process next query. As the caller holds the mutex of
    // the handler class the continuation will be resumed after.
    MultiThreadingMgr& mt_mgr = MultiThreadingMgr::instance();
    if (mt_mgr.getMode()) {
        if (!mt_mgr.getThreadPool().addFront(client_->cont_)) {
            LOG_DEBUG(dhcp4_logger, DBG_DHCP4_BASIC, DHCP4_PACKET_QUEUE_FULL);
        }
    }
}

bool
ClientHandler::tryLock(Pkt4Ptr query, ContinuationPtr cont) {
    // Sanity checks.
    if (!query) {
        isc_throw(InvalidParameter, "null query in ClientHandler::tryLock");
    }
    if (locked_client_id_) {
        isc_throw(Unexpected,
                  "already handling client-id in ClientHandler::tryLock");
    }
    if (locked_hwaddr_) {
        isc_throw(Unexpected,
                  "already handling hwaddr in ClientHandler::tryLock");
    }

    // Get identifiers.
    OptionPtr opt_client_id = query->getOption(DHO_DHCP_CLIENT_IDENTIFIER);
    ClientIdPtr client_id;
    if (opt_client_id) {
        client_id.reset(new ClientId(opt_client_id->getData()));
    }
    HWAddrPtr hwaddr = query->getHWAddr();
    if (hwaddr && hwaddr->hwaddr_.empty()) {
        hwaddr.reset();
    }
    if (!client_id && !hwaddr) {
        // Can't do something useful: cross fingers.
        return (true);
    }

    ClientPtr holder_id;
    ClientPtr holder_hw;
    Pkt4Ptr next_query_id;
    Pkt4Ptr next_query_hw;
    client_.reset(new Client(query, client_id, hwaddr));

    {
        lock_guard<mutex> lk(mutex_);
        // Try first duid.
        if (client_id) {
            // Try to acquire the by-client-id lock and return the holder
            // when it failed.
            holder_id = lookup(client_id);
            if (!holder_id) {
                locked_client_id_ = client_id;
                lockById();
            } else if (cont) {
                next_query_id = holder_id->next_query_;
                holder_id->next_query_ = query;
                holder_id->cont_ = cont;
            }
        }
        if (!holder_id) {
            if (!hwaddr) {
                return (true);
            }
            // Try to acquire the by-hw-addr lock and return the holder
            // when it failed.
            holder_hw = lookup(hwaddr);
            if (!holder_hw) {
                locked_hwaddr_ = hwaddr;
                lockByHWAddr();
                return (true);
            } else if (cont) {
                next_query_hw = holder_hw->next_query_;
                holder_hw->next_query_ = query;
                holder_hw->cont_ = cont;
            }
        }
    }

    if (holder_id) {
        // This query is a by-id duplicate so put the continuation.
        if (cont) {
            if (next_query_id) {
                // Logging a warning as it is supposed to be a rare event
                // with well behaving clients...
                LOG_DEBUG(bad_packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0011)
                    .arg(next_query_id->getHWAddrLabel())
                    .arg(next_query_id->toText())
                    .arg(this_thread::get_id())
                    .arg(holder_id->query_->getHWAddrLabel())
                    .arg(holder_id->query_->toText())
                    .arg(holder_id->thread_);
                stats::StatsMgr::instance().addValue("pkt4-receive-drop",
                                                     static_cast<int64_t>(1));
            }
        } else {
            // Logging a warning as it is supposed to be a rare event
            // with well behaving clients...
            LOG_DEBUG(bad_packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0011)
                .arg(query->getHWAddrLabel())
                .arg(query->toText())
                .arg(this_thread::get_id())
                .arg(holder_id->query_->getHWAddrLabel())
                .arg(holder_id->query_->toText())
                .arg(holder_id->thread_);
            stats::StatsMgr::instance().addValue("pkt4-receive-drop",
                                                 static_cast<int64_t>(1));
        }
    } else {
        // This query is a by-hw duplicate so put the continuation.
        if (cont) {
            if (next_query_hw) {
                // Logging a warning as it is supposed to be a rare event
                // with well behaving clients...
                LOG_DEBUG(bad_packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0012)
                    .arg(next_query_hw->getHWAddrLabel())
                    .arg(next_query_hw->toText())
                    .arg(this_thread::get_id())
                    .arg(holder_hw->query_->getHWAddrLabel())
                    .arg(holder_hw->query_->toText())
                    .arg(holder_hw->thread_);
                stats::StatsMgr::instance().addValue("pkt4-receive-drop",
                                                     static_cast<int64_t>(1));
            }
        } else {
            // Logging a warning as it is supposed to be a rare event
            // with well behaving clients...
            LOG_DEBUG(bad_packet4_logger, DBGLVL_PKT_HANDLING, DHCP4_PACKET_DROP_0012)
                .arg(query->getHWAddrLabel())
                .arg(query->toText())
                .arg(this_thread::get_id())
                .arg(holder_hw->query_->getHWAddrLabel())
                .arg(holder_hw->query_->toText())
                .arg(holder_hw->thread_);
            stats::StatsMgr::instance().addValue("pkt4-receive-drop",
                                                 static_cast<int64_t>(1));
        }
    }
    return (false);
}

void
ClientHandler::lockById() {
    // Sanity check.
    if (!locked_client_id_) {
        isc_throw(Unexpected, "nothing to lock in ClientHandler::lockById");
    }

    addById(client_);
}

void
ClientHandler::lockByHWAddr() {
    // Sanity check.
    if (!locked_hwaddr_) {
        isc_throw(Unexpected,
                  "nothing to lock in ClientHandler::lockByHWAddr");
    }

    addByHWAddr(client_);
}

void
ClientHandler::unLockById() {
    // Sanity check.
    if (!locked_client_id_) {
        isc_throw(Unexpected,
                  "nothing to unlock in ClientHandler::unLockById");
    }

    del(locked_client_id_);
    locked_client_id_.reset();
}

void
ClientHandler::unLockByHWAddr() {
    // Sanity check.
    if (!locked_hwaddr_) {
        isc_throw(Unexpected,
                  "nothing to unlock in ClientHandler::unLockByHWAddr");
    }

    del(locked_hwaddr_);
    locked_hwaddr_.reset();
}

}  // namespace dhcp
}  // namespace isc