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