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 * This code contributed by Atanu Ghosh (atanu (at) cs.ucl.ac.uk), 22 * University College London, and subsequently modified by 23 * Guy Harris (guy (at) alum.mit.edu), Mark Pizzolato 24 * <List-tcpdump-workers (at) subscriptions.pizzolato.net>, 25 * Mark C. Brown (mbrown (at) hp.com), and Sagun Shakya <Sagun.Shakya (at) Sun.COM>. 26 */ 27 28 /* 29 * Packet capture routine for DLPI under SunOS 5, HP-UX 9/10/11, and AIX. 30 * 31 * Notes: 32 * 33 * - The DLIOCRAW ioctl() is specific to SunOS. 34 * 35 * - There is a bug in bufmod(7) such that setting the snapshot 36 * length results in data being left of the front of the packet. 37 * 38 * - It might be desirable to use pfmod(7) to filter packets in the 39 * kernel when possible. 40 * 41 * - An older version of the HP-UX DLPI Programmer's Guide, which 42 * I think was advertised as the 10.20 version, used to be available 43 * at 44 * 45 * http://docs.hp.com/hpux/onlinedocs/B2355-90093/B2355-90093.html 46 * 47 * but is no longer available; it can still be found at 48 * 49 * http://h21007.www2.hp.com/dspp/files/unprotected/Drivers/Docs/Refs/B2355-90093.pdf 50 * 51 * in PDF form. 52 * 53 * - The HP-UX 10.x, 11.0, and 11i v1.6 version of the HP-UX DLPI 54 * Programmer's Guide, which I think was once advertised as the 55 * 11.00 version is available at 56 * 57 * http://docs.hp.com/en/B2355-90139/index.html 58 * 59 * - The HP-UX 11i v2 version of the HP-UX DLPI Programmer's Guide 60 * is available at 61 * 62 * http://docs.hp.com/en/B2355-90871/index.html 63 * 64 * - All of the HP documents describe raw-mode services, which are 65 * what we use if DL_HP_RAWDLS is defined. XXX - we use __hpux 66 * in some places to test for HP-UX, but use DL_HP_RAWDLS in 67 * other places; do we support any versions of HP-UX without 68 * DL_HP_RAWDLS? 69 */ 70 71 #ifndef lint 72 static const char rcsid[] _U_ = 73 "@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.128 2008-12-02 16:20:23 guy Exp $ (LBL)"; 74 #endif 75 76 #ifdef HAVE_CONFIG_H 77 #include "config.h" 78 #endif 79 80 #include <sys/types.h> 81 #include <sys/time.h> 82 #ifdef HAVE_SYS_BUFMOD_H 83 #include <sys/bufmod.h> 84 #endif 85 #include <sys/dlpi.h> 86 #ifdef HAVE_SYS_DLPI_EXT_H 87 #include <sys/dlpi_ext.h> 88 #endif 89 #ifdef HAVE_HPUX9 90 #include <sys/socket.h> 91 #endif 92 #ifdef DL_HP_PPA_REQ 93 #include <sys/stat.h> 94 #endif 95 #include <sys/stream.h> 96 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H) 97 #include <sys/systeminfo.h> 98 #endif 99 100 #ifdef HAVE_HPUX9 101 #include <net/if.h> 102 #endif 103 104 #include <ctype.h> 105 #ifdef HAVE_HPUX9 106 #include <nlist.h> 107 #endif 108 #include <errno.h> 109 #include <fcntl.h> 110 #include <memory.h> 111 #include <stdio.h> 112 #include <stdlib.h> 113 #include <string.h> 114 #include <stropts.h> 115 #include <unistd.h> 116 117 #ifdef HAVE_LIMITS_H 118 #include <limits.h> 119 #else 120 #define INT_MAX 2147483647 121 #endif 122 123 #include "pcap-int.h" 124 #include "dlpisubs.h" 125 126 #ifdef HAVE_OS_PROTO_H 127 #include "os-proto.h" 128 #endif 129 130 #ifndef PCAP_DEV_PREFIX 131 #ifdef _AIX 132 #define PCAP_DEV_PREFIX "/dev/dlpi" 133 #else 134 #define PCAP_DEV_PREFIX "/dev" 135 #endif 136 #endif 137 138 #define MAXDLBUF 8192 139 140 /* Forwards */ 141 static char *split_dname(char *, int *, char *); 142 static int dl_doattach(int, int, char *); 143 #ifdef DL_HP_RAWDLS 144 static int dl_dohpuxbind(int, char *); 145 #endif 146 static int dlpromiscon(pcap_t *, bpf_u_int32); 147 static int dlbindreq(int, bpf_u_int32, char *); 148 static int dlbindack(int, char *, char *, int *); 149 static int dlokack(int, const char *, char *, char *); 150 static int dlinforeq(int, char *); 151 static int dlinfoack(int, char *, char *); 152 153 #ifdef HAVE_DLPI_PASSIVE 154 static void dlpassive(int, char *); 155 #endif 156 157 #ifdef DL_HP_RAWDLS 158 static int dlrawdatareq(int, const u_char *, int); 159 #endif 160 static int recv_ack(int, int, const char *, char *, char *, int *); 161 static char *dlstrerror(bpf_u_int32); 162 static char *dlprim(bpf_u_int32); 163 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H) 164 static char *get_release(bpf_u_int32 *, bpf_u_int32 *, bpf_u_int32 *); 165 #endif 166 static int send_request(int, char *, int, char *, char *); 167 #ifdef HAVE_HPUX9 168 static int dlpi_kread(int, off_t, void *, u_int, char *); 169 #endif 170 #ifdef HAVE_DEV_DLPI 171 static int get_dlpi_ppa(int, const char *, int, char *); 172 #endif 173 174 /* XXX Needed by HP-UX (at least) */ 175 static bpf_u_int32 ctlbuf[MAXDLBUF]; 176 static struct strbuf ctl = { 177 MAXDLBUF, 178 0, 179 (char *)ctlbuf 180 }; 181 182 /* 183 * Cast a buffer to "union DL_primitives" without provoking warnings 184 * from the compiler. 185 */ 186 #define MAKE_DL_PRIMITIVES(ptr) ((union DL_primitives *)(void *)(ptr)) 187 188 static int 189 pcap_read_dlpi(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 190 { 191 int cc; 192 u_char *bp; 193 int flags; 194 struct strbuf data; 195 196 flags = 0; 197 cc = p->cc; 198 if (cc == 0) { 199 data.buf = (char *)p->buffer + p->offset; 200 data.maxlen = p->bufsize; 201 data.len = 0; 202 do { 203 /* 204 * Has "pcap_breakloop()" been called? 205 */ 206 if (p->break_loop) { 207 /* 208 * Yes - clear the flag that indicates 209 * that it has, and return -2 to 210 * indicate that we were told to 211 * break out of the loop. 212 */ 213 p->break_loop = 0; 214 return (-2); 215 } 216 /* 217 * XXX - check for the DLPI primitive, which 218 * would be DL_HP_RAWDATA_IND on HP-UX 219 * if we're in raw mode? 220 */ 221 if (getmsg(p->fd, &ctl, &data, &flags) < 0) { 222 /* Don't choke when we get ptraced */ 223 switch (errno) { 224 225 case EINTR: 226 cc = 0; 227 continue; 228 229 case EAGAIN: 230 return (0); 231 } 232 strlcpy(p->errbuf, pcap_strerror(errno), 233 sizeof(p->errbuf)); 234 return (-1); 235 } 236 cc = data.len; 237 } while (cc == 0); 238 bp = p->buffer + p->offset; 239 } else 240 bp = p->bp; 241 242 return (pcap_process_pkts(p, callback, user, cnt, bp, cc)); 243 } 244 245 static int 246 pcap_inject_dlpi(pcap_t *p, const void *buf, size_t size) 247 { 248 #ifdef DL_HP_RAWDLS 249 struct pcap_dlpi *pd = p->priv; 250 #endif 251 int ret; 252 253 #if defined(DLIOCRAW) 254 ret = write(p->fd, buf, size); 255 if (ret == -1) { 256 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", 257 pcap_strerror(errno)); 258 return (-1); 259 } 260 #elif defined(DL_HP_RAWDLS) 261 if (pd->send_fd < 0) { 262 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 263 "send: Output FD couldn't be opened"); 264 return (-1); 265 } 266 ret = dlrawdatareq(pd->send_fd, buf, size); 267 if (ret == -1) { 268 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", 269 pcap_strerror(errno)); 270 return (-1); 271 } 272 /* 273 * putmsg() returns either 0 or -1; it doesn't indicate how 274 * many bytes were written (presumably they were all written 275 * or none of them were written). OpenBSD's pcap_inject() 276 * returns the number of bytes written, so, for API compatibility, 277 * we return the number of bytes we were told to write. 278 */ 279 ret = size; 280 #else /* no raw mode */ 281 /* 282 * XXX - this is a pain, because you might have to extract 283 * the address from the packet and use it in a DL_UNITDATA_REQ 284 * request. That would be dependent on the link-layer type. 285 * 286 * I also don't know what SAP you'd have to bind the descriptor 287 * to, or whether you'd need separate "receive" and "send" FDs, 288 * nor do I know whether you'd need different bindings for 289 * D/I/X Ethernet and 802.3, or for {FDDI,Token Ring} plus 290 * 802.2 and {FDDI,Token Ring} plus 802.2 plus SNAP. 291 * 292 * So, for now, we just return a "you can't send" indication, 293 * and leave it up to somebody with a DLPI-based system lacking 294 * both DLIOCRAW and DL_HP_RAWDLS to supply code to implement 295 * packet transmission on that system. If they do, they should 296 * send it to us - but should not send us code that assumes 297 * Ethernet; if the code doesn't work on non-Ethernet interfaces, 298 * it should check "p->linktype" and reject the send request if 299 * it's anything other than DLT_EN10MB. 300 */ 301 strlcpy(p->errbuf, "send: Not supported on this version of this OS", 302 PCAP_ERRBUF_SIZE); 303 ret = -1; 304 #endif /* raw mode */ 305 return (ret); 306 } 307 308 #ifndef DL_IPATM 309 #define DL_IPATM 0x12 /* ATM Classical IP interface */ 310 #endif 311 312 #ifdef HAVE_SOLARIS 313 /* 314 * For SunATM. 315 */ 316 #ifndef A_GET_UNITS 317 #define A_GET_UNITS (('A'<<8)|118) 318 #endif /* A_GET_UNITS */ 319 #ifndef A_PROMISCON_REQ 320 #define A_PROMISCON_REQ (('A'<<8)|121) 321 #endif /* A_PROMISCON_REQ */ 322 #endif /* HAVE_SOLARIS */ 323 324 static void 325 pcap_cleanup_dlpi(pcap_t *p) 326 { 327 #ifdef DL_HP_RAWDLS 328 struct pcap_dlpi *pd = p->priv; 329 330 if (pd->send_fd >= 0) { 331 close(pd->send_fd); 332 pd->send_fd = -1; 333 } 334 #endif 335 pcap_cleanup_live_common(p); 336 } 337 338 static int 339 pcap_activate_dlpi(pcap_t *p) 340 { 341 #ifdef DL_HP_RAWDLS 342 struct pcap_dlpi *pd = p->priv; 343 #endif 344 register char *cp; 345 int ppa; 346 #ifdef HAVE_SOLARIS 347 int isatm = 0; 348 #endif 349 register dl_info_ack_t *infop; 350 #ifdef HAVE_SYS_BUFMOD_H 351 bpf_u_int32 ss; 352 #ifdef HAVE_SOLARIS 353 register char *release; 354 bpf_u_int32 osmajor, osminor, osmicro; 355 #endif 356 #endif 357 bpf_u_int32 buf[MAXDLBUF]; 358 char dname[100]; 359 #ifndef HAVE_DEV_DLPI 360 char dname2[100]; 361 #endif 362 int status = PCAP_ERROR; 363 364 #ifdef HAVE_DEV_DLPI 365 /* 366 ** Remove any "/dev/" on the front of the device. 367 */ 368 cp = strrchr(p->opt.source, '/'); 369 if (cp == NULL) 370 strlcpy(dname, p->opt.source, sizeof(dname)); 371 else 372 strlcpy(dname, cp + 1, sizeof(dname)); 373 374 /* 375 * Split the device name into a device type name and a unit number; 376 * chop off the unit number, so "dname" is just a device type name. 377 */ 378 cp = split_dname(dname, &ppa, p->errbuf); 379 if (cp == NULL) { 380 status = PCAP_ERROR_NO_SUCH_DEVICE; 381 goto bad; 382 } 383 *cp = '\0'; 384 385 /* 386 * Use "/dev/dlpi" as the device. 387 * 388 * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that 389 * the "dl_mjr_num" field is for the "major number of interface 390 * driver"; that's the major of "/dev/dlpi" on the system on 391 * which I tried this, but there may be DLPI devices that 392 * use a different driver, in which case we may need to 393 * search "/dev" for the appropriate device with that major 394 * device number, rather than hardwiring "/dev/dlpi". 395 */ 396 cp = "/dev/dlpi"; 397 if ((p->fd = open(cp, O_RDWR)) < 0) { 398 if (errno == EPERM || errno == EACCES) 399 status = PCAP_ERROR_PERM_DENIED; 400 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 401 "%s: %s", cp, pcap_strerror(errno)); 402 goto bad; 403 } 404 405 #ifdef DL_HP_RAWDLS 406 /* 407 * XXX - HP-UX 10.20 and 11.xx don't appear to support sending and 408 * receiving packets on the same descriptor - you need separate 409 * descriptors for sending and receiving, bound to different SAPs. 410 * 411 * If the open fails, we just leave -1 in "pd->send_fd" and reject 412 * attempts to send packets, just as if, in pcap-bpf.c, we fail 413 * to open the BPF device for reading and writing, we just try 414 * to open it for reading only and, if that succeeds, just let 415 * the send attempts fail. 416 */ 417 pd->send_fd = open(cp, O_RDWR); 418 #endif 419 420 /* 421 * Get a table of all PPAs for that device, and search that 422 * table for the specified device type name and unit number. 423 */ 424 ppa = get_dlpi_ppa(p->fd, dname, ppa, p->errbuf); 425 if (ppa < 0) { 426 status = ppa; 427 goto bad; 428 } 429 #else 430 /* 431 * If the device name begins with "/", assume it begins with 432 * the pathname of the directory containing the device to open; 433 * otherwise, concatenate the device directory name and the 434 * device name. 435 */ 436 if (*p->opt.source == '/') 437 strlcpy(dname, p->opt.source, sizeof(dname)); 438 else 439 snprintf(dname, sizeof(dname), "%s/%s", PCAP_DEV_PREFIX, 440 p->opt.source); 441 442 /* 443 * Get the unit number, and a pointer to the end of the device 444 * type name. 445 */ 446 cp = split_dname(dname, &ppa, p->errbuf); 447 if (cp == NULL) { 448 status = PCAP_ERROR_NO_SUCH_DEVICE; 449 goto bad; 450 } 451 452 /* 453 * Make a copy of the device pathname, and then remove the unit 454 * number from the device pathname. 455 */ 456 strlcpy(dname2, dname, sizeof(dname)); 457 *cp = '\0'; 458 459 /* Try device without unit number */ 460 if ((p->fd = open(dname, O_RDWR)) < 0) { 461 if (errno != ENOENT) { 462 if (errno == EPERM || errno == EACCES) 463 status = PCAP_ERROR_PERM_DENIED; 464 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", dname, 465 pcap_strerror(errno)); 466 goto bad; 467 } 468 469 /* Try again with unit number */ 470 if ((p->fd = open(dname2, O_RDWR)) < 0) { 471 if (errno == ENOENT) { 472 status = PCAP_ERROR_NO_SUCH_DEVICE; 473 474 /* 475 * We provide an error message even 476 * for this error, for diagnostic 477 * purposes (so that, for example, 478 * the app can show the message if the 479 * user requests it). 480 * 481 * In it, we just report "No DLPI device 482 * found" with the device name, so people 483 * don't get confused and think, for example, 484 * that if they can't capture on "lo0" 485 * on Solaris the fix is to change libpcap 486 * (or the application that uses it) to 487 * look for something other than "/dev/lo0", 488 * as the fix is to look for an operating 489 * system other than Solaris - you just 490 * *can't* capture on a loopback interface 491 * on Solaris, the lack of a DLPI device 492 * for the loopback interface is just a 493 * symptom of that inability. 494 */ 495 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 496 "%s: No DLPI device found", p->opt.source); 497 } else { 498 if (errno == EPERM || errno == EACCES) 499 status = PCAP_ERROR_PERM_DENIED; 500 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", 501 dname2, pcap_strerror(errno)); 502 } 503 goto bad; 504 } 505 /* XXX Assume unit zero */ 506 ppa = 0; 507 } 508 #endif 509 510 /* 511 ** Attach if "style 2" provider 512 */ 513 if (dlinforeq(p->fd, p->errbuf) < 0 || 514 dlinfoack(p->fd, (char *)buf, p->errbuf) < 0) 515 goto bad; 516 infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack; 517 #ifdef HAVE_SOLARIS 518 if (infop->dl_mac_type == DL_IPATM) 519 isatm = 1; 520 #endif 521 if (infop->dl_provider_style == DL_STYLE2) { 522 status = dl_doattach(p->fd, ppa, p->errbuf); 523 if (status < 0) 524 goto bad; 525 #ifdef DL_HP_RAWDLS 526 if (pd->send_fd >= 0) { 527 if (dl_doattach(pd->send_fd, ppa, p->errbuf) < 0) 528 goto bad; 529 } 530 #endif 531 } 532 533 if (p->opt.rfmon) { 534 /* 535 * This device exists, but we don't support monitor mode 536 * any platforms that support DLPI. 537 */ 538 status = PCAP_ERROR_RFMON_NOTSUP; 539 goto bad; 540 } 541 542 #ifdef HAVE_DLPI_PASSIVE 543 /* 544 * Enable Passive mode to be able to capture on aggregated link. 545 * Not supported in all Solaris versions. 546 */ 547 dlpassive(p->fd, p->errbuf); 548 #endif 549 /* 550 ** Bind (defer if using HP-UX 9 or HP-UX 10.20 or later, totally 551 ** skip if using SINIX) 552 */ 553 #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20_OR_LATER) && !defined(sinix) 554 #ifdef _AIX 555 /* 556 ** AIX. 557 ** According to IBM's AIX Support Line, the dl_sap value 558 ** should not be less than 0x600 (1536) for standard Ethernet. 559 ** However, we seem to get DL_BADADDR - "DLSAP addr in improper 560 ** format or invalid" - errors if we use 1537 on the "tr0" 561 ** device, which, given that its name starts with "tr" and that 562 ** it's IBM, probably means a Token Ring device. (Perhaps we 563 ** need to use 1537 on "/dev/dlpi/en" because that device is for 564 ** D/I/X Ethernet, the "SAP" is actually an Ethernet type, and 565 ** it rejects invalid Ethernet types.) 566 ** 567 ** So if 1537 fails, we try 2, as Hyung Sik Yoon of IBM Korea 568 ** says that works on Token Ring (he says that 0 does *not* 569 ** work; perhaps that's considered an invalid LLC SAP value - I 570 ** assume the SAP value in a DLPI bind is an LLC SAP for network 571 ** types that use 802.2 LLC). 572 */ 573 if ((dlbindreq(p->fd, 1537, p->errbuf) < 0 && 574 dlbindreq(p->fd, 2, p->errbuf) < 0) || 575 dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0) 576 goto bad; 577 #elif defined(DL_HP_RAWDLS) 578 /* 579 ** HP-UX 10.0x and 10.1x. 580 */ 581 if (dl_dohpuxbind(p->fd, p->errbuf) < 0) 582 goto bad; 583 if (pd->send_fd >= 0) { 584 /* 585 ** XXX - if this fails, just close send_fd and 586 ** set it to -1, so that you can't send but can 587 ** still receive? 588 */ 589 if (dl_dohpuxbind(pd->send_fd, p->errbuf) < 0) 590 goto bad; 591 } 592 #else /* neither AIX nor HP-UX */ 593 /* 594 ** Not Sinix, and neither AIX nor HP-UX - Solaris, and any other 595 ** OS using DLPI. 596 **/ 597 if (dlbindreq(p->fd, 0, p->errbuf) < 0 || 598 dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0) 599 goto bad; 600 #endif /* AIX vs. HP-UX vs. other */ 601 #endif /* !HP-UX 9 and !HP-UX 10.20 or later and !SINIX */ 602 603 #ifdef HAVE_SOLARIS 604 if (isatm) { 605 /* 606 ** Have to turn on some special ATM promiscuous mode 607 ** for SunATM. 608 ** Do *NOT* turn regular promiscuous mode on; it doesn't 609 ** help, and may break things. 610 */ 611 if (strioctl(p->fd, A_PROMISCON_REQ, 0, NULL) < 0) { 612 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 613 "A_PROMISCON_REQ: %s", pcap_strerror(errno)); 614 goto bad; 615 } 616 } else 617 #endif 618 if (p->opt.promisc) { 619 /* 620 ** Enable promiscuous (not necessary on send FD) 621 */ 622 status = dlpromiscon(p, DL_PROMISC_PHYS); 623 if (status < 0) { 624 if (status == PCAP_ERROR_PERM_DENIED) 625 status = PCAP_ERROR_PROMISC_PERM_DENIED; 626 goto bad; 627 } 628 629 /* 630 ** Try to enable multicast (you would have thought 631 ** promiscuous would be sufficient). (Skip if using 632 ** HP-UX or SINIX) (Not necessary on send FD) 633 */ 634 #if !defined(__hpux) && !defined(sinix) 635 status = dlpromiscon(p, DL_PROMISC_MULTI); 636 if (status < 0) 637 status = PCAP_WARNING; 638 #endif 639 } 640 /* 641 ** Try to enable SAP promiscuity (when not in promiscuous mode 642 ** when using HP-UX, when not doing SunATM on Solaris, and never 643 ** under SINIX) (Not necessary on send FD) 644 */ 645 #ifndef sinix 646 #if defined(__hpux) 647 /* HP-UX - only do this when not in promiscuous mode */ 648 if (!p->opt.promisc) { 649 #elif defined(HAVE_SOLARIS) 650 /* Solaris - don't do this on SunATM devices */ 651 if (!isatm) { 652 #else 653 /* Everything else (except for SINIX) - always do this */ 654 { 655 #endif 656 status = dlpromiscon(p, DL_PROMISC_SAP); 657 if (status < 0) { 658 /* 659 * Not fatal, since the DL_PROMISC_PHYS mode worked. 660 * Report it as a warning, however. 661 */ 662 if (p->opt.promisc) 663 status = PCAP_WARNING; 664 else 665 goto bad; 666 } 667 } 668 #endif /* sinix */ 669 670 /* 671 ** HP-UX 9, and HP-UX 10.20 or later, must bind after setting 672 ** promiscuous options. 673 */ 674 #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20_OR_LATER) 675 if (dl_dohpuxbind(p->fd, p->errbuf) < 0) 676 goto bad; 677 /* 678 ** We don't set promiscuous mode on the send FD, but we'll defer 679 ** binding it anyway, just to keep the HP-UX 9/10.20 or later 680 ** code together. 681 */ 682 if (pd->send_fd >= 0) { 683 /* 684 ** XXX - if this fails, just close send_fd and 685 ** set it to -1, so that you can't send but can 686 ** still receive? 687 */ 688 if (dl_dohpuxbind(pd->send_fd, p->errbuf) < 0) 689 goto bad; 690 } 691 #endif 692 693 /* 694 ** Determine link type 695 ** XXX - get SAP length and address length as well, for use 696 ** when sending packets. 697 */ 698 if (dlinforeq(p->fd, p->errbuf) < 0 || 699 dlinfoack(p->fd, (char *)buf, p->errbuf) < 0) 700 goto bad; 701 702 infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack; 703 if (pcap_process_mactype(p, infop->dl_mac_type) != 0) 704 goto bad; 705 706 #ifdef DLIOCRAW 707 /* 708 ** This is a non standard SunOS hack to get the full raw link-layer 709 ** header. 710 */ 711 if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) { 712 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "DLIOCRAW: %s", 713 pcap_strerror(errno)); 714 goto bad; 715 } 716 #endif 717 718 #ifdef HAVE_SYS_BUFMOD_H 719 ss = p->snapshot; 720 721 /* 722 ** There is a bug in bufmod(7). When dealing with messages of 723 ** less than snaplen size it strips data from the beginning not 724 ** the end. 725 ** 726 ** This bug is fixed in 5.3.2. Also, there is a patch available. 727 ** Ask for bugid 1149065. 728 */ 729 #ifdef HAVE_SOLARIS 730 release = get_release(&osmajor, &osminor, &osmicro); 731 if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) && 732 getenv("BUFMOD_FIXED") == NULL) { 733 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 734 "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.", 735 release); 736 ss = 0; 737 status = PCAP_WARNING; 738 } 739 #endif 740 741 /* Push and configure bufmod. */ 742 if (pcap_conf_bufmod(p, ss) != 0) 743 goto bad; 744 #endif 745 746 /* 747 ** As the last operation flush the read side. 748 */ 749 if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) { 750 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s", 751 pcap_strerror(errno)); 752 goto bad; 753 } 754 755 /* Allocate data buffer. */ 756 if (pcap_alloc_databuf(p) != 0) 757 goto bad; 758 759 /* Success - but perhaps with a warning */ 760 if (status < 0) 761 status = 0; 762 763 /* 764 * "p->fd" is an FD for a STREAMS device, so "select()" and 765 * "poll()" should work on it. 766 */ 767 p->selectable_fd = p->fd; 768 769 p->read_op = pcap_read_dlpi; 770 p->inject_op = pcap_inject_dlpi; 771 p->setfilter_op = install_bpf_program; /* no kernel filtering */ 772 p->setdirection_op = NULL; /* Not implemented.*/ 773 p->set_datalink_op = NULL; /* can't change data link type */ 774 p->getnonblock_op = pcap_getnonblock_fd; 775 p->setnonblock_op = pcap_setnonblock_fd; 776 p->stats_op = pcap_stats_dlpi; 777 p->cleanup_op = pcap_cleanup_dlpi; 778 779 return (status); 780 bad: 781 pcap_cleanup_dlpi(p); 782 return (status); 783 } 784 785 /* 786 * Split a device name into a device type name and a unit number; 787 * return the a pointer to the beginning of the unit number, which 788 * is the end of the device type name, and set "*unitp" to the unit 789 * number. 790 * 791 * Returns NULL on error, and fills "ebuf" with an error message. 792 */ 793 static char * 794 split_dname(char *device, int *unitp, char *ebuf) 795 { 796 char *cp; 797 char *eos; 798 long unit; 799 800 /* 801 * Look for a number at the end of the device name string. 802 */ 803 cp = device + strlen(device) - 1; 804 if (*cp < '0' || *cp > '9') { 805 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s missing unit number", 806 device); 807 return (NULL); 808 } 809 810 /* Digits at end of string are unit number */ 811 while (cp-1 >= device && *(cp-1) >= '0' && *(cp-1) <= '9') 812 cp--; 813 814 errno = 0; 815 unit = strtol(cp, &eos, 10); 816 if (*eos != '\0') { 817 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s bad unit number", device); 818 return (NULL); 819 } 820 if (errno == ERANGE || unit > INT_MAX) { 821 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number too large", 822 device); 823 return (NULL); 824 } 825 if (unit < 0) { 826 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number is negative", 827 device); 828 return (NULL); 829 } 830 *unitp = (int)unit; 831 return (cp); 832 } 833 834 static int 835 dl_doattach(int fd, int ppa, char *ebuf) 836 { 837 dl_attach_req_t req; 838 bpf_u_int32 buf[MAXDLBUF]; 839 int err; 840 841 req.dl_primitive = DL_ATTACH_REQ; 842 req.dl_ppa = ppa; 843 if (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf) < 0) 844 return (PCAP_ERROR); 845 846 err = dlokack(fd, "attach", (char *)buf, ebuf); 847 if (err < 0) 848 return (err); 849 return (0); 850 } 851 852 #ifdef DL_HP_RAWDLS 853 static int 854 dl_dohpuxbind(int fd, char *ebuf) 855 { 856 int hpsap; 857 int uerror; 858 bpf_u_int32 buf[MAXDLBUF]; 859 860 /* 861 * XXX - we start at 22 because we used to use only 22, but 862 * that was just because that was the value used in some 863 * sample code from HP. With what value *should* we start? 864 * Does it matter, given that we're enabling SAP promiscuity 865 * on the input FD? 866 */ 867 hpsap = 22; 868 for (;;) { 869 if (dlbindreq(fd, hpsap, ebuf) < 0) 870 return (-1); 871 if (dlbindack(fd, (char *)buf, ebuf, &uerror) >= 0) 872 break; 873 /* 874 * For any error other than a UNIX EBUSY, give up. 875 */ 876 if (uerror != EBUSY) { 877 /* 878 * dlbindack() has already filled in ebuf for 879 * this error. 880 */ 881 return (-1); 882 } 883 884 /* 885 * For EBUSY, try the next SAP value; that means that 886 * somebody else is using that SAP. Clear ebuf so 887 * that application doesn't report the "Device busy" 888 * error as a warning. 889 */ 890 *ebuf = '\0'; 891 hpsap++; 892 if (hpsap > 100) { 893 strlcpy(ebuf, 894 "All SAPs from 22 through 100 are in use", 895 PCAP_ERRBUF_SIZE); 896 return (-1); 897 } 898 } 899 return (0); 900 } 901 #endif 902 903 #define STRINGIFY(n) #n 904 905 static int 906 dlpromiscon(pcap_t *p, bpf_u_int32 level) 907 { 908 dl_promiscon_req_t req; 909 bpf_u_int32 buf[MAXDLBUF]; 910 int err; 911 912 req.dl_primitive = DL_PROMISCON_REQ; 913 req.dl_level = level; 914 if (send_request(p->fd, (char *)&req, sizeof(req), "promiscon", 915 p->errbuf) < 0) 916 return (PCAP_ERROR); 917 err = dlokack(p->fd, "promiscon" STRINGIFY(level), (char *)buf, 918 p->errbuf); 919 if (err < 0) 920 return (err); 921 return (0); 922 } 923 924 int 925 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 926 { 927 #ifdef HAVE_SOLARIS 928 int fd; 929 union { 930 u_int nunits; 931 char pad[516]; /* XXX - must be at least 513; is 516 932 in "atmgetunits" */ 933 } buf; 934 char baname[2+1+1]; 935 u_int i; 936 937 /* 938 * We may have to do special magic to get ATM devices. 939 */ 940 if ((fd = open("/dev/ba", O_RDWR)) < 0) { 941 /* 942 * We couldn't open the "ba" device. 943 * For now, just give up; perhaps we should 944 * return an error if the problem is neither 945 * a "that device doesn't exist" error (ENOENT, 946 * ENXIO, etc.) or a "you're not allowed to do 947 * that" error (EPERM, EACCES). 948 */ 949 return (0); 950 } 951 952 if (strioctl(fd, A_GET_UNITS, sizeof(buf), (char *)&buf) < 0) { 953 snprintf(errbuf, PCAP_ERRBUF_SIZE, "A_GET_UNITS: %s", 954 pcap_strerror(errno)); 955 return (-1); 956 } 957 for (i = 0; i < buf.nunits; i++) { 958 snprintf(baname, sizeof baname, "ba%u", i); 959 if (pcap_add_if(alldevsp, baname, 0, NULL, errbuf) < 0) 960 return (-1); 961 } 962 #endif 963 964 return (0); 965 } 966 967 static int 968 send_request(int fd, char *ptr, int len, char *what, char *ebuf) 969 { 970 struct strbuf ctl; 971 int flags; 972 973 ctl.maxlen = 0; 974 ctl.len = len; 975 ctl.buf = ptr; 976 977 flags = 0; 978 if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) { 979 snprintf(ebuf, PCAP_ERRBUF_SIZE, 980 "send_request: putmsg \"%s\": %s", 981 what, pcap_strerror(errno)); 982 return (-1); 983 } 984 return (0); 985 } 986 987 static int 988 recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf, int *uerror) 989 { 990 union DL_primitives *dlp; 991 struct strbuf ctl; 992 int flags; 993 994 /* 995 * Clear out "*uerror", so it's only set for DL_ERROR_ACK/DL_SYSERR, 996 * making that the only place where EBUSY is treated specially. 997 */ 998 if (uerror != NULL) 999 *uerror = 0; 1000 1001 ctl.maxlen = MAXDLBUF; 1002 ctl.len = 0; 1003 ctl.buf = bufp; 1004 1005 flags = 0; 1006 if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) { 1007 snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s getmsg: %s", 1008 what, pcap_strerror(errno)); 1009 return (PCAP_ERROR); 1010 } 1011 1012 dlp = MAKE_DL_PRIMITIVES(ctl.buf); 1013 switch (dlp->dl_primitive) { 1014 1015 case DL_INFO_ACK: 1016 case DL_BIND_ACK: 1017 case DL_OK_ACK: 1018 #ifdef DL_HP_PPA_ACK 1019 case DL_HP_PPA_ACK: 1020 #endif 1021 /* These are OK */ 1022 break; 1023 1024 case DL_ERROR_ACK: 1025 switch (dlp->error_ack.dl_errno) { 1026 1027 case DL_SYSERR: 1028 if (uerror != NULL) 1029 *uerror = dlp->error_ack.dl_unix_errno; 1030 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1031 "recv_ack: %s: UNIX error - %s", 1032 what, pcap_strerror(dlp->error_ack.dl_unix_errno)); 1033 if (dlp->error_ack.dl_unix_errno == EPERM || 1034 dlp->error_ack.dl_unix_errno == EACCES) 1035 return (PCAP_ERROR_PERM_DENIED); 1036 break; 1037 1038 default: 1039 snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s: %s", 1040 what, dlstrerror(dlp->error_ack.dl_errno)); 1041 if (dlp->error_ack.dl_errno == DL_BADPPA) 1042 return (PCAP_ERROR_NO_SUCH_DEVICE); 1043 else if (dlp->error_ack.dl_errno == DL_ACCESS) 1044 return (PCAP_ERROR_PERM_DENIED); 1045 break; 1046 } 1047 return (PCAP_ERROR); 1048 1049 default: 1050 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1051 "recv_ack: %s: Unexpected primitive ack %s", 1052 what, dlprim(dlp->dl_primitive)); 1053 return (PCAP_ERROR); 1054 } 1055 1056 if (ctl.len < size) { 1057 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1058 "recv_ack: %s: Ack too small (%d < %d)", 1059 what, ctl.len, size); 1060 return (PCAP_ERROR); 1061 } 1062 return (ctl.len); 1063 } 1064 1065 static char * 1066 dlstrerror(bpf_u_int32 dl_errno) 1067 { 1068 static char errstring[6+2+8+1]; 1069 1070 switch (dl_errno) { 1071 1072 case DL_ACCESS: 1073 return ("Improper permissions for request"); 1074 1075 case DL_BADADDR: 1076 return ("DLSAP addr in improper format or invalid"); 1077 1078 case DL_BADCORR: 1079 return ("Seq number not from outstand DL_CONN_IND"); 1080 1081 case DL_BADDATA: 1082 return ("User data exceeded provider limit"); 1083 1084 case DL_BADPPA: 1085 #ifdef HAVE_DEV_DLPI 1086 /* 1087 * With a single "/dev/dlpi" device used for all 1088 * DLPI providers, PPAs have nothing to do with 1089 * unit numbers. 1090 */ 1091 return ("Specified PPA was invalid"); 1092 #else 1093 /* 1094 * We have separate devices for separate devices; 1095 * the PPA is just the unit number. 1096 */ 1097 return ("Specified PPA (device unit) was invalid"); 1098 #endif 1099 1100 case DL_BADPRIM: 1101 return ("Primitive received not known by provider"); 1102 1103 case DL_BADQOSPARAM: 1104 return ("QOS parameters contained invalid values"); 1105 1106 case DL_BADQOSTYPE: 1107 return ("QOS structure type is unknown/unsupported"); 1108 1109 case DL_BADSAP: 1110 return ("Bad LSAP selector"); 1111 1112 case DL_BADTOKEN: 1113 return ("Token used not an active stream"); 1114 1115 case DL_BOUND: 1116 return ("Attempted second bind with dl_max_conind"); 1117 1118 case DL_INITFAILED: 1119 return ("Physical link initialization failed"); 1120 1121 case DL_NOADDR: 1122 return ("Provider couldn't allocate alternate address"); 1123 1124 case DL_NOTINIT: 1125 return ("Physical link not initialized"); 1126 1127 case DL_OUTSTATE: 1128 return ("Primitive issued in improper state"); 1129 1130 case DL_SYSERR: 1131 return ("UNIX system error occurred"); 1132 1133 case DL_UNSUPPORTED: 1134 return ("Requested service not supplied by provider"); 1135 1136 case DL_UNDELIVERABLE: 1137 return ("Previous data unit could not be delivered"); 1138 1139 case DL_NOTSUPPORTED: 1140 return ("Primitive is known but not supported"); 1141 1142 case DL_TOOMANY: 1143 return ("Limit exceeded"); 1144 1145 case DL_NOTENAB: 1146 return ("Promiscuous mode not enabled"); 1147 1148 case DL_BUSY: 1149 return ("Other streams for PPA in post-attached"); 1150 1151 case DL_NOAUTO: 1152 return ("Automatic handling XID&TEST not supported"); 1153 1154 case DL_NOXIDAUTO: 1155 return ("Automatic handling of XID not supported"); 1156 1157 case DL_NOTESTAUTO: 1158 return ("Automatic handling of TEST not supported"); 1159 1160 case DL_XIDAUTO: 1161 return ("Automatic handling of XID response"); 1162 1163 case DL_TESTAUTO: 1164 return ("Automatic handling of TEST response"); 1165 1166 case DL_PENDING: 1167 return ("Pending outstanding connect indications"); 1168 1169 default: 1170 sprintf(errstring, "Error %02x", dl_errno); 1171 return (errstring); 1172 } 1173 } 1174 1175 static char * 1176 dlprim(bpf_u_int32 prim) 1177 { 1178 static char primbuf[80]; 1179 1180 switch (prim) { 1181 1182 case DL_INFO_REQ: 1183 return ("DL_INFO_REQ"); 1184 1185 case DL_INFO_ACK: 1186 return ("DL_INFO_ACK"); 1187 1188 case DL_ATTACH_REQ: 1189 return ("DL_ATTACH_REQ"); 1190 1191 case DL_DETACH_REQ: 1192 return ("DL_DETACH_REQ"); 1193 1194 case DL_BIND_REQ: 1195 return ("DL_BIND_REQ"); 1196 1197 case DL_BIND_ACK: 1198 return ("DL_BIND_ACK"); 1199 1200 case DL_UNBIND_REQ: 1201 return ("DL_UNBIND_REQ"); 1202 1203 case DL_OK_ACK: 1204 return ("DL_OK_ACK"); 1205 1206 case DL_ERROR_ACK: 1207 return ("DL_ERROR_ACK"); 1208 1209 case DL_SUBS_BIND_REQ: 1210 return ("DL_SUBS_BIND_REQ"); 1211 1212 case DL_SUBS_BIND_ACK: 1213 return ("DL_SUBS_BIND_ACK"); 1214 1215 case DL_UNITDATA_REQ: 1216 return ("DL_UNITDATA_REQ"); 1217 1218 case DL_UNITDATA_IND: 1219 return ("DL_UNITDATA_IND"); 1220 1221 case DL_UDERROR_IND: 1222 return ("DL_UDERROR_IND"); 1223 1224 case DL_UDQOS_REQ: 1225 return ("DL_UDQOS_REQ"); 1226 1227 case DL_CONNECT_REQ: 1228 return ("DL_CONNECT_REQ"); 1229 1230 case DL_CONNECT_IND: 1231 return ("DL_CONNECT_IND"); 1232 1233 case DL_CONNECT_RES: 1234 return ("DL_CONNECT_RES"); 1235 1236 case DL_CONNECT_CON: 1237 return ("DL_CONNECT_CON"); 1238 1239 case DL_TOKEN_REQ: 1240 return ("DL_TOKEN_REQ"); 1241 1242 case DL_TOKEN_ACK: 1243 return ("DL_TOKEN_ACK"); 1244 1245 case DL_DISCONNECT_REQ: 1246 return ("DL_DISCONNECT_REQ"); 1247 1248 case DL_DISCONNECT_IND: 1249 return ("DL_DISCONNECT_IND"); 1250 1251 case DL_RESET_REQ: 1252 return ("DL_RESET_REQ"); 1253 1254 case DL_RESET_IND: 1255 return ("DL_RESET_IND"); 1256 1257 case DL_RESET_RES: 1258 return ("DL_RESET_RES"); 1259 1260 case DL_RESET_CON: 1261 return ("DL_RESET_CON"); 1262 1263 default: 1264 (void) sprintf(primbuf, "unknown primitive 0x%x", prim); 1265 return (primbuf); 1266 } 1267 } 1268 1269 static int 1270 dlbindreq(int fd, bpf_u_int32 sap, char *ebuf) 1271 { 1272 1273 dl_bind_req_t req; 1274 1275 memset((char *)&req, 0, sizeof(req)); 1276 req.dl_primitive = DL_BIND_REQ; 1277 /* XXX - what if neither of these are defined? */ 1278 #if defined(DL_HP_RAWDLS) 1279 req.dl_max_conind = 1; /* XXX magic number */ 1280 req.dl_service_mode = DL_HP_RAWDLS; 1281 #elif defined(DL_CLDLS) 1282 req.dl_service_mode = DL_CLDLS; 1283 #endif 1284 req.dl_sap = sap; 1285 1286 return (send_request(fd, (char *)&req, sizeof(req), "bind", ebuf)); 1287 } 1288 1289 static int 1290 dlbindack(int fd, char *bufp, char *ebuf, int *uerror) 1291 { 1292 1293 return (recv_ack(fd, DL_BIND_ACK_SIZE, "bind", bufp, ebuf, uerror)); 1294 } 1295 1296 static int 1297 dlokack(int fd, const char *what, char *bufp, char *ebuf) 1298 { 1299 1300 return (recv_ack(fd, DL_OK_ACK_SIZE, what, bufp, ebuf, NULL)); 1301 } 1302 1303 1304 static int 1305 dlinforeq(int fd, char *ebuf) 1306 { 1307 dl_info_req_t req; 1308 1309 req.dl_primitive = DL_INFO_REQ; 1310 1311 return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf)); 1312 } 1313 1314 static int 1315 dlinfoack(int fd, char *bufp, char *ebuf) 1316 { 1317 1318 return (recv_ack(fd, DL_INFO_ACK_SIZE, "info", bufp, ebuf, NULL)); 1319 } 1320 1321 #ifdef HAVE_DLPI_PASSIVE 1322 /* 1323 * Enable DLPI passive mode. We do not care if this request fails, as this 1324 * indicates the underlying DLPI device does not support link aggregation. 1325 */ 1326 static void 1327 dlpassive(int fd, char *ebuf) 1328 { 1329 dl_passive_req_t req; 1330 bpf_u_int32 buf[MAXDLBUF]; 1331 1332 req.dl_primitive = DL_PASSIVE_REQ; 1333 1334 if (send_request(fd, (char *)&req, sizeof(req), "dlpassive", ebuf) == 0) 1335 (void) dlokack(fd, "dlpassive", (char *)buf, ebuf); 1336 } 1337 #endif 1338 1339 #ifdef DL_HP_RAWDLS 1340 /* 1341 * There's an ack *if* there's an error. 1342 */ 1343 static int 1344 dlrawdatareq(int fd, const u_char *datap, int datalen) 1345 { 1346 struct strbuf ctl, data; 1347 long buf[MAXDLBUF]; /* XXX - char? */ 1348 union DL_primitives *dlp; 1349 int dlen; 1350 1351 dlp = MAKE_DL_PRIMITIVES(buf); 1352 1353 dlp->dl_primitive = DL_HP_RAWDATA_REQ; 1354 dlen = DL_HP_RAWDATA_REQ_SIZE; 1355 1356 /* 1357 * HP's documentation doesn't appear to show us supplying any 1358 * address pointed to by the control part of the message. 1359 * I think that's what raw mode means - you just send the raw 1360 * packet, you don't specify where to send it to, as that's 1361 * implied by the destination address. 1362 */ 1363 ctl.maxlen = 0; 1364 ctl.len = dlen; 1365 ctl.buf = (void *)buf; 1366 1367 data.maxlen = 0; 1368 data.len = datalen; 1369 data.buf = (void *)datap; 1370 1371 return (putmsg(fd, &ctl, &data, 0)); 1372 } 1373 #endif /* DL_HP_RAWDLS */ 1374 1375 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H) 1376 static char * 1377 get_release(bpf_u_int32 *majorp, bpf_u_int32 *minorp, bpf_u_int32 *microp) 1378 { 1379 char *cp; 1380 static char buf[32]; 1381 1382 *majorp = 0; 1383 *minorp = 0; 1384 *microp = 0; 1385 if (sysinfo(SI_RELEASE, buf, sizeof(buf)) < 0) 1386 return ("?"); 1387 cp = buf; 1388 if (!isdigit((unsigned char)*cp)) 1389 return (buf); 1390 *majorp = strtol(cp, &cp, 10); 1391 if (*cp++ != '.') 1392 return (buf); 1393 *minorp = strtol(cp, &cp, 10); 1394 if (*cp++ != '.') 1395 return (buf); 1396 *microp = strtol(cp, &cp, 10); 1397 return (buf); 1398 } 1399 #endif 1400 1401 #ifdef DL_HP_PPA_REQ 1402 /* 1403 * Under HP-UX 10 and HP-UX 11, we can ask for the ppa 1404 */ 1405 1406 1407 /* 1408 * Determine ppa number that specifies ifname. 1409 * 1410 * If the "dl_hp_ppa_info_t" doesn't have a "dl_module_id_1" member, 1411 * the code that's used here is the old code for HP-UX 10.x. 1412 * 1413 * However, HP-UX 10.20, at least, appears to have such a member 1414 * in its "dl_hp_ppa_info_t" structure, so the new code is used. 1415 * The new code didn't work on an old 10.20 system on which Rick 1416 * Jones of HP tried it, but with later patches installed, it 1417 * worked - it appears that the older system had those members but 1418 * didn't put anything in them, so, if the search by name fails, we 1419 * do the old search. 1420 * 1421 * Rick suggests that making sure your system is "up on the latest 1422 * lancommon/DLPI/driver patches" is probably a good idea; it'd fix 1423 * that problem, as well as allowing libpcap to see packets sent 1424 * from the system on which the libpcap application is being run. 1425 * (On 10.20, in addition to getting the latest patches, you need 1426 * to turn the kernel "lanc_outbound_promisc_flag" flag on with ADB; 1427 * a posting to "comp.sys.hp.hpux" at 1428 * 1429 * http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=558092266 1430 * 1431 * says that, to see the machine's outgoing traffic, you'd need to 1432 * apply the right patches to your system, and also set that variable 1433 * with: 1434 1435 echo 'lanc_outbound_promisc_flag/W1' | /usr/bin/adb -w /stand/vmunix /dev/kmem 1436 1437 * which could be put in, for example, "/sbin/init.d/lan". 1438 * 1439 * Setting the variable is not necessary on HP-UX 11.x. 1440 */ 1441 static int 1442 get_dlpi_ppa(register int fd, register const char *device, register int unit, 1443 register char *ebuf) 1444 { 1445 register dl_hp_ppa_ack_t *ap; 1446 register dl_hp_ppa_info_t *ipstart, *ip; 1447 register int i; 1448 char dname[100]; 1449 register u_long majdev; 1450 struct stat statbuf; 1451 dl_hp_ppa_req_t req; 1452 char buf[MAXDLBUF]; 1453 char *ppa_data_buf; 1454 dl_hp_ppa_ack_t *dlp; 1455 struct strbuf ctl; 1456 int flags; 1457 int ppa; 1458 1459 memset((char *)&req, 0, sizeof(req)); 1460 req.dl_primitive = DL_HP_PPA_REQ; 1461 1462 memset((char *)buf, 0, sizeof(buf)); 1463 if (send_request(fd, (char *)&req, sizeof(req), "hpppa", ebuf) < 0) 1464 return (PCAP_ERROR); 1465 1466 ctl.maxlen = DL_HP_PPA_ACK_SIZE; 1467 ctl.len = 0; 1468 ctl.buf = (char *)buf; 1469 1470 flags = 0; 1471 /* 1472 * DLPI may return a big chunk of data for a DL_HP_PPA_REQ. The normal 1473 * recv_ack will fail because it set the maxlen to MAXDLBUF (8192) 1474 * which is NOT big enough for a DL_HP_PPA_REQ. 1475 * 1476 * This causes libpcap applications to fail on a system with HP-APA 1477 * installed. 1478 * 1479 * To figure out how big the returned data is, we first call getmsg 1480 * to get the small head and peek at the head to get the actual data 1481 * length, and then issue another getmsg to get the actual PPA data. 1482 */ 1483 /* get the head first */ 1484 if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) { 1485 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1486 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno)); 1487 return (PCAP_ERROR); 1488 } 1489 1490 dlp = (dl_hp_ppa_ack_t *)ctl.buf; 1491 if (dlp->dl_primitive != DL_HP_PPA_ACK) { 1492 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1493 "get_dlpi_ppa: hpppa unexpected primitive ack 0x%x", 1494 (bpf_u_int32)dlp->dl_primitive); 1495 return (PCAP_ERROR); 1496 } 1497 1498 if (ctl.len < DL_HP_PPA_ACK_SIZE) { 1499 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1500 "get_dlpi_ppa: hpppa ack too small (%d < %lu)", 1501 ctl.len, (unsigned long)DL_HP_PPA_ACK_SIZE); 1502 return (PCAP_ERROR); 1503 } 1504 1505 /* allocate buffer */ 1506 if ((ppa_data_buf = (char *)malloc(dlp->dl_length)) == NULL) { 1507 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1508 "get_dlpi_ppa: hpppa malloc: %s", pcap_strerror(errno)); 1509 return (PCAP_ERROR); 1510 } 1511 ctl.maxlen = dlp->dl_length; 1512 ctl.len = 0; 1513 ctl.buf = (char *)ppa_data_buf; 1514 /* get the data */ 1515 if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) { 1516 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1517 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno)); 1518 free(ppa_data_buf); 1519 return (PCAP_ERROR); 1520 } 1521 if (ctl.len < dlp->dl_length) { 1522 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1523 "get_dlpi_ppa: hpppa ack too small (%d < %lu)", 1524 ctl.len, (unsigned long)dlp->dl_length); 1525 free(ppa_data_buf); 1526 return (PCAP_ERROR); 1527 } 1528 1529 ap = (dl_hp_ppa_ack_t *)buf; 1530 ipstart = (dl_hp_ppa_info_t *)ppa_data_buf; 1531 ip = ipstart; 1532 1533 #ifdef HAVE_HP_PPA_INFO_T_DL_MODULE_ID_1 1534 /* 1535 * The "dl_hp_ppa_info_t" structure has a "dl_module_id_1" 1536 * member that should, in theory, contain the part of the 1537 * name for the device that comes before the unit number, 1538 * and should also have a "dl_module_id_2" member that may 1539 * contain an alternate name (e.g., I think Ethernet devices 1540 * have both "lan", for "lanN", and "snap", for "snapN", with 1541 * the former being for Ethernet packets and the latter being 1542 * for 802.3/802.2 packets). 1543 * 1544 * Search for the device that has the specified name and 1545 * instance number. 1546 */ 1547 for (i = 0; i < ap->dl_count; i++) { 1548 if ((strcmp((const char *)ip->dl_module_id_1, device) == 0 || 1549 strcmp((const char *)ip->dl_module_id_2, device) == 0) && 1550 ip->dl_instance_num == unit) 1551 break; 1552 1553 ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset); 1554 } 1555 #else 1556 /* 1557 * We don't have that member, so the search is impossible; make it 1558 * look as if the search failed. 1559 */ 1560 i = ap->dl_count; 1561 #endif 1562 1563 if (i == ap->dl_count) { 1564 /* 1565 * Well, we didn't, or can't, find the device by name. 1566 * 1567 * HP-UX 10.20, whilst it has "dl_module_id_1" and 1568 * "dl_module_id_2" fields in the "dl_hp_ppa_info_t", 1569 * doesn't seem to fill them in unless the system is 1570 * at a reasonably up-to-date patch level. 1571 * 1572 * Older HP-UX 10.x systems might not have those fields 1573 * at all. 1574 * 1575 * Therefore, we'll search for the entry with the major 1576 * device number of a device with the name "/dev/<dev><unit>", 1577 * if such a device exists, as the old code did. 1578 */ 1579 snprintf(dname, sizeof(dname), "/dev/%s%d", device, unit); 1580 if (stat(dname, &statbuf) < 0) { 1581 snprintf(ebuf, PCAP_ERRBUF_SIZE, "stat: %s: %s", 1582 dname, pcap_strerror(errno)); 1583 return (PCAP_ERROR); 1584 } 1585 majdev = major(statbuf.st_rdev); 1586 1587 ip = ipstart; 1588 1589 for (i = 0; i < ap->dl_count; i++) { 1590 if (ip->dl_mjr_num == majdev && 1591 ip->dl_instance_num == unit) 1592 break; 1593 1594 ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset); 1595 } 1596 } 1597 if (i == ap->dl_count) { 1598 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1599 "can't find /dev/dlpi PPA for %s%d", device, unit); 1600 return (PCAP_ERROR_NO_SUCH_DEVICE); 1601 } 1602 if (ip->dl_hdw_state == HDW_DEAD) { 1603 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1604 "%s%d: hardware state: DOWN\n", device, unit); 1605 free(ppa_data_buf); 1606 return (PCAP_ERROR); 1607 } 1608 ppa = ip->dl_ppa; 1609 free(ppa_data_buf); 1610 return (ppa); 1611 } 1612 #endif 1613 1614 #ifdef HAVE_HPUX9 1615 /* 1616 * Under HP-UX 9, there is no good way to determine the ppa. 1617 * So punt and read it from /dev/kmem. 1618 */ 1619 static struct nlist nl[] = { 1620 #define NL_IFNET 0 1621 { "ifnet" }, 1622 { "" } 1623 }; 1624 1625 static char path_vmunix[] = "/hp-ux"; 1626 1627 /* Determine ppa number that specifies ifname */ 1628 static int 1629 get_dlpi_ppa(register int fd, register const char *ifname, register int unit, 1630 register char *ebuf) 1631 { 1632 register const char *cp; 1633 register int kd; 1634 void *addr; 1635 struct ifnet ifnet; 1636 char if_name[sizeof(ifnet.if_name) + 1]; 1637 1638 cp = strrchr(ifname, '/'); 1639 if (cp != NULL) 1640 ifname = cp + 1; 1641 if (nlist(path_vmunix, &nl) < 0) { 1642 snprintf(ebuf, PCAP_ERRBUF_SIZE, "nlist %s failed", 1643 path_vmunix); 1644 return (-1); 1645 } 1646 if (nl[NL_IFNET].n_value == 0) { 1647 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1648 "could't find %s kernel symbol", 1649 nl[NL_IFNET].n_name); 1650 return (-1); 1651 } 1652 kd = open("/dev/kmem", O_RDONLY); 1653 if (kd < 0) { 1654 snprintf(ebuf, PCAP_ERRBUF_SIZE, "kmem open: %s", 1655 pcap_strerror(errno)); 1656 return (-1); 1657 } 1658 if (dlpi_kread(kd, nl[NL_IFNET].n_value, 1659 &addr, sizeof(addr), ebuf) < 0) { 1660 close(kd); 1661 return (-1); 1662 } 1663 for (; addr != NULL; addr = ifnet.if_next) { 1664 if (dlpi_kread(kd, (off_t)addr, 1665 &ifnet, sizeof(ifnet), ebuf) < 0 || 1666 dlpi_kread(kd, (off_t)ifnet.if_name, 1667 if_name, sizeof(ifnet.if_name), ebuf) < 0) { 1668 (void)close(kd); 1669 return (-1); 1670 } 1671 if_name[sizeof(ifnet.if_name)] = '\0'; 1672 if (strcmp(if_name, ifname) == 0 && ifnet.if_unit == unit) 1673 return (ifnet.if_index); 1674 } 1675 1676 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't find %s", ifname); 1677 return (-1); 1678 } 1679 1680 static int 1681 dlpi_kread(register int fd, register off_t addr, 1682 register void *buf, register u_int len, register char *ebuf) 1683 { 1684 register int cc; 1685 1686 if (lseek(fd, addr, SEEK_SET) < 0) { 1687 snprintf(ebuf, PCAP_ERRBUF_SIZE, "lseek: %s", 1688 pcap_strerror(errno)); 1689 return (-1); 1690 } 1691 cc = read(fd, buf, len); 1692 if (cc < 0) { 1693 snprintf(ebuf, PCAP_ERRBUF_SIZE, "read: %s", 1694 pcap_strerror(errno)); 1695 return (-1); 1696 } else if (cc != len) { 1697 snprintf(ebuf, PCAP_ERRBUF_SIZE, "short read (%d != %d)", cc, 1698 len); 1699 return (-1); 1700 } 1701 return (cc); 1702 } 1703 #endif 1704 1705 pcap_t * 1706 pcap_create_interface(const char *device, char *ebuf) 1707 { 1708 pcap_t *p; 1709 #ifdef DL_HP_RAWDLS 1710 struct pcap_dlpi *pd; 1711 #endif 1712 1713 p = pcap_create_common(device, ebuf, sizeof (struct pcap_dlpi)); 1714 if (p == NULL) 1715 return (NULL); 1716 1717 #ifdef DL_HP_RAWDLS 1718 pd = p->priv; 1719 pd->send_fd = -1; /* it hasn't been opened yet */ 1720 #endif 1721 1722 p->activate_op = pcap_activate_dlpi; 1723 return (p); 1724 } 1725