Blame
Date:
Mon Jan 11 09:00:47 2021 UTC
Message:
a usleep makes it unnecessary slow, I guess it forces a context switch...
0001
2014-11-14
pjp
/*
0002
2021-01-04
pjp
* Copyright (c) 2005-2021 Peter J. Philipp
0003
2014-11-14
pjp
* All rights reserved.
0004
2014-11-14
pjp
*
0005
2014-11-14
pjp
* Redistribution and use in source and binary forms, with or without
0006
2014-11-14
pjp
* modification, are permitted provided that the following conditions
0007
2014-11-14
pjp
* are met:
0008
2014-11-14
pjp
* 1. Redistributions of source code must retain the above copyright
0009
2014-11-14
pjp
* notice, this list of conditions and the following disclaimer.
0010
2014-11-14
pjp
* 2. Redistributions in binary form must reproduce the above copyright
0011
2014-11-14
pjp
* notice, this list of conditions and the following disclaimer in the
0012
2014-11-14
pjp
* documentation and/or other materials provided with the distribution.
0013
2014-11-14
pjp
* 3. The name of the author may not be used to endorse or promote products
0014
2014-11-14
pjp
* derived from this software without specific prior written permission
0015
2014-11-14
pjp
*
0016
2014-11-14
pjp
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017
2014-11-14
pjp
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018
2014-11-14
pjp
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019
2014-11-14
pjp
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020
2014-11-14
pjp
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021
2014-11-14
pjp
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022
2014-11-14
pjp
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023
2014-11-14
pjp
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024
2014-11-14
pjp
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025
2014-11-14
pjp
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026
2014-11-14
pjp
*
0027
2014-11-14
pjp
*/
0028
2017-10-26
pjp
0029
2019-06-06
pjp
#include <sys/types.h>
0030
2019-06-06
pjp
#include <sys/socket.h>
0031
2017-10-26
pjp
0032
2019-06-06
pjp
#include <netinet/in.h>
0033
2019-06-06
pjp
#include <arpa/inet.h>
0034
2019-06-06
pjp
#include <netdb.h>
0035
2019-02-27
pjp
0036
2019-06-06
pjp
#include <stdio.h>
0037
2019-06-06
pjp
#include <stdlib.h>
0038
2019-06-06
pjp
#include <string.h>
0039
2019-06-06
pjp
#include <errno.h>
0040
2019-06-06
pjp
#include <syslog.h>
0041
2020-07-06
pjp
#include <time.h>
0042
2019-06-06
pjp
0043
2019-06-06
pjp
#ifdef __linux__
0044
2019-06-06
pjp
#include <grp.h>
0045
2019-06-06
pjp
#define __USE_BSD 1
0046
2019-06-06
pjp
#include <endian.h>
0047
2019-06-06
pjp
#include <bsd/stdlib.h>
0048
2019-06-06
pjp
#include <bsd/string.h>
0049
2019-06-06
pjp
#include <bsd/sys/queue.h>
0050
2019-06-06
pjp
#define __unused
0051
2019-06-06
pjp
#include <bsd/sys/tree.h>
0052
2019-06-06
pjp
#include <bsd/sys/endian.h>
0053
2019-06-06
pjp
#else /* not linux */
0054
2019-06-06
pjp
#include <sys/queue.h>
0055
2019-06-06
pjp
#include <sys/tree.h>
0056
2019-06-06
pjp
#endif /* __linux__ */
0057
2019-06-06
pjp
0058
2019-06-06
pjp
#ifndef NTOHS
0059
2019-06-06
pjp
#include "endian.h"
0060
2019-06-06
pjp
#endif
0061
2019-06-06
pjp
0062
2019-02-27
pjp
#include <openssl/evp.h>
0063
2019-02-27
pjp
#include <openssl/hmac.h>
0064
2019-02-27
pjp
0065
2019-06-06
pjp
#include "ddd-dns.h"
0066
2019-06-06
pjp
#include "ddd-db.h"
0067
2016-07-06
pjp
0068
2014-11-14
pjp
/* prototypes */
0069
2014-11-14
pjp
0070
2019-12-03
pjp
extern void pack(char *, char *, int);
0071
2019-12-03
pjp
extern void pack32(char *, u_int32_t);
0072
2019-12-03
pjp
extern void pack16(char *, u_int16_t);
0073
2019-12-03
pjp
extern void pack8(char *, u_int8_t);
0074
2019-12-03
pjp
extern uint32_t unpack32(char *);
0075
2019-12-03
pjp
extern uint16_t unpack16(char *);
0076
2019-12-03
pjp
extern void unpack(char *, char *, int);
0077
2019-12-03
pjp
0078
2019-02-15
pjp
extern int checklabel(ddDB *, struct rbtree *, struct rbtree *, struct question *);
0079
2020-07-06
pjp
extern int additional_nsec3(char *, int, int, struct rbtree *, char *, int, int, int *, int);
0080
2019-02-15
pjp
extern int additional_a(char *, int, struct rbtree *, char *, int, int, int *);
0081
2019-02-15
pjp
extern int additional_aaaa(char *, int, struct rbtree *, char *, int, int, int *);
0082
2019-02-15
pjp
extern int additional_mx(char *, int, struct rbtree *, char *, int, int, int *);
0083
2019-11-09
pjp
extern int additional_ds(char *, int, struct rbtree *, char *, int, int, int *);
0084
2019-02-15
pjp
extern int additional_ptr(char *, int, struct rbtree *, char *, int, int, int *);
0085
2014-11-14
pjp
extern int additional_opt(struct question *, char *, int, int);
0086
2021-01-07
pjp
extern int additional_tsig(struct question *, char *, int, int, int, int, HMAC_CTX *, uint16_t);
0087
2020-07-06
pjp
extern int additional_rrsig(char *, int, int, struct rbtree *, char *, int, int, int *, int);
0088
2020-07-06
pjp
extern int additional_nsec(char *, int, int, struct rbtree *, char *, int, int, int);
0089
2019-02-26
pjp
extern struct question *build_fake_question(char *, int, u_int16_t, char *, int);
0090
2014-11-14
pjp
extern int compress_label(u_char *, int, int);
0091
2014-11-14
pjp
extern void dolog(int, char *, ...);
0092
2014-11-14
pjp
extern int free_question(struct question *);
0093
2017-06-26
pjp
extern int get_record_size(ddDB *, char *, int);
0094
2015-07-01
pjp
extern char * dns_label(char *, int *);
0095
2014-11-14
pjp
0096
2019-02-15
pjp
extern struct rbtree * find_rrset(ddDB *db, char *name, int len);
0097
2019-02-15
pjp
extern struct rrset * find_rr(struct rbtree *rbt, u_int16_t rrtype);
0098
2019-02-15
pjp
extern int display_rr(struct rrset *rrset);
0099
2019-02-15
pjp
extern int rotate_rr(struct rrset *rrset);
0100
2019-10-30
pjp
extern struct rbtree * find_nsec3_cover_next_closer(char *name, int namelen, struct rbtree *, ddDB *db);
0101
2019-10-30
pjp
extern struct rbtree * find_nsec3_match_closest(char *name, int namelen, struct rbtree *, ddDB *db);
0102
2019-10-30
pjp
extern struct rbtree * find_nsec3_wildcard_closest(char *name, int namelen, struct rbtree *, ddDB *db);
0103
2019-10-30
pjp
extern struct rbtree * find_nsec3_match_qname(char *name, int namelen, struct rbtree *, ddDB *db);
0104
2019-10-31
pjp
extern struct rbtree * get_soa(ddDB *, struct question *);
0105
2019-10-31
pjp
extern struct rbtree * get_ns(ddDB *, struct rbtree *, int *);
0106
2019-10-31
pjp
extern struct rbtree * Lookup_zone(ddDB *, char *, u_int16_t, u_int16_t, int);
0107
2019-11-12
pjp
extern int dn_contains(char *, int, char *, int);
0108
2021-01-05
pjp
extern struct zoneentry * zone_findzone(struct rbtree *);
0109
2019-02-15
pjp
0110
2019-02-15
pjp
0111
2021-01-05
pjp
u_int16_t create_anyreply(struct sreply *, char *, int, int, int, uint32_t);
0112
2020-07-23
pjp
int reply_caa(struct sreply *, int *, ddDB *);
0113
2020-07-23
pjp
int reply_hinfo(struct sreply *, int *, ddDB *);
0114
2020-07-23
pjp
int reply_rp(struct sreply *, int *, ddDB *);
0115
2020-07-21
pjp
int reply_generic(struct sreply *, int *, ddDB *);
0116
2020-07-21
pjp
int reply_a(struct sreply *, int *, ddDB *);
0117
2020-07-21
pjp
int reply_nsec3(struct sreply *, int *, ddDB *);
0118
2020-07-21
pjp
int reply_nsec3param(struct sreply *, int *, ddDB *);
0119
2020-07-21
pjp
int reply_nsec(struct sreply *, int *, ddDB *);
0120
2020-07-21
pjp
int reply_dnskey(struct sreply *, int *, ddDB *);
0121
2020-07-21
pjp
int reply_ds(struct sreply *, int *, ddDB *);
0122
2020-07-21
pjp
int reply_rrsig(struct sreply *, int *, ddDB *);
0123
2020-07-21
pjp
int reply_aaaa(struct sreply *, int *, ddDB *);
0124
2020-07-21
pjp
int reply_mx(struct sreply *, int *, ddDB *);
0125
2020-07-21
pjp
int reply_ns(struct sreply *, int *, ddDB *);
0126
2020-07-21
pjp
int reply_notimpl(struct sreply *, int *, ddDB *);
0127
2020-07-21
pjp
int reply_nxdomain(struct sreply *, int *, ddDB *);
0128
2020-07-21
pjp
int reply_noerror(struct sreply *, int *, ddDB *);
0129
2020-07-21
pjp
int reply_badvers(struct sreply *, int *, ddDB *);
0130
2020-07-21
pjp
int reply_nodata(struct sreply *, int *, ddDB *);
0131
2020-07-21
pjp
int reply_soa(struct sreply *, int *, ddDB *);
0132
2020-07-21
pjp
int reply_ptr(struct sreply *, int *, ddDB *);
0133
2020-07-21
pjp
int reply_txt(struct sreply *, int *, ddDB *);
0134
2020-07-21
pjp
int reply_version(struct sreply *, int *, ddDB *);
0135
2020-07-21
pjp
int reply_srv(struct sreply *, int *, ddDB *);
0136
2020-07-21
pjp
int reply_naptr(struct sreply *, int *, ddDB *);
0137
2020-07-21
pjp
int reply_sshfp(struct sreply *, int *, ddDB *);
0138
2020-07-21
pjp
int reply_tlsa(struct sreply *, int *, ddDB *);
0139
2020-07-21
pjp
int reply_cname(struct sreply *, int *, ddDB *);
0140
2020-07-21
pjp
int reply_any(struct sreply *, int *, ddDB *);
0141
2020-07-21
pjp
int reply_refused(struct sreply *, int *, ddDB *);
0142
2020-07-21
pjp
int reply_fmterror(struct sreply *, int *, ddDB *);
0143
2020-07-21
pjp
int reply_notauth(struct sreply *, int *, ddDB *);
0144
2020-07-21
pjp
int reply_notify(struct sreply *, int *, ddDB *);
0145
2019-02-15
pjp
struct rbtree * find_nsec(char *name, int namelen, struct rbtree *, ddDB *db);
0146
2015-07-01
pjp
int nsec_comp(const void *a, const void *b);
0147
2015-07-01
pjp
int count_dots(char *name);
0148
2015-09-12
pjp
char * base32hex_encode(u_char *input, int len);
0149
2020-07-20
pjp
void set_reply_flags(struct rbtree *, struct dns_header *, struct question *);
0150
2014-11-14
pjp
0151
2020-04-23
pjp
extern int debug, verbose, dnssec, tcpanyonly;
0152
2015-09-05
pjp
extern char *versionstring;
0153
2015-09-05
pjp
extern uint8_t vslen;
0154
2020-09-30
pjp
extern u_int max_udp_payload;
0155
2014-11-14
pjp
0156
2014-11-14
pjp
0157
2014-11-14
pjp
0158
2014-11-14
pjp
/*
0159
2014-11-14
pjp
* REPLY_A() - replies a DNS question (*q) on socket (so)
0160
2014-11-14
pjp
*
0161
2014-11-14
pjp
*/
0162
2014-11-14
pjp
0163
2014-11-14
pjp
int
0164
2020-07-21
pjp
reply_a(struct sreply *sreply, int *sretlen, ddDB *db)
0165
2014-11-14
pjp
{
0166
2014-11-14
pjp
char *reply = sreply->replybuf;
0167
2014-11-14
pjp
struct dns_header *odh;
0168
2015-06-20
pjp
u_int16_t outlen = 0;
0169
2014-11-14
pjp
int a_count;
0170
2014-11-14
pjp
0171
2014-11-14
pjp
struct answer {
0172
2014-11-14
pjp
char name[2];
0173
2014-11-14
pjp
u_int16_t type;
0174
2014-11-14
pjp
u_int16_t class;
0175
2014-11-14
pjp
u_int32_t ttl;
0176
2014-11-14
pjp
u_int16_t rdlength; /* 12 */
0177
2014-11-14
pjp
in_addr_t rdata; /* 16 */
0178
2014-11-14
pjp
} __attribute__((packed));
0179
2014-11-14
pjp
0180
2014-11-14
pjp
struct answer *answer;
0181
2014-11-14
pjp
0182
2014-11-14
pjp
int so = sreply->so;
0183
2014-11-14
pjp
char *buf = sreply->buf;
0184
2014-11-14
pjp
int len = sreply->len;
0185
2014-11-14
pjp
struct question *q = sreply->q;
0186
2014-11-14
pjp
struct sockaddr *sa = sreply->sa;
0187
2014-11-14
pjp
int salen = sreply->salen;
0188
2019-02-15
pjp
0189
2019-02-15
pjp
struct rbtree *rbt = sreply->rbt1;
0190
2019-02-15
pjp
struct rrset *rrset = NULL;
0191
2019-02-15
pjp
struct rr *rrp;
0192
2014-11-14
pjp
0193
2014-11-14
pjp
int istcp = sreply->istcp;
0194
2014-11-14
pjp
int replysize = 512;
0195
2014-11-14
pjp
int retlen = -1;
0196
2017-09-18
pjp
u_int16_t rollback;
0197
2020-07-06
pjp
time_t now;
0198
2021-01-05
pjp
uint32_t zonenumberx;
0199
2021-01-05
pjp
struct zoneentry *res = NULL;
0200
2014-11-14
pjp
0201
2021-01-07
pjp
if (rbt->flags & RBT_CACHE) {
0202
2021-01-05
pjp
zonenumberx = (uint32_t)-1;
0203
2021-01-07
pjp
} else {
0204
2021-01-07
pjp
res = zone_findzone(rbt);
0205
2021-01-07
pjp
if (res != NULL)
0206
2021-01-07
pjp
zonenumberx = res->zonenumber;
0207
2021-01-07
pjp
else
0208
2021-01-07
pjp
zonenumberx = (uint32_t)-1;
0209
2021-01-07
pjp
}
0210
2021-01-05
pjp
0211
2020-07-06
pjp
now = time(NULL);
0212
2020-07-06
pjp
0213
2019-02-27
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_A)) == 0)
0214
2015-06-16
pjp
return -1;
0215
2015-06-16
pjp
0216
2014-11-14
pjp
if (istcp) {
0217
2014-11-14
pjp
replysize = 65535;
0218
2014-11-14
pjp
}
0219
2014-11-14
pjp
0220
2015-06-20
pjp
if (!istcp && q->edns0len > 512)
0221
2020-09-30
pjp
replysize = MIN(q->edns0len, max_udp_payload);
0222
2014-11-14
pjp
0223
2021-01-05
pjp
0224
2014-11-14
pjp
odh = (struct dns_header *)&reply[0];
0225
2014-11-14
pjp
0226
2014-11-14
pjp
outlen = sizeof(struct dns_header);
0227
2014-11-14
pjp
0228
2014-11-14
pjp
if (len > replysize) {
0229
2014-11-14
pjp
return (retlen);
0230
2014-11-14
pjp
}
0231
2014-11-14
pjp
0232
2014-11-14
pjp
memcpy(reply, buf, sizeof(struct dns_header) + q->hdr->namelen + 4);
0233
2014-11-14
pjp
memset((char *)&odh->query, 0, sizeof(u_int16_t));
0234
2014-11-14
pjp
0235
2014-11-14
pjp
outlen += (q->hdr->namelen + 4);
0236
2017-09-18
pjp
rollback = outlen;
0237
2014-11-14
pjp
0238
2020-07-20
pjp
set_reply_flags(rbt, odh, q);
0239
2014-11-14
pjp
0240
2014-11-14
pjp
odh->question = htons(1);
0241
2019-02-15
pjp
odh->answer = htons(0);
0242
2014-11-14
pjp
odh->nsrr = 0;
0243
2014-11-14
pjp
odh->additional = 0;
0244
2014-11-14
pjp
0245
2014-11-14
pjp
/* skip dns header, question name, qtype and qclass */
0246
2014-11-14
pjp
answer = (struct answer *)(&reply[0] + sizeof(struct dns_header) +
0247
2014-11-14
pjp
q->hdr->namelen + 4);
0248
2014-11-14
pjp
0249
2014-11-14
pjp
a_count = 0;
0250
2014-11-14
pjp
0251
2019-02-15
pjp
TAILQ_FOREACH(rrp, &rrset->rr_head, entries) {
0252
2021-01-05
pjp
if (rrp->zonenumber != zonenumberx)
0253
2021-01-05
pjp
continue;
0254
2014-11-14
pjp
/*
0255
2014-11-14
pjp
* answer->name is a pointer to the request (0xc00c)
0256
2014-11-14
pjp
*/
0257
2014-11-14
pjp
0258
2014-11-14
pjp
answer->name[0] = 0xc0; /* 1 byte */
0259
2014-11-14
pjp
answer->name[1] = 0x0c; /* 2 bytes */
0260
2014-11-14
pjp
answer->type = q->hdr->qtype; /* 4 bytes */
0261
2014-11-14
pjp
answer->class = q->hdr->qclass; /* 6 bytes */
0262
2019-02-15
pjp
0263
2020-07-06
pjp
if (q->aa)
0264
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl); /* 10 b */
0265
2020-07-06
pjp
else
0266
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl - (MIN(rrset->ttl, difftime(now, rrset->created))));
0267
2020-07-06
pjp
0268
2014-11-14
pjp
answer->rdlength = htons(sizeof(in_addr_t)); /* 12 bytes */
0269
2014-11-14
pjp
0270
2019-02-15
pjp
memcpy((char *)&answer->rdata,
0271
2019-02-15
pjp
(char *)&((struct a *)rrp->rdata)->a,
0272
2014-11-14
pjp
sizeof(in_addr_t)); /* 16 bytes */
0273
2014-11-14
pjp
0274
2014-11-14
pjp
a_count++;
0275
2014-11-14
pjp
outlen += 16;
0276
2014-11-14
pjp
0277
2014-11-14
pjp
/* can we afford to write another header? if no truncate */
0278
2019-02-15
pjp
if (outlen + 16 > replysize) {
0279
2014-11-14
pjp
NTOHS(odh->query);
0280
2014-11-14
pjp
SET_DNS_TRUNCATION(odh);
0281
2014-11-14
pjp
HTONS(odh->query);
0282
2017-09-18
pjp
odh->answer = 0;
0283
2017-09-18
pjp
odh->nsrr = 0;
0284
2017-09-18
pjp
odh->additional = 0;
0285
2017-09-18
pjp
outlen = rollback;
0286
2014-11-14
pjp
goto out;
0287
2014-11-14
pjp
}
0288
2014-11-14
pjp
0289
2014-11-14
pjp
0290
2014-11-14
pjp
/* set new offset for answer */
0291
2014-11-14
pjp
answer = (struct answer *)&reply[outlen];
0292
2019-02-15
pjp
}
0293
2014-11-14
pjp
0294
2019-02-15
pjp
odh->answer = htons(a_count);
0295
2019-02-15
pjp
0296
2015-06-27
pjp
/* Add RRSIG reply_a */
0297
2019-11-11
pjp
if (dnssec && q->dnssecok && (rbt->flags & RBT_DNSSEC)) {
0298
2015-06-20
pjp
int tmplen = 0;
0299
2015-06-22
pjp
int origlen = outlen;
0300
2020-04-01
pjp
int retcount;
0301
2015-06-20
pjp
0302
2020-07-06
pjp
tmplen = additional_rrsig(q->hdr->name, q->hdr->namelen, DNS_TYPE_A, rbt, reply, replysize, outlen, &retcount, q->aa);
0303
2015-06-20
pjp
0304
2015-06-20
pjp
if (tmplen == 0) {
0305
2020-07-08
pjp
/* we're forwarding and had no RRSIG return with -1 */
0306
2020-07-08
pjp
if (q->aa != 1)
0307
2020-07-08
pjp
return -1;
0308
2020-07-08
pjp
0309
2015-06-20
pjp
NTOHS(odh->query);
0310
2015-06-20
pjp
SET_DNS_TRUNCATION(odh);
0311
2015-06-20
pjp
HTONS(odh->query);
0312
2017-09-18
pjp
odh->answer = 0;
0313
2017-09-18
pjp
odh->nsrr = 0;
0314
2017-09-18
pjp
odh->additional = 0;
0315
2017-09-18
pjp
outlen = rollback;
0316
2015-06-20
pjp
goto out;
0317
2015-06-20
pjp
}
0318
2015-06-20
pjp
0319
2015-06-20
pjp
outlen = tmplen;
0320
2015-06-22
pjp
0321
2015-06-22
pjp
if (outlen > origlen)
0322
2020-04-01
pjp
odh->answer = htons(a_count + retcount);
0323
2015-06-22
pjp
0324
2014-11-14
pjp
}
0325
2014-11-14
pjp
0326
2015-11-14
pjp
out:
0327
2014-11-14
pjp
if (q->edns0len) {
0328
2014-11-14
pjp
/* tag on edns0 opt record */
0329
2014-11-14
pjp
odh->additional = htons(1);
0330
2014-11-14
pjp
outlen = additional_opt(q, reply, replysize, outlen);
0331
2014-11-14
pjp
}
0332
2014-11-14
pjp
0333
2019-02-24
pjp
if (q->tsig.tsigverified == 1) {
0334
2021-01-07
pjp
outlen = additional_tsig(q, reply, replysize, outlen, 0, 0, NULL, DEFAULT_TSIG_FUDGE);
0335
2019-02-24
pjp
0336
2019-02-24
pjp
NTOHS(odh->additional);
0337
2019-02-24
pjp
odh->additional++;
0338
2019-02-24
pjp
HTONS(odh->additional);
0339
2019-02-24
pjp
}
0340
2019-02-24
pjp
0341
2015-12-19
pjp
if (istcp) {
0342
2015-12-19
pjp
char *tmpbuf;
0343
2015-12-19
pjp
0344
2015-12-19
pjp
tmpbuf = malloc(outlen + 2);
0345
2019-02-27
pjp
if (tmpbuf == 0) {
0346
2015-12-19
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
0347
2015-12-19
pjp
}
0348
2019-12-03
pjp
pack16(tmpbuf, htons(outlen));
0349
2015-12-19
pjp
memcpy(&tmpbuf[2], reply, outlen);
0350
2015-12-19
pjp
0351
2015-12-19
pjp
if ((retlen = send(so, tmpbuf, outlen + 2, 0)) < 0) {
0352
2015-12-19
pjp
dolog(LOG_INFO, "send: %s\n", strerror(errno));
0353
2015-12-19
pjp
}
0354
2015-12-19
pjp
free(tmpbuf);
0355
2015-12-19
pjp
} else {
0356
2020-07-21
pjp
if (q->rawsocket) {
0357
2020-07-21
pjp
*sretlen = retlen = outlen;
0358
2020-07-21
pjp
} else {
0359
2020-07-21
pjp
if ((retlen = sendto(so, reply, outlen, 0, sa, salen)) < 0) {
0360
2020-07-21
pjp
dolog(LOG_INFO, "sendto: %s\n", strerror(errno));
0361
2020-07-21
pjp
}
0362
2015-06-20
pjp
}
0363
2015-12-19
pjp
}
0364
2015-06-20
pjp
0365
2015-06-20
pjp
/*
0366
2019-02-15
pjp
* update order XXX
0367
2015-06-20
pjp
*/
0368
2015-06-20
pjp
0369
2019-02-15
pjp
rotate_rr(rrset);
0370
2019-02-15
pjp
0371
2015-06-27
pjp
return (retlen);
0372
2015-06-27
pjp
}
0373
2015-06-20
pjp
0374
2015-06-27
pjp
/*
0375
2015-06-27
pjp
* REPLY_NSEC3PARAM() - replies a DNS question (*q) on socket (so)
0376
2015-06-27
pjp
*
0377
2015-06-27
pjp
*/
0378
2015-06-27
pjp
0379
2015-06-27
pjp
int
0380
2020-07-21
pjp
reply_nsec3param(struct sreply *sreply, int *sretlen, ddDB *db)
0381
2015-06-27
pjp
{
0382
2015-06-27
pjp
char *reply = sreply->replybuf;
0383
2015-06-27
pjp
struct dns_header *odh;
0384
2015-06-27
pjp
u_int16_t outlen = 0;
0385
2015-06-27
pjp
int a_count;
0386
2015-06-27
pjp
0387
2015-06-27
pjp
struct answer {
0388
2015-06-27
pjp
char name[2];
0389
2015-06-27
pjp
u_int16_t type;
0390
2015-06-27
pjp
u_int16_t class;
0391
2015-06-27
pjp
u_int32_t ttl;
0392
2015-06-27
pjp
u_int16_t rdlength; /* 12 */
0393
2015-06-27
pjp
u_int8_t algorithm;
0394
2015-06-27
pjp
u_int8_t flags;
0395
2015-06-27
pjp
u_int16_t iterations;
0396
2015-06-27
pjp
u_int8_t saltlen;
0397
2015-06-27
pjp
} __attribute__((packed));
0398
2015-06-27
pjp
0399
2015-06-27
pjp
struct answer *answer;
0400
2015-06-27
pjp
0401
2015-06-27
pjp
int so = sreply->so;
0402
2015-06-27
pjp
char *buf = sreply->buf;
0403
2015-06-27
pjp
int len = sreply->len;
0404
2015-06-27
pjp
struct question *q = sreply->q;
0405
2015-06-27
pjp
struct sockaddr *sa = sreply->sa;
0406
2015-06-27
pjp
int salen = sreply->salen;
0407
2019-02-15
pjp
0408
2019-02-15
pjp
struct rbtree *rbt = sreply->rbt1;
0409
2019-02-15
pjp
struct rrset *rrset = NULL;
0410
2019-02-15
pjp
struct rr *rrp = NULL;
0411
2015-06-27
pjp
0412
2015-06-27
pjp
int istcp = sreply->istcp;
0413
2015-06-27
pjp
int replysize = 512;
0414
2015-06-27
pjp
int retlen = -1;
0415
2017-09-18
pjp
u_int16_t rollback;
0416
2019-02-15
pjp
int saltlen;
0417
2020-07-06
pjp
0418
2020-07-06
pjp
time_t now;
0419
2021-01-05
pjp
uint32_t zonenumberx;
0420
2021-01-05
pjp
struct zoneentry *res = NULL;
0421
2015-06-27
pjp
0422
2021-01-07
pjp
if (rbt->flags & RBT_CACHE) {
0423
2021-01-05
pjp
zonenumberx = (uint32_t)-1;
0424
2021-01-07
pjp
} else {
0425
2021-01-07
pjp
res = zone_findzone(rbt);
0426
2021-01-07
pjp
if (res != NULL)
0427
2021-01-07
pjp
zonenumberx = res->zonenumber;
0428
2021-01-07
pjp
else
0429
2021-01-07
pjp
zonenumberx = (uint32_t)-1;
0430
2021-01-07
pjp
}
0431
2021-01-05
pjp
0432
2020-07-06
pjp
now = time(NULL);
0433
2020-07-06
pjp
0434
2019-02-27
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_NSEC3PARAM)) == 0)
0435
2015-06-27
pjp
return -1;
0436
2015-06-27
pjp
0437
2015-06-27
pjp
if (istcp) {
0438
2015-06-27
pjp
replysize = 65535;
0439
2015-06-27
pjp
}
0440
2015-06-27
pjp
0441
2015-06-27
pjp
if (!istcp && q->edns0len > 512)
0442
2020-09-30
pjp
replysize = MIN(q->edns0len, max_udp_payload);
0443
2015-06-27
pjp
0444
2015-06-27
pjp
odh = (struct dns_header *)&reply[0];
0445
2015-06-27
pjp
0446
2015-06-27
pjp
outlen = sizeof(struct dns_header);
0447
2015-06-27
pjp
0448
2015-06-27
pjp
if (len > replysize) {
0449
2015-06-27
pjp
return (retlen);
0450
2015-06-27
pjp
}
0451
2015-06-27
pjp
0452
2015-06-27
pjp
memcpy(reply, buf, sizeof(struct dns_header) + q->hdr->namelen + 4);
0453
2015-06-27
pjp
memset((char *)&odh->query, 0, sizeof(u_int16_t));
0454
2015-06-27
pjp
0455
2015-06-27
pjp
outlen += (q->hdr->namelen + 4);
0456
2017-09-18
pjp
rollback = outlen;
0457
2015-06-27
pjp
0458
2020-07-20
pjp
set_reply_flags(rbt, odh, q);
0459
2015-06-27
pjp
0460
2015-06-27
pjp
odh->question = htons(1);
0461
2015-06-27
pjp
odh->answer = htons(1);
0462
2015-06-27
pjp
odh->nsrr = 0;
0463
2015-06-27
pjp
odh->additional = 0;
0464
2015-06-27
pjp
0465
2019-02-15
pjp
0466
2015-06-27
pjp
/* skip dns header, question name, qtype and qclass */
0467
2015-06-27
pjp
answer = (struct answer *)(&reply[0] + sizeof(struct dns_header) +
0468
2015-06-27
pjp
q->hdr->namelen + 4);
0469
2015-06-27
pjp
0470
2015-06-27
pjp
a_count = 0;
0471
2015-06-27
pjp
0472
2019-02-15
pjp
rrp = TAILQ_FIRST(&rrset->rr_head);
0473
2019-02-27
pjp
if (rrp == 0)
0474
2019-02-15
pjp
return -1;
0475
2019-02-15
pjp
0476
2019-02-15
pjp
saltlen = ((struct nsec3param *)rrp->rdata)->saltlen;
0477
2019-02-15
pjp
if ((outlen + sizeof(struct answer) + saltlen ) > replysize) {
0478
2015-06-27
pjp
NTOHS(odh->query);
0479
2015-06-27
pjp
SET_DNS_TRUNCATION(odh);
0480
2015-06-27
pjp
HTONS(odh->query);
0481
2017-09-18
pjp
odh->answer = 0;
0482
2017-09-18
pjp
odh->nsrr = 0;
0483
2017-09-18
pjp
odh->additional = 0;
0484
2017-09-18
pjp
outlen = rollback;
0485
2015-06-27
pjp
goto out;
0486
2015-06-27
pjp
}
0487
2015-06-27
pjp
0488
2019-02-15
pjp
0489
2019-02-15
pjp
0490
2015-06-27
pjp
/*
0491
2015-06-27
pjp
* answer->name is a pointer to the request (0xc00c)
0492
2015-06-27
pjp
*/
0493
2015-06-27
pjp
0494
2015-06-27
pjp
answer->name[0] = 0xc0; /* 1 byte */
0495
2015-06-27
pjp
answer->name[1] = 0x0c; /* 2 bytes */
0496
2015-06-27
pjp
answer->type = q->hdr->qtype; /* 4 bytes */
0497
2015-06-27
pjp
answer->class = q->hdr->qclass; /* 6 bytes */
0498
2019-02-15
pjp
0499
2020-07-06
pjp
if (q->aa)
0500
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl);
0501
2020-07-06
pjp
else
0502
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl - (MIN(rrset->ttl, difftime(now, rrset->created))));
0503
2020-07-06
pjp
0504
2019-02-15
pjp
answer->rdlength = htons(((struct nsec3param *)rrp->rdata)->saltlen + 5); /* 5 = rest */
0505
2015-06-27
pjp
0506
2019-02-15
pjp
answer->algorithm = ((struct nsec3param *)rrp->rdata)->algorithm;
0507
2019-02-15
pjp
answer->flags = ((struct nsec3param *)rrp->rdata)->flags;
0508
2019-02-15
pjp
answer->iterations = htons(((struct nsec3param *)rrp->rdata)->iterations);
0509
2019-02-15
pjp
answer->saltlen = saltlen;
0510
2015-06-27
pjp
outlen += sizeof(struct answer);
0511
2015-06-27
pjp
0512
2019-02-15
pjp
if (saltlen) {
0513
2019-02-15
pjp
memcpy(&reply[outlen],
0514
2019-02-15
pjp
&((struct nsec3param*)rrp->rdata)->salt,
0515
2019-02-15
pjp
saltlen);
0516
2019-02-15
pjp
0517
2019-02-15
pjp
outlen += saltlen;
0518
2015-06-27
pjp
}
0519
2015-06-27
pjp
0520
2015-06-27
pjp
a_count++;
0521
2015-06-27
pjp
0522
2015-06-27
pjp
/* set new offset for answer */
0523
2015-06-27
pjp
answer = (struct answer *)&reply[outlen];
0524
2015-06-27
pjp
0525
2015-06-27
pjp
0526
2015-06-27
pjp
/* Add RRSIG reply_nsec3 */
0527
2019-11-11
pjp
if (dnssec && q->dnssecok && (rbt->flags & RBT_DNSSEC)) {
0528
2015-06-27
pjp
int tmplen = 0;
0529
2015-06-27
pjp
int origlen = outlen;
0530
2020-04-01
pjp
int retcount;
0531
2015-06-27
pjp
0532
2020-07-06
pjp
tmplen = additional_rrsig(q->hdr->name, q->hdr->namelen, DNS_TYPE_NSEC3PARAM, rbt, reply, replysize, outlen, &retcount, q->aa);
0533
2015-06-27
pjp
0534
2018-02-27
pjp
if (tmplen == 0) {
0535
2020-07-08
pjp
/* we're forwarding and had no RRSIG return with -1 */
0536
2020-07-08
pjp
if (q->aa != 1)
0537
2020-07-08
pjp
return -1;
0538
2020-07-08
pjp
0539
2018-02-27
pjp
NTOHS(odh->query);
0540
2018-02-27
pjp
SET_DNS_TRUNCATION(odh);
0541
2018-02-27
pjp
HTONS(odh->query);
0542
2018-02-27
pjp
outlen = rollback;
0543
2018-02-27
pjp
odh->answer = 0;
0544
2018-02-27
pjp
odh->nsrr = 0;
0545
2018-02-27
pjp
odh->additional = 0;
0546
2018-02-27
pjp
goto out;
0547
2015-06-27
pjp
}
0548
2015-06-27
pjp
0549
2018-02-27
pjp
outlen = tmplen;
0550
2018-02-27
pjp
0551
2018-02-27
pjp
if (outlen > origlen)
0552
2020-04-01
pjp
odh->answer = htons(a_count + retcount + 1);
0553
2015-11-14
pjp
}
0554
2015-11-14
pjp
0555
2015-11-14
pjp
out:
0556
2015-06-27
pjp
if (q->edns0len) {
0557
2015-06-27
pjp
/* tag on edns0 opt record */
0558
2015-06-27
pjp
odh->additional = htons(1);
0559
2015-06-27
pjp
outlen = additional_opt(q, reply, replysize, outlen);
0560
2015-06-27
pjp
}
0561
2015-06-27
pjp
0562
2019-02-24
pjp
if (q->tsig.tsigverified == 1) {
0563
2021-01-07
pjp
outlen = additional_tsig(q, reply, replysize, outlen, 0, 0, NULL, DEFAULT_TSIG_FUDGE);
0564
2019-02-24
pjp
0565
2019-02-24
pjp
NTOHS(odh->additional);
0566
2019-02-24
pjp
odh->additional++;
0567
2019-02-24
pjp
HTONS(odh->additional);
0568
2019-02-24
pjp
}
0569
2019-02-24
pjp
0570
2019-02-24
pjp
0571
2015-12-19
pjp
if (istcp) {
0572
2015-12-19
pjp
char *tmpbuf;
0573
2015-12-19
pjp
0574
2015-12-19
pjp
tmpbuf = malloc(outlen + 2);
0575
2019-02-27
pjp
if (tmpbuf == 0) {
0576
2015-12-19
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
0577
2015-12-19
pjp
}
0578
2019-12-03
pjp
pack16(tmpbuf, htons(outlen));
0579
2015-12-19
pjp
memcpy(&tmpbuf[2], reply, outlen);
0580
2015-12-19
pjp
0581
2015-12-19
pjp
if ((retlen = send(so, tmpbuf, outlen + 2, 0)) < 0) {
0582
2015-12-19
pjp
dolog(LOG_INFO, "send: %s\n", strerror(errno));
0583
2015-12-19
pjp
}
0584
2015-12-19
pjp
free(tmpbuf);
0585
2015-12-19
pjp
} else {
0586
2020-07-21
pjp
if (q->rawsocket) {
0587
2020-07-21
pjp
*sretlen = retlen = outlen;
0588
2020-07-21
pjp
} else {
0589
2020-07-21
pjp
if ((retlen = sendto(so, reply, outlen, 0, sa, salen)) < 0) {
0590
2020-07-21
pjp
dolog(LOG_INFO, "sendto: %s\n", strerror(errno));
0591
2020-07-21
pjp
}
0592
2015-06-27
pjp
}
0593
2015-12-19
pjp
}
0594
2015-06-27
pjp
0595
2015-06-25
pjp
return (retlen);
0596
2015-06-25
pjp
}
0597
2015-06-25
pjp
0598
2015-06-25
pjp
0599
2015-06-25
pjp
/*
0600
2015-06-27
pjp
* REPLY_NSEC3() - replies a DNS question (*q) on socket (so)
0601
2015-06-25
pjp
*
0602
2015-06-25
pjp
*/
0603
2015-06-25
pjp
0604
2015-06-25
pjp
int
0605
2020-07-21
pjp
reply_nsec3(struct sreply *sreply, int *sretlen, ddDB *db)
0606
2015-06-25
pjp
{
0607
2015-06-25
pjp
char *reply = sreply->replybuf;
0608
2015-06-25
pjp
struct dns_header *odh;
0609
2015-06-25
pjp
u_int16_t outlen = 0;
0610
2015-06-25
pjp
int a_count;
0611
2015-06-25
pjp
0612
2015-06-25
pjp
struct answer {
0613
2015-06-25
pjp
char name[2];
0614
2015-06-25
pjp
u_int16_t type;
0615
2015-06-25
pjp
u_int16_t class;
0616
2015-06-25
pjp
u_int32_t ttl;
0617
2015-06-25
pjp
u_int16_t rdlength; /* 12 */
0618
2015-06-27
pjp
u_int8_t algorithm;
0619
2015-06-27
pjp
u_int8_t flags;
0620
2015-06-27
pjp
u_int16_t iterations;
0621
2015-06-27
pjp
u_int8_t saltlen;
0622
2015-06-25
pjp
} __attribute__((packed));
0623
2015-06-25
pjp
0624
2015-06-25
pjp
struct answer *answer;
0625
2015-06-25
pjp
0626
2015-06-25
pjp
int so = sreply->so;
0627
2015-06-25
pjp
char *buf = sreply->buf;
0628
2015-06-25
pjp
int len = sreply->len;
0629
2015-06-25
pjp
struct question *q = sreply->q;
0630
2015-06-25
pjp
struct sockaddr *sa = sreply->sa;
0631
2015-06-25
pjp
int salen = sreply->salen;
0632
2019-02-15
pjp
struct rbtree *rbt = sreply->rbt1;
0633
2019-02-15
pjp
struct rrset *rrset = NULL;
0634
2019-02-15
pjp
struct rr *rrp = NULL;
0635
2015-06-25
pjp
0636
2015-06-25
pjp
int istcp = sreply->istcp;
0637
2015-06-25
pjp
int replysize = 512;
0638
2015-06-25
pjp
int retlen = -1;
0639
2017-09-18
pjp
u_int16_t rollback;
0640
2015-06-27
pjp
u_int8_t *somelen;
0641
2019-02-15
pjp
int bitmaplen, saltlen, nextlen;
0642
2015-06-25
pjp
0643
2020-07-06
pjp
time_t now;
0644
2021-01-05
pjp
uint32_t zonenumberx;
0645
2021-01-05
pjp
struct zoneentry *res = NULL;
0646
2020-07-06
pjp
0647
2021-01-07
pjp
if (rbt->flags & RBT_CACHE) {
0648
2021-01-05
pjp
zonenumberx = (uint32_t)-1;
0649
2021-01-07
pjp
} else {
0650
2021-01-07
pjp
res = zone_findzone(rbt);
0651
2021-01-07
pjp
if (res != NULL)
0652
2021-01-07
pjp
zonenumberx = res->zonenumber;
0653
2021-01-07
pjp
else
0654
2021-01-07
pjp
zonenumberx = (uint32_t)-1;
0655
2021-01-07
pjp
}
0656
2021-01-05
pjp
0657
2020-07-06
pjp
now = time(NULL);
0658
2020-07-06
pjp
0659
2019-02-27
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_A)) == 0)
0660
2015-06-25
pjp
return -1;
0661
2015-06-25
pjp
0662
2015-06-25
pjp
if (istcp) {
0663
2015-06-25
pjp
replysize = 65535;
0664
2015-06-25
pjp
}
0665
2015-06-25
pjp
0666
2015-06-25
pjp
if (!istcp && q->edns0len > 512)
0667
2020-09-30
pjp
replysize = MIN(q->edns0len, max_udp_payload);
0668
2015-06-25
pjp
0669
2015-07-01
pjp
0670
2015-07-01
pjp
/* RFC 5155 section 7.2.8 */
0671
2019-02-15
pjp
/* perhaps we are accompanied by an rrsig */
0672
2019-02-15
pjp
if (find_rr(rbt, DNS_TYPE_NSEC3) && find_rr(rbt, DNS_TYPE_RRSIG)) {
0673
2020-07-21
pjp
return (reply_nxdomain(sreply, sretlen, db));
0674
2015-07-01
pjp
}
0675
2015-06-25
pjp
odh = (struct dns_header *)&reply[0];
0676
2015-06-25
pjp
0677
2015-06-25
pjp
outlen = sizeof(struct dns_header);
0678
2015-06-25
pjp
0679
2015-06-25
pjp
if (len > replysize) {
0680
2015-06-25
pjp
return (retlen);
0681
2015-06-25
pjp
}
0682
2015-06-25
pjp
0683
2015-06-25
pjp
memcpy(reply, buf, sizeof(struct dns_header) + q->hdr->namelen + 4);
0684
2015-06-25
pjp
memset((char *)&odh->query, 0, sizeof(u_int16_t));
0685
2015-06-25
pjp
0686
2015-06-25
pjp
outlen += (q->hdr->namelen + 4);
0687
2017-09-18
pjp
rollback = outlen;
0688
2015-06-25
pjp
0689
2020-07-20
pjp
set_reply_flags(rbt, odh, q);
0690
2015-06-25
pjp
0691
2015-06-24
pjp
odh->question = htons(1);
0692
2015-06-25
pjp
odh->answer = htons(1);
0693
2015-06-24
pjp
odh->nsrr = 0;
0694
2015-06-24
pjp
odh->additional = 0;
0695
2015-06-24
pjp
0696
2015-06-24
pjp
/* skip dns header, question name, qtype and qclass */
0697
2015-06-24
pjp
answer = (struct answer *)(&reply[0] + sizeof(struct dns_header) +
0698
2015-06-24
pjp
q->hdr->namelen + 4);
0699
2015-06-24
pjp
0700
2015-06-24
pjp
a_count = 0;
0701
2015-06-24
pjp
0702
2019-02-15
pjp
rrp = TAILQ_FIRST(&rrset->rr_head);
0703
2019-02-27
pjp
if (rrp == 0)
0704
2019-02-15
pjp
return -1;
0705
2019-02-15
pjp
0706
2019-02-15
pjp
saltlen = ((struct nsec3 *)rrp->rdata)->saltlen;
0707
2019-02-15
pjp
bitmaplen = ((struct nsec3 *)rrp->rdata)->bitmap_len;
0708
2019-02-15
pjp
nextlen = ((struct nsec3 *)rrp->rdata)->nextlen;
0709
2019-02-15
pjp
0710
2019-02-15
pjp
if ((outlen + sizeof(struct answer) +
0711
2019-02-15
pjp
nextlen + saltlen + 1 + bitmaplen) > replysize) {
0712
2015-06-25
pjp
NTOHS(odh->query);
0713
2015-06-25
pjp
SET_DNS_TRUNCATION(odh);
0714
2015-06-25
pjp
HTONS(odh->query);
0715
2017-09-18
pjp
odh->answer = 0;
0716
2017-09-18
pjp
odh->nsrr = 0;
0717
2017-09-18
pjp
odh->additional = 0;
0718
2017-09-18
pjp
outlen = rollback;
0719
2015-06-25
pjp
goto out;
0720
2015-06-25
pjp
}
0721
2015-06-25
pjp
0722
2015-06-25
pjp
/*
0723
2015-06-25
pjp
* answer->name is a pointer to the request (0xc00c)
0724
2015-06-25
pjp
*/
0725
2015-06-25
pjp
0726
2015-06-25
pjp
answer->name[0] = 0xc0; /* 1 byte */
0727
2015-06-25
pjp
answer->name[1] = 0x0c; /* 2 bytes */
0728
2015-06-25
pjp
answer->type = q->hdr->qtype; /* 4 bytes */
0729
2015-06-25
pjp
answer->class = q->hdr->qclass; /* 6 bytes */
0730
2020-07-06
pjp
0731
2020-07-06
pjp
if (q->aa)
0732
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl); /* 10 b */
0733
2020-07-06
pjp
else
0734
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl - (MIN(rrset->ttl, difftime(now, rrset->created))));
0735
2019-02-15
pjp
0736
2019-02-15
pjp
answer->rdlength = htons(nextlen + bitmaplen + saltlen + 6); /* 6 = rest */
0737
2015-06-25
pjp
0738
2019-02-15
pjp
answer->algorithm = ((struct nsec3 *)rrp->rdata)->algorithm;
0739
2019-02-15
pjp
answer->flags = ((struct nsec3 *)rrp->rdata)->flags;
0740
2019-02-15
pjp
answer->iterations = htons(((struct nsec3 *)rrp->rdata)->iterations);
0741
2019-02-15
pjp
answer->saltlen = saltlen;
0742
2015-06-25
pjp
outlen += sizeof(struct answer);
0743
2015-06-27
pjp
0744
2019-02-15
pjp
if (saltlen) {
0745
2019-02-15
pjp
memcpy(&reply[outlen],
0746
2019-02-15
pjp
(char *)&((struct nsec3 *)rrp->rdata)->salt,
0747
2019-02-15
pjp
saltlen);
0748
2019-02-15
pjp
0749
2019-02-15
pjp
outlen += saltlen;
0750
2015-06-27
pjp
}
0751
2015-06-25
pjp
0752
2015-06-27
pjp
somelen = (u_int8_t *)&reply[outlen];
0753
2019-02-15
pjp
*somelen = nextlen;
0754
2015-06-25
pjp
0755
2015-06-27
pjp
outlen += 1;
0756
2015-06-25
pjp
0757
2019-02-15
pjp
memcpy(&reply[outlen], ((struct nsec3 *)rrp->rdata)->next, nextlen);
0758
2015-06-27
pjp
0759
2019-02-15
pjp
outlen += nextlen;
0760
2015-06-27
pjp
0761
2019-02-15
pjp
memcpy(&reply[outlen], ((struct nsec3 *)rrp->rdata)->bitmap, bitmaplen);
0762
2019-02-15
pjp
outlen += bitmaplen;
0763
2015-06-27
pjp
0764
2015-06-25
pjp
a_count++;
0765
2015-06-25
pjp
0766
2015-06-25
pjp
/* set new offset for answer */
0767
2015-06-25
pjp
answer = (struct answer *)&reply[outlen];
0768
2015-06-25
pjp
0769
2015-06-25
pjp
0770
2015-06-27
pjp
/* Add RRSIG reply_nsec3 */
0771
2019-11-11
pjp
if (dnssec && q->dnssecok && (rbt->flags & RBT_DNSSEC)) {
0772
2015-06-25
pjp
int tmplen = 0;
0773
2015-06-25
pjp
int origlen = outlen;
0774
2020-04-01
pjp
int retcount;
0775
2015-06-25
pjp
0776
2020-07-06
pjp
tmplen = additional_rrsig(q->hdr->name, q->hdr->namelen, DNS_TYPE_NSEC3, rbt, reply, replysize, outlen, &retcount, q->aa);
0777
2018-02-27
pjp
0778
2018-02-27
pjp
if (tmplen == 0) {
0779
2020-07-08
pjp
/* we're forwarding and had no RRSIG return with -1 */
0780
2020-07-08
pjp
if (q->aa != 1)
0781
2020-07-08
pjp
return -1;
0782
2020-07-08
pjp
0783
2018-02-27
pjp
NTOHS(odh->query);
0784
2018-02-27
pjp
SET_DNS_TRUNCATION(odh);
0785
2018-02-27
pjp
HTONS(odh->query);
0786
2018-02-27
pjp
odh->answer = 0;
0787
2018-02-27
pjp
odh->nsrr = 0;
0788
2018-02-27
pjp
odh->additional = 0;
0789
2018-02-27
pjp
outlen = rollback;
0790
2018-02-27
pjp
goto out;
0791
2018-02-27
pjp
}
0792
2018-02-27
pjp
0793
2018-02-27
pjp
outlen = tmplen;
0794
2018-02-27
pjp
0795
2018-02-27
pjp
if (outlen > origlen)
0796
2020-04-01
pjp
odh->answer = htons(a_count + retcount + 1);
0797
2018-02-27
pjp
}
0798
2018-02-27
pjp
0799
2018-02-27
pjp
out:
0800
2018-02-27
pjp
if (q->edns0len) {
0801
2018-02-27
pjp
/* tag on edns0 opt record */
0802
2018-02-27
pjp
odh->additional = htons(1);
0803
2018-02-27
pjp
outlen = additional_opt(q, reply, replysize, outlen);
0804
2018-02-27
pjp
}
0805
2018-02-27
pjp
0806
2019-02-24
pjp
if (q->tsig.tsigverified == 1) {
0807
2021-01-07
pjp
outlen = additional_tsig(q, reply, replysize, outlen, 0, 0, NULL, DEFAULT_TSIG_FUDGE);
0808
2019-02-24
pjp
0809
2019-02-24
pjp
NTOHS(odh->additional);
0810
2019-02-24
pjp
odh->additional++;
0811
2019-02-24
pjp
HTONS(odh->additional);
0812
2019-02-24
pjp
}
0813
2019-02-24
pjp
0814
2018-02-27
pjp
if (istcp) {
0815
2018-02-27
pjp
char *tmpbuf;
0816
2018-02-27
pjp
0817
2018-02-27
pjp
tmpbuf = malloc(outlen + 2);
0818
2019-02-27
pjp
if (tmpbuf == 0) {
0819
2018-02-27
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
0820
2018-02-27
pjp
}
0821
2019-12-03
pjp
pack16(tmpbuf, htons(outlen));
0822
2019-12-03
pjp
memcpy(&tmpbuf[2], reply, outlen);
0823
2019-12-03
pjp
0824
2019-12-03
pjp
if ((retlen = send(so, tmpbuf, outlen + 2, 0)) < 0) {
0825
2019-12-03
pjp
dolog(LOG_INFO, "send: %s\n", strerror(errno));
0826
2019-12-03
pjp
}
0827
2019-12-03
pjp
free(tmpbuf);
0828
2019-12-03
pjp
} else {
0829
2020-07-21
pjp
if (q->rawsocket) {
0830
2020-07-21
pjp
*sretlen = retlen = outlen;
0831
2020-07-21
pjp
} else {
0832
2020-07-21
pjp
if ((retlen = sendto(so, reply, outlen, 0, sa, salen)) < 0) {
0833
2020-07-21
pjp
dolog(LOG_INFO, "sendto: %s\n", strerror(errno));
0834
2020-07-21
pjp
}
0835
2019-12-03
pjp
}
0836
2019-12-03
pjp
}
0837
2019-12-03
pjp
0838
2019-12-03
pjp
return (retlen);
0839
2019-12-03
pjp
}
0840
2019-12-03
pjp
0841
2020-07-23
pjp
/*
0842
2020-07-23
pjp
* REPLY_CAA
0843
2020-07-23
pjp
*/
0844
2020-07-23
pjp
0845
2020-07-23
pjp
int
0846
2020-07-23
pjp
reply_caa(struct sreply *sreply, int *sretlen, ddDB *db)
0847
2020-07-23
pjp
{
0848
2020-07-23
pjp
char *reply = sreply->replybuf;
0849
2020-07-23
pjp
struct dns_header *odh;
0850
2020-07-23
pjp
u_int16_t outlen = 0;
0851
2020-07-23
pjp
int caa_count;
0852
2020-07-23
pjp
0853
2020-07-23
pjp
struct answer {
0854
2020-07-23
pjp
char name[2];
0855
2020-07-23
pjp
u_int16_t type;
0856
2020-07-23
pjp
u_int16_t class;
0857
2020-07-23
pjp
u_int32_t ttl;
0858
2020-07-23
pjp
u_int16_t rdlength; /* 12 */
0859
2020-07-23
pjp
} __attribute__((packed));
0860
2020-07-23
pjp
0861
2020-07-23
pjp
struct answer *answer;
0862
2020-07-23
pjp
0863
2020-07-23
pjp
int so = sreply->so;
0864
2020-07-23
pjp
char *buf = sreply->buf;
0865
2020-07-23
pjp
int len = sreply->len;
0866
2020-07-23
pjp
struct question *q = sreply->q;
0867
2020-07-23
pjp
struct sockaddr *sa = sreply->sa;
0868
2020-07-23
pjp
int salen = sreply->salen;
0869
2020-07-23
pjp
struct rbtree *rbt = sreply->rbt1;
0870
2020-07-23
pjp
struct rrset *rrset = NULL;
0871
2020-07-23
pjp
struct rr *rrp = NULL;
0872
2020-07-23
pjp
0873
2020-07-23
pjp
int istcp = sreply->istcp;
0874
2020-07-23
pjp
int replysize = 512;
0875
2020-07-23
pjp
int retlen = -1;
0876
2020-07-23
pjp
u_int16_t rollback;
0877
2020-07-23
pjp
int valuelen, taglen;
0878
2020-07-23
pjp
time_t now;
0879
2021-01-05
pjp
uint32_t zonenumberx;
0880
2021-01-05
pjp
struct zoneentry *res = NULL;
0881
2020-07-23
pjp
0882
2021-01-07
pjp
if (rbt->flags & RBT_CACHE) {
0883
2021-01-05
pjp
zonenumberx = (uint32_t)-1;
0884
2021-01-07
pjp
} else {
0885
2021-01-07
pjp
res = zone_findzone(rbt);
0886
2021-01-07
pjp
if (res != NULL)
0887
2021-01-07
pjp
zonenumberx = res->zonenumber;
0888
2021-01-07
pjp
else
0889
2021-01-07
pjp
zonenumberx = (uint32_t)-1;
0890
2021-01-07
pjp
}
0891
2021-01-05
pjp
0892
2020-07-23
pjp
0893
2020-07-23
pjp
now = time(NULL);
0894
2020-07-23
pjp
0895
2020-07-23
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_CAA)) == 0)
0896
2020-07-23
pjp
return -1;
0897
2020-07-23
pjp
0898
2020-07-23
pjp
if (istcp) {
0899
2020-07-23
pjp
replysize = 65535;
0900
2020-07-23
pjp
}
0901
2020-07-23
pjp
0902
2020-07-23
pjp
if (!istcp && q->edns0len > 512)
0903
2020-09-30
pjp
replysize = MIN(q->edns0len, max_udp_payload);
0904
2020-07-23
pjp
0905
2020-07-23
pjp
odh = (struct dns_header *)&reply[0];
0906
2020-07-23
pjp
0907
2020-07-23
pjp
outlen = sizeof(struct dns_header);
0908
2020-07-23
pjp
0909
2020-07-23
pjp
if (len > replysize) {
0910
2020-07-23
pjp
return (retlen);
0911
2020-07-23
pjp
}
0912
2020-07-23
pjp
0913
2020-07-23
pjp
memcpy(reply, buf, sizeof(struct dns_header) + q->hdr->namelen + 4);
0914
2020-07-23
pjp
0915
2020-07-23
pjp
outlen += (q->hdr->namelen + 4);
0916
2020-07-23
pjp
rollback = outlen;
0917
2020-07-23
pjp
0918
2020-07-23
pjp
memset((char *)&odh->query, 0, sizeof(u_int16_t));
0919
2020-07-23
pjp
set_reply_flags(rbt, odh, q);
0920
2020-07-23
pjp
0921
2020-07-23
pjp
odh->question = htons(1);
0922
2020-07-23
pjp
odh->answer = htons(1);
0923
2020-07-23
pjp
odh->nsrr = 0;
0924
2020-07-23
pjp
odh->additional = 0;
0925
2020-07-23
pjp
0926
2020-07-23
pjp
/* skip dns header, question name, qtype and qclass */
0927
2020-07-23
pjp
answer = (struct answer *)(&reply[0] + sizeof(struct dns_header) +
0928
2020-07-23
pjp
q->hdr->namelen + 4);
0929
2020-07-23
pjp
0930
2020-07-23
pjp
caa_count = 0;
0931
2020-07-23
pjp
0932
2020-07-23
pjp
TAILQ_FOREACH(rrp, &rrset->rr_head, entries) {
0933
2021-01-05
pjp
if (rrp->zonenumber != zonenumberx)
0934
2021-01-05
pjp
continue;
0935
2020-07-23
pjp
if ((outlen + sizeof(struct answer) + 2 + \
0936
2020-07-23
pjp
((struct caa *)rrp->rdata)->taglen + \
0937
2020-07-23
pjp
((struct caa *)rrp->rdata)->valuelen) > replysize) {
0938
2020-07-23
pjp
0939
2020-07-23
pjp
NTOHS(odh->query);
0940
2020-07-23
pjp
SET_DNS_TRUNCATION(odh);
0941
2020-07-23
pjp
HTONS(odh->query);
0942
2020-07-23
pjp
odh->answer = 0;
0943
2020-07-23
pjp
odh->nsrr = 0;
0944
2020-07-23
pjp
odh->additional = 0;
0945
2020-07-23
pjp
outlen = rollback;
0946
2020-07-23
pjp
goto out;
0947
2020-07-23
pjp
}
0948
2020-07-23
pjp
0949
2020-07-23
pjp
answer->name[0] = 0xc0; /* 1 byte */
0950
2020-07-23
pjp
answer->name[1] = 0x0c; /* 2 bytes */
0951
2020-07-23
pjp
answer->type = q->hdr->qtype; /* 4 bytes */
0952
2020-07-23
pjp
answer->class = q->hdr->qclass; /* 6 bytes */
0953
2020-07-23
pjp
0954
2020-07-23
pjp
if (q->aa)
0955
2020-07-23
pjp
answer->ttl = htonl(rrset->ttl); /* 10 b */
0956
2020-07-23
pjp
else
0957
2020-07-23
pjp
answer->ttl = htonl(rrset->ttl - (MIN(rrset->ttl, difftime(now, rrset->created))));
0958
2020-07-23
pjp
0959
2020-07-23
pjp
0960
2020-07-23
pjp
caa_count++;
0961
2020-07-23
pjp
outlen += 12;
0962
2020-07-23
pjp
0963
2020-07-23
pjp
taglen = ((struct caa *)rrp->rdata)->taglen;
0964
2020-07-23
pjp
valuelen = ((struct caa *)rrp->rdata)->valuelen;
0965
2020-07-23
pjp
0966
2020-07-23
pjp
pack8(&reply[outlen++], ((struct caa *)rrp->rdata)->flags);
0967
2020-07-23
pjp
pack8(&reply[outlen++], taglen);
0968
2020-07-23
pjp
pack(&reply[outlen], ((struct caa *)rrp->rdata)->tag, taglen);
0969
2020-07-23
pjp
outlen += taglen;
0970
2020-07-23
pjp
pack(&reply[outlen],((struct caa *)rrp->rdata)->value,valuelen);
0971
2020-07-23
pjp
outlen += valuelen;
0972
2020-07-23
pjp
0973
2020-07-23
pjp
answer->rdlength = htons(2 + taglen + valuelen);
0974
2020-07-23
pjp
0975
2020-07-23
pjp
/* set new offset for answer */
0976
2020-07-23
pjp
answer = (struct answer *)&reply[outlen];
0977
2020-07-23
pjp
}
0978
2020-07-23
pjp
0979
2020-07-23
pjp
odh->answer = htons(caa_count);
0980
2020-07-23
pjp
0981
2020-07-23
pjp
0982
2020-07-23
pjp
/* Add RRSIG reply_nsec */
0983
2020-07-23
pjp
if (dnssec && q->dnssecok && (rbt->flags & RBT_DNSSEC)) {
0984
2020-07-23
pjp
int tmplen = 0;
0985
2020-07-23
pjp
int origlen = outlen;
0986
2020-07-23
pjp
int retcount;
0987
2020-07-23
pjp
0988
2020-07-23
pjp
tmplen = additional_rrsig(q->hdr->name, q->hdr->namelen, DNS_TYPE_CAA, rbt, reply, replysize, outlen, &retcount, q->aa);
0989
2020-07-23
pjp
0990
2020-07-23
pjp
if (tmplen == 0) {
0991
2020-07-23
pjp
0992
2020-07-23
pjp
/* we're forwarding and had no RRSIG return with -1 */
0993
2020-07-23
pjp
if (q->aa != 1)
0994
2020-07-23
pjp
return -1;
0995
2020-07-23
pjp
0996
2020-07-23
pjp
NTOHS(odh->query);
0997
2020-07-23
pjp
SET_DNS_TRUNCATION(odh);
0998
2020-07-23
pjp
HTONS(odh->query);
0999
2020-07-23
pjp
odh->answer = 0;
1000
2020-07-23
pjp
odh->nsrr = 0;
1001
2020-07-23
pjp
odh->additional = 0;
1002
2020-07-23
pjp
outlen = rollback;
1003
2020-07-23
pjp
goto out;
1004
2020-07-23
pjp
}
1005
2020-07-23
pjp
1006
2020-07-23
pjp
outlen = tmplen;
1007
2020-07-23
pjp
1008
2020-07-23
pjp
if (outlen > origlen)
1009
2020-07-23
pjp
odh->answer = htons(caa_count + retcount);
1010
2020-07-23
pjp
1011
2020-07-23
pjp
}
1012
2020-07-23
pjp
1013
2020-07-23
pjp
out:
1014
2020-07-23
pjp
if (q->edns0len) {
1015
2020-07-23
pjp
/* tag on edns0 opt record */
1016
2020-07-23
pjp
odh->additional = htons(1);
1017
2020-07-23
pjp
outlen = additional_opt(q, reply, replysize, outlen);
1018
2020-07-23
pjp
}
1019
2020-07-23
pjp
1020
2020-07-23
pjp
if (q->tsig.tsigverified == 1) {
1021
2021-01-07
pjp
outlen = additional_tsig(q, reply, replysize, outlen, 0, 0, NULL, DEFAULT_TSIG_FUDGE);
1022
2020-07-23
pjp
1023
2020-07-23
pjp
NTOHS(odh->additional);
1024
2020-07-23
pjp
odh->additional++;
1025
2020-07-23
pjp
HTONS(odh->additional);
1026
2020-07-23
pjp
}
1027
2020-07-23
pjp
1028
2020-07-23
pjp
if (istcp) {
1029
2020-07-23
pjp
char *tmpbuf;
1030
2020-07-23
pjp
1031
2020-07-23
pjp
tmpbuf = malloc(outlen + 2);
1032
2020-07-23
pjp
if (tmpbuf == 0) {
1033
2020-07-23
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
1034
2020-07-23
pjp
}
1035
2020-07-23
pjp
pack16(tmpbuf, htons(outlen));
1036
2020-07-23
pjp
memcpy(&tmpbuf[2], reply, outlen);
1037
2020-07-23
pjp
1038
2020-07-23
pjp
if ((retlen = send(so, tmpbuf, outlen + 2, 0)) < 0) {
1039
2020-07-23
pjp
dolog(LOG_INFO, "send: %s\n", strerror(errno));
1040
2020-07-23
pjp
}
1041
2020-07-23
pjp
free(tmpbuf);
1042
2020-07-23
pjp
} else {
1043
2020-07-23
pjp
if (q->rawsocket) {
1044
2020-07-23
pjp
*sretlen = retlen = outlen;
1045
2020-07-23
pjp
} else {
1046
2020-07-23
pjp
if ((retlen = sendto(so, reply, outlen, 0, sa, salen)) < 0) {
1047
2020-07-23
pjp
dolog(LOG_INFO, "sendto: %s\n", strerror(errno));
1048
2020-07-23
pjp
}
1049
2020-07-23
pjp
}
1050
2020-07-23
pjp
}
1051
2020-07-23
pjp
1052
2020-07-23
pjp
1053
2020-07-23
pjp
return (retlen);
1054
2020-07-23
pjp
}
1055
2020-07-23
pjp
1056
2020-07-23
pjp
int
1057
2020-07-23
pjp
reply_hinfo(struct sreply *sreply, int *sretlen, ddDB *db)
1058
2020-07-23
pjp
{
1059
2020-07-23
pjp
char *reply = sreply->replybuf;
1060
2020-07-23
pjp
struct dns_header *odh;
1061
2020-07-23
pjp
u_int16_t outlen = 0;
1062
2020-07-23
pjp
int hinfo_count;
1063
2020-07-23
pjp
1064
2020-07-23
pjp
struct answer {
1065
2020-07-23
pjp
char name[2];
1066
2020-07-23
pjp
u_int16_t type;
1067
2020-07-23
pjp
u_int16_t class;
1068
2020-07-23
pjp
u_int32_t ttl;
1069
2020-07-23
pjp
u_int16_t rdlength; /* 12 */
1070
2020-07-23
pjp
} __attribute__((packed));
1071
2020-07-23
pjp
1072
2020-07-23
pjp
struct answer *answer;
1073
2020-07-23
pjp
1074
2020-07-23
pjp
int so = sreply->so;
1075
2020-07-23
pjp
char *buf = sreply->buf;
1076
2020-07-23
pjp
int len = sreply->len;
1077
2020-07-23
pjp
struct question *q = sreply->q;
1078
2020-07-23
pjp
struct sockaddr *sa = sreply->sa;
1079
2020-07-23
pjp
int salen = sreply->salen;
1080
2020-07-23
pjp
struct rbtree *rbt = sreply->rbt1;
1081
2020-07-23
pjp
struct rrset *rrset = NULL;
1082
2020-07-23
pjp
struct rr *rrp = NULL;
1083
2020-07-23
pjp
1084
2020-07-23
pjp
int istcp = sreply->istcp;
1085
2020-07-23
pjp
int replysize = 512;
1086
2020-07-23
pjp
int retlen = -1;
1087
2020-07-23
pjp
u_int16_t rollback;
1088
2020-07-23
pjp
time_t now;
1089
2021-01-05
pjp
uint32_t zonenumberx;
1090
2021-01-05
pjp
struct zoneentry *res = NULL;
1091
2020-07-23
pjp
1092
2021-01-07
pjp
if (rbt->flags & RBT_CACHE) {
1093
2021-01-05
pjp
zonenumberx = (uint32_t)-1;
1094
2021-01-07
pjp
} else {
1095
2021-01-07
pjp
res = zone_findzone(rbt);
1096
2021-01-07
pjp
if (res != NULL)
1097
2021-01-07
pjp
zonenumberx = res->zonenumber;
1098
2021-01-07
pjp
else
1099
2021-01-07
pjp
zonenumberx = (uint32_t)-1;
1100
2021-01-07
pjp
}
1101
2021-01-05
pjp
1102
2020-07-23
pjp
1103
2020-07-23
pjp
now = time(NULL);
1104
2020-07-23
pjp
1105
2020-07-23
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_HINFO)) == 0)
1106
2020-07-23
pjp
return -1;
1107
2020-07-23
pjp
1108
2020-07-23
pjp
if (istcp) {
1109
2020-07-23
pjp
replysize = 65535;
1110
2020-07-23
pjp
}
1111
2020-07-23
pjp
1112
2020-07-23
pjp
if (!istcp && q->edns0len > 512)
1113
2020-09-30
pjp
replysize = MIN(q->edns0len, max_udp_payload);
1114
2020-07-23
pjp
1115
2020-07-23
pjp
odh = (struct dns_header *)&reply[0];
1116
2020-07-23
pjp
1117
2020-07-23
pjp
outlen = sizeof(struct dns_header);
1118
2020-07-23
pjp
1119
2020-07-23
pjp
if (len > replysize) {
1120
2020-07-23
pjp
return (retlen);
1121
2020-07-23
pjp
}
1122
2020-07-23
pjp
1123
2020-07-23
pjp
memcpy(reply, buf, sizeof(struct dns_header) + q->hdr->namelen + 4);
1124
2020-07-23
pjp
1125
2020-07-23
pjp
outlen += (q->hdr->namelen + 4);
1126
2020-07-23
pjp
rollback = outlen;
1127
2020-07-23
pjp
1128
2020-07-23
pjp
memset((char *)&odh->query, 0, sizeof(u_int16_t));
1129
2020-07-23
pjp
set_reply_flags(rbt, odh, q);
1130
2020-07-23
pjp
1131
2020-07-23
pjp
odh->question = htons(1);
1132
2020-07-23
pjp
odh->answer = htons(1);
1133
2020-07-23
pjp
odh->nsrr = 0;
1134
2020-07-23
pjp
odh->additional = 0;
1135
2020-07-23
pjp
1136
2020-07-23
pjp
/* skip dns header, question name, qtype and qclass */
1137
2020-07-23
pjp
answer = (struct answer *)(&reply[0] + sizeof(struct dns_header) +
1138
2020-07-23
pjp
q->hdr->namelen + 4);
1139
2020-07-23
pjp
1140
2020-07-23
pjp
hinfo_count = 0;
1141
2020-07-23
pjp
1142
2020-07-23
pjp
TAILQ_FOREACH(rrp, &rrset->rr_head, entries) {
1143
2021-01-05
pjp
if (rrp->zonenumber != zonenumberx)
1144
2021-01-05
pjp
continue;
1145
2020-07-23
pjp
if ((outlen + sizeof(struct answer) + 2 + \
1146
2020-07-23
pjp
((struct hinfo *)rrp->rdata)->cpulen + \
1147
2020-07-23
pjp
((struct hinfo *)rrp->rdata)->oslen) > replysize) {
1148
2020-07-23
pjp
1149
2020-07-23
pjp
NTOHS(odh->query);
1150
2020-07-23
pjp
SET_DNS_TRUNCATION(odh);
1151
2020-07-23
pjp
HTONS(odh->query);
1152
2020-07-23
pjp
odh->answer = 0;
1153
2020-07-23
pjp
odh->nsrr = 0;
1154
2020-07-23
pjp
odh->additional = 0;
1155
2020-07-23
pjp
outlen = rollback;
1156
2020-07-23
pjp
goto out;
1157
2020-07-23
pjp
}
1158
2020-07-23
pjp
1159
2020-07-23
pjp
answer->name[0] = 0xc0; /* 1 byte */
1160
2020-07-23
pjp
answer->name[1] = 0x0c; /* 2 bytes */
1161
2020-07-23
pjp
answer->type = q->hdr->qtype; /* 4 bytes */
1162
2020-07-23
pjp
answer->class = q->hdr->qclass; /* 6 bytes */
1163
2020-07-23
pjp
1164
2020-07-23
pjp
if (q->aa)
1165
2020-07-23
pjp
answer->ttl = htonl(rrset->ttl); /* 10 b */
1166
2020-07-23
pjp
else
1167
2020-07-23
pjp
answer->ttl = htonl(rrset->ttl - (MIN(rrset->ttl, difftime(now, rrset->created))));
1168
2020-07-23
pjp
1169
2020-07-23
pjp
1170
2020-07-23
pjp
hinfo_count++;
1171
2020-07-23
pjp
outlen += 12;
1172
2020-07-23
pjp
1173
2020-07-23
pjp
pack8(&reply[outlen++], ((struct hinfo *)rrp->rdata)->cpulen);
1174
2020-07-23
pjp
pack(&reply[outlen], ((struct hinfo *)rrp->rdata)->cpu,
1175
2020-07-23
pjp
((struct hinfo *)rrp->rdata)->cpulen);
1176
2020-07-23
pjp
outlen += ((struct hinfo *)rrp->rdata)->cpulen;
1177
2020-07-23
pjp
pack8(&reply[outlen++], ((struct hinfo *)rrp->rdata)->oslen);
1178
2020-07-23
pjp
pack(&reply[outlen], ((struct hinfo *)rrp->rdata)->os,
1179
2020-07-23
pjp
((struct hinfo *)rrp->rdata)->oslen);
1180
2020-07-23
pjp
outlen += ((struct hinfo *)rrp->rdata)->oslen;
1181
2020-07-23
pjp
1182
2020-07-23
pjp
answer->rdlength = htons(2 + \
1183
2020-07-23
pjp
((struct hinfo *)rrp->rdata)->oslen + \
1184
2020-07-23
pjp
((struct hinfo *)rrp->rdata)->cpulen);
1185
2020-07-23
pjp
1186
2020-07-23
pjp
/* set new offset for answer */
1187
2020-07-23
pjp
answer = (struct answer *)&reply[outlen];
1188
2020-07-23
pjp
}
1189
2020-07-23
pjp
1190
2020-07-23
pjp
odh->answer = htons(hinfo_count);
1191
2020-07-23
pjp
1192
2020-07-23
pjp
1193
2020-07-23
pjp
/* Add RRSIG reply_nsec */
1194
2020-07-23
pjp
if (dnssec && q->dnssecok && (rbt->flags & RBT_DNSSEC)) {
1195
2020-07-23
pjp
int tmplen = 0;
1196
2020-07-23
pjp
int origlen = outlen;
1197
2020-07-23
pjp
int retcount;
1198
2020-07-23
pjp
1199
2020-07-23
pjp
tmplen = additional_rrsig(q->hdr->name, q->hdr->namelen, DNS_TYPE_HINFO, rbt, reply, replysize, outlen, &retcount, q->aa);
1200
2020-07-23
pjp
1201
2020-07-23
pjp
if (tmplen == 0) {
1202
2020-07-23
pjp
1203
2020-07-23
pjp
/* we're forwarding and had no RRSIG return with -1 */
1204
2020-07-23
pjp
if (q->aa != 1)
1205
2020-07-23
pjp
return -1;
1206
2020-07-23
pjp
1207
2020-07-23
pjp
NTOHS(odh->query);
1208
2020-07-23
pjp
SET_DNS_TRUNCATION(odh);
1209
2020-07-23
pjp
HTONS(odh->query);
1210
2020-07-23
pjp
odh->answer = 0;
1211
2020-07-23
pjp
odh->nsrr = 0;
1212
2020-07-23
pjp
odh->additional = 0;
1213
2020-07-23
pjp
outlen = rollback;
1214
2020-07-23
pjp
goto out;
1215
2020-07-23
pjp
}
1216
2020-07-23
pjp
1217
2020-07-23
pjp
outlen = tmplen;
1218
2020-07-23
pjp
1219
2020-07-23
pjp
if (outlen > origlen)
1220
2020-07-23
pjp
odh->answer = htons(hinfo_count + retcount);
1221
2020-07-23
pjp
1222
2020-07-23
pjp
}
1223
2020-07-23
pjp
1224
2020-07-23
pjp
out:
1225
2020-07-23
pjp
if (q->edns0len) {
1226
2020-07-23
pjp
/* tag on edns0 opt record */
1227
2020-07-23
pjp
odh->additional = htons(1);
1228
2020-07-23
pjp
outlen = additional_opt(q, reply, replysize, outlen);
1229
2020-07-23
pjp
}
1230
2020-07-23
pjp
1231
2020-07-23
pjp
if (q->tsig.tsigverified == 1) {
1232
2021-01-07
pjp
outlen = additional_tsig(q, reply, replysize, outlen, 0, 0, NULL, DEFAULT_TSIG_FUDGE);
1233
2020-07-23
pjp
1234
2020-07-23
pjp
NTOHS(odh->additional);
1235
2020-07-23
pjp
odh->additional++;
1236
2020-07-23
pjp
HTONS(odh->additional);
1237
2020-07-23
pjp
}
1238
2020-07-23
pjp
1239
2020-07-23
pjp
if (istcp) {
1240
2020-07-23
pjp
char *tmpbuf;
1241
2020-07-23
pjp
1242
2020-07-23
pjp
tmpbuf = malloc(outlen + 2);
1243
2020-07-23
pjp
if (tmpbuf == 0) {
1244
2020-07-23
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
1245
2020-07-23
pjp
}
1246
2020-07-23
pjp
pack16(tmpbuf, htons(outlen));
1247
2020-07-23
pjp
memcpy(&tmpbuf[2], reply, outlen);
1248
2020-07-23
pjp
1249
2020-07-23
pjp
if ((retlen = send(so, tmpbuf, outlen + 2, 0)) < 0) {
1250
2020-07-23
pjp
dolog(LOG_INFO, "send: %s\n", strerror(errno));
1251
2020-07-23
pjp
}
1252
2020-07-23
pjp
free(tmpbuf);
1253
2020-07-23
pjp
} else {
1254
2020-07-23
pjp
if (q->rawsocket) {
1255
2020-07-23
pjp
*sretlen = retlen = outlen;
1256
2020-07-23
pjp
} else {
1257
2020-07-23
pjp
if ((retlen = sendto(so, reply, outlen, 0, sa, salen)) < 0) {
1258
2020-07-23
pjp
dolog(LOG_INFO, "sendto: %s\n", strerror(errno));
1259
2020-07-23
pjp
}
1260
2020-07-23
pjp
}
1261
2020-07-23
pjp
}
1262
2020-07-23
pjp
1263
2020-07-23
pjp
1264
2020-07-23
pjp
return (retlen);
1265
2020-07-23
pjp
}
1266
2020-07-23
pjp
1267
2020-07-23
pjp
int
1268
2020-07-23
pjp
reply_rp(struct sreply *sreply, int *sretlen, ddDB *db)
1269
2020-07-23
pjp
{
1270
2020-07-23
pjp
char *reply = sreply->replybuf;
1271
2020-07-23
pjp
struct dns_header *odh;
1272
2020-07-23
pjp
u_int16_t outlen = 0;
1273
2020-07-23
pjp
int rp_count;
1274
2020-07-23
pjp
1275
2020-07-23
pjp
struct answer {
1276
2020-07-23
pjp
char name[2];
1277
2020-07-23
pjp
u_int16_t type;
1278
2020-07-23
pjp
u_int16_t class;
1279
2020-07-23
pjp
u_int32_t ttl;
1280
2020-07-23
pjp
u_int16_t rdlength; /* 12 */
1281
2020-07-23
pjp
} __attribute__((packed));
1282
2020-07-23
pjp
1283
2020-07-23
pjp
struct answer *answer;
1284
2020-07-23
pjp
1285
2020-07-23
pjp
int so = sreply->so;
1286
2020-07-23
pjp
char *buf = sreply->buf;
1287
2020-07-23
pjp
int len = sreply->len;
1288
2020-07-23
pjp
struct question *q = sreply->q;
1289
2020-07-23
pjp
struct sockaddr *sa = sreply->sa;
1290
2020-07-23
pjp
int salen = sreply->salen;
1291
2020-07-23
pjp
struct rbtree *rbt = sreply->rbt1;
1292
2020-07-23
pjp
struct rrset *rrset = NULL;
1293
2020-07-23
pjp
struct rr *rrp = NULL;
1294
2020-07-23
pjp
1295
2020-07-23
pjp
int istcp = sreply->istcp;
1296
2020-07-23
pjp
int replysize = 512;
1297
2020-07-23
pjp
int retlen = -1;
1298
2020-07-23
pjp
u_int16_t rollback;
1299
2020-07-23
pjp
time_t now;
1300
2021-01-05
pjp
uint32_t zonenumberx;
1301
2021-01-05
pjp
struct zoneentry *res = NULL;
1302
2020-07-23
pjp
1303
2021-01-07
pjp
if (rbt->flags & RBT_CACHE) {
1304
2021-01-05
pjp
zonenumberx = (uint32_t)-1;
1305
2021-01-07
pjp
} else {
1306
2021-01-07
pjp
res = zone_findzone(rbt);
1307
2021-01-07
pjp
if (res != NULL)
1308
2021-01-07
pjp
zonenumberx = res->zonenumber;
1309
2021-01-07
pjp
else
1310
2021-01-07
pjp
zonenumberx = (uint32_t)-1;
1311
2021-01-07
pjp
}
1312
2021-01-05
pjp
1313
2020-07-23
pjp
1314
2020-07-23
pjp
now = time(NULL);
1315
2020-07-23
pjp
1316
2020-07-23
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_RP)) == 0)
1317
2020-07-23
pjp
return -1;
1318
2020-07-23
pjp
1319
2020-07-23
pjp
if (istcp) {
1320
2020-07-23
pjp
replysize = 65535;
1321
2020-07-23
pjp
}
1322
2020-07-23
pjp
1323
2020-07-23
pjp
if (!istcp && q->edns0len > 512)
1324
2020-09-30
pjp
replysize = MIN(q->edns0len, max_udp_payload);
1325
2020-07-23
pjp
1326
2020-07-23
pjp
odh = (struct dns_header *)&reply[0];
1327
2020-07-23
pjp
1328
2020-07-23
pjp
outlen = sizeof(struct dns_header);
1329
2020-07-23
pjp
1330
2020-07-23
pjp
if (len > replysize) {
1331
2020-07-23
pjp
return (retlen);
1332
2020-07-23
pjp
}
1333
2020-07-23
pjp
1334
2020-07-23
pjp
memcpy(reply, buf, sizeof(struct dns_header) + q->hdr->namelen + 4);
1335
2020-07-23
pjp
1336
2020-07-23
pjp
outlen += (q->hdr->namelen + 4);
1337
2020-07-23
pjp
rollback = outlen;
1338
2020-07-23
pjp
1339
2020-07-23
pjp
memset((char *)&odh->query, 0, sizeof(u_int16_t));
1340
2020-07-23
pjp
set_reply_flags(rbt, odh, q);
1341
2020-07-23
pjp
1342
2020-07-23
pjp
odh->question = htons(1);
1343
2020-07-23
pjp
odh->answer = htons(1);
1344
2020-07-23
pjp
odh->nsrr = 0;
1345
2020-07-23
pjp
odh->additional = 0;
1346
2020-07-23
pjp
1347
2020-07-23
pjp
/* skip dns header, question name, qtype and qclass */
1348
2020-07-23
pjp
answer = (struct answer *)(&reply[0] + sizeof(struct dns_header) +
1349
2020-07-23
pjp
q->hdr->namelen + 4);
1350
2020-07-23
pjp
1351
2020-07-23
pjp
rp_count = 0;
1352
2020-07-23
pjp
1353
2020-07-23
pjp
TAILQ_FOREACH(rrp, &rrset->rr_head, entries) {
1354
2021-01-05
pjp
if (rrp->zonenumber != zonenumberx)
1355
2021-01-05
pjp
continue;
1356
2020-07-23
pjp
if ((outlen + sizeof(struct answer) + \
1357
2020-07-23
pjp
((struct rp *)rrp->rdata)->mboxlen + \
1358
2020-07-23
pjp
((struct rp *)rrp->rdata)->txtlen) > replysize) {
1359
2020-07-23
pjp
1360
2020-07-23
pjp
NTOHS(odh->query);
1361
2020-07-23
pjp
SET_DNS_TRUNCATION(odh);
1362
2020-07-23
pjp
HTONS(odh->query);
1363
2020-07-23
pjp
odh->answer = 0;
1364
2020-07-23
pjp
odh->nsrr = 0;
1365
2020-07-23
pjp
odh->additional = 0;
1366
2020-07-23
pjp
outlen = rollback;
1367
2020-07-23
pjp
goto out;
1368
2020-07-23
pjp
}
1369
2020-07-23
pjp
1370
2020-07-23
pjp
answer->name[0] = 0xc0; /* 1 byte */
1371
2020-07-23
pjp
answer->name[1] = 0x0c; /* 2 bytes */
1372
2020-07-23
pjp
answer->type = q->hdr->qtype; /* 4 bytes */
1373
2020-07-23
pjp
answer->class = q->hdr->qclass; /* 6 bytes */
1374
2020-07-23
pjp
1375
2020-07-23
pjp
if (q->aa)
1376
2020-07-23
pjp
answer->ttl = htonl(rrset->ttl); /* 10 b */
1377
2020-07-23
pjp
else
1378
2020-07-23
pjp
answer->ttl = htonl(rrset->ttl - (MIN(rrset->ttl, difftime(now, rrset->created))));
1379
2020-07-23
pjp
1380
2020-07-23
pjp
1381
2020-07-23
pjp
rp_count++;
1382
2020-07-23
pjp
outlen += 12;
1383
2020-07-23
pjp
1384
2020-07-23
pjp
pack(&reply[outlen], ((struct rp *)rrp->rdata)->mbox,
1385
2020-07-23
pjp
((struct rp *)rrp->rdata)->mboxlen);
1386
2020-07-23
pjp
outlen += ((struct rp *)rrp->rdata)->mboxlen;
1387
2020-07-23
pjp
1388
2020-07-23
pjp
pack(&reply[outlen], ((struct rp *)rrp->rdata)->txt,
1389
2020-07-23
pjp
((struct rp *)rrp->rdata)->txtlen);
1390
2020-07-23
pjp
outlen += ((struct rp *)rrp->rdata)->txtlen;
1391
2020-07-23
pjp
1392
2020-07-23
pjp
answer->rdlength = htons(((struct rp *)rrp->rdata)->mboxlen + \
1393
2020-07-23
pjp
((struct rp *)rrp->rdata)->txtlen);
1394
2020-07-23
pjp
1395
2020-07-23
pjp
/* set new offset for answer */
1396
2020-07-23
pjp
answer = (struct answer *)&reply[outlen];
1397
2020-07-23
pjp
}
1398
2020-07-23
pjp
1399
2020-07-23
pjp
odh->answer = htons(rp_count);
1400
2020-07-23
pjp
1401
2020-07-23
pjp
1402
2020-07-23
pjp
/* Add RRSIG reply_nsec */
1403
2020-07-23
pjp
if (dnssec && q->dnssecok && (rbt->flags & RBT_DNSSEC)) {
1404
2020-07-23
pjp
int tmplen = 0;
1405
2020-07-23
pjp
int origlen = outlen;
1406
2020-07-23
pjp
int retcount;
1407
2020-07-23
pjp
1408
2020-07-23
pjp
tmplen = additional_rrsig(q->hdr->name, q->hdr->namelen, DNS_TYPE_RP, rbt, reply, replysize, outlen, &retcount, q->aa);
1409
2020-07-23
pjp
1410
2020-07-23
pjp
if (tmplen == 0) {
1411
2020-07-23
pjp
1412
2020-07-23
pjp
/* we're forwarding and had no RRSIG return with -1 */
1413
2020-07-23
pjp
if (q->aa != 1)
1414
2020-07-23
pjp
return -1;
1415
2020-07-23
pjp
1416
2020-07-23
pjp
NTOHS(odh->query);
1417
2020-07-23
pjp
SET_DNS_TRUNCATION(odh);
1418
2020-07-23
pjp
HTONS(odh->query);
1419
2020-07-23
pjp
odh->answer = 0;
1420
2020-07-23
pjp
odh->nsrr = 0;
1421
2020-07-23
pjp
odh->additional = 0;
1422
2020-07-23
pjp
outlen = rollback;
1423
2020-07-23
pjp
goto out;
1424
2020-07-23
pjp
}
1425
2020-07-23
pjp
1426
2020-07-23
pjp
outlen = tmplen;
1427
2020-07-23
pjp
1428
2020-07-23
pjp
if (outlen > origlen)
1429
2020-07-23
pjp
odh->answer = htons(rp_count + retcount);
1430
2020-07-23
pjp
1431
2020-07-23
pjp
}
1432
2020-07-23
pjp
1433
2020-07-23
pjp
out:
1434
2020-07-23
pjp
if (q->edns0len) {
1435
2020-07-23
pjp
/* tag on edns0 opt record */
1436
2020-07-23
pjp
odh->additional = htons(1);
1437
2020-07-23
pjp
outlen = additional_opt(q, reply, replysize, outlen);
1438
2020-07-23
pjp
}
1439
2020-07-23
pjp
1440
2020-07-23
pjp
if (q->tsig.tsigverified == 1) {
1441
2021-01-07
pjp
outlen = additional_tsig(q, reply, replysize, outlen, 0, 0, NULL, DEFAULT_TSIG_FUDGE);
1442
2020-07-23
pjp
1443
2020-07-23
pjp
NTOHS(odh->additional);
1444
2020-07-23
pjp
odh->additional++;
1445
2020-07-23
pjp
HTONS(odh->additional);
1446
2020-07-23
pjp
}
1447
2020-07-23
pjp
1448
2020-07-23
pjp
if (istcp) {
1449
2020-07-23
pjp
char *tmpbuf;
1450
2020-07-23
pjp
1451
2020-07-23
pjp
tmpbuf = malloc(outlen + 2);
1452
2020-07-23
pjp
if (tmpbuf == 0) {
1453
2020-07-23
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
1454
2020-07-23
pjp
}
1455
2020-07-23
pjp
pack16(tmpbuf, htons(outlen));
1456
2020-07-23
pjp
memcpy(&tmpbuf[2], reply, outlen);
1457
2020-07-23
pjp
1458
2020-07-23
pjp
if ((retlen = send(so, tmpbuf, outlen + 2, 0)) < 0) {
1459
2020-07-23
pjp
dolog(LOG_INFO, "send: %s\n", strerror(errno));
1460
2020-07-23
pjp
}
1461
2020-07-23
pjp
free(tmpbuf);
1462
2020-07-23
pjp
} else {
1463
2020-07-23
pjp
if (q->rawsocket) {
1464
2020-07-23
pjp
*sretlen = retlen = outlen;
1465
2020-07-23
pjp
} else {
1466
2020-07-23
pjp
if ((retlen = sendto(so, reply, outlen, 0, sa, salen)) < 0) {
1467
2020-07-23
pjp
dolog(LOG_INFO, "sendto: %s\n", strerror(errno));
1468
2020-07-23
pjp
}
1469
2020-07-23
pjp
}
1470
2020-07-23
pjp
}
1471
2020-07-23
pjp
1472
2020-07-23
pjp
1473
2020-07-23
pjp
return (retlen);
1474
2020-07-23
pjp
}
1475
2020-07-23
pjp
1476
2015-06-27
pjp
/*
1477
2015-06-27
pjp
* REPLY_NSEC() - replies a DNS question (*q) on socket (so)
1478
2015-06-27
pjp
*
1479
2015-06-27
pjp
*/
1480
2015-06-27
pjp
1481
2015-06-27
pjp
int
1482
2020-07-21
pjp
reply_nsec(struct sreply *sreply, int *sretlen, ddDB *db)
1483
2015-06-27
pjp
{
1484
2015-06-27
pjp
char *reply = sreply->replybuf;
1485
2015-06-27
pjp
struct dns_header *odh;
1486
2015-06-27
pjp
u_int16_t outlen = 0;
1487
2015-06-27
pjp
int a_count;
1488
2015-06-27
pjp
1489
2015-06-27
pjp
struct answer {
1490
2015-06-27
pjp
char name[2];
1491
2015-06-27
pjp
u_int16_t type;
1492
2015-06-27
pjp
u_int16_t class;
1493
2015-06-27
pjp
u_int32_t ttl;
1494
2015-06-27
pjp
u_int16_t rdlength; /* 12 */
1495
2015-06-27
pjp
} __attribute__((packed));
1496
2015-06-27
pjp
1497
2015-06-27
pjp
struct answer *answer;
1498
2015-06-27
pjp
1499
2015-06-27
pjp
int so = sreply->so;
1500
2015-06-27
pjp
char *buf = sreply->buf;
1501
2015-06-27
pjp
int len = sreply->len;
1502
2015-06-27
pjp
struct question *q = sreply->q;
1503
2015-06-27
pjp
struct sockaddr *sa = sreply->sa;
1504
2015-06-27
pjp
int salen = sreply->salen;
1505
2019-02-15
pjp
struct rbtree *rbt = sreply->rbt1;
1506
2019-02-15
pjp
struct rrset *rrset = NULL;
1507
2019-02-15
pjp
struct rr *rrp = NULL;
1508
2015-06-27
pjp
1509
2015-06-27
pjp
int istcp = sreply->istcp;
1510
2015-06-27
pjp
int replysize = 512;
1511
2015-06-27
pjp
int retlen = -1;
1512
2017-09-18
pjp
u_int16_t rollback;
1513
2019-02-15
pjp
int ndnlen, bitmaplen;
1514
2020-07-06
pjp
time_t now;
1515
2021-01-05
pjp
uint32_t zonenumberx;
1516
2021-01-05
pjp
struct zoneentry *res = NULL;
1517
2015-06-27
pjp
1518
2021-01-07
pjp
if (rbt->flags & RBT_CACHE) {
1519
2021-01-05
pjp
zonenumberx = (uint32_t)-1;
1520
2021-01-07
pjp
} else {
1521
2021-01-07
pjp
res = zone_findzone(rbt);
1522
2021-01-07
pjp
if (res != NULL)
1523
2021-01-07
pjp
zonenumberx = res->zonenumber;
1524
2021-01-07
pjp
else
1525
2021-01-07
pjp
zonenumberx = (uint32_t)-1;
1526
2021-01-07
pjp
}
1527
2021-01-05
pjp
1528
2020-07-06
pjp
now = time(NULL);
1529
2020-07-06
pjp
1530
2019-02-27
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_A)) == 0)
1531
2015-06-27
pjp
return -1;
1532
2015-06-27
pjp
1533
2015-06-27
pjp
if (istcp) {
1534
2015-06-27
pjp
replysize = 65535;
1535
2015-06-27
pjp
}
1536
2015-06-27
pjp
1537
2015-06-27
pjp
if (!istcp && q->edns0len > 512)
1538
2020-09-30
pjp
replysize = MIN(q->edns0len, max_udp_payload);
1539
2015-06-27
pjp
1540
2015-06-27
pjp
odh = (struct dns_header *)&reply[0];
1541
2015-06-27
pjp
1542
2015-06-27
pjp
outlen = sizeof(struct dns_header);
1543
2015-06-27
pjp
1544
2015-06-27
pjp
if (len > replysize) {
1545
2015-06-27
pjp
return (retlen);
1546
2015-06-27
pjp
}
1547
2015-06-27
pjp
1548
2015-06-27
pjp
memcpy(reply, buf, sizeof(struct dns_header) + q->hdr->namelen + 4);
1549
2015-06-27
pjp
1550
2015-06-27
pjp
outlen += (q->hdr->namelen + 4);
1551
2017-09-18
pjp
rollback = outlen;
1552
2015-06-27
pjp
1553
2020-07-18
pjp
memset((char *)&odh->query, 0, sizeof(u_int16_t));
1554
2020-07-20
pjp
set_reply_flags(rbt, odh, q);
1555
2015-06-27
pjp
1556
2015-06-27
pjp
odh->question = htons(1);
1557
2015-06-27
pjp
odh->answer = htons(1);
1558
2015-06-27
pjp
odh->nsrr = 0;
1559
2015-06-27
pjp
odh->additional = 0;
1560
2015-06-27
pjp
1561
2015-06-27
pjp
/* skip dns header, question name, qtype and qclass */
1562
2015-06-27
pjp
answer = (struct answer *)(&reply[0] + sizeof(struct dns_header) +
1563
2015-06-27
pjp
q->hdr->namelen + 4);
1564
2015-06-27
pjp
1565
2015-06-27
pjp
a_count = 0;
1566
2015-06-27
pjp
1567
2019-02-15
pjp
rrp = TAILQ_FIRST(&rrset->rr_head);
1568
2019-02-27
pjp
if (rrp == 0)
1569
2019-02-15
pjp
return -1;
1570
2019-02-15
pjp
1571
2019-02-15
pjp
ndnlen = ((struct nsec *)rrp->rdata)->ndn_len;
1572
2019-02-15
pjp
bitmaplen = ((struct nsec *)rrp->rdata)->bitmap_len;
1573
2019-02-15
pjp
1574
2019-02-15
pjp
if ((outlen + sizeof(struct answer) + ndnlen + bitmaplen) > replysize) {
1575
2015-06-27
pjp
NTOHS(odh->query);
1576
2015-06-27
pjp
SET_DNS_TRUNCATION(odh);
1577
2015-06-27
pjp
HTONS(odh->query);
1578
2017-09-18
pjp
odh->answer = 0;
1579
2017-09-18
pjp
odh->nsrr = 0;
1580
2017-09-18
pjp
odh->additional = 0;
1581
2017-09-18
pjp
outlen = rollback;
1582
2015-06-27
pjp
goto out;
1583
2015-06-27
pjp
}
1584
2015-06-27
pjp
1585
2015-06-27
pjp
/*
1586
2015-06-27
pjp
* answer->name is a pointer to the request (0xc00c)
1587
2015-06-27
pjp
*/
1588
2015-06-27
pjp
1589
2015-06-27
pjp
answer->name[0] = 0xc0; /* 1 byte */
1590
2015-06-27
pjp
answer->name[1] = 0x0c; /* 2 bytes */
1591
2015-06-27
pjp
answer->type = q->hdr->qtype; /* 4 bytes */
1592
2015-06-27
pjp
answer->class = q->hdr->qclass; /* 6 bytes */
1593
2019-02-15
pjp
1594
2020-07-06
pjp
if (q->aa)
1595
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl); /* 10 b */
1596
2020-07-06
pjp
else
1597
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl - (MIN(rrset->ttl, difftime(now, rrset->created))));
1598
2020-07-06
pjp
1599
2019-02-15
pjp
answer->rdlength = htons(ndnlen + bitmaplen);
1600
2015-06-27
pjp
1601
2015-06-27
pjp
outlen += sizeof(struct answer);
1602
2015-06-27
pjp
1603
2019-02-15
pjp
memcpy(&reply[outlen], ((struct nsec *)rrp->rdata)->next_domain_name,
1604
2019-02-15
pjp
ndnlen);
1605
2015-06-27
pjp
1606
2019-02-15
pjp
outlen += ndnlen;
1607
2015-06-27
pjp
1608
2019-02-15
pjp
memcpy(&reply[outlen], ((struct nsec *)rrp->rdata)->bitmap, bitmaplen);
1609
2019-02-15
pjp
outlen += bitmaplen;
1610
2015-06-27
pjp
1611
2015-06-27
pjp
a_count++;
1612
2015-06-27
pjp
1613
2015-06-27
pjp
/* set new offset for answer */
1614
2015-06-27
pjp
answer = (struct answer *)&reply[outlen];
1615
2015-06-27
pjp
1616
2015-06-27
pjp
1617
2015-06-27
pjp
/* Add RRSIG reply_nsec */
1618
2019-11-11
pjp
if (dnssec && q->dnssecok && (rbt->flags & RBT_DNSSEC)) {
1619
2015-06-27
pjp
int tmplen = 0;
1620
2015-06-27
pjp
int origlen = outlen;
1621
2020-04-01
pjp
int retcount;
1622
2015-06-27
pjp
1623
2020-07-06
pjp
tmplen = additional_rrsig(q->hdr->name, q->hdr->namelen, DNS_TYPE_NSEC, rbt, reply, replysize, outlen, &retcount, q->aa);
1624
2015-06-27
pjp
1625
2018-02-27
pjp
if (tmplen == 0) {
1626
2020-07-08
pjp
1627
2020-07-08
pjp
/* we're forwarding and had no RRSIG return with -1 */
1628
2020-07-08
pjp
if (q->aa != 1)
1629
2020-07-08
pjp
return -1;
1630
2020-07-08
pjp
1631
2018-02-27
pjp
NTOHS(odh->query);
1632
2018-02-27
pjp
SET_DNS_TRUNCATION(odh);
1633
2018-02-27
pjp
HTONS(odh->query);
1634
2018-02-27
pjp
odh->answer = 0;
1635
2018-02-27
pjp
odh->nsrr = 0;
1636
2018-02-27
pjp
odh->additional = 0;
1637
2018-02-27
pjp
outlen = rollback;
1638
2018-02-27
pjp
goto out;
1639
2015-06-27
pjp
}
1640
2015-06-27
pjp
1641
2018-02-27
pjp
outlen = tmplen;
1642
2018-02-27
pjp
1643
2018-02-27
pjp
if (outlen > origlen)
1644
2020-04-01
pjp
odh->answer = htons(a_count + retcount + 1);
1645
2018-02-27
pjp
1646
2015-11-14
pjp
}
1647
2015-11-14
pjp
1648
2015-11-14
pjp
out:
1649
2015-06-27
pjp
if (q->edns0len) {
1650
2015-06-27
pjp
/* tag on edns0 opt record */
1651
2015-06-27
pjp
odh->additional = htons(1);
1652
2015-06-27
pjp
outlen = additional_opt(q, reply, replysize, outlen);
1653
2015-06-27
pjp
}
1654
2015-06-27
pjp
1655
2019-02-24
pjp
if (q->tsig.tsigverified == 1) {
1656
2021-01-07
pjp
outlen = additional_tsig(q, reply, replysize, outlen, 0, 0, NULL, DEFAULT_TSIG_FUDGE);
1657
2019-02-24
pjp
1658
2019-02-24
pjp
NTOHS(odh->additional);
1659
2019-02-24
pjp
odh->additional++;
1660
2019-02-24
pjp
HTONS(odh->additional);
1661
2019-02-24
pjp
}
1662
2019-02-24
pjp
1663
2015-12-19
pjp
if (istcp) {
1664
2015-12-19
pjp
char *tmpbuf;
1665
2015-12-19
pjp
1666
2015-12-19
pjp
tmpbuf = malloc(outlen + 2);
1667
2019-02-27
pjp
if (tmpbuf == 0) {
1668
2015-12-19
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
1669
2015-12-19
pjp
}
1670
2019-12-03
pjp
pack16(tmpbuf, htons(outlen));
1671
2015-12-19
pjp
memcpy(&tmpbuf[2], reply, outlen);
1672
2015-12-19
pjp
1673
2015-12-19
pjp
if ((retlen = send(so, tmpbuf, outlen + 2, 0)) < 0) {
1674
2015-12-19
pjp
dolog(LOG_INFO, "send: %s\n", strerror(errno));
1675
2015-12-19
pjp
}
1676
2015-12-19
pjp
free(tmpbuf);
1677
2015-12-19
pjp
} else {
1678
2020-07-21
pjp
if (q->rawsocket) {
1679
2020-07-21
pjp
*sretlen = retlen = outlen;
1680
2020-07-21
pjp
} else {
1681
2020-07-21
pjp
if ((retlen = sendto(so, reply, outlen, 0, sa, salen)) < 0) {
1682
2020-07-21
pjp
dolog(LOG_INFO, "sendto: %s\n", strerror(errno));
1683
2020-07-21
pjp
}
1684
2015-06-25
pjp
}
1685
2015-12-19
pjp
}
1686
2015-06-25
pjp
1687
2015-06-25
pjp
1688
2015-06-25
pjp
return (retlen);
1689
2015-06-25
pjp
}
1690
2015-06-25
pjp
1691
2015-06-25
pjp
/*
1692
2015-06-25
pjp
* REPLY_DS() - replies a DNS question (*q) on socket (so)
1693
2015-06-25
pjp
*
1694
2015-06-25
pjp
*/
1695
2015-06-25
pjp
1696
2015-06-25
pjp
int
1697
2020-07-21
pjp
reply_ds(struct sreply *sreply, int *sretlen, ddDB *db)
1698
2015-06-25
pjp
{
1699
2015-06-25
pjp
char *reply = sreply->replybuf;
1700
2015-06-25
pjp
struct dns_header *odh;
1701
2015-06-25
pjp
u_int16_t outlen = 0;
1702
2015-06-25
pjp
int a_count;
1703
2015-06-25
pjp
1704
2015-06-25
pjp
struct answer {
1705
2015-06-25
pjp
char name[2];
1706
2015-06-25
pjp
u_int16_t type;
1707
2015-06-25
pjp
u_int16_t class;
1708
2015-06-25
pjp
u_int32_t ttl;
1709
2015-06-25
pjp
u_int16_t rdlength; /* 12 */
1710
2015-06-25
pjp
u_int16_t key_tag;
1711
2015-06-25
pjp
u_int8_t algorithm;
1712
2015-06-25
pjp
u_int8_t digest_type;
1713
2015-06-25
pjp
} __attribute__((packed));
1714
2015-06-25
pjp
1715
2015-06-25
pjp
struct answer *answer;
1716
2015-06-25
pjp
1717
2015-06-25
pjp
int so = sreply->so;
1718
2015-06-25
pjp
char *buf = sreply->buf;
1719
2015-06-25
pjp
int len = sreply->len;
1720
2015-06-25
pjp
struct question *q = sreply->q;
1721
2015-06-25
pjp
struct sockaddr *sa = sreply->sa;
1722
2015-06-25
pjp
int salen = sreply->salen;
1723
2019-02-15
pjp
struct rbtree *rbt = sreply->rbt1;
1724
2019-02-15
pjp
struct rrset *rrset = NULL;
1725
2019-02-15
pjp
struct rr *rrp = NULL;
1726
2015-06-25
pjp
1727
2015-06-25
pjp
int istcp = sreply->istcp;
1728
2015-06-25
pjp
int replysize = 512;
1729
2015-06-25
pjp
int retlen = -1;
1730
2017-09-18
pjp
u_int16_t rollback;
1731
2020-07-06
pjp
time_t now;
1732
2021-01-05
pjp
uint32_t zonenumberx;
1733
2021-01-05
pjp
struct zoneentry *res = NULL;
1734
2015-06-25
pjp
1735
2021-01-07
pjp
if (rbt->flags & RBT_CACHE) {
1736
2021-01-05
pjp
zonenumberx = (uint32_t)-1;
1737
2021-01-07
pjp
} else {
1738
2021-01-07
pjp
res = zone_findzone(rbt);
1739
2021-01-07
pjp
if (res != NULL)
1740
2021-01-07
pjp
zonenumberx = res->zonenumber;
1741
2021-01-07
pjp
else
1742
2021-01-07
pjp
zonenumberx = (uint32_t)-1;
1743
2021-01-07
pjp
}
1744
2021-01-05
pjp
1745
2020-07-06
pjp
now = time(NULL);
1746
2020-07-06
pjp
1747
2019-02-27
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_DS)) == 0)
1748
2015-06-25
pjp
return -1;
1749
2015-06-25
pjp
1750
2015-06-25
pjp
if (istcp) {
1751
2015-06-25
pjp
replysize = 65535;
1752
2015-06-24
pjp
}
1753
2015-06-25
pjp
1754
2015-06-25
pjp
if (!istcp && q->edns0len > 512)
1755
2020-09-30
pjp
replysize = MIN(q->edns0len, max_udp_payload);
1756
2015-06-25
pjp
1757
2015-06-25
pjp
odh = (struct dns_header *)&reply[0];
1758
2015-06-25
pjp
1759
2015-06-25
pjp
outlen = sizeof(struct dns_header);
1760
2015-06-25
pjp
1761
2015-06-25
pjp
if (len > replysize) {
1762
2015-06-25
pjp
return (retlen);
1763
2015-06-24
pjp
}
1764
2015-06-25
pjp
1765
2015-06-25
pjp
memcpy(reply, buf, sizeof(struct dns_header) + q->hdr->namelen + 4);
1766
2015-06-25
pjp
1767
2015-06-25
pjp
outlen += (q->hdr->namelen + 4);
1768
2017-09-18
pjp
rollback = outlen;
1769
2015-06-25
pjp
1770
2020-07-18
pjp
memset((char *)&odh->query, 0, sizeof(u_int16_t));
1771
2020-07-20
pjp
set_reply_flags(rbt, odh, q);
1772
2015-06-25
pjp
1773
2015-06-25
pjp
odh->question = htons(1);
1774
2015-06-25
pjp
odh->nsrr = 0;
1775
2015-06-25
pjp
odh->additional = 0;
1776
2015-06-25
pjp
1777
2015-06-25
pjp
/* skip dns header, question name, qtype and qclass */
1778
2015-06-25
pjp
answer = (struct answer *)(&reply[0] + sizeof(struct dns_header) +
1779
2015-06-25
pjp
q->hdr->namelen + 4);
1780
2015-06-25
pjp
1781
2015-06-25
pjp
a_count = 0;
1782
2015-06-25
pjp
1783
2019-02-15
pjp
TAILQ_FOREACH(rrp, &rrset->rr_head, entries) {
1784
2021-01-05
pjp
if (rrp->zonenumber != zonenumberx)
1785
2021-01-05
pjp
continue;
1786
2015-06-25
pjp
if ((outlen + sizeof(struct answer) +
1787
2019-02-15
pjp
((struct ds *)rrp->rdata)->digestlen) > replysize) {
1788
2015-06-25
pjp
NTOHS(odh->query);
1789
2015-06-25
pjp
SET_DNS_TRUNCATION(odh);
1790
2015-06-25
pjp
HTONS(odh->query);
1791
2017-09-18
pjp
odh->answer = 0;
1792
2017-09-18
pjp
odh->nsrr = 0;
1793
2017-09-18
pjp
odh->additional = 0;
1794
2017-09-18
pjp
outlen = rollback;
1795
2015-06-25
pjp
goto out;
1796
2015-06-25
pjp
}
1797
2015-06-25
pjp
1798
2015-06-25
pjp
/*
1799
2015-06-25
pjp
* answer->name is a pointer to the request (0xc00c)
1800
2015-06-25
pjp
*/
1801
2015-06-25
pjp
1802
2015-06-25
pjp
answer->name[0] = 0xc0; /* 1 byte */
1803
2015-06-25
pjp
answer->name[1] = 0x0c; /* 2 bytes */
1804
2015-06-25
pjp
answer->type = q->hdr->qtype; /* 4 bytes */
1805
2015-06-25
pjp
answer->class = q->hdr->qclass; /* 6 bytes */
1806
2019-02-15
pjp
1807
2020-07-06
pjp
if (q->aa)
1808
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl); /* 10 */
1809
2020-07-06
pjp
else
1810
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl - (MIN(rrset->ttl, difftime(now, rrset->created))));
1811
2020-07-06
pjp
1812
2020-07-06
pjp
1813
2019-02-15
pjp
answer->rdlength = htons(((struct ds *)rrp->rdata)->digestlen + 4); /* 12 bytes */
1814
2015-06-25
pjp
1815
2019-02-15
pjp
answer->key_tag = htons(((struct ds *)rrp->rdata)->key_tag);
1816
2019-02-15
pjp
answer->algorithm = ((struct ds *)rrp->rdata)->algorithm;
1817
2019-02-15
pjp
answer->digest_type = ((struct ds *)rrp->rdata)->digest_type;
1818
2015-06-25
pjp
1819
2015-06-25
pjp
outlen += sizeof(struct answer);
1820
2015-06-25
pjp
1821
2019-02-15
pjp
memcpy(&reply[outlen], ((struct ds *)rrp->rdata)->digest,
1822
2019-02-15
pjp
((struct ds *)rrp->rdata)->digestlen);
1823
2015-06-25
pjp
1824
2019-02-15
pjp
outlen += ((struct ds *)rrp->rdata)->digestlen;
1825
2015-06-25
pjp
1826
2015-06-25
pjp
a_count++;
1827
2015-06-25
pjp
/* set new offset for answer */
1828
2015-06-25
pjp
answer = (struct answer *)&reply[outlen];
1829
2019-02-15
pjp
}
1830
2015-06-25
pjp
1831
2015-06-25
pjp
odh->answer = htons(a_count);
1832
2015-06-25
pjp
1833
2015-06-27
pjp
/* Add RRSIG reply_ds */
1834
2019-11-11
pjp
if (dnssec && q->dnssecok && (rbt->flags & RBT_DNSSEC)) {
1835
2015-06-25
pjp
int tmplen = 0;
1836
2015-06-25
pjp
int origlen = outlen;
1837
2020-04-01
pjp
int retcount;
1838
2015-06-25
pjp
1839
2020-07-06
pjp
tmplen = additional_rrsig(q->hdr->name, q->hdr->namelen, DNS_TYPE_DS, rbt, reply, replysize, outlen, &retcount, q->aa);
1840
2015-06-25
pjp
1841
2018-03-03
pjp
if (tmplen == 0) {
1842
2020-07-08
pjp
1843
2020-07-08
pjp
/* we're forwarding and had no RRSIG return with -1 */
1844
2020-07-08
pjp
if (q->aa != 1)
1845
2020-07-08
pjp
return -1;
1846
2020-07-08
pjp
1847
2018-03-03
pjp
NTOHS(odh->query);
1848
2018-03-03
pjp
SET_DNS_TRUNCATION(odh);
1849
2018-03-03
pjp
HTONS(odh->query);
1850
2018-03-03
pjp
odh->answer = 0;
1851
2018-03-03
pjp
odh->nsrr = 0;
1852
2018-03-03
pjp
odh->additional = 0;
1853
2018-03-03
pjp
outlen = rollback;
1854
2018-03-03
pjp
goto out;
1855
2015-06-25
pjp
}
1856
2015-06-25
pjp
1857
2018-03-03
pjp
outlen = tmplen;
1858
2018-03-03
pjp
1859
2019-11-11
pjp
if (outlen > origlen) {
1860
2019-11-11
pjp
NTOHS(odh->answer);
1861
2020-04-01
pjp
odh->answer += retcount;
1862
2019-11-11
pjp
HTONS(odh->answer);
1863
2019-11-11
pjp
}
1864
2015-11-14
pjp
}
1865
2015-06-25
pjp
1866
2015-11-14
pjp
out:
1867
2015-06-25
pjp
if (q->edns0len) {
1868
2015-06-25
pjp
/* tag on edns0 opt record */
1869
2015-06-25
pjp
odh->additional = htons(1);
1870
2015-06-25
pjp
outlen = additional_opt(q, reply, replysize, outlen);
1871
2015-06-24
pjp
}
1872
2015-06-25
pjp
1873
2019-02-24
pjp
if (q->tsig.tsigverified == 1) {
1874
2021-01-07
pjp
outlen = additional_tsig(q, reply, replysize, outlen, 0, 0, NULL, DEFAULT_TSIG_FUDGE);
1875
2019-02-24
pjp
1876
2019-02-24
pjp
NTOHS(odh->additional);
1877
2019-02-24
pjp
odh->additional++;
1878
2019-02-24
pjp
HTONS(odh->additional);
1879
2019-02-24
pjp
}
1880
2019-02-24
pjp
1881
2015-12-19
pjp
if (istcp) {
1882
2015-12-19
pjp
char *tmpbuf;
1883
2015-12-19
pjp
1884
2015-12-19
pjp
tmpbuf = malloc(outlen + 2);
1885
2019-02-27
pjp
if (tmpbuf == 0) {
1886
2015-12-19
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
1887
2015-12-19
pjp
}
1888
2019-12-03
pjp
pack16(tmpbuf, htons(outlen));
1889
2015-12-19
pjp
memcpy(&tmpbuf[2], reply, outlen);
1890
2015-12-19
pjp
1891
2015-12-19
pjp
if ((retlen = send(so, tmpbuf, outlen + 2, 0)) < 0) {
1892
2015-12-19
pjp
dolog(LOG_INFO, "send: %s\n", strerror(errno));
1893
2015-12-19
pjp
}
1894
2015-12-19
pjp
free(tmpbuf);
1895
2015-12-19
pjp
} else {
1896
2020-07-21
pjp
if (q->rawsocket) {
1897
2020-07-21
pjp
*sretlen = retlen = outlen;
1898
2020-07-21
pjp
} else {
1899
2020-07-21
pjp
if ((retlen = sendto(so, reply, outlen, 0, sa, salen)) < 0) {
1900
2020-07-21
pjp
dolog(LOG_INFO, "sendto: %s\n", strerror(errno));
1901
2020-07-21
pjp
}
1902
2015-06-25
pjp
}
1903
2015-12-19
pjp
}
1904
2015-06-25
pjp
1905
2019-02-15
pjp
rotate_rr(rrset);
1906
2019-02-15
pjp
1907
2015-06-25
pjp
return (retlen);
1908
2015-06-25
pjp
}
1909
2015-06-25
pjp
1910
2015-06-25
pjp
/*
1911
2015-06-25
pjp
* REPLY_DNSKEY() - replies a DNS question (*q) on socket (so)
1912
2015-06-25
pjp
*
1913
2015-06-25
pjp
*/
1914
2015-06-25
pjp
1915
2015-06-25
pjp
int
1916
2020-07-21
pjp
reply_dnskey(struct sreply *sreply, int *sretlen, ddDB *db)
1917
2015-06-25
pjp
{
1918
2015-06-25
pjp
char *reply = sreply->replybuf;
1919
2015-06-25
pjp
struct dns_header *odh;
1920
2015-06-25
pjp
u_int16_t outlen = 0;
1921
2019-02-15
pjp
int dnskey_count;
1922
2015-06-25
pjp
1923
2015-06-25
pjp
struct answer {
1924
2015-06-25
pjp
char name[2];
1925
2015-06-25
pjp
u_int16_t type;
1926
2015-06-25
pjp
u_int16_t class;
1927
2015-06-25
pjp
u_int32_t ttl;
1928
2015-06-25
pjp
u_int16_t rdlength; /* 12 */
1929
2015-06-25
pjp
u_int16_t flags;
1930
2015-06-25
pjp
u_int8_t protocol;
1931
2015-06-25
pjp
u_int8_t algorithm;
1932
2015-06-25
pjp
} __attribute__((packed));
1933
2015-06-25
pjp
1934
2015-06-25
pjp
struct answer *answer;
1935
2015-06-25
pjp
1936
2015-06-25
pjp
int so = sreply->so;
1937
2015-06-25
pjp
char *buf = sreply->buf;
1938
2015-06-25
pjp
int len = sreply->len;
1939
2015-06-25
pjp
struct question *q = sreply->q;
1940
2015-06-25
pjp
struct sockaddr *sa = sreply->sa;
1941
2015-06-25
pjp
int salen = sreply->salen;
1942
2019-02-15
pjp
struct rbtree *rbt = sreply->rbt1;
1943
2019-02-15
pjp
struct rrset *rrset = NULL;
1944
2019-02-15
pjp
struct rr *rrp = NULL;
1945
2015-06-25
pjp
1946
2015-06-25
pjp
int istcp = sreply->istcp;
1947
2015-06-25
pjp
int replysize = 512;
1948
2015-06-25
pjp
int retlen = -1;
1949
2019-02-15
pjp
int rrsig_count = 0;
1950
2017-09-18
pjp
u_int16_t rollback;
1951
2020-07-06
pjp
time_t now;
1952
2021-01-05
pjp
uint32_t zonenumberx;
1953
2021-01-05
pjp
struct zoneentry *res = NULL;
1954
2015-06-25
pjp
1955
2021-01-07
pjp
if (rbt->flags & RBT_CACHE) {
1956
2021-01-05
pjp
zonenumberx = (uint32_t)-1;
1957
2021-01-07
pjp
} else {
1958
2021-01-07
pjp
res = zone_findzone(rbt);
1959
2021-01-07
pjp
if (res != NULL)
1960
2021-01-07
pjp
zonenumberx = res->zonenumber;
1961
2021-01-07
pjp
else
1962
2021-01-07
pjp
zonenumberx = (uint32_t)-1;
1963
2021-01-07
pjp
}
1964
2021-01-05
pjp
1965
2020-07-06
pjp
now = time(NULL);
1966
2020-07-06
pjp
1967
2019-02-27
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_DNSKEY)) == 0)
1968
2015-06-25
pjp
return -1;
1969
2015-06-25
pjp
1970
2015-06-25
pjp
if (istcp) {
1971
2015-06-25
pjp
replysize = 65535;
1972
2015-06-25
pjp
}
1973
2015-06-25
pjp
1974
2015-06-25
pjp
if (!istcp && q->edns0len > 512)
1975
2020-09-30
pjp
replysize = MIN(q->edns0len, max_udp_payload);
1976
2015-06-25
pjp
1977
2015-06-25
pjp
odh = (struct dns_header *)&reply[0];
1978
2015-06-25
pjp
1979
2015-06-25
pjp
outlen = sizeof(struct dns_header);
1980
2015-06-25
pjp
1981
2015-06-25
pjp
if (len > replysize) {
1982
2015-06-25
pjp
return (retlen);
1983
2015-06-25
pjp
}
1984
2015-06-25
pjp
1985
2015-06-25
pjp
memcpy(reply, buf, sizeof(struct dns_header) + q->hdr->namelen + 4);
1986
2015-06-25
pjp
1987
2015-06-25
pjp
outlen += (q->hdr->namelen + 4);
1988
2017-09-18
pjp
rollback = outlen;
1989
2015-06-25
pjp
1990
2020-07-18
pjp
memset((char *)&odh->query, 0, sizeof(u_int16_t));
1991
2020-07-20
pjp
set_reply_flags(rbt, odh, q);
1992
2015-06-25
pjp
1993
2015-06-25
pjp
odh->question = htons(1);
1994
2019-02-15
pjp
odh->answer = htons(0);
1995
2015-06-25
pjp
odh->nsrr = 0;
1996
2015-06-25
pjp
odh->additional = 0;
1997
2015-06-25
pjp
1998
2015-06-25
pjp
/* skip dns header, question name, qtype and qclass */
1999
2015-06-25
pjp
answer = (struct answer *)(&reply[0] + sizeof(struct dns_header) +
2000
2015-06-25
pjp
q->hdr->namelen + 4);
2001
2015-06-25
pjp
2002
2019-02-15
pjp
dnskey_count = 0;
2003
2015-06-25
pjp
2004
2019-02-15
pjp
TAILQ_FOREACH(rrp, &rrset->rr_head, entries) {
2005
2021-01-05
pjp
if (rrp->zonenumber != zonenumberx)
2006
2021-01-05
pjp
continue;
2007
2015-06-25
pjp
if ((outlen + sizeof(struct answer) +
2008
2019-02-15
pjp
((struct dnskey *)rrp->rdata)->publickey_len) > replysize) {
2009
2015-06-25
pjp
NTOHS(odh->query);
2010
2015-06-25
pjp
SET_DNS_TRUNCATION(odh);
2011
2015-06-25
pjp
HTONS(odh->query);
2012
2017-09-18
pjp
odh->answer = 0;
2013
2017-09-18
pjp
odh->nsrr = 0;
2014
2017-09-18
pjp
odh->additional = 0;
2015
2017-09-18
pjp
outlen = rollback;
2016
2015-06-25
pjp
goto out;
2017
2015-06-25
pjp
}
2018
2015-06-25
pjp
2019
2015-06-25
pjp
/*
2020
2015-06-25
pjp
* answer->name is a pointer to the request (0xc00c)
2021
2015-06-25
pjp
*/
2022
2015-06-25
pjp
2023
2015-06-25
pjp
answer->name[0] = 0xc0; /* 1 byte */
2024
2015-06-25
pjp
answer->name[1] = 0x0c; /* 2 bytes */
2025
2015-06-25
pjp
answer->type = q->hdr->qtype; /* 4 bytes */
2026
2015-06-25
pjp
answer->class = q->hdr->qclass; /* 6 bytes */
2027
2019-02-15
pjp
2028
2020-07-06
pjp
if (q->aa)
2029
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl);
2030
2020-07-06
pjp
else
2031
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl - (MIN(rrset->ttl, difftime(now, rrset->created))));
2032
2020-07-06
pjp
2033
2019-02-15
pjp
answer->rdlength = htons(((struct dnskey *)rrp->rdata)->publickey_len + 4); /* 12 bytes */
2034
2015-06-25
pjp
2035
2019-02-15
pjp
answer->flags = htons(((struct dnskey *)rrp->rdata)->flags);
2036
2019-02-15
pjp
answer->protocol = ((struct dnskey *)rrp->rdata)->protocol;
2037
2019-02-15
pjp
answer->algorithm = ((struct dnskey *)rrp->rdata)->algorithm;
2038
2015-06-25
pjp
2039
2015-06-25
pjp
outlen += sizeof(struct answer);
2040
2015-06-25
pjp
2041
2019-02-15
pjp
memcpy(&reply[outlen], ((struct dnskey*)rrp->rdata)->public_key,
2042
2019-02-15
pjp
((struct dnskey *)rrp->rdata)->publickey_len);
2043
2015-06-25
pjp
2044
2019-02-15
pjp
outlen += ((struct dnskey *)rrp->rdata)->publickey_len;
2045
2015-06-25
pjp
2046
2019-02-15
pjp
dnskey_count++;
2047
2015-06-25
pjp
/* set new offset for answer */
2048
2015-06-25
pjp
answer = (struct answer *)&reply[outlen];
2049
2019-02-15
pjp
}
2050
2015-06-25
pjp
2051
2019-02-15
pjp
odh->answer = htons(dnskey_count);
2052
2019-02-15
pjp
2053
2015-06-27
pjp
/* Add RRSIG reply_dnskey */
2054
2019-11-11
pjp
if (dnssec && q->dnssecok && (rbt->flags & RBT_DNSSEC)) {
2055
2015-06-25
pjp
int tmplen = 0;
2056
2015-06-25
pjp
int origlen = outlen;
2057
2018-02-27
pjp
2058
2020-07-06
pjp
tmplen = additional_rrsig(q->hdr->name, q->hdr->namelen, DNS_TYPE_DNSKEY, rbt, reply, replysize, outlen, &rrsig_count, q->aa);
2059
2015-06-25
pjp
2060
2020-04-01
pjp
if (tmplen == 0) {
2061
2020-07-08
pjp
2062
2020-07-08
pjp
/* we're forwarding and had no RRSIG return with -1 */
2063
2020-07-08
pjp
if (q->aa != 1)
2064
2020-07-08
pjp
return -1;
2065
2020-07-08
pjp
2066
2020-04-01
pjp
NTOHS(odh->query);
2067
2020-04-01
pjp
SET_DNS_TRUNCATION(odh);
2068
2020-04-01
pjp
HTONS(odh->query);
2069
2020-04-01
pjp
odh->answer = 0;
2070
2020-04-01
pjp
odh->nsrr = 0;
2071
2020-04-01
pjp
odh->additional = 0;
2072
2020-04-01
pjp
outlen = rollback;
2073
2020-04-01
pjp
goto out;
2074
2015-06-25
pjp
}
2075
2015-06-25
pjp
2076
2020-04-01
pjp
outlen = tmplen;
2077
2020-04-01
pjp
2078
2020-04-01
pjp
if (outlen > origlen)
2079
2020-04-01
pjp
odh->answer = htons(dnskey_count + rrsig_count);
2080
2015-11-10
pjp
}
2081
2015-11-10
pjp
2082
2015-11-14
pjp
out:
2083
2015-06-25
pjp
if (q->edns0len) {
2084
2015-06-25
pjp
/* tag on edns0 opt record */
2085
2015-06-25
pjp
odh->additional = htons(1);
2086
2015-06-25
pjp
outlen = additional_opt(q, reply, replysize, outlen);
2087
2015-06-25
pjp
}
2088
2015-06-25
pjp
2089
2019-02-24
pjp
if (q->tsig.tsigverified == 1) {
2090
2021-01-07
pjp
outlen = additional_tsig(q, reply, replysize, outlen, 0, 0, NULL, DEFAULT_TSIG_FUDGE);
2091
2019-02-24
pjp
2092
2019-02-24
pjp
NTOHS(odh->additional);
2093
2019-02-24
pjp
odh->additional++;
2094
2019-02-24
pjp
HTONS(odh->additional);
2095
2019-02-24
pjp
}
2096
2019-02-24
pjp
2097
2015-12-19
pjp
if (istcp) {
2098
2015-12-19
pjp
char *tmpbuf;
2099
2015-12-19
pjp
2100
2015-12-19
pjp
tmpbuf = malloc(outlen + 2);
2101
2019-02-27
pjp
if (tmpbuf == 0) {
2102
2015-12-19
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
2103
2015-12-19
pjp
}
2104
2019-12-03
pjp
pack16(tmpbuf, htons(outlen));
2105
2015-12-19
pjp
memcpy(&tmpbuf[2], reply, outlen);
2106
2015-12-19
pjp
2107
2015-12-19
pjp
if ((retlen = send(so, tmpbuf, outlen + 2, 0)) < 0) {
2108
2015-12-19
pjp
dolog(LOG_INFO, "send: %s\n", strerror(errno));
2109
2015-12-19
pjp
}
2110
2015-12-19
pjp
free(tmpbuf);
2111
2015-12-19
pjp
} else {
2112
2020-07-21
pjp
if (q->rawsocket) {
2113
2020-07-21
pjp
*sretlen = retlen = outlen;
2114
2020-07-21
pjp
} else {
2115
2020-07-21
pjp
if ((retlen = sendto(so, reply, outlen, 0, sa, salen)) < 0) {
2116
2020-07-21
pjp
dolog(LOG_INFO, "sendto: %s\n", strerror(errno));
2117
2020-07-21
pjp
}
2118
2015-06-25
pjp
}
2119
2015-12-19
pjp
}
2120
2015-06-25
pjp
2121
2015-06-25
pjp
return (retlen);
2122
2015-06-25
pjp
}
2123
2015-06-25
pjp
2124
2015-06-25
pjp
/*
2125
2015-06-25
pjp
* REPLY_RRSIG() - replies a DNS question (*q) on socket (so)
2126
2015-06-25
pjp
*
2127
2015-06-25
pjp
*/
2128
2015-06-25
pjp
2129
2015-06-25
pjp
2130
2015-06-25
pjp
int
2131
2020-07-21
pjp
reply_rrsig(struct sreply *sreply, int *sretlen, ddDB *db)
2132
2015-06-25
pjp
{
2133
2015-06-25
pjp
char *reply = sreply->replybuf;
2134
2015-06-25
pjp
struct dns_header *odh;
2135
2015-06-25
pjp
u_int16_t outlen = 0;
2136
2015-06-25
pjp
int a_count;
2137
2015-06-25
pjp
2138
2015-06-25
pjp
struct answer {
2139
2015-06-25
pjp
char name[2];
2140
2015-06-25
pjp
u_int16_t type;
2141
2015-06-25
pjp
u_int16_t class;
2142
2015-06-25
pjp
u_int32_t ttl;
2143
2015-06-25
pjp
u_int16_t rdlength; /* 12 */
2144
2015-06-25
pjp
in_addr_t rdata; /* 16 */
2145
2015-06-25
pjp
} __attribute__((packed));
2146
2015-06-25
pjp
2147
2015-06-25
pjp
int so = sreply->so;
2148
2015-06-25
pjp
char *buf = sreply->buf;
2149
2015-06-25
pjp
int len = sreply->len;
2150
2015-06-25
pjp
struct question *q = sreply->q;
2151
2015-06-25
pjp
struct sockaddr *sa = sreply->sa;
2152
2015-06-25
pjp
int salen = sreply->salen;
2153
2019-02-15
pjp
struct rbtree *rbt = sreply->rbt1;
2154
2015-06-25
pjp
2155
2015-06-25
pjp
int istcp = sreply->istcp;
2156
2015-06-25
pjp
int replysize = 512;
2157
2015-06-25
pjp
int retlen = -1;
2158
2015-06-25
pjp
int tmplen = 0;
2159
2017-09-18
pjp
u_int16_t rollback;
2160
2021-01-05
pjp
uint32_t zonenumberx;
2161
2021-01-05
pjp
struct zoneentry *res = NULL;
2162
2015-06-25
pjp
2163
2021-01-07
pjp
if (rbt->flags & RBT_CACHE) {
2164
2021-01-05
pjp
zonenumberx = (uint32_t)-1;
2165
2021-01-07
pjp
} else {
2166
2021-01-07
pjp
res = zone_findzone(rbt);
2167
2021-01-07
pjp
if (res != NULL)
2168
2021-01-07
pjp
zonenumberx = res->zonenumber;
2169
2021-01-07
pjp
else
2170
2021-01-07
pjp
zonenumberx = (uint32_t)-1;
2171
2021-01-07
pjp
}
2172
2021-01-05
pjp
2173
2019-02-27
pjp
if ((find_rr(rbt, DNS_TYPE_RRSIG)) == 0)
2174
2015-06-25
pjp
return -1;
2175
2015-06-25
pjp
2176
2015-06-25
pjp
if (istcp) {
2177
2015-06-25
pjp
replysize = 65535;
2178
2015-06-25
pjp
}
2179
2015-06-25
pjp
2180
2015-06-25
pjp
if (! istcp && q->edns0len > 512)
2181
2020-09-30
pjp
replysize = MIN(q->edns0len, max_udp_payload);
2182
2015-06-25
pjp
2183
2015-06-25
pjp
odh = (struct dns_header *)&reply[0];
2184
2015-06-25
pjp
2185
2015-06-25
pjp
outlen = sizeof(struct dns_header);
2186
2015-06-25
pjp
2187
2015-06-25
pjp
if (len > replysize) {
2188
2015-06-25
pjp
return (retlen);
2189
2015-06-25
pjp
}
2190
2015-06-25
pjp
2191
2015-06-25
pjp
memcpy(reply, buf, sizeof(struct dns_header) + q->hdr->namelen + 4);
2192
2015-06-25
pjp
2193
2015-06-25
pjp
outlen += (q->hdr->namelen + 4);
2194
2017-09-18
pjp
rollback = outlen;
2195
2015-06-25
pjp
2196
2020-07-18
pjp
memset((char *)&odh->query, 0, sizeof(u_int16_t));
2197
2020-07-20
pjp
set_reply_flags(rbt, odh, q);
2198
2015-06-25
pjp
2199
2015-06-25
pjp
odh->question = htons(1);
2200
2015-06-25
pjp
odh->nsrr = 0;
2201
2015-06-25
pjp
odh->additional = 0;
2202
2015-06-25
pjp
2203
2020-07-06
pjp
tmplen = additional_rrsig(q->hdr->name, q->hdr->namelen, -1, rbt, reply, replysize, outlen, &a_count, q->aa);
2204
2020-04-01
pjp
if (tmplen == 0) {
2205
2020-07-08
pjp
/* we're forwarding and had no RRSIG return with -1 */
2206
2020-07-08
pjp
if (q->aa != 1)
2207
2020-07-08
pjp
return -1;
2208
2020-07-08
pjp
2209
2020-04-01
pjp
NTOHS(odh->query);
2210
2020-04-01
pjp
SET_DNS_TRUNCATION(odh);
2211
2020-04-01
pjp
HTONS(odh->query);
2212
2020-04-01
pjp
odh->answer = 0;
2213
2020-04-01
pjp
odh->nsrr = 0;
2214
2020-04-01
pjp
odh->additional = 0;
2215
2020-04-01
pjp
outlen = rollback;
2216
2020-04-01
pjp
goto out;
2217
2015-06-25
pjp
}
2218
2020-04-01
pjp
outlen = tmplen;
2219
2015-06-25
pjp
2220
2015-06-24
pjp
odh->answer = htons(a_count);
2221
2015-06-24
pjp
2222
2015-11-14
pjp
out:
2223
2015-06-24
pjp
if (q->edns0len) {
2224
2015-06-24
pjp
/* tag on edns0 opt record */
2225
2015-06-24
pjp
odh->additional = htons(1);
2226
2015-06-24
pjp
outlen = additional_opt(q, reply, replysize, outlen);
2227
2015-06-24
pjp
}
2228
2015-06-24
pjp
2229
2019-02-24
pjp
if (q->tsig.tsigverified == 1) {
2230
2021-01-07
pjp
outlen = additional_tsig(q, reply, replysize, outlen, 0, 0, NULL, DEFAULT_TSIG_FUDGE);
2231
2019-02-24
pjp
2232
2019-02-24
pjp
NTOHS(odh->additional);
2233
2019-02-24
pjp
odh->additional++;
2234
2019-02-24
pjp
HTONS(odh->additional);
2235
2019-02-24
pjp
}
2236
2019-02-24
pjp
2237
2015-12-19
pjp
if (istcp) {
2238
2015-12-19
pjp
char *tmpbuf;
2239
2015-12-19
pjp
2240
2015-12-19
pjp
tmpbuf = malloc(outlen + 2);
2241
2019-02-27
pjp
if (tmpbuf == 0) {
2242
2015-12-19
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
2243
2015-12-19
pjp
}
2244
2019-12-03
pjp
pack16(tmpbuf, htons(outlen));
2245
2015-12-19
pjp
memcpy(&tmpbuf[2], reply, outlen);
2246
2015-12-19
pjp
2247
2015-12-19
pjp
if ((retlen = send(so, tmpbuf, outlen + 2, 0)) < 0) {
2248
2015-12-19
pjp
dolog(LOG_INFO, "send: %s\n", strerror(errno));
2249
2015-12-19
pjp
}
2250
2015-12-19
pjp
free(tmpbuf);
2251
2015-12-19
pjp
} else {
2252
2020-07-21
pjp
if (q->rawsocket) {
2253
2020-07-21
pjp
*sretlen = retlen = outlen;
2254
2020-07-21
pjp
} else {
2255
2020-07-21
pjp
if ((retlen = sendto(so, reply, outlen, 0, sa, salen)) < 0) {
2256
2020-07-21
pjp
dolog(LOG_INFO, "sendto: %s\n", strerror(errno));
2257
2020-07-21
pjp
}
2258
2015-06-24
pjp
}
2259
2015-12-19
pjp
}
2260
2015-06-24
pjp
2261
2015-06-20
pjp
return (retlen);
2262
2015-06-20
pjp
2263
2015-06-20
pjp
2264
2015-06-20
pjp
}
2265
2015-06-20
pjp
2266
2015-06-20
pjp
/*
2267
2014-11-14
pjp
* REPLY_AAAA() - replies a DNS question (*q) on socket (so)
2268
2014-11-14
pjp
*
2269
2014-11-14
pjp
*/
2270
2014-11-14
pjp
2271
2014-11-14
pjp
int
2272
2020-07-21
pjp
reply_aaaa(struct sreply *sreply, int *sretlen, ddDB *db)
2273
2014-11-14
pjp
{
2274
2014-11-14
pjp
char *reply = sreply->replybuf;
2275
2014-11-14
pjp
struct dns_header *odh;
2276
2015-06-20
pjp
u_int16_t outlen = 0;
2277
2014-11-14
pjp
int aaaa_count;
2278
2014-11-14
pjp
2279
2014-11-14
pjp
struct answer {
2280
2014-11-14
pjp
char name[2];
2281
2014-11-14
pjp
u_int16_t type;
2282
2014-11-14
pjp
u_int16_t class;
2283
2014-11-14
pjp
u_int32_t ttl;
2284
2014-11-14
pjp
u_int16_t rdlength; /* 12 */
2285
2014-11-14
pjp
char rdata; /* 12 + 16 */
2286
2014-11-14
pjp
} __attribute__((packed));
2287
2014-11-14
pjp
2288
2014-11-14
pjp
struct answer *answer;
2289
2014-11-14
pjp
2290
2014-11-14
pjp
int so = sreply->so;
2291
2014-11-14
pjp
char *buf = sreply->buf;
2292
2014-11-14
pjp
int len = sreply->len;
2293
2014-11-14
pjp
struct question *q = sreply->q;
2294
2014-11-14
pjp
struct sockaddr *sa = sreply->sa;
2295
2014-11-14
pjp
int salen = sreply->salen;
2296
2019-02-15
pjp
struct rbtree *rbt = sreply->rbt1;
2297
2019-02-15
pjp
struct rr *rrp = NULL;
2298
2019-02-15
pjp
struct rrset *rrset = NULL;
2299
2014-11-14
pjp
int istcp = sreply->istcp;
2300
2014-11-14
pjp
int replysize = 512;
2301
2014-11-14
pjp
int retlen = -1;
2302
2017-09-18
pjp
u_int16_t rollback;
2303
2020-07-06
pjp
time_t now;
2304
2021-01-05
pjp
uint32_t zonenumberx;
2305
2021-01-05
pjp
struct zoneentry *res = NULL;
2306
2014-11-14
pjp
2307
2021-01-07
pjp
if (rbt->flags & RBT_CACHE) {
2308
2021-01-05
pjp
zonenumberx = (uint32_t)-1;
2309
2021-01-07
pjp
} else {
2310
2021-01-07
pjp
res = zone_findzone(rbt);
2311
2021-01-07
pjp
if (res != NULL)
2312
2021-01-07
pjp
zonenumberx = res->zonenumber;
2313
2021-01-07
pjp
else
2314
2021-01-07
pjp
zonenumberx = (uint32_t)-1;
2315
2021-01-07
pjp
}
2316
2021-01-05
pjp
2317
2020-07-06
pjp
now = time(NULL);
2318
2020-07-06
pjp
2319
2019-02-27
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_AAAA)) == 0)
2320
2015-06-16
pjp
return -1;
2321
2015-06-16
pjp
2322
2014-11-14
pjp
if (istcp) {
2323
2014-11-14
pjp
replysize = 65535;
2324
2014-11-14
pjp
}
2325
2014-11-14
pjp
2326
2015-06-20
pjp
if (! istcp && q->edns0len > 512)
2327
2020-09-30
pjp
replysize = MIN(q->edns0len, max_udp_payload);
2328
2014-11-14
pjp
2329
2014-11-14
pjp
2330
2014-11-14
pjp
odh = (struct dns_header *)&reply[0];
2331
2014-11-14
pjp
2332
2014-11-14
pjp
outlen = sizeof(struct dns_header);
2333
2014-11-14
pjp
2334
2014-11-14
pjp
if (len > replysize) {
2335
2014-11-14
pjp
return (retlen);
2336
2014-11-14
pjp
}
2337
2014-11-14
pjp
2338
2014-11-14
pjp
memcpy(reply, buf, sizeof(struct dns_header) + q->hdr->namelen + 4);
2339
2014-11-14
pjp
2340
2014-11-14
pjp
outlen += (q->hdr->namelen + 4);
2341
2017-09-18
pjp
rollback = outlen;
2342
2014-11-14
pjp
2343
2020-07-18
pjp
memset((char *)&odh->query, 0, sizeof(u_int16_t));
2344
2020-07-20
pjp
set_reply_flags(rbt, odh, q);
2345
2014-11-14
pjp
2346
2014-11-14
pjp
odh->question = htons(1);
2347
2019-02-15
pjp
odh->answer = htons(0);
2348
2014-11-14
pjp
odh->nsrr = 0;
2349
2014-11-14
pjp
odh->additional = 0;
2350
2014-11-14
pjp
2351
2014-11-14
pjp
/* skip dns header, question name, qtype and qclass */
2352
2014-11-14
pjp
answer = (struct answer *)(&reply[0] + sizeof(struct dns_header) +
2353
2014-11-14
pjp
q->hdr->namelen + 4);
2354
2014-11-14
pjp
2355
2014-11-14
pjp
2356
2014-11-14
pjp
aaaa_count = 0;
2357
2014-11-14
pjp
2358
2019-02-15
pjp
TAILQ_FOREACH(rrp, &rrset->rr_head, entries) {
2359
2021-01-05
pjp
if (rrp->zonenumber != zonenumberx)
2360
2021-01-05
pjp
continue;
2361
2014-11-14
pjp
answer->name[0] = 0xc0;
2362
2014-11-14
pjp
answer->name[1] = 0x0c;
2363
2014-11-14
pjp
answer->type = q->hdr->qtype;
2364
2014-11-14
pjp
answer->class = q->hdr->qclass;
2365
2020-07-06
pjp
2366
2020-07-06
pjp
if (q->aa)
2367
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl);
2368
2020-07-06
pjp
else
2369
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl - (MIN(rrset->ttl, difftime(now, rrset->created))));
2370
2014-11-14
pjp
2371
2014-11-14
pjp
answer->rdlength = htons(sizeof(struct in6_addr));
2372
2014-11-14
pjp
2373
2019-02-15
pjp
memcpy((char *)&answer->rdata, (char *)&((struct aaaa *)rrp->rdata)->aaaa, sizeof(struct in6_addr));
2374
2014-11-14
pjp
outlen += 28;
2375
2014-11-14
pjp
2376
2015-06-27
pjp
aaaa_count++;
2377
2015-06-27
pjp
2378
2015-06-27
pjp
/* set new offset for answer */
2379
2015-06-27
pjp
answer = (struct answer *)&reply[outlen];
2380
2019-02-15
pjp
};
2381
2015-06-27
pjp
2382
2019-02-15
pjp
odh->answer = htons(aaaa_count);
2383
2019-02-15
pjp
2384
2015-06-27
pjp
/* RRSIG reply_aaaa */
2385
2019-11-11
pjp
if (dnssec && q->dnssecok && (rbt->flags & RBT_DNSSEC)) {
2386
2015-06-27
pjp
int tmplen = 0;
2387
2015-06-27
pjp
int origlen = outlen;
2388
2020-04-01
pjp
int retcount;
2389
2015-06-27
pjp
2390
2020-07-06
pjp
tmplen = additional_rrsig(q->hdr->name, q->hdr->namelen, DNS_TYPE_AAAA, rbt, reply, replysize, outlen, &retcount, q->aa);
2391
2015-06-27
pjp
2392
2015-06-27
pjp
if (tmplen == 0) {
2393
2020-07-08
pjp
2394
2020-07-08
pjp
/* we're forwarding and had no RRSIG return with -1 */
2395
2020-07-08
pjp
if (q->aa != 1)
2396
2020-07-08
pjp
return -1;
2397
2020-07-08
pjp
2398
2014-11-14
pjp
NTOHS(odh->query);
2399
2014-11-14
pjp
SET_DNS_TRUNCATION(odh);
2400
2014-11-14
pjp
HTONS(odh->query);
2401
2017-09-18
pjp
odh->answer = 0;
2402
2017-09-18
pjp
odh->nsrr = 0;
2403
2017-09-18
pjp
odh->additional = 0;
2404
2017-09-18
pjp
outlen = rollback;
2405
2014-11-14
pjp
goto out;
2406
2014-11-14
pjp
}
2407
2014-11-14
pjp
2408
2015-06-27
pjp
outlen = tmplen;
2409
2014-11-14
pjp
2410
2015-06-27
pjp
if (outlen > origlen)
2411
2020-04-01
pjp
odh->answer = htons(aaaa_count + retcount);
2412
2014-11-14
pjp
2413
2015-06-27
pjp
}
2414
2015-06-27
pjp
2415
2015-11-14
pjp
out:
2416
2014-11-14
pjp
if (q->edns0len) {
2417
2014-11-14
pjp
/* tag on edns0 opt record */
2418
2014-11-14
pjp
odh->additional = htons(1);
2419
2014-11-14
pjp
outlen = additional_opt(q, reply, replysize, outlen);
2420
2014-11-14
pjp
}
2421
2014-11-14
pjp
2422
2019-02-24
pjp
if (q->tsig.tsigverified == 1) {
2423
2021-01-07
pjp
outlen = additional_tsig(q, reply, replysize, outlen, 0, 0, NULL, DEFAULT_TSIG_FUDGE);
2424
2019-02-24
pjp
2425
2019-02-24
pjp
NTOHS(odh->additional);
2426
2019-02-24
pjp
odh->additional++;
2427
2019-02-24
pjp
HTONS(odh->additional);
2428
2019-02-24
pjp
}
2429
2019-02-24
pjp
2430
2019-02-24
pjp
2431
2015-12-19
pjp
if (istcp) {
2432
2015-12-19
pjp
char *tmpbuf;
2433
2015-12-19
pjp
2434
2015-12-19
pjp
tmpbuf = malloc(outlen + 2);
2435
2019-02-27
pjp
if (tmpbuf == 0) {
2436
2015-12-19
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
2437
2015-12-19
pjp
}
2438
2019-12-03
pjp
pack16(tmpbuf, htons(outlen));
2439
2015-12-19
pjp
memcpy(&tmpbuf[2], reply, outlen);
2440
2015-12-19
pjp
2441
2015-12-19
pjp
if ((retlen = send(so, tmpbuf, outlen + 2, 0)) < 0) {
2442
2015-12-19
pjp
dolog(LOG_INFO, "send: %s\n", strerror(errno));
2443
2015-12-19
pjp
}
2444
2015-12-19
pjp
free(tmpbuf);
2445
2015-12-19
pjp
} else {
2446
2020-07-21
pjp
if (q->rawsocket) {
2447
2020-07-21
pjp
*sretlen = retlen = outlen;
2448
2020-07-21
pjp
} else {
2449
2020-07-21
pjp
if ((retlen = sendto(so, reply, outlen, 0, sa, salen)) < 0) {
2450
2020-07-21
pjp
dolog(LOG_INFO, "sendto: %s\n", strerror(errno));
2451
2020-07-21
pjp
}
2452
2014-11-14
pjp
}
2453
2014-11-14
pjp
}
2454
2014-11-14
pjp
2455
2019-02-15
pjp
rotate_rr(rrset);
2456
2014-11-14
pjp
2457
2014-11-14
pjp
return (retlen);
2458
2014-11-14
pjp
}
2459
2014-11-14
pjp
2460
2014-11-14
pjp
/*
2461
2014-11-14
pjp
* REPLY_MX() - replies a DNS question (*q) on socket (so)
2462
2014-11-14
pjp
*
2463
2014-11-14
pjp
*/
2464
2014-11-14
pjp
2465
2014-11-14
pjp
int
2466
2020-07-21
pjp
reply_mx(struct sreply *sreply, int *sretlen, ddDB *db)
2467
2014-11-14
pjp
{
2468
2014-11-14
pjp
char *reply = sreply->replybuf;
2469
2014-11-14
pjp
struct dns_header *odh;
2470
2014-11-14
pjp
int mx_count;
2471
2014-11-14
pjp
char *name;
2472
2015-06-20
pjp
u_int16_t outlen = 0;
2473
2014-11-14
pjp
u_int16_t namelen;
2474
2015-06-20
pjp
int tmplen = 0;
2475
2014-11-14
pjp
2476
2014-11-14
pjp
struct answer {
2477
2014-11-14
pjp
char name[2];
2478
2014-11-14
pjp
u_int16_t type;
2479
2014-11-14
pjp
u_int16_t class;
2480
2014-11-14
pjp
u_int32_t ttl;
2481
2014-11-14
pjp
u_int16_t rdlength; /* 12 */
2482
2014-11-14
pjp
u_int16_t mx_priority;
2483
2014-11-14
pjp
char exchange;
2484
2014-11-14
pjp
} __attribute__((packed));
2485
2014-11-14
pjp
2486
2014-11-14
pjp
struct answer *answer;
2487
2014-11-14
pjp
2488
2014-11-14
pjp
int so = sreply->so;
2489
2014-11-14
pjp
char *buf = sreply->buf;
2490
2014-11-14
pjp
int len = sreply->len;
2491
2014-11-14
pjp
struct question *q = sreply->q;
2492
2014-11-14
pjp
struct sockaddr *sa = sreply->sa;
2493
2014-11-14
pjp
int salen = sreply->salen;
2494
2019-02-15
pjp
struct rbtree *rbt = sreply->rbt1;
2495
2019-11-11
pjp
struct rbtree *rbt0 = NULL;
2496
2019-02-15
pjp
struct rrset *rrset = NULL;
2497
2019-02-15
pjp
struct rr *rrp = NULL;
2498
2014-11-14
pjp
int istcp = sreply->istcp;
2499
2014-11-14
pjp
int replysize = 512;
2500
2014-11-14
pjp
int retlen = -1;
2501
2017-09-18
pjp
u_int16_t rollback;
2502
2020-07-06
pjp
time_t now;
2503
2019-11-11
pjp
2504
2019-11-11
pjp
int addiscount;
2505
2019-11-11
pjp
2506
2019-11-11
pjp
SLIST_HEAD(, addis) addishead;
2507
2019-11-11
pjp
struct addis {
2508
2019-11-11
pjp
char name[DNS_MAXNAME];
2509
2019-11-11
pjp
int namelen;
2510
2019-11-12
pjp
int contained;
2511
2019-11-11
pjp
SLIST_ENTRY(addis) addis_entries;
2512
2019-11-11
pjp
} *ad0, *ad1;
2513
2021-01-05
pjp
uint32_t zonenumberx;
2514
2021-01-05
pjp
struct zoneentry *res = NULL;
2515
2019-11-11
pjp
2516
2021-01-07
pjp
if (rbt->flags & RBT_CACHE) {
2517
2021-01-05
pjp
zonenumberx = (uint32_t)-1;
2518
2021-01-07
pjp
} else {
2519
2021-01-07
pjp
res = zone_findzone(rbt);
2520
2021-01-07
pjp
if (res != NULL)
2521
2021-01-07
pjp
zonenumberx = res->zonenumber;
2522
2021-01-07
pjp
else
2523
2021-01-07
pjp
zonenumberx = (uint32_t)-1;
2524
2021-01-07
pjp
}
2525
2021-01-05
pjp
2526
2019-11-11
pjp
SLIST_INIT(&addishead);
2527
2019-11-11
pjp
/* check for apex, delegations */
2528
2020-07-06
pjp
2529
2020-07-06
pjp
now = time(NULL);
2530
2016-01-29
pjp
2531
2019-02-27
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_MX)) == 0)
2532
2016-01-29
pjp
return -1;
2533
2016-01-29
pjp
2534
2016-01-29
pjp
2535
2014-11-14
pjp
if (istcp) {
2536
2014-11-14
pjp
replysize = 65535;
2537
2014-11-14
pjp
}
2538
2014-11-14
pjp
2539
2015-06-20
pjp
if (! istcp && q->edns0len > 512)
2540
2020-09-30
pjp
replysize = MIN(q->edns0len, max_udp_payload);
2541
2014-11-14
pjp
2542
2014-11-14
pjp
odh = (struct dns_header *)&reply[0];
2543
2014-11-14
pjp
2544
2014-11-14
pjp
outlen = sizeof(struct dns_header);
2545
2014-11-14
pjp
2546
2014-11-14
pjp
if (len > replysize) {
2547
2014-11-14
pjp
return (retlen);
2548
2014-11-14
pjp
}
2549
2014-11-14
pjp
2550
2014-11-14
pjp
memcpy(reply, buf, sizeof(struct dns_header) + q->hdr->namelen + 4);
2551
2014-11-14
pjp
2552
2014-11-14
pjp
outlen += (q->hdr->namelen + 4);
2553
2017-09-18
pjp
rollback = outlen;
2554
2014-11-14
pjp
2555
2020-07-18
pjp
memset((char *)&odh->query, 0, sizeof(u_int16_t));
2556
2020-07-20
pjp
set_reply_flags(rbt, odh, q);
2557
2014-11-14
pjp
2558
2014-11-14
pjp
odh->question = htons(1);
2559
2019-02-15
pjp
odh->answer = htons(0);
2560
2014-11-14
pjp
odh->nsrr = 0;
2561
2014-11-14
pjp
odh->additional = 0;
2562
2014-11-14
pjp
2563
2014-11-14
pjp
/* skip dns header, question name, qtype and qclass */
2564
2014-11-14
pjp
answer = (struct answer *)(&reply[0] + sizeof(struct dns_header) +
2565
2014-11-14
pjp
q->hdr->namelen + 4);
2566
2014-11-14
pjp
2567
2014-11-14
pjp
mx_count = 0;
2568
2019-02-15
pjp
2569
2019-02-15
pjp
TAILQ_FOREACH(rrp, &rrset->rr_head, entries) {
2570
2021-01-05
pjp
if (rrp->zonenumber != zonenumberx)
2571
2021-01-05
pjp
continue;
2572
2014-11-14
pjp
answer->name[0] = 0xc0;
2573
2014-11-14
pjp
answer->name[1] = 0x0c;
2574
2014-11-14
pjp
answer->type = q->hdr->qtype;
2575
2014-11-14
pjp
answer->class = q->hdr->qclass;
2576
2020-07-06
pjp
2577
2020-07-06
pjp
if (q->aa)
2578
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl);
2579
2020-07-06
pjp
else
2580
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl - (MIN(rrset->ttl, difftime(now, rrset->created))));
2581
2014-11-14
pjp
2582
2019-02-15
pjp
answer->rdlength = htons(sizeof(u_int16_t) + ((struct smx *)rrp->rdata)->exchangelen);
2583
2014-11-14
pjp
2584
2019-02-15
pjp
answer->mx_priority = htons(((struct smx *)rrp->rdata)->preference);
2585
2019-02-15
pjp
memcpy((char *)&answer->exchange, (char *)((struct smx *)rrp->rdata)->exchange, ((struct smx *)rrp->rdata)->exchangelen);
2586
2014-11-14
pjp
2587
2019-02-15
pjp
name = ((struct smx *)rrp->rdata)->exchange;
2588
2019-02-15
pjp
namelen = ((struct smx *)rrp->rdata)->exchangelen;
2589
2014-11-14
pjp
2590
2019-02-15
pjp
outlen += (12 + 2 + ((struct smx *)rrp->rdata)->exchangelen);
2591
2014-11-14
pjp
2592
2015-06-20
pjp
/* can we afford to write another header? if no truncate */
2593
2019-02-15
pjp
if ((outlen + 12 + 2 + ((struct smx *)rrp->rdata)->exchangelen) > replysize) {
2594
2015-06-20
pjp
NTOHS(odh->query);
2595
2015-06-20
pjp
SET_DNS_TRUNCATION(odh);
2596
2015-06-20
pjp
HTONS(odh->query);
2597
2017-09-18
pjp
odh->answer = 0;
2598
2017-09-18
pjp
odh->nsrr = 0;
2599
2017-09-18
pjp
odh->additional = 0;
2600
2017-09-18
pjp
outlen = rollback;
2601
2015-06-20
pjp
goto out;
2602
2014-11-14
pjp
}
2603
2014-11-14
pjp
2604
2019-11-11
pjp
ad0 = malloc(sizeof(struct addis));
2605
2019-11-11
pjp
if (ad0 == NULL) {
2606
2019-11-11
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
2607
2019-11-11
pjp
return -1;
2608
2019-11-11
pjp
}
2609
2019-11-11
pjp
2610
2019-11-11
pjp
memcpy(ad0->name, name, namelen);
2611
2019-11-11
pjp
ad0->namelen = namelen;
2612
2019-11-11
pjp
2613
2019-11-12
pjp
if (dn_contains(name, namelen, rbt->zone, rbt->zonelen) == 1)
2614
2019-11-12
pjp
ad0->contained = 1;
2615
2019-11-12
pjp
else
2616
2019-11-12
pjp
ad0->contained = 0;
2617
2019-11-12
pjp
2618
2019-11-11
pjp
SLIST_INSERT_HEAD(&addishead, ad0, addis_entries);
2619
2019-11-11
pjp
2620
2015-06-20
pjp
/* set new offset for answer */
2621
2015-06-20
pjp
answer = (struct answer *)&reply[outlen];
2622
2019-02-15
pjp
mx_count++;
2623
2019-02-15
pjp
}
2624
2015-06-20
pjp
2625
2019-02-24
pjp
odh->answer = htons(mx_count);
2626
2019-02-15
pjp
2627
2015-06-27
pjp
/* RRSIG reply_mx*/
2628
2015-06-27
pjp
2629
2019-11-11
pjp
if (dnssec && q->dnssecok && (rbt->flags & RBT_DNSSEC)) {
2630
2015-06-22
pjp
int origlen = outlen;
2631
2020-04-01
pjp
int retcount;
2632
2015-06-20
pjp
2633
2020-07-06
pjp
tmplen = additional_rrsig(q->hdr->name, q->hdr->namelen, DNS_TYPE_MX, rbt, reply, replysize, outlen, &retcount, q->aa);
2634
2015-06-22
pjp
2635
2015-06-20
pjp
if (tmplen == 0) {
2636
2020-07-08
pjp
2637
2020-07-08
pjp
/* we're forwarding and had no RRSIG return with -1 */
2638
2020-07-08
pjp
if (q->aa != 1)
2639
2020-07-08
pjp
return -1;
2640
2020-07-08
pjp
2641
2014-11-14
pjp
NTOHS(odh->query);
2642
2014-11-14
pjp
SET_DNS_TRUNCATION(odh);
2643
2014-11-14
pjp
HTONS(odh->query);
2644
2017-09-18
pjp
odh->answer = 0;
2645
2017-09-18
pjp
odh->nsrr = 0;
2646
2017-09-18
pjp
odh->additional = 0;
2647
2017-09-18
pjp
outlen = rollback;
2648
2014-11-14
pjp
goto out;
2649
2014-11-14
pjp
}
2650
2014-11-14
pjp
2651
2015-06-20
pjp
outlen = tmplen;
2652
2014-11-14
pjp
2653
2015-06-22
pjp
if (outlen > origlen)
2654
2020-04-01
pjp
odh->answer = htons(mx_count + retcount);
2655
2014-11-14
pjp
2656
2015-06-20
pjp
}
2657
2014-11-14
pjp
2658
2019-11-11
pjp
/* tack on additional A or AAAA records */
2659
2019-11-11
pjp
2660
2019-11-11
pjp
SLIST_FOREACH(ad0, &addishead, addis_entries) {
2661
2019-11-12
pjp
if (ad0->contained == 0)
2662
2019-11-12
pjp
continue;
2663
2019-11-12
pjp
2664
2019-11-11
pjp
addiscount = 0;
2665
2019-11-11
pjp
rbt0 = find_rrset(db, ad0->name, ad0->namelen);
2666
2019-11-11
pjp
if (rbt0 != NULL && find_rr(rbt0, DNS_TYPE_AAAA) != NULL) {
2667
2019-11-11
pjp
tmplen = additional_aaaa(ad0->name, ad0->namelen, rbt0, reply, replysize, outlen, &addiscount);
2668
2019-11-11
pjp
if (tmplen == 0) {
2669
2019-11-11
pjp
NTOHS(odh->query);
2670
2019-11-11
pjp
SET_DNS_TRUNCATION(odh);
2671
2019-11-11
pjp
HTONS(odh->query);
2672
2019-11-11
pjp
odh->answer = 0;
2673
2019-11-11
pjp
odh->nsrr = 0;
2674
2019-11-11
pjp
odh->additional = 0;
2675
2019-11-11
pjp
outlen = rollback;
2676
2019-11-11
pjp
goto out;
2677
2019-11-11
pjp
}
2678
2019-11-11
pjp
2679
2019-11-11
pjp
outlen = tmplen;
2680
2019-11-11
pjp
NTOHS(odh->additional);
2681
2019-11-11
pjp
odh->additional += addiscount;
2682
2019-11-11
pjp
HTONS(odh->additional);
2683
2019-11-11
pjp
2684
2019-11-11
pjp
/* additional RRSIG for the additional AAAA */
2685
2019-11-11
pjp
if (dnssec && q->dnssecok && (rbt0->flags & RBT_DNSSEC)) {
2686
2020-04-01
pjp
int retcount;
2687
2019-11-11
pjp
2688
2020-07-06
pjp
tmplen = additional_rrsig(ad0->name, ad0->namelen, DNS_TYPE_AAAA, rbt0, reply, replysize, outlen, &retcount, q->aa);
2689
2020-04-01
pjp
2690
2019-11-11
pjp
if (tmplen == 0) {
2691
2020-07-08
pjp
/* we're forwarding and had no RRSIG return with -1 */
2692
2020-07-08
pjp
if (q->aa != 1)
2693
2020-07-08
pjp
return -1;
2694
2020-07-08
pjp
2695
2019-11-11
pjp
NTOHS(odh->query);
2696
2019-11-11
pjp
SET_DNS_TRUNCATION(odh);
2697
2019-11-11
pjp
HTONS(odh->query);
2698
2019-11-11
pjp
odh->answer = 0;
2699
2019-11-11
pjp
odh->nsrr = 0;
2700
2019-11-11
pjp
odh->additional = 0;
2701
2019-11-11
pjp
outlen = rollback;
2702
2019-11-11
pjp
goto out;
2703
2019-11-11
pjp
}
2704
2019-11-11
pjp
2705
2019-11-11
pjp
NTOHS(odh->additional);
2706
2020-04-01
pjp
odh->additional += retcount;
2707
2019-11-11
pjp
HTONS(odh->additional);
2708
2019-11-11
pjp
2709
2019-11-11
pjp
outlen = tmplen;
2710
2019-11-11
pjp
}
2711
2019-11-11
pjp
2712
2019-11-11
pjp
rbt0 = NULL;
2713
2019-11-11
pjp
}
2714
2019-11-11
pjp
2715
2019-11-11
pjp
addiscount = 0;
2716
2019-11-11
pjp
rbt0 = find_rrset(db, ad0->name, ad0->namelen);
2717
2019-11-11
pjp
if (rbt0 != NULL && find_rr(rbt0, DNS_TYPE_A) != NULL) {
2718
2019-11-11
pjp
tmplen = additional_a(ad0->name, ad0->namelen, rbt0, reply, replysize, outlen, &addiscount);
2719
2019-11-11
pjp
if (tmplen == 0) {
2720
2019-11-11
pjp
NTOHS(odh->query);
2721
2019-11-11
pjp
SET_DNS_TRUNCATION(odh);
2722
2019-11-11
pjp
HTONS(odh->query);
2723
2019-11-11
pjp
odh->answer = 0;
2724
2019-11-11
pjp
odh->nsrr = 0;
2725
2019-11-11
pjp
odh->additional = 0;
2726
2019-11-11
pjp
outlen = rollback;
2727
2019-11-11
pjp
goto out;
2728
2019-11-11
pjp
}
2729
2019-11-11
pjp
2730
2019-11-11
pjp
outlen = tmplen;
2731
2019-11-11
pjp
NTOHS(odh->additional);
2732
2019-11-11
pjp
odh->additional += addiscount;
2733
2019-11-11
pjp
HTONS(odh->additional);
2734
2019-11-11
pjp
2735
2019-11-11
pjp
/* additional RRSIG for the additional A RR */
2736
2019-11-11
pjp
if (dnssec && q->dnssecok && (rbt0->flags & RBT_DNSSEC)) {
2737
2020-04-01
pjp
int retcount;
2738
2019-11-11
pjp
2739
2020-07-06
pjp
tmplen = additional_rrsig(ad0->name, ad0->namelen, DNS_TYPE_A, rbt0, reply, replysize, outlen, &retcount, q->aa);
2740
2020-04-01
pjp
2741
2019-11-11
pjp
if (tmplen == 0) {
2742
2020-07-08
pjp
/* we're forwarding and had no RRSIG return with -1 */
2743
2020-07-08
pjp
if (q->aa != 1)
2744
2020-07-08
pjp
return -1;
2745
2020-07-08
pjp
2746
2019-11-11
pjp
NTOHS(odh->query);
2747
2019-11-11
pjp
SET_DNS_TRUNCATION(odh);
2748
2019-11-11
pjp
HTONS(odh->query);
2749
2019-11-11
pjp
odh->answer = 0;
2750
2019-11-11
pjp
odh->nsrr = 0;
2751
2019-11-11
pjp
odh->additional = 0;
2752
2019-11-11
pjp
outlen = rollback;
2753
2019-11-11
pjp
goto out;
2754
2019-11-11
pjp
}
2755
2019-11-11
pjp
2756
2019-11-11
pjp
NTOHS(odh->additional);
2757
2020-04-01
pjp
odh->additional += retcount;
2758
2019-11-11
pjp
HTONS(odh->additional);
2759
2019-11-11
pjp
2760
2019-11-11
pjp
outlen = tmplen;
2761
2019-11-11
pjp
}
2762
2019-11-11
pjp
2763
2019-11-11
pjp
rbt0 = NULL;
2764
2019-11-11
pjp
}
2765
2019-11-11
pjp
2766
2019-11-11
pjp
}
2767
2019-11-11
pjp
2768
2015-11-14
pjp
out:
2769
2019-11-11
pjp
while (!SLIST_EMPTY(&addishead)) { /* clean up */
2770
2019-11-11
pjp
ad1 = SLIST_FIRST(&addishead);
2771
2019-11-11
pjp
SLIST_REMOVE_HEAD(&addishead, addis_entries);
2772
2019-11-11
pjp
free(ad1);
2773
2019-11-11
pjp
}
2774
2019-11-11
pjp
2775
2014-11-14
pjp
if (q->edns0len) {
2776
2014-11-14
pjp
/* tag on edns0 opt record */
2777
2014-11-14
pjp
NTOHS(odh->additional);
2778
2014-11-14
pjp
odh->additional++;
2779
2014-11-14
pjp
HTONS(odh->additional);
2780
2014-11-14
pjp
2781
2014-11-14
pjp
outlen = additional_opt(q, reply, replysize, outlen);
2782
2014-11-14
pjp
}
2783
2014-11-14
pjp
2784
2019-02-24
pjp
if (q->tsig.tsigverified == 1) {
2785
2021-01-07
pjp
outlen = additional_tsig(q, reply, replysize, outlen, 0, 0, NULL, DEFAULT_TSIG_FUDGE);
2786
2019-02-24
pjp
2787
2019-02-24
pjp
NTOHS(odh->additional);
2788
2019-02-24
pjp
odh->additional++;
2789
2019-02-24
pjp
HTONS(odh->additional);
2790
2019-02-24
pjp
}
2791
2019-02-24
pjp
2792
2015-12-19
pjp
if (istcp) {
2793
2015-12-19
pjp
char *tmpbuf;
2794
2015-06-20
pjp
2795
2015-12-19
pjp
tmpbuf = malloc(outlen + 2);
2796
2019-02-27
pjp
if (tmpbuf == 0) {
2797
2015-12-19
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
2798
2015-12-19
pjp
}
2799
2019-12-03
pjp
pack16(tmpbuf, htons(outlen));
2800
2015-12-19
pjp
memcpy(&tmpbuf[2], reply, outlen);
2801
2015-12-19
pjp
if ((retlen = send(so, tmpbuf, outlen + 2, 0)) < 0) {
2802
2015-12-19
pjp
dolog(LOG_INFO, "send: %s\n", strerror(errno));
2803
2015-12-19
pjp
}
2804
2015-12-19
pjp
free(tmpbuf);
2805
2015-12-19
pjp
} else {
2806
2020-07-21
pjp
if (q->rawsocket) {
2807
2020-07-21
pjp
*sretlen = retlen = outlen;
2808
2020-07-21
pjp
} else {
2809
2020-07-21
pjp
if ((retlen = sendto(so, reply, outlen, 0, sa, salen)) < 0) {
2810
2020-07-21
pjp
dolog(LOG_INFO, "sendto: %s\n", strerror(errno));
2811
2020-07-21
pjp
}
2812
2014-11-14
pjp
}
2813
2014-11-14
pjp
}
2814
2014-11-14
pjp
2815
2014-11-14
pjp
return (retlen);
2816
2014-11-14
pjp
}
2817
2014-11-14
pjp
2818
2014-11-14
pjp
/*
2819
2014-11-14
pjp
* REPLY_NS() - replies a DNS question (*q) on socket (so)
2820
2014-11-14
pjp
*
2821
2014-11-14
pjp
*/
2822
2014-11-14
pjp
2823
2014-11-14
pjp
int
2824
2020-07-21
pjp
reply_ns(struct sreply *sreply, int *sretlen, ddDB *db)
2825
2014-11-14
pjp
{
2826
2014-11-14
pjp
char *reply = sreply->replybuf;
2827
2014-11-14
pjp
struct dns_header *odh;
2828
2015-06-20
pjp
int tmplen = 0;
2829
2014-11-14
pjp
int ns_count;
2830
2014-11-14
pjp
char *name;
2831
2015-06-20
pjp
u_int16_t outlen = 0;
2832
2014-11-14
pjp
u_int16_t namelen;
2833
2014-11-14
pjp
2834
2014-11-14
pjp
struct answer {
2835
2014-11-14
pjp
u_int16_t type;
2836
2014-11-14
pjp
u_int16_t class;
2837
2014-11-14
pjp
u_int32_t ttl;
2838
2014-11-14
pjp
u_int16_t rdlength; /* 12 */
2839
2014-11-14
pjp
char ns;
2840
2014-11-14
pjp
} __attribute__((packed));
2841
2014-11-14
pjp
2842
2014-11-14
pjp
struct answer *answer;
2843
2014-11-14
pjp
2844
2014-11-14
pjp
int so = sreply->so;
2845
2014-11-14
pjp
char *buf = sreply->buf;
2846
2014-11-14
pjp
int len = sreply->len;
2847
2014-11-14
pjp
struct question *q = sreply->q;
2848
2014-11-14
pjp
struct sockaddr *sa = sreply->sa;
2849
2014-11-14
pjp
int salen = sreply->salen;
2850
2019-02-15
pjp
struct rbtree *rbt = sreply->rbt1;
2851
2019-10-30
pjp
struct rbtree *rbt0 = NULL, *rbt1 = NULL;
2852
2019-10-30
pjp
struct rbtree *nrbt = NULL;
2853
2019-02-15
pjp
struct rrset *rrset = NULL;
2854
2019-02-15
pjp
struct rr *rrp = NULL;
2855
2014-11-14
pjp
int istcp = sreply->istcp;
2856
2014-11-14
pjp
int replysize = 512;
2857
2014-11-14
pjp
int retlen = -1;
2858
2017-09-18
pjp
u_int16_t rollback;
2859
2019-10-30
pjp
int delegation, addiscount;
2860
2019-11-09
pjp
int addcount = 0;
2861
2020-04-01
pjp
int retcount;
2862
2020-07-06
pjp
time_t now;
2863
2014-11-14
pjp
2864
2019-10-30
pjp
SLIST_HEAD(, addis) addishead;
2865
2019-10-30
pjp
struct addis {
2866
2019-10-30
pjp
char name[DNS_MAXNAME];
2867
2019-10-30
pjp
int namelen;
2868
2019-10-30
pjp
SLIST_ENTRY(addis) addis_entries;
2869
2019-10-30
pjp
} *ad0, *ad1;
2870
2021-01-05
pjp
uint32_t zonenumberx;
2871
2021-01-05
pjp
struct zoneentry *res = NULL;
2872
2019-10-30
pjp
2873
2021-01-07
pjp
if (rbt->flags & RBT_CACHE) {
2874
2021-01-05
pjp
zonenumberx = (uint32_t)-1;
2875
2021-01-07
pjp
} else {
2876
2021-01-07
pjp
res = zone_findzone(rbt);
2877
2021-01-07
pjp
if (res != NULL)
2878
2021-01-07
pjp
zonenumberx = res->zonenumber;
2879
2021-01-07
pjp
else
2880
2021-01-07
pjp
zonenumberx = (uint32_t)-1;
2881
2021-01-07
pjp
}
2882
2021-01-05
pjp
2883
2019-10-30
pjp
SLIST_INIT(&addishead);
2884
2019-10-30
pjp
/* check for apex, delegations */
2885
2020-07-06
pjp
2886
2020-07-06
pjp
now = time(NULL);
2887
2020-07-06
pjp
2888
2019-10-30
pjp
rbt1 = get_ns(db, rbt, &delegation);
2889
2019-10-30
pjp
2890
2019-10-30
pjp
if ((rrset = find_rr(rbt, DNS_TYPE_NS)) == NULL) {
2891
2015-06-16
pjp
return -1;
2892
2019-10-30
pjp
}
2893
2015-06-16
pjp
2894
2014-11-14
pjp
if (istcp) {
2895
2014-11-14
pjp
replysize = 65535;
2896
2014-11-14
pjp
}
2897
2014-11-14
pjp
2898
2015-06-20
pjp
if (! istcp && q->edns0len > 512)
2899
2020-09-30
pjp
replysize = MIN(q->edns0len, max_udp_payload);
2900
2014-11-14
pjp
2901
2014-11-14
pjp
odh = (struct dns_header *)&reply[0];
2902
2014-11-14
pjp
2903
2014-11-14
pjp
outlen = sizeof(struct dns_header);
2904
2014-11-14
pjp
2905
2014-11-14
pjp
if (len > replysize) {
2906
2014-11-14
pjp
return (retlen);
2907
2014-11-14
pjp
}
2908
2014-11-14
pjp
2909
2014-11-14
pjp
memcpy(reply, buf, sizeof(struct dns_header) + q->hdr->namelen + 4);
2910
2014-11-14
pjp
2911
2014-11-14
pjp
2912
2014-11-14
pjp
outlen += (q->hdr->namelen + 4);
2913
2017-09-18
pjp
rollback = outlen;
2914
2017-09-18
pjp
2915
2020-07-18
pjp
memset((char *)&odh->query, 0, sizeof(u_int16_t));
2916
2020-07-18
pjp
/* no set_reply_flags here, it differs */
2917
2020-07-18
pjp
2918
2014-11-14
pjp
SET_DNS_REPLY(odh);
2919
2019-10-30
pjp
2920
2020-07-06
pjp
if (! delegation && q->aa)
2921
2019-10-30
pjp
SET_DNS_AUTHORITATIVE(odh);
2922
2014-11-14
pjp
2923
2017-09-05
pjp
if (q->rd) {
2924
2017-09-05
pjp
SET_DNS_RECURSION(odh);
2925
2020-07-06
pjp
2926
2020-07-06
pjp
if (! q->aa)
2927
2020-07-06
pjp
SET_DNS_RECURSION_AVAIL(odh);
2928
2017-09-05
pjp
}
2929
2014-11-14
pjp
2930
2014-11-14
pjp
HTONS(odh->query);
2931
2014-11-14
pjp
2932
2014-11-14
pjp
odh->question = htons(1);
2933
2019-02-15
pjp
odh->answer = 0;
2934
2019-02-15
pjp
odh->nsrr = 0;
2935
2014-11-14
pjp
odh->additional = 0;
2936
2014-11-14
pjp
2937
2014-11-14
pjp
2938
2014-11-14
pjp
ns_count = 0;
2939
2014-11-14
pjp
2940
2019-02-15
pjp
TAILQ_FOREACH(rrp, &rrset->rr_head, entries) {
2941
2021-01-05
pjp
if (rrp->zonenumber != zonenumberx)
2942
2021-01-05
pjp
continue;
2943
2019-10-30
pjp
memcpy(&reply[outlen], rbt1->zone, rbt1->zonelen);
2944
2019-10-30
pjp
answer = (struct answer *)(&reply[outlen] + rbt1->zonelen);
2945
2014-11-14
pjp
answer->type = htons(DNS_TYPE_NS);
2946
2014-11-14
pjp
answer->class = q->hdr->qclass;
2947
2019-02-15
pjp
2948
2020-07-06
pjp
if (q->aa)
2949
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl);
2950
2020-07-06
pjp
else
2951
2020-07-06
pjp
answer->ttl = htonl(rrset->ttl - (MIN(rrset->ttl, difftime(now, rrset->created))));
2952
2020-07-06
pjp
2953
2019-02-15
pjp
name = ((struct ns *)rrp->rdata)->nsserver;
2954
2019-02-15
pjp
namelen = ((struct ns *)rrp->rdata)->nslen;
2955
2020-08-08
pjp
#if 0
2956
2019-02-15
pjp
ns_type = ((struct ns *)rrp->rdata)->ns_type;
2957
2020-08-08
pjp
#endif
2958
2014-11-14
pjp
2959
2014-11-14
pjp
answer->rdlength = htons(namelen);
2960
2014-11-14
pjp
2961
2014-11-14
pjp
memcpy((char *)&answer->ns, (char *)name, namelen);
2962
2014-11-14
pjp
2963
2019-10-30
pjp
outlen += (10 + namelen + rbt1->zonelen);
2964
2014-11-14
pjp
2965
2019-10-30
pjp
ad0 = malloc(sizeof(struct addis));
2966
2019-10-30
pjp
if (ad0 == NULL) {
2967
2019-10-30
pjp
dolog(LOG_INFO, "malloc: %s\n", strerror(errno));
2968
2019-10-30
pjp
return -1;
2969
2019-10-30
pjp
}
2970
2019-10-30
pjp
2971
2019-10-30
pjp
memcpy(ad0->name, name, namelen);
2972
2019-10-30
pjp
ad0->namelen = namelen;
2973
2019-10-30
pjp
2974
2019-10-30
pjp
SLIST_INSERT_HEAD(&addishead, ad0, addis_entries);
2975
2019-10-30
pjp
2976
2014-11-14
pjp
/* compress the label if possible */
2977
2014-11-14
pjp
if ((tmplen = compress_label((u_char*)reply, outlen, namelen)) > 0) {
2978
2014-11-14
pjp
/* XXX */
2979
2014-11-14
pjp
outlen = tmplen;
2980
2014-11-14
pjp
}
2981
2014-11-14
pjp
2982
2014-11-14
pjp
answer->rdlength = htons(&reply[outlen] - &answer->ns);
2983
2019-02-15
pjp
ns_count++;
2984
2019-02-15
pjp
}
2985
2014-11-14
pjp
2986
2019-10-30
pjp
if (delegation) {
2987
2019-02-15
pjp
odh->answer = 0;
2988
2019-02-15
pjp
odh->nsrr = htons(ns_count);
2989
2019-10-30
pjp
} else {
2990
2019-02-15
pjp
odh->answer = htons(ns_count);
2991
2019-02-15
pjp
odh->nsrr = 0;
2992
2019-02-15
pjp
}
2993
2014-11-14
pjp
2994
2015-06-27
pjp
/* add RRSIG reply_ns */
2995
2019-11-11
pjp
if (dnssec && q->dnssecok && (rbt->flags & RBT_DNSSEC)) {
2996
2015-06-22
pjp
int origlen = outlen;
2997
2020-04-01
pjp
int retcount;
2998
2015-06-22
pjp
2999
2020-07-06
pjp
tmplen = additional_rrsig(rbt1->zone, rbt1->zonelen, DNS_TYPE_NS, rbt1, reply, replysize, outlen, &retcount, q->aa);
3000
2015-06-22
pjp
3001
2015-06-22
pjp
if (tmplen == 0) {
3002
2020-07-08
pjp
/* we're forwarding and had no RRSIG return with -1 */
3003
2020-07-08
pjp
if (q->aa != 1)
3004
2020-07-08
pjp
return -1;
3005
2020-07-08
pjp
3006
2014-11-14
pjp
NTOHS(odh->query);
3007
2014-11-14
pjp
SET_DNS_TRUNCATION(odh);
3008
2014-11-14
pjp
HTONS(odh->query);
3009
2017-09-18
pjp
odh->answer = 0;
3010
2017-09-18
pjp
odh->nsrr = 0;
3011
2017-09-18
pjp
odh->additional = 0;
3012
2017-09-18
pjp
outlen = rollback;
3013
2014-11-14
pjp
goto out;
3014
2014-11-14
pjp
}
3015
2014-11-14
pjp
3016
2015-06-22
pjp
outlen = tmplen;
3017
2015-06-22
pjp
if (outlen > origlen) {
3018
2015-06-22
pjp
if (odh->answer)
3019
2020-04-01
pjp
odh->answer = htons(ns_count + retcount);
3020
2015-06-22
pjp
else if (odh->nsrr)
3021
2020-04-01
pjp
odh->nsrr = htons(ns_count + retcount);
3022
2015-06-22
pjp
}
3023
2019-10-30
pjp
3024
2019-10-30
pjp
if (delegation) {
3025
2019-11-09
pjp
tmplen = additional_ds(rbt1->zone, rbt1->zonelen, rbt1, reply, replysize, outlen, &addcount);
3026
2019-11-09
pjp
if (tmplen != 0) {
3027
2019-11-09
pjp
outlen = tmplen;
3028
2019-10-30
pjp
3029
2019-11-09
pjp
NTOHS(odh->nsrr);
3030
2019-11-09
pjp
odh->nsrr += addcount;
3031
2019-11-09
pjp
HTONS(odh->nsrr);
3032
2019-10-30
pjp
3033
2020-07-06
pjp
tmplen = additional_rrsig(rbt1->zone, rbt1->zonelen, DNS_TYPE_DS, rbt1, reply, replysize, outlen, &retcount, q->aa);
3034
2019-11-09
pjp
3035
2019-10-30
pjp
if (tmplen == 0) {
3036
2020-07-08
pjp
/* we're forwarding and had no RRSIG return with -1 */
3037
2020-07-08
pjp
if (q->aa != 1)
3038
2020-07-08
pjp
return -1;
3039
2020-07-08
pjp
3040
2019-10-30
pjp
NTOHS(odh->query);
3041
2019-10-30
pjp
SET_DNS_TRUNCATION(odh);
3042
2019-10-30
pjp
HTONS(odh->query);
3043
2019-10-30
pjp
odh->answer = 0;
3044
2019-10-30
pjp
odh->nsrr = 0;
3045
2019-10-30
pjp
odh->additional = 0;
3046
2019-10-30
pjp
outlen = rollback;
3047
2019-10-30
pjp
goto out;
3048
2019-10-30
pjp
}
3049
2019-10-30
pjp
3050
2019-10-30
pjp
outlen = tmplen;
3051
2019-10-30
pjp
3052
2019-11-09
pjp
NTOHS(odh->nsrr);
3053
2020-04-01
pjp
odh->nsrr += retcount;
3054
2019-11-09
pjp
HTONS(odh->nsrr);
3055
2019-11-09
pjp
3056
2019-11-09
pjp
} else {
3057
2019-11-09
pjp
rbt0 = get_soa(db, q);
3058
2019-11-09
pjp
if (rbt0 == NULL) {
3059
2019-11-09
pjp
return -1;
3060
2019-10-30
pjp
}
3061
2019-10-30
pjp
3062
2019-11-09
pjp
nrbt = find_nsec3_match_qname(rbt1->zone, rbt1->zonelen, rbt0, db);
3063
2019-11-09
pjp
if (nrbt != NULL) {
3064
2020-07-06
pjp
tmplen = additional_nsec3(nrbt->zone, nrbt->zonelen, DNS_TYPE_NSEC3, nrbt, reply, replysize, outlen, &retcount, q->aa);
3065
2019-10-30
pjp
3066
2019-11-09
pjp
if (tmplen == 0) {
3067
2019-11-09
pjp
NTOHS(odh->query);
3068
2019-11-09
pjp
SET_DNS_TRUNCATION(odh);
3069
2019-11-09
pjp
HTONS(odh->query);
3070
2019-11-09
pjp
odh->answer = 0;
3071
2019-11-09
pjp
odh->nsrr = 0;
3072
2019-11-09
pjp
odh->additional = 0;
3073
2019-11-09
pjp
outlen = rollback;
3074
2019-11-09
pjp
goto out;
3075
2019-11-09
pjp
}
3076
2019-10-30
pjp
3077
2019-11-09
pjp
outlen = tmplen;
3078
2019-10-30
pjp
3079
2019-11-09
pjp
/* additional_nsec3 adds an RRSIG automatically */
3080
2019-11-09
pjp
NTOHS(odh->nsrr);
3081
2020-04-01
pjp
odh->nsrr += retcount;
3082
2019-11-09
pjp
HTONS(odh->nsrr);
3083
2019-11-09
pjp
3084
2019-11-09
pjp
}
3085
2019-11-09
pjp
3086
2019-11-09
pjp
} /* nrbt != NULL */
3087
2019-11-09
pjp
} /* else tmplen != 0 */
3088
2019-11-09
pjp
} /* if delegation */
3089
2019-11-09
pjp
3090
2019-10-30
pjp
/* tack on additional A or AAAA records */
3091
2019-10-30
pjp
3092
2019-10-30
pjp
SLIST_FOREACH(ad0, &addishead, addis_entries) {
3093
2019-10-30
pjp
addiscount = 0;
3094
2019-11-11
pjp
rbt0 = find_rrset(db, ad0->name, ad0->namelen);
3095
2019-10-30
pjp
if (rbt0 != NULL && find_rr(rbt0, DNS_TYPE_AAAA) != NULL) {
3096
2019-10-30
pjp
tmplen = additional_aaaa(ad0->name, ad0->namelen, rbt0, reply, replysize, outlen, &addiscount);
3097
2019-10-30
pjp
if (tmplen == 0) {
3098
2019-10-30
pjp
NTOHS(odh->query);
3099
2019-10-30
pjp
SET_DNS_TRUNCATION(odh);
3100
2019-10-30
pjp
HTONS(odh->query);
3101
2019-10-30
pjp
odh->answer = 0;
3102
2019-10-30
pjp
odh->nsrr = 0;
3103
2019-10-30
pjp
odh->additional = 0;
3104
2019-10-30
pjp
outlen = rollback;
3105
2019-10-30
pjp
goto out;
3106
2019-10-30
pjp
}
3107
2019-10-30
pjp
3108
2019-10-30
pjp
outlen = tmplen;
3109
2019-10-30
pjp
NTOHS(odh->additional);
3110
2019-10-30
pjp
odh->additional += addiscount;
3111
2019-10-30
pjp
HTONS(odh->additional);
3112
2019-10-30
pjp
3113
2019-10-30
pjp
/* additional RRSIG for the additional AAAA */
3114
2019-11-11
pjp
if (dnssec && q->dnssecok && (rbt0->flags & RBT_DNSSEC)) {
3115
2020-07-06
pjp
tmplen = additional_rrsig(ad0->name, ad0->namelen, DNS_TYPE_AAAA, rbt0, reply, replysize, outlen, &retcount, q->aa);
3116
2019-10-30
pjp
3117
2019-10-30
pjp
if (tmplen == 0) {
3118
2020-07-08
pjp
/* we're forwarding and had no RRSIG return with -1 */
3119
2020-07-08
pjp
if (q->aa != 1)
3120
2020-07-08
pjp
return -1;
3121
2020-07-08
pjp
3122
2019-10-30
pjp
NTOHS(odh->query);
3123
2019-10-30
pjp
SET_DNS_TRUNCATION(odh);
3124
2019-10-30
pjp
HTONS(odh->query);
3125
2019-10-30
pjp
odh->answer = 0;
3126
2019-10-30
pjp
odh->nsrr = 0;
3127
2019-10-30
pjp
odh->additional = 0;
3128
2019-10-30
pjp
outlen = rollback;
3129
2019-10-30
pjp
goto out;
3130
2019-10-30
pjp
}
3131
2019-10-30
pjp
3132
2019-10-30
pjp
NTOHS(odh->additional);
3133
2020-04-01
pjp
odh->additional += retcount;
3134
2019-10-30
pjp
HTONS(odh->additional);
3135
2019-10-30
pjp
3136
2019-10-30
pjp
outlen = tmplen;
3137
2019-10-30
pjp
}
3138
2019-10-30
pjp
3139
2019-10-30
pjp
rbt0 = NULL;
3140
2019-10-30
pjp
}
3141
2019-10-30
pjp
3142
2019-10-30
pjp
3143
2019-10-30
pjp
addiscount = 0;
3144
2019-11-11
pjp
rbt0 = find_rrset(db, ad0->name, ad0->namelen);
3145
2019-10-30
pjp
if (rbt0 != NULL && find_rr(rbt0, DNS_TYPE_A) != NULL) {
3146
2019-10-30
pjp
tmplen = additional_a(ad0->name, ad0->namelen, rbt0, reply, replysize, outlen, &addiscount);
3147
2019-10-30
pjp
if (tmplen == 0) {