1 From 6b17335209639a56f214d011eaed4ebcde8dd276 Mon Sep 17 00:00:00 2001
2 From: Simon Kelley <simon@thekelleys.org.uk>
3 Date: Tue, 8 May 2018 18:32:14 +0100
4 Subject: [PATCH 04/17] Add packet-dump debugging facility.
6 Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
12 src/config.h | 16 ++++-
13 src/dnsmasq.c | 16 ++++-
14 src/dnsmasq.h | 29 +++++++-
15 src/dump.c | 210 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16 src/forward.c | 37 ++++++++--
17 src/option.c | 14 ++++
18 10 files changed, 329 insertions(+), 11 deletions(-)
19 create mode 100644 src/dump.c
23 @@ -17,6 +17,12 @@ version 2.80
24 Fix DHCP broken-ness when --no-ping AND --dhcp-sequential-ip
25 are set. Thanks to Daniel Miess for help with this.
27 + Add a facilty to store DNS packets sent/recieved in a
28 + pcap-format file for later debugging. The file location
29 + is given by the --dumpfile option, and a bitmap controlling
30 + which packets should be dumped is given by the --dumpmask
35 Fix parsing of CNAME arguments, which are confused by extra spaces.
38 @@ -76,7 +76,7 @@ objs = cache.o rfc1035.o util.o option.o
39 helper.o tftp.o log.o conntrack.o dhcp6.o rfc3315.o \
40 dhcp-common.o outpacket.o radv.o slaac.o auth.o ipset.o \
41 domain.o dnssec.o blockdata.o tables.o loop.o inotify.o \
42 - poll.o rrfilter.o edns0.o arp.o crypto.o
43 + poll.o rrfilter.o edns0.o arp.o crypto.o dump.o
45 hdrs = dnsmasq.h config.h dhcp-protocol.h dhcp6-protocol.h \
46 dns-protocol.h radv-protocol.h ip6addr.h
49 @@ -10,7 +10,8 @@ LOCAL_SRC_FILES := bpf.c cache.c dbus.c
50 dhcp6.c rfc3315.c dhcp-common.c outpacket.c \
51 radv.c slaac.c auth.c ipset.c domain.c \
52 dnssec.c dnssec-openssl.c blockdata.c tables.c \
53 - loop.c inotify.c poll.c rrfilter.c edns0.c arp.c crypto.c
54 + loop.c inotify.c poll.c rrfilter.c edns0.c arp.c \
57 LOCAL_MODULE := dnsmasq
61 @@ -647,6 +647,13 @@ V4 mapped IPv6 addresses, which have a r
62 The address range can be of the form
63 <ip address>,<ip address> or <ip address>/<netmask> in both forms of the option.
65 +.B --dumpfile=<path/to/file>
66 +Specify the location of a pcap-format file which dnsmasq uses to dump copies of network packets for debugging purposes. If the file exists when dnsmasq starts, it is not deleted; new packets are added to the end.
69 +Specify which types of packets should be added to the dumpfile. The argument should be the OR of the bitmasks for each type of packet to be dumped: it can be specified in hex by preceding the number with 0x in the normal way. Each time a packet is written to the dumpfile, dnsmasq logs the packet sequence and the mask
70 +representing its type. The current types are: 0x0001 - DNS queries from clients 0x0002 DNS replies to clients 0x0004 - DNS queries to upstream 0x0008 - DNS replies from upstream 0x0010 - queries send upstream for DNSSEC validation 0x0020 - replies to queries for DNSSEC validation 0x0040 - replies to client queries which fail DNSSEC validation 0x0080 replies to queries for DNSSEC validation which fail validation.
72 .B --add-mac[=base64|text]
73 Add the MAC address of the requestor to DNS queries which are
74 forwarded upstream. This may be used to DNS filtering by the upstream
77 @@ -117,6 +117,9 @@ HAVE_AUTH
79 include DNSSEC validator.
82 + include code to dump packets to a libpcap-format file for debugging.
85 include functionality to probe for and remove DNS forwarding loops.
87 @@ -132,6 +135,7 @@ NO_DHCP6
93 these are available to explicitly disable compile time options which would
94 otherwise be enabled automatically (HAVE_IPV6, >2Gb file sizes) or
95 @@ -164,6 +168,7 @@ RESOLVFILE
99 +#define HAVE_DUMPFILE
101 /* Build options which require external libraries.
103 @@ -363,6 +368,10 @@ HAVE_SOCKADDR_SA_LEN
108 +#undef HAVE_DUMPFILE
111 #if defined (HAVE_LINUX_NETWORK) && !defined(NO_INOTIFY)
114 @@ -451,8 +460,11 @@ static char *compile_opts =
121 +#ifndef HAVE_DUMPFILE
130 @@ -366,7 +366,16 @@ int main (int argc, char **argv)
132 daemon->inotifyfd = -1;
136 + if (daemon->dump_file)
137 +#ifdef HAVE_DUMPFILE
140 + daemon->dumpfd = -1;
142 + die(_("Packet dumps not available: set HAVE_DUMP in src/config.h"), NULL, EC_BADCONF);
145 if (option_bool(OPT_DBUS))
148 @@ -1424,6 +1433,11 @@ static void async_event(int pipe, time_t
151 unlink(daemon->runfile);
153 +#ifdef HAVE_DUMPFILE
154 + if (daemon->dumpfd != -1)
155 + close(daemon->dumpfd);
158 my_syslog(LOG_INFO, _("exiting on receipt of SIGTERM"));
162 @@ -119,6 +119,9 @@ typedef unsigned long long u64;
163 #include <net/if_arp.h>
164 #include <netinet/in_systm.h>
165 #include <netinet/ip.h>
167 +#include <netinet/ip6.h>
169 #include <netinet/ip_icmp.h>
172 @@ -598,6 +601,16 @@ struct hostsfile {
173 unsigned int index; /* matches to cache entries for logging */
176 +/* packet-dump flags */
177 +#define DUMP_QUERY 0x0001
178 +#define DUMP_REPLY 0x0002
179 +#define DUMP_UP_QUERY 0x0004
180 +#define DUMP_UP_REPLY 0x0008
181 +#define DUMP_SEC_QUERY 0x0010
182 +#define DUMP_SEC_REPLY 0x0020
183 +#define DUMP_BOGUS 0x0040
184 +#define DUMP_SEC_BOGUS 0x0080
187 /* DNSSEC status values. */
188 #define STAT_SECURE 1
189 @@ -1020,14 +1033,14 @@ extern struct daemon {
190 unsigned int duid_enterprise, duid_config_len;
191 unsigned char *duid_config;
195 unsigned long soa_sn, soa_refresh, soa_retry, soa_expiry;
196 #ifdef OPTION6_PREFIX_CLASS
197 struct prefix_class *prefix_classes;
200 struct ds_config *ds;
201 - int dnssec_no_time_check;
202 - int back_to_the_future;
203 char *timestamp_file;
206 @@ -1040,6 +1053,8 @@ extern struct daemon {
207 char *workspacename; /* ditto */
208 char *rr_status; /* flags for individual RRs */
210 + int dnssec_no_time_check;
211 + int back_to_the_future;
213 unsigned int local_answer, queries_forwarded, auth_answer;
214 struct frec *frec_list;
215 @@ -1094,6 +1109,10 @@ extern struct daemon {
217 char *addrbuff2; /* only allocated when OPT_EXTRALOG */
219 +#ifdef HAVE_DUMPFILE
220 + /* file for packet dumps. */
226 @@ -1588,3 +1607,9 @@ int check_source(struct dns_header *head
228 int find_mac(union mysockaddr *addr, unsigned char *mac, int lazy, time_t now);
229 int do_arp_script_run(void);
232 +#ifdef HAVE_DUMPFILE
233 +void dump_init(void);
234 +void dump_packet(int mask, void *packet, size_t len, union mysockaddr *src, union mysockaddr *dst);
239 +/* dnsmasq is Copyright (c) 2000-2018 Simon Kelley
241 + This program is free software; you can redistribute it and/or modify
242 + it under the terms of the GNU General Public License as published by
243 + the Free Software Foundation; version 2 dated June, 1991, or
244 + (at your option) version 3 dated 29 June, 2007.
246 + This program is distributed in the hope that it will be useful,
247 + but WITHOUT ANY WARRANTY; without even the implied warranty of
248 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
249 + GNU General Public License for more details.
251 + You should have received a copy of the GNU General Public License
252 + along with this program. If not, see <http://www.gnu.org/licenses/>.
255 +#include "dnsmasq.h"
257 +#ifdef HAVE_DUMPFILE
259 +static u32 packet_count;
261 +/* https://wiki.wireshark.org/Development/LibpcapFileFormat */
263 + u32 magic_number; /* magic number */
264 + u16 version_major; /* major version number */
265 + u16 version_minor; /* minor version number */
266 + u32 thiszone; /* GMT to local correction */
267 + u32 sigfigs; /* accuracy of timestamps */
268 + u32 snaplen; /* max length of captured packets, in octets */
269 + u32 network; /* data link type */
272 +struct pcaprec_hdr_s {
273 + u32 ts_sec; /* timestamp seconds */
274 + u32 ts_usec; /* timestamp microseconds */
275 + u32 incl_len; /* number of octets of packet saved in file */
276 + u32 orig_len; /* actual length of packet */
280 +void dump_init(void)
283 + struct pcap_hdr_s header;
284 + struct pcaprec_hdr_s pcap_header;
288 + if (stat(daemon->dump_file, &buf) == -1)
290 + /* doesn't exist, create and add header */
291 + header.magic_number = 0xa1b2c3d4;
292 + header.version_major = 2;
293 + header.version_minor = 4;
294 + header.thiszone = 0;
295 + header.sigfigs = 0;
296 + header.snaplen = daemon->edns_pktsz + 200; /* slop for IP/UDP headers */
297 + header.network = 101; /* DLT_RAW http://www.tcpdump.org/linktypes.html */
299 + if (errno != ENOENT ||
300 + (daemon->dumpfd = creat(daemon->dump_file, S_IRUSR | S_IWUSR)) == -1 ||
301 + !read_write(daemon->dumpfd, (void *)&header, sizeof(header), 0))
302 + die(_("cannot create %s: %s"), daemon->dump_file, EC_FILE);
304 + else if ((daemon->dumpfd = open(daemon->dump_file, O_APPEND | O_RDWR)) == -1 ||
305 + !read_write(daemon->dumpfd, (void *)&header, sizeof(header), 1) ||
306 + header.magic_number != 0xa1b2c3d4)
307 + die(_("cannot access %s: %s"), daemon->dump_file, EC_FILE);
310 + /* count existing records */
311 + while (read_write(daemon->dumpfd, (void *)&pcap_header, sizeof(pcap_header), 1))
313 + lseek(daemon->dumpfd, pcap_header.incl_len, SEEK_CUR);
319 +void dump_packet(int mask, void *packet, size_t len, union mysockaddr *src, union mysockaddr *dst)
323 + struct ip6_hdr ip6;
327 + u16 uh_sport; /* source port */
328 + u16 uh_dport; /* destination port */
329 + u16 uh_ulen; /* udp length */
330 + u16 uh_sum; /* udp checksum */
332 + struct pcaprec_hdr_s pcap_header;
333 + struct timeval time;
339 + if (daemon->dumpfd == -1 || !(mask & daemon->dump_mask))
342 + /* So wireshark can Id the packet. */
343 + udp.uh_sport = udp.uh_dport = htons(NAMESERVER_PORT);
347 + family = src->sa.sa_family;
349 + family = dst->sa.sa_family;
351 + if (family == AF_INET6)
354 + ipsz = sizeof(ip6);
355 + memset(&ip6, 0, sizeof(ip6));
357 + ip6.ip6_vfc = 6 << 4;
358 + ip6.ip6_plen = htons(sizeof(struct udphdr) + len);
359 + ip6.ip6_nxt = IPPROTO_UDP;
364 + memcpy(&ip6.ip6_src, &src->in6.sin6_addr, IN6ADDRSZ);
365 + udp.uh_sport = src->in6.sin6_port;
370 + memcpy(&ip6.ip6_dst, &dst->in6.sin6_addr, IN6ADDRSZ);
371 + udp.uh_dport = dst->in6.sin6_port;
374 + /* start UDP checksum */
375 + for (sum = 0, i = 0; i < IN6ADDRSZ; i++)
376 + sum += ((u16 *)&ip6.ip6_src)[i];
383 + memset(&ip, 0, sizeof(ip));
385 + ip.ip_v = IPVERSION;
386 + ip.ip_hl = sizeof(struct ip) / 4;
387 + ip.ip_len = htons(sizeof(struct ip) + sizeof(struct udphdr) + len);
388 + ip.ip_ttl = IPDEFTTL;
389 + ip.ip_p = IPPROTO_UDP;
393 + ip.ip_src = src->in.sin_addr;
394 + udp.uh_sport = src->in.sin_port;
399 + ip.ip_dst = dst->in.sin_addr;
400 + udp.uh_dport = dst->in.sin_port;
404 + for (sum = 0, i = 0; i < sizeof(struct ip) / 2; i++)
405 + sum += ((u16 *)&ip)[i];
407 + sum = (sum & 0xffff) + (sum >> 16);
408 + ip.ip_sum = (sum == 0xffff) ? sum : ~sum;
410 + /* start UDP checksum */
411 + sum = ip.ip_src.s_addr & 0xffff;
412 + sum += (ip.ip_src.s_addr >> 16) & 0xffff;
413 + sum += ip.ip_dst.s_addr & 0xffff;
414 + sum += (ip.ip_dst.s_addr >> 16) & 0xffff;
418 + ((unsigned char *)packet)[len] = 0; /* for checksum, in case length is odd. */
421 + udp.uh_ulen = htons(sizeof(struct udphdr) + len);
422 + sum += htons(IPPROTO_UDP);
423 + sum += htons(sizeof(struct udphdr) + len);
424 + for (i = 0; i < sizeof(struct udphdr)/2; i++)
425 + sum += ((u16 *)&udp)[i];
426 + for (i = 0; i < (len + 1) / 2; i++)
427 + sum += ((u16 *)packet)[i];
429 + sum = (sum & 0xffff) + (sum >> 16);
430 + udp.uh_sum = (sum == 0xffff) ? sum : ~sum;
432 + rc = gettimeofday(&time, NULL);
433 + pcap_header.ts_sec = time.tv_sec;
434 + pcap_header.ts_usec = time.tv_usec;
435 + pcap_header.incl_len = pcap_header.orig_len = ipsz + sizeof(udp) + len;
438 + !read_write(daemon->dumpfd, (void *)&pcap_header, sizeof(pcap_header), 0) ||
439 + !read_write(daemon->dumpfd, iphdr, ipsz, 0) ||
440 + !read_write(daemon->dumpfd, (void *)&udp, sizeof(udp), 0) ||
441 + !read_write(daemon->dumpfd, (void *)packet, len, 0))
442 + my_syslog(LOG_ERR, _("failed to write packet dump"));
444 + my_syslog(LOG_INFO, _("dumping UDP packet %u mask 0x%04x"), ++packet_count, mask);
451 @@ -508,6 +508,10 @@ static int forward_query(int udpfd, unio
455 +#ifdef HAVE_DUMPFILE
456 + dump_packet(DUMP_UP_QUERY, (void *)header, plen, NULL, &start->addr);
459 /* Keep info in case we want to re-send this packet */
460 daemon->srv_save = start;
461 daemon->packet_len = plen;
462 @@ -769,7 +773,7 @@ void reply_query(int fd, int family, tim
465 header = (struct dns_header *)daemon->packet;
468 if (n < (int)sizeof(struct dns_header) || !(header->hb3 & HB3_QR))
471 @@ -796,6 +800,12 @@ void reply_query(int fd, int family, tim
472 if (!(forward = lookup_frec(ntohs(header->id), hash)))
475 +#ifdef HAVE_DUMPFILE
476 + dump_packet((forward->flags & (FREC_DNSKEY_QUERY | FREC_DS_QUERY)) ? DUMP_SEC_REPLY : DUMP_UP_REPLY,
477 + (void *)header, n, &serveraddr, NULL);
481 /* log_query gets called indirectly all over the place, so
482 pass these in global variables - sorry. */
483 daemon->log_display_id = forward->log_id;
484 @@ -934,6 +944,11 @@ void reply_query(int fd, int family, tim
485 status = dnssec_validate_reply(now, header, n, daemon->namebuff, daemon->keyname, &forward->class,
486 !option_bool(OPT_DNSSEC_IGN_NS) && (server->flags & SERV_DO_DNSSEC),
488 +#ifdef HAVE_DUMPFILE
489 + if (status == STAT_BOGUS)
490 + dump_packet((forward->flags & (FREC_DNSKEY_QUERY | FREC_DS_QUERY)) ? DUMP_SEC_BOGUS : DUMP_BOGUS,
491 + header, (size_t)n, &serveraddr, NULL);
495 /* Can't validate, as we're missing key data. Put this
496 @@ -1060,6 +1075,11 @@ void reply_query(int fd, int family, tim
497 setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int));
501 +#ifdef HAVE_DUMPFILE
502 + dump_packet(DUMP_SEC_QUERY, (void *)header, (size_t)nn, NULL, &server->addr);
505 while (retry_send(sendto(fd, (char *)header, nn, 0,
507 sa_len(&server->addr))));
508 @@ -1114,8 +1134,8 @@ void reply_query(int fd, int family, tim
516 /* restore CD bit to the value in the query */
517 if (forward->flags & FREC_CHECKING_DISABLED)
518 header->hb4 |= HB4_CD;
519 @@ -1141,6 +1161,11 @@ void reply_query(int fd, int family, tim
520 nn = resize_packet(header, nn, NULL, 0);
524 +#ifdef HAVE_DUMPFILE
525 + dump_packet(DUMP_REPLY, daemon->packet, (size_t)nn, NULL, &forward->source);
528 send_from(forward->fd, option_bool(OPT_NOWILD) || option_bool (OPT_CLEVERBIND), daemon->packet, nn,
529 &forward->source, &forward->dest, forward->iface);
531 @@ -1394,7 +1419,11 @@ void receive_query(struct listener *list
532 pass these in global variables - sorry. */
533 daemon->log_display_id = ++daemon->log_id;
534 daemon->log_source_addr = &source_addr;
537 +#ifdef HAVE_DUMPFILE
538 + dump_packet(DUMP_QUERY, daemon->packet, (size_t)n, &source_addr, NULL);
541 if (extract_request(header, (size_t)n, daemon->namebuff, &type))
546 @@ -161,6 +161,8 @@ struct myoption {
547 #define LOPT_TFTP_MTU 349
548 #define LOPT_REPLY_DELAY 350
549 #define LOPT_RAPID_COMMIT 351
550 +#define LOPT_DUMPFILE 352
551 +#define LOPT_DUMPMASK 353
553 #ifdef HAVE_GETOPT_LONG
554 static const struct option opts[] =
555 @@ -327,6 +329,8 @@ static const struct myoption opts[] =
556 { "dhcp-ttl", 1, 0 , LOPT_DHCPTTL },
557 { "dhcp-reply-delay", 1, 0, LOPT_REPLY_DELAY },
558 { "dhcp-rapid-commit", 0, 0, LOPT_RAPID_COMMIT },
559 + { "dumpfile", 1, 0, LOPT_DUMPFILE },
560 + { "dumpmask", 1, 0, LOPT_DUMPMASK },
564 @@ -500,6 +504,8 @@ static struct {
565 { LOPT_DHCPTTL, ARG_ONE, "<ttl>", gettext_noop("Set TTL in DNS responses with DHCP-derived addresses."), NULL },
566 { LOPT_REPLY_DELAY, ARG_ONE, "<integer>", gettext_noop("Delay DHCP replies for at least number of seconds."), NULL },
567 { LOPT_RAPID_COMMIT, OPT_RAPID_COMMIT, NULL, gettext_noop("Enables DHCPv4 Rapid Commit option."), NULL },
568 + { LOPT_DUMPFILE, ARG_ONE, "<path>", gettext_noop("Path to debug packet dump file"), NULL },
569 + { LOPT_DUMPMASK, ARG_ONE, "<hex>", gettext_noop("Mask which packets to dump"), NULL },
570 { 0, 0, NULL, NULL, NULL }
573 @@ -1811,6 +1817,14 @@ static int one_opt(int option, char *arg
574 ret_err(_("bad MX target"));
577 + case LOPT_DUMPFILE: /* --dumpfile */
578 + daemon->dump_file = opt_string_alloc(arg);
581 + case LOPT_DUMPMASK: /* --dumpmask */
582 + daemon->dump_mask = strtol(arg, NULL, 0);
586 case 'l': /* --dhcp-leasefile */
587 daemon->lease_file = opt_string_alloc(arg);