]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-dlpi.c
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.
26 * Packet capture routine for DLPI under SunOS 5, HP-UX 9/10/11, and AIX.
30 * - Apparently the DLIOCRAW ioctl() is specific to SunOS.
32 * - There is a bug in bufmod(7) such that setting the snapshot
33 * length results in data being left of the front of the packet.
35 * - It might be desirable to use pfmod(7) to filter packets in the
36 * kernel when possible.
40 static const char rcsid
[] =
41 "@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.90 2003-07-25 05:32:03 guy Exp $ (LBL)";
48 #include <sys/types.h>
50 #ifdef HAVE_SYS_BUFMOD_H
51 #include <sys/bufmod.h>
54 #ifdef HAVE_SYS_DLPI_EXT_H
55 #include <sys/dlpi_ext.h>
58 #include <sys/socket.h>
60 #ifdef DL_HP_PPA_ACK_OBS
63 #include <sys/stream.h>
64 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
65 #include <sys/systeminfo.h>
87 #ifdef HAVE_OS_PROTO_H
91 #ifndef PCAP_DEV_PREFIX
93 #define PCAP_DEV_PREFIX "/dev/dlpi"
95 #define PCAP_DEV_PREFIX "/dev"
101 #ifdef HAVE_SYS_BUFMOD_H
104 * Size of a bufmod chunk to pass upstream; that appears to be the biggest
105 * value to which you can set it, and setting it to that value (which
106 * is bigger than what appears to be the Solaris default of 8192)
107 * reduces the number of packet drops.
109 #define CHUNKSIZE 65536
112 * Size of the buffer to allocate for packet data we read; it must be
113 * large enough to hold a chunk.
115 #define PKTBUFSIZE CHUNKSIZE
117 #else /* HAVE_SYS_BUFMOD_H */
120 * Size of the buffer to allocate for packet data we read; this is
121 * what the value used to be - there's no particular reason why it
122 * should be tied to MAXDLBUF, but we'll leave it as this for now.
124 #define PKTBUFSIZE (MAXDLBUF * sizeof(bpf_u_int32))
129 static char *split_dname(char *, int *, char *);
130 static int dlattachreq(int, bpf_u_int32
, char *);
131 static int dlbindack(int, char *, char *);
132 static int dlbindreq(int, bpf_u_int32
, char *);
133 static int dlinfoack(int, char *, char *);
134 static int dlinforeq(int, char *);
135 static int dlokack(int, const char *, char *, char *);
136 static int recv_ack(int, int, const char *, char *, char *);
137 static char *dlstrerror(bpf_u_int32
);
138 static char *dlprim(bpf_u_int32
);
139 static int dlpromisconreq(int, bpf_u_int32
, char *);
140 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
141 static char *get_release(bpf_u_int32
*, bpf_u_int32
*, bpf_u_int32
*);
143 static int send_request(int, char *, int, char *, char *);
144 #ifdef HAVE_SYS_BUFMOD_H
145 static int strioctl(int, int, int, char *);
148 static int dlpi_kread(int, off_t
, void *, u_int
, char *);
151 static int get_dlpi_ppa(int, const char *, int, char *);
155 pcap_stats_dlpi(pcap_t
*p
, struct pcap_stat
*ps
)
159 * "ps_recv" counts packets handed to the filter, not packets
160 * that passed the filter. As filtering is done in userland,
161 * this does not include packets dropped because we ran out
164 * "ps_drop" counts packets dropped inside the DLPI service
165 * provider device device because of flow control requirements
166 * or resource exhaustion; it doesn't count packets dropped by
167 * the interface driver, or packets dropped upstream. As
168 * filtering is done in userland, it counts packets regardless
169 * of whether they would've passed the filter.
171 * These statistics don't include packets not yet read from
172 * the kernel by libpcap, but they may include packets not
173 * yet read from libpcap by the application.
179 /* XXX Needed by HP-UX (at least) */
180 static bpf_u_int32 ctlbuf
[MAXDLBUF
];
181 static struct strbuf ctl
= {
188 pcap_read_dlpi(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
190 register int cc
, n
, caplen
, origlen
;
191 register u_char
*bp
, *ep
, *pk
;
192 register struct bpf_insn
*fcode
;
193 #ifdef HAVE_SYS_BUFMOD_H
194 register struct sb_hdr
*sbp
;
201 struct pcap_pkthdr pkthdr
;
206 data
.buf
= (char *)p
->buffer
+ p
->offset
;
207 data
.maxlen
= p
->bufsize
;
210 if (getmsg(p
->fd
, &ctl
, &data
, &flags
) < 0) {
211 /* Don't choke when we get ptraced */
212 if (errno
== EINTR
) {
216 strlcpy(p
->errbuf
, pcap_strerror(errno
),
222 bp
= p
->buffer
+ p
->offset
;
226 /* Loop through packets */
227 fcode
= p
->fcode
.bf_insns
;
230 #ifdef HAVE_SYS_BUFMOD_H
235 memcpy(sbp
, bp
, sizeof(*sbp
));
238 sbp
= (struct sb_hdr
*)bp
;
239 p
->md
.stat
.ps_drop
= sbp
->sbh_drops
;
240 pk
= bp
+ sizeof(*sbp
);
241 bp
+= sbp
->sbh_totlen
;
242 origlen
= sbp
->sbh_origlen
;
243 caplen
= sbp
->sbh_msglen
;
246 caplen
= min(p
->snapshot
, cc
);
250 ++p
->md
.stat
.ps_recv
;
251 if (bpf_filter(fcode
, pk
, origlen
, caplen
)) {
252 #ifdef HAVE_SYS_BUFMOD_H
253 pkthdr
.ts
.tv_sec
= sbp
->sbh_timestamp
.tv_sec
;
254 pkthdr
.ts
.tv_usec
= sbp
->sbh_timestamp
.tv_usec
;
256 (void)gettimeofday(&pkthdr
.ts
, NULL
);
258 pkthdr
.len
= origlen
;
259 pkthdr
.caplen
= caplen
;
260 /* Insure caplen does not exceed snapshot */
261 if (pkthdr
.caplen
> p
->snapshot
)
262 pkthdr
.caplen
= p
->snapshot
;
263 (*callback
)(user
, &pkthdr
, pk
);
264 if (++n
>= cnt
&& cnt
>= 0) {
270 #ifdef HAVE_SYS_BUFMOD_H
278 #define DL_IPATM 0x12 /* ATM Classical IP interface */
286 #define A_GET_UNITS (('A'<<8)|118)
287 #endif /* A_GET_UNITS */
288 #ifndef A_PROMISCON_REQ
289 #define A_PROMISCON_REQ (('A'<<8)|121)
290 #endif /* A_PROMISCON_REQ */
291 #endif /* HAVE_SOLARIS */
294 pcap_close_dlpi(pcap_t
*p
)
296 if (p
->buffer
!= NULL
)
303 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
,
312 register dl_info_ack_t
*infop
;
313 #ifdef HAVE_SYS_BUFMOD_H
314 bpf_u_int32 ss
, chunksize
;
316 register char *release
;
317 bpf_u_int32 osmajor
, osminor
, osmicro
;
320 bpf_u_int32 buf
[MAXDLBUF
];
322 #ifndef HAVE_DEV_DLPI
326 p
= (pcap_t
*)malloc(sizeof(*p
));
328 strlcpy(ebuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
331 memset(p
, 0, sizeof(*p
));
332 p
->fd
= -1; /* indicate that it hasn't been opened yet */
336 ** Remove any "/dev/" on the front of the device.
338 cp
= strrchr(device
, '/');
340 strlcpy(dname
, device
, sizeof(dname
));
342 strlcpy(dname
, cp
+ 1, sizeof(dname
));
345 * Split the device name into a device type name and a unit number;
346 * chop off the unit number, so "dname" is just a device type name.
348 cp
= split_dname(dname
, &ppa
, ebuf
);
354 * Use "/dev/dlpi" as the device.
356 * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that
357 * the "dl_mjr_num" field is for the "major number of interface
358 * driver"; that's the major of "/dev/dlpi" on the system on
359 * which I tried this, but there may be DLPI devices that
360 * use a different driver, in which case we may need to
361 * search "/dev" for the appropriate device with that major
362 * device number, rather than hardwiring "/dev/dlpi".
365 if ((p
->fd
= open(cp
, O_RDWR
)) < 0) {
366 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
367 "%s: %s", cp
, pcap_strerror(errno
));
372 * Get a table of all PPAs for that device, and search that
373 * table for the specified device type name and unit number.
375 ppa
= get_dlpi_ppa(p
->fd
, dname
, ppa
, ebuf
);
380 * If the device name begins with "/", assume it begins with
381 * the pathname of the directory containing the device to open;
382 * otherwise, concatenate the device directory name and the
386 strlcpy(dname
, device
, sizeof(dname
));
388 snprintf(dname
, sizeof(dname
), "%s/%s", PCAP_DEV_PREFIX
,
392 * Get the unit number, and a pointer to the end of the device
395 cp
= split_dname(dname
, &ppa
, ebuf
);
400 * Make a copy of the device pathname, and then remove the unit
401 * number from the device pathname.
403 strlcpy(dname2
, dname
, sizeof(dname
));
406 /* Try device without unit number */
407 if ((p
->fd
= open(dname
, O_RDWR
)) < 0) {
408 if (errno
!= ENOENT
) {
409 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s: %s", dname
,
410 pcap_strerror(errno
));
414 /* Try again with unit number */
415 if ((p
->fd
= open(dname2
, O_RDWR
)) < 0) {
416 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s: %s", dname2
,
417 pcap_strerror(errno
));
420 /* XXX Assume unit zero */
425 p
->snapshot
= snaplen
;
428 ** Attach if "style 2" provider
430 if (dlinforeq(p
->fd
, ebuf
) < 0 ||
431 dlinfoack(p
->fd
, (char *)buf
, ebuf
) < 0)
433 infop
= &((union DL_primitives
*)buf
)->info_ack
;
435 if (infop
->dl_mac_type
== DL_IPATM
)
438 if (infop
->dl_provider_style
== DL_STYLE2
&&
439 (dlattachreq(p
->fd
, ppa
, ebuf
) < 0 ||
440 dlokack(p
->fd
, "attach", (char *)buf
, ebuf
) < 0))
443 ** Bind (defer if using HP-UX 9 or HP-UX 10.20, totally skip if
446 #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20) && !defined(sinix)
448 /* According to IBM's AIX Support Line, the dl_sap value
449 ** should not be less than 0x600 (1536) for standard Ethernet.
450 ** However, we seem to get DL_BADADDR - "DLSAP addr in improper
451 ** format or invalid" - errors if we use 1537 on the "tr0"
452 ** device, which, given that its name starts with "tr" and that
453 ** it's IBM, probably means a Token Ring device. (Perhaps we
454 ** need to use 1537 on "/dev/dlpi/en" because that device is for
455 ** D/I/X Ethernet, the "SAP" is actually an Ethernet type, and
456 ** it rejects invalid Ethernet types.)
458 ** So if 1537 fails, we try 2, as Hyung Sik Yoon of IBM Korea
459 ** says that works on Token Ring (he says that 0 does *not*
460 ** work; perhaps that's considered an invalid LLC SAP value - I
461 ** assume the SAP value in a DLPI bind is an LLC SAP for network
462 ** types that use 802.2 LLC).
464 if ((dlbindreq(p
->fd
, 1537, ebuf
) < 0 &&
465 dlbindreq(p
->fd
, 2, ebuf
) < 0) ||
467 if (dlbindreq(p
->fd
, 0, ebuf
) < 0 ||
469 dlbindack(p
->fd
, (char *)buf
, ebuf
) < 0)
476 ** Have to turn on some special ATM promiscuous mode
478 ** Do *NOT* turn regular promiscuous mode on; it doesn't
479 ** help, and may break things.
481 if (strioctl(p
->fd
, A_PROMISCON_REQ
, 0, NULL
) < 0) {
482 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "A_PROMISCON_REQ: %s",
483 pcap_strerror(errno
));
490 ** Enable promiscuous
492 if (dlpromisconreq(p
->fd
, DL_PROMISC_PHYS
, ebuf
) < 0 ||
493 dlokack(p
->fd
, "promisc_phys", (char *)buf
, ebuf
) < 0)
497 ** Try to enable multicast (you would have thought
498 ** promiscuous would be sufficient). (Skip if using
501 #if !defined(__hpux) && !defined(sinix)
502 if (dlpromisconreq(p
->fd
, DL_PROMISC_MULTI
, ebuf
) < 0 ||
503 dlokack(p
->fd
, "promisc_multi", (char *)buf
, ebuf
) < 0)
505 "WARNING: DL_PROMISC_MULTI failed (%s)\n", ebuf
);
509 ** Try to enable sap (when not in promiscuous mode when using
510 ** using HP-UX, when not doing SunATM on Solaris, and never
521 (dlpromisconreq(p
->fd
, DL_PROMISC_SAP
, ebuf
) < 0 ||
522 dlokack(p
->fd
, "promisc_sap", (char *)buf
, ebuf
) < 0)) {
523 /* Not fatal if promisc since the DL_PROMISC_PHYS worked */
526 "WARNING: DL_PROMISC_SAP failed (%s)\n", ebuf
);
533 ** HP-UX 9 and HP-UX 10.20 must bind after setting promiscuous
536 #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20)
537 if (dlbindreq(p
->fd
, 0, ebuf
) < 0 ||
538 dlbindack(p
->fd
, (char *)buf
, ebuf
) < 0)
543 ** Determine link type
545 if (dlinforeq(p
->fd
, ebuf
) < 0 ||
546 dlinfoack(p
->fd
, (char *)buf
, ebuf
) < 0)
549 infop
= &((union DL_primitives
*)buf
)->info_ack
;
550 switch (infop
->dl_mac_type
) {
554 p
->linktype
= DLT_EN10MB
;
559 p
->linktype
= DLT_FDDI
;
564 p
->linktype
= DLT_IEEE802
;
570 p
->linktype
= DLT_SUNATM
;
571 p
->offset
= 0; /* works for LANE and LLC encapsulation */
576 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "unknown mac type %lu",
577 (unsigned long)infop
->dl_mac_type
);
583 ** This is a non standard SunOS hack to get the full raw link-layer
586 if (strioctl(p
->fd
, DLIOCRAW
, 0, NULL
) < 0) {
587 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "DLIOCRAW: %s",
588 pcap_strerror(errno
));
593 #ifdef HAVE_SYS_BUFMOD_H
595 ** Another non standard call to get the data nicely buffered
597 if (ioctl(p
->fd
, I_PUSH
, "bufmod") != 0) {
598 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "I_PUSH bufmod: %s",
599 pcap_strerror(errno
));
604 ** Now that the bufmod is pushed lets configure it.
606 ** There is a bug in bufmod(7). When dealing with messages of
607 ** less than snaplen size it strips data from the beginning not
610 ** This bug is supposed to be fixed in 5.3.2. Also, there is a
611 ** patch available. Ask for bugid 1149065.
615 release
= get_release(&osmajor
, &osminor
, &osmicro
);
616 if (osmajor
== 5 && (osminor
<= 2 || (osminor
== 3 && osmicro
< 2)) &&
617 getenv("BUFMOD_FIXED") == NULL
) {
619 "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.\n",
625 strioctl(p
->fd
, SBIOCSSNAP
, sizeof(ss
), (char *)&ss
) != 0) {
626 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "SBIOCSSNAP: %s",
627 pcap_strerror(errno
));
632 ** Set up the bufmod timeout
637 to
.tv_sec
= to_ms
/ 1000;
638 to
.tv_usec
= (to_ms
* 1000) % 1000000;
639 if (strioctl(p
->fd
, SBIOCSTIME
, sizeof(to
), (char *)&to
) != 0) {
640 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "SBIOCSTIME: %s",
641 pcap_strerror(errno
));
647 ** Set the chunk length.
649 chunksize
= CHUNKSIZE
;
650 if (strioctl(p
->fd
, SBIOCSCHUNK
, sizeof(chunksize
), (char *)&chunksize
)
652 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "SBIOCSCHUNKP: %s",
653 pcap_strerror(errno
));
659 ** As the last operation flush the read side.
661 if (ioctl(p
->fd
, I_FLUSH
, FLUSHR
) != 0) {
662 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "FLUSHR: %s",
663 pcap_strerror(errno
));
667 /* Allocate data buffer */
668 p
->bufsize
= PKTBUFSIZE
;
669 p
->buffer
= (u_char
*)malloc(p
->bufsize
+ p
->offset
);
670 if (p
->buffer
== NULL
) {
671 strlcpy(ebuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
675 p
->read_op
= pcap_read_dlpi
;
676 p
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
677 p
->set_datalink_op
= NULL
; /* can't change data link type */
678 p
->stats_op
= pcap_stats_dlpi
;
679 p
->close_op
= pcap_close_dlpi
;
690 * Split a device name into a device type name and a unit number;
691 * return the a pointer to the beginning of the unit number, which
692 * is the end of the device type name, and set "*unitp" to the unit
695 * Returns NULL on error, and fills "ebuf" with an error message.
698 split_dname(char *device
, int *unitp
, char *ebuf
)
705 * Look for a number at the end of the device name string.
707 cp
= device
+ strlen(device
) - 1;
708 if (*cp
< '0' || *cp
> '9') {
709 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s missing unit number",
714 /* Digits at end of string are unit number */
715 while (cp
-1 >= device
&& *(cp
-1) >= '0' && *(cp
-1) <= '9')
718 unit
= strtol(cp
, &eos
, 10);
720 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s bad unit number", device
);
728 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
734 char pad
[516]; /* XXX - must be at least 513; is 516
741 * We may have to do special magic to get ATM devices.
743 if ((fd
= open("/dev/ba", O_RDWR
)) < 0) {
745 * We couldn't open the "ba" device.
746 * For now, just give up; perhaps we should
747 * return an error if the problem is neither
748 * a "that device doesn't exist" error (ENOENT,
749 * ENXIO, etc.) or a "you're not allowed to do
750 * that" error (EPERM, EACCES).
755 if (strioctl(fd
, A_GET_UNITS
, sizeof(buf
), (char *)&buf
) < 0) {
756 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "A_GET_UNITS: %s",
757 pcap_strerror(errno
));
760 for (i
= 0; i
< buf
.nunits
; i
++) {
761 snprintf(baname
, sizeof baname
, "ba%u", i
);
762 if (pcap_add_if(alldevsp
, baname
, 0, NULL
, errbuf
) < 0)
771 send_request(int fd
, char *ptr
, int len
, char *what
, char *ebuf
)
781 if (putmsg(fd
, &ctl
, (struct strbuf
*) NULL
, flags
) < 0) {
782 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
783 "send_request: putmsg \"%s\": %s",
784 what
, pcap_strerror(errno
));
791 recv_ack(int fd
, int size
, const char *what
, char *bufp
, char *ebuf
)
793 union DL_primitives
*dlp
;
797 ctl
.maxlen
= MAXDLBUF
;
802 if (getmsg(fd
, &ctl
, (struct strbuf
*)NULL
, &flags
) < 0) {
803 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "recv_ack: %s getmsg: %s",
804 what
, pcap_strerror(errno
));
808 dlp
= (union DL_primitives
*) ctl
.buf
;
809 switch (dlp
->dl_primitive
) {
821 switch (dlp
->error_ack
.dl_errno
) {
824 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
825 "recv_ack: %s: UNIX error - %s",
826 what
, pcap_strerror(dlp
->error_ack
.dl_unix_errno
));
830 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "recv_ack: %s: %s",
831 what
, dlstrerror(dlp
->error_ack
.dl_errno
));
837 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
838 "recv_ack: %s: Unexpected primitive ack %s",
839 what
, dlprim(dlp
->dl_primitive
));
843 if (ctl
.len
< size
) {
844 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
845 "recv_ack: %s: Ack too small (%d < %d)",
846 what
, ctl
.len
, size
);
853 dlstrerror(bpf_u_int32 dl_errno
)
855 static char errstring
[6+2+8+1];
860 return ("Improper permissions for request");
863 return ("DLSAP addr in improper format or invalid");
866 return ("Seq number not from outstand DL_CONN_IND");
869 return ("User data exceeded provider limit");
874 * With a single "/dev/dlpi" device used for all
875 * DLPI providers, PPAs have nothing to do with
878 return ("Specified PPA was invalid");
881 * We have separate devices for separate devices;
882 * the PPA is just the unit number.
884 return ("Specified PPA (device unit) was invalid");
888 return ("Primitive received not known by provider");
891 return ("QOS parameters contained invalid values");
894 return ("QOS structure type is unknown/unsupported");
897 return ("Bad LSAP selector");
900 return ("Token used not an active stream");
903 return ("Attempted second bind with dl_max_conind");
906 return ("Physical link initialization failed");
909 return ("Provider couldn't allocate alternate address");
912 return ("Physical link not initialized");
915 return ("Primitive issued in improper state");
918 return ("UNIX system error occurred");
921 return ("Requested service not supplied by provider");
923 case DL_UNDELIVERABLE
:
924 return ("Previous data unit could not be delivered");
926 case DL_NOTSUPPORTED
:
927 return ("Primitive is known but not supported");
930 return ("Limit exceeded");
933 return ("Promiscuous mode not enabled");
936 return ("Other streams for PPA in post-attached");
939 return ("Automatic handling XID&TEST not supported");
942 return ("Automatic handling of XID not supported");
945 return ("Automatic handling of TEST not supported");
948 return ("Automatic handling of XID response");
951 return ("Automatic handling of TEST response");
954 return ("Pending outstanding connect indications");
957 sprintf(errstring
, "Error %02x", dl_errno
);
963 dlprim(bpf_u_int32 prim
)
965 static char primbuf
[80];
970 return ("DL_INFO_REQ");
973 return ("DL_INFO_ACK");
976 return ("DL_ATTACH_REQ");
979 return ("DL_DETACH_REQ");
982 return ("DL_BIND_REQ");
985 return ("DL_BIND_ACK");
988 return ("DL_UNBIND_REQ");
991 return ("DL_OK_ACK");
994 return ("DL_ERROR_ACK");
996 case DL_SUBS_BIND_REQ
:
997 return ("DL_SUBS_BIND_REQ");
999 case DL_SUBS_BIND_ACK
:
1000 return ("DL_SUBS_BIND_ACK");
1002 case DL_UNITDATA_REQ
:
1003 return ("DL_UNITDATA_REQ");
1005 case DL_UNITDATA_IND
:
1006 return ("DL_UNITDATA_IND");
1008 case DL_UDERROR_IND
:
1009 return ("DL_UDERROR_IND");
1012 return ("DL_UDQOS_REQ");
1014 case DL_CONNECT_REQ
:
1015 return ("DL_CONNECT_REQ");
1017 case DL_CONNECT_IND
:
1018 return ("DL_CONNECT_IND");
1020 case DL_CONNECT_RES
:
1021 return ("DL_CONNECT_RES");
1023 case DL_CONNECT_CON
:
1024 return ("DL_CONNECT_CON");
1027 return ("DL_TOKEN_REQ");
1030 return ("DL_TOKEN_ACK");
1032 case DL_DISCONNECT_REQ
:
1033 return ("DL_DISCONNECT_REQ");
1035 case DL_DISCONNECT_IND
:
1036 return ("DL_DISCONNECT_IND");
1039 return ("DL_RESET_REQ");
1042 return ("DL_RESET_IND");
1045 return ("DL_RESET_RES");
1048 return ("DL_RESET_CON");
1051 (void) sprintf(primbuf
, "unknown primitive 0x%x", prim
);
1057 dlattachreq(int fd
, bpf_u_int32 ppa
, char *ebuf
)
1059 dl_attach_req_t req
;
1061 req
.dl_primitive
= DL_ATTACH_REQ
;
1064 return (send_request(fd
, (char *)&req
, sizeof(req
), "attach", ebuf
));
1068 dlbindreq(int fd
, bpf_u_int32 sap
, char *ebuf
)
1073 memset((char *)&req
, 0, sizeof(req
));
1074 req
.dl_primitive
= DL_BIND_REQ
;
1076 req
.dl_max_conind
= 1; /* XXX magic number */
1077 /* 22 is INSAP as per the HP-UX DLPI Programmer's Guide */
1079 req
.dl_service_mode
= DL_HP_RAWDLS
;
1083 req
.dl_service_mode
= DL_CLDLS
;
1087 return (send_request(fd
, (char *)&req
, sizeof(req
), "bind", ebuf
));
1091 dlbindack(int fd
, char *bufp
, char *ebuf
)
1094 return (recv_ack(fd
, DL_BIND_ACK_SIZE
, "bind", bufp
, ebuf
));
1098 dlpromisconreq(int fd
, bpf_u_int32 level
, char *ebuf
)
1100 dl_promiscon_req_t req
;
1102 req
.dl_primitive
= DL_PROMISCON_REQ
;
1103 req
.dl_level
= level
;
1105 return (send_request(fd
, (char *)&req
, sizeof(req
), "promiscon", ebuf
));
1109 dlokack(int fd
, const char *what
, char *bufp
, char *ebuf
)
1112 return (recv_ack(fd
, DL_OK_ACK_SIZE
, what
, bufp
, ebuf
));
1117 dlinforeq(int fd
, char *ebuf
)
1121 req
.dl_primitive
= DL_INFO_REQ
;
1123 return (send_request(fd
, (char *)&req
, sizeof(req
), "info", ebuf
));
1127 dlinfoack(int fd
, char *bufp
, char *ebuf
)
1130 return (recv_ack(fd
, DL_INFO_ACK_SIZE
, "info", bufp
, ebuf
));
1133 #ifdef HAVE_SYS_BUFMOD_H
1135 strioctl(int fd
, int cmd
, int len
, char *dp
)
1137 struct strioctl str
;
1144 rc
= ioctl(fd
, I_STR
, &str
);
1149 return (str
.ic_len
);
1153 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
1155 get_release(bpf_u_int32
*majorp
, bpf_u_int32
*minorp
, bpf_u_int32
*microp
)
1158 static char buf
[32];
1163 if (sysinfo(SI_RELEASE
, buf
, sizeof(buf
)) < 0)
1166 if (!isdigit((unsigned char)*cp
))
1168 *majorp
= strtol(cp
, &cp
, 10);
1171 *minorp
= strtol(cp
, &cp
, 10);
1174 *microp
= strtol(cp
, &cp
, 10);
1179 #ifdef DL_HP_PPA_ACK_OBS
1181 * Under HP-UX 10 and HP-UX 11, we can ask for the ppa
1186 * Determine ppa number that specifies ifname.
1188 * If the "dl_hp_ppa_info_t" doesn't have a "dl_module_id_1" member,
1189 * the code that's used here is the old code for HP-UX 10.x.
1191 * However, HP-UX 10.20, at least, appears to have such a member
1192 * in its "dl_hp_ppa_info_t" structure, so the new code is used.
1193 * The new code didn't work on an old 10.20 system on which Rick
1194 * Jones of HP tried it, but with later patches installed, it
1195 * worked - it appears that the older system had those members but
1196 * didn't put anything in them, so, if the search by name fails, we
1197 * do the old search.
1199 * Rick suggests that making sure your system is "up on the latest
1200 * lancommon/DLPI/driver patches" is probably a good idea; it'd fix
1201 * that problem, as well as allowing libpcap to see packets sent
1202 * from the system on which the libpcap application is being run.
1203 * (On 10.20, in addition to getting the latest patches, you need
1204 * to turn the kernel "lanc_outbound_promisc_flag" flag on with ADB;
1205 * a posting to "comp.sys.hp.hpux" at
1207 * http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=558092266
1209 * says that, to see the machine's outgoing traffic, you'd need to
1210 * apply the right patches to your system, and also set that variable
1213 echo 'lanc_outbound_promisc_flag/W1' | /usr/bin/adb -w /stand/vmunix /dev/kmem
1215 * which could be put in, for example, "/sbin/init.d/lan".
1217 * Setting the variable is not necessary on HP-UX 11.x.
1220 get_dlpi_ppa(register int fd
, register const char *device
, register int unit
,
1221 register char *ebuf
)
1223 register dl_hp_ppa_ack_t
*ap
;
1224 register dl_hp_ppa_info_t
*ipstart
, *ip
;
1227 register u_long majdev
;
1228 struct stat statbuf
;
1229 dl_hp_ppa_req_t req
;
1232 dl_hp_ppa_ack_t
*dlp
;
1237 memset((char *)&req
, 0, sizeof(req
));
1238 req
.dl_primitive
= DL_HP_PPA_REQ
;
1240 memset((char *)buf
, 0, sizeof(buf
));
1241 if (send_request(fd
, (char *)&req
, sizeof(req
), "hpppa", ebuf
) < 0)
1244 ctl
.maxlen
= DL_HP_PPA_ACK_SIZE
;
1246 ctl
.buf
= (char *)buf
;
1250 * DLPI may return a big chunk of data for a DL_HP_PPA_REQ. The normal
1251 * recv_ack will fail because it set the maxlen to MAXDLBUF (8192)
1252 * which is NOT big enough for a DL_HP_PPA_REQ.
1254 * This causes libpcap applications to fail on a system with HP-APA
1257 * To figure out how big the returned data is, we first call getmsg
1258 * to get the small head and peek at the head to get the actual data
1259 * length, and then issue another getmsg to get the actual PPA data.
1261 /* get the head first */
1262 if (getmsg(fd
, &ctl
, (struct strbuf
*)NULL
, &flags
) < 0) {
1263 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1264 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno
));
1268 dlp
= (dl_hp_ppa_ack_t
*)ctl
.buf
;
1269 if (dlp
->dl_primitive
!= DL_HP_PPA_ACK
) {
1270 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1271 "get_dlpi_ppa: hpppa unexpected primitive ack 0x%x",
1272 (bpf_u_int32
)dlp
->dl_primitive
);
1276 if (ctl
.len
< DL_HP_PPA_ACK_SIZE
) {
1277 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1278 "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1279 ctl
.len
, (unsigned long)DL_HP_PPA_ACK_SIZE
);
1283 /* allocate buffer */
1284 if ((ppa_data_buf
= (char *)malloc(dlp
->dl_length
)) == NULL
) {
1285 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1286 "get_dlpi_ppa: hpppa malloc: %s", pcap_strerror(errno
));
1289 ctl
.maxlen
= dlp
->dl_length
;
1291 ctl
.buf
= (char *)ppa_data_buf
;
1293 if (getmsg(fd
, &ctl
, (struct strbuf
*)NULL
, &flags
) < 0) {
1294 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1295 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno
));
1299 if (ctl
.len
< dlp
->dl_length
) {
1300 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1301 "get_dlpi_ppa: hpppa ack too small (%d < %d)",
1302 ctl
.len
, dlp
->dl_length
);
1307 ap
= (dl_hp_ppa_ack_t
*)buf
;
1308 ipstart
= (dl_hp_ppa_info_t
*)ppa_data_buf
;
1311 #ifdef HAVE_HP_PPA_INFO_T_DL_MODULE_ID_1
1313 * The "dl_hp_ppa_info_t" structure has a "dl_module_id_1"
1314 * member that should, in theory, contain the part of the
1315 * name for the device that comes before the unit number,
1316 * and should also have a "dl_module_id_2" member that may
1317 * contain an alternate name (e.g., I think Ethernet devices
1318 * have both "lan", for "lanN", and "snap", for "snapN", with
1319 * the former being for Ethernet packets and the latter being
1320 * for 802.3/802.2 packets).
1322 * Search for the device that has the specified name and
1325 for (i
= 0; i
< ap
->dl_count
; i
++) {
1326 if ((strcmp((const char *)ip
->dl_module_id_1
, device
) == 0 ||
1327 strcmp((const char *)ip
->dl_module_id_2
, device
) == 0) &&
1328 ip
->dl_instance_num
== unit
)
1331 ip
= (dl_hp_ppa_info_t
*)((u_char
*)ipstart
+ ip
->dl_next_offset
);
1335 * We don't have that member, so the search is impossible; make it
1336 * look as if the search failed.
1341 if (i
== ap
->dl_count
) {
1343 * Well, we didn't, or can't, find the device by name.
1345 * HP-UX 10.20, whilst it has "dl_module_id_1" and
1346 * "dl_module_id_2" fields in the "dl_hp_ppa_info_t",
1347 * doesn't seem to fill them in unless the system is
1348 * at a reasonably up-to-date patch level.
1350 * Older HP-UX 10.x systems might not have those fields
1353 * Therefore, we'll search for the entry with the major
1354 * device number of a device with the name "/dev/<dev><unit>",
1355 * if such a device exists, as the old code did.
1357 snprintf(dname
, sizeof(dname
), "/dev/%s%d", device
, unit
);
1358 if (stat(dname
, &statbuf
) < 0) {
1359 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "stat: %s: %s",
1360 dname
, pcap_strerror(errno
));
1363 majdev
= major(statbuf
.st_rdev
);
1367 for (i
= 0; i
< ap
->dl_count
; i
++) {
1368 if (ip
->dl_mjr_num
== majdev
&&
1369 ip
->dl_instance_num
== unit
)
1372 ip
= (dl_hp_ppa_info_t
*)((u_char
*)ipstart
+ ip
->dl_next_offset
);
1375 if (i
== ap
->dl_count
) {
1376 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1377 "can't find /dev/dlpi PPA for %s%d", device
, unit
);
1380 if (ip
->dl_hdw_state
== HDW_DEAD
) {
1381 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1382 "%s%d: hardware state: DOWN\n", device
, unit
);
1394 * Under HP-UX 9, there is no good way to determine the ppa.
1395 * So punt and read it from /dev/kmem.
1397 static struct nlist nl
[] = {
1403 static char path_vmunix
[] = "/hp-ux";
1405 /* Determine ppa number that specifies ifname */
1407 get_dlpi_ppa(register int fd
, register const char *ifname
, register int unit
,
1408 register char *ebuf
)
1410 register const char *cp
;
1414 char if_name
[sizeof(ifnet
.if_name
) + 1];
1416 cp
= strrchr(ifname
, '/');
1419 if (nlist(path_vmunix
, &nl
) < 0) {
1420 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "nlist %s failed",
1424 if (nl
[NL_IFNET
].n_value
== 0) {
1425 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1426 "could't find %s kernel symbol",
1427 nl
[NL_IFNET
].n_name
);
1430 kd
= open("/dev/kmem", O_RDONLY
);
1432 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "kmem open: %s",
1433 pcap_strerror(errno
));
1436 if (dlpi_kread(kd
, nl
[NL_IFNET
].n_value
,
1437 &addr
, sizeof(addr
), ebuf
) < 0) {
1441 for (; addr
!= NULL
; addr
= ifnet
.if_next
) {
1442 if (dlpi_kread(kd
, (off_t
)addr
,
1443 &ifnet
, sizeof(ifnet
), ebuf
) < 0 ||
1444 dlpi_kread(kd
, (off_t
)ifnet
.if_name
,
1445 if_name
, sizeof(ifnet
.if_name
), ebuf
) < 0) {
1449 if_name
[sizeof(ifnet
.if_name
)] = '\0';
1450 if (strcmp(if_name
, ifname
) == 0 && ifnet
.if_unit
== unit
)
1451 return (ifnet
.if_index
);
1454 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Can't find %s", ifname
);
1459 dlpi_kread(register int fd
, register off_t addr
,
1460 register void *buf
, register u_int len
, register char *ebuf
)
1464 if (lseek(fd
, addr
, SEEK_SET
) < 0) {
1465 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "lseek: %s",
1466 pcap_strerror(errno
));
1469 cc
= read(fd
, buf
, len
);
1471 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "read: %s",
1472 pcap_strerror(errno
));
1474 } else if (cc
!= len
) {
1475 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "short read (%d != %d)", cc
,