1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker * Copyright (c) 1993, 1994, 1995, 1996, 1997
3*8b26181fSAndroid Build Coastguard Worker * The Regents of the University of California. All rights reserved.
4*8b26181fSAndroid Build Coastguard Worker *
5*8b26181fSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*8b26181fSAndroid Build Coastguard Worker * modification, are permitted provided that: (1) source code distributions
7*8b26181fSAndroid Build Coastguard Worker * retain the above copyright notice and this paragraph in its entirety, (2)
8*8b26181fSAndroid Build Coastguard Worker * distributions including binary code include the above copyright notice and
9*8b26181fSAndroid Build Coastguard Worker * this paragraph in its entirety in the documentation or other materials
10*8b26181fSAndroid Build Coastguard Worker * provided with the distribution, and (3) all advertising materials mentioning
11*8b26181fSAndroid Build Coastguard Worker * features or use of this software display the following acknowledgement:
12*8b26181fSAndroid Build Coastguard Worker * ``This product includes software developed by the University of California,
13*8b26181fSAndroid Build Coastguard Worker * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*8b26181fSAndroid Build Coastguard Worker * the University nor the names of its contributors may be used to endorse
15*8b26181fSAndroid Build Coastguard Worker * or promote products derived from this software without specific prior
16*8b26181fSAndroid Build Coastguard Worker * written permission.
17*8b26181fSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*8b26181fSAndroid Build Coastguard Worker * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*8b26181fSAndroid Build Coastguard Worker * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*8b26181fSAndroid Build Coastguard Worker *
21*8b26181fSAndroid Build Coastguard Worker * This code contributed by Atanu Ghosh ([email protected]),
22*8b26181fSAndroid Build Coastguard Worker * University College London, and subsequently modified by
23*8b26181fSAndroid Build Coastguard Worker * Guy Harris ([email protected]), Mark Pizzolato
24*8b26181fSAndroid Build Coastguard Worker * <[email protected]>,
25*8b26181fSAndroid Build Coastguard Worker * Mark C. Brown ([email protected]), and Sagun Shakya <[email protected]>.
26*8b26181fSAndroid Build Coastguard Worker */
27*8b26181fSAndroid Build Coastguard Worker
28*8b26181fSAndroid Build Coastguard Worker /*
29*8b26181fSAndroid Build Coastguard Worker * Packet capture routine for DLPI under SunOS 5, HP-UX 9/10/11, and AIX.
30*8b26181fSAndroid Build Coastguard Worker *
31*8b26181fSAndroid Build Coastguard Worker * Notes:
32*8b26181fSAndroid Build Coastguard Worker *
33*8b26181fSAndroid Build Coastguard Worker * - The DLIOCRAW ioctl() is specific to SunOS.
34*8b26181fSAndroid Build Coastguard Worker *
35*8b26181fSAndroid Build Coastguard Worker * - There is a bug in bufmod(7) such that setting the snapshot
36*8b26181fSAndroid Build Coastguard Worker * length results in data being left of the front of the packet.
37*8b26181fSAndroid Build Coastguard Worker *
38*8b26181fSAndroid Build Coastguard Worker * - It might be desirable to use pfmod(7) to filter packets in the
39*8b26181fSAndroid Build Coastguard Worker * kernel when possible.
40*8b26181fSAndroid Build Coastguard Worker *
41*8b26181fSAndroid Build Coastguard Worker * - An older version of the HP-UX DLPI Programmer's Guide, which
42*8b26181fSAndroid Build Coastguard Worker * I think was advertised as the 10.20 version, used to be available
43*8b26181fSAndroid Build Coastguard Worker * at
44*8b26181fSAndroid Build Coastguard Worker *
45*8b26181fSAndroid Build Coastguard Worker * http://docs.hp.com/hpux/onlinedocs/B2355-90093/B2355-90093.html
46*8b26181fSAndroid Build Coastguard Worker *
47*8b26181fSAndroid Build Coastguard Worker * but is no longer available; it can still be found at
48*8b26181fSAndroid Build Coastguard Worker *
49*8b26181fSAndroid Build Coastguard Worker * http://h21007.www2.hp.com/dspp/files/unprotected/Drivers/Docs/Refs/B2355-90093.pdf
50*8b26181fSAndroid Build Coastguard Worker *
51*8b26181fSAndroid Build Coastguard Worker * in PDF form.
52*8b26181fSAndroid Build Coastguard Worker *
53*8b26181fSAndroid Build Coastguard Worker * - The HP-UX 10.x, 11.0, and 11i v1.6 version of the HP-UX DLPI
54*8b26181fSAndroid Build Coastguard Worker * Programmer's Guide, which I think was once advertised as the
55*8b26181fSAndroid Build Coastguard Worker * 11.00 version is available at
56*8b26181fSAndroid Build Coastguard Worker *
57*8b26181fSAndroid Build Coastguard Worker * http://docs.hp.com/en/B2355-90139/index.html
58*8b26181fSAndroid Build Coastguard Worker *
59*8b26181fSAndroid Build Coastguard Worker * - The HP-UX 11i v2 version of the HP-UX DLPI Programmer's Guide
60*8b26181fSAndroid Build Coastguard Worker * is available at
61*8b26181fSAndroid Build Coastguard Worker *
62*8b26181fSAndroid Build Coastguard Worker * http://docs.hp.com/en/B2355-90871/index.html
63*8b26181fSAndroid Build Coastguard Worker *
64*8b26181fSAndroid Build Coastguard Worker * - All of the HP documents describe raw-mode services, which are
65*8b26181fSAndroid Build Coastguard Worker * what we use if DL_HP_RAWDLS is defined. XXX - we use __hpux
66*8b26181fSAndroid Build Coastguard Worker * in some places to test for HP-UX, but use DL_HP_RAWDLS in
67*8b26181fSAndroid Build Coastguard Worker * other places; do we support any versions of HP-UX without
68*8b26181fSAndroid Build Coastguard Worker * DL_HP_RAWDLS?
69*8b26181fSAndroid Build Coastguard Worker */
70*8b26181fSAndroid Build Coastguard Worker
71*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
72*8b26181fSAndroid Build Coastguard Worker #include <config.h>
73*8b26181fSAndroid Build Coastguard Worker #endif
74*8b26181fSAndroid Build Coastguard Worker
75*8b26181fSAndroid Build Coastguard Worker #include <sys/types.h>
76*8b26181fSAndroid Build Coastguard Worker #include <sys/time.h>
77*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_BUFMOD_H
78*8b26181fSAndroid Build Coastguard Worker #include <sys/bufmod.h>
79*8b26181fSAndroid Build Coastguard Worker #endif
80*8b26181fSAndroid Build Coastguard Worker #include <sys/dlpi.h>
81*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_DLPI_EXT_H
82*8b26181fSAndroid Build Coastguard Worker #include <sys/dlpi_ext.h>
83*8b26181fSAndroid Build Coastguard Worker #endif
84*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_HPUX9
85*8b26181fSAndroid Build Coastguard Worker #include <sys/socket.h>
86*8b26181fSAndroid Build Coastguard Worker #endif
87*8b26181fSAndroid Build Coastguard Worker #ifdef DL_HP_PPA_REQ
88*8b26181fSAndroid Build Coastguard Worker #include <sys/stat.h>
89*8b26181fSAndroid Build Coastguard Worker #endif
90*8b26181fSAndroid Build Coastguard Worker #include <sys/stream.h>
91*8b26181fSAndroid Build Coastguard Worker #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
92*8b26181fSAndroid Build Coastguard Worker #include <sys/systeminfo.h>
93*8b26181fSAndroid Build Coastguard Worker #endif
94*8b26181fSAndroid Build Coastguard Worker
95*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_HPUX9
96*8b26181fSAndroid Build Coastguard Worker #include <net/if.h>
97*8b26181fSAndroid Build Coastguard Worker #endif
98*8b26181fSAndroid Build Coastguard Worker
99*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_HPUX9
100*8b26181fSAndroid Build Coastguard Worker #include <nlist.h>
101*8b26181fSAndroid Build Coastguard Worker #endif
102*8b26181fSAndroid Build Coastguard Worker #include <errno.h>
103*8b26181fSAndroid Build Coastguard Worker #include <fcntl.h>
104*8b26181fSAndroid Build Coastguard Worker #include <memory.h>
105*8b26181fSAndroid Build Coastguard Worker #include <stdio.h>
106*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h>
107*8b26181fSAndroid Build Coastguard Worker #include <string.h>
108*8b26181fSAndroid Build Coastguard Worker #include <stropts.h>
109*8b26181fSAndroid Build Coastguard Worker #include <unistd.h>
110*8b26181fSAndroid Build Coastguard Worker #include <limits.h>
111*8b26181fSAndroid Build Coastguard Worker
112*8b26181fSAndroid Build Coastguard Worker #include "pcap-int.h"
113*8b26181fSAndroid Build Coastguard Worker #include "dlpisubs.h"
114*8b26181fSAndroid Build Coastguard Worker
115*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OS_PROTO_H
116*8b26181fSAndroid Build Coastguard Worker #include "os-proto.h"
117*8b26181fSAndroid Build Coastguard Worker #endif
118*8b26181fSAndroid Build Coastguard Worker
119*8b26181fSAndroid Build Coastguard Worker #if defined(__hpux)
120*8b26181fSAndroid Build Coastguard Worker /*
121*8b26181fSAndroid Build Coastguard Worker * HP-UX has a /dev/dlpi device; you open it and set the PPA of the actual
122*8b26181fSAndroid Build Coastguard Worker * network device you want.
123*8b26181fSAndroid Build Coastguard Worker */
124*8b26181fSAndroid Build Coastguard Worker #define HAVE_DEV_DLPI
125*8b26181fSAndroid Build Coastguard Worker #elif defined(_AIX)
126*8b26181fSAndroid Build Coastguard Worker /*
127*8b26181fSAndroid Build Coastguard Worker * AIX has a /dev/dlpi directory, with devices named after the interfaces
128*8b26181fSAndroid Build Coastguard Worker * underneath it.
129*8b26181fSAndroid Build Coastguard Worker */
130*8b26181fSAndroid Build Coastguard Worker #define PCAP_DEV_PREFIX "/dev/dlpi"
131*8b26181fSAndroid Build Coastguard Worker #elif defined(HAVE_SOLARIS)
132*8b26181fSAndroid Build Coastguard Worker /*
133*8b26181fSAndroid Build Coastguard Worker * Solaris has devices named after the interfaces underneath /dev.
134*8b26181fSAndroid Build Coastguard Worker */
135*8b26181fSAndroid Build Coastguard Worker #define PCAP_DEV_PREFIX "/dev"
136*8b26181fSAndroid Build Coastguard Worker #endif
137*8b26181fSAndroid Build Coastguard Worker
138*8b26181fSAndroid Build Coastguard Worker #define MAXDLBUF 8192
139*8b26181fSAndroid Build Coastguard Worker
140*8b26181fSAndroid Build Coastguard Worker /* Forwards */
141*8b26181fSAndroid Build Coastguard Worker static char *split_dname(char *, u_int *, char *);
142*8b26181fSAndroid Build Coastguard Worker static int dl_doattach(int, int, char *);
143*8b26181fSAndroid Build Coastguard Worker #ifdef DL_HP_RAWDLS
144*8b26181fSAndroid Build Coastguard Worker static int dl_dohpuxbind(int, char *);
145*8b26181fSAndroid Build Coastguard Worker #endif
146*8b26181fSAndroid Build Coastguard Worker static int dlpromiscon(pcap_t *, bpf_u_int32);
147*8b26181fSAndroid Build Coastguard Worker static int dlbindreq(int, bpf_u_int32, char *);
148*8b26181fSAndroid Build Coastguard Worker static int dlbindack(int, char *, char *, int *);
149*8b26181fSAndroid Build Coastguard Worker static int dlokack(int, const char *, char *, char *, int *);
150*8b26181fSAndroid Build Coastguard Worker static int dlinforeq(int, char *);
151*8b26181fSAndroid Build Coastguard Worker static int dlinfoack(int, char *, char *);
152*8b26181fSAndroid Build Coastguard Worker
153*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DL_PASSIVE_REQ_T
154*8b26181fSAndroid Build Coastguard Worker static void dlpassive(int, char *);
155*8b26181fSAndroid Build Coastguard Worker #endif
156*8b26181fSAndroid Build Coastguard Worker
157*8b26181fSAndroid Build Coastguard Worker #ifdef DL_HP_RAWDLS
158*8b26181fSAndroid Build Coastguard Worker static int dlrawdatareq(int, const u_char *, int);
159*8b26181fSAndroid Build Coastguard Worker #endif
160*8b26181fSAndroid Build Coastguard Worker static int recv_ack(int, int, const char *, char *, char *, int *);
161*8b26181fSAndroid Build Coastguard Worker static char *dlstrerror(char *, size_t, bpf_u_int32);
162*8b26181fSAndroid Build Coastguard Worker static char *dlprim(char *, size_t, bpf_u_int32);
163*8b26181fSAndroid Build Coastguard Worker #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
164*8b26181fSAndroid Build Coastguard Worker #define GET_RELEASE_BUFSIZE 32
165*8b26181fSAndroid Build Coastguard Worker static void get_release(char *, size_t, bpf_u_int32 *, bpf_u_int32 *,
166*8b26181fSAndroid Build Coastguard Worker bpf_u_int32 *);
167*8b26181fSAndroid Build Coastguard Worker #endif
168*8b26181fSAndroid Build Coastguard Worker static int send_request(int, char *, int, char *, char *);
169*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_HPUX9
170*8b26181fSAndroid Build Coastguard Worker static int dlpi_kread(int, off_t, void *, u_int, char *);
171*8b26181fSAndroid Build Coastguard Worker #endif
172*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DEV_DLPI
173*8b26181fSAndroid Build Coastguard Worker static int get_dlpi_ppa(int, const char *, u_int, u_int *, char *);
174*8b26181fSAndroid Build Coastguard Worker #endif
175*8b26181fSAndroid Build Coastguard Worker
176*8b26181fSAndroid Build Coastguard Worker /*
177*8b26181fSAndroid Build Coastguard Worker * Cast a buffer to "union DL_primitives" without provoking warnings
178*8b26181fSAndroid Build Coastguard Worker * from the compiler.
179*8b26181fSAndroid Build Coastguard Worker */
180*8b26181fSAndroid Build Coastguard Worker #define MAKE_DL_PRIMITIVES(ptr) ((union DL_primitives *)(void *)(ptr))
181*8b26181fSAndroid Build Coastguard Worker
182*8b26181fSAndroid Build Coastguard Worker static int
pcap_read_dlpi(pcap_t * p,int cnt,pcap_handler callback,u_char * user)183*8b26181fSAndroid Build Coastguard Worker pcap_read_dlpi(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
184*8b26181fSAndroid Build Coastguard Worker {
185*8b26181fSAndroid Build Coastguard Worker int cc;
186*8b26181fSAndroid Build Coastguard Worker u_char *bp;
187*8b26181fSAndroid Build Coastguard Worker int flags;
188*8b26181fSAndroid Build Coastguard Worker bpf_u_int32 ctlbuf[MAXDLBUF];
189*8b26181fSAndroid Build Coastguard Worker struct strbuf ctl = {
190*8b26181fSAndroid Build Coastguard Worker MAXDLBUF,
191*8b26181fSAndroid Build Coastguard Worker 0,
192*8b26181fSAndroid Build Coastguard Worker (char *)ctlbuf
193*8b26181fSAndroid Build Coastguard Worker };
194*8b26181fSAndroid Build Coastguard Worker struct strbuf data;
195*8b26181fSAndroid Build Coastguard Worker
196*8b26181fSAndroid Build Coastguard Worker flags = 0;
197*8b26181fSAndroid Build Coastguard Worker cc = p->cc;
198*8b26181fSAndroid Build Coastguard Worker if (cc == 0) {
199*8b26181fSAndroid Build Coastguard Worker data.buf = (char *)p->buffer + p->offset;
200*8b26181fSAndroid Build Coastguard Worker data.maxlen = p->bufsize;
201*8b26181fSAndroid Build Coastguard Worker data.len = 0;
202*8b26181fSAndroid Build Coastguard Worker do {
203*8b26181fSAndroid Build Coastguard Worker /*
204*8b26181fSAndroid Build Coastguard Worker * Has "pcap_breakloop()" been called?
205*8b26181fSAndroid Build Coastguard Worker */
206*8b26181fSAndroid Build Coastguard Worker if (p->break_loop) {
207*8b26181fSAndroid Build Coastguard Worker /*
208*8b26181fSAndroid Build Coastguard Worker * Yes - clear the flag that indicates
209*8b26181fSAndroid Build Coastguard Worker * that it has, and return -2 to
210*8b26181fSAndroid Build Coastguard Worker * indicate that we were told to
211*8b26181fSAndroid Build Coastguard Worker * break out of the loop.
212*8b26181fSAndroid Build Coastguard Worker */
213*8b26181fSAndroid Build Coastguard Worker p->break_loop = 0;
214*8b26181fSAndroid Build Coastguard Worker return (-2);
215*8b26181fSAndroid Build Coastguard Worker }
216*8b26181fSAndroid Build Coastguard Worker /*
217*8b26181fSAndroid Build Coastguard Worker * XXX - check for the DLPI primitive, which
218*8b26181fSAndroid Build Coastguard Worker * would be DL_HP_RAWDATA_IND on HP-UX
219*8b26181fSAndroid Build Coastguard Worker * if we're in raw mode?
220*8b26181fSAndroid Build Coastguard Worker */
221*8b26181fSAndroid Build Coastguard Worker ctl.buf = (char *)ctlbuf;
222*8b26181fSAndroid Build Coastguard Worker ctl.maxlen = MAXDLBUF;
223*8b26181fSAndroid Build Coastguard Worker ctl.len = 0;
224*8b26181fSAndroid Build Coastguard Worker if (getmsg(p->fd, &ctl, &data, &flags) < 0) {
225*8b26181fSAndroid Build Coastguard Worker /* Don't choke when we get ptraced */
226*8b26181fSAndroid Build Coastguard Worker switch (errno) {
227*8b26181fSAndroid Build Coastguard Worker
228*8b26181fSAndroid Build Coastguard Worker case EINTR:
229*8b26181fSAndroid Build Coastguard Worker cc = 0;
230*8b26181fSAndroid Build Coastguard Worker continue;
231*8b26181fSAndroid Build Coastguard Worker
232*8b26181fSAndroid Build Coastguard Worker case EAGAIN:
233*8b26181fSAndroid Build Coastguard Worker return (0);
234*8b26181fSAndroid Build Coastguard Worker }
235*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf,
236*8b26181fSAndroid Build Coastguard Worker sizeof(p->errbuf), errno, "getmsg");
237*8b26181fSAndroid Build Coastguard Worker return (-1);
238*8b26181fSAndroid Build Coastguard Worker }
239*8b26181fSAndroid Build Coastguard Worker cc = data.len;
240*8b26181fSAndroid Build Coastguard Worker } while (cc == 0);
241*8b26181fSAndroid Build Coastguard Worker bp = (u_char *)p->buffer + p->offset;
242*8b26181fSAndroid Build Coastguard Worker } else
243*8b26181fSAndroid Build Coastguard Worker bp = p->bp;
244*8b26181fSAndroid Build Coastguard Worker
245*8b26181fSAndroid Build Coastguard Worker return (pcap_process_pkts(p, callback, user, cnt, bp, cc));
246*8b26181fSAndroid Build Coastguard Worker }
247*8b26181fSAndroid Build Coastguard Worker
248*8b26181fSAndroid Build Coastguard Worker static int
pcap_inject_dlpi(pcap_t * p,const void * buf,int size)249*8b26181fSAndroid Build Coastguard Worker pcap_inject_dlpi(pcap_t *p, const void *buf, int size)
250*8b26181fSAndroid Build Coastguard Worker {
251*8b26181fSAndroid Build Coastguard Worker #ifdef DL_HP_RAWDLS
252*8b26181fSAndroid Build Coastguard Worker struct pcap_dlpi *pd = p->priv;
253*8b26181fSAndroid Build Coastguard Worker #endif
254*8b26181fSAndroid Build Coastguard Worker int ret;
255*8b26181fSAndroid Build Coastguard Worker
256*8b26181fSAndroid Build Coastguard Worker #if defined(DLIOCRAW)
257*8b26181fSAndroid Build Coastguard Worker ret = write(p->fd, buf, size);
258*8b26181fSAndroid Build Coastguard Worker if (ret == -1) {
259*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
260*8b26181fSAndroid Build Coastguard Worker errno, "send");
261*8b26181fSAndroid Build Coastguard Worker return (-1);
262*8b26181fSAndroid Build Coastguard Worker }
263*8b26181fSAndroid Build Coastguard Worker #elif defined(DL_HP_RAWDLS)
264*8b26181fSAndroid Build Coastguard Worker if (pd->send_fd < 0) {
265*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
266*8b26181fSAndroid Build Coastguard Worker "send: Output FD couldn't be opened");
267*8b26181fSAndroid Build Coastguard Worker return (-1);
268*8b26181fSAndroid Build Coastguard Worker }
269*8b26181fSAndroid Build Coastguard Worker ret = dlrawdatareq(pd->send_fd, buf, size);
270*8b26181fSAndroid Build Coastguard Worker if (ret == -1) {
271*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
272*8b26181fSAndroid Build Coastguard Worker errno, "send");
273*8b26181fSAndroid Build Coastguard Worker return (-1);
274*8b26181fSAndroid Build Coastguard Worker }
275*8b26181fSAndroid Build Coastguard Worker /*
276*8b26181fSAndroid Build Coastguard Worker * putmsg() returns either 0 or -1; it doesn't indicate how
277*8b26181fSAndroid Build Coastguard Worker * many bytes were written (presumably they were all written
278*8b26181fSAndroid Build Coastguard Worker * or none of them were written). OpenBSD's pcap_inject()
279*8b26181fSAndroid Build Coastguard Worker * returns the number of bytes written, so, for API compatibility,
280*8b26181fSAndroid Build Coastguard Worker * we return the number of bytes we were told to write.
281*8b26181fSAndroid Build Coastguard Worker */
282*8b26181fSAndroid Build Coastguard Worker ret = size;
283*8b26181fSAndroid Build Coastguard Worker #else /* no raw mode */
284*8b26181fSAndroid Build Coastguard Worker /*
285*8b26181fSAndroid Build Coastguard Worker * XXX - this is a pain, because you might have to extract
286*8b26181fSAndroid Build Coastguard Worker * the address from the packet and use it in a DL_UNITDATA_REQ
287*8b26181fSAndroid Build Coastguard Worker * request. That would be dependent on the link-layer type.
288*8b26181fSAndroid Build Coastguard Worker *
289*8b26181fSAndroid Build Coastguard Worker * I also don't know what SAP you'd have to bind the descriptor
290*8b26181fSAndroid Build Coastguard Worker * to, or whether you'd need separate "receive" and "send" FDs,
291*8b26181fSAndroid Build Coastguard Worker * nor do I know whether you'd need different bindings for
292*8b26181fSAndroid Build Coastguard Worker * D/I/X Ethernet and 802.3, or for {FDDI,Token Ring} plus
293*8b26181fSAndroid Build Coastguard Worker * 802.2 and {FDDI,Token Ring} plus 802.2 plus SNAP.
294*8b26181fSAndroid Build Coastguard Worker *
295*8b26181fSAndroid Build Coastguard Worker * So, for now, we just return a "you can't send" indication,
296*8b26181fSAndroid Build Coastguard Worker * and leave it up to somebody with a DLPI-based system lacking
297*8b26181fSAndroid Build Coastguard Worker * both DLIOCRAW and DL_HP_RAWDLS to supply code to implement
298*8b26181fSAndroid Build Coastguard Worker * packet transmission on that system. If they do, they should
299*8b26181fSAndroid Build Coastguard Worker * send it to us - but should not send us code that assumes
300*8b26181fSAndroid Build Coastguard Worker * Ethernet; if the code doesn't work on non-Ethernet interfaces,
301*8b26181fSAndroid Build Coastguard Worker * it should check "p->linktype" and reject the send request if
302*8b26181fSAndroid Build Coastguard Worker * it's anything other than DLT_EN10MB.
303*8b26181fSAndroid Build Coastguard Worker */
304*8b26181fSAndroid Build Coastguard Worker pcap_strlcpy(p->errbuf, "send: Not supported on this version of this OS",
305*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE);
306*8b26181fSAndroid Build Coastguard Worker ret = -1;
307*8b26181fSAndroid Build Coastguard Worker #endif /* raw mode */
308*8b26181fSAndroid Build Coastguard Worker return (ret);
309*8b26181fSAndroid Build Coastguard Worker }
310*8b26181fSAndroid Build Coastguard Worker
311*8b26181fSAndroid Build Coastguard Worker #ifndef DL_IPATM
312*8b26181fSAndroid Build Coastguard Worker #define DL_IPATM 0x12 /* ATM Classical IP interface */
313*8b26181fSAndroid Build Coastguard Worker #endif
314*8b26181fSAndroid Build Coastguard Worker
315*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SOLARIS
316*8b26181fSAndroid Build Coastguard Worker /*
317*8b26181fSAndroid Build Coastguard Worker * For SunATM.
318*8b26181fSAndroid Build Coastguard Worker */
319*8b26181fSAndroid Build Coastguard Worker #ifndef A_GET_UNITS
320*8b26181fSAndroid Build Coastguard Worker #define A_GET_UNITS (('A'<<8)|118)
321*8b26181fSAndroid Build Coastguard Worker #endif /* A_GET_UNITS */
322*8b26181fSAndroid Build Coastguard Worker #ifndef A_PROMISCON_REQ
323*8b26181fSAndroid Build Coastguard Worker #define A_PROMISCON_REQ (('A'<<8)|121)
324*8b26181fSAndroid Build Coastguard Worker #endif /* A_PROMISCON_REQ */
325*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_SOLARIS */
326*8b26181fSAndroid Build Coastguard Worker
327*8b26181fSAndroid Build Coastguard Worker static void
pcap_cleanup_dlpi(pcap_t * p)328*8b26181fSAndroid Build Coastguard Worker pcap_cleanup_dlpi(pcap_t *p)
329*8b26181fSAndroid Build Coastguard Worker {
330*8b26181fSAndroid Build Coastguard Worker #ifdef DL_HP_RAWDLS
331*8b26181fSAndroid Build Coastguard Worker struct pcap_dlpi *pd = p->priv;
332*8b26181fSAndroid Build Coastguard Worker
333*8b26181fSAndroid Build Coastguard Worker if (pd->send_fd >= 0) {
334*8b26181fSAndroid Build Coastguard Worker close(pd->send_fd);
335*8b26181fSAndroid Build Coastguard Worker pd->send_fd = -1;
336*8b26181fSAndroid Build Coastguard Worker }
337*8b26181fSAndroid Build Coastguard Worker #endif
338*8b26181fSAndroid Build Coastguard Worker pcap_cleanup_live_common(p);
339*8b26181fSAndroid Build Coastguard Worker }
340*8b26181fSAndroid Build Coastguard Worker
341*8b26181fSAndroid Build Coastguard Worker static int
open_dlpi_device(const char * name,u_int * ppa,char * errbuf)342*8b26181fSAndroid Build Coastguard Worker open_dlpi_device(const char *name, u_int *ppa, char *errbuf)
343*8b26181fSAndroid Build Coastguard Worker {
344*8b26181fSAndroid Build Coastguard Worker int status;
345*8b26181fSAndroid Build Coastguard Worker char dname[100];
346*8b26181fSAndroid Build Coastguard Worker char *cp;
347*8b26181fSAndroid Build Coastguard Worker int fd;
348*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DEV_DLPI
349*8b26181fSAndroid Build Coastguard Worker u_int unit;
350*8b26181fSAndroid Build Coastguard Worker #else
351*8b26181fSAndroid Build Coastguard Worker char dname2[100];
352*8b26181fSAndroid Build Coastguard Worker #endif
353*8b26181fSAndroid Build Coastguard Worker
354*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DEV_DLPI
355*8b26181fSAndroid Build Coastguard Worker /*
356*8b26181fSAndroid Build Coastguard Worker ** Remove any "/dev/" on the front of the device.
357*8b26181fSAndroid Build Coastguard Worker */
358*8b26181fSAndroid Build Coastguard Worker cp = strrchr(name, '/');
359*8b26181fSAndroid Build Coastguard Worker if (cp == NULL)
360*8b26181fSAndroid Build Coastguard Worker pcap_strlcpy(dname, name, sizeof(dname));
361*8b26181fSAndroid Build Coastguard Worker else
362*8b26181fSAndroid Build Coastguard Worker pcap_strlcpy(dname, cp + 1, sizeof(dname));
363*8b26181fSAndroid Build Coastguard Worker
364*8b26181fSAndroid Build Coastguard Worker /*
365*8b26181fSAndroid Build Coastguard Worker * Split the device name into a device type name and a unit number;
366*8b26181fSAndroid Build Coastguard Worker * chop off the unit number, so "dname" is just a device type name.
367*8b26181fSAndroid Build Coastguard Worker */
368*8b26181fSAndroid Build Coastguard Worker cp = split_dname(dname, &unit, errbuf);
369*8b26181fSAndroid Build Coastguard Worker if (cp == NULL) {
370*8b26181fSAndroid Build Coastguard Worker /*
371*8b26181fSAndroid Build Coastguard Worker * split_dname() has filled in the error message.
372*8b26181fSAndroid Build Coastguard Worker */
373*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR_NO_SUCH_DEVICE);
374*8b26181fSAndroid Build Coastguard Worker }
375*8b26181fSAndroid Build Coastguard Worker *cp = '\0';
376*8b26181fSAndroid Build Coastguard Worker
377*8b26181fSAndroid Build Coastguard Worker /*
378*8b26181fSAndroid Build Coastguard Worker * Use "/dev/dlpi" as the device.
379*8b26181fSAndroid Build Coastguard Worker *
380*8b26181fSAndroid Build Coastguard Worker * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that
381*8b26181fSAndroid Build Coastguard Worker * the "dl_mjr_num" field is for the "major number of interface
382*8b26181fSAndroid Build Coastguard Worker * driver"; that's the major of "/dev/dlpi" on the system on
383*8b26181fSAndroid Build Coastguard Worker * which I tried this, but there may be DLPI devices that
384*8b26181fSAndroid Build Coastguard Worker * use a different driver, in which case we may need to
385*8b26181fSAndroid Build Coastguard Worker * search "/dev" for the appropriate device with that major
386*8b26181fSAndroid Build Coastguard Worker * device number, rather than hardwiring "/dev/dlpi".
387*8b26181fSAndroid Build Coastguard Worker */
388*8b26181fSAndroid Build Coastguard Worker cp = "/dev/dlpi";
389*8b26181fSAndroid Build Coastguard Worker if ((fd = open(cp, O_RDWR)) < 0) {
390*8b26181fSAndroid Build Coastguard Worker if (errno == EPERM || errno == EACCES) {
391*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR_PERM_DENIED;
392*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
393*8b26181fSAndroid Build Coastguard Worker "Attempt to open %s failed with %s - root privilege may be required",
394*8b26181fSAndroid Build Coastguard Worker cp, (errno == EPERM) ? "EPERM" : "EACCES");
395*8b26181fSAndroid Build Coastguard Worker } else {
396*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
397*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
398*8b26181fSAndroid Build Coastguard Worker errno, "Attempt to open %s failed", cp);
399*8b26181fSAndroid Build Coastguard Worker }
400*8b26181fSAndroid Build Coastguard Worker return (status);
401*8b26181fSAndroid Build Coastguard Worker }
402*8b26181fSAndroid Build Coastguard Worker
403*8b26181fSAndroid Build Coastguard Worker /*
404*8b26181fSAndroid Build Coastguard Worker * Get a table of all PPAs for that device, and search that
405*8b26181fSAndroid Build Coastguard Worker * table for the specified device type name and unit number.
406*8b26181fSAndroid Build Coastguard Worker */
407*8b26181fSAndroid Build Coastguard Worker status = get_dlpi_ppa(fd, dname, unit, ppa, errbuf);
408*8b26181fSAndroid Build Coastguard Worker if (status < 0) {
409*8b26181fSAndroid Build Coastguard Worker close(fd);
410*8b26181fSAndroid Build Coastguard Worker return (status);
411*8b26181fSAndroid Build Coastguard Worker }
412*8b26181fSAndroid Build Coastguard Worker #else
413*8b26181fSAndroid Build Coastguard Worker /*
414*8b26181fSAndroid Build Coastguard Worker * If the device name begins with "/", assume it begins with
415*8b26181fSAndroid Build Coastguard Worker * the pathname of the directory containing the device to open;
416*8b26181fSAndroid Build Coastguard Worker * otherwise, concatenate the device directory name and the
417*8b26181fSAndroid Build Coastguard Worker * device name.
418*8b26181fSAndroid Build Coastguard Worker */
419*8b26181fSAndroid Build Coastguard Worker if (*name == '/')
420*8b26181fSAndroid Build Coastguard Worker pcap_strlcpy(dname, name, sizeof(dname));
421*8b26181fSAndroid Build Coastguard Worker else
422*8b26181fSAndroid Build Coastguard Worker snprintf(dname, sizeof(dname), "%s/%s", PCAP_DEV_PREFIX,
423*8b26181fSAndroid Build Coastguard Worker name);
424*8b26181fSAndroid Build Coastguard Worker
425*8b26181fSAndroid Build Coastguard Worker /*
426*8b26181fSAndroid Build Coastguard Worker * Get the unit number, and a pointer to the end of the device
427*8b26181fSAndroid Build Coastguard Worker * type name.
428*8b26181fSAndroid Build Coastguard Worker */
429*8b26181fSAndroid Build Coastguard Worker cp = split_dname(dname, ppa, errbuf);
430*8b26181fSAndroid Build Coastguard Worker if (cp == NULL) {
431*8b26181fSAndroid Build Coastguard Worker /*
432*8b26181fSAndroid Build Coastguard Worker * split_dname() has filled in the error message.
433*8b26181fSAndroid Build Coastguard Worker */
434*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR_NO_SUCH_DEVICE);
435*8b26181fSAndroid Build Coastguard Worker }
436*8b26181fSAndroid Build Coastguard Worker
437*8b26181fSAndroid Build Coastguard Worker /*
438*8b26181fSAndroid Build Coastguard Worker * Make a copy of the device pathname, and then remove the unit
439*8b26181fSAndroid Build Coastguard Worker * number from the device pathname.
440*8b26181fSAndroid Build Coastguard Worker */
441*8b26181fSAndroid Build Coastguard Worker pcap_strlcpy(dname2, dname, sizeof(dname));
442*8b26181fSAndroid Build Coastguard Worker *cp = '\0';
443*8b26181fSAndroid Build Coastguard Worker
444*8b26181fSAndroid Build Coastguard Worker /* Try device without unit number */
445*8b26181fSAndroid Build Coastguard Worker if ((fd = open(dname, O_RDWR)) < 0) {
446*8b26181fSAndroid Build Coastguard Worker if (errno != ENOENT) {
447*8b26181fSAndroid Build Coastguard Worker if (errno == EPERM || errno == EACCES) {
448*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR_PERM_DENIED;
449*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
450*8b26181fSAndroid Build Coastguard Worker "Attempt to open %s failed with %s - root privilege may be required",
451*8b26181fSAndroid Build Coastguard Worker dname,
452*8b26181fSAndroid Build Coastguard Worker (errno == EPERM) ? "EPERM" : "EACCES");
453*8b26181fSAndroid Build Coastguard Worker } else {
454*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
455*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf,
456*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, errno,
457*8b26181fSAndroid Build Coastguard Worker "Attempt to open %s failed", dname);
458*8b26181fSAndroid Build Coastguard Worker }
459*8b26181fSAndroid Build Coastguard Worker return (status);
460*8b26181fSAndroid Build Coastguard Worker }
461*8b26181fSAndroid Build Coastguard Worker
462*8b26181fSAndroid Build Coastguard Worker /* Try again with unit number */
463*8b26181fSAndroid Build Coastguard Worker if ((fd = open(dname2, O_RDWR)) < 0) {
464*8b26181fSAndroid Build Coastguard Worker if (errno == ENOENT) {
465*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR_NO_SUCH_DEVICE;
466*8b26181fSAndroid Build Coastguard Worker
467*8b26181fSAndroid Build Coastguard Worker /*
468*8b26181fSAndroid Build Coastguard Worker * We provide an error message even
469*8b26181fSAndroid Build Coastguard Worker * for this error, for diagnostic
470*8b26181fSAndroid Build Coastguard Worker * purposes (so that, for example,
471*8b26181fSAndroid Build Coastguard Worker * the app can show the message if the
472*8b26181fSAndroid Build Coastguard Worker * user requests it).
473*8b26181fSAndroid Build Coastguard Worker *
474*8b26181fSAndroid Build Coastguard Worker * In it, we just report "No DLPI device
475*8b26181fSAndroid Build Coastguard Worker * found" with the device name, so people
476*8b26181fSAndroid Build Coastguard Worker * don't get confused and think, for example,
477*8b26181fSAndroid Build Coastguard Worker * that if they can't capture on "lo0"
478*8b26181fSAndroid Build Coastguard Worker * on Solaris prior to Solaris 11 the fix
479*8b26181fSAndroid Build Coastguard Worker * is to change libpcap (or the application
480*8b26181fSAndroid Build Coastguard Worker * that uses it) to look for something other
481*8b26181fSAndroid Build Coastguard Worker * than "/dev/lo0", as the fix is to use
482*8b26181fSAndroid Build Coastguard Worker * Solaris 11 or some operating system
483*8b26181fSAndroid Build Coastguard Worker * other than Solaris - you just *can't*
484*8b26181fSAndroid Build Coastguard Worker * capture on a loopback interface
485*8b26181fSAndroid Build Coastguard Worker * on Solaris prior to Solaris 11, the lack
486*8b26181fSAndroid Build Coastguard Worker * of a DLPI device for the loopback
487*8b26181fSAndroid Build Coastguard Worker * interface is just a symptom of that
488*8b26181fSAndroid Build Coastguard Worker * inability.
489*8b26181fSAndroid Build Coastguard Worker */
490*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
491*8b26181fSAndroid Build Coastguard Worker "%s: No DLPI device found", name);
492*8b26181fSAndroid Build Coastguard Worker } else {
493*8b26181fSAndroid Build Coastguard Worker if (errno == EPERM || errno == EACCES) {
494*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR_PERM_DENIED;
495*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, PCAP_ERRBUF_SIZE,
496*8b26181fSAndroid Build Coastguard Worker "Attempt to open %s failed with %s - root privilege may be required",
497*8b26181fSAndroid Build Coastguard Worker dname2,
498*8b26181fSAndroid Build Coastguard Worker (errno == EPERM) ? "EPERM" : "EACCES");
499*8b26181fSAndroid Build Coastguard Worker } else {
500*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
501*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf,
502*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE, errno,
503*8b26181fSAndroid Build Coastguard Worker "Attempt to open %s failed",
504*8b26181fSAndroid Build Coastguard Worker dname2);
505*8b26181fSAndroid Build Coastguard Worker }
506*8b26181fSAndroid Build Coastguard Worker }
507*8b26181fSAndroid Build Coastguard Worker return (status);
508*8b26181fSAndroid Build Coastguard Worker }
509*8b26181fSAndroid Build Coastguard Worker /* XXX Assume unit zero */
510*8b26181fSAndroid Build Coastguard Worker *ppa = 0;
511*8b26181fSAndroid Build Coastguard Worker }
512*8b26181fSAndroid Build Coastguard Worker #endif
513*8b26181fSAndroid Build Coastguard Worker return (fd);
514*8b26181fSAndroid Build Coastguard Worker }
515*8b26181fSAndroid Build Coastguard Worker
516*8b26181fSAndroid Build Coastguard Worker static int
pcap_activate_dlpi(pcap_t * p)517*8b26181fSAndroid Build Coastguard Worker pcap_activate_dlpi(pcap_t *p)
518*8b26181fSAndroid Build Coastguard Worker {
519*8b26181fSAndroid Build Coastguard Worker #ifdef DL_HP_RAWDLS
520*8b26181fSAndroid Build Coastguard Worker struct pcap_dlpi *pd = p->priv;
521*8b26181fSAndroid Build Coastguard Worker #endif
522*8b26181fSAndroid Build Coastguard Worker int status = 0;
523*8b26181fSAndroid Build Coastguard Worker int retv;
524*8b26181fSAndroid Build Coastguard Worker u_int ppa;
525*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SOLARIS
526*8b26181fSAndroid Build Coastguard Worker int isatm = 0;
527*8b26181fSAndroid Build Coastguard Worker #endif
528*8b26181fSAndroid Build Coastguard Worker register dl_info_ack_t *infop;
529*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_BUFMOD_H
530*8b26181fSAndroid Build Coastguard Worker bpf_u_int32 ss;
531*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SOLARIS
532*8b26181fSAndroid Build Coastguard Worker char release[GET_RELEASE_BUFSIZE];
533*8b26181fSAndroid Build Coastguard Worker bpf_u_int32 osmajor, osminor, osmicro;
534*8b26181fSAndroid Build Coastguard Worker #endif
535*8b26181fSAndroid Build Coastguard Worker #endif
536*8b26181fSAndroid Build Coastguard Worker bpf_u_int32 buf[MAXDLBUF];
537*8b26181fSAndroid Build Coastguard Worker
538*8b26181fSAndroid Build Coastguard Worker p->fd = open_dlpi_device(p->opt.device, &ppa, p->errbuf);
539*8b26181fSAndroid Build Coastguard Worker if (p->fd < 0) {
540*8b26181fSAndroid Build Coastguard Worker status = p->fd;
541*8b26181fSAndroid Build Coastguard Worker goto bad;
542*8b26181fSAndroid Build Coastguard Worker }
543*8b26181fSAndroid Build Coastguard Worker
544*8b26181fSAndroid Build Coastguard Worker #ifdef DL_HP_RAWDLS
545*8b26181fSAndroid Build Coastguard Worker /*
546*8b26181fSAndroid Build Coastguard Worker * XXX - HP-UX 10.20 and 11.xx don't appear to support sending and
547*8b26181fSAndroid Build Coastguard Worker * receiving packets on the same descriptor - you need separate
548*8b26181fSAndroid Build Coastguard Worker * descriptors for sending and receiving, bound to different SAPs.
549*8b26181fSAndroid Build Coastguard Worker *
550*8b26181fSAndroid Build Coastguard Worker * If the open fails, we just leave -1 in "pd->send_fd" and reject
551*8b26181fSAndroid Build Coastguard Worker * attempts to send packets, just as if, in pcap-bpf.c, we fail
552*8b26181fSAndroid Build Coastguard Worker * to open the BPF device for reading and writing, we just try
553*8b26181fSAndroid Build Coastguard Worker * to open it for reading only and, if that succeeds, just let
554*8b26181fSAndroid Build Coastguard Worker * the send attempts fail.
555*8b26181fSAndroid Build Coastguard Worker */
556*8b26181fSAndroid Build Coastguard Worker pd->send_fd = open("/dev/dlpi", O_RDWR);
557*8b26181fSAndroid Build Coastguard Worker #endif
558*8b26181fSAndroid Build Coastguard Worker
559*8b26181fSAndroid Build Coastguard Worker /*
560*8b26181fSAndroid Build Coastguard Worker ** Attach if "style 2" provider
561*8b26181fSAndroid Build Coastguard Worker */
562*8b26181fSAndroid Build Coastguard Worker if (dlinforeq(p->fd, p->errbuf) < 0 ||
563*8b26181fSAndroid Build Coastguard Worker dlinfoack(p->fd, (char *)buf, p->errbuf) < 0) {
564*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
565*8b26181fSAndroid Build Coastguard Worker goto bad;
566*8b26181fSAndroid Build Coastguard Worker }
567*8b26181fSAndroid Build Coastguard Worker infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack;
568*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SOLARIS
569*8b26181fSAndroid Build Coastguard Worker if (infop->dl_mac_type == DL_IPATM)
570*8b26181fSAndroid Build Coastguard Worker isatm = 1;
571*8b26181fSAndroid Build Coastguard Worker #endif
572*8b26181fSAndroid Build Coastguard Worker if (infop->dl_provider_style == DL_STYLE2) {
573*8b26181fSAndroid Build Coastguard Worker retv = dl_doattach(p->fd, ppa, p->errbuf);
574*8b26181fSAndroid Build Coastguard Worker if (retv < 0) {
575*8b26181fSAndroid Build Coastguard Worker status = retv;
576*8b26181fSAndroid Build Coastguard Worker goto bad;
577*8b26181fSAndroid Build Coastguard Worker }
578*8b26181fSAndroid Build Coastguard Worker #ifdef DL_HP_RAWDLS
579*8b26181fSAndroid Build Coastguard Worker if (pd->send_fd >= 0) {
580*8b26181fSAndroid Build Coastguard Worker retv = dl_doattach(pd->send_fd, ppa, p->errbuf);
581*8b26181fSAndroid Build Coastguard Worker if (retv < 0) {
582*8b26181fSAndroid Build Coastguard Worker status = retv;
583*8b26181fSAndroid Build Coastguard Worker goto bad;
584*8b26181fSAndroid Build Coastguard Worker }
585*8b26181fSAndroid Build Coastguard Worker }
586*8b26181fSAndroid Build Coastguard Worker #endif
587*8b26181fSAndroid Build Coastguard Worker }
588*8b26181fSAndroid Build Coastguard Worker
589*8b26181fSAndroid Build Coastguard Worker if (p->opt.rfmon) {
590*8b26181fSAndroid Build Coastguard Worker /*
591*8b26181fSAndroid Build Coastguard Worker * This device exists, but we don't support monitor mode
592*8b26181fSAndroid Build Coastguard Worker * any platforms that support DLPI.
593*8b26181fSAndroid Build Coastguard Worker */
594*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR_RFMON_NOTSUP;
595*8b26181fSAndroid Build Coastguard Worker goto bad;
596*8b26181fSAndroid Build Coastguard Worker }
597*8b26181fSAndroid Build Coastguard Worker
598*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DL_PASSIVE_REQ_T
599*8b26181fSAndroid Build Coastguard Worker /*
600*8b26181fSAndroid Build Coastguard Worker * Enable Passive mode to be able to capture on aggregated link.
601*8b26181fSAndroid Build Coastguard Worker * Not supported in all Solaris versions.
602*8b26181fSAndroid Build Coastguard Worker */
603*8b26181fSAndroid Build Coastguard Worker dlpassive(p->fd, p->errbuf);
604*8b26181fSAndroid Build Coastguard Worker #endif
605*8b26181fSAndroid Build Coastguard Worker /*
606*8b26181fSAndroid Build Coastguard Worker ** Bind (defer if using HP-UX 9 or HP-UX 10.20 or later, totally
607*8b26181fSAndroid Build Coastguard Worker ** skip if using SINIX)
608*8b26181fSAndroid Build Coastguard Worker */
609*8b26181fSAndroid Build Coastguard Worker #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20_OR_LATER) && !defined(sinix)
610*8b26181fSAndroid Build Coastguard Worker #ifdef _AIX
611*8b26181fSAndroid Build Coastguard Worker /*
612*8b26181fSAndroid Build Coastguard Worker ** AIX.
613*8b26181fSAndroid Build Coastguard Worker ** According to IBM's AIX Support Line, the dl_sap value
614*8b26181fSAndroid Build Coastguard Worker ** should not be less than 0x600 (1536) for standard Ethernet.
615*8b26181fSAndroid Build Coastguard Worker ** However, we seem to get DL_BADADDR - "DLSAP addr in improper
616*8b26181fSAndroid Build Coastguard Worker ** format or invalid" - errors if we use 1537 on the "tr0"
617*8b26181fSAndroid Build Coastguard Worker ** device, which, given that its name starts with "tr" and that
618*8b26181fSAndroid Build Coastguard Worker ** it's IBM, probably means a Token Ring device. (Perhaps we
619*8b26181fSAndroid Build Coastguard Worker ** need to use 1537 on "/dev/dlpi/en" because that device is for
620*8b26181fSAndroid Build Coastguard Worker ** D/I/X Ethernet, the "SAP" is actually an Ethernet type, and
621*8b26181fSAndroid Build Coastguard Worker ** it rejects invalid Ethernet types.)
622*8b26181fSAndroid Build Coastguard Worker **
623*8b26181fSAndroid Build Coastguard Worker ** So if 1537 fails, we try 2, as Hyung Sik Yoon of IBM Korea
624*8b26181fSAndroid Build Coastguard Worker ** says that works on Token Ring (he says that 0 does *not*
625*8b26181fSAndroid Build Coastguard Worker ** work; perhaps that's considered an invalid LLC SAP value - I
626*8b26181fSAndroid Build Coastguard Worker ** assume the SAP value in a DLPI bind is an LLC SAP for network
627*8b26181fSAndroid Build Coastguard Worker ** types that use 802.2 LLC).
628*8b26181fSAndroid Build Coastguard Worker */
629*8b26181fSAndroid Build Coastguard Worker if ((dlbindreq(p->fd, 1537, p->errbuf) < 0 &&
630*8b26181fSAndroid Build Coastguard Worker dlbindreq(p->fd, 2, p->errbuf) < 0) ||
631*8b26181fSAndroid Build Coastguard Worker dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0) {
632*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
633*8b26181fSAndroid Build Coastguard Worker goto bad;
634*8b26181fSAndroid Build Coastguard Worker }
635*8b26181fSAndroid Build Coastguard Worker #elif defined(DL_HP_RAWDLS)
636*8b26181fSAndroid Build Coastguard Worker /*
637*8b26181fSAndroid Build Coastguard Worker ** HP-UX 10.0x and 10.1x.
638*8b26181fSAndroid Build Coastguard Worker */
639*8b26181fSAndroid Build Coastguard Worker if (dl_dohpuxbind(p->fd, p->errbuf) < 0) {
640*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
641*8b26181fSAndroid Build Coastguard Worker goto bad;
642*8b26181fSAndroid Build Coastguard Worker }
643*8b26181fSAndroid Build Coastguard Worker if (pd->send_fd >= 0) {
644*8b26181fSAndroid Build Coastguard Worker /*
645*8b26181fSAndroid Build Coastguard Worker ** XXX - if this fails, just close send_fd and
646*8b26181fSAndroid Build Coastguard Worker ** set it to -1, so that you can't send but can
647*8b26181fSAndroid Build Coastguard Worker ** still receive?
648*8b26181fSAndroid Build Coastguard Worker */
649*8b26181fSAndroid Build Coastguard Worker if (dl_dohpuxbind(pd->send_fd, p->errbuf) < 0) {
650*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
651*8b26181fSAndroid Build Coastguard Worker goto bad;
652*8b26181fSAndroid Build Coastguard Worker }
653*8b26181fSAndroid Build Coastguard Worker }
654*8b26181fSAndroid Build Coastguard Worker #else /* neither AIX nor HP-UX */
655*8b26181fSAndroid Build Coastguard Worker /*
656*8b26181fSAndroid Build Coastguard Worker ** Not Sinix, and neither AIX nor HP-UX - Solaris, and any other
657*8b26181fSAndroid Build Coastguard Worker ** OS using DLPI.
658*8b26181fSAndroid Build Coastguard Worker **/
659*8b26181fSAndroid Build Coastguard Worker if (dlbindreq(p->fd, 0, p->errbuf) < 0 ||
660*8b26181fSAndroid Build Coastguard Worker dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0) {
661*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
662*8b26181fSAndroid Build Coastguard Worker goto bad;
663*8b26181fSAndroid Build Coastguard Worker }
664*8b26181fSAndroid Build Coastguard Worker #endif /* AIX vs. HP-UX vs. other */
665*8b26181fSAndroid Build Coastguard Worker #endif /* !HP-UX 9 and !HP-UX 10.20 or later and !SINIX */
666*8b26181fSAndroid Build Coastguard Worker
667*8b26181fSAndroid Build Coastguard Worker /*
668*8b26181fSAndroid Build Coastguard Worker * Turn a negative snapshot value (invalid), a snapshot value of
669*8b26181fSAndroid Build Coastguard Worker * 0 (unspecified), or a value bigger than the normal maximum
670*8b26181fSAndroid Build Coastguard Worker * value, into the maximum allowed value.
671*8b26181fSAndroid Build Coastguard Worker *
672*8b26181fSAndroid Build Coastguard Worker * If some application really *needs* a bigger snapshot
673*8b26181fSAndroid Build Coastguard Worker * length, we should just increase MAXIMUM_SNAPLEN.
674*8b26181fSAndroid Build Coastguard Worker */
675*8b26181fSAndroid Build Coastguard Worker if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
676*8b26181fSAndroid Build Coastguard Worker p->snapshot = MAXIMUM_SNAPLEN;
677*8b26181fSAndroid Build Coastguard Worker
678*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SOLARIS
679*8b26181fSAndroid Build Coastguard Worker if (isatm) {
680*8b26181fSAndroid Build Coastguard Worker /*
681*8b26181fSAndroid Build Coastguard Worker ** Have to turn on some special ATM promiscuous mode
682*8b26181fSAndroid Build Coastguard Worker ** for SunATM.
683*8b26181fSAndroid Build Coastguard Worker ** Do *NOT* turn regular promiscuous mode on; it doesn't
684*8b26181fSAndroid Build Coastguard Worker ** help, and may break things.
685*8b26181fSAndroid Build Coastguard Worker */
686*8b26181fSAndroid Build Coastguard Worker if (strioctl(p->fd, A_PROMISCON_REQ, 0, NULL) < 0) {
687*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
688*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
689*8b26181fSAndroid Build Coastguard Worker errno, "A_PROMISCON_REQ");
690*8b26181fSAndroid Build Coastguard Worker goto bad;
691*8b26181fSAndroid Build Coastguard Worker }
692*8b26181fSAndroid Build Coastguard Worker } else
693*8b26181fSAndroid Build Coastguard Worker #endif
694*8b26181fSAndroid Build Coastguard Worker if (p->opt.promisc) {
695*8b26181fSAndroid Build Coastguard Worker /*
696*8b26181fSAndroid Build Coastguard Worker ** Enable promiscuous (not necessary on send FD)
697*8b26181fSAndroid Build Coastguard Worker */
698*8b26181fSAndroid Build Coastguard Worker retv = dlpromiscon(p, DL_PROMISC_PHYS);
699*8b26181fSAndroid Build Coastguard Worker if (retv < 0) {
700*8b26181fSAndroid Build Coastguard Worker if (retv == PCAP_ERROR_PERM_DENIED)
701*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR_PROMISC_PERM_DENIED;
702*8b26181fSAndroid Build Coastguard Worker else
703*8b26181fSAndroid Build Coastguard Worker status = retv;
704*8b26181fSAndroid Build Coastguard Worker goto bad;
705*8b26181fSAndroid Build Coastguard Worker }
706*8b26181fSAndroid Build Coastguard Worker
707*8b26181fSAndroid Build Coastguard Worker /*
708*8b26181fSAndroid Build Coastguard Worker ** Try to enable multicast (you would have thought
709*8b26181fSAndroid Build Coastguard Worker ** promiscuous would be sufficient). (Skip if using
710*8b26181fSAndroid Build Coastguard Worker ** HP-UX or SINIX) (Not necessary on send FD)
711*8b26181fSAndroid Build Coastguard Worker */
712*8b26181fSAndroid Build Coastguard Worker #if !defined(__hpux) && !defined(sinix)
713*8b26181fSAndroid Build Coastguard Worker retv = dlpromiscon(p, DL_PROMISC_MULTI);
714*8b26181fSAndroid Build Coastguard Worker if (retv < 0)
715*8b26181fSAndroid Build Coastguard Worker status = PCAP_WARNING;
716*8b26181fSAndroid Build Coastguard Worker #endif
717*8b26181fSAndroid Build Coastguard Worker }
718*8b26181fSAndroid Build Coastguard Worker /*
719*8b26181fSAndroid Build Coastguard Worker ** Try to enable SAP promiscuity (when not in promiscuous mode
720*8b26181fSAndroid Build Coastguard Worker ** when using HP-UX, when not doing SunATM on Solaris, and never
721*8b26181fSAndroid Build Coastguard Worker ** under SINIX) (Not necessary on send FD)
722*8b26181fSAndroid Build Coastguard Worker */
723*8b26181fSAndroid Build Coastguard Worker #ifndef sinix
724*8b26181fSAndroid Build Coastguard Worker #if defined(__hpux)
725*8b26181fSAndroid Build Coastguard Worker /* HP-UX - only do this when not in promiscuous mode */
726*8b26181fSAndroid Build Coastguard Worker if (!p->opt.promisc) {
727*8b26181fSAndroid Build Coastguard Worker #elif defined(HAVE_SOLARIS)
728*8b26181fSAndroid Build Coastguard Worker /* Solaris - don't do this on SunATM devices */
729*8b26181fSAndroid Build Coastguard Worker if (!isatm) {
730*8b26181fSAndroid Build Coastguard Worker #else
731*8b26181fSAndroid Build Coastguard Worker /* Everything else (except for SINIX) - always do this */
732*8b26181fSAndroid Build Coastguard Worker {
733*8b26181fSAndroid Build Coastguard Worker #endif
734*8b26181fSAndroid Build Coastguard Worker retv = dlpromiscon(p, DL_PROMISC_SAP);
735*8b26181fSAndroid Build Coastguard Worker if (retv < 0) {
736*8b26181fSAndroid Build Coastguard Worker if (p->opt.promisc) {
737*8b26181fSAndroid Build Coastguard Worker /*
738*8b26181fSAndroid Build Coastguard Worker * Not fatal, since the DL_PROMISC_PHYS mode
739*8b26181fSAndroid Build Coastguard Worker * worked.
740*8b26181fSAndroid Build Coastguard Worker *
741*8b26181fSAndroid Build Coastguard Worker * Report it as a warning, however.
742*8b26181fSAndroid Build Coastguard Worker */
743*8b26181fSAndroid Build Coastguard Worker status = PCAP_WARNING;
744*8b26181fSAndroid Build Coastguard Worker } else {
745*8b26181fSAndroid Build Coastguard Worker /*
746*8b26181fSAndroid Build Coastguard Worker * Fatal.
747*8b26181fSAndroid Build Coastguard Worker */
748*8b26181fSAndroid Build Coastguard Worker status = retv;
749*8b26181fSAndroid Build Coastguard Worker goto bad;
750*8b26181fSAndroid Build Coastguard Worker }
751*8b26181fSAndroid Build Coastguard Worker }
752*8b26181fSAndroid Build Coastguard Worker }
753*8b26181fSAndroid Build Coastguard Worker #endif /* sinix */
754*8b26181fSAndroid Build Coastguard Worker
755*8b26181fSAndroid Build Coastguard Worker /*
756*8b26181fSAndroid Build Coastguard Worker ** HP-UX 9, and HP-UX 10.20 or later, must bind after setting
757*8b26181fSAndroid Build Coastguard Worker ** promiscuous options.
758*8b26181fSAndroid Build Coastguard Worker */
759*8b26181fSAndroid Build Coastguard Worker #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20_OR_LATER)
760*8b26181fSAndroid Build Coastguard Worker if (dl_dohpuxbind(p->fd, p->errbuf) < 0) {
761*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
762*8b26181fSAndroid Build Coastguard Worker goto bad;
763*8b26181fSAndroid Build Coastguard Worker }
764*8b26181fSAndroid Build Coastguard Worker /*
765*8b26181fSAndroid Build Coastguard Worker ** We don't set promiscuous mode on the send FD, but we'll defer
766*8b26181fSAndroid Build Coastguard Worker ** binding it anyway, just to keep the HP-UX 9/10.20 or later
767*8b26181fSAndroid Build Coastguard Worker ** code together.
768*8b26181fSAndroid Build Coastguard Worker */
769*8b26181fSAndroid Build Coastguard Worker if (pd->send_fd >= 0) {
770*8b26181fSAndroid Build Coastguard Worker /*
771*8b26181fSAndroid Build Coastguard Worker ** XXX - if this fails, just close send_fd and
772*8b26181fSAndroid Build Coastguard Worker ** set it to -1, so that you can't send but can
773*8b26181fSAndroid Build Coastguard Worker ** still receive?
774*8b26181fSAndroid Build Coastguard Worker */
775*8b26181fSAndroid Build Coastguard Worker if (dl_dohpuxbind(pd->send_fd, p->errbuf) < 0) {
776*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
777*8b26181fSAndroid Build Coastguard Worker goto bad;
778*8b26181fSAndroid Build Coastguard Worker }
779*8b26181fSAndroid Build Coastguard Worker }
780*8b26181fSAndroid Build Coastguard Worker #endif
781*8b26181fSAndroid Build Coastguard Worker
782*8b26181fSAndroid Build Coastguard Worker /*
783*8b26181fSAndroid Build Coastguard Worker ** Determine link type
784*8b26181fSAndroid Build Coastguard Worker ** XXX - get SAP length and address length as well, for use
785*8b26181fSAndroid Build Coastguard Worker ** when sending packets.
786*8b26181fSAndroid Build Coastguard Worker */
787*8b26181fSAndroid Build Coastguard Worker if (dlinforeq(p->fd, p->errbuf) < 0 ||
788*8b26181fSAndroid Build Coastguard Worker dlinfoack(p->fd, (char *)buf, p->errbuf) < 0) {
789*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
790*8b26181fSAndroid Build Coastguard Worker goto bad;
791*8b26181fSAndroid Build Coastguard Worker }
792*8b26181fSAndroid Build Coastguard Worker
793*8b26181fSAndroid Build Coastguard Worker infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack;
794*8b26181fSAndroid Build Coastguard Worker if (pcap_process_mactype(p, infop->dl_mac_type) != 0) {
795*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
796*8b26181fSAndroid Build Coastguard Worker goto bad;
797*8b26181fSAndroid Build Coastguard Worker }
798*8b26181fSAndroid Build Coastguard Worker
799*8b26181fSAndroid Build Coastguard Worker #ifdef DLIOCRAW
800*8b26181fSAndroid Build Coastguard Worker /*
801*8b26181fSAndroid Build Coastguard Worker ** This is a non standard SunOS hack to get the full raw link-layer
802*8b26181fSAndroid Build Coastguard Worker ** header.
803*8b26181fSAndroid Build Coastguard Worker */
804*8b26181fSAndroid Build Coastguard Worker if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) {
805*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
806*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
807*8b26181fSAndroid Build Coastguard Worker errno, "DLIOCRAW");
808*8b26181fSAndroid Build Coastguard Worker goto bad;
809*8b26181fSAndroid Build Coastguard Worker }
810*8b26181fSAndroid Build Coastguard Worker #endif
811*8b26181fSAndroid Build Coastguard Worker
812*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_BUFMOD_H
813*8b26181fSAndroid Build Coastguard Worker ss = p->snapshot;
814*8b26181fSAndroid Build Coastguard Worker
815*8b26181fSAndroid Build Coastguard Worker /*
816*8b26181fSAndroid Build Coastguard Worker ** There is a bug in bufmod(7). When dealing with messages of
817*8b26181fSAndroid Build Coastguard Worker ** less than snaplen size it strips data from the beginning not
818*8b26181fSAndroid Build Coastguard Worker ** the end.
819*8b26181fSAndroid Build Coastguard Worker **
820*8b26181fSAndroid Build Coastguard Worker ** This bug is fixed in 5.3.2. Also, there is a patch available.
821*8b26181fSAndroid Build Coastguard Worker ** Ask for bugid 1149065.
822*8b26181fSAndroid Build Coastguard Worker */
823*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SOLARIS
824*8b26181fSAndroid Build Coastguard Worker get_release(release, sizeof (release), &osmajor, &osminor, &osmicro);
825*8b26181fSAndroid Build Coastguard Worker if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) &&
826*8b26181fSAndroid Build Coastguard Worker getenv("BUFMOD_FIXED") == NULL) {
827*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
828*8b26181fSAndroid Build Coastguard Worker "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.",
829*8b26181fSAndroid Build Coastguard Worker release);
830*8b26181fSAndroid Build Coastguard Worker ss = 0;
831*8b26181fSAndroid Build Coastguard Worker status = PCAP_WARNING;
832*8b26181fSAndroid Build Coastguard Worker }
833*8b26181fSAndroid Build Coastguard Worker #endif
834*8b26181fSAndroid Build Coastguard Worker
835*8b26181fSAndroid Build Coastguard Worker /* Push and configure bufmod. */
836*8b26181fSAndroid Build Coastguard Worker if (pcap_conf_bufmod(p, ss) != 0) {
837*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
838*8b26181fSAndroid Build Coastguard Worker goto bad;
839*8b26181fSAndroid Build Coastguard Worker }
840*8b26181fSAndroid Build Coastguard Worker #endif
841*8b26181fSAndroid Build Coastguard Worker
842*8b26181fSAndroid Build Coastguard Worker /*
843*8b26181fSAndroid Build Coastguard Worker ** As the last operation flush the read side.
844*8b26181fSAndroid Build Coastguard Worker */
845*8b26181fSAndroid Build Coastguard Worker if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
846*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
847*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
848*8b26181fSAndroid Build Coastguard Worker errno, "FLUSHR");
849*8b26181fSAndroid Build Coastguard Worker goto bad;
850*8b26181fSAndroid Build Coastguard Worker }
851*8b26181fSAndroid Build Coastguard Worker
852*8b26181fSAndroid Build Coastguard Worker /* Allocate data buffer. */
853*8b26181fSAndroid Build Coastguard Worker if (pcap_alloc_databuf(p) != 0) {
854*8b26181fSAndroid Build Coastguard Worker status = PCAP_ERROR;
855*8b26181fSAndroid Build Coastguard Worker goto bad;
856*8b26181fSAndroid Build Coastguard Worker }
857*8b26181fSAndroid Build Coastguard Worker
858*8b26181fSAndroid Build Coastguard Worker /*
859*8b26181fSAndroid Build Coastguard Worker * Success.
860*8b26181fSAndroid Build Coastguard Worker *
861*8b26181fSAndroid Build Coastguard Worker * "p->fd" is an FD for a STREAMS device, so "select()" and
862*8b26181fSAndroid Build Coastguard Worker * "poll()" should work on it.
863*8b26181fSAndroid Build Coastguard Worker */
864*8b26181fSAndroid Build Coastguard Worker p->selectable_fd = p->fd;
865*8b26181fSAndroid Build Coastguard Worker
866*8b26181fSAndroid Build Coastguard Worker p->read_op = pcap_read_dlpi;
867*8b26181fSAndroid Build Coastguard Worker p->inject_op = pcap_inject_dlpi;
868*8b26181fSAndroid Build Coastguard Worker p->setfilter_op = install_bpf_program; /* no kernel filtering */
869*8b26181fSAndroid Build Coastguard Worker p->setdirection_op = NULL; /* Not implemented.*/
870*8b26181fSAndroid Build Coastguard Worker p->set_datalink_op = NULL; /* can't change data link type */
871*8b26181fSAndroid Build Coastguard Worker p->getnonblock_op = pcap_getnonblock_fd;
872*8b26181fSAndroid Build Coastguard Worker p->setnonblock_op = pcap_setnonblock_fd;
873*8b26181fSAndroid Build Coastguard Worker p->stats_op = pcap_stats_dlpi;
874*8b26181fSAndroid Build Coastguard Worker p->cleanup_op = pcap_cleanup_dlpi;
875*8b26181fSAndroid Build Coastguard Worker
876*8b26181fSAndroid Build Coastguard Worker return (status);
877*8b26181fSAndroid Build Coastguard Worker bad:
878*8b26181fSAndroid Build Coastguard Worker pcap_cleanup_dlpi(p);
879*8b26181fSAndroid Build Coastguard Worker return (status);
880*8b26181fSAndroid Build Coastguard Worker }
881*8b26181fSAndroid Build Coastguard Worker
882*8b26181fSAndroid Build Coastguard Worker /*
883*8b26181fSAndroid Build Coastguard Worker * Split a device name into a device type name and a unit number;
884*8b26181fSAndroid Build Coastguard Worker * return the a pointer to the beginning of the unit number, which
885*8b26181fSAndroid Build Coastguard Worker * is the end of the device type name, and set "*unitp" to the unit
886*8b26181fSAndroid Build Coastguard Worker * number.
887*8b26181fSAndroid Build Coastguard Worker *
888*8b26181fSAndroid Build Coastguard Worker * Returns NULL on error, and fills "ebuf" with an error message.
889*8b26181fSAndroid Build Coastguard Worker */
890*8b26181fSAndroid Build Coastguard Worker static char *
891*8b26181fSAndroid Build Coastguard Worker split_dname(char *device, u_int *unitp, char *ebuf)
892*8b26181fSAndroid Build Coastguard Worker {
893*8b26181fSAndroid Build Coastguard Worker char *cp;
894*8b26181fSAndroid Build Coastguard Worker char *eos;
895*8b26181fSAndroid Build Coastguard Worker long unit;
896*8b26181fSAndroid Build Coastguard Worker
897*8b26181fSAndroid Build Coastguard Worker /*
898*8b26181fSAndroid Build Coastguard Worker * Look for a number at the end of the device name string.
899*8b26181fSAndroid Build Coastguard Worker */
900*8b26181fSAndroid Build Coastguard Worker cp = device + strlen(device) - 1;
901*8b26181fSAndroid Build Coastguard Worker if (*cp < '0' || *cp > '9') {
902*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s missing unit number",
903*8b26181fSAndroid Build Coastguard Worker device);
904*8b26181fSAndroid Build Coastguard Worker return (NULL);
905*8b26181fSAndroid Build Coastguard Worker }
906*8b26181fSAndroid Build Coastguard Worker
907*8b26181fSAndroid Build Coastguard Worker /* Digits at end of string are unit number */
908*8b26181fSAndroid Build Coastguard Worker while (cp-1 >= device && *(cp-1) >= '0' && *(cp-1) <= '9')
909*8b26181fSAndroid Build Coastguard Worker cp--;
910*8b26181fSAndroid Build Coastguard Worker
911*8b26181fSAndroid Build Coastguard Worker errno = 0;
912*8b26181fSAndroid Build Coastguard Worker unit = strtol(cp, &eos, 10);
913*8b26181fSAndroid Build Coastguard Worker if (*eos != '\0') {
914*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s bad unit number", device);
915*8b26181fSAndroid Build Coastguard Worker return (NULL);
916*8b26181fSAndroid Build Coastguard Worker }
917*8b26181fSAndroid Build Coastguard Worker if (errno == ERANGE || unit > INT_MAX) {
918*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number too large",
919*8b26181fSAndroid Build Coastguard Worker device);
920*8b26181fSAndroid Build Coastguard Worker return (NULL);
921*8b26181fSAndroid Build Coastguard Worker }
922*8b26181fSAndroid Build Coastguard Worker if (unit < 0) {
923*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number is negative",
924*8b26181fSAndroid Build Coastguard Worker device);
925*8b26181fSAndroid Build Coastguard Worker return (NULL);
926*8b26181fSAndroid Build Coastguard Worker }
927*8b26181fSAndroid Build Coastguard Worker *unitp = (u_int)unit;
928*8b26181fSAndroid Build Coastguard Worker return (cp);
929*8b26181fSAndroid Build Coastguard Worker }
930*8b26181fSAndroid Build Coastguard Worker
931*8b26181fSAndroid Build Coastguard Worker static int
932*8b26181fSAndroid Build Coastguard Worker dl_doattach(int fd, int ppa, char *ebuf)
933*8b26181fSAndroid Build Coastguard Worker {
934*8b26181fSAndroid Build Coastguard Worker dl_attach_req_t req;
935*8b26181fSAndroid Build Coastguard Worker bpf_u_int32 buf[MAXDLBUF];
936*8b26181fSAndroid Build Coastguard Worker int err;
937*8b26181fSAndroid Build Coastguard Worker
938*8b26181fSAndroid Build Coastguard Worker req.dl_primitive = DL_ATTACH_REQ;
939*8b26181fSAndroid Build Coastguard Worker req.dl_ppa = ppa;
940*8b26181fSAndroid Build Coastguard Worker if (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf) < 0)
941*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
942*8b26181fSAndroid Build Coastguard Worker
943*8b26181fSAndroid Build Coastguard Worker err = dlokack(fd, "attach", (char *)buf, ebuf, NULL);
944*8b26181fSAndroid Build Coastguard Worker if (err < 0)
945*8b26181fSAndroid Build Coastguard Worker return (err);
946*8b26181fSAndroid Build Coastguard Worker return (0);
947*8b26181fSAndroid Build Coastguard Worker }
948*8b26181fSAndroid Build Coastguard Worker
949*8b26181fSAndroid Build Coastguard Worker #ifdef DL_HP_RAWDLS
950*8b26181fSAndroid Build Coastguard Worker static int
951*8b26181fSAndroid Build Coastguard Worker dl_dohpuxbind(int fd, char *ebuf)
952*8b26181fSAndroid Build Coastguard Worker {
953*8b26181fSAndroid Build Coastguard Worker int hpsap;
954*8b26181fSAndroid Build Coastguard Worker int uerror;
955*8b26181fSAndroid Build Coastguard Worker bpf_u_int32 buf[MAXDLBUF];
956*8b26181fSAndroid Build Coastguard Worker
957*8b26181fSAndroid Build Coastguard Worker /*
958*8b26181fSAndroid Build Coastguard Worker * XXX - we start at 22 because we used to use only 22, but
959*8b26181fSAndroid Build Coastguard Worker * that was just because that was the value used in some
960*8b26181fSAndroid Build Coastguard Worker * sample code from HP. With what value *should* we start?
961*8b26181fSAndroid Build Coastguard Worker * Does it matter, given that we're enabling SAP promiscuity
962*8b26181fSAndroid Build Coastguard Worker * on the input FD?
963*8b26181fSAndroid Build Coastguard Worker */
964*8b26181fSAndroid Build Coastguard Worker hpsap = 22;
965*8b26181fSAndroid Build Coastguard Worker for (;;) {
966*8b26181fSAndroid Build Coastguard Worker if (dlbindreq(fd, hpsap, ebuf) < 0)
967*8b26181fSAndroid Build Coastguard Worker return (-1);
968*8b26181fSAndroid Build Coastguard Worker if (dlbindack(fd, (char *)buf, ebuf, &uerror) >= 0)
969*8b26181fSAndroid Build Coastguard Worker break;
970*8b26181fSAndroid Build Coastguard Worker /*
971*8b26181fSAndroid Build Coastguard Worker * For any error other than a UNIX EBUSY, give up.
972*8b26181fSAndroid Build Coastguard Worker */
973*8b26181fSAndroid Build Coastguard Worker if (uerror != EBUSY) {
974*8b26181fSAndroid Build Coastguard Worker /*
975*8b26181fSAndroid Build Coastguard Worker * dlbindack() has already filled in ebuf for
976*8b26181fSAndroid Build Coastguard Worker * this error.
977*8b26181fSAndroid Build Coastguard Worker */
978*8b26181fSAndroid Build Coastguard Worker return (-1);
979*8b26181fSAndroid Build Coastguard Worker }
980*8b26181fSAndroid Build Coastguard Worker
981*8b26181fSAndroid Build Coastguard Worker /*
982*8b26181fSAndroid Build Coastguard Worker * For EBUSY, try the next SAP value; that means that
983*8b26181fSAndroid Build Coastguard Worker * somebody else is using that SAP. Clear ebuf so
984*8b26181fSAndroid Build Coastguard Worker * that application doesn't report the "Device busy"
985*8b26181fSAndroid Build Coastguard Worker * error as a warning.
986*8b26181fSAndroid Build Coastguard Worker */
987*8b26181fSAndroid Build Coastguard Worker *ebuf = '\0';
988*8b26181fSAndroid Build Coastguard Worker hpsap++;
989*8b26181fSAndroid Build Coastguard Worker if (hpsap > 100) {
990*8b26181fSAndroid Build Coastguard Worker pcap_strlcpy(ebuf,
991*8b26181fSAndroid Build Coastguard Worker "All SAPs from 22 through 100 are in use",
992*8b26181fSAndroid Build Coastguard Worker PCAP_ERRBUF_SIZE);
993*8b26181fSAndroid Build Coastguard Worker return (-1);
994*8b26181fSAndroid Build Coastguard Worker }
995*8b26181fSAndroid Build Coastguard Worker }
996*8b26181fSAndroid Build Coastguard Worker return (0);
997*8b26181fSAndroid Build Coastguard Worker }
998*8b26181fSAndroid Build Coastguard Worker #endif
999*8b26181fSAndroid Build Coastguard Worker
1000*8b26181fSAndroid Build Coastguard Worker #define STRINGIFY(n) #n
1001*8b26181fSAndroid Build Coastguard Worker
1002*8b26181fSAndroid Build Coastguard Worker static int
1003*8b26181fSAndroid Build Coastguard Worker dlpromiscon(pcap_t *p, bpf_u_int32 level)
1004*8b26181fSAndroid Build Coastguard Worker {
1005*8b26181fSAndroid Build Coastguard Worker dl_promiscon_req_t req;
1006*8b26181fSAndroid Build Coastguard Worker bpf_u_int32 buf[MAXDLBUF];
1007*8b26181fSAndroid Build Coastguard Worker int err;
1008*8b26181fSAndroid Build Coastguard Worker int uerror;
1009*8b26181fSAndroid Build Coastguard Worker
1010*8b26181fSAndroid Build Coastguard Worker req.dl_primitive = DL_PROMISCON_REQ;
1011*8b26181fSAndroid Build Coastguard Worker req.dl_level = level;
1012*8b26181fSAndroid Build Coastguard Worker if (send_request(p->fd, (char *)&req, sizeof(req), "promiscon",
1013*8b26181fSAndroid Build Coastguard Worker p->errbuf) < 0)
1014*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1015*8b26181fSAndroid Build Coastguard Worker err = dlokack(p->fd, "promiscon" STRINGIFY(level), (char *)buf,
1016*8b26181fSAndroid Build Coastguard Worker p->errbuf, &uerror);
1017*8b26181fSAndroid Build Coastguard Worker if (err < 0) {
1018*8b26181fSAndroid Build Coastguard Worker if (err == PCAP_ERROR_PERM_DENIED) {
1019*8b26181fSAndroid Build Coastguard Worker snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1020*8b26181fSAndroid Build Coastguard Worker "Attempt to set promiscuous mode failed with %s - root privilege may be required",
1021*8b26181fSAndroid Build Coastguard Worker (uerror == EPERM) ? "EPERM" : "EACCES");
1022*8b26181fSAndroid Build Coastguard Worker err = PCAP_ERROR_PROMISC_PERM_DENIED;
1023*8b26181fSAndroid Build Coastguard Worker }
1024*8b26181fSAndroid Build Coastguard Worker return (err);
1025*8b26181fSAndroid Build Coastguard Worker }
1026*8b26181fSAndroid Build Coastguard Worker return (0);
1027*8b26181fSAndroid Build Coastguard Worker }
1028*8b26181fSAndroid Build Coastguard Worker
1029*8b26181fSAndroid Build Coastguard Worker /*
1030*8b26181fSAndroid Build Coastguard Worker * Not all interfaces are DLPI interfaces, and thus not all interfaces
1031*8b26181fSAndroid Build Coastguard Worker * can be opened with DLPI (for example, the loopback interface is not
1032*8b26181fSAndroid Build Coastguard Worker * a DLPI interface on Solaris prior to Solaris 11), so try to open
1033*8b26181fSAndroid Build Coastguard Worker * the specified interface; return 0 if we fail with PCAP_ERROR_NO_SUCH_DEVICE
1034*8b26181fSAndroid Build Coastguard Worker * and 1 otherwise.
1035*8b26181fSAndroid Build Coastguard Worker */
1036*8b26181fSAndroid Build Coastguard Worker static int
1037*8b26181fSAndroid Build Coastguard Worker is_dlpi_interface(const char *name)
1038*8b26181fSAndroid Build Coastguard Worker {
1039*8b26181fSAndroid Build Coastguard Worker int fd;
1040*8b26181fSAndroid Build Coastguard Worker u_int ppa;
1041*8b26181fSAndroid Build Coastguard Worker char errbuf[PCAP_ERRBUF_SIZE];
1042*8b26181fSAndroid Build Coastguard Worker
1043*8b26181fSAndroid Build Coastguard Worker fd = open_dlpi_device(name, &ppa, errbuf);
1044*8b26181fSAndroid Build Coastguard Worker if (fd < 0) {
1045*8b26181fSAndroid Build Coastguard Worker /*
1046*8b26181fSAndroid Build Coastguard Worker * Error - was it PCAP_ERROR_NO_SUCH_DEVICE?
1047*8b26181fSAndroid Build Coastguard Worker */
1048*8b26181fSAndroid Build Coastguard Worker if (fd == PCAP_ERROR_NO_SUCH_DEVICE) {
1049*8b26181fSAndroid Build Coastguard Worker /*
1050*8b26181fSAndroid Build Coastguard Worker * Yes, so we can't open this because it's
1051*8b26181fSAndroid Build Coastguard Worker * not a DLPI interface.
1052*8b26181fSAndroid Build Coastguard Worker */
1053*8b26181fSAndroid Build Coastguard Worker return (0);
1054*8b26181fSAndroid Build Coastguard Worker }
1055*8b26181fSAndroid Build Coastguard Worker /*
1056*8b26181fSAndroid Build Coastguard Worker * No, so, in the case where there's a single DLPI
1057*8b26181fSAndroid Build Coastguard Worker * device for all interfaces of this type ("style
1058*8b26181fSAndroid Build Coastguard Worker * 2" providers?), we don't know whether it's a DLPI
1059*8b26181fSAndroid Build Coastguard Worker * interface or not, as we didn't try an attach.
1060*8b26181fSAndroid Build Coastguard Worker * Say it is a DLPI device, so that the user can at
1061*8b26181fSAndroid Build Coastguard Worker * least try to open it and report the error (which
1062*8b26181fSAndroid Build Coastguard Worker * is probably "you don't have permission to open that
1063*8b26181fSAndroid Build Coastguard Worker * DLPI device"; reporting those interfaces means
1064*8b26181fSAndroid Build Coastguard Worker * users will ask "why am I getting a permissions error
1065*8b26181fSAndroid Build Coastguard Worker * when I try to capture" rather than "why am I not
1066*8b26181fSAndroid Build Coastguard Worker * seeing any interfaces", making the underlying problem
1067*8b26181fSAndroid Build Coastguard Worker * clearer).
1068*8b26181fSAndroid Build Coastguard Worker */
1069*8b26181fSAndroid Build Coastguard Worker return (1);
1070*8b26181fSAndroid Build Coastguard Worker }
1071*8b26181fSAndroid Build Coastguard Worker
1072*8b26181fSAndroid Build Coastguard Worker /*
1073*8b26181fSAndroid Build Coastguard Worker * Success.
1074*8b26181fSAndroid Build Coastguard Worker */
1075*8b26181fSAndroid Build Coastguard Worker close(fd);
1076*8b26181fSAndroid Build Coastguard Worker return (1);
1077*8b26181fSAndroid Build Coastguard Worker }
1078*8b26181fSAndroid Build Coastguard Worker
1079*8b26181fSAndroid Build Coastguard Worker static int
1080*8b26181fSAndroid Build Coastguard Worker get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
1081*8b26181fSAndroid Build Coastguard Worker {
1082*8b26181fSAndroid Build Coastguard Worker /*
1083*8b26181fSAndroid Build Coastguard Worker * Nothing we can do other than mark loopback devices as "the
1084*8b26181fSAndroid Build Coastguard Worker * connected/disconnected status doesn't apply".
1085*8b26181fSAndroid Build Coastguard Worker *
1086*8b26181fSAndroid Build Coastguard Worker * XXX - on Solaris, can we do what the dladm command does,
1087*8b26181fSAndroid Build Coastguard Worker * i.e. get a connected/disconnected indication from a kstat?
1088*8b26181fSAndroid Build Coastguard Worker * (Note that you can also get the link speed, and possibly
1089*8b26181fSAndroid Build Coastguard Worker * other information, from a kstat as well.)
1090*8b26181fSAndroid Build Coastguard Worker */
1091*8b26181fSAndroid Build Coastguard Worker if (*flags & PCAP_IF_LOOPBACK) {
1092*8b26181fSAndroid Build Coastguard Worker /*
1093*8b26181fSAndroid Build Coastguard Worker * Loopback devices aren't wireless, and "connected"/
1094*8b26181fSAndroid Build Coastguard Worker * "disconnected" doesn't apply to them.
1095*8b26181fSAndroid Build Coastguard Worker */
1096*8b26181fSAndroid Build Coastguard Worker *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
1097*8b26181fSAndroid Build Coastguard Worker return (0);
1098*8b26181fSAndroid Build Coastguard Worker }
1099*8b26181fSAndroid Build Coastguard Worker return (0);
1100*8b26181fSAndroid Build Coastguard Worker }
1101*8b26181fSAndroid Build Coastguard Worker
1102*8b26181fSAndroid Build Coastguard Worker int
1103*8b26181fSAndroid Build Coastguard Worker pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
1104*8b26181fSAndroid Build Coastguard Worker {
1105*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SOLARIS
1106*8b26181fSAndroid Build Coastguard Worker int fd;
1107*8b26181fSAndroid Build Coastguard Worker union {
1108*8b26181fSAndroid Build Coastguard Worker u_int nunits;
1109*8b26181fSAndroid Build Coastguard Worker char pad[516]; /* XXX - must be at least 513; is 516
1110*8b26181fSAndroid Build Coastguard Worker in "atmgetunits" */
1111*8b26181fSAndroid Build Coastguard Worker } buf;
1112*8b26181fSAndroid Build Coastguard Worker char baname[2+1+1];
1113*8b26181fSAndroid Build Coastguard Worker u_int i;
1114*8b26181fSAndroid Build Coastguard Worker #endif
1115*8b26181fSAndroid Build Coastguard Worker
1116*8b26181fSAndroid Build Coastguard Worker /*
1117*8b26181fSAndroid Build Coastguard Worker * Get the list of regular interfaces first.
1118*8b26181fSAndroid Build Coastguard Worker */
1119*8b26181fSAndroid Build Coastguard Worker if (pcap_findalldevs_interfaces(devlistp, errbuf, is_dlpi_interface,
1120*8b26181fSAndroid Build Coastguard Worker get_if_flags) == -1)
1121*8b26181fSAndroid Build Coastguard Worker return (-1); /* failure */
1122*8b26181fSAndroid Build Coastguard Worker
1123*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SOLARIS
1124*8b26181fSAndroid Build Coastguard Worker /*
1125*8b26181fSAndroid Build Coastguard Worker * We may have to do special magic to get ATM devices.
1126*8b26181fSAndroid Build Coastguard Worker */
1127*8b26181fSAndroid Build Coastguard Worker if ((fd = open("/dev/ba", O_RDWR)) < 0) {
1128*8b26181fSAndroid Build Coastguard Worker /*
1129*8b26181fSAndroid Build Coastguard Worker * We couldn't open the "ba" device.
1130*8b26181fSAndroid Build Coastguard Worker * For now, just give up; perhaps we should
1131*8b26181fSAndroid Build Coastguard Worker * return an error if the problem is neither
1132*8b26181fSAndroid Build Coastguard Worker * a "that device doesn't exist" error (ENOENT,
1133*8b26181fSAndroid Build Coastguard Worker * ENXIO, etc.) or a "you're not allowed to do
1134*8b26181fSAndroid Build Coastguard Worker * that" error (EPERM, EACCES).
1135*8b26181fSAndroid Build Coastguard Worker */
1136*8b26181fSAndroid Build Coastguard Worker return (0);
1137*8b26181fSAndroid Build Coastguard Worker }
1138*8b26181fSAndroid Build Coastguard Worker
1139*8b26181fSAndroid Build Coastguard Worker if (strioctl(fd, A_GET_UNITS, sizeof(buf), (char *)&buf) < 0) {
1140*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1141*8b26181fSAndroid Build Coastguard Worker errno, "A_GET_UNITS");
1142*8b26181fSAndroid Build Coastguard Worker return (-1);
1143*8b26181fSAndroid Build Coastguard Worker }
1144*8b26181fSAndroid Build Coastguard Worker for (i = 0; i < buf.nunits; i++) {
1145*8b26181fSAndroid Build Coastguard Worker snprintf(baname, sizeof baname, "ba%u", i);
1146*8b26181fSAndroid Build Coastguard Worker /*
1147*8b26181fSAndroid Build Coastguard Worker * XXX - is there a notion of "up" and "running"?
1148*8b26181fSAndroid Build Coastguard Worker * And is there a way to determine whether the
1149*8b26181fSAndroid Build Coastguard Worker * interface is plugged into a network?
1150*8b26181fSAndroid Build Coastguard Worker */
1151*8b26181fSAndroid Build Coastguard Worker if (add_dev(devlistp, baname, 0, NULL, errbuf) == NULL)
1152*8b26181fSAndroid Build Coastguard Worker return (-1);
1153*8b26181fSAndroid Build Coastguard Worker }
1154*8b26181fSAndroid Build Coastguard Worker #endif
1155*8b26181fSAndroid Build Coastguard Worker
1156*8b26181fSAndroid Build Coastguard Worker return (0);
1157*8b26181fSAndroid Build Coastguard Worker }
1158*8b26181fSAndroid Build Coastguard Worker
1159*8b26181fSAndroid Build Coastguard Worker static int
1160*8b26181fSAndroid Build Coastguard Worker send_request(int fd, char *ptr, int len, char *what, char *ebuf)
1161*8b26181fSAndroid Build Coastguard Worker {
1162*8b26181fSAndroid Build Coastguard Worker struct strbuf ctl;
1163*8b26181fSAndroid Build Coastguard Worker int flags;
1164*8b26181fSAndroid Build Coastguard Worker
1165*8b26181fSAndroid Build Coastguard Worker ctl.maxlen = 0;
1166*8b26181fSAndroid Build Coastguard Worker ctl.len = len;
1167*8b26181fSAndroid Build Coastguard Worker ctl.buf = ptr;
1168*8b26181fSAndroid Build Coastguard Worker
1169*8b26181fSAndroid Build Coastguard Worker flags = 0;
1170*8b26181fSAndroid Build Coastguard Worker if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) {
1171*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1172*8b26181fSAndroid Build Coastguard Worker errno, "send_request: putmsg \"%s\"", what);
1173*8b26181fSAndroid Build Coastguard Worker return (-1);
1174*8b26181fSAndroid Build Coastguard Worker }
1175*8b26181fSAndroid Build Coastguard Worker return (0);
1176*8b26181fSAndroid Build Coastguard Worker }
1177*8b26181fSAndroid Build Coastguard Worker
1178*8b26181fSAndroid Build Coastguard Worker static int
1179*8b26181fSAndroid Build Coastguard Worker recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf, int *uerror)
1180*8b26181fSAndroid Build Coastguard Worker {
1181*8b26181fSAndroid Build Coastguard Worker union DL_primitives *dlp;
1182*8b26181fSAndroid Build Coastguard Worker struct strbuf ctl;
1183*8b26181fSAndroid Build Coastguard Worker int flags;
1184*8b26181fSAndroid Build Coastguard Worker char errmsgbuf[PCAP_ERRBUF_SIZE];
1185*8b26181fSAndroid Build Coastguard Worker char dlprimbuf[64];
1186*8b26181fSAndroid Build Coastguard Worker
1187*8b26181fSAndroid Build Coastguard Worker /*
1188*8b26181fSAndroid Build Coastguard Worker * Clear out "*uerror", so it's only set for DL_ERROR_ACK/DL_SYSERR,
1189*8b26181fSAndroid Build Coastguard Worker * making that the only place where EBUSY is treated specially.
1190*8b26181fSAndroid Build Coastguard Worker */
1191*8b26181fSAndroid Build Coastguard Worker if (uerror != NULL)
1192*8b26181fSAndroid Build Coastguard Worker *uerror = 0;
1193*8b26181fSAndroid Build Coastguard Worker
1194*8b26181fSAndroid Build Coastguard Worker ctl.maxlen = MAXDLBUF;
1195*8b26181fSAndroid Build Coastguard Worker ctl.len = 0;
1196*8b26181fSAndroid Build Coastguard Worker ctl.buf = bufp;
1197*8b26181fSAndroid Build Coastguard Worker
1198*8b26181fSAndroid Build Coastguard Worker flags = 0;
1199*8b26181fSAndroid Build Coastguard Worker if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) {
1200*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1201*8b26181fSAndroid Build Coastguard Worker errno, "recv_ack: %s getmsg", what);
1202*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1203*8b26181fSAndroid Build Coastguard Worker }
1204*8b26181fSAndroid Build Coastguard Worker
1205*8b26181fSAndroid Build Coastguard Worker dlp = MAKE_DL_PRIMITIVES(ctl.buf);
1206*8b26181fSAndroid Build Coastguard Worker switch (dlp->dl_primitive) {
1207*8b26181fSAndroid Build Coastguard Worker
1208*8b26181fSAndroid Build Coastguard Worker case DL_INFO_ACK:
1209*8b26181fSAndroid Build Coastguard Worker case DL_BIND_ACK:
1210*8b26181fSAndroid Build Coastguard Worker case DL_OK_ACK:
1211*8b26181fSAndroid Build Coastguard Worker #ifdef DL_HP_PPA_ACK
1212*8b26181fSAndroid Build Coastguard Worker case DL_HP_PPA_ACK:
1213*8b26181fSAndroid Build Coastguard Worker #endif
1214*8b26181fSAndroid Build Coastguard Worker /* These are OK */
1215*8b26181fSAndroid Build Coastguard Worker break;
1216*8b26181fSAndroid Build Coastguard Worker
1217*8b26181fSAndroid Build Coastguard Worker case DL_ERROR_ACK:
1218*8b26181fSAndroid Build Coastguard Worker switch (dlp->error_ack.dl_errno) {
1219*8b26181fSAndroid Build Coastguard Worker
1220*8b26181fSAndroid Build Coastguard Worker case DL_SYSERR:
1221*8b26181fSAndroid Build Coastguard Worker if (uerror != NULL)
1222*8b26181fSAndroid Build Coastguard Worker *uerror = dlp->error_ack.dl_unix_errno;
1223*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1224*8b26181fSAndroid Build Coastguard Worker dlp->error_ack.dl_unix_errno,
1225*8b26181fSAndroid Build Coastguard Worker "recv_ack: %s: UNIX error", what);
1226*8b26181fSAndroid Build Coastguard Worker if (dlp->error_ack.dl_unix_errno == EPERM ||
1227*8b26181fSAndroid Build Coastguard Worker dlp->error_ack.dl_unix_errno == EACCES)
1228*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR_PERM_DENIED);
1229*8b26181fSAndroid Build Coastguard Worker break;
1230*8b26181fSAndroid Build Coastguard Worker
1231*8b26181fSAndroid Build Coastguard Worker default:
1232*8b26181fSAndroid Build Coastguard Worker /*
1233*8b26181fSAndroid Build Coastguard Worker * Neither EPERM nor EACCES.
1234*8b26181fSAndroid Build Coastguard Worker */
1235*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE,
1236*8b26181fSAndroid Build Coastguard Worker "recv_ack: %s: %s", what,
1237*8b26181fSAndroid Build Coastguard Worker dlstrerror(errmsgbuf, sizeof (errmsgbuf), dlp->error_ack.dl_errno));
1238*8b26181fSAndroid Build Coastguard Worker if (dlp->error_ack.dl_errno == DL_BADPPA)
1239*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR_NO_SUCH_DEVICE);
1240*8b26181fSAndroid Build Coastguard Worker else if (dlp->error_ack.dl_errno == DL_ACCESS)
1241*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR_PERM_DENIED);
1242*8b26181fSAndroid Build Coastguard Worker break;
1243*8b26181fSAndroid Build Coastguard Worker }
1244*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1245*8b26181fSAndroid Build Coastguard Worker
1246*8b26181fSAndroid Build Coastguard Worker default:
1247*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE,
1248*8b26181fSAndroid Build Coastguard Worker "recv_ack: %s: Unexpected primitive ack %s",
1249*8b26181fSAndroid Build Coastguard Worker what, dlprim(dlprimbuf, sizeof (dlprimbuf), dlp->dl_primitive));
1250*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1251*8b26181fSAndroid Build Coastguard Worker }
1252*8b26181fSAndroid Build Coastguard Worker
1253*8b26181fSAndroid Build Coastguard Worker if (ctl.len < size) {
1254*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE,
1255*8b26181fSAndroid Build Coastguard Worker "recv_ack: %s: Ack too small (%d < %d)",
1256*8b26181fSAndroid Build Coastguard Worker what, ctl.len, size);
1257*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1258*8b26181fSAndroid Build Coastguard Worker }
1259*8b26181fSAndroid Build Coastguard Worker return (ctl.len);
1260*8b26181fSAndroid Build Coastguard Worker }
1261*8b26181fSAndroid Build Coastguard Worker
1262*8b26181fSAndroid Build Coastguard Worker static char *
1263*8b26181fSAndroid Build Coastguard Worker dlstrerror(char *errbuf, size_t errbufsize, bpf_u_int32 dl_errno)
1264*8b26181fSAndroid Build Coastguard Worker {
1265*8b26181fSAndroid Build Coastguard Worker switch (dl_errno) {
1266*8b26181fSAndroid Build Coastguard Worker
1267*8b26181fSAndroid Build Coastguard Worker case DL_ACCESS:
1268*8b26181fSAndroid Build Coastguard Worker return ("Improper permissions for request");
1269*8b26181fSAndroid Build Coastguard Worker
1270*8b26181fSAndroid Build Coastguard Worker case DL_BADADDR:
1271*8b26181fSAndroid Build Coastguard Worker return ("DLSAP addr in improper format or invalid");
1272*8b26181fSAndroid Build Coastguard Worker
1273*8b26181fSAndroid Build Coastguard Worker case DL_BADCORR:
1274*8b26181fSAndroid Build Coastguard Worker return ("Seq number not from outstand DL_CONN_IND");
1275*8b26181fSAndroid Build Coastguard Worker
1276*8b26181fSAndroid Build Coastguard Worker case DL_BADDATA:
1277*8b26181fSAndroid Build Coastguard Worker return ("User data exceeded provider limit");
1278*8b26181fSAndroid Build Coastguard Worker
1279*8b26181fSAndroid Build Coastguard Worker case DL_BADPPA:
1280*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DEV_DLPI
1281*8b26181fSAndroid Build Coastguard Worker /*
1282*8b26181fSAndroid Build Coastguard Worker * With a single "/dev/dlpi" device used for all
1283*8b26181fSAndroid Build Coastguard Worker * DLPI providers, PPAs have nothing to do with
1284*8b26181fSAndroid Build Coastguard Worker * unit numbers.
1285*8b26181fSAndroid Build Coastguard Worker */
1286*8b26181fSAndroid Build Coastguard Worker return ("Specified PPA was invalid");
1287*8b26181fSAndroid Build Coastguard Worker #else
1288*8b26181fSAndroid Build Coastguard Worker /*
1289*8b26181fSAndroid Build Coastguard Worker * We have separate devices for separate devices;
1290*8b26181fSAndroid Build Coastguard Worker * the PPA is just the unit number.
1291*8b26181fSAndroid Build Coastguard Worker */
1292*8b26181fSAndroid Build Coastguard Worker return ("Specified PPA (device unit) was invalid");
1293*8b26181fSAndroid Build Coastguard Worker #endif
1294*8b26181fSAndroid Build Coastguard Worker
1295*8b26181fSAndroid Build Coastguard Worker case DL_BADPRIM:
1296*8b26181fSAndroid Build Coastguard Worker return ("Primitive received not known by provider");
1297*8b26181fSAndroid Build Coastguard Worker
1298*8b26181fSAndroid Build Coastguard Worker case DL_BADQOSPARAM:
1299*8b26181fSAndroid Build Coastguard Worker return ("QOS parameters contained invalid values");
1300*8b26181fSAndroid Build Coastguard Worker
1301*8b26181fSAndroid Build Coastguard Worker case DL_BADQOSTYPE:
1302*8b26181fSAndroid Build Coastguard Worker return ("QOS structure type is unknown/unsupported");
1303*8b26181fSAndroid Build Coastguard Worker
1304*8b26181fSAndroid Build Coastguard Worker case DL_BADSAP:
1305*8b26181fSAndroid Build Coastguard Worker return ("Bad LSAP selector");
1306*8b26181fSAndroid Build Coastguard Worker
1307*8b26181fSAndroid Build Coastguard Worker case DL_BADTOKEN:
1308*8b26181fSAndroid Build Coastguard Worker return ("Token used not an active stream");
1309*8b26181fSAndroid Build Coastguard Worker
1310*8b26181fSAndroid Build Coastguard Worker case DL_BOUND:
1311*8b26181fSAndroid Build Coastguard Worker return ("Attempted second bind with dl_max_conind");
1312*8b26181fSAndroid Build Coastguard Worker
1313*8b26181fSAndroid Build Coastguard Worker case DL_INITFAILED:
1314*8b26181fSAndroid Build Coastguard Worker return ("Physical link initialization failed");
1315*8b26181fSAndroid Build Coastguard Worker
1316*8b26181fSAndroid Build Coastguard Worker case DL_NOADDR:
1317*8b26181fSAndroid Build Coastguard Worker return ("Provider couldn't allocate alternate address");
1318*8b26181fSAndroid Build Coastguard Worker
1319*8b26181fSAndroid Build Coastguard Worker case DL_NOTINIT:
1320*8b26181fSAndroid Build Coastguard Worker return ("Physical link not initialized");
1321*8b26181fSAndroid Build Coastguard Worker
1322*8b26181fSAndroid Build Coastguard Worker case DL_OUTSTATE:
1323*8b26181fSAndroid Build Coastguard Worker return ("Primitive issued in improper state");
1324*8b26181fSAndroid Build Coastguard Worker
1325*8b26181fSAndroid Build Coastguard Worker case DL_SYSERR:
1326*8b26181fSAndroid Build Coastguard Worker return ("UNIX system error occurred");
1327*8b26181fSAndroid Build Coastguard Worker
1328*8b26181fSAndroid Build Coastguard Worker case DL_UNSUPPORTED:
1329*8b26181fSAndroid Build Coastguard Worker return ("Requested service not supplied by provider");
1330*8b26181fSAndroid Build Coastguard Worker
1331*8b26181fSAndroid Build Coastguard Worker case DL_UNDELIVERABLE:
1332*8b26181fSAndroid Build Coastguard Worker return ("Previous data unit could not be delivered");
1333*8b26181fSAndroid Build Coastguard Worker
1334*8b26181fSAndroid Build Coastguard Worker case DL_NOTSUPPORTED:
1335*8b26181fSAndroid Build Coastguard Worker return ("Primitive is known but not supported");
1336*8b26181fSAndroid Build Coastguard Worker
1337*8b26181fSAndroid Build Coastguard Worker case DL_TOOMANY:
1338*8b26181fSAndroid Build Coastguard Worker return ("Limit exceeded");
1339*8b26181fSAndroid Build Coastguard Worker
1340*8b26181fSAndroid Build Coastguard Worker case DL_NOTENAB:
1341*8b26181fSAndroid Build Coastguard Worker return ("Promiscuous mode not enabled");
1342*8b26181fSAndroid Build Coastguard Worker
1343*8b26181fSAndroid Build Coastguard Worker case DL_BUSY:
1344*8b26181fSAndroid Build Coastguard Worker return ("Other streams for PPA in post-attached");
1345*8b26181fSAndroid Build Coastguard Worker
1346*8b26181fSAndroid Build Coastguard Worker case DL_NOAUTO:
1347*8b26181fSAndroid Build Coastguard Worker return ("Automatic handling XID&TEST not supported");
1348*8b26181fSAndroid Build Coastguard Worker
1349*8b26181fSAndroid Build Coastguard Worker case DL_NOXIDAUTO:
1350*8b26181fSAndroid Build Coastguard Worker return ("Automatic handling of XID not supported");
1351*8b26181fSAndroid Build Coastguard Worker
1352*8b26181fSAndroid Build Coastguard Worker case DL_NOTESTAUTO:
1353*8b26181fSAndroid Build Coastguard Worker return ("Automatic handling of TEST not supported");
1354*8b26181fSAndroid Build Coastguard Worker
1355*8b26181fSAndroid Build Coastguard Worker case DL_XIDAUTO:
1356*8b26181fSAndroid Build Coastguard Worker return ("Automatic handling of XID response");
1357*8b26181fSAndroid Build Coastguard Worker
1358*8b26181fSAndroid Build Coastguard Worker case DL_TESTAUTO:
1359*8b26181fSAndroid Build Coastguard Worker return ("Automatic handling of TEST response");
1360*8b26181fSAndroid Build Coastguard Worker
1361*8b26181fSAndroid Build Coastguard Worker case DL_PENDING:
1362*8b26181fSAndroid Build Coastguard Worker return ("Pending outstanding connect indications");
1363*8b26181fSAndroid Build Coastguard Worker
1364*8b26181fSAndroid Build Coastguard Worker default:
1365*8b26181fSAndroid Build Coastguard Worker snprintf(errbuf, errbufsize, "Error %02x", dl_errno);
1366*8b26181fSAndroid Build Coastguard Worker return (errbuf);
1367*8b26181fSAndroid Build Coastguard Worker }
1368*8b26181fSAndroid Build Coastguard Worker }
1369*8b26181fSAndroid Build Coastguard Worker
1370*8b26181fSAndroid Build Coastguard Worker static char *
1371*8b26181fSAndroid Build Coastguard Worker dlprim(char *primbuf, size_t primbufsize, bpf_u_int32 prim)
1372*8b26181fSAndroid Build Coastguard Worker {
1373*8b26181fSAndroid Build Coastguard Worker switch (prim) {
1374*8b26181fSAndroid Build Coastguard Worker
1375*8b26181fSAndroid Build Coastguard Worker case DL_INFO_REQ:
1376*8b26181fSAndroid Build Coastguard Worker return ("DL_INFO_REQ");
1377*8b26181fSAndroid Build Coastguard Worker
1378*8b26181fSAndroid Build Coastguard Worker case DL_INFO_ACK:
1379*8b26181fSAndroid Build Coastguard Worker return ("DL_INFO_ACK");
1380*8b26181fSAndroid Build Coastguard Worker
1381*8b26181fSAndroid Build Coastguard Worker case DL_ATTACH_REQ:
1382*8b26181fSAndroid Build Coastguard Worker return ("DL_ATTACH_REQ");
1383*8b26181fSAndroid Build Coastguard Worker
1384*8b26181fSAndroid Build Coastguard Worker case DL_DETACH_REQ:
1385*8b26181fSAndroid Build Coastguard Worker return ("DL_DETACH_REQ");
1386*8b26181fSAndroid Build Coastguard Worker
1387*8b26181fSAndroid Build Coastguard Worker case DL_BIND_REQ:
1388*8b26181fSAndroid Build Coastguard Worker return ("DL_BIND_REQ");
1389*8b26181fSAndroid Build Coastguard Worker
1390*8b26181fSAndroid Build Coastguard Worker case DL_BIND_ACK:
1391*8b26181fSAndroid Build Coastguard Worker return ("DL_BIND_ACK");
1392*8b26181fSAndroid Build Coastguard Worker
1393*8b26181fSAndroid Build Coastguard Worker case DL_UNBIND_REQ:
1394*8b26181fSAndroid Build Coastguard Worker return ("DL_UNBIND_REQ");
1395*8b26181fSAndroid Build Coastguard Worker
1396*8b26181fSAndroid Build Coastguard Worker case DL_OK_ACK:
1397*8b26181fSAndroid Build Coastguard Worker return ("DL_OK_ACK");
1398*8b26181fSAndroid Build Coastguard Worker
1399*8b26181fSAndroid Build Coastguard Worker case DL_ERROR_ACK:
1400*8b26181fSAndroid Build Coastguard Worker return ("DL_ERROR_ACK");
1401*8b26181fSAndroid Build Coastguard Worker
1402*8b26181fSAndroid Build Coastguard Worker case DL_SUBS_BIND_REQ:
1403*8b26181fSAndroid Build Coastguard Worker return ("DL_SUBS_BIND_REQ");
1404*8b26181fSAndroid Build Coastguard Worker
1405*8b26181fSAndroid Build Coastguard Worker case DL_SUBS_BIND_ACK:
1406*8b26181fSAndroid Build Coastguard Worker return ("DL_SUBS_BIND_ACK");
1407*8b26181fSAndroid Build Coastguard Worker
1408*8b26181fSAndroid Build Coastguard Worker case DL_UNITDATA_REQ:
1409*8b26181fSAndroid Build Coastguard Worker return ("DL_UNITDATA_REQ");
1410*8b26181fSAndroid Build Coastguard Worker
1411*8b26181fSAndroid Build Coastguard Worker case DL_UNITDATA_IND:
1412*8b26181fSAndroid Build Coastguard Worker return ("DL_UNITDATA_IND");
1413*8b26181fSAndroid Build Coastguard Worker
1414*8b26181fSAndroid Build Coastguard Worker case DL_UDERROR_IND:
1415*8b26181fSAndroid Build Coastguard Worker return ("DL_UDERROR_IND");
1416*8b26181fSAndroid Build Coastguard Worker
1417*8b26181fSAndroid Build Coastguard Worker case DL_UDQOS_REQ:
1418*8b26181fSAndroid Build Coastguard Worker return ("DL_UDQOS_REQ");
1419*8b26181fSAndroid Build Coastguard Worker
1420*8b26181fSAndroid Build Coastguard Worker case DL_CONNECT_REQ:
1421*8b26181fSAndroid Build Coastguard Worker return ("DL_CONNECT_REQ");
1422*8b26181fSAndroid Build Coastguard Worker
1423*8b26181fSAndroid Build Coastguard Worker case DL_CONNECT_IND:
1424*8b26181fSAndroid Build Coastguard Worker return ("DL_CONNECT_IND");
1425*8b26181fSAndroid Build Coastguard Worker
1426*8b26181fSAndroid Build Coastguard Worker case DL_CONNECT_RES:
1427*8b26181fSAndroid Build Coastguard Worker return ("DL_CONNECT_RES");
1428*8b26181fSAndroid Build Coastguard Worker
1429*8b26181fSAndroid Build Coastguard Worker case DL_CONNECT_CON:
1430*8b26181fSAndroid Build Coastguard Worker return ("DL_CONNECT_CON");
1431*8b26181fSAndroid Build Coastguard Worker
1432*8b26181fSAndroid Build Coastguard Worker case DL_TOKEN_REQ:
1433*8b26181fSAndroid Build Coastguard Worker return ("DL_TOKEN_REQ");
1434*8b26181fSAndroid Build Coastguard Worker
1435*8b26181fSAndroid Build Coastguard Worker case DL_TOKEN_ACK:
1436*8b26181fSAndroid Build Coastguard Worker return ("DL_TOKEN_ACK");
1437*8b26181fSAndroid Build Coastguard Worker
1438*8b26181fSAndroid Build Coastguard Worker case DL_DISCONNECT_REQ:
1439*8b26181fSAndroid Build Coastguard Worker return ("DL_DISCONNECT_REQ");
1440*8b26181fSAndroid Build Coastguard Worker
1441*8b26181fSAndroid Build Coastguard Worker case DL_DISCONNECT_IND:
1442*8b26181fSAndroid Build Coastguard Worker return ("DL_DISCONNECT_IND");
1443*8b26181fSAndroid Build Coastguard Worker
1444*8b26181fSAndroid Build Coastguard Worker case DL_RESET_REQ:
1445*8b26181fSAndroid Build Coastguard Worker return ("DL_RESET_REQ");
1446*8b26181fSAndroid Build Coastguard Worker
1447*8b26181fSAndroid Build Coastguard Worker case DL_RESET_IND:
1448*8b26181fSAndroid Build Coastguard Worker return ("DL_RESET_IND");
1449*8b26181fSAndroid Build Coastguard Worker
1450*8b26181fSAndroid Build Coastguard Worker case DL_RESET_RES:
1451*8b26181fSAndroid Build Coastguard Worker return ("DL_RESET_RES");
1452*8b26181fSAndroid Build Coastguard Worker
1453*8b26181fSAndroid Build Coastguard Worker case DL_RESET_CON:
1454*8b26181fSAndroid Build Coastguard Worker return ("DL_RESET_CON");
1455*8b26181fSAndroid Build Coastguard Worker
1456*8b26181fSAndroid Build Coastguard Worker default:
1457*8b26181fSAndroid Build Coastguard Worker snprintf(primbuf, primbufsize, "unknown primitive 0x%x",
1458*8b26181fSAndroid Build Coastguard Worker prim);
1459*8b26181fSAndroid Build Coastguard Worker return (primbuf);
1460*8b26181fSAndroid Build Coastguard Worker }
1461*8b26181fSAndroid Build Coastguard Worker }
1462*8b26181fSAndroid Build Coastguard Worker
1463*8b26181fSAndroid Build Coastguard Worker static int
1464*8b26181fSAndroid Build Coastguard Worker dlbindreq(int fd, bpf_u_int32 sap, char *ebuf)
1465*8b26181fSAndroid Build Coastguard Worker {
1466*8b26181fSAndroid Build Coastguard Worker
1467*8b26181fSAndroid Build Coastguard Worker dl_bind_req_t req;
1468*8b26181fSAndroid Build Coastguard Worker
1469*8b26181fSAndroid Build Coastguard Worker memset((char *)&req, 0, sizeof(req));
1470*8b26181fSAndroid Build Coastguard Worker req.dl_primitive = DL_BIND_REQ;
1471*8b26181fSAndroid Build Coastguard Worker /* XXX - what if neither of these are defined? */
1472*8b26181fSAndroid Build Coastguard Worker #if defined(DL_HP_RAWDLS)
1473*8b26181fSAndroid Build Coastguard Worker req.dl_max_conind = 1; /* XXX magic number */
1474*8b26181fSAndroid Build Coastguard Worker req.dl_service_mode = DL_HP_RAWDLS;
1475*8b26181fSAndroid Build Coastguard Worker #elif defined(DL_CLDLS)
1476*8b26181fSAndroid Build Coastguard Worker req.dl_service_mode = DL_CLDLS;
1477*8b26181fSAndroid Build Coastguard Worker #endif
1478*8b26181fSAndroid Build Coastguard Worker req.dl_sap = sap;
1479*8b26181fSAndroid Build Coastguard Worker
1480*8b26181fSAndroid Build Coastguard Worker return (send_request(fd, (char *)&req, sizeof(req), "bind", ebuf));
1481*8b26181fSAndroid Build Coastguard Worker }
1482*8b26181fSAndroid Build Coastguard Worker
1483*8b26181fSAndroid Build Coastguard Worker static int
1484*8b26181fSAndroid Build Coastguard Worker dlbindack(int fd, char *bufp, char *ebuf, int *uerror)
1485*8b26181fSAndroid Build Coastguard Worker {
1486*8b26181fSAndroid Build Coastguard Worker
1487*8b26181fSAndroid Build Coastguard Worker return (recv_ack(fd, DL_BIND_ACK_SIZE, "bind", bufp, ebuf, uerror));
1488*8b26181fSAndroid Build Coastguard Worker }
1489*8b26181fSAndroid Build Coastguard Worker
1490*8b26181fSAndroid Build Coastguard Worker static int
1491*8b26181fSAndroid Build Coastguard Worker dlokack(int fd, const char *what, char *bufp, char *ebuf, int *uerror)
1492*8b26181fSAndroid Build Coastguard Worker {
1493*8b26181fSAndroid Build Coastguard Worker
1494*8b26181fSAndroid Build Coastguard Worker return (recv_ack(fd, DL_OK_ACK_SIZE, what, bufp, ebuf, uerror));
1495*8b26181fSAndroid Build Coastguard Worker }
1496*8b26181fSAndroid Build Coastguard Worker
1497*8b26181fSAndroid Build Coastguard Worker
1498*8b26181fSAndroid Build Coastguard Worker static int
1499*8b26181fSAndroid Build Coastguard Worker dlinforeq(int fd, char *ebuf)
1500*8b26181fSAndroid Build Coastguard Worker {
1501*8b26181fSAndroid Build Coastguard Worker dl_info_req_t req;
1502*8b26181fSAndroid Build Coastguard Worker
1503*8b26181fSAndroid Build Coastguard Worker req.dl_primitive = DL_INFO_REQ;
1504*8b26181fSAndroid Build Coastguard Worker
1505*8b26181fSAndroid Build Coastguard Worker return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf));
1506*8b26181fSAndroid Build Coastguard Worker }
1507*8b26181fSAndroid Build Coastguard Worker
1508*8b26181fSAndroid Build Coastguard Worker static int
1509*8b26181fSAndroid Build Coastguard Worker dlinfoack(int fd, char *bufp, char *ebuf)
1510*8b26181fSAndroid Build Coastguard Worker {
1511*8b26181fSAndroid Build Coastguard Worker
1512*8b26181fSAndroid Build Coastguard Worker return (recv_ack(fd, DL_INFO_ACK_SIZE, "info", bufp, ebuf, NULL));
1513*8b26181fSAndroid Build Coastguard Worker }
1514*8b26181fSAndroid Build Coastguard Worker
1515*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DL_PASSIVE_REQ_T
1516*8b26181fSAndroid Build Coastguard Worker /*
1517*8b26181fSAndroid Build Coastguard Worker * Enable DLPI passive mode. We do not care if this request fails, as this
1518*8b26181fSAndroid Build Coastguard Worker * indicates the underlying DLPI device does not support link aggregation.
1519*8b26181fSAndroid Build Coastguard Worker */
1520*8b26181fSAndroid Build Coastguard Worker static void
1521*8b26181fSAndroid Build Coastguard Worker dlpassive(int fd, char *ebuf)
1522*8b26181fSAndroid Build Coastguard Worker {
1523*8b26181fSAndroid Build Coastguard Worker dl_passive_req_t req;
1524*8b26181fSAndroid Build Coastguard Worker bpf_u_int32 buf[MAXDLBUF];
1525*8b26181fSAndroid Build Coastguard Worker
1526*8b26181fSAndroid Build Coastguard Worker req.dl_primitive = DL_PASSIVE_REQ;
1527*8b26181fSAndroid Build Coastguard Worker
1528*8b26181fSAndroid Build Coastguard Worker if (send_request(fd, (char *)&req, sizeof(req), "dlpassive", ebuf) == 0)
1529*8b26181fSAndroid Build Coastguard Worker (void) dlokack(fd, "dlpassive", (char *)buf, ebuf, NULL);
1530*8b26181fSAndroid Build Coastguard Worker }
1531*8b26181fSAndroid Build Coastguard Worker #endif
1532*8b26181fSAndroid Build Coastguard Worker
1533*8b26181fSAndroid Build Coastguard Worker #ifdef DL_HP_RAWDLS
1534*8b26181fSAndroid Build Coastguard Worker /*
1535*8b26181fSAndroid Build Coastguard Worker * There's an ack *if* there's an error.
1536*8b26181fSAndroid Build Coastguard Worker */
1537*8b26181fSAndroid Build Coastguard Worker static int
1538*8b26181fSAndroid Build Coastguard Worker dlrawdatareq(int fd, const u_char *datap, int datalen)
1539*8b26181fSAndroid Build Coastguard Worker {
1540*8b26181fSAndroid Build Coastguard Worker struct strbuf ctl, data;
1541*8b26181fSAndroid Build Coastguard Worker long buf[MAXDLBUF]; /* XXX - char? */
1542*8b26181fSAndroid Build Coastguard Worker union DL_primitives *dlp;
1543*8b26181fSAndroid Build Coastguard Worker int dlen;
1544*8b26181fSAndroid Build Coastguard Worker
1545*8b26181fSAndroid Build Coastguard Worker dlp = MAKE_DL_PRIMITIVES(buf);
1546*8b26181fSAndroid Build Coastguard Worker
1547*8b26181fSAndroid Build Coastguard Worker dlp->dl_primitive = DL_HP_RAWDATA_REQ;
1548*8b26181fSAndroid Build Coastguard Worker dlen = DL_HP_RAWDATA_REQ_SIZE;
1549*8b26181fSAndroid Build Coastguard Worker
1550*8b26181fSAndroid Build Coastguard Worker /*
1551*8b26181fSAndroid Build Coastguard Worker * HP's documentation doesn't appear to show us supplying any
1552*8b26181fSAndroid Build Coastguard Worker * address pointed to by the control part of the message.
1553*8b26181fSAndroid Build Coastguard Worker * I think that's what raw mode means - you just send the raw
1554*8b26181fSAndroid Build Coastguard Worker * packet, you don't specify where to send it to, as that's
1555*8b26181fSAndroid Build Coastguard Worker * implied by the destination address.
1556*8b26181fSAndroid Build Coastguard Worker */
1557*8b26181fSAndroid Build Coastguard Worker ctl.maxlen = 0;
1558*8b26181fSAndroid Build Coastguard Worker ctl.len = dlen;
1559*8b26181fSAndroid Build Coastguard Worker ctl.buf = (void *)buf;
1560*8b26181fSAndroid Build Coastguard Worker
1561*8b26181fSAndroid Build Coastguard Worker data.maxlen = 0;
1562*8b26181fSAndroid Build Coastguard Worker data.len = datalen;
1563*8b26181fSAndroid Build Coastguard Worker data.buf = (void *)datap;
1564*8b26181fSAndroid Build Coastguard Worker
1565*8b26181fSAndroid Build Coastguard Worker return (putmsg(fd, &ctl, &data, 0));
1566*8b26181fSAndroid Build Coastguard Worker }
1567*8b26181fSAndroid Build Coastguard Worker #endif /* DL_HP_RAWDLS */
1568*8b26181fSAndroid Build Coastguard Worker
1569*8b26181fSAndroid Build Coastguard Worker #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
1570*8b26181fSAndroid Build Coastguard Worker static void
1571*8b26181fSAndroid Build Coastguard Worker get_release(char *buf, size_t bufsize, bpf_u_int32 *majorp,
1572*8b26181fSAndroid Build Coastguard Worker bpf_u_int32 *minorp, bpf_u_int32 *microp)
1573*8b26181fSAndroid Build Coastguard Worker {
1574*8b26181fSAndroid Build Coastguard Worker char *cp;
1575*8b26181fSAndroid Build Coastguard Worker
1576*8b26181fSAndroid Build Coastguard Worker *majorp = 0;
1577*8b26181fSAndroid Build Coastguard Worker *minorp = 0;
1578*8b26181fSAndroid Build Coastguard Worker *microp = 0;
1579*8b26181fSAndroid Build Coastguard Worker if (sysinfo(SI_RELEASE, buf, bufsize) < 0) {
1580*8b26181fSAndroid Build Coastguard Worker pcap_strlcpy(buf, "?", bufsize);
1581*8b26181fSAndroid Build Coastguard Worker return;
1582*8b26181fSAndroid Build Coastguard Worker }
1583*8b26181fSAndroid Build Coastguard Worker cp = buf;
1584*8b26181fSAndroid Build Coastguard Worker if (!PCAP_ISDIGIT((unsigned char)*cp))
1585*8b26181fSAndroid Build Coastguard Worker return;
1586*8b26181fSAndroid Build Coastguard Worker *majorp = strtol(cp, &cp, 10);
1587*8b26181fSAndroid Build Coastguard Worker if (*cp++ != '.')
1588*8b26181fSAndroid Build Coastguard Worker return;
1589*8b26181fSAndroid Build Coastguard Worker *minorp = strtol(cp, &cp, 10);
1590*8b26181fSAndroid Build Coastguard Worker if (*cp++ != '.')
1591*8b26181fSAndroid Build Coastguard Worker return;
1592*8b26181fSAndroid Build Coastguard Worker *microp = strtol(cp, &cp, 10);
1593*8b26181fSAndroid Build Coastguard Worker }
1594*8b26181fSAndroid Build Coastguard Worker #endif
1595*8b26181fSAndroid Build Coastguard Worker
1596*8b26181fSAndroid Build Coastguard Worker #ifdef DL_HP_PPA_REQ
1597*8b26181fSAndroid Build Coastguard Worker /*
1598*8b26181fSAndroid Build Coastguard Worker * Under HP-UX 10 and HP-UX 11, we can ask for the ppa
1599*8b26181fSAndroid Build Coastguard Worker */
1600*8b26181fSAndroid Build Coastguard Worker
1601*8b26181fSAndroid Build Coastguard Worker
1602*8b26181fSAndroid Build Coastguard Worker /*
1603*8b26181fSAndroid Build Coastguard Worker * Determine ppa number that specifies ifname.
1604*8b26181fSAndroid Build Coastguard Worker *
1605*8b26181fSAndroid Build Coastguard Worker * If the "dl_hp_ppa_info_t" doesn't have a "dl_module_id_1" member,
1606*8b26181fSAndroid Build Coastguard Worker * the code that's used here is the old code for HP-UX 10.x.
1607*8b26181fSAndroid Build Coastguard Worker *
1608*8b26181fSAndroid Build Coastguard Worker * However, HP-UX 10.20, at least, appears to have such a member
1609*8b26181fSAndroid Build Coastguard Worker * in its "dl_hp_ppa_info_t" structure, so the new code is used.
1610*8b26181fSAndroid Build Coastguard Worker * The new code didn't work on an old 10.20 system on which Rick
1611*8b26181fSAndroid Build Coastguard Worker * Jones of HP tried it, but with later patches installed, it
1612*8b26181fSAndroid Build Coastguard Worker * worked - it appears that the older system had those members but
1613*8b26181fSAndroid Build Coastguard Worker * didn't put anything in them, so, if the search by name fails, we
1614*8b26181fSAndroid Build Coastguard Worker * do the old search.
1615*8b26181fSAndroid Build Coastguard Worker *
1616*8b26181fSAndroid Build Coastguard Worker * Rick suggests that making sure your system is "up on the latest
1617*8b26181fSAndroid Build Coastguard Worker * lancommon/DLPI/driver patches" is probably a good idea; it'd fix
1618*8b26181fSAndroid Build Coastguard Worker * that problem, as well as allowing libpcap to see packets sent
1619*8b26181fSAndroid Build Coastguard Worker * from the system on which the libpcap application is being run.
1620*8b26181fSAndroid Build Coastguard Worker * (On 10.20, in addition to getting the latest patches, you need
1621*8b26181fSAndroid Build Coastguard Worker * to turn the kernel "lanc_outbound_promisc_flag" flag on with ADB;
1622*8b26181fSAndroid Build Coastguard Worker * a posting to "comp.sys.hp.hpux" at
1623*8b26181fSAndroid Build Coastguard Worker *
1624*8b26181fSAndroid Build Coastguard Worker * http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=558092266
1625*8b26181fSAndroid Build Coastguard Worker *
1626*8b26181fSAndroid Build Coastguard Worker * says that, to see the machine's outgoing traffic, you'd need to
1627*8b26181fSAndroid Build Coastguard Worker * apply the right patches to your system, and also set that variable
1628*8b26181fSAndroid Build Coastguard Worker * with:
1629*8b26181fSAndroid Build Coastguard Worker
1630*8b26181fSAndroid Build Coastguard Worker echo 'lanc_outbound_promisc_flag/W1' | /usr/bin/adb -w /stand/vmunix /dev/kmem
1631*8b26181fSAndroid Build Coastguard Worker
1632*8b26181fSAndroid Build Coastguard Worker * which could be put in, for example, "/sbin/init.d/lan".
1633*8b26181fSAndroid Build Coastguard Worker *
1634*8b26181fSAndroid Build Coastguard Worker * Setting the variable is not necessary on HP-UX 11.x.
1635*8b26181fSAndroid Build Coastguard Worker */
1636*8b26181fSAndroid Build Coastguard Worker static int
1637*8b26181fSAndroid Build Coastguard Worker get_dlpi_ppa(register int fd, register const char *device, register u_int unit,
1638*8b26181fSAndroid Build Coastguard Worker u_int *ppa, register char *ebuf)
1639*8b26181fSAndroid Build Coastguard Worker {
1640*8b26181fSAndroid Build Coastguard Worker register dl_hp_ppa_ack_t *ap;
1641*8b26181fSAndroid Build Coastguard Worker register dl_hp_ppa_info_t *ipstart, *ip;
1642*8b26181fSAndroid Build Coastguard Worker register u_int i;
1643*8b26181fSAndroid Build Coastguard Worker char dname[100];
1644*8b26181fSAndroid Build Coastguard Worker register u_long majdev;
1645*8b26181fSAndroid Build Coastguard Worker struct stat statbuf;
1646*8b26181fSAndroid Build Coastguard Worker dl_hp_ppa_req_t req;
1647*8b26181fSAndroid Build Coastguard Worker char buf[MAXDLBUF];
1648*8b26181fSAndroid Build Coastguard Worker char *ppa_data_buf;
1649*8b26181fSAndroid Build Coastguard Worker dl_hp_ppa_ack_t *dlp;
1650*8b26181fSAndroid Build Coastguard Worker struct strbuf ctl;
1651*8b26181fSAndroid Build Coastguard Worker int flags;
1652*8b26181fSAndroid Build Coastguard Worker
1653*8b26181fSAndroid Build Coastguard Worker memset((char *)&req, 0, sizeof(req));
1654*8b26181fSAndroid Build Coastguard Worker req.dl_primitive = DL_HP_PPA_REQ;
1655*8b26181fSAndroid Build Coastguard Worker
1656*8b26181fSAndroid Build Coastguard Worker memset((char *)buf, 0, sizeof(buf));
1657*8b26181fSAndroid Build Coastguard Worker if (send_request(fd, (char *)&req, sizeof(req), "hpppa", ebuf) < 0)
1658*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1659*8b26181fSAndroid Build Coastguard Worker
1660*8b26181fSAndroid Build Coastguard Worker ctl.maxlen = DL_HP_PPA_ACK_SIZE;
1661*8b26181fSAndroid Build Coastguard Worker ctl.len = 0;
1662*8b26181fSAndroid Build Coastguard Worker ctl.buf = (char *)buf;
1663*8b26181fSAndroid Build Coastguard Worker
1664*8b26181fSAndroid Build Coastguard Worker flags = 0;
1665*8b26181fSAndroid Build Coastguard Worker /*
1666*8b26181fSAndroid Build Coastguard Worker * DLPI may return a big chunk of data for a DL_HP_PPA_REQ. The normal
1667*8b26181fSAndroid Build Coastguard Worker * recv_ack will fail because it set the maxlen to MAXDLBUF (8192)
1668*8b26181fSAndroid Build Coastguard Worker * which is NOT big enough for a DL_HP_PPA_REQ.
1669*8b26181fSAndroid Build Coastguard Worker *
1670*8b26181fSAndroid Build Coastguard Worker * This causes libpcap applications to fail on a system with HP-APA
1671*8b26181fSAndroid Build Coastguard Worker * installed.
1672*8b26181fSAndroid Build Coastguard Worker *
1673*8b26181fSAndroid Build Coastguard Worker * To figure out how big the returned data is, we first call getmsg
1674*8b26181fSAndroid Build Coastguard Worker * to get the small head and peek at the head to get the actual data
1675*8b26181fSAndroid Build Coastguard Worker * length, and then issue another getmsg to get the actual PPA data.
1676*8b26181fSAndroid Build Coastguard Worker */
1677*8b26181fSAndroid Build Coastguard Worker /* get the head first */
1678*8b26181fSAndroid Build Coastguard Worker if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
1679*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1680*8b26181fSAndroid Build Coastguard Worker errno, "get_dlpi_ppa: hpppa getmsg");
1681*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1682*8b26181fSAndroid Build Coastguard Worker }
1683*8b26181fSAndroid Build Coastguard Worker if (ctl.len == -1) {
1684*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE,
1685*8b26181fSAndroid Build Coastguard Worker "get_dlpi_ppa: hpppa getmsg: control buffer has no data");
1686*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1687*8b26181fSAndroid Build Coastguard Worker }
1688*8b26181fSAndroid Build Coastguard Worker
1689*8b26181fSAndroid Build Coastguard Worker dlp = (dl_hp_ppa_ack_t *)ctl.buf;
1690*8b26181fSAndroid Build Coastguard Worker if (dlp->dl_primitive != DL_HP_PPA_ACK) {
1691*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE,
1692*8b26181fSAndroid Build Coastguard Worker "get_dlpi_ppa: hpppa unexpected primitive ack 0x%x",
1693*8b26181fSAndroid Build Coastguard Worker (bpf_u_int32)dlp->dl_primitive);
1694*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1695*8b26181fSAndroid Build Coastguard Worker }
1696*8b26181fSAndroid Build Coastguard Worker
1697*8b26181fSAndroid Build Coastguard Worker if ((size_t)ctl.len < DL_HP_PPA_ACK_SIZE) {
1698*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE,
1699*8b26181fSAndroid Build Coastguard Worker "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1700*8b26181fSAndroid Build Coastguard Worker ctl.len, (unsigned long)DL_HP_PPA_ACK_SIZE);
1701*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1702*8b26181fSAndroid Build Coastguard Worker }
1703*8b26181fSAndroid Build Coastguard Worker
1704*8b26181fSAndroid Build Coastguard Worker /* allocate buffer */
1705*8b26181fSAndroid Build Coastguard Worker if ((ppa_data_buf = (char *)malloc(dlp->dl_length)) == NULL) {
1706*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1707*8b26181fSAndroid Build Coastguard Worker errno, "get_dlpi_ppa: hpppa malloc");
1708*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1709*8b26181fSAndroid Build Coastguard Worker }
1710*8b26181fSAndroid Build Coastguard Worker ctl.maxlen = dlp->dl_length;
1711*8b26181fSAndroid Build Coastguard Worker ctl.len = 0;
1712*8b26181fSAndroid Build Coastguard Worker ctl.buf = (char *)ppa_data_buf;
1713*8b26181fSAndroid Build Coastguard Worker /* get the data */
1714*8b26181fSAndroid Build Coastguard Worker if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
1715*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1716*8b26181fSAndroid Build Coastguard Worker errno, "get_dlpi_ppa: hpppa getmsg");
1717*8b26181fSAndroid Build Coastguard Worker free(ppa_data_buf);
1718*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1719*8b26181fSAndroid Build Coastguard Worker }
1720*8b26181fSAndroid Build Coastguard Worker if (ctl.len == -1) {
1721*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE,
1722*8b26181fSAndroid Build Coastguard Worker "get_dlpi_ppa: hpppa getmsg: control buffer has no data");
1723*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1724*8b26181fSAndroid Build Coastguard Worker }
1725*8b26181fSAndroid Build Coastguard Worker if ((u_int)ctl.len < dlp->dl_length) {
1726*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE,
1727*8b26181fSAndroid Build Coastguard Worker "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1728*8b26181fSAndroid Build Coastguard Worker ctl.len, (unsigned long)dlp->dl_length);
1729*8b26181fSAndroid Build Coastguard Worker free(ppa_data_buf);
1730*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1731*8b26181fSAndroid Build Coastguard Worker }
1732*8b26181fSAndroid Build Coastguard Worker
1733*8b26181fSAndroid Build Coastguard Worker ap = (dl_hp_ppa_ack_t *)buf;
1734*8b26181fSAndroid Build Coastguard Worker ipstart = (dl_hp_ppa_info_t *)ppa_data_buf;
1735*8b26181fSAndroid Build Coastguard Worker ip = ipstart;
1736*8b26181fSAndroid Build Coastguard Worker
1737*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DL_HP_PPA_INFO_T_DL_MODULE_ID_1
1738*8b26181fSAndroid Build Coastguard Worker /*
1739*8b26181fSAndroid Build Coastguard Worker * The "dl_hp_ppa_info_t" structure has a "dl_module_id_1"
1740*8b26181fSAndroid Build Coastguard Worker * member that should, in theory, contain the part of the
1741*8b26181fSAndroid Build Coastguard Worker * name for the device that comes before the unit number,
1742*8b26181fSAndroid Build Coastguard Worker * and should also have a "dl_module_id_2" member that may
1743*8b26181fSAndroid Build Coastguard Worker * contain an alternate name (e.g., I think Ethernet devices
1744*8b26181fSAndroid Build Coastguard Worker * have both "lan", for "lanN", and "snap", for "snapN", with
1745*8b26181fSAndroid Build Coastguard Worker * the former being for Ethernet packets and the latter being
1746*8b26181fSAndroid Build Coastguard Worker * for 802.3/802.2 packets).
1747*8b26181fSAndroid Build Coastguard Worker *
1748*8b26181fSAndroid Build Coastguard Worker * Search for the device that has the specified name and
1749*8b26181fSAndroid Build Coastguard Worker * instance number.
1750*8b26181fSAndroid Build Coastguard Worker */
1751*8b26181fSAndroid Build Coastguard Worker for (i = 0; i < ap->dl_count; i++) {
1752*8b26181fSAndroid Build Coastguard Worker if ((strcmp((const char *)ip->dl_module_id_1, device) == 0 ||
1753*8b26181fSAndroid Build Coastguard Worker strcmp((const char *)ip->dl_module_id_2, device) == 0) &&
1754*8b26181fSAndroid Build Coastguard Worker ip->dl_instance_num == unit)
1755*8b26181fSAndroid Build Coastguard Worker break;
1756*8b26181fSAndroid Build Coastguard Worker
1757*8b26181fSAndroid Build Coastguard Worker ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
1758*8b26181fSAndroid Build Coastguard Worker }
1759*8b26181fSAndroid Build Coastguard Worker #else
1760*8b26181fSAndroid Build Coastguard Worker /*
1761*8b26181fSAndroid Build Coastguard Worker * We don't have that member, so the search is impossible; make it
1762*8b26181fSAndroid Build Coastguard Worker * look as if the search failed.
1763*8b26181fSAndroid Build Coastguard Worker */
1764*8b26181fSAndroid Build Coastguard Worker i = ap->dl_count;
1765*8b26181fSAndroid Build Coastguard Worker #endif
1766*8b26181fSAndroid Build Coastguard Worker
1767*8b26181fSAndroid Build Coastguard Worker if (i == ap->dl_count) {
1768*8b26181fSAndroid Build Coastguard Worker /*
1769*8b26181fSAndroid Build Coastguard Worker * Well, we didn't, or can't, find the device by name.
1770*8b26181fSAndroid Build Coastguard Worker *
1771*8b26181fSAndroid Build Coastguard Worker * HP-UX 10.20, whilst it has "dl_module_id_1" and
1772*8b26181fSAndroid Build Coastguard Worker * "dl_module_id_2" fields in the "dl_hp_ppa_info_t",
1773*8b26181fSAndroid Build Coastguard Worker * doesn't seem to fill them in unless the system is
1774*8b26181fSAndroid Build Coastguard Worker * at a reasonably up-to-date patch level.
1775*8b26181fSAndroid Build Coastguard Worker *
1776*8b26181fSAndroid Build Coastguard Worker * Older HP-UX 10.x systems might not have those fields
1777*8b26181fSAndroid Build Coastguard Worker * at all.
1778*8b26181fSAndroid Build Coastguard Worker *
1779*8b26181fSAndroid Build Coastguard Worker * Therefore, we'll search for the entry with the major
1780*8b26181fSAndroid Build Coastguard Worker * device number of a device with the name "/dev/<dev><unit>",
1781*8b26181fSAndroid Build Coastguard Worker * if such a device exists, as the old code did.
1782*8b26181fSAndroid Build Coastguard Worker */
1783*8b26181fSAndroid Build Coastguard Worker snprintf(dname, sizeof(dname), "/dev/%s%u", device, unit);
1784*8b26181fSAndroid Build Coastguard Worker if (stat(dname, &statbuf) < 0) {
1785*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1786*8b26181fSAndroid Build Coastguard Worker errno, "stat: %s", dname);
1787*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1788*8b26181fSAndroid Build Coastguard Worker }
1789*8b26181fSAndroid Build Coastguard Worker majdev = major(statbuf.st_rdev);
1790*8b26181fSAndroid Build Coastguard Worker
1791*8b26181fSAndroid Build Coastguard Worker ip = ipstart;
1792*8b26181fSAndroid Build Coastguard Worker
1793*8b26181fSAndroid Build Coastguard Worker for (i = 0; i < ap->dl_count; i++) {
1794*8b26181fSAndroid Build Coastguard Worker if (ip->dl_mjr_num == majdev &&
1795*8b26181fSAndroid Build Coastguard Worker ip->dl_instance_num == unit)
1796*8b26181fSAndroid Build Coastguard Worker break;
1797*8b26181fSAndroid Build Coastguard Worker
1798*8b26181fSAndroid Build Coastguard Worker ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
1799*8b26181fSAndroid Build Coastguard Worker }
1800*8b26181fSAndroid Build Coastguard Worker }
1801*8b26181fSAndroid Build Coastguard Worker if (i == ap->dl_count) {
1802*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE,
1803*8b26181fSAndroid Build Coastguard Worker "can't find /dev/dlpi PPA for %s%u", device, unit);
1804*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR_NO_SUCH_DEVICE);
1805*8b26181fSAndroid Build Coastguard Worker }
1806*8b26181fSAndroid Build Coastguard Worker if (ip->dl_hdw_state == HDW_DEAD) {
1807*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE,
1808*8b26181fSAndroid Build Coastguard Worker "%s%d: hardware state: DOWN\n", device, unit);
1809*8b26181fSAndroid Build Coastguard Worker free(ppa_data_buf);
1810*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1811*8b26181fSAndroid Build Coastguard Worker }
1812*8b26181fSAndroid Build Coastguard Worker *ppa = ip->dl_ppa;
1813*8b26181fSAndroid Build Coastguard Worker free(ppa_data_buf);
1814*8b26181fSAndroid Build Coastguard Worker return (0);
1815*8b26181fSAndroid Build Coastguard Worker }
1816*8b26181fSAndroid Build Coastguard Worker #endif
1817*8b26181fSAndroid Build Coastguard Worker
1818*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_HPUX9
1819*8b26181fSAndroid Build Coastguard Worker /*
1820*8b26181fSAndroid Build Coastguard Worker * Under HP-UX 9, there is no good way to determine the ppa.
1821*8b26181fSAndroid Build Coastguard Worker * So punt and read it from /dev/kmem.
1822*8b26181fSAndroid Build Coastguard Worker */
1823*8b26181fSAndroid Build Coastguard Worker static struct nlist nl[] = {
1824*8b26181fSAndroid Build Coastguard Worker #define NL_IFNET 0
1825*8b26181fSAndroid Build Coastguard Worker { "ifnet" },
1826*8b26181fSAndroid Build Coastguard Worker { "" }
1827*8b26181fSAndroid Build Coastguard Worker };
1828*8b26181fSAndroid Build Coastguard Worker
1829*8b26181fSAndroid Build Coastguard Worker static char path_vmunix[] = "/hp-ux";
1830*8b26181fSAndroid Build Coastguard Worker
1831*8b26181fSAndroid Build Coastguard Worker /* Determine ppa number that specifies ifname */
1832*8b26181fSAndroid Build Coastguard Worker static int
1833*8b26181fSAndroid Build Coastguard Worker get_dlpi_ppa(register int fd, register const char *ifname, register u_int unit,
1834*8b26181fSAndroid Build Coastguard Worker u_int *ppa, register char *ebuf)
1835*8b26181fSAndroid Build Coastguard Worker {
1836*8b26181fSAndroid Build Coastguard Worker register const char *cp;
1837*8b26181fSAndroid Build Coastguard Worker register int kd;
1838*8b26181fSAndroid Build Coastguard Worker void *addr;
1839*8b26181fSAndroid Build Coastguard Worker struct ifnet ifnet;
1840*8b26181fSAndroid Build Coastguard Worker char if_name[sizeof(ifnet.if_name) + 1];
1841*8b26181fSAndroid Build Coastguard Worker
1842*8b26181fSAndroid Build Coastguard Worker cp = strrchr(ifname, '/');
1843*8b26181fSAndroid Build Coastguard Worker if (cp != NULL)
1844*8b26181fSAndroid Build Coastguard Worker ifname = cp + 1;
1845*8b26181fSAndroid Build Coastguard Worker if (nlist(path_vmunix, &nl) < 0) {
1846*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE, "nlist %s failed",
1847*8b26181fSAndroid Build Coastguard Worker path_vmunix);
1848*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1849*8b26181fSAndroid Build Coastguard Worker }
1850*8b26181fSAndroid Build Coastguard Worker if (nl[NL_IFNET].n_value == 0) {
1851*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE,
1852*8b26181fSAndroid Build Coastguard Worker "couldn't find %s kernel symbol",
1853*8b26181fSAndroid Build Coastguard Worker nl[NL_IFNET].n_name);
1854*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1855*8b26181fSAndroid Build Coastguard Worker }
1856*8b26181fSAndroid Build Coastguard Worker kd = open("/dev/kmem", O_RDONLY);
1857*8b26181fSAndroid Build Coastguard Worker if (kd < 0) {
1858*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1859*8b26181fSAndroid Build Coastguard Worker errno, "kmem open");
1860*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1861*8b26181fSAndroid Build Coastguard Worker }
1862*8b26181fSAndroid Build Coastguard Worker if (dlpi_kread(kd, nl[NL_IFNET].n_value,
1863*8b26181fSAndroid Build Coastguard Worker &addr, sizeof(addr), ebuf) < 0) {
1864*8b26181fSAndroid Build Coastguard Worker close(kd);
1865*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1866*8b26181fSAndroid Build Coastguard Worker }
1867*8b26181fSAndroid Build Coastguard Worker for (; addr != NULL; addr = ifnet.if_next) {
1868*8b26181fSAndroid Build Coastguard Worker if (dlpi_kread(kd, (off_t)addr,
1869*8b26181fSAndroid Build Coastguard Worker &ifnet, sizeof(ifnet), ebuf) < 0 ||
1870*8b26181fSAndroid Build Coastguard Worker dlpi_kread(kd, (off_t)ifnet.if_name,
1871*8b26181fSAndroid Build Coastguard Worker if_name, sizeof(ifnet.if_name), ebuf) < 0) {
1872*8b26181fSAndroid Build Coastguard Worker (void)close(kd);
1873*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR);
1874*8b26181fSAndroid Build Coastguard Worker }
1875*8b26181fSAndroid Build Coastguard Worker if_name[sizeof(ifnet.if_name)] = '\0';
1876*8b26181fSAndroid Build Coastguard Worker if (strcmp(if_name, ifname) == 0 && ifnet.if_unit == unit) {
1877*8b26181fSAndroid Build Coastguard Worker *ppa = ifnet.if_index;
1878*8b26181fSAndroid Build Coastguard Worker return (0);
1879*8b26181fSAndroid Build Coastguard Worker }
1880*8b26181fSAndroid Build Coastguard Worker }
1881*8b26181fSAndroid Build Coastguard Worker
1882*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't find %s", ifname);
1883*8b26181fSAndroid Build Coastguard Worker return (PCAP_ERROR_NO_SUCH_DEVICE);
1884*8b26181fSAndroid Build Coastguard Worker }
1885*8b26181fSAndroid Build Coastguard Worker
1886*8b26181fSAndroid Build Coastguard Worker static int
1887*8b26181fSAndroid Build Coastguard Worker dlpi_kread(register int fd, register off_t addr,
1888*8b26181fSAndroid Build Coastguard Worker register void *buf, register u_int len, register char *ebuf)
1889*8b26181fSAndroid Build Coastguard Worker {
1890*8b26181fSAndroid Build Coastguard Worker register int cc;
1891*8b26181fSAndroid Build Coastguard Worker
1892*8b26181fSAndroid Build Coastguard Worker if (lseek(fd, addr, SEEK_SET) < 0) {
1893*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1894*8b26181fSAndroid Build Coastguard Worker errno, "lseek");
1895*8b26181fSAndroid Build Coastguard Worker return (-1);
1896*8b26181fSAndroid Build Coastguard Worker }
1897*8b26181fSAndroid Build Coastguard Worker cc = read(fd, buf, len);
1898*8b26181fSAndroid Build Coastguard Worker if (cc < 0) {
1899*8b26181fSAndroid Build Coastguard Worker pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1900*8b26181fSAndroid Build Coastguard Worker errno, "read");
1901*8b26181fSAndroid Build Coastguard Worker return (-1);
1902*8b26181fSAndroid Build Coastguard Worker } else if (cc != len) {
1903*8b26181fSAndroid Build Coastguard Worker snprintf(ebuf, PCAP_ERRBUF_SIZE, "short read (%d != %d)", cc,
1904*8b26181fSAndroid Build Coastguard Worker len);
1905*8b26181fSAndroid Build Coastguard Worker return (-1);
1906*8b26181fSAndroid Build Coastguard Worker }
1907*8b26181fSAndroid Build Coastguard Worker return (cc);
1908*8b26181fSAndroid Build Coastguard Worker }
1909*8b26181fSAndroid Build Coastguard Worker #endif
1910*8b26181fSAndroid Build Coastguard Worker
1911*8b26181fSAndroid Build Coastguard Worker pcap_t *
1912*8b26181fSAndroid Build Coastguard Worker pcap_create_interface(const char *device _U_, char *ebuf)
1913*8b26181fSAndroid Build Coastguard Worker {
1914*8b26181fSAndroid Build Coastguard Worker pcap_t *p;
1915*8b26181fSAndroid Build Coastguard Worker #ifdef DL_HP_RAWDLS
1916*8b26181fSAndroid Build Coastguard Worker struct pcap_dlpi *pd;
1917*8b26181fSAndroid Build Coastguard Worker #endif
1918*8b26181fSAndroid Build Coastguard Worker
1919*8b26181fSAndroid Build Coastguard Worker p = PCAP_CREATE_COMMON(ebuf, struct pcap_dlpi);
1920*8b26181fSAndroid Build Coastguard Worker if (p == NULL)
1921*8b26181fSAndroid Build Coastguard Worker return (NULL);
1922*8b26181fSAndroid Build Coastguard Worker
1923*8b26181fSAndroid Build Coastguard Worker #ifdef DL_HP_RAWDLS
1924*8b26181fSAndroid Build Coastguard Worker pd = p->priv;
1925*8b26181fSAndroid Build Coastguard Worker pd->send_fd = -1; /* it hasn't been opened yet */
1926*8b26181fSAndroid Build Coastguard Worker #endif
1927*8b26181fSAndroid Build Coastguard Worker
1928*8b26181fSAndroid Build Coastguard Worker p->activate_op = pcap_activate_dlpi;
1929*8b26181fSAndroid Build Coastguard Worker return (p);
1930*8b26181fSAndroid Build Coastguard Worker }
1931*8b26181fSAndroid Build Coastguard Worker
1932*8b26181fSAndroid Build Coastguard Worker /*
1933*8b26181fSAndroid Build Coastguard Worker * Libpcap version string.
1934*8b26181fSAndroid Build Coastguard Worker */
1935*8b26181fSAndroid Build Coastguard Worker const char *
1936*8b26181fSAndroid Build Coastguard Worker pcap_lib_version(void)
1937*8b26181fSAndroid Build Coastguard Worker {
1938*8b26181fSAndroid Build Coastguard Worker return (PCAP_VERSION_STRING);
1939*8b26181fSAndroid Build Coastguard Worker }
1940