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