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
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
40 static const char rcsid[] =
41 "@(#) $Header: /usr/local/cvs/linux/tools/build/e100boot/libpcap-0.4/pcap-dlpi.c,v 1.1 1999/08/26 10:05:23 johana Exp $ (LBL)";
44 #include <sys/types.h>
46 #ifdef HAVE_SYS_BUFMOD_H
47 #include <sys/bufmod.h>
50 #ifdef HAVE_SYS_DLPI_EXT_H
51 #include <sys/dlpi_ext.h>
54 #include <sys/socket.h>
56 #ifdef DL_HP_PPA_ACK_OBS
59 #include <sys/stream.h>
60 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
61 #include <sys/systeminfo.h>
84 #ifdef HAVE_OS_PROTO_H
88 #ifndef PCAP_DEV_PREFIX
89 #define PCAP_DEV_PREFIX "/dev"
95 static int dlattachreq(int, bpf_u_int32, char *);
96 static int dlbindack(int, char *, char *);
97 static int dlbindreq(int, bpf_u_int32, char *);
98 static int dlinfoack(int, char *, char *);
99 static int dlinforeq(int, char *);
100 static int dlokack(int, const char *, char *, char *);
101 static int recv_ack(int, int, const char *, char *, char *);
102 static int dlpromisconreq(int, bpf_u_int32, char *);
103 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
104 static char *get_release(bpf_u_int32 *, bpf_u_int32 *, bpf_u_int32 *);
106 static int send_request(int, char *, int, char *, char *);
107 #ifdef HAVE_SYS_BUFMOD_H
108 static int strioctl(int, int, int, char *);
111 static int dlpi_kread(int, off_t, void *, u_int, char *);
114 static int get_dlpi_ppa(int, const char *, int, char *);
118 pcap_stats(pcap_t *p, struct pcap_stat *ps)
125 /* XXX Needed by HP-UX (at least) */
126 static bpf_u_int32 ctlbuf[MAXDLBUF];
127 static struct strbuf ctl = {
134 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
136 register int cc, n, caplen, origlen;
137 register u_char *bp, *ep, *pk;
138 register struct bpf_insn *fcode;
139 #ifdef HAVE_SYS_BUFMOD_H
140 register struct sb_hdr *sbp;
147 struct pcap_pkthdr pkthdr;
152 data.buf = (char *)p->buffer + p->offset;
153 data.maxlen = MAXDLBUF;
156 if (getmsg(p->fd, &ctl, &data, &flags) < 0) {
157 /* Don't choke when we get ptraced */
158 if (errno == EINTR) {
162 strcpy(p->errbuf, pcap_strerror(errno));
167 bp = p->buffer + p->offset;
171 /* Loop through packets */
172 fcode = p->fcode.bf_insns;
175 #ifdef HAVE_SYS_BUFMOD_H
180 memcpy(sbp, bp, sizeof(*sbp));
183 sbp = (struct sb_hdr *)bp;
184 p->md.stat.ps_drop += sbp->sbh_drops;
185 pk = bp + sizeof(*sbp);
186 bp += sbp->sbh_totlen;
187 origlen = sbp->sbh_origlen;
188 caplen = sbp->sbh_msglen;
191 caplen = min(p->snapshot, cc);
195 ++p->md.stat.ps_recv;
196 if (bpf_filter(fcode, pk, origlen, caplen)) {
197 #ifdef HAVE_SYS_BUFMOD_H
198 pkthdr.ts = sbp->sbh_timestamp;
200 (void)gettimeofday(&pkthdr.ts, NULL);
202 pkthdr.len = origlen;
203 pkthdr.caplen = caplen;
204 /* Insure caplen does not exceed snapshot */
205 if (pkthdr.caplen > p->snapshot)
206 pkthdr.caplen = p->snapshot;
207 (*callback)(user, &pkthdr, pk);
208 if (++n >= cnt && cnt >= 0) {
214 #ifdef HAVE_SYS_BUFMOD_H
222 pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
228 register dl_info_ack_t *infop;
229 #ifdef HAVE_SYS_BUFMOD_H
230 bpf_u_int32 ss, flag;
232 register char *release;
233 bpf_u_int32 osmajor, osminor, osmicro;
236 bpf_u_int32 buf[MAXDLBUF];
238 #ifndef HAVE_DEV_DLPI
242 p = (pcap_t *)malloc(sizeof(*p));
244 strcpy(ebuf, pcap_strerror(errno));
247 memset(p, 0, sizeof(*p));
250 ** Determine device and ppa
252 cp = strpbrk(device, "0123456789");
254 sprintf(ebuf, "%s missing unit number", device);
257 ppa = strtol(cp, &eos, 10);
259 sprintf(ebuf, "%s bad unit number", device);
264 strcpy(dname, device);
266 sprintf(dname, "%s/%s", PCAP_DEV_PREFIX, device);
268 /* Map network device to /dev/dlpi unit */
270 if ((p->fd = open(cp, O_RDWR)) < 0) {
271 sprintf(ebuf, "%s: %s", cp, pcap_strerror(errno));
274 /* Map network interface to /dev/dlpi unit */
275 ppa = get_dlpi_ppa(p->fd, dname, ppa, ebuf);
279 /* Try device without unit number */
280 strcpy(dname2, dname);
281 cp = strchr(dname, *cp);
283 if ((p->fd = open(dname, O_RDWR)) < 0) {
284 if (errno != ENOENT) {
285 sprintf(ebuf, "%s: %s", dname, pcap_strerror(errno));
289 /* Try again with unit number */
290 if ((p->fd = open(dname2, O_RDWR)) < 0) {
291 sprintf(ebuf, "%s: %s", dname2, pcap_strerror(errno));
294 /* XXX Assume unit zero */
299 p->snapshot = snaplen;
302 ** Attach if "style 2" provider
304 if (dlinforeq(p->fd, ebuf) < 0 ||
305 dlinfoack(p->fd, (char *)buf, ebuf) < 0)
307 infop = &((union DL_primitives *)buf)->info_ack;
308 if (infop->dl_provider_style == DL_STYLE2 &&
309 (dlattachreq(p->fd, ppa, ebuf) < 0 ||
310 dlokack(p->fd, "attach", (char *)buf, ebuf) < 0))
313 ** Bind (defer if using HP-UX 9 or HP-UX 10.20, totally skip if
316 #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20) && !defined(sinix)
317 if (dlbindreq(p->fd, 0, ebuf) < 0 ||
318 dlbindack(p->fd, (char *)buf, ebuf) < 0)
324 ** Enable promiscuous
326 if (dlpromisconreq(p->fd, DL_PROMISC_PHYS, ebuf) < 0 ||
327 dlokack(p->fd, "promisc_phys", (char *)buf, ebuf) < 0)
331 ** Try to enable multicast (you would have thought
332 ** promiscuous would be sufficient). (Skip if using
335 #if !defined(__hpux) && !defined(sinix)
336 if (dlpromisconreq(p->fd, DL_PROMISC_MULTI, ebuf) < 0 ||
337 dlokack(p->fd, "promisc_multi", (char *)buf, ebuf) < 0)
339 "WARNING: DL_PROMISC_MULTI failed (%s)\n", ebuf);
343 ** Try to enable sap (when not in promiscuous mode when using
344 ** using HP-UX and never under SINIX)
351 (dlpromisconreq(p->fd, DL_PROMISC_SAP, ebuf) < 0 ||
352 dlokack(p->fd, "promisc_sap", (char *)buf, ebuf) < 0)) {
353 /* Not fatal if promisc since the DL_PROMISC_PHYS worked */
356 "WARNING: DL_PROMISC_SAP failed (%s)\n", ebuf);
363 ** HP-UX 9 and HP-UX 10.20 must bind after setting promiscuous
366 #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20)
367 if (dlbindreq(p->fd, 0, ebuf) < 0 ||
368 dlbindack(p->fd, (char *)buf, ebuf) < 0)
373 ** Determine link type
375 if (dlinforeq(p->fd, ebuf) < 0 ||
376 dlinfoack(p->fd, (char *)buf, ebuf) < 0)
379 infop = &((union DL_primitives *)buf)->info_ack;
380 switch (infop->dl_mac_type) {
384 p->linktype = DLT_EN10MB;
389 p->linktype = DLT_FDDI;
394 sprintf(ebuf, "unknown mac type 0x%lu", infop->dl_mac_type);
400 ** This is a non standard SunOS hack to get the ethernet header.
402 if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) {
403 sprintf(ebuf, "DLIOCRAW: %s", pcap_strerror(errno));
408 #ifdef HAVE_SYS_BUFMOD_H
410 ** Another non standard call to get the data nicely buffered
412 if (ioctl(p->fd, I_PUSH, "bufmod") != 0) {
413 sprintf(ebuf, "I_PUSH bufmod: %s", pcap_strerror(errno));
418 ** Now that the bufmod is pushed lets configure it.
420 ** There is a bug in bufmod(7). When dealing with messages of
421 ** less than snaplen size it strips data from the beginning not
424 ** This bug is supposed to be fixed in 5.3.2. Also, there is a
425 ** patch available. Ask for bugid 1149065.
429 release = get_release(&osmajor, &osminor, &osmicro);
430 if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) &&
431 getenv("BUFMOD_FIXED") == NULL) {
433 "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.\n",
439 strioctl(p->fd, SBIOCSSNAP, sizeof(ss), (char *)&ss) != 0) {
440 sprintf(ebuf, "SBIOCSSNAP: %s", pcap_strerror(errno));
445 ** Set up the bufmod flags
447 if (strioctl(p->fd, SBIOCGFLAGS, sizeof(flag), (char *)&flag) < 0) {
448 sprintf(ebuf, "SBIOCGFLAGS: %s", pcap_strerror(errno));
452 if (strioctl(p->fd, SBIOCSFLAGS, sizeof(flag), (char *)&flag) != 0) {
453 sprintf(ebuf, "SBIOCSFLAGS: %s", pcap_strerror(errno));
457 ** Set up the bufmod timeout
462 to.tv_sec = to_ms / 1000;
463 to.tv_usec = (to_ms * 1000) % 1000000;
464 if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) {
465 sprintf(ebuf, "SBIOCSTIME: %s", pcap_strerror(errno));
472 ** As the last operation flush the read side.
474 if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
475 sprintf(ebuf, "FLUSHR: %s", pcap_strerror(errno));
478 /* Allocate data buffer */
479 p->bufsize = MAXDLBUF * sizeof(bpf_u_int32);
480 p->buffer = (u_char *)malloc(p->bufsize + p->offset);
489 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
497 send_request(int fd, char *ptr, int len, char *what, char *ebuf)
507 if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) {
508 sprintf(ebuf, "send_request: putmsg \"%s\": %s",
509 what, pcap_strerror(errno));
516 recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf)
518 union DL_primitives *dlp;
522 ctl.maxlen = MAXDLBUF;
527 if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) {
528 sprintf(ebuf, "recv_ack: %s getmsg: %s",
529 what, pcap_strerror(errno));
533 dlp = (union DL_primitives *) ctl.buf;
534 switch (dlp->dl_primitive) {
547 switch (dlp->error_ack.dl_errno) {
550 sprintf(ebuf, "recv_ack: %s bad ppa (device unit)",
556 sprintf(ebuf, "recv_ack: %s: %s",
557 what, pcap_strerror(dlp->error_ack.dl_unix_errno));
562 "recv_ack: %s: Service not supplied by provider",
567 sprintf(ebuf, "recv_ack: %s error 0x%x",
568 what, (bpf_u_int32)dlp->error_ack.dl_errno);
574 sprintf(ebuf, "recv_ack: %s unexpected primitive ack 0x%x ",
575 what, (bpf_u_int32)dlp->dl_primitive);
579 if (ctl.len < size) {
580 sprintf(ebuf, "recv_ack: %s ack too small (%d < %d)",
581 what, ctl.len, size);
588 dlattachreq(int fd, bpf_u_int32 ppa, char *ebuf)
592 req.dl_primitive = DL_ATTACH_REQ;
595 return (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf));
599 dlbindreq(int fd, bpf_u_int32 sap, char *ebuf)
604 memset((char *)&req, 0, sizeof(req));
605 req.dl_primitive = DL_BIND_REQ;
607 req.dl_max_conind = 1; /* XXX magic number */
608 /* 22 is INSAP as per the HP-UX DLPI Programmer's Guide */
610 req.dl_service_mode = DL_HP_RAWDLS;
614 req.dl_service_mode = DL_CLDLS;
618 return (send_request(fd, (char *)&req, sizeof(req), "bind", ebuf));
622 dlbindack(int fd, char *bufp, char *ebuf)
625 return (recv_ack(fd, DL_BIND_ACK_SIZE, "bind", bufp, ebuf));
629 dlpromisconreq(int fd, bpf_u_int32 level, char *ebuf)
631 dl_promiscon_req_t req;
633 req.dl_primitive = DL_PROMISCON_REQ;
634 req.dl_level = level;
636 return (send_request(fd, (char *)&req, sizeof(req), "promiscon", ebuf));
640 dlokack(int fd, const char *what, char *bufp, char *ebuf)
643 return (recv_ack(fd, DL_OK_ACK_SIZE, what, bufp, ebuf));
648 dlinforeq(int fd, char *ebuf)
652 req.dl_primitive = DL_INFO_REQ;
654 return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf));
658 dlinfoack(int fd, char *bufp, char *ebuf)
661 return (recv_ack(fd, DL_INFO_ACK_SIZE, "info", bufp, ebuf));
664 #ifdef HAVE_SYS_BUFMOD_H
666 strioctl(int fd, int cmd, int len, char *dp)
675 rc = ioctl(fd, I_STR, &str);
684 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
686 get_release(bpf_u_int32 *majorp, bpf_u_int32 *minorp, bpf_u_int32 *microp)
694 if (sysinfo(SI_RELEASE, buf, sizeof(buf)) < 0)
699 *majorp = strtol(cp, &cp, 10);
702 *minorp = strtol(cp, &cp, 10);
705 *microp = strtol(cp, &cp, 10);
710 #ifdef DL_HP_PPA_ACK_OBS
712 * Under HP-UX 10, we can ask for the ppa
716 /* Determine ppa number that specifies ifname */
718 get_dlpi_ppa(register int fd, register const char *device, register int unit,
721 register dl_hp_ppa_ack_t *ap;
722 register dl_hp_ppa_info_t *ip;
724 register u_long majdev;
727 bpf_u_int32 buf[MAXDLBUF];
729 if (stat(device, &statbuf) < 0) {
730 sprintf(ebuf, "stat: %s: %s", device, pcap_strerror(errno));
733 majdev = major(statbuf.st_rdev);
735 memset((char *)&req, 0, sizeof(req));
736 req.dl_primitive = DL_HP_PPA_REQ;
738 memset((char *)buf, 0, sizeof(buf));
739 if (send_request(fd, (char *)&req, sizeof(req), "hpppa", ebuf) < 0 ||
740 recv_ack(fd, DL_HP_PPA_ACK_SIZE, "hpppa", (char *)buf, ebuf) < 0)
743 ap = (dl_hp_ppa_ack_t *)buf;
744 ip = (dl_hp_ppa_info_t *)((u_char *)ap + ap->dl_offset);
746 for(i = 0; i < ap->dl_count; i++) {
747 if (ip->dl_mjr_num == majdev && ip->dl_instance_num == unit)
750 ip = (dl_hp_ppa_info_t *)((u_char *)ip + ip->dl_next_offset);
752 if (i == ap->dl_count) {
753 sprintf(ebuf, "can't find PPA for %s", device);
756 if (ip->dl_hdw_state == HDW_DEAD) {
757 sprintf(ebuf, "%s: hardware state: DOWN\n", device);
760 return ((int)ip->dl_ppa);
766 * Under HP-UX 9, there is no good way to determine the ppa.
767 * So punt and read it from /dev/kmem.
769 static struct nlist nl[] = {
775 static char path_vmunix[] = "/hp-ux";
777 /* Determine ppa number that specifies ifname */
779 get_dlpi_ppa(register int fd, register const char *ifname, register int unit,
782 register const char *cp;
786 char if_name[sizeof(ifnet.if_name)], tifname[32];
788 cp = strrchr(ifname, '/');
791 if (nlist(path_vmunix, &nl) < 0) {
792 sprintf(ebuf, "nlist %s failed", path_vmunix);
795 if (nl[NL_IFNET].n_value == 0) {
796 sprintf(ebuf, "could't find %s kernel symbol",
797 nl[NL_IFNET].n_name);
800 kd = open("/dev/kmem", O_RDONLY);
802 sprintf(ebuf, "kmem open: %s", pcap_strerror(errno));
805 if (dlpi_kread(kd, nl[NL_IFNET].n_value,
806 &addr, sizeof(addr), ebuf) < 0) {
810 for (; addr != NULL; addr = ifnet.if_next) {
811 if (dlpi_kread(kd, (off_t)addr,
812 &ifnet, sizeof(ifnet), ebuf) < 0 ||
813 dlpi_kread(kd, (off_t)ifnet.if_name,
814 if_name, sizeof(if_name), ebuf) < 0) {
818 sprintf(tifname, "%.*s%d",
819 (int)sizeof(if_name), if_name, ifnet.if_unit);
820 if (strcmp(tifname, ifname) == 0)
821 return (ifnet.if_index);
824 sprintf(ebuf, "Can't find %s", ifname);
829 dlpi_kread(register int fd, register off_t addr,
830 register void *buf, register u_int len, register char *ebuf)
834 if (lseek(fd, addr, SEEK_SET) < 0) {
835 sprintf(ebuf, "lseek: %s", pcap_strerror(errno));
838 cc = read(fd, buf, len);
840 sprintf(ebuf, "read: %s", pcap_strerror(errno));
842 } else if (cc != len) {
843 sprintf(ebuf, "short read (%d != %d)", cc, len);