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