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