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