Add access challenge as a response type in radsniff
[freeradius.git] / src / main / radsniff.c
1 /*
2  *   This program is is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License, version 2 if the
4  *   License as published by the Free Software Foundation.
5  *
6  *   This program is distributed in the hope that it will be useful,
7  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
8  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  *   GNU General Public License for more details.
10  *
11  *   You should have received a copy of the GNU General Public License
12  *   along with this program; if not, write to the Free Software
13  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
14  */
15
16 /**
17  * $Id$
18  * @file radsniff.c
19  * @brief Capture, filter, and generate statistics for RADIUS traffic
20  *
21  * @copyright 2013 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
22  * @copyright 2006 The FreeRADIUS server project
23  * @copyright 2006 Nicolas Baradakis <nicolas.baradakis@cegetel.net>
24  */
25
26 RCSID("$Id$")
27
28 #define _LIBRADIUS 1
29 #include <assert.h>
30 #include <time.h>
31 #include <math.h>
32 #include <freeradius-devel/libradius.h>
33 #include <freeradius-devel/event.h>
34
35 #include <freeradius-devel/radpaths.h>
36 #include <freeradius-devel/conf.h>
37 #include <freeradius-devel/pcap.h>
38 #include <freeradius-devel/radsniff.h>
39
40 #ifdef HAVE_COLLECTDC_H
41 #  include <collectd/client.h>
42 #endif
43
44 static rs_t *conf;
45 struct timeval start_pcap = {0, 0};
46 static char timestr[50];
47
48 static rbtree_t *request_tree = NULL;
49 static rbtree_t *link_tree = NULL;
50 static fr_event_list_t *events;
51 static bool cleanup;
52
53 static int self_pipe[2] = {-1, -1};             //!< Signals from sig handlers
54
55 typedef int (*rbcmp)(void const *, void const *);
56
57 static char const *radsniff_version = "radsniff version " RADIUSD_VERSION_STRING
58 #ifdef RADIUSD_VERSION_COMMIT
59 " (git #" STRINGIFY(RADIUSD_VERSION_COMMIT) ")"
60 #endif
61 ", built on " __DATE__ " at " __TIME__;
62
63 static int rs_useful_codes[] = {
64         PW_CODE_AUTHENTICATION_REQUEST,         //!< RFC2865 - Authentication request
65         PW_CODE_AUTHENTICATION_ACK,             //!< RFC2865 - Access-Accept
66         PW_CODE_AUTHENTICATION_REJECT,          //!< RFC2865 - Access-Reject
67         PW_CODE_ACCOUNTING_REQUEST,             //!< RFC2866 - Accounting-Request
68         PW_CODE_ACCOUNTING_RESPONSE,            //!< RFC2866 - Accounting-Response
69         PW_CODE_ACCESS_CHALLENGE,               //!< RFC2865 - Access-Challenge
70         PW_CODE_STATUS_SERVER,                  //!< RFC2865/RFC5997 - Status Server (request)
71         PW_CODE_STATUS_CLIENT,                  //!< RFC2865/RFC5997 - Status Server (response)
72         PW_CODE_DISCONNECT_REQUEST,             //!< RFC3575/RFC5176 - Disconnect-Request
73         PW_CODE_DISCONNECT_ACK,                 //!< RFC3575/RFC5176 - Disconnect-Ack (positive)
74         PW_CODE_DISCONNECT_NAK,                 //!< RFC3575/RFC5176 - Disconnect-Nak (not willing to perform)
75         PW_CODE_COA_REQUEST,                    //!< RFC3575/RFC5176 - CoA-Request
76         PW_CODE_COA_ACK,                        //!< RFC3575/RFC5176 - CoA-Ack (positive)
77         PW_CODE_COA_NAK,                        //!< RFC3575/RFC5176 - CoA-Nak (not willing to perform)
78 };
79
80 const FR_NAME_NUMBER rs_events[] = {
81         { "received",   RS_NORMAL       },
82         { "norsp",      RS_LOST         },
83         { "rtx",        RS_RTX          },
84         { "noreq",      RS_UNLINKED     },
85         { "reused",     RS_REUSED       },
86         { "error",      RS_ERROR        },
87         {  NULL , -1 }
88 };
89
90 static void NEVER_RETURNS usage(int status);
91
92 /** Fork and kill the parent process, writing out our PID
93  *
94  * @param pidfile the PID file to write our PID to
95  */
96 static void rs_daemonize(char const *pidfile)
97 {
98         FILE *fp;
99         pid_t pid, sid;
100
101         pid = fork();
102         if (pid < 0) {
103                 exit(EXIT_FAILURE);
104         }
105
106         /*
107          *      Kill the parent...
108          */
109         if (pid > 0) {
110                 close(self_pipe[0]);
111                 close(self_pipe[1]);
112                 exit(EXIT_SUCCESS);
113         }
114
115         /*
116          *      Continue as the child.
117          */
118
119         /* Create a new SID for the child process */
120         sid = setsid();
121         if (sid < 0) {
122                 exit(EXIT_FAILURE);
123         }
124
125         /*
126          *      Change the current working directory. This prevents the current
127          *      directory from being locked; hence not being able to remove it.
128          */
129         if ((chdir("/")) < 0) {
130                 exit(EXIT_FAILURE);
131         }
132
133         /*
134          *      And write it AFTER we've forked, so that we write the
135          *      correct PID.
136          */
137         fp = fopen(pidfile, "w");
138         if (fp != NULL) {
139                 fprintf(fp, "%d\n", (int) sid);
140                 fclose(fp);
141         } else {
142                 ERROR("Failed creating PID file %s: %s", pidfile, fr_syserror(errno));
143                 exit(EXIT_FAILURE);
144         }
145
146         /*
147          *      Close stdout and stderr if they've not been redirected.
148          */
149         if (isatty(fileno(stdout))) {
150                 if (!freopen("/dev/null", "w", stdout)) {
151                         exit(EXIT_FAILURE);
152                 }
153         }
154
155         if (isatty(fileno(stderr))) {
156                 if (!freopen("/dev/null", "w", stderr)) {
157                         exit(EXIT_FAILURE);
158                 }
159         }
160 }
161
162 #define USEC 1000000
163 static void rs_tv_sub(struct timeval const *end, struct timeval const *start, struct timeval *elapsed)
164 {
165         elapsed->tv_sec = end->tv_sec - start->tv_sec;
166         if (elapsed->tv_sec > 0) {
167                 elapsed->tv_sec--;
168                 elapsed->tv_usec = USEC;
169         } else {
170                 elapsed->tv_usec = 0;
171         }
172         elapsed->tv_usec += end->tv_usec;
173         elapsed->tv_usec -= start->tv_usec;
174
175         if (elapsed->tv_usec >= USEC) {
176                 elapsed->tv_usec -= USEC;
177                 elapsed->tv_sec++;
178         }
179 }
180
181 static void rs_tv_add_ms(struct timeval const *start, unsigned long interval, struct timeval *result) {
182     result->tv_sec = start->tv_sec + (interval / 1000);
183     result->tv_usec = start->tv_usec + ((interval % 1000) * 1000);
184
185     if (result->tv_usec > USEC) {
186         result->tv_usec -= USEC;
187         result->tv_sec++;
188     }
189 }
190
191 static void rs_time_print(char *out, size_t len, struct timeval const *t)
192 {
193         size_t ret;
194         struct timeval now;
195         uint32_t usec;
196
197         if (!t) {
198                 gettimeofday(&now, NULL);
199                 t = &now;
200         }
201
202         ret = strftime(out, len, "%Y-%m-%d %H:%M:%S", localtime(&t->tv_sec));
203         if (ret >= len) {
204                 return;
205         }
206
207         usec = t->tv_usec;
208
209         if (usec) {
210                 while (usec < 100000) usec *= 10;
211                 snprintf(out + ret, len - ret, ".%i", usec);
212         } else {
213                 snprintf(out + ret, len - ret, ".000000");
214         }
215 }
216
217 static void rs_packet_print_null(UNUSED uint64_t count, UNUSED rs_status_t status, UNUSED fr_pcap_t *handle,
218                                  UNUSED RADIUS_PACKET *packet, UNUSED struct timeval *elapsed,
219                                  UNUSED struct timeval *latency, UNUSED bool response, UNUSED bool body)
220 {
221         return;
222 }
223
224 static size_t rs_prints_csv(char *out, size_t outlen, char const *in, size_t inlen)
225 {
226         char const      *start = out;
227         uint8_t const   *str = (uint8_t const *) in;
228
229         if (!in) {
230                 if (outlen) {
231                         *out = '\0';
232                 }
233
234                 return 0;
235         }
236
237         if (inlen == 0) {
238                 inlen = strlen(in);
239         }
240
241         while ((inlen > 0) && (outlen > 2)) {
242                 /*
243                  *      Escape double quotes with... MORE DOUBLE QUOTES!
244                  */
245                 if (*str == '"') {
246                         *out++ = '"';
247                         outlen--;
248                 }
249
250                 /*
251                  *      Safe chars which require no escaping
252                  */
253                 if ((*str == '\r') || (*str == '\n') || ((*str >= '\x20') && (*str <= '\x7E'))) {
254                         *out++ = *str++;
255                         outlen--;
256                         inlen--;
257
258                         continue;
259                 }
260
261                 /*
262                  *      Everything else is dropped
263                  */
264                 str++;
265                 inlen--;
266         }
267         *out = '\0';
268
269         return out - start;
270 }
271
272 static void rs_packet_print_csv_header(void)
273 {
274         char buffer[2048];
275         char *p = buffer;
276         int i;
277
278         ssize_t len, s = sizeof(buffer);
279
280         len = strlcpy(p, "\"Status\",\"Count\",\"Time\",\"Latency\",\"Type\",\"Interface\","
281                       "\"Src IP\",\"Src Port\",\"Dst IP\",\"Dst Port\",\"ID\",", s);
282         p += len;
283         s -= len;
284
285         if (s <= 0) return;
286
287         for (i = 0; i < conf->list_da_num; i++) {
288                 char const *in;
289
290                 *p++ = '"';
291                 s -= 1;
292                 if (s <= 0) return;
293
294                 for (in = conf->list_da[i]->name; *in; in++) {
295                         *p++ = *in;
296                         s -= len;
297                         if (s <= 0) return;
298                 }
299
300                 *p++ = '"';
301                 s -= 1;
302                 if (s <= 0) return;
303                 *p++ = ',';
304                 s -= 1;
305                 if (s <= 0) return;
306         }
307
308         *--p = '\0';
309
310         fprintf(stdout , "%s\n", buffer);
311 }
312
313 static void rs_packet_print_csv(uint64_t count, rs_status_t status, fr_pcap_t *handle, RADIUS_PACKET *packet,
314                                 UNUSED struct timeval *elapsed, struct timeval *latency, UNUSED bool response,
315                                 bool body)
316 {
317         char const *status_str;
318         char buffer[2048];
319         char *p = buffer;
320
321         char src[INET6_ADDRSTRLEN];
322         char dst[INET6_ADDRSTRLEN];
323
324         ssize_t len, s = sizeof(buffer);
325
326         inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, src, sizeof(src));
327         inet_ntop(packet->dst_ipaddr.af, &packet->dst_ipaddr.ipaddr, dst, sizeof(dst));
328
329         status_str = fr_int2str(rs_events, status, NULL);
330         assert(status_str);
331
332         len = snprintf(p, s, "%s,%" PRIu64 ",%s,", status_str, count, timestr);
333         p += len;
334         s -= len;
335
336         if (s <= 0) return;
337
338         if (latency) {
339                 len = snprintf(p, s, "%u.%03u,",
340                                (unsigned int) latency->tv_sec, ((unsigned int) latency->tv_usec / 1000));
341                 p += len;
342                 s -= len;
343         } else {
344                 *p = ',';
345                 p += 1;
346                 s -= 1;
347         }
348
349         if (s <= 0) return;
350
351         /* Status, Type, Interface, Src, Src port, Dst, Dst port, ID */
352         if (is_radius_code(packet->code)) {
353                 len = snprintf(p, s, "%s,%s,%s,%i,%s,%i,%i,", fr_packet_codes[packet->code], handle->name,
354                                src, packet->src_port, dst, packet->dst_port, packet->id);
355         } else {
356                 len = snprintf(p, s, "%i,%s,%s,%i,%s,%i,%i,", packet->code, handle->name,
357                                src, packet->src_port, dst, packet->dst_port, packet->id);
358         }
359         p += len;
360         s -= len;
361
362         if (s <= 0) return;
363
364         if (body) {
365                 int i;
366                 VALUE_PAIR *vp;
367
368                 for (i = 0; i < conf->list_da_num; i++) {
369                         vp = pairfind_da(packet->vps, conf->list_da[i], TAG_ANY);
370                         if (vp && (vp->length > 0)) {
371                                 if (conf->list_da[i]->type == PW_TYPE_STRING) {
372                                         *p++ = '"';
373                                         s--;
374                                         if (s <= 0) return;
375
376                                         len = rs_prints_csv(p, s, vp->vp_strvalue, vp->length);
377                                         p += len;
378                                         s -= len;
379                                         if (s <= 0) return;
380
381                                         *p++ = '"';
382                                         s--;
383                                         if (s <= 0) return;
384                                 } else {
385                                         len = vp_prints_value(p, s, vp, 0);
386                                         p += len;
387                                         s -= len;
388                                         if (s <= 0) return;
389                                 }
390                         }
391
392                         *p++ = ',';
393                         s -= 1;
394                         if (s <= 0) return;
395                 }
396         } else {
397                 s -= conf->list_da_num;
398                 if (s <= 0) return;
399
400                 memset(p, ',', conf->list_da_num);
401                 p += conf->list_da_num;
402         }
403
404         *--p = '\0';
405         fprintf(stdout , "%s\n", buffer);
406 }
407
408 static void rs_packet_print_fancy(uint64_t count, rs_status_t status, fr_pcap_t *handle, RADIUS_PACKET *packet,
409                                   struct timeval *elapsed, struct timeval *latency, bool response, bool body)
410 {
411         char buffer[2048];
412         char *p = buffer;
413
414         char src[INET6_ADDRSTRLEN];
415         char dst[INET6_ADDRSTRLEN];
416
417         ssize_t len, s = sizeof(buffer);
418
419         inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, src, sizeof(src));
420         inet_ntop(packet->dst_ipaddr.af, &packet->dst_ipaddr.ipaddr, dst, sizeof(dst));
421
422         /* Only print out status str if something's not right */
423         if (status != RS_NORMAL) {
424                 char const *status_str;
425
426                 status_str = fr_int2str(rs_events, status, NULL);
427                 assert(status_str);
428
429                 len = snprintf(p, s, "** %s ** ", status_str);
430                 p += len;
431                 s -= len;
432                 if (s <= 0) return;
433         }
434
435         if (is_radius_code(packet->code)) {
436                 len = snprintf(p, s, "%s Id %i %s:%s:%d %s %s:%i ",
437                                fr_packet_codes[packet->code],
438                                packet->id,
439                                handle->name,
440                                response ? dst : src,
441                                response ? packet->dst_port : packet->src_port,
442                                response ? "<-" : "->",
443                                response ? src : dst ,
444                                response ? packet->src_port : packet->dst_port);
445         } else {
446                 len = snprintf(p, s, "%i Id %i %s:%s:%i %s %s:%i ",
447                                packet->code,
448                                packet->id,
449                                handle->name,
450                                response ? dst : src,
451                                response ? packet->dst_port : packet->src_port,
452                                response ? "<-" : "->",
453                                response ? src : dst ,
454                                response ? packet->src_port : packet->dst_port);
455         }
456         p += len;
457         s -= len;
458         if (s <= 0) return;
459
460         if (elapsed) {
461                 len = snprintf(p, s, "+%u.%03u ",
462                                (unsigned int) elapsed->tv_sec, ((unsigned int) elapsed->tv_usec / 1000));
463                 p += len;
464                 s -= len;
465                 if (s <= 0) return;
466         }
467
468         if (latency) {
469                 len = snprintf(p, s, "+%u.%03u ",
470                                (unsigned int) latency->tv_sec, ((unsigned int) latency->tv_usec / 1000));
471                 p += len;
472                 s -= len;
473                 if (s <= 0) return;
474         }
475
476         *--p = '\0';
477
478         RIDEBUG("%s", buffer);
479
480         if (body) {
481                 /*
482                  *      Print out verbose HEX output
483                  */
484                 if (conf->print_packet && (fr_debug_flag > 3)) {
485                         rad_print_hex(packet);
486                 }
487
488                 if (conf->print_packet && (fr_debug_flag > 1) && packet->vps) {
489                         pairsort(&packet->vps, attrtagcmp);
490                         vp_printlist(fr_log_fp, packet->vps);
491                 }
492         }
493 }
494
495 static void rs_stats_print(rs_latency_t *stats, PW_CODE code)
496 {
497         int i;
498         bool have_rt = false;
499
500         for (i = 0; i <= RS_RETRANSMIT_MAX; i++) {
501                 if (stats->interval.rt[i]) {
502                         have_rt = true;
503                 }
504         }
505
506         if (!stats->interval.received && !have_rt && !stats->interval.reused) {
507                 return;
508         }
509
510         if (stats->interval.received || stats->interval.linked) {
511                 INFO("%s counters:", fr_packet_codes[code]);
512                 if (stats->interval.received > 0) {
513                         INFO("\tTotal     : %.3lf/s" , stats->interval.received);
514                 }
515         }
516
517         if (stats->interval.linked > 0) {
518                 INFO("\tLinked    : %.3lf/s", stats->interval.linked);
519                 INFO("\tUnlinked  : %.3lf/s", stats->interval.unlinked);
520                 INFO("%s latency:", fr_packet_codes[code]);
521                 INFO("\tHigh      : %.3lfms", stats->interval.latency_high);
522                 INFO("\tLow       : %.3lfms", stats->interval.latency_low);
523                 INFO("\tAverage   : %.3lfms", stats->interval.latency_average);
524                 INFO("\tMA        : %.3lfms", stats->latency_smoothed);
525         }
526
527         if (have_rt || stats->interval.lost || stats->interval.reused) {
528                 INFO("%s retransmits & loss:",  fr_packet_codes[code]);
529
530                 if (stats->interval.lost) {
531                         INFO("\tLost      : %.3lf/s", stats->interval.lost);
532                 }
533
534                 if (stats->interval.reused) {
535                         INFO("\tID Reused : %.3lf/s", stats->interval.reused);
536                 }
537
538                 for (i = 0; i <= RS_RETRANSMIT_MAX; i++) {
539                         if (!stats->interval.rt[i]) {
540                                 continue;
541                         }
542
543                         if (i != RS_RETRANSMIT_MAX) {
544                                 INFO("\tRT (%i)    : %.3lf/s", i, stats->interval.rt[i]);
545                         } else {
546                                 INFO("\tRT (%i+)   : %.3lf/s", i, stats->interval.rt[i]);
547                         }
548                 }
549         }
550 }
551
552 /** Query libpcap to see if it dropped any packets
553  *
554  * We need to check to see if libpcap dropped any packets and if it did, we need to stop stats output for long
555  * enough for inaccurate statistics to be cleared out.
556  *
557  * @param in pcap handle to check.
558  * @param interval time between checks (used for debug output)
559  * @return 0, no drops, -1 we couldn't check, -2 dropped because of buffer exhaustion, -3 dropped because of NIC.
560  */
561 static int rs_check_pcap_drop(fr_pcap_t *in, int interval) {
562         int ret = 0;
563         struct pcap_stat pstats;
564
565         if (pcap_stats(in->handle, &pstats) != 0) {
566                 ERROR("%s failed retrieving pcap stats: %s", in->name, pcap_geterr(in->handle));
567                 return -1;
568         }
569
570         INFO("\t%s%*s: %.3lf/s", in->name, (int) (10 - strlen(in->name)), "",
571              ((double) (pstats.ps_recv - in->pstats.ps_recv)) / interval);
572
573         if (pstats.ps_drop - in->pstats.ps_drop > 0) {
574                 ERROR("%s dropped %i packets: Buffer exhaustion", in->name, pstats.ps_drop - in->pstats.ps_drop);
575                 ret = -2;
576         }
577
578         if (pstats.ps_ifdrop - in->pstats.ps_ifdrop > 0) {
579                 ERROR("%s dropped %i packets: Interface", in->name, pstats.ps_ifdrop - in->pstats.ps_ifdrop);
580                 ret = -3;
581         }
582
583         in->pstats = pstats;
584
585         return ret;
586 }
587
588 /** Update smoothed average
589  *
590  */
591 static void rs_stats_process_latency(rs_latency_t *stats)
592 {
593         /*
594          *      If we didn't link any packets during this interval, we don't have a value to return.
595          *      returning 0 is misleading as it would be like saying the latency had dropped to 0.
596          *      We instead set NaN which libcollectd converts to a 'U' or unknown value.
597          *
598          *      This will cause gaps in graphs, but is completely legitimate as we are missing data.
599          *      This is unfortunately an effect of being just a passive observer.
600          */
601         if (stats->interval.linked_total == 0) {
602                 double unk = strtod("NAN()", (char **) NULL);
603
604                 stats->interval.latency_average = unk;
605                 stats->interval.latency_high = unk;
606                 stats->interval.latency_low = unk;
607
608                 /*
609                  *      We've not yet been able to determine latency, so latency_smoothed is also NaN
610                  */
611                 if (stats->latency_smoothed_count == 0) {
612                         stats->latency_smoothed = unk;
613                 }
614                 return;
615         }
616
617         if (stats->interval.linked_total && stats->interval.latency_total) {
618                 stats->interval.latency_average = (stats->interval.latency_total / stats->interval.linked_total);
619         }
620
621         if (isnan(stats->latency_smoothed)) {
622                 stats->latency_smoothed = 0;
623         }
624         if (stats->interval.latency_average > 0) {
625                 stats->latency_smoothed_count++;
626                 stats->latency_smoothed += ((stats->interval.latency_average - stats->latency_smoothed) /
627                                        ((stats->latency_smoothed_count < 100) ? stats->latency_smoothed_count : 100));
628         }
629 }
630
631 static void rs_stats_process_counters(rs_latency_t *stats)
632 {
633         int i;
634
635         stats->interval.received = ((long double) stats->interval.received_total) / conf->stats.interval;
636         stats->interval.linked = ((long double) stats->interval.linked_total) / conf->stats.interval;
637         stats->interval.unlinked = ((long double) stats->interval.unlinked_total) / conf->stats.interval;
638         stats->interval.reused = ((long double) stats->interval.reused_total) / conf->stats.interval;
639         stats->interval.lost = ((long double) stats->interval.lost_total) / conf->stats.interval;
640
641         for (i = 0; i < RS_RETRANSMIT_MAX; i++) {
642                 stats->interval.rt[i] = ((long double) stats->interval.rt_total[i]) / conf->stats.interval;
643         }
644 }
645
646 /** Process stats for a single interval
647  *
648  */
649 static void rs_stats_process(void *ctx)
650 {
651         size_t i;
652         size_t rs_codes_len = (sizeof(rs_useful_codes) / sizeof(*rs_useful_codes));
653         fr_pcap_t               *in_p;
654         rs_update_t             *this = ctx;
655         rs_stats_t              *stats = this->stats;
656         struct timeval          now;
657
658         gettimeofday(&now, NULL);
659
660         stats->intervals++;
661
662         INFO("######### Stats Iteration %i #########", stats->intervals);
663
664         /*
665          *      Verify that none of the pcap handles have dropped packets.
666          */
667         INFO("Interface capture rate:");
668         for (in_p = this->in;
669              in_p;
670              in_p = in_p->next) {
671                 if (rs_check_pcap_drop(in_p, conf->stats.interval) < 0) {
672                         ERROR("Muting stats for the next %i milliseconds", conf->stats.timeout);
673
674                         rs_tv_add_ms(&now, conf->stats.timeout, &stats->quiet);
675                         goto clear;
676                 }
677         }
678
679         if ((stats->quiet.tv_sec + (stats->quiet.tv_usec / 1000000.0)) -
680             (now.tv_sec + (now.tv_usec / 1000000.0)) > 0) {
681                 INFO("Stats muted because of warmup, or previous error");
682                 goto clear;
683         }
684
685         /*
686          *      Latency stats need a bit more work to calculate the SMA.
687          *
688          *      No further work is required for codes.
689          */
690         for (i = 0; i < rs_codes_len; i++) {
691                 rs_stats_process_latency(&stats->exchange[rs_useful_codes[i]]);
692                 rs_stats_process_counters(&stats->exchange[rs_useful_codes[i]]);
693                 if (fr_debug_flag > 0) {
694                         rs_stats_print(&stats->exchange[rs_useful_codes[i]], rs_useful_codes[i]);
695                 }
696         }
697
698 #ifdef HAVE_COLLECTDC_H
699         /*
700          *      Update stats in collectd using the complex structures we
701          *      initialised earlier.
702          */
703         if ((conf->stats.out == RS_STATS_OUT_COLLECTD) && conf->stats.handle) {
704                 rs_stats_collectd_do_stats(conf, conf->stats.tmpl, &now);
705         }
706 #endif
707
708         clear:
709         /*
710          *      Rinse and repeat...
711          */
712         for (i = 0; i < rs_codes_len; i++) {
713                 memset(&stats->exchange[rs_useful_codes[i]].interval, 0,
714                        sizeof(stats->exchange[rs_useful_codes[i]].interval));
715         }
716
717         {
718                 static fr_event_t *event;
719
720                 now.tv_sec += conf->stats.interval;
721                 now.tv_usec = 0;
722
723                 if (!fr_event_insert(this->list, rs_stats_process, ctx, &now, &event)) {
724                         ERROR("Failed inserting stats interval event");
725                 }
726         }
727 }
728
729
730 /** Update latency statistics for request/response and forwarded packets
731  *
732  */
733 static void rs_stats_update_latency(rs_latency_t *stats, struct timeval *latency)
734 {
735         double lint;
736
737         stats->interval.linked_total++;
738         /* More useful is this in milliseconds */
739         lint = (latency->tv_sec + (latency->tv_usec / 1000000.0)) * 1000;
740         if (lint > stats->interval.latency_high) {
741                 stats->interval.latency_high = lint;
742         }
743         if (!stats->interval.latency_low || (lint < stats->interval.latency_low)) {
744                 stats->interval.latency_low = lint;
745         }
746         stats->interval.latency_total += lint;
747
748 }
749
750 /** Copy a subset of attributes from one list into the other
751  *
752  * Should be O(n) if all the attributes exist.  List must be pre-sorted.
753  */
754 static int rs_get_pairs(TALLOC_CTX *ctx, VALUE_PAIR **out, VALUE_PAIR *vps, DICT_ATTR const *da[], int num)
755 {
756         vp_cursor_t list_cursor, out_cursor;
757         VALUE_PAIR *match, *last_match, *copy;
758         uint64_t count = 0;
759         int i;
760
761         last_match = vps;
762
763         fr_cursor_init(&list_cursor, &last_match);
764         fr_cursor_init(&out_cursor, out);
765         for (i = 0; i < num; i++) {
766                 match = fr_cursor_next_by_da(&list_cursor, da[i], TAG_ANY);
767                 if (!match) {
768                         fr_cursor_init(&list_cursor, &last_match);
769                         continue;
770                 }
771
772                 do {
773                         copy = paircopyvp(ctx, match);
774                         if (!copy) {
775                                 pairfree(out);
776                                 return -1;
777                         }
778                         fr_cursor_insert(&out_cursor, copy);
779                         last_match = match;
780
781                         count++;
782                 } while ((match = fr_cursor_next_by_da(&list_cursor, da[i], TAG_ANY)));
783         }
784
785         return count;
786 }
787
788 static int _request_free(rs_request_t *request)
789 {
790         /*
791          *      If were attempting to cleanup the request, and it's no longer in the request_tree
792          *      something has gone very badly wrong.
793          */
794         if (request->in_request_tree) {
795                 assert(rbtree_deletebydata(request_tree, request));
796         }
797
798         if (request->in_link_tree) {
799                 assert(rbtree_deletebydata(link_tree, request));
800         }
801
802         if (request->event) {
803                 assert(fr_event_delete(events, &request->event));
804         }
805
806         rad_free(&request->packet);
807         rad_free(&request->expect);
808         rad_free(&request->linked);
809
810         return 0;
811 }
812
813 static void rs_packet_cleanup(rs_request_t *request)
814 {
815
816         RADIUS_PACKET *packet = request->packet;
817         uint64_t count = request->id;
818
819         assert(request->stats_req);
820         assert(!request->rt_rsp || request->stats_rsp);
821         assert(packet);
822
823         /*
824          *      Don't pollute stats or print spurious messages as radsniff closes.
825          */
826         if (cleanup) {
827                 talloc_free(request);
828                 return;
829         }
830
831         if (RIDEBUG_ENABLED()) {
832                 rs_time_print(timestr, sizeof(timestr), &request->when);
833         }
834
835         /*
836          *      Were at packet cleanup time which is when the packet was received + timeout
837          *      and it's not been linked with a forwarded packet or a response.
838          *
839          *      We now count it as lost.
840          */
841         if (!request->silent_cleanup) {
842                 if (!request->linked) {
843                         request->stats_req->interval.lost_total++;
844
845                         if (conf->event_flags & RS_LOST) {
846                                 /* @fixme We should use flags in the request to indicate whether it's been dumped
847                                  * to a PCAP file or logged yet, this simplifies the body logging logic */
848                                 conf->logger(request->id, RS_LOST, request->in, packet, NULL, NULL, false,
849                                              conf->filter_response_vps || !(conf->event_flags & RS_NORMAL));
850                         }
851                 }
852
853                 if (request->in->type == PCAP_INTERFACE_IN) {
854                         RDEBUG("Cleaning up request packet ID %i", request->expect->id);
855                 }
856         }
857
858         /*
859          *      Now the request is done, we can update the retransmission stats
860          */
861         if (request->rt_req > RS_RETRANSMIT_MAX) {
862                 request->stats_req->interval.rt_total[RS_RETRANSMIT_MAX]++;
863         } else {
864                 request->stats_req->interval.rt_total[request->rt_req]++;
865         }
866
867         if (request->rt_rsp) {
868                 if (request->rt_rsp > RS_RETRANSMIT_MAX) {
869                         request->stats_rsp->interval.rt_total[RS_RETRANSMIT_MAX]++;
870                 } else {
871                         request->stats_rsp->interval.rt_total[request->rt_rsp]++;
872                 }
873         }
874
875         talloc_free(request);
876 }
877
878 static void _rs_event(void *ctx)
879 {
880         rs_request_t *request = talloc_get_type_abort(ctx, rs_request_t);
881         request->event = NULL;
882         rs_packet_cleanup(request);
883 }
884
885 /** Wrapper around fr_packet_cmp to strip off the outer request struct
886  *
887  */
888 static int rs_packet_cmp(rs_request_t const *a, rs_request_t const *b)
889 {
890         return fr_packet_cmp(a->expect, b->expect);
891 }
892
893 /* This is the same as immediately scheduling the cleanup event */
894 #define RS_CLEANUP_NOW(_x, _s)\
895         {\
896                 _x->silent_cleanup = _s;\
897                 _x->when = header->ts;\
898                 rs_packet_cleanup(_x);\
899                 _x = NULL;\
900         } while (0)
901
902 static void rs_packet_process(uint64_t count, rs_event_t *event, struct pcap_pkthdr const *header, uint8_t const *data)
903 {
904         rs_stats_t              *stats = event->stats;
905         struct timeval          elapsed = {0, 0};
906         struct timeval          latency;
907
908         /*
909          *      Pointers into the packet data we just received
910          */
911         ssize_t len;
912         uint8_t const           *p = data;
913
914         ip_header_t const       *ip = NULL;             /* The IP header */
915         ip_header6_t const      *ip6 = NULL;            /* The IPv6 header */
916         udp_header_t const      *udp;                   /* The UDP header */
917         uint8_t                 version;                /* IP header version */
918         bool                    response;               /* Was it a response code */
919
920         decode_fail_t           reason;                 /* Why we failed decoding the packet */
921         static uint64_t         captured = 0;
922
923         rs_status_t             status = RS_NORMAL;     /* Any special conditions (RTX, Unlinked, ID-Reused) */
924         RADIUS_PACKET           *current;               /* Current packet were processing */
925         rs_request_t            *original = NULL;
926
927         rs_request_t            search;
928
929         memset(&search, 0, sizeof(search));
930
931         if (!start_pcap.tv_sec) {
932                 start_pcap = header->ts;
933         }
934
935         if (RIDEBUG_ENABLED()) {
936                 rs_time_print(timestr, sizeof(timestr), &header->ts);
937         }
938
939         len = fr_pcap_link_layer_offset(data, header->caplen, event->in->link_type);
940         if (len < 0) {
941                 REDEBUG("Failed determining link layer header offset");
942                 return;
943         }
944         p += len;
945
946         version = (p[0] & 0xf0) >> 4;
947         switch (version) {
948         case 4:
949                 ip = (ip_header_t const *)p;
950                 len = (0x0f & ip->ip_vhl) * 4;  /* ip_hl specifies length in 32bit words */
951                 p += len;
952                 break;
953
954         case 6:
955                 ip6 = (ip_header6_t const *)p;
956                 p += sizeof(ip_header6_t);
957
958                 break;
959
960         default:
961                 REDEBUG("IP version invalid %i", version);
962                 return;
963         }
964
965         /*
966          *      End of variable length bits, do basic check now to see if packet looks long enough
967          */
968         len = (p - data) + sizeof(udp_header_t) + (sizeof(radius_packet_t) - 1);        /* length value */
969         if ((size_t) len > header->caplen) {
970                 REDEBUG("Packet too small, we require at least %zu bytes, captured %i bytes",
971                         (size_t) len, header->caplen);
972                 return;
973         }
974
975         /*
976          *      UDP header validation.
977          */
978         udp = (udp_header_t const *)p;
979         {
980                 uint16_t udp_len;
981                 ssize_t diff;
982
983                 udp_len = ntohs(udp->len);
984                 diff = udp_len - (header->caplen - (p - data));
985                 /* Truncated data */
986                 if (diff > 0) {
987                         REDEBUG("Packet too small by %zi bytes, UDP header + Payload should be %hu bytes",
988                                 diff, udp_len);
989                         return;
990                 }
991                 /* Trailing data */
992                 else if (diff < 0) {
993                         REDEBUG("Packet too big by %zi bytes, UDP header + Payload should be %hu bytes",
994                                 diff * -1, udp_len);
995                         return;
996                 }
997         }
998         if ((version == 4) && conf->verify_udp_checksum) {
999                 uint16_t expected;
1000
1001                 expected = fr_udp_checksum((uint8_t const *) udp, ntohs(udp->len), udp->checksum,
1002                                            ip->ip_src, ip->ip_dst);
1003                 if (udp->checksum != expected) {
1004                         REDEBUG("UDP checksum invalid, packet: 0x%04hx calculated: 0x%04hx",
1005                                 ntohs(udp->checksum), ntohs(expected));
1006                         /* Not a fatal error */
1007                 }
1008         }
1009         p += sizeof(udp_header_t);
1010
1011         /*
1012          *      With artificial talloc memory limits there's a good chance we can
1013          *      recover once some requests timeout, so make an effort to deal
1014          *      with allocation failures gracefully.
1015          */
1016         current = rad_alloc(conf, 0);
1017         if (!current) {
1018                 REDEBUG("Failed allocating memory to hold decoded packet");
1019                 rs_tv_add_ms(&header->ts, conf->stats.timeout, &stats->quiet);
1020                 return;
1021         }
1022
1023         current->timestamp = header->ts;
1024         current->data_len = header->caplen - (p - data);
1025         memcpy(&current->data, &p, sizeof(current->data));
1026
1027         /*
1028          *      Populate IP/UDP fields from PCAP data
1029          */
1030         if (ip) {
1031                 current->src_ipaddr.af = AF_INET;
1032                 current->src_ipaddr.ipaddr.ip4addr.s_addr = ip->ip_src.s_addr;
1033
1034                 current->dst_ipaddr.af = AF_INET;
1035                 current->dst_ipaddr.ipaddr.ip4addr.s_addr = ip->ip_dst.s_addr;
1036         } else {
1037                 current->src_ipaddr.af = AF_INET6;
1038                 memcpy(&current->src_ipaddr.ipaddr.ip6addr.s6_addr, &ip6->ip_src.s6_addr,
1039                        sizeof(current->src_ipaddr.ipaddr.ip6addr.s6_addr));
1040
1041                 current->dst_ipaddr.af = AF_INET6;
1042                 memcpy(&current->dst_ipaddr.ipaddr.ip6addr.s6_addr, &ip6->ip_dst.s6_addr,
1043                        sizeof(current->dst_ipaddr.ipaddr.ip6addr.s6_addr));
1044         }
1045
1046         current->src_port = ntohs(udp->src);
1047         current->dst_port = ntohs(udp->dst);
1048
1049         if (!rad_packet_ok(current, 0, &reason)) {
1050                 REDEBUG("%s", fr_strerror());
1051                 if (conf->event_flags & RS_ERROR) {
1052                         conf->logger(count, RS_ERROR, event->in, current, &elapsed, NULL, false, false);
1053                 }
1054                 rad_free(&current);
1055
1056                 return;
1057         }
1058
1059         switch (current->code) {
1060         case PW_CODE_ACCOUNTING_RESPONSE:
1061         case PW_CODE_AUTHENTICATION_REJECT:
1062         case PW_CODE_AUTHENTICATION_ACK:
1063         case PW_CODE_ACCESS_CHALLENGE:
1064         case PW_CODE_COA_NAK:
1065         case PW_CODE_COA_ACK:
1066         case PW_CODE_DISCONNECT_NAK:
1067         case PW_CODE_DISCONNECT_ACK:
1068         case PW_CODE_STATUS_CLIENT:
1069         {
1070                 /* look for a matching request and use it for decoding */
1071                 search.expect = current;
1072                 original = rbtree_finddata(request_tree, &search);
1073
1074                 /*
1075                  *      Verify this code is allowed
1076                  */
1077                 if (conf->filter_response_code && (conf->filter_response_code != current->code)) {
1078                         drop_response:
1079                         RDEBUG2("Response dropped by filter");
1080                         rad_free(&current);
1081
1082                         /* We now need to cleanup the original request too */
1083                         if (original) {
1084                                 RS_CLEANUP_NOW(original, true);
1085                         }
1086                         return;
1087                 }
1088
1089                 /*
1090                  *      Only decode attributes if we want to print them or filter on them
1091                  *      rad_packet_ok does checks to verify the packet is actually valid.
1092                  */
1093                 if (conf->decode_attrs) {
1094                         int ret;
1095                         FILE *log_fp = fr_log_fp;
1096
1097                         fr_log_fp = NULL;
1098                         ret = rad_decode(current, original ? original->expect : NULL, conf->radius_secret);
1099                         fr_log_fp = log_fp;
1100                         if (ret != 0) {
1101                                 rad_free(&current);
1102                                 REDEBUG("Failed decoding");
1103                                 return;
1104                         }
1105                 }
1106
1107                 /*
1108                  *      Check if we've managed to link it to a request
1109                  */
1110                 if (original) {
1111                         /*
1112                          *      Now verify the packet passes the attribute filter
1113                          */
1114                         if (conf->filter_response_vps) {
1115                                 pairsort(&current->vps, attrtagcmp);
1116                                 if (!pairvalidate_relaxed(NULL, conf->filter_response_vps, current->vps)) {
1117                                         goto drop_response;
1118                                 }
1119                         }
1120
1121                         /*
1122                          *      Is this a retransmission?
1123                          */
1124                         if (original->linked) {
1125                                 status = RS_RTX;
1126                                 original->rt_rsp++;
1127
1128                                 rad_free(&original->linked);
1129                                 fr_event_delete(event->list, &original->event);
1130                         /*
1131                          *      ...nope it's the first response to a request.
1132                          */
1133                         } else {
1134                                 original->stats_rsp = &stats->exchange[current->code];
1135                         }
1136
1137                         /*
1138                          *      Insert a callback to remove the request and response
1139                          *      from the tree after the timeout period.
1140                          *      The delay is so we can detect retransmissions.
1141                          */
1142                         original->linked = talloc_steal(original, current);
1143                         rs_tv_add_ms(&header->ts, conf->stats.timeout, &original->when);
1144                         if (!fr_event_insert(event->list, _rs_event, original, &original->when,
1145                                              &original->event)) {
1146                                 REDEBUG("Failed inserting new event");
1147                                 /*
1148                                  *      Delete the original request/event, it's no longer valid
1149                                  *      for statistics.
1150                                  */
1151                                 talloc_free(original);
1152                                 return;
1153                         }
1154                 /*
1155                  *      No request seen, or request was dropped by attribute filter
1156                  */
1157                 } else {
1158                         /*
1159                          *      If conf->filter_request_vps are set assume the original request was dropped,
1160                          *      the alternative is maintaining another 'filter', but that adds
1161                          *      complexity, reduces max capture rate, and is generally a PITA.
1162                          */
1163                         if (conf->filter_request) {
1164                                 rad_free(&current);
1165                                 RDEBUG2("Original request dropped by filter");
1166                                 return;
1167                         }
1168
1169                         status = RS_UNLINKED;
1170                         stats->exchange[current->code].interval.unlinked_total++;
1171                 }
1172
1173                 response = true;
1174                 break;
1175         }
1176
1177         case PW_CODE_ACCOUNTING_REQUEST:
1178         case PW_CODE_AUTHENTICATION_REQUEST:
1179         case PW_CODE_COA_REQUEST:
1180         case PW_CODE_DISCONNECT_REQUEST:
1181         case PW_CODE_STATUS_SERVER:
1182         {
1183                 /*
1184                  *      Verify this code is allowed
1185                  */
1186                 if (conf->filter_request_code && (conf->filter_request_code != current->code)) {
1187                         drop_request:
1188
1189                         RDEBUG2("Request dropped by filter");
1190                         rad_free(&current);
1191
1192                         return;
1193                 }
1194
1195                 /*
1196                  *      Only decode attributes if we want to print them or filter on them
1197                  *      rad_packet_ok does checks to verify the packet is actually valid.
1198                  */
1199                 if (conf->decode_attrs) {
1200                         int ret;
1201                         FILE *log_fp = fr_log_fp;
1202
1203                         fr_log_fp = NULL;
1204                         ret = rad_decode(current, NULL, conf->radius_secret);
1205                         fr_log_fp = log_fp;
1206
1207                         if (ret != 0) {
1208                                 rad_free(&current);
1209                                 REDEBUG("Failed decoding");
1210                                 return;
1211                         }
1212
1213                         pairsort(&current->vps, attrtagcmp);
1214                 }
1215
1216                 /*
1217                  *      Save the request for later matching
1218                  */
1219                 search.expect = rad_alloc_reply(current, current);
1220                 if (!search.expect) {
1221                         REDEBUG("Failed allocating memory to hold expected reply");
1222                         rs_tv_add_ms(&header->ts, conf->stats.timeout, &stats->quiet);
1223                         rad_free(&current);
1224                         return;
1225                 }
1226                 search.expect->code = current->code;
1227
1228                 if ((conf->link_da_num > 0) && current->vps) {
1229                         int ret;
1230                         ret = rs_get_pairs(current, &search.link_vps, current->vps, conf->link_da,
1231                                            conf->link_da_num);
1232                         if (ret < 0) {
1233                                 ERROR("Failed extracting RTX linking pairs from request");
1234                                 rad_free(&current);
1235                                 return;
1236                         }
1237                 }
1238
1239                 /*
1240                  *      If we have linking attributes set, attempt to find a request in the linking tree.
1241                  */
1242                 if (search.link_vps) {
1243                         rs_request_t *tuple;
1244
1245                         original = rbtree_finddata(link_tree, &search);
1246                         tuple = rbtree_finddata(request_tree, &search);
1247
1248                         /*
1249                          *      If the packet we matched using attributes is not the same
1250                          *      as the packet in the request tree, then we need to clean up
1251                          *      the packet in the request tree.
1252                          */
1253                         if (tuple && (original != tuple)) {
1254                                 RS_CLEANUP_NOW(tuple, true);
1255                         }
1256                 /*
1257                  *      Detect duplicates using the normal 5-tuple of src/dst ips/ports id
1258                  */
1259                 } else {
1260                         original = rbtree_finddata(request_tree, &search);
1261                         if (original && memcmp(original->expect->vector, current->vector,
1262                             sizeof(original->expect->vector)) != 0) {
1263                                 /*
1264                                  *      ID reused before the request timed out (which may be an issue)...
1265                                  */
1266                                 if (!original->linked) {
1267                                         status = RS_REUSED;
1268                                         stats->exchange[current->code].interval.reused_total++;
1269                                         /* Occurs regularly downstream of proxy servers (so don't complain) */
1270                                         RS_CLEANUP_NOW(original, true);
1271                                 /*
1272                                  *      ...and before we saw a response (which may be a bigger issue).
1273                                  */
1274                                 } else {
1275                                         RS_CLEANUP_NOW(original, false);
1276                                 }
1277                                 /* else it's a proper RTX with the same src/dst id authenticator/nonce */
1278                         }
1279                 }
1280
1281                 /*
1282                  *      Now verify the packet passes the attribute filter
1283                  */
1284                 if (conf->filter_request_vps) {
1285                         if (!pairvalidate_relaxed(NULL, conf->filter_request_vps, current->vps)) {
1286                                 goto drop_request;
1287                         }
1288                 }
1289
1290                 /*
1291                  *      Is this a retransmission?
1292                  */
1293                 if (original) {
1294                         status = RS_RTX;
1295                         original->rt_req++;
1296
1297                         rad_free(&original->packet);
1298
1299                         /* We may of seen the response, but it may of been lost upstream */
1300                         rad_free(&original->linked);
1301
1302                         original->packet = talloc_steal(original, current);
1303
1304                         /* Request may need to be reinserted as the 5 tuple of the response may of changed */
1305                         if (rs_packet_cmp(original, &search) != 0) {
1306                                 rbtree_deletebydata(request_tree, original);
1307                         }
1308
1309                         rad_free(&original->expect);
1310                         original->expect = talloc_steal(original, search.expect);
1311
1312                         /* Disarm the timer for the cleanup event for the original request */
1313                         fr_event_delete(event->list, &original->event);
1314                 /*
1315                  *      ...nope it's a new request.
1316                  */
1317                 } else {
1318                         original = talloc_zero(conf, rs_request_t);
1319                         talloc_set_destructor(original, _request_free);
1320
1321                         original->id = count;
1322                         original->in = event->in;
1323                         original->stats_req = &stats->exchange[current->code];
1324
1325                         original->packet = talloc_steal(original, current);
1326                         original->expect = talloc_steal(original, search.expect);
1327
1328                         if (search.link_vps) {
1329                                 original->link_vps = pairsteal(original, search.link_vps);
1330
1331                                 /* We should never have conflicts */
1332                                 assert(rbtree_insert(link_tree, original));
1333                                 original->in_link_tree = true;
1334                         }
1335
1336                         /*
1337                          *      Special case for when were filtering by response,
1338                          *      we never count any requests as lost, because we
1339                          *      don't know what the response to that request would
1340                          *      of been.
1341                          */
1342                         if (conf->filter_response_vps) {
1343                                 original->silent_cleanup = true;
1344                         }
1345                 }
1346
1347                 if (!original->in_request_tree) {
1348                         /* We should never have conflicts */
1349                         assert(rbtree_insert(request_tree, original));
1350                         original->in_request_tree = true;
1351                 }
1352
1353                 /*
1354                  *      Insert a callback to remove the request from the tree
1355                  */
1356                 original->packet->timestamp = header->ts;
1357                 rs_tv_add_ms(&header->ts, conf->stats.timeout, &original->when);
1358                 if (!fr_event_insert(event->list, _rs_event, original,
1359                                      &original->when, &original->event)) {
1360                         REDEBUG("Failed inserting new event");
1361
1362                         talloc_free(original);
1363                         return;
1364                 }
1365                 response = false;
1366                 break;
1367         }
1368
1369         default:
1370                 REDEBUG("Unsupported code %i", current->code);
1371                 rad_free(&current);
1372
1373                 return;
1374         }
1375
1376         if (event->out) {
1377                 pcap_dump((void *) (event->out->dumper), header, data);
1378         }
1379
1380         rs_tv_sub(&header->ts, &start_pcap, &elapsed);
1381
1382         /*
1383          *      Increase received count
1384          */
1385         stats->exchange[current->code].interval.received_total++;
1386
1387         /*
1388          *      It's a linked response
1389          */
1390         if (original && original->linked) {
1391                 rs_tv_sub(&current->timestamp, &original->packet->timestamp, &latency);
1392
1393                 /*
1394                  *      Update stats for both the request and response types.
1395                  *
1396                  *      This isn't useful for things like Access-Requests, but will be useful for
1397                  *      CoA and Disconnect Messages, as we get the average latency across both
1398                  *      response types.
1399                  *
1400                  *      It also justifies allocating PW_CODE_MAX instances of rs_latency_t.
1401                  */
1402                 rs_stats_update_latency(&stats->exchange[current->code], &latency);
1403                 rs_stats_update_latency(&stats->exchange[original->expect->code], &latency);
1404
1405                 /*
1406                  *      Were filtering on response, now print out the full data from the request
1407                  */
1408                 if (conf->filter_response && RIDEBUG_ENABLED() && (conf->event_flags & RS_NORMAL)) {
1409                         rs_time_print(timestr, sizeof(timestr), &original->packet->timestamp);
1410                         rs_tv_sub(&original->packet->timestamp, &start_pcap, &elapsed);
1411                         conf->logger(original->id, RS_NORMAL, original->in, original->packet, &elapsed, NULL, false, true);
1412                         rs_tv_sub(&header->ts, &start_pcap, &elapsed);
1413                         rs_time_print(timestr, sizeof(timestr), &header->ts);
1414                 }
1415
1416                 if (conf->event_flags & status) {
1417                         conf->logger(count, status, event->in, current, &elapsed, &latency, response, true);
1418                 }
1419         /*
1420          *      It's the original request
1421          *
1422          *      If were filtering on responses we can only indicate we received it on response, or timeout.
1423          */
1424         } else if (!conf->filter_response && (conf->event_flags & status)) {
1425                 conf->logger(original ? original->id : count, status, event->in,
1426                              current, &elapsed, NULL, response, true);
1427         }
1428
1429         fflush(fr_log_fp);
1430
1431         /*
1432          *      If it's a unlinked response, we need to free it explicitly, as it will
1433          *      not be done by the event queue.
1434          */
1435         if (response && !original) {
1436                 rad_free(&current);
1437         }
1438
1439         captured++;
1440         /*
1441          *      We've hit our capture limit, break out of the event loop
1442          */
1443         if ((conf->limit > 0) && (captured >= conf->limit)) {
1444                 INFO("Captured %" PRIu64 " packets, exiting...", captured);
1445                 fr_event_loop_exit(events, 1);
1446         }
1447 }
1448
1449 static void rs_got_packet(UNUSED fr_event_list_t *el, int fd, void *ctx)
1450 {
1451         static uint64_t count = 0;      /* Packets seen */
1452         rs_event_t      *event = ctx;
1453         pcap_t          *handle = event->in->handle;
1454
1455         int i;
1456         int ret;
1457         const uint8_t *data;
1458         struct pcap_pkthdr *header;
1459
1460         /*
1461          *      Consume entire capture, interleaving not currently possible
1462          */
1463         if ((event->in->type == PCAP_FILE_IN) || (event->in->type == PCAP_STDIO_IN)) {
1464                 while (!fr_event_loop_exiting(el)) {
1465                         struct timeval now;
1466
1467                         ret = pcap_next_ex(handle, &header, &data);
1468                         if (ret == 0) {
1469                                 /* No more packets available at this time */
1470                                 return;
1471                         }
1472                         if (ret == -2) {
1473                                 DEBUG("Done reading packets (%s)", event->in->name);
1474                                 fr_event_fd_delete(events, 0, fd);
1475
1476                                 if (fr_event_list_num_fds(events) == 0) {
1477                                         fr_event_loop_exit(events, 1);
1478                                 }
1479
1480                                 return;
1481                         }
1482                         if (ret < 0) {
1483                                 ERROR("Error requesting next packet, got (%i): %s", ret, pcap_geterr(handle));
1484                                 return;
1485                         }
1486
1487                         do {
1488                                 now = header->ts;
1489                         } while (fr_event_run(el, &now) == 1);
1490                         count++;
1491
1492                         rs_packet_process(count, event, header, data);
1493                 }
1494                 return;
1495         }
1496
1497         /*
1498          *      Consume multiple packets from the capture buffer.
1499          *      We occasionally need to yield to allow events to run.
1500          */
1501         for (i = 0; i < RS_FORCE_YIELD; i++) {
1502                 ret = pcap_next_ex(handle, &header, &data);
1503                 if (ret == 0) {
1504                         /* No more packets available at this time */
1505                         return;
1506                 }
1507                 if (ret < 0) {
1508                         ERROR("Error requesting next packet, got (%i): %s", ret, pcap_geterr(handle));
1509                         return;
1510                 }
1511
1512                 count++;
1513                 rs_packet_process(count, event, header, data);
1514         }
1515 }
1516
1517 static void _rs_event_status(struct timeval *wake)
1518 {
1519         if (wake && ((wake->tv_sec != 0) || (wake->tv_usec >= 100000))) {
1520                 DEBUG2("Waking up in %d.%01u seconds.", (int) wake->tv_sec, (unsigned int) wake->tv_usec / 100000);
1521
1522                 if (RIDEBUG_ENABLED()) {
1523                         rs_time_print(timestr, sizeof(timestr), wake);
1524                 }
1525         }
1526 }
1527
1528 /** Compare requests using packet info and lists of attributes
1529  *
1530  */
1531 static int rs_rtx_cmp(rs_request_t const *a, rs_request_t const *b)
1532 {
1533         int rcode;
1534
1535         assert(a->link_vps);
1536         assert(b->link_vps);
1537
1538         rcode = (int) a->expect->code - (int) b->expect->code;
1539         if (rcode != 0) return rcode;
1540
1541         rcode = a->expect->sockfd - b->expect->sockfd;
1542         if (rcode != 0) return rcode;
1543
1544         rcode = fr_ipaddr_cmp(&a->expect->src_ipaddr, &b->expect->src_ipaddr);
1545         if (rcode != 0) return rcode;
1546
1547         rcode = fr_ipaddr_cmp(&a->expect->dst_ipaddr, &b->expect->dst_ipaddr);
1548         if (rcode != 0) return rcode;
1549
1550         return pairlistcmp(a->link_vps, b->link_vps);
1551 }
1552
1553 static int rs_build_dict_list(DICT_ATTR const **out, size_t len, char *list)
1554 {
1555         size_t i = 0;
1556         char *p, *tok;
1557
1558         p = list;
1559         while ((tok = strsep(&p, "\t ,")) != NULL) {
1560                 DICT_ATTR const *da;
1561                 if ((*tok == '\t') || (*tok == ' ') || (*tok == '\0')) {
1562                         continue;
1563                 }
1564
1565                 if (i == len) {
1566                         ERROR("Too many attributes, maximum allowed is %zu", len);
1567                         return -1;
1568                 }
1569
1570                 da = dict_attrbyname(tok);
1571                 if (!da) {
1572                         ERROR("Error parsing attribute name \"%s\"", tok);
1573                         return -1;
1574                 }
1575
1576                 out[i] = da;
1577                 i++;
1578         }
1579
1580         return i;
1581 }
1582
1583 static int rs_build_filter(VALUE_PAIR **out, char const *filter)
1584 {
1585         vp_cursor_t cursor;
1586         VALUE_PAIR *vp;
1587         FR_TOKEN code;
1588
1589         code = userparse(conf, filter, out);
1590         if (code == T_OP_INVALID) {
1591                 ERROR("Invalid RADIUS filter \"%s\" (%s)", filter, fr_strerror());
1592                 return -1;
1593         }
1594
1595         if (!*out) {
1596                 ERROR("Empty RADIUS filter '%s'", filter);
1597                 return -1;
1598         }
1599
1600         for (vp = fr_cursor_init(&cursor, out);
1601              vp;
1602              vp = fr_cursor_next(&cursor)) {
1603                 /*
1604                  *      xlat expansion isn't support here
1605                  */
1606                 if (vp->type == VT_XLAT) {
1607                         vp->type = VT_DATA;
1608                         vp->vp_strvalue = vp->value.xlat;
1609                 }
1610         }
1611
1612         /*
1613          *      This allows efficient list comparisons later
1614          */
1615         pairsort(out, attrtagcmp);
1616
1617         return 0;
1618 }
1619
1620 static int rs_build_flags(int *flags, FR_NAME_NUMBER const *map, char *list)
1621 {
1622         size_t i = 0;
1623         char *p, *tok;
1624
1625         p = list;
1626         while ((tok = strsep(&p, "\t ,")) != NULL) {
1627                 int flag;
1628
1629                 if ((*tok == '\t') || (*tok == ' ') || (*tok == '\0')) {
1630                         continue;
1631                 }
1632
1633                 *flags |= flag = fr_str2int(map, tok, -1);
1634                 if (flag < 0) {
1635                         ERROR("Invalid flag \"%s\"", tok);
1636                         return -1;
1637                 }
1638
1639                 i++;
1640         }
1641
1642         return i;
1643 }
1644
1645 /** Callback for when the request is removed from the request tree
1646  *
1647  * @param request being removed.
1648  */
1649 static void _unmark_request(void *request)
1650 {
1651         rs_request_t *this = request;
1652         this->in_request_tree = false;
1653 }
1654
1655 /** Callback for when the request is removed from the link tree
1656  *
1657  * @param request being removed.
1658  */
1659 static void _unmark_link(void *request)
1660 {
1661         rs_request_t *this = request;
1662         this->in_link_tree = false;
1663 }
1664
1665 /** Write the last signal to the signal pipe
1666  *
1667  * @param sig raised
1668  */
1669 static void rs_signal_self(int sig)
1670 {
1671         if (write(self_pipe[1], &sig, sizeof(sig)) < 0) {
1672                 ERROR("Failed writing signal %s to pipe: %s", strsignal(sig), fr_syserror(errno));
1673                 exit(EXIT_FAILURE);
1674         }
1675 }
1676
1677 #ifdef HAVE_COLLECTDC_H
1678 /** Re-open the collectd socket
1679  *
1680  */
1681 static void rs_collectd_reopen(void *ctx)
1682 {
1683         fr_event_list_t *list = ctx;
1684         static fr_event_t *event;
1685         struct timeval now, when;
1686
1687         if (rs_stats_collectd_open(conf) == 0) {
1688                 DEBUG2("Stats output socket (re)opened");
1689                 return;
1690         }
1691
1692         ERROR("Will attempt to re-establish connection in %i ms", RS_SOCKET_REOPEN_DELAY);
1693
1694         gettimeofday(&now, NULL);
1695         rs_tv_add_ms(&now, RS_SOCKET_REOPEN_DELAY, &when);
1696         if (!fr_event_insert(list, rs_collectd_reopen, list, &when, &event)) {
1697                 ERROR("Failed inserting re-open event");
1698                 assert(0);
1699         }
1700 }
1701 #endif
1702
1703 /** Read the last signal from the signal pipe
1704  *
1705  */
1706 static void rs_signal_action(UNUSED fr_event_list_t *list, int fd, UNUSED void *ctx)
1707 {
1708         int sig;
1709         ssize_t ret;
1710
1711         ret = read(fd, &sig, sizeof(sig));
1712         if (ret < 0) {
1713                 ERROR("Failed reading signal from pipe: %s", fr_syserror(errno));
1714                 exit(EXIT_FAILURE);
1715         }
1716
1717         if (ret != sizeof(sig)) {
1718                 ERROR("Failed reading signal from pipe: "
1719                       "Expected signal to be %zu bytes but only read %zu byes", sizeof(sig), ret);
1720                 exit(EXIT_FAILURE);
1721         }
1722
1723         switch (sig) {
1724 #ifdef HAVE_COLLECTDC_H
1725         case SIGPIPE:
1726                 rs_collectd_reopen(list);
1727                 break;
1728 #endif
1729
1730         case SIGINT:
1731         case SIGTERM:
1732         case SIGQUIT:
1733                 DEBUG2("Signalling event loop to exit");
1734                 fr_event_loop_exit(events, 1);
1735                 break;
1736
1737         default:
1738                 ERROR("Unhandled signal %s", strsignal(sig));
1739                 exit(EXIT_FAILURE);
1740         }
1741 }
1742
1743
1744 static void NEVER_RETURNS usage(int status)
1745 {
1746         FILE *output = status ? stderr : stdout;
1747         fprintf(output, "Usage: radsniff [options][stats options] -- [pcap files]\n");
1748         fprintf(output, "options:\n");
1749         fprintf(output, "  -a                    List all interfaces available for capture.\n");
1750         fprintf(output, "  -c <count>            Number of packets to capture.\n");
1751         fprintf(output, "  -C                    Enable UDP checksum validation.\n");
1752         fprintf(output, "  -d <directory>        Set dictionary directory.\n");
1753         fprintf(stderr, "  -d <raddb>            Set configuration directory (defaults to " RADDBDIR ").\n");
1754         fprintf(stderr, "  -D <dictdir>          Set main dictionary directory (defaults to " DICTDIR ").\n");
1755         fprintf(output, "  -e <event>[,<event>]  Only log requests with these event flags.\n");
1756         fprintf(output, "                        Event may be one of the following:\n");
1757         fprintf(output, "                        - received - a request or response.\n");
1758         fprintf(output, "                        - norsp    - seen for a request.\n");
1759         fprintf(output, "                        - rtx      - of a request that we've seen before.\n");
1760         fprintf(output, "                        - noreq    - could be matched with the response.\n");
1761         fprintf(output, "                        - reused   - ID too soon.\n");
1762         fprintf(output, "                        - error    - decoding the packet.\n");
1763         fprintf(output, "  -f <filter>           PCAP filter (default is 'udp port <port> or <port + 1> or 3799')\n");
1764         fprintf(output, "  -h                    This help message.\n");
1765         fprintf(output, "  -i <interface>        Capture packets from interface (defaults to all if supported).\n");
1766         fprintf(output, "  -I <file>             Read packets from file (overrides input of -F).\n");
1767         fprintf(output, "  -l <attr>[,<attr>]    Output packet sig and a list of attributes.\n");
1768         fprintf(output, "  -L <attr>[,<attr>]    Detect retransmissions using these attributes to link requests.\n");
1769         fprintf(output, "  -m                    Don't put interface(s) into promiscuous mode.\n");
1770         fprintf(output, "  -p <port>             Filter packets by port (default is 1812).\n");
1771         fprintf(output, "  -P <pidfile>          Daemonize and write out <pidfile>.\n");
1772         fprintf(output, "  -q                    Print less debugging information.\n");
1773         fprintf(output, "  -r <filter>           RADIUS attribute request filter.\n");
1774         fprintf(output, "  -R <filter>           RADIUS attribute response filter.\n");
1775         fprintf(output, "  -s <secret>           RADIUS secret.\n");
1776         fprintf(output, "  -S                    Write PCAP data to stdout.\n");
1777         fprintf(output, "  -v                    Show program version information.\n");
1778         fprintf(output, "  -w <file>             Write output packets to file.\n");
1779         fprintf(output, "  -x                    Print more debugging information (defaults to -xx).\n");
1780         fprintf(output, "stats options:\n");
1781         fprintf(output, "  -W <interval>         Periodically write out statistics every <interval> seconds.\n");
1782         fprintf(output, "  -T <timeout>          How many milliseconds before the request is counted as lost "
1783                 "(defaults to %i).\n", RS_DEFAULT_TIMEOUT);
1784 #ifdef HAVE_COLLECTDC_H
1785         fprintf(output, "  -N <prefix>           The instance name passed to the collectd plugin.\n");
1786         fprintf(output, "  -O <server>           Write statistics to this collectd server.\n");
1787 #endif
1788         exit(status);
1789 }
1790
1791 int main(int argc, char *argv[])
1792 {
1793         fr_pcap_t *in = NULL, *in_p;
1794         fr_pcap_t **in_head = &in;
1795         fr_pcap_t *out = NULL;
1796
1797         int ret = 1;                                    /* Exit status */
1798
1799         char errbuf[PCAP_ERRBUF_SIZE];                  /* Error buffer */
1800         int port = 1812;
1801
1802         char buffer[1024];
1803
1804         int opt;
1805         char const *radius_dir = RADDBDIR;
1806         char const *dict_dir = DICTDIR;
1807
1808         rs_stats_t stats;
1809
1810         fr_debug_flag = 1;
1811         fr_log_fp = stdout;
1812
1813         /*
1814          *      Useful if using radsniff as a long running stats daemon
1815          */
1816 #ifndef NDEBUG
1817         if (fr_fault_setup(getenv("PANIC_ACTION"), argv[0]) < 0) {
1818                 fr_perror("radsniff");
1819                 exit(EXIT_FAILURE);
1820         }
1821 #endif
1822
1823         talloc_set_log_stderr();
1824
1825         conf = talloc_zero(NULL, rs_t);
1826         if (!fr_assert(conf)) {
1827                 exit (1);
1828         }
1829
1830         /*
1831          *  We don't really want probes taking down machines
1832          */
1833 #ifdef HAVE_TALLOC_SET_MEMLIMIT
1834         /*
1835          *      @fixme causes hang in talloc steal
1836          */
1837          //talloc_set_memlimit(conf, 524288000);                /* 50 MB */
1838 #endif
1839
1840         /*
1841          *      Set some defaults
1842          */
1843         conf->print_packet = true;
1844         conf->limit = 0;
1845         conf->promiscuous = true;
1846 #ifdef HAVE_COLLECTDC_H
1847         conf->stats.prefix = RS_DEFAULT_PREFIX;
1848 #endif
1849         conf->radius_secret = RS_DEFAULT_SECRET;
1850         conf->logger = rs_packet_print_null;
1851
1852 #ifdef HAVE_COLLECTDC_H
1853         conf->stats.prefix = RS_DEFAULT_PREFIX;
1854 #endif
1855
1856         /*
1857          *  Get options
1858          */
1859         while ((opt = getopt(argc, argv, "ab:c:Cd:D:e:Ff:hi:I:l:L:mp:P:qr:R:s:Svw:xXW:T:P:N:O:")) != EOF) {
1860                 switch (opt) {
1861                 case 'a':
1862                         {
1863                                 pcap_if_t *all_devices = NULL;
1864                                 pcap_if_t *dev_p;
1865
1866                                 if (pcap_findalldevs(&all_devices, errbuf) < 0) {
1867                                         ERROR("Error getting available capture devices: %s", errbuf);
1868                                         goto finish;
1869                                 }
1870
1871                                 int i = 1;
1872                                 for (dev_p = all_devices;
1873                                      dev_p;
1874                                      dev_p = dev_p->next) {
1875                                         INFO("%i.%s", i++, dev_p->name);
1876                                 }
1877                                 ret = 0;
1878                                 goto finish;
1879                         }
1880
1881                 /* super secret option */
1882                 case 'b':
1883                         conf->buffer_pkts = atoi(optarg);
1884                         if (conf->buffer_pkts == 0) {
1885                                 ERROR("Invalid buffer length \"%s\"", optarg);
1886                                 usage(1);
1887                         }
1888                         break;
1889
1890                 case 'c':
1891                         conf->limit = atoi(optarg);
1892                         if (conf->limit == 0) {
1893                                 ERROR("Invalid number of packets \"%s\"", optarg);
1894                                 usage(1);
1895                         }
1896                         break;
1897
1898                 /* udp checksum */
1899                 case 'C':
1900                         conf->verify_udp_checksum = true;
1901                         break;
1902
1903                 case 'd':
1904                         radius_dir = optarg;
1905                         break;
1906
1907                 case 'D':
1908                         dict_dir = optarg;
1909                         break;
1910
1911                 case 'e':
1912                         if (rs_build_flags((int *) &conf->event_flags, rs_events, optarg) < 0) {
1913                                 usage(64);
1914                         }
1915                         break;
1916
1917                 case 'f':
1918                         conf->pcap_filter = optarg;
1919                         break;
1920
1921                 case 'h':
1922                         usage(0);
1923                         break;
1924
1925                 case 'i':
1926                         *in_head = fr_pcap_init(conf, optarg, PCAP_INTERFACE_IN);
1927                         if (!*in_head) {
1928                                 goto finish;
1929                         }
1930                         in_head = &(*in_head)->next;
1931                         conf->from_dev = true;
1932                         break;
1933
1934                 case 'I':
1935                         *in_head = fr_pcap_init(conf, optarg, PCAP_FILE_IN);
1936                         if (!*in_head) {
1937                                 goto finish;
1938                         }
1939                         in_head = &(*in_head)->next;
1940                         conf->from_file = true;
1941                         break;
1942
1943                 case 'l':
1944                         conf->list_attributes = optarg;
1945                         break;
1946
1947                 case 'L':
1948                         conf->link_attributes = optarg;
1949                         break;
1950
1951                 case 'm':
1952                         conf->promiscuous = false;
1953                         break;
1954
1955                 case 'p':
1956                         port = atoi(optarg);
1957                         break;
1958
1959                 case 'P':
1960                         conf->daemonize = true;
1961                         conf->pidfile = optarg;
1962                         break;
1963
1964                 case 'q':
1965                         if (fr_debug_flag > 0) {
1966                                 fr_debug_flag--;
1967                         }
1968                         break;
1969
1970                 case 'r':
1971                         conf->filter_request = optarg;
1972                         break;
1973
1974                 case 'R':
1975                         conf->filter_response = optarg;
1976                         break;
1977
1978                 case 's':
1979                         conf->radius_secret = optarg;
1980                         break;
1981
1982                 case 'S':
1983                         conf->to_stdout = true;
1984                         break;
1985
1986                 case 'v':
1987 #ifdef HAVE_COLLECTDC_H
1988                         INFO("%s, %s, collectdclient version %s", radsniff_version, pcap_lib_version(),
1989                              lcc_version_string());
1990 #else
1991                         INFO("%s %s", radsniff_version, pcap_lib_version());
1992 #endif
1993                         exit(EXIT_SUCCESS);
1994                         break;
1995
1996                 case 'w':
1997                         out = fr_pcap_init(conf, optarg, PCAP_FILE_OUT);
1998                         conf->to_file = true;
1999                         break;
2000
2001                 case 'x':
2002                 case 'X':
2003                         fr_debug_flag++;
2004                         break;
2005
2006                 case 'W':
2007                         conf->stats.interval = atoi(optarg);
2008                         conf->print_packet = false;
2009                         if (conf->stats.interval <= 0) {
2010                                 ERROR("Stats interval must be > 0");
2011                                 usage(64);
2012                         }
2013                         break;
2014
2015                 case 'T':
2016                         conf->stats.timeout = atoi(optarg);
2017                         if (conf->stats.timeout <= 0) {
2018                                 ERROR("Timeout value must be > 0");
2019                                 usage(64);
2020                         }
2021                         break;
2022
2023 #ifdef HAVE_COLLECTDC_H
2024                 case 'N':
2025                         conf->stats.prefix = optarg;
2026                         break;
2027
2028                 case 'O':
2029                         conf->stats.collectd = optarg;
2030                         conf->stats.out = RS_STATS_OUT_COLLECTD;
2031                         break;
2032 #endif
2033                 default:
2034                         usage(64);
2035                 }
2036         }
2037
2038         /*
2039          *      Mismatch between the binary and the libraries it depends on
2040          */
2041         if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
2042                 fr_perror("radsniff");
2043                 exit(EXIT_FAILURE);
2044         }
2045
2046         /* Useful for file globbing */
2047         while (optind < argc) {
2048                 *in_head = fr_pcap_init(conf, argv[optind], PCAP_FILE_IN);
2049                 if (!*in_head) {
2050                         goto finish;
2051                 }
2052                 in_head = &(*in_head)->next;
2053                 conf->from_file = true;
2054                 optind++;
2055         }
2056
2057         /* Is stdin not a tty? If so it's probably a pipe */
2058         if (!isatty(fileno(stdin))) {
2059                 conf->from_stdin = true;
2060         }
2061
2062         /* What's the point in specifying -F ?! */
2063         if (conf->from_stdin && conf->from_file && conf->to_file) {
2064                 usage(64);
2065         }
2066
2067         /* Can't read from both... */
2068         if (conf->from_file && conf->from_dev) {
2069                 usage(64);
2070         }
2071
2072         /* Reading from file overrides stdin */
2073         if (conf->from_stdin && (conf->from_file || conf->from_dev)) {
2074                 conf->from_stdin = false;
2075         }
2076
2077         /* Writing to file overrides stdout */
2078         if (conf->to_file && conf->to_stdout) {
2079                 conf->to_stdout = false;
2080         }
2081
2082         if (conf->to_stdout) {
2083                 out = fr_pcap_init(conf, "stdout", PCAP_STDIO_OUT);
2084                 if (!out) {
2085                         goto finish;
2086                 }
2087         }
2088
2089         if (conf->from_stdin) {
2090                 *in_head = fr_pcap_init(conf, "stdin", PCAP_STDIO_IN);
2091                 if (!*in_head) {
2092                         goto finish;
2093                 }
2094                 in_head = &(*in_head)->next;
2095         }
2096
2097         if (conf->stats.interval && !conf->stats.out) {
2098                 conf->stats.out = RS_STATS_OUT_STDIO;
2099         }
2100
2101         if (conf->stats.timeout == 0) {
2102                 conf->stats.timeout = RS_DEFAULT_TIMEOUT;
2103         }
2104
2105         /*
2106          *      If were writing pcap data, or CSV to stdout we *really* don't want to send
2107          *      logging there as well.
2108          */
2109         if (conf->to_stdout || conf->list_attributes) {
2110                 fr_log_fp = stderr;
2111         }
2112
2113         if (conf->list_attributes) {
2114                 conf->logger = rs_packet_print_csv;
2115         } else if (fr_debug_flag > 0) {
2116                 conf->logger = rs_packet_print_fancy;
2117         }
2118
2119 #if !defined(HAVE_PCAP_FOPEN_OFFLINE) || !defined(HAVE_PCAP_DUMP_FOPEN)
2120         if (conf->from_stdin || conf->to_stdout) {
2121                 ERROR("PCAP streams not supported");
2122                 goto finish;
2123         }
2124 #endif
2125
2126         if (!conf->pcap_filter) {
2127                 snprintf(buffer, sizeof(buffer), "udp port %d or %d or %d",
2128                          port, port + 1, 3799);
2129                 conf->pcap_filter = buffer;
2130         }
2131
2132         if (dict_init(dict_dir, RADIUS_DICTIONARY) < 0) {
2133                 fr_perror("radsniff");
2134                 ret = 64;
2135                 goto finish;
2136         }
2137
2138         if (dict_read(radius_dir, RADIUS_DICTIONARY) == -1) {
2139                 fr_perror("radsniff");
2140                 ret = 64;
2141                 goto finish;
2142         }
2143
2144         fr_strerror();  /* Clear out any non-fatal errors */
2145
2146         if (conf->list_attributes) {
2147                 conf->list_da_num = rs_build_dict_list(conf->list_da, sizeof(conf->list_da) / sizeof(*conf->list_da),
2148                                                        conf->list_attributes);
2149                 if (conf->list_da_num < 0) {
2150                         usage(64);
2151                 }
2152                 rs_packet_print_csv_header();
2153         }
2154
2155         if (conf->link_attributes) {
2156                 conf->link_da_num = rs_build_dict_list(conf->link_da, sizeof(conf->link_da) / sizeof(*conf->link_da),
2157                                                        conf->link_attributes);
2158                 if (conf->link_da_num < 0) {
2159                         usage(64);
2160                 }
2161
2162                 link_tree = rbtree_create((rbcmp) rs_rtx_cmp, _unmark_link, 0);
2163                 if (!link_tree) {
2164                         ERROR("Failed creating RTX tree");
2165                         goto finish;
2166                 }
2167         }
2168
2169         if (conf->filter_request) {
2170                 vp_cursor_t cursor;
2171                 VALUE_PAIR *type;
2172
2173                 if (rs_build_filter(&conf->filter_request_vps, conf->filter_request) < 0) {
2174                         usage(64);
2175                 }
2176
2177                 fr_cursor_init(&cursor, &conf->filter_request_vps);
2178                 type = fr_cursor_next_by_num(&cursor, PW_PACKET_TYPE, 0, TAG_ANY);
2179                 if (type) {
2180                         fr_cursor_remove(&cursor);
2181                         conf->filter_request_code = type->vp_integer;
2182                         talloc_free(type);
2183                 }
2184         }
2185
2186         if (conf->filter_response) {
2187                 vp_cursor_t cursor;
2188                 VALUE_PAIR *type;
2189
2190                 if (rs_build_filter(&conf->filter_response_vps, conf->filter_response) < 0) {
2191                         usage(64);
2192                 }
2193
2194                 fr_cursor_init(&cursor, &conf->filter_response_vps);
2195                 type = fr_cursor_next_by_num(&cursor, PW_PACKET_TYPE, 0, TAG_ANY);
2196                 if (type) {
2197                         fr_cursor_remove(&cursor);
2198                         conf->filter_response_code = type->vp_integer;
2199                         talloc_free(type);
2200                 }
2201         }
2202
2203         /*
2204          *      Default to logging and capturing all events
2205          */
2206         if (conf->event_flags == 0) {
2207                 DEBUG("Logging all events");
2208                 memset(&conf->event_flags, 0xff, sizeof(conf->event_flags));
2209         }
2210
2211         /*
2212          *      If we need to list attributes, link requests using attributes, filter attributes
2213          *      or print the packet contents, we need to decode the attributes.
2214          *
2215          *      But, if were just logging requests, or graphing packets, we don't need to decode
2216          *      attributes.
2217          */
2218         if (conf->list_da_num || conf->link_da_num || conf->filter_response_vps || conf->filter_request_vps ||
2219             conf->print_packet) {
2220                 conf->decode_attrs = true;
2221         }
2222
2223         /*
2224          *      Setup the request tree
2225          */
2226         request_tree = rbtree_create((rbcmp) rs_packet_cmp, _unmark_request, 0);
2227         if (!request_tree) {
2228                 ERROR("Failed creating request tree");
2229                 goto finish;
2230         }
2231
2232         /*
2233          *      Get the default capture device
2234          */
2235         if (!conf->from_stdin && !conf->from_file && !conf->from_dev) {
2236                 pcap_if_t *all_devices;                 /* List of all devices libpcap can listen on */
2237                 pcap_if_t *dev_p;
2238
2239                 if (pcap_findalldevs(&all_devices, errbuf) < 0) {
2240                         ERROR("Error getting available capture devices: %s", errbuf);
2241                         goto finish;
2242                 }
2243
2244                 if (!all_devices) {
2245                         ERROR("No capture files specified and no live interfaces available");
2246                         ret = 64;
2247                         goto finish;
2248                 }
2249
2250                 for (dev_p = all_devices;
2251                      dev_p;
2252                      dev_p = dev_p->next) {
2253                         /* Don't use the any devices, it's horribly broken */
2254                         if (!strcmp(dev_p->name, "any")) continue;
2255                         *in_head = fr_pcap_init(conf, dev_p->name, PCAP_INTERFACE_IN);
2256                         in_head = &(*in_head)->next;
2257                 }
2258                 conf->from_auto = true;
2259                 conf->from_dev = true;
2260                 INFO("Defaulting to capture on all interfaces");
2261         }
2262
2263         /*
2264          *      Print captures values which will be used
2265          */
2266         if (fr_debug_flag > 2) {
2267                 DEBUG2("Sniffing with options:");
2268                 if (conf->from_dev)     {
2269                         char *buff = fr_pcap_device_names(conf, in, ' ');
2270                         DEBUG2("  Device(s)               : [%s]", buff);
2271                         talloc_free(buff);
2272                 }
2273                 if (conf->to_file || conf->to_stdout) {
2274                         DEBUG2("  Writing to              : [%s]", out->name);
2275                 }
2276                 if (conf->limit > 0)    {
2277                         DEBUG2("  Capture limit (packets) : [%" PRIu64 "]", conf->limit);
2278                 }
2279                         DEBUG2("  PCAP filter             : [%s]", conf->pcap_filter);
2280                         DEBUG2("  RADIUS secret           : [%s]", conf->radius_secret);
2281
2282                 if (conf->filter_request_code) {
2283                         DEBUG2("  RADIUS request code     : [%s]", fr_packet_codes[conf->filter_request_code]);
2284                 }
2285
2286                 if (conf->filter_request_vps){
2287                         DEBUG2("  RADIUS request filter   :");
2288                         vp_printlist(fr_log_fp, conf->filter_request_vps);
2289                 }
2290
2291                 if (conf->filter_response_code) {
2292                         DEBUG2("  RADIUS response code    : [%s]", fr_packet_codes[conf->filter_response_code]);
2293                 }
2294
2295                 if (conf->filter_response_vps){
2296                         DEBUG2("  RADIUS response filter  :");
2297                         vp_printlist(fr_log_fp, conf->filter_response_vps);
2298                 }
2299         }
2300
2301         /*
2302          *      Setup collectd templates
2303          */
2304 #ifdef HAVE_COLLECTDC_H
2305         if (conf->stats.out == RS_STATS_OUT_COLLECTD) {
2306                 size_t i;
2307                 rs_stats_tmpl_t *tmpl, **next;
2308
2309                 if (rs_stats_collectd_open(conf) < 0) {
2310                         exit(EXIT_FAILURE);
2311                 }
2312
2313                 next = &conf->stats.tmpl;
2314
2315                 for (i = 0; i < (sizeof(rs_useful_codes) / sizeof(*rs_useful_codes)); i++) {
2316                         tmpl = rs_stats_collectd_init_latency(conf, next, conf, "exchanged",
2317                                                               &stats.exchange[rs_useful_codes[i]],
2318                                                               rs_useful_codes[i]);
2319                         if (!tmpl) {
2320                                 ERROR("Error allocating memory for stats template");
2321                                 goto finish;
2322                         }
2323                         next = &(tmpl->next);
2324                 }
2325         }
2326 #endif
2327
2328         /*
2329          *      This actually opens the capture interfaces/files (we just allocated the memory earlier)
2330          */
2331         {
2332                 fr_pcap_t *tmp;
2333                 fr_pcap_t **tmp_p = &tmp;
2334
2335                 for (in_p = in;
2336                      in_p;
2337                      in_p = in_p->next) {
2338                         in_p->promiscuous = conf->promiscuous;
2339                         in_p->buffer_pkts = conf->buffer_pkts;
2340                         if (fr_pcap_open(in_p) < 0) {
2341                                 ERROR("Failed opening pcap handle (%s): %s", in_p->name, fr_strerror());
2342                                 if (conf->from_auto || (in_p->type == PCAP_FILE_IN)) {
2343                                         continue;
2344                                 }
2345
2346                                 goto finish;
2347                         }
2348
2349                         if (conf->pcap_filter) {
2350                                 if (fr_pcap_apply_filter(in_p, conf->pcap_filter) < 0) {
2351                                         ERROR("Failed applying filter");
2352                                         goto finish;
2353                                 }
2354                         }
2355
2356                         *tmp_p = in_p;
2357                         tmp_p = &(in_p->next);
2358                 }
2359                 *tmp_p = NULL;
2360                 in = tmp;
2361
2362                 if (!in) {
2363                         ERROR("No PCAP sources available");
2364                         exit(EXIT_FAILURE);
2365                 }
2366
2367                 /* Clear any irrelevant errors */
2368                 fr_strerror();
2369         }
2370
2371         /*
2372          *      Open our output interface (if we have one);
2373          */
2374         if (out) {
2375                 out->link_type = -1;    /* Infer output link type from input */
2376
2377                 for (in_p = in;
2378                      in_p;
2379                      in_p = in_p->next) {
2380                         if (out->link_type < 0) {
2381                                 out->link_type = in_p->link_type;
2382                                 continue;
2383                         }
2384
2385                         if (out->link_type != in_p->link_type) {
2386                                 ERROR("Asked to write to output file, but inputs do not have the same link type");
2387                                 ret = 64;
2388                                 goto finish;
2389                         }
2390                 }
2391
2392                 assert(out->link_type > 0);
2393
2394                 if (fr_pcap_open(out) < 0) {
2395                         ERROR("Failed opening pcap output (%s): %s", out->name, fr_strerror());
2396                         goto finish;
2397                 }
2398         }
2399
2400         /*
2401          *      Setup and enter the main event loop. Who needs libev when you can roll your own...
2402          */
2403          {
2404                 struct timeval now;
2405                 rs_update_t update;
2406
2407                 char *buff;
2408
2409                 memset(&stats, 0, sizeof(stats));
2410                 memset(&update, 0, sizeof(update));
2411
2412                 events = fr_event_list_create(conf, _rs_event_status);
2413                 if (!events) {
2414                         ERROR();
2415                         goto finish;
2416                 }
2417
2418                 /*
2419                  *  Initialise the signal handler pipe
2420                  */
2421                 if (pipe(self_pipe) < 0) {
2422                         ERROR("Couldn't open signal pipe: %s", fr_syserror(errno));
2423                         exit(EXIT_FAILURE);
2424                 }
2425
2426                 if (!fr_event_fd_insert(events, 0, self_pipe[0], rs_signal_action, events)) {
2427                         ERROR("Failed inserting signal pipe descriptor: %s", fr_strerror());
2428                         goto finish;
2429                 }
2430
2431                 /*
2432                  *  Now add fd's for each of the pcap sessions we opened
2433                  */
2434                 for (in_p = in;
2435                      in_p;
2436                      in_p = in_p->next) {
2437                         rs_event_t *event;
2438
2439                         event = talloc_zero(events, rs_event_t);
2440                         event->list = events;
2441                         event->in = in_p;
2442                         event->out = out;
2443                         event->stats = &stats;
2444
2445                         if (!fr_event_fd_insert(events, 0, in_p->fd, rs_got_packet, event)) {
2446                                 ERROR("Failed inserting file descriptor");
2447                                 goto finish;
2448                         }
2449                 }
2450
2451                 buff = fr_pcap_device_names(conf, in, ' ');
2452                 DEBUG("Sniffing on (%s)", buff);
2453                 talloc_free(buff);
2454
2455                 gettimeofday(&now, NULL);
2456
2457                 /*
2458                  *  Insert our stats processor
2459                  */
2460                 if (conf->stats.interval) {
2461                         static fr_event_t *event;
2462
2463                         update.list = events;
2464                         update.stats = &stats;
2465                         update.in = in;
2466
2467                         now.tv_sec += conf->stats.interval;
2468                         now.tv_usec = 0;
2469                         if (!fr_event_insert(events, rs_stats_process, (void *) &update, &now, &event)) {
2470                                 ERROR("Failed inserting stats event");
2471                         }
2472
2473                         INFO("Muting stats for the next %i milliseconds (warmup)", conf->stats.timeout);
2474                         rs_tv_add_ms(&now, conf->stats.timeout, &stats.quiet);
2475                 }
2476         }
2477
2478
2479         /*
2480          *      Do this as late as possible so we can return an error code if something went wrong.
2481          */
2482         if (conf->daemonize) {
2483                 rs_daemonize(conf->pidfile);
2484         }
2485
2486         /*
2487          *      Setup signal handlers so we always exit gracefully, ensuring output buffers are always
2488          *      flushed.
2489          */
2490         fr_set_signal(SIGPIPE, rs_signal_self);
2491         fr_set_signal(SIGINT, rs_signal_self);
2492         fr_set_signal(SIGTERM, rs_signal_self);
2493 #ifdef SIGQUIT
2494         fr_set_signal(SIGQUIT, rs_signal_self);
2495 #endif
2496
2497         fr_event_loop(events);  /* Enter the main event loop */
2498
2499         DEBUG("Done sniffing");
2500
2501         finish:
2502
2503         cleanup = true;
2504
2505         /*
2506          *      Free all the things! This also closes all the sockets and file descriptors
2507          */
2508         talloc_free(conf);
2509
2510         if (conf->daemonize) {
2511                 unlink(conf->pidfile);
2512         }
2513
2514         return ret;
2515 }