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