Create 'k' versions of the kernel malloc API.
[dragonfly.git] / usr.sbin / 802_11 / l2_packet.c
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>
5  *
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.
9  *
10  * Alternatively, this software may be distributed under the terms of BSD
11  * license.
12  *
13  * See README and COPYING for more details.
14  *
15  * $FreeBSD: src/usr.sbin/wpa/l2_packet.c,v 1.1.2.2 2006/03/24 01:43:17 sam Exp $
16  * $DragonFly: src/usr.sbin/802_11/l2_packet.c,v 1.2 2006/09/02 05:40:35 sephe Exp $
17  */
18
19 /*
20  * FreeBSD-specific implementation.
21  */
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
25 #include <sys/sysctl.h>
26 #include <sys/time.h>
27
28 #include <net/if.h>
29 #include <net/if_dl.h>
30 #include <net/route.h>
31 #include <net/bpf.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34
35 #include <fcntl.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <errno.h>
39 #include <pcap.h>
40
41 #include "common.h"
42 #include "eloop.h"
43 #include "l2_packet.h"
44
45 struct l2_packet_data {
46         pcap_t *pcap;
47         char ifname[100];
48         u8 own_addr[ETH_ALEN];
49         void (*rx_callback)(void *ctx, const u8 *src_addr,
50                             const u8 *buf, size_t len);
51         void *rx_callback_ctx;
52         int l2_hdr; /* whether to include layer 2 (Ethernet) header data
53                      * buffers */
54 };
55
56 /*
57  * The version of libpcap in FreeBSD 5.2.1 doesn't have these routines.
58  * Call me insane if you will, but I still run 5.2.1 on my laptop, and
59  * I'd like to use WPA there.
60  */
61
62 int
63 pcap_get_selectable_fd(pcap_t *p)
64 {
65         return(pcap_fileno(p));
66 }
67
68 /*
69  * The old version of libpcap opens its BPF descriptor in read-only
70  * mode. We need to temporarily create a new one we can write to.
71  */
72
73 int
74 pcap_inject(pcap_t *p, const void *buf, size_t len)
75 {
76         int                     fd;
77         int                     res, n = 0;
78         char                    device[sizeof "/dev/bpf0000000000"];
79         struct ifreq            ifr;
80
81         /*
82          * Go through all the minors and find one that isn't in use.
83          */
84         do {
85                 (void)snprintf(device, sizeof(device), "/dev/bpf%d", n++);
86                 fd = open(device, O_RDWR);
87         } while (fd < 0 && errno == EBUSY);
88
89         if (fd == -1)
90                 return(-1);
91
92         bzero((char *)&ifr, sizeof(ifr));
93         ioctl(pcap_fileno(p), BIOCGETIF, (caddr_t)&ifr);
94         ioctl(fd, BIOCSETIF, (caddr_t)&ifr);
95
96         res = write(fd, buf, len);
97
98         close(fd);
99
100         return(res);
101 }
102
103 int
104 l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
105 {
106         memcpy(addr, l2->own_addr, ETH_ALEN);
107         return 0;
108 }
109
110 int
111 l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
112 {
113         pcap_if_t *devs, *dev;
114         struct pcap_addr *addr;
115         struct sockaddr_in *saddr;
116         int found = 0;
117         char err[PCAP_ERRBUF_SIZE + 1];
118
119         if (pcap_findalldevs(&devs, err) < 0) {
120                 wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
121                 return -1;
122         }
123
124         for (dev = devs; dev && !found; dev = dev->next) {
125                 if (strcmp(dev->name, l2->ifname) != 0)
126                         continue;
127
128                 addr = dev->addresses;
129                 while (addr) {
130                         saddr = (struct sockaddr_in *) addr->addr;
131                         if (saddr && saddr->sin_family == AF_INET) {
132                                 snprintf(buf, len, "%s",
133                                          inet_ntoa(saddr->sin_addr));
134                                 found = 1;
135                                 break;
136                         }
137                         addr = addr->next;
138                 }
139         }
140
141         pcap_freealldevs(devs);
142
143         return found ? 0 : -1;
144 }
145
146 void
147 l2_packet_notify_auth_start(struct l2_packet_data *l2)
148 {
149 }
150
151 int
152 l2_packet_send(struct l2_packet_data *l2,
153         const u8 *dst_addr, u16 proto, const u8 *buf, size_t len)
154 {
155         if (!l2->l2_hdr) {
156                 int ret;
157                 struct l2_ethhdr *eth = malloc(sizeof(*eth) + len);
158                 if (eth == NULL)
159                         return -1;
160                 memcpy(eth->h_dest, dst_addr, ETH_ALEN);
161                 memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
162                 eth->h_proto = htons(proto);
163                 memcpy(eth + 1, buf, len);
164                 ret = pcap_inject(l2->pcap, (u8 *) eth, len + sizeof(*eth));
165                 free(eth);
166                 return ret;
167         } else
168                 return pcap_inject(l2->pcap, buf, len);
169 }
170
171
172 static void
173 l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
174 {
175         struct l2_packet_data *l2 = eloop_ctx;
176         pcap_t *pcap = sock_ctx;
177         struct pcap_pkthdr hdr;
178         const u_char *packet;
179         struct l2_ethhdr *ethhdr;
180         unsigned char *buf;
181         size_t len;
182
183         packet = pcap_next(pcap, &hdr);
184
185         if (packet == NULL || hdr.caplen < sizeof(*ethhdr))
186                 return;
187
188         ethhdr = (struct l2_ethhdr *) packet;
189         if (l2->l2_hdr) {
190                 buf = (unsigned char *) ethhdr;
191                 len = hdr.caplen;
192         } else {
193                 buf = (unsigned char *) (ethhdr + 1);
194                 len = hdr.caplen - sizeof(*ethhdr);
195         }
196         l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
197 }
198
199 static int
200 l2_packet_init_libpcap(struct l2_packet_data *l2, unsigned short protocol)
201 {
202         bpf_u_int32 pcap_maskp, pcap_netp;
203         char pcap_filter[100], pcap_err[PCAP_ERRBUF_SIZE];
204         struct bpf_program pcap_fp;
205
206         pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
207         l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
208         if (l2->pcap == NULL) {
209                 fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
210                 fprintf(stderr, "ifname='%s'\n", l2->ifname);
211                 return -1;
212         }
213         if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
214             pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
215                 fprintf(stderr, "pcap_set_datalinke(DLT_EN10MB): %s\n",
216                         pcap_geterr(l2->pcap));
217                 return -1;
218         }
219         snprintf(pcap_filter, sizeof(pcap_filter),
220                  "ether dst " MACSTR " and ether proto 0x%x",
221                  MAC2STR(l2->own_addr), protocol);
222         if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
223                 fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
224                 return -1;
225         }
226
227         if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
228                 fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
229                 return -1;
230         }
231
232         pcap_freecode(&pcap_fp);
233         /*
234          * When libpcap uses BPF we must enable "immediate mode" to
235          * receive frames right away; otherwise the system may
236          * buffer them for us.
237          */
238         { unsigned int on = 1;
239           if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
240                 fprintf(stderr, "%s: cannot enable immediate mode on "
241                         "interface %s: %s\n",
242                         __func__, l2->ifname, strerror(errno));
243                 /* XXX should we fail? */
244           }
245         }
246
247         eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
248                                  l2_packet_receive, l2, l2->pcap);
249
250         return 0;
251 }
252
253 static void
254 l2_packet_deinit_libpcap(struct l2_packet_data *l2)
255 {
256         if (l2->pcap != NULL) {
257                 eloop_unregister_read_sock(pcap_get_selectable_fd(l2->pcap));
258                 pcap_close(l2->pcap);
259                 l2->pcap = NULL;
260         }
261 }
262
263 static int
264 eth_get(const char *device, u8 ea[ETH_ALEN])
265 {
266         struct if_msghdr *ifm;
267         struct sockaddr_dl *sdl;
268         u_char *p, *buf;
269         size_t len;
270         int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };
271
272         if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
273                 return -1;
274         if ((buf = malloc(len)) == NULL)
275                 return -1;
276         if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
277                 free(buf);
278                 return -1;
279         }
280         for (p = buf; p < buf + len; p += ifm->ifm_msglen) {
281                 ifm = (struct if_msghdr *)p;
282                 sdl = (struct sockaddr_dl *)(ifm + 1);
283                 if (ifm->ifm_type != RTM_IFINFO ||
284                     (ifm->ifm_addrs & RTA_IFP) == 0)
285                         continue;
286                 if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
287                     memcmp(sdl->sdl_data, device, sdl->sdl_nlen) != 0)
288                         continue;
289                 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
290                 break;
291         }
292         free(buf);
293
294         if (p >= buf + len) {
295                 errno = ESRCH;
296                 return -1;
297         }
298         return 0;
299 }
300
301 struct l2_packet_data *
302 l2_packet_init(const char *ifname, const u8 *own_addr, unsigned short protocol,
303         void (*rx_callback)(void *ctx, const u8 *src_addr,
304                             const u8 *buf, size_t len),
305         void *rx_callback_ctx, int l2_hdr)
306 {
307         struct l2_packet_data *l2;
308
309         l2 = malloc(sizeof(struct l2_packet_data));
310         if (l2 == NULL)
311                 return NULL;
312         memset(l2, 0, sizeof(*l2));
313         strncpy(l2->ifname, ifname, sizeof(l2->ifname));
314         l2->rx_callback = rx_callback;
315         l2->rx_callback_ctx = rx_callback_ctx;
316         l2->l2_hdr = l2_hdr;
317
318         if (eth_get(l2->ifname, l2->own_addr) < 0) {
319                 fprintf(stderr, "Failed to get link-level address for "
320                         "interface '%s'.\n", l2->ifname);
321                 free(l2);
322                 return NULL;
323         }
324
325         if (l2_packet_init_libpcap(l2, protocol) != 0) {
326                 free(l2);
327                 return NULL;
328         }
329         return l2;
330 }
331
332 void
333 l2_packet_deinit(struct l2_packet_data *l2)
334 {
335         if (l2 != NULL) {
336                 l2_packet_deinit_libpcap(l2);
337                 free(l2);
338         }
339 }