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