1 /* $NetBSD: pcap-snit.c,v 1.1.1.4 2013/12/31 16:57:24 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 #ifndef lint 29 static const char rcsid[] _U_ = 30 "@(#) Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.77 2008-04-14 20:40:58 guy Exp (LBL)"; 31 #endif 32 33 #ifdef HAVE_CONFIG_H 34 #include "config.h" 35 #endif 36 37 #include <sys/types.h> 38 #include <sys/time.h> 39 #include <sys/timeb.h> 40 #include <sys/dir.h> 41 #include <sys/fcntlcom.h> 42 #include <sys/file.h> 43 #include <sys/ioctl.h> 44 #include <sys/socket.h> 45 #include <sys/stropts.h> 46 47 #include <net/if.h> 48 #include <net/nit.h> 49 #include <net/nit_if.h> 50 #include <net/nit_pf.h> 51 #include <net/nit_buf.h> 52 53 #include <netinet/in.h> 54 #include <netinet/in_systm.h> 55 #include <netinet/ip.h> 56 #include <netinet/if_ether.h> 57 #include <netinet/ip_var.h> 58 #include <netinet/udp.h> 59 #include <netinet/udp_var.h> 60 #include <netinet/tcp.h> 61 #include <netinet/tcpip.h> 62 63 #include <ctype.h> 64 #include <errno.h> 65 #include <stdio.h> 66 #include <string.h> 67 #include <unistd.h> 68 69 #include "pcap-int.h" 70 71 #ifdef HAVE_OS_PROTO_H 72 #include "os-proto.h" 73 #endif 74 75 /* 76 * The chunk size for NIT. This is the amount of buffering 77 * done for read calls. 78 */ 79 #define CHUNKSIZE (2*1024) 80 81 /* 82 * The total buffer space used by NIT. 83 */ 84 #define BUFSPACE (4*CHUNKSIZE) 85 86 /* Forwards */ 87 static int nit_setflags(int, int, int, char *); 88 89 /* 90 * Private data for capturing on STREAMS NIT devices. 91 */ 92 struct pcap_snit { 93 struct pcap_stat stat; 94 }; 95 96 static int 97 pcap_stats_snit(pcap_t *p, struct pcap_stat *ps) 98 { 99 struct pcap_snit *psn = p->priv; 100 101 /* 102 * "ps_recv" counts packets handed to the filter, not packets 103 * that passed the filter. As filtering is done in userland, 104 * this does not include packets dropped because we ran out 105 * of buffer space. 106 * 107 * "ps_drop" counts packets dropped inside the "/dev/nit" 108 * device because of flow control requirements or resource 109 * exhaustion; it doesn't count packets dropped by the 110 * interface driver, or packets dropped upstream. As filtering 111 * is done in userland, it counts packets regardless of whether 112 * they would've passed the filter. 113 * 114 * These statistics don't include packets not yet read from the 115 * kernel by libpcap or packets not yet read from libpcap by the 116 * application. 117 */ 118 *ps = psn->stat; 119 return (0); 120 } 121 122 static int 123 pcap_read_snit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 124 { 125 struct pcap_snit *psn = p->priv; 126 register int cc, n; 127 register u_char *bp, *cp, *ep; 128 register struct nit_bufhdr *hdrp; 129 register struct nit_iftime *ntp; 130 register struct nit_iflen *nlp; 131 register struct nit_ifdrops *ndp; 132 register int caplen; 133 134 cc = p->cc; 135 if (cc == 0) { 136 cc = read(p->fd, (char *)p->buffer, p->bufsize); 137 if (cc < 0) { 138 if (errno == EWOULDBLOCK) 139 return (0); 140 snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s", 141 pcap_strerror(errno)); 142 return (-1); 143 } 144 bp = p->buffer; 145 } else 146 bp = p->bp; 147 148 /* 149 * loop through each snapshot in the chunk 150 */ 151 n = 0; 152 ep = bp + cc; 153 while (bp < ep) { 154 /* 155 * Has "pcap_breakloop()" been called? 156 * If so, return immediately - if we haven't read any 157 * packets, clear the flag and return -2 to indicate 158 * that we were told to break out of the loop, otherwise 159 * leave the flag set, so that the *next* call will break 160 * out of the loop without having read any packets, and 161 * return the number of packets we've processed so far. 162 */ 163 if (p->break_loop) { 164 if (n == 0) { 165 p->break_loop = 0; 166 return (-2); 167 } else { 168 p->bp = bp; 169 p->cc = ep - bp; 170 return (n); 171 } 172 } 173 174 ++psn->stat.ps_recv; 175 cp = bp; 176 177 /* get past NIT buffer */ 178 hdrp = (struct nit_bufhdr *)cp; 179 cp += sizeof(*hdrp); 180 181 /* get past NIT timer */ 182 ntp = (struct nit_iftime *)cp; 183 cp += sizeof(*ntp); 184 185 ndp = (struct nit_ifdrops *)cp; 186 psn->stat.ps_drop = ndp->nh_drops; 187 cp += sizeof *ndp; 188 189 /* get past packet len */ 190 nlp = (struct nit_iflen *)cp; 191 cp += sizeof(*nlp); 192 193 /* next snapshot */ 194 bp += hdrp->nhb_totlen; 195 196 caplen = nlp->nh_pktlen; 197 if (caplen > p->snapshot) 198 caplen = p->snapshot; 199 200 if (bpf_filter(p->fcode.bf_insns, cp, nlp->nh_pktlen, caplen)) { 201 struct pcap_pkthdr h; 202 h.ts = ntp->nh_timestamp; 203 h.len = nlp->nh_pktlen; 204 h.caplen = caplen; 205 (*callback)(user, &h, cp); 206 if (++n >= cnt && cnt > 0) { 207 p->cc = ep - bp; 208 p->bp = bp; 209 return (n); 210 } 211 } 212 } 213 p->cc = 0; 214 return (n); 215 } 216 217 static int 218 pcap_inject_snit(pcap_t *p, const void *buf, size_t size) 219 { 220 struct strbuf ctl, data; 221 222 /* 223 * XXX - can we just do 224 * 225 ret = write(pd->f, buf, size); 226 */ 227 ctl.len = sizeof(*sa); /* XXX - what was this? */ 228 ctl.buf = (char *)sa; 229 data.buf = buf; 230 data.len = size; 231 ret = putmsg(p->fd, &ctl, &data); 232 if (ret == -1) { 233 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", 234 pcap_strerror(errno)); 235 return (-1); 236 } 237 return (ret); 238 } 239 240 static int 241 nit_setflags(pcap_t *p) 242 { 243 bpf_u_int32 flags; 244 struct strioctl si; 245 u_int zero = 0; 246 struct timeval timeout; 247 248 if (p->opt.immediate) { 249 /* 250 * Set the chunk size to zero, so that chunks get sent 251 * up immediately. 252 */ 253 si.ic_cmd = NIOCSCHUNK; 254 si.ic_len = sizeof(zero); 255 si.ic_dp = (char *)&zero; 256 if (ioctl(p->fd, I_STR, (char *)&si) < 0) { 257 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCSCHUNK: %s", 258 pcap_strerror(errno)); 259 return (-1); 260 } 261 } 262 si.ic_timout = INFTIM; 263 if (p->opt.timeout != 0) { 264 timeout.tv_sec = p->opt.timeout / 1000; 265 timeout.tv_usec = (p->opt.timeout * 1000) % 1000000; 266 si.ic_cmd = NIOCSTIME; 267 si.ic_len = sizeof(timeout); 268 si.ic_dp = (char *)&timeout; 269 if (ioctl(p->fd, I_STR, (char *)&si) < 0) { 270 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCSTIME: %s", 271 pcap_strerror(errno)); 272 return (-1); 273 } 274 } 275 flags = NI_TIMESTAMP | NI_LEN | NI_DROPS; 276 if (p->opt.promisc) 277 flags |= NI_PROMISC; 278 si.ic_cmd = NIOCSFLAGS; 279 si.ic_len = sizeof(flags); 280 si.ic_dp = (char *)&flags; 281 if (ioctl(p->fd, I_STR, (char *)&si) < 0) { 282 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCSFLAGS: %s", 283 pcap_strerror(errno)); 284 return (-1); 285 } 286 return (0); 287 } 288 289 static int 290 pcap_activate_snit(pcap_t *p) 291 { 292 struct strioctl si; /* struct for ioctl() */ 293 struct ifreq ifr; /* interface request struct */ 294 int chunksize = CHUNKSIZE; 295 int fd; 296 static char dev[] = "/dev/nit"; 297 298 if (p->opt.rfmon) { 299 /* 300 * No monitor mode on SunOS 4.x (no Wi-Fi devices on 301 * hardware supported by SunOS 4.x). 302 */ 303 return (PCAP_ERROR_RFMON_NOTSUP); 304 } 305 306 if (p->snapshot < 96) 307 /* 308 * NIT requires a snapshot length of at least 96. 309 */ 310 p->snapshot = 96; 311 312 /* 313 * Initially try a read/write open (to allow the inject 314 * method to work). If that fails due to permission 315 * issues, fall back to read-only. This allows a 316 * non-root user to be granted specific access to pcap 317 * capabilities via file permissions. 318 * 319 * XXX - we should have an API that has a flag that 320 * controls whether to open read-only or read-write, 321 * so that denial of permission to send (or inability 322 * to send, if sending packets isn't supported on 323 * the device in question) can be indicated at open 324 * time. 325 */ 326 p->fd = fd = open(dev, O_RDWR); 327 if (fd < 0 && errno == EACCES) 328 p->fd = fd = open(dev, O_RDONLY); 329 if (fd < 0) { 330 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", dev, 331 pcap_strerror(errno)); 332 goto bad; 333 } 334 335 /* arrange to get discrete messages from the STREAM and use NIT_BUF */ 336 if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) { 337 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "I_SRDOPT: %s", 338 pcap_strerror(errno)); 339 goto bad; 340 } 341 if (ioctl(fd, I_PUSH, "nbuf") < 0) { 342 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "push nbuf: %s", 343 pcap_strerror(errno)); 344 goto bad; 345 } 346 /* set the chunksize */ 347 si.ic_cmd = NIOCSCHUNK; 348 si.ic_timout = INFTIM; 349 si.ic_len = sizeof(chunksize); 350 si.ic_dp = (char *)&chunksize; 351 if (ioctl(fd, I_STR, (char *)&si) < 0) { 352 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCSCHUNK: %s", 353 pcap_strerror(errno)); 354 goto bad; 355 } 356 357 /* request the interface */ 358 strncpy(ifr.ifr_name, p->opt.source, sizeof(ifr.ifr_name)); 359 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0'; 360 si.ic_cmd = NIOCBIND; 361 si.ic_len = sizeof(ifr); 362 si.ic_dp = (char *)𝔦 363 if (ioctl(fd, I_STR, (char *)&si) < 0) { 364 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCBIND: %s: %s", 365 ifr.ifr_name, pcap_strerror(errno)); 366 goto bad; 367 } 368 369 /* set the snapshot length */ 370 si.ic_cmd = NIOCSSNAP; 371 si.ic_len = sizeof(p->snapshot); 372 si.ic_dp = (char *)&p->snapshot; 373 if (ioctl(fd, I_STR, (char *)&si) < 0) { 374 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCSSNAP: %s", 375 pcap_strerror(errno)); 376 goto bad; 377 } 378 if (nit_setflags(p) < 0) 379 goto bad; 380 381 (void)ioctl(fd, I_FLUSH, (char *)FLUSHR); 382 /* 383 * NIT supports only ethernets. 384 */ 385 p->linktype = DLT_EN10MB; 386 387 p->bufsize = BUFSPACE; 388 p->buffer = (u_char *)malloc(p->bufsize); 389 if (p->buffer == NULL) { 390 strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); 391 goto bad; 392 } 393 394 /* 395 * "p->fd" is an FD for a STREAMS device, so "select()" and 396 * "poll()" should work on it. 397 */ 398 p->selectable_fd = p->fd; 399 400 /* 401 * This is (presumably) a real Ethernet capture; give it a 402 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so 403 * that an application can let you choose it, in case you're 404 * capturing DOCSIS traffic that a Cisco Cable Modem 405 * Termination System is putting out onto an Ethernet (it 406 * doesn't put an Ethernet header onto the wire, it puts raw 407 * DOCSIS frames out on the wire inside the low-level 408 * Ethernet framing). 409 */ 410 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); 411 /* 412 * If that fails, just leave the list empty. 413 */ 414 if (p->dlt_list != NULL) { 415 p->dlt_list[0] = DLT_EN10MB; 416 p->dlt_list[1] = DLT_DOCSIS; 417 p->dlt_count = 2; 418 } 419 420 p->read_op = pcap_read_snit; 421 p->inject_op = pcap_inject_snit; 422 p->setfilter_op = install_bpf_program; /* no kernel filtering */ 423 p->setdirection_op = NULL; /* Not implemented. */ 424 p->set_datalink_op = NULL; /* can't change data link type */ 425 p->getnonblock_op = pcap_getnonblock_fd; 426 p->setnonblock_op = pcap_setnonblock_fd; 427 p->stats_op = pcap_stats_snit; 428 429 return (0); 430 bad: 431 pcap_cleanup_live_common(p); 432 return (PCAP_ERROR); 433 } 434 435 pcap_t * 436 pcap_create_interface(const char *device, char *ebuf) 437 { 438 pcap_t *p; 439 440 p = pcap_create_common(device, ebuf, sizeof (struct pcap_snit)); 441 if (p == NULL) 442 return (NULL); 443 444 p->activate_op = pcap_activate_snit; 445 return (p); 446 } 447 448 int 449 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 450 { 451 return (0); 452 } 453