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