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