1a8e07101SRui Paulo /* 2a8e07101SRui Paulo * Copyright (c) 1993, 1994, 1995, 1996, 1997 3a8e07101SRui Paulo * The Regents of the University of California. All rights reserved. 4a8e07101SRui Paulo * 5a8e07101SRui Paulo * Redistribution and use in source and binary forms, with or without 6a8e07101SRui Paulo * modification, are permitted provided that: (1) source code distributions 7a8e07101SRui Paulo * retain the above copyright notice and this paragraph in its entirety, (2) 8a8e07101SRui Paulo * distributions including binary code include the above copyright notice and 9a8e07101SRui Paulo * this paragraph in its entirety in the documentation or other materials 10a8e07101SRui Paulo * provided with the distribution, and (3) all advertising materials mentioning 11a8e07101SRui Paulo * features or use of this software display the following acknowledgement: 12a8e07101SRui Paulo * ``This product includes software developed by the University of California, 13a8e07101SRui Paulo * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14a8e07101SRui Paulo * the University nor the names of its contributors may be used to endorse 15a8e07101SRui Paulo * or promote products derived from this software without specific prior 16a8e07101SRui Paulo * written permission. 17a8e07101SRui Paulo * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18a8e07101SRui Paulo * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19a8e07101SRui Paulo * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20a8e07101SRui Paulo * 21a8e07101SRui Paulo * This code contributed by Sagun Shakya (sagun.shakya@sun.com) 22a8e07101SRui Paulo */ 23a8e07101SRui Paulo /* 24a8e07101SRui Paulo * Packet capture routines for DLPI using libdlpi under SunOS 5.11. 25a8e07101SRui Paulo */ 26a8e07101SRui Paulo 27a8e07101SRui Paulo #ifndef lint 28a8e07101SRui Paulo static const char rcsid[] _U_ = 29*a0ee43a1SRui Paulo "@(#) $Header: /tcpdump/master/libpcap/pcap-libdlpi.c,v 1.6 2008-04-14 20:40:58 guy Exp $ (LBL)"; 30a8e07101SRui Paulo #endif 31a8e07101SRui Paulo 32a8e07101SRui Paulo #ifdef HAVE_CONFIG_H 33a8e07101SRui Paulo #include "config.h" 34a8e07101SRui Paulo #endif 35a8e07101SRui Paulo 36a8e07101SRui Paulo #include <sys/types.h> 37a8e07101SRui Paulo #include <sys/time.h> 38a8e07101SRui Paulo #include <sys/bufmod.h> 39a8e07101SRui Paulo #include <sys/stream.h> 40a8e07101SRui Paulo #include <libdlpi.h> 41a8e07101SRui Paulo #include <errno.h> 42a8e07101SRui Paulo #include <memory.h> 43a8e07101SRui Paulo #include <stropts.h> 44a8e07101SRui Paulo #include <stdio.h> 45a8e07101SRui Paulo #include <stdlib.h> 46a8e07101SRui Paulo #include <string.h> 47a8e07101SRui Paulo 48a8e07101SRui Paulo #include "pcap-int.h" 49a8e07101SRui Paulo #include "dlpisubs.h" 50a8e07101SRui Paulo 51a8e07101SRui Paulo /* Forwards. */ 52a8e07101SRui Paulo static int pcap_read_libdlpi(pcap_t *, int, pcap_handler, u_char *); 53a8e07101SRui Paulo static int pcap_inject_libdlpi(pcap_t *, const void *, size_t); 54a8e07101SRui Paulo static void pcap_close_libdlpi(pcap_t *); 55a8e07101SRui Paulo static void pcap_libdlpi_err(const char *, const char *, int, char *); 56*a0ee43a1SRui Paulo static void pcap_cleanup_libdlpi(pcap_t *); 57a8e07101SRui Paulo 58a8e07101SRui Paulo /* 59a8e07101SRui Paulo * list_interfaces() will list all the network links that are 60a8e07101SRui Paulo * available on a system. 61a8e07101SRui Paulo */ 62a8e07101SRui Paulo static boolean_t list_interfaces(const char *, void *); 63a8e07101SRui Paulo 64a8e07101SRui Paulo typedef struct linknamelist { 65a8e07101SRui Paulo char linkname[DLPI_LINKNAME_MAX]; 66a8e07101SRui Paulo struct linknamelist *lnl_next; 67a8e07101SRui Paulo } linknamelist_t; 68a8e07101SRui Paulo 69a8e07101SRui Paulo typedef struct linkwalk { 70a8e07101SRui Paulo linknamelist_t *lw_list; 71a8e07101SRui Paulo int lw_err; 72a8e07101SRui Paulo } linkwalk_t; 73a8e07101SRui Paulo 74a8e07101SRui Paulo /* 75a8e07101SRui Paulo * The caller of this function should free the memory allocated 76a8e07101SRui Paulo * for each linknamelist_t "entry" allocated. 77a8e07101SRui Paulo */ 78a8e07101SRui Paulo static boolean_t 79a8e07101SRui Paulo list_interfaces(const char *linkname, void *arg) 80a8e07101SRui Paulo { 81a8e07101SRui Paulo linkwalk_t *lwp = arg; 82a8e07101SRui Paulo linknamelist_t *entry; 83a8e07101SRui Paulo 84a8e07101SRui Paulo if ((entry = calloc(1, sizeof(linknamelist_t))) == NULL) { 85a8e07101SRui Paulo lwp->lw_err = ENOMEM; 86a8e07101SRui Paulo return (B_TRUE); 87a8e07101SRui Paulo } 88a8e07101SRui Paulo (void) strlcpy(entry->linkname, linkname, DLPI_LINKNAME_MAX); 89a8e07101SRui Paulo 90a8e07101SRui Paulo if (lwp->lw_list == NULL) { 91a8e07101SRui Paulo lwp->lw_list = entry; 92a8e07101SRui Paulo } else { 93a8e07101SRui Paulo entry->lnl_next = lwp->lw_list; 94a8e07101SRui Paulo lwp->lw_list = entry; 95a8e07101SRui Paulo } 96a8e07101SRui Paulo 97a8e07101SRui Paulo return (B_FALSE); 98a8e07101SRui Paulo } 99a8e07101SRui Paulo 100a8e07101SRui Paulo static int 101a8e07101SRui Paulo pcap_activate_libdlpi(pcap_t *p) 102a8e07101SRui Paulo { 103a8e07101SRui Paulo int retv; 104a8e07101SRui Paulo dlpi_handle_t dh; 105a8e07101SRui Paulo dlpi_info_t dlinfo; 106a8e07101SRui Paulo int err = PCAP_ERROR; 107a8e07101SRui Paulo 108a8e07101SRui Paulo /* 109a8e07101SRui Paulo * Enable Solaris raw and passive DLPI extensions; 110a8e07101SRui Paulo * dlpi_open() will not fail if the underlying link does not support 111a8e07101SRui Paulo * passive mode. See dlpi(7P) for details. 112a8e07101SRui Paulo */ 113a8e07101SRui Paulo retv = dlpi_open(p->opt.source, &dh, DLPI_RAW|DLPI_PASSIVE); 114a8e07101SRui Paulo if (retv != DLPI_SUCCESS) { 115a8e07101SRui Paulo if (retv == DLPI_ELINKNAMEINVAL || retv == DLPI_ENOLINK) 116a8e07101SRui Paulo err = PCAP_ERROR_NO_SUCH_DEVICE; 117*a0ee43a1SRui Paulo else if (retv == DL_SYSERR && errno == EACCES) 118a8e07101SRui Paulo err = PCAP_ERROR_PERM_DENIED; 119a8e07101SRui Paulo pcap_libdlpi_err(p->opt.source, "dlpi_open", retv, 120a8e07101SRui Paulo p->errbuf); 121a8e07101SRui Paulo return (err); 122a8e07101SRui Paulo } 123a8e07101SRui Paulo p->dlpi_hd = dh; 124a8e07101SRui Paulo 125a8e07101SRui Paulo if (p->opt.rfmon) { 126a8e07101SRui Paulo /* 127a8e07101SRui Paulo * This device exists, but we don't support monitor mode 128a8e07101SRui Paulo * any platforms that support DLPI. 129a8e07101SRui Paulo */ 130a8e07101SRui Paulo err = PCAP_ERROR_RFMON_NOTSUP; 131a8e07101SRui Paulo goto bad; 132a8e07101SRui Paulo } 133a8e07101SRui Paulo 134a8e07101SRui Paulo /* Bind with DLPI_ANY_SAP. */ 135a8e07101SRui Paulo if ((retv = dlpi_bind(p->dlpi_hd, DLPI_ANY_SAP, 0)) != DLPI_SUCCESS) { 136a8e07101SRui Paulo pcap_libdlpi_err(p->opt.source, "dlpi_bind", retv, p->errbuf); 137a8e07101SRui Paulo goto bad; 138a8e07101SRui Paulo } 139a8e07101SRui Paulo 140a8e07101SRui Paulo /* Enable promiscuous mode. */ 141a8e07101SRui Paulo if (p->opt.promisc) { 142a8e07101SRui Paulo retv = dlpi_promiscon(p->dlpi_hd, DL_PROMISC_PHYS); 143a8e07101SRui Paulo if (retv != DLPI_SUCCESS) { 144a8e07101SRui Paulo pcap_libdlpi_err(p->opt.source, 145a8e07101SRui Paulo "dlpi_promisc(PHYSICAL)", retv, p->errbuf); 146a8e07101SRui Paulo goto bad; 147a8e07101SRui Paulo } 148a8e07101SRui Paulo } else { 149a8e07101SRui Paulo /* Try to enable multicast. */ 150a8e07101SRui Paulo retv = dlpi_promiscon(p->dlpi_hd, DL_PROMISC_MULTI); 151a8e07101SRui Paulo if (retv != DLPI_SUCCESS) { 152a8e07101SRui Paulo pcap_libdlpi_err(p->opt.source, "dlpi_promisc(MULTI)", 153a8e07101SRui Paulo retv, p->errbuf); 154a8e07101SRui Paulo goto bad; 155a8e07101SRui Paulo } 156a8e07101SRui Paulo } 157a8e07101SRui Paulo 158a8e07101SRui Paulo /* Try to enable SAP promiscuity. */ 159*a0ee43a1SRui Paulo retv = dlpi_promiscon(p->dlpi_hd, DL_PROMISC_SAP); 160*a0ee43a1SRui Paulo if (retv != DLPI_SUCCESS) { 161*a0ee43a1SRui Paulo if (p->opt.promisc) { 162a8e07101SRui Paulo pcap_libdlpi_err(p->opt.source, "dlpi_promisc(SAP)", 163a8e07101SRui Paulo retv, p->errbuf); 164a8e07101SRui Paulo goto bad; 165a8e07101SRui Paulo } 166a8e07101SRui Paulo 167a8e07101SRui Paulo /* Not fatal, since the DL_PROMISC_PHYS mode worked. */ 168a8e07101SRui Paulo fprintf(stderr, "WARNING: dlpi_promisc(SAP) failed on" 169a8e07101SRui Paulo " %s:(%s)\n", p->opt.source, dlpi_strerror(retv)); 170a8e07101SRui Paulo } 171a8e07101SRui Paulo 172a8e07101SRui Paulo /* Determine link type. */ 173a8e07101SRui Paulo if ((retv = dlpi_info(p->dlpi_hd, &dlinfo, 0)) != DLPI_SUCCESS) { 174a8e07101SRui Paulo pcap_libdlpi_err(p->opt.source, "dlpi_info", retv, p->errbuf); 175a8e07101SRui Paulo goto bad; 176a8e07101SRui Paulo } 177a8e07101SRui Paulo 178a8e07101SRui Paulo if (pcap_process_mactype(p, dlinfo.di_mactype) != 0) 179a8e07101SRui Paulo goto bad; 180a8e07101SRui Paulo 181a8e07101SRui Paulo p->fd = dlpi_fd(p->dlpi_hd); 182a8e07101SRui Paulo 183a8e07101SRui Paulo /* Push and configure bufmod. */ 184*a0ee43a1SRui Paulo if (pcap_conf_bufmod(p, p->snapshot, p->md.timeout) != 0) 185a8e07101SRui Paulo goto bad; 186a8e07101SRui Paulo 187a8e07101SRui Paulo /* 188a8e07101SRui Paulo * Flush the read side. 189a8e07101SRui Paulo */ 190a8e07101SRui Paulo if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) { 191a8e07101SRui Paulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s", 192a8e07101SRui Paulo pcap_strerror(errno)); 193a8e07101SRui Paulo goto bad; 194a8e07101SRui Paulo } 195a8e07101SRui Paulo 196a8e07101SRui Paulo /* Allocate data buffer. */ 197a8e07101SRui Paulo if (pcap_alloc_databuf(p) != 0) 198a8e07101SRui Paulo goto bad; 199a8e07101SRui Paulo 200a8e07101SRui Paulo /* 201a8e07101SRui Paulo * "p->fd" is a FD for a STREAMS device, so "select()" and 202a8e07101SRui Paulo * "poll()" should work on it. 203a8e07101SRui Paulo */ 204a8e07101SRui Paulo p->selectable_fd = p->fd; 205a8e07101SRui Paulo 206a8e07101SRui Paulo p->read_op = pcap_read_libdlpi; 207a8e07101SRui Paulo p->inject_op = pcap_inject_libdlpi; 208a8e07101SRui Paulo p->setfilter_op = install_bpf_program; /* No kernel filtering */ 209a8e07101SRui Paulo p->setdirection_op = NULL; /* Not implemented */ 210a8e07101SRui Paulo p->set_datalink_op = NULL; /* Can't change data link type */ 211a8e07101SRui Paulo p->getnonblock_op = pcap_getnonblock_fd; 212a8e07101SRui Paulo p->setnonblock_op = pcap_setnonblock_fd; 213a8e07101SRui Paulo p->stats_op = pcap_stats_dlpi; 214a8e07101SRui Paulo p->cleanup_op = pcap_cleanup_libdlpi; 215a8e07101SRui Paulo 216a8e07101SRui Paulo return (0); 217a8e07101SRui Paulo bad: 218a8e07101SRui Paulo pcap_cleanup_libdlpi(p); 219a8e07101SRui Paulo return (err); 220a8e07101SRui Paulo } 221a8e07101SRui Paulo 222a8e07101SRui Paulo /* 223a8e07101SRui Paulo * In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find 224a8e07101SRui Paulo * network links that are plumbed and are up. dlpi_walk(3DLPI) will find 225a8e07101SRui Paulo * additional network links present in the system. 226a8e07101SRui Paulo */ 227a8e07101SRui Paulo int 228a8e07101SRui Paulo pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 229a8e07101SRui Paulo { 230a8e07101SRui Paulo int retv = 0; 231a8e07101SRui Paulo 232a8e07101SRui Paulo linknamelist_t *entry, *next; 233a8e07101SRui Paulo linkwalk_t lw = {NULL, 0}; 234a8e07101SRui Paulo int save_errno; 235a8e07101SRui Paulo 236a8e07101SRui Paulo /* dlpi_walk() for loopback will be added here. */ 237a8e07101SRui Paulo 238a8e07101SRui Paulo dlpi_walk(list_interfaces, &lw, 0); 239a8e07101SRui Paulo 240a8e07101SRui Paulo if (lw.lw_err != 0) { 241a8e07101SRui Paulo snprintf(errbuf, PCAP_ERRBUF_SIZE, 242a8e07101SRui Paulo "dlpi_walk: %s", pcap_strerror(lw.lw_err)); 243a8e07101SRui Paulo retv = -1; 244a8e07101SRui Paulo goto done; 245a8e07101SRui Paulo } 246a8e07101SRui Paulo 247a8e07101SRui Paulo /* Add linkname if it does not exist on the list. */ 248a8e07101SRui Paulo for (entry = lw.lw_list; entry != NULL; entry = entry->lnl_next) { 249a8e07101SRui Paulo if (pcap_add_if(alldevsp, entry->linkname, 0, NULL, errbuf) < 0) 250a8e07101SRui Paulo retv = -1; 251a8e07101SRui Paulo } 252a8e07101SRui Paulo done: 253a8e07101SRui Paulo save_errno = errno; 254a8e07101SRui Paulo for (entry = lw.lw_list; entry != NULL; entry = next) { 255a8e07101SRui Paulo next = entry->lnl_next; 256a8e07101SRui Paulo free(entry); 257a8e07101SRui Paulo } 258a8e07101SRui Paulo errno = save_errno; 259a8e07101SRui Paulo 260a8e07101SRui Paulo return (retv); 261a8e07101SRui Paulo } 262a8e07101SRui Paulo 263a8e07101SRui Paulo /* 264a8e07101SRui Paulo * Read data received on DLPI handle. Returns -2 if told to terminate, else 265a8e07101SRui Paulo * returns the number of packets read. 266a8e07101SRui Paulo */ 267a8e07101SRui Paulo static int 268a8e07101SRui Paulo pcap_read_libdlpi(pcap_t *p, int count, pcap_handler callback, u_char *user) 269a8e07101SRui Paulo { 270a8e07101SRui Paulo int len; 271a8e07101SRui Paulo u_char *bufp; 272a8e07101SRui Paulo size_t msglen; 273a8e07101SRui Paulo int retv; 274a8e07101SRui Paulo 275a8e07101SRui Paulo len = p->cc; 276a8e07101SRui Paulo if (len != 0) { 277a8e07101SRui Paulo bufp = p->bp; 278a8e07101SRui Paulo goto process_pkts; 279a8e07101SRui Paulo } 280a8e07101SRui Paulo do { 281a8e07101SRui Paulo /* Has "pcap_breakloop()" been called? */ 282a8e07101SRui Paulo if (p->break_loop) { 283a8e07101SRui Paulo /* 284a8e07101SRui Paulo * Yes - clear the flag that indicates that it has, 285a8e07101SRui Paulo * and return -2 to indicate that we were told to 286a8e07101SRui Paulo * break out of the loop. 287a8e07101SRui Paulo */ 288a8e07101SRui Paulo p->break_loop = 0; 289a8e07101SRui Paulo return (-2); 290a8e07101SRui Paulo } 291a8e07101SRui Paulo 292a8e07101SRui Paulo msglen = p->bufsize; 293a8e07101SRui Paulo bufp = p->buffer + p->offset; 294a8e07101SRui Paulo 295a8e07101SRui Paulo retv = dlpi_recv(p->dlpi_hd, NULL, NULL, bufp, 296a8e07101SRui Paulo &msglen, -1, NULL); 297a8e07101SRui Paulo if (retv != DLPI_SUCCESS) { 298a8e07101SRui Paulo /* 299a8e07101SRui Paulo * This is most likely a call to terminate out of the 300a8e07101SRui Paulo * loop. So, do not return an error message, instead 301a8e07101SRui Paulo * check if "pcap_breakloop()" has been called above. 302a8e07101SRui Paulo */ 303a8e07101SRui Paulo if (retv == DL_SYSERR && errno == EINTR) { 304a8e07101SRui Paulo len = 0; 305a8e07101SRui Paulo continue; 306a8e07101SRui Paulo } 307a8e07101SRui Paulo pcap_libdlpi_err(dlpi_linkname(p->dlpi_hd), 308a8e07101SRui Paulo "dlpi_recv", retv, p->errbuf); 309a8e07101SRui Paulo return (-1); 310a8e07101SRui Paulo } 311a8e07101SRui Paulo len = msglen; 312a8e07101SRui Paulo } while (len == 0); 313a8e07101SRui Paulo 314a8e07101SRui Paulo process_pkts: 315a8e07101SRui Paulo return (pcap_process_pkts(p, callback, user, count, bufp, len)); 316a8e07101SRui Paulo } 317a8e07101SRui Paulo 318a8e07101SRui Paulo static int 319a8e07101SRui Paulo pcap_inject_libdlpi(pcap_t *p, const void *buf, size_t size) 320a8e07101SRui Paulo { 321a8e07101SRui Paulo int retv; 322a8e07101SRui Paulo 323a8e07101SRui Paulo retv = dlpi_send(p->dlpi_hd, NULL, 0, buf, size, NULL); 324a8e07101SRui Paulo if (retv != DLPI_SUCCESS) { 325a8e07101SRui Paulo pcap_libdlpi_err(dlpi_linkname(p->dlpi_hd), "dlpi_send", retv, 326a8e07101SRui Paulo p->errbuf); 327a8e07101SRui Paulo return (-1); 328a8e07101SRui Paulo } 329a8e07101SRui Paulo /* 330a8e07101SRui Paulo * dlpi_send(3DLPI) does not provide a way to return the number of 331a8e07101SRui Paulo * bytes sent on the wire. Based on the fact that DLPI_SUCCESS was 332a8e07101SRui Paulo * returned we are assuming 'size' bytes were sent. 333a8e07101SRui Paulo */ 334a8e07101SRui Paulo return (size); 335a8e07101SRui Paulo } 336a8e07101SRui Paulo 337a8e07101SRui Paulo /* 338a8e07101SRui Paulo * Close dlpi handle. 339a8e07101SRui Paulo */ 340a8e07101SRui Paulo static void 341a8e07101SRui Paulo pcap_cleanup_libdlpi(pcap_t *p) 342a8e07101SRui Paulo { 343a8e07101SRui Paulo if (p->dlpi_hd != NULL) { 344a8e07101SRui Paulo dlpi_close(p->dlpi_hd); 345a8e07101SRui Paulo p->dlpi_hd = NULL; 346a8e07101SRui Paulo p->fd = -1; 347a8e07101SRui Paulo } 348a8e07101SRui Paulo pcap_cleanup_live_common(p); 349a8e07101SRui Paulo } 350a8e07101SRui Paulo 351a8e07101SRui Paulo /* 352a8e07101SRui Paulo * Write error message to buffer. 353a8e07101SRui Paulo */ 354a8e07101SRui Paulo static void 355a8e07101SRui Paulo pcap_libdlpi_err(const char *linkname, const char *func, int err, char *errbuf) 356a8e07101SRui Paulo { 357a8e07101SRui Paulo snprintf(errbuf, PCAP_ERRBUF_SIZE, "libpcap: %s failed on %s: %s", 358a8e07101SRui Paulo func, linkname, dlpi_strerror(err)); 359a8e07101SRui Paulo } 360a8e07101SRui Paulo 361a8e07101SRui Paulo pcap_t * 362a8e07101SRui Paulo pcap_create(const char *device, char *ebuf) 363a8e07101SRui Paulo { 364a8e07101SRui Paulo pcap_t *p; 365a8e07101SRui Paulo 366a8e07101SRui Paulo p = pcap_create_common(device, ebuf); 367a8e07101SRui Paulo if (p == NULL) 368a8e07101SRui Paulo return (NULL); 369a8e07101SRui Paulo 370a8e07101SRui Paulo p->activate_op = pcap_activate_libdlpi; 371a8e07101SRui Paulo return (p); 372a8e07101SRui Paulo } 373