Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / libpcap / pcap-dlpi.c
blob78bb45118330d8486d4907477c53c0608fb1ceac
1 /*
2 * Copyright (c) 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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.
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), Mark Pizzolato
24 * <List-tcpdump-workers@subscriptions.pizzolato.net>,
25 * Mark C. Brown (mbrown@hp.com), and Sagun Shakya <Sagun.Shakya@Sun.COM>.
29 * Packet capture routine for DLPI under SunOS 5, HP-UX 9/10/11, and AIX.
31 * Notes:
33 * - The DLIOCRAW ioctl() is specific to SunOS.
35 * - There is a bug in bufmod(7) such that setting the snapshot
36 * length results in data being left of the front of the packet.
38 * - It might be desirable to use pfmod(7) to filter packets in the
39 * kernel when possible.
41 * - An older version of the HP-UX DLPI Programmer's Guide, which
42 * I think was advertised as the 10.20 version, used to be available
43 * at
45 * http://docs.hp.com/hpux/onlinedocs/B2355-90093/B2355-90093.html
47 * but is no longer available; it can still be found at
49 * http://h21007.www2.hp.com/dspp/files/unprotected/Drivers/Docs/Refs/B2355-90093.pdf
51 * in PDF form.
53 * - The HP-UX 10.x, 11.0, and 11i v1.6 version of the HP-UX DLPI
54 * Programmer's Guide, which I think was once advertised as the
55 * 11.00 version is available at
57 * http://docs.hp.com/en/B2355-90139/index.html
59 * - The HP-UX 11i v2 version of the HP-UX DLPI Programmer's Guide
60 * is available at
62 * http://docs.hp.com/en/B2355-90871/index.html
64 * - All of the HP documents describe raw-mode services, which are
65 * what we use if DL_HP_RAWDLS is defined. XXX - we use __hpux
66 * in some places to test for HP-UX, but use DL_HP_RAWDLS in
67 * other places; do we support any versions of HP-UX without
68 * DL_HP_RAWDLS?
71 #ifndef lint
72 static const char rcsid[] _U_ =
73 "@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.128 2008-12-02 16:20:23 guy Exp $ (LBL)";
74 #endif
76 #ifdef HAVE_CONFIG_H
77 #include "config.h"
78 #endif
80 #include <sys/types.h>
81 #include <sys/time.h>
82 #ifdef HAVE_SYS_BUFMOD_H
83 #include <sys/bufmod.h>
84 #endif
85 #include <sys/dlpi.h>
86 #ifdef HAVE_SYS_DLPI_EXT_H
87 #include <sys/dlpi_ext.h>
88 #endif
89 #ifdef HAVE_HPUX9
90 #include <sys/socket.h>
91 #endif
92 #ifdef DL_HP_PPA_REQ
93 #include <sys/stat.h>
94 #endif
95 #include <sys/stream.h>
96 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
97 #include <sys/systeminfo.h>
98 #endif
100 #ifdef HAVE_HPUX9
101 #include <net/if.h>
102 #endif
104 #include <ctype.h>
105 #ifdef HAVE_HPUX9
106 #include <nlist.h>
107 #endif
108 #include <errno.h>
109 #include <fcntl.h>
110 #include <memory.h>
111 #include <stdio.h>
112 #include <stdlib.h>
113 #include <string.h>
114 #include <stropts.h>
115 #include <unistd.h>
117 #ifdef HAVE_LIMITS_H
118 #include <limits.h>
119 #else
120 #define INT_MAX 2147483647
121 #endif
123 #include "pcap-int.h"
124 #include "dlpisubs.h"
126 #ifdef HAVE_OS_PROTO_H
127 #include "os-proto.h"
128 #endif
130 #ifndef PCAP_DEV_PREFIX
131 #ifdef _AIX
132 #define PCAP_DEV_PREFIX "/dev/dlpi"
133 #else
134 #define PCAP_DEV_PREFIX "/dev"
135 #endif
136 #endif
138 #define MAXDLBUF 8192
140 /* Forwards */
141 static char *split_dname(char *, int *, char *);
142 static int dl_doattach(int, int, char *);
143 #ifdef DL_HP_RAWDLS
144 static int dl_dohpuxbind(int, char *);
145 #endif
146 static int dlpromiscon(pcap_t *, bpf_u_int32);
147 static int dlbindreq(int, bpf_u_int32, char *);
148 static int dlbindack(int, char *, char *, int *);
149 static int dlokack(int, const char *, char *, char *);
150 static int dlinforeq(int, char *);
151 static int dlinfoack(int, char *, char *);
153 #ifdef HAVE_DLPI_PASSIVE
154 static void dlpassive(int, char *);
155 #endif
157 #ifdef DL_HP_RAWDLS
158 static int dlrawdatareq(int, const u_char *, int);
159 #endif
160 static int recv_ack(int, int, const char *, char *, char *, int *);
161 static char *dlstrerror(bpf_u_int32);
162 static char *dlprim(bpf_u_int32);
163 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
164 static char *get_release(bpf_u_int32 *, bpf_u_int32 *, bpf_u_int32 *);
165 #endif
166 static int send_request(int, char *, int, char *, char *);
167 #ifdef HAVE_HPUX9
168 static int dlpi_kread(int, off_t, void *, u_int, char *);
169 #endif
170 #ifdef HAVE_DEV_DLPI
171 static int get_dlpi_ppa(int, const char *, int, char *);
172 #endif
174 /* XXX Needed by HP-UX (at least) */
175 static bpf_u_int32 ctlbuf[MAXDLBUF];
176 static struct strbuf ctl = {
177 MAXDLBUF,
179 (char *)ctlbuf
183 * Cast a buffer to "union DL_primitives" without provoking warnings
184 * from the compiler.
186 #define MAKE_DL_PRIMITIVES(ptr) ((union DL_primitives *)(void *)(ptr))
188 static int
189 pcap_read_dlpi(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
191 int cc;
192 u_char *bp;
193 int flags;
194 struct strbuf data;
196 flags = 0;
197 cc = p->cc;
198 if (cc == 0) {
199 data.buf = (char *)p->buffer + p->offset;
200 data.maxlen = p->bufsize;
201 data.len = 0;
202 do {
204 * Has "pcap_breakloop()" been called?
206 if (p->break_loop) {
208 * Yes - clear the flag that indicates
209 * that it has, and return -2 to
210 * indicate that we were told to
211 * break out of the loop.
213 p->break_loop = 0;
214 return (-2);
217 * XXX - check for the DLPI primitive, which
218 * would be DL_HP_RAWDATA_IND on HP-UX
219 * if we're in raw mode?
221 if (getmsg(p->fd, &ctl, &data, &flags) < 0) {
222 /* Don't choke when we get ptraced */
223 switch (errno) {
225 case EINTR:
226 cc = 0;
227 continue;
229 case EAGAIN:
230 return (0);
232 strlcpy(p->errbuf, pcap_strerror(errno),
233 sizeof(p->errbuf));
234 return (-1);
236 cc = data.len;
237 } while (cc == 0);
238 bp = p->buffer + p->offset;
239 } else
240 bp = p->bp;
242 return (pcap_process_pkts(p, callback, user, cnt, bp, cc));
245 static int
246 pcap_inject_dlpi(pcap_t *p, const void *buf, size_t size)
248 int ret;
250 #if defined(DLIOCRAW)
251 ret = write(p->fd, buf, size);
252 if (ret == -1) {
253 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
254 pcap_strerror(errno));
255 return (-1);
257 #elif defined(DL_HP_RAWDLS)
258 if (p->send_fd < 0) {
259 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
260 "send: Output FD couldn't be opened");
261 return (-1);
263 ret = dlrawdatareq(p->send_fd, buf, size);
264 if (ret == -1) {
265 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
266 pcap_strerror(errno));
267 return (-1);
270 * putmsg() returns either 0 or -1; it doesn't indicate how
271 * many bytes were written (presumably they were all written
272 * or none of them were written). OpenBSD's pcap_inject()
273 * returns the number of bytes written, so, for API compatibility,
274 * we return the number of bytes we were told to write.
276 ret = size;
277 #else /* no raw mode */
279 * XXX - this is a pain, because you might have to extract
280 * the address from the packet and use it in a DL_UNITDATA_REQ
281 * request. That would be dependent on the link-layer type.
283 * I also don't know what SAP you'd have to bind the descriptor
284 * to, or whether you'd need separate "receive" and "send" FDs,
285 * nor do I know whether you'd need different bindings for
286 * D/I/X Ethernet and 802.3, or for {FDDI,Token Ring} plus
287 * 802.2 and {FDDI,Token Ring} plus 802.2 plus SNAP.
289 * So, for now, we just return a "you can't send" indication,
290 * and leave it up to somebody with a DLPI-based system lacking
291 * both DLIOCRAW and DL_HP_RAWDLS to supply code to implement
292 * packet transmission on that system. If they do, they should
293 * send it to us - but should not send us code that assumes
294 * Ethernet; if the code doesn't work on non-Ethernet interfaces,
295 * it should check "p->linktype" and reject the send request if
296 * it's anything other than DLT_EN10MB.
298 strlcpy(p->errbuf, "send: Not supported on this version of this OS",
299 PCAP_ERRBUF_SIZE);
300 ret = -1;
301 #endif /* raw mode */
302 return (ret);
305 #ifndef DL_IPATM
306 #define DL_IPATM 0x12 /* ATM Classical IP interface */
307 #endif
309 #ifdef HAVE_SOLARIS
311 * For SunATM.
313 #ifndef A_GET_UNITS
314 #define A_GET_UNITS (('A'<<8)|118)
315 #endif /* A_GET_UNITS */
316 #ifndef A_PROMISCON_REQ
317 #define A_PROMISCON_REQ (('A'<<8)|121)
318 #endif /* A_PROMISCON_REQ */
319 #endif /* HAVE_SOLARIS */
321 static void
322 pcap_cleanup_dlpi(pcap_t *p)
324 if (p->send_fd >= 0) {
325 close(p->send_fd);
326 p->send_fd = -1;
328 pcap_cleanup_live_common(p);
331 static int
332 pcap_activate_dlpi(pcap_t *p)
334 register char *cp;
335 int ppa;
336 #ifdef HAVE_SOLARIS
337 int isatm = 0;
338 #endif
339 register dl_info_ack_t *infop;
340 #ifdef HAVE_SYS_BUFMOD_H
341 bpf_u_int32 ss;
342 #ifdef HAVE_SOLARIS
343 register char *release;
344 bpf_u_int32 osmajor, osminor, osmicro;
345 #endif
346 #endif
347 bpf_u_int32 buf[MAXDLBUF];
348 char dname[100];
349 #ifndef HAVE_DEV_DLPI
350 char dname2[100];
351 #endif
352 int status = PCAP_ERROR;
354 #ifdef HAVE_DEV_DLPI
356 ** Remove any "/dev/" on the front of the device.
358 cp = strrchr(p->opt.source, '/');
359 if (cp == NULL)
360 strlcpy(dname, p->opt.source, sizeof(dname));
361 else
362 strlcpy(dname, cp + 1, sizeof(dname));
365 * Split the device name into a device type name and a unit number;
366 * chop off the unit number, so "dname" is just a device type name.
368 cp = split_dname(dname, &ppa, p->errbuf);
369 if (cp == NULL) {
370 status = PCAP_ERROR_NO_SUCH_DEVICE;
371 goto bad;
373 *cp = '\0';
376 * Use "/dev/dlpi" as the device.
378 * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that
379 * the "dl_mjr_num" field is for the "major number of interface
380 * driver"; that's the major of "/dev/dlpi" on the system on
381 * which I tried this, but there may be DLPI devices that
382 * use a different driver, in which case we may need to
383 * search "/dev" for the appropriate device with that major
384 * device number, rather than hardwiring "/dev/dlpi".
386 cp = "/dev/dlpi";
387 if ((p->fd = open(cp, O_RDWR)) < 0) {
388 if (errno == EPERM || errno == EACCES)
389 status = PCAP_ERROR_PERM_DENIED;
390 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
391 "%s: %s", cp, pcap_strerror(errno));
392 goto bad;
395 #ifdef DL_HP_RAWDLS
397 * XXX - HP-UX 10.20 and 11.xx don't appear to support sending and
398 * receiving packets on the same descriptor - you need separate
399 * descriptors for sending and receiving, bound to different SAPs.
401 * If the open fails, we just leave -1 in "p->send_fd" and reject
402 * attempts to send packets, just as if, in pcap-bpf.c, we fail
403 * to open the BPF device for reading and writing, we just try
404 * to open it for reading only and, if that succeeds, just let
405 * the send attempts fail.
407 p->send_fd = open(cp, O_RDWR);
408 #endif
411 * Get a table of all PPAs for that device, and search that
412 * table for the specified device type name and unit number.
414 ppa = get_dlpi_ppa(p->fd, dname, ppa, p->errbuf);
415 if (ppa < 0) {
416 status = ppa;
417 goto bad;
419 #else
421 * If the device name begins with "/", assume it begins with
422 * the pathname of the directory containing the device to open;
423 * otherwise, concatenate the device directory name and the
424 * device name.
426 if (*p->opt.source == '/')
427 strlcpy(dname, p->opt.source, sizeof(dname));
428 else
429 snprintf(dname, sizeof(dname), "%s/%s", PCAP_DEV_PREFIX,
430 p->opt.source);
433 * Get the unit number, and a pointer to the end of the device
434 * type name.
436 cp = split_dname(dname, &ppa, p->errbuf);
437 if (cp == NULL) {
438 status = PCAP_ERROR_NO_SUCH_DEVICE;
439 goto bad;
443 * Make a copy of the device pathname, and then remove the unit
444 * number from the device pathname.
446 strlcpy(dname2, dname, sizeof(dname));
447 *cp = '\0';
449 /* Try device without unit number */
450 if ((p->fd = open(dname, O_RDWR)) < 0) {
451 if (errno != ENOENT) {
452 if (errno == EPERM || errno == EACCES)
453 status = PCAP_ERROR_PERM_DENIED;
454 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", dname,
455 pcap_strerror(errno));
456 goto bad;
459 /* Try again with unit number */
460 if ((p->fd = open(dname2, O_RDWR)) < 0) {
461 if (errno == ENOENT) {
462 status = PCAP_ERROR_NO_SUCH_DEVICE;
465 * We provide an error message even
466 * for this error, for diagnostic
467 * purposes (so that, for example,
468 * the app can show the message if the
469 * user requests it).
471 * In it, we just report "No DLPI device
472 * found" with the device name, so people
473 * don't get confused and think, for example,
474 * that if they can't capture on "lo0"
475 * on Solaris the fix is to change libpcap
476 * (or the application that uses it) to
477 * look for something other than "/dev/lo0",
478 * as the fix is to look for an operating
479 * system other than Solaris - you just
480 * *can't* capture on a loopback interface
481 * on Solaris, the lack of a DLPI device
482 * for the loopback interface is just a
483 * symptom of that inability.
485 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
486 "%s: No DLPI device found", p->opt.source);
487 } else {
488 if (errno == EPERM || errno == EACCES)
489 status = PCAP_ERROR_PERM_DENIED;
490 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
491 dname2, pcap_strerror(errno));
493 goto bad;
495 /* XXX Assume unit zero */
496 ppa = 0;
498 #endif
501 ** Attach if "style 2" provider
503 if (dlinforeq(p->fd, p->errbuf) < 0 ||
504 dlinfoack(p->fd, (char *)buf, p->errbuf) < 0)
505 goto bad;
506 infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack;
507 #ifdef HAVE_SOLARIS
508 if (infop->dl_mac_type == DL_IPATM)
509 isatm = 1;
510 #endif
511 if (infop->dl_provider_style == DL_STYLE2) {
512 status = dl_doattach(p->fd, ppa, p->errbuf);
513 if (status < 0)
514 goto bad;
515 #ifdef DL_HP_RAWDLS
516 if (p->send_fd >= 0) {
517 if (dl_doattach(p->send_fd, ppa, p->errbuf) < 0)
518 goto bad;
520 #endif
523 if (p->opt.rfmon) {
525 * This device exists, but we don't support monitor mode
526 * any platforms that support DLPI.
528 status = PCAP_ERROR_RFMON_NOTSUP;
529 goto bad;
532 #ifdef HAVE_DLPI_PASSIVE
534 * Enable Passive mode to be able to capture on aggregated link.
535 * Not supported in all Solaris versions.
537 dlpassive(p->fd, p->errbuf);
538 #endif
540 ** Bind (defer if using HP-UX 9 or HP-UX 10.20 or later, totally
541 ** skip if using SINIX)
543 #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20_OR_LATER) && !defined(sinix)
544 #ifdef _AIX
546 ** AIX.
547 ** According to IBM's AIX Support Line, the dl_sap value
548 ** should not be less than 0x600 (1536) for standard Ethernet.
549 ** However, we seem to get DL_BADADDR - "DLSAP addr in improper
550 ** format or invalid" - errors if we use 1537 on the "tr0"
551 ** device, which, given that its name starts with "tr" and that
552 ** it's IBM, probably means a Token Ring device. (Perhaps we
553 ** need to use 1537 on "/dev/dlpi/en" because that device is for
554 ** D/I/X Ethernet, the "SAP" is actually an Ethernet type, and
555 ** it rejects invalid Ethernet types.)
557 ** So if 1537 fails, we try 2, as Hyung Sik Yoon of IBM Korea
558 ** says that works on Token Ring (he says that 0 does *not*
559 ** work; perhaps that's considered an invalid LLC SAP value - I
560 ** assume the SAP value in a DLPI bind is an LLC SAP for network
561 ** types that use 802.2 LLC).
563 if ((dlbindreq(p->fd, 1537, p->errbuf) < 0 &&
564 dlbindreq(p->fd, 2, p->errbuf) < 0) ||
565 dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0)
566 goto bad;
567 #elif defined(DL_HP_RAWDLS)
569 ** HP-UX 10.0x and 10.1x.
571 if (dl_dohpuxbind(p->fd, p->errbuf) < 0)
572 goto bad;
573 if (p->send_fd >= 0) {
575 ** XXX - if this fails, just close send_fd and
576 ** set it to -1, so that you can't send but can
577 ** still receive?
579 if (dl_dohpuxbind(p->send_fd, p->errbuf) < 0)
580 goto bad;
582 #else /* neither AIX nor HP-UX */
584 ** Not Sinix, and neither AIX nor HP-UX - Solaris, and any other
585 ** OS using DLPI.
587 if (dlbindreq(p->fd, 0, p->errbuf) < 0 ||
588 dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0)
589 goto bad;
590 #endif /* AIX vs. HP-UX vs. other */
591 #endif /* !HP-UX 9 and !HP-UX 10.20 or later and !SINIX */
593 #ifdef HAVE_SOLARIS
594 if (isatm) {
596 ** Have to turn on some special ATM promiscuous mode
597 ** for SunATM.
598 ** Do *NOT* turn regular promiscuous mode on; it doesn't
599 ** help, and may break things.
601 if (strioctl(p->fd, A_PROMISCON_REQ, 0, NULL) < 0) {
602 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
603 "A_PROMISCON_REQ: %s", pcap_strerror(errno));
604 goto bad;
606 } else
607 #endif
608 if (p->opt.promisc) {
610 ** Enable promiscuous (not necessary on send FD)
612 status = dlpromiscon(p, DL_PROMISC_PHYS);
613 if (status < 0) {
614 if (status == PCAP_ERROR_PERM_DENIED)
615 status = PCAP_ERROR_PROMISC_PERM_DENIED;
616 goto bad;
620 ** Try to enable multicast (you would have thought
621 ** promiscuous would be sufficient). (Skip if using
622 ** HP-UX or SINIX) (Not necessary on send FD)
624 #if !defined(__hpux) && !defined(sinix)
625 status = dlpromiscon(p, DL_PROMISC_MULTI);
626 if (status < 0)
627 status = PCAP_WARNING;
628 #endif
631 ** Try to enable SAP promiscuity (when not in promiscuous mode
632 ** when using HP-UX, when not doing SunATM on Solaris, and never
633 ** under SINIX) (Not necessary on send FD)
635 #ifndef sinix
636 #if defined(__hpux)
637 /* HP-UX - only do this when not in promiscuous mode */
638 if (!p->opt.promisc) {
639 #elif defined(HAVE_SOLARIS)
640 /* Solaris - don't do this on SunATM devices */
641 if (!isatm) {
642 #else
643 /* Everything else (except for SINIX) - always do this */
645 #endif
646 status = dlpromiscon(p, DL_PROMISC_SAP);
647 if (status < 0) {
649 * Not fatal, since the DL_PROMISC_PHYS mode worked.
650 * Report it as a warning, however.
652 if (p->opt.promisc)
653 status = PCAP_WARNING;
654 else
655 goto bad;
658 #endif /* sinix */
661 ** HP-UX 9, and HP-UX 10.20 or later, must bind after setting
662 ** promiscuous options.
664 #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20_OR_LATER)
665 if (dl_dohpuxbind(p->fd, p->errbuf) < 0)
666 goto bad;
668 ** We don't set promiscuous mode on the send FD, but we'll defer
669 ** binding it anyway, just to keep the HP-UX 9/10.20 or later
670 ** code together.
672 if (p->send_fd >= 0) {
674 ** XXX - if this fails, just close send_fd and
675 ** set it to -1, so that you can't send but can
676 ** still receive?
678 if (dl_dohpuxbind(p->send_fd, p->errbuf) < 0)
679 goto bad;
681 #endif
684 ** Determine link type
685 ** XXX - get SAP length and address length as well, for use
686 ** when sending packets.
688 if (dlinforeq(p->fd, p->errbuf) < 0 ||
689 dlinfoack(p->fd, (char *)buf, p->errbuf) < 0)
690 goto bad;
692 infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack;
693 if (pcap_process_mactype(p, infop->dl_mac_type) != 0)
694 goto bad;
696 #ifdef DLIOCRAW
698 ** This is a non standard SunOS hack to get the full raw link-layer
699 ** header.
701 if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) {
702 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "DLIOCRAW: %s",
703 pcap_strerror(errno));
704 goto bad;
706 #endif
708 #ifdef HAVE_SYS_BUFMOD_H
709 ss = p->snapshot;
712 ** There is a bug in bufmod(7). When dealing with messages of
713 ** less than snaplen size it strips data from the beginning not
714 ** the end.
716 ** This bug is fixed in 5.3.2. Also, there is a patch available.
717 ** Ask for bugid 1149065.
719 #ifdef HAVE_SOLARIS
720 release = get_release(&osmajor, &osminor, &osmicro);
721 if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) &&
722 getenv("BUFMOD_FIXED") == NULL) {
723 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
724 "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.",
725 release);
726 ss = 0;
727 status = PCAP_WARNING;
729 #endif
731 /* Push and configure bufmod. */
732 if (pcap_conf_bufmod(p, ss, p->md.timeout) != 0)
733 goto bad;
734 #endif
737 ** As the last operation flush the read side.
739 if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
740 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s",
741 pcap_strerror(errno));
742 goto bad;
745 /* Allocate data buffer. */
746 if (pcap_alloc_databuf(p) != 0)
747 goto bad;
749 /* Success - but perhaps with a warning */
750 if (status < 0)
751 status = 0;
754 * "p->fd" is an FD for a STREAMS device, so "select()" and
755 * "poll()" should work on it.
757 p->selectable_fd = p->fd;
759 p->read_op = pcap_read_dlpi;
760 p->inject_op = pcap_inject_dlpi;
761 p->setfilter_op = install_bpf_program; /* no kernel filtering */
762 p->setdirection_op = NULL; /* Not implemented.*/
763 p->set_datalink_op = NULL; /* can't change data link type */
764 p->getnonblock_op = pcap_getnonblock_fd;
765 p->setnonblock_op = pcap_setnonblock_fd;
766 p->stats_op = pcap_stats_dlpi;
767 p->cleanup_op = pcap_cleanup_dlpi;
769 return (status);
770 bad:
771 pcap_cleanup_dlpi(p);
772 return (status);
776 * Split a device name into a device type name and a unit number;
777 * return the a pointer to the beginning of the unit number, which
778 * is the end of the device type name, and set "*unitp" to the unit
779 * number.
781 * Returns NULL on error, and fills "ebuf" with an error message.
783 static char *
784 split_dname(char *device, int *unitp, char *ebuf)
786 char *cp;
787 char *eos;
788 long unit;
791 * Look for a number at the end of the device name string.
793 cp = device + strlen(device) - 1;
794 if (*cp < '0' || *cp > '9') {
795 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s missing unit number",
796 device);
797 return (NULL);
800 /* Digits at end of string are unit number */
801 while (cp-1 >= device && *(cp-1) >= '0' && *(cp-1) <= '9')
802 cp--;
804 errno = 0;
805 unit = strtol(cp, &eos, 10);
806 if (*eos != '\0') {
807 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s bad unit number", device);
808 return (NULL);
810 if (errno == ERANGE || unit > INT_MAX) {
811 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number too large",
812 device);
813 return (NULL);
815 if (unit < 0) {
816 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number is negative",
817 device);
818 return (NULL);
820 *unitp = (int)unit;
821 return (cp);
824 static int
825 dl_doattach(int fd, int ppa, char *ebuf)
827 dl_attach_req_t req;
828 bpf_u_int32 buf[MAXDLBUF];
829 int err;
831 req.dl_primitive = DL_ATTACH_REQ;
832 req.dl_ppa = ppa;
833 if (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf) < 0)
834 return (PCAP_ERROR);
836 err = dlokack(fd, "attach", (char *)buf, ebuf);
837 if (err < 0)
838 return (err);
839 return (0);
842 #ifdef DL_HP_RAWDLS
843 static int
844 dl_dohpuxbind(int fd, char *ebuf)
846 int hpsap;
847 int uerror;
848 bpf_u_int32 buf[MAXDLBUF];
851 * XXX - we start at 22 because we used to use only 22, but
852 * that was just because that was the value used in some
853 * sample code from HP. With what value *should* we start?
854 * Does it matter, given that we're enabling SAP promiscuity
855 * on the input FD?
857 hpsap = 22;
858 for (;;) {
859 if (dlbindreq(fd, hpsap, ebuf) < 0)
860 return (-1);
861 if (dlbindack(fd, (char *)buf, ebuf, &uerror) >= 0)
862 break;
864 * For any error other than a UNIX EBUSY, give up.
866 if (uerror != EBUSY) {
868 * dlbindack() has already filled in ebuf for
869 * this error.
871 return (-1);
875 * For EBUSY, try the next SAP value; that means that
876 * somebody else is using that SAP. Clear ebuf so
877 * that application doesn't report the "Device busy"
878 * error as a warning.
880 *ebuf = '\0';
881 hpsap++;
882 if (hpsap > 100) {
883 strlcpy(ebuf,
884 "All SAPs from 22 through 100 are in use",
885 PCAP_ERRBUF_SIZE);
886 return (-1);
889 return (0);
891 #endif
893 #define STRINGIFY(n) #n
895 static int
896 dlpromiscon(pcap_t *p, bpf_u_int32 level)
898 dl_promiscon_req_t req;
899 bpf_u_int32 buf[MAXDLBUF];
900 int err;
902 req.dl_primitive = DL_PROMISCON_REQ;
903 req.dl_level = level;
904 if (send_request(p->fd, (char *)&req, sizeof(req), "promiscon",
905 p->errbuf) < 0)
906 return (PCAP_ERROR);
907 err = dlokack(p->fd, "promiscon" STRINGIFY(level), (char *)buf,
908 p->errbuf);
909 if (err < 0)
910 return (err);
911 return (0);
915 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
917 #ifdef HAVE_SOLARIS
918 int fd;
919 union {
920 u_int nunits;
921 char pad[516]; /* XXX - must be at least 513; is 516
922 in "atmgetunits" */
923 } buf;
924 char baname[2+1+1];
925 u_int i;
928 * We may have to do special magic to get ATM devices.
930 if ((fd = open("/dev/ba", O_RDWR)) < 0) {
932 * We couldn't open the "ba" device.
933 * For now, just give up; perhaps we should
934 * return an error if the problem is neither
935 * a "that device doesn't exist" error (ENOENT,
936 * ENXIO, etc.) or a "you're not allowed to do
937 * that" error (EPERM, EACCES).
939 return (0);
942 if (strioctl(fd, A_GET_UNITS, sizeof(buf), (char *)&buf) < 0) {
943 snprintf(errbuf, PCAP_ERRBUF_SIZE, "A_GET_UNITS: %s",
944 pcap_strerror(errno));
945 return (-1);
947 for (i = 0; i < buf.nunits; i++) {
948 snprintf(baname, sizeof baname, "ba%u", i);
949 if (pcap_add_if(alldevsp, baname, 0, NULL, errbuf) < 0)
950 return (-1);
952 #endif
954 return (0);
957 static int
958 send_request(int fd, char *ptr, int len, char *what, char *ebuf)
960 struct strbuf ctl;
961 int flags;
963 ctl.maxlen = 0;
964 ctl.len = len;
965 ctl.buf = ptr;
967 flags = 0;
968 if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) {
969 snprintf(ebuf, PCAP_ERRBUF_SIZE,
970 "send_request: putmsg \"%s\": %s",
971 what, pcap_strerror(errno));
972 return (-1);
974 return (0);
977 static int
978 recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf, int *uerror)
980 union DL_primitives *dlp;
981 struct strbuf ctl;
982 int flags;
985 * Clear out "*uerror", so it's only set for DL_ERROR_ACK/DL_SYSERR,
986 * making that the only place where EBUSY is treated specially.
988 if (uerror != NULL)
989 *uerror = 0;
991 ctl.maxlen = MAXDLBUF;
992 ctl.len = 0;
993 ctl.buf = bufp;
995 flags = 0;
996 if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) {
997 snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s getmsg: %s",
998 what, pcap_strerror(errno));
999 return (PCAP_ERROR);
1002 dlp = MAKE_DL_PRIMITIVES(ctl.buf);
1003 switch (dlp->dl_primitive) {
1005 case DL_INFO_ACK:
1006 case DL_BIND_ACK:
1007 case DL_OK_ACK:
1008 #ifdef DL_HP_PPA_ACK
1009 case DL_HP_PPA_ACK:
1010 #endif
1011 /* These are OK */
1012 break;
1014 case DL_ERROR_ACK:
1015 switch (dlp->error_ack.dl_errno) {
1017 case DL_SYSERR:
1018 if (uerror != NULL)
1019 *uerror = dlp->error_ack.dl_unix_errno;
1020 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1021 "recv_ack: %s: UNIX error - %s",
1022 what, pcap_strerror(dlp->error_ack.dl_unix_errno));
1023 if (dlp->error_ack.dl_unix_errno == EPERM ||
1024 dlp->error_ack.dl_unix_errno == EACCES)
1025 return (PCAP_ERROR_PERM_DENIED);
1026 break;
1028 default:
1029 snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s: %s",
1030 what, dlstrerror(dlp->error_ack.dl_errno));
1031 if (dlp->error_ack.dl_errno == DL_BADPPA)
1032 return (PCAP_ERROR_NO_SUCH_DEVICE);
1033 else if (dlp->error_ack.dl_errno == DL_ACCESS)
1034 return (PCAP_ERROR_PERM_DENIED);
1035 break;
1037 return (PCAP_ERROR);
1039 default:
1040 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1041 "recv_ack: %s: Unexpected primitive ack %s",
1042 what, dlprim(dlp->dl_primitive));
1043 return (PCAP_ERROR);
1046 if (ctl.len < size) {
1047 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1048 "recv_ack: %s: Ack too small (%d < %d)",
1049 what, ctl.len, size);
1050 return (PCAP_ERROR);
1052 return (ctl.len);
1055 static char *
1056 dlstrerror(bpf_u_int32 dl_errno)
1058 static char errstring[6+2+8+1];
1060 switch (dl_errno) {
1062 case DL_ACCESS:
1063 return ("Improper permissions for request");
1065 case DL_BADADDR:
1066 return ("DLSAP addr in improper format or invalid");
1068 case DL_BADCORR:
1069 return ("Seq number not from outstand DL_CONN_IND");
1071 case DL_BADDATA:
1072 return ("User data exceeded provider limit");
1074 case DL_BADPPA:
1075 #ifdef HAVE_DEV_DLPI
1077 * With a single "/dev/dlpi" device used for all
1078 * DLPI providers, PPAs have nothing to do with
1079 * unit numbers.
1081 return ("Specified PPA was invalid");
1082 #else
1084 * We have separate devices for separate devices;
1085 * the PPA is just the unit number.
1087 return ("Specified PPA (device unit) was invalid");
1088 #endif
1090 case DL_BADPRIM:
1091 return ("Primitive received not known by provider");
1093 case DL_BADQOSPARAM:
1094 return ("QOS parameters contained invalid values");
1096 case DL_BADQOSTYPE:
1097 return ("QOS structure type is unknown/unsupported");
1099 case DL_BADSAP:
1100 return ("Bad LSAP selector");
1102 case DL_BADTOKEN:
1103 return ("Token used not an active stream");
1105 case DL_BOUND:
1106 return ("Attempted second bind with dl_max_conind");
1108 case DL_INITFAILED:
1109 return ("Physical link initialization failed");
1111 case DL_NOADDR:
1112 return ("Provider couldn't allocate alternate address");
1114 case DL_NOTINIT:
1115 return ("Physical link not initialized");
1117 case DL_OUTSTATE:
1118 return ("Primitive issued in improper state");
1120 case DL_SYSERR:
1121 return ("UNIX system error occurred");
1123 case DL_UNSUPPORTED:
1124 return ("Requested service not supplied by provider");
1126 case DL_UNDELIVERABLE:
1127 return ("Previous data unit could not be delivered");
1129 case DL_NOTSUPPORTED:
1130 return ("Primitive is known but not supported");
1132 case DL_TOOMANY:
1133 return ("Limit exceeded");
1135 case DL_NOTENAB:
1136 return ("Promiscuous mode not enabled");
1138 case DL_BUSY:
1139 return ("Other streams for PPA in post-attached");
1141 case DL_NOAUTO:
1142 return ("Automatic handling XID&TEST not supported");
1144 case DL_NOXIDAUTO:
1145 return ("Automatic handling of XID not supported");
1147 case DL_NOTESTAUTO:
1148 return ("Automatic handling of TEST not supported");
1150 case DL_XIDAUTO:
1151 return ("Automatic handling of XID response");
1153 case DL_TESTAUTO:
1154 return ("Automatic handling of TEST response");
1156 case DL_PENDING:
1157 return ("Pending outstanding connect indications");
1159 default:
1160 sprintf(errstring, "Error %02x", dl_errno);
1161 return (errstring);
1165 static char *
1166 dlprim(bpf_u_int32 prim)
1168 static char primbuf[80];
1170 switch (prim) {
1172 case DL_INFO_REQ:
1173 return ("DL_INFO_REQ");
1175 case DL_INFO_ACK:
1176 return ("DL_INFO_ACK");
1178 case DL_ATTACH_REQ:
1179 return ("DL_ATTACH_REQ");
1181 case DL_DETACH_REQ:
1182 return ("DL_DETACH_REQ");
1184 case DL_BIND_REQ:
1185 return ("DL_BIND_REQ");
1187 case DL_BIND_ACK:
1188 return ("DL_BIND_ACK");
1190 case DL_UNBIND_REQ:
1191 return ("DL_UNBIND_REQ");
1193 case DL_OK_ACK:
1194 return ("DL_OK_ACK");
1196 case DL_ERROR_ACK:
1197 return ("DL_ERROR_ACK");
1199 case DL_SUBS_BIND_REQ:
1200 return ("DL_SUBS_BIND_REQ");
1202 case DL_SUBS_BIND_ACK:
1203 return ("DL_SUBS_BIND_ACK");
1205 case DL_UNITDATA_REQ:
1206 return ("DL_UNITDATA_REQ");
1208 case DL_UNITDATA_IND:
1209 return ("DL_UNITDATA_IND");
1211 case DL_UDERROR_IND:
1212 return ("DL_UDERROR_IND");
1214 case DL_UDQOS_REQ:
1215 return ("DL_UDQOS_REQ");
1217 case DL_CONNECT_REQ:
1218 return ("DL_CONNECT_REQ");
1220 case DL_CONNECT_IND:
1221 return ("DL_CONNECT_IND");
1223 case DL_CONNECT_RES:
1224 return ("DL_CONNECT_RES");
1226 case DL_CONNECT_CON:
1227 return ("DL_CONNECT_CON");
1229 case DL_TOKEN_REQ:
1230 return ("DL_TOKEN_REQ");
1232 case DL_TOKEN_ACK:
1233 return ("DL_TOKEN_ACK");
1235 case DL_DISCONNECT_REQ:
1236 return ("DL_DISCONNECT_REQ");
1238 case DL_DISCONNECT_IND:
1239 return ("DL_DISCONNECT_IND");
1241 case DL_RESET_REQ:
1242 return ("DL_RESET_REQ");
1244 case DL_RESET_IND:
1245 return ("DL_RESET_IND");
1247 case DL_RESET_RES:
1248 return ("DL_RESET_RES");
1250 case DL_RESET_CON:
1251 return ("DL_RESET_CON");
1253 default:
1254 (void) sprintf(primbuf, "unknown primitive 0x%x", prim);
1255 return (primbuf);
1259 static int
1260 dlbindreq(int fd, bpf_u_int32 sap, char *ebuf)
1263 dl_bind_req_t req;
1265 memset((char *)&req, 0, sizeof(req));
1266 req.dl_primitive = DL_BIND_REQ;
1267 /* XXX - what if neither of these are defined? */
1268 #if defined(DL_HP_RAWDLS)
1269 req.dl_max_conind = 1; /* XXX magic number */
1270 req.dl_service_mode = DL_HP_RAWDLS;
1271 #elif defined(DL_CLDLS)
1272 req.dl_service_mode = DL_CLDLS;
1273 #endif
1274 req.dl_sap = sap;
1276 return (send_request(fd, (char *)&req, sizeof(req), "bind", ebuf));
1279 static int
1280 dlbindack(int fd, char *bufp, char *ebuf, int *uerror)
1283 return (recv_ack(fd, DL_BIND_ACK_SIZE, "bind", bufp, ebuf, uerror));
1286 static int
1287 dlokack(int fd, const char *what, char *bufp, char *ebuf)
1290 return (recv_ack(fd, DL_OK_ACK_SIZE, what, bufp, ebuf, NULL));
1294 static int
1295 dlinforeq(int fd, char *ebuf)
1297 dl_info_req_t req;
1299 req.dl_primitive = DL_INFO_REQ;
1301 return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf));
1304 static int
1305 dlinfoack(int fd, char *bufp, char *ebuf)
1308 return (recv_ack(fd, DL_INFO_ACK_SIZE, "info", bufp, ebuf, NULL));
1311 #ifdef HAVE_DLPI_PASSIVE
1313 * Enable DLPI passive mode. We do not care if this request fails, as this
1314 * indicates the underlying DLPI device does not support link aggregation.
1316 static void
1317 dlpassive(int fd, char *ebuf)
1319 dl_passive_req_t req;
1320 bpf_u_int32 buf[MAXDLBUF];
1322 req.dl_primitive = DL_PASSIVE_REQ;
1324 if (send_request(fd, (char *)&req, sizeof(req), "dlpassive", ebuf) == 0)
1325 (void) dlokack(fd, "dlpassive", (char *)buf, ebuf);
1327 #endif
1329 #ifdef DL_HP_RAWDLS
1331 * There's an ack *if* there's an error.
1333 static int
1334 dlrawdatareq(int fd, const u_char *datap, int datalen)
1336 struct strbuf ctl, data;
1337 long buf[MAXDLBUF]; /* XXX - char? */
1338 union DL_primitives *dlp;
1339 int dlen;
1341 dlp = MAKE_DL_PRIMITIVES(buf);
1343 dlp->dl_primitive = DL_HP_RAWDATA_REQ;
1344 dlen = DL_HP_RAWDATA_REQ_SIZE;
1347 * HP's documentation doesn't appear to show us supplying any
1348 * address pointed to by the control part of the message.
1349 * I think that's what raw mode means - you just send the raw
1350 * packet, you don't specify where to send it to, as that's
1351 * implied by the destination address.
1353 ctl.maxlen = 0;
1354 ctl.len = dlen;
1355 ctl.buf = (void *)buf;
1357 data.maxlen = 0;
1358 data.len = datalen;
1359 data.buf = (void *)datap;
1361 return (putmsg(fd, &ctl, &data, 0));
1363 #endif /* DL_HP_RAWDLS */
1365 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
1366 static char *
1367 get_release(bpf_u_int32 *majorp, bpf_u_int32 *minorp, bpf_u_int32 *microp)
1369 char *cp;
1370 static char buf[32];
1372 *majorp = 0;
1373 *minorp = 0;
1374 *microp = 0;
1375 if (sysinfo(SI_RELEASE, buf, sizeof(buf)) < 0)
1376 return ("?");
1377 cp = buf;
1378 if (!isdigit((unsigned char)*cp))
1379 return (buf);
1380 *majorp = strtol(cp, &cp, 10);
1381 if (*cp++ != '.')
1382 return (buf);
1383 *minorp = strtol(cp, &cp, 10);
1384 if (*cp++ != '.')
1385 return (buf);
1386 *microp = strtol(cp, &cp, 10);
1387 return (buf);
1389 #endif
1391 #ifdef DL_HP_PPA_REQ
1393 * Under HP-UX 10 and HP-UX 11, we can ask for the ppa
1398 * Determine ppa number that specifies ifname.
1400 * If the "dl_hp_ppa_info_t" doesn't have a "dl_module_id_1" member,
1401 * the code that's used here is the old code for HP-UX 10.x.
1403 * However, HP-UX 10.20, at least, appears to have such a member
1404 * in its "dl_hp_ppa_info_t" structure, so the new code is used.
1405 * The new code didn't work on an old 10.20 system on which Rick
1406 * Jones of HP tried it, but with later patches installed, it
1407 * worked - it appears that the older system had those members but
1408 * didn't put anything in them, so, if the search by name fails, we
1409 * do the old search.
1411 * Rick suggests that making sure your system is "up on the latest
1412 * lancommon/DLPI/driver patches" is probably a good idea; it'd fix
1413 * that problem, as well as allowing libpcap to see packets sent
1414 * from the system on which the libpcap application is being run.
1415 * (On 10.20, in addition to getting the latest patches, you need
1416 * to turn the kernel "lanc_outbound_promisc_flag" flag on with ADB;
1417 * a posting to "comp.sys.hp.hpux" at
1419 * http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=558092266
1421 * says that, to see the machine's outgoing traffic, you'd need to
1422 * apply the right patches to your system, and also set that variable
1423 * with:
1425 echo 'lanc_outbound_promisc_flag/W1' | /usr/bin/adb -w /stand/vmunix /dev/kmem
1427 * which could be put in, for example, "/sbin/init.d/lan".
1429 * Setting the variable is not necessary on HP-UX 11.x.
1431 static int
1432 get_dlpi_ppa(register int fd, register const char *device, register int unit,
1433 register char *ebuf)
1435 register dl_hp_ppa_ack_t *ap;
1436 register dl_hp_ppa_info_t *ipstart, *ip;
1437 register int i;
1438 char dname[100];
1439 register u_long majdev;
1440 struct stat statbuf;
1441 dl_hp_ppa_req_t req;
1442 char buf[MAXDLBUF];
1443 char *ppa_data_buf;
1444 dl_hp_ppa_ack_t *dlp;
1445 struct strbuf ctl;
1446 int flags;
1447 int ppa;
1449 memset((char *)&req, 0, sizeof(req));
1450 req.dl_primitive = DL_HP_PPA_REQ;
1452 memset((char *)buf, 0, sizeof(buf));
1453 if (send_request(fd, (char *)&req, sizeof(req), "hpppa", ebuf) < 0)
1454 return (PCAP_ERROR);
1456 ctl.maxlen = DL_HP_PPA_ACK_SIZE;
1457 ctl.len = 0;
1458 ctl.buf = (char *)buf;
1460 flags = 0;
1462 * DLPI may return a big chunk of data for a DL_HP_PPA_REQ. The normal
1463 * recv_ack will fail because it set the maxlen to MAXDLBUF (8192)
1464 * which is NOT big enough for a DL_HP_PPA_REQ.
1466 * This causes libpcap applications to fail on a system with HP-APA
1467 * installed.
1469 * To figure out how big the returned data is, we first call getmsg
1470 * to get the small head and peek at the head to get the actual data
1471 * length, and then issue another getmsg to get the actual PPA data.
1473 /* get the head first */
1474 if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
1475 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1476 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno));
1477 return (PCAP_ERROR);
1480 dlp = (dl_hp_ppa_ack_t *)ctl.buf;
1481 if (dlp->dl_primitive != DL_HP_PPA_ACK) {
1482 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1483 "get_dlpi_ppa: hpppa unexpected primitive ack 0x%x",
1484 (bpf_u_int32)dlp->dl_primitive);
1485 return (PCAP_ERROR);
1488 if (ctl.len < DL_HP_PPA_ACK_SIZE) {
1489 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1490 "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1491 ctl.len, (unsigned long)DL_HP_PPA_ACK_SIZE);
1492 return (PCAP_ERROR);
1495 /* allocate buffer */
1496 if ((ppa_data_buf = (char *)malloc(dlp->dl_length)) == NULL) {
1497 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1498 "get_dlpi_ppa: hpppa malloc: %s", pcap_strerror(errno));
1499 return (PCAP_ERROR);
1501 ctl.maxlen = dlp->dl_length;
1502 ctl.len = 0;
1503 ctl.buf = (char *)ppa_data_buf;
1504 /* get the data */
1505 if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
1506 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1507 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno));
1508 free(ppa_data_buf);
1509 return (PCAP_ERROR);
1511 if (ctl.len < dlp->dl_length) {
1512 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1513 "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1514 ctl.len, (unsigned long)dlp->dl_length);
1515 free(ppa_data_buf);
1516 return (PCAP_ERROR);
1519 ap = (dl_hp_ppa_ack_t *)buf;
1520 ipstart = (dl_hp_ppa_info_t *)ppa_data_buf;
1521 ip = ipstart;
1523 #ifdef HAVE_HP_PPA_INFO_T_DL_MODULE_ID_1
1525 * The "dl_hp_ppa_info_t" structure has a "dl_module_id_1"
1526 * member that should, in theory, contain the part of the
1527 * name for the device that comes before the unit number,
1528 * and should also have a "dl_module_id_2" member that may
1529 * contain an alternate name (e.g., I think Ethernet devices
1530 * have both "lan", for "lanN", and "snap", for "snapN", with
1531 * the former being for Ethernet packets and the latter being
1532 * for 802.3/802.2 packets).
1534 * Search for the device that has the specified name and
1535 * instance number.
1537 for (i = 0; i < ap->dl_count; i++) {
1538 if ((strcmp((const char *)ip->dl_module_id_1, device) == 0 ||
1539 strcmp((const char *)ip->dl_module_id_2, device) == 0) &&
1540 ip->dl_instance_num == unit)
1541 break;
1543 ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
1545 #else
1547 * We don't have that member, so the search is impossible; make it
1548 * look as if the search failed.
1550 i = ap->dl_count;
1551 #endif
1553 if (i == ap->dl_count) {
1555 * Well, we didn't, or can't, find the device by name.
1557 * HP-UX 10.20, whilst it has "dl_module_id_1" and
1558 * "dl_module_id_2" fields in the "dl_hp_ppa_info_t",
1559 * doesn't seem to fill them in unless the system is
1560 * at a reasonably up-to-date patch level.
1562 * Older HP-UX 10.x systems might not have those fields
1563 * at all.
1565 * Therefore, we'll search for the entry with the major
1566 * device number of a device with the name "/dev/<dev><unit>",
1567 * if such a device exists, as the old code did.
1569 snprintf(dname, sizeof(dname), "/dev/%s%d", device, unit);
1570 if (stat(dname, &statbuf) < 0) {
1571 snprintf(ebuf, PCAP_ERRBUF_SIZE, "stat: %s: %s",
1572 dname, pcap_strerror(errno));
1573 return (PCAP_ERROR);
1575 majdev = major(statbuf.st_rdev);
1577 ip = ipstart;
1579 for (i = 0; i < ap->dl_count; i++) {
1580 if (ip->dl_mjr_num == majdev &&
1581 ip->dl_instance_num == unit)
1582 break;
1584 ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
1587 if (i == ap->dl_count) {
1588 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1589 "can't find /dev/dlpi PPA for %s%d", device, unit);
1590 return (PCAP_ERROR_NO_SUCH_DEVICE);
1592 if (ip->dl_hdw_state == HDW_DEAD) {
1593 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1594 "%s%d: hardware state: DOWN\n", device, unit);
1595 free(ppa_data_buf);
1596 return (PCAP_ERROR);
1598 ppa = ip->dl_ppa;
1599 free(ppa_data_buf);
1600 return (ppa);
1602 #endif
1604 #ifdef HAVE_HPUX9
1606 * Under HP-UX 9, there is no good way to determine the ppa.
1607 * So punt and read it from /dev/kmem.
1609 static struct nlist nl[] = {
1610 #define NL_IFNET 0
1611 { "ifnet" },
1612 { "" }
1615 static char path_vmunix[] = "/hp-ux";
1617 /* Determine ppa number that specifies ifname */
1618 static int
1619 get_dlpi_ppa(register int fd, register const char *ifname, register int unit,
1620 register char *ebuf)
1622 register const char *cp;
1623 register int kd;
1624 void *addr;
1625 struct ifnet ifnet;
1626 char if_name[sizeof(ifnet.if_name) + 1];
1628 cp = strrchr(ifname, '/');
1629 if (cp != NULL)
1630 ifname = cp + 1;
1631 if (nlist(path_vmunix, &nl) < 0) {
1632 snprintf(ebuf, PCAP_ERRBUF_SIZE, "nlist %s failed",
1633 path_vmunix);
1634 return (-1);
1636 if (nl[NL_IFNET].n_value == 0) {
1637 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1638 "could't find %s kernel symbol",
1639 nl[NL_IFNET].n_name);
1640 return (-1);
1642 kd = open("/dev/kmem", O_RDONLY);
1643 if (kd < 0) {
1644 snprintf(ebuf, PCAP_ERRBUF_SIZE, "kmem open: %s",
1645 pcap_strerror(errno));
1646 return (-1);
1648 if (dlpi_kread(kd, nl[NL_IFNET].n_value,
1649 &addr, sizeof(addr), ebuf) < 0) {
1650 close(kd);
1651 return (-1);
1653 for (; addr != NULL; addr = ifnet.if_next) {
1654 if (dlpi_kread(kd, (off_t)addr,
1655 &ifnet, sizeof(ifnet), ebuf) < 0 ||
1656 dlpi_kread(kd, (off_t)ifnet.if_name,
1657 if_name, sizeof(ifnet.if_name), ebuf) < 0) {
1658 (void)close(kd);
1659 return (-1);
1661 if_name[sizeof(ifnet.if_name)] = '\0';
1662 if (strcmp(if_name, ifname) == 0 && ifnet.if_unit == unit)
1663 return (ifnet.if_index);
1666 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't find %s", ifname);
1667 return (-1);
1670 static int
1671 dlpi_kread(register int fd, register off_t addr,
1672 register void *buf, register u_int len, register char *ebuf)
1674 register int cc;
1676 if (lseek(fd, addr, SEEK_SET) < 0) {
1677 snprintf(ebuf, PCAP_ERRBUF_SIZE, "lseek: %s",
1678 pcap_strerror(errno));
1679 return (-1);
1681 cc = read(fd, buf, len);
1682 if (cc < 0) {
1683 snprintf(ebuf, PCAP_ERRBUF_SIZE, "read: %s",
1684 pcap_strerror(errno));
1685 return (-1);
1686 } else if (cc != len) {
1687 snprintf(ebuf, PCAP_ERRBUF_SIZE, "short read (%d != %d)", cc,
1688 len);
1689 return (-1);
1691 return (cc);
1693 #endif
1695 pcap_t *
1696 pcap_create(const char *device, char *ebuf)
1698 pcap_t *p;
1700 p = pcap_create_common(device, ebuf);
1701 if (p == NULL)
1702 return (NULL);
1704 p->send_fd = -1; /* it hasn't been opened yet */
1706 p->activate_op = pcap_activate_dlpi;
1707 return (p);