1 /* 2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that: (1) source code distributions 7 * retain the above copyright notice and this paragraph in its entirety, (2) 8 * distributions including binary code include the above copyright notice and 9 * this paragraph in its entirety in the documentation or other materials 10 * provided with the distribution, and (3) all advertising materials mentioning 11 * features or use of this software display the following acknowledgement: 12 * ``This product includes software developed by the University of California, 13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14 * the University nor the names of its contributors may be used to endorse 15 * or promote products derived from this software without specific prior 16 * written permission. 17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20 * 21 * Modifications made to accommodate the new SunOS4.0 NIT facility by 22 * Micky Liu, micky@cunixc.cc.columbia.edu, Columbia University in May, 1989. 23 * This module now handles the STREAMS based NIT. 24 */ 25 26 #ifndef lint 27 static const char rcsid[] _U_ = 28 "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.77 2008-04-14 20:40:58 guy Exp $ (LBL)"; 29 #endif 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 static int 88 pcap_stats_snit(pcap_t *p, struct pcap_stat *ps) 89 { 90 91 /* 92 * "ps_recv" counts packets handed to the filter, not packets 93 * that passed the filter. As filtering is done in userland, 94 * this does not include packets dropped because we ran out 95 * of buffer space. 96 * 97 * "ps_drop" counts packets dropped inside the "/dev/nit" 98 * device because of flow control requirements or resource 99 * exhaustion; it doesn't count packets dropped by the 100 * interface driver, or packets dropped upstream. As filtering 101 * is done in userland, it counts packets regardless of whether 102 * they would've passed the filter. 103 * 104 * These statistics don't include packets not yet read from the 105 * kernel by libpcap or packets not yet read from libpcap by the 106 * application. 107 */ 108 *ps = p->md.stat; 109 return (0); 110 } 111 112 static int 113 pcap_read_snit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 114 { 115 register int cc, n; 116 register u_char *bp, *cp, *ep; 117 register struct nit_bufhdr *hdrp; 118 register struct nit_iftime *ntp; 119 register struct nit_iflen *nlp; 120 register struct nit_ifdrops *ndp; 121 register int caplen; 122 123 cc = p->cc; 124 if (cc == 0) { 125 cc = read(p->fd, (char *)p->buffer, p->bufsize); 126 if (cc < 0) { 127 if (errno == EWOULDBLOCK) 128 return (0); 129 snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s", 130 pcap_strerror(errno)); 131 return (-1); 132 } 133 bp = p->buffer; 134 } else 135 bp = p->bp; 136 137 /* 138 * loop through each snapshot in the chunk 139 */ 140 n = 0; 141 ep = bp + cc; 142 while (bp < ep) { 143 /* 144 * Has "pcap_breakloop()" been called? 145 * If so, return immediately - if we haven't read any 146 * packets, clear the flag and return -2 to indicate 147 * that we were told to break out of the loop, otherwise 148 * leave the flag set, so that the *next* call will break 149 * out of the loop without having read any packets, and 150 * return the number of packets we've processed so far. 151 */ 152 if (p->break_loop) { 153 if (n == 0) { 154 p->break_loop = 0; 155 return (-2); 156 } else { 157 p->bp = bp; 158 p->cc = ep - bp; 159 return (n); 160 } 161 } 162 163 ++p->md.stat.ps_recv; 164 cp = bp; 165 166 /* get past NIT buffer */ 167 hdrp = (struct nit_bufhdr *)cp; 168 cp += sizeof(*hdrp); 169 170 /* get past NIT timer */ 171 ntp = (struct nit_iftime *)cp; 172 cp += sizeof(*ntp); 173 174 ndp = (struct nit_ifdrops *)cp; 175 p->md.stat.ps_drop = ndp->nh_drops; 176 cp += sizeof *ndp; 177 178 /* get past packet len */ 179 nlp = (struct nit_iflen *)cp; 180 cp += sizeof(*nlp); 181 182 /* next snapshot */ 183 bp += hdrp->nhb_totlen; 184 185 caplen = nlp->nh_pktlen; 186 if (caplen > p->snapshot) 187 caplen = p->snapshot; 188 189 if (bpf_filter(p->fcode.bf_insns, cp, nlp->nh_pktlen, caplen)) { 190 struct pcap_pkthdr h; 191 h.ts = ntp->nh_timestamp; 192 h.len = nlp->nh_pktlen; 193 h.caplen = caplen; 194 (*callback)(user, &h, cp); 195 if (++n >= cnt && cnt > 0) { 196 p->cc = ep - bp; 197 p->bp = bp; 198 return (n); 199 } 200 } 201 } 202 p->cc = 0; 203 return (n); 204 } 205 206 static int 207 pcap_inject_snit(pcap_t *p, const void *buf, size_t size) 208 { 209 struct strbuf ctl, data; 210 211 /* 212 * XXX - can we just do 213 * 214 ret = write(pd->f, buf, size); 215 */ 216 ctl.len = sizeof(*sa); /* XXX - what was this? */ 217 ctl.buf = (char *)sa; 218 data.buf = buf; 219 data.len = size; 220 ret = putmsg(p->fd, &ctl, &data); 221 if (ret == -1) { 222 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", 223 pcap_strerror(errno)); 224 return (-1); 225 } 226 return (ret); 227 } 228 229 static int 230 nit_setflags(int fd, int promisc, int to_ms, char *ebuf) 231 { 232 bpf_u_int32 flags; 233 struct strioctl si; 234 struct timeval timeout; 235 236 si.ic_timout = INFTIM; 237 if (to_ms != 0) { 238 timeout.tv_sec = to_ms / 1000; 239 timeout.tv_usec = (to_ms * 1000) % 1000000; 240 si.ic_cmd = NIOCSTIME; 241 si.ic_len = sizeof(timeout); 242 si.ic_dp = (char *)&timeout; 243 if (ioctl(fd, I_STR, (char *)&si) < 0) { 244 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSTIME: %s", 245 pcap_strerror(errno)); 246 return (-1); 247 } 248 } 249 flags = NI_TIMESTAMP | NI_LEN | NI_DROPS; 250 if (promisc) 251 flags |= NI_PROMISC; 252 si.ic_cmd = NIOCSFLAGS; 253 si.ic_len = sizeof(flags); 254 si.ic_dp = (char *)&flags; 255 if (ioctl(fd, I_STR, (char *)&si) < 0) { 256 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSFLAGS: %s", 257 pcap_strerror(errno)); 258 return (-1); 259 } 260 return (0); 261 } 262 263 static int 264 pcap_activate_snit(pcap_t *p) 265 { 266 struct strioctl si; /* struct for ioctl() */ 267 struct ifreq ifr; /* interface request struct */ 268 int chunksize = CHUNKSIZE; 269 int fd; 270 static char dev[] = "/dev/nit"; 271 272 if (p->opt.rfmon) { 273 /* 274 * No monitor mode on SunOS 4.x (no Wi-Fi devices on 275 * hardware supported by SunOS 4.x). 276 */ 277 return (PCAP_ERROR_RFMON_NOTSUP); 278 } 279 280 if (p->snapshot < 96) 281 /* 282 * NIT requires a snapshot length of at least 96. 283 */ 284 p->snapshot = 96; 285 286 /* 287 * Initially try a read/write open (to allow the inject 288 * method to work). If that fails due to permission 289 * issues, fall back to read-only. This allows a 290 * non-root user to be granted specific access to pcap 291 * capabilities via file permissions. 292 * 293 * XXX - we should have an API that has a flag that 294 * controls whether to open read-only or read-write, 295 * so that denial of permission to send (or inability 296 * to send, if sending packets isn't supported on 297 * the device in question) can be indicated at open 298 * time. 299 */ 300 p->fd = fd = open(dev, O_RDWR); 301 if (fd < 0 && errno == EACCES) 302 p->fd = fd = open(dev, O_RDONLY); 303 if (fd < 0) { 304 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", dev, 305 pcap_strerror(errno)); 306 goto bad; 307 } 308 309 /* arrange to get discrete messages from the STREAM and use NIT_BUF */ 310 if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) { 311 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "I_SRDOPT: %s", 312 pcap_strerror(errno)); 313 goto bad; 314 } 315 if (ioctl(fd, I_PUSH, "nbuf") < 0) { 316 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "push nbuf: %s", 317 pcap_strerror(errno)); 318 goto bad; 319 } 320 /* set the chunksize */ 321 si.ic_cmd = NIOCSCHUNK; 322 si.ic_timout = INFTIM; 323 si.ic_len = sizeof(chunksize); 324 si.ic_dp = (char *)&chunksize; 325 if (ioctl(fd, I_STR, (char *)&si) < 0) { 326 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCSCHUNK: %s", 327 pcap_strerror(errno)); 328 goto bad; 329 } 330 331 /* request the interface */ 332 strncpy(ifr.ifr_name, p->opt.source, sizeof(ifr.ifr_name)); 333 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0'; 334 si.ic_cmd = NIOCBIND; 335 si.ic_len = sizeof(ifr); 336 si.ic_dp = (char *)𝔦 337 if (ioctl(fd, I_STR, (char *)&si) < 0) { 338 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCBIND: %s: %s", 339 ifr.ifr_name, pcap_strerror(errno)); 340 goto bad; 341 } 342 343 /* set the snapshot length */ 344 si.ic_cmd = NIOCSSNAP; 345 si.ic_len = sizeof(p->snapshot); 346 si.ic_dp = (char *)&p->snapshot; 347 if (ioctl(fd, I_STR, (char *)&si) < 0) { 348 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCSSNAP: %s", 349 pcap_strerror(errno)); 350 goto bad; 351 } 352 if (nit_setflags(p->fd, p->opt.promisc, p->md.timeout, p->errbuf) < 0) 353 goto bad; 354 355 (void)ioctl(fd, I_FLUSH, (char *)FLUSHR); 356 /* 357 * NIT supports only ethernets. 358 */ 359 p->linktype = DLT_EN10MB; 360 361 p->bufsize = BUFSPACE; 362 p->buffer = (u_char *)malloc(p->bufsize); 363 if (p->buffer == NULL) { 364 strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); 365 goto bad; 366 } 367 368 /* 369 * "p->fd" is an FD for a STREAMS device, so "select()" and 370 * "poll()" should work on it. 371 */ 372 p->selectable_fd = p->fd; 373 374 /* 375 * This is (presumably) a real Ethernet capture; give it a 376 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so 377 * that an application can let you choose it, in case you're 378 * capturing DOCSIS traffic that a Cisco Cable Modem 379 * Termination System is putting out onto an Ethernet (it 380 * doesn't put an Ethernet header onto the wire, it puts raw 381 * DOCSIS frames out on the wire inside the low-level 382 * Ethernet framing). 383 */ 384 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); 385 /* 386 * If that fails, just leave the list empty. 387 */ 388 if (p->dlt_list != NULL) { 389 p->dlt_list[0] = DLT_EN10MB; 390 p->dlt_list[1] = DLT_DOCSIS; 391 p->dlt_count = 2; 392 } 393 394 p->read_op = pcap_read_snit; 395 p->inject_op = pcap_inject_snit; 396 p->setfilter_op = install_bpf_program; /* no kernel filtering */ 397 p->setdirection_op = NULL; /* Not implemented. */ 398 p->set_datalink_op = NULL; /* can't change data link type */ 399 p->getnonblock_op = pcap_getnonblock_fd; 400 p->setnonblock_op = pcap_setnonblock_fd; 401 p->stats_op = pcap_stats_snit; 402 403 return (0); 404 bad: 405 pcap_cleanup_live_common(p); 406 return (PCAP_ERROR); 407 } 408 409 pcap_t * 410 pcap_create_interface(const char *device, char *ebuf) 411 { 412 pcap_t *p; 413 414 p = pcap_create_common(device, ebuf); 415 if (p == NULL) 416 return (NULL); 417 418 p->activate_op = pcap_activate_snit; 419 return (p); 420 } 421 422 int 423 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 424 { 425 return (0); 426 } 427