00001
00017 #include <stdlib.h>
00018 #include <stdio.h>
00019 #include <string.h>
00020 #include <errno.h>
00021 #include <pcap.h>
00022
00023 #include <sys/types.h>
00024 #include <sys/ioctl.h>
00025 #include <sys/socket.h>
00026 #include <sys/sysctl.h>
00027
00028 #include <net/if.h>
00029 #include <net/if_dl.h>
00030 #include <net/route.h>
00031 #include <netinet/in.h>
00032 #include <arpa/inet.h>
00033
00034 #include "common.h"
00035 #include "eloop.h"
00036 #include "l2_packet.h"
00037
00038
00039 struct l2_packet_data {
00040 pcap_t *pcap;
00041 char ifname[100];
00042 u8 own_addr[ETH_ALEN];
00043 void (*rx_callback)(void *ctx, const u8 *src_addr,
00044 const u8 *buf, size_t len);
00045 void *rx_callback_ctx;
00046 int l2_hdr;
00047
00048 };
00049
00050
00051 int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
00052 {
00053 memcpy(addr, l2->own_addr, ETH_ALEN);
00054 return 0;
00055 }
00056
00057
00058 int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
00059 const u8 *buf, size_t len)
00060 {
00061 if (!l2->l2_hdr) {
00062 int ret;
00063 struct l2_ethhdr *eth = malloc(sizeof(*eth) + len);
00064 if (eth == NULL)
00065 return -1;
00066 memcpy(eth->h_dest, dst_addr, ETH_ALEN);
00067 memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
00068 eth->h_proto = htons(proto);
00069 memcpy(eth + 1, buf, len);
00070 ret = pcap_inject(l2->pcap, (u8 *) eth, len + sizeof(*eth));
00071 free(eth);
00072 return ret;
00073 } else
00074 return pcap_inject(l2->pcap, buf, len);
00075 }
00076
00077
00078 static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
00079 {
00080 struct l2_packet_data *l2 = eloop_ctx;
00081 pcap_t *pcap = sock_ctx;
00082 struct pcap_pkthdr hdr;
00083 const u_char *packet;
00084 struct l2_ethhdr *ethhdr;
00085 unsigned char *buf;
00086 size_t len;
00087
00088 packet = pcap_next(pcap, &hdr);
00089
00090 if (packet == NULL || hdr.caplen < sizeof(*ethhdr))
00091 return;
00092
00093 ethhdr = (struct l2_ethhdr *) packet;
00094 if (l2->l2_hdr) {
00095 buf = (unsigned char *) ethhdr;
00096 len = hdr.caplen;
00097 } else {
00098 buf = (unsigned char *) (ethhdr + 1);
00099 len = hdr.caplen - sizeof(*ethhdr);
00100 }
00101 l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
00102 }
00103
00104
00105 static int l2_packet_init_libpcap(struct l2_packet_data *l2,
00106 unsigned short protocol)
00107 {
00108 bpf_u_int32 pcap_maskp, pcap_netp;
00109 char pcap_filter[100], pcap_err[PCAP_ERRBUF_SIZE];
00110 struct bpf_program pcap_fp;
00111
00112 pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
00113 l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
00114 if (l2->pcap == NULL) {
00115 fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
00116 fprintf(stderr, "ifname='%s'\n", l2->ifname);
00117 return -1;
00118 }
00119 if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
00120 pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
00121 fprintf(stderr, "pcap_set_datalinke(DLT_EN10MB): %s\n",
00122 pcap_geterr(l2->pcap));
00123 return -1;
00124 }
00125 snprintf(pcap_filter, sizeof(pcap_filter),
00126 "ether dst " MACSTR " and ether proto 0x%x",
00127 MAC2STR(l2->own_addr), protocol);
00128 if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
00129 fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
00130 return -1;
00131 }
00132
00133 if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
00134 fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
00135 return -1;
00136 }
00137
00138 pcap_freecode(&pcap_fp);
00139
00140
00141
00142
00143
00144 {
00145 unsigned int on = 1;
00146 if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
00147 fprintf(stderr, "%s: cannot enable immediate mode on "
00148 "interface %s: %s\n",
00149 __func__, l2->ifname, strerror(errno));
00150
00151 }
00152 }
00153
00154 eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
00155 l2_packet_receive, l2, l2->pcap);
00156
00157 return 0;
00158 }
00159
00160
00161 static int eth_get(const char *device, u8 ea[ETH_ALEN])
00162 {
00163 struct if_msghdr *ifm;
00164 struct sockaddr_dl *sdl;
00165 u_char *p, *buf;
00166 size_t len;
00167 int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };
00168
00169 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
00170 return -1;
00171 if ((buf = malloc(len)) == NULL)
00172 return -1;
00173 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
00174 free(buf);
00175 return -1;
00176 }
00177 for (p = buf; p < buf + len; p += ifm->ifm_msglen) {
00178 ifm = (struct if_msghdr *)p;
00179 sdl = (struct sockaddr_dl *)(ifm + 1);
00180 if (ifm->ifm_type != RTM_IFINFO ||
00181 (ifm->ifm_addrs & RTA_IFP) == 0)
00182 continue;
00183 if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
00184 memcmp(sdl->sdl_data, device, sdl->sdl_nlen) != 0)
00185 continue;
00186 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
00187 break;
00188 }
00189 free(buf);
00190
00191 if (p >= buf + len) {
00192 errno = ESRCH;
00193 return -1;
00194 }
00195 return 0;
00196 }
00197
00198
00199 struct l2_packet_data * l2_packet_init(
00200 const char *ifname, const u8 *own_addr, unsigned short protocol,
00201 void (*rx_callback)(void *ctx, const u8 *src_addr,
00202 const u8 *buf, size_t len),
00203 void *rx_callback_ctx, int l2_hdr)
00204 {
00205 struct l2_packet_data *l2;
00206
00207 l2 = malloc(sizeof(struct l2_packet_data));
00208 if (l2 == NULL)
00209 return NULL;
00210 memset(l2, 0, sizeof(*l2));
00211 strncpy(l2->ifname, ifname, sizeof(l2->ifname));
00212 l2->rx_callback = rx_callback;
00213 l2->rx_callback_ctx = rx_callback_ctx;
00214 l2->l2_hdr = l2_hdr;
00215
00216 if (eth_get(l2->ifname, l2->own_addr) < 0) {
00217 fprintf(stderr, "Failed to get link-level address for "
00218 "interface '%s'.\n", l2->ifname);
00219 free(l2);
00220 return NULL;
00221 }
00222
00223 if (l2_packet_init_libpcap(l2, protocol)) {
00224 free(l2);
00225 return NULL;
00226 }
00227
00228 return l2;
00229 }
00230
00231
00232 void l2_packet_deinit(struct l2_packet_data *l2)
00233 {
00234 if (l2 != NULL) {
00235 if (l2->pcap)
00236 pcap_close(l2->pcap);
00237 free(l2);
00238 }
00239 }
00240
00241
00242 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
00243 {
00244 pcap_if_t *devs, *dev;
00245 struct pcap_addr *addr;
00246 struct sockaddr_in *saddr;
00247 int found = 0;
00248 char err[PCAP_ERRBUF_SIZE + 1];
00249
00250 if (pcap_findalldevs(&devs, err) < 0) {
00251 wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
00252 return -1;
00253 }
00254
00255 for (dev = devs; dev && !found; dev = dev->next) {
00256 if (strcmp(dev->name, l2->ifname) != 0)
00257 continue;
00258
00259 addr = dev->addresses;
00260 while (addr) {
00261 saddr = (struct sockaddr_in *) addr->addr;
00262 if (saddr && saddr->sin_family == AF_INET) {
00263 snprintf(buf, len, "%s",
00264 inet_ntoa(saddr->sin_addr));
00265 found = 1;
00266 break;
00267 }
00268 addr = addr->next;
00269 }
00270 }
00271
00272 pcap_freealldevs(devs);
00273
00274 return found ? 0 : -1;
00275 }
00276
00277
00278 void l2_packet_notify_auth_start(struct l2_packet_data *l2)
00279 {
00280 }
00281