1189136Sdas/* 2189136Sdas * Copyright (c) 1993, 1994, 1995, 1996, 1997 3189136Sdas * The Regents of the University of California. All rights reserved. 4189136Sdas * 5189136Sdas * Redistribution and use in source and binary forms, with or without 6189136Sdas * modification, are permitted provided that: (1) source code distributions 7189136Sdas * retain the above copyright notice and this paragraph in its entirety, (2) 8189136Sdas * distributions including binary code include the above copyright notice and 9189136Sdas * this paragraph in its entirety in the documentation or other materials 10189136Sdas * provided with the distribution, and (3) all advertising materials mentioning 11189136Sdas * features or use of this software display the following acknowledgement: 12189136Sdas * ``This product includes software developed by the University of California, 13189136Sdas * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14189136Sdas * the University nor the names of its contributors may be used to endorse 15189136Sdas * or promote products derived from this software without specific prior 16189136Sdas * written permission. 17189136Sdas * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18189136Sdas * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19189136Sdas * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20189136Sdas * 21189136Sdas * This code contributed by Sagun Shakya (sagun.shakya@sun.com) 22189136Sdas */ 23189136Sdas/* 24189136Sdas * Packet capture routines for DLPI using libdlpi under SunOS 5.11. 25189136Sdas */ 26189136Sdas 27189136Sdas#ifndef lint 28189136Sdasstatic const char rcsid[] _U_ = 29189136Sdas "@(#) $Header: /tcpdump/master/libpcap/pcap-libdlpi.c,v 1.6 2008-04-14 20:40:58 guy Exp $ (LBL)"; 30189136Sdas#endif 31189136Sdas 32189136Sdas#ifdef HAVE_CONFIG_H 33189136Sdas#include "config.h" 34189136Sdas#endif 35189136Sdas 36189136Sdas#include <sys/types.h> 37189136Sdas#include <sys/time.h> 38189136Sdas#include <sys/bufmod.h> 39189136Sdas#include <sys/stream.h> 40189136Sdas#include <libdlpi.h> 41189136Sdas#include <errno.h> 42189136Sdas#include <memory.h> 43189136Sdas#include <stropts.h> 44189136Sdas#include <stdio.h> 45189136Sdas#include <stdlib.h> 46189136Sdas#include <string.h> 47189136Sdas 48189136Sdas#include "pcap-int.h" 49189136Sdas#include "dlpisubs.h" 50189136Sdas 51189136Sdas/* Forwards. */ 52189136Sdasstatic int dlpromiscon(pcap_t *, bpf_u_int32); 53189136Sdasstatic int pcap_read_libdlpi(pcap_t *, int, pcap_handler, u_char *); 54189136Sdasstatic int pcap_inject_libdlpi(pcap_t *, const void *, size_t); 55189136Sdasstatic void pcap_close_libdlpi(pcap_t *); 56189136Sdasstatic void pcap_libdlpi_err(const char *, const char *, int, char *); 57189136Sdasstatic void pcap_cleanup_libdlpi(pcap_t *); 58189136Sdas 59189136Sdas/* 60189136Sdas * list_interfaces() will list all the network links that are 61189136Sdas * available on a system. 62189136Sdas */ 63189136Sdasstatic boolean_t list_interfaces(const char *, void *); 64189136Sdas 65189136Sdastypedef struct linknamelist { 66189136Sdas char linkname[DLPI_LINKNAME_MAX]; 67189136Sdas struct linknamelist *lnl_next; 68189136Sdas} linknamelist_t; 69189136Sdas 70189136Sdastypedef struct linkwalk { 71189136Sdas linknamelist_t *lw_list; 72189136Sdas int lw_err; 73189136Sdas} linkwalk_t; 74189136Sdas 75189136Sdas/* 76189136Sdas * The caller of this function should free the memory allocated 77189136Sdas * for each linknamelist_t "entry" allocated. 78189136Sdas */ 79189136Sdasstatic boolean_t 80189136Sdaslist_interfaces(const char *linkname, void *arg) 81189136Sdas{ 82189136Sdas linkwalk_t *lwp = arg; 83189136Sdas linknamelist_t *entry; 84189136Sdas 85189136Sdas if ((entry = calloc(1, sizeof(linknamelist_t))) == NULL) { 86189136Sdas lwp->lw_err = ENOMEM; 87189136Sdas return (B_TRUE); 88189136Sdas } 89189136Sdas (void) strlcpy(entry->linkname, linkname, DLPI_LINKNAME_MAX); 90189136Sdas 91189136Sdas if (lwp->lw_list == NULL) { 92189136Sdas lwp->lw_list = entry; 93189136Sdas } else { 94189136Sdas entry->lnl_next = lwp->lw_list; 95189136Sdas lwp->lw_list = entry; 96189136Sdas } 97189136Sdas 98189136Sdas return (B_FALSE); 99189136Sdas} 100189136Sdas 101189136Sdasstatic int 102189136Sdaspcap_activate_libdlpi(pcap_t *p) 103189136Sdas{ 104189136Sdas int retv; 105189136Sdas dlpi_handle_t dh; 106189136Sdas dlpi_info_t dlinfo; 107189136Sdas int err = PCAP_ERROR; 108189136Sdas 109189136Sdas /* 110189136Sdas * Enable Solaris raw and passive DLPI extensions; 111189136Sdas * dlpi_open() will not fail if the underlying link does not support 112189136Sdas * passive mode. See dlpi(7P) for details. 113189136Sdas */ 114189136Sdas retv = dlpi_open(p->opt.source, &dh, DLPI_RAW|DLPI_PASSIVE); 115321074Skib if (retv != DLPI_SUCCESS) { 116189136Sdas if (retv == DLPI_ELINKNAMEINVAL || retv == DLPI_ENOLINK) 117189136Sdas err = PCAP_ERROR_NO_SUCH_DEVICE; 118189136Sdas else if (retv == DL_SYSERR && 119189136Sdas (errno == EPERM || errno == EACCES)) 120189136Sdas err = PCAP_ERROR_PERM_DENIED; 121189136Sdas pcap_libdlpi_err(p->opt.source, "dlpi_open", retv, 122189136Sdas p->errbuf); 123197752Sdas return (err); 124197752Sdas } 125189136Sdas p->dlpi_hd = dh; 126189136Sdas 127189136Sdas if (p->opt.rfmon) { 128304888Sache /* 129189136Sdas * This device exists, but we don't support monitor mode 130190773Sdas * any platforms that support DLPI. 131321074Skib */ 132321074Skib err = PCAP_ERROR_RFMON_NOTSUP; 133189136Sdas goto bad; 134189136Sdas } 135190773Sdas 136189136Sdas /* Bind with DLPI_ANY_SAP. */ 137189136Sdas if ((retv = dlpi_bind(p->dlpi_hd, DLPI_ANY_SAP, 0)) != DLPI_SUCCESS) { 138189136Sdas pcap_libdlpi_err(p->opt.source, "dlpi_bind", retv, p->errbuf); 139189136Sdas goto bad; 140304888Sache } 141189136Sdas 142189136Sdas /* Enable promiscuous mode. */ 143189136Sdas if (p->opt.promisc) { 144189136Sdas err = dlpromiscon(p, DL_PROMISC_PHYS); 145189136Sdas if (err < 0) { 146189136Sdas /* 147189136Sdas * "You don't have permission to capture on 148189136Sdas * this device" and "you don't have permission 149189136Sdas * to capture in promiscuous mode on this 150189136Sdas * device" are different; let the user know, 151189136Sdas * so if they can't get permission to 152189136Sdas * capture in promiscuous mode, they can at 153321074Skib * least try to capture in non-promiscuous 154321074Skib * mode. 155189136Sdas * 156189136Sdas * XXX - you might have to capture in 157189136Sdas * promiscuous mode to see outgoing packets. 158189136Sdas */ 159321074Skib if (err == PCAP_ERROR_PERM_DENIED) 160321074Skib err = PCAP_ERROR_PROMISC_PERM_DENIED; 161189136Sdas goto bad; 162 } 163 } else { 164 /* Try to enable multicast. */ 165 err = dlpromiscon(p, DL_PROMISC_MULTI); 166 if (err < 0) 167 goto bad; 168 } 169 170 /* Try to enable SAP promiscuity. */ 171 err = dlpromiscon(p, DL_PROMISC_SAP); 172 if (err < 0) { 173 /* 174 * Not fatal, since the DL_PROMISC_PHYS mode worked. 175 * Report it as a warning, however. 176 */ 177 if (p->opt.promisc) 178 err = PCAP_WARNING; 179 else 180 goto bad; 181 } 182 183 /* Determine link type. */ 184 if ((retv = dlpi_info(p->dlpi_hd, &dlinfo, 0)) != DLPI_SUCCESS) { 185 pcap_libdlpi_err(p->opt.source, "dlpi_info", retv, p->errbuf); 186 goto bad; 187 } 188 189 if (pcap_process_mactype(p, dlinfo.di_mactype) != 0) 190 goto bad; 191 192 p->fd = dlpi_fd(p->dlpi_hd); 193 194 /* Push and configure bufmod. */ 195 if (pcap_conf_bufmod(p, p->snapshot, p->md.timeout) != 0) 196 goto bad; 197 198 /* 199 * Flush the read side. 200 */ 201 if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) { 202 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s", 203 pcap_strerror(errno)); 204 goto bad; 205 } 206 207 /* Allocate data buffer. */ 208 if (pcap_alloc_databuf(p) != 0) 209 goto bad; 210 211 /* 212 * "p->fd" is a FD for a STREAMS device, so "select()" and 213 * "poll()" should work on it. 214 */ 215 p->selectable_fd = p->fd; 216 217 p->read_op = pcap_read_libdlpi; 218 p->inject_op = pcap_inject_libdlpi; 219 p->setfilter_op = install_bpf_program; /* No kernel filtering */ 220 p->setdirection_op = NULL; /* Not implemented */ 221 p->set_datalink_op = NULL; /* Can't change data link type */ 222 p->getnonblock_op = pcap_getnonblock_fd; 223 p->setnonblock_op = pcap_setnonblock_fd; 224 p->stats_op = pcap_stats_dlpi; 225 p->cleanup_op = pcap_cleanup_libdlpi; 226 227 return (0); 228bad: 229 pcap_cleanup_libdlpi(p); 230 return (err); 231} 232 233#define STRINGIFY(n) #n 234 235static int 236dlpromiscon(pcap_t *p, bpf_u_int32 level) 237{ 238 int err; 239 240 retv = dlpi_promiscon(p->dlpi_hd, level); 241 if (retv != DLPI_SUCCESS) { 242 if (retv == DL_SYSERR && 243 (errno == EPERM || errno == EACCES)) 244 err = PCAP_ERROR_PERM_DENIED; 245 else 246 err = PCAP_ERROR; 247 pcap_libdlpi_err(p->opt.source, "dlpi_promiscon" STRINGIFY(level), 248 retv, p->errbuf); 249 return (err); 250 } 251 return (0); 252} 253 254/* 255 * In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find 256 * network links that are plumbed and are up. dlpi_walk(3DLPI) will find 257 * additional network links present in the system. 258 */ 259int 260pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 261{ 262 int retv = 0; 263 264 linknamelist_t *entry, *next; 265 linkwalk_t lw = {NULL, 0}; 266 int save_errno; 267 268 /* dlpi_walk() for loopback will be added here. */ 269 270 dlpi_walk(list_interfaces, &lw, 0); 271 272 if (lw.lw_err != 0) { 273 snprintf(errbuf, PCAP_ERRBUF_SIZE, 274 "dlpi_walk: %s", pcap_strerror(lw.lw_err)); 275 retv = -1; 276 goto done; 277 } 278 279 /* Add linkname if it does not exist on the list. */ 280 for (entry = lw.lw_list; entry != NULL; entry = entry->lnl_next) { 281 if (pcap_add_if(alldevsp, entry->linkname, 0, NULL, errbuf) < 0) 282 retv = -1; 283 } 284done: 285 save_errno = errno; 286 for (entry = lw.lw_list; entry != NULL; entry = next) { 287 next = entry->lnl_next; 288 free(entry); 289 } 290 errno = save_errno; 291 292 return (retv); 293} 294 295/* 296 * Read data received on DLPI handle. Returns -2 if told to terminate, else 297 * returns the number of packets read. 298 */ 299static int 300pcap_read_libdlpi(pcap_t *p, int count, pcap_handler callback, u_char *user) 301{ 302 int len; 303 u_char *bufp; 304 size_t msglen; 305 int retv; 306 307 len = p->cc; 308 if (len != 0) { 309 bufp = p->bp; 310 goto process_pkts; 311 } 312 do { 313 /* Has "pcap_breakloop()" been called? */ 314 if (p->break_loop) { 315 /* 316 * Yes - clear the flag that indicates that it has, 317 * and return -2 to indicate that we were told to 318 * break out of the loop. 319 */ 320 p->break_loop = 0; 321 return (-2); 322 } 323 324 msglen = p->bufsize; 325 bufp = p->buffer + p->offset; 326 327 retv = dlpi_recv(p->dlpi_hd, NULL, NULL, bufp, 328 &msglen, -1, NULL); 329 if (retv != DLPI_SUCCESS) { 330 /* 331 * This is most likely a call to terminate out of the 332 * loop. So, do not return an error message, instead 333 * check if "pcap_breakloop()" has been called above. 334 */ 335 if (retv == DL_SYSERR && errno == EINTR) { 336 len = 0; 337 continue; 338 } 339 pcap_libdlpi_err(dlpi_linkname(p->dlpi_hd), 340 "dlpi_recv", retv, p->errbuf); 341 return (-1); 342 } 343 len = msglen; 344 } while (len == 0); 345 346process_pkts: 347 return (pcap_process_pkts(p, callback, user, count, bufp, len)); 348} 349 350static int 351pcap_inject_libdlpi(pcap_t *p, const void *buf, size_t size) 352{ 353 int retv; 354 355 retv = dlpi_send(p->dlpi_hd, NULL, 0, buf, size, NULL); 356 if (retv != DLPI_SUCCESS) { 357 pcap_libdlpi_err(dlpi_linkname(p->dlpi_hd), "dlpi_send", retv, 358 p->errbuf); 359 return (-1); 360 } 361 /* 362 * dlpi_send(3DLPI) does not provide a way to return the number of 363 * bytes sent on the wire. Based on the fact that DLPI_SUCCESS was 364 * returned we are assuming 'size' bytes were sent. 365 */ 366 return (size); 367} 368 369/* 370 * Close dlpi handle. 371 */ 372static void 373pcap_cleanup_libdlpi(pcap_t *p) 374{ 375 if (p->dlpi_hd != NULL) { 376 dlpi_close(p->dlpi_hd); 377 p->dlpi_hd = NULL; 378 p->fd = -1; 379 } 380 pcap_cleanup_live_common(p); 381} 382 383/* 384 * Write error message to buffer. 385 */ 386static void 387pcap_libdlpi_err(const char *linkname, const char *func, int err, char *errbuf) 388{ 389 snprintf(errbuf, PCAP_ERRBUF_SIZE, "libpcap: %s failed on %s: %s", 390 func, linkname, dlpi_strerror(err)); 391} 392 393pcap_t * 394pcap_create_interface(const char *device, char *ebuf) 395{ 396 pcap_t *p; 397 398 p = pcap_create_common(device, ebuf); 399 if (p == NULL) 400 return (NULL); 401 402 p->activate_op = pcap_activate_libdlpi; 403 return (p); 404} 405