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