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