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