blob: 6b5a1e9a830695daa9addbea7257b36d1a8ae01f [file] [log] [blame]
Simon Kelley6b173352018-05-08 18:32:14 +01001/* dnsmasq is Copyright (c) 2000-2018 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
21static u32 packet_count;
22
23/* https://wiki.wireshark.org/Development/LibpcapFileFormat */
24struct pcap_hdr_s {
25 u32 magic_number; /* magic number */
26 u16 version_major; /* major version number */
27 u16 version_minor; /* minor version number */
28 u32 thiszone; /* GMT to local correction */
29 u32 sigfigs; /* accuracy of timestamps */
30 u32 snaplen; /* max length of captured packets, in octets */
31 u32 network; /* data link type */
32};
33
34struct pcaprec_hdr_s {
35 u32 ts_sec; /* timestamp seconds */
36 u32 ts_usec; /* timestamp microseconds */
37 u32 incl_len; /* number of octets of packet saved in file */
38 u32 orig_len; /* actual length of packet */
39};
40
41
42void dump_init(void)
43{
44 struct stat buf;
45 struct pcap_hdr_s header;
46 struct pcaprec_hdr_s pcap_header;
47
48 packet_count = 0;
49
50 if (stat(daemon->dump_file, &buf) == -1)
51 {
52 /* doesn't exist, create and add header */
53 header.magic_number = 0xa1b2c3d4;
54 header.version_major = 2;
55 header.version_minor = 4;
56 header.thiszone = 0;
57 header.sigfigs = 0;
58 header.snaplen = daemon->edns_pktsz + 200; /* slop for IP/UDP headers */
59 header.network = 101; /* DLT_RAW http://www.tcpdump.org/linktypes.html */
60
61 if (errno != ENOENT ||
62 (daemon->dumpfd = creat(daemon->dump_file, S_IRUSR | S_IWUSR)) == -1 ||
63 !read_write(daemon->dumpfd, (void *)&header, sizeof(header), 0))
64 die(_("cannot create %s: %s"), daemon->dump_file, EC_FILE);
65 }
66 else if ((daemon->dumpfd = open(daemon->dump_file, O_APPEND | O_RDWR)) == -1 ||
67 !read_write(daemon->dumpfd, (void *)&header, sizeof(header), 1) ||
68 header.magic_number != 0xa1b2c3d4)
69 die(_("cannot access %s: %s"), daemon->dump_file, EC_FILE);
70 else
71 {
72 /* count existing records */
73 while (read_write(daemon->dumpfd, (void *)&pcap_header, sizeof(pcap_header), 1))
74 {
75 lseek(daemon->dumpfd, pcap_header.incl_len, SEEK_CUR);
76 packet_count++;
77 }
78 }
79}
80
81void dump_packet(int mask, void *packet, size_t len, union mysockaddr *src, union mysockaddr *dst)
82{
83 struct ip ip;
84#ifdef HAVE_IPV6
85 struct ip6_hdr ip6;
86 int family;
87#endif
88 struct udphdr {
89 u16 uh_sport; /* source port */
90 u16 uh_dport; /* destination port */
91 u16 uh_ulen; /* udp length */
92 u16 uh_sum; /* udp checksum */
93 } udp;
94 struct pcaprec_hdr_s pcap_header;
95 struct timeval time;
96 u32 i, sum;
97 void *iphdr;
98 size_t ipsz;
99 int rc;
100
101 if (daemon->dumpfd == -1 || !(mask & daemon->dump_mask))
102 return;
103
104 /* So wireshark can Id the packet. */
105 udp.uh_sport = udp.uh_dport = htons(NAMESERVER_PORT);
106
107#ifdef HAVE_IPV6
108 if (src)
109 family = src->sa.sa_family;
110 else
111 family = dst->sa.sa_family;
112
113 if (family == AF_INET6)
114 {
115 iphdr = &ip6;
116 ipsz = sizeof(ip6);
117 memset(&ip6, 0, sizeof(ip6));
118
119 ip6.ip6_vfc = 6 << 4;
120 ip6.ip6_plen = htons(sizeof(struct udphdr) + len);
121 ip6.ip6_nxt = IPPROTO_UDP;
122 ip6.ip6_hops = 64;
123
124 if (src)
125 {
126 memcpy(&ip6.ip6_src, &src->in6.sin6_addr, IN6ADDRSZ);
127 udp.uh_sport = src->in6.sin6_port;
128 }
129
130 if (dst)
131 {
132 memcpy(&ip6.ip6_dst, &dst->in6.sin6_addr, IN6ADDRSZ);
133 udp.uh_dport = dst->in6.sin6_port;
134 }
135
136 /* start UDP checksum */
137 for (sum = 0, i = 0; i < IN6ADDRSZ; i++)
138 sum += ((u16 *)&ip6.ip6_src)[i];
139 }
140 else
141#endif
142 {
143 iphdr = &ip;
144 ipsz = sizeof(ip);
145 memset(&ip, 0, sizeof(ip));
146
147 ip.ip_v = IPVERSION;
148 ip.ip_hl = sizeof(struct ip) / 4;
149 ip.ip_len = htons(sizeof(struct ip) + sizeof(struct udphdr) + len);
150 ip.ip_ttl = IPDEFTTL;
151 ip.ip_p = IPPROTO_UDP;
152
153 if (src)
154 {
155 ip.ip_src = src->in.sin_addr;
156 udp.uh_sport = src->in.sin_port;
157 }
158
159 if (dst)
160 {
161 ip.ip_dst = dst->in.sin_addr;
162 udp.uh_dport = dst->in.sin_port;
163 }
164
165 ip.ip_sum = 0;
166 for (sum = 0, i = 0; i < sizeof(struct ip) / 2; i++)
167 sum += ((u16 *)&ip)[i];
168 while (sum >> 16)
169 sum = (sum & 0xffff) + (sum >> 16);
170 ip.ip_sum = (sum == 0xffff) ? sum : ~sum;
171
172 /* start UDP checksum */
173 sum = ip.ip_src.s_addr & 0xffff;
174 sum += (ip.ip_src.s_addr >> 16) & 0xffff;
175 sum += ip.ip_dst.s_addr & 0xffff;
176 sum += (ip.ip_dst.s_addr >> 16) & 0xffff;
177 }
178
179 if (len & 1)
180 ((unsigned char *)packet)[len] = 0; /* for checksum, in case length is odd. */
181
182 udp.uh_sum = 0;
183 udp.uh_ulen = htons(sizeof(struct udphdr) + len);
184 sum += htons(IPPROTO_UDP);
185 sum += htons(sizeof(struct udphdr) + len);
186 for (i = 0; i < sizeof(struct udphdr)/2; i++)
187 sum += ((u16 *)&udp)[i];
188 for (i = 0; i < (len + 1) / 2; i++)
189 sum += ((u16 *)packet)[i];
190 while (sum >> 16)
191 sum = (sum & 0xffff) + (sum >> 16);
192 udp.uh_sum = (sum == 0xffff) ? sum : ~sum;
193
194 rc = gettimeofday(&time, NULL);
195 pcap_header.ts_sec = time.tv_sec;
196 pcap_header.ts_usec = time.tv_usec;
197 pcap_header.incl_len = pcap_header.orig_len = ipsz + sizeof(udp) + len;
198
199 if (rc == -1 ||
200 !read_write(daemon->dumpfd, (void *)&pcap_header, sizeof(pcap_header), 0) ||
201 !read_write(daemon->dumpfd, iphdr, ipsz, 0) ||
202 !read_write(daemon->dumpfd, (void *)&udp, sizeof(udp), 0) ||
203 !read_write(daemon->dumpfd, (void *)packet, len, 0))
204 my_syslog(LOG_ERR, _("failed to write packet dump"));
205 else
206 my_syslog(LOG_INFO, _("dumping UDP packet %u mask 0x%04x"), ++packet_count, mask);
207
208}
209
210#endif