2 * Copyright (c) 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * This code contributed by Atanu Ghosh (atanu@cs.ucl.ac.uk),
22 * University College London, and subsequently modified by
23 * Guy Harris (guy@alum.mit.edu), Mark Pizzolato
24 * <List-tcpdump-workers@subscriptions.pizzolato.net>,
25 * Mark C. Brown (mbrown@hp.com), and Sagun Shakya <Sagun.Shakya@Sun.COM>.
29 * Packet capture routine for DLPI under SunOS 5, HP-UX 9/10/11, and AIX.
33 * - The DLIOCRAW ioctl() is specific to SunOS.
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.
38 * - It might be desirable to use pfmod(7) to filter packets in the
39 * kernel when possible.
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
45 * http://docs.hp.com/hpux/onlinedocs/B2355-90093/B2355-90093.html
47 * but is no longer available; it can still be found at
49 * http://h21007.www2.hp.com/dspp/files/unprotected/Drivers/Docs/Refs/B2355-90093.pdf
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
57 * http://docs.hp.com/en/B2355-90139/index.html
59 * - The HP-UX 11i v2 version of the HP-UX DLPI Programmer's Guide
62 * http://docs.hp.com/en/B2355-90871/index.html
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
75 #include <sys/types.h>
77 #ifdef HAVE_SYS_BUFMOD_H
78 #include <sys/bufmod.h>
81 #ifdef HAVE_SYS_DLPI_EXT_H
82 #include <sys/dlpi_ext.h>
85 #include <sys/socket.h>
90 #include <sys/stream.h>
91 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
92 #include <sys/systeminfo.h>
112 #include "pcap-int.h"
113 #include "dlpisubs.h"
115 #ifdef HAVE_OS_PROTO_H
116 #include "os-proto.h"
121 * HP-UX has a /dev/dlpi device; you open it and set the PPA of the actual
122 * network device you want.
124 #define HAVE_DEV_DLPI
127 * AIX has a /dev/dlpi directory, with devices named after the interfaces
130 #define PCAP_DEV_PREFIX "/dev/dlpi"
131 #elif defined(HAVE_SOLARIS)
133 * Solaris has devices named after the interfaces underneath /dev.
135 #define PCAP_DEV_PREFIX "/dev"
138 #define MAXDLBUF 8192
141 static char *split_dname(char *, u_int
*, char *);
142 static int dl_doattach(int, int, char *);
144 static int dl_dohpuxbind(int, char *);
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 *, int *);
150 static int dlinforeq(int, char *);
151 static int dlinfoack(int, char *, char *);
153 #ifdef HAVE_DL_PASSIVE_REQ_T
154 static void dlpassive(int, char *);
158 static int dlrawdatareq(int, const u_char
*, int);
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
*,
168 static int send_request(int, char *, int, char *, char *);
170 static int dlpi_kread(int, off_t
, void *, u_int
, char *);
173 static int get_dlpi_ppa(int, const char *, u_int
, u_int
*, char *);
177 * Cast a buffer to "union DL_primitives" without provoking warnings
180 #define MAKE_DL_PRIMITIVES(ptr) ((union DL_primitives *)(void *)(ptr))
183 pcap_read_dlpi(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
188 bpf_u_int32 ctlbuf
[MAXDLBUF
];
189 struct strbuf ctl
= {
199 data
.buf
= (char *)p
->buffer
+ p
->offset
;
200 data
.maxlen
= p
->bufsize
;
204 * Has "pcap_breakloop()" been called?
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.
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?
221 ctl
.buf
= (char *)ctlbuf
;
222 ctl
.maxlen
= MAXDLBUF
;
224 if (getmsg(p
->fd
, &ctl
, &data
, &flags
) < 0) {
225 /* Don't choke when we get ptraced */
235 pcap_fmt_errmsg_for_errno(p
->errbuf
,
236 sizeof(p
->errbuf
), errno
, "getmsg");
241 bp
= p
->buffer
+ p
->offset
;
245 return (pcap_process_pkts(p
, callback
, user
, cnt
, bp
, cc
));
249 pcap_inject_dlpi(pcap_t
*p
, const void *buf
, int size
)
252 struct pcap_dlpi
*pd
= p
->priv
;
256 #if defined(DLIOCRAW)
257 ret
= write(p
->fd
, buf
, size
);
259 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
263 #elif defined(DL_HP_RAWDLS)
264 if (pd
->send_fd
< 0) {
265 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
266 "send: Output FD couldn't be opened");
269 ret
= dlrawdatareq(pd
->send_fd
, buf
, size
);
271 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
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.
283 #else /* no raw mode */
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.
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.
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.
304 pcap_strlcpy(p
->errbuf
, "send: Not supported on this version of this OS",
307 #endif /* raw mode */
312 #define DL_IPATM 0x12 /* ATM Classical IP interface */
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 */
328 pcap_cleanup_dlpi(pcap_t
*p
)
331 struct pcap_dlpi
*pd
= p
->priv
;
333 if (pd
->send_fd
>= 0) {
338 pcap_cleanup_live_common(p
);
342 open_dlpi_device(const char *name
, u_int
*ppa
, char *errbuf
)
356 ** Remove any "/dev/" on the front of the device.
358 cp
= strrchr(name
, '/');
360 pcap_strlcpy(dname
, name
, sizeof(dname
));
362 pcap_strlcpy(dname
, cp
+ 1, sizeof(dname
));
365 * Split the device name into a device type name and a unit number;
366 * chop off the unit number, so "dname" is just a device type name.
368 cp
= split_dname(dname
, &unit
, errbuf
);
371 * split_dname() has filled in the error message.
373 return (PCAP_ERROR_NO_SUCH_DEVICE
);
378 * Use "/dev/dlpi" as the device.
380 * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that
381 * the "dl_mjr_num" field is for the "major number of interface
382 * driver"; that's the major of "/dev/dlpi" on the system on
383 * which I tried this, but there may be DLPI devices that
384 * use a different driver, in which case we may need to
385 * search "/dev" for the appropriate device with that major
386 * device number, rather than hardwiring "/dev/dlpi".
389 if ((fd
= open(cp
, O_RDWR
)) < 0) {
390 if (errno
== EPERM
|| errno
== EACCES
) {
391 status
= PCAP_ERROR_PERM_DENIED
;
392 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
393 "Attempt to open %s failed with %s - root privilege may be required",
394 cp
, (errno
== EPERM
) ? "EPERM" : "EACCES");
397 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
398 errno
, "Attempt to open %s failed", cp
);
404 * Get a table of all PPAs for that device, and search that
405 * table for the specified device type name and unit number.
407 status
= get_dlpi_ppa(fd
, dname
, unit
, ppa
, errbuf
);
414 * If the device name begins with "/", assume it begins with
415 * the pathname of the directory containing the device to open;
416 * otherwise, concatenate the device directory name and the
420 pcap_strlcpy(dname
, name
, sizeof(dname
));
422 snprintf(dname
, sizeof(dname
), "%s/%s", PCAP_DEV_PREFIX
,
426 * Get the unit number, and a pointer to the end of the device
429 cp
= split_dname(dname
, ppa
, errbuf
);
432 * split_dname() has filled in the error message.
434 return (PCAP_ERROR_NO_SUCH_DEVICE
);
438 * Make a copy of the device pathname, and then remove the unit
439 * number from the device pathname.
441 pcap_strlcpy(dname2
, dname
, sizeof(dname
));
444 /* Try device without unit number */
445 if ((fd
= open(dname
, O_RDWR
)) < 0) {
446 if (errno
!= ENOENT
) {
447 if (errno
== EPERM
|| errno
== EACCES
) {
448 status
= PCAP_ERROR_PERM_DENIED
;
449 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
450 "Attempt to open %s failed with %s - root privilege may be required",
452 (errno
== EPERM
) ? "EPERM" : "EACCES");
455 pcap_fmt_errmsg_for_errno(errbuf
,
456 PCAP_ERRBUF_SIZE
, errno
,
457 "Attempt to open %s failed", dname
);
462 /* Try again with unit number */
463 if ((fd
= open(dname2
, O_RDWR
)) < 0) {
464 if (errno
== ENOENT
) {
465 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
468 * We provide an error message even
469 * for this error, for diagnostic
470 * purposes (so that, for example,
471 * the app can show the message if the
474 * In it, we just report "No DLPI device
475 * found" with the device name, so people
476 * don't get confused and think, for example,
477 * that if they can't capture on "lo0"
478 * on Solaris prior to Solaris 11 the fix
479 * is to change libpcap (or the application
480 * that uses it) to look for something other
481 * than "/dev/lo0", as the fix is to use
482 * Solaris 11 or some operating system
483 * other than Solaris - you just *can't*
484 * capture on a loopback interface
485 * on Solaris prior to Solaris 11, the lack
486 * of a DLPI device for the loopback
487 * interface is just a symptom of that
490 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
491 "%s: No DLPI device found", name
);
493 if (errno
== EPERM
|| errno
== EACCES
) {
494 status
= PCAP_ERROR_PERM_DENIED
;
495 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
496 "Attempt to open %s failed with %s - root privilege may be required",
498 (errno
== EPERM
) ? "EPERM" : "EACCES");
501 pcap_fmt_errmsg_for_errno(errbuf
,
502 PCAP_ERRBUF_SIZE
, errno
,
503 "Attempt to open %s failed",
509 /* XXX Assume unit zero */
517 pcap_activate_dlpi(pcap_t
*p
)
520 struct pcap_dlpi
*pd
= p
->priv
;
528 register dl_info_ack_t
*infop
;
529 #ifdef HAVE_SYS_BUFMOD_H
532 char release
[GET_RELEASE_BUFSIZE
];
533 bpf_u_int32 osmajor
, osminor
, osmicro
;
536 bpf_u_int32 buf
[MAXDLBUF
];
538 p
->fd
= open_dlpi_device(p
->opt
.device
, &ppa
, p
->errbuf
);
546 * XXX - HP-UX 10.20 and 11.xx don't appear to support sending and
547 * receiving packets on the same descriptor - you need separate
548 * descriptors for sending and receiving, bound to different SAPs.
550 * If the open fails, we just leave -1 in "pd->send_fd" and reject
551 * attempts to send packets, just as if, in pcap-bpf.c, we fail
552 * to open the BPF device for reading and writing, we just try
553 * to open it for reading only and, if that succeeds, just let
554 * the send attempts fail.
556 pd
->send_fd
= open("/dev/dlpi", O_RDWR
);
560 ** Attach if "style 2" provider
562 if (dlinforeq(p
->fd
, p
->errbuf
) < 0 ||
563 dlinfoack(p
->fd
, (char *)buf
, p
->errbuf
) < 0) {
567 infop
= &(MAKE_DL_PRIMITIVES(buf
))->info_ack
;
569 if (infop
->dl_mac_type
== DL_IPATM
)
572 if (infop
->dl_provider_style
== DL_STYLE2
) {
573 retv
= dl_doattach(p
->fd
, ppa
, p
->errbuf
);
579 if (pd
->send_fd
>= 0) {
580 retv
= dl_doattach(pd
->send_fd
, ppa
, p
->errbuf
);
591 * This device exists, but we don't support monitor mode
592 * any platforms that support DLPI.
594 status
= PCAP_ERROR_RFMON_NOTSUP
;
598 #ifdef HAVE_DL_PASSIVE_REQ_T
600 * Enable Passive mode to be able to capture on aggregated link.
601 * Not supported in all Solaris versions.
603 dlpassive(p
->fd
, p
->errbuf
);
606 ** Bind (defer if using HP-UX 9 or HP-UX 10.20 or later, totally
607 ** skip if using SINIX)
609 #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20_OR_LATER) && !defined(sinix)
613 ** According to IBM's AIX Support Line, the dl_sap value
614 ** should not be less than 0x600 (1536) for standard Ethernet.
615 ** However, we seem to get DL_BADADDR - "DLSAP addr in improper
616 ** format or invalid" - errors if we use 1537 on the "tr0"
617 ** device, which, given that its name starts with "tr" and that
618 ** it's IBM, probably means a Token Ring device. (Perhaps we
619 ** need to use 1537 on "/dev/dlpi/en" because that device is for
620 ** D/I/X Ethernet, the "SAP" is actually an Ethernet type, and
621 ** it rejects invalid Ethernet types.)
623 ** So if 1537 fails, we try 2, as Hyung Sik Yoon of IBM Korea
624 ** says that works on Token Ring (he says that 0 does *not*
625 ** work; perhaps that's considered an invalid LLC SAP value - I
626 ** assume the SAP value in a DLPI bind is an LLC SAP for network
627 ** types that use 802.2 LLC).
629 if ((dlbindreq(p
->fd
, 1537, p
->errbuf
) < 0 &&
630 dlbindreq(p
->fd
, 2, p
->errbuf
) < 0) ||
631 dlbindack(p
->fd
, (char *)buf
, p
->errbuf
, NULL
) < 0) {
635 #elif defined(DL_HP_RAWDLS)
637 ** HP-UX 10.0x and 10.1x.
639 if (dl_dohpuxbind(p
->fd
, p
->errbuf
) < 0) {
643 if (pd
->send_fd
>= 0) {
645 ** XXX - if this fails, just close send_fd and
646 ** set it to -1, so that you can't send but can
649 if (dl_dohpuxbind(pd
->send_fd
, p
->errbuf
) < 0) {
654 #else /* neither AIX nor HP-UX */
656 ** Not Sinix, and neither AIX nor HP-UX - Solaris, and any other
659 if (dlbindreq(p
->fd
, 0, p
->errbuf
) < 0 ||
660 dlbindack(p
->fd
, (char *)buf
, p
->errbuf
, NULL
) < 0) {
664 #endif /* AIX vs. HP-UX vs. other */
665 #endif /* !HP-UX 9 and !HP-UX 10.20 or later and !SINIX */
668 * Turn a negative snapshot value (invalid), a snapshot value of
669 * 0 (unspecified), or a value bigger than the normal maximum
670 * value, into the maximum allowed value.
672 * If some application really *needs* a bigger snapshot
673 * length, we should just increase MAXIMUM_SNAPLEN.
675 if (p
->snapshot
<= 0 || p
->snapshot
> MAXIMUM_SNAPLEN
)
676 p
->snapshot
= MAXIMUM_SNAPLEN
;
681 ** Have to turn on some special ATM promiscuous mode
683 ** Do *NOT* turn regular promiscuous mode on; it doesn't
684 ** help, and may break things.
686 if (strioctl(p
->fd
, A_PROMISCON_REQ
, 0, NULL
) < 0) {
688 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
689 errno
, "A_PROMISCON_REQ");
694 if (p
->opt
.promisc
) {
696 ** Enable promiscuous (not necessary on send FD)
698 retv
= dlpromiscon(p
, DL_PROMISC_PHYS
);
700 if (retv
== PCAP_ERROR_PERM_DENIED
)
701 status
= PCAP_ERROR_PROMISC_PERM_DENIED
;
708 ** Try to enable multicast (you would have thought
709 ** promiscuous would be sufficient). (Skip if using
710 ** HP-UX or SINIX) (Not necessary on send FD)
712 #if !defined(__hpux) && !defined(sinix)
713 retv
= dlpromiscon(p
, DL_PROMISC_MULTI
);
715 status
= PCAP_WARNING
;
719 ** Try to enable SAP promiscuity (when not in promiscuous mode
720 ** when using HP-UX, when not doing SunATM on Solaris, and never
721 ** under SINIX) (Not necessary on send FD)
725 /* HP-UX - only do this when not in promiscuous mode */
726 if (!p
->opt
.promisc
) {
727 #elif defined(HAVE_SOLARIS)
728 /* Solaris - don't do this on SunATM devices */
731 /* Everything else (except for SINIX) - always do this */
734 retv
= dlpromiscon(p
, DL_PROMISC_SAP
);
736 if (p
->opt
.promisc
) {
738 * Not fatal, since the DL_PROMISC_PHYS mode
741 * Report it as a warning, however.
743 status
= PCAP_WARNING
;
756 ** HP-UX 9, and HP-UX 10.20 or later, must bind after setting
757 ** promiscuous options.
759 #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20_OR_LATER)
760 if (dl_dohpuxbind(p
->fd
, p
->errbuf
) < 0) {
765 ** We don't set promiscuous mode on the send FD, but we'll defer
766 ** binding it anyway, just to keep the HP-UX 9/10.20 or later
769 if (pd
->send_fd
>= 0) {
771 ** XXX - if this fails, just close send_fd and
772 ** set it to -1, so that you can't send but can
775 if (dl_dohpuxbind(pd
->send_fd
, p
->errbuf
) < 0) {
783 ** Determine link type
784 ** XXX - get SAP length and address length as well, for use
785 ** when sending packets.
787 if (dlinforeq(p
->fd
, p
->errbuf
) < 0 ||
788 dlinfoack(p
->fd
, (char *)buf
, p
->errbuf
) < 0) {
793 infop
= &(MAKE_DL_PRIMITIVES(buf
))->info_ack
;
794 if (pcap_process_mactype(p
, infop
->dl_mac_type
) != 0) {
801 ** This is a non standard SunOS hack to get the full raw link-layer
804 if (strioctl(p
->fd
, DLIOCRAW
, 0, NULL
) < 0) {
806 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
812 #ifdef HAVE_SYS_BUFMOD_H
816 ** There is a bug in bufmod(7). When dealing with messages of
817 ** less than snaplen size it strips data from the beginning not
820 ** This bug is fixed in 5.3.2. Also, there is a patch available.
821 ** Ask for bugid 1149065.
824 get_release(release
, sizeof (release
), &osmajor
, &osminor
, &osmicro
);
825 if (osmajor
== 5 && (osminor
<= 2 || (osminor
== 3 && osmicro
< 2)) &&
826 getenv("BUFMOD_FIXED") == NULL
) {
827 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
828 "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.",
831 status
= PCAP_WARNING
;
835 /* Push and configure bufmod. */
836 if (pcap_conf_bufmod(p
, ss
) != 0) {
843 ** As the last operation flush the read side.
845 if (ioctl(p
->fd
, I_FLUSH
, FLUSHR
) != 0) {
847 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
852 /* Allocate data buffer. */
853 if (pcap_alloc_databuf(p
) != 0) {
861 * "p->fd" is an FD for a STREAMS device, so "select()" and
862 * "poll()" should work on it.
864 p
->selectable_fd
= p
->fd
;
866 p
->read_op
= pcap_read_dlpi
;
867 p
->inject_op
= pcap_inject_dlpi
;
868 p
->setfilter_op
= pcap_install_bpf_program
; /* no kernel filtering */
869 p
->setdirection_op
= NULL
; /* Not implemented.*/
870 p
->set_datalink_op
= NULL
; /* can't change data link type */
871 p
->getnonblock_op
= pcap_getnonblock_fd
;
872 p
->setnonblock_op
= pcap_setnonblock_fd
;
873 p
->stats_op
= pcap_stats_dlpi
;
874 p
->cleanup_op
= pcap_cleanup_dlpi
;
878 pcap_cleanup_dlpi(p
);
883 * Split a device name into a device type name and a unit number;
884 * return the a pointer to the beginning of the unit number, which
885 * is the end of the device type name, and set "*unitp" to the unit
888 * Returns NULL on error, and fills "ebuf" with an error message.
891 split_dname(char *device
, u_int
*unitp
, char *ebuf
)
898 * Look for a number at the end of the device name string.
900 cp
= device
+ strlen(device
) - 1;
901 if (*cp
< '0' || *cp
> '9') {
902 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s missing unit number",
907 /* Digits at end of string are unit number */
908 while (cp
-1 >= device
&& *(cp
-1) >= '0' && *(cp
-1) <= '9')
912 unit
= strtol(cp
, &eos
, 10);
914 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s bad unit number", device
);
917 if (errno
== ERANGE
|| unit
> INT_MAX
) {
918 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s unit number too large",
923 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s unit number is negative",
927 *unitp
= (u_int
)unit
;
932 dl_doattach(int fd
, int ppa
, char *ebuf
)
935 bpf_u_int32 buf
[MAXDLBUF
];
938 req
.dl_primitive
= DL_ATTACH_REQ
;
940 if (send_request(fd
, (char *)&req
, sizeof(req
), "attach", ebuf
) < 0)
943 err
= dlokack(fd
, "attach", (char *)buf
, ebuf
, NULL
);
951 dl_dohpuxbind(int fd
, char *ebuf
)
955 bpf_u_int32 buf
[MAXDLBUF
];
958 * XXX - we start at 22 because we used to use only 22, but
959 * that was just because that was the value used in some
960 * sample code from HP. With what value *should* we start?
961 * Does it matter, given that we're enabling SAP promiscuity
966 if (dlbindreq(fd
, hpsap
, ebuf
) < 0)
968 if (dlbindack(fd
, (char *)buf
, ebuf
, &uerror
) >= 0)
971 * For any error other than a UNIX EBUSY, give up.
973 if (uerror
!= EBUSY
) {
975 * dlbindack() has already filled in ebuf for
982 * For EBUSY, try the next SAP value; that means that
983 * somebody else is using that SAP. Clear ebuf so
984 * that application doesn't report the "Device busy"
985 * error as a warning.
991 "All SAPs from 22 through 100 are in use",
1000 #define STRINGIFY(n) #n
1003 dlpromiscon(pcap_t
*p
, bpf_u_int32 level
)
1005 dl_promiscon_req_t req
;
1006 bpf_u_int32 buf
[MAXDLBUF
];
1010 req
.dl_primitive
= DL_PROMISCON_REQ
;
1011 req
.dl_level
= level
;
1012 if (send_request(p
->fd
, (char *)&req
, sizeof(req
), "promiscon",
1014 return (PCAP_ERROR
);
1015 err
= dlokack(p
->fd
, "promiscon" STRINGIFY(level
), (char *)buf
,
1016 p
->errbuf
, &uerror
);
1018 if (err
== PCAP_ERROR_PERM_DENIED
) {
1019 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1020 "Attempt to set promiscuous mode failed with %s - root privilege may be required",
1021 (uerror
== EPERM
) ? "EPERM" : "EACCES");
1022 err
= PCAP_ERROR_PROMISC_PERM_DENIED
;
1030 * Not all interfaces are DLPI interfaces, and thus not all interfaces
1031 * can be opened with DLPI (for example, the loopback interface is not
1032 * a DLPI interface on Solaris prior to Solaris 11), so try to open
1033 * the specified interface; return 0 if we fail with PCAP_ERROR_NO_SUCH_DEVICE
1037 is_dlpi_interface(const char *name
)
1041 char errbuf
[PCAP_ERRBUF_SIZE
];
1043 fd
= open_dlpi_device(name
, &ppa
, errbuf
);
1046 * Error - was it PCAP_ERROR_NO_SUCH_DEVICE?
1048 if (fd
== PCAP_ERROR_NO_SUCH_DEVICE
) {
1050 * Yes, so we can't open this because it's
1051 * not a DLPI interface.
1056 * No, so, in the case where there's a single DLPI
1057 * device for all interfaces of this type ("style
1058 * 2" providers?), we don't know whether it's a DLPI
1059 * interface or not, as we didn't try an attach.
1060 * Say it is a DLPI device, so that the user can at
1061 * least try to open it and report the error (which
1062 * is probably "you don't have permission to open that
1063 * DLPI device"; reporting those interfaces means
1064 * users will ask "why am I getting a permissions error
1065 * when I try to capture" rather than "why am I not
1066 * seeing any interfaces", making the underlying problem
1080 get_if_flags(const char *name _U_
, bpf_u_int32
*flags _U_
, char *errbuf _U_
)
1083 * Nothing we can do other than mark loopback devices as "the
1084 * connected/disconnected status doesn't apply".
1086 * XXX - on Solaris, can we do what the dladm command does,
1087 * i.e. get a connected/disconnected indication from a kstat?
1088 * (Note that you can also get the link speed, and possibly
1089 * other information, from a kstat as well.)
1091 if (*flags
& PCAP_IF_LOOPBACK
) {
1093 * Loopback devices aren't wireless, and "connected"/
1094 * "disconnected" doesn't apply to them.
1096 *flags
|= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
;
1103 pcap_platform_finddevs(pcap_if_list_t
*devlistp
, char *errbuf
)
1109 char pad
[516]; /* XXX - must be at least 513; is 516
1117 * Get the list of regular interfaces first.
1119 if (pcap_findalldevs_interfaces(devlistp
, errbuf
, is_dlpi_interface
,
1120 get_if_flags
) == -1)
1121 return (-1); /* failure */
1125 * We may have to do special magic to get ATM devices.
1127 if ((fd
= open("/dev/ba", O_RDWR
)) < 0) {
1129 * We couldn't open the "ba" device.
1130 * For now, just give up; perhaps we should
1131 * return an error if the problem is neither
1132 * a "that device doesn't exist" error (ENOENT,
1133 * ENXIO, etc.) or a "you're not allowed to do
1134 * that" error (EPERM, EACCES).
1139 if (strioctl(fd
, A_GET_UNITS
, sizeof(buf
), (char *)&buf
) < 0) {
1140 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1141 errno
, "A_GET_UNITS");
1144 for (i
= 0; i
< buf
.nunits
; i
++) {
1145 snprintf(baname
, sizeof baname
, "ba%u", i
);
1147 * XXX - is there a notion of "up" and "running"?
1148 * And is there a way to determine whether the
1149 * interface is plugged into a network?
1151 if (pcap_add_dev(devlistp
, baname
, 0, NULL
, errbuf
) == NULL
)
1160 send_request(int fd
, char *ptr
, int len
, char *what
, char *ebuf
)
1170 if (putmsg(fd
, &ctl
, (struct strbuf
*) NULL
, flags
) < 0) {
1171 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1172 errno
, "send_request: putmsg \"%s\"", what
);
1179 recv_ack(int fd
, int size
, const char *what
, char *bufp
, char *ebuf
, int *uerror
)
1181 union DL_primitives
*dlp
;
1184 char errmsgbuf
[PCAP_ERRBUF_SIZE
];
1188 * Clear out "*uerror", so it's only set for DL_ERROR_ACK/DL_SYSERR,
1189 * making that the only place where EBUSY is treated specially.
1194 ctl
.maxlen
= MAXDLBUF
;
1199 if (getmsg(fd
, &ctl
, (struct strbuf
*)NULL
, &flags
) < 0) {
1200 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1201 errno
, "recv_ack: %s getmsg", what
);
1202 return (PCAP_ERROR
);
1205 dlp
= MAKE_DL_PRIMITIVES(ctl
.buf
);
1206 switch (dlp
->dl_primitive
) {
1211 #ifdef DL_HP_PPA_ACK
1218 switch (dlp
->error_ack
.dl_errno
) {
1222 *uerror
= dlp
->error_ack
.dl_unix_errno
;
1223 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1224 dlp
->error_ack
.dl_unix_errno
,
1225 "recv_ack: %s: UNIX error", what
);
1226 if (dlp
->error_ack
.dl_unix_errno
== EPERM
||
1227 dlp
->error_ack
.dl_unix_errno
== EACCES
)
1228 return (PCAP_ERROR_PERM_DENIED
);
1233 * Neither EPERM nor EACCES.
1235 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1236 "recv_ack: %s: %s", what
,
1237 dlstrerror(errmsgbuf
, sizeof (errmsgbuf
), dlp
->error_ack
.dl_errno
));
1238 if (dlp
->error_ack
.dl_errno
== DL_BADPPA
)
1239 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1240 else if (dlp
->error_ack
.dl_errno
== DL_ACCESS
)
1241 return (PCAP_ERROR_PERM_DENIED
);
1244 return (PCAP_ERROR
);
1247 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1248 "recv_ack: %s: Unexpected primitive ack %s",
1249 what
, dlprim(dlprimbuf
, sizeof (dlprimbuf
), dlp
->dl_primitive
));
1250 return (PCAP_ERROR
);
1253 if (ctl
.len
< size
) {
1254 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1255 "recv_ack: %s: Ack too small (%d < %d)",
1256 what
, ctl
.len
, size
);
1257 return (PCAP_ERROR
);
1263 dlstrerror(char *errbuf
, size_t errbufsize
, bpf_u_int32 dl_errno
)
1268 return ("Improper permissions for request");
1271 return ("DLSAP addr in improper format or invalid");
1274 return ("Seq number not from outstand DL_CONN_IND");
1277 return ("User data exceeded provider limit");
1280 #ifdef HAVE_DEV_DLPI
1282 * With a single "/dev/dlpi" device used for all
1283 * DLPI providers, PPAs have nothing to do with
1286 return ("Specified PPA was invalid");
1289 * We have separate devices for separate devices;
1290 * the PPA is just the unit number.
1292 return ("Specified PPA (device unit) was invalid");
1296 return ("Primitive received not known by provider");
1298 case DL_BADQOSPARAM
:
1299 return ("QOS parameters contained invalid values");
1302 return ("QOS structure type is unknown/unsupported");
1305 return ("Bad LSAP selector");
1308 return ("Token used not an active stream");
1311 return ("Attempted second bind with dl_max_conind");
1314 return ("Physical link initialization failed");
1317 return ("Provider couldn't allocate alternate address");
1320 return ("Physical link not initialized");
1323 return ("Primitive issued in improper state");
1326 return ("UNIX system error occurred");
1328 case DL_UNSUPPORTED
:
1329 return ("Requested service not supplied by provider");
1331 case DL_UNDELIVERABLE
:
1332 return ("Previous data unit could not be delivered");
1334 case DL_NOTSUPPORTED
:
1335 return ("Primitive is known but not supported");
1338 return ("Limit exceeded");
1341 return ("Promiscuous mode not enabled");
1344 return ("Other streams for PPA in post-attached");
1347 return ("Automatic handling XID&TEST not supported");
1350 return ("Automatic handling of XID not supported");
1353 return ("Automatic handling of TEST not supported");
1356 return ("Automatic handling of XID response");
1359 return ("Automatic handling of TEST response");
1362 return ("Pending outstanding connect indications");
1365 snprintf(errbuf
, errbufsize
, "Error %02x", dl_errno
);
1371 dlprim(char *primbuf
, size_t primbufsize
, bpf_u_int32 prim
)
1376 return ("DL_INFO_REQ");
1379 return ("DL_INFO_ACK");
1382 return ("DL_ATTACH_REQ");
1385 return ("DL_DETACH_REQ");
1388 return ("DL_BIND_REQ");
1391 return ("DL_BIND_ACK");
1394 return ("DL_UNBIND_REQ");
1397 return ("DL_OK_ACK");
1400 return ("DL_ERROR_ACK");
1402 case DL_SUBS_BIND_REQ
:
1403 return ("DL_SUBS_BIND_REQ");
1405 case DL_SUBS_BIND_ACK
:
1406 return ("DL_SUBS_BIND_ACK");
1408 case DL_UNITDATA_REQ
:
1409 return ("DL_UNITDATA_REQ");
1411 case DL_UNITDATA_IND
:
1412 return ("DL_UNITDATA_IND");
1414 case DL_UDERROR_IND
:
1415 return ("DL_UDERROR_IND");
1418 return ("DL_UDQOS_REQ");
1420 case DL_CONNECT_REQ
:
1421 return ("DL_CONNECT_REQ");
1423 case DL_CONNECT_IND
:
1424 return ("DL_CONNECT_IND");
1426 case DL_CONNECT_RES
:
1427 return ("DL_CONNECT_RES");
1429 case DL_CONNECT_CON
:
1430 return ("DL_CONNECT_CON");
1433 return ("DL_TOKEN_REQ");
1436 return ("DL_TOKEN_ACK");
1438 case DL_DISCONNECT_REQ
:
1439 return ("DL_DISCONNECT_REQ");
1441 case DL_DISCONNECT_IND
:
1442 return ("DL_DISCONNECT_IND");
1445 return ("DL_RESET_REQ");
1448 return ("DL_RESET_IND");
1451 return ("DL_RESET_RES");
1454 return ("DL_RESET_CON");
1457 snprintf(primbuf
, primbufsize
, "unknown primitive 0x%x",
1464 dlbindreq(int fd
, bpf_u_int32 sap
, char *ebuf
)
1469 memset((char *)&req
, 0, sizeof(req
));
1470 req
.dl_primitive
= DL_BIND_REQ
;
1471 /* XXX - what if neither of these are defined? */
1472 #if defined(DL_HP_RAWDLS)
1473 req
.dl_max_conind
= 1; /* XXX magic number */
1474 req
.dl_service_mode
= DL_HP_RAWDLS
;
1475 #elif defined(DL_CLDLS)
1476 req
.dl_service_mode
= DL_CLDLS
;
1480 return (send_request(fd
, (char *)&req
, sizeof(req
), "bind", ebuf
));
1484 dlbindack(int fd
, char *bufp
, char *ebuf
, int *uerror
)
1487 return (recv_ack(fd
, DL_BIND_ACK_SIZE
, "bind", bufp
, ebuf
, uerror
));
1491 dlokack(int fd
, const char *what
, char *bufp
, char *ebuf
, int *uerror
)
1494 return (recv_ack(fd
, DL_OK_ACK_SIZE
, what
, bufp
, ebuf
, uerror
));
1499 dlinforeq(int fd
, char *ebuf
)
1503 req
.dl_primitive
= DL_INFO_REQ
;
1505 return (send_request(fd
, (char *)&req
, sizeof(req
), "info", ebuf
));
1509 dlinfoack(int fd
, char *bufp
, char *ebuf
)
1512 return (recv_ack(fd
, DL_INFO_ACK_SIZE
, "info", bufp
, ebuf
, NULL
));
1515 #ifdef HAVE_DL_PASSIVE_REQ_T
1517 * Enable DLPI passive mode. We do not care if this request fails, as this
1518 * indicates the underlying DLPI device does not support link aggregation.
1521 dlpassive(int fd
, char *ebuf
)
1523 dl_passive_req_t req
;
1524 bpf_u_int32 buf
[MAXDLBUF
];
1526 req
.dl_primitive
= DL_PASSIVE_REQ
;
1528 if (send_request(fd
, (char *)&req
, sizeof(req
), "dlpassive", ebuf
) == 0)
1529 (void) dlokack(fd
, "dlpassive", (char *)buf
, ebuf
, NULL
);
1535 * There's an ack *if* there's an error.
1538 dlrawdatareq(int fd
, const u_char
*datap
, int datalen
)
1540 struct strbuf ctl
, data
;
1541 long buf
[MAXDLBUF
]; /* XXX - char? */
1542 union DL_primitives
*dlp
;
1545 dlp
= MAKE_DL_PRIMITIVES(buf
);
1547 dlp
->dl_primitive
= DL_HP_RAWDATA_REQ
;
1548 dlen
= DL_HP_RAWDATA_REQ_SIZE
;
1551 * HP's documentation doesn't appear to show us supplying any
1552 * address pointed to by the control part of the message.
1553 * I think that's what raw mode means - you just send the raw
1554 * packet, you don't specify where to send it to, as that's
1555 * implied by the destination address.
1559 ctl
.buf
= (void *)buf
;
1563 data
.buf
= (void *)datap
;
1565 return (putmsg(fd
, &ctl
, &data
, 0));
1567 #endif /* DL_HP_RAWDLS */
1569 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
1571 get_release(char *buf
, size_t bufsize
, bpf_u_int32
*majorp
,
1572 bpf_u_int32
*minorp
, bpf_u_int32
*microp
)
1579 if (sysinfo(SI_RELEASE
, buf
, bufsize
) < 0) {
1580 pcap_strlcpy(buf
, "?", bufsize
);
1584 if (!PCAP_ISDIGIT((unsigned char)*cp
))
1586 *majorp
= strtol(cp
, &cp
, 10);
1589 *minorp
= strtol(cp
, &cp
, 10);
1592 *microp
= strtol(cp
, &cp
, 10);
1596 #ifdef DL_HP_PPA_REQ
1598 * Under HP-UX 10 and HP-UX 11, we can ask for the ppa
1603 * Determine ppa number that specifies ifname.
1605 * If the "dl_hp_ppa_info_t" doesn't have a "dl_module_id_1" member,
1606 * the code that's used here is the old code for HP-UX 10.x.
1608 * However, HP-UX 10.20, at least, appears to have such a member
1609 * in its "dl_hp_ppa_info_t" structure, so the new code is used.
1610 * The new code didn't work on an old 10.20 system on which Rick
1611 * Jones of HP tried it, but with later patches installed, it
1612 * worked - it appears that the older system had those members but
1613 * didn't put anything in them, so, if the search by name fails, we
1614 * do the old search.
1616 * Rick suggests that making sure your system is "up on the latest
1617 * lancommon/DLPI/driver patches" is probably a good idea; it'd fix
1618 * that problem, as well as allowing libpcap to see packets sent
1619 * from the system on which the libpcap application is being run.
1620 * (On 10.20, in addition to getting the latest patches, you need
1621 * to turn the kernel "lanc_outbound_promisc_flag" flag on with ADB;
1622 * a posting to "comp.sys.hp.hpux" at
1624 * http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=558092266
1626 * says that, to see the machine's outgoing traffic, you'd need to
1627 * apply the right patches to your system, and also set that variable
1630 echo 'lanc_outbound_promisc_flag/W1' | /usr/bin/adb -w /stand/vmunix /dev/kmem
1632 * which could be put in, for example, "/sbin/init.d/lan".
1634 * Setting the variable is not necessary on HP-UX 11.x.
1637 get_dlpi_ppa(register int fd
, register const char *device
, register u_int unit
,
1638 u_int
*ppa
, register char *ebuf
)
1640 register dl_hp_ppa_ack_t
*ap
;
1641 register dl_hp_ppa_info_t
*ipstart
, *ip
;
1644 register u_long majdev
;
1645 struct stat statbuf
;
1646 dl_hp_ppa_req_t req
;
1649 dl_hp_ppa_ack_t
*dlp
;
1653 memset((char *)&req
, 0, sizeof(req
));
1654 req
.dl_primitive
= DL_HP_PPA_REQ
;
1656 memset((char *)buf
, 0, sizeof(buf
));
1657 if (send_request(fd
, (char *)&req
, sizeof(req
), "hpppa", ebuf
) < 0)
1658 return (PCAP_ERROR
);
1660 ctl
.maxlen
= DL_HP_PPA_ACK_SIZE
;
1662 ctl
.buf
= (char *)buf
;
1666 * DLPI may return a big chunk of data for a DL_HP_PPA_REQ. The normal
1667 * recv_ack will fail because it set the maxlen to MAXDLBUF (8192)
1668 * which is NOT big enough for a DL_HP_PPA_REQ.
1670 * This causes libpcap applications to fail on a system with HP-APA
1673 * To figure out how big the returned data is, we first call getmsg
1674 * to get the small head and peek at the head to get the actual data
1675 * length, and then issue another getmsg to get the actual PPA data.
1677 /* get the head first */
1678 if (getmsg(fd
, &ctl
, (struct strbuf
*)NULL
, &flags
) < 0) {
1679 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1680 errno
, "get_dlpi_ppa: hpppa getmsg");
1681 return (PCAP_ERROR
);
1683 if (ctl
.len
== -1) {
1684 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1685 "get_dlpi_ppa: hpppa getmsg: control buffer has no data");
1686 return (PCAP_ERROR
);
1689 dlp
= (dl_hp_ppa_ack_t
*)ctl
.buf
;
1690 if (dlp
->dl_primitive
!= DL_HP_PPA_ACK
) {
1691 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1692 "get_dlpi_ppa: hpppa unexpected primitive ack 0x%x",
1693 (bpf_u_int32
)dlp
->dl_primitive
);
1694 return (PCAP_ERROR
);
1697 if ((size_t)ctl
.len
< DL_HP_PPA_ACK_SIZE
) {
1698 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1699 "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1700 ctl
.len
, (unsigned long)DL_HP_PPA_ACK_SIZE
);
1701 return (PCAP_ERROR
);
1704 /* allocate buffer */
1705 if ((ppa_data_buf
= (char *)malloc(dlp
->dl_length
)) == NULL
) {
1706 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1707 errno
, "get_dlpi_ppa: hpppa malloc");
1708 return (PCAP_ERROR
);
1710 ctl
.maxlen
= dlp
->dl_length
;
1712 ctl
.buf
= (char *)ppa_data_buf
;
1714 if (getmsg(fd
, &ctl
, (struct strbuf
*)NULL
, &flags
) < 0) {
1715 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1716 errno
, "get_dlpi_ppa: hpppa getmsg");
1718 return (PCAP_ERROR
);
1720 if (ctl
.len
== -1) {
1721 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1722 "get_dlpi_ppa: hpppa getmsg: control buffer has no data");
1723 return (PCAP_ERROR
);
1725 if ((u_int
)ctl
.len
< dlp
->dl_length
) {
1726 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1727 "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1728 ctl
.len
, (unsigned long)dlp
->dl_length
);
1730 return (PCAP_ERROR
);
1733 ap
= (dl_hp_ppa_ack_t
*)buf
;
1734 ipstart
= (dl_hp_ppa_info_t
*)ppa_data_buf
;
1737 #ifdef HAVE_DL_HP_PPA_INFO_T_DL_MODULE_ID_1
1739 * The "dl_hp_ppa_info_t" structure has a "dl_module_id_1"
1740 * member that should, in theory, contain the part of the
1741 * name for the device that comes before the unit number,
1742 * and should also have a "dl_module_id_2" member that may
1743 * contain an alternate name (e.g., I think Ethernet devices
1744 * have both "lan", for "lanN", and "snap", for "snapN", with
1745 * the former being for Ethernet packets and the latter being
1746 * for 802.3/802.2 packets).
1748 * Search for the device that has the specified name and
1751 for (i
= 0; i
< ap
->dl_count
; i
++) {
1752 if ((strcmp((const char *)ip
->dl_module_id_1
, device
) == 0 ||
1753 strcmp((const char *)ip
->dl_module_id_2
, device
) == 0) &&
1754 ip
->dl_instance_num
== unit
)
1757 ip
= (dl_hp_ppa_info_t
*)((u_char
*)ipstart
+ ip
->dl_next_offset
);
1761 * We don't have that member, so the search is impossible; make it
1762 * look as if the search failed.
1767 if (i
== ap
->dl_count
) {
1769 * Well, we didn't, or can't, find the device by name.
1771 * HP-UX 10.20, whilst it has "dl_module_id_1" and
1772 * "dl_module_id_2" fields in the "dl_hp_ppa_info_t",
1773 * doesn't seem to fill them in unless the system is
1774 * at a reasonably up-to-date patch level.
1776 * Older HP-UX 10.x systems might not have those fields
1779 * Therefore, we'll search for the entry with the major
1780 * device number of a device with the name "/dev/<dev><unit>",
1781 * if such a device exists, as the old code did.
1783 snprintf(dname
, sizeof(dname
), "/dev/%s%u", device
, unit
);
1784 if (stat(dname
, &statbuf
) < 0) {
1785 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1786 errno
, "stat: %s", dname
);
1787 return (PCAP_ERROR
);
1789 majdev
= major(statbuf
.st_rdev
);
1793 for (i
= 0; i
< ap
->dl_count
; i
++) {
1794 if (ip
->dl_mjr_num
== majdev
&&
1795 ip
->dl_instance_num
== unit
)
1798 ip
= (dl_hp_ppa_info_t
*)((u_char
*)ipstart
+ ip
->dl_next_offset
);
1801 if (i
== ap
->dl_count
) {
1802 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1803 "can't find /dev/dlpi PPA for %s%u", device
, unit
);
1804 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1806 if (ip
->dl_hdw_state
== HDW_DEAD
) {
1807 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1808 "%s%d: hardware state: DOWN\n", device
, unit
);
1810 return (PCAP_ERROR
);
1820 * Under HP-UX 9, there is no good way to determine the ppa.
1821 * So punt and read it from /dev/kmem.
1823 static struct nlist nl
[] = {
1829 static char path_vmunix
[] = "/hp-ux";
1831 /* Determine ppa number that specifies ifname */
1833 get_dlpi_ppa(register int fd
, register const char *ifname
, register u_int unit
,
1834 u_int
*ppa
, register char *ebuf
)
1836 register const char *cp
;
1840 char if_name
[sizeof(ifnet
.if_name
) + 1];
1842 cp
= strrchr(ifname
, '/');
1845 if (nlist(path_vmunix
, &nl
) < 0) {
1846 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "nlist %s failed",
1848 return (PCAP_ERROR
);
1850 if (nl
[NL_IFNET
].n_value
== 0) {
1851 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1852 "couldn't find %s kernel symbol",
1853 nl
[NL_IFNET
].n_name
);
1854 return (PCAP_ERROR
);
1856 kd
= open("/dev/kmem", O_RDONLY
);
1858 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1859 errno
, "kmem open");
1860 return (PCAP_ERROR
);
1862 if (dlpi_kread(kd
, nl
[NL_IFNET
].n_value
,
1863 &addr
, sizeof(addr
), ebuf
) < 0) {
1865 return (PCAP_ERROR
);
1867 for (; addr
!= NULL
; addr
= ifnet
.if_next
) {
1868 if (dlpi_kread(kd
, (off_t
)addr
,
1869 &ifnet
, sizeof(ifnet
), ebuf
) < 0 ||
1870 dlpi_kread(kd
, (off_t
)ifnet
.if_name
,
1871 if_name
, sizeof(ifnet
.if_name
), ebuf
) < 0) {
1873 return (PCAP_ERROR
);
1875 if_name
[sizeof(ifnet
.if_name
)] = '\0';
1876 if (strcmp(if_name
, ifname
) == 0 && ifnet
.if_unit
== unit
) {
1877 *ppa
= ifnet
.if_index
;
1882 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Can't find %s", ifname
);
1883 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1887 dlpi_kread(register int fd
, register off_t addr
,
1888 register void *buf
, register u_int len
, register char *ebuf
)
1892 if (lseek(fd
, addr
, SEEK_SET
) < 0) {
1893 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1897 cc
= read(fd
, buf
, len
);
1899 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
1902 } else if (cc
!= len
) {
1903 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "short read (%d != %d)", cc
,
1912 pcap_create_interface(const char *device _U_
, char *ebuf
)
1916 struct pcap_dlpi
*pd
;
1919 p
= PCAP_CREATE_COMMON(ebuf
, struct pcap_dlpi
);
1925 pd
->send_fd
= -1; /* it hasn't been opened yet */
1928 p
->activate_op
= pcap_activate_dlpi
;
1933 * Libpcap version string.
1936 pcap_lib_version(void)
1938 return (PCAP_VERSION_STRING
);