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