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