]> The Tcpdump Group git mirrors - libpcap/blob - pcap-dlpi.c
Add support for libnl 2.x, adapted from a newer version of the iw command.
[libpcap] / pcap-dlpi.c
1 /*
2 * Copyright (c) 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * This code contributed by Atanu Ghosh (atanu@cs.ucl.ac.uk),
22 * University College London, and subsequently modified by
23 * Guy Harris (guy@alum.mit.edu), Mark Pizzolato
24 * <List-tcpdump-workers@subscriptions.pizzolato.net>,
25 * Mark C. Brown (mbrown@hp.com), and Sagun Shakya <Sagun.Shakya@Sun.COM>.
26 */
27
28 /*
29 * Packet capture routine for DLPI under SunOS 5, HP-UX 9/10/11, and AIX.
30 *
31 * Notes:
32 *
33 * - The DLIOCRAW ioctl() is specific to SunOS.
34 *
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.
37 *
38 * - It might be desirable to use pfmod(7) to filter packets in the
39 * kernel when possible.
40 *
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
44 *
45 * http://docs.hp.com/hpux/onlinedocs/B2355-90093/B2355-90093.html
46 *
47 * but is no longer available; it can still be found at
48 *
49 * http://h21007.www2.hp.com/dspp/files/unprotected/Drivers/Docs/Refs/B2355-90093.pdf
50 *
51 * in PDF form.
52 *
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
56 *
57 * http://docs.hp.com/en/B2355-90139/index.html
58 *
59 * - The HP-UX 11i v2 version of the HP-UX DLPI Programmer's Guide
60 * is available at
61 *
62 * http://docs.hp.com/en/B2355-90871/index.html
63 *
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?
69 */
70
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
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 int ret;
249
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);
256 }
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);
262 }
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);
268 }
269 /*
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.
275 */
276 ret = size;
277 #else /* no raw mode */
278 /*
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.
282 *
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.
288 *
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.
297 */
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);
303 }
304
305 #ifndef DL_IPATM
306 #define DL_IPATM 0x12 /* ATM Classical IP interface */
307 #endif
308
309 #ifdef HAVE_SOLARIS
310 /*
311 * For SunATM.
312 */
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 */
320
321 static void
322 pcap_cleanup_dlpi(pcap_t *p)
323 {
324 if (p->send_fd >= 0) {
325 close(p->send_fd);
326 p->send_fd = -1;
327 }
328 pcap_cleanup_live_common(p);
329 }
330
331 static int
332 pcap_activate_dlpi(pcap_t *p)
333 {
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;
353
354 #ifdef HAVE_DEV_DLPI
355 /*
356 ** Remove any "/dev/" on the front of the device.
357 */
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));
363
364 /*
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.
367 */
368 cp = split_dname(dname, &ppa, p->errbuf);
369 if (cp == NULL) {
370 status = PCAP_ERROR_NO_SUCH_DEVICE;
371 goto bad;
372 }
373 *cp = '\0';
374
375 /*
376 * Use "/dev/dlpi" as the device.
377 *
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".
385 */
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;
393 }
394
395 #ifdef DL_HP_RAWDLS
396 /*
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.
400 *
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.
406 */
407 p->send_fd = open(cp, O_RDWR);
408 #endif
409
410 /*
411 * Get a table of all PPAs for that device, and search that
412 * table for the specified device type name and unit number.
413 */
414 ppa = get_dlpi_ppa(p->fd, dname, ppa, p->errbuf);
415 if (ppa < 0) {
416 status = ppa;
417 goto bad;
418 }
419 #else
420 /*
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.
425 */
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);
431
432 /*
433 * Get the unit number, and a pointer to the end of the device
434 * type name.
435 */
436 cp = split_dname(dname, &ppa, p->errbuf);
437 if (cp == NULL) {
438 status = PCAP_ERROR_NO_SUCH_DEVICE;
439 goto bad;
440 }
441
442 /*
443 * Make a copy of the device pathname, and then remove the unit
444 * number from the device pathname.
445 */
446 strlcpy(dname2, dname, sizeof(dname));
447 *cp = '\0';
448
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;
457 }
458
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;
463
464 /*
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).
470 *
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.
484 */
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));
492 }
493 goto bad;
494 }
495 /* XXX Assume unit zero */
496 ppa = 0;
497 }
498 #endif
499
500 /*
501 ** Attach if "style 2" provider
502 */
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;
519 }
520 #endif
521 }
522
523 if (p->opt.rfmon) {
524 /*
525 * This device exists, but we don't support monitor mode
526 * any platforms that support DLPI.
527 */
528 status = PCAP_ERROR_RFMON_NOTSUP;
529 goto bad;
530 }
531
532 #ifdef HAVE_DLPI_PASSIVE
533 /*
534 * Enable Passive mode to be able to capture on aggregated link.
535 * Not supported in all Solaris versions.
536 */
537 dlpassive(p->fd, p->errbuf);
538 #endif
539 /*
540 ** Bind (defer if using HP-UX 9 or HP-UX 10.20 or later, totally
541 ** skip if using SINIX)
542 */
543 #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20_OR_LATER) && !defined(sinix)
544 #ifdef _AIX
545 /*
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.)
556 **
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).
562 */
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)
568 /*
569 ** HP-UX 10.0x and 10.1x.
570 */
571 if (dl_dohpuxbind(p->fd, p->errbuf) < 0)
572 goto bad;
573 if (p->send_fd >= 0) {
574 /*
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?
578 */
579 if (dl_dohpuxbind(p->send_fd, p->errbuf) < 0)
580 goto bad;
581 }
582 #else /* neither AIX nor HP-UX */
583 /*
584 ** Not Sinix, and neither AIX nor HP-UX - Solaris, and any other
585 ** OS using DLPI.
586 **/
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 */
592
593 #ifdef HAVE_SOLARIS
594 if (isatm) {
595 /*
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.
600 */
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;
605 }
606 } else
607 #endif
608 if (p->opt.promisc) {
609 /*
610 ** Enable promiscuous (not necessary on send FD)
611 */
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;
617 }
618
619 /*
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)
623 */
624 #if !defined(__hpux) && !defined(sinix)
625 status = dlpromiscon(p, DL_PROMISC_MULTI);
626 if (status < 0)
627 status = PCAP_WARNING;
628 #endif
629 }
630 /*
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)
634 */
635 #ifndef sinix
636 if (
637 #ifdef __hpux
638 !p->opt.promisc &&
639 #endif
640 #ifdef HAVE_SOLARIS
641 !isatm
642 #endif
643 ) {
644 status = dlpromiscon(p, DL_PROMISC_SAP);
645 if (status < 0) {
646 /*
647 * Not fatal, since the DL_PROMISC_PHYS mode worked.
648 * Report it as a warning, however.
649 */
650 if (p->opt.promisc)
651 status = PCAP_WARNING;
652 else
653 goto bad;
654 }
655 }
656 #endif /* sinix */
657
658 /*
659 ** HP-UX 9, and HP-UX 10.20 or later, must bind after setting
660 ** promiscuous options.
661 */
662 #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20_OR_LATER)
663 if (dl_dohpuxbind(p->fd, p->errbuf) < 0)
664 goto bad;
665 /*
666 ** We don't set promiscuous mode on the send FD, but we'll defer
667 ** binding it anyway, just to keep the HP-UX 9/10.20 or later
668 ** code together.
669 */
670 if (p->send_fd >= 0) {
671 /*
672 ** XXX - if this fails, just close send_fd and
673 ** set it to -1, so that you can't send but can
674 ** still receive?
675 */
676 if (dl_dohpuxbind(p->send_fd, p->errbuf) < 0)
677 goto bad;
678 }
679 #endif
680
681 /*
682 ** Determine link type
683 ** XXX - get SAP length and address length as well, for use
684 ** when sending packets.
685 */
686 if (dlinforeq(p->fd, p->errbuf) < 0 ||
687 dlinfoack(p->fd, (char *)buf, p->errbuf) < 0)
688 goto bad;
689
690 infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack;
691 if (pcap_process_mactype(p, infop->dl_mac_type) != 0)
692 goto bad;
693
694 #ifdef DLIOCRAW
695 /*
696 ** This is a non standard SunOS hack to get the full raw link-layer
697 ** header.
698 */
699 if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) {
700 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "DLIOCRAW: %s",
701 pcap_strerror(errno));
702 goto bad;
703 }
704 #endif
705
706 #ifdef HAVE_SYS_BUFMOD_H
707 ss = p->snapshot;
708
709 /*
710 ** There is a bug in bufmod(7). When dealing with messages of
711 ** less than snaplen size it strips data from the beginning not
712 ** the end.
713 **
714 ** This bug is fixed in 5.3.2. Also, there is a patch available.
715 ** Ask for bugid 1149065.
716 */
717 #ifdef HAVE_SOLARIS
718 release = get_release(&osmajor, &osminor, &osmicro);
719 if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) &&
720 getenv("BUFMOD_FIXED") == NULL) {
721 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
722 "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.",
723 release);
724 ss = 0;
725 status = PCAP_WARNING;
726 }
727 #endif
728
729 /* Push and configure bufmod. */
730 if (pcap_conf_bufmod(p, ss, p->md.timeout) != 0)
731 goto bad;
732 #endif
733
734 /*
735 ** As the last operation flush the read side.
736 */
737 if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
738 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s",
739 pcap_strerror(errno));
740 goto bad;
741 }
742
743 /* Allocate data buffer. */
744 if (pcap_alloc_databuf(p) != 0)
745 goto bad;
746
747 /* Success - but perhaps with a warning */
748 if (status < 0)
749 status = 0;
750
751 /*
752 * "p->fd" is an FD for a STREAMS device, so "select()" and
753 * "poll()" should work on it.
754 */
755 p->selectable_fd = p->fd;
756
757 p->read_op = pcap_read_dlpi;
758 p->inject_op = pcap_inject_dlpi;
759 p->setfilter_op = install_bpf_program; /* no kernel filtering */
760 p->setdirection_op = NULL; /* Not implemented.*/
761 p->set_datalink_op = NULL; /* can't change data link type */
762 p->getnonblock_op = pcap_getnonblock_fd;
763 p->setnonblock_op = pcap_setnonblock_fd;
764 p->stats_op = pcap_stats_dlpi;
765 p->cleanup_op = pcap_cleanup_dlpi;
766
767 return (status);
768 bad:
769 pcap_cleanup_dlpi(p);
770 return (status);
771 }
772
773 /*
774 * Split a device name into a device type name and a unit number;
775 * return the a pointer to the beginning of the unit number, which
776 * is the end of the device type name, and set "*unitp" to the unit
777 * number.
778 *
779 * Returns NULL on error, and fills "ebuf" with an error message.
780 */
781 static char *
782 split_dname(char *device, int *unitp, char *ebuf)
783 {
784 char *cp;
785 char *eos;
786 long unit;
787
788 /*
789 * Look for a number at the end of the device name string.
790 */
791 cp = device + strlen(device) - 1;
792 if (*cp < '0' || *cp > '9') {
793 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s missing unit number",
794 device);
795 return (NULL);
796 }
797
798 /* Digits at end of string are unit number */
799 while (cp-1 >= device && *(cp-1) >= '0' && *(cp-1) <= '9')
800 cp--;
801
802 errno = 0;
803 unit = strtol(cp, &eos, 10);
804 if (*eos != '\0') {
805 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s bad unit number", device);
806 return (NULL);
807 }
808 if (errno == ERANGE || unit > INT_MAX) {
809 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number too large",
810 device);
811 return (NULL);
812 }
813 if (unit < 0) {
814 snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number is negative",
815 device);
816 return (NULL);
817 }
818 *unitp = (int)unit;
819 return (cp);
820 }
821
822 static int
823 dl_doattach(int fd, int ppa, char *ebuf)
824 {
825 dl_attach_req_t req;
826 bpf_u_int32 buf[MAXDLBUF];
827 int err;
828
829 req.dl_primitive = DL_ATTACH_REQ;
830 req.dl_ppa = ppa;
831 if (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf) < 0)
832 return (PCAP_ERROR);
833
834 err = dlokack(fd, "attach", (char *)buf, ebuf);
835 if (err < 0)
836 return (err);
837 return (0);
838 }
839
840 #ifdef DL_HP_RAWDLS
841 static int
842 dl_dohpuxbind(int fd, char *ebuf)
843 {
844 int hpsap;
845 int uerror;
846 bpf_u_int32 buf[MAXDLBUF];
847
848 /*
849 * XXX - we start at 22 because we used to use only 22, but
850 * that was just because that was the value used in some
851 * sample code from HP. With what value *should* we start?
852 * Does it matter, given that we're enabling SAP promiscuity
853 * on the input FD?
854 */
855 hpsap = 22;
856 for (;;) {
857 if (dlbindreq(fd, hpsap, ebuf) < 0)
858 return (-1);
859 if (dlbindack(fd, (char *)buf, ebuf, &uerror) >= 0)
860 break;
861 /*
862 * For any error other than a UNIX EBUSY, give up.
863 */
864 if (uerror != EBUSY) {
865 /*
866 * dlbindack() has already filled in ebuf for
867 * this error.
868 */
869 return (-1);
870 }
871
872 /*
873 * For EBUSY, try the next SAP value; that means that
874 * somebody else is using that SAP. Clear ebuf so
875 * that application doesn't report the "Device busy"
876 * error as a warning.
877 */
878 *ebuf = '\0';
879 hpsap++;
880 if (hpsap > 100) {
881 strlcpy(ebuf,
882 "All SAPs from 22 through 100 are in use",
883 PCAP_ERRBUF_SIZE);
884 return (-1);
885 }
886 }
887 return (0);
888 }
889 #endif
890
891 #define STRINGIFY(n) #n
892
893 static int
894 dlpromiscon(pcap_t *p, bpf_u_int32 level)
895 {
896 dl_promiscon_req_t req;
897 bpf_u_int32 buf[MAXDLBUF];
898 int err;
899
900 req.dl_primitive = DL_PROMISCON_REQ;
901 req.dl_level = level;
902 if (send_request(p->fd, (char *)&req, sizeof(req), "promiscon",
903 p->errbuf) < 0)
904 return (PCAP_ERROR);
905 err = dlokack(p->fd, "promiscon" STRINGIFY(level), (char *)buf,
906 p->errbuf);
907 if (err < 0)
908 return (err);
909 return (0);
910 }
911
912 int
913 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
914 {
915 #ifdef HAVE_SOLARIS
916 int fd;
917 union {
918 u_int nunits;
919 char pad[516]; /* XXX - must be at least 513; is 516
920 in "atmgetunits" */
921 } buf;
922 char baname[2+1+1];
923 u_int i;
924
925 /*
926 * We may have to do special magic to get ATM devices.
927 */
928 if ((fd = open("/dev/ba", O_RDWR)) < 0) {
929 /*
930 * We couldn't open the "ba" device.
931 * For now, just give up; perhaps we should
932 * return an error if the problem is neither
933 * a "that device doesn't exist" error (ENOENT,
934 * ENXIO, etc.) or a "you're not allowed to do
935 * that" error (EPERM, EACCES).
936 */
937 return (0);
938 }
939
940 if (strioctl(fd, A_GET_UNITS, sizeof(buf), (char *)&buf) < 0) {
941 snprintf(errbuf, PCAP_ERRBUF_SIZE, "A_GET_UNITS: %s",
942 pcap_strerror(errno));
943 return (-1);
944 }
945 for (i = 0; i < buf.nunits; i++) {
946 snprintf(baname, sizeof baname, "ba%u", i);
947 if (pcap_add_if(alldevsp, baname, 0, NULL, errbuf) < 0)
948 return (-1);
949 }
950 #endif
951
952 return (0);
953 }
954
955 static int
956 send_request(int fd, char *ptr, int len, char *what, char *ebuf)
957 {
958 struct strbuf ctl;
959 int flags;
960
961 ctl.maxlen = 0;
962 ctl.len = len;
963 ctl.buf = ptr;
964
965 flags = 0;
966 if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) {
967 snprintf(ebuf, PCAP_ERRBUF_SIZE,
968 "send_request: putmsg \"%s\": %s",
969 what, pcap_strerror(errno));
970 return (-1);
971 }
972 return (0);
973 }
974
975 static int
976 recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf, int *uerror)
977 {
978 union DL_primitives *dlp;
979 struct strbuf ctl;
980 int flags;
981
982 /*
983 * Clear out "*uerror", so it's only set for DL_ERROR_ACK/DL_SYSERR,
984 * making that the only place where EBUSY is treated specially.
985 */
986 if (uerror != NULL)
987 *uerror = 0;
988
989 ctl.maxlen = MAXDLBUF;
990 ctl.len = 0;
991 ctl.buf = bufp;
992
993 flags = 0;
994 if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) {
995 snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s getmsg: %s",
996 what, pcap_strerror(errno));
997 return (PCAP_ERROR);
998 }
999
1000 dlp = MAKE_DL_PRIMITIVES(ctl.buf);
1001 switch (dlp->dl_primitive) {
1002
1003 case DL_INFO_ACK:
1004 case DL_BIND_ACK:
1005 case DL_OK_ACK:
1006 #ifdef DL_HP_PPA_ACK
1007 case DL_HP_PPA_ACK:
1008 #endif
1009 /* These are OK */
1010 break;
1011
1012 case DL_ERROR_ACK:
1013 switch (dlp->error_ack.dl_errno) {
1014
1015 case DL_SYSERR:
1016 if (uerror != NULL)
1017 *uerror = dlp->error_ack.dl_unix_errno;
1018 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1019 "recv_ack: %s: UNIX error - %s",
1020 what, pcap_strerror(dlp->error_ack.dl_unix_errno));
1021 if (dlp->error_ack.dl_unix_errno == EPERM ||
1022 dlp->error_ack.dl_unix_errno == EACCES)
1023 return (PCAP_ERROR_PERM_DENIED);
1024 break;
1025
1026 default:
1027 snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s: %s",
1028 what, dlstrerror(dlp->error_ack.dl_errno));
1029 if (dlp->error_ack.dl_errno == DL_BADPPA)
1030 return (PCAP_ERROR_NO_SUCH_DEVICE);
1031 else if (dlp->error_ack.dl_errno == DL_ACCESS)
1032 return (PCAP_ERROR_PERM_DENIED);
1033 break;
1034 }
1035 return (PCAP_ERROR);
1036
1037 default:
1038 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1039 "recv_ack: %s: Unexpected primitive ack %s",
1040 what, dlprim(dlp->dl_primitive));
1041 return (PCAP_ERROR);
1042 }
1043
1044 if (ctl.len < size) {
1045 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1046 "recv_ack: %s: Ack too small (%d < %d)",
1047 what, ctl.len, size);
1048 return (PCAP_ERROR);
1049 }
1050 return (ctl.len);
1051 }
1052
1053 static char *
1054 dlstrerror(bpf_u_int32 dl_errno)
1055 {
1056 static char errstring[6+2+8+1];
1057
1058 switch (dl_errno) {
1059
1060 case DL_ACCESS:
1061 return ("Improper permissions for request");
1062
1063 case DL_BADADDR:
1064 return ("DLSAP addr in improper format or invalid");
1065
1066 case DL_BADCORR:
1067 return ("Seq number not from outstand DL_CONN_IND");
1068
1069 case DL_BADDATA:
1070 return ("User data exceeded provider limit");
1071
1072 case DL_BADPPA:
1073 #ifdef HAVE_DEV_DLPI
1074 /*
1075 * With a single "/dev/dlpi" device used for all
1076 * DLPI providers, PPAs have nothing to do with
1077 * unit numbers.
1078 */
1079 return ("Specified PPA was invalid");
1080 #else
1081 /*
1082 * We have separate devices for separate devices;
1083 * the PPA is just the unit number.
1084 */
1085 return ("Specified PPA (device unit) was invalid");
1086 #endif
1087
1088 case DL_BADPRIM:
1089 return ("Primitive received not known by provider");
1090
1091 case DL_BADQOSPARAM:
1092 return ("QOS parameters contained invalid values");
1093
1094 case DL_BADQOSTYPE:
1095 return ("QOS structure type is unknown/unsupported");
1096
1097 case DL_BADSAP:
1098 return ("Bad LSAP selector");
1099
1100 case DL_BADTOKEN:
1101 return ("Token used not an active stream");
1102
1103 case DL_BOUND:
1104 return ("Attempted second bind with dl_max_conind");
1105
1106 case DL_INITFAILED:
1107 return ("Physical link initialization failed");
1108
1109 case DL_NOADDR:
1110 return ("Provider couldn't allocate alternate address");
1111
1112 case DL_NOTINIT:
1113 return ("Physical link not initialized");
1114
1115 case DL_OUTSTATE:
1116 return ("Primitive issued in improper state");
1117
1118 case DL_SYSERR:
1119 return ("UNIX system error occurred");
1120
1121 case DL_UNSUPPORTED:
1122 return ("Requested service not supplied by provider");
1123
1124 case DL_UNDELIVERABLE:
1125 return ("Previous data unit could not be delivered");
1126
1127 case DL_NOTSUPPORTED:
1128 return ("Primitive is known but not supported");
1129
1130 case DL_TOOMANY:
1131 return ("Limit exceeded");
1132
1133 case DL_NOTENAB:
1134 return ("Promiscuous mode not enabled");
1135
1136 case DL_BUSY:
1137 return ("Other streams for PPA in post-attached");
1138
1139 case DL_NOAUTO:
1140 return ("Automatic handling XID&TEST not supported");
1141
1142 case DL_NOXIDAUTO:
1143 return ("Automatic handling of XID not supported");
1144
1145 case DL_NOTESTAUTO:
1146 return ("Automatic handling of TEST not supported");
1147
1148 case DL_XIDAUTO:
1149 return ("Automatic handling of XID response");
1150
1151 case DL_TESTAUTO:
1152 return ("Automatic handling of TEST response");
1153
1154 case DL_PENDING:
1155 return ("Pending outstanding connect indications");
1156
1157 default:
1158 sprintf(errstring, "Error %02x", dl_errno);
1159 return (errstring);
1160 }
1161 }
1162
1163 static char *
1164 dlprim(bpf_u_int32 prim)
1165 {
1166 static char primbuf[80];
1167
1168 switch (prim) {
1169
1170 case DL_INFO_REQ:
1171 return ("DL_INFO_REQ");
1172
1173 case DL_INFO_ACK:
1174 return ("DL_INFO_ACK");
1175
1176 case DL_ATTACH_REQ:
1177 return ("DL_ATTACH_REQ");
1178
1179 case DL_DETACH_REQ:
1180 return ("DL_DETACH_REQ");
1181
1182 case DL_BIND_REQ:
1183 return ("DL_BIND_REQ");
1184
1185 case DL_BIND_ACK:
1186 return ("DL_BIND_ACK");
1187
1188 case DL_UNBIND_REQ:
1189 return ("DL_UNBIND_REQ");
1190
1191 case DL_OK_ACK:
1192 return ("DL_OK_ACK");
1193
1194 case DL_ERROR_ACK:
1195 return ("DL_ERROR_ACK");
1196
1197 case DL_SUBS_BIND_REQ:
1198 return ("DL_SUBS_BIND_REQ");
1199
1200 case DL_SUBS_BIND_ACK:
1201 return ("DL_SUBS_BIND_ACK");
1202
1203 case DL_UNITDATA_REQ:
1204 return ("DL_UNITDATA_REQ");
1205
1206 case DL_UNITDATA_IND:
1207 return ("DL_UNITDATA_IND");
1208
1209 case DL_UDERROR_IND:
1210 return ("DL_UDERROR_IND");
1211
1212 case DL_UDQOS_REQ:
1213 return ("DL_UDQOS_REQ");
1214
1215 case DL_CONNECT_REQ:
1216 return ("DL_CONNECT_REQ");
1217
1218 case DL_CONNECT_IND:
1219 return ("DL_CONNECT_IND");
1220
1221 case DL_CONNECT_RES:
1222 return ("DL_CONNECT_RES");
1223
1224 case DL_CONNECT_CON:
1225 return ("DL_CONNECT_CON");
1226
1227 case DL_TOKEN_REQ:
1228 return ("DL_TOKEN_REQ");
1229
1230 case DL_TOKEN_ACK:
1231 return ("DL_TOKEN_ACK");
1232
1233 case DL_DISCONNECT_REQ:
1234 return ("DL_DISCONNECT_REQ");
1235
1236 case DL_DISCONNECT_IND:
1237 return ("DL_DISCONNECT_IND");
1238
1239 case DL_RESET_REQ:
1240 return ("DL_RESET_REQ");
1241
1242 case DL_RESET_IND:
1243 return ("DL_RESET_IND");
1244
1245 case DL_RESET_RES:
1246 return ("DL_RESET_RES");
1247
1248 case DL_RESET_CON:
1249 return ("DL_RESET_CON");
1250
1251 default:
1252 (void) sprintf(primbuf, "unknown primitive 0x%x", prim);
1253 return (primbuf);
1254 }
1255 }
1256
1257 static int
1258 dlbindreq(int fd, bpf_u_int32 sap, char *ebuf)
1259 {
1260
1261 dl_bind_req_t req;
1262
1263 memset((char *)&req, 0, sizeof(req));
1264 req.dl_primitive = DL_BIND_REQ;
1265 /* XXX - what if neither of these are defined? */
1266 #if defined(DL_HP_RAWDLS)
1267 req.dl_max_conind = 1; /* XXX magic number */
1268 req.dl_service_mode = DL_HP_RAWDLS;
1269 #elif defined(DL_CLDLS)
1270 req.dl_service_mode = DL_CLDLS;
1271 #endif
1272 req.dl_sap = sap;
1273
1274 return (send_request(fd, (char *)&req, sizeof(req), "bind", ebuf));
1275 }
1276
1277 static int
1278 dlbindack(int fd, char *bufp, char *ebuf, int *uerror)
1279 {
1280
1281 return (recv_ack(fd, DL_BIND_ACK_SIZE, "bind", bufp, ebuf, uerror));
1282 }
1283
1284 static int
1285 dlokack(int fd, const char *what, char *bufp, char *ebuf)
1286 {
1287
1288 return (recv_ack(fd, DL_OK_ACK_SIZE, what, bufp, ebuf, NULL));
1289 }
1290
1291
1292 static int
1293 dlinforeq(int fd, char *ebuf)
1294 {
1295 dl_info_req_t req;
1296
1297 req.dl_primitive = DL_INFO_REQ;
1298
1299 return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf));
1300 }
1301
1302 static int
1303 dlinfoack(int fd, char *bufp, char *ebuf)
1304 {
1305
1306 return (recv_ack(fd, DL_INFO_ACK_SIZE, "info", bufp, ebuf, NULL));
1307 }
1308
1309 #ifdef HAVE_DLPI_PASSIVE
1310 /*
1311 * Enable DLPI passive mode. We do not care if this request fails, as this
1312 * indicates the underlying DLPI device does not support link aggregation.
1313 */
1314 static void
1315 dlpassive(int fd, char *ebuf)
1316 {
1317 dl_passive_req_t req;
1318 bpf_u_int32 buf[MAXDLBUF];
1319
1320 req.dl_primitive = DL_PASSIVE_REQ;
1321
1322 if (send_request(fd, (char *)&req, sizeof(req), "dlpassive", ebuf) == 0)
1323 (void) dlokack(fd, "dlpassive", (char *)buf, ebuf);
1324 }
1325 #endif
1326
1327 #ifdef DL_HP_RAWDLS
1328 /*
1329 * There's an ack *if* there's an error.
1330 */
1331 static int
1332 dlrawdatareq(int fd, const u_char *datap, int datalen)
1333 {
1334 struct strbuf ctl, data;
1335 long buf[MAXDLBUF]; /* XXX - char? */
1336 union DL_primitives *dlp;
1337 int dlen;
1338
1339 dlp = MAKE_DL_PRIMITIVES(buf);
1340
1341 dlp->dl_primitive = DL_HP_RAWDATA_REQ;
1342 dlen = DL_HP_RAWDATA_REQ_SIZE;
1343
1344 /*
1345 * HP's documentation doesn't appear to show us supplying any
1346 * address pointed to by the control part of the message.
1347 * I think that's what raw mode means - you just send the raw
1348 * packet, you don't specify where to send it to, as that's
1349 * implied by the destination address.
1350 */
1351 ctl.maxlen = 0;
1352 ctl.len = dlen;
1353 ctl.buf = (void *)buf;
1354
1355 data.maxlen = 0;
1356 data.len = datalen;
1357 data.buf = (void *)datap;
1358
1359 return (putmsg(fd, &ctl, &data, 0));
1360 }
1361 #endif /* DL_HP_RAWDLS */
1362
1363 #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
1364 static char *
1365 get_release(bpf_u_int32 *majorp, bpf_u_int32 *minorp, bpf_u_int32 *microp)
1366 {
1367 char *cp;
1368 static char buf[32];
1369
1370 *majorp = 0;
1371 *minorp = 0;
1372 *microp = 0;
1373 if (sysinfo(SI_RELEASE, buf, sizeof(buf)) < 0)
1374 return ("?");
1375 cp = buf;
1376 if (!isdigit((unsigned char)*cp))
1377 return (buf);
1378 *majorp = strtol(cp, &cp, 10);
1379 if (*cp++ != '.')
1380 return (buf);
1381 *minorp = strtol(cp, &cp, 10);
1382 if (*cp++ != '.')
1383 return (buf);
1384 *microp = strtol(cp, &cp, 10);
1385 return (buf);
1386 }
1387 #endif
1388
1389 #ifdef DL_HP_PPA_REQ
1390 /*
1391 * Under HP-UX 10 and HP-UX 11, we can ask for the ppa
1392 */
1393
1394
1395 /*
1396 * Determine ppa number that specifies ifname.
1397 *
1398 * If the "dl_hp_ppa_info_t" doesn't have a "dl_module_id_1" member,
1399 * the code that's used here is the old code for HP-UX 10.x.
1400 *
1401 * However, HP-UX 10.20, at least, appears to have such a member
1402 * in its "dl_hp_ppa_info_t" structure, so the new code is used.
1403 * The new code didn't work on an old 10.20 system on which Rick
1404 * Jones of HP tried it, but with later patches installed, it
1405 * worked - it appears that the older system had those members but
1406 * didn't put anything in them, so, if the search by name fails, we
1407 * do the old search.
1408 *
1409 * Rick suggests that making sure your system is "up on the latest
1410 * lancommon/DLPI/driver patches" is probably a good idea; it'd fix
1411 * that problem, as well as allowing libpcap to see packets sent
1412 * from the system on which the libpcap application is being run.
1413 * (On 10.20, in addition to getting the latest patches, you need
1414 * to turn the kernel "lanc_outbound_promisc_flag" flag on with ADB;
1415 * a posting to "comp.sys.hp.hpux" at
1416 *
1417 * http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=558092266
1418 *
1419 * says that, to see the machine's outgoing traffic, you'd need to
1420 * apply the right patches to your system, and also set that variable
1421 * with:
1422
1423 echo 'lanc_outbound_promisc_flag/W1' | /usr/bin/adb -w /stand/vmunix /dev/kmem
1424
1425 * which could be put in, for example, "/sbin/init.d/lan".
1426 *
1427 * Setting the variable is not necessary on HP-UX 11.x.
1428 */
1429 static int
1430 get_dlpi_ppa(register int fd, register const char *device, register int unit,
1431 register char *ebuf)
1432 {
1433 register dl_hp_ppa_ack_t *ap;
1434 register dl_hp_ppa_info_t *ipstart, *ip;
1435 register int i;
1436 char dname[100];
1437 register u_long majdev;
1438 struct stat statbuf;
1439 dl_hp_ppa_req_t req;
1440 char buf[MAXDLBUF];
1441 char *ppa_data_buf;
1442 dl_hp_ppa_ack_t *dlp;
1443 struct strbuf ctl;
1444 int flags;
1445 int ppa;
1446
1447 memset((char *)&req, 0, sizeof(req));
1448 req.dl_primitive = DL_HP_PPA_REQ;
1449
1450 memset((char *)buf, 0, sizeof(buf));
1451 if (send_request(fd, (char *)&req, sizeof(req), "hpppa", ebuf) < 0)
1452 return (PCAP_ERROR);
1453
1454 ctl.maxlen = DL_HP_PPA_ACK_SIZE;
1455 ctl.len = 0;
1456 ctl.buf = (char *)buf;
1457
1458 flags = 0;
1459 /*
1460 * DLPI may return a big chunk of data for a DL_HP_PPA_REQ. The normal
1461 * recv_ack will fail because it set the maxlen to MAXDLBUF (8192)
1462 * which is NOT big enough for a DL_HP_PPA_REQ.
1463 *
1464 * This causes libpcap applications to fail on a system with HP-APA
1465 * installed.
1466 *
1467 * To figure out how big the returned data is, we first call getmsg
1468 * to get the small head and peek at the head to get the actual data
1469 * length, and then issue another getmsg to get the actual PPA data.
1470 */
1471 /* get the head first */
1472 if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
1473 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1474 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno));
1475 return (PCAP_ERROR);
1476 }
1477
1478 dlp = (dl_hp_ppa_ack_t *)ctl.buf;
1479 if (dlp->dl_primitive != DL_HP_PPA_ACK) {
1480 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1481 "get_dlpi_ppa: hpppa unexpected primitive ack 0x%x",
1482 (bpf_u_int32)dlp->dl_primitive);
1483 return (PCAP_ERROR);
1484 }
1485
1486 if (ctl.len < DL_HP_PPA_ACK_SIZE) {
1487 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1488 "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1489 ctl.len, (unsigned long)DL_HP_PPA_ACK_SIZE);
1490 return (PCAP_ERROR);
1491 }
1492
1493 /* allocate buffer */
1494 if ((ppa_data_buf = (char *)malloc(dlp->dl_length)) == NULL) {
1495 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1496 "get_dlpi_ppa: hpppa malloc: %s", pcap_strerror(errno));
1497 return (PCAP_ERROR);
1498 }
1499 ctl.maxlen = dlp->dl_length;
1500 ctl.len = 0;
1501 ctl.buf = (char *)ppa_data_buf;
1502 /* get the data */
1503 if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
1504 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1505 "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno));
1506 free(ppa_data_buf);
1507 return (PCAP_ERROR);
1508 }
1509 if (ctl.len < dlp->dl_length) {
1510 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1511 "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1512 ctl.len, (unsigned long)dlp->dl_length);
1513 free(ppa_data_buf);
1514 return (PCAP_ERROR);
1515 }
1516
1517 ap = (dl_hp_ppa_ack_t *)buf;
1518 ipstart = (dl_hp_ppa_info_t *)ppa_data_buf;
1519 ip = ipstart;
1520
1521 #ifdef HAVE_HP_PPA_INFO_T_DL_MODULE_ID_1
1522 /*
1523 * The "dl_hp_ppa_info_t" structure has a "dl_module_id_1"
1524 * member that should, in theory, contain the part of the
1525 * name for the device that comes before the unit number,
1526 * and should also have a "dl_module_id_2" member that may
1527 * contain an alternate name (e.g., I think Ethernet devices
1528 * have both "lan", for "lanN", and "snap", for "snapN", with
1529 * the former being for Ethernet packets and the latter being
1530 * for 802.3/802.2 packets).
1531 *
1532 * Search for the device that has the specified name and
1533 * instance number.
1534 */
1535 for (i = 0; i < ap->dl_count; i++) {
1536 if ((strcmp((const char *)ip->dl_module_id_1, device) == 0 ||
1537 strcmp((const char *)ip->dl_module_id_2, device) == 0) &&
1538 ip->dl_instance_num == unit)
1539 break;
1540
1541 ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
1542 }
1543 #else
1544 /*
1545 * We don't have that member, so the search is impossible; make it
1546 * look as if the search failed.
1547 */
1548 i = ap->dl_count;
1549 #endif
1550
1551 if (i == ap->dl_count) {
1552 /*
1553 * Well, we didn't, or can't, find the device by name.
1554 *
1555 * HP-UX 10.20, whilst it has "dl_module_id_1" and
1556 * "dl_module_id_2" fields in the "dl_hp_ppa_info_t",
1557 * doesn't seem to fill them in unless the system is
1558 * at a reasonably up-to-date patch level.
1559 *
1560 * Older HP-UX 10.x systems might not have those fields
1561 * at all.
1562 *
1563 * Therefore, we'll search for the entry with the major
1564 * device number of a device with the name "/dev/<dev><unit>",
1565 * if such a device exists, as the old code did.
1566 */
1567 snprintf(dname, sizeof(dname), "/dev/%s%d", device, unit);
1568 if (stat(dname, &statbuf) < 0) {
1569 snprintf(ebuf, PCAP_ERRBUF_SIZE, "stat: %s: %s",
1570 dname, pcap_strerror(errno));
1571 return (PCAP_ERROR);
1572 }
1573 majdev = major(statbuf.st_rdev);
1574
1575 ip = ipstart;
1576
1577 for (i = 0; i < ap->dl_count; i++) {
1578 if (ip->dl_mjr_num == majdev &&
1579 ip->dl_instance_num == unit)
1580 break;
1581
1582 ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
1583 }
1584 }
1585 if (i == ap->dl_count) {
1586 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1587 "can't find /dev/dlpi PPA for %s%d", device, unit);
1588 return (PCAP_ERROR_NO_SUCH_DEVICE);
1589 }
1590 if (ip->dl_hdw_state == HDW_DEAD) {
1591 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1592 "%s%d: hardware state: DOWN\n", device, unit);
1593 free(ppa_data_buf);
1594 return (PCAP_ERROR);
1595 }
1596 ppa = ip->dl_ppa;
1597 free(ppa_data_buf);
1598 return (ppa);
1599 }
1600 #endif
1601
1602 #ifdef HAVE_HPUX9
1603 /*
1604 * Under HP-UX 9, there is no good way to determine the ppa.
1605 * So punt and read it from /dev/kmem.
1606 */
1607 static struct nlist nl[] = {
1608 #define NL_IFNET 0
1609 { "ifnet" },
1610 { "" }
1611 };
1612
1613 static char path_vmunix[] = "/hp-ux";
1614
1615 /* Determine ppa number that specifies ifname */
1616 static int
1617 get_dlpi_ppa(register int fd, register const char *ifname, register int unit,
1618 register char *ebuf)
1619 {
1620 register const char *cp;
1621 register int kd;
1622 void *addr;
1623 struct ifnet ifnet;
1624 char if_name[sizeof(ifnet.if_name) + 1];
1625
1626 cp = strrchr(ifname, '/');
1627 if (cp != NULL)
1628 ifname = cp + 1;
1629 if (nlist(path_vmunix, &nl) < 0) {
1630 snprintf(ebuf, PCAP_ERRBUF_SIZE, "nlist %s failed",
1631 path_vmunix);
1632 return (-1);
1633 }
1634 if (nl[NL_IFNET].n_value == 0) {
1635 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1636 "could't find %s kernel symbol",
1637 nl[NL_IFNET].n_name);
1638 return (-1);
1639 }
1640 kd = open("/dev/kmem", O_RDONLY);
1641 if (kd < 0) {
1642 snprintf(ebuf, PCAP_ERRBUF_SIZE, "kmem open: %s",
1643 pcap_strerror(errno));
1644 return (-1);
1645 }
1646 if (dlpi_kread(kd, nl[NL_IFNET].n_value,
1647 &addr, sizeof(addr), ebuf) < 0) {
1648 close(kd);
1649 return (-1);
1650 }
1651 for (; addr != NULL; addr = ifnet.if_next) {
1652 if (dlpi_kread(kd, (off_t)addr,
1653 &ifnet, sizeof(ifnet), ebuf) < 0 ||
1654 dlpi_kread(kd, (off_t)ifnet.if_name,
1655 if_name, sizeof(ifnet.if_name), ebuf) < 0) {
1656 (void)close(kd);
1657 return (-1);
1658 }
1659 if_name[sizeof(ifnet.if_name)] = '\0';
1660 if (strcmp(if_name, ifname) == 0 && ifnet.if_unit == unit)
1661 return (ifnet.if_index);
1662 }
1663
1664 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't find %s", ifname);
1665 return (-1);
1666 }
1667
1668 static int
1669 dlpi_kread(register int fd, register off_t addr,
1670 register void *buf, register u_int len, register char *ebuf)
1671 {
1672 register int cc;
1673
1674 if (lseek(fd, addr, SEEK_SET) < 0) {
1675 snprintf(ebuf, PCAP_ERRBUF_SIZE, "lseek: %s",
1676 pcap_strerror(errno));
1677 return (-1);
1678 }
1679 cc = read(fd, buf, len);
1680 if (cc < 0) {
1681 snprintf(ebuf, PCAP_ERRBUF_SIZE, "read: %s",
1682 pcap_strerror(errno));
1683 return (-1);
1684 } else if (cc != len) {
1685 snprintf(ebuf, PCAP_ERRBUF_SIZE, "short read (%d != %d)", cc,
1686 len);
1687 return (-1);
1688 }
1689 return (cc);
1690 }
1691 #endif
1692
1693 pcap_t *
1694 pcap_create(const char *device, char *ebuf)
1695 {
1696 pcap_t *p;
1697
1698 p = pcap_create_common(device, ebuf);
1699 if (p == NULL)
1700 return (NULL);
1701
1702 p->send_fd = -1; /* it hasn't been opened yet */
1703
1704 p->activate_op = pcap_activate_dlpi;
1705 return (p);
1706 }