JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1 | #ifdef HAVE_CONFIG_H |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2 | #include <config.h> |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3 | #endif |
| 4 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 5 | #ifndef _WIN32 |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 6 | #include <sys/param.h> |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 7 | #endif /* !_WIN32 */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include <errno.h> |
| 12 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 13 | #ifndef _WIN32 |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 14 | #include <netinet/in.h> |
| 15 | #include <sys/mman.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <unistd.h> |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 19 | #endif /* !_WIN32 */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 20 | |
| 21 | #include <snf.h> |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 22 | #if SNF_VERSION_API >= 0x0003 |
| 23 | #define SNF_HAVE_INJECT_API |
| 24 | #endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 25 | |
| 26 | #include "pcap-int.h" |
| 27 | #include "pcap-snf.h" |
| 28 | |
| 29 | /* |
| 30 | * Private data for capturing on SNF devices. |
| 31 | */ |
| 32 | struct pcap_snf { |
| 33 | snf_handle_t snf_handle; /* opaque device handle */ |
| 34 | snf_ring_t snf_ring; /* opaque device ring handle */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 35 | #ifdef SNF_HAVE_INJECT_API |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 36 | snf_inject_t snf_inj; /* inject handle, if inject is used */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 37 | #endif |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 38 | int snf_timeout; |
| 39 | int snf_boardnum; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static int |
| 43 | snf_set_datalink(pcap_t *p, int dlt) |
| 44 | { |
| 45 | p->linktype = dlt; |
| 46 | return (0); |
| 47 | } |
| 48 | |
| 49 | static int |
| 50 | snf_pcap_stats(pcap_t *p, struct pcap_stat *ps) |
| 51 | { |
| 52 | struct snf_ring_stats stats; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 53 | struct pcap_snf *snfps = p->priv; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 54 | int rc; |
| 55 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 56 | if ((rc = snf_ring_getstats(snfps->snf_ring, &stats))) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 57 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 58 | rc, "snf_get_stats"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 59 | 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 | |
| 67 | static void |
| 68 | snf_platform_cleanup(pcap_t *p) |
| 69 | { |
| 70 | struct pcap_snf *ps = p->priv; |
| 71 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 72 | #ifdef SNF_HAVE_INJECT_API |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 73 | if (ps->snf_inj) |
| 74 | snf_inject_close(ps->snf_inj); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 75 | #endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 76 | snf_ring_close(ps->snf_ring); |
| 77 | snf_close(ps->snf_handle); |
| 78 | pcap_cleanup_live_common(p); |
| 79 | } |
| 80 | |
| 81 | static int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 82 | snf_getnonblock(pcap_t *p) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 83 | { |
| 84 | struct pcap_snf *ps = p->priv; |
| 85 | |
| 86 | return (ps->snf_timeout == 0); |
| 87 | } |
| 88 | |
| 89 | static int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 90 | snf_setnonblock(pcap_t *p, int nonblock) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 91 | { |
| 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 | |
| 107 | static inline |
| 108 | struct timeval |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 109 | snf_timestamp_to_timeval(const int64_t ts_nanosec, const int tstamp_precision) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 110 | { |
| 111 | struct timeval tv; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 112 | long tv_nsec; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 113 | const static struct timeval zero_timeval; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 114 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 115 | if (ts_nanosec == 0) |
| 116 | return zero_timeval; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 117 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 118 | tv.tv_sec = ts_nanosec / _NSEC_PER_SEC; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 119 | 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 Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 127 | return tv; |
| 128 | } |
| 129 | |
| 130 | static int |
| 131 | snf_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 Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 137 | int nonblock, timeout; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 138 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 139 | if (!p) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 140 | return -1; |
| 141 | |
| 142 | n = 0; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 143 | timeout = ps->snf_timeout; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 144 | 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 Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 157 | err = snf_ring_recv(ps->snf_ring, timeout, &req); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 158 | |
| 159 | if (err) { |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 160 | if (err == EBUSY || err == EAGAIN) { |
| 161 | return (n); |
| 162 | } |
| 163 | else if (err == EINTR) { |
| 164 | timeout = 0; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 165 | continue; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 166 | } |
| 167 | else { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 168 | pcap_fmt_errmsg_for_errno(p->errbuf, |
| 169 | PCAP_ERRBUF_SIZE, err, "snf_read"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 170 | 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 Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 179 | pcap_filter(p->fcode.bf_insns, req.pkt_addr, req.length, caplen)) { |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 180 | hdr.ts = snf_timestamp_to_timeval(req.timestamp, p->opt.tstamp_precision); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 181 | hdr.caplen = caplen; |
| 182 | hdr.len = req.length; |
| 183 | callback(user, &hdr, req.pkt_addr); |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 184 | n++; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 185 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 186 | |
| 187 | /* After one successful packet is received, we won't block |
| 188 | * again for that timeout. */ |
| 189 | if (timeout != 0) |
| 190 | timeout = 0; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 191 | } |
| 192 | return (n); |
| 193 | } |
| 194 | |
| 195 | static int |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 196 | snf_inject(pcap_t *p, const void *buf _U_, int size _U_) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 197 | { |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 198 | #ifdef SNF_HAVE_INJECT_API |
| 199 | struct pcap_snf *ps = p->priv; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 200 | 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 Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 209 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 210 | 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 Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 219 | #else |
Haibo Huang | 4ccd683 | 2020-04-23 18:03:48 -0700 | [diff] [blame] | 220 | pcap_strlcpy(p->errbuf, "Sending packets isn't supported with this snf version", |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 221 | PCAP_ERRBUF_SIZE); |
| 222 | return (-1); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 223 | #endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static int |
| 227 | snf_activate(pcap_t* p) |
| 228 | { |
| 229 | struct pcap_snf *ps = p->priv; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 230 | char *device = p->opt.device; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 231 | const char *nr = NULL; |
| 232 | int err; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 233 | int flags = -1, ring_id = -1; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 234 | |
| 235 | if (device == NULL) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 236 | snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "device is NULL"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 237 | 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 Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 242 | 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 Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 246 | else |
| 247 | nr = NULL; |
| 248 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 249 | |
| 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 Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 256 | 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 Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 259 | (p->opt.buffer_size > 0 && p->opt.buffer_size < 1048576) ? 1048576 : p->opt.buffer_size, /* default to SNF_DATARING_SIZE from env */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 260 | flags, /* may want pshared */ |
| 261 | &ps->snf_handle); |
| 262 | if (err != 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 263 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 264 | err, "snf_open failed"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 265 | return -1; |
| 266 | } |
| 267 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 268 | 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 Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 272 | if (err != 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 273 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 274 | err, "snf_ring_open_id(ring=%d) failed", ring_id); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 275 | return -1; |
| 276 | } |
| 277 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 278 | /* |
| 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 Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 289 | 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 Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 296 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 297 | err, "snf_start failed"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 298 | return -1; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * "select()" and "poll()" don't work on snf descriptors. |
| 303 | */ |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 304 | #ifndef _WIN32 |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 305 | p->selectable_fd = -1; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 306 | #endif /* !_WIN32 */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 307 | p->linktype = DLT_EN10MB; |
| 308 | p->read_op = snf_read; |
| 309 | p->inject_op = snf_inject; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 310 | p->setfilter_op = install_bpf_program; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 311 | 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 Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 317 | #ifdef SNF_HAVE_INJECT_API |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 318 | ps->snf_inj = NULL; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 319 | #endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 323 | #define MAX_DESC_LENGTH 128 |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 324 | int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 325 | snf_findalldevs(pcap_if_list_t *devlistp, char *errbuf) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 326 | { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 327 | pcap_if_t *dev; |
| 328 | #ifdef _WIN32 |
| 329 | struct sockaddr_in addr; |
| 330 | #endif |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 331 | struct snf_ifaddrs *ifaddrs, *ifa; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 332 | char name[MAX_DESC_LENGTH]; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 333 | char desc[MAX_DESC_LENGTH]; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 334 | int ret, allports = 0, merge = 0; |
| 335 | const char *nr = NULL; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 336 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 337 | if (snf_init(SNF_VERSION_API)) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 338 | (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 339 | "snf_getifaddrs: snf_init failed"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 340 | return (-1); |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 341 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 342 | |
| 343 | if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL) |
| 344 | { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 345 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
| 346 | errno, "snf_getifaddrs"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 347 | return (-1); |
| 348 | } |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 349 | if ((nr = getenv("SNF_FLAGS")) && *nr) { |
| 350 | errno = 0; |
| 351 | merge = strtol(nr, NULL, 0); |
| 352 | if (errno) { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 353 | (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 354 | "snf_getifaddrs: SNF_FLAGS is not a valid number"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 355 | return (-1); |
| 356 | } |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 357 | merge = merge & SNF_F_AGGREGATE_PORTMASK; |
| 358 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 359 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 360 | 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 Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 391 | (void)snprintf(desc,MAX_DESC_LENGTH,"Myricom %ssnf%d", |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 392 | 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 Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 409 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 410 | 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 Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 458 | } |
| 459 | snf_freeifaddrs(ifaddrs); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 460 | /* |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 461 | * 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 Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 467 | (void)snprintf(name,MAX_DESC_LENGTH,"snf%d",allports); |
| 468 | (void)snprintf(desc,MAX_DESC_LENGTH,"Myricom Merge Bitmask All Ports snf%d", |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 469 | 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 Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | pcap_t * |
| 493 | snf_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 Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 519 | if (strncmp(device, ifa->snf_ifa_name, devlen) == 0) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 520 | 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 Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 537 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /* OK, it's probably ours. */ |
| 541 | *is_ours = 1; |
| 542 | |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 543 | p = PCAP_CREATE_COMMON(ebuf, struct pcap_snf); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 544 | if (p == NULL) |
| 545 | return NULL; |
| 546 | ps = p->priv; |
| 547 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 548 | /* |
| 549 | * We support microsecond and nanosecond time stamps. |
| 550 | */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 551 | p->tstamp_precision_list = malloc(2 * sizeof(u_int)); |
| 552 | if (p->tstamp_precision_list == NULL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 553 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno, |
| 554 | "malloc"); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 555 | pcap_close(p); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 556 | return NULL; |
| 557 | } |
| 558 | p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO; |
| 559 | p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO; |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 560 | p->tstamp_precision_count = 2; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 561 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 562 | p->activate_op = snf_activate; |
| 563 | ps->snf_boardnum = boardnum; |
| 564 | return p; |
| 565 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 566 | |
| 567 | #ifdef SNF_ONLY |
| 568 | /* |
| 569 | * This libpcap build supports only SNF cards, not regular network |
| 570 | * interfaces.. |
| 571 | */ |
| 572 | |
| 573 | /* |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 574 | * There are no regular interfaces, just SNF interfaces. |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 575 | */ |
| 576 | int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 577 | pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 578 | { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 579 | return (0); |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Attempts to open a regular interface fail. |
| 584 | */ |
| 585 | pcap_t * |
| 586 | pcap_create_interface(const char *device, char *errbuf) |
| 587 | { |
Haibo Huang | ee759ce | 2021-01-05 21:34:29 -0800 | [diff] [blame] | 588 | snprintf(errbuf, PCAP_ERRBUF_SIZE, |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 589 | "This version of libpcap only supports SNF cards"); |
| 590 | return NULL; |
| 591 | } |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 592 | |
| 593 | /* |
| 594 | * Libpcap version string. |
| 595 | */ |
| 596 | const char * |
| 597 | pcap_lib_version(void) |
| 598 | { |
| 599 | return (PCAP_VERSION_STRING " (SNF-only)"); |
| 600 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 601 | #endif |