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
72 static const char rcsid
[] _U_
=
73 "@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.128 2008-12-02 16:20:23 guy Exp $ (LBL)";
80 #include <sys/types.h>
82 #ifdef HAVE_SYS_BUFMOD_H
83 #include <sys/bufmod.h>
86 #ifdef HAVE_SYS_DLPI_EXT_H
87 #include <sys/dlpi_ext.h>
90 #include <sys/socket.h>
95 #include <sys/stream.h>
96 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
97 #include <sys/systeminfo.h>
120 #define INT_MAX 2147483647
123 #include "pcap-int.h"
124 #include "dlpisubs.h"
126 #ifdef HAVE_OS_PROTO_H
127 #include "os-proto.h"
130 #ifndef PCAP_DEV_PREFIX
132 #define PCAP_DEV_PREFIX "/dev/dlpi"
134 #define PCAP_DEV_PREFIX "/dev"
138 #define MAXDLBUF 8192
141 static char *split_dname(char *, 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 *);
150 static int dlinforeq(int, char *);
151 static int dlinfoack(int, char *, char *);
153 #ifdef HAVE_DLPI_PASSIVE
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(bpf_u_int32
);
162 static char *dlprim(bpf_u_int32
);
163 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
164 static char *get_release(bpf_u_int32
*, bpf_u_int32
*, bpf_u_int32
*);
166 static int send_request(int, char *, int, char *, char *);
168 static int dlpi_kread(int, off_t
, void *, u_int
, char *);
171 static int get_dlpi_ppa(int, const char *, int, char *);
174 /* XXX Needed by HP-UX (at least) */
175 static bpf_u_int32 ctlbuf
[MAXDLBUF
];
176 static struct strbuf ctl
= {
183 * Cast a buffer to "union DL_primitives" without provoking warnings
186 #define MAKE_DL_PRIMITIVES(ptr) ((union DL_primitives *)(void *)(ptr))
189 pcap_read_dlpi(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
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 if (getmsg(p
->fd
, &ctl
, &data
, &flags
) < 0) {
222 /* Don't choke when we get ptraced */
232 strlcpy(p
->errbuf
, pcap_strerror(errno
),
238 bp
= p
->buffer
+ p
->offset
;
242 return (pcap_process_pkts(p
, callback
, user
, cnt
, bp
, cc
));
246 pcap_inject_dlpi(pcap_t
*p
, const void *buf
, size_t size
)
250 #if defined(DLIOCRAW)
251 ret
= write(p
->fd
, buf
, size
);
253 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
254 pcap_strerror(errno
));
257 #elif defined(DL_HP_RAWDLS)
258 if (p
->send_fd
< 0) {
259 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
260 "send: Output FD couldn't be opened");
263 ret
= dlrawdatareq(p
->send_fd
, buf
, size
);
265 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
266 pcap_strerror(errno
));
270 * putmsg() returns either 0 or -1; it doesn't indicate how
271 * many bytes were written (presumably they were all written
272 * or none of them were written). OpenBSD's pcap_inject()
273 * returns the number of bytes written, so, for API compatibility,
274 * we return the number of bytes we were told to write.
277 #else /* no raw mode */
279 * XXX - this is a pain, because you might have to extract
280 * the address from the packet and use it in a DL_UNITDATA_REQ
281 * request. That would be dependent on the link-layer type.
283 * I also don't know what SAP you'd have to bind the descriptor
284 * to, or whether you'd need separate "receive" and "send" FDs,
285 * nor do I know whether you'd need different bindings for
286 * D/I/X Ethernet and 802.3, or for {FDDI,Token Ring} plus
287 * 802.2 and {FDDI,Token Ring} plus 802.2 plus SNAP.
289 * So, for now, we just return a "you can't send" indication,
290 * and leave it up to somebody with a DLPI-based system lacking
291 * both DLIOCRAW and DL_HP_RAWDLS to supply code to implement
292 * packet transmission on that system. If they do, they should
293 * send it to us - but should not send us code that assumes
294 * Ethernet; if the code doesn't work on non-Ethernet interfaces,
295 * it should check "p->linktype" and reject the send request if
296 * it's anything other than DLT_EN10MB.
298 strlcpy(p
->errbuf
, "send: Not supported on this version of this OS",
301 #endif /* raw mode */
306 #define DL_IPATM 0x12 /* ATM Classical IP interface */
314 #define A_GET_UNITS (('A'<<8)|118)
315 #endif /* A_GET_UNITS */
316 #ifndef A_PROMISCON_REQ
317 #define A_PROMISCON_REQ (('A'<<8)|121)
318 #endif /* A_PROMISCON_REQ */
319 #endif /* HAVE_SOLARIS */
322 pcap_cleanup_dlpi(pcap_t
*p
)
324 if (p
->send_fd
>= 0) {
328 pcap_cleanup_live_common(p
);
332 pcap_activate_dlpi(pcap_t
*p
)
339 register dl_info_ack_t
*infop
;
340 #ifdef HAVE_SYS_BUFMOD_H
343 register char *release
;
344 bpf_u_int32 osmajor
, osminor
, osmicro
;
347 bpf_u_int32 buf
[MAXDLBUF
];
349 #ifndef HAVE_DEV_DLPI
352 int status
= PCAP_ERROR
;
356 ** Remove any "/dev/" on the front of the device.
358 cp
= strrchr(p
->opt
.source
, '/');
360 strlcpy(dname
, p
->opt
.source
, sizeof(dname
));
362 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
, &ppa
, p
->errbuf
);
370 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
376 * Use "/dev/dlpi" as the device.
378 * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that
379 * the "dl_mjr_num" field is for the "major number of interface
380 * driver"; that's the major of "/dev/dlpi" on the system on
381 * which I tried this, but there may be DLPI devices that
382 * use a different driver, in which case we may need to
383 * search "/dev" for the appropriate device with that major
384 * device number, rather than hardwiring "/dev/dlpi".
387 if ((p
->fd
= open(cp
, O_RDWR
)) < 0) {
388 if (errno
== EPERM
|| errno
== EACCES
)
389 status
= PCAP_ERROR_PERM_DENIED
;
390 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
391 "%s: %s", cp
, pcap_strerror(errno
));
397 * XXX - HP-UX 10.20 and 11.xx don't appear to support sending and
398 * receiving packets on the same descriptor - you need separate
399 * descriptors for sending and receiving, bound to different SAPs.
401 * If the open fails, we just leave -1 in "p->send_fd" and reject
402 * attempts to send packets, just as if, in pcap-bpf.c, we fail
403 * to open the BPF device for reading and writing, we just try
404 * to open it for reading only and, if that succeeds, just let
405 * the send attempts fail.
407 p
->send_fd
= open(cp
, O_RDWR
);
411 * Get a table of all PPAs for that device, and search that
412 * table for the specified device type name and unit number.
414 ppa
= get_dlpi_ppa(p
->fd
, dname
, ppa
, p
->errbuf
);
421 * If the device name begins with "/", assume it begins with
422 * the pathname of the directory containing the device to open;
423 * otherwise, concatenate the device directory name and the
426 if (*p
->opt
.source
== '/')
427 strlcpy(dname
, p
->opt
.source
, sizeof(dname
));
429 snprintf(dname
, sizeof(dname
), "%s/%s", PCAP_DEV_PREFIX
,
433 * Get the unit number, and a pointer to the end of the device
436 cp
= split_dname(dname
, &ppa
, p
->errbuf
);
438 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
443 * Make a copy of the device pathname, and then remove the unit
444 * number from the device pathname.
446 strlcpy(dname2
, dname
, sizeof(dname
));
449 /* Try device without unit number */
450 if ((p
->fd
= open(dname
, O_RDWR
)) < 0) {
451 if (errno
!= ENOENT
) {
452 if (errno
== EPERM
|| errno
== EACCES
)
453 status
= PCAP_ERROR_PERM_DENIED
;
454 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "%s: %s", dname
,
455 pcap_strerror(errno
));
459 /* Try again with unit number */
460 if ((p
->fd
= open(dname2
, O_RDWR
)) < 0) {
461 if (errno
== ENOENT
) {
462 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
465 * We provide an error message even
466 * for this error, for diagnostic
467 * purposes (so that, for example,
468 * the app can show the message if the
471 * In it, we just report "No DLPI device
472 * found" with the device name, so people
473 * don't get confused and think, for example,
474 * that if they can't capture on "lo0"
475 * on Solaris the fix is to change libpcap
476 * (or the application that uses it) to
477 * look for something other than "/dev/lo0",
478 * as the fix is to look for an operating
479 * system other than Solaris - you just
480 * *can't* capture on a loopback interface
481 * on Solaris, the lack of a DLPI device
482 * for the loopback interface is just a
483 * symptom of that inability.
485 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
486 "%s: No DLPI device found", p
->opt
.source
);
488 if (errno
== EPERM
|| errno
== EACCES
)
489 status
= PCAP_ERROR_PERM_DENIED
;
490 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "%s: %s",
491 dname2
, pcap_strerror(errno
));
495 /* XXX Assume unit zero */
501 ** Attach if "style 2" provider
503 if (dlinforeq(p
->fd
, p
->errbuf
) < 0 ||
504 dlinfoack(p
->fd
, (char *)buf
, p
->errbuf
) < 0)
506 infop
= &(MAKE_DL_PRIMITIVES(buf
))->info_ack
;
508 if (infop
->dl_mac_type
== DL_IPATM
)
511 if (infop
->dl_provider_style
== DL_STYLE2
) {
512 status
= dl_doattach(p
->fd
, ppa
, p
->errbuf
);
516 if (p
->send_fd
>= 0) {
517 if (dl_doattach(p
->send_fd
, ppa
, p
->errbuf
) < 0)
525 * This device exists, but we don't support monitor mode
526 * any platforms that support DLPI.
528 status
= PCAP_ERROR_RFMON_NOTSUP
;
532 #ifdef HAVE_DLPI_PASSIVE
534 * Enable Passive mode to be able to capture on aggregated link.
535 * Not supported in all Solaris versions.
537 dlpassive(p
->fd
, p
->errbuf
);
540 ** Bind (defer if using HP-UX 9 or HP-UX 10.20 or later, totally
541 ** skip if using SINIX)
543 #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20_OR_LATER) && !defined(sinix)
547 ** According to IBM's AIX Support Line, the dl_sap value
548 ** should not be less than 0x600 (1536) for standard Ethernet.
549 ** However, we seem to get DL_BADADDR - "DLSAP addr in improper
550 ** format or invalid" - errors if we use 1537 on the "tr0"
551 ** device, which, given that its name starts with "tr" and that
552 ** it's IBM, probably means a Token Ring device. (Perhaps we
553 ** need to use 1537 on "/dev/dlpi/en" because that device is for
554 ** D/I/X Ethernet, the "SAP" is actually an Ethernet type, and
555 ** it rejects invalid Ethernet types.)
557 ** So if 1537 fails, we try 2, as Hyung Sik Yoon of IBM Korea
558 ** says that works on Token Ring (he says that 0 does *not*
559 ** work; perhaps that's considered an invalid LLC SAP value - I
560 ** assume the SAP value in a DLPI bind is an LLC SAP for network
561 ** types that use 802.2 LLC).
563 if ((dlbindreq(p
->fd
, 1537, p
->errbuf
) < 0 &&
564 dlbindreq(p
->fd
, 2, p
->errbuf
) < 0) ||
565 dlbindack(p
->fd
, (char *)buf
, p
->errbuf
, NULL
) < 0)
567 #elif defined(DL_HP_RAWDLS)
569 ** HP-UX 10.0x and 10.1x.
571 if (dl_dohpuxbind(p
->fd
, p
->errbuf
) < 0)
573 if (p
->send_fd
>= 0) {
575 ** XXX - if this fails, just close send_fd and
576 ** set it to -1, so that you can't send but can
579 if (dl_dohpuxbind(p
->send_fd
, p
->errbuf
) < 0)
582 #else /* neither AIX nor HP-UX */
584 ** Not Sinix, and neither AIX nor HP-UX - Solaris, and any other
587 if (dlbindreq(p
->fd
, 0, p
->errbuf
) < 0 ||
588 dlbindack(p
->fd
, (char *)buf
, p
->errbuf
, NULL
) < 0)
590 #endif /* AIX vs. HP-UX vs. other */
591 #endif /* !HP-UX 9 and !HP-UX 10.20 or later and !SINIX */
596 ** Have to turn on some special ATM promiscuous mode
598 ** Do *NOT* turn regular promiscuous mode on; it doesn't
599 ** help, and may break things.
601 if (strioctl(p
->fd
, A_PROMISCON_REQ
, 0, NULL
) < 0) {
602 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
603 "A_PROMISCON_REQ: %s", pcap_strerror(errno
));
608 if (p
->opt
.promisc
) {
610 ** Enable promiscuous (not necessary on send FD)
612 status
= dlpromiscon(p
, DL_PROMISC_PHYS
);
614 if (status
== PCAP_ERROR_PERM_DENIED
)
615 status
= PCAP_ERROR_PROMISC_PERM_DENIED
;
620 ** Try to enable multicast (you would have thought
621 ** promiscuous would be sufficient). (Skip if using
622 ** HP-UX or SINIX) (Not necessary on send FD)
624 #if !defined(__hpux) && !defined(sinix)
625 status
= dlpromiscon(p
, DL_PROMISC_MULTI
);
627 status
= PCAP_WARNING
;
631 ** Try to enable SAP promiscuity (when not in promiscuous mode
632 ** when using HP-UX, when not doing SunATM on Solaris, and never
633 ** under SINIX) (Not necessary on send FD)
644 status
= dlpromiscon(p
, DL_PROMISC_SAP
);
647 * Not fatal, since the DL_PROMISC_PHYS mode worked.
648 * Report it as a warning, however.
651 status
= PCAP_WARNING
;
659 ** HP-UX 9, and HP-UX 10.20 or later, must bind after setting
660 ** promiscuous options.
662 #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20_OR_LATER)
663 if (dl_dohpuxbind(p
->fd
, p
->errbuf
) < 0)
666 ** We don't set promiscuous mode on the send FD, but we'll defer
667 ** binding it anyway, just to keep the HP-UX 9/10.20 or later
670 if (p
->send_fd
>= 0) {
672 ** XXX - if this fails, just close send_fd and
673 ** set it to -1, so that you can't send but can
676 if (dl_dohpuxbind(p
->send_fd
, p
->errbuf
) < 0)
682 ** Determine link type
683 ** XXX - get SAP length and address length as well, for use
684 ** when sending packets.
686 if (dlinforeq(p
->fd
, p
->errbuf
) < 0 ||
687 dlinfoack(p
->fd
, (char *)buf
, p
->errbuf
) < 0)
690 infop
= &(MAKE_DL_PRIMITIVES(buf
))->info_ack
;
691 if (pcap_process_mactype(p
, infop
->dl_mac_type
) != 0)
696 ** This is a non standard SunOS hack to get the full raw link-layer
699 if (strioctl(p
->fd
, DLIOCRAW
, 0, NULL
) < 0) {
700 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "DLIOCRAW: %s",
701 pcap_strerror(errno
));
706 #ifdef HAVE_SYS_BUFMOD_H
710 ** There is a bug in bufmod(7). When dealing with messages of
711 ** less than snaplen size it strips data from the beginning not
714 ** This bug is fixed in 5.3.2. Also, there is a patch available.
715 ** Ask for bugid 1149065.
718 release
= get_release(&osmajor
, &osminor
, &osmicro
);
719 if (osmajor
== 5 && (osminor
<= 2 || (osminor
== 3 && osmicro
< 2)) &&
720 getenv("BUFMOD_FIXED") == NULL
) {
721 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
722 "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.",
725 status
= PCAP_WARNING
;
729 /* Push and configure bufmod. */
730 if (pcap_conf_bufmod(p
, ss
, p
->md
.timeout
) != 0)
735 ** As the last operation flush the read side.
737 if (ioctl(p
->fd
, I_FLUSH
, FLUSHR
) != 0) {
738 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "FLUSHR: %s",
739 pcap_strerror(errno
));
743 /* Allocate data buffer. */
744 if (pcap_alloc_databuf(p
) != 0)
747 /* Success - but perhaps with a warning */
752 * "p->fd" is an FD for a STREAMS device, so "select()" and
753 * "poll()" should work on it.
755 p
->selectable_fd
= p
->fd
;
757 p
->read_op
= pcap_read_dlpi
;
758 p
->inject_op
= pcap_inject_dlpi
;
759 p
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
760 p
->setdirection_op
= NULL
; /* Not implemented.*/
761 p
->set_datalink_op
= NULL
; /* can't change data link type */
762 p
->getnonblock_op
= pcap_getnonblock_fd
;
763 p
->setnonblock_op
= pcap_setnonblock_fd
;
764 p
->stats_op
= pcap_stats_dlpi
;
765 p
->cleanup_op
= pcap_cleanup_dlpi
;
769 pcap_cleanup_dlpi(p
);
774 * Split a device name into a device type name and a unit number;
775 * return the a pointer to the beginning of the unit number, which
776 * is the end of the device type name, and set "*unitp" to the unit
779 * Returns NULL on error, and fills "ebuf" with an error message.
782 split_dname(char *device
, int *unitp
, char *ebuf
)
789 * Look for a number at the end of the device name string.
791 cp
= device
+ strlen(device
) - 1;
792 if (*cp
< '0' || *cp
> '9') {
793 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s missing unit number",
798 /* Digits at end of string are unit number */
799 while (cp
-1 >= device
&& *(cp
-1) >= '0' && *(cp
-1) <= '9')
803 unit
= strtol(cp
, &eos
, 10);
805 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s bad unit number", device
);
808 if (errno
== ERANGE
|| unit
> INT_MAX
) {
809 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s unit number too large",
814 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s unit number is negative",
823 dl_doattach(int fd
, int ppa
, char *ebuf
)
826 bpf_u_int32 buf
[MAXDLBUF
];
829 req
.dl_primitive
= DL_ATTACH_REQ
;
831 if (send_request(fd
, (char *)&req
, sizeof(req
), "attach", ebuf
) < 0)
834 err
= dlokack(fd
, "attach", (char *)buf
, ebuf
);
842 dl_dohpuxbind(int fd
, char *ebuf
)
846 bpf_u_int32 buf
[MAXDLBUF
];
849 * XXX - we start at 22 because we used to use only 22, but
850 * that was just because that was the value used in some
851 * sample code from HP. With what value *should* we start?
852 * Does it matter, given that we're enabling SAP promiscuity
857 if (dlbindreq(fd
, hpsap
, ebuf
) < 0)
859 if (dlbindack(fd
, (char *)buf
, ebuf
, &uerror
) >= 0)
862 * For any error other than a UNIX EBUSY, give up.
864 if (uerror
!= EBUSY
) {
866 * dlbindack() has already filled in ebuf for
873 * For EBUSY, try the next SAP value; that means that
874 * somebody else is using that SAP. Clear ebuf so
875 * that application doesn't report the "Device busy"
876 * error as a warning.
882 "All SAPs from 22 through 100 are in use",
891 #define STRINGIFY(n) #n
894 dlpromiscon(pcap_t
*p
, bpf_u_int32 level
)
896 dl_promiscon_req_t req
;
897 bpf_u_int32 buf
[MAXDLBUF
];
900 req
.dl_primitive
= DL_PROMISCON_REQ
;
901 req
.dl_level
= level
;
902 if (send_request(p
->fd
, (char *)&req
, sizeof(req
), "promiscon",
905 err
= dlokack(p
->fd
, "promiscon" STRINGIFY(level
), (char *)buf
,
913 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
919 char pad
[516]; /* XXX - must be at least 513; is 516
926 * We may have to do special magic to get ATM devices.
928 if ((fd
= open("/dev/ba", O_RDWR
)) < 0) {
930 * We couldn't open the "ba" device.
931 * For now, just give up; perhaps we should
932 * return an error if the problem is neither
933 * a "that device doesn't exist" error (ENOENT,
934 * ENXIO, etc.) or a "you're not allowed to do
935 * that" error (EPERM, EACCES).
940 if (strioctl(fd
, A_GET_UNITS
, sizeof(buf
), (char *)&buf
) < 0) {
941 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "A_GET_UNITS: %s",
942 pcap_strerror(errno
));
945 for (i
= 0; i
< buf
.nunits
; i
++) {
946 snprintf(baname
, sizeof baname
, "ba%u", i
);
947 if (pcap_add_if(alldevsp
, baname
, 0, NULL
, errbuf
) < 0)
956 send_request(int fd
, char *ptr
, int len
, char *what
, char *ebuf
)
966 if (putmsg(fd
, &ctl
, (struct strbuf
*) NULL
, flags
) < 0) {
967 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
968 "send_request: putmsg \"%s\": %s",
969 what
, pcap_strerror(errno
));
976 recv_ack(int fd
, int size
, const char *what
, char *bufp
, char *ebuf
, int *uerror
)
978 union DL_primitives
*dlp
;
983 * Clear out "*uerror", so it's only set for DL_ERROR_ACK/DL_SYSERR,
984 * making that the only place where EBUSY is treated specially.
989 ctl
.maxlen
= MAXDLBUF
;
994 if (getmsg(fd
, &ctl
, (struct strbuf
*)NULL
, &flags
) < 0) {
995 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "recv_ack: %s getmsg: %s",
996 what
, pcap_strerror(errno
));
1000 dlp
= MAKE_DL_PRIMITIVES(ctl
.buf
);
1001 switch (dlp
->dl_primitive
) {
1006 #ifdef DL_HP_PPA_ACK
1013 switch (dlp
->error_ack
.dl_errno
) {
1017 *uerror
= dlp
->error_ack
.dl_unix_errno
;
1018 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1019 "recv_ack: %s: UNIX error - %s",
1020 what
, pcap_strerror(dlp
->error_ack
.dl_unix_errno
));
1021 if (dlp
->error_ack
.dl_unix_errno
== EPERM
||
1022 dlp
->error_ack
.dl_unix_errno
== EACCES
)
1023 return (PCAP_ERROR_PERM_DENIED
);
1027 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "recv_ack: %s: %s",
1028 what
, dlstrerror(dlp
->error_ack
.dl_errno
));
1029 if (dlp
->error_ack
.dl_errno
== DL_BADPPA
)
1030 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1031 else if (dlp
->error_ack
.dl_errno
== DL_ACCESS
)
1032 return (PCAP_ERROR_PERM_DENIED
);
1035 return (PCAP_ERROR
);
1038 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1039 "recv_ack: %s: Unexpected primitive ack %s",
1040 what
, dlprim(dlp
->dl_primitive
));
1041 return (PCAP_ERROR
);
1044 if (ctl
.len
< size
) {
1045 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1046 "recv_ack: %s: Ack too small (%d < %d)",
1047 what
, ctl
.len
, size
);
1048 return (PCAP_ERROR
);
1054 dlstrerror(bpf_u_int32 dl_errno
)
1056 static char errstring
[6+2+8+1];
1061 return ("Improper permissions for request");
1064 return ("DLSAP addr in improper format or invalid");
1067 return ("Seq number not from outstand DL_CONN_IND");
1070 return ("User data exceeded provider limit");
1073 #ifdef HAVE_DEV_DLPI
1075 * With a single "/dev/dlpi" device used for all
1076 * DLPI providers, PPAs have nothing to do with
1079 return ("Specified PPA was invalid");
1082 * We have separate devices for separate devices;
1083 * the PPA is just the unit number.
1085 return ("Specified PPA (device unit) was invalid");
1089 return ("Primitive received not known by provider");
1091 case DL_BADQOSPARAM
:
1092 return ("QOS parameters contained invalid values");
1095 return ("QOS structure type is unknown/unsupported");
1098 return ("Bad LSAP selector");
1101 return ("Token used not an active stream");
1104 return ("Attempted second bind with dl_max_conind");
1107 return ("Physical link initialization failed");
1110 return ("Provider couldn't allocate alternate address");
1113 return ("Physical link not initialized");
1116 return ("Primitive issued in improper state");
1119 return ("UNIX system error occurred");
1121 case DL_UNSUPPORTED
:
1122 return ("Requested service not supplied by provider");
1124 case DL_UNDELIVERABLE
:
1125 return ("Previous data unit could not be delivered");
1127 case DL_NOTSUPPORTED
:
1128 return ("Primitive is known but not supported");
1131 return ("Limit exceeded");
1134 return ("Promiscuous mode not enabled");
1137 return ("Other streams for PPA in post-attached");
1140 return ("Automatic handling XID&TEST not supported");
1143 return ("Automatic handling of XID not supported");
1146 return ("Automatic handling of TEST not supported");
1149 return ("Automatic handling of XID response");
1152 return ("Automatic handling of TEST response");
1155 return ("Pending outstanding connect indications");
1158 sprintf(errstring
, "Error %02x", dl_errno
);
1164 dlprim(bpf_u_int32 prim
)
1166 static char primbuf
[80];
1171 return ("DL_INFO_REQ");
1174 return ("DL_INFO_ACK");
1177 return ("DL_ATTACH_REQ");
1180 return ("DL_DETACH_REQ");
1183 return ("DL_BIND_REQ");
1186 return ("DL_BIND_ACK");
1189 return ("DL_UNBIND_REQ");
1192 return ("DL_OK_ACK");
1195 return ("DL_ERROR_ACK");
1197 case DL_SUBS_BIND_REQ
:
1198 return ("DL_SUBS_BIND_REQ");
1200 case DL_SUBS_BIND_ACK
:
1201 return ("DL_SUBS_BIND_ACK");
1203 case DL_UNITDATA_REQ
:
1204 return ("DL_UNITDATA_REQ");
1206 case DL_UNITDATA_IND
:
1207 return ("DL_UNITDATA_IND");
1209 case DL_UDERROR_IND
:
1210 return ("DL_UDERROR_IND");
1213 return ("DL_UDQOS_REQ");
1215 case DL_CONNECT_REQ
:
1216 return ("DL_CONNECT_REQ");
1218 case DL_CONNECT_IND
:
1219 return ("DL_CONNECT_IND");
1221 case DL_CONNECT_RES
:
1222 return ("DL_CONNECT_RES");
1224 case DL_CONNECT_CON
:
1225 return ("DL_CONNECT_CON");
1228 return ("DL_TOKEN_REQ");
1231 return ("DL_TOKEN_ACK");
1233 case DL_DISCONNECT_REQ
:
1234 return ("DL_DISCONNECT_REQ");
1236 case DL_DISCONNECT_IND
:
1237 return ("DL_DISCONNECT_IND");
1240 return ("DL_RESET_REQ");
1243 return ("DL_RESET_IND");
1246 return ("DL_RESET_RES");
1249 return ("DL_RESET_CON");
1252 (void) sprintf(primbuf
, "unknown primitive 0x%x", prim
);
1258 dlbindreq(int fd
, bpf_u_int32 sap
, char *ebuf
)
1263 memset((char *)&req
, 0, sizeof(req
));
1264 req
.dl_primitive
= DL_BIND_REQ
;
1265 /* XXX - what if neither of these are defined? */
1266 #if defined(DL_HP_RAWDLS)
1267 req
.dl_max_conind
= 1; /* XXX magic number */
1268 req
.dl_service_mode
= DL_HP_RAWDLS
;
1269 #elif defined(DL_CLDLS)
1270 req
.dl_service_mode
= DL_CLDLS
;
1274 return (send_request(fd
, (char *)&req
, sizeof(req
), "bind", ebuf
));
1278 dlbindack(int fd
, char *bufp
, char *ebuf
, int *uerror
)
1281 return (recv_ack(fd
, DL_BIND_ACK_SIZE
, "bind", bufp
, ebuf
, uerror
));
1285 dlokack(int fd
, const char *what
, char *bufp
, char *ebuf
)
1288 return (recv_ack(fd
, DL_OK_ACK_SIZE
, what
, bufp
, ebuf
, NULL
));
1293 dlinforeq(int fd
, char *ebuf
)
1297 req
.dl_primitive
= DL_INFO_REQ
;
1299 return (send_request(fd
, (char *)&req
, sizeof(req
), "info", ebuf
));
1303 dlinfoack(int fd
, char *bufp
, char *ebuf
)
1306 return (recv_ack(fd
, DL_INFO_ACK_SIZE
, "info", bufp
, ebuf
, NULL
));
1309 #ifdef HAVE_DLPI_PASSIVE
1311 * Enable DLPI passive mode. We do not care if this request fails, as this
1312 * indicates the underlying DLPI device does not support link aggregation.
1315 dlpassive(int fd
, char *ebuf
)
1317 dl_passive_req_t req
;
1318 bpf_u_int32 buf
[MAXDLBUF
];
1320 req
.dl_primitive
= DL_PASSIVE_REQ
;
1322 if (send_request(fd
, (char *)&req
, sizeof(req
), "dlpassive", ebuf
) == 0)
1323 (void) dlokack(fd
, "dlpassive", (char *)buf
, ebuf
);
1329 * There's an ack *if* there's an error.
1332 dlrawdatareq(int fd
, const u_char
*datap
, int datalen
)
1334 struct strbuf ctl
, data
;
1335 long buf
[MAXDLBUF
]; /* XXX - char? */
1336 union DL_primitives
*dlp
;
1339 dlp
= MAKE_DL_PRIMITIVES(buf
);
1341 dlp
->dl_primitive
= DL_HP_RAWDATA_REQ
;
1342 dlen
= DL_HP_RAWDATA_REQ_SIZE
;
1345 * HP's documentation doesn't appear to show us supplying any
1346 * address pointed to by the control part of the message.
1347 * I think that's what raw mode means - you just send the raw
1348 * packet, you don't specify where to send it to, as that's
1349 * implied by the destination address.
1353 ctl
.buf
= (void *)buf
;
1357 data
.buf
= (void *)datap
;
1359 return (putmsg(fd
, &ctl
, &data
, 0));
1361 #endif /* DL_HP_RAWDLS */
1363 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
1365 get_release(bpf_u_int32
*majorp
, bpf_u_int32
*minorp
, bpf_u_int32
*microp
)
1368 static char buf
[32];
1373 if (sysinfo(SI_RELEASE
, buf
, sizeof(buf
)) < 0)
1376 if (!isdigit((unsigned char)*cp
))
1378 *majorp
= strtol(cp
, &cp
, 10);
1381 *minorp
= strtol(cp
, &cp
, 10);
1384 *microp
= strtol(cp
, &cp
, 10);
1389 #ifdef DL_HP_PPA_REQ
1391 * Under HP-UX 10 and HP-UX 11, we can ask for the ppa
1396 * Determine ppa number that specifies ifname.
1398 * If the "dl_hp_ppa_info_t" doesn't have a "dl_module_id_1" member,
1399 * the code that's used here is the old code for HP-UX 10.x.
1401 * However, HP-UX 10.20, at least, appears to have such a member
1402 * in its "dl_hp_ppa_info_t" structure, so the new code is used.
1403 * The new code didn't work on an old 10.20 system on which Rick
1404 * Jones of HP tried it, but with later patches installed, it
1405 * worked - it appears that the older system had those members but
1406 * didn't put anything in them, so, if the search by name fails, we
1407 * do the old search.
1409 * Rick suggests that making sure your system is "up on the latest
1410 * lancommon/DLPI/driver patches" is probably a good idea; it'd fix
1411 * that problem, as well as allowing libpcap to see packets sent
1412 * from the system on which the libpcap application is being run.
1413 * (On 10.20, in addition to getting the latest patches, you need
1414 * to turn the kernel "lanc_outbound_promisc_flag" flag on with ADB;
1415 * a posting to "comp.sys.hp.hpux" at
1417 * http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=558092266
1419 * says that, to see the machine's outgoing traffic, you'd need to
1420 * apply the right patches to your system, and also set that variable
1423 echo 'lanc_outbound_promisc_flag/W1' | /usr/bin/adb -w /stand/vmunix /dev/kmem
1425 * which could be put in, for example, "/sbin/init.d/lan".
1427 * Setting the variable is not necessary on HP-UX 11.x.
1430 get_dlpi_ppa(register int fd
, register const char *device
, register int unit
,
1431 register char *ebuf
)
1433 register dl_hp_ppa_ack_t
*ap
;
1434 register dl_hp_ppa_info_t
*ipstart
, *ip
;
1437 register u_long majdev
;
1438 struct stat statbuf
;
1439 dl_hp_ppa_req_t req
;
1442 dl_hp_ppa_ack_t
*dlp
;
1447 memset((char *)&req
, 0, sizeof(req
));
1448 req
.dl_primitive
= DL_HP_PPA_REQ
;
1450 memset((char *)buf
, 0, sizeof(buf
));
1451 if (send_request(fd
, (char *)&req
, sizeof(req
), "hpppa", ebuf
) < 0)
1452 return (PCAP_ERROR
);
1454 ctl
.maxlen
= DL_HP_PPA_ACK_SIZE
;
1456 ctl
.buf
= (char *)buf
;
1460 * DLPI may return a big chunk of data for a DL_HP_PPA_REQ. The normal
1461 * recv_ack will fail because it set the maxlen to MAXDLBUF (8192)
1462 * which is NOT big enough for a DL_HP_PPA_REQ.
1464 * This causes libpcap applications to fail on a system with HP-APA
1467 * To figure out how big the returned data is, we first call getmsg
1468 * to get the small head and peek at the head to get the actual data
1469 * length, and then issue another getmsg to get the actual PPA data.
1471 /* get the head first */
1472 if (getmsg(fd
, &ctl
, (struct strbuf
*)NULL
, &flags
) < 0) {
1473 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1474 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno
));
1475 return (PCAP_ERROR
);
1478 dlp
= (dl_hp_ppa_ack_t
*)ctl
.buf
;
1479 if (dlp
->dl_primitive
!= DL_HP_PPA_ACK
) {
1480 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1481 "get_dlpi_ppa: hpppa unexpected primitive ack 0x%x",
1482 (bpf_u_int32
)dlp
->dl_primitive
);
1483 return (PCAP_ERROR
);
1486 if (ctl
.len
< DL_HP_PPA_ACK_SIZE
) {
1487 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1488 "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1489 ctl
.len
, (unsigned long)DL_HP_PPA_ACK_SIZE
);
1490 return (PCAP_ERROR
);
1493 /* allocate buffer */
1494 if ((ppa_data_buf
= (char *)malloc(dlp
->dl_length
)) == NULL
) {
1495 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1496 "get_dlpi_ppa: hpppa malloc: %s", pcap_strerror(errno
));
1497 return (PCAP_ERROR
);
1499 ctl
.maxlen
= dlp
->dl_length
;
1501 ctl
.buf
= (char *)ppa_data_buf
;
1503 if (getmsg(fd
, &ctl
, (struct strbuf
*)NULL
, &flags
) < 0) {
1504 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1505 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno
));
1507 return (PCAP_ERROR
);
1509 if (ctl
.len
< dlp
->dl_length
) {
1510 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1511 "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1512 ctl
.len
, (unsigned long)dlp
->dl_length
);
1514 return (PCAP_ERROR
);
1517 ap
= (dl_hp_ppa_ack_t
*)buf
;
1518 ipstart
= (dl_hp_ppa_info_t
*)ppa_data_buf
;
1521 #ifdef HAVE_HP_PPA_INFO_T_DL_MODULE_ID_1
1523 * The "dl_hp_ppa_info_t" structure has a "dl_module_id_1"
1524 * member that should, in theory, contain the part of the
1525 * name for the device that comes before the unit number,
1526 * and should also have a "dl_module_id_2" member that may
1527 * contain an alternate name (e.g., I think Ethernet devices
1528 * have both "lan", for "lanN", and "snap", for "snapN", with
1529 * the former being for Ethernet packets and the latter being
1530 * for 802.3/802.2 packets).
1532 * Search for the device that has the specified name and
1535 for (i
= 0; i
< ap
->dl_count
; i
++) {
1536 if ((strcmp((const char *)ip
->dl_module_id_1
, device
) == 0 ||
1537 strcmp((const char *)ip
->dl_module_id_2
, device
) == 0) &&
1538 ip
->dl_instance_num
== unit
)
1541 ip
= (dl_hp_ppa_info_t
*)((u_char
*)ipstart
+ ip
->dl_next_offset
);
1545 * We don't have that member, so the search is impossible; make it
1546 * look as if the search failed.
1551 if (i
== ap
->dl_count
) {
1553 * Well, we didn't, or can't, find the device by name.
1555 * HP-UX 10.20, whilst it has "dl_module_id_1" and
1556 * "dl_module_id_2" fields in the "dl_hp_ppa_info_t",
1557 * doesn't seem to fill them in unless the system is
1558 * at a reasonably up-to-date patch level.
1560 * Older HP-UX 10.x systems might not have those fields
1563 * Therefore, we'll search for the entry with the major
1564 * device number of a device with the name "/dev/<dev><unit>",
1565 * if such a device exists, as the old code did.
1567 snprintf(dname
, sizeof(dname
), "/dev/%s%d", device
, unit
);
1568 if (stat(dname
, &statbuf
) < 0) {
1569 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "stat: %s: %s",
1570 dname
, pcap_strerror(errno
));
1571 return (PCAP_ERROR
);
1573 majdev
= major(statbuf
.st_rdev
);
1577 for (i
= 0; i
< ap
->dl_count
; i
++) {
1578 if (ip
->dl_mjr_num
== majdev
&&
1579 ip
->dl_instance_num
== unit
)
1582 ip
= (dl_hp_ppa_info_t
*)((u_char
*)ipstart
+ ip
->dl_next_offset
);
1585 if (i
== ap
->dl_count
) {
1586 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1587 "can't find /dev/dlpi PPA for %s%d", device
, unit
);
1588 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1590 if (ip
->dl_hdw_state
== HDW_DEAD
) {
1591 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1592 "%s%d: hardware state: DOWN\n", device
, unit
);
1594 return (PCAP_ERROR
);
1604 * Under HP-UX 9, there is no good way to determine the ppa.
1605 * So punt and read it from /dev/kmem.
1607 static struct nlist nl
[] = {
1613 static char path_vmunix
[] = "/hp-ux";
1615 /* Determine ppa number that specifies ifname */
1617 get_dlpi_ppa(register int fd
, register const char *ifname
, register int unit
,
1618 register char *ebuf
)
1620 register const char *cp
;
1624 char if_name
[sizeof(ifnet
.if_name
) + 1];
1626 cp
= strrchr(ifname
, '/');
1629 if (nlist(path_vmunix
, &nl
) < 0) {
1630 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "nlist %s failed",
1634 if (nl
[NL_IFNET
].n_value
== 0) {
1635 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1636 "could't find %s kernel symbol",
1637 nl
[NL_IFNET
].n_name
);
1640 kd
= open("/dev/kmem", O_RDONLY
);
1642 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "kmem open: %s",
1643 pcap_strerror(errno
));
1646 if (dlpi_kread(kd
, nl
[NL_IFNET
].n_value
,
1647 &addr
, sizeof(addr
), ebuf
) < 0) {
1651 for (; addr
!= NULL
; addr
= ifnet
.if_next
) {
1652 if (dlpi_kread(kd
, (off_t
)addr
,
1653 &ifnet
, sizeof(ifnet
), ebuf
) < 0 ||
1654 dlpi_kread(kd
, (off_t
)ifnet
.if_name
,
1655 if_name
, sizeof(ifnet
.if_name
), ebuf
) < 0) {
1659 if_name
[sizeof(ifnet
.if_name
)] = '\0';
1660 if (strcmp(if_name
, ifname
) == 0 && ifnet
.if_unit
== unit
)
1661 return (ifnet
.if_index
);
1664 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Can't find %s", ifname
);
1669 dlpi_kread(register int fd
, register off_t addr
,
1670 register void *buf
, register u_int len
, register char *ebuf
)
1674 if (lseek(fd
, addr
, SEEK_SET
) < 0) {
1675 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "lseek: %s",
1676 pcap_strerror(errno
));
1679 cc
= read(fd
, buf
, len
);
1681 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "read: %s",
1682 pcap_strerror(errno
));
1684 } else if (cc
!= len
) {
1685 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "short read (%d != %d)", cc
,
1694 pcap_create(const char *device
, char *ebuf
)
1698 p
= pcap_create_common(device
, ebuf
);
1702 p
->send_fd
= -1; /* it hasn't been opened yet */
1704 p
->activate_op
= pcap_activate_dlpi
;