1 /* 2 * Copyright (c) 1993, 1994, 1995, 1996, 1997 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 const char rcsid[] _U_ = 23 "@(#) $Header: /tcpdump/master/libpcap/pcap-snoop.c,v 1.54.2.1 2005/05/03 18:54:38 guy Exp $ (LBL)"; 24 #endif 25 26 #ifdef HAVE_CONFIG_H 27 #include "config.h" 28 #endif 29 30 #include <sys/param.h> 31 #include <sys/file.h> 32 #include <sys/ioctl.h> 33 #include <sys/socket.h> 34 #include <sys/time.h> 35 36 #include <net/raw.h> 37 #include <net/if.h> 38 39 #include <netinet/in.h> 40 #include <netinet/in_systm.h> 41 #include <netinet/ip.h> 42 #include <netinet/if_ether.h> 43 #include <netinet/ip_var.h> 44 #include <netinet/udp.h> 45 #include <netinet/udp_var.h> 46 #include <netinet/tcp.h> 47 #include <netinet/tcpip.h> 48 49 #include <errno.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <unistd.h> 54 55 #include "pcap-int.h" 56 57 #ifdef HAVE_OS_PROTO_H 58 #include "os-proto.h" 59 #endif 60 61 static int 62 pcap_read_snoop(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 63 { 64 int cc; 65 register struct snoopheader *sh; 66 register u_int datalen; 67 register u_int caplen; 68 register u_char *cp; 69 70 again: 71 /* 72 * Has "pcap_breakloop()" been called? 73 */ 74 if (p->break_loop) { 75 /* 76 * Yes - clear the flag that indicates that it 77 * has, and return -2 to indicate that we were 78 * told to break out of the loop. 79 */ 80 p->break_loop = 0; 81 return (-2); 82 } 83 cc = read(p->fd, (char *)p->buffer, p->bufsize); 84 if (cc < 0) { 85 /* Don't choke when we get ptraced */ 86 switch (errno) { 87 88 case EINTR: 89 goto again; 90 91 case EWOULDBLOCK: 92 return (0); /* XXX */ 93 } 94 snprintf(p->errbuf, sizeof(p->errbuf), 95 "read: %s", pcap_strerror(errno)); 96 return (-1); 97 } 98 sh = (struct snoopheader *)p->buffer; 99 datalen = sh->snoop_packetlen; 100 101 /* 102 * XXX - Sigh, snoop_packetlen is a 16 bit quantity. If we 103 * got a short length, but read a full sized snoop pakcet, 104 * assume we overflowed and add back the 64K... 105 */ 106 if (cc == (p->snapshot + sizeof(struct snoopheader)) && 107 (datalen < p->snapshot)) 108 datalen += (64 * 1024); 109 110 caplen = (datalen < p->snapshot) ? datalen : p->snapshot; 111 cp = (u_char *)(sh + 1) + p->offset; /* XXX */ 112 113 /* 114 * XXX unfortunately snoop loopback isn't exactly like 115 * BSD's. The address family is encoded in the first 2 116 * bytes rather than the first 4 bytes! Luckily the last 117 * two snoop loopback bytes are zeroed. 118 */ 119 if (p->linktype == DLT_NULL && *((short *)(cp + 2)) == 0) { 120 u_int *uip = (u_int *)cp; 121 *uip >>= 16; 122 } 123 124 if (p->fcode.bf_insns == NULL || 125 bpf_filter(p->fcode.bf_insns, cp, datalen, caplen)) { 126 struct pcap_pkthdr h; 127 ++p->md.stat.ps_recv; 128 h.ts.tv_sec = sh->snoop_timestamp.tv_sec; 129 h.ts.tv_usec = sh->snoop_timestamp.tv_usec; 130 h.len = datalen; 131 h.caplen = caplen; 132 (*callback)(user, &h, cp); 133 return (1); 134 } 135 return (0); 136 } 137 138 static int 139 pcap_inject_snoop(pcap_t *p, const void *buf, size_t size) 140 { 141 int ret; 142 143 /* 144 * XXX - libnet overwrites the source address with what I 145 * presume is the interface's address; is that required? 146 */ 147 ret = write(p->fd, buf, size); 148 if (ret == -1) { 149 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", 150 pcap_strerror(errno)); 151 return (-1); 152 } 153 return (ret); 154 } 155 156 static int 157 pcap_stats_snoop(pcap_t *p, struct pcap_stat *ps) 158 { 159 register struct rawstats *rs; 160 struct rawstats rawstats; 161 162 rs = &rawstats; 163 memset(rs, 0, sizeof(*rs)); 164 if (ioctl(p->fd, SIOCRAWSTATS, (char *)rs) < 0) { 165 snprintf(p->errbuf, sizeof(p->errbuf), 166 "SIOCRAWSTATS: %s", pcap_strerror(errno)); 167 return (-1); 168 } 169 170 /* 171 * "ifdrops" are those dropped by the network interface 172 * due to resource shortages or hardware errors. 173 * 174 * "sbdrops" are those dropped due to socket buffer limits. 175 * 176 * As filter is done in userland, "sbdrops" counts packets 177 * regardless of whether they would've passed the filter. 178 * 179 * XXX - does this count *all* Snoop or Drain sockets, 180 * rather than just this socket? If not, why does it have 181 * both Snoop and Drain statistics? 182 */ 183 p->md.stat.ps_drop = 184 rs->rs_snoop.ss_ifdrops + rs->rs_snoop.ss_sbdrops + 185 rs->rs_drain.ds_ifdrops + rs->rs_drain.ds_sbdrops; 186 187 /* 188 * "ps_recv" counts only packets that passed the filter. 189 * As filtering is done in userland, this does not include 190 * packets dropped because we ran out of buffer space. 191 */ 192 *ps = p->md.stat; 193 return (0); 194 } 195 196 /* XXX can't disable promiscuous */ 197 pcap_t * 198 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, 199 char *ebuf) 200 { 201 int fd; 202 struct sockaddr_raw sr; 203 struct snoopfilter sf; 204 u_int v; 205 int ll_hdrlen; 206 int snooplen; 207 pcap_t *p; 208 struct ifreq ifr; 209 210 p = (pcap_t *)malloc(sizeof(*p)); 211 if (p == NULL) { 212 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", 213 pcap_strerror(errno)); 214 return (NULL); 215 } 216 memset(p, 0, sizeof(*p)); 217 fd = socket(PF_RAW, SOCK_RAW, RAWPROTO_SNOOP); 218 if (fd < 0) { 219 snprintf(ebuf, PCAP_ERRBUF_SIZE, "snoop socket: %s", 220 pcap_strerror(errno)); 221 goto bad; 222 } 223 p->fd = fd; 224 memset(&sr, 0, sizeof(sr)); 225 sr.sr_family = AF_RAW; 226 (void)strncpy(sr.sr_ifname, device, sizeof(sr.sr_ifname)); 227 if (bind(fd, (struct sockaddr *)&sr, sizeof(sr))) { 228 snprintf(ebuf, PCAP_ERRBUF_SIZE, "snoop bind: %s", 229 pcap_strerror(errno)); 230 goto bad; 231 } 232 memset(&sf, 0, sizeof(sf)); 233 if (ioctl(fd, SIOCADDSNOOP, &sf) < 0) { 234 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SIOCADDSNOOP: %s", 235 pcap_strerror(errno)); 236 goto bad; 237 } 238 v = 64 * 1024; 239 (void)setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&v, sizeof(v)); 240 /* 241 * XXX hack - map device name to link layer type 242 */ 243 if (strncmp("et", device, 2) == 0 || /* Challenge 10 Mbit */ 244 strncmp("ec", device, 2) == 0 || /* Indigo/Indy 10 Mbit, 245 O2 10/100 */ 246 strncmp("ef", device, 2) == 0 || /* O200/2000 10/100 Mbit */ 247 strncmp("eg", device, 2) == 0 || /* Octane/O2xxx/O3xxx Gigabit */ 248 strncmp("gfe", device, 3) == 0 || /* GIO 100 Mbit */ 249 strncmp("fxp", device, 3) == 0 || /* Challenge VME Enet */ 250 strncmp("ep", device, 2) == 0 || /* Challenge 8x10 Mbit EPLEX */ 251 strncmp("vfe", device, 3) == 0 || /* Challenge VME 100Mbit */ 252 strncmp("fa", device, 2) == 0 || 253 strncmp("qaa", device, 3) == 0 || 254 strncmp("cip", device, 3) == 0 || 255 strncmp("el", device, 2) == 0) { 256 p->linktype = DLT_EN10MB; 257 p->offset = RAW_HDRPAD(sizeof(struct ether_header)); 258 ll_hdrlen = sizeof(struct ether_header); 259 /* 260 * This is (presumably) a real Ethernet capture; give it a 261 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so 262 * that an application can let you choose it, in case you're 263 * capturing DOCSIS traffic that a Cisco Cable Modem 264 * Termination System is putting out onto an Ethernet (it 265 * doesn't put an Ethernet header onto the wire, it puts raw 266 * DOCSIS frames out on the wire inside the low-level 267 * Ethernet framing). 268 * 269 * XXX - are there any sorts of "fake Ethernet" that have 270 * Ethernet link-layer headers but that *shouldn't offer 271 * DLT_DOCSIS as a Cisco CMTS won't put traffic onto it 272 * or get traffic bridged onto it? "el" is for ATM LANE 273 * Ethernet devices, so that might be the case for them; 274 * the same applies for "qaa" classical IP devices. If 275 * "fa" devices are for FORE SPANS, that'd apply to them 276 * as well; what are "cip" devices - some other ATM 277 * Classical IP devices? 278 */ 279 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); 280 /* 281 * If that fails, just leave the list empty. 282 */ 283 if (p->dlt_list != NULL) { 284 p->dlt_list[0] = DLT_EN10MB; 285 p->dlt_list[1] = DLT_DOCSIS; 286 p->dlt_count = 2; 287 } 288 } else if (strncmp("ipg", device, 3) == 0 || 289 strncmp("rns", device, 3) == 0 || /* O2/200/2000 FDDI */ 290 strncmp("xpi", device, 3) == 0) { 291 p->linktype = DLT_FDDI; 292 p->offset = 3; /* XXX yeah? */ 293 ll_hdrlen = 13; 294 } else if (strncmp("ppp", device, 3) == 0) { 295 p->linktype = DLT_RAW; 296 ll_hdrlen = 0; /* DLT_RAW meaning "no PPP header, just the IP packet"? */ 297 } else if (strncmp("qfa", device, 3) == 0) { 298 p->linktype = DLT_IP_OVER_FC; 299 ll_hdrlen = 24; 300 } else if (strncmp("pl", device, 2) == 0) { 301 p->linktype = DLT_RAW; 302 ll_hdrlen = 0; /* Cray UNICOS/mp pseudo link */ 303 } else if (strncmp("lo", device, 2) == 0) { 304 p->linktype = DLT_NULL; 305 ll_hdrlen = 4; 306 } else { 307 snprintf(ebuf, PCAP_ERRBUF_SIZE, 308 "snoop: unknown physical layer type"); 309 goto bad; 310 } 311 #ifdef SIOCGIFMTU 312 /* 313 * XXX - IRIX appears to give you an error if you try to set the 314 * capture length to be greater than the MTU, so let's try to get 315 * the MTU first and, if that succeeds, trim the snap length 316 * to be no greater than the MTU. 317 */ 318 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); 319 if (ioctl(fd, SIOCGIFMTU, (char *)&ifr) < 0) { 320 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SIOCGIFMTU: %s", 321 pcap_strerror(errno)); 322 goto bad; 323 } 324 /* 325 * OK, we got it. 326 * 327 * XXX - some versions of IRIX 6.5 define "ifr_mtu" and have an 328 * "ifru_metric" member of the "ifr_ifru" union in an "ifreq" 329 * structure, others don't. 330 * 331 * I've no idea what's going on, so, if "ifr_mtu" isn't defined, 332 * we define it as "ifr_metric", as using that field appears to 333 * work on the versions that lack "ifr_mtu" (and, on those that 334 * don't lack it, "ifru_metric" and "ifru_mtu" are both "int" 335 * members of the "ifr_ifru" union, which suggests that they 336 * may be interchangeable in this case). 337 */ 338 #ifndef ifr_mtu 339 #define ifr_mtu ifr_metric 340 #endif 341 if (snaplen > ifr.ifr_mtu + ll_hdrlen) 342 snaplen = ifr.ifr_mtu + ll_hdrlen; 343 #endif 344 345 /* 346 * The argument to SIOCSNOOPLEN is the number of link-layer 347 * payload bytes to capture - it doesn't count link-layer 348 * header bytes. 349 */ 350 snooplen = snaplen - ll_hdrlen; 351 if (snooplen < 0) 352 snooplen = 0; 353 if (ioctl(fd, SIOCSNOOPLEN, &snooplen) < 0) { 354 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SIOCSNOOPLEN: %s", 355 pcap_strerror(errno)); 356 goto bad; 357 } 358 p->snapshot = snaplen; 359 v = 1; 360 if (ioctl(fd, SIOCSNOOPING, &v) < 0) { 361 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SIOCSNOOPING: %s", 362 pcap_strerror(errno)); 363 goto bad; 364 } 365 366 p->bufsize = 4096; /* XXX */ 367 p->buffer = (u_char *)malloc(p->bufsize); 368 if (p->buffer == NULL) { 369 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", 370 pcap_strerror(errno)); 371 goto bad; 372 } 373 374 /* 375 * "p->fd" is a socket, so "select()" should work on it. 376 */ 377 p->selectable_fd = p->fd; 378 379 p->read_op = pcap_read_snoop; 380 p->inject_op = pcap_inject_snoop; 381 p->setfilter_op = install_bpf_program; /* no kernel filtering */ 382 p->setdirection_op = NULL; /* Not implemented. */ 383 p->set_datalink_op = NULL; /* can't change data link type */ 384 p->getnonblock_op = pcap_getnonblock_fd; 385 p->setnonblock_op = pcap_setnonblock_fd; 386 p->stats_op = pcap_stats_snoop; 387 p->close_op = pcap_close_common; 388 389 return (p); 390 bad: 391 (void)close(fd); 392 /* 393 * Get rid of any link-layer type list we allocated. 394 */ 395 if (p->dlt_list != NULL) 396 free(p->dlt_list); 397 free(p); 398 return (NULL); 399 } 400 401 int 402 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 403 { 404 return (0); 405 } 406