blob: a9162eba50105ffe865a2ee4116ae6ec38b0e2ee [file] [log] [blame]
JP Abgrall511eca32014-02-12 13:46:45 -08001#ifdef HAVE_CONFIG_H
Haibo Huang165065a2018-07-23 17:26:52 -07002#include <config.h>
JP Abgrall511eca32014-02-12 13:46:45 -08003#endif
4
Haibo Huang165065a2018-07-23 17:26:52 -07005#ifndef _WIN32
JP Abgrall511eca32014-02-12 13:46:45 -08006#include <sys/param.h>
Haibo Huang165065a2018-07-23 17:26:52 -07007#endif /* !_WIN32 */
JP Abgrall511eca32014-02-12 13:46:45 -08008
9#include <stdlib.h>
10#include <string.h>
11#include <errno.h>
12
Haibo Huang165065a2018-07-23 17:26:52 -070013#ifndef _WIN32
JP Abgrall511eca32014-02-12 13:46:45 -080014#include <netinet/in.h>
15#include <sys/mman.h>
16#include <sys/socket.h>
17#include <sys/types.h>
18#include <unistd.h>
Haibo Huang165065a2018-07-23 17:26:52 -070019#endif /* !_WIN32 */
JP Abgrall511eca32014-02-12 13:46:45 -080020
21#include <snf.h>
Elliott Hughesd8845d72015-10-19 18:07:04 -070022#if SNF_VERSION_API >= 0x0003
23#define SNF_HAVE_INJECT_API
24#endif
JP Abgrall511eca32014-02-12 13:46:45 -080025
26#include "pcap-int.h"
27#include "pcap-snf.h"
28
29/*
30 * Private data for capturing on SNF devices.
31 */
32struct pcap_snf {
33 snf_handle_t snf_handle; /* opaque device handle */
34 snf_ring_t snf_ring; /* opaque device ring handle */
Elliott Hughesd8845d72015-10-19 18:07:04 -070035#ifdef SNF_HAVE_INJECT_API
Haibo Huang165065a2018-07-23 17:26:52 -070036 snf_inject_t snf_inj; /* inject handle, if inject is used */
Elliott Hughesd8845d72015-10-19 18:07:04 -070037#endif
Haibo Huang165065a2018-07-23 17:26:52 -070038 int snf_timeout;
39 int snf_boardnum;
JP Abgrall511eca32014-02-12 13:46:45 -080040};
41
42static int
43snf_set_datalink(pcap_t *p, int dlt)
44{
45 p->linktype = dlt;
46 return (0);
47}
48
49static int
50snf_pcap_stats(pcap_t *p, struct pcap_stat *ps)
51{
52 struct snf_ring_stats stats;
Elliott Hughesd8845d72015-10-19 18:07:04 -070053 struct pcap_snf *snfps = p->priv;
JP Abgrall511eca32014-02-12 13:46:45 -080054 int rc;
55
Elliott Hughesd8845d72015-10-19 18:07:04 -070056 if ((rc = snf_ring_getstats(snfps->snf_ring, &stats))) {
Haibo Huang165065a2018-07-23 17:26:52 -070057 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
58 rc, "snf_get_stats");
JP Abgrall511eca32014-02-12 13:46:45 -080059 return -1;
60 }
61 ps->ps_recv = stats.ring_pkt_recv + stats.ring_pkt_overflow;
62 ps->ps_drop = stats.ring_pkt_overflow;
63 ps->ps_ifdrop = stats.nic_pkt_overflow + stats.nic_pkt_bad;
64 return 0;
65}
66
67static void
68snf_platform_cleanup(pcap_t *p)
69{
70 struct pcap_snf *ps = p->priv;
71
Elliott Hughesd8845d72015-10-19 18:07:04 -070072#ifdef SNF_HAVE_INJECT_API
Haibo Huang165065a2018-07-23 17:26:52 -070073 if (ps->snf_inj)
74 snf_inject_close(ps->snf_inj);
Elliott Hughesd8845d72015-10-19 18:07:04 -070075#endif
JP Abgrall511eca32014-02-12 13:46:45 -080076 snf_ring_close(ps->snf_ring);
77 snf_close(ps->snf_handle);
78 pcap_cleanup_live_common(p);
79}
80
81static int
Haibo Huang165065a2018-07-23 17:26:52 -070082snf_getnonblock(pcap_t *p)
JP Abgrall511eca32014-02-12 13:46:45 -080083{
84 struct pcap_snf *ps = p->priv;
85
86 return (ps->snf_timeout == 0);
87}
88
89static int
Haibo Huang165065a2018-07-23 17:26:52 -070090snf_setnonblock(pcap_t *p, int nonblock)
JP Abgrall511eca32014-02-12 13:46:45 -080091{
92 struct pcap_snf *ps = p->priv;
93
94 if (nonblock)
95 ps->snf_timeout = 0;
96 else {
97 if (p->opt.timeout <= 0)
98 ps->snf_timeout = -1; /* forever */
99 else
100 ps->snf_timeout = p->opt.timeout;
101 }
102 return (0);
103}
104
105#define _NSEC_PER_SEC 1000000000
106
107static inline
108struct timeval
Elliott Hughesd8845d72015-10-19 18:07:04 -0700109snf_timestamp_to_timeval(const int64_t ts_nanosec, const int tstamp_precision)
JP Abgrall511eca32014-02-12 13:46:45 -0800110{
111 struct timeval tv;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700112 long tv_nsec;
Haibo Huang165065a2018-07-23 17:26:52 -0700113 const static struct timeval zero_timeval;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700114
Haibo Huang165065a2018-07-23 17:26:52 -0700115 if (ts_nanosec == 0)
116 return zero_timeval;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700117
JP Abgrall511eca32014-02-12 13:46:45 -0800118 tv.tv_sec = ts_nanosec / _NSEC_PER_SEC;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700119 tv_nsec = (ts_nanosec % _NSEC_PER_SEC);
120
121 /* libpcap expects tv_usec to be nanos if using nanosecond precision. */
122 if (tstamp_precision == PCAP_TSTAMP_PRECISION_NANO)
123 tv.tv_usec = tv_nsec;
124 else
125 tv.tv_usec = tv_nsec / 1000;
126
JP Abgrall511eca32014-02-12 13:46:45 -0800127 return tv;
128}
129
130static int
131snf_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
132{
133 struct pcap_snf *ps = p->priv;
134 struct pcap_pkthdr hdr;
135 int i, flags, err, caplen, n;
136 struct snf_recv_req req;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700137 int nonblock, timeout;
JP Abgrall511eca32014-02-12 13:46:45 -0800138
Elliott Hughesd8845d72015-10-19 18:07:04 -0700139 if (!p)
JP Abgrall511eca32014-02-12 13:46:45 -0800140 return -1;
141
142 n = 0;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700143 timeout = ps->snf_timeout;
JP Abgrall511eca32014-02-12 13:46:45 -0800144 while (n < cnt || PACKET_COUNT_IS_UNLIMITED(cnt)) {
145 /*
146 * Has "pcap_breakloop()" been called?
147 */
148 if (p->break_loop) {
149 if (n == 0) {
150 p->break_loop = 0;
151 return (-2);
152 } else {
153 return (n);
154 }
155 }
156
Elliott Hughesd8845d72015-10-19 18:07:04 -0700157 err = snf_ring_recv(ps->snf_ring, timeout, &req);
JP Abgrall511eca32014-02-12 13:46:45 -0800158
159 if (err) {
Elliott Hughesd8845d72015-10-19 18:07:04 -0700160 if (err == EBUSY || err == EAGAIN) {
161 return (n);
162 }
163 else if (err == EINTR) {
164 timeout = 0;
JP Abgrall511eca32014-02-12 13:46:45 -0800165 continue;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700166 }
167 else {
Haibo Huang165065a2018-07-23 17:26:52 -0700168 pcap_fmt_errmsg_for_errno(p->errbuf,
169 PCAP_ERRBUF_SIZE, err, "snf_read");
JP Abgrall511eca32014-02-12 13:46:45 -0800170 return -1;
171 }
172 }
173
174 caplen = req.length;
175 if (caplen > p->snapshot)
176 caplen = p->snapshot;
177
178 if ((p->fcode.bf_insns == NULL) ||
Haibo Huangee759ce2021-01-05 21:34:29 -0800179 pcap_filter(p->fcode.bf_insns, req.pkt_addr, req.length, caplen)) {
Elliott Hughesd8845d72015-10-19 18:07:04 -0700180 hdr.ts = snf_timestamp_to_timeval(req.timestamp, p->opt.tstamp_precision);
JP Abgrall511eca32014-02-12 13:46:45 -0800181 hdr.caplen = caplen;
182 hdr.len = req.length;
183 callback(user, &hdr, req.pkt_addr);
Haibo Huang4ccd6832020-04-23 18:03:48 -0700184 n++;
JP Abgrall511eca32014-02-12 13:46:45 -0800185 }
Elliott Hughesd8845d72015-10-19 18:07:04 -0700186
187 /* After one successful packet is received, we won't block
188 * again for that timeout. */
189 if (timeout != 0)
190 timeout = 0;
JP Abgrall511eca32014-02-12 13:46:45 -0800191 }
192 return (n);
193}
194
195static int
Haibo Huangee759ce2021-01-05 21:34:29 -0800196snf_inject(pcap_t *p, const void *buf _U_, int size _U_)
JP Abgrall511eca32014-02-12 13:46:45 -0800197{
Elliott Hughesd8845d72015-10-19 18:07:04 -0700198#ifdef SNF_HAVE_INJECT_API
199 struct pcap_snf *ps = p->priv;
Haibo Huang165065a2018-07-23 17:26:52 -0700200 int rc;
201 if (ps->snf_inj == NULL) {
202 rc = snf_inject_open(ps->snf_boardnum, 0, &ps->snf_inj);
203 if (rc) {
204 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
205 rc, "snf_inject_open");
206 return (-1);
207 }
208 }
Elliott Hughesd8845d72015-10-19 18:07:04 -0700209
Haibo Huang165065a2018-07-23 17:26:52 -0700210 rc = snf_inject_send(ps->snf_inj, -1, 0, buf, size);
211 if (!rc) {
212 return (size);
213 }
214 else {
215 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
216 rc, "snf_inject_send");
217 return (-1);
218 }
Elliott Hughesd8845d72015-10-19 18:07:04 -0700219#else
Haibo Huang4ccd6832020-04-23 18:03:48 -0700220 pcap_strlcpy(p->errbuf, "Sending packets isn't supported with this snf version",
JP Abgrall511eca32014-02-12 13:46:45 -0800221 PCAP_ERRBUF_SIZE);
222 return (-1);
Elliott Hughesd8845d72015-10-19 18:07:04 -0700223#endif
JP Abgrall511eca32014-02-12 13:46:45 -0800224}
225
226static int
227snf_activate(pcap_t* p)
228{
229 struct pcap_snf *ps = p->priv;
Elliott Hughes965a4b52017-05-15 10:37:39 -0700230 char *device = p->opt.device;
JP Abgrall511eca32014-02-12 13:46:45 -0800231 const char *nr = NULL;
232 int err;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700233 int flags = -1, ring_id = -1;
JP Abgrall511eca32014-02-12 13:46:45 -0800234
235 if (device == NULL) {
Haibo Huangee759ce2021-01-05 21:34:29 -0800236 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "device is NULL");
JP Abgrall511eca32014-02-12 13:46:45 -0800237 return -1;
238 }
239
240 /* In Libpcap, we set pshared by default if NUM_RINGS is set to > 1.
241 * Since libpcap isn't thread-safe */
Elliott Hughesd8845d72015-10-19 18:07:04 -0700242 if ((nr = getenv("SNF_FLAGS")) && *nr)
243 flags = strtol(nr, NULL, 0);
244 else if ((nr = getenv("SNF_NUM_RINGS")) && *nr && atoi(nr) > 1)
245 flags = SNF_F_PSHARED;
JP Abgrall511eca32014-02-12 13:46:45 -0800246 else
247 nr = NULL;
248
Haibo Huang165065a2018-07-23 17:26:52 -0700249
250 /* Allow pcap_set_buffer_size() to set dataring_size.
251 * Default is zero which allows setting from env SNF_DATARING_SIZE.
252 * pcap_set_buffer_size() is in bytes while snf_open() accepts values
253 * between 0 and 1048576 in Megabytes. Values in this range are
254 * mapped to 1MB.
255 */
JP Abgrall511eca32014-02-12 13:46:45 -0800256 err = snf_open(ps->snf_boardnum,
257 0, /* let SNF API parse SNF_NUM_RINGS, if set */
258 NULL, /* default RSS, or use SNF_RSS_FLAGS env */
Haibo Huang165065a2018-07-23 17:26:52 -0700259 (p->opt.buffer_size > 0 && p->opt.buffer_size < 1048576) ? 1048576 : p->opt.buffer_size, /* default to SNF_DATARING_SIZE from env */
JP Abgrall511eca32014-02-12 13:46:45 -0800260 flags, /* may want pshared */
261 &ps->snf_handle);
262 if (err != 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700263 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
264 err, "snf_open failed");
JP Abgrall511eca32014-02-12 13:46:45 -0800265 return -1;
266 }
267
Elliott Hughesd8845d72015-10-19 18:07:04 -0700268 if ((nr = getenv("SNF_PCAP_RING_ID")) && *nr) {
269 ring_id = (int) strtol(nr, NULL, 0);
270 }
271 err = snf_ring_open_id(ps->snf_handle, ring_id, &ps->snf_ring);
JP Abgrall511eca32014-02-12 13:46:45 -0800272 if (err != 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700273 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
274 err, "snf_ring_open_id(ring=%d) failed", ring_id);
JP Abgrall511eca32014-02-12 13:46:45 -0800275 return -1;
276 }
277
Haibo Huang165065a2018-07-23 17:26:52 -0700278 /*
279 * Turn a negative snapshot value (invalid), a snapshot value of
280 * 0 (unspecified), or a value bigger than the normal maximum
281 * value, into the maximum allowed value.
282 *
283 * If some application really *needs* a bigger snapshot
284 * length, we should just increase MAXIMUM_SNAPLEN.
285 */
286 if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
287 p->snapshot = MAXIMUM_SNAPLEN;
288
JP Abgrall511eca32014-02-12 13:46:45 -0800289 if (p->opt.timeout <= 0)
290 ps->snf_timeout = -1;
291 else
292 ps->snf_timeout = p->opt.timeout;
293
294 err = snf_start(ps->snf_handle);
295 if (err != 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700296 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
297 err, "snf_start failed");
JP Abgrall511eca32014-02-12 13:46:45 -0800298 return -1;
299 }
300
301 /*
302 * "select()" and "poll()" don't work on snf descriptors.
303 */
Haibo Huang165065a2018-07-23 17:26:52 -0700304#ifndef _WIN32
JP Abgrall511eca32014-02-12 13:46:45 -0800305 p->selectable_fd = -1;
Haibo Huang165065a2018-07-23 17:26:52 -0700306#endif /* !_WIN32 */
JP Abgrall511eca32014-02-12 13:46:45 -0800307 p->linktype = DLT_EN10MB;
308 p->read_op = snf_read;
309 p->inject_op = snf_inject;
Haibo Huangee759ce2021-01-05 21:34:29 -0800310 p->setfilter_op = install_bpf_program;
JP Abgrall511eca32014-02-12 13:46:45 -0800311 p->setdirection_op = NULL; /* Not implemented.*/
312 p->set_datalink_op = snf_set_datalink;
313 p->getnonblock_op = snf_getnonblock;
314 p->setnonblock_op = snf_setnonblock;
315 p->stats_op = snf_pcap_stats;
316 p->cleanup_op = snf_platform_cleanup;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700317#ifdef SNF_HAVE_INJECT_API
Haibo Huang165065a2018-07-23 17:26:52 -0700318 ps->snf_inj = NULL;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700319#endif
JP Abgrall511eca32014-02-12 13:46:45 -0800320 return 0;
321}
322
Elliott Hughesd8845d72015-10-19 18:07:04 -0700323#define MAX_DESC_LENGTH 128
JP Abgrall511eca32014-02-12 13:46:45 -0800324int
Haibo Huang165065a2018-07-23 17:26:52 -0700325snf_findalldevs(pcap_if_list_t *devlistp, char *errbuf)
JP Abgrall511eca32014-02-12 13:46:45 -0800326{
Haibo Huang165065a2018-07-23 17:26:52 -0700327 pcap_if_t *dev;
328#ifdef _WIN32
329 struct sockaddr_in addr;
330#endif
Elliott Hughesd8845d72015-10-19 18:07:04 -0700331 struct snf_ifaddrs *ifaddrs, *ifa;
Haibo Huang165065a2018-07-23 17:26:52 -0700332 char name[MAX_DESC_LENGTH];
Elliott Hughesd8845d72015-10-19 18:07:04 -0700333 char desc[MAX_DESC_LENGTH];
Haibo Huang165065a2018-07-23 17:26:52 -0700334 int ret, allports = 0, merge = 0;
335 const char *nr = NULL;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700336
Haibo Huang165065a2018-07-23 17:26:52 -0700337 if (snf_init(SNF_VERSION_API)) {
Haibo Huangee759ce2021-01-05 21:34:29 -0800338 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
Haibo Huang165065a2018-07-23 17:26:52 -0700339 "snf_getifaddrs: snf_init failed");
Elliott Hughesd8845d72015-10-19 18:07:04 -0700340 return (-1);
Haibo Huang165065a2018-07-23 17:26:52 -0700341 }
Elliott Hughesd8845d72015-10-19 18:07:04 -0700342
343 if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL)
344 {
Haibo Huang165065a2018-07-23 17:26:52 -0700345 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
346 errno, "snf_getifaddrs");
Elliott Hughesd8845d72015-10-19 18:07:04 -0700347 return (-1);
348 }
Haibo Huang165065a2018-07-23 17:26:52 -0700349 if ((nr = getenv("SNF_FLAGS")) && *nr) {
350 errno = 0;
351 merge = strtol(nr, NULL, 0);
352 if (errno) {
Haibo Huangee759ce2021-01-05 21:34:29 -0800353 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
Haibo Huang165065a2018-07-23 17:26:52 -0700354 "snf_getifaddrs: SNF_FLAGS is not a valid number");
Elliott Hughesd8845d72015-10-19 18:07:04 -0700355 return (-1);
356 }
Haibo Huang165065a2018-07-23 17:26:52 -0700357 merge = merge & SNF_F_AGGREGATE_PORTMASK;
358 }
Elliott Hughesd8845d72015-10-19 18:07:04 -0700359
Haibo Huang165065a2018-07-23 17:26:52 -0700360 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->snf_ifa_next) {
361 /*
362 * Myricom SNF adapter ports may appear as regular
363 * network interfaces, which would already have been
364 * added to the list of adapters by pcap_platform_finddevs()
365 * if this isn't an SNF-only version of libpcap.
366 *
367 * Our create routine intercepts pcap_create() calls for
368 * those interfaces and arranges that they will be
369 * opened using the SNF API instead.
370 *
371 * So if we already have an entry for the device, we
372 * don't add an additional entry for it, we just
373 * update the description for it, if any, to indicate
374 * which snfN device it is. Otherwise, we add an entry
375 * for it.
376 *
377 * In either case, if SNF_F_AGGREGATE_PORTMASK is set
378 * in SNF_FLAGS, we add this port to the bitmask
379 * of ports, which we use to generate a device
380 * we can use to capture on all ports.
381 *
382 * Generate the description string. If port aggregation
383 * is set, use 2^{port number} as the unit number,
384 * rather than {port number}.
385 *
386 * XXX - do entries in this list have IP addresses for
387 * the port? If so, should we add them to the
388 * entry for the device, if they're not already in the
389 * list of IP addresses for the device?
390 */
Haibo Huangee759ce2021-01-05 21:34:29 -0800391 (void)snprintf(desc,MAX_DESC_LENGTH,"Myricom %ssnf%d",
Haibo Huang165065a2018-07-23 17:26:52 -0700392 merge ? "Merge Bitmask Port " : "",
393 merge ? 1 << ifa->snf_ifa_portnum : ifa->snf_ifa_portnum);
394 /*
395 * Add the port to the bitmask.
396 */
397 if (merge)
398 allports |= 1 << ifa->snf_ifa_portnum;
399 /*
400 * See if there's already an entry for the device
401 * with the name ifa->snf_ifa_name.
402 */
403 dev = find_dev(devlistp, ifa->snf_ifa_name);
404 if (dev != NULL) {
405 /*
406 * Yes. Update its description.
407 */
408 char *desc_str;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700409
Haibo Huang165065a2018-07-23 17:26:52 -0700410 desc_str = strdup(desc);
411 if (desc_str == NULL) {
412 pcap_fmt_errmsg_for_errno(errbuf,
413 PCAP_ERRBUF_SIZE, errno,
414 "snf_findalldevs strdup");
415 return -1;
416 }
417 free(dev->description);
418 dev->description = desc_str;
419 } else {
420 /*
421 * No. Add an entry for it.
422 *
423 * XXX - is there a notion of "up" or "running",
424 * and can we determine whether something's
425 * plugged into the adapter and set
426 * PCAP_IF_CONNECTION_STATUS_CONNECTED or
427 * PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
428 */
429 dev = add_dev(devlistp, ifa->snf_ifa_name, 0, desc,
430 errbuf);
431 if (dev == NULL)
432 return -1;
433#ifdef _WIN32
434 /*
435 * On Windows, fill in IP# from device name
436 */
437 ret = inet_pton(AF_INET, dev->name, &addr.sin_addr);
438 if (ret == 1) {
439 /*
440 * Successful conversion of device name
441 * to IPv4 address.
442 */
443 addr.sin_family = AF_INET;
444 if (add_addr_to_dev(dev, &addr, sizeof(addr),
445 NULL, 0, NULL, 0, NULL, 0, errbuf) == -1)
446 return -1;
447 } else if (ret == -1) {
448 /*
449 * Error.
450 */
451 pcap_fmt_errmsg_for_errno(errbuf,
452 PCAP_ERRBUF_SIZE, errno,
453 "sinf_findalldevs inet_pton");
454 return -1;
455 }
456#endif _WIN32
457 }
Elliott Hughesd8845d72015-10-19 18:07:04 -0700458 }
459 snf_freeifaddrs(ifaddrs);
JP Abgrall511eca32014-02-12 13:46:45 -0800460 /*
Haibo Huang165065a2018-07-23 17:26:52 -0700461 * Create a snfX entry if port aggregation is enabled
462 */
463 if (merge) {
464 /*
465 * Add a new entry with all ports bitmask
466 */
Haibo Huangee759ce2021-01-05 21:34:29 -0800467 (void)snprintf(name,MAX_DESC_LENGTH,"snf%d",allports);
468 (void)snprintf(desc,MAX_DESC_LENGTH,"Myricom Merge Bitmask All Ports snf%d",
Haibo Huang165065a2018-07-23 17:26:52 -0700469 allports);
470 /*
471 * XXX - is there any notion of "up" and "running" that
472 * would apply to this device, given that it handles
473 * multiple ports?
474 *
475 * Presumably, there's no notion of "connected" vs.
476 * "disconnected", as "is this plugged into a network?"
477 * would be a per-port property.
478 */
479 if (add_dev(devlistp, name,
480 PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE, desc,
481 errbuf) == NULL)
482 return (-1);
483 /*
484 * XXX - should we give it a list of addresses with all
485 * the addresses for all the ports?
486 */
487 }
488
JP Abgrall511eca32014-02-12 13:46:45 -0800489 return 0;
490}
491
492pcap_t *
493snf_create(const char *device, char *ebuf, int *is_ours)
494{
495 pcap_t *p;
496 int boardnum = -1;
497 struct snf_ifaddrs *ifaddrs, *ifa;
498 size_t devlen;
499 struct pcap_snf *ps;
500
501 if (snf_init(SNF_VERSION_API)) {
502 /* Can't initialize the API, so no SNF devices */
503 *is_ours = 0;
504 return NULL;
505 }
506
507 /*
508 * Match a given interface name to our list of interface names, from
509 * which we can obtain the intended board number
510 */
511 if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL) {
512 /* Can't get SNF addresses */
513 *is_ours = 0;
514 return NULL;
515 }
516 devlen = strlen(device) + 1;
517 ifa = ifaddrs;
518 while (ifa) {
Haibo Huang165065a2018-07-23 17:26:52 -0700519 if (strncmp(device, ifa->snf_ifa_name, devlen) == 0) {
JP Abgrall511eca32014-02-12 13:46:45 -0800520 boardnum = ifa->snf_ifa_boardnum;
521 break;
522 }
523 ifa = ifa->snf_ifa_next;
524 }
525 snf_freeifaddrs(ifaddrs);
526
527 if (ifa == NULL) {
528 /*
529 * If we can't find the device by name, support the name "snfX"
530 * and "snf10gX" where X is the board number.
531 */
532 if (sscanf(device, "snf10g%d", &boardnum) != 1 &&
533 sscanf(device, "snf%d", &boardnum) != 1) {
534 /* Nope, not a supported name */
535 *is_ours = 0;
536 return NULL;
Haibo Huang165065a2018-07-23 17:26:52 -0700537 }
JP Abgrall511eca32014-02-12 13:46:45 -0800538 }
539
540 /* OK, it's probably ours. */
541 *is_ours = 1;
542
Haibo Huangee759ce2021-01-05 21:34:29 -0800543 p = PCAP_CREATE_COMMON(ebuf, struct pcap_snf);
JP Abgrall511eca32014-02-12 13:46:45 -0800544 if (p == NULL)
545 return NULL;
546 ps = p->priv;
547
Elliott Hughesd8845d72015-10-19 18:07:04 -0700548 /*
549 * We support microsecond and nanosecond time stamps.
550 */
Elliott Hughesd8845d72015-10-19 18:07:04 -0700551 p->tstamp_precision_list = malloc(2 * sizeof(u_int));
552 if (p->tstamp_precision_list == NULL) {
Haibo Huang165065a2018-07-23 17:26:52 -0700553 pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno,
554 "malloc");
Elliott Hughes965a4b52017-05-15 10:37:39 -0700555 pcap_close(p);
Elliott Hughesd8845d72015-10-19 18:07:04 -0700556 return NULL;
557 }
558 p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO;
559 p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO;
Haibo Huangee759ce2021-01-05 21:34:29 -0800560 p->tstamp_precision_count = 2;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700561
JP Abgrall511eca32014-02-12 13:46:45 -0800562 p->activate_op = snf_activate;
563 ps->snf_boardnum = boardnum;
564 return p;
565}
Elliott Hughes965a4b52017-05-15 10:37:39 -0700566
567#ifdef SNF_ONLY
568/*
569 * This libpcap build supports only SNF cards, not regular network
570 * interfaces..
571 */
572
573/*
Haibo Huang165065a2018-07-23 17:26:52 -0700574 * There are no regular interfaces, just SNF interfaces.
Elliott Hughes965a4b52017-05-15 10:37:39 -0700575 */
576int
Haibo Huang165065a2018-07-23 17:26:52 -0700577pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
Elliott Hughes965a4b52017-05-15 10:37:39 -0700578{
Elliott Hughes965a4b52017-05-15 10:37:39 -0700579 return (0);
580}
581
582/*
583 * Attempts to open a regular interface fail.
584 */
585pcap_t *
586pcap_create_interface(const char *device, char *errbuf)
587{
Haibo Huangee759ce2021-01-05 21:34:29 -0800588 snprintf(errbuf, PCAP_ERRBUF_SIZE,
Elliott Hughes965a4b52017-05-15 10:37:39 -0700589 "This version of libpcap only supports SNF cards");
590 return NULL;
591}
Haibo Huang165065a2018-07-23 17:26:52 -0700592
593/*
594 * Libpcap version string.
595 */
596const char *
597pcap_lib_version(void)
598{
599 return (PCAP_VERSION_STRING " (SNF-only)");
600}
Elliott Hughes965a4b52017-05-15 10:37:39 -0700601#endif