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