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