]>
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
[] _U_
=
41 "@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.96 2004-03-23 19:18:05 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
;
211 * Has "pcap_breakloop()" been called?
215 * Yes - clear the flag that indicates
216 * that it has, and return -2 to
217 * indicate that we were told to
218 * break out of the loop.
224 * XXX - check for the DLPI primitive, which
225 * would be DL_HP_RAWDATA_IND on HP-UX
226 * if we're in raw mode?
228 if (getmsg(p
->fd
, &ctl
, &data
, &flags
) < 0) {
229 /* Don't choke when we get ptraced */
230 if (errno
== EINTR
) {
234 strlcpy(p
->errbuf
, pcap_strerror(errno
),
240 bp
= p
->buffer
+ p
->offset
;
244 /* Loop through packets */
245 fcode
= p
->fcode
.bf_insns
;
248 #ifdef HAVE_SYS_BUFMOD_H
251 * Has "pcap_breakloop()" been called?
252 * If so, return immediately - if we haven't read any
253 * packets, clear the flag and return -2 to indicate
254 * that we were told to break out of the loop, otherwise
255 * leave the flag set, so that the *next* call will break
256 * out of the loop without having read any packets, and
257 * return the number of packets we've processed so far.
272 memcpy(sbp
, bp
, sizeof(*sbp
));
275 sbp
= (struct sb_hdr
*)bp
;
276 p
->md
.stat
.ps_drop
= sbp
->sbh_drops
;
277 pk
= bp
+ sizeof(*sbp
);
278 bp
+= sbp
->sbh_totlen
;
279 origlen
= sbp
->sbh_origlen
;
280 caplen
= sbp
->sbh_msglen
;
283 caplen
= min(p
->snapshot
, cc
);
287 ++p
->md
.stat
.ps_recv
;
288 if (bpf_filter(fcode
, pk
, origlen
, caplen
)) {
289 #ifdef HAVE_SYS_BUFMOD_H
290 pkthdr
.ts
.tv_sec
= sbp
->sbh_timestamp
.tv_sec
;
291 pkthdr
.ts
.tv_usec
= sbp
->sbh_timestamp
.tv_usec
;
293 (void)gettimeofday(&pkthdr
.ts
, NULL
);
295 pkthdr
.len
= origlen
;
296 pkthdr
.caplen
= caplen
;
297 /* Insure caplen does not exceed snapshot */
298 if (pkthdr
.caplen
> p
->snapshot
)
299 pkthdr
.caplen
= p
->snapshot
;
300 (*callback
)(user
, &pkthdr
, pk
);
301 if (++n
>= cnt
&& cnt
>= 0) {
307 #ifdef HAVE_SYS_BUFMOD_H
315 pcap_inject_dlpi(pcap_t
*p
, const void *buf
, size_t size
)
320 * XXX - use "dlunitdatareq()" on HP-UX.
322 ret
= write(p
->fd
, buf
, size
);
324 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
325 pcap_strerror(errno
));
332 #define DL_IPATM 0x12 /* ATM Classical IP interface */
340 #define A_GET_UNITS (('A'<<8)|118)
341 #endif /* A_GET_UNITS */
342 #ifndef A_PROMISCON_REQ
343 #define A_PROMISCON_REQ (('A'<<8)|121)
344 #endif /* A_PROMISCON_REQ */
345 #endif /* HAVE_SOLARIS */
348 pcap_close_dlpi(pcap_t
*p
)
350 if (p
->buffer
!= NULL
)
357 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
,
366 register dl_info_ack_t
*infop
;
367 #ifdef HAVE_SYS_BUFMOD_H
368 bpf_u_int32 ss
, chunksize
;
370 register char *release
;
371 bpf_u_int32 osmajor
, osminor
, osmicro
;
374 bpf_u_int32 buf
[MAXDLBUF
];
376 #ifndef HAVE_DEV_DLPI
380 p
= (pcap_t
*)malloc(sizeof(*p
));
382 strlcpy(ebuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
385 memset(p
, 0, sizeof(*p
));
386 p
->fd
= -1; /* indicate that it hasn't been opened yet */
390 ** Remove any "/dev/" on the front of the device.
392 cp
= strrchr(device
, '/');
394 strlcpy(dname
, device
, sizeof(dname
));
396 strlcpy(dname
, cp
+ 1, sizeof(dname
));
399 * Split the device name into a device type name and a unit number;
400 * chop off the unit number, so "dname" is just a device type name.
402 cp
= split_dname(dname
, &ppa
, ebuf
);
408 * Use "/dev/dlpi" as the device.
410 * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that
411 * the "dl_mjr_num" field is for the "major number of interface
412 * driver"; that's the major of "/dev/dlpi" on the system on
413 * which I tried this, but there may be DLPI devices that
414 * use a different driver, in which case we may need to
415 * search "/dev" for the appropriate device with that major
416 * device number, rather than hardwiring "/dev/dlpi".
419 if ((p
->fd
= open(cp
, O_RDWR
)) < 0) {
420 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
421 "%s: %s", cp
, pcap_strerror(errno
));
426 * Get a table of all PPAs for that device, and search that
427 * table for the specified device type name and unit number.
429 ppa
= get_dlpi_ppa(p
->fd
, dname
, ppa
, ebuf
);
434 * If the device name begins with "/", assume it begins with
435 * the pathname of the directory containing the device to open;
436 * otherwise, concatenate the device directory name and the
440 strlcpy(dname
, device
, sizeof(dname
));
442 snprintf(dname
, sizeof(dname
), "%s/%s", PCAP_DEV_PREFIX
,
446 * Get the unit number, and a pointer to the end of the device
449 cp
= split_dname(dname
, &ppa
, ebuf
);
454 * Make a copy of the device pathname, and then remove the unit
455 * number from the device pathname.
457 strlcpy(dname2
, dname
, sizeof(dname
));
460 /* Try device without unit number */
461 if ((p
->fd
= open(dname
, O_RDWR
)) < 0) {
462 if (errno
!= ENOENT
) {
463 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s: %s", dname
,
464 pcap_strerror(errno
));
468 /* Try again with unit number */
469 if ((p
->fd
= open(dname2
, O_RDWR
)) < 0) {
470 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s: %s", dname2
,
471 pcap_strerror(errno
));
474 /* XXX Assume unit zero */
479 p
->snapshot
= snaplen
;
482 ** Attach if "style 2" provider
484 if (dlinforeq(p
->fd
, ebuf
) < 0 ||
485 dlinfoack(p
->fd
, (char *)buf
, ebuf
) < 0)
487 infop
= &((union DL_primitives
*)buf
)->info_ack
;
489 if (infop
->dl_mac_type
== DL_IPATM
)
492 if (infop
->dl_provider_style
== DL_STYLE2
&&
493 (dlattachreq(p
->fd
, ppa
, ebuf
) < 0 ||
494 dlokack(p
->fd
, "attach", (char *)buf
, ebuf
) < 0))
497 ** Bind (defer if using HP-UX 9 or HP-UX 10.20, totally skip if
500 #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20) && !defined(sinix)
502 /* According to IBM's AIX Support Line, the dl_sap value
503 ** should not be less than 0x600 (1536) for standard Ethernet.
504 ** However, we seem to get DL_BADADDR - "DLSAP addr in improper
505 ** format or invalid" - errors if we use 1537 on the "tr0"
506 ** device, which, given that its name starts with "tr" and that
507 ** it's IBM, probably means a Token Ring device. (Perhaps we
508 ** need to use 1537 on "/dev/dlpi/en" because that device is for
509 ** D/I/X Ethernet, the "SAP" is actually an Ethernet type, and
510 ** it rejects invalid Ethernet types.)
512 ** So if 1537 fails, we try 2, as Hyung Sik Yoon of IBM Korea
513 ** says that works on Token Ring (he says that 0 does *not*
514 ** work; perhaps that's considered an invalid LLC SAP value - I
515 ** assume the SAP value in a DLPI bind is an LLC SAP for network
516 ** types that use 802.2 LLC).
518 if ((dlbindreq(p
->fd
, 1537, ebuf
) < 0 &&
519 dlbindreq(p
->fd
, 2, ebuf
) < 0) ||
521 if (dlbindreq(p
->fd
, 0, ebuf
) < 0 ||
523 dlbindack(p
->fd
, (char *)buf
, ebuf
) < 0)
530 ** Have to turn on some special ATM promiscuous mode
532 ** Do *NOT* turn regular promiscuous mode on; it doesn't
533 ** help, and may break things.
535 if (strioctl(p
->fd
, A_PROMISCON_REQ
, 0, NULL
) < 0) {
536 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "A_PROMISCON_REQ: %s",
537 pcap_strerror(errno
));
544 ** Enable promiscuous
546 if (dlpromisconreq(p
->fd
, DL_PROMISC_PHYS
, ebuf
) < 0 ||
547 dlokack(p
->fd
, "promisc_phys", (char *)buf
, ebuf
) < 0)
551 ** Try to enable multicast (you would have thought
552 ** promiscuous would be sufficient). (Skip if using
555 #if !defined(__hpux) && !defined(sinix)
556 if (dlpromisconreq(p
->fd
, DL_PROMISC_MULTI
, ebuf
) < 0 ||
557 dlokack(p
->fd
, "promisc_multi", (char *)buf
, ebuf
) < 0)
559 "WARNING: DL_PROMISC_MULTI failed (%s)\n", ebuf
);
563 ** Try to enable sap (when not in promiscuous mode when using
564 ** using HP-UX, when not doing SunATM on Solaris, and never
575 (dlpromisconreq(p
->fd
, DL_PROMISC_SAP
, ebuf
) < 0 ||
576 dlokack(p
->fd
, "promisc_sap", (char *)buf
, ebuf
) < 0)) {
577 /* Not fatal if promisc since the DL_PROMISC_PHYS worked */
580 "WARNING: DL_PROMISC_SAP failed (%s)\n", ebuf
);
587 ** HP-UX 9 and HP-UX 10.20 must bind after setting promiscuous
590 #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20)
591 if (dlbindreq(p
->fd
, 0, ebuf
) < 0 ||
592 dlbindack(p
->fd
, (char *)buf
, ebuf
) < 0)
597 ** Determine link type
598 ** XXX - get SAP length and address length as well, for use
599 ** when sending packets.
601 if (dlinforeq(p
->fd
, ebuf
) < 0 ||
602 dlinfoack(p
->fd
, (char *)buf
, ebuf
) < 0)
605 infop
= &((union DL_primitives
*)buf
)->info_ack
;
606 switch (infop
->dl_mac_type
) {
610 p
->linktype
= DLT_EN10MB
;
613 * This is (presumably) a real Ethernet capture; give it a
614 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
615 * that an application can let you choose it, in case you're
616 * capturing DOCSIS traffic that a Cisco Cable Modem
617 * Termination System is putting out onto an Ethernet (it
618 * doesn't put an Ethernet header onto the wire, it puts raw
619 * DOCSIS frames out on the wire inside the low-level
622 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
624 * If that fails, just leave the list empty.
626 if (p
->dlt_list
!= NULL
) {
627 p
->dlt_list
[0] = DLT_EN10MB
;
628 p
->dlt_list
[1] = DLT_DOCSIS
;
634 p
->linktype
= DLT_FDDI
;
640 * XXX - what about DL_TPB? Is that Token Bus?
642 p
->linktype
= DLT_IEEE802
;
648 p
->linktype
= DLT_SUNATM
;
649 p
->offset
= 0; /* works for LANE and LLC encapsulation */
654 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "unknown mac type %lu",
655 (unsigned long)infop
->dl_mac_type
);
661 ** This is a non standard SunOS hack to get the full raw link-layer
664 if (strioctl(p
->fd
, DLIOCRAW
, 0, NULL
) < 0) {
665 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "DLIOCRAW: %s",
666 pcap_strerror(errno
));
671 #ifdef HAVE_SYS_BUFMOD_H
673 ** Another non standard call to get the data nicely buffered
675 if (ioctl(p
->fd
, I_PUSH
, "bufmod") != 0) {
676 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "I_PUSH bufmod: %s",
677 pcap_strerror(errno
));
682 ** Now that the bufmod is pushed lets configure it.
684 ** There is a bug in bufmod(7). When dealing with messages of
685 ** less than snaplen size it strips data from the beginning not
688 ** This bug is supposed to be fixed in 5.3.2. Also, there is a
689 ** patch available. Ask for bugid 1149065.
693 release
= get_release(&osmajor
, &osminor
, &osmicro
);
694 if (osmajor
== 5 && (osminor
<= 2 || (osminor
== 3 && osmicro
< 2)) &&
695 getenv("BUFMOD_FIXED") == NULL
) {
697 "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.\n",
703 strioctl(p
->fd
, SBIOCSSNAP
, sizeof(ss
), (char *)&ss
) != 0) {
704 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "SBIOCSSNAP: %s",
705 pcap_strerror(errno
));
710 ** Set up the bufmod timeout
715 to
.tv_sec
= to_ms
/ 1000;
716 to
.tv_usec
= (to_ms
* 1000) % 1000000;
717 if (strioctl(p
->fd
, SBIOCSTIME
, sizeof(to
), (char *)&to
) != 0) {
718 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "SBIOCSTIME: %s",
719 pcap_strerror(errno
));
725 ** Set the chunk length.
727 chunksize
= CHUNKSIZE
;
728 if (strioctl(p
->fd
, SBIOCSCHUNK
, sizeof(chunksize
), (char *)&chunksize
)
730 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "SBIOCSCHUNKP: %s",
731 pcap_strerror(errno
));
737 ** As the last operation flush the read side.
739 if (ioctl(p
->fd
, I_FLUSH
, FLUSHR
) != 0) {
740 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "FLUSHR: %s",
741 pcap_strerror(errno
));
745 /* Allocate data buffer */
746 p
->bufsize
= PKTBUFSIZE
;
747 p
->buffer
= (u_char
*)malloc(p
->bufsize
+ p
->offset
);
748 if (p
->buffer
== NULL
) {
749 strlcpy(ebuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
754 * "p->fd" is an FD for a STREAMS device, so "select()" and
755 * "poll()" should work on it.
757 p
->selectable_fd
= p
->fd
;
759 p
->read_op
= pcap_read_dlpi
;
760 p
->inject_op
= pcap_inject_dlpi
;
761 p
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
762 p
->set_datalink_op
= NULL
; /* can't change data link type */
763 p
->getnonblock_op
= pcap_getnonblock_fd
;
764 p
->setnonblock_op
= pcap_setnonblock_fd
;
765 p
->stats_op
= pcap_stats_dlpi
;
766 p
->close_op
= pcap_close_dlpi
;
773 * Get rid of any link-layer type list we allocated.
775 if (p
->dlt_list
!= NULL
)
782 * Split a device name into a device type name and a unit number;
783 * return the a pointer to the beginning of the unit number, which
784 * is the end of the device type name, and set "*unitp" to the unit
787 * Returns NULL on error, and fills "ebuf" with an error message.
790 split_dname(char *device
, int *unitp
, char *ebuf
)
797 * Look for a number at the end of the device name string.
799 cp
= device
+ strlen(device
) - 1;
800 if (*cp
< '0' || *cp
> '9') {
801 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s missing unit number",
806 /* Digits at end of string are unit number */
807 while (cp
-1 >= device
&& *(cp
-1) >= '0' && *(cp
-1) <= '9')
810 unit
= strtol(cp
, &eos
, 10);
812 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s bad unit number", device
);
820 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
826 char pad
[516]; /* XXX - must be at least 513; is 516
833 * We may have to do special magic to get ATM devices.
835 if ((fd
= open("/dev/ba", O_RDWR
)) < 0) {
837 * We couldn't open the "ba" device.
838 * For now, just give up; perhaps we should
839 * return an error if the problem is neither
840 * a "that device doesn't exist" error (ENOENT,
841 * ENXIO, etc.) or a "you're not allowed to do
842 * that" error (EPERM, EACCES).
847 if (strioctl(fd
, A_GET_UNITS
, sizeof(buf
), (char *)&buf
) < 0) {
848 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "A_GET_UNITS: %s",
849 pcap_strerror(errno
));
852 for (i
= 0; i
< buf
.nunits
; i
++) {
853 snprintf(baname
, sizeof baname
, "ba%u", i
);
854 if (pcap_add_if(alldevsp
, baname
, 0, NULL
, errbuf
) < 0)
863 send_request(int fd
, char *ptr
, int len
, char *what
, char *ebuf
)
873 if (putmsg(fd
, &ctl
, (struct strbuf
*) NULL
, flags
) < 0) {
874 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
875 "send_request: putmsg \"%s\": %s",
876 what
, pcap_strerror(errno
));
883 recv_ack(int fd
, int size
, const char *what
, char *bufp
, char *ebuf
)
885 union DL_primitives
*dlp
;
889 ctl
.maxlen
= MAXDLBUF
;
894 if (getmsg(fd
, &ctl
, (struct strbuf
*)NULL
, &flags
) < 0) {
895 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "recv_ack: %s getmsg: %s",
896 what
, pcap_strerror(errno
));
900 dlp
= (union DL_primitives
*) ctl
.buf
;
901 switch (dlp
->dl_primitive
) {
913 switch (dlp
->error_ack
.dl_errno
) {
916 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
917 "recv_ack: %s: UNIX error - %s",
918 what
, pcap_strerror(dlp
->error_ack
.dl_unix_errno
));
922 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "recv_ack: %s: %s",
923 what
, dlstrerror(dlp
->error_ack
.dl_errno
));
929 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
930 "recv_ack: %s: Unexpected primitive ack %s",
931 what
, dlprim(dlp
->dl_primitive
));
935 if (ctl
.len
< size
) {
936 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
937 "recv_ack: %s: Ack too small (%d < %d)",
938 what
, ctl
.len
, size
);
945 dlstrerror(bpf_u_int32 dl_errno
)
947 static char errstring
[6+2+8+1];
952 return ("Improper permissions for request");
955 return ("DLSAP addr in improper format or invalid");
958 return ("Seq number not from outstand DL_CONN_IND");
961 return ("User data exceeded provider limit");
966 * With a single "/dev/dlpi" device used for all
967 * DLPI providers, PPAs have nothing to do with
970 return ("Specified PPA was invalid");
973 * We have separate devices for separate devices;
974 * the PPA is just the unit number.
976 return ("Specified PPA (device unit) was invalid");
980 return ("Primitive received not known by provider");
983 return ("QOS parameters contained invalid values");
986 return ("QOS structure type is unknown/unsupported");
989 return ("Bad LSAP selector");
992 return ("Token used not an active stream");
995 return ("Attempted second bind with dl_max_conind");
998 return ("Physical link initialization failed");
1001 return ("Provider couldn't allocate alternate address");
1004 return ("Physical link not initialized");
1007 return ("Primitive issued in improper state");
1010 return ("UNIX system error occurred");
1012 case DL_UNSUPPORTED
:
1013 return ("Requested service not supplied by provider");
1015 case DL_UNDELIVERABLE
:
1016 return ("Previous data unit could not be delivered");
1018 case DL_NOTSUPPORTED
:
1019 return ("Primitive is known but not supported");
1022 return ("Limit exceeded");
1025 return ("Promiscuous mode not enabled");
1028 return ("Other streams for PPA in post-attached");
1031 return ("Automatic handling XID&TEST not supported");
1034 return ("Automatic handling of XID not supported");
1037 return ("Automatic handling of TEST not supported");
1040 return ("Automatic handling of XID response");
1043 return ("Automatic handling of TEST response");
1046 return ("Pending outstanding connect indications");
1049 sprintf(errstring
, "Error %02x", dl_errno
);
1055 dlprim(bpf_u_int32 prim
)
1057 static char primbuf
[80];
1062 return ("DL_INFO_REQ");
1065 return ("DL_INFO_ACK");
1068 return ("DL_ATTACH_REQ");
1071 return ("DL_DETACH_REQ");
1074 return ("DL_BIND_REQ");
1077 return ("DL_BIND_ACK");
1080 return ("DL_UNBIND_REQ");
1083 return ("DL_OK_ACK");
1086 return ("DL_ERROR_ACK");
1088 case DL_SUBS_BIND_REQ
:
1089 return ("DL_SUBS_BIND_REQ");
1091 case DL_SUBS_BIND_ACK
:
1092 return ("DL_SUBS_BIND_ACK");
1094 case DL_UNITDATA_REQ
:
1095 return ("DL_UNITDATA_REQ");
1097 case DL_UNITDATA_IND
:
1098 return ("DL_UNITDATA_IND");
1100 case DL_UDERROR_IND
:
1101 return ("DL_UDERROR_IND");
1104 return ("DL_UDQOS_REQ");
1106 case DL_CONNECT_REQ
:
1107 return ("DL_CONNECT_REQ");
1109 case DL_CONNECT_IND
:
1110 return ("DL_CONNECT_IND");
1112 case DL_CONNECT_RES
:
1113 return ("DL_CONNECT_RES");
1115 case DL_CONNECT_CON
:
1116 return ("DL_CONNECT_CON");
1119 return ("DL_TOKEN_REQ");
1122 return ("DL_TOKEN_ACK");
1124 case DL_DISCONNECT_REQ
:
1125 return ("DL_DISCONNECT_REQ");
1127 case DL_DISCONNECT_IND
:
1128 return ("DL_DISCONNECT_IND");
1131 return ("DL_RESET_REQ");
1134 return ("DL_RESET_IND");
1137 return ("DL_RESET_RES");
1140 return ("DL_RESET_CON");
1143 (void) sprintf(primbuf
, "unknown primitive 0x%x", prim
);
1149 dlattachreq(int fd
, bpf_u_int32 ppa
, char *ebuf
)
1151 dl_attach_req_t req
;
1153 req
.dl_primitive
= DL_ATTACH_REQ
;
1156 return (send_request(fd
, (char *)&req
, sizeof(req
), "attach", ebuf
));
1160 dlbindreq(int fd
, bpf_u_int32 sap
, char *ebuf
)
1165 memset((char *)&req
, 0, sizeof(req
));
1166 req
.dl_primitive
= DL_BIND_REQ
;
1168 req
.dl_max_conind
= 1; /* XXX magic number */
1169 /* 22 is INSAP as per the HP-UX DLPI Programmer's Guide */
1171 req
.dl_service_mode
= DL_HP_RAWDLS
;
1175 req
.dl_service_mode
= DL_CLDLS
;
1179 return (send_request(fd
, (char *)&req
, sizeof(req
), "bind", ebuf
));
1183 dlbindack(int fd
, char *bufp
, char *ebuf
)
1186 return (recv_ack(fd
, DL_BIND_ACK_SIZE
, "bind", bufp
, ebuf
));
1190 dlpromisconreq(int fd
, bpf_u_int32 level
, char *ebuf
)
1192 dl_promiscon_req_t req
;
1194 req
.dl_primitive
= DL_PROMISCON_REQ
;
1195 req
.dl_level
= level
;
1197 return (send_request(fd
, (char *)&req
, sizeof(req
), "promiscon", ebuf
));
1201 dlokack(int fd
, const char *what
, char *bufp
, char *ebuf
)
1204 return (recv_ack(fd
, DL_OK_ACK_SIZE
, what
, bufp
, ebuf
));
1209 dlinforeq(int fd
, char *ebuf
)
1213 req
.dl_primitive
= DL_INFO_REQ
;
1215 return (send_request(fd
, (char *)&req
, sizeof(req
), "info", ebuf
));
1219 dlinfoack(int fd
, char *bufp
, char *ebuf
)
1222 return (recv_ack(fd
, DL_INFO_ACK_SIZE
, "info", bufp
, ebuf
));
1229 * On HP-UX, use DL_HP_RAWDATA_REQ instead, *if* we're in RAW mode.
1230 * There is an ack *IF* there's an error.
1232 * XXX - this is not needed on Solaris; we're running in raw mode so
1233 * we can just do a "write()".
1234 * What about, say, AIX?
1237 dlunitdatareq(int fd
, u_char
*addrp
, int addrlen
, u_char
*datap
, int datalen
)
1239 struct strbuf ctl
, data
;
1240 long buf
[MAXDLBUF
]; /* XXX - char? */
1241 union DL_primitives
*dlp
;
1243 dlp
= (union DL_primitives
*) buf
;
1245 dlp
->unitdata_req
.dl_primitive
= DL_UNITDATA_REQ
;
1246 dlp
->unitdata_req
.dl_dest_addr_length
= addrlen
;
1247 dlp
->unitdata_req
.dl_dest_addr_offset
= DL_UNITDATA_REQ_SIZE
;
1248 dlp
->unitdata_req
.dl_priority
.dl_min
= 0;
1249 dlp
->unitdata_req
.dl_priority
.dl_max
= 0
1252 * XXX - the "address", on Ethernet, is the destination address,
1253 * followed by the link-layer type. What is it for other
1256 * The address length "dl_addr_length" from the "info_ack"
1257 * structure is the total length of the link-layer address.
1259 * The SAP length "dl_sap_length" is the length of the SAP part
1261 * If positive, the SAP comes before the destination address;
1262 * if negative, the SAP comes after the destination address.
1264 * XXX - what about Ethernet vs. 802.3? Is the SAP the Ethertype
1265 * or the DSAP? How can we send both? *Can* we send both on
1268 * Note that in raw mode, we send a raw link-layer packet.
1269 * In that mode, can we avoid worrying about this crap?
1271 * For ATM (ha ha), we might not be able to send packets,
1272 * even in raw mode, without binding to a VC. (Presumably
1273 * going into SAP promiscuous mode lets us *see* all packets.)
1275 * XXX - extract it from the packet to be sent.
1277 memcpy((char *)dlp
+ DL_UNITDATA_REQ_SIZE
, addrp
, addrlen
);
1280 ctl
.len
= DL_UNITDATA_REQ_SIZE
+ addrlen
;
1287 if (putmsg(fd
, &ctl
, &data
, 0) < 0) {
1288 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1289 "dlunitdatareq: putmsg: %s", pcap_strerror(errno
));
1296 #ifdef HAVE_SYS_BUFMOD_H
1298 strioctl(int fd
, int cmd
, int len
, char *dp
)
1300 struct strioctl str
;
1307 rc
= ioctl(fd
, I_STR
, &str
);
1312 return (str
.ic_len
);
1316 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
1318 get_release(bpf_u_int32
*majorp
, bpf_u_int32
*minorp
, bpf_u_int32
*microp
)
1321 static char buf
[32];
1326 if (sysinfo(SI_RELEASE
, buf
, sizeof(buf
)) < 0)
1329 if (!isdigit((unsigned char)*cp
))
1331 *majorp
= strtol(cp
, &cp
, 10);
1334 *minorp
= strtol(cp
, &cp
, 10);
1337 *microp
= strtol(cp
, &cp
, 10);
1342 #ifdef DL_HP_PPA_ACK_OBS
1344 * Under HP-UX 10 and HP-UX 11, we can ask for the ppa
1349 * Determine ppa number that specifies ifname.
1351 * If the "dl_hp_ppa_info_t" doesn't have a "dl_module_id_1" member,
1352 * the code that's used here is the old code for HP-UX 10.x.
1354 * However, HP-UX 10.20, at least, appears to have such a member
1355 * in its "dl_hp_ppa_info_t" structure, so the new code is used.
1356 * The new code didn't work on an old 10.20 system on which Rick
1357 * Jones of HP tried it, but with later patches installed, it
1358 * worked - it appears that the older system had those members but
1359 * didn't put anything in them, so, if the search by name fails, we
1360 * do the old search.
1362 * Rick suggests that making sure your system is "up on the latest
1363 * lancommon/DLPI/driver patches" is probably a good idea; it'd fix
1364 * that problem, as well as allowing libpcap to see packets sent
1365 * from the system on which the libpcap application is being run.
1366 * (On 10.20, in addition to getting the latest patches, you need
1367 * to turn the kernel "lanc_outbound_promisc_flag" flag on with ADB;
1368 * a posting to "comp.sys.hp.hpux" at
1370 * http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=558092266
1372 * says that, to see the machine's outgoing traffic, you'd need to
1373 * apply the right patches to your system, and also set that variable
1376 echo 'lanc_outbound_promisc_flag/W1' | /usr/bin/adb -w /stand/vmunix /dev/kmem
1378 * which could be put in, for example, "/sbin/init.d/lan".
1380 * Setting the variable is not necessary on HP-UX 11.x.
1383 get_dlpi_ppa(register int fd
, register const char *device
, register int unit
,
1384 register char *ebuf
)
1386 register dl_hp_ppa_ack_t
*ap
;
1387 register dl_hp_ppa_info_t
*ipstart
, *ip
;
1390 register u_long majdev
;
1391 struct stat statbuf
;
1392 dl_hp_ppa_req_t req
;
1395 dl_hp_ppa_ack_t
*dlp
;
1400 memset((char *)&req
, 0, sizeof(req
));
1401 req
.dl_primitive
= DL_HP_PPA_REQ
;
1403 memset((char *)buf
, 0, sizeof(buf
));
1404 if (send_request(fd
, (char *)&req
, sizeof(req
), "hpppa", ebuf
) < 0)
1407 ctl
.maxlen
= DL_HP_PPA_ACK_SIZE
;
1409 ctl
.buf
= (char *)buf
;
1413 * DLPI may return a big chunk of data for a DL_HP_PPA_REQ. The normal
1414 * recv_ack will fail because it set the maxlen to MAXDLBUF (8192)
1415 * which is NOT big enough for a DL_HP_PPA_REQ.
1417 * This causes libpcap applications to fail on a system with HP-APA
1420 * To figure out how big the returned data is, we first call getmsg
1421 * to get the small head and peek at the head to get the actual data
1422 * length, and then issue another getmsg to get the actual PPA data.
1424 /* get the head first */
1425 if (getmsg(fd
, &ctl
, (struct strbuf
*)NULL
, &flags
) < 0) {
1426 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1427 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno
));
1431 dlp
= (dl_hp_ppa_ack_t
*)ctl
.buf
;
1432 if (dlp
->dl_primitive
!= DL_HP_PPA_ACK
) {
1433 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1434 "get_dlpi_ppa: hpppa unexpected primitive ack 0x%x",
1435 (bpf_u_int32
)dlp
->dl_primitive
);
1439 if (ctl
.len
< DL_HP_PPA_ACK_SIZE
) {
1440 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1441 "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1442 ctl
.len
, (unsigned long)DL_HP_PPA_ACK_SIZE
);
1446 /* allocate buffer */
1447 if ((ppa_data_buf
= (char *)malloc(dlp
->dl_length
)) == NULL
) {
1448 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1449 "get_dlpi_ppa: hpppa malloc: %s", pcap_strerror(errno
));
1452 ctl
.maxlen
= dlp
->dl_length
;
1454 ctl
.buf
= (char *)ppa_data_buf
;
1456 if (getmsg(fd
, &ctl
, (struct strbuf
*)NULL
, &flags
) < 0) {
1457 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1458 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno
));
1462 if (ctl
.len
< dlp
->dl_length
) {
1463 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1464 "get_dlpi_ppa: hpppa ack too small (%d < %d)",
1465 ctl
.len
, dlp
->dl_length
);
1470 ap
= (dl_hp_ppa_ack_t
*)buf
;
1471 ipstart
= (dl_hp_ppa_info_t
*)ppa_data_buf
;
1474 #ifdef HAVE_HP_PPA_INFO_T_DL_MODULE_ID_1
1476 * The "dl_hp_ppa_info_t" structure has a "dl_module_id_1"
1477 * member that should, in theory, contain the part of the
1478 * name for the device that comes before the unit number,
1479 * and should also have a "dl_module_id_2" member that may
1480 * contain an alternate name (e.g., I think Ethernet devices
1481 * have both "lan", for "lanN", and "snap", for "snapN", with
1482 * the former being for Ethernet packets and the latter being
1483 * for 802.3/802.2 packets).
1485 * Search for the device that has the specified name and
1488 for (i
= 0; i
< ap
->dl_count
; i
++) {
1489 if ((strcmp((const char *)ip
->dl_module_id_1
, device
) == 0 ||
1490 strcmp((const char *)ip
->dl_module_id_2
, device
) == 0) &&
1491 ip
->dl_instance_num
== unit
)
1494 ip
= (dl_hp_ppa_info_t
*)((u_char
*)ipstart
+ ip
->dl_next_offset
);
1498 * We don't have that member, so the search is impossible; make it
1499 * look as if the search failed.
1504 if (i
== ap
->dl_count
) {
1506 * Well, we didn't, or can't, find the device by name.
1508 * HP-UX 10.20, whilst it has "dl_module_id_1" and
1509 * "dl_module_id_2" fields in the "dl_hp_ppa_info_t",
1510 * doesn't seem to fill them in unless the system is
1511 * at a reasonably up-to-date patch level.
1513 * Older HP-UX 10.x systems might not have those fields
1516 * Therefore, we'll search for the entry with the major
1517 * device number of a device with the name "/dev/<dev><unit>",
1518 * if such a device exists, as the old code did.
1520 snprintf(dname
, sizeof(dname
), "/dev/%s%d", device
, unit
);
1521 if (stat(dname
, &statbuf
) < 0) {
1522 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "stat: %s: %s",
1523 dname
, pcap_strerror(errno
));
1526 majdev
= major(statbuf
.st_rdev
);
1530 for (i
= 0; i
< ap
->dl_count
; i
++) {
1531 if (ip
->dl_mjr_num
== majdev
&&
1532 ip
->dl_instance_num
== unit
)
1535 ip
= (dl_hp_ppa_info_t
*)((u_char
*)ipstart
+ ip
->dl_next_offset
);
1538 if (i
== ap
->dl_count
) {
1539 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1540 "can't find /dev/dlpi PPA for %s%d", device
, unit
);
1543 if (ip
->dl_hdw_state
== HDW_DEAD
) {
1544 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1545 "%s%d: hardware state: DOWN\n", device
, unit
);
1557 * Under HP-UX 9, there is no good way to determine the ppa.
1558 * So punt and read it from /dev/kmem.
1560 static struct nlist nl
[] = {
1566 static char path_vmunix
[] = "/hp-ux";
1568 /* Determine ppa number that specifies ifname */
1570 get_dlpi_ppa(register int fd
, register const char *ifname
, register int unit
,
1571 register char *ebuf
)
1573 register const char *cp
;
1577 char if_name
[sizeof(ifnet
.if_name
) + 1];
1579 cp
= strrchr(ifname
, '/');
1582 if (nlist(path_vmunix
, &nl
) < 0) {
1583 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "nlist %s failed",
1587 if (nl
[NL_IFNET
].n_value
== 0) {
1588 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1589 "could't find %s kernel symbol",
1590 nl
[NL_IFNET
].n_name
);
1593 kd
= open("/dev/kmem", O_RDONLY
);
1595 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "kmem open: %s",
1596 pcap_strerror(errno
));
1599 if (dlpi_kread(kd
, nl
[NL_IFNET
].n_value
,
1600 &addr
, sizeof(addr
), ebuf
) < 0) {
1604 for (; addr
!= NULL
; addr
= ifnet
.if_next
) {
1605 if (dlpi_kread(kd
, (off_t
)addr
,
1606 &ifnet
, sizeof(ifnet
), ebuf
) < 0 ||
1607 dlpi_kread(kd
, (off_t
)ifnet
.if_name
,
1608 if_name
, sizeof(ifnet
.if_name
), ebuf
) < 0) {
1612 if_name
[sizeof(ifnet
.if_name
)] = '\0';
1613 if (strcmp(if_name
, ifname
) == 0 && ifnet
.if_unit
== unit
)
1614 return (ifnet
.if_index
);
1617 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "Can't find %s", ifname
);
1622 dlpi_kread(register int fd
, register off_t addr
,
1623 register void *buf
, register u_int len
, register char *ebuf
)
1627 if (lseek(fd
, addr
, SEEK_SET
) < 0) {
1628 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "lseek: %s",
1629 pcap_strerror(errno
));
1632 cc
= read(fd
, buf
, len
);
1634 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "read: %s",
1635 pcap_strerror(errno
));
1637 } else if (cc
!= len
) {
1638 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "short read (%d != %d)", cc
,