2 * collectd - src/utils_dns.c
3 * Copyright (C) 2006 Florian octo Forster
4 * Copyright (C) 2002 The Measurement Factory, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 * 3. Neither the name of The Measurement Factory nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 * The Measurement Factory, Inc. <http://www.measurement-factory.com/>
33 * Florian octo Forster <octo at collectd.org>
36 #define _DEFAULT_SOURCE
45 #include <net/if_arp.h>
50 #if HAVE_NET_PPP_DEFS_H
51 #include <net/ppp_defs.h>
54 #include <net/if_ppp.h>
57 #if HAVE_NETINET_IN_SYSTM_H
58 #include <netinet/in_systm.h>
61 #include <netinet/in.h>
63 #if HAVE_NETINET_IP6_H
64 #include <netinet/ip6.h>
66 #if HAVE_NETINET_IF_ETHER_H
67 #include <netinet/if_ether.h>
70 #include <netinet/ip.h>
72 #ifdef HAVE_NETINET_IP_VAR_H
73 #include <netinet/ip_var.h>
75 #if HAVE_NETINET_UDP_H
76 #include <netinet/udp.h>
80 #include <arpa/inet.h>
82 #if HAVE_ARPA_NAMESER_H
83 #include <arpa/nameser.h>
85 #if HAVE_ARPA_NAMESER_COMPAT_H
86 #include <arpa/nameser_compat.h>
97 #define PCAP_SNAPLEN 1460
99 #define ETHER_ADDR_LEN 6
100 #define ETHER_TYPE_LEN 2
101 #define ETHER_HDR_LEN (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN)
103 #ifndef ETHERTYPE_8021Q
104 #define ETHERTYPE_8021Q 0x8100
106 #ifndef ETHERTYPE_IPV6
107 #define ETHERTYPE_IPV6 0x86DD
110 #ifndef PPP_ADDRESS_VAL
111 #define PPP_ADDRESS_VAL 0xff /* The address byte value */
113 #ifndef PPP_CONTROL_VAL
114 #define PPP_CONTROL_VAL 0x03 /* The control byte value */
117 #if HAVE_STRUCT_UDPHDR_UH_DPORT && HAVE_STRUCT_UDPHDR_UH_SPORT
118 #define UDP_DEST uh_dport
119 #define UDP_SRC uh_sport
120 #elif HAVE_STRUCT_UDPHDR_DEST && HAVE_STRUCT_UDPHDR_SOURCE
121 #define UDP_DEST dest
122 #define UDP_SRC source
124 #error "`struct udphdr' is unusable."
127 #if HAVE_NETINET_IP6_H && HAVE_STRUCT_IP6_EXT
131 #include "utils_dns.h"
137 struct in6_addr addr;
139 struct ip_list_s *next;
141 typedef struct ip_list_s ip_list_t;
143 typedef int(printer)(const char *, ...);
146 * flags/features for non-interactive mode
161 static pcap_t *pcap_obj = NULL;
164 static ip_list_t *IgnoreList = NULL;
167 static void (*Callback)(const rfc1035_header_t *) = NULL;
169 static int query_count_intvl = 0;
170 static int query_count_total = 0;
172 static struct bpf_timeval last_ts;
174 static struct timeval last_ts;
175 #endif /* __OpenBSD__ */
176 #endif /* HAVE_PCAP_H */
178 static int cmp_in6_addr(const struct in6_addr *a, const struct in6_addr *b) {
181 assert(sizeof(struct in6_addr) == 16);
183 for (i = 0; i < 16; i++)
184 if (a->s6_addr[i] != b->s6_addr[i])
190 return a->s6_addr[i] > b->s6_addr[i] ? 1 : -1;
191 } /* int cmp_addrinfo */
193 static inline int ignore_list_match(const struct in6_addr *addr) {
194 for (ip_list_t *ptr = IgnoreList; ptr != NULL; ptr = ptr->next)
195 if (cmp_in6_addr(addr, &ptr->addr) == 0)
198 } /* int ignore_list_match */
200 static void ignore_list_add(const struct in6_addr *addr) {
203 if (ignore_list_match(addr) != 0)
206 new = malloc(sizeof(*new));
212 memcpy(&new->addr, addr, sizeof(struct in6_addr));
213 new->next = IgnoreList;
216 } /* void ignore_list_add */
218 void ignore_list_add_name(const char *name) {
219 struct addrinfo *ai_list;
220 struct in6_addr addr;
223 status = getaddrinfo(name, NULL, NULL, &ai_list);
227 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
228 ai_ptr = ai_ptr->ai_next) {
229 if (ai_ptr->ai_family == AF_INET) {
230 memset(&addr, '\0', sizeof(addr));
231 addr.s6_addr[10] = 0xFF;
232 addr.s6_addr[11] = 0xFF;
233 memcpy(addr.s6_addr + 12,
234 &((struct sockaddr_in *)ai_ptr->ai_addr)->sin_addr, 4);
236 ignore_list_add(&addr);
238 ignore_list_add(&((struct sockaddr_in6 *)ai_ptr->ai_addr)->sin6_addr);
242 freeaddrinfo(ai_list);
246 static void in6_addr_from_buffer(struct in6_addr *ia, const void *buf,
247 size_t buf_len, int family) {
248 memset(ia, 0, sizeof(struct in6_addr));
249 if ((AF_INET == family) && (sizeof(uint32_t) == buf_len)) {
250 ia->s6_addr[10] = 0xFF;
251 ia->s6_addr[11] = 0xFF;
252 memcpy(ia->s6_addr + 12, buf, buf_len);
253 } else if ((AF_INET6 == family) && (sizeof(struct in6_addr) == buf_len)) {
254 memcpy(ia, buf, buf_len);
256 } /* void in6_addr_from_buffer */
258 void dnstop_set_pcap_obj(pcap_t *po) { pcap_obj = po; }
260 void dnstop_set_callback(void (*cb)(const rfc1035_header_t *)) {
264 #define RFC1035_MAXLABELSZ 63
265 static int rfc1035NameUnpack(const char *buf, size_t sz, off_t *off, char *name,
270 static int loop_detect = 0;
272 return 4; /* compression loop */
274 return 4; /* probably compression loop */
276 if ((*off) >= ((off_t)sz))
280 /* blasted compression */
284 memcpy(&s, buf + (*off), sizeof(s));
288 if ((*off) >= ((off_t)sz))
289 return 1; /* message too short */
291 /* Make sure the pointer is inside this message */
292 if (ptr >= ((off_t)sz))
293 return 2; /* bad compression ptr */
294 if (ptr < DNS_MSG_HDR_SZ)
295 return 2; /* bad compression ptr */
297 rc = rfc1035NameUnpack(buf, sz, &ptr, name + no, ns - no);
300 } else if (c > RFC1035_MAXLABELSZ) {
302 * "(The 10 and 01 combinations are reserved for future use.)"
304 return 3; /* reserved label/compression flags */
312 if ((*off) + len > sz)
313 return 4; /* message is too short */
314 if (no + len + 1 > ns)
315 return 5; /* qname would overflow name buffer */
316 memcpy(name + no, buf + (*off), len);
319 *(name + (no++)) = '.';
323 *(name + no - 1) = '\0';
324 /* make sure we didn't allow someone to overflow the name buffer */
325 assert(no <= ((off_t)ns));
329 static int handle_dns(const char *buf, int len) {
336 /* The DNS header is 12 bytes long */
337 if (len < DNS_MSG_HDR_SZ)
340 memcpy(&us, buf + 0, 2);
343 memcpy(&us, buf + 2, 2);
345 qh.qr = (us >> 15) & 0x01;
346 qh.opcode = (us >> 11) & 0x0F;
347 qh.aa = (us >> 10) & 0x01;
348 qh.tc = (us >> 9) & 0x01;
349 qh.rd = (us >> 8) & 0x01;
350 qh.ra = (us >> 7) & 0x01;
351 qh.z = (us >> 6) & 0x01;
352 qh.ad = (us >> 5) & 0x01;
353 qh.cd = (us >> 4) & 0x01;
354 qh.rcode = us & 0x0F;
356 memcpy(&us, buf + 4, 2);
357 qh.qdcount = ntohs(us);
359 memcpy(&us, buf + 6, 2);
360 qh.ancount = ntohs(us);
362 memcpy(&us, buf + 8, 2);
363 qh.nscount = ntohs(us);
365 memcpy(&us, buf + 10, 2);
366 qh.arcount = ntohs(us);
368 offset = DNS_MSG_HDR_SZ;
369 memset(qh.qname, '\0', MAX_QNAME_SZ);
370 status = rfc1035NameUnpack(buf, len, &offset, qh.qname, MAX_QNAME_SZ);
372 INFO("utils_dns: handle_dns: rfc1035NameUnpack failed "
377 if ('\0' == qh.qname[0])
378 sstrncpy(qh.qname, ".", sizeof(qh.qname));
379 while ((t = strchr(qh.qname, '\n')))
381 while ((t = strchr(qh.qname, '\r')))
383 for (t = qh.qname; *t; t++)
384 *t = tolower((int)*t);
386 memcpy(&us, buf + offset, 2);
387 qh.qtype = ntohs(us);
388 memcpy(&us, buf + offset + 2, 2);
389 qh.qclass = ntohs(us);
391 qh.length = (uint16_t)len;
393 if (Callback != NULL)
399 static int handle_udp(const struct udphdr *udp, int len) {
400 char buf[PCAP_SNAPLEN];
401 if ((ntohs(udp->UDP_DEST) != 53) && (ntohs(udp->UDP_SRC) != 53))
403 memcpy(buf, udp + 1, len - sizeof(*udp));
404 if (0 == handle_dns(buf, len - sizeof(*udp)))
410 static int handle_ipv6(struct ip6_hdr *ipv6, int len) {
411 char buf[PCAP_SNAPLEN];
415 struct in6_addr c_src_addr;
416 uint16_t payload_len;
421 offset = sizeof(struct ip6_hdr);
422 nexthdr = ipv6->ip6_nxt;
423 c_src_addr = ipv6->ip6_src;
424 payload_len = ntohs(ipv6->ip6_plen);
426 if (ignore_list_match(&c_src_addr))
429 /* Parse extension headers. This only handles the standard headers, as
430 * defined in RFC 2460, correctly. Fragments are discarded. */
431 while ((IPPROTO_ROUTING == nexthdr) /* routing header */
432 || (IPPROTO_HOPOPTS == nexthdr) /* Hop-by-Hop options. */
433 || (IPPROTO_FRAGMENT == nexthdr) /* fragmentation header. */
434 || (IPPROTO_DSTOPTS == nexthdr) /* destination options. */
435 || (IPPROTO_AH == nexthdr) /* destination options. */
436 || (IPPROTO_ESP == nexthdr)) /* encapsulating security payload. */
438 struct ip6_ext ext_hdr;
439 uint16_t ext_hdr_len;
441 /* Catch broken packets */
442 if ((offset + sizeof(struct ip6_ext)) > (unsigned int)len)
445 /* Cannot handle fragments. */
446 if (IPPROTO_FRAGMENT == nexthdr)
449 memcpy(&ext_hdr, (char *)ipv6 + offset, sizeof(struct ip6_ext));
450 nexthdr = ext_hdr.ip6e_nxt;
451 ext_hdr_len = (8 * (ntohs(ext_hdr.ip6e_len) + 1));
453 /* This header is longer than the packets payload.. WTF? */
454 if (ext_hdr_len > payload_len)
457 offset += ext_hdr_len;
458 payload_len -= ext_hdr_len;
461 /* Catch broken and empty packets */
462 if (((offset + payload_len) > (unsigned int)len) || (payload_len == 0) ||
463 (payload_len > PCAP_SNAPLEN))
466 if (IPPROTO_UDP != nexthdr)
469 memcpy(buf, (char *)ipv6 + offset, payload_len);
470 if (handle_udp((struct udphdr *)buf, payload_len) == 0)
473 return 1; /* Success */
474 } /* int handle_ipv6 */
475 /* #endif HAVE_IPV6 */
477 #else /* if !HAVE_IPV6 */
478 static int handle_ipv6(__attribute__((unused)) void *pkg,
479 __attribute__((unused)) int len) {
482 #endif /* !HAVE_IPV6 */
484 static int handle_ip(const struct ip *ip, int len) {
485 char buf[PCAP_SNAPLEN];
486 int offset = ip->ip_hl << 2;
487 struct in6_addr c_src_addr;
488 struct in6_addr c_dst_addr;
491 return handle_ipv6((void *)ip, len);
493 in6_addr_from_buffer(&c_src_addr, &ip->ip_src.s_addr,
494 sizeof(ip->ip_src.s_addr), AF_INET);
495 in6_addr_from_buffer(&c_dst_addr, &ip->ip_dst.s_addr,
496 sizeof(ip->ip_dst.s_addr), AF_INET);
497 if (ignore_list_match(&c_src_addr))
499 if (IPPROTO_UDP != ip->ip_p)
501 memcpy(buf, ((char *)ip) + offset, len - offset);
502 if (0 == handle_udp((struct udphdr *)buf, len - offset))
507 #if HAVE_NET_IF_PPP_H
508 static int handle_ppp(const u_char *pkt, int len) {
509 char buf[PCAP_SNAPLEN];
511 unsigned short proto;
514 if (*pkt == PPP_ADDRESS_VAL && *(pkt + 1) == PPP_CONTROL_VAL) {
515 pkt += 2; /* ACFC not used */
521 proto = *pkt; /* PFC is used */
525 memcpy(&us, pkt, sizeof(us));
530 if (ETHERTYPE_IP != proto && PPP_IP != proto)
532 memcpy(buf, pkt, len);
533 return handle_ip((struct ip *)buf, len);
535 #endif /* HAVE_NET_IF_PPP_H */
537 static int handle_null(const u_char *pkt, int len) {
539 memcpy(&family, pkt, sizeof(family));
540 if (AF_INET != family)
542 return handle_ip((struct ip *)(pkt + 4), len - 4);
546 static int handle_loop(const u_char *pkt, int len) {
548 memcpy(&family, pkt, sizeof(family));
549 if (AF_INET != ntohl(family))
551 return handle_ip((struct ip *)(pkt + 4), len - 4);
557 static int handle_raw(const u_char *pkt, int len) {
558 return handle_ip((struct ip *)pkt, len);
563 static int handle_ether(const u_char *pkt, int len) {
564 char buf[PCAP_SNAPLEN];
565 struct ether_header *e = (void *)pkt;
566 unsigned short etype = ntohs(e->ether_type);
567 if (len < ETHER_HDR_LEN)
569 pkt += ETHER_HDR_LEN;
570 len -= ETHER_HDR_LEN;
571 if (ETHERTYPE_8021Q == etype) {
572 etype = ntohs(*(unsigned short *)(pkt + 2));
576 if ((ETHERTYPE_IP != etype) && (ETHERTYPE_IPV6 != etype))
578 memcpy(buf, pkt, len);
579 if (ETHERTYPE_IPV6 == etype)
580 return handle_ipv6((void *)buf, len);
582 return handle_ip((struct ip *)buf, len);
586 static int handle_linux_sll(const u_char *pkt, int len) {
596 if ((0 > len) || ((unsigned int)len < sizeof(struct sll_header)))
599 hdr = (struct sll_header *)pkt;
600 pkt = (u_char *)(hdr + 1);
601 len -= sizeof(struct sll_header);
603 etype = ntohs(hdr->proto_type);
605 if ((ETHERTYPE_IP != etype) && (ETHERTYPE_IPV6 != etype))
608 if (ETHERTYPE_IPV6 == etype)
609 return handle_ipv6((void *)pkt, len);
611 return handle_ip((struct ip *)pkt, len);
613 #endif /* DLT_LINUX_SLL */
615 /* public function */
616 void handle_pcap(u_char *udata, const struct pcap_pkthdr *hdr,
620 if (hdr->caplen < ETHER_HDR_LEN)
623 switch (pcap_datalink(pcap_obj)) {
625 status = handle_ether(pkt, hdr->caplen);
627 #if HAVE_NET_IF_PPP_H
629 status = handle_ppp(pkt, hdr->caplen);
634 status = handle_loop(pkt, hdr->caplen);
639 status = handle_raw(pkt, hdr->caplen);
644 status = handle_linux_sll(pkt, hdr->caplen);
648 status = handle_null(pkt, hdr->caplen);
652 ERROR("handle_pcap: unsupported data link type %d",
653 pcap_datalink(pcap_obj));
656 } /* switch (pcap_datalink(pcap_obj)) */
665 #endif /* HAVE_PCAP_H */
667 const char *qtype_str(int t) {
670 #if (defined(__NAMESER)) && (__NAMESER >= 19991001)
743 #if __NAMESER >= 19991006
757 #if __NAMESER >= 20090302
776 case ns_t_nsec3param:
793 #if __NAMESER >= 19991006
797 #if __NAMESER >= 20090302
801 /* #endif __NAMESER >= 19991001 */
802 #elif (defined(__BIND)) && (__BIND >= 19950621)
804 return "A"; /* 1 ... */
872 return "NAPTR"; /* ... 35 */
873 #if (__BIND >= 19960801)
875 return "KX"; /* 36 ... */
897 return "DNSKEY"; /* ... 48 */
899 return "TKEY"; /* 249 */
900 #endif /* __BIND >= 19960801 */
902 return "TSIG"; /* 250 ... */
912 return "ANY"; /* ... 255 */
913 #endif /* __BIND >= 19950621 */
915 snprintf(buf, sizeof(buf), "#%i", t);
920 const char *opcode_str(int o) {
934 snprintf(buf, sizeof(buf), "Opcode%d", o);
939 const char *rcode_str(int rcode) {
942 #if (defined(__NAMESER)) && (__NAMESER >= 19991006)
973 /* #endif __NAMESER >= 19991006 */
974 #elif (defined(__BIND)) && (__BIND >= 19950621)
987 #if defined(YXDOMAIN) && defined(NXRRSET)
998 #endif /* RFC2136 rcodes */
999 #endif /* __BIND >= 19950621 */
1001 snprintf(buf, sizeof(buf), "RCode%i", rcode);
1004 } /* const char *rcode_str (int rcode) */
1008 main(int argc, char *argv[])
1010 char errbuf[PCAP_ERRBUF_SIZE];
1013 int readfile_state = 0;
1014 struct bpf_program fp;
1017 SubReport = Sources_report;
1018 ignore_addr.s_addr = 0;
1019 progname = strdup(strrchr(argv[0], '/') ? strchr(argv[0], '/') + 1 : argv[0]);
1020 srandom(time(NULL));
1023 while ((x = getopt(argc, argv, "ab:f:i:pst")) != -1) {
1038 bpf_program_str = strdup(optarg);
1041 ignore_addr.s_addr = inet_addr(optarg);
1056 device = strdup(argv[0]);
1058 if (0 == stat(device, &sb))
1060 if (readfile_state) {
1061 pcap_obj = pcap_open_offline(device, errbuf);
1063 pcap_obj = pcap_open_live(device, PCAP_SNAPLEN, promisc_flag, 1000, errbuf);
1065 if (NULL == pcap_obj) {
1066 fprintf(stderr, "pcap_open_*: %s\n", errbuf);
1070 if (0 == isatty(1)) {
1071 if (0 == readfile_state) {
1072 fprintf(stderr, "Non-interactive mode requires savefile argument\n");
1076 print_func = printf;
1079 memset(&fp, '\0', sizeof(fp));
1080 x = pcap_compile(pcap_obj, &fp, bpf_program_str, 1, 0);
1082 fprintf(stderr, "pcap_compile failed\n");
1085 x = pcap_setfilter(pcap_obj, &fp);
1087 fprintf(stderr, "pcap_setfilter failed\n");
1092 * non-blocking call added for Mac OS X bugfix. Sent by Max Horn.
1093 * ref http://www.tcpdump.org/lists/workers/2002/09/msg00033.html
1095 x = pcap_setnonblock(pcap_obj, 1, errbuf);
1097 fprintf(stderr, "pcap_setnonblock failed: %s\n", errbuf);
1101 switch (pcap_datalink(pcap_obj)) {
1103 handle_datalink = handle_ether;
1105 #if HAVE_NET_IF_PPP_H
1107 handle_datalink = handle_ppp;
1112 handle_datalink = handle_loop;
1117 handle_datalink = handle_raw;
1121 handle_datalink = handle_null;
1124 fprintf(stderr, "unsupported data link type %d\n",
1125 pcap_datalink(pcap_obj));
1132 if (readfile_state < 2) {
1134 * On some OSes select() might return 0 even when
1135 * there are packets to process. Thus, we always
1136 * ignore its return value and just call pcap_dispatch()
1139 if (0 == readfile_state) /* interactive */
1140 pcap_select(pcap_obj, 1, 0);
1141 x = pcap_dispatch(pcap_obj, 50, handle_pcap, NULL);
1143 if (0 == x && 1 == readfile_state) {
1144 /* block on keyboard until user quits */
1153 endwin(); /* klin, Thu Nov 28 08:56:51 2002 */
1155 while (pcap_dispatch(pcap_obj, 50, handle_pcap, NULL))
1158 Sources_report(); print_func("\n");
1159 Destinatioreport(); print_func("\n");
1160 Qtypes_report(); print_func("\n");
1161 Opcodes_report(); print_func("\n");
1162 Tld_report(); print_func("\n");
1163 Sld_report(); print_func("\n");
1164 Nld_report(); print_func("\n");
1165 SldBySource_report();
1168 pcap_close(pcap_obj);
1170 } /* static int main(int argc, char *argv[]) */