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