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