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 | |
| 13 | #include <ctype.h> |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 14 | #ifndef _WIN32 |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 15 | #include <netinet/in.h> |
| 16 | #include <sys/mman.h> |
| 17 | #include <sys/socket.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <unistd.h> |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 20 | #endif /* !_WIN32 */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 21 | |
| 22 | #include <snf.h> |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 23 | #if SNF_VERSION_API >= 0x0003 |
| 24 | #define SNF_HAVE_INJECT_API |
| 25 | #endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 26 | |
| 27 | #include "pcap-int.h" |
| 28 | #include "pcap-snf.h" |
| 29 | |
| 30 | /* |
| 31 | * Private data for capturing on SNF devices. |
| 32 | */ |
| 33 | struct pcap_snf { |
| 34 | snf_handle_t snf_handle; /* opaque device handle */ |
| 35 | snf_ring_t snf_ring; /* opaque device ring handle */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 36 | #ifdef SNF_HAVE_INJECT_API |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 37 | snf_inject_t snf_inj; /* inject handle, if inject is used */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 38 | #endif |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 39 | int snf_timeout; |
| 40 | int snf_boardnum; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static int |
| 44 | snf_set_datalink(pcap_t *p, int dlt) |
| 45 | { |
| 46 | p->linktype = dlt; |
| 47 | return (0); |
| 48 | } |
| 49 | |
| 50 | static int |
| 51 | snf_pcap_stats(pcap_t *p, struct pcap_stat *ps) |
| 52 | { |
| 53 | struct snf_ring_stats stats; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 54 | struct pcap_snf *snfps = p->priv; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 55 | int rc; |
| 56 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 57 | if ((rc = snf_ring_getstats(snfps->snf_ring, &stats))) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 58 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 59 | rc, "snf_get_stats"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 60 | return -1; |
| 61 | } |
| 62 | ps->ps_recv = stats.ring_pkt_recv + stats.ring_pkt_overflow; |
| 63 | ps->ps_drop = stats.ring_pkt_overflow; |
| 64 | ps->ps_ifdrop = stats.nic_pkt_overflow + stats.nic_pkt_bad; |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static void |
| 69 | snf_platform_cleanup(pcap_t *p) |
| 70 | { |
| 71 | struct pcap_snf *ps = p->priv; |
| 72 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 73 | #ifdef SNF_HAVE_INJECT_API |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 74 | if (ps->snf_inj) |
| 75 | snf_inject_close(ps->snf_inj); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 76 | #endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 77 | snf_ring_close(ps->snf_ring); |
| 78 | snf_close(ps->snf_handle); |
| 79 | pcap_cleanup_live_common(p); |
| 80 | } |
| 81 | |
| 82 | static int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 83 | snf_getnonblock(pcap_t *p) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 84 | { |
| 85 | struct pcap_snf *ps = p->priv; |
| 86 | |
| 87 | return (ps->snf_timeout == 0); |
| 88 | } |
| 89 | |
| 90 | static int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 91 | snf_setnonblock(pcap_t *p, int nonblock) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 92 | { |
| 93 | struct pcap_snf *ps = p->priv; |
| 94 | |
| 95 | if (nonblock) |
| 96 | ps->snf_timeout = 0; |
| 97 | else { |
| 98 | if (p->opt.timeout <= 0) |
| 99 | ps->snf_timeout = -1; /* forever */ |
| 100 | else |
| 101 | ps->snf_timeout = p->opt.timeout; |
| 102 | } |
| 103 | return (0); |
| 104 | } |
| 105 | |
| 106 | #define _NSEC_PER_SEC 1000000000 |
| 107 | |
| 108 | static inline |
| 109 | struct timeval |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 110 | 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] | 111 | { |
| 112 | struct timeval tv; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 113 | long tv_nsec; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 114 | const static struct timeval zero_timeval; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 115 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 116 | if (ts_nanosec == 0) |
| 117 | return zero_timeval; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 118 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 119 | tv.tv_sec = ts_nanosec / _NSEC_PER_SEC; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 120 | tv_nsec = (ts_nanosec % _NSEC_PER_SEC); |
| 121 | |
| 122 | /* libpcap expects tv_usec to be nanos if using nanosecond precision. */ |
| 123 | if (tstamp_precision == PCAP_TSTAMP_PRECISION_NANO) |
| 124 | tv.tv_usec = tv_nsec; |
| 125 | else |
| 126 | tv.tv_usec = tv_nsec / 1000; |
| 127 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 128 | return tv; |
| 129 | } |
| 130 | |
| 131 | static int |
| 132 | snf_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) |
| 133 | { |
| 134 | struct pcap_snf *ps = p->priv; |
| 135 | struct pcap_pkthdr hdr; |
| 136 | int i, flags, err, caplen, n; |
| 137 | struct snf_recv_req req; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 138 | int nonblock, timeout; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 139 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 140 | if (!p) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 141 | return -1; |
| 142 | |
| 143 | n = 0; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 144 | timeout = ps->snf_timeout; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 145 | while (n < cnt || PACKET_COUNT_IS_UNLIMITED(cnt)) { |
| 146 | /* |
| 147 | * Has "pcap_breakloop()" been called? |
| 148 | */ |
| 149 | if (p->break_loop) { |
| 150 | if (n == 0) { |
| 151 | p->break_loop = 0; |
| 152 | return (-2); |
| 153 | } else { |
| 154 | return (n); |
| 155 | } |
| 156 | } |
| 157 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 158 | err = snf_ring_recv(ps->snf_ring, timeout, &req); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 159 | |
| 160 | if (err) { |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 161 | if (err == EBUSY || err == EAGAIN) { |
| 162 | return (n); |
| 163 | } |
| 164 | else if (err == EINTR) { |
| 165 | timeout = 0; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 166 | continue; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 167 | } |
| 168 | else { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 169 | pcap_fmt_errmsg_for_errno(p->errbuf, |
| 170 | PCAP_ERRBUF_SIZE, err, "snf_read"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 171 | return -1; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | caplen = req.length; |
| 176 | if (caplen > p->snapshot) |
| 177 | caplen = p->snapshot; |
| 178 | |
| 179 | if ((p->fcode.bf_insns == NULL) || |
| 180 | bpf_filter(p->fcode.bf_insns, req.pkt_addr, req.length, caplen)) { |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 181 | hdr.ts = snf_timestamp_to_timeval(req.timestamp, p->opt.tstamp_precision); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 182 | hdr.caplen = caplen; |
| 183 | hdr.len = req.length; |
| 184 | callback(user, &hdr, req.pkt_addr); |
| 185 | } |
| 186 | n++; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 187 | |
| 188 | /* After one successful packet is received, we won't block |
| 189 | * again for that timeout. */ |
| 190 | if (timeout != 0) |
| 191 | timeout = 0; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 192 | } |
| 193 | return (n); |
| 194 | } |
| 195 | |
| 196 | static int |
| 197 | snf_setfilter(pcap_t *p, struct bpf_program *fp) |
| 198 | { |
| 199 | if (!p) |
| 200 | return -1; |
| 201 | if (!fp) { |
| 202 | strncpy(p->errbuf, "setfilter: No filter specified", |
| 203 | sizeof(p->errbuf)); |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | /* Make our private copy of the filter */ |
| 208 | |
| 209 | if (install_bpf_program(p, fp) < 0) |
| 210 | return -1; |
| 211 | |
| 212 | return (0); |
| 213 | } |
| 214 | |
| 215 | static int |
| 216 | snf_inject(pcap_t *p, const void *buf _U_, size_t size _U_) |
| 217 | { |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 218 | #ifdef SNF_HAVE_INJECT_API |
| 219 | struct pcap_snf *ps = p->priv; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 220 | int rc; |
| 221 | if (ps->snf_inj == NULL) { |
| 222 | rc = snf_inject_open(ps->snf_boardnum, 0, &ps->snf_inj); |
| 223 | if (rc) { |
| 224 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 225 | rc, "snf_inject_open"); |
| 226 | return (-1); |
| 227 | } |
| 228 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 229 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 230 | rc = snf_inject_send(ps->snf_inj, -1, 0, buf, size); |
| 231 | if (!rc) { |
| 232 | return (size); |
| 233 | } |
| 234 | else { |
| 235 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 236 | rc, "snf_inject_send"); |
| 237 | return (-1); |
| 238 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 239 | #else |
| 240 | strlcpy(p->errbuf, "Sending packets isn't supported with this snf version", |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 241 | PCAP_ERRBUF_SIZE); |
| 242 | return (-1); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 243 | #endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static int |
| 247 | snf_activate(pcap_t* p) |
| 248 | { |
| 249 | struct pcap_snf *ps = p->priv; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 250 | char *device = p->opt.device; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 251 | const char *nr = NULL; |
| 252 | int err; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 253 | int flags = -1, ring_id = -1; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 254 | |
| 255 | if (device == NULL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 256 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "device is NULL"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 257 | return -1; |
| 258 | } |
| 259 | |
| 260 | /* In Libpcap, we set pshared by default if NUM_RINGS is set to > 1. |
| 261 | * Since libpcap isn't thread-safe */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 262 | if ((nr = getenv("SNF_FLAGS")) && *nr) |
| 263 | flags = strtol(nr, NULL, 0); |
| 264 | else if ((nr = getenv("SNF_NUM_RINGS")) && *nr && atoi(nr) > 1) |
| 265 | flags = SNF_F_PSHARED; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 266 | else |
| 267 | nr = NULL; |
| 268 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 269 | |
| 270 | /* Allow pcap_set_buffer_size() to set dataring_size. |
| 271 | * Default is zero which allows setting from env SNF_DATARING_SIZE. |
| 272 | * pcap_set_buffer_size() is in bytes while snf_open() accepts values |
| 273 | * between 0 and 1048576 in Megabytes. Values in this range are |
| 274 | * mapped to 1MB. |
| 275 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 276 | err = snf_open(ps->snf_boardnum, |
| 277 | 0, /* let SNF API parse SNF_NUM_RINGS, if set */ |
| 278 | NULL, /* default RSS, or use SNF_RSS_FLAGS env */ |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 279 | (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] | 280 | flags, /* may want pshared */ |
| 281 | &ps->snf_handle); |
| 282 | if (err != 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 283 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 284 | err, "snf_open failed"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 285 | return -1; |
| 286 | } |
| 287 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 288 | if ((nr = getenv("SNF_PCAP_RING_ID")) && *nr) { |
| 289 | ring_id = (int) strtol(nr, NULL, 0); |
| 290 | } |
| 291 | 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] | 292 | if (err != 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 293 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 294 | err, "snf_ring_open_id(ring=%d) failed", ring_id); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 295 | return -1; |
| 296 | } |
| 297 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 298 | /* |
| 299 | * Turn a negative snapshot value (invalid), a snapshot value of |
| 300 | * 0 (unspecified), or a value bigger than the normal maximum |
| 301 | * value, into the maximum allowed value. |
| 302 | * |
| 303 | * If some application really *needs* a bigger snapshot |
| 304 | * length, we should just increase MAXIMUM_SNAPLEN. |
| 305 | */ |
| 306 | if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN) |
| 307 | p->snapshot = MAXIMUM_SNAPLEN; |
| 308 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 309 | if (p->opt.timeout <= 0) |
| 310 | ps->snf_timeout = -1; |
| 311 | else |
| 312 | ps->snf_timeout = p->opt.timeout; |
| 313 | |
| 314 | err = snf_start(ps->snf_handle); |
| 315 | if (err != 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 316 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 317 | err, "snf_start failed"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 318 | return -1; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * "select()" and "poll()" don't work on snf descriptors. |
| 323 | */ |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 324 | #ifndef _WIN32 |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 325 | p->selectable_fd = -1; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 326 | #endif /* !_WIN32 */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 327 | p->linktype = DLT_EN10MB; |
| 328 | p->read_op = snf_read; |
| 329 | p->inject_op = snf_inject; |
| 330 | p->setfilter_op = snf_setfilter; |
| 331 | p->setdirection_op = NULL; /* Not implemented.*/ |
| 332 | p->set_datalink_op = snf_set_datalink; |
| 333 | p->getnonblock_op = snf_getnonblock; |
| 334 | p->setnonblock_op = snf_setnonblock; |
| 335 | p->stats_op = snf_pcap_stats; |
| 336 | p->cleanup_op = snf_platform_cleanup; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 337 | #ifdef SNF_HAVE_INJECT_API |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 338 | ps->snf_inj = NULL; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 339 | #endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 343 | #define MAX_DESC_LENGTH 128 |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 344 | int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 345 | snf_findalldevs(pcap_if_list_t *devlistp, char *errbuf) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 346 | { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 347 | pcap_if_t *dev; |
| 348 | #ifdef _WIN32 |
| 349 | struct sockaddr_in addr; |
| 350 | #endif |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 351 | struct snf_ifaddrs *ifaddrs, *ifa; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 352 | char name[MAX_DESC_LENGTH]; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 353 | char desc[MAX_DESC_LENGTH]; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 354 | int ret, allports = 0, merge = 0; |
| 355 | const char *nr = NULL; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 356 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 357 | if (snf_init(SNF_VERSION_API)) { |
| 358 | (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
| 359 | "snf_getifaddrs: snf_init failed"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 360 | return (-1); |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 361 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 362 | |
| 363 | if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL) |
| 364 | { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 365 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
| 366 | errno, "snf_getifaddrs"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 367 | return (-1); |
| 368 | } |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 369 | if ((nr = getenv("SNF_FLAGS")) && *nr) { |
| 370 | errno = 0; |
| 371 | merge = strtol(nr, NULL, 0); |
| 372 | if (errno) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 373 | (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 374 | "snf_getifaddrs: SNF_FLAGS is not a valid number"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 375 | return (-1); |
| 376 | } |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 377 | merge = merge & SNF_F_AGGREGATE_PORTMASK; |
| 378 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 379 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 380 | for (ifa = ifaddrs; ifa != NULL; ifa = ifa->snf_ifa_next) { |
| 381 | /* |
| 382 | * Myricom SNF adapter ports may appear as regular |
| 383 | * network interfaces, which would already have been |
| 384 | * added to the list of adapters by pcap_platform_finddevs() |
| 385 | * if this isn't an SNF-only version of libpcap. |
| 386 | * |
| 387 | * Our create routine intercepts pcap_create() calls for |
| 388 | * those interfaces and arranges that they will be |
| 389 | * opened using the SNF API instead. |
| 390 | * |
| 391 | * So if we already have an entry for the device, we |
| 392 | * don't add an additional entry for it, we just |
| 393 | * update the description for it, if any, to indicate |
| 394 | * which snfN device it is. Otherwise, we add an entry |
| 395 | * for it. |
| 396 | * |
| 397 | * In either case, if SNF_F_AGGREGATE_PORTMASK is set |
| 398 | * in SNF_FLAGS, we add this port to the bitmask |
| 399 | * of ports, which we use to generate a device |
| 400 | * we can use to capture on all ports. |
| 401 | * |
| 402 | * Generate the description string. If port aggregation |
| 403 | * is set, use 2^{port number} as the unit number, |
| 404 | * rather than {port number}. |
| 405 | * |
| 406 | * XXX - do entries in this list have IP addresses for |
| 407 | * the port? If so, should we add them to the |
| 408 | * entry for the device, if they're not already in the |
| 409 | * list of IP addresses for the device? |
| 410 | */ |
| 411 | (void)pcap_snprintf(desc,MAX_DESC_LENGTH,"Myricom %ssnf%d", |
| 412 | merge ? "Merge Bitmask Port " : "", |
| 413 | merge ? 1 << ifa->snf_ifa_portnum : ifa->snf_ifa_portnum); |
| 414 | /* |
| 415 | * Add the port to the bitmask. |
| 416 | */ |
| 417 | if (merge) |
| 418 | allports |= 1 << ifa->snf_ifa_portnum; |
| 419 | /* |
| 420 | * See if there's already an entry for the device |
| 421 | * with the name ifa->snf_ifa_name. |
| 422 | */ |
| 423 | dev = find_dev(devlistp, ifa->snf_ifa_name); |
| 424 | if (dev != NULL) { |
| 425 | /* |
| 426 | * Yes. Update its description. |
| 427 | */ |
| 428 | char *desc_str; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 429 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 430 | desc_str = strdup(desc); |
| 431 | if (desc_str == NULL) { |
| 432 | pcap_fmt_errmsg_for_errno(errbuf, |
| 433 | PCAP_ERRBUF_SIZE, errno, |
| 434 | "snf_findalldevs strdup"); |
| 435 | return -1; |
| 436 | } |
| 437 | free(dev->description); |
| 438 | dev->description = desc_str; |
| 439 | } else { |
| 440 | /* |
| 441 | * No. Add an entry for it. |
| 442 | * |
| 443 | * XXX - is there a notion of "up" or "running", |
| 444 | * and can we determine whether something's |
| 445 | * plugged into the adapter and set |
| 446 | * PCAP_IF_CONNECTION_STATUS_CONNECTED or |
| 447 | * PCAP_IF_CONNECTION_STATUS_DISCONNECTED? |
| 448 | */ |
| 449 | dev = add_dev(devlistp, ifa->snf_ifa_name, 0, desc, |
| 450 | errbuf); |
| 451 | if (dev == NULL) |
| 452 | return -1; |
| 453 | #ifdef _WIN32 |
| 454 | /* |
| 455 | * On Windows, fill in IP# from device name |
| 456 | */ |
| 457 | ret = inet_pton(AF_INET, dev->name, &addr.sin_addr); |
| 458 | if (ret == 1) { |
| 459 | /* |
| 460 | * Successful conversion of device name |
| 461 | * to IPv4 address. |
| 462 | */ |
| 463 | addr.sin_family = AF_INET; |
| 464 | if (add_addr_to_dev(dev, &addr, sizeof(addr), |
| 465 | NULL, 0, NULL, 0, NULL, 0, errbuf) == -1) |
| 466 | return -1; |
| 467 | } else if (ret == -1) { |
| 468 | /* |
| 469 | * Error. |
| 470 | */ |
| 471 | pcap_fmt_errmsg_for_errno(errbuf, |
| 472 | PCAP_ERRBUF_SIZE, errno, |
| 473 | "sinf_findalldevs inet_pton"); |
| 474 | return -1; |
| 475 | } |
| 476 | #endif _WIN32 |
| 477 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 478 | } |
| 479 | snf_freeifaddrs(ifaddrs); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 480 | /* |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 481 | * Create a snfX entry if port aggregation is enabled |
| 482 | */ |
| 483 | if (merge) { |
| 484 | /* |
| 485 | * Add a new entry with all ports bitmask |
| 486 | */ |
| 487 | (void)pcap_snprintf(name,MAX_DESC_LENGTH,"snf%d",allports); |
| 488 | (void)pcap_snprintf(desc,MAX_DESC_LENGTH,"Myricom Merge Bitmask All Ports snf%d", |
| 489 | allports); |
| 490 | /* |
| 491 | * XXX - is there any notion of "up" and "running" that |
| 492 | * would apply to this device, given that it handles |
| 493 | * multiple ports? |
| 494 | * |
| 495 | * Presumably, there's no notion of "connected" vs. |
| 496 | * "disconnected", as "is this plugged into a network?" |
| 497 | * would be a per-port property. |
| 498 | */ |
| 499 | if (add_dev(devlistp, name, |
| 500 | PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE, desc, |
| 501 | errbuf) == NULL) |
| 502 | return (-1); |
| 503 | /* |
| 504 | * XXX - should we give it a list of addresses with all |
| 505 | * the addresses for all the ports? |
| 506 | */ |
| 507 | } |
| 508 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | pcap_t * |
| 513 | snf_create(const char *device, char *ebuf, int *is_ours) |
| 514 | { |
| 515 | pcap_t *p; |
| 516 | int boardnum = -1; |
| 517 | struct snf_ifaddrs *ifaddrs, *ifa; |
| 518 | size_t devlen; |
| 519 | struct pcap_snf *ps; |
| 520 | |
| 521 | if (snf_init(SNF_VERSION_API)) { |
| 522 | /* Can't initialize the API, so no SNF devices */ |
| 523 | *is_ours = 0; |
| 524 | return NULL; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Match a given interface name to our list of interface names, from |
| 529 | * which we can obtain the intended board number |
| 530 | */ |
| 531 | if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL) { |
| 532 | /* Can't get SNF addresses */ |
| 533 | *is_ours = 0; |
| 534 | return NULL; |
| 535 | } |
| 536 | devlen = strlen(device) + 1; |
| 537 | ifa = ifaddrs; |
| 538 | while (ifa) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 539 | if (strncmp(device, ifa->snf_ifa_name, devlen) == 0) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 540 | boardnum = ifa->snf_ifa_boardnum; |
| 541 | break; |
| 542 | } |
| 543 | ifa = ifa->snf_ifa_next; |
| 544 | } |
| 545 | snf_freeifaddrs(ifaddrs); |
| 546 | |
| 547 | if (ifa == NULL) { |
| 548 | /* |
| 549 | * If we can't find the device by name, support the name "snfX" |
| 550 | * and "snf10gX" where X is the board number. |
| 551 | */ |
| 552 | if (sscanf(device, "snf10g%d", &boardnum) != 1 && |
| 553 | sscanf(device, "snf%d", &boardnum) != 1) { |
| 554 | /* Nope, not a supported name */ |
| 555 | *is_ours = 0; |
| 556 | return NULL; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 557 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /* OK, it's probably ours. */ |
| 561 | *is_ours = 1; |
| 562 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 563 | p = pcap_create_common(ebuf, sizeof (struct pcap_snf)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 564 | if (p == NULL) |
| 565 | return NULL; |
| 566 | ps = p->priv; |
| 567 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 568 | /* |
| 569 | * We support microsecond and nanosecond time stamps. |
| 570 | */ |
| 571 | p->tstamp_precision_count = 2; |
| 572 | p->tstamp_precision_list = malloc(2 * sizeof(u_int)); |
| 573 | if (p->tstamp_precision_list == NULL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 574 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno, |
| 575 | "malloc"); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 576 | pcap_close(p); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 577 | return NULL; |
| 578 | } |
| 579 | p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO; |
| 580 | p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO; |
| 581 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 582 | p->activate_op = snf_activate; |
| 583 | ps->snf_boardnum = boardnum; |
| 584 | return p; |
| 585 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 586 | |
| 587 | #ifdef SNF_ONLY |
| 588 | /* |
| 589 | * This libpcap build supports only SNF cards, not regular network |
| 590 | * interfaces.. |
| 591 | */ |
| 592 | |
| 593 | /* |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 594 | * There are no regular interfaces, just SNF interfaces. |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 595 | */ |
| 596 | int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 597 | pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 598 | { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 599 | return (0); |
| 600 | } |
| 601 | |
| 602 | /* |
| 603 | * Attempts to open a regular interface fail. |
| 604 | */ |
| 605 | pcap_t * |
| 606 | pcap_create_interface(const char *device, char *errbuf) |
| 607 | { |
| 608 | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
| 609 | "This version of libpcap only supports SNF cards"); |
| 610 | return NULL; |
| 611 | } |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 612 | |
| 613 | /* |
| 614 | * Libpcap version string. |
| 615 | */ |
| 616 | const char * |
| 617 | pcap_lib_version(void) |
| 618 | { |
| 619 | return (PCAP_VERSION_STRING " (SNF-only)"); |
| 620 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 621 | #endif |