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