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[] = 28 "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.54 2000/10/28 00:01:30 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 int 88 pcap_stats(pcap_t *p, struct pcap_stat *ps) 89 { 90 91 *ps = p->md.stat; 92 return (0); 93 } 94 95 int 96 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 97 { 98 register int cc, n; 99 register struct bpf_insn *fcode = p->fcode.bf_insns; 100 register u_char *bp, *cp, *ep; 101 register struct nit_bufhdr *hdrp; 102 register struct nit_iftime *ntp; 103 register struct nit_iflen *nlp; 104 register struct nit_ifdrops *ndp; 105 register int caplen; 106 107 cc = p->cc; 108 if (cc == 0) { 109 cc = read(p->fd, (char *)p->buffer, p->bufsize); 110 if (cc < 0) { 111 if (errno == EWOULDBLOCK) 112 return (0); 113 snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s", 114 pcap_strerror(errno)); 115 return (-1); 116 } 117 bp = p->buffer; 118 } else 119 bp = p->bp; 120 121 /* 122 * loop through each snapshot in the chunk 123 */ 124 n = 0; 125 ep = bp + cc; 126 while (bp < ep) { 127 ++p->md.stat.ps_recv; 128 cp = bp; 129 130 /* get past NIT buffer */ 131 hdrp = (struct nit_bufhdr *)cp; 132 cp += sizeof(*hdrp); 133 134 /* get past NIT timer */ 135 ntp = (struct nit_iftime *)cp; 136 cp += sizeof(*ntp); 137 138 ndp = (struct nit_ifdrops *)cp; 139 p->md.stat.ps_drop = ndp->nh_drops; 140 cp += sizeof *ndp; 141 142 /* get past packet len */ 143 nlp = (struct nit_iflen *)cp; 144 cp += sizeof(*nlp); 145 146 /* next snapshot */ 147 bp += hdrp->nhb_totlen; 148 149 caplen = nlp->nh_pktlen; 150 if (caplen > p->snapshot) 151 caplen = p->snapshot; 152 153 if (bpf_filter(fcode, cp, nlp->nh_pktlen, caplen)) { 154 struct pcap_pkthdr h; 155 h.ts = ntp->nh_timestamp; 156 h.len = nlp->nh_pktlen; 157 h.caplen = caplen; 158 (*callback)(user, &h, cp); 159 if (++n >= cnt && cnt >= 0) { 160 p->cc = ep - bp; 161 p->bp = bp; 162 return (n); 163 } 164 } 165 } 166 p->cc = 0; 167 return (n); 168 } 169 170 static int 171 nit_setflags(int fd, int promisc, int to_ms, char *ebuf) 172 { 173 bpf_u_int32 flags; 174 struct strioctl si; 175 struct timeval timeout; 176 177 si.ic_timout = INFTIM; 178 if (to_ms != 0) { 179 timeout.tv_sec = to_ms / 1000; 180 timeout.tv_usec = (to_ms * 1000) % 1000000; 181 si.ic_cmd = NIOCSTIME; 182 si.ic_len = sizeof(timeout); 183 si.ic_dp = (char *)&timeout; 184 if (ioctl(fd, I_STR, (char *)&si) < 0) { 185 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSTIME: %s", 186 pcap_strerror(errno)); 187 return (-1); 188 } 189 } 190 flags = NI_TIMESTAMP | NI_LEN | NI_DROPS; 191 if (promisc) 192 flags |= NI_PROMISC; 193 si.ic_cmd = NIOCSFLAGS; 194 si.ic_len = sizeof(flags); 195 si.ic_dp = (char *)&flags; 196 if (ioctl(fd, I_STR, (char *)&si) < 0) { 197 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSFLAGS: %s", 198 pcap_strerror(errno)); 199 return (-1); 200 } 201 return (0); 202 } 203 204 pcap_t * 205 pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf) 206 { 207 struct strioctl si; /* struct for ioctl() */ 208 struct ifreq ifr; /* interface request struct */ 209 int chunksize = CHUNKSIZE; 210 int fd; 211 static char dev[] = "/dev/nit"; 212 register pcap_t *p; 213 214 p = (pcap_t *)malloc(sizeof(*p)); 215 if (p == NULL) { 216 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); 217 return (NULL); 218 } 219 220 if (snaplen < 96) 221 /* 222 * NIT requires a snapshot length of at least 96. 223 */ 224 snaplen = 96; 225 226 memset(p, 0, sizeof(*p)); 227 p->fd = fd = open(dev, O_RDONLY); 228 if (fd < 0) { 229 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s", dev, 230 pcap_strerror(errno)); 231 goto bad; 232 } 233 234 /* arrange to get discrete messages from the STREAM and use NIT_BUF */ 235 if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) { 236 snprintf(ebuf, PCAP_ERRBUF_SIZE, "I_SRDOPT: %s", 237 pcap_strerror(errno)); 238 goto bad; 239 } 240 if (ioctl(fd, I_PUSH, "nbuf") < 0) { 241 snprintf(ebuf, PCAP_ERRBUF_SIZE, "push nbuf: %s", 242 pcap_strerror(errno)); 243 goto bad; 244 } 245 /* set the chunksize */ 246 si.ic_cmd = NIOCSCHUNK; 247 si.ic_timout = INFTIM; 248 si.ic_len = sizeof(chunksize); 249 si.ic_dp = (char *)&chunksize; 250 if (ioctl(fd, I_STR, (char *)&si) < 0) { 251 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSCHUNK: %s", 252 pcap_strerror(errno)); 253 goto bad; 254 } 255 256 /* request the interface */ 257 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); 258 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = ' '; 259 si.ic_cmd = NIOCBIND; 260 si.ic_len = sizeof(ifr); 261 si.ic_dp = (char *)𝔦 262 if (ioctl(fd, I_STR, (char *)&si) < 0) { 263 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCBIND: %s: %s", 264 ifr.ifr_name, pcap_strerror(errno)); 265 goto bad; 266 } 267 268 /* set the snapshot length */ 269 si.ic_cmd = NIOCSSNAP; 270 si.ic_len = sizeof(snaplen); 271 si.ic_dp = (char *)&snaplen; 272 if (ioctl(fd, I_STR, (char *)&si) < 0) { 273 snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSSNAP: %s", 274 pcap_strerror(errno)); 275 goto bad; 276 } 277 p->snapshot = snaplen; 278 if (nit_setflags(p->fd, promisc, to_ms, ebuf) < 0) 279 goto bad; 280 281 (void)ioctl(fd, I_FLUSH, (char *)FLUSHR); 282 /* 283 * NIT supports only ethernets. 284 */ 285 p->linktype = DLT_EN10MB; 286 287 p->bufsize = BUFSPACE; 288 p->buffer = (u_char *)malloc(p->bufsize); 289 if (p->buffer == NULL) { 290 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); 291 goto bad; 292 } 293 return (p); 294 bad: 295 if (fd >= 0) 296 close(fd); 297 free(p); 298 return (NULL); 299 } 300 301 int 302 pcap_setfilter(pcap_t *p, struct bpf_program *fp) 303 { 304 305 if (install_bpf_program(p, fp) < 0) 306 return (-1); 307 return (0); 308 } 309