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