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