2 * $Id: libnet_link_bpf.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
5 * libnet_bpf.c - low-level bpf routines
7 * Copyright (c) 1998 - 2001 Mike D. Schiffman <mike@infonexus.com>
10 * Copyright (c) 1993, 1994, 1995, 1996, 1998
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that: (1) source code distributions
15 * retain the above copyright notice and this paragraph in its entirety, (2)
16 * distributions including binary code include the above copyright notice and
17 * this paragraph in its entirety in the documentation or other materials
18 * provided with the distribution, and (3) all advertising materials mentioning
19 * features or use of this software display the following acknowledgement:
20 * ``This product includes software developed by the University of California,
21 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
22 * the University nor the names of its contributors may be used to endorse
23 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 #include <sys/param.h> /* optionally get BSD define */
31 #include <sys/timeb.h>
33 #include <sys/ioctl.h>
35 #include <sys/types.h>
40 #include "../include/config.h"
42 #include "../include/libnet.h"
43 #include <sys/sysctl.h>
44 #include <net/route.h>
45 #include <net/if_dl.h>
46 #include "../include/gnuc.h"
47 #include "../include/bpf.h"
49 #ifdef HAVE_OS_PROTO_H
50 #include "../include/os-proto.h"
54 libnet_bpf_open(char *errbuf)
57 char device[sizeof "/dev/bpf000"];
60 * Go through all the minors and find one that isn't in use.
64 sprintf(device, "/dev/bpf%d", i);
66 fd = open(device, O_RDWR);
67 if (fd == -1 && errno == EBUSY)
77 * Either we've got a valid file descriptor, or errno is not
78 * EBUSY meaning we've probably run out of devices.
86 sprintf(errbuf, "%s: %s", device, ll_strerror(errno));
92 struct libnet_link_int *
93 libnet_open_link_interface(char *device, char *ebuf)
96 struct bpf_version bv;
98 struct libnet_link_int *l;
100 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
101 u_int spoof_eth_src = 1;
104 l = (struct libnet_link_int *)malloc(sizeof(*l));
107 sprintf(ebuf, "malloc: %s", ll_strerror(errno));
109 libnet_error(LN_ERR_CRITICAL, "bpf libnet_open_link_interface: malloc %s",
115 memset(l, 0, sizeof(*l));
117 l->fd = libnet_bpf_open(ebuf);
126 if (ioctl(l->fd, BIOCVERSION, (caddr_t)&bv) < 0)
128 sprintf(ebuf, "BIOCVERSION: %s", ll_strerror(errno));
132 if (bv.bv_major != BPF_MAJOR_VERSION || bv.bv_minor < BPF_MINOR_VERSION)
134 sprintf(ebuf, "kernel bpf filter out of date");
139 * Attach network interface to bpf device.
141 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
142 if (ioctl(l->fd, BIOCSETIF, (caddr_t)&ifr) == -1)
144 sprintf(ebuf, "%s: %s", device, ll_strerror(errno));
149 * Get the data link layer type.
151 if (ioctl(l->fd, BIOCGDLT, (caddr_t)&v) == -1)
153 sprintf(ebuf, "BIOCGDLT: %s", ll_strerror(errno));
158 * NetBSD and FreeBSD BPF have an ioctl for enabling/disabling
159 * automatic filling of the link level source address.
161 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
162 if (ioctl(l->fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1)
164 sprintf(ebuf, "BIOCSHDRCMPLT: %s", ll_strerror(errno));
170 * Assign link type and offset.
175 l->linkoffset = 0x10;
181 l->linkoffset = 0x04;
185 l->linkoffset = 0xe; /* default to ethernet */
188 #if _BSDI_VERSION - 0 >= 199510
193 l->linkoffset = 0x10;
197 l->linkoffset = 0x04;
206 close(l->fd); /* this can fail ok */
213 libnet_close_link_interface(struct libnet_link_int *l)
215 if (close(l->fd) == 0)
227 libnet_write_link_layer(struct libnet_link_int *l, const char *device,
228 u_char *buf, int len)
232 c = write(l->fd, buf, len);
236 libnet_error(LN_ERR_WARNING,
237 "write_link_layer: %d bytes written (%s)\n", c,
246 libnet_get_hwaddr(struct libnet_link_int *l, const char *device, char *ebuf)
250 char *buf, *next, *end;
251 struct if_msghdr *ifm;
252 struct sockaddr_dl *sdl;
253 struct ether_addr *ea = NULL;
259 mib[4] = NET_RT_IFLIST;
262 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
267 if (!(buf = (char *)malloc(len)))
271 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0)
278 for (next = buf ; next < end ; next += ifm->ifm_msglen)
280 ifm = (struct if_msghdr *)next;
281 if (ifm->ifm_type == RTM_IFINFO)
283 sdl = (struct sockaddr_dl *)(ifm + 1);
284 if (strncmp(&sdl->sdl_data[0], device, sdl->sdl_nlen) == 0)
286 if (!(ea = malloc(sizeof(struct ether_addr))))
290 memcpy(ea->ether_addr_octet, LLADDR(sdl), ETHER_ADDR_LEN);