1335640Shselasky/* 2335640Shselasky * Copyright (c) 1993, 1994, 1995, 1996, 1997 3335640Shselasky * The Regents of the University of California. All rights reserved. 4335640Shselasky * 5335640Shselasky * Redistribution and use in source and binary forms, with or without 6335640Shselasky * modification, are permitted provided that: (1) source code distributions 7335640Shselasky * retain the above copyright notice and this paragraph in its entirety, (2) 8335640Shselasky * distributions including binary code include the above copyright notice and 9335640Shselasky * this paragraph in its entirety in the documentation or other materials 10335640Shselasky * provided with the distribution, and (3) all advertising materials mentioning 11335640Shselasky * features or use of this software display the following acknowledgement: 12335640Shselasky * ``This product includes software developed by the University of California, 13335640Shselasky * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14335640Shselasky * the University nor the names of its contributors may be used to endorse 15335640Shselasky * or promote products derived from this software without specific prior 16335640Shselasky * written permission. 17335640Shselasky * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18335640Shselasky * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19335640Shselasky * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20335640Shselasky * 21335640Shselasky * This code contributed by Sagun Shakya (sagun.shakya@sun.com) 22335640Shselasky */ 23335640Shselasky/* 24335640Shselasky * Packet capture routines for DLPI using libdlpi under SunOS 5.11. 25335640Shselasky */ 26335640Shselasky 27335640Shselasky#ifdef HAVE_CONFIG_H 28335640Shselasky#include <config.h> 29335640Shselasky#endif 30335640Shselasky 31335640Shselasky#include <sys/types.h> 32335640Shselasky#include <sys/time.h> 33335640Shselasky#include <sys/bufmod.h> 34335640Shselasky#include <sys/stream.h> 35335640Shselasky#include <libdlpi.h> 36335640Shselasky#include <errno.h> 37335640Shselasky#include <memory.h> 38335640Shselasky#include <stropts.h> 39335640Shselasky#include <stdio.h> 40335640Shselasky#include <stdlib.h> 41335640Shselasky#include <string.h> 42335640Shselasky 43335640Shselasky#include "pcap-int.h" 44335640Shselasky#include "dlpisubs.h" 45335640Shselasky 46335640Shselasky/* Forwards. */ 47335640Shselaskystatic int dlpromiscon(pcap_t *, bpf_u_int32); 48335640Shselaskystatic int pcap_read_libdlpi(pcap_t *, int, pcap_handler, u_char *); 49335640Shselaskystatic int pcap_inject_libdlpi(pcap_t *, const void *, size_t); 50335640Shselaskystatic void pcap_libdlpi_err(const char *, const char *, int, char *); 51335640Shselaskystatic void pcap_cleanup_libdlpi(pcap_t *); 52335640Shselasky 53335640Shselasky/* 54335640Shselasky * list_interfaces() will list all the network links that are 55335640Shselasky * available on a system. 56335640Shselasky */ 57335640Shselaskystatic boolean_t list_interfaces(const char *, void *); 58335640Shselasky 59335640Shselaskytypedef struct linknamelist { 60335640Shselasky char linkname[DLPI_LINKNAME_MAX]; 61335640Shselasky struct linknamelist *lnl_next; 62335640Shselasky} linknamelist_t; 63335640Shselasky 64335640Shselaskytypedef struct linkwalk { 65335640Shselasky linknamelist_t *lw_list; 66335640Shselasky int lw_err; 67335640Shselasky} linkwalk_t; 68335640Shselasky 69335640Shselasky/* 70335640Shselasky * The caller of this function should free the memory allocated 71335640Shselasky * for each linknamelist_t "entry" allocated. 72335640Shselasky */ 73335640Shselaskystatic boolean_t 74335640Shselaskylist_interfaces(const char *linkname, void *arg) 75335640Shselasky{ 76335640Shselasky linkwalk_t *lwp = arg; 77335640Shselasky linknamelist_t *entry; 78335640Shselasky 79335640Shselasky if ((entry = calloc(1, sizeof(linknamelist_t))) == NULL) { 80335640Shselasky lwp->lw_err = ENOMEM; 81335640Shselasky return (B_TRUE); 82335640Shselasky } 83356341Scy (void) pcap_strlcpy(entry->linkname, linkname, DLPI_LINKNAME_MAX); 84335640Shselasky 85335640Shselasky if (lwp->lw_list == NULL) { 86335640Shselasky lwp->lw_list = entry; 87335640Shselasky } else { 88335640Shselasky entry->lnl_next = lwp->lw_list; 89335640Shselasky lwp->lw_list = entry; 90335640Shselasky } 91335640Shselasky 92335640Shselasky return (B_FALSE); 93335640Shselasky} 94335640Shselasky 95335640Shselaskystatic int 96335640Shselaskypcap_activate_libdlpi(pcap_t *p) 97335640Shselasky{ 98335640Shselasky struct pcap_dlpi *pd = p->priv; 99335640Shselasky int status = 0; 100335640Shselasky int retv; 101335640Shselasky dlpi_handle_t dh; 102335640Shselasky dlpi_info_t dlinfo; 103335640Shselasky 104335640Shselasky /* 105335640Shselasky * Enable Solaris raw and passive DLPI extensions; 106335640Shselasky * dlpi_open() will not fail if the underlying link does not support 107335640Shselasky * passive mode. See dlpi(7P) for details. 108335640Shselasky */ 109335640Shselasky retv = dlpi_open(p->opt.device, &dh, DLPI_RAW|DLPI_PASSIVE); 110335640Shselasky if (retv != DLPI_SUCCESS) { 111335640Shselasky if (retv == DLPI_ELINKNAMEINVAL || retv == DLPI_ENOLINK) 112335640Shselasky status = PCAP_ERROR_NO_SUCH_DEVICE; 113335640Shselasky else if (retv == DL_SYSERR && 114335640Shselasky (errno == EPERM || errno == EACCES)) 115335640Shselasky status = PCAP_ERROR_PERM_DENIED; 116335640Shselasky else 117335640Shselasky status = PCAP_ERROR; 118335640Shselasky pcap_libdlpi_err(p->opt.device, "dlpi_open", retv, 119335640Shselasky p->errbuf); 120335640Shselasky return (status); 121335640Shselasky } 122335640Shselasky pd->dlpi_hd = dh; 123335640Shselasky 124335640Shselasky if (p->opt.rfmon) { 125335640Shselasky /* 126335640Shselasky * This device exists, but we don't support monitor mode 127335640Shselasky * any platforms that support DLPI. 128335640Shselasky */ 129335640Shselasky status = PCAP_ERROR_RFMON_NOTSUP; 130335640Shselasky goto bad; 131335640Shselasky } 132335640Shselasky 133335640Shselasky /* Bind with DLPI_ANY_SAP. */ 134335640Shselasky if ((retv = dlpi_bind(pd->dlpi_hd, DLPI_ANY_SAP, 0)) != DLPI_SUCCESS) { 135335640Shselasky status = PCAP_ERROR; 136335640Shselasky pcap_libdlpi_err(p->opt.device, "dlpi_bind", retv, p->errbuf); 137335640Shselasky goto bad; 138335640Shselasky } 139335640Shselasky 140335640Shselasky /* 141335640Shselasky * Turn a negative snapshot value (invalid), a snapshot value of 142335640Shselasky * 0 (unspecified), or a value bigger than the normal maximum 143335640Shselasky * value, into the maximum allowed value. 144335640Shselasky * 145335640Shselasky * If some application really *needs* a bigger snapshot 146335640Shselasky * length, we should just increase MAXIMUM_SNAPLEN. 147335640Shselasky */ 148335640Shselasky if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN) 149335640Shselasky p->snapshot = MAXIMUM_SNAPLEN; 150335640Shselasky 151335640Shselasky /* Enable promiscuous mode. */ 152335640Shselasky if (p->opt.promisc) { 153335640Shselasky retv = dlpromiscon(p, DL_PROMISC_PHYS); 154335640Shselasky if (retv < 0) { 155335640Shselasky /* 156335640Shselasky * "You don't have permission to capture on 157335640Shselasky * this device" and "you don't have permission 158335640Shselasky * to capture in promiscuous mode on this 159335640Shselasky * device" are different; let the user know, 160335640Shselasky * so if they can't get permission to 161335640Shselasky * capture in promiscuous mode, they can at 162335640Shselasky * least try to capture in non-promiscuous 163335640Shselasky * mode. 164335640Shselasky * 165335640Shselasky * XXX - you might have to capture in 166335640Shselasky * promiscuous mode to see outgoing packets. 167335640Shselasky */ 168335640Shselasky if (retv == PCAP_ERROR_PERM_DENIED) 169335640Shselasky status = PCAP_ERROR_PROMISC_PERM_DENIED; 170335640Shselasky else 171335640Shselasky status = retv; 172335640Shselasky goto bad; 173335640Shselasky } 174335640Shselasky } else { 175335640Shselasky /* Try to enable multicast. */ 176335640Shselasky retv = dlpromiscon(p, DL_PROMISC_MULTI); 177335640Shselasky if (retv < 0) { 178335640Shselasky status = retv; 179335640Shselasky goto bad; 180335640Shselasky } 181335640Shselasky } 182335640Shselasky 183335640Shselasky /* Try to enable SAP promiscuity. */ 184335640Shselasky retv = dlpromiscon(p, DL_PROMISC_SAP); 185335640Shselasky if (retv < 0) { 186335640Shselasky /* 187335640Shselasky * Not fatal, since the DL_PROMISC_PHYS mode worked. 188335640Shselasky * Report it as a warning, however. 189335640Shselasky */ 190335640Shselasky if (p->opt.promisc) 191335640Shselasky status = PCAP_WARNING; 192335640Shselasky else { 193335640Shselasky status = retv; 194335640Shselasky goto bad; 195335640Shselasky } 196335640Shselasky } 197335640Shselasky 198335640Shselasky /* Determine link type. */ 199335640Shselasky if ((retv = dlpi_info(pd->dlpi_hd, &dlinfo, 0)) != DLPI_SUCCESS) { 200335640Shselasky status = PCAP_ERROR; 201335640Shselasky pcap_libdlpi_err(p->opt.device, "dlpi_info", retv, p->errbuf); 202335640Shselasky goto bad; 203335640Shselasky } 204335640Shselasky 205335640Shselasky if (pcap_process_mactype(p, dlinfo.di_mactype) != 0) { 206335640Shselasky status = PCAP_ERROR; 207335640Shselasky goto bad; 208335640Shselasky } 209335640Shselasky 210335640Shselasky p->fd = dlpi_fd(pd->dlpi_hd); 211335640Shselasky 212335640Shselasky /* Push and configure bufmod. */ 213335640Shselasky if (pcap_conf_bufmod(p, p->snapshot) != 0) { 214335640Shselasky status = PCAP_ERROR; 215335640Shselasky goto bad; 216335640Shselasky } 217335640Shselasky 218335640Shselasky /* 219335640Shselasky * Flush the read side. 220335640Shselasky */ 221335640Shselasky if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) { 222335640Shselasky status = PCAP_ERROR; 223335640Shselasky pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, 224335640Shselasky errno, "FLUSHR"); 225335640Shselasky goto bad; 226335640Shselasky } 227335640Shselasky 228335640Shselasky /* Allocate data buffer. */ 229335640Shselasky if (pcap_alloc_databuf(p) != 0) { 230335640Shselasky status = PCAP_ERROR; 231335640Shselasky goto bad; 232335640Shselasky } 233335640Shselasky 234335640Shselasky /* 235335640Shselasky * "p->fd" is a FD for a STREAMS device, so "select()" and 236335640Shselasky * "poll()" should work on it. 237335640Shselasky */ 238335640Shselasky p->selectable_fd = p->fd; 239335640Shselasky 240335640Shselasky p->read_op = pcap_read_libdlpi; 241335640Shselasky p->inject_op = pcap_inject_libdlpi; 242335640Shselasky p->setfilter_op = install_bpf_program; /* No kernel filtering */ 243335640Shselasky p->setdirection_op = NULL; /* Not implemented */ 244335640Shselasky p->set_datalink_op = NULL; /* Can't change data link type */ 245335640Shselasky p->getnonblock_op = pcap_getnonblock_fd; 246335640Shselasky p->setnonblock_op = pcap_setnonblock_fd; 247335640Shselasky p->stats_op = pcap_stats_dlpi; 248335640Shselasky p->cleanup_op = pcap_cleanup_libdlpi; 249335640Shselasky 250335640Shselasky return (status); 251335640Shselaskybad: 252335640Shselasky pcap_cleanup_libdlpi(p); 253335640Shselasky return (status); 254335640Shselasky} 255335640Shselasky 256335640Shselasky#define STRINGIFY(n) #n 257335640Shselasky 258335640Shselaskystatic int 259335640Shselaskydlpromiscon(pcap_t *p, bpf_u_int32 level) 260335640Shselasky{ 261335640Shselasky struct pcap_dlpi *pd = p->priv; 262335640Shselasky int retv; 263335640Shselasky int err; 264335640Shselasky 265335640Shselasky retv = dlpi_promiscon(pd->dlpi_hd, level); 266335640Shselasky if (retv != DLPI_SUCCESS) { 267335640Shselasky if (retv == DL_SYSERR && 268335640Shselasky (errno == EPERM || errno == EACCES)) 269335640Shselasky err = PCAP_ERROR_PERM_DENIED; 270335640Shselasky else 271335640Shselasky err = PCAP_ERROR; 272335640Shselasky pcap_libdlpi_err(p->opt.device, "dlpi_promiscon" STRINGIFY(level), 273335640Shselasky retv, p->errbuf); 274335640Shselasky return (err); 275335640Shselasky } 276335640Shselasky return (0); 277335640Shselasky} 278335640Shselasky 279335640Shselasky/* 280335640Shselasky * Presumably everything returned by dlpi_walk() is a DLPI device, 281335640Shselasky * so there's no work to be done here to check whether name refers 282335640Shselasky * to a DLPI device. 283335640Shselasky */ 284335640Shselaskystatic int 285335640Shselaskyis_dlpi_interface(const char *name _U_) 286335640Shselasky{ 287335640Shselasky return (1); 288335640Shselasky} 289335640Shselasky 290335640Shselaskystatic int 291335640Shselaskyget_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_) 292335640Shselasky{ 293335640Shselasky /* 294335640Shselasky * Nothing we can do other than mark loopback devices as "the 295335640Shselasky * connected/disconnected status doesn't apply". 296335640Shselasky * 297335640Shselasky * XXX - on Solaris, can we do what the dladm command does, 298335640Shselasky * i.e. get a connected/disconnected indication from a kstat? 299335640Shselasky * (Note that you can also get the link speed, and possibly 300335640Shselasky * other information, from a kstat as well.) 301335640Shselasky */ 302335640Shselasky if (*flags & PCAP_IF_LOOPBACK) { 303335640Shselasky /* 304335640Shselasky * Loopback devices aren't wireless, and "connected"/ 305335640Shselasky * "disconnected" doesn't apply to them. 306335640Shselasky */ 307335640Shselasky *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE; 308335640Shselasky return (0); 309335640Shselasky } 310335640Shselasky return (0); 311335640Shselasky} 312335640Shselasky 313335640Shselasky/* 314335640Shselasky * In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find 315335640Shselasky * network links that are plumbed and are up. dlpi_walk(3DLPI) will find 316335640Shselasky * additional network links present in the system. 317335640Shselasky */ 318335640Shselaskyint 319335640Shselaskypcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) 320335640Shselasky{ 321335640Shselasky int retv = 0; 322335640Shselasky 323335640Shselasky linknamelist_t *entry, *next; 324335640Shselasky linkwalk_t lw = {NULL, 0}; 325335640Shselasky int save_errno; 326335640Shselasky 327335640Shselasky /* 328335640Shselasky * Get the list of regular interfaces first. 329335640Shselasky */ 330335640Shselasky if (pcap_findalldevs_interfaces(devlistp, errbuf, 331335640Shselasky is_dlpi_interface, get_if_flags) == -1) 332335640Shselasky return (-1); /* failure */ 333335640Shselasky 334335640Shselasky /* dlpi_walk() for loopback will be added here. */ 335335640Shselasky 336335640Shselasky /* 337335640Shselasky * Find all DLPI devices in the current zone. 338335640Shselasky * 339335640Shselasky * XXX - will pcap_findalldevs_interfaces() find any devices 340335640Shselasky * outside the current zone? If not, the only reason to call 341335640Shselasky * it would be to get the interface addresses. 342335640Shselasky */ 343335640Shselasky dlpi_walk(list_interfaces, &lw, 0); 344335640Shselasky 345335640Shselasky if (lw.lw_err != 0) { 346335640Shselasky pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, 347335640Shselasky lw.lw_err, "dlpi_walk"); 348335640Shselasky retv = -1; 349335640Shselasky goto done; 350335640Shselasky } 351335640Shselasky 352335640Shselasky /* Add linkname if it does not exist on the list. */ 353335640Shselasky for (entry = lw.lw_list; entry != NULL; entry = entry->lnl_next) { 354335640Shselasky /* 355335640Shselasky * If it isn't already in the list of devices, try to 356335640Shselasky * add it. 357335640Shselasky */ 358335640Shselasky if (find_or_add_dev(devlistp, entry->linkname, 0, get_if_flags, 359335640Shselasky NULL, errbuf) == NULL) 360335640Shselasky retv = -1; 361335640Shselasky } 362335640Shselaskydone: 363335640Shselasky save_errno = errno; 364335640Shselasky for (entry = lw.lw_list; entry != NULL; entry = next) { 365335640Shselasky next = entry->lnl_next; 366335640Shselasky free(entry); 367335640Shselasky } 368335640Shselasky errno = save_errno; 369335640Shselasky 370335640Shselasky return (retv); 371335640Shselasky} 372335640Shselasky 373335640Shselasky/* 374335640Shselasky * Read data received on DLPI handle. Returns -2 if told to terminate, else 375335640Shselasky * returns the number of packets read. 376335640Shselasky */ 377335640Shselaskystatic int 378335640Shselaskypcap_read_libdlpi(pcap_t *p, int count, pcap_handler callback, u_char *user) 379335640Shselasky{ 380335640Shselasky struct pcap_dlpi *pd = p->priv; 381335640Shselasky int len; 382335640Shselasky u_char *bufp; 383335640Shselasky size_t msglen; 384335640Shselasky int retv; 385335640Shselasky 386335640Shselasky len = p->cc; 387335640Shselasky if (len != 0) { 388335640Shselasky bufp = p->bp; 389335640Shselasky goto process_pkts; 390335640Shselasky } 391335640Shselasky do { 392335640Shselasky /* Has "pcap_breakloop()" been called? */ 393335640Shselasky if (p->break_loop) { 394335640Shselasky /* 395335640Shselasky * Yes - clear the flag that indicates that it has, 396335640Shselasky * and return -2 to indicate that we were told to 397335640Shselasky * break out of the loop. 398335640Shselasky */ 399335640Shselasky p->break_loop = 0; 400335640Shselasky return (-2); 401335640Shselasky } 402335640Shselasky 403335640Shselasky msglen = p->bufsize; 404335640Shselasky bufp = (u_char *)p->buffer + p->offset; 405335640Shselasky 406335640Shselasky retv = dlpi_recv(pd->dlpi_hd, NULL, NULL, bufp, 407335640Shselasky &msglen, -1, NULL); 408335640Shselasky if (retv != DLPI_SUCCESS) { 409335640Shselasky /* 410335640Shselasky * This is most likely a call to terminate out of the 411335640Shselasky * loop. So, do not return an error message, instead 412335640Shselasky * check if "pcap_breakloop()" has been called above. 413335640Shselasky */ 414335640Shselasky if (retv == DL_SYSERR && errno == EINTR) { 415335640Shselasky len = 0; 416335640Shselasky continue; 417335640Shselasky } 418335640Shselasky pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd), 419335640Shselasky "dlpi_recv", retv, p->errbuf); 420335640Shselasky return (-1); 421335640Shselasky } 422335640Shselasky len = msglen; 423335640Shselasky } while (len == 0); 424335640Shselasky 425335640Shselaskyprocess_pkts: 426335640Shselasky return (pcap_process_pkts(p, callback, user, count, bufp, len)); 427335640Shselasky} 428335640Shselasky 429335640Shselaskystatic int 430335640Shselaskypcap_inject_libdlpi(pcap_t *p, const void *buf, size_t size) 431335640Shselasky{ 432335640Shselasky struct pcap_dlpi *pd = p->priv; 433335640Shselasky int retv; 434335640Shselasky 435335640Shselasky retv = dlpi_send(pd->dlpi_hd, NULL, 0, buf, size, NULL); 436335640Shselasky if (retv != DLPI_SUCCESS) { 437335640Shselasky pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd), "dlpi_send", retv, 438335640Shselasky p->errbuf); 439335640Shselasky return (-1); 440335640Shselasky } 441335640Shselasky /* 442335640Shselasky * dlpi_send(3DLPI) does not provide a way to return the number of 443335640Shselasky * bytes sent on the wire. Based on the fact that DLPI_SUCCESS was 444335640Shselasky * returned we are assuming 'size' bytes were sent. 445335640Shselasky */ 446335640Shselasky return (size); 447335640Shselasky} 448335640Shselasky 449335640Shselasky/* 450335640Shselasky * Close dlpi handle. 451335640Shselasky */ 452335640Shselaskystatic void 453335640Shselaskypcap_cleanup_libdlpi(pcap_t *p) 454335640Shselasky{ 455335640Shselasky struct pcap_dlpi *pd = p->priv; 456335640Shselasky 457335640Shselasky if (pd->dlpi_hd != NULL) { 458335640Shselasky dlpi_close(pd->dlpi_hd); 459335640Shselasky pd->dlpi_hd = NULL; 460335640Shselasky p->fd = -1; 461335640Shselasky } 462335640Shselasky pcap_cleanup_live_common(p); 463335640Shselasky} 464335640Shselasky 465335640Shselasky/* 466335640Shselasky * Write error message to buffer. 467335640Shselasky */ 468335640Shselaskystatic void 469335640Shselaskypcap_libdlpi_err(const char *linkname, const char *func, int err, char *errbuf) 470335640Shselasky{ 471335640Shselasky pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "libpcap: %s failed on %s: %s", 472335640Shselasky func, linkname, dlpi_strerror(err)); 473335640Shselasky} 474335640Shselasky 475335640Shselaskypcap_t * 476335640Shselaskypcap_create_interface(const char *device _U_, char *ebuf) 477335640Shselasky{ 478335640Shselasky pcap_t *p; 479335640Shselasky 480335640Shselasky p = pcap_create_common(ebuf, sizeof (struct pcap_dlpi)); 481335640Shselasky if (p == NULL) 482335640Shselasky return (NULL); 483335640Shselasky 484335640Shselasky p->activate_op = pcap_activate_libdlpi; 485335640Shselasky return (p); 486335640Shselasky} 487335640Shselasky 488335640Shselasky/* 489335640Shselasky * Libpcap version string. 490335640Shselasky */ 491335640Shselaskyconst char * 492335640Shselaskypcap_lib_version(void) 493335640Shselasky{ 494335640Shselasky return (PCAP_VERSION_STRING); 495335640Shselasky} 496