callouts.cc 16 KB
Newer Older
Baptiste Jonglez's avatar
Baptiste Jonglez committed
1
2
3
4
5
6
7
/* Copyright (c) 2017-2019 by Baptiste Jonglez
 *
 * 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 https://mozilla.org/MPL/2.0/.
 */

Baptiste Jonglez's avatar
Baptiste Jonglez committed
8
9
10
11
#include <hooks/hooks.h>
#include <dhcp/pkt4.h>
#include <dhcp/dhcp6.h>
#include <dhcp/pkt6.h>
12
#include <dhcp/option6_ia.h>
13
14
#include <dhcpsrv/subnet.h>
#include <dhcpsrv/lease.h>
Baptiste Jonglez's avatar
Baptiste Jonglez committed
15

16
17
18
#include <string>
#include <vector>

Baptiste Jonglez's avatar
Baptiste Jonglez committed
19
20
21
22
23
24
25
#include "runscript.h"

using namespace isc::dhcp;
using namespace isc::hooks;

extern "C" {

26
27
/* These are helpers that extract relevant information from Kea data
 * structures and store them in environment variables. */
28
29
30
31
32
void extract_bool(std::vector<std::string>& env, const std::string variable, bool value)
{
    env.push_back(variable + "=" + std::string(value ? "1" : "0"));
}

33
34
35
/* Extract information from a DHCPv4 packet (query received, or response
 * about to be sent) */
void extract_pkt4(std::vector<std::string>& env, const std::string envprefix, const Pkt4Ptr pkt4)
36
{
37
    /* General information */
38
39
    env.push_back(envprefix + "TYPE=" + std::string(pkt4->getName()));
    env.push_back(envprefix + "INTERFACE=" + pkt4->getIface());
40
    env.push_back(envprefix + "IFINDEX=" + std::to_string(pkt4->getIndex()));
41
    /* Hardware address */
42
    HWAddrPtr hwaddr = pkt4->getHWAddr();
43
    if (hwaddr) {
44
45
46
        env.push_back(envprefix + "HWADDR=" + hwaddr->toText(false));
        env.push_back(envprefix + "HWADDR_TYPE=" + std::to_string(hwaddr->htype_));
        env.push_back(envprefix + "HWADDR_SOURCE=" + std::to_string(hwaddr->source_));
47
    } else {
48
49
50
        env.push_back(envprefix + "HWADDR=");
        env.push_back(envprefix + "HWADDR_TYPE=");
        env.push_back(envprefix + "HWADDR_SOURCE=");
51
    }
52
    /* Misc */
53
54
55
56
57
58
59
    env.push_back(envprefix + "CIADDR=" + pkt4->getCiaddr().toText());
    env.push_back(envprefix + "SIADDR=" + pkt4->getSiaddr().toText());
    env.push_back(envprefix + "YIADDR=" + pkt4->getYiaddr().toText());
    env.push_back(envprefix + "GIADDR=" + pkt4->getGiaddr().toText());
    env.push_back(envprefix + "RELAYED=" + std::to_string(pkt4->isRelayed()));
    env.push_back(envprefix + "RELAY_HOPS=" + std::to_string(pkt4->getHops()));

Jeff Groom's avatar
Jeff Groom committed
60
61
62
63
64
    /* Specific Options */
    OptionPtr option60 = pkt4->getOption(60);
    if (option60) {
        env.push_back(envprefix + "OPTION60=" + option60->toString());
    }
Joost Bekkers's avatar
Joost Bekkers committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

    OptionPtr rai = pkt4->getOption(82);
    if (rai) {
        env.push_back(envprefix + "RAI=" + rai->toHexString());

        OptionPtr circuit_id = rai->getOption(RAI_OPTION_AGENT_CIRCUIT_ID);
        if (circuit_id) {
            env.push_back(envprefix + "RAI_CIRCUIT_ID=" + circuit_id->toHexString());
        }

        OptionPtr remote_id = rai->getOption(RAI_OPTION_REMOTE_ID);
        if (remote_id) {
            env.push_back(envprefix + "RAI_REMOTE_ID=" + remote_id->toHexString());
        }
    }
80
81
}

82
83
84
85
86
87
88
89
90
91
92
93
94
void extract_query4(std::vector<std::string>& env, const Pkt4Ptr query)
{
    extract_pkt4(env, "KEA_QUERY4_", query);
}

void extract_response4(std::vector<std::string>& env, const Pkt4Ptr response)
{
    extract_pkt4(env, "KEA_RESPONSE4_", response);
}

/* Extract information from a DHCPv6 packet (query received, or response
 * about to be sent) */
void extract_pkt6(std::vector<std::string>& env, const std::string envprefix, const Pkt6Ptr pkt6)
95
96
{
    /* General information */
97
98
99
100
    env.push_back(envprefix + "TYPE=" + std::string(pkt6->getName()));
    env.push_back(envprefix + "INTERFACE=" + pkt6->getIface());
    env.push_back(envprefix + "IFINDEX=" + std::to_string(pkt6->getIndex()));
    HWAddrPtr hwaddr = pkt6->getMAC(HWAddr::HWADDR_SOURCE_ANY);
101
    if (hwaddr) {
102
103
104
        env.push_back(envprefix + "HWADDR=" + hwaddr->toText(false));
        env.push_back(envprefix + "HWADDR_TYPE=" + std::to_string(hwaddr->htype_));
        env.push_back(envprefix + "HWADDR_SOURCE=" + std::to_string(hwaddr->source_));
105
    } else {
106
107
108
        env.push_back(envprefix + "HWADDR=");
        env.push_back(envprefix + "HWADDR_TYPE=");
        env.push_back(envprefix + "HWADDR_SOURCE=");
109
    }
110
111
112
113
114
115
    env.push_back(envprefix + "LOCAL_ADDRESS=" + pkt6->getLocalAddr().toText());
    env.push_back(envprefix + "LOCAL_PORT=" + std::to_string(pkt6->getLocalPort()));
    env.push_back(envprefix + "REMOTE_ADDRESS=" + pkt6->getRemoteAddr().toText());
    env.push_back(envprefix + "REMOTE_PORT=" + std::to_string(pkt6->getRemotePort()));
    env.push_back(envprefix + "LABEL=" + pkt6->getLabel());
    env.push_back(envprefix + "TRANSACTION_ID=" + std::to_string(pkt6->getTransid()));
116
    /* TODO */
117
    env.push_back(envprefix + "DUID=");
118
119
    /* TODO: all options?  Only common ones?  Which format? */
    /* TODO */
120
    env.push_back(envprefix + "DEBUG=" + pkt6->toText());
121
122
}

123
void extract_query6(std::vector<std::string>& env, const Pkt6Ptr query)
124
{
125
    extract_pkt6(env, "KEA_QUERY6_", query);
126
127
}

128

129
130
void extract_response6(std::vector<std::string>& env, const Pkt6Ptr response)
{
131
    extract_pkt6(env, "KEA_RESPONSE6_", response);
132
133
}

134
135
void extract_subnet4(std::vector<std::string>& env, const Subnet4Ptr subnet)
{
136
137
138
139
140
141
142
143
144
145
146
147
    /* The subnet given by Kea might be NULL, this seems to happen when
     * Kea fails to find a matching subnet for a client request. */
    if (subnet != NULL) {
        env.push_back("KEA_SUBNET4=" + subnet->toText());
        std::pair<isc::asiolink::IOAddress, uint8_t> prefix = subnet->get();
        env.push_back("KEA_SUBNET4_PREFIX=" + prefix.first.toText());
        env.push_back("KEA_SUBNET4_PREFIXLEN=" + std::to_string(prefix.second));
    } else {
        env.push_back("KEA_SUBNET4=");
        env.push_back("KEA_SUBNET4_PREFIX=");
        env.push_back("KEA_SUBNET4_PREFIXLEN=");
    }
148
149
}

150
151
void extract_subnet6(std::vector<std::string>& env, const Subnet6Ptr subnet)
{
152
153
154
155
156
157
158
159
160
161
    if (subnet != NULL) {
        env.push_back("KEA_SUBNET6=" + subnet->toText());
        std::pair<isc::asiolink::IOAddress, uint8_t> prefix = subnet->get();
        env.push_back("KEA_SUBNET6_PREFIX=" + prefix.first.toText());
        env.push_back("KEA_SUBNET6_PREFIXLEN=" + std::to_string(prefix.second));
    } else {
        env.push_back("KEA_SUBNET6=");
        env.push_back("KEA_SUBNET6_PREFIX=");
        env.push_back("KEA_SUBNET6_PREFIXLEN=");
    }
162
163
}

164
165
void extract_lease4(std::vector<std::string>& env, const Lease4Ptr lease)
{
Baptiste Jonglez's avatar
Baptiste Jonglez committed
166
167
168
    env.push_back("KEA_LEASE4_TYPE=V4");
    env.push_back("KEA_LEASE4_STATE=" + lease->basicStatesToText(lease->state_));
    extract_bool(env, "KEA_LEASE4_IS_EXPIRED", lease->expired());
169
    env.push_back("KEA_LEASE4_ADDRESS=" + lease->addr_.toText());
170
171
172
173
174
    if (lease->hwaddr_) {
        env.push_back("KEA_LEASE4_HWADDR=" + lease->hwaddr_->toText(false));
    } else {
        env.push_back("KEA_LEASE4_HWADDR=");
    }
Baptiste Jonglez's avatar
Baptiste Jonglez committed
175
176
177
    env.push_back("KEA_LEASE4_HOSTNAME=" + lease->hostname_);
    env.push_back("KEA_LEASE4_CLIENT_LAST_TRANSMISSION=" + std::to_string(lease->cltt_));
    env.push_back("KEA_LEASE4_VALID_LIFETIME=" + std::to_string(lease->valid_lft_));
178
    env.push_back("KEA_LEASE4_DEBUG=" + lease->toText());
179
180
}

181
182
void extract_lease6(std::vector<std::string>& env, const Lease6Ptr lease)
{
183
184
185
    env.push_back("KEA_LEASE6_TYPE=" + lease->typeToText(lease->type_));
    env.push_back("KEA_LEASE6_STATE=" + lease->basicStatesToText(lease->state_));
    extract_bool(env, "KEA_LEASE6_IS_EXPIRED", lease->expired());
186
    env.push_back("KEA_LEASE6_ADDRESS=" + lease->addr_.toText());
187
188
189
190
    if (lease->type_ == Lease::TYPE_PD) {
        env.push_back("KEA_LEASE6_DELEGATED_PREFIX=" + lease->addr_.toText() + "/" + std::to_string(lease->prefixlen_));
        env.push_back("KEA_LEASE6_DELEGATED_PREFIXLEN=" + std::to_string(lease->prefixlen_));
    }
191
192
193
194
195
    if (lease->hwaddr_) {
        env.push_back("KEA_LEASE6_HWADDR=" + lease->hwaddr_->toText(false));
    } else {
        env.push_back("KEA_LEASE6_HWADDR=");
    }
196
197
198
199
200
201
    env.push_back("KEA_LEASE6_HOSTNAME=" + lease->hostname_);
    env.push_back("KEA_LEASE6_CLIENT_DUID=" + lease->duid_->toText());
    env.push_back("KEA_LEASE6_CLIENT_LAST_TRANSMISSION=" + std::to_string(lease->cltt_));
    env.push_back("KEA_LEASE6_VALID_LIFETIME=" + std::to_string(lease->valid_lft_));
    env.push_back("KEA_LEASE6_PREFERRED_LIFETIME=" + std::to_string(lease->preferred_lft_));
    env.push_back("KEA_LEASE6_IAID=" + std::to_string(lease->iaid_));
202
    env.push_back("KEA_LEASE6_DEBUG=" + lease->toText());
203
204
}

205
206
207
208
209
210
/* IPv4 callouts */
int pkt4_receive(CalloutHandle& handle) {
    std::vector<std::string> env;
    Pkt4Ptr query;
    handle.getArgument("query4", query);
    extract_query4(env, query);
211
212
    /* Run script */
    int ret;
213
    ret = run_script("pkt4_receive", env);
Baptiste Jonglez's avatar
Baptiste Jonglez committed
214
215
216
    return 0;
}

217
218
219
220
221
222
223
224
225
226
227
228
229
230
int pkt4_send(CalloutHandle& handle) {
    std::vector<std::string> env;
    Pkt4Ptr response;
    Pkt4Ptr query;
    handle.getArgument("response4", response);
    extract_response4(env, response);
    handle.getArgument("query4", query);
    extract_query4(env, query);
    /* Run script */
    int ret;
    ret = run_script("pkt4_send", env);
    return 0;
}

231
232
233
234
235
236
237
238
239
240
241
242
243
244
int subnet4_select(CalloutHandle& handle) {
    std::vector<std::string> env;
    Pkt4Ptr query;
    Subnet4Ptr subnet;
    handle.getArgument("query4", query);
    extract_query4(env, query);
    handle.getArgument("subnet4", subnet);
    extract_subnet4(env, subnet);
    /* Run script */
    int ret;
    ret = run_script("subnet4_select", env);
    return 0;
}

245
246
247
248
249
250
251
int lease4_select(CalloutHandle& handle) {
    std::vector<std::string> env;
    Pkt4Ptr query;
    Subnet4Ptr subnet;
    bool fake_allocation;
    Lease4Ptr lease;
    handle.getArgument("query4", query);
252
    extract_query4(env, query);
253
    handle.getArgument("subnet4", subnet);
254
    extract_subnet4(env, subnet);
255
    handle.getArgument("fake_allocation", fake_allocation);
256
    extract_bool(env, "KEA_FAKE_ALLOCATION", fake_allocation);
257
    handle.getArgument("lease4", lease);
258
    extract_lease4(env, lease);
259
260
261
262
263
264
    /* Run script */
    int ret;
    ret = run_script("lease4_select", env);
    return 0;
}

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
int lease4_renew(CalloutHandle& handle) {
    std::vector<std::string> env;
    Pkt4Ptr query;
    Subnet4Ptr subnet;
    Lease4Ptr lease;
    handle.getArgument("query4", query);
    extract_query4(env, query);
    handle.getArgument("subnet4", subnet);
    extract_subnet4(env, subnet);
    /* TODO: what is this?  Is it different from what is in the query? */
    //handle.getArgument("clientid", XX);
    //handle.getArgument("hwaddr", XX);
    handle.getArgument("lease4", lease);
    extract_lease4(env, lease);
    /* Run script */
    int ret;
    ret = run_script("lease4_renew", env);
    return 0;
}

int lease4_release(CalloutHandle& handle) {
    std::vector<std::string> env;
    Pkt4Ptr query;
    Lease4Ptr lease;
    handle.getArgument("query4", query);
    extract_query4(env, query);
    handle.getArgument("lease4", lease);
    extract_lease4(env, lease);
    /* Run script */
    int ret;
    ret = run_script("lease4_release", env);
    return 0;
}

int lease4_decline(CalloutHandle& handle) {
    std::vector<std::string> env;
    Pkt4Ptr query;
    Lease4Ptr lease;
    handle.getArgument("query4", query);
    extract_query4(env, query);
    handle.getArgument("lease4", lease);
    extract_lease4(env, lease);
    /* Run script */
    int ret;
    ret = run_script("lease4_decline", env);
    return 0;
}

313
int lease4_expire(CalloutHandle& handle) {
314
    std::vector<std::string> env;
315
316
317
318
319
    Lease4Ptr lease;
    bool remove_lease;
    handle.getArgument("lease4", lease);
    extract_lease4(env, lease);
    handle.getArgument("remove_lease", remove_lease);
320
    extract_bool(env, "KEA_REMOVE_LEASE", remove_lease);
321
322
    /* Run script */
    int ret;
323
324
325
326
327
328
329
330
331
332
333
334
    ret = run_script("lease4_expire", env);
    return 0;
}

int lease4_recover(CalloutHandle& handle) {
    std::vector<std::string> env;
    Lease4Ptr lease;
    handle.getArgument("lease4", lease);
    extract_lease4(env, lease);
    /* Run script */
    int ret;
    ret = run_script("lease4_recover", env);
335
336
337
    return 0;
}

338
/* IPv6 callouts */
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
int pkt6_receive(CalloutHandle& handle) {
    std::vector<std::string> env;
    Pkt6Ptr query;
    handle.getArgument("query6", query);
    extract_query6(env, query);
    /* Run script */
    int ret;
    ret = run_script("pkt6_receive", env);
    return 0;
}

int pkt6_send(CalloutHandle& handle) {
    std::vector<std::string> env;
    Pkt6Ptr query, response;
    handle.getArgument("query6", query);
    extract_query6(env, query);
    handle.getArgument("response6", response);
    extract_response6(env, response);
    /* Run script */
    int ret;
    ret = run_script("pkt6_send", env);
    return 0;
}

int subnet6_select(CalloutHandle& handle) {
    std::vector<std::string> env;
    Pkt6Ptr query;
    Subnet6Ptr subnet;
    handle.getArgument("query6", query);
    extract_query6(env, query);
    handle.getArgument("subnet6", subnet);
    extract_subnet6(env, subnet);
    /* Run script */
    int ret;
    ret = run_script("subnet6_select", env);
    return 0;
}

int lease6_select(CalloutHandle& handle) {
    std::vector<std::string> env;
    Pkt6Ptr query;
    Subnet6Ptr subnet;
    bool fake_allocation;
    Lease6Ptr lease;
    handle.getArgument("query6", query);
    extract_query6(env, query);
    handle.getArgument("subnet6", subnet);
    extract_subnet6(env, subnet);
    handle.getArgument("fake_allocation", fake_allocation);
388
    extract_bool(env, "KEA_FAKE_ALLOCATION", fake_allocation);
389
390
391
392
393
394
395
396
397
398
399
400
401
    handle.getArgument("lease6", lease);
    extract_lease6(env, lease);
    /* Run script */
    int ret;
    ret = run_script("lease6_select", env);
    return 0;
}

int lease6_renew(CalloutHandle& handle) {
    std::vector<std::string> env;
    Pkt6Ptr query;
    Lease6Ptr lease;
    boost::shared_ptr<Option6IA> ia_na;
402
    boost::shared_ptr<Option6IA> ia_pd;
403
404
405
406
    handle.getArgument("query6", query);
    extract_query6(env, query);
    handle.getArgument("lease6", lease);
    extract_lease6(env, lease);
407
408
409
410
411
412
413
414
    try {
        handle.getArgument("ia_na", ia_na);
        /* TODO: use ia_na */
    } catch (const NoSuchArgument&) { }
    try {
        handle.getArgument("ia_pd", ia_pd);
        /* TODO: use ia_pd */
    } catch (const NoSuchArgument&) { }
415
416
417
418
419
420
421
422
423
424
425
    /* Run script */
    int ret;
    ret = run_script("lease6_renew", env);
    return 0;
}

int lease6_rebind(CalloutHandle& handle) {
    std::vector<std::string> env;
    Pkt6Ptr query;
    Lease6Ptr lease;
    boost::shared_ptr<Option6IA> ia_na;
426
    boost::shared_ptr<Option6IA> ia_pd;
427
428
429
430
    handle.getArgument("query6", query);
    extract_query6(env, query);
    handle.getArgument("lease6", lease);
    extract_lease6(env, lease);
431
432
433
434
435
436
437
438
    try {
        handle.getArgument("ia_na", ia_na);
        /* TODO: use ia_na */
    } catch (const NoSuchArgument&) { }
    try {
        handle.getArgument("ia_pd", ia_pd);
        /* TODO: use ia_pd */
    } catch (const NoSuchArgument&) { }
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
    /* Run script */
    int ret;
    ret = run_script("lease6_rebind", env);
    return 0;
}

int lease6_decline(CalloutHandle& handle) {
    std::vector<std::string> env;
    Pkt6Ptr query;
    Lease6Ptr lease;
    handle.getArgument("query6", query);
    extract_query6(env, query);
    handle.getArgument("lease6", lease);
    extract_lease6(env, lease);
    /* Run script */
    int ret;
    ret = run_script("lease6_decline", env);
    return 0;
}

int lease6_release(CalloutHandle& handle) {
    std::vector<std::string> env;
    Pkt6Ptr query;
    Lease6Ptr lease;
    handle.getArgument("query6", query);
    extract_query6(env, query);
    handle.getArgument("lease6", lease);
    extract_lease6(env, lease);
    /* Run script */
    int ret;
    ret = run_script("lease6_release", env);
    return 0;
}

int lease6_expire(CalloutHandle& handle) {
    std::vector<std::string> env;
    Lease6Ptr lease;
    bool remove_lease;
    handle.getArgument("lease6", lease);
    extract_lease6(env, lease);
    handle.getArgument("remove_lease", remove_lease);
480
    extract_bool(env, "KEA_REMOVE_LEASE", remove_lease);
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
    /* Run script */
    int ret;
    ret = run_script("lease6_expire", env);
    return 0;
}

int lease6_recover(CalloutHandle& handle) {
    std::vector<std::string> env;
    Lease6Ptr lease;
    handle.getArgument("lease6", lease);
    extract_lease6(env, lease);
    /* Run script */
    int ret;
    ret = run_script("lease6_recover", env);
    return 0;
}
497

Baptiste Jonglez's avatar
Baptiste Jonglez committed
498
} // end extern "C"