| 1 |
/* dnsmasq is Copyright (c) 2000-2026 Simon Kelley |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License as published by |
| 5 |
the Free Software Foundation; version 2 dated June, 1991, or |
| 6 |
(at your option) version 3 dated 29 June, 2007. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, |
| 9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License |
| 14 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "dnsmasq.h" |
| 18 |
|
| 19 |
#ifdef HAVE_DUMPFILE |
| 20 |
|
| 21 |
#include <netinet/icmp6.h> |
| 22 |
|
| 23 |
static u32 packet_count; |
| 24 |
static void do_dump_packet(int mask, void *packet, size_t len, |
| 25 |
union mysockaddr *src, union mysockaddr *dst, int port, int proto); |
| 26 |
|
| 27 |
/* https://wiki.wireshark.org/Development/LibpcapFileFormat */ |
| 28 |
struct pcap_hdr_s { |
| 29 |
u32 magic_number; /* magic number */ |
| 30 |
u16 version_major; /* major version number */ |
| 31 |
u16 version_minor; /* minor version number */ |
| 32 |
u32 thiszone; /* GMT to local correction */ |
| 33 |
u32 sigfigs; /* accuracy of timestamps */ |
| 34 |
u32 snaplen; /* max length of captured packets, in octets */ |
| 35 |
u32 network; /* data link type */ |
| 36 |
}; |
| 37 |
|
| 38 |
struct pcaprec_hdr_s { |
| 39 |
u32 ts_sec; /* timestamp seconds */ |
| 40 |
u32 ts_usec; /* timestamp microseconds */ |
| 41 |
u32 incl_len; /* number of octets of packet saved in file */ |
| 42 |
u32 orig_len; /* actual length of packet */ |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
void dump_init(void) |
| 47 |
{ |
| 48 |
struct stat buf; |
| 49 |
struct pcap_hdr_s header; |
| 50 |
struct pcaprec_hdr_s pcap_header; |
| 51 |
|
| 52 |
packet_count = 0; |
| 53 |
|
| 54 |
header.magic_number = 0xa1b2c3d4; |
| 55 |
header.version_major = 2; |
| 56 |
header.version_minor = 4; |
| 57 |
header.thiszone = 0; |
| 58 |
header.sigfigs = 0; |
| 59 |
header.snaplen = daemon->edns_pktsz + 200; /* slop for IP/UDP headers */ |
| 60 |
header.network = 101; /* DLT_RAW http://www.tcpdump.org/linktypes.html */ |
| 61 |
|
| 62 |
if (stat(daemon->dump_file, &buf) == -1) |
| 63 |
{ |
| 64 |
/* doesn't exist, create and add header */ |
| 65 |
if (errno != ENOENT || |
| 66 |
(daemon->dumpfd = creat(daemon->dump_file, S_IRUSR | S_IWUSR)) == -1 || |
| 67 |
!read_write(daemon->dumpfd, (void *)&header, sizeof(header), RW_WRITE)) |
| 68 |
die(_("cannot create %s: %s"), daemon->dump_file, EC_FILE); |
| 69 |
} |
| 70 |
else if (S_ISFIFO(buf.st_mode)) |
| 71 |
{ |
| 72 |
/* File is named pipe (with wireshark on the other end, probably.) |
| 73 |
Send header. */ |
| 74 |
if ((daemon->dumpfd = open(daemon->dump_file, O_APPEND | O_RDWR)) == -1 || |
| 75 |
!read_write(daemon->dumpfd, (void *)&header, sizeof(header), RW_WRITE)) |
| 76 |
die(_("cannot open pipe %s: %s"), daemon->dump_file, EC_FILE); |
| 77 |
} |
| 78 |
else if ((daemon->dumpfd = open(daemon->dump_file, O_APPEND | O_RDWR)) == -1 || |
| 79 |
!read_write(daemon->dumpfd, (void *)&header, sizeof(header), RW_READ)) |
| 80 |
die(_("cannot access %s: %s"), daemon->dump_file, EC_FILE); |
| 81 |
else if (header.magic_number != 0xa1b2c3d4) |
| 82 |
die(_("bad header in %s"), daemon->dump_file, EC_FILE); |
| 83 |
else |
| 84 |
{ |
| 85 |
/* count existing records */ |
| 86 |
while (read_write(daemon->dumpfd, (void *)&pcap_header, sizeof(pcap_header), RW_READ)) |
| 87 |
{ |
| 88 |
if (lseek(daemon->dumpfd, pcap_header.incl_len, SEEK_CUR) == (off_t)-1) |
| 89 |
break; |
| 90 |
packet_count++; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
void dump_packet_udp(int mask, void *packet, size_t len, |
| 96 |
union mysockaddr *src, union mysockaddr *dst, int fd) |
| 97 |
{ |
| 98 |
union mysockaddr fd_addr; |
| 99 |
socklen_t addr_len = sizeof(fd_addr); |
| 100 |
|
| 101 |
if (daemon->dumpfd != -1 && (mask & daemon->dump_mask)) |
| 102 |
{ |
| 103 |
/* if fd is negative it carries a port number (negated) |
| 104 |
which we use as a source or destination when not otherwise |
| 105 |
specified so wireshark can ID the packet. |
| 106 |
If both src and dst are specified, set this to -1 to avoid |
| 107 |
a spurious getsockname() call. */ |
| 108 |
int port = (fd < 0) ? -fd : -1; |
| 109 |
|
| 110 |
/* fd >= 0 is a file descriptor and the address of that file descriptor is used |
| 111 |
in place of a NULL src or dst. */ |
| 112 |
if (fd >= 0 && getsockname(fd, (struct sockaddr *)&fd_addr, &addr_len) != -1) |
| 113 |
{ |
| 114 |
if (!src) |
| 115 |
src = &fd_addr; |
| 116 |
|
| 117 |
if (!dst) |
| 118 |
dst = &fd_addr; |
| 119 |
} |
| 120 |
|
| 121 |
do_dump_packet(mask, packet, len, src, dst, port, IPPROTO_UDP); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
void dump_packet_icmp(int mask, void *packet, size_t len, |
| 126 |
union mysockaddr *src, union mysockaddr *dst) |
| 127 |
{ |
| 128 |
if (daemon->dumpfd != -1 && (mask & daemon->dump_mask)) |
| 129 |
do_dump_packet(mask, packet, len, src, dst, -1, IPPROTO_ICMP); |
| 130 |
} |
| 131 |
|
| 132 |
static void do_dump_packet(int mask, void *packet, size_t len, |
| 133 |
union mysockaddr *src, union mysockaddr *dst, int port, int proto) |
| 134 |
{ |
| 135 |
struct ip ip; |
| 136 |
struct ip6_hdr ip6; |
| 137 |
int family; |
| 138 |
struct udphdr { |
| 139 |
u16 uh_sport; /* source port */ |
| 140 |
u16 uh_dport; /* destination port */ |
| 141 |
u16 uh_ulen; /* udp length */ |
| 142 |
u16 uh_sum; /* udp checksum */ |
| 143 |
} udp; |
| 144 |
struct pcaprec_hdr_s pcap_header; |
| 145 |
struct timeval time; |
| 146 |
u32 i, sum; |
| 147 |
void *iphdr; |
| 148 |
size_t ipsz; |
| 149 |
int rc; |
| 150 |
|
| 151 |
/* if port != -1 it carries a port number |
| 152 |
which we use as a source or destination when not otherwise |
| 153 |
specified so wireshark can ID the packet. |
| 154 |
If both src and dst are specified, set this to -1 to avoid |
| 155 |
a spurious getsockname() call. */ |
| 156 |
udp.uh_sport = udp.uh_dport = htons(port < 0 ? 0 : port); |
| 157 |
|
| 158 |
if (src) |
| 159 |
family = src->sa.sa_family; |
| 160 |
else |
| 161 |
family = dst->sa.sa_family; |
| 162 |
|
| 163 |
if (family == AF_INET6) |
| 164 |
{ |
| 165 |
iphdr = &ip6; |
| 166 |
ipsz = sizeof(ip6); |
| 167 |
memset(&ip6, 0, sizeof(ip6)); |
| 168 |
|
| 169 |
ip6.ip6_vfc = 6 << 4; |
| 170 |
ip6.ip6_hops = 64; |
| 171 |
|
| 172 |
if ((ip6.ip6_nxt = proto) == IPPROTO_UDP) |
| 173 |
ip6.ip6_plen = htons(sizeof(struct udphdr) + len); |
| 174 |
else |
| 175 |
{ |
| 176 |
proto = ip6.ip6_nxt = IPPROTO_ICMPV6; |
| 177 |
ip6.ip6_plen = htons(len); |
| 178 |
} |
| 179 |
|
| 180 |
if (src) |
| 181 |
{ |
| 182 |
memcpy(&ip6.ip6_src, &src->in6.sin6_addr, IN6ADDRSZ); |
| 183 |
udp.uh_sport = src->in6.sin6_port; |
| 184 |
} |
| 185 |
|
| 186 |
if (dst) |
| 187 |
{ |
| 188 |
memcpy(&ip6.ip6_dst, &dst->in6.sin6_addr, IN6ADDRSZ); |
| 189 |
udp.uh_dport = dst->in6.sin6_port; |
| 190 |
} |
| 191 |
|
| 192 |
/* start UDP checksum */ |
| 193 |
for (sum = 0, i = 0; i < IN6ADDRSZ; i+=2) |
| 194 |
{ |
| 195 |
sum += ntohs((ip6.ip6_src.s6_addr[i] << 8) + (ip6.ip6_src.s6_addr[i+1])) ; |
| 196 |
sum += ntohs((ip6.ip6_dst.s6_addr[i] << 8) + (ip6.ip6_dst.s6_addr[i+1])) ; |
| 197 |
} |
| 198 |
} |
| 199 |
else |
| 200 |
{ |
| 201 |
iphdr = &ip; |
| 202 |
ipsz = sizeof(ip); |
| 203 |
memset(&ip, 0, sizeof(ip)); |
| 204 |
|
| 205 |
ip.ip_v = IPVERSION; |
| 206 |
ip.ip_hl = sizeof(struct ip) / 4; |
| 207 |
ip.ip_ttl = IPDEFTTL; |
| 208 |
|
| 209 |
if ((ip.ip_p = proto) == IPPROTO_UDP) |
| 210 |
ip.ip_len = htons(sizeof(struct ip) + sizeof(struct udphdr) + len); |
| 211 |
else |
| 212 |
{ |
| 213 |
ip.ip_len = htons(sizeof(struct ip) + len); |
| 214 |
proto = ip.ip_p = IPPROTO_ICMP; |
| 215 |
} |
| 216 |
|
| 217 |
if (src) |
| 218 |
{ |
| 219 |
ip.ip_src = src->in.sin_addr; |
| 220 |
udp.uh_sport = src->in.sin_port; |
| 221 |
} |
| 222 |
|
| 223 |
if (dst) |
| 224 |
{ |
| 225 |
ip.ip_dst = dst->in.sin_addr; |
| 226 |
udp.uh_dport = dst->in.sin_port; |
| 227 |
} |
| 228 |
|
| 229 |
ip.ip_sum = 0; |
| 230 |
for (sum = 0, i = 0; i < sizeof(struct ip) / 2; i++) |
| 231 |
sum += ((u16 *)&ip)[i]; |
| 232 |
while (sum >> 16) |
| 233 |
sum = (sum & 0xffff) + (sum >> 16); |
| 234 |
ip.ip_sum = (sum == 0xffff) ? sum : ~sum; |
| 235 |
|
| 236 |
/* start UDP/ICMP checksum */ |
| 237 |
sum = ip.ip_src.s_addr & 0xffff; |
| 238 |
sum += (ip.ip_src.s_addr >> 16) & 0xffff; |
| 239 |
sum += ip.ip_dst.s_addr & 0xffff; |
| 240 |
sum += (ip.ip_dst.s_addr >> 16) & 0xffff; |
| 241 |
} |
| 242 |
|
| 243 |
if (len & 1) |
| 244 |
((unsigned char *)packet)[len] = 0; /* for checksum, in case length is odd. */ |
| 245 |
|
| 246 |
if (proto == IPPROTO_UDP) |
| 247 |
{ |
| 248 |
/* Add Remaining part of the pseudoheader. Note that though the |
| 249 |
IPv6 pseudoheader is very different to the IPv4 one, the |
| 250 |
net result of this calculation is correct as long as the |
| 251 |
packet length is less than 65536, which is fine for us. */ |
| 252 |
sum += htons(IPPROTO_UDP); |
| 253 |
sum += htons(sizeof(struct udphdr) + len); |
| 254 |
|
| 255 |
udp.uh_sum = 0; |
| 256 |
udp.uh_ulen = htons(sizeof(struct udphdr) + len); |
| 257 |
|
| 258 |
for (i = 0; i < sizeof(struct udphdr)/2; i++) |
| 259 |
sum += ((u16 *)&udp)[i]; |
| 260 |
for (i = 0; i < (len + 1) / 2; i++) |
| 261 |
sum += ((u16 *)packet)[i]; |
| 262 |
while (sum >> 16) |
| 263 |
sum = (sum & 0xffff) + (sum >> 16); |
| 264 |
udp.uh_sum = (sum == 0xffff) ? sum : ~sum; |
| 265 |
|
| 266 |
pcap_header.incl_len = pcap_header.orig_len = ipsz + sizeof(udp) + len; |
| 267 |
} |
| 268 |
else |
| 269 |
{ |
| 270 |
/* ICMP - ICMPv6 packet is a superset of ICMP */ |
| 271 |
struct icmp6_hdr *icmp = packet; |
| 272 |
|
| 273 |
/* See comment in UDP code above. */ |
| 274 |
sum += htons(proto); |
| 275 |
sum += htons(len); |
| 276 |
|
| 277 |
icmp->icmp6_cksum = 0; |
| 278 |
for (i = 0; i < (len + 1) / 2; i++) |
| 279 |
sum += ((u16 *)packet)[i]; |
| 280 |
while (sum >> 16) |
| 281 |
sum = (sum & 0xffff) + (sum >> 16); |
| 282 |
icmp->icmp6_cksum = (sum == 0xffff) ? sum : ~sum; |
| 283 |
|
| 284 |
pcap_header.incl_len = pcap_header.orig_len = ipsz + len; |
| 285 |
} |
| 286 |
|
| 287 |
rc = gettimeofday(&time, NULL); |
| 288 |
pcap_header.ts_sec = time.tv_sec; |
| 289 |
pcap_header.ts_usec = time.tv_usec; |
| 290 |
|
| 291 |
if (rc == -1 || |
| 292 |
!read_write(daemon->dumpfd, (void *)&pcap_header, sizeof(pcap_header), RW_WRITE) || |
| 293 |
!read_write(daemon->dumpfd, iphdr, ipsz, RW_WRITE) || |
| 294 |
(proto == IPPROTO_UDP && !read_write(daemon->dumpfd, (void *)&udp, sizeof(udp), RW_WRITE)) || |
| 295 |
!read_write(daemon->dumpfd, (void *)packet, len, RW_WRITE)) |
| 296 |
my_syslog(LOG_ERR, _("failed to write packet dump")); |
| 297 |
else if (option_bool(OPT_EXTRALOG) && (mask & 0x00ff)) |
| 298 |
my_syslog(LOG_INFO, _("%u dumping packet %u mask 0x%04x"), daemon->log_display_id, ++packet_count, mask); |
| 299 |
else |
| 300 |
my_syslog(LOG_INFO, _("dumping packet %u mask 0x%04x"), ++packet_count, mask); |
| 301 |
|
| 302 |
} |
| 303 |
|
| 304 |
#endif |