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