1 /* $NetBSD: pcap-nit.c,v 1.1.1.3 2013/04/06 15:57:46 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 #ifndef lint 24 static const char rcsid[] _U_ = 25 "@(#) Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.62 2008-04-14 20:40:58 guy Exp (LBL)"; 26 #endif 27 28 #ifdef HAVE_CONFIG_H 29 #include "config.h" 30 #endif 31 32 #include <sys/types.h> 33 #include <sys/time.h> 34 #include <sys/timeb.h> 35 #include <sys/file.h> 36 #include <sys/ioctl.h> 37 #include <sys/socket.h> 38 39 #include <net/if.h> 40 #include <net/nit.h> 41 42 #include <netinet/in.h> 43 #include <netinet/in_systm.h> 44 #include <netinet/ip.h> 45 #include <netinet/if_ether.h> 46 #include <netinet/ip_var.h> 47 #include <netinet/udp.h> 48 #include <netinet/udp_var.h> 49 #include <netinet/tcp.h> 50 #include <netinet/tcpip.h> 51 52 #include <ctype.h> 53 #include <errno.h> 54 #include <stdio.h> 55 56 #include "pcap-int.h" 57 58 #ifdef HAVE_OS_PROTO_H 59 #include "os-proto.h" 60 #endif 61 62 /* 63 * The chunk size for NIT. This is the amount of buffering 64 * done for read calls. 65 */ 66 #define CHUNKSIZE (2*1024) 67 68 /* 69 * The total buffer space used by NIT. 70 */ 71 #define BUFSPACE (4*CHUNKSIZE) 72 73 /* Forwards */ 74 static int nit_setflags(int, int, int, char *); 75 76 static int 77 pcap_stats_nit(pcap_t *p, struct pcap_stat *ps) 78 { 79 80 /* 81 * "ps_recv" counts packets handed to the filter, not packets 82 * that passed the filter. As filtering is done in userland, 83 * this does not include packets dropped because we ran out 84 * of buffer space. 85 * 86 * "ps_drop" presumably counts packets dropped by the socket 87 * because of flow control requirements or resource exhaustion; 88 * it doesn't count packets dropped by the interface driver. 89 * As filtering is done in userland, it counts packets regardless 90 * of whether they would've passed the filter. 91 * 92 * These statistics don't include packets not yet read from the 93 * kernel by libpcap or packets not yet read from libpcap by the 94 * application. 95 */ 96 *ps = p->md.stat; 97 return (0); 98 } 99 100 static int 101 pcap_read_nit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 102 { 103 register int cc, n; 104 register u_char *bp, *cp, *ep; 105 register struct nit_hdr *nh; 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 snprintf(p->errbuf, sizeof(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 packet. The increment expression 124 * rounds up to the next int boundary past the end of 125 * the previous packet. 126 */ 127 n = 0; 128 ep = bp + cc; 129 while (bp < ep) { 130 /* 131 * Has "pcap_breakloop()" been called? 132 * If so, return immediately - if we haven't read any 133 * packets, clear the flag and return -2 to indicate 134 * that we were told to break out of the loop, otherwise 135 * leave the flag set, so that the *next* call will break 136 * out of the loop without having read any packets, and 137 * return the number of packets we've processed so far. 138 */ 139 if (p->break_loop) { 140 if (n == 0) { 141 p->break_loop = 0; 142 return (-2); 143 } else { 144 p->cc = ep - bp; 145 p->bp = bp; 146 return (n); 147 } 148 } 149 150 nh = (struct nit_hdr *)bp; 151 cp = bp + sizeof(*nh); 152 153 switch (nh->nh_state) { 154 155 case NIT_CATCH: 156 break; 157 158 case NIT_NOMBUF: 159 case NIT_NOCLUSTER: 160 case NIT_NOSPACE: 161 p->md.stat.ps_drop = nh->nh_dropped; 162 continue; 163 164 case NIT_SEQNO: 165 continue; 166 167 default: 168 snprintf(p->errbuf, sizeof(p->errbuf), 169 "bad nit state %d", nh->nh_state); 170 return (-1); 171 } 172 ++p->md.stat.ps_recv; 173 bp += ((sizeof(struct nit_hdr) + nh->nh_datalen + 174 sizeof(int) - 1) & ~(sizeof(int) - 1)); 175 176 caplen = nh->nh_wirelen; 177 if (caplen > p->snapshot) 178 caplen = p->snapshot; 179 if (bpf_filter(p->fcode.bf_insns, cp, nh->nh_wirelen, caplen)) { 180 struct pcap_pkthdr h; 181 h.ts = nh->nh_timestamp; 182 h.len = nh->nh_wirelen; 183 h.caplen = caplen; 184 (*callback)(user, &h, cp); 185 if (++n >= cnt && cnt > 0) { 186 p->cc = ep - bp; 187 p->bp = bp; 188 return (n); 189 } 190 } 191 } 192 p->cc = 0; 193 return (n); 194 } 195 196 static int 197 pcap_inject_nit(pcap_t *p, const void *buf, size_t size) 198 { 199 struct sockaddr sa; 200 int ret; 201 202 memset(&sa, 0, sizeof(sa)); 203 strncpy(sa.sa_data, device, sizeof(sa.sa_data)); 204 ret = sendto(p->fd, buf, size, 0, &sa, sizeof(sa)); 205 if (ret == -1) { 206 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", 207 pcap_strerror(errno)); 208 return (-1); 209 } 210 return (ret); 211 } 212 213 static int 214 nit_setflags(int fd, int promisc, int to_ms, char *ebuf) 215 { 216 struct nit_ioc nioc; 217 218 memset(&nioc, 0, sizeof(nioc)); 219 nioc.nioc_bufspace = BUFSPACE; 220 nioc.nioc_chunksize = CHUNKSIZE; 221 nioc.nioc_typetomatch = NT_ALLTYPES; 222 nioc.nioc_snaplen = p->snapshot; 223 nioc.nioc_bufalign = sizeof(int); 224 nioc.nioc_bufoffset = 0; 225 226 if (to_ms != 0) { 227 nioc.nioc_flags |= NF_TIMEOUT; 228 nioc.nioc_timeout.tv_sec = to_ms / 1000; 229 nioc.nioc_timeout.tv_usec = (to_ms * 1000) % 1000000; 230 } 231 if (promisc) 232 nioc.nioc_flags |= NF_PROMISC; 233 234 if (ioctl(fd, SIOCSNIT, &nioc) < 0) { 235 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SIOCSNIT: %s", 236 pcap_strerror(errno)); 237 return (-1); 238 } 239 return (0); 240 } 241 242 static int 243 pcap_activate_nit(pcap_t *p) 244 { 245 int fd; 246 struct sockaddr_nit snit; 247 248 if (p->opt.rfmon) { 249 /* 250 * No monitor mode on SunOS 3.x or earlier (no 251 * Wi-Fi *devices* for the hardware that supported 252 * them!). 253 */ 254 return (PCAP_ERROR_RFMON_NOTSUP); 255 } 256 257 if (p->snapshot < 96) 258 /* 259 * NIT requires a snapshot length of at least 96. 260 */ 261 p->snapshot = 96; 262 263 memset(p, 0, sizeof(*p)); 264 p->fd = fd = socket(AF_NIT, SOCK_RAW, NITPROTO_RAW); 265 if (fd < 0) { 266 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 267 "socket: %s", pcap_strerror(errno)); 268 goto bad; 269 } 270 snit.snit_family = AF_NIT; 271 (void)strncpy(snit.snit_ifname, p->opt.source, NITIFSIZ); 272 273 if (bind(fd, (struct sockaddr *)&snit, sizeof(snit))) { 274 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 275 "bind: %s: %s", snit.snit_ifname, pcap_strerror(errno)); 276 goto bad; 277 } 278 nit_setflags(p->fd, p->opt.promisc, p->md.timeout, p->errbuf); 279 280 /* 281 * NIT supports only ethernets. 282 */ 283 p->linktype = DLT_EN10MB; 284 285 p->bufsize = BUFSPACE; 286 p->buffer = (u_char *)malloc(p->bufsize); 287 if (p->buffer == NULL) { 288 strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); 289 goto bad; 290 } 291 292 /* 293 * "p->fd" is a socket, so "select()" should work on it. 294 */ 295 p->selectable_fd = p->fd; 296 297 /* 298 * This is (presumably) a real Ethernet capture; give it a 299 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so 300 * that an application can let you choose it, in case you're 301 * capturing DOCSIS traffic that a Cisco Cable Modem 302 * Termination System is putting out onto an Ethernet (it 303 * doesn't put an Ethernet header onto the wire, it puts raw 304 * DOCSIS frames out on the wire inside the low-level 305 * Ethernet framing). 306 */ 307 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); 308 /* 309 * If that fails, just leave the list empty. 310 */ 311 if (p->dlt_list != NULL) { 312 p->dlt_list[0] = DLT_EN10MB; 313 p->dlt_list[1] = DLT_DOCSIS; 314 p->dlt_count = 2; 315 } 316 317 p->read_op = pcap_read_nit; 318 p->inject_op = pcap_inject_nit; 319 p->setfilter_op = install_bpf_program; /* no kernel filtering */ 320 p->setdirection_op = NULL; /* Not implemented. */ 321 p->set_datalink_op = NULL; /* can't change data link type */ 322 p->getnonblock_op = pcap_getnonblock_fd; 323 p->setnonblock_op = pcap_setnonblock_fd; 324 p->stats_op = pcap_stats_nit; 325 326 return (0); 327 bad: 328 pcap_cleanup_live_common(p); 329 return (PCAP_ERROR); 330 } 331 332 pcap_t * 333 pcap_create(const char *device, char *ebuf) 334 { 335 pcap_t *p; 336 337 p = pcap_create_common(device, ebuf); 338 if (p == NULL) 339 return (NULL); 340 341 p->activate_op = pcap_activate_nit; 342 return (p); 343 } 344 345 int 346 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 347 { 348 return (0); 349 } 350