xref: /netbsd/external/bsd/libpcap/dist/pcap-snf.c (revision d5dfe396)
1e3899b6dSchristos #include <sys/cdefs.h>
2*d5dfe396Schristos __RCSID("$NetBSD: pcap-snf.c,v 1.6 2019/10/01 16:02:12 christos Exp $");
3e3899b6dSchristos 
4*d5dfe396Schristos /*	$NetBSD: pcap-snf.c,v 1.6 2019/10/01 16:02:12 christos Exp $	*/
5e3899b6dSchristos 
654a6ec8aSchristos #ifdef HAVE_CONFIG_H
7e1375535Schristos #include <config.h>
854a6ec8aSchristos #endif
954a6ec8aSchristos 
10e1375535Schristos #ifndef _WIN32
1154a6ec8aSchristos #include <sys/param.h>
12e1375535Schristos #endif /* !_WIN32 */
1354a6ec8aSchristos 
1454a6ec8aSchristos #include <stdlib.h>
1554a6ec8aSchristos #include <string.h>
1654a6ec8aSchristos #include <errno.h>
1754a6ec8aSchristos 
1854a6ec8aSchristos #include <ctype.h>
19e1375535Schristos #ifndef _WIN32
2054a6ec8aSchristos #include <netinet/in.h>
2154a6ec8aSchristos #include <sys/mman.h>
2254a6ec8aSchristos #include <sys/socket.h>
2354a6ec8aSchristos #include <sys/types.h>
2454a6ec8aSchristos #include <unistd.h>
25e1375535Schristos #endif /* !_WIN32 */
2654a6ec8aSchristos 
27c15d5effSchristos #include <snf.h>
286c7e2519Schristos #if SNF_VERSION_API >= 0x0003
296c7e2519Schristos #define SNF_HAVE_INJECT_API
306c7e2519Schristos #endif
3154a6ec8aSchristos 
32c15d5effSchristos #include "pcap-int.h"
33c15d5effSchristos #include "pcap-snf.h"
34c15d5effSchristos 
35c15d5effSchristos /*
36c15d5effSchristos  * Private data for capturing on SNF devices.
37c15d5effSchristos  */
38c15d5effSchristos struct pcap_snf {
39c15d5effSchristos 	snf_handle_t snf_handle; /* opaque device handle */
40c15d5effSchristos 	snf_ring_t   snf_ring;   /* opaque device ring handle */
416c7e2519Schristos #ifdef SNF_HAVE_INJECT_API
426c7e2519Schristos 	snf_inject_t snf_inj;    /* inject handle, if inject is used */
436c7e2519Schristos #endif
44c15d5effSchristos 	int          snf_timeout;
45c15d5effSchristos 	int          snf_boardnum;
46c15d5effSchristos };
4754a6ec8aSchristos 
4854a6ec8aSchristos static int
snf_set_datalink(pcap_t * p,int dlt)4954a6ec8aSchristos snf_set_datalink(pcap_t *p, int dlt)
5054a6ec8aSchristos {
5154a6ec8aSchristos 	p->linktype = dlt;
5254a6ec8aSchristos 	return (0);
5354a6ec8aSchristos }
5454a6ec8aSchristos 
5554a6ec8aSchristos static int
snf_pcap_stats(pcap_t * p,struct pcap_stat * ps)5654a6ec8aSchristos snf_pcap_stats(pcap_t *p, struct pcap_stat *ps)
5754a6ec8aSchristos {
5854a6ec8aSchristos 	struct snf_ring_stats stats;
596c7e2519Schristos 	struct pcap_snf *snfps = p->priv;
6054a6ec8aSchristos 	int rc;
6154a6ec8aSchristos 
626c7e2519Schristos 	if ((rc = snf_ring_getstats(snfps->snf_ring, &stats))) {
63e1375535Schristos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
64e1375535Schristos 		    rc, "snf_get_stats");
6554a6ec8aSchristos 		return -1;
6654a6ec8aSchristos 	}
6754a6ec8aSchristos 	ps->ps_recv = stats.ring_pkt_recv + stats.ring_pkt_overflow;
6854a6ec8aSchristos 	ps->ps_drop = stats.ring_pkt_overflow;
6954a6ec8aSchristos 	ps->ps_ifdrop = stats.nic_pkt_overflow + stats.nic_pkt_bad;
7054a6ec8aSchristos 	return 0;
7154a6ec8aSchristos }
7254a6ec8aSchristos 
7354a6ec8aSchristos static void
snf_platform_cleanup(pcap_t * p)7454a6ec8aSchristos snf_platform_cleanup(pcap_t *p)
7554a6ec8aSchristos {
76c15d5effSchristos 	struct pcap_snf *ps = p->priv;
77c15d5effSchristos 
786c7e2519Schristos #ifdef SNF_HAVE_INJECT_API
796c7e2519Schristos 	if (ps->snf_inj)
806c7e2519Schristos 		snf_inject_close(ps->snf_inj);
816c7e2519Schristos #endif
82c15d5effSchristos 	snf_ring_close(ps->snf_ring);
83c15d5effSchristos 	snf_close(ps->snf_handle);
8454a6ec8aSchristos 	pcap_cleanup_live_common(p);
8554a6ec8aSchristos }
8654a6ec8aSchristos 
8754a6ec8aSchristos static int
snf_getnonblock(pcap_t * p)88e1375535Schristos snf_getnonblock(pcap_t *p)
8954a6ec8aSchristos {
90c15d5effSchristos 	struct pcap_snf *ps = p->priv;
91c15d5effSchristos 
92c15d5effSchristos 	return (ps->snf_timeout == 0);
9354a6ec8aSchristos }
9454a6ec8aSchristos 
9554a6ec8aSchristos static int
snf_setnonblock(pcap_t * p,int nonblock)96e1375535Schristos snf_setnonblock(pcap_t *p, int nonblock)
9754a6ec8aSchristos {
98c15d5effSchristos 	struct pcap_snf *ps = p->priv;
99c15d5effSchristos 
10054a6ec8aSchristos 	if (nonblock)
101c15d5effSchristos 		ps->snf_timeout = 0;
10254a6ec8aSchristos 	else {
103c15d5effSchristos 		if (p->opt.timeout <= 0)
104c15d5effSchristos 			ps->snf_timeout = -1; /* forever */
10554a6ec8aSchristos 		else
106c15d5effSchristos 			ps->snf_timeout = p->opt.timeout;
10754a6ec8aSchristos 	}
10854a6ec8aSchristos 	return (0);
10954a6ec8aSchristos }
11054a6ec8aSchristos 
11154a6ec8aSchristos #define _NSEC_PER_SEC 1000000000
11254a6ec8aSchristos 
11354a6ec8aSchristos static inline
11454a6ec8aSchristos struct timeval
snf_timestamp_to_timeval(const int64_t ts_nanosec,const int tstamp_precision)1156c7e2519Schristos snf_timestamp_to_timeval(const int64_t ts_nanosec, const int tstamp_precision)
11654a6ec8aSchristos {
11754a6ec8aSchristos 	struct timeval tv;
1186c7e2519Schristos 	long tv_nsec;
119e1375535Schristos         const static struct timeval zero_timeval;
1206c7e2519Schristos 
12154a6ec8aSchristos         if (ts_nanosec == 0)
122e1375535Schristos                 return zero_timeval;
1236c7e2519Schristos 
12454a6ec8aSchristos 	tv.tv_sec = ts_nanosec / _NSEC_PER_SEC;
1256c7e2519Schristos 	tv_nsec = (ts_nanosec % _NSEC_PER_SEC);
1266c7e2519Schristos 
1276c7e2519Schristos 	/* libpcap expects tv_usec to be nanos if using nanosecond precision. */
1286c7e2519Schristos 	if (tstamp_precision == PCAP_TSTAMP_PRECISION_NANO)
1296c7e2519Schristos 		tv.tv_usec = tv_nsec;
1306c7e2519Schristos 	else
1316c7e2519Schristos 		tv.tv_usec = tv_nsec / 1000;
1326c7e2519Schristos 
13354a6ec8aSchristos 	return tv;
13454a6ec8aSchristos }
13554a6ec8aSchristos 
13654a6ec8aSchristos static int
snf_read(pcap_t * p,int cnt,pcap_handler callback,u_char * user)13754a6ec8aSchristos snf_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
13854a6ec8aSchristos {
139c15d5effSchristos 	struct pcap_snf *ps = p->priv;
14054a6ec8aSchristos 	struct pcap_pkthdr hdr;
14154a6ec8aSchristos 	int i, flags, err, caplen, n;
14254a6ec8aSchristos 	struct snf_recv_req req;
1436c7e2519Schristos 	int nonblock, timeout;
14454a6ec8aSchristos 
1456c7e2519Schristos 	if (!p)
14654a6ec8aSchristos 		return -1;
14754a6ec8aSchristos 
14854a6ec8aSchristos 	n = 0;
1496c7e2519Schristos 	timeout = ps->snf_timeout;
1507dd943c4Schristos 	while (n < cnt || PACKET_COUNT_IS_UNLIMITED(cnt)) {
15154a6ec8aSchristos 		/*
15254a6ec8aSchristos 		 * Has "pcap_breakloop()" been called?
15354a6ec8aSchristos 		 */
15454a6ec8aSchristos 		if (p->break_loop) {
15554a6ec8aSchristos 			if (n == 0) {
15654a6ec8aSchristos 				p->break_loop = 0;
15754a6ec8aSchristos 				return (-2);
15854a6ec8aSchristos 			} else {
15954a6ec8aSchristos 				return (n);
16054a6ec8aSchristos 			}
16154a6ec8aSchristos 		}
16254a6ec8aSchristos 
1636c7e2519Schristos 		err = snf_ring_recv(ps->snf_ring, timeout, &req);
16454a6ec8aSchristos 
16554a6ec8aSchristos 		if (err) {
1666c7e2519Schristos 			if (err == EBUSY || err == EAGAIN) {
1676c7e2519Schristos 				return (n);
1686c7e2519Schristos 			}
1696c7e2519Schristos 			else if (err == EINTR) {
1706c7e2519Schristos 				timeout = 0;
17154a6ec8aSchristos 				continue;
1726c7e2519Schristos 			}
1736c7e2519Schristos 			else {
174e1375535Schristos 				pcap_fmt_errmsg_for_errno(p->errbuf,
175e1375535Schristos 				    PCAP_ERRBUF_SIZE, err, "snf_read");
17654a6ec8aSchristos 				return -1;
17754a6ec8aSchristos 			}
17854a6ec8aSchristos 		}
17954a6ec8aSchristos 
18054a6ec8aSchristos 		caplen = req.length;
18154a6ec8aSchristos 		if (caplen > p->snapshot)
18254a6ec8aSchristos 			caplen = p->snapshot;
18354a6ec8aSchristos 
18454a6ec8aSchristos 		if ((p->fcode.bf_insns == NULL) ||
18554a6ec8aSchristos 		     bpf_filter(p->fcode.bf_insns, req.pkt_addr, req.length, caplen)) {
1866c7e2519Schristos 			hdr.ts = snf_timestamp_to_timeval(req.timestamp, p->opt.tstamp_precision);
18754a6ec8aSchristos 			hdr.caplen = caplen;
18854a6ec8aSchristos 			hdr.len = req.length;
18954a6ec8aSchristos 			callback(user, &hdr, req.pkt_addr);
19054a6ec8aSchristos 			n++;
191*d5dfe396Schristos 		}
1926c7e2519Schristos 
1936c7e2519Schristos 		/* After one successful packet is received, we won't block
1946c7e2519Schristos 		* again for that timeout. */
1956c7e2519Schristos 		if (timeout != 0)
1966c7e2519Schristos 			timeout = 0;
19754a6ec8aSchristos 	}
19854a6ec8aSchristos 	return (n);
19954a6ec8aSchristos }
20054a6ec8aSchristos 
20154a6ec8aSchristos static int
snf_setfilter(pcap_t * p,struct bpf_program * fp)20254a6ec8aSchristos snf_setfilter(pcap_t *p, struct bpf_program *fp)
20354a6ec8aSchristos {
20454a6ec8aSchristos 	if (!p)
20554a6ec8aSchristos 		return -1;
20654a6ec8aSchristos 	if (!fp) {
20754a6ec8aSchristos 		strncpy(p->errbuf, "setfilter: No filter specified",
20854a6ec8aSchristos 			sizeof(p->errbuf));
20954a6ec8aSchristos 		return -1;
21054a6ec8aSchristos 	}
21154a6ec8aSchristos 
21254a6ec8aSchristos 	/* Make our private copy of the filter */
21354a6ec8aSchristos 
21454a6ec8aSchristos 	if (install_bpf_program(p, fp) < 0)
21554a6ec8aSchristos 		return -1;
21654a6ec8aSchristos 
21754a6ec8aSchristos 	return (0);
21854a6ec8aSchristos }
21954a6ec8aSchristos 
22054a6ec8aSchristos static int
snf_inject(pcap_t * p,const void * buf _U_,size_t size _U_)22154a6ec8aSchristos snf_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
22254a6ec8aSchristos {
2236c7e2519Schristos #ifdef SNF_HAVE_INJECT_API
2246c7e2519Schristos 	struct pcap_snf *ps = p->priv;
2256c7e2519Schristos 	int rc;
2266c7e2519Schristos 	if (ps->snf_inj == NULL) {
2276c7e2519Schristos 		rc = snf_inject_open(ps->snf_boardnum, 0, &ps->snf_inj);
2286c7e2519Schristos 		if (rc) {
229e1375535Schristos 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
230e1375535Schristos 			    rc, "snf_inject_open");
2316c7e2519Schristos 			return (-1);
2326c7e2519Schristos 		}
2336c7e2519Schristos 	}
2346c7e2519Schristos 
2356c7e2519Schristos 	rc = snf_inject_send(ps->snf_inj, -1, 0, buf, size);
2366c7e2519Schristos 	if (!rc) {
2376c7e2519Schristos 		return (size);
2386c7e2519Schristos 	}
2396c7e2519Schristos 	else {
240e1375535Schristos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
241e1375535Schristos 		    rc, "snf_inject_send");
2426c7e2519Schristos 		return (-1);
2436c7e2519Schristos 	}
2446c7e2519Schristos #else
245*d5dfe396Schristos 	pcap_strlcpy(p->errbuf, "Sending packets isn't supported with this snf version",
24654a6ec8aSchristos 	    PCAP_ERRBUF_SIZE);
24754a6ec8aSchristos 	return (-1);
2486c7e2519Schristos #endif
24954a6ec8aSchristos }
25054a6ec8aSchristos 
25154a6ec8aSchristos static int
snf_activate(pcap_t * p)25254a6ec8aSchristos snf_activate(pcap_t* p)
25354a6ec8aSchristos {
254c15d5effSchristos 	struct pcap_snf *ps = p->priv;
25503e25b48Schristos 	char *device = p->opt.device;
25654a6ec8aSchristos 	const char *nr = NULL;
25754a6ec8aSchristos 	int err;
2586c7e2519Schristos 	int flags = -1, ring_id = -1;
25954a6ec8aSchristos 
26054a6ec8aSchristos 	if (device == NULL) {
261e1375535Schristos 		pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "device is NULL");
26254a6ec8aSchristos 		return -1;
26354a6ec8aSchristos 	}
26454a6ec8aSchristos 
26554a6ec8aSchristos 	/* In Libpcap, we set pshared by default if NUM_RINGS is set to > 1.
26654a6ec8aSchristos 	 * Since libpcap isn't thread-safe */
2676c7e2519Schristos 	if ((nr = getenv("SNF_FLAGS")) && *nr)
2686c7e2519Schristos 		flags = strtol(nr, NULL, 0);
2696c7e2519Schristos 	else if ((nr = getenv("SNF_NUM_RINGS")) && *nr && atoi(nr) > 1)
2706c7e2519Schristos 		flags = SNF_F_PSHARED;
27154a6ec8aSchristos 	else
27254a6ec8aSchristos 		nr = NULL;
27354a6ec8aSchristos 
274e1375535Schristos 
275e1375535Schristos         /* Allow pcap_set_buffer_size() to set dataring_size.
276e1375535Schristos          * Default is zero which allows setting from env SNF_DATARING_SIZE.
277e1375535Schristos          * pcap_set_buffer_size() is in bytes while snf_open() accepts values
278e1375535Schristos          * between 0 and 1048576 in Megabytes. Values in this range are
279e1375535Schristos          * mapped to 1MB.
280e1375535Schristos          */
281c15d5effSchristos 	err = snf_open(ps->snf_boardnum,
28254a6ec8aSchristos 			0, /* let SNF API parse SNF_NUM_RINGS, if set */
28354a6ec8aSchristos 			NULL, /* default RSS, or use SNF_RSS_FLAGS env */
284e1375535Schristos                         (p->opt.buffer_size > 0 && p->opt.buffer_size < 1048576) ? 1048576 : p->opt.buffer_size, /* default to SNF_DATARING_SIZE from env */
28554a6ec8aSchristos 			flags, /* may want pshared */
286c15d5effSchristos 			&ps->snf_handle);
28754a6ec8aSchristos 	if (err != 0) {
288e1375535Schristos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
289e1375535Schristos 		    err, "snf_open failed");
29054a6ec8aSchristos 		return -1;
29154a6ec8aSchristos 	}
29254a6ec8aSchristos 
2936c7e2519Schristos 	if ((nr = getenv("SNF_PCAP_RING_ID")) && *nr) {
2946c7e2519Schristos 		ring_id = (int) strtol(nr, NULL, 0);
2956c7e2519Schristos 	}
2966c7e2519Schristos 	err = snf_ring_open_id(ps->snf_handle, ring_id, &ps->snf_ring);
29754a6ec8aSchristos 	if (err != 0) {
298e1375535Schristos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
299e1375535Schristos 		    err, "snf_ring_open_id(ring=%d) failed", ring_id);
30054a6ec8aSchristos 		return -1;
30154a6ec8aSchristos 	}
30254a6ec8aSchristos 
303e1375535Schristos 	/*
304e1375535Schristos 	 * Turn a negative snapshot value (invalid), a snapshot value of
305e1375535Schristos 	 * 0 (unspecified), or a value bigger than the normal maximum
306e1375535Schristos 	 * value, into the maximum allowed value.
307e1375535Schristos 	 *
308e1375535Schristos 	 * If some application really *needs* a bigger snapshot
309e1375535Schristos 	 * length, we should just increase MAXIMUM_SNAPLEN.
310e1375535Schristos 	 */
311e1375535Schristos 	if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
312e1375535Schristos 		p->snapshot = MAXIMUM_SNAPLEN;
313e1375535Schristos 
314c15d5effSchristos 	if (p->opt.timeout <= 0)
315c15d5effSchristos 		ps->snf_timeout = -1;
31654a6ec8aSchristos 	else
317c15d5effSchristos 		ps->snf_timeout = p->opt.timeout;
31854a6ec8aSchristos 
319c15d5effSchristos 	err = snf_start(ps->snf_handle);
32054a6ec8aSchristos 	if (err != 0) {
321e1375535Schristos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
322e1375535Schristos 		    err, "snf_start failed");
32354a6ec8aSchristos 		return -1;
32454a6ec8aSchristos 	}
32554a6ec8aSchristos 
32654a6ec8aSchristos 	/*
32754a6ec8aSchristos 	 * "select()" and "poll()" don't work on snf descriptors.
32854a6ec8aSchristos 	 */
329e1375535Schristos #ifndef _WIN32
33054a6ec8aSchristos 	p->selectable_fd = -1;
331e1375535Schristos #endif /* !_WIN32 */
33254a6ec8aSchristos 	p->linktype = DLT_EN10MB;
33354a6ec8aSchristos 	p->read_op = snf_read;
33454a6ec8aSchristos 	p->inject_op = snf_inject;
33554a6ec8aSchristos 	p->setfilter_op = snf_setfilter;
33654a6ec8aSchristos 	p->setdirection_op = NULL; /* Not implemented.*/
33754a6ec8aSchristos 	p->set_datalink_op = snf_set_datalink;
33854a6ec8aSchristos 	p->getnonblock_op = snf_getnonblock;
33954a6ec8aSchristos 	p->setnonblock_op = snf_setnonblock;
34054a6ec8aSchristos 	p->stats_op = snf_pcap_stats;
34154a6ec8aSchristos 	p->cleanup_op = snf_platform_cleanup;
3426c7e2519Schristos #ifdef SNF_HAVE_INJECT_API
3436c7e2519Schristos 	ps->snf_inj = NULL;
3446c7e2519Schristos #endif
34554a6ec8aSchristos 	return 0;
34654a6ec8aSchristos }
34754a6ec8aSchristos 
3486c7e2519Schristos #define MAX_DESC_LENGTH 128
34954a6ec8aSchristos int
snf_findalldevs(pcap_if_list_t * devlistp,char * errbuf)350e1375535Schristos snf_findalldevs(pcap_if_list_t *devlistp, char *errbuf)
35154a6ec8aSchristos {
352e1375535Schristos 	pcap_if_t *dev;
353e1375535Schristos #ifdef _WIN32
354e1375535Schristos 	struct sockaddr_in addr;
355e1375535Schristos #endif
3566c7e2519Schristos 	struct snf_ifaddrs *ifaddrs, *ifa;
357e1375535Schristos 	char name[MAX_DESC_LENGTH];
3586c7e2519Schristos 	char desc[MAX_DESC_LENGTH];
359e1375535Schristos 	int ret, allports = 0, merge = 0;
360e1375535Schristos 	const char *nr = NULL;
3616c7e2519Schristos 
362e1375535Schristos 	if (snf_init(SNF_VERSION_API)) {
363e1375535Schristos 		(void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
364e1375535Schristos 		    "snf_getifaddrs: snf_init failed");
3656c7e2519Schristos 		return (-1);
366e1375535Schristos 	}
3676c7e2519Schristos 
3686c7e2519Schristos 	if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL)
3696c7e2519Schristos 	{
370e1375535Schristos 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
371e1375535Schristos 		    errno, "snf_getifaddrs");
3726c7e2519Schristos 		return (-1);
3736c7e2519Schristos 	}
374e1375535Schristos 	if ((nr = getenv("SNF_FLAGS")) && *nr) {
375e1375535Schristos 		errno = 0;
376e1375535Schristos 		merge = strtol(nr, NULL, 0);
377e1375535Schristos 		if (errno) {
37803e25b48Schristos 			(void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
379e1375535Schristos 				"snf_getifaddrs: SNF_FLAGS is not a valid number");
3806c7e2519Schristos 			return (-1);
3816c7e2519Schristos 		}
382e1375535Schristos 		merge = merge & SNF_F_AGGREGATE_PORTMASK;
3836c7e2519Schristos 	}
3846c7e2519Schristos 
385e1375535Schristos 	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->snf_ifa_next) {
386e1375535Schristos 		/*
387e1375535Schristos 		 * Myricom SNF adapter ports may appear as regular
388e1375535Schristos 		 * network interfaces, which would already have been
389e1375535Schristos 		 * added to the list of adapters by pcap_platform_finddevs()
390e1375535Schristos 		 * if this isn't an SNF-only version of libpcap.
391e1375535Schristos 		 *
392e1375535Schristos 		 * Our create routine intercepts pcap_create() calls for
393e1375535Schristos 		 * those interfaces and arranges that they will be
394e1375535Schristos 		 * opened using the SNF API instead.
395e1375535Schristos 		 *
396e1375535Schristos 		 * So if we already have an entry for the device, we
397e1375535Schristos 		 * don't add an additional entry for it, we just
398e1375535Schristos 		 * update the description for it, if any, to indicate
399e1375535Schristos 		 * which snfN device it is.  Otherwise, we add an entry
400e1375535Schristos 		 * for it.
401e1375535Schristos 		 *
402e1375535Schristos 		 * In either case, if SNF_F_AGGREGATE_PORTMASK is set
403e1375535Schristos 		 * in SNF_FLAGS, we add this port to the bitmask
404e1375535Schristos 		 * of ports, which we use to generate a device
405e1375535Schristos 		 * we can use to capture on all ports.
406e1375535Schristos 		 *
407e1375535Schristos 		 * Generate the description string.  If port aggregation
408e1375535Schristos 		 * is set, use 2^{port number} as the unit number,
409e1375535Schristos 		 * rather than {port number}.
410e1375535Schristos 		 *
411e1375535Schristos 		 * XXX - do entries in this list have IP addresses for
412e1375535Schristos 		 * the port?  If so, should we add them to the
413e1375535Schristos 		 * entry for the device, if they're not already in the
414e1375535Schristos 		 * list of IP addresses for the device?
415e1375535Schristos  		 */
416e1375535Schristos 		(void)pcap_snprintf(desc,MAX_DESC_LENGTH,"Myricom %ssnf%d",
417e1375535Schristos 			merge ? "Merge Bitmask Port " : "",
418e1375535Schristos 			merge ? 1 << ifa->snf_ifa_portnum : ifa->snf_ifa_portnum);
419e1375535Schristos 		/*
420e1375535Schristos 		 * Add the port to the bitmask.
421e1375535Schristos 		 */
422e1375535Schristos 		if (merge)
423e1375535Schristos 			allports |= 1 << ifa->snf_ifa_portnum;
424e1375535Schristos 		/*
425e1375535Schristos 		 * See if there's already an entry for the device
426e1375535Schristos 		 * with the name ifa->snf_ifa_name.
427e1375535Schristos 		 */
428e1375535Schristos 		dev = find_dev(devlistp, ifa->snf_ifa_name);
429e1375535Schristos 		if (dev != NULL) {
430e1375535Schristos 			/*
431e1375535Schristos 			 * Yes.  Update its description.
432e1375535Schristos 			 */
433e1375535Schristos 			char *desc_str;
4346c7e2519Schristos 
435e1375535Schristos 			desc_str = strdup(desc);
436e1375535Schristos 			if (desc_str == NULL) {
437e1375535Schristos 				pcap_fmt_errmsg_for_errno(errbuf,
438e1375535Schristos 				    PCAP_ERRBUF_SIZE, errno,
439e1375535Schristos 				    "snf_findalldevs strdup");
440e1375535Schristos 				return -1;
441e1375535Schristos 			}
442e1375535Schristos 			free(dev->description);
443e1375535Schristos 			dev->description = desc_str;
444e1375535Schristos 		} else {
445e1375535Schristos 			/*
446e1375535Schristos 			 * No.  Add an entry for it.
447e1375535Schristos 			 *
448e1375535Schristos 			 * XXX - is there a notion of "up" or "running",
449e1375535Schristos 			 * and can we determine whether something's
450e1375535Schristos 			 * plugged into the adapter and set
451e1375535Schristos 			 * PCAP_IF_CONNECTION_STATUS_CONNECTED or
452e1375535Schristos 			 * PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
453e1375535Schristos 			 */
454e1375535Schristos 			dev = add_dev(devlistp, ifa->snf_ifa_name, 0, desc,
455e1375535Schristos 			    errbuf);
456e1375535Schristos 			if (dev == NULL)
457e1375535Schristos 				return -1;
458e1375535Schristos #ifdef _WIN32
459e1375535Schristos 			/*
460e1375535Schristos 			 * On Windows, fill in IP# from device name
461e1375535Schristos 			 */
462e1375535Schristos                         ret = inet_pton(AF_INET, dev->name, &addr.sin_addr);
463e1375535Schristos                         if (ret == 1) {
464e1375535Schristos                         	/*
465e1375535Schristos                         	 * Successful conversion of device name
466e1375535Schristos                         	 * to IPv4 address.
467e1375535Schristos                         	 */
468e1375535Schristos 	                        addr.sin_family = AF_INET;
469e1375535Schristos         	                if (add_addr_to_dev(dev, &addr, sizeof(addr),
470e1375535Schristos                 	            NULL, 0, NULL, 0, NULL, 0, errbuf) == -1)
471e1375535Schristos                         		return -1;
472e1375535Schristos                         } else if (ret == -1) {
473e1375535Schristos 				/*
474e1375535Schristos 				 * Error.
475e1375535Schristos 				 */
476e1375535Schristos 				pcap_fmt_errmsg_for_errno(errbuf,
477e1375535Schristos 				    PCAP_ERRBUF_SIZE, errno,
478e1375535Schristos 				    "sinf_findalldevs inet_pton");
479e1375535Schristos                                 return -1;
480e1375535Schristos                         }
481e1375535Schristos #endif _WIN32
482e1375535Schristos 		}
4836c7e2519Schristos 	}
4846c7e2519Schristos 	snf_freeifaddrs(ifaddrs);
48554a6ec8aSchristos 	/*
486e1375535Schristos 	 * Create a snfX entry if port aggregation is enabled
48754a6ec8aSchristos        	 */
488e1375535Schristos 	if (merge) {
489e1375535Schristos 		/*
490e1375535Schristos 		 * Add a new entry with all ports bitmask
491e1375535Schristos 		 */
492e1375535Schristos 		(void)pcap_snprintf(name,MAX_DESC_LENGTH,"snf%d",allports);
493e1375535Schristos 		(void)pcap_snprintf(desc,MAX_DESC_LENGTH,"Myricom Merge Bitmask All Ports snf%d",
494e1375535Schristos 			allports);
495e1375535Schristos 		/*
496e1375535Schristos 		 * XXX - is there any notion of "up" and "running" that
497e1375535Schristos 		 * would apply to this device, given that it handles
498e1375535Schristos 		 * multiple ports?
499e1375535Schristos 		 *
500e1375535Schristos 		 * Presumably, there's no notion of "connected" vs.
501e1375535Schristos 		 * "disconnected", as "is this plugged into a network?"
502e1375535Schristos 		 * would be a per-port property.
503e1375535Schristos 		 */
504e1375535Schristos 		if (add_dev(devlistp, name,
505e1375535Schristos 		    PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE, desc,
506e1375535Schristos 		    errbuf) == NULL)
507e1375535Schristos 			return (-1);
508e1375535Schristos 		/*
509e1375535Schristos 		 * XXX - should we give it a list of addresses with all
510e1375535Schristos 		 * the addresses for all the ports?
511e1375535Schristos 		 */
512e1375535Schristos 	}
513e1375535Schristos 
51454a6ec8aSchristos 	return 0;
51554a6ec8aSchristos }
51654a6ec8aSchristos 
51754a6ec8aSchristos pcap_t *
snf_create(const char * device,char * ebuf,int * is_ours)518c15d5effSchristos snf_create(const char *device, char *ebuf, int *is_ours)
51954a6ec8aSchristos {
52054a6ec8aSchristos 	pcap_t *p;
52154a6ec8aSchristos 	int boardnum = -1;
52254a6ec8aSchristos 	struct snf_ifaddrs *ifaddrs, *ifa;
52354a6ec8aSchristos 	size_t devlen;
524c15d5effSchristos 	struct pcap_snf *ps;
52554a6ec8aSchristos 
526c15d5effSchristos 	if (snf_init(SNF_VERSION_API)) {
527c15d5effSchristos 		/* Can't initialize the API, so no SNF devices */
528c15d5effSchristos 		*is_ours = 0;
52954a6ec8aSchristos 		return NULL;
530c15d5effSchristos 	}
53154a6ec8aSchristos 
53254a6ec8aSchristos 	/*
53354a6ec8aSchristos 	 * Match a given interface name to our list of interface names, from
53454a6ec8aSchristos 	 * which we can obtain the intended board number
53554a6ec8aSchristos 	 */
536c15d5effSchristos 	if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL) {
537c15d5effSchristos 		/* Can't get SNF addresses */
538c15d5effSchristos 		*is_ours = 0;
53954a6ec8aSchristos 		return NULL;
540c15d5effSchristos 	}
54154a6ec8aSchristos 	devlen = strlen(device) + 1;
54254a6ec8aSchristos 	ifa = ifaddrs;
54354a6ec8aSchristos 	while (ifa) {
544e1375535Schristos 		if (strncmp(device, ifa->snf_ifa_name, devlen) == 0) {
54554a6ec8aSchristos 			boardnum = ifa->snf_ifa_boardnum;
54654a6ec8aSchristos 			break;
54754a6ec8aSchristos 		}
54854a6ec8aSchristos 		ifa = ifa->snf_ifa_next;
54954a6ec8aSchristos 	}
55054a6ec8aSchristos 	snf_freeifaddrs(ifaddrs);
55154a6ec8aSchristos 
55254a6ec8aSchristos 	if (ifa == NULL) {
55354a6ec8aSchristos 		/*
55454a6ec8aSchristos 		 * If we can't find the device by name, support the name "snfX"
55554a6ec8aSchristos 		 * and "snf10gX" where X is the board number.
55654a6ec8aSchristos 		 */
55754a6ec8aSchristos 		if (sscanf(device, "snf10g%d", &boardnum) != 1 &&
558c15d5effSchristos 		    sscanf(device, "snf%d", &boardnum) != 1) {
559c15d5effSchristos 			/* Nope, not a supported name */
560c15d5effSchristos 			*is_ours = 0;
56154a6ec8aSchristos 			return NULL;
56254a6ec8aSchristos 		}
563c15d5effSchristos 	}
56454a6ec8aSchristos 
565c15d5effSchristos 	/* OK, it's probably ours. */
566c15d5effSchristos 	*is_ours = 1;
567c15d5effSchristos 
56803e25b48Schristos 	p = pcap_create_common(ebuf, sizeof (struct pcap_snf));
56954a6ec8aSchristos 	if (p == NULL)
57054a6ec8aSchristos 		return NULL;
571c15d5effSchristos 	ps = p->priv;
57254a6ec8aSchristos 
5736c7e2519Schristos 	/*
5746c7e2519Schristos 	 * We support microsecond and nanosecond time stamps.
5756c7e2519Schristos 	 */
5766c7e2519Schristos 	p->tstamp_precision_count = 2;
5776c7e2519Schristos 	p->tstamp_precision_list = malloc(2 * sizeof(u_int));
5786c7e2519Schristos 	if (p->tstamp_precision_list == NULL) {
579e1375535Schristos 		pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno,
580e1375535Schristos 		    "malloc");
58103e25b48Schristos 		pcap_close(p);
5826c7e2519Schristos 		return NULL;
5836c7e2519Schristos 	}
5846c7e2519Schristos 	p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO;
5856c7e2519Schristos 	p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO;
5866c7e2519Schristos 
58754a6ec8aSchristos 	p->activate_op = snf_activate;
588c15d5effSchristos 	ps->snf_boardnum = boardnum;
58954a6ec8aSchristos 	return p;
59054a6ec8aSchristos }
59103e25b48Schristos 
59203e25b48Schristos #ifdef SNF_ONLY
59303e25b48Schristos /*
59403e25b48Schristos  * This libpcap build supports only SNF cards, not regular network
59503e25b48Schristos  * interfaces..
59603e25b48Schristos  */
59703e25b48Schristos 
59803e25b48Schristos /*
599e1375535Schristos  * There are no regular interfaces, just SNF interfaces.
60003e25b48Schristos  */
60103e25b48Schristos int
pcap_platform_finddevs(pcap_if_list_t * devlistp,char * errbuf)602e1375535Schristos pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
60303e25b48Schristos {
60403e25b48Schristos 	return (0);
60503e25b48Schristos }
60603e25b48Schristos 
60703e25b48Schristos /*
60803e25b48Schristos  * Attempts to open a regular interface fail.
60903e25b48Schristos  */
61003e25b48Schristos pcap_t *
pcap_create_interface(const char * device,char * errbuf)61103e25b48Schristos pcap_create_interface(const char *device, char *errbuf)
61203e25b48Schristos {
61303e25b48Schristos 	pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
61403e25b48Schristos 	    "This version of libpcap only supports SNF cards");
61503e25b48Schristos 	return NULL;
61603e25b48Schristos }
617e1375535Schristos 
618e1375535Schristos /*
619e1375535Schristos  * Libpcap version string.
620e1375535Schristos  */
621e1375535Schristos const char *
pcap_lib_version(void)622e1375535Schristos pcap_lib_version(void)
623e1375535Schristos {
624e1375535Schristos 	return (PCAP_VERSION_STRING " (SNF-only)");
625e1375535Schristos }
62603e25b48Schristos #endif
627