Remove empty DragonFly CVS IDs.
[dragonfly.git] / usr.sbin / 802_11 / l2_packet.c
blobabdc96fac3b0d0b6d058b0bc01f19d08ecc4ebc0
1 /*
2 * WPA Supplicant - Layer2 packet handling
3 * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright (c) 2005, Sam Leffler <sam@errno.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
11 * license.
13 * See README and COPYING for more details.
15 * $FreeBSD: head/usr.sbin/wpa/l2_packet.c 172682 2007-10-16 02:12:06Z mlaier $
19 * DragonFly-specific implementation.
21 #include <sys/types.h>
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
24 #include <sys/sysctl.h>
26 #include <net/bpf.h>
27 #include <net/if.h>
28 #include <net/if_dl.h>
29 #include <net/route.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <pcap.h>
39 #include "common.h"
40 #include "eloop.h"
41 #include "l2_packet.h"
43 static const u8 pae_group_addr[ETH_ALEN] =
44 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
46 struct l2_packet_data {
47 pcap_t *pcap;
48 char ifname[100];
49 u8 own_addr[ETH_ALEN];
50 void (*rx_callback)(void *ctx, const u8 *src_addr,
51 const u8 *buf, size_t len);
52 void *rx_callback_ctx;
53 int l2_hdr; /* whether to include layer 2 (Ethernet) header data
54 * buffers */
57 int
58 l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
60 memcpy(addr, l2->own_addr, ETH_ALEN);
61 return 0;
64 int
65 l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
67 pcap_if_t *devs, *dev;
68 struct pcap_addr *addr;
69 struct sockaddr_in *saddr;
70 int found = 0;
71 char err[PCAP_ERRBUF_SIZE + 1];
73 if (pcap_findalldevs(&devs, err) < 0) {
74 wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
75 return -1;
78 for (dev = devs; dev && !found; dev = dev->next) {
79 if (strcmp(dev->name, l2->ifname) != 0)
80 continue;
82 addr = dev->addresses;
83 while (addr) {
84 saddr = (struct sockaddr_in *) addr->addr;
85 if (saddr && saddr->sin_family == AF_INET) {
86 snprintf(buf, len, "%s",
87 inet_ntoa(saddr->sin_addr));
88 found = 1;
89 break;
91 addr = addr->next;
95 pcap_freealldevs(devs);
97 return found ? 0 : -1;
100 void
101 l2_packet_notify_auth_start(struct l2_packet_data *l2)
106 l2_packet_send(struct l2_packet_data *l2,
107 const u8 *dst_addr, u16 proto, const u8 *buf, size_t len)
109 if (!l2->l2_hdr) {
110 int ret;
111 struct l2_ethhdr *eth = malloc(sizeof(*eth) + len);
112 if (eth == NULL)
113 return -1;
114 memcpy(eth->h_dest, dst_addr, ETH_ALEN);
115 memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
116 eth->h_proto = htons(proto);
117 memcpy(eth + 1, buf, len);
118 ret = pcap_inject(l2->pcap, (u8 *) eth, len + sizeof(*eth));
119 free(eth);
120 return ret;
121 } else
122 return pcap_inject(l2->pcap, buf, len);
126 static void
127 l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
129 struct l2_packet_data *l2 = eloop_ctx;
130 pcap_t *pcap = sock_ctx;
131 struct pcap_pkthdr hdr;
132 const u_char *packet;
133 struct l2_ethhdr *ethhdr;
134 unsigned char *buf;
135 size_t len;
137 packet = pcap_next(pcap, &hdr);
139 if (packet == NULL || hdr.caplen < sizeof(*ethhdr))
140 return;
142 ethhdr = (struct l2_ethhdr *) packet;
143 if (l2->l2_hdr) {
144 buf = (unsigned char *) ethhdr;
145 len = hdr.caplen;
146 } else {
147 buf = (unsigned char *) (ethhdr + 1);
148 len = hdr.caplen - sizeof(*ethhdr);
150 l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
153 static int
154 l2_packet_init_libpcap(struct l2_packet_data *l2, unsigned short protocol)
156 bpf_u_int32 pcap_maskp, pcap_netp;
157 char pcap_filter[200], pcap_err[PCAP_ERRBUF_SIZE];
158 struct bpf_program pcap_fp;
160 pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
161 l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
162 if (l2->pcap == NULL) {
163 fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
164 fprintf(stderr, "ifname='%s'\n", l2->ifname);
165 return -1;
167 if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
168 pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
169 fprintf(stderr, "pcap_set_datalink(DLT_EN10MB): %s\n",
170 pcap_geterr(l2->pcap));
171 return -1;
173 snprintf(pcap_filter, sizeof(pcap_filter),
174 "not ether src " MACSTR " and "
175 "( ether dst " MACSTR " or ether dst " MACSTR " ) and "
176 "ether proto 0x%x",
177 MAC2STR(l2->own_addr), /* do not receive own packets */
178 MAC2STR(l2->own_addr), MAC2STR(pae_group_addr),
179 protocol);
180 if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
181 fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
182 return -1;
185 if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
186 fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
187 return -1;
190 pcap_freecode(&pcap_fp);
192 * When libpcap uses BPF we must enable "immediate mode" to
193 * receive frames right away; otherwise the system may
194 * buffer them for us.
196 { unsigned int on = 1;
197 if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
198 fprintf(stderr, "%s: cannot enable immediate mode on "
199 "interface %s: %s\n",
200 __func__, l2->ifname, strerror(errno));
201 /* XXX should we fail? */
205 eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
206 l2_packet_receive, l2, l2->pcap);
208 return 0;
211 static void
212 l2_packet_deinit_libpcap(struct l2_packet_data *l2)
214 if (l2->pcap != NULL) {
215 eloop_unregister_read_sock(pcap_get_selectable_fd(l2->pcap));
216 pcap_close(l2->pcap);
217 l2->pcap = NULL;
221 static int
222 eth_get(const char *device, u8 ea[ETH_ALEN])
224 struct if_msghdr *ifm;
225 struct sockaddr_dl *sdl;
226 u_char *p, *buf;
227 size_t len;
228 int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };
230 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
231 return -1;
232 if ((buf = malloc(len)) == NULL)
233 return -1;
234 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
235 free(buf);
236 return -1;
238 for (p = buf; p < buf + len; p += ifm->ifm_msglen) {
239 ifm = (struct if_msghdr *)p;
240 sdl = (struct sockaddr_dl *)(ifm + 1);
241 if (ifm->ifm_type != RTM_IFINFO ||
242 (ifm->ifm_addrs & RTA_IFP) == 0)
243 continue;
244 if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
245 memcmp(sdl->sdl_data, device, sdl->sdl_nlen) != 0)
246 continue;
247 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
248 break;
250 free(buf);
252 if (p >= buf + len) {
253 errno = ESRCH;
254 return -1;
256 return 0;
259 struct l2_packet_data *
260 l2_packet_init(const char *ifname, const u8 *own_addr, unsigned short protocol,
261 void (*rx_callback)(void *ctx, const u8 *src_addr,
262 const u8 *buf, size_t len),
263 void *rx_callback_ctx, int l2_hdr)
265 struct l2_packet_data *l2;
267 l2 = malloc(sizeof(struct l2_packet_data));
268 if (l2 == NULL)
269 return NULL;
270 memset(l2, 0, sizeof(*l2));
271 strncpy(l2->ifname, ifname, sizeof(l2->ifname));
272 l2->rx_callback = rx_callback;
273 l2->rx_callback_ctx = rx_callback_ctx;
274 l2->l2_hdr = l2_hdr;
276 if (eth_get(l2->ifname, l2->own_addr) < 0) {
277 fprintf(stderr, "Failed to get link-level address for "
278 "interface '%s'.\n", l2->ifname);
279 free(l2);
280 return NULL;
283 if (l2_packet_init_libpcap(l2, protocol) != 0) {
284 free(l2);
285 return NULL;
287 return l2;
290 void
291 l2_packet_deinit(struct l2_packet_data *l2)
293 if (l2 != NULL) {
294 l2_packet_deinit_libpcap(l2);
295 free(l2);