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 #ifndef lint 22 static char rcsid[] = 23 "@(#)$Header: pcap-snit.c,v 1.42 96/07/15 00:48:51 leres Exp $ (LBL)"; 24 #endif 25 26 /* 27 * Modifications made to accommodate the new SunOS4.0 NIT facility by 28 * Micky Liu, micky@cunixc.cc.columbia.edu, Columbia University in May, 1989. 29 * This module now handles the STREAMS based NIT. 30 */ 31 32 #include <sys/types.h> 33 #include <sys/time.h> 34 #include <sys/timeb.h> 35 #include <sys/dir.h> 36 #include <sys/fcntlcom.h> 37 #include <sys/file.h> 38 #include <sys/ioctl.h> 39 #include <sys/socket.h> 40 #include <sys/stropts.h> 41 42 #include <net/if.h> 43 #include <net/nit.h> 44 #include <net/nit_if.h> 45 #include <net/nit_pf.h> 46 #include <net/nit_buf.h> 47 48 #include <netinet/in.h> 49 #include <netinet/in_systm.h> 50 #include <netinet/ip.h> 51 #include <netinet/if_ether.h> 52 #include <netinet/ip_var.h> 53 #include <netinet/udp.h> 54 #include <netinet/udp_var.h> 55 #include <netinet/tcp.h> 56 #include <netinet/tcpip.h> 57 58 #include <ctype.h> 59 #include <errno.h> 60 #ifdef HAVE_MALLOC_H 61 #include <malloc.h> 62 #endif 63 #include <stdio.h> 64 #include <string.h> 65 #include <unistd.h> 66 67 #include "pcap-int.h" 68 69 #include "gnuc.h" 70 #ifdef HAVE_OS_PROTO_H 71 #include "os-proto.h" 72 #endif 73 74 /* 75 * The chunk size for NIT. This is the amount of buffering 76 * done for read calls. 77 */ 78 #define CHUNKSIZE (2*1024) 79 80 /* 81 * The total buffer space used by NIT. 82 */ 83 #define BUFSPACE (4*CHUNKSIZE) 84 85 /* Forwards */ 86 static int nit_setflags(int, int, int, char *); 87 88 int 89 pcap_stats(pcap_t *p, struct pcap_stat *ps) 90 { 91 92 *ps = p->md.stat; 93 return (0); 94 } 95 96 int 97 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 98 { 99 register int cc, n; 100 register struct bpf_insn *fcode = p->fcode.bf_insns; 101 register u_char *bp, *cp, *ep; 102 register struct nit_bufhdr *hdrp; 103 register struct nit_iftime *ntp; 104 register struct nit_iflen *nlp; 105 register struct nit_ifdrops *ndp; 106 register int caplen; 107 108 cc = p->cc; 109 if (cc == 0) { 110 cc = read(p->fd, (char *)p->buffer, p->bufsize); 111 if (cc < 0) { 112 if (errno == EWOULDBLOCK) 113 return (0); 114 sprintf(p->errbuf, "pcap_read: %s", 115 pcap_strerror(errno)); 116 return (-1); 117 } 118 bp = p->buffer; 119 } else 120 bp = p->bp; 121 122 /* 123 * loop through each snapshot in the chunk 124 */ 125 n = 0; 126 ep = bp + cc; 127 while (bp < ep) { 128 ++p->md.stat.ps_recv; 129 cp = bp; 130 131 /* get past NIT buffer */ 132 hdrp = (struct nit_bufhdr *)cp; 133 cp += sizeof(*hdrp); 134 135 /* get past NIT timer */ 136 ntp = (struct nit_iftime *)cp; 137 cp += sizeof(*ntp); 138 139 ndp = (struct nit_ifdrops *)cp; 140 p->md.stat.ps_drop = ndp->nh_drops; 141 cp += sizeof *ndp; 142 143 /* get past packet len */ 144 nlp = (struct nit_iflen *)cp; 145 cp += sizeof(*nlp); 146 147 /* next snapshot */ 148 bp += hdrp->nhb_totlen; 149 150 caplen = nlp->nh_pktlen; 151 if (caplen > p->snapshot) 152 caplen = p->snapshot; 153 154 if (bpf_filter(fcode, cp, nlp->nh_pktlen, caplen)) { 155 struct pcap_pkthdr h; 156 h.ts = ntp->nh_timestamp; 157 h.len = nlp->nh_pktlen; 158 h.caplen = caplen; 159 (*callback)(user, &h, cp); 160 if (++n >= cnt && cnt >= 0) { 161 p->cc = ep - bp; 162 p->bp = bp; 163 return (n); 164 } 165 } 166 } 167 p->cc = 0; 168 return (n); 169 } 170 171 static int 172 nit_setflags(int fd, int promisc, int to_ms, char *ebuf) 173 { 174 bpf_u_int32 flags; 175 struct strioctl si; 176 struct timeval timeout; 177 178 si.ic_timout = INFTIM; 179 if (to_ms != 0) { 180 timeout.tv_sec = to_ms / 1000; 181 timeout.tv_usec = (to_ms * 1000) % 1000000; 182 si.ic_cmd = NIOCSTIME; 183 si.ic_len = sizeof(timeout); 184 si.ic_dp = (char *)&timeout; 185 if (ioctl(fd, I_STR, (char *)&si) < 0) { 186 sprintf(ebuf, "NIOCSTIME: %s", 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 sprintf(ebuf, "NIOCSFLAGS: %s", pcap_strerror(errno)); 198 return (-1); 199 } 200 return (0); 201 } 202 203 pcap_t * 204 pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf) 205 { 206 struct strioctl si; /* struct for ioctl() */ 207 struct ifreq ifr; /* interface request struct */ 208 int chunksize = CHUNKSIZE; 209 int fd; 210 static char dev[] = "/dev/nit"; 211 register pcap_t *p; 212 213 p = (pcap_t *)malloc(sizeof(*p)); 214 if (p == NULL) { 215 strcpy(ebuf, pcap_strerror(errno)); 216 return (NULL); 217 } 218 219 if (snaplen < 96) 220 /* 221 * NIT requires a snapshot length of at least 96. 222 */ 223 snaplen = 96; 224 225 bzero(p, sizeof(*p)); 226 p->fd = fd = open(dev, O_RDONLY); 227 if (fd < 0) { 228 sprintf(ebuf, "%s: %s", dev, pcap_strerror(errno)); 229 goto bad; 230 } 231 232 /* arrange to get discrete messages from the STREAM and use NIT_BUF */ 233 if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) { 234 sprintf(ebuf, "I_SRDOPT: %s", pcap_strerror(errno)); 235 goto bad; 236 } 237 if (ioctl(fd, I_PUSH, "nbuf") < 0) { 238 sprintf(ebuf, "push nbuf: %s", pcap_strerror(errno)); 239 goto bad; 240 } 241 /* set the chunksize */ 242 si.ic_cmd = NIOCSCHUNK; 243 si.ic_timout = INFTIM; 244 si.ic_len = sizeof(chunksize); 245 si.ic_dp = (char *)&chunksize; 246 if (ioctl(fd, I_STR, (char *)&si) < 0) { 247 sprintf(ebuf, "NIOCSCHUNK: %s", pcap_strerror(errno)); 248 goto bad; 249 } 250 251 /* request the interface */ 252 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); 253 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = ' '; 254 si.ic_cmd = NIOCBIND; 255 si.ic_len = sizeof(ifr); 256 si.ic_dp = (char *)𝔦 257 if (ioctl(fd, I_STR, (char *)&si) < 0) { 258 sprintf(ebuf, "NIOCBIND: %s: %s", 259 ifr.ifr_name, pcap_strerror(errno)); 260 goto bad; 261 } 262 263 /* set the snapshot length */ 264 si.ic_cmd = NIOCSSNAP; 265 si.ic_len = sizeof(snaplen); 266 si.ic_dp = (char *)&snaplen; 267 if (ioctl(fd, I_STR, (char *)&si) < 0) { 268 sprintf(ebuf, "NIOCSSNAP: %s", pcap_strerror(errno)); 269 goto bad; 270 } 271 p->snapshot = snaplen; 272 if (nit_setflags(p->fd, promisc, to_ms, ebuf) < 0) 273 goto bad; 274 275 (void)ioctl(fd, I_FLUSH, (char *)FLUSHR); 276 /* 277 * NIT supports only ethernets. 278 */ 279 p->linktype = DLT_EN10MB; 280 281 p->bufsize = BUFSPACE; 282 p->buffer = (u_char *)malloc(p->bufsize); 283 if (p->buffer == NULL) { 284 strcpy(ebuf, pcap_strerror(errno)); 285 goto bad; 286 } 287 return (p); 288 bad: 289 if (fd >= 0) 290 close(fd); 291 free(p); 292 return (NULL); 293 } 294 295 int 296 pcap_setfilter(pcap_t *p, struct bpf_program *fp) 297 { 298 299 p->fcode = *fp; 300 return (0); 301 } 302