1 /* $NetBSD: pcap-snit.c,v 1.5 2018/09/03 15:26:43 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that: (1) source code distributions 9 * retain the above copyright notice and this paragraph in its entirety, (2) 10 * distributions including binary code include the above copyright notice and 11 * this paragraph in its entirety in the documentation or other materials 12 * provided with the distribution, and (3) all advertising materials mentioning 13 * features or use of this software display the following acknowledgement: 14 * ``This product includes software developed by the University of California, 15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 16 * the University nor the names of its contributors may be used to endorse 17 * or promote products derived from this software without specific prior 18 * written permission. 19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 * 23 * Modifications made to accommodate the new SunOS4.0 NIT facility by 24 * Micky Liu, micky@cunixc.cc.columbia.edu, Columbia University in May, 1989. 25 * This module now handles the STREAMS based NIT. 26 */ 27 28 #include <sys/cdefs.h> 29 __RCSID("$NetBSD: pcap-snit.c,v 1.5 2018/09/03 15:26:43 christos Exp $"); 30 31 #ifdef HAVE_CONFIG_H 32 #include <config.h> 33 #endif 34 35 #include <sys/types.h> 36 #include <sys/time.h> 37 #include <sys/timeb.h> 38 #include <sys/dir.h> 39 #include <sys/fcntlcom.h> 40 #include <sys/file.h> 41 #include <sys/ioctl.h> 42 #include <sys/socket.h> 43 #include <sys/stropts.h> 44 45 #include <net/if.h> 46 #include <net/nit.h> 47 #include <net/nit_if.h> 48 #include <net/nit_pf.h> 49 #include <net/nit_buf.h> 50 51 #include <netinet/in.h> 52 #include <netinet/in_systm.h> 53 #include <netinet/ip.h> 54 #include <netinet/if_ether.h> 55 #include <netinet/ip_var.h> 56 #include <netinet/udp.h> 57 #include <netinet/udp_var.h> 58 #include <netinet/tcp.h> 59 #include <netinet/tcpip.h> 60 61 #include <ctype.h> 62 #include <errno.h> 63 #include <stdio.h> 64 #include <string.h> 65 #include <unistd.h> 66 67 #include "pcap-int.h" 68 69 #ifdef HAVE_OS_PROTO_H 70 #include "os-proto.h" 71 #endif 72 73 /* 74 * The chunk size for NIT. This is the amount of buffering 75 * done for read calls. 76 */ 77 #define CHUNKSIZE (2*1024) 78 79 /* 80 * The total buffer space used by NIT. 81 */ 82 #define BUFSPACE (4*CHUNKSIZE) 83 84 /* Forwards */ 85 static int nit_setflags(int, int, int, char *); 86 87 /* 88 * Private data for capturing on STREAMS NIT devices. 89 */ 90 struct pcap_snit { 91 struct pcap_stat stat; 92 }; 93 94 static int 95 pcap_stats_snit(pcap_t *p, struct pcap_stat *ps) 96 { 97 struct pcap_snit *psn = p->priv; 98 99 /* 100 * "ps_recv" counts packets handed to the filter, not packets 101 * that passed the filter. As filtering is done in userland, 102 * this does not include packets dropped because we ran out 103 * of buffer space. 104 * 105 * "ps_drop" counts packets dropped inside the "/dev/nit" 106 * device because of flow control requirements or resource 107 * exhaustion; it doesn't count packets dropped by the 108 * interface driver, or packets dropped upstream. As filtering 109 * is done in userland, it counts packets regardless of whether 110 * they would've passed the filter. 111 * 112 * These statistics don't include packets not yet read from the 113 * kernel by libpcap or packets not yet read from libpcap by the 114 * application. 115 */ 116 *ps = psn->stat; 117 return (0); 118 } 119 120 static int 121 pcap_read_snit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 122 { 123 struct pcap_snit *psn = p->priv; 124 register int cc, n; 125 register u_char *bp, *cp, *ep; 126 register struct nit_bufhdr *hdrp; 127 register struct nit_iftime *ntp; 128 register struct nit_iflen *nlp; 129 register struct nit_ifdrops *ndp; 130 register int caplen; 131 132 cc = p->cc; 133 if (cc == 0) { 134 cc = read(p->fd, (char *)p->buffer, p->bufsize); 135 if (cc < 0) { 136 if (errno == EWOULDBLOCK) 137 return (0); 138 pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), 139 errno, "pcap_read"); 140 return (-1); 141 } 142 bp = (u_char *)p->buffer; 143 } else 144 bp = p->bp; 145 146 /* 147 * loop through each snapshot in the chunk 148 */ 149 n = 0; 150 ep = bp + cc; 151 while (bp < ep) { 152 /* 153 * Has "pcap_breakloop()" been called? 154 * If so, return immediately - if we haven't read any 155 * packets, clear the flag and return -2 to indicate 156 * that we were told to break out of the loop, otherwise 157 * leave the flag set, so that the *next* call will break 158 * out of the loop without having read any packets, and 159 * return the number of packets we've processed so far. 160 */ 161 if (p->break_loop) { 162 if (n == 0) { 163 p->break_loop = 0; 164 return (-2); 165 } else { 166 p->bp = bp; 167 p->cc = ep - bp; 168 return (n); 169 } 170 } 171 172 ++psn->stat.ps_recv; 173 cp = bp; 174 175 /* get past NIT buffer */ 176 hdrp = (struct nit_bufhdr *)cp; 177 cp += sizeof(*hdrp); 178 179 /* get past NIT timer */ 180 ntp = (struct nit_iftime *)cp; 181 cp += sizeof(*ntp); 182 183 ndp = (struct nit_ifdrops *)cp; 184 psn->stat.ps_drop = ndp->nh_drops; 185 cp += sizeof *ndp; 186 187 /* get past packet len */ 188 nlp = (struct nit_iflen *)cp; 189 cp += sizeof(*nlp); 190 191 /* next snapshot */ 192 bp += hdrp->nhb_totlen; 193 194 caplen = nlp->nh_pktlen; 195 if (caplen > p->snapshot) 196 caplen = p->snapshot; 197 198 if (bpf_filter(p->fcode.bf_insns, cp, nlp->nh_pktlen, caplen)) { 199 struct pcap_pkthdr h; 200 h.ts = ntp->nh_timestamp; 201 h.len = nlp->nh_pktlen; 202 h.caplen = caplen; 203 (*callback)(user, &h, cp); 204 if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) { 205 p->cc = ep - bp; 206 p->bp = bp; 207 return (n); 208 } 209 } 210 } 211 p->cc = 0; 212 return (n); 213 } 214 215 static int 216 pcap_inject_snit(pcap_t *p, const void *buf, size_t size) 217 { 218 struct strbuf ctl, data; 219 220 /* 221 * XXX - can we just do 222 * 223 ret = write(pd->f, buf, size); 224 */ 225 ctl.len = sizeof(*sa); /* XXX - what was this? */ 226 ctl.buf = (char *)sa; 227 data.buf = buf; 228 data.len = size; 229 ret = putmsg(p->fd, &ctl, &data); 230 if (ret == -1) { 231 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, 232 errno, "send"); 233 return (-1); 234 } 235 return (ret); 236 } 237 238 static int 239 nit_setflags(pcap_t *p) 240 { 241 bpf_u_int32 flags; 242 struct strioctl si; 243 u_int zero = 0; 244 struct timeval timeout; 245 246 if (p->opt.immediate) { 247 /* 248 * Set the chunk size to zero, so that chunks get sent 249 * up immediately. 250 */ 251 si.ic_cmd = NIOCSCHUNK; 252 si.ic_len = sizeof(zero); 253 si.ic_dp = (char *)&zero; 254 if (ioctl(p->fd, I_STR, (char *)&si) < 0) { 255 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, 256 errno, "NIOCSCHUNK"); 257 return (-1); 258 } 259 } 260 si.ic_timout = INFTIM; 261 if (p->opt.timeout != 0) { 262 timeout.tv_sec = p->opt.timeout / 1000; 263 timeout.tv_usec = (p->opt.timeout * 1000) % 1000000; 264 si.ic_cmd = NIOCSTIME; 265 si.ic_len = sizeof(timeout); 266 si.ic_dp = (char *)&timeout; 267 if (ioctl(p->fd, I_STR, (char *)&si) < 0) { 268 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, 269 errno, "NIOCSTIME"); 270 return (-1); 271 } 272 } 273 flags = NI_TIMESTAMP | NI_LEN | NI_DROPS; 274 if (p->opt.promisc) 275 flags |= NI_PROMISC; 276 si.ic_cmd = NIOCSFLAGS; 277 si.ic_len = sizeof(flags); 278 si.ic_dp = (char *)&flags; 279 if (ioctl(p->fd, I_STR, (char *)&si) < 0) { 280 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, 281 errno, "NIOCSFLAGS"); 282 return (-1); 283 } 284 return (0); 285 } 286 287 static int 288 pcap_activate_snit(pcap_t *p) 289 { 290 struct strioctl si; /* struct for ioctl() */ 291 struct ifreq ifr; /* interface request struct */ 292 int chunksize = CHUNKSIZE; 293 int fd; 294 static const char dev[] = "/dev/nit"; 295 int err; 296 297 if (p->opt.rfmon) { 298 /* 299 * No monitor mode on SunOS 4.x (no Wi-Fi devices on 300 * hardware supported by SunOS 4.x). 301 */ 302 return (PCAP_ERROR_RFMON_NOTSUP); 303 } 304 305 /* 306 * Turn a negative snapshot value (invalid), a snapshot value of 307 * 0 (unspecified), or a value bigger than the normal maximum 308 * value, into the maximum allowed value. 309 * 310 * If some application really *needs* a bigger snapshot 311 * length, we should just increase MAXIMUM_SNAPLEN. 312 */ 313 if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN) 314 p->snapshot = MAXIMUM_SNAPLEN; 315 316 if (p->snapshot < 96) 317 /* 318 * NIT requires a snapshot length of at least 96. 319 */ 320 p->snapshot = 96; 321 322 /* 323 * Initially try a read/write open (to allow the inject 324 * method to work). If that fails due to permission 325 * issues, fall back to read-only. This allows a 326 * non-root user to be granted specific access to pcap 327 * capabilities via file permissions. 328 * 329 * XXX - we should have an API that has a flag that 330 * controls whether to open read-only or read-write, 331 * so that denial of permission to send (or inability 332 * to send, if sending packets isn't supported on 333 * the device in question) can be indicated at open 334 * time. 335 */ 336 p->fd = fd = open(dev, O_RDWR); 337 if (fd < 0 && errno == EACCES) 338 p->fd = fd = open(dev, O_RDONLY); 339 if (fd < 0) { 340 if (errno == EACCES) 341 err = PCAP_ERROR_PERM_DENIED; 342 else 343 err = PCAP_ERROR; 344 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, 345 errno, "%s", dev); 346 goto bad; 347 } 348 349 /* arrange to get discrete messages from the STREAM and use NIT_BUF */ 350 if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) { 351 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, 352 errno, "I_SRDOPT"); 353 err = PCAP_ERROR; 354 goto bad; 355 } 356 if (ioctl(fd, I_PUSH, "nbuf") < 0) { 357 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, 358 errno, "push nbuf"); 359 err = PCAP_ERROR; 360 goto bad; 361 } 362 /* set the chunksize */ 363 si.ic_cmd = NIOCSCHUNK; 364 si.ic_timout = INFTIM; 365 si.ic_len = sizeof(chunksize); 366 si.ic_dp = (char *)&chunksize; 367 if (ioctl(fd, I_STR, (char *)&si) < 0) { 368 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, 369 errno, "NIOCSCHUNK"); 370 err = PCAP_ERROR; 371 goto bad; 372 } 373 374 /* request the interface */ 375 strncpy(ifr.ifr_name, p->opt.device, sizeof(ifr.ifr_name)); 376 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0'; 377 si.ic_cmd = NIOCBIND; 378 si.ic_len = sizeof(ifr); 379 si.ic_dp = (char *)𝔦 380 if (ioctl(fd, I_STR, (char *)&si) < 0) { 381 /* 382 * XXX - is there an error that means "no such device"? 383 * Is there one that means "that device doesn't support 384 * STREAMS NIT"? 385 */ 386 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, 387 errno, "NIOCBIND: %s", ifr.ifr_name); 388 err = PCAP_ERROR; 389 goto bad; 390 } 391 392 /* set the snapshot length */ 393 si.ic_cmd = NIOCSSNAP; 394 si.ic_len = sizeof(p->snapshot); 395 si.ic_dp = (char *)&p->snapshot; 396 if (ioctl(fd, I_STR, (char *)&si) < 0) { 397 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, 398 errno, "NIOCSSNAP"); 399 err = PCAP_ERROR; 400 goto bad; 401 } 402 if (nit_setflags(p) < 0) { 403 err = PCAP_ERROR; 404 goto bad; 405 } 406 407 (void)ioctl(fd, I_FLUSH, (char *)FLUSHR); 408 /* 409 * NIT supports only ethernets. 410 */ 411 p->linktype = DLT_EN10MB; 412 413 p->bufsize = BUFSPACE; 414 p->buffer = malloc(p->bufsize); 415 if (p->buffer == NULL) { 416 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, 417 errno, "malloc"); 418 err = PCAP_ERROR; 419 goto bad; 420 } 421 422 /* 423 * "p->fd" is an FD for a STREAMS device, so "select()" and 424 * "poll()" should work on it. 425 */ 426 p->selectable_fd = p->fd; 427 428 /* 429 * This is (presumably) a real Ethernet capture; give it a 430 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so 431 * that an application can let you choose it, in case you're 432 * capturing DOCSIS traffic that a Cisco Cable Modem 433 * Termination System is putting out onto an Ethernet (it 434 * doesn't put an Ethernet header onto the wire, it puts raw 435 * DOCSIS frames out on the wire inside the low-level 436 * Ethernet framing). 437 */ 438 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); 439 /* 440 * If that fails, just leave the list empty. 441 */ 442 if (p->dlt_list != NULL) { 443 p->dlt_list[0] = DLT_EN10MB; 444 p->dlt_list[1] = DLT_DOCSIS; 445 p->dlt_count = 2; 446 } 447 448 p->read_op = pcap_read_snit; 449 p->inject_op = pcap_inject_snit; 450 p->setfilter_op = install_bpf_program; /* no kernel filtering */ 451 p->setdirection_op = NULL; /* Not implemented. */ 452 p->set_datalink_op = NULL; /* can't change data link type */ 453 p->getnonblock_op = pcap_getnonblock_fd; 454 p->setnonblock_op = pcap_setnonblock_fd; 455 p->stats_op = pcap_stats_snit; 456 457 return (0); 458 bad: 459 pcap_cleanup_live_common(p); 460 return (err); 461 } 462 463 pcap_t * 464 pcap_create_interface(const char *device _U_, char *ebuf) 465 { 466 pcap_t *p; 467 468 p = pcap_create_common(ebuf, sizeof (struct pcap_snit)); 469 if (p == NULL) 470 return (NULL); 471 472 p->activate_op = pcap_activate_snit; 473 return (p); 474 } 475 476 /* 477 * XXX - there's probably a NIOCBIND error that means "that device 478 * doesn't support NIT"; if so, we should try an NIOCBIND and use that. 479 */ 480 static int 481 can_be_bound(const char *name _U_) 482 { 483 return (1); 484 } 485 486 static int 487 get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_) 488 { 489 /* 490 * Nothing we can do. 491 * XXX - is there a way to find out whether an adapter has 492 * something plugged into it? 493 */ 494 return (0); 495 } 496 497 int 498 pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) 499 { 500 return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound, 501 get_if_flags)); 502 } 503 504 /* 505 * Libpcap version string. 506 */ 507 const char * 508 pcap_lib_version(void) 509 { 510 return (PCAP_VERSION_STRING); 511 } 512