]> The Tcpdump Group git mirrors - libpcap/blob - pcap-dlpi.c
bb1548dcbc7b6fc24b3fba37e30f09e4e17713af
[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.57 2000-07-06 01:50:36 assar Exp $ (LBL)";
42 #endif
43
44 #include <sys/types.h>
45 #include <sys/time.h>
46 #ifdef HAVE_SYS_BUFMOD_H
47 #include <sys/bufmod.h>
48 #endif
49 #include <sys/dlpi.h>
50 #ifdef HAVE_SYS_DLPI_EXT_H
51 #include <sys/dlpi_ext.h>
52 #endif
53 #ifdef HAVE_HPUX9
54 #include <sys/socket.h>
55 #endif
56 #ifdef DL_HP_PPA_ACK_OBS
57 #include <sys/stat.h>
58 #endif
59 #include <sys/stream.h>
60 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
61 #include <sys/systeminfo.h>
62 #endif
63
64 #ifdef HAVE_HPUX9
65 #include <net/if.h>
66 #endif
67
68 #include <ctype.h>
69 #ifdef HAVE_HPUX9
70 #include <nlist.h>
71 #endif
72 #include <errno.h>
73 #include <fcntl.h>
74 #include <memory.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <stropts.h>
79 #include <unistd.h>
80
81 #include "pcap-int.h"
82
83 #include "gnuc.h"
84 #ifdef HAVE_OS_PROTO_H
85 #include "os-proto.h"
86 #endif
87
88 #ifndef PCAP_DEV_PREFIX
89 #define PCAP_DEV_PREFIX "/dev"
90 #endif
91
92 #define MAXDLBUF 8192
93
94 /* Forwards */
95 static int dlattachreq(int, bpf_u_int32, char *);
96 static int dlbindack(int, char *, char *);
97 static int dlbindreq(int, bpf_u_int32, char *);
98 static int dlinfoack(int, char *, char *);
99 static int dlinforeq(int, char *);
100 static int dlokack(int, const char *, char *, char *);
101 static int recv_ack(int, int, const char *, char *, char *);
102 static int dlpromisconreq(int, bpf_u_int32, char *);
103 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
104 static char *get_release(bpf_u_int32 *, bpf_u_int32 *, bpf_u_int32 *);
105 #endif
106 static int send_request(int, char *, int, char *, char *);
107 #ifdef HAVE_SYS_BUFMOD_H
108 static int strioctl(int, int, int, char *);
109 #endif
110 #ifdef HAVE_HPUX9
111 static int dlpi_kread(int, off_t, void *, u_int, char *);
112 #endif
113 #ifdef HAVE_DEV_DLPI
114 static int get_dlpi_ppa(int, const char *, int, char *);
115 #endif
116
117 int
118 pcap_stats(pcap_t *p, struct pcap_stat *ps)
119 {
120
121 *ps = p->md.stat;
122 return (0);
123 }
124
125 /* XXX Needed by HP-UX (at least) */
126 static bpf_u_int32 ctlbuf[MAXDLBUF];
127 static struct strbuf ctl = {
128 MAXDLBUF,
129 0,
130 (char *)ctlbuf
131 };
132
133 int
134 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
135 {
136 register int cc, n, caplen, origlen;
137 register u_char *bp, *ep, *pk;
138 register struct bpf_insn *fcode;
139 #ifdef HAVE_SYS_BUFMOD_H
140 register struct sb_hdr *sbp;
141 #ifdef LBL_ALIGN
142 struct sb_hdr sbhdr;
143 #endif
144 #endif
145 int flags;
146 struct strbuf data;
147 struct pcap_pkthdr pkthdr;
148
149 flags = 0;
150 cc = p->cc;
151 if (cc == 0) {
152 data.buf = (char *)p->buffer + p->offset;
153 data.maxlen = MAXDLBUF;
154 data.len = 0;
155 do {
156 if (getmsg(p->fd, &ctl, &data, &flags) < 0) {
157 /* Don't choke when we get ptraced */
158 if (errno == EINTR) {
159 cc = 0;
160 continue;
161 }
162 strlcpy(p->errbuf, pcap_strerror(errno),
163 sizeof(p->errbuf));
164 return (-1);
165 }
166 cc = data.len;
167 } while (cc == 0);
168 bp = p->buffer + p->offset;
169 } else
170 bp = p->bp;
171
172 /* Loop through packets */
173 fcode = p->fcode.bf_insns;
174 ep = bp + cc;
175 n = 0;
176 #ifdef HAVE_SYS_BUFMOD_H
177 while (bp < ep) {
178 #ifdef LBL_ALIGN
179 if ((long)bp & 3) {
180 sbp = &sbhdr;
181 memcpy(sbp, bp, sizeof(*sbp));
182 } else
183 #endif
184 sbp = (struct sb_hdr *)bp;
185 p->md.stat.ps_drop += sbp->sbh_drops;
186 pk = bp + sizeof(*sbp);
187 bp += sbp->sbh_totlen;
188 origlen = sbp->sbh_origlen;
189 caplen = sbp->sbh_msglen;
190 #else
191 origlen = cc;
192 caplen = min(p->snapshot, cc);
193 pk = bp;
194 bp += caplen;
195 #endif
196 ++p->md.stat.ps_recv;
197 if (bpf_filter(fcode, pk, origlen, caplen)) {
198 #ifdef HAVE_SYS_BUFMOD_H
199 pkthdr.ts = sbp->sbh_timestamp;
200 #else
201 (void)gettimeofday(&pkthdr.ts, NULL);
202 #endif
203 pkthdr.len = origlen;
204 pkthdr.caplen = caplen;
205 /* Insure caplen does not exceed snapshot */
206 if (pkthdr.caplen > p->snapshot)
207 pkthdr.caplen = p->snapshot;
208 (*callback)(user, &pkthdr, pk);
209 if (++n >= cnt && cnt >= 0) {
210 p->cc = ep - bp;
211 p->bp = bp;
212 return (n);
213 }
214 }
215 #ifdef HAVE_SYS_BUFMOD_H
216 }
217 #endif
218 p->cc = 0;
219 return (n);
220 }
221
222 pcap_t *
223 pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
224 {
225 register char *cp;
226 char *eos;
227 register pcap_t *p;
228 register int ppa;
229 register dl_info_ack_t *infop;
230 #ifdef HAVE_SYS_BUFMOD_H
231 bpf_u_int32 ss, flag;
232 #ifdef HAVE_SOLARIS
233 register char *release;
234 bpf_u_int32 osmajor, osminor, osmicro;
235 #endif
236 #endif
237 bpf_u_int32 buf[MAXDLBUF];
238 char dname[100];
239 #ifndef HAVE_DEV_DLPI
240 char dname2[100];
241 #endif
242
243 p = (pcap_t *)malloc(sizeof(*p));
244 if (p == NULL) {
245 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
246 return (NULL);
247 }
248 memset(p, 0, sizeof(*p));
249
250 #ifdef HAVE_DEV_DLPI
251 /*
252 ** Remove any "/dev/" on the front of the device.
253 */
254 cp = strrchr(device, '/');
255 if (cp == NULL)
256 cp = device;
257 else
258 cp++;
259 strlcpy(dname, cp, sizeof(dname));
260
261 /*
262 * Split the name into a device type and a unit number.
263 */
264 cp = strpbrk(dname, "0123456789");
265 if (cp == NULL) {
266 snprintf(ebuf, PCAP_ERRBUF_SIZE,
267 "%s missing unit number", device);
268 goto bad;
269 }
270 ppa = strtol(cp, &eos, 10);
271 if (*eos != '\0') {
272 snprintf(ebuf, PCAP_ERRBUF_SIZE,
273 "%s bad unit number", device);
274 goto bad;
275 }
276 *cp = '\0';
277
278 /*
279 * Use "/dev/dlpi" as the device.
280 *
281 * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that
282 * the "dl_mjr_num" field is for the "major number of interface
283 * driver"; that's the major of "/dev/dlpi" on the system on
284 * which I tried this, but there may be DLPI devices that
285 * use a different driver, in which case we may need to
286 * search "/dev" for the appropriate device with that major
287 * device number, rather than hardwiring "/dev/dlpi".
288 */
289 cp = "/dev/dlpi";
290 if ((p->fd = open(cp, O_RDWR)) < 0) {
291 snprintf(ebuf, PCAP_ERRBUF_SIZE,
292 "%s: %s", cp, pcap_strerror(errno));
293 goto bad;
294 }
295
296 /*
297 * Get a table of all PPAs for that device, and search that
298 * table for the specified device type name and unit number.
299 */
300 ppa = get_dlpi_ppa(p->fd, dname, ppa, ebuf);
301 if (ppa < 0)
302 goto bad;
303 #else
304 /*
305 ** Determine device and ppa
306 */
307 cp = strpbrk(device, "0123456789");
308 if (cp == NULL) {
309 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s missing unit number",
310 device);
311 goto bad;
312 }
313 ppa = strtol(cp, &eos, 10);
314 if (*eos != '\0') {
315 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s bad unit number", device);
316 goto bad;
317 }
318
319 if (*device == '/')
320 strlcpy(dname, device, sizeof(dname));
321 else
322 snprintf(dname, sizeof(dname), "%s/%s", PCAP_DEV_PREFIX,
323 device);
324
325 /* Try device without unit number */
326 strlcpy(dname2, dname, sizeof(dname2));
327 cp = strchr(dname, *cp);
328 *cp = '\0';
329 if ((p->fd = open(dname, O_RDWR)) < 0) {
330 if (errno != ENOENT) {
331 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s", dname,
332 pcap_strerror(errno));
333 goto bad;
334 }
335
336 /* Try again with unit number */
337 if ((p->fd = open(dname2, O_RDWR)) < 0) {
338 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s", dname2,
339 pcap_strerror(errno));
340 goto bad;
341 }
342 /* XXX Assume unit zero */
343 ppa = 0;
344 }
345 #endif
346
347 p->snapshot = snaplen;
348
349 /*
350 ** Attach if "style 2" provider
351 */
352 if (dlinforeq(p->fd, ebuf) < 0 ||
353 dlinfoack(p->fd, (char *)buf, ebuf) < 0)
354 goto bad;
355 infop = &((union DL_primitives *)buf)->info_ack;
356 if (infop->dl_provider_style == DL_STYLE2 &&
357 (dlattachreq(p->fd, ppa, ebuf) < 0 ||
358 dlokack(p->fd, "attach", (char *)buf, ebuf) < 0))
359 goto bad;
360 /*
361 ** Bind (defer if using HP-UX 9 or HP-UX 10.20, totally skip if
362 ** using SINIX)
363 */
364 #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20) && !defined(sinix)
365 #ifdef _AIX
366 /* According to IBM's AIX Support Line, the dl_sap value
367 ** should not be less than 0x600 (1536) for standard ethernet
368 */
369 if (dlbindreq(p->fd, 1537, ebuf) < 0 ||
370 #else
371 if (dlbindreq(p->fd, 0, ebuf) < 0 ||
372 #endif
373 dlbindack(p->fd, (char *)buf, ebuf) < 0)
374 goto bad;
375 #endif
376
377 if (promisc) {
378 /*
379 ** Enable promiscuous
380 */
381 if (dlpromisconreq(p->fd, DL_PROMISC_PHYS, ebuf) < 0 ||
382 dlokack(p->fd, "promisc_phys", (char *)buf, ebuf) < 0)
383 goto bad;
384
385 /*
386 ** Try to enable multicast (you would have thought
387 ** promiscuous would be sufficient). (Skip if using
388 ** HP-UX or SINIX)
389 */
390 #if !defined(__hpux) && !defined(sinix)
391 if (dlpromisconreq(p->fd, DL_PROMISC_MULTI, ebuf) < 0 ||
392 dlokack(p->fd, "promisc_multi", (char *)buf, ebuf) < 0)
393 fprintf(stderr,
394 "WARNING: DL_PROMISC_MULTI failed (%s)\n", ebuf);
395 #endif
396 }
397 /*
398 ** Try to enable sap (when not in promiscuous mode when using
399 ** using HP-UX and never under SINIX)
400 */
401 #ifndef sinix
402 if (
403 #ifdef __hpux
404 !promisc &&
405 #endif
406 (dlpromisconreq(p->fd, DL_PROMISC_SAP, ebuf) < 0 ||
407 dlokack(p->fd, "promisc_sap", (char *)buf, ebuf) < 0)) {
408 /* Not fatal if promisc since the DL_PROMISC_PHYS worked */
409 if (promisc)
410 fprintf(stderr,
411 "WARNING: DL_PROMISC_SAP failed (%s)\n", ebuf);
412 else
413 goto bad;
414 }
415 #endif
416
417 /*
418 ** HP-UX 9 and HP-UX 10.20 must bind after setting promiscuous
419 ** options)
420 */
421 #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20)
422 if (dlbindreq(p->fd, 0, ebuf) < 0 ||
423 dlbindack(p->fd, (char *)buf, ebuf) < 0)
424 goto bad;
425 #endif
426
427 /*
428 ** Determine link type
429 */
430 if (dlinforeq(p->fd, ebuf) < 0 ||
431 dlinfoack(p->fd, (char *)buf, ebuf) < 0)
432 goto bad;
433
434 infop = &((union DL_primitives *)buf)->info_ack;
435 switch (infop->dl_mac_type) {
436
437 case DL_CSMACD:
438 case DL_ETHER:
439 p->linktype = DLT_EN10MB;
440 p->offset = 2;
441 break;
442
443 case DL_FDDI:
444 p->linktype = DLT_FDDI;
445 p->offset = 3;
446 break;
447
448 default:
449 snprintf(ebuf, PCAP_ERRBUF_SIZE, "unknown mac type 0x%lu",
450 infop->dl_mac_type);
451 goto bad;
452 }
453
454 #ifdef DLIOCRAW
455 /*
456 ** This is a non standard SunOS hack to get the ethernet header.
457 */
458 if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) {
459 snprintf(ebuf, PCAP_ERRBUF_SIZE, "DLIOCRAW: %s",
460 pcap_strerror(errno));
461 goto bad;
462 }
463 #endif
464
465 #ifdef HAVE_SYS_BUFMOD_H
466 /*
467 ** Another non standard call to get the data nicely buffered
468 */
469 if (ioctl(p->fd, I_PUSH, "bufmod") != 0) {
470 snprintf(ebuf, PCAP_ERRBUF_SIZE, "I_PUSH bufmod: %s",
471 pcap_strerror(errno));
472 goto bad;
473 }
474
475 /*
476 ** Now that the bufmod is pushed lets configure it.
477 **
478 ** There is a bug in bufmod(7). When dealing with messages of
479 ** less than snaplen size it strips data from the beginning not
480 ** the end.
481 **
482 ** This bug is supposed to be fixed in 5.3.2. Also, there is a
483 ** patch available. Ask for bugid 1149065.
484 */
485 ss = snaplen;
486 #ifdef HAVE_SOLARIS
487 release = get_release(&osmajor, &osminor, &osmicro);
488 if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) &&
489 getenv("BUFMOD_FIXED") == NULL) {
490 fprintf(stderr,
491 "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.\n",
492 release);
493 ss = 0;
494 }
495 #endif
496 if (ss > 0 &&
497 strioctl(p->fd, SBIOCSSNAP, sizeof(ss), (char *)&ss) != 0) {
498 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SBIOCSSNAP: %s",
499 pcap_strerror(errno));
500 goto bad;
501 }
502
503 /*
504 ** Set up the bufmod flags
505 */
506 if (strioctl(p->fd, SBIOCGFLAGS, sizeof(flag), (char *)&flag) < 0) {
507 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SBIOCGFLAGS: %s",
508 pcap_strerror(errno));
509 goto bad;
510 }
511 flag |= SB_NO_DROPS;
512 if (strioctl(p->fd, SBIOCSFLAGS, sizeof(flag), (char *)&flag) != 0) {
513 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SBIOCSFLAGS: %s",
514 pcap_strerror(errno));
515 goto bad;
516 }
517 /*
518 ** Set up the bufmod timeout
519 */
520 if (to_ms != 0) {
521 struct timeval to;
522
523 to.tv_sec = to_ms / 1000;
524 to.tv_usec = (to_ms * 1000) % 1000000;
525 if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) {
526 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SBIOCSTIME: %s",
527 pcap_strerror(errno));
528 goto bad;
529 }
530 }
531 #endif
532
533 /*
534 ** As the last operation flush the read side.
535 */
536 if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
537 snprintf(ebuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s",
538 pcap_strerror(errno));
539 goto bad;
540 }
541 /* Allocate data buffer */
542 p->bufsize = MAXDLBUF * sizeof(bpf_u_int32);
543 p->buffer = (u_char *)malloc(p->bufsize + p->offset);
544
545 return (p);
546 bad:
547 free(p);
548 return (NULL);
549 }
550
551 int
552 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
553 {
554
555 p->fcode = *fp;
556 return (0);
557 }
558
559 static int
560 send_request(int fd, char *ptr, int len, char *what, char *ebuf)
561 {
562 struct strbuf ctl;
563 int flags;
564
565 ctl.maxlen = 0;
566 ctl.len = len;
567 ctl.buf = ptr;
568
569 flags = 0;
570 if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) {
571 snprintf(ebuf, PCAP_ERRBUF_SIZE,
572 "send_request: putmsg \"%s\": %s",
573 what, pcap_strerror(errno));
574 return (-1);
575 }
576 return (0);
577 }
578
579 static int
580 recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf)
581 {
582 union DL_primitives *dlp;
583 struct strbuf ctl;
584 int flags;
585
586 ctl.maxlen = MAXDLBUF;
587 ctl.len = 0;
588 ctl.buf = bufp;
589
590 flags = 0;
591 if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) {
592 snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s getmsg: %s",
593 what, pcap_strerror(errno));
594 return (-1);
595 }
596
597 dlp = (union DL_primitives *) ctl.buf;
598 switch (dlp->dl_primitive) {
599
600 case DL_INFO_ACK:
601 case DL_BIND_ACK:
602 case DL_OK_ACK:
603 #ifdef DL_HP_PPA_ACK
604 case DL_HP_PPA_ACK:
605 #endif
606
607 /* These are OK */
608 break;
609
610 case DL_ERROR_ACK:
611 switch (dlp->error_ack.dl_errno) {
612
613 case DL_BADPPA:
614 snprintf(ebuf, PCAP_ERRBUF_SIZE,
615 "recv_ack: %s bad ppa (device unit)", what);
616 break;
617
618
619 case DL_SYSERR:
620 snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s: %s",
621 what, pcap_strerror(dlp->error_ack.dl_unix_errno));
622 break;
623
624 case DL_UNSUPPORTED:
625 snprintf(ebuf, PCAP_ERRBUF_SIZE,
626 "recv_ack: %s: Service not supplied by provider",
627 what);
628 break;
629
630 default:
631 snprintf(ebuf, PCAP_ERRBUF_SIZE,
632 "recv_ack: %s error 0x%x",
633 what, (bpf_u_int32)dlp->error_ack.dl_errno);
634 break;
635 }
636 return (-1);
637
638 default:
639 snprintf(ebuf, PCAP_ERRBUF_SIZE,
640 "recv_ack: %s unexpected primitive ack 0x%x ",
641 what, (bpf_u_int32)dlp->dl_primitive);
642 return (-1);
643 }
644
645 if (ctl.len < size) {
646 snprintf(ebuf, PCAP_ERRBUF_SIZE,
647 "recv_ack: %s ack too small (%d < %d)",
648 what, ctl.len, size);
649 return (-1);
650 }
651 return (ctl.len);
652 }
653
654 static int
655 dlattachreq(int fd, bpf_u_int32 ppa, char *ebuf)
656 {
657 dl_attach_req_t req;
658
659 req.dl_primitive = DL_ATTACH_REQ;
660 req.dl_ppa = ppa;
661
662 return (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf));
663 }
664
665 static int
666 dlbindreq(int fd, bpf_u_int32 sap, char *ebuf)
667 {
668
669 dl_bind_req_t req;
670
671 memset((char *)&req, 0, sizeof(req));
672 req.dl_primitive = DL_BIND_REQ;
673 #ifdef DL_HP_RAWDLS
674 req.dl_max_conind = 1; /* XXX magic number */
675 /* 22 is INSAP as per the HP-UX DLPI Programmer's Guide */
676 req.dl_sap = 22;
677 req.dl_service_mode = DL_HP_RAWDLS;
678 #else
679 req.dl_sap = sap;
680 #ifdef DL_CLDLS
681 req.dl_service_mode = DL_CLDLS;
682 #endif
683 #endif
684
685 return (send_request(fd, (char *)&req, sizeof(req), "bind", ebuf));
686 }
687
688 static int
689 dlbindack(int fd, char *bufp, char *ebuf)
690 {
691
692 return (recv_ack(fd, DL_BIND_ACK_SIZE, "bind", bufp, ebuf));
693 }
694
695 static int
696 dlpromisconreq(int fd, bpf_u_int32 level, char *ebuf)
697 {
698 dl_promiscon_req_t req;
699
700 req.dl_primitive = DL_PROMISCON_REQ;
701 req.dl_level = level;
702
703 return (send_request(fd, (char *)&req, sizeof(req), "promiscon", ebuf));
704 }
705
706 static int
707 dlokack(int fd, const char *what, char *bufp, char *ebuf)
708 {
709
710 return (recv_ack(fd, DL_OK_ACK_SIZE, what, bufp, ebuf));
711 }
712
713
714 static int
715 dlinforeq(int fd, char *ebuf)
716 {
717 dl_info_req_t req;
718
719 req.dl_primitive = DL_INFO_REQ;
720
721 return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf));
722 }
723
724 static int
725 dlinfoack(int fd, char *bufp, char *ebuf)
726 {
727
728 return (recv_ack(fd, DL_INFO_ACK_SIZE, "info", bufp, ebuf));
729 }
730
731 #ifdef HAVE_SYS_BUFMOD_H
732 static int
733 strioctl(int fd, int cmd, int len, char *dp)
734 {
735 struct strioctl str;
736 int rc;
737
738 str.ic_cmd = cmd;
739 str.ic_timout = -1;
740 str.ic_len = len;
741 str.ic_dp = dp;
742 rc = ioctl(fd, I_STR, &str);
743
744 if (rc < 0)
745 return (rc);
746 else
747 return (str.ic_len);
748 }
749 #endif
750
751 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
752 static char *
753 get_release(bpf_u_int32 *majorp, bpf_u_int32 *minorp, bpf_u_int32 *microp)
754 {
755 char *cp;
756 static char buf[32];
757
758 *majorp = 0;
759 *minorp = 0;
760 *microp = 0;
761 if (sysinfo(SI_RELEASE, buf, sizeof(buf)) < 0)
762 return ("?");
763 cp = buf;
764 if (!isdigit(*cp))
765 return (buf);
766 *majorp = strtol(cp, &cp, 10);
767 if (*cp++ != '.')
768 return (buf);
769 *minorp = strtol(cp, &cp, 10);
770 if (*cp++ != '.')
771 return (buf);
772 *microp = strtol(cp, &cp, 10);
773 return (buf);
774 }
775 #endif
776
777 #ifdef DL_HP_PPA_ACK_OBS
778 /*
779 * Under HP-UX 10 and HP-UX 11, we can ask for the ppa
780 */
781
782
783 /*
784 * Determine ppa number that specifies ifname.
785 *
786 * If the "dl_hp_ppa_info_t" doesn't have a "dl_module_id_1" member,
787 * the code that's used here is the old code for HP-UX 10.x.
788 *
789 * However, HP-UX 10.20, at least, appears to have such a member
790 * in its "dl_hp_ppa_info_t" structure, so the new code is used.
791 * The new code didn't work on an old 10.20 system on which Rick
792 * Jones of HP tried it, but with later patches installed, it
793 * worked - it appears that the older system had those members but
794 * didn't put anything in them, so, if the search by name fails, we
795 * do the old search.
796 *
797 * Rick suggests that making sure your system is "up on the latest
798 * lancommon/DLPI/driver patches" is probably a good idea; it'd fix
799 * that problem, as well as allowing libpcap to see packets sent
800 * from the system on which the libpcap application is being run.
801 * (On 10.20, in addition to getting the latest patches, you need
802 * to turn the kernel "lanc_outbound_promisc_flag" flag on with ADB;
803 * a posting to "comp.sys.hp.hpux" at
804 *
805 * http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=558092266
806 *
807 * says that, to see the machine's outgoing traffic, you'd need to
808 * apply the right patches to your system, and also set that variable
809 * with:
810
811 echo 'lanc_outbound_promisc_flag/W1' | /usr/bin/adb -w /stand/vmunix /dev/kmem
812
813 * which could be put in, for example, "/sbin/init.d/lan".
814 *
815 * Setting the variable is not necessary on HP-UX 11.x.
816 */
817 static int
818 get_dlpi_ppa(register int fd, register const char *device, register int unit,
819 register char *ebuf)
820 {
821 register dl_hp_ppa_ack_t *ap;
822 register dl_hp_ppa_info_t *ipstart, *ip;
823 register int i;
824 char dname[100];
825 register u_long majdev;
826 struct stat statbuf;
827 dl_hp_ppa_req_t req;
828 bpf_u_int32 buf[MAXDLBUF];
829
830 memset((char *)&req, 0, sizeof(req));
831 req.dl_primitive = DL_HP_PPA_REQ;
832
833 memset((char *)buf, 0, sizeof(buf));
834 if (send_request(fd, (char *)&req, sizeof(req), "hpppa", ebuf) < 0 ||
835 recv_ack(fd, DL_HP_PPA_ACK_SIZE, "hpppa", (char *)buf, ebuf) < 0)
836 return (-1);
837
838 ap = (dl_hp_ppa_ack_t *)buf;
839 ipstart = (dl_hp_ppa_info_t *)((u_char *)ap + ap->dl_offset);
840 ip = ipstart;
841
842 #ifdef HAVE_HP_PPA_INFO_T_DL_MODULE_ID_1
843 /*
844 * The "dl_hp_ppa_info_t" structure has a "dl_module_id_1"
845 * member that should, in theory, contain the part of the
846 * name for the device that comes before the unit number,
847 * and should also have a "dl_module_id_2" member that may
848 * contain an alternate name (e.g., I think Ethernet devices
849 * have both "lan", for "lanN", and "snap", for "snapN", with
850 * the former being for Ethernet packets and the latter being
851 * for 802.3/802.2 packets).
852 *
853 * Search for the device that has the specified name and
854 * instance number.
855 */
856 for (i = 0; i < ap->dl_count; i++) {
857 if ((strcmp(ip->dl_module_id_1, device) == 0 ||
858 strcmp(ip->dl_module_id_2, device) == 0) &&
859 ip->dl_instance_num == unit)
860 break;
861
862 ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
863 }
864 #else
865 /*
866 * We don't have that member, so the search is impossible; make it
867 * look as if the search failed.
868 */
869 i = ap->dl_count;
870 #endif
871
872 if (i == ap->dl_count) {
873 /*
874 * Well, we didn't, or can't, find the device by name.
875 *
876 * HP-UX 10.20, whilst it has "dl_module_id_1" and
877 * "dl_module_id_2" fields in the "dl_hp_ppa_info_t",
878 * doesn't seem to fill them in unless the system is
879 * at a reasonably up-to-date patch level.
880 *
881 * Older HP-UX 10.x systems might not have those fields
882 * at all.
883 *
884 * Therefore, we'll search for the entry with the major
885 * device number of a device with the name "/dev/<dev><unit>",
886 * if such a device exists, as the old code did.
887 */
888 snprintf(dname, sizeof(dname), "/dev/%s%d", device, unit);
889 if (stat(dname, &statbuf) < 0) {
890 snprintf(ebuf, PCAP_ERRBUF_SIZE, "stat: %s: %s",
891 dname, pcap_strerror(errno));
892 return (-1);
893 }
894 majdev = major(statbuf.st_rdev);
895
896 ip = ipstart;
897
898 for (i = 0; i < ap->dl_count; i++) {
899 if (ip->dl_mjr_num == majdev &&
900 ip->dl_instance_num == unit)
901 break;
902
903 ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
904 }
905 }
906 if (i == ap->dl_count) {
907 snprintf(ebuf, PCAP_ERRBUF_SIZE,
908 "can't find PPA for %s", device);
909 return (-1);
910 }
911 if (ip->dl_hdw_state == HDW_DEAD) {
912 snprintf(ebuf, PCAP_ERRBUF_SIZE,
913 "%s: hardware state: DOWN\n", device);
914 return (-1);
915 }
916 return ((int)ip->dl_ppa);
917 }
918 #endif
919
920 #ifdef HAVE_HPUX9
921 /*
922 * Under HP-UX 9, there is no good way to determine the ppa.
923 * So punt and read it from /dev/kmem.
924 */
925 static struct nlist nl[] = {
926 #define NL_IFNET 0
927 { "ifnet" },
928 { "" }
929 };
930
931 static char path_vmunix[] = "/hp-ux";
932
933 /* Determine ppa number that specifies ifname */
934 static int
935 get_dlpi_ppa(register int fd, register const char *ifname, register int unit,
936 register char *ebuf)
937 {
938 register const char *cp;
939 register int kd;
940 void *addr;
941 struct ifnet ifnet;
942 char if_name[sizeof(ifnet.if_name) + 1];
943
944 cp = strrchr(ifname, '/');
945 if (cp != NULL)
946 ifname = cp + 1;
947 if (nlist(path_vmunix, &nl) < 0) {
948 snprintf(ebuf, PCAP_ERRBUF_SIZE, "nlist %s failed",
949 path_vmunix);
950 return (-1);
951 }
952 if (nl[NL_IFNET].n_value == 0) {
953 snprintf(ebuf, PCAP_ERRBUF_SIZE,
954 "could't find %s kernel symbol",
955 nl[NL_IFNET].n_name);
956 return (-1);
957 }
958 kd = open("/dev/kmem", O_RDONLY);
959 if (kd < 0) {
960 snprintf(ebuf, PCAP_ERRBUF_SIZE, "kmem open: %s",
961 pcap_strerror(errno));
962 return (-1);
963 }
964 if (dlpi_kread(kd, nl[NL_IFNET].n_value,
965 &addr, sizeof(addr), ebuf) < 0) {
966 close(kd);
967 return (-1);
968 }
969 for (; addr != NULL; addr = ifnet.if_next) {
970 if (dlpi_kread(kd, (off_t)addr,
971 &ifnet, sizeof(ifnet), ebuf) < 0 ||
972 dlpi_kread(kd, (off_t)ifnet.if_name,
973 if_name, sizeof(ifnet.if_name), ebuf) < 0) {
974 (void)close(kd);
975 return (-1);
976 }
977 if_name[sizeof(ifnet.if_name)] = '\0';
978 if (strcmp(if_name, ifname) == 0 && ifnet.if_unit == unit)
979 return (ifnet.if_index);
980 }
981
982 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't find %s", ifname);
983 return (-1);
984 }
985
986 static int
987 dlpi_kread(register int fd, register off_t addr,
988 register void *buf, register u_int len, register char *ebuf)
989 {
990 register int cc;
991
992 if (lseek(fd, addr, SEEK_SET) < 0) {
993 snprintf(ebuf, PCAP_ERRBUF_SIZE, "lseek: %s",
994 pcap_strerror(errno));
995 return (-1);
996 }
997 cc = read(fd, buf, len);
998 if (cc < 0) {
999 snprintf(ebuf, PCAP_ERRBUF_SIZE, "read: %s",
1000 pcap_strerror(errno));
1001 return (-1);
1002 } else if (cc != len) {
1003 snprintf(ebuf, PCAP_ERRBUF_SIZE, "short read (%d != %d)", cc,
1004 len);
1005 return (-1);
1006 }
1007 return (cc);
1008 }
1009 #endif