]> The Tcpdump Group git mirrors - libpcap/blob - pcap-dlpi.c
Add a "setfilter" function pointer to the pcap_t structure, which
[libpcap] / pcap-dlpi.c
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.
23 */
24
25 /*
26 * Packet capture routine for DLPI under SunOS 5, HP-UX 9/10/11, and AIX.
27 *
28 * Notes:
29 *
30 * - Apparently the DLIOCRAW ioctl() is specific to SunOS.
31 *
32 * - There is a bug in bufmod(7) such that setting the snapshot
33 * length results in data being left of the front of the packet.
34 *
35 * - It might be desirable to use pfmod(7) to filter packets in the
36 * kernel when possible.
37 */
38
39 #ifndef lint
40 static const char rcsid[] =
41 "@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.88 2003-07-25 04:42:02 guy Exp $ (LBL)";
42 #endif
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <sys/types.h>
49 #include <sys/time.h>
50 #ifdef HAVE_SYS_BUFMOD_H
51 #include <sys/bufmod.h>
52 #endif
53 #include <sys/dlpi.h>
54 #ifdef HAVE_SYS_DLPI_EXT_H
55 #include <sys/dlpi_ext.h>
56 #endif
57 #ifdef HAVE_HPUX9
58 #include <sys/socket.h>
59 #endif
60 #ifdef DL_HP_PPA_ACK_OBS
61 #include <sys/stat.h>
62 #endif
63 #include <sys/stream.h>
64 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
65 #include <sys/systeminfo.h>
66 #endif
67
68 #ifdef HAVE_HPUX9
69 #include <net/if.h>
70 #endif
71
72 #include <ctype.h>
73 #ifdef HAVE_HPUX9
74 #include <nlist.h>
75 #endif
76 #include <errno.h>
77 #include <fcntl.h>
78 #include <memory.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <stropts.h>
83 #include <unistd.h>
84
85 #include "pcap-int.h"
86
87 #ifdef HAVE_OS_PROTO_H
88 #include "os-proto.h"
89 #endif
90
91 #ifndef PCAP_DEV_PREFIX
92 #ifdef _AIX
93 #define PCAP_DEV_PREFIX "/dev/dlpi"
94 #else
95 #define PCAP_DEV_PREFIX "/dev"
96 #endif
97 #endif
98
99 #define MAXDLBUF 8192
100
101 #ifdef HAVE_SYS_BUFMOD_H
102
103 /*
104 * Size of a bufmod chunk to pass upstream; that appears to be the biggest
105 * value to which you can set it, and setting it to that value (which
106 * is bigger than what appears to be the Solaris default of 8192)
107 * reduces the number of packet drops.
108 */
109 #define CHUNKSIZE 65536
110
111 /*
112 * Size of the buffer to allocate for packet data we read; it must be
113 * large enough to hold a chunk.
114 */
115 #define PKTBUFSIZE CHUNKSIZE
116
117 #else /* HAVE_SYS_BUFMOD_H */
118
119 /*
120 * Size of the buffer to allocate for packet data we read; this is
121 * what the value used to be - there's no particular reason why it
122 * should be tied to MAXDLBUF, but we'll leave it as this for now.
123 */
124 #define PKTBUFSIZE (MAXDLBUF * sizeof(bpf_u_int32))
125
126 #endif
127
128 /* Forwards */
129 static char *split_dname(char *, int *, char *);
130 static int dlattachreq(int, bpf_u_int32, char *);
131 static int dlbindack(int, char *, char *);
132 static int dlbindreq(int, bpf_u_int32, char *);
133 static int dlinfoack(int, char *, char *);
134 static int dlinforeq(int, char *);
135 static int dlokack(int, const char *, char *, char *);
136 static int recv_ack(int, int, const char *, char *, char *);
137 static char *dlstrerror(bpf_u_int32);
138 static char *dlprim(bpf_u_int32);
139 static int dlpromisconreq(int, bpf_u_int32, char *);
140 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
141 static char *get_release(bpf_u_int32 *, bpf_u_int32 *, bpf_u_int32 *);
142 #endif
143 static int send_request(int, char *, int, char *, char *);
144 #ifdef HAVE_SYS_BUFMOD_H
145 static int strioctl(int, int, int, char *);
146 #endif
147 #ifdef HAVE_HPUX9
148 static int dlpi_kread(int, off_t, void *, u_int, char *);
149 #endif
150 #ifdef HAVE_DEV_DLPI
151 static int get_dlpi_ppa(int, const char *, int, char *);
152 #endif
153
154 static int
155 pcap_stats_dlpi(pcap_t *p, struct pcap_stat *ps)
156 {
157
158 /*
159 * "ps_recv" counts packets handed to the filter, not packets
160 * that passed the filter. As filtering is done in userland,
161 * this does not include packets dropped because we ran out
162 * of buffer space.
163 *
164 * "ps_drop" counts packets dropped inside the DLPI service
165 * provider device device because of flow control requirements
166 * or resource exhaustion; it doesn't count packets dropped by
167 * the interface driver, or packets dropped upstream. As
168 * filtering is done in userland, it counts packets regardless
169 * of whether they would've passed the filter.
170 *
171 * These statistics don't include packets not yet read from
172 * the kernel by libpcap, but they may include packets not
173 * yet read from libpcap by the application.
174 */
175 *ps = p->md.stat;
176 return (0);
177 }
178
179 /* XXX Needed by HP-UX (at least) */
180 static bpf_u_int32 ctlbuf[MAXDLBUF];
181 static struct strbuf ctl = {
182 MAXDLBUF,
183 0,
184 (char *)ctlbuf
185 };
186
187 int
188 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
189 {
190 register int cc, n, caplen, origlen;
191 register u_char *bp, *ep, *pk;
192 register struct bpf_insn *fcode;
193 #ifdef HAVE_SYS_BUFMOD_H
194 register struct sb_hdr *sbp;
195 #ifdef LBL_ALIGN
196 struct sb_hdr sbhdr;
197 #endif
198 #endif
199 int flags;
200 struct strbuf data;
201 struct pcap_pkthdr pkthdr;
202
203 flags = 0;
204 cc = p->cc;
205 if (cc == 0) {
206 data.buf = (char *)p->buffer + p->offset;
207 data.maxlen = p->bufsize;
208 data.len = 0;
209 do {
210 if (getmsg(p->fd, &ctl, &data, &flags) < 0) {
211 /* Don't choke when we get ptraced */
212 if (errno == EINTR) {
213 cc = 0;
214 continue;
215 }
216 strlcpy(p->errbuf, pcap_strerror(errno),
217 sizeof(p->errbuf));
218 return (-1);
219 }
220 cc = data.len;
221 } while (cc == 0);
222 bp = p->buffer + p->offset;
223 } else
224 bp = p->bp;
225
226 /* Loop through packets */
227 fcode = p->fcode.bf_insns;
228 ep = bp + cc;
229 n = 0;
230 #ifdef HAVE_SYS_BUFMOD_H
231 while (bp < ep) {
232 #ifdef LBL_ALIGN
233 if ((long)bp & 3) {
234 sbp = &sbhdr;
235 memcpy(sbp, bp, sizeof(*sbp));
236 } else
237 #endif
238 sbp = (struct sb_hdr *)bp;
239 p->md.stat.ps_drop = sbp->sbh_drops;
240 pk = bp + sizeof(*sbp);
241 bp += sbp->sbh_totlen;
242 origlen = sbp->sbh_origlen;
243 caplen = sbp->sbh_msglen;
244 #else
245 origlen = cc;
246 caplen = min(p->snapshot, cc);
247 pk = bp;
248 bp += caplen;
249 #endif
250 ++p->md.stat.ps_recv;
251 if (bpf_filter(fcode, pk, origlen, caplen)) {
252 #ifdef HAVE_SYS_BUFMOD_H
253 pkthdr.ts.tv_sec = sbp->sbh_timestamp.tv_sec;
254 pkthdr.ts.tv_usec = sbp->sbh_timestamp.tv_usec;
255 #else
256 (void)gettimeofday(&pkthdr.ts, NULL);
257 #endif
258 pkthdr.len = origlen;
259 pkthdr.caplen = caplen;
260 /* Insure caplen does not exceed snapshot */
261 if (pkthdr.caplen > p->snapshot)
262 pkthdr.caplen = p->snapshot;
263 (*callback)(user, &pkthdr, pk);
264 if (++n >= cnt && cnt >= 0) {
265 p->cc = ep - bp;
266 p->bp = bp;
267 return (n);
268 }
269 }
270 #ifdef HAVE_SYS_BUFMOD_H
271 }
272 #endif
273 p->cc = 0;
274 return (n);
275 }
276
277 #ifndef DL_IPATM
278 #define DL_IPATM 0x12 /* ATM Classical IP interface */
279 #endif
280
281 #ifdef HAVE_SOLARIS
282 /*
283 * For SunATM.
284 */
285 #ifndef A_GET_UNITS
286 #define A_GET_UNITS (('A'<<8)|118)
287 #endif /* A_GET_UNITS */
288 #ifndef A_PROMISCON_REQ
289 #define A_PROMISCON_REQ (('A'<<8)|121)
290 #endif /* A_PROMISCON_REQ */
291 #endif /* HAVE_SOLARIS */
292
293 static void
294 pcap_close_dlpi(pcap_t *p)
295 {
296 if (p->buffer != NULL)
297 free(p->buffer);
298 if (p->fd >= 0)
299 close(p->fd);
300 }
301
302 pcap_t *
303 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
304 char *ebuf)
305 {
306 register char *cp;
307 register pcap_t *p;
308 int ppa;
309 #ifdef HAVE_SOLARIS
310 int isatm = 0;
311 #endif
312 register dl_info_ack_t *infop;
313 #ifdef HAVE_SYS_BUFMOD_H
314 bpf_u_int32 ss, chunksize;
315 #ifdef HAVE_SOLARIS
316 register char *release;
317 bpf_u_int32 osmajor, osminor, osmicro;
318 #endif
319 #endif
320 bpf_u_int32 buf[MAXDLBUF];
321 char dname[100];
322 #ifndef HAVE_DEV_DLPI
323 char dname2[100];
324 #endif
325
326 p = (pcap_t *)malloc(sizeof(*p));
327 if (p == NULL) {
328 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
329 return (NULL);
330 }
331 memset(p, 0, sizeof(*p));
332 p->fd = -1; /* indicate that it hasn't been opened yet */
333
334 #ifdef HAVE_DEV_DLPI
335 /*
336 ** Remove any "/dev/" on the front of the device.
337 */
338 cp = strrchr(device, '/');
339 if (cp == NULL)
340 strlcpy(dname, device, sizeof(dname));
341 else
342 strlcpy(dname, cp + 1, sizeof(dname));
343
344 /*
345 * Split the device name into a device type name and a unit number;
346 * chop off the unit number, so "dname" is just a device type name.
347 */
348 cp = split_dname(dname, &ppa, ebuf);
349 if (cp == NULL)
350 goto bad;
351 *cp = '\0';
352
353 /*
354 * Use "/dev/dlpi" as the device.
355 *
356 * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that
357 * the "dl_mjr_num" field is for the "major number of interface
358 * driver"; that's the major of "/dev/dlpi" on the system on
359 * which I tried this, but there may be DLPI devices that
360 * use a different driver, in which case we may need to
361 * search "/dev" for the appropriate device with that major
362 * device number, rather than hardwiring "/dev/dlpi".
363 */
364 cp = "/dev/dlpi";
365 if ((p->fd = open(cp, O_RDWR)) < 0) {
366 snprintf(ebuf, PCAP_ERRBUF_SIZE,
367 "%s: %s", cp, pcap_strerror(errno));
368 goto bad;
369 }
370
371 /*
372 * Get a table of all PPAs for that device, and search that
373 * table for the specified device type name and unit number.
374 */
375 ppa = get_dlpi_ppa(p->fd, dname, ppa, ebuf);
376 if (ppa < 0)
377 goto bad;
378 #else
379 /*
380 * If the device name begins with "/", assume it begins with
381 * the pathname of the directory containing the device to open;
382 * otherwise, concatenate the device directory name and the
383 * device name.
384 */
385 if (*device == '/')
386 strlcpy(dname, device, sizeof(dname));
387 else
388 snprintf(dname, sizeof(dname), "%s/%s", PCAP_DEV_PREFIX,
389 device);
390
391 /*
392 * Get the unit number, and a pointer to the end of the device
393 * type name.
394 */
395 cp = split_dname(dname, &ppa, ebuf);
396 if (cp == NULL)
397 goto bad;
398
399 /*
400 * Make a copy of the device pathname, and then remove the unit
401 * number from the device pathname.
402 */
403 strlcpy(dname2, dname, sizeof(dname));
404 *cp = '\0';
405
406 /* Try device without unit number */
407 if ((p->fd = open(dname, O_RDWR)) < 0) {
408 if (errno != ENOENT) {
409 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s", dname,
410 pcap_strerror(errno));
411 goto bad;
412 }
413
414 /* Try again with unit number */
415 if ((p->fd = open(dname2, O_RDWR)) < 0) {
416 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s", dname2,
417 pcap_strerror(errno));
418 goto bad;
419 }
420 /* XXX Assume unit zero */
421 ppa = 0;
422 }
423 #endif
424
425 p->snapshot = snaplen;
426
427 /*
428 ** Attach if "style 2" provider
429 */
430 if (dlinforeq(p->fd, ebuf) < 0 ||
431 dlinfoack(p->fd, (char *)buf, ebuf) < 0)
432 goto bad;
433 infop = &((union DL_primitives *)buf)->info_ack;
434 #ifdef HAVE_SOLARIS
435 if (infop->dl_mac_type == DL_IPATM)
436 isatm = 1;
437 #endif
438 if (infop->dl_provider_style == DL_STYLE2 &&
439 (dlattachreq(p->fd, ppa, ebuf) < 0 ||
440 dlokack(p->fd, "attach", (char *)buf, ebuf) < 0))
441 goto bad;
442 /*
443 ** Bind (defer if using HP-UX 9 or HP-UX 10.20, totally skip if
444 ** using SINIX)
445 */
446 #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20) && !defined(sinix)
447 #ifdef _AIX
448 /* According to IBM's AIX Support Line, the dl_sap value
449 ** should not be less than 0x600 (1536) for standard Ethernet.
450 ** However, we seem to get DL_BADADDR - "DLSAP addr in improper
451 ** format or invalid" - errors if we use 1537 on the "tr0"
452 ** device, which, given that its name starts with "tr" and that
453 ** it's IBM, probably means a Token Ring device. (Perhaps we
454 ** need to use 1537 on "/dev/dlpi/en" because that device is for
455 ** D/I/X Ethernet, the "SAP" is actually an Ethernet type, and
456 ** it rejects invalid Ethernet types.)
457 **
458 ** So if 1537 fails, we try 2, as Hyung Sik Yoon of IBM Korea
459 ** says that works on Token Ring (he says that 0 does *not*
460 ** work; perhaps that's considered an invalid LLC SAP value - I
461 ** assume the SAP value in a DLPI bind is an LLC SAP for network
462 ** types that use 802.2 LLC).
463 */
464 if ((dlbindreq(p->fd, 1537, ebuf) < 0 &&
465 dlbindreq(p->fd, 2, ebuf) < 0) ||
466 #else
467 if (dlbindreq(p->fd, 0, ebuf) < 0 ||
468 #endif
469 dlbindack(p->fd, (char *)buf, ebuf) < 0)
470 goto bad;
471 #endif
472
473 #ifdef HAVE_SOLARIS
474 if (isatm) {
475 /*
476 ** Have to turn on some special ATM promiscuous mode
477 ** for SunATM.
478 ** Do *NOT* turn regular promiscuous mode on; it doesn't
479 ** help, and may break things.
480 */
481 if (strioctl(p->fd, A_PROMISCON_REQ, 0, NULL) < 0) {
482 snprintf(ebuf, PCAP_ERRBUF_SIZE, "A_PROMISCON_REQ: %s",
483 pcap_strerror(errno));
484 goto bad;
485 }
486 } else
487 #endif
488 if (promisc) {
489 /*
490 ** Enable promiscuous
491 */
492 if (dlpromisconreq(p->fd, DL_PROMISC_PHYS, ebuf) < 0 ||
493 dlokack(p->fd, "promisc_phys", (char *)buf, ebuf) < 0)
494 goto bad;
495
496 /*
497 ** Try to enable multicast (you would have thought
498 ** promiscuous would be sufficient). (Skip if using
499 ** HP-UX or SINIX)
500 */
501 #if !defined(__hpux) && !defined(sinix)
502 if (dlpromisconreq(p->fd, DL_PROMISC_MULTI, ebuf) < 0 ||
503 dlokack(p->fd, "promisc_multi", (char *)buf, ebuf) < 0)
504 fprintf(stderr,
505 "WARNING: DL_PROMISC_MULTI failed (%s)\n", ebuf);
506 #endif
507 }
508 /*
509 ** Try to enable sap (when not in promiscuous mode when using
510 ** using HP-UX, when not doing SunATM on Solaris, and never
511 ** under SINIX)
512 */
513 #ifndef sinix
514 if (
515 #ifdef __hpux
516 !promisc &&
517 #endif
518 #ifdef HAVE_SOLARIS
519 !isatm &&
520 #endif
521 (dlpromisconreq(p->fd, DL_PROMISC_SAP, ebuf) < 0 ||
522 dlokack(p->fd, "promisc_sap", (char *)buf, ebuf) < 0)) {
523 /* Not fatal if promisc since the DL_PROMISC_PHYS worked */
524 if (promisc)
525 fprintf(stderr,
526 "WARNING: DL_PROMISC_SAP failed (%s)\n", ebuf);
527 else
528 goto bad;
529 }
530 #endif
531
532 /*
533 ** HP-UX 9 and HP-UX 10.20 must bind after setting promiscuous
534 ** options)
535 */
536 #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20)
537 if (dlbindreq(p->fd, 0, ebuf) < 0 ||
538 dlbindack(p->fd, (char *)buf, ebuf) < 0)
539 goto bad;
540 #endif
541
542 /*
543 ** Determine link type
544 */
545 if (dlinforeq(p->fd, ebuf) < 0 ||
546 dlinfoack(p->fd, (char *)buf, ebuf) < 0)
547 goto bad;
548
549 infop = &((union DL_primitives *)buf)->info_ack;
550 switch (infop->dl_mac_type) {
551
552 case DL_CSMACD:
553 case DL_ETHER:
554 p->linktype = DLT_EN10MB;
555 p->offset = 2;
556 break;
557
558 case DL_FDDI:
559 p->linktype = DLT_FDDI;
560 p->offset = 3;
561 break;
562
563 case DL_TPR:
564 p->linktype = DLT_IEEE802;
565 p->offset = 2;
566 break;
567
568 #ifdef HAVE_SOLARIS
569 case DL_IPATM:
570 p->linktype = DLT_SUNATM;
571 p->offset = 0; /* works for LANE and LLC encapsulation */
572 break;
573 #endif
574
575 default:
576 snprintf(ebuf, PCAP_ERRBUF_SIZE, "unknown mac type %lu",
577 (unsigned long)infop->dl_mac_type);
578 goto bad;
579 }
580
581 #ifdef DLIOCRAW
582 /*
583 ** This is a non standard SunOS hack to get the full raw link-layer
584 ** header.
585 */
586 if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) {
587 snprintf(ebuf, PCAP_ERRBUF_SIZE, "DLIOCRAW: %s",
588 pcap_strerror(errno));
589 goto bad;
590 }
591 #endif
592
593 #ifdef HAVE_SYS_BUFMOD_H
594 /*
595 ** Another non standard call to get the data nicely buffered
596 */
597 if (ioctl(p->fd, I_PUSH, "bufmod") != 0) {
598 snprintf(ebuf, PCAP_ERRBUF_SIZE, "I_PUSH bufmod: %s",
599 pcap_strerror(errno));
600 goto bad;
601 }
602
603 /*
604 ** Now that the bufmod is pushed lets configure it.
605 **
606 ** There is a bug in bufmod(7). When dealing with messages of
607 ** less than snaplen size it strips data from the beginning not
608 ** the end.
609 **
610 ** This bug is supposed to be fixed in 5.3.2. Also, there is a
611 ** patch available. Ask for bugid 1149065.
612 */
613 ss = snaplen;
614 #ifdef HAVE_SOLARIS
615 release = get_release(&osmajor, &osminor, &osmicro);
616 if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) &&
617 getenv("BUFMOD_FIXED") == NULL) {
618 fprintf(stderr,
619 "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.\n",
620 release);
621 ss = 0;
622 }
623 #endif
624 if (ss > 0 &&
625 strioctl(p->fd, SBIOCSSNAP, sizeof(ss), (char *)&ss) != 0) {
626 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SBIOCSSNAP: %s",
627 pcap_strerror(errno));
628 goto bad;
629 }
630
631 /*
632 ** Set up the bufmod timeout
633 */
634 if (to_ms != 0) {
635 struct timeval to;
636
637 to.tv_sec = to_ms / 1000;
638 to.tv_usec = (to_ms * 1000) % 1000000;
639 if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) {
640 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SBIOCSTIME: %s",
641 pcap_strerror(errno));
642 goto bad;
643 }
644 }
645
646 /*
647 ** Set the chunk length.
648 */
649 chunksize = CHUNKSIZE;
650 if (strioctl(p->fd, SBIOCSCHUNK, sizeof(chunksize), (char *)&chunksize)
651 != 0) {
652 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SBIOCSCHUNKP: %s",
653 pcap_strerror(errno));
654 goto bad;
655 }
656 #endif
657
658 /*
659 ** As the last operation flush the read side.
660 */
661 if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
662 snprintf(ebuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s",
663 pcap_strerror(errno));
664 goto bad;
665 }
666
667 /* Allocate data buffer */
668 p->bufsize = PKTBUFSIZE;
669 p->buffer = (u_char *)malloc(p->bufsize + p->offset);
670 if (p->buffer == NULL) {
671 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
672 goto bad;
673 }
674
675 p->setfilter_op = install_bpf_program; /* no kernel filtering */
676 p->stats_op = pcap_stats_dlpi;
677 p->close_op = pcap_close_dlpi;
678
679 return (p);
680 bad:
681 if (p->fd >= 0)
682 close(p->fd);
683 free(p);
684 return (NULL);
685 }
686
687 /*
688 * Split a device name into a device type name and a unit number;
689 * return the a pointer to the beginning of the unit number, which
690 * is the end of the device type name, and set "*unitp" to the unit
691 * number.
692 *
693 * Returns NULL on error, and fills "ebuf" with an error message.
694 */
695 static char *
696 split_dname(char *device, int *unitp, char *ebuf)
697 {
698 char *cp;
699 char *eos;
700 int unit;
701
702 /*
703 * Look for a number at the end of the device name string.
704 */
705 cp = device + strlen(device) - 1;
706 if (*cp < '0' || *cp > '9') {
707 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s missing unit number",
708 device);
709 return (NULL);
710 }
711
712 /* Digits at end of string are unit number */
713 while (cp-1 >= device && *(cp-1) >= '0' && *(cp-1) <= '9')
714 cp--;
715
716 unit = strtol(cp, &eos, 10);
717 if (*eos != '\0') {
718 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s bad unit number", device);
719 return (NULL);
720 }
721 *unitp = unit;
722 return (cp);
723 }
724
725 int
726 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
727 {
728 #ifdef HAVE_SOLARIS
729 int fd;
730 union {
731 u_int nunits;
732 char pad[516]; /* XXX - must be at least 513; is 516
733 in "atmgetunits" */
734 } buf;
735 char baname[2+1+1];
736 u_int i;
737
738 /*
739 * We may have to do special magic to get ATM devices.
740 */
741 if ((fd = open("/dev/ba", O_RDWR)) < 0) {
742 /*
743 * We couldn't open the "ba" device.
744 * For now, just give up; perhaps we should
745 * return an error if the problem is neither
746 * a "that device doesn't exist" error (ENOENT,
747 * ENXIO, etc.) or a "you're not allowed to do
748 * that" error (EPERM, EACCES).
749 */
750 return (0);
751 }
752
753 if (strioctl(fd, A_GET_UNITS, sizeof(buf), (char *)&buf) < 0) {
754 snprintf(errbuf, PCAP_ERRBUF_SIZE, "A_GET_UNITS: %s",
755 pcap_strerror(errno));
756 return (-1);
757 }
758 for (i = 0; i < buf.nunits; i++) {
759 snprintf(baname, sizeof baname, "ba%u", i);
760 if (pcap_add_if(alldevsp, baname, 0, NULL, errbuf) < 0)
761 return (-1);
762 }
763 #endif
764
765 return (0);
766 }
767
768 static int
769 send_request(int fd, char *ptr, int len, char *what, char *ebuf)
770 {
771 struct strbuf ctl;
772 int flags;
773
774 ctl.maxlen = 0;
775 ctl.len = len;
776 ctl.buf = ptr;
777
778 flags = 0;
779 if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) {
780 snprintf(ebuf, PCAP_ERRBUF_SIZE,
781 "send_request: putmsg \"%s\": %s",
782 what, pcap_strerror(errno));
783 return (-1);
784 }
785 return (0);
786 }
787
788 static int
789 recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf)
790 {
791 union DL_primitives *dlp;
792 struct strbuf ctl;
793 int flags;
794
795 ctl.maxlen = MAXDLBUF;
796 ctl.len = 0;
797 ctl.buf = bufp;
798
799 flags = 0;
800 if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) {
801 snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s getmsg: %s",
802 what, pcap_strerror(errno));
803 return (-1);
804 }
805
806 dlp = (union DL_primitives *) ctl.buf;
807 switch (dlp->dl_primitive) {
808
809 case DL_INFO_ACK:
810 case DL_BIND_ACK:
811 case DL_OK_ACK:
812 #ifdef DL_HP_PPA_ACK
813 case DL_HP_PPA_ACK:
814 #endif
815 /* These are OK */
816 break;
817
818 case DL_ERROR_ACK:
819 switch (dlp->error_ack.dl_errno) {
820
821 case DL_SYSERR:
822 snprintf(ebuf, PCAP_ERRBUF_SIZE,
823 "recv_ack: %s: UNIX error - %s",
824 what, pcap_strerror(dlp->error_ack.dl_unix_errno));
825 break;
826
827 default:
828 snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s: %s",
829 what, dlstrerror(dlp->error_ack.dl_errno));
830 break;
831 }
832 return (-1);
833
834 default:
835 snprintf(ebuf, PCAP_ERRBUF_SIZE,
836 "recv_ack: %s: Unexpected primitive ack %s",
837 what, dlprim(dlp->dl_primitive));
838 return (-1);
839 }
840
841 if (ctl.len < size) {
842 snprintf(ebuf, PCAP_ERRBUF_SIZE,
843 "recv_ack: %s: Ack too small (%d < %d)",
844 what, ctl.len, size);
845 return (-1);
846 }
847 return (ctl.len);
848 }
849
850 static char *
851 dlstrerror(bpf_u_int32 dl_errno)
852 {
853 static char errstring[6+2+8+1];
854
855 switch (dl_errno) {
856
857 case DL_ACCESS:
858 return ("Improper permissions for request");
859
860 case DL_BADADDR:
861 return ("DLSAP addr in improper format or invalid");
862
863 case DL_BADCORR:
864 return ("Seq number not from outstand DL_CONN_IND");
865
866 case DL_BADDATA:
867 return ("User data exceeded provider limit");
868
869 case DL_BADPPA:
870 #ifdef HAVE_DEV_DLPI
871 /*
872 * With a single "/dev/dlpi" device used for all
873 * DLPI providers, PPAs have nothing to do with
874 * unit numbers.
875 */
876 return ("Specified PPA was invalid");
877 #else
878 /*
879 * We have separate devices for separate devices;
880 * the PPA is just the unit number.
881 */
882 return ("Specified PPA (device unit) was invalid");
883 #endif
884
885 case DL_BADPRIM:
886 return ("Primitive received not known by provider");
887
888 case DL_BADQOSPARAM:
889 return ("QOS parameters contained invalid values");
890
891 case DL_BADQOSTYPE:
892 return ("QOS structure type is unknown/unsupported");
893
894 case DL_BADSAP:
895 return ("Bad LSAP selector");
896
897 case DL_BADTOKEN:
898 return ("Token used not an active stream");
899
900 case DL_BOUND:
901 return ("Attempted second bind with dl_max_conind");
902
903 case DL_INITFAILED:
904 return ("Physical link initialization failed");
905
906 case DL_NOADDR:
907 return ("Provider couldn't allocate alternate address");
908
909 case DL_NOTINIT:
910 return ("Physical link not initialized");
911
912 case DL_OUTSTATE:
913 return ("Primitive issued in improper state");
914
915 case DL_SYSERR:
916 return ("UNIX system error occurred");
917
918 case DL_UNSUPPORTED:
919 return ("Requested service not supplied by provider");
920
921 case DL_UNDELIVERABLE:
922 return ("Previous data unit could not be delivered");
923
924 case DL_NOTSUPPORTED:
925 return ("Primitive is known but not supported");
926
927 case DL_TOOMANY:
928 return ("Limit exceeded");
929
930 case DL_NOTENAB:
931 return ("Promiscuous mode not enabled");
932
933 case DL_BUSY:
934 return ("Other streams for PPA in post-attached");
935
936 case DL_NOAUTO:
937 return ("Automatic handling XID&TEST not supported");
938
939 case DL_NOXIDAUTO:
940 return ("Automatic handling of XID not supported");
941
942 case DL_NOTESTAUTO:
943 return ("Automatic handling of TEST not supported");
944
945 case DL_XIDAUTO:
946 return ("Automatic handling of XID response");
947
948 case DL_TESTAUTO:
949 return ("Automatic handling of TEST response");
950
951 case DL_PENDING:
952 return ("Pending outstanding connect indications");
953
954 default:
955 sprintf(errstring, "Error %02x", dl_errno);
956 return (errstring);
957 }
958 }
959
960 static char *
961 dlprim(bpf_u_int32 prim)
962 {
963 static char primbuf[80];
964
965 switch (prim) {
966
967 case DL_INFO_REQ:
968 return ("DL_INFO_REQ");
969
970 case DL_INFO_ACK:
971 return ("DL_INFO_ACK");
972
973 case DL_ATTACH_REQ:
974 return ("DL_ATTACH_REQ");
975
976 case DL_DETACH_REQ:
977 return ("DL_DETACH_REQ");
978
979 case DL_BIND_REQ:
980 return ("DL_BIND_REQ");
981
982 case DL_BIND_ACK:
983 return ("DL_BIND_ACK");
984
985 case DL_UNBIND_REQ:
986 return ("DL_UNBIND_REQ");
987
988 case DL_OK_ACK:
989 return ("DL_OK_ACK");
990
991 case DL_ERROR_ACK:
992 return ("DL_ERROR_ACK");
993
994 case DL_SUBS_BIND_REQ:
995 return ("DL_SUBS_BIND_REQ");
996
997 case DL_SUBS_BIND_ACK:
998 return ("DL_SUBS_BIND_ACK");
999
1000 case DL_UNITDATA_REQ:
1001 return ("DL_UNITDATA_REQ");
1002
1003 case DL_UNITDATA_IND:
1004 return ("DL_UNITDATA_IND");
1005
1006 case DL_UDERROR_IND:
1007 return ("DL_UDERROR_IND");
1008
1009 case DL_UDQOS_REQ:
1010 return ("DL_UDQOS_REQ");
1011
1012 case DL_CONNECT_REQ:
1013 return ("DL_CONNECT_REQ");
1014
1015 case DL_CONNECT_IND:
1016 return ("DL_CONNECT_IND");
1017
1018 case DL_CONNECT_RES:
1019 return ("DL_CONNECT_RES");
1020
1021 case DL_CONNECT_CON:
1022 return ("DL_CONNECT_CON");
1023
1024 case DL_TOKEN_REQ:
1025 return ("DL_TOKEN_REQ");
1026
1027 case DL_TOKEN_ACK:
1028 return ("DL_TOKEN_ACK");
1029
1030 case DL_DISCONNECT_REQ:
1031 return ("DL_DISCONNECT_REQ");
1032
1033 case DL_DISCONNECT_IND:
1034 return ("DL_DISCONNECT_IND");
1035
1036 case DL_RESET_REQ:
1037 return ("DL_RESET_REQ");
1038
1039 case DL_RESET_IND:
1040 return ("DL_RESET_IND");
1041
1042 case DL_RESET_RES:
1043 return ("DL_RESET_RES");
1044
1045 case DL_RESET_CON:
1046 return ("DL_RESET_CON");
1047
1048 default:
1049 (void) sprintf(primbuf, "unknown primitive 0x%x", prim);
1050 return (primbuf);
1051 }
1052 }
1053
1054 static int
1055 dlattachreq(int fd, bpf_u_int32 ppa, char *ebuf)
1056 {
1057 dl_attach_req_t req;
1058
1059 req.dl_primitive = DL_ATTACH_REQ;
1060 req.dl_ppa = ppa;
1061
1062 return (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf));
1063 }
1064
1065 static int
1066 dlbindreq(int fd, bpf_u_int32 sap, char *ebuf)
1067 {
1068
1069 dl_bind_req_t req;
1070
1071 memset((char *)&req, 0, sizeof(req));
1072 req.dl_primitive = DL_BIND_REQ;
1073 #ifdef DL_HP_RAWDLS
1074 req.dl_max_conind = 1; /* XXX magic number */
1075 /* 22 is INSAP as per the HP-UX DLPI Programmer's Guide */
1076 req.dl_sap = 22;
1077 req.dl_service_mode = DL_HP_RAWDLS;
1078 #else
1079 req.dl_sap = sap;
1080 #ifdef DL_CLDLS
1081 req.dl_service_mode = DL_CLDLS;
1082 #endif
1083 #endif
1084
1085 return (send_request(fd, (char *)&req, sizeof(req), "bind", ebuf));
1086 }
1087
1088 static int
1089 dlbindack(int fd, char *bufp, char *ebuf)
1090 {
1091
1092 return (recv_ack(fd, DL_BIND_ACK_SIZE, "bind", bufp, ebuf));
1093 }
1094
1095 static int
1096 dlpromisconreq(int fd, bpf_u_int32 level, char *ebuf)
1097 {
1098 dl_promiscon_req_t req;
1099
1100 req.dl_primitive = DL_PROMISCON_REQ;
1101 req.dl_level = level;
1102
1103 return (send_request(fd, (char *)&req, sizeof(req), "promiscon", ebuf));
1104 }
1105
1106 static int
1107 dlokack(int fd, const char *what, char *bufp, char *ebuf)
1108 {
1109
1110 return (recv_ack(fd, DL_OK_ACK_SIZE, what, bufp, ebuf));
1111 }
1112
1113
1114 static int
1115 dlinforeq(int fd, char *ebuf)
1116 {
1117 dl_info_req_t req;
1118
1119 req.dl_primitive = DL_INFO_REQ;
1120
1121 return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf));
1122 }
1123
1124 static int
1125 dlinfoack(int fd, char *bufp, char *ebuf)
1126 {
1127
1128 return (recv_ack(fd, DL_INFO_ACK_SIZE, "info", bufp, ebuf));
1129 }
1130
1131 #ifdef HAVE_SYS_BUFMOD_H
1132 static int
1133 strioctl(int fd, int cmd, int len, char *dp)
1134 {
1135 struct strioctl str;
1136 int rc;
1137
1138 str.ic_cmd = cmd;
1139 str.ic_timout = -1;
1140 str.ic_len = len;
1141 str.ic_dp = dp;
1142 rc = ioctl(fd, I_STR, &str);
1143
1144 if (rc < 0)
1145 return (rc);
1146 else
1147 return (str.ic_len);
1148 }
1149 #endif
1150
1151 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
1152 static char *
1153 get_release(bpf_u_int32 *majorp, bpf_u_int32 *minorp, bpf_u_int32 *microp)
1154 {
1155 char *cp;
1156 static char buf[32];
1157
1158 *majorp = 0;
1159 *minorp = 0;
1160 *microp = 0;
1161 if (sysinfo(SI_RELEASE, buf, sizeof(buf)) < 0)
1162 return ("?");
1163 cp = buf;
1164 if (!isdigit((unsigned char)*cp))
1165 return (buf);
1166 *majorp = strtol(cp, &cp, 10);
1167 if (*cp++ != '.')
1168 return (buf);
1169 *minorp = strtol(cp, &cp, 10);
1170 if (*cp++ != '.')
1171 return (buf);
1172 *microp = strtol(cp, &cp, 10);
1173 return (buf);
1174 }
1175 #endif
1176
1177 #ifdef DL_HP_PPA_ACK_OBS
1178 /*
1179 * Under HP-UX 10 and HP-UX 11, we can ask for the ppa
1180 */
1181
1182
1183 /*
1184 * Determine ppa number that specifies ifname.
1185 *
1186 * If the "dl_hp_ppa_info_t" doesn't have a "dl_module_id_1" member,
1187 * the code that's used here is the old code for HP-UX 10.x.
1188 *
1189 * However, HP-UX 10.20, at least, appears to have such a member
1190 * in its "dl_hp_ppa_info_t" structure, so the new code is used.
1191 * The new code didn't work on an old 10.20 system on which Rick
1192 * Jones of HP tried it, but with later patches installed, it
1193 * worked - it appears that the older system had those members but
1194 * didn't put anything in them, so, if the search by name fails, we
1195 * do the old search.
1196 *
1197 * Rick suggests that making sure your system is "up on the latest
1198 * lancommon/DLPI/driver patches" is probably a good idea; it'd fix
1199 * that problem, as well as allowing libpcap to see packets sent
1200 * from the system on which the libpcap application is being run.
1201 * (On 10.20, in addition to getting the latest patches, you need
1202 * to turn the kernel "lanc_outbound_promisc_flag" flag on with ADB;
1203 * a posting to "comp.sys.hp.hpux" at
1204 *
1205 * http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=558092266
1206 *
1207 * says that, to see the machine's outgoing traffic, you'd need to
1208 * apply the right patches to your system, and also set that variable
1209 * with:
1210
1211 echo 'lanc_outbound_promisc_flag/W1' | /usr/bin/adb -w /stand/vmunix /dev/kmem
1212
1213 * which could be put in, for example, "/sbin/init.d/lan".
1214 *
1215 * Setting the variable is not necessary on HP-UX 11.x.
1216 */
1217 static int
1218 get_dlpi_ppa(register int fd, register const char *device, register int unit,
1219 register char *ebuf)
1220 {
1221 register dl_hp_ppa_ack_t *ap;
1222 register dl_hp_ppa_info_t *ipstart, *ip;
1223 register int i;
1224 char dname[100];
1225 register u_long majdev;
1226 struct stat statbuf;
1227 dl_hp_ppa_req_t req;
1228 char buf[MAXDLBUF];
1229 char *ppa_data_buf;
1230 dl_hp_ppa_ack_t *dlp;
1231 struct strbuf ctl;
1232 int flags;
1233 int ppa;
1234
1235 memset((char *)&req, 0, sizeof(req));
1236 req.dl_primitive = DL_HP_PPA_REQ;
1237
1238 memset((char *)buf, 0, sizeof(buf));
1239 if (send_request(fd, (char *)&req, sizeof(req), "hpppa", ebuf) < 0)
1240 return (-1);
1241
1242 ctl.maxlen = DL_HP_PPA_ACK_SIZE;
1243 ctl.len = 0;
1244 ctl.buf = (char *)buf;
1245
1246 flags = 0;
1247 /*
1248 * DLPI may return a big chunk of data for a DL_HP_PPA_REQ. The normal
1249 * recv_ack will fail because it set the maxlen to MAXDLBUF (8192)
1250 * which is NOT big enough for a DL_HP_PPA_REQ.
1251 *
1252 * This causes libpcap applications to fail on a system with HP-APA
1253 * installed.
1254 *
1255 * To figure out how big the returned data is, we first call getmsg
1256 * to get the small head and peek at the head to get the actual data
1257 * length, and then issue another getmsg to get the actual PPA data.
1258 */
1259 /* get the head first */
1260 if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
1261 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1262 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno));
1263 return (-1);
1264 }
1265
1266 dlp = (dl_hp_ppa_ack_t *)ctl.buf;
1267 if (dlp->dl_primitive != DL_HP_PPA_ACK) {
1268 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1269 "get_dlpi_ppa: hpppa unexpected primitive ack 0x%x",
1270 (bpf_u_int32)dlp->dl_primitive);
1271 return (-1);
1272 }
1273
1274 if (ctl.len < DL_HP_PPA_ACK_SIZE) {
1275 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1276 "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1277 ctl.len, (unsigned long)DL_HP_PPA_ACK_SIZE);
1278 return (-1);
1279 }
1280
1281 /* allocate buffer */
1282 if ((ppa_data_buf = (char *)malloc(dlp->dl_length)) == NULL) {
1283 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1284 "get_dlpi_ppa: hpppa malloc: %s", pcap_strerror(errno));
1285 return (-1);
1286 }
1287 ctl.maxlen = dlp->dl_length;
1288 ctl.len = 0;
1289 ctl.buf = (char *)ppa_data_buf;
1290 /* get the data */
1291 if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
1292 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1293 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno));
1294 free(ppa_data_buf);
1295 return (-1);
1296 }
1297 if (ctl.len < dlp->dl_length) {
1298 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1299 "get_dlpi_ppa: hpppa ack too small (%d < %d)",
1300 ctl.len, dlp->dl_length);
1301 free(ppa_data_buf);
1302 return (-1);
1303 }
1304
1305 ap = (dl_hp_ppa_ack_t *)buf;
1306 ipstart = (dl_hp_ppa_info_t *)ppa_data_buf;
1307 ip = ipstart;
1308
1309 #ifdef HAVE_HP_PPA_INFO_T_DL_MODULE_ID_1
1310 /*
1311 * The "dl_hp_ppa_info_t" structure has a "dl_module_id_1"
1312 * member that should, in theory, contain the part of the
1313 * name for the device that comes before the unit number,
1314 * and should also have a "dl_module_id_2" member that may
1315 * contain an alternate name (e.g., I think Ethernet devices
1316 * have both "lan", for "lanN", and "snap", for "snapN", with
1317 * the former being for Ethernet packets and the latter being
1318 * for 802.3/802.2 packets).
1319 *
1320 * Search for the device that has the specified name and
1321 * instance number.
1322 */
1323 for (i = 0; i < ap->dl_count; i++) {
1324 if ((strcmp((const char *)ip->dl_module_id_1, device) == 0 ||
1325 strcmp((const char *)ip->dl_module_id_2, device) == 0) &&
1326 ip->dl_instance_num == unit)
1327 break;
1328
1329 ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
1330 }
1331 #else
1332 /*
1333 * We don't have that member, so the search is impossible; make it
1334 * look as if the search failed.
1335 */
1336 i = ap->dl_count;
1337 #endif
1338
1339 if (i == ap->dl_count) {
1340 /*
1341 * Well, we didn't, or can't, find the device by name.
1342 *
1343 * HP-UX 10.20, whilst it has "dl_module_id_1" and
1344 * "dl_module_id_2" fields in the "dl_hp_ppa_info_t",
1345 * doesn't seem to fill them in unless the system is
1346 * at a reasonably up-to-date patch level.
1347 *
1348 * Older HP-UX 10.x systems might not have those fields
1349 * at all.
1350 *
1351 * Therefore, we'll search for the entry with the major
1352 * device number of a device with the name "/dev/<dev><unit>",
1353 * if such a device exists, as the old code did.
1354 */
1355 snprintf(dname, sizeof(dname), "/dev/%s%d", device, unit);
1356 if (stat(dname, &statbuf) < 0) {
1357 snprintf(ebuf, PCAP_ERRBUF_SIZE, "stat: %s: %s",
1358 dname, pcap_strerror(errno));
1359 return (-1);
1360 }
1361 majdev = major(statbuf.st_rdev);
1362
1363 ip = ipstart;
1364
1365 for (i = 0; i < ap->dl_count; i++) {
1366 if (ip->dl_mjr_num == majdev &&
1367 ip->dl_instance_num == unit)
1368 break;
1369
1370 ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
1371 }
1372 }
1373 if (i == ap->dl_count) {
1374 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1375 "can't find /dev/dlpi PPA for %s%d", device, unit);
1376 return (-1);
1377 }
1378 if (ip->dl_hdw_state == HDW_DEAD) {
1379 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1380 "%s%d: hardware state: DOWN\n", device, unit);
1381 free(ppa_data_buf);
1382 return (-1);
1383 }
1384 ppa = ip->dl_ppa;
1385 free(ppa_data_buf);
1386 return (ppa);
1387 }
1388 #endif
1389
1390 #ifdef HAVE_HPUX9
1391 /*
1392 * Under HP-UX 9, there is no good way to determine the ppa.
1393 * So punt and read it from /dev/kmem.
1394 */
1395 static struct nlist nl[] = {
1396 #define NL_IFNET 0
1397 { "ifnet" },
1398 { "" }
1399 };
1400
1401 static char path_vmunix[] = "/hp-ux";
1402
1403 /* Determine ppa number that specifies ifname */
1404 static int
1405 get_dlpi_ppa(register int fd, register const char *ifname, register int unit,
1406 register char *ebuf)
1407 {
1408 register const char *cp;
1409 register int kd;
1410 void *addr;
1411 struct ifnet ifnet;
1412 char if_name[sizeof(ifnet.if_name) + 1];
1413
1414 cp = strrchr(ifname, '/');
1415 if (cp != NULL)
1416 ifname = cp + 1;
1417 if (nlist(path_vmunix, &nl) < 0) {
1418 snprintf(ebuf, PCAP_ERRBUF_SIZE, "nlist %s failed",
1419 path_vmunix);
1420 return (-1);
1421 }
1422 if (nl[NL_IFNET].n_value == 0) {
1423 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1424 "could't find %s kernel symbol",
1425 nl[NL_IFNET].n_name);
1426 return (-1);
1427 }
1428 kd = open("/dev/kmem", O_RDONLY);
1429 if (kd < 0) {
1430 snprintf(ebuf, PCAP_ERRBUF_SIZE, "kmem open: %s",
1431 pcap_strerror(errno));
1432 return (-1);
1433 }
1434 if (dlpi_kread(kd, nl[NL_IFNET].n_value,
1435 &addr, sizeof(addr), ebuf) < 0) {
1436 close(kd);
1437 return (-1);
1438 }
1439 for (; addr != NULL; addr = ifnet.if_next) {
1440 if (dlpi_kread(kd, (off_t)addr,
1441 &ifnet, sizeof(ifnet), ebuf) < 0 ||
1442 dlpi_kread(kd, (off_t)ifnet.if_name,
1443 if_name, sizeof(ifnet.if_name), ebuf) < 0) {
1444 (void)close(kd);
1445 return (-1);
1446 }
1447 if_name[sizeof(ifnet.if_name)] = '\0';
1448 if (strcmp(if_name, ifname) == 0 && ifnet.if_unit == unit)
1449 return (ifnet.if_index);
1450 }
1451
1452 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't find %s", ifname);
1453 return (-1);
1454 }
1455
1456 static int
1457 dlpi_kread(register int fd, register off_t addr,
1458 register void *buf, register u_int len, register char *ebuf)
1459 {
1460 register int cc;
1461
1462 if (lseek(fd, addr, SEEK_SET) < 0) {
1463 snprintf(ebuf, PCAP_ERRBUF_SIZE, "lseek: %s",
1464 pcap_strerror(errno));
1465 return (-1);
1466 }
1467 cc = read(fd, buf, len);
1468 if (cc < 0) {
1469 snprintf(ebuf, PCAP_ERRBUF_SIZE, "read: %s",
1470 pcap_strerror(errno));
1471 return (-1);
1472 } else if (cc != len) {
1473 snprintf(ebuf, PCAP_ERRBUF_SIZE, "short read (%d != %d)", cc,
1474 len);
1475 return (-1);
1476 }
1477 return (cc);
1478 }
1479 #endif
1480
1481 int
1482 pcap_set_datalink_platform(pcap_t *p, int dlt)
1483 {
1484 return (0);
1485 }