2 * Copyright (c) 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 * This code contributed by Atanu Ghosh (atanu@cs.ucl.ac.uk),
22 * University College London, and subsequently modified by
23 * Guy Harris (guy@alum.mit.edu), Mark Pizzolato
24 * <List-tcpdump-workers@subscriptions.pizzolato.net>,
25 * Mark C. Brown (mbrown@hp.com), and Sagun Shakya <Sagun.Shakya@Sun.COM>.
29 * Packet capture routine for DLPI under SunOS 5, HP-UX 9/10/11, and AIX.
33 * - The DLIOCRAW ioctl() is specific to SunOS.
35 * - There is a bug in bufmod(7) such that setting the snapshot
36 * length results in data being left of the front of the packet.
38 * - It might be desirable to use pfmod(7) to filter packets in the
39 * kernel when possible.
41 * - An older version of the HP-UX DLPI Programmer's Guide, which
42 * I think was advertised as the 10.20 version, used to be available
45 * http://docs.hp.com/hpux/onlinedocs/B2355-90093/B2355-90093.html
47 * but is no longer available; it can still be found at
49 * http://h21007.www2.hp.com/dspp/files/unprotected/Drivers/Docs/Refs/B2355-90093.pdf
53 * - The HP-UX 10.x, 11.0, and 11i v1.6 version of the HP-UX DLPI
54 * Programmer's Guide, which I think was once advertised as the
55 * 11.00 version is available at
57 * http://docs.hp.com/en/B2355-90139/index.html
59 * - The HP-UX 11i v2 version of the HP-UX DLPI Programmer's Guide
62 * http://docs.hp.com/en/B2355-90871/index.html
64 * - All of the HP documents describe raw-mode services, which are
65 * what we use if DL_HP_RAWDLS is defined. XXX - we use __hpux
66 * in some places to test for HP-UX, but use DL_HP_RAWDLS in
67 * other places; do we support any versions of HP-UX without
75 #include <sys/types.h>
77 #ifdef HAVE_SYS_BUFMOD_H
78 #include <sys/bufmod.h>
81 #ifdef HAVE_SYS_DLPI_EXT_H
82 #include <sys/dlpi_ext.h>
85 #include <sys/socket.h>
90 #include <sys/stream.h>
91 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
92 #include <sys/systeminfo.h>
115 #define INT_MAX 2147483647
118 #include "pcap-int.h"
119 #include "dlpisubs.h"
121 #ifdef HAVE_OS_PROTO_H
122 #include "os-proto.h"
125 #ifndef PCAP_DEV_PREFIX
127 #define PCAP_DEV_PREFIX "/dev/dlpi"
129 #define PCAP_DEV_PREFIX "/dev"
133 #define MAXDLBUF 8192
136 static char *split_dname(char *, int *, char *);
137 static int dl_doattach(int, int, char *);
139 static int dl_dohpuxbind(int, char *);
141 static int dlpromiscon(pcap_t *, bpf_u_int32);
142 static int dlbindreq(int, bpf_u_int32, char *);
143 static int dlbindack(int, char *, char *, int *);
144 static int dlokack(int, const char *, char *, char *);
145 static int dlinforeq(int, char *);
146 static int dlinfoack(int, char *, char *);
148 #ifdef HAVE_DLPI_PASSIVE
149 static void dlpassive(int, char *);
153 static int dlrawdatareq(int, const u_char *, int);
155 static int recv_ack(int, int, const char *, char *, char *, int *);
156 static char *dlstrerror(char *, size_t, bpf_u_int32);
157 static char *dlprim(char *, size_t, bpf_u_int32);
158 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
159 #define GET_RELEASE_BUFSIZE 32
160 static void get_release(char *, size_t, bpf_u_int32 *, bpf_u_int32 *,
163 static int send_request(int, char *, int, char *, char *);
165 static int dlpi_kread(int, off_t, void *, u_int, char *);
168 static int get_dlpi_ppa(int, const char *, int, char *);
172 * Cast a buffer to "union DL_primitives" without provoking warnings
175 #define MAKE_DL_PRIMITIVES(ptr) ((union DL_primitives *)(void *)(ptr))
178 pcap_read_dlpi(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
183 bpf_u_int32 ctlbuf[MAXDLBUF];
184 struct strbuf ctl = {
194 data.buf = (char *)p->buffer + p->offset;
195 data.maxlen = p->bufsize;
199 * Has "pcap_breakloop()" been called?
203 * Yes - clear the flag that indicates
204 * that it has, and return -2 to
205 * indicate that we were told to
206 * break out of the loop.
212 * XXX - check for the DLPI primitive, which
213 * would be DL_HP_RAWDATA_IND on HP-UX
214 * if we're in raw mode?
216 ctl.buf = (char *)ctlbuf;
217 ctl.maxlen = MAXDLBUF;
219 if (getmsg(p->fd, &ctl, &data, &flags) < 0) {
220 /* Don't choke when we get ptraced */
230 strlcpy(p->errbuf, pcap_strerror(errno),
236 bp = (u_char *)p->buffer + p->offset;
240 return (pcap_process_pkts(p, callback, user, cnt, bp, cc));
244 pcap_inject_dlpi(pcap_t *p, const void *buf, size_t size)
247 struct pcap_dlpi *pd = p->priv;
251 #if defined(DLIOCRAW)
252 ret = write(p->fd, buf, size);
254 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
255 pcap_strerror(errno));
258 #elif defined(DL_HP_RAWDLS)
259 if (pd->send_fd < 0) {
260 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
261 "send: Output FD couldn't be opened");
264 ret = dlrawdatareq(pd->send_fd, buf, size);
266 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
267 pcap_strerror(errno));
271 * putmsg() returns either 0 or -1; it doesn't indicate how
272 * many bytes were written (presumably they were all written
273 * or none of them were written). OpenBSD's pcap_inject()
274 * returns the number of bytes written, so, for API compatibility,
275 * we return the number of bytes we were told to write.
278 #else /* no raw mode */
280 * XXX - this is a pain, because you might have to extract
281 * the address from the packet and use it in a DL_UNITDATA_REQ
282 * request. That would be dependent on the link-layer type.
284 * I also don't know what SAP you'd have to bind the descriptor
285 * to, or whether you'd need separate "receive" and "send" FDs,
286 * nor do I know whether you'd need different bindings for
287 * D/I/X Ethernet and 802.3, or for {FDDI,Token Ring} plus
288 * 802.2 and {FDDI,Token Ring} plus 802.2 plus SNAP.
290 * So, for now, we just return a "you can't send" indication,
291 * and leave it up to somebody with a DLPI-based system lacking
292 * both DLIOCRAW and DL_HP_RAWDLS to supply code to implement
293 * packet transmission on that system. If they do, they should
294 * send it to us - but should not send us code that assumes
295 * Ethernet; if the code doesn't work on non-Ethernet interfaces,
296 * it should check "p->linktype" and reject the send request if
297 * it's anything other than DLT_EN10MB.
299 strlcpy(p->errbuf, "send: Not supported on this version of this OS",
302 #endif /* raw mode */
307 #define DL_IPATM 0x12 /* ATM Classical IP interface */
315 #define A_GET_UNITS (('A'<<8)|118)
316 #endif /* A_GET_UNITS */
317 #ifndef A_PROMISCON_REQ
318 #define A_PROMISCON_REQ (('A'<<8)|121)
319 #endif /* A_PROMISCON_REQ */
320 #endif /* HAVE_SOLARIS */
323 pcap_cleanup_dlpi(pcap_t *p)
326 struct pcap_dlpi *pd = p->priv;
328 if (pd->send_fd >= 0) {
333 pcap_cleanup_live_common(p);
337 open_dlpi_device(const char *name, int *ppa, char *errbuf)
343 #ifndef HAVE_DEV_DLPI
349 ** Remove any "/dev/" on the front of the device.
351 cp = strrchr(name, '/');
353 strlcpy(dname, name, sizeof(dname));
355 strlcpy(dname, cp + 1, sizeof(dname));
358 * Split the device name into a device type name and a unit number;
359 * chop off the unit number, so "dname" is just a device type name.
361 cp = split_dname(dname, ppa, errbuf);
363 return (PCAP_ERROR_NO_SUCH_DEVICE);
367 * Use "/dev/dlpi" as the device.
369 * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that
370 * the "dl_mjr_num" field is for the "major number of interface
371 * driver"; that's the major of "/dev/dlpi" on the system on
372 * which I tried this, but there may be DLPI devices that
373 * use a different driver, in which case we may need to
374 * search "/dev" for the appropriate device with that major
375 * device number, rather than hardwiring "/dev/dlpi".
378 if ((fd = open(cp, O_RDWR)) < 0) {
379 if (errno == EPERM || errno == EACCES)
380 status = PCAP_ERROR_PERM_DENIED;
383 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
384 "%s: %s", cp, pcap_strerror(errno));
389 * Get a table of all PPAs for that device, and search that
390 * table for the specified device type name and unit number.
392 *ppa = get_dlpi_ppa(fd, dname, *ppa, errbuf);
399 * If the device name begins with "/", assume it begins with
400 * the pathname of the directory containing the device to open;
401 * otherwise, concatenate the device directory name and the
405 strlcpy(dname, name, sizeof(dname));
407 pcap_snprintf(dname, sizeof(dname), "%s/%s", PCAP_DEV_PREFIX,
411 * Get the unit number, and a pointer to the end of the device
414 cp = split_dname(dname, ppa, errbuf);
416 return (PCAP_ERROR_NO_SUCH_DEVICE);
419 * Make a copy of the device pathname, and then remove the unit
420 * number from the device pathname.
422 strlcpy(dname2, dname, sizeof(dname));
425 /* Try device without unit number */
426 if ((fd = open(dname, O_RDWR)) < 0) {
427 if (errno != ENOENT) {
428 if (errno == EPERM || errno == EACCES)
429 status = PCAP_ERROR_PERM_DENIED;
432 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", dname,
433 pcap_strerror(errno));
437 /* Try again with unit number */
438 if ((fd = open(dname2, O_RDWR)) < 0) {
439 if (errno == ENOENT) {
440 status = PCAP_ERROR_NO_SUCH_DEVICE;
443 * We provide an error message even
444 * for this error, for diagnostic
445 * purposes (so that, for example,
446 * the app can show the message if the
449 * In it, we just report "No DLPI device
450 * found" with the device name, so people
451 * don't get confused and think, for example,
452 * that if they can't capture on "lo0"
453 * on Solaris prior to Solaris 11 the fix
454 * is to change libpcap (or the application
455 * that uses it) to look for something other
456 * than "/dev/lo0", as the fix is to use
457 * Solaris 11 or some operating system
458 * other than Solaris - you just *can't*
459 * capture on a loopback interface
460 * on Solaris prior to Solaris 11, the lack
461 * of a DLPI device for the loopback
462 * interface is just a symptom of that
465 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
466 "%s: No DLPI device found", name);
468 if (errno == EPERM || errno == EACCES)
469 status = PCAP_ERROR_PERM_DENIED;
472 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
473 dname2, pcap_strerror(errno));
477 /* XXX Assume unit zero */
485 pcap_activate_dlpi(pcap_t *p)
488 struct pcap_dlpi *pd = p->priv;
496 register dl_info_ack_t *infop;
497 #ifdef HAVE_SYS_BUFMOD_H
500 char release[GET_RELEASE_BUFSIZE];
501 bpf_u_int32 osmajor, osminor, osmicro;
504 bpf_u_int32 buf[MAXDLBUF];
506 p->fd = open_dlpi_device(p->opt.device, &ppa, p->errbuf);
514 * XXX - HP-UX 10.20 and 11.xx don't appear to support sending and
515 * receiving packets on the same descriptor - you need separate
516 * descriptors for sending and receiving, bound to different SAPs.
518 * If the open fails, we just leave -1 in "pd->send_fd" and reject
519 * attempts to send packets, just as if, in pcap-bpf.c, we fail
520 * to open the BPF device for reading and writing, we just try
521 * to open it for reading only and, if that succeeds, just let
522 * the send attempts fail.
524 pd->send_fd = open("/dev/dlpi", O_RDWR);
528 ** Attach if "style 2" provider
530 if (dlinforeq(p->fd, p->errbuf) < 0 ||
531 dlinfoack(p->fd, (char *)buf, p->errbuf) < 0) {
535 infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack;
537 if (infop->dl_mac_type == DL_IPATM)
540 if (infop->dl_provider_style == DL_STYLE2) {
541 retv = dl_doattach(p->fd, ppa, p->errbuf);
547 if (pd->send_fd >= 0) {
548 retv = dl_doattach(pd->send_fd, ppa, p->errbuf);
559 * This device exists, but we don't support monitor mode
560 * any platforms that support DLPI.
562 status = PCAP_ERROR_RFMON_NOTSUP;
566 #ifdef HAVE_DLPI_PASSIVE
568 * Enable Passive mode to be able to capture on aggregated link.
569 * Not supported in all Solaris versions.
571 dlpassive(p->fd, p->errbuf);
574 ** Bind (defer if using HP-UX 9 or HP-UX 10.20 or later, totally
575 ** skip if using SINIX)
577 #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20_OR_LATER) && !defined(sinix)
581 ** According to IBM's AIX Support Line, the dl_sap value
582 ** should not be less than 0x600 (1536) for standard Ethernet.
583 ** However, we seem to get DL_BADADDR - "DLSAP addr in improper
584 ** format or invalid" - errors if we use 1537 on the "tr0"
585 ** device, which, given that its name starts with "tr" and that
586 ** it's IBM, probably means a Token Ring device. (Perhaps we
587 ** need to use 1537 on "/dev/dlpi/en" because that device is for
588 ** D/I/X Ethernet, the "SAP" is actually an Ethernet type, and
589 ** it rejects invalid Ethernet types.)
591 ** So if 1537 fails, we try 2, as Hyung Sik Yoon of IBM Korea
592 ** says that works on Token Ring (he says that 0 does *not*
593 ** work; perhaps that's considered an invalid LLC SAP value - I
594 ** assume the SAP value in a DLPI bind is an LLC SAP for network
595 ** types that use 802.2 LLC).
597 if ((dlbindreq(p->fd, 1537, p->errbuf) < 0 &&
598 dlbindreq(p->fd, 2, p->errbuf) < 0) ||
599 dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0) {
603 #elif defined(DL_HP_RAWDLS)
605 ** HP-UX 10.0x and 10.1x.
607 if (dl_dohpuxbind(p->fd, p->errbuf) < 0) {
611 if (pd->send_fd >= 0) {
613 ** XXX - if this fails, just close send_fd and
614 ** set it to -1, so that you can't send but can
617 if (dl_dohpuxbind(pd->send_fd, p->errbuf) < 0) {
622 #else /* neither AIX nor HP-UX */
624 ** Not Sinix, and neither AIX nor HP-UX - Solaris, and any other
627 if (dlbindreq(p->fd, 0, p->errbuf) < 0 ||
628 dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0) {
632 #endif /* AIX vs. HP-UX vs. other */
633 #endif /* !HP-UX 9 and !HP-UX 10.20 or later and !SINIX */
638 ** Have to turn on some special ATM promiscuous mode
640 ** Do *NOT* turn regular promiscuous mode on; it doesn't
641 ** help, and may break things.
643 if (strioctl(p->fd, A_PROMISCON_REQ, 0, NULL) < 0) {
645 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
646 "A_PROMISCON_REQ: %s", pcap_strerror(errno));
651 if (p->opt.promisc) {
653 ** Enable promiscuous (not necessary on send FD)
655 retv = dlpromiscon(p, DL_PROMISC_PHYS);
657 if (retv == PCAP_ERROR_PERM_DENIED)
658 status = PCAP_ERROR_PROMISC_PERM_DENIED;
665 ** Try to enable multicast (you would have thought
666 ** promiscuous would be sufficient). (Skip if using
667 ** HP-UX or SINIX) (Not necessary on send FD)
669 #if !defined(__hpux) && !defined(sinix)
670 retv = dlpromiscon(p, DL_PROMISC_MULTI);
672 status = PCAP_WARNING;
676 ** Try to enable SAP promiscuity (when not in promiscuous mode
677 ** when using HP-UX, when not doing SunATM on Solaris, and never
678 ** under SINIX) (Not necessary on send FD)
682 /* HP-UX - only do this when not in promiscuous mode */
683 if (!p->opt.promisc) {
684 #elif defined(HAVE_SOLARIS)
685 /* Solaris - don't do this on SunATM devices */
688 /* Everything else (except for SINIX) - always do this */
691 retv = dlpromiscon(p, DL_PROMISC_SAP);
693 if (p->opt.promisc) {
695 * Not fatal, since the DL_PROMISC_PHYS mode
698 * Report it as a warning, however.
700 status = PCAP_WARNING;
713 ** HP-UX 9, and HP-UX 10.20 or later, must bind after setting
714 ** promiscuous options.
716 #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20_OR_LATER)
717 if (dl_dohpuxbind(p->fd, p->errbuf) < 0) {
722 ** We don't set promiscuous mode on the send FD, but we'll defer
723 ** binding it anyway, just to keep the HP-UX 9/10.20 or later
726 if (pd->send_fd >= 0) {
728 ** XXX - if this fails, just close send_fd and
729 ** set it to -1, so that you can't send but can
732 if (dl_dohpuxbind(pd->send_fd, p->errbuf) < 0) {
740 ** Determine link type
741 ** XXX - get SAP length and address length as well, for use
742 ** when sending packets.
744 if (dlinforeq(p->fd, p->errbuf) < 0 ||
745 dlinfoack(p->fd, (char *)buf, p->errbuf) < 0) {
750 infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack;
751 if (pcap_process_mactype(p, infop->dl_mac_type) != 0) {
758 ** This is a non standard SunOS hack to get the full raw link-layer
761 if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) {
763 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "DLIOCRAW: %s",
764 pcap_strerror(errno));
769 #ifdef HAVE_SYS_BUFMOD_H
773 ** There is a bug in bufmod(7). When dealing with messages of
774 ** less than snaplen size it strips data from the beginning not
777 ** This bug is fixed in 5.3.2. Also, there is a patch available.
778 ** Ask for bugid 1149065.
781 get_release(release, sizeof (release), &osmajor, &osminor, &osmicro);
782 if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) &&
783 getenv("BUFMOD_FIXED") == NULL) {
784 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
785 "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.",
788 status = PCAP_WARNING;
792 /* Push and configure bufmod. */
793 if (pcap_conf_bufmod(p, ss) != 0) {
800 ** As the last operation flush the read side.
802 if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
804 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s",
805 pcap_strerror(errno));
809 /* Allocate data buffer. */
810 if (pcap_alloc_databuf(p) != 0) {
818 * "p->fd" is an FD for a STREAMS device, so "select()" and
819 * "poll()" should work on it.
821 p->selectable_fd = p->fd;
823 p->read_op = pcap_read_dlpi;
824 p->inject_op = pcap_inject_dlpi;
825 p->setfilter_op = install_bpf_program; /* no kernel filtering */
826 p->setdirection_op = NULL; /* Not implemented.*/
827 p->set_datalink_op = NULL; /* can't change data link type */
828 p->getnonblock_op = pcap_getnonblock_fd;
829 p->setnonblock_op = pcap_setnonblock_fd;
830 p->stats_op = pcap_stats_dlpi;
831 p->cleanup_op = pcap_cleanup_dlpi;
835 pcap_cleanup_dlpi(p);
840 * Split a device name into a device type name and a unit number;
841 * return the a pointer to the beginning of the unit number, which
842 * is the end of the device type name, and set "*unitp" to the unit
845 * Returns NULL on error, and fills "ebuf" with an error message.
848 split_dname(char *device, int *unitp, char *ebuf)
855 * Look for a number at the end of the device name string.
857 cp = device + strlen(device) - 1;
858 if (*cp < '0' || *cp > '9') {
859 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s missing unit number",
864 /* Digits at end of string are unit number */
865 while (cp-1 >= device && *(cp-1) >= '0' && *(cp-1) <= '9')
869 unit = strtol(cp, &eos, 10);
871 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s bad unit number", device);
874 if (errno == ERANGE || unit > INT_MAX) {
875 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number too large",
880 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number is negative",
889 dl_doattach(int fd, int ppa, char *ebuf)
892 bpf_u_int32 buf[MAXDLBUF];
895 req.dl_primitive = DL_ATTACH_REQ;
897 if (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf) < 0)
900 err = dlokack(fd, "attach", (char *)buf, ebuf);
908 dl_dohpuxbind(int fd, char *ebuf)
912 bpf_u_int32 buf[MAXDLBUF];
915 * XXX - we start at 22 because we used to use only 22, but
916 * that was just because that was the value used in some
917 * sample code from HP. With what value *should* we start?
918 * Does it matter, given that we're enabling SAP promiscuity
923 if (dlbindreq(fd, hpsap, ebuf) < 0)
925 if (dlbindack(fd, (char *)buf, ebuf, &uerror) >= 0)
928 * For any error other than a UNIX EBUSY, give up.
930 if (uerror != EBUSY) {
932 * dlbindack() has already filled in ebuf for
939 * For EBUSY, try the next SAP value; that means that
940 * somebody else is using that SAP. Clear ebuf so
941 * that application doesn't report the "Device busy"
942 * error as a warning.
948 "All SAPs from 22 through 100 are in use",
957 #define STRINGIFY(n) #n
960 dlpromiscon(pcap_t *p, bpf_u_int32 level)
962 dl_promiscon_req_t req;
963 bpf_u_int32 buf[MAXDLBUF];
966 req.dl_primitive = DL_PROMISCON_REQ;
967 req.dl_level = level;
968 if (send_request(p->fd, (char *)&req, sizeof(req), "promiscon",
971 err = dlokack(p->fd, "promiscon" STRINGIFY(level), (char *)buf,
979 * Not all interfaces are DLPI interfaces, and thus not all interfaces
980 * can be opened with DLPI (for example, the loopback interface is not
981 * a DLPI interface on Solaris prior to Solaris 11), so try to open
982 * the specified interface; return 0 if we fail with PCAP_ERROR_NO_SUCH_DEVICE
986 is_dlpi_interface(const char *name)
990 char errbuf[PCAP_ERRBUF_SIZE];
992 fd = open_dlpi_device(name, &ppa, errbuf);
995 * Error - was it PCAP_ERROR_NO_SUCH_DEVICE?
997 if (fd == PCAP_ERROR_NO_SUCH_DEVICE) {
999 * Yes, so we can't open this because it's
1000 * not a DLPI interface.
1005 * No, so, in the case where there's a single DLPI
1006 * device for all interfaces of this type ("style
1007 * 2" providers?), we don't know whether it's a DLPI
1008 * interface or not, as we didn't try an attach.
1009 * Say it is a DLPI device, so that the user can at
1010 * least try to open it and report the error (which
1011 * is probably "you don't have permission to open that
1012 * DLPI device"; reporting those interfaces means
1013 * users will ask "why am I getting a permissions error
1014 * when I try to capture" rather than "why am I not
1015 * seeing any interfaces", making the underlying problem
1029 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
1035 char pad[516]; /* XXX - must be at least 513; is 516
1043 * Get the list of regular interfaces first.
1045 if (pcap_findalldevs_interfaces(alldevsp, errbuf, is_dlpi_interface) == -1)
1046 return (-1); /* failure */
1050 * We may have to do special magic to get ATM devices.
1052 if ((fd = open("/dev/ba", O_RDWR)) < 0) {
1054 * We couldn't open the "ba" device.
1055 * For now, just give up; perhaps we should
1056 * return an error if the problem is neither
1057 * a "that device doesn't exist" error (ENOENT,
1058 * ENXIO, etc.) or a "you're not allowed to do
1059 * that" error (EPERM, EACCES).
1064 if (strioctl(fd, A_GET_UNITS, sizeof(buf), (char *)&buf) < 0) {
1065 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "A_GET_UNITS: %s",
1066 pcap_strerror(errno));
1069 for (i = 0; i < buf.nunits; i++) {
1070 pcap_snprintf(baname, sizeof baname, "ba%u", i);
1071 if (pcap_add_if(alldevsp, baname, 0, NULL, errbuf) < 0)
1080 send_request(int fd, char *ptr, int len, char *what, char *ebuf)
1090 if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) {
1091 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1092 "send_request: putmsg \"%s\": %s",
1093 what, pcap_strerror(errno));
1100 recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf, int *uerror)
1102 union DL_primitives *dlp;
1105 char errmsgbuf[PCAP_ERRBUF_SIZE];
1109 * Clear out "*uerror", so it's only set for DL_ERROR_ACK/DL_SYSERR,
1110 * making that the only place where EBUSY is treated specially.
1115 ctl.maxlen = MAXDLBUF;
1120 if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) {
1121 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s getmsg: %s",
1122 what, pcap_strerror(errno));
1123 return (PCAP_ERROR);
1126 dlp = MAKE_DL_PRIMITIVES(ctl.buf);
1127 switch (dlp->dl_primitive) {
1132 #ifdef DL_HP_PPA_ACK
1139 switch (dlp->error_ack.dl_errno) {
1143 *uerror = dlp->error_ack.dl_unix_errno;
1144 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1145 "recv_ack: %s: UNIX error - %s",
1146 what, pcap_strerror(dlp->error_ack.dl_unix_errno));
1147 if (dlp->error_ack.dl_unix_errno == EPERM ||
1148 dlp->error_ack.dl_unix_errno == EACCES)
1149 return (PCAP_ERROR_PERM_DENIED);
1153 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1154 "recv_ack: %s: %s", what,
1155 dlstrerror(errmsgbuf, sizeof (errmsgbuf), dlp->error_ack.dl_errno));
1156 if (dlp->error_ack.dl_errno == DL_BADPPA)
1157 return (PCAP_ERROR_NO_SUCH_DEVICE);
1158 else if (dlp->error_ack.dl_errno == DL_ACCESS)
1159 return (PCAP_ERROR_PERM_DENIED);
1162 return (PCAP_ERROR);
1165 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1166 "recv_ack: %s: Unexpected primitive ack %s",
1167 what, dlprim(dlprimbuf, sizeof (dlprimbuf), dlp->dl_primitive));
1168 return (PCAP_ERROR);
1171 if (ctl.len < size) {
1172 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1173 "recv_ack: %s: Ack too small (%d < %d)",
1174 what, ctl.len, size);
1175 return (PCAP_ERROR);
1181 dlstrerror(char *errbuf, size_t errbufsize, bpf_u_int32 dl_errno)
1186 return ("Improper permissions for request");
1189 return ("DLSAP addr in improper format or invalid");
1192 return ("Seq number not from outstand DL_CONN_IND");
1195 return ("User data exceeded provider limit");
1198 #ifdef HAVE_DEV_DLPI
1200 * With a single "/dev/dlpi" device used for all
1201 * DLPI providers, PPAs have nothing to do with
1204 return ("Specified PPA was invalid");
1207 * We have separate devices for separate devices;
1208 * the PPA is just the unit number.
1210 return ("Specified PPA (device unit) was invalid");
1214 return ("Primitive received not known by provider");
1216 case DL_BADQOSPARAM:
1217 return ("QOS parameters contained invalid values");
1220 return ("QOS structure type is unknown/unsupported");
1223 return ("Bad LSAP selector");
1226 return ("Token used not an active stream");
1229 return ("Attempted second bind with dl_max_conind");
1232 return ("Physical link initialization failed");
1235 return ("Provider couldn't allocate alternate address");
1238 return ("Physical link not initialized");
1241 return ("Primitive issued in improper state");
1244 return ("UNIX system error occurred");
1246 case DL_UNSUPPORTED:
1247 return ("Requested service not supplied by provider");
1249 case DL_UNDELIVERABLE:
1250 return ("Previous data unit could not be delivered");
1252 case DL_NOTSUPPORTED:
1253 return ("Primitive is known but not supported");
1256 return ("Limit exceeded");
1259 return ("Promiscuous mode not enabled");
1262 return ("Other streams for PPA in post-attached");
1265 return ("Automatic handling XID&TEST not supported");
1268 return ("Automatic handling of XID not supported");
1271 return ("Automatic handling of TEST not supported");
1274 return ("Automatic handling of XID response");
1277 return ("Automatic handling of TEST response");
1280 return ("Pending outstanding connect indications");
1283 pcap_snprintf(errbuf, errbufsize, "Error %02x", dl_errno);
1289 dlprim(char *primbuf, size_t primbufsize, bpf_u_int32 prim)
1294 return ("DL_INFO_REQ");
1297 return ("DL_INFO_ACK");
1300 return ("DL_ATTACH_REQ");
1303 return ("DL_DETACH_REQ");
1306 return ("DL_BIND_REQ");
1309 return ("DL_BIND_ACK");
1312 return ("DL_UNBIND_REQ");
1315 return ("DL_OK_ACK");
1318 return ("DL_ERROR_ACK");
1320 case DL_SUBS_BIND_REQ:
1321 return ("DL_SUBS_BIND_REQ");
1323 case DL_SUBS_BIND_ACK:
1324 return ("DL_SUBS_BIND_ACK");
1326 case DL_UNITDATA_REQ:
1327 return ("DL_UNITDATA_REQ");
1329 case DL_UNITDATA_IND:
1330 return ("DL_UNITDATA_IND");
1332 case DL_UDERROR_IND:
1333 return ("DL_UDERROR_IND");
1336 return ("DL_UDQOS_REQ");
1338 case DL_CONNECT_REQ:
1339 return ("DL_CONNECT_REQ");
1341 case DL_CONNECT_IND:
1342 return ("DL_CONNECT_IND");
1344 case DL_CONNECT_RES:
1345 return ("DL_CONNECT_RES");
1347 case DL_CONNECT_CON:
1348 return ("DL_CONNECT_CON");
1351 return ("DL_TOKEN_REQ");
1354 return ("DL_TOKEN_ACK");
1356 case DL_DISCONNECT_REQ:
1357 return ("DL_DISCONNECT_REQ");
1359 case DL_DISCONNECT_IND:
1360 return ("DL_DISCONNECT_IND");
1363 return ("DL_RESET_REQ");
1366 return ("DL_RESET_IND");
1369 return ("DL_RESET_RES");
1372 return ("DL_RESET_CON");
1375 pcap_snprintf(primbuf, primbufsize, "unknown primitive 0x%x",
1382 dlbindreq(int fd, bpf_u_int32 sap, char *ebuf)
1387 memset((char *)&req, 0, sizeof(req));
1388 req.dl_primitive = DL_BIND_REQ;
1389 /* XXX - what if neither of these are defined? */
1390 #if defined(DL_HP_RAWDLS)
1391 req.dl_max_conind = 1; /* XXX magic number */
1392 req.dl_service_mode = DL_HP_RAWDLS;
1393 #elif defined(DL_CLDLS)
1394 req.dl_service_mode = DL_CLDLS;
1398 return (send_request(fd, (char *)&req, sizeof(req), "bind", ebuf));
1402 dlbindack(int fd, char *bufp, char *ebuf, int *uerror)
1405 return (recv_ack(fd, DL_BIND_ACK_SIZE, "bind", bufp, ebuf, uerror));
1409 dlokack(int fd, const char *what, char *bufp, char *ebuf)
1412 return (recv_ack(fd, DL_OK_ACK_SIZE, what, bufp, ebuf, NULL));
1417 dlinforeq(int fd, char *ebuf)
1421 req.dl_primitive = DL_INFO_REQ;
1423 return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf));
1427 dlinfoack(int fd, char *bufp, char *ebuf)
1430 return (recv_ack(fd, DL_INFO_ACK_SIZE, "info", bufp, ebuf, NULL));
1433 #ifdef HAVE_DLPI_PASSIVE
1435 * Enable DLPI passive mode. We do not care if this request fails, as this
1436 * indicates the underlying DLPI device does not support link aggregation.
1439 dlpassive(int fd, char *ebuf)
1441 dl_passive_req_t req;
1442 bpf_u_int32 buf[MAXDLBUF];
1444 req.dl_primitive = DL_PASSIVE_REQ;
1446 if (send_request(fd, (char *)&req, sizeof(req), "dlpassive", ebuf) == 0)
1447 (void) dlokack(fd, "dlpassive", (char *)buf, ebuf);
1453 * There's an ack *if* there's an error.
1456 dlrawdatareq(int fd, const u_char *datap, int datalen)
1458 struct strbuf ctl, data;
1459 long buf[MAXDLBUF]; /* XXX - char? */
1460 union DL_primitives *dlp;
1463 dlp = MAKE_DL_PRIMITIVES(buf);
1465 dlp->dl_primitive = DL_HP_RAWDATA_REQ;
1466 dlen = DL_HP_RAWDATA_REQ_SIZE;
1469 * HP's documentation doesn't appear to show us supplying any
1470 * address pointed to by the control part of the message.
1471 * I think that's what raw mode means - you just send the raw
1472 * packet, you don't specify where to send it to, as that's
1473 * implied by the destination address.
1477 ctl.buf = (void *)buf;
1481 data.buf = (void *)datap;
1483 return (putmsg(fd, &ctl, &data, 0));
1485 #endif /* DL_HP_RAWDLS */
1487 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
1489 get_release(char *buf, size_t bufsize, bpf_u_int32 *majorp,
1490 bpf_u_int32 *minorp, bpf_u_int32 *microp)
1497 if (sysinfo(SI_RELEASE, buf, bufsize) < 0) {
1498 strlcpy(buf, "?", bufsize);
1502 if (!isdigit((unsigned char)*cp))
1504 *majorp = strtol(cp, &cp, 10);
1507 *minorp = strtol(cp, &cp, 10);
1510 *microp = strtol(cp, &cp, 10);
1514 #ifdef DL_HP_PPA_REQ
1516 * Under HP-UX 10 and HP-UX 11, we can ask for the ppa
1521 * Determine ppa number that specifies ifname.
1523 * If the "dl_hp_ppa_info_t" doesn't have a "dl_module_id_1" member,
1524 * the code that's used here is the old code for HP-UX 10.x.
1526 * However, HP-UX 10.20, at least, appears to have such a member
1527 * in its "dl_hp_ppa_info_t" structure, so the new code is used.
1528 * The new code didn't work on an old 10.20 system on which Rick
1529 * Jones of HP tried it, but with later patches installed, it
1530 * worked - it appears that the older system had those members but
1531 * didn't put anything in them, so, if the search by name fails, we
1532 * do the old search.
1534 * Rick suggests that making sure your system is "up on the latest
1535 * lancommon/DLPI/driver patches" is probably a good idea; it'd fix
1536 * that problem, as well as allowing libpcap to see packets sent
1537 * from the system on which the libpcap application is being run.
1538 * (On 10.20, in addition to getting the latest patches, you need
1539 * to turn the kernel "lanc_outbound_promisc_flag" flag on with ADB;
1540 * a posting to "comp.sys.hp.hpux" at
1542 * http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=558092266
1544 * says that, to see the machine's outgoing traffic, you'd need to
1545 * apply the right patches to your system, and also set that variable
1548 echo 'lanc_outbound_promisc_flag/W1' | /usr/bin/adb -w /stand/vmunix /dev/kmem
1550 * which could be put in, for example, "/sbin/init.d/lan".
1552 * Setting the variable is not necessary on HP-UX 11.x.
1555 get_dlpi_ppa(register int fd, register const char *device, register int unit,
1556 register char *ebuf)
1558 register dl_hp_ppa_ack_t *ap;
1559 register dl_hp_ppa_info_t *ipstart, *ip;
1562 register u_long majdev;
1563 struct stat statbuf;
1564 dl_hp_ppa_req_t req;
1567 dl_hp_ppa_ack_t *dlp;
1572 memset((char *)&req, 0, sizeof(req));
1573 req.dl_primitive = DL_HP_PPA_REQ;
1575 memset((char *)buf, 0, sizeof(buf));
1576 if (send_request(fd, (char *)&req, sizeof(req), "hpppa", ebuf) < 0)
1577 return (PCAP_ERROR);
1579 ctl.maxlen = DL_HP_PPA_ACK_SIZE;
1581 ctl.buf = (char *)buf;
1585 * DLPI may return a big chunk of data for a DL_HP_PPA_REQ. The normal
1586 * recv_ack will fail because it set the maxlen to MAXDLBUF (8192)
1587 * which is NOT big enough for a DL_HP_PPA_REQ.
1589 * This causes libpcap applications to fail on a system with HP-APA
1592 * To figure out how big the returned data is, we first call getmsg
1593 * to get the small head and peek at the head to get the actual data
1594 * length, and then issue another getmsg to get the actual PPA data.
1596 /* get the head first */
1597 if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
1598 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1599 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno));
1600 return (PCAP_ERROR);
1603 dlp = (dl_hp_ppa_ack_t *)ctl.buf;
1604 if (dlp->dl_primitive != DL_HP_PPA_ACK) {
1605 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1606 "get_dlpi_ppa: hpppa unexpected primitive ack 0x%x",
1607 (bpf_u_int32)dlp->dl_primitive);
1608 return (PCAP_ERROR);
1611 if (ctl.len < DL_HP_PPA_ACK_SIZE) {
1612 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1613 "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1614 ctl.len, (unsigned long)DL_HP_PPA_ACK_SIZE);
1615 return (PCAP_ERROR);
1618 /* allocate buffer */
1619 if ((ppa_data_buf = (char *)malloc(dlp->dl_length)) == NULL) {
1620 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1621 "get_dlpi_ppa: hpppa malloc: %s", pcap_strerror(errno));
1622 return (PCAP_ERROR);
1624 ctl.maxlen = dlp->dl_length;
1626 ctl.buf = (char *)ppa_data_buf;
1628 if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
1629 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1630 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno));
1632 return (PCAP_ERROR);
1634 if (ctl.len < dlp->dl_length) {
1635 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1636 "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1637 ctl.len, (unsigned long)dlp->dl_length);
1639 return (PCAP_ERROR);
1642 ap = (dl_hp_ppa_ack_t *)buf;
1643 ipstart = (dl_hp_ppa_info_t *)ppa_data_buf;
1646 #ifdef HAVE_HP_PPA_INFO_T_DL_MODULE_ID_1
1648 * The "dl_hp_ppa_info_t" structure has a "dl_module_id_1"
1649 * member that should, in theory, contain the part of the
1650 * name for the device that comes before the unit number,
1651 * and should also have a "dl_module_id_2" member that may
1652 * contain an alternate name (e.g., I think Ethernet devices
1653 * have both "lan", for "lanN", and "snap", for "snapN", with
1654 * the former being for Ethernet packets and the latter being
1655 * for 802.3/802.2 packets).
1657 * Search for the device that has the specified name and
1660 for (i = 0; i < ap->dl_count; i++) {
1661 if ((strcmp((const char *)ip->dl_module_id_1, device) == 0 ||
1662 strcmp((const char *)ip->dl_module_id_2, device) == 0) &&
1663 ip->dl_instance_num == unit)
1666 ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
1670 * We don't have that member, so the search is impossible; make it
1671 * look as if the search failed.
1676 if (i == ap->dl_count) {
1678 * Well, we didn't, or can't, find the device by name.
1680 * HP-UX 10.20, whilst it has "dl_module_id_1" and
1681 * "dl_module_id_2" fields in the "dl_hp_ppa_info_t",
1682 * doesn't seem to fill them in unless the system is
1683 * at a reasonably up-to-date patch level.
1685 * Older HP-UX 10.x systems might not have those fields
1688 * Therefore, we'll search for the entry with the major
1689 * device number of a device with the name "/dev/<dev><unit>",
1690 * if such a device exists, as the old code did.
1692 pcap_snprintf(dname, sizeof(dname), "/dev/%s%d", device, unit);
1693 if (stat(dname, &statbuf) < 0) {
1694 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "stat: %s: %s",
1695 dname, pcap_strerror(errno));
1696 return (PCAP_ERROR);
1698 majdev = major(statbuf.st_rdev);
1702 for (i = 0; i < ap->dl_count; i++) {
1703 if (ip->dl_mjr_num == majdev &&
1704 ip->dl_instance_num == unit)
1707 ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
1710 if (i == ap->dl_count) {
1711 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1712 "can't find /dev/dlpi PPA for %s%d", device, unit);
1713 return (PCAP_ERROR_NO_SUCH_DEVICE);
1715 if (ip->dl_hdw_state == HDW_DEAD) {
1716 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1717 "%s%d: hardware state: DOWN\n", device, unit);
1719 return (PCAP_ERROR);
1729 * Under HP-UX 9, there is no good way to determine the ppa.
1730 * So punt and read it from /dev/kmem.
1732 static struct nlist nl[] = {
1738 static char path_vmunix[] = "/hp-ux";
1740 /* Determine ppa number that specifies ifname */
1742 get_dlpi_ppa(register int fd, register const char *ifname, register int unit,
1743 register char *ebuf)
1745 register const char *cp;
1749 char if_name[sizeof(ifnet.if_name) + 1];
1751 cp = strrchr(ifname, '/');
1754 if (nlist(path_vmunix, &nl) < 0) {
1755 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "nlist %s failed",
1759 if (nl[NL_IFNET].n_value == 0) {
1760 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
1761 "could't find %s kernel symbol",
1762 nl[NL_IFNET].n_name);
1765 kd = open("/dev/kmem", O_RDONLY);
1767 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "kmem open: %s",
1768 pcap_strerror(errno));
1771 if (dlpi_kread(kd, nl[NL_IFNET].n_value,
1772 &addr, sizeof(addr), ebuf) < 0) {
1776 for (; addr != NULL; addr = ifnet.if_next) {
1777 if (dlpi_kread(kd, (off_t)addr,
1778 &ifnet, sizeof(ifnet), ebuf) < 0 ||
1779 dlpi_kread(kd, (off_t)ifnet.if_name,
1780 if_name, sizeof(ifnet.if_name), ebuf) < 0) {
1784 if_name[sizeof(ifnet.if_name)] = '\0';
1785 if (strcmp(if_name, ifname) == 0 && ifnet.if_unit == unit)
1786 return (ifnet.if_index);
1789 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't find %s", ifname);
1794 dlpi_kread(register int fd, register off_t addr,
1795 register void *buf, register u_int len, register char *ebuf)
1799 if (lseek(fd, addr, SEEK_SET) < 0) {
1800 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "lseek: %s",
1801 pcap_strerror(errno));
1804 cc = read(fd, buf, len);
1806 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "read: %s",
1807 pcap_strerror(errno));
1809 } else if (cc != len) {
1810 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "short read (%d != %d)", cc,
1819 pcap_create_interface(const char *device _U_, char *ebuf)
1823 struct pcap_dlpi *pd;
1826 p = pcap_create_common(ebuf, sizeof (struct pcap_dlpi));
1832 pd->send_fd = -1; /* it hasn't been opened yet */
1835 p->activate_op = pcap_activate_dlpi;