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