Coverage Report

Created: 2025-11-24 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bind9/lib/dns/peer.c
Line
Count
Source
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0
5
 *
6
 * This Source Code Form is subject to the terms of the Mozilla Public
7
 * License, v. 2.0. If a copy of the MPL was not distributed with this
8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9
 *
10
 * See the COPYRIGHT file distributed with this work for additional
11
 * information regarding copyright ownership.
12
 */
13
14
/*! \file */
15
16
#include <inttypes.h>
17
#include <limits.h>
18
#include <stdbool.h>
19
20
#include <isc/mem.h>
21
#include <isc/sockaddr.h>
22
#include <isc/string.h>
23
#include <isc/util.h>
24
25
#include <dns/bit.h>
26
#include <dns/fixedname.h>
27
#include <dns/name.h>
28
#include <dns/peer.h>
29
30
/***
31
 *** Types
32
 ***/
33
34
struct dns_peerlist {
35
  unsigned int magic;
36
  isc_refcount_t refs;
37
38
  isc_mem_t *mem;
39
40
  ISC_LIST(dns_peer_t) elements;
41
};
42
43
struct dns_peer {
44
  unsigned int magic;
45
  isc_refcount_t refs;
46
47
  isc_mem_t *mem;
48
49
  isc_netaddr_t address;
50
  unsigned int prefixlen;
51
  bool bogus;
52
  dns_transfer_format_t transfer_format;
53
  uint32_t transfers;
54
  uint32_t request_ixfr_maxdiffs;
55
  bool support_ixfr;
56
  bool provide_ixfr;
57
  bool request_ixfr;
58
  bool support_edns;
59
  bool request_nsid;
60
  bool request_zoneversion;
61
  bool send_cookie;
62
  bool require_cookie;
63
  bool request_expire;
64
  bool force_tcp;
65
  bool tcp_keepalive;
66
  bool check_axfr_id;
67
  dns_name_t *key;
68
  isc_sockaddr_t *transfer_source;
69
  isc_sockaddr_t *notify_source;
70
  isc_sockaddr_t *query_source;
71
  uint16_t udpsize;    /* receive size */
72
  uint16_t maxudp;     /* transmit size */
73
  uint16_t padding;    /* pad block size */
74
  uint8_t ednsversion; /* edns version */
75
76
  uint32_t bitflags;
77
78
  ISC_LINK(dns_peer_t) next;
79
};
80
81
/*%
82
 * Bit positions in the dns_peer_t structure flags field
83
 */
84
enum {
85
  BOGUS_BIT = 0,
86
  SERVER_TRANSFER_FORMAT_BIT,
87
  TRANSFERS_BIT,
88
  PROVIDE_IXFR_BIT,
89
  REQUEST_IXFR_BIT,
90
  REQUEST_IXFRMAXDIFFS_BIT,
91
  SUPPORT_EDNS_BIT,
92
  SERVER_UDPSIZE_BIT,
93
  SERVER_MAXUDP_BIT,
94
  REQUEST_NSID_BIT,
95
  SEND_COOKIE_BIT,
96
  REQUEST_EXPIRE_BIT,
97
  EDNS_VERSION_BIT,
98
  FORCE_TCP_BIT,
99
  SERVER_PADDING_BIT,
100
  REQUEST_TCP_KEEPALIVE_BIT,
101
  REQUIRE_COOKIE_BIT,
102
  DNS_PEER_FLAGS_COUNT,
103
  REQUEST_ZONEVERSION
104
};
105
106
STATIC_ASSERT(DNS_PEER_FLAGS_COUNT <= CHAR_BIT * sizeof(uint32_t),
107
        "dns_peer_t structure flags fields are too many for uint32_t");
108
109
static void
110
peerlist_delete(dns_peerlist_t **list);
111
112
static void
113
peer_delete(dns_peer_t **peer);
114
115
void
116
0
dns_peerlist_new(isc_mem_t *mem, dns_peerlist_t **list) {
117
0
  dns_peerlist_t *l;
118
119
0
  REQUIRE(list != NULL);
120
121
0
  l = isc_mem_get(mem, sizeof(*l));
122
123
0
  ISC_LIST_INIT(l->elements);
124
0
  l->mem = mem;
125
0
  isc_refcount_init(&l->refs, 1);
126
0
  l->magic = DNS_PEERLIST_MAGIC;
127
128
0
  *list = l;
129
0
}
130
131
void
132
0
dns_peerlist_attach(dns_peerlist_t *source, dns_peerlist_t **target) {
133
0
  REQUIRE(DNS_PEERLIST_VALID(source));
134
0
  REQUIRE(target != NULL);
135
0
  REQUIRE(*target == NULL);
136
137
0
  isc_refcount_increment(&source->refs);
138
139
0
  *target = source;
140
0
}
141
142
void
143
0
dns_peerlist_detach(dns_peerlist_t **list) {
144
0
  dns_peerlist_t *plist;
145
146
0
  REQUIRE(list != NULL);
147
0
  REQUIRE(*list != NULL);
148
0
  REQUIRE(DNS_PEERLIST_VALID(*list));
149
150
0
  plist = *list;
151
0
  *list = NULL;
152
153
0
  if (isc_refcount_decrement(&plist->refs) == 1) {
154
0
    peerlist_delete(&plist);
155
0
  }
156
0
}
157
158
static void
159
0
peerlist_delete(dns_peerlist_t **list) {
160
0
  dns_peerlist_t *l = NULL;
161
162
0
  REQUIRE(list != NULL);
163
0
  REQUIRE(DNS_PEERLIST_VALID(*list));
164
165
0
  l = *list;
166
0
  *list = NULL;
167
168
0
  isc_refcount_destroy(&l->refs);
169
170
0
  ISC_LIST_FOREACH(l->elements, server, next) {
171
0
    ISC_LIST_UNLINK(l->elements, server, next);
172
0
    dns_peer_detach(&server);
173
0
  }
174
175
0
  l->magic = 0;
176
0
  isc_mem_put(l->mem, l, sizeof(*l));
177
0
}
178
179
void
180
0
dns_peerlist_addpeer(dns_peerlist_t *peers, dns_peer_t *peer) {
181
  /*
182
   * More specifics to front of list.
183
   */
184
0
  dns_peer_attach(peer, &(dns_peer_t *){ NULL });
185
0
  ISC_LIST_FOREACH(peers->elements, p, next) {
186
0
    if (p->prefixlen < peer->prefixlen) {
187
0
      ISC_LIST_INSERTBEFORE(peers->elements, p, peer, next);
188
0
      return;
189
0
    }
190
0
  }
191
192
0
  ISC_LIST_APPEND(peers->elements, peer, next);
193
0
}
194
195
isc_result_t
196
dns_peerlist_peerbyaddr(dns_peerlist_t *servers, const isc_netaddr_t *addr,
197
0
      dns_peer_t **retval) {
198
0
  REQUIRE(retval != NULL);
199
0
  REQUIRE(DNS_PEERLIST_VALID(servers));
200
201
0
  ISC_LIST_FOREACH(servers->elements, server, next) {
202
0
    if (isc_netaddr_eqprefix(addr, &server->address,
203
0
           server->prefixlen))
204
0
    {
205
0
      *retval = server;
206
0
      return ISC_R_SUCCESS;
207
0
    }
208
0
  }
209
210
0
  return ISC_R_NOTFOUND;
211
0
}
212
213
isc_result_t
214
0
dns_peerlist_currpeer(dns_peerlist_t *peers, dns_peer_t **retval) {
215
0
  dns_peer_t *p = NULL;
216
217
0
  p = ISC_LIST_TAIL(peers->elements);
218
219
0
  dns_peer_attach(p, retval);
220
221
0
  return ISC_R_SUCCESS;
222
0
}
223
224
isc_result_t
225
0
dns_peer_new(isc_mem_t *mem, const isc_netaddr_t *addr, dns_peer_t **peerptr) {
226
0
  unsigned int prefixlen = 0;
227
228
0
  REQUIRE(peerptr != NULL);
229
0
  switch (addr->family) {
230
0
  case AF_INET:
231
0
    prefixlen = 32;
232
0
    break;
233
0
  case AF_INET6:
234
0
    prefixlen = 128;
235
0
    break;
236
0
  default:
237
0
    UNREACHABLE();
238
0
  }
239
240
0
  return dns_peer_newprefix(mem, addr, prefixlen, peerptr);
241
0
}
242
243
isc_result_t
244
dns_peer_newprefix(isc_mem_t *mem, const isc_netaddr_t *addr,
245
0
       unsigned int prefixlen, dns_peer_t **peerptr) {
246
0
  dns_peer_t *peer;
247
248
0
  REQUIRE(peerptr != NULL && *peerptr == NULL);
249
250
0
  peer = isc_mem_get(mem, sizeof(*peer));
251
252
0
  *peer = (dns_peer_t){
253
0
    .magic = DNS_PEER_MAGIC,
254
0
    .address = *addr,
255
0
    .prefixlen = prefixlen,
256
0
    .mem = mem,
257
0
    .transfer_format = dns_one_answer,
258
0
  };
259
260
0
  isc_refcount_init(&peer->refs, 1);
261
262
0
  ISC_LINK_INIT(peer, next);
263
264
0
  *peerptr = peer;
265
266
0
  return ISC_R_SUCCESS;
267
0
}
268
269
void
270
0
dns_peer_attach(dns_peer_t *source, dns_peer_t **target) {
271
0
  REQUIRE(DNS_PEER_VALID(source));
272
0
  REQUIRE(target != NULL);
273
0
  REQUIRE(*target == NULL);
274
275
0
  isc_refcount_increment(&source->refs);
276
277
0
  *target = source;
278
0
}
279
280
void
281
0
dns_peer_detach(dns_peer_t **peer) {
282
0
  dns_peer_t *p;
283
284
0
  REQUIRE(peer != NULL);
285
0
  REQUIRE(*peer != NULL);
286
0
  REQUIRE(DNS_PEER_VALID(*peer));
287
288
0
  p = *peer;
289
0
  *peer = NULL;
290
291
0
  if (isc_refcount_decrement(&p->refs) == 1) {
292
0
    peer_delete(&p);
293
0
  }
294
0
}
295
296
static void
297
0
peer_delete(dns_peer_t **peer) {
298
0
  dns_peer_t *p;
299
0
  isc_mem_t *mem;
300
301
0
  REQUIRE(peer != NULL);
302
0
  REQUIRE(DNS_PEER_VALID(*peer));
303
304
0
  p = *peer;
305
0
  *peer = NULL;
306
307
0
  isc_refcount_destroy(&p->refs);
308
309
0
  mem = p->mem;
310
0
  p->mem = NULL;
311
0
  p->magic = 0;
312
313
0
  if (p->key != NULL) {
314
0
    dns_name_free(p->key, mem);
315
0
    isc_mem_put(mem, p->key, sizeof(dns_name_t));
316
0
  }
317
318
0
  if (p->query_source != NULL) {
319
0
    isc_mem_put(mem, p->query_source, sizeof(*p->query_source));
320
0
  }
321
322
0
  if (p->notify_source != NULL) {
323
0
    isc_mem_put(mem, p->notify_source, sizeof(*p->notify_source));
324
0
  }
325
326
0
  if (p->transfer_source != NULL) {
327
0
    isc_mem_put(mem, p->transfer_source,
328
0
          sizeof(*p->transfer_source));
329
0
  }
330
331
0
  isc_mem_put(mem, p, sizeof(*p));
332
0
}
333
334
#define ACCESS_OPTION(name, macro, type, element)                        \
335
0
  isc_result_t dns_peer_get##name(dns_peer_t *peer, type *value) { \
336
0
    REQUIRE(DNS_PEER_VALID(peer));                           \
337
0
    REQUIRE(value != NULL);                                  \
338
0
    if (DNS_BIT_CHECK(macro, &peer->bitflags)) {             \
339
0
      *value = peer->element;                          \
340
0
      return (ISC_R_SUCCESS);                          \
341
0
    } else {                                                 \
342
0
      return (ISC_R_NOTFOUND);                         \
343
0
    }                                                        \
344
0
  }                                                                \
Unexecuted instantiation: dns_peer_getbogus
Unexecuted instantiation: dns_peer_getforcetcp
Unexecuted instantiation: dns_peer_getmaxudp
Unexecuted instantiation: dns_peer_getprovideixfr
Unexecuted instantiation: dns_peer_getrequestexpire
Unexecuted instantiation: dns_peer_getrequestixfr
Unexecuted instantiation: dns_peer_getrequestixfrmaxdiffs
Unexecuted instantiation: dns_peer_getrequestnsid
Unexecuted instantiation: dns_peer_getrequestzoneversion
Unexecuted instantiation: dns_peer_getrequirecookie
Unexecuted instantiation: dns_peer_getsendcookie
Unexecuted instantiation: dns_peer_getsupportedns
Unexecuted instantiation: dns_peer_gettcpkeepalive
Unexecuted instantiation: dns_peer_gettransferformat
Unexecuted instantiation: dns_peer_gettransfers
Unexecuted instantiation: dns_peer_getudpsize
345
0
  isc_result_t dns_peer_set##name(dns_peer_t *peer, type value) {  \
346
0
    bool existed;                                            \
347
0
    REQUIRE(DNS_PEER_VALID(peer));                           \
348
0
    existed = DNS_BIT_CHECK(macro, &peer->bitflags);         \
349
0
    peer->element = value;                                   \
350
0
    DNS_BIT_SET(macro, &peer->bitflags);                     \
351
0
    return (existed ? ISC_R_EXISTS : ISC_R_SUCCESS);         \
352
0
  }
353
354
0
ACCESS_OPTION(bogus, BOGUS_BIT, bool, bogus)
355
0
ACCESS_OPTION(forcetcp, FORCE_TCP_BIT, bool, force_tcp)
356
0
ACCESS_OPTION(maxudp, SERVER_MAXUDP_BIT, uint16_t, maxudp)
357
0
ACCESS_OPTION(provideixfr, PROVIDE_IXFR_BIT, bool, provide_ixfr)
358
0
ACCESS_OPTION(requestexpire, REQUEST_EXPIRE_BIT, bool, request_expire)
359
0
ACCESS_OPTION(requestixfr, REQUEST_IXFR_BIT, bool, request_ixfr)
360
0
ACCESS_OPTION(requestixfrmaxdiffs, REQUEST_IXFRMAXDIFFS_BIT, uint32_t,
361
        request_ixfr_maxdiffs)
362
0
ACCESS_OPTION(requestnsid, REQUEST_NSID_BIT, bool, request_nsid)
363
0
ACCESS_OPTION(requestzoneversion, REQUEST_ZONEVERSION, bool,
364
        request_zoneversion)
365
0
ACCESS_OPTION(requirecookie, REQUIRE_COOKIE_BIT, bool, require_cookie)
366
0
ACCESS_OPTION(sendcookie, SEND_COOKIE_BIT, bool, send_cookie)
367
0
ACCESS_OPTION(supportedns, SUPPORT_EDNS_BIT, bool, support_edns)
368
0
ACCESS_OPTION(tcpkeepalive, REQUEST_TCP_KEEPALIVE_BIT, bool, tcp_keepalive)
369
0
ACCESS_OPTION(transferformat, SERVER_TRANSFER_FORMAT_BIT, dns_transfer_format_t,
370
        transfer_format)
371
0
ACCESS_OPTION(transfers, TRANSFERS_BIT, uint32_t, transfers)
372
0
ACCESS_OPTION(udpsize, SERVER_UDPSIZE_BIT, uint16_t, udpsize)
373
374
#define ACCESS_OPTIONMAX(name, macro, type, element, max)                \
375
0
  isc_result_t dns_peer_get##name(dns_peer_t *peer, type *value) { \
376
0
    REQUIRE(DNS_PEER_VALID(peer));                           \
377
0
    REQUIRE(value != NULL);                                  \
378
0
    if (DNS_BIT_CHECK(macro, &peer->bitflags)) {             \
379
0
      *value = peer->element;                          \
380
0
      return (ISC_R_SUCCESS);                          \
381
0
    } else {                                                 \
382
0
      return (ISC_R_NOTFOUND);                         \
383
0
    }                                                        \
384
0
  }                                                                \
385
0
  isc_result_t dns_peer_set##name(dns_peer_t *peer, type value) {  \
386
0
    bool existed;                                            \
387
0
    REQUIRE(DNS_PEER_VALID(peer));                           \
388
0
    existed = DNS_BIT_CHECK(macro, &peer->bitflags);         \
389
0
    if (value > max) {                                       \
390
0
      value = max;                                     \
391
0
    }                                                        \
392
0
    peer->element = value;                                   \
393
0
    DNS_BIT_SET(macro, &peer->bitflags);                     \
394
0
    return (existed ? ISC_R_EXISTS : ISC_R_SUCCESS);         \
395
0
  }
396
397
0
ACCESS_OPTIONMAX(padding, SERVER_PADDING_BIT, uint16_t, padding, 512)
398
399
#define ACCESS_SOCKADDR(name, element)                                       \
400
  isc_result_t dns_peer_get##name(dns_peer_t *peer,                    \
401
0
          isc_sockaddr_t *value) {             \
402
0
    REQUIRE(DNS_PEER_VALID(peer));                               \
403
0
    REQUIRE(value != NULL);                                      \
404
0
    if (peer->element == NULL) {                                 \
405
0
      return (ISC_R_NOTFOUND);                             \
406
0
    }                                                            \
407
0
    *value = *peer->element;                                     \
408
0
    return (ISC_R_SUCCESS);                                      \
409
0
  }                                                                    \
410
  isc_result_t dns_peer_set##name(dns_peer_t *peer,                    \
411
0
          const isc_sockaddr_t *value) {       \
412
0
    REQUIRE(DNS_PEER_VALID(peer));                               \
413
0
    if (peer->element != NULL) {                                 \
414
0
      isc_mem_put(peer->mem, peer->element,                \
415
0
            sizeof(*peer->element));                 \
416
0
      peer->element = NULL;                                \
417
0
    }                                                            \
418
0
    if (value != NULL) {                                         \
419
0
      peer->element = isc_mem_get(peer->mem,               \
420
0
                sizeof(*peer->element)); \
421
0
      *peer->element = *value;                             \
422
0
    }                                                            \
423
0
    return (ISC_R_SUCCESS);                                      \
424
0
  }
425
426
0
ACCESS_SOCKADDR(notifysource, notify_source)
Unexecuted instantiation: dns_peer_getnotifysource
Unexecuted instantiation: dns_peer_setnotifysource
427
0
ACCESS_SOCKADDR(querysource, query_source)
Unexecuted instantiation: dns_peer_getquerysource
Unexecuted instantiation: dns_peer_setquerysource
428
0
ACCESS_SOCKADDR(transfersource, transfer_source)
Unexecuted instantiation: dns_peer_gettransfersource
Unexecuted instantiation: dns_peer_settransfersource
429
430
#define ACCESS_OPTION_OVERWRITE(name, macro, type, element)              \
431
0
  isc_result_t dns_peer_get##name(dns_peer_t *peer, type *value) { \
432
0
    REQUIRE(DNS_PEER_VALID(peer));                           \
433
0
    REQUIRE(value != NULL);                                  \
434
0
    if (DNS_BIT_CHECK(macro, &peer->bitflags)) {             \
435
0
      *value = peer->element;                          \
436
0
      return (ISC_R_SUCCESS);                          \
437
0
    } else {                                                 \
438
0
      return (ISC_R_NOTFOUND);                         \
439
0
    }                                                        \
440
0
  }                                                                \
441
0
  isc_result_t dns_peer_set##name(dns_peer_t *peer, type value) {  \
442
0
    REQUIRE(DNS_PEER_VALID(peer));                           \
443
0
    peer->element = value;                                   \
444
0
    DNS_BIT_SET(macro, &peer->bitflags);                     \
445
0
    return (ISC_R_SUCCESS);                                  \
446
0
  }
447
448
ACCESS_OPTION_OVERWRITE(ednsversion, EDNS_VERSION_BIT, uint8_t, ednsversion)
449
450
isc_result_t
451
0
dns_peer_getkey(dns_peer_t *peer, dns_name_t **retval) {
452
0
  REQUIRE(DNS_PEER_VALID(peer));
453
0
  REQUIRE(retval != NULL);
454
455
0
  if (peer->key != NULL) {
456
0
    *retval = peer->key;
457
0
  }
458
459
0
  return peer->key == NULL ? ISC_R_NOTFOUND : ISC_R_SUCCESS;
460
0
}
461
462
isc_result_t
463
0
dns_peer_setkey(dns_peer_t *peer, dns_name_t **keyval) {
464
0
  bool exists = false;
465
466
0
  if (peer->key != NULL) {
467
0
    dns_name_free(peer->key, peer->mem);
468
0
    isc_mem_put(peer->mem, peer->key, sizeof(dns_name_t));
469
0
    exists = true;
470
0
  }
471
472
0
  peer->key = *keyval;
473
0
  *keyval = NULL;
474
475
0
  return exists ? ISC_R_EXISTS : ISC_R_SUCCESS;
476
0
}
477
478
isc_result_t
479
0
dns_peer_setkeybycharp(dns_peer_t *peer, const char *keyval) {
480
0
  isc_buffer_t b;
481
0
  dns_fixedname_t fname;
482
0
  dns_name_t *name;
483
0
  isc_result_t result;
484
485
0
  dns_fixedname_init(&fname);
486
0
  isc_buffer_constinit(&b, keyval, strlen(keyval));
487
0
  isc_buffer_add(&b, strlen(keyval));
488
0
  result = dns_name_fromtext(dns_fixedname_name(&fname), &b, dns_rootname,
489
0
           0);
490
0
  if (result != ISC_R_SUCCESS) {
491
0
    return result;
492
0
  }
493
494
0
  name = isc_mem_get(peer->mem, sizeof(dns_name_t));
495
496
0
  dns_name_init(name);
497
0
  dns_name_dup(dns_fixedname_name(&fname), peer->mem, name);
498
499
0
  result = dns_peer_setkey(peer, &name);
500
0
  if (result != ISC_R_SUCCESS) {
501
0
    isc_mem_put(peer->mem, name, sizeof(dns_name_t));
502
0
  }
503
504
0
  return result;
505
0
}