- Add usr.sbin/802_11 to hold wpa_supplicant and other 802.11 related
[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.1 2006/06/24 07:29:44 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 <stdlib.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <pcap.h>
39
40 #include "common.h"
41 #include "eloop.h"
42 #include "l2_packet.h"
43
44 struct l2_packet_data {
45         pcap_t *pcap;
46         char ifname[100];
47         u8 own_addr[ETH_ALEN];
48         void (*rx_callback)(void *ctx, const u8 *src_addr,
49                             const u8 *buf, size_t len);
50         void *rx_callback_ctx;
51         int l2_hdr; /* whether to include layer 2 (Ethernet) header data
52                      * buffers */
53 };
54
55 int
56 l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
57 {
58         memcpy(addr, l2->own_addr, ETH_ALEN);
59         return 0;
60 }
61
62 int
63 l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
64 {
65         pcap_if_t *devs, *dev;
66         struct pcap_addr *addr;
67         struct sockaddr_in *saddr;
68         int found = 0;
69         char err[PCAP_ERRBUF_SIZE + 1];
70
71         if (pcap_findalldevs(&devs, err) < 0) {
72                 wpa_printf(MSG_DEBUG, "pcap_findalldevs: %s\n", err);
73                 return -1;
74         }
75
76         for (dev = devs; dev && !found; dev = dev->next) {
77                 if (strcmp(dev->name, l2->ifname) != 0)
78                         continue;
79
80                 addr = dev->addresses;
81                 while (addr) {
82                         saddr = (struct sockaddr_in *) addr->addr;
83                         if (saddr && saddr->sin_family == AF_INET) {
84                                 snprintf(buf, len, "%s",
85                                          inet_ntoa(saddr->sin_addr));
86                                 found = 1;
87                                 break;
88                         }
89                         addr = addr->next;
90                 }
91         }
92
93         pcap_freealldevs(devs);
94
95         return found ? 0 : -1;
96 }
97
98 void
99 l2_packet_notify_auth_start(struct l2_packet_data *l2)
100 {
101 }
102
103 int
104 l2_packet_send(struct l2_packet_data *l2,
105         const u8 *dst_addr, u16 proto, const u8 *buf, size_t len)
106 {
107         if (!l2->l2_hdr) {
108                 int ret;
109                 struct l2_ethhdr *eth = malloc(sizeof(*eth) + len);
110                 if (eth == NULL)
111                         return -1;
112                 memcpy(eth->h_dest, dst_addr, ETH_ALEN);
113                 memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
114                 eth->h_proto = htons(proto);
115                 memcpy(eth + 1, buf, len);
116                 ret = pcap_inject(l2->pcap, (u8 *) eth, len + sizeof(*eth));
117                 free(eth);
118                 return ret;
119         } else
120                 return pcap_inject(l2->pcap, buf, len);
121 }
122
123
124 static void
125 l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
126 {
127         struct l2_packet_data *l2 = eloop_ctx;
128         pcap_t *pcap = sock_ctx;
129         struct pcap_pkthdr hdr;
130         const u_char *packet;
131         struct l2_ethhdr *ethhdr;
132         unsigned char *buf;
133         size_t len;
134
135         packet = pcap_next(pcap, &hdr);
136
137         if (packet == NULL || hdr.caplen < sizeof(*ethhdr))
138                 return;
139
140         ethhdr = (struct l2_ethhdr *) packet;
141         if (l2->l2_hdr) {
142                 buf = (unsigned char *) ethhdr;
143                 len = hdr.caplen;
144         } else {
145                 buf = (unsigned char *) (ethhdr + 1);
146                 len = hdr.caplen - sizeof(*ethhdr);
147         }
148         l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
149 }
150
151 static int
152 l2_packet_init_libpcap(struct l2_packet_data *l2, unsigned short protocol)
153 {
154         bpf_u_int32 pcap_maskp, pcap_netp;
155         char pcap_filter[100], pcap_err[PCAP_ERRBUF_SIZE];
156         struct bpf_program pcap_fp;
157
158         pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
159         l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
160         if (l2->pcap == NULL) {
161                 fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
162                 fprintf(stderr, "ifname='%s'\n", l2->ifname);
163                 return -1;
164         }
165         if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
166             pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
167                 fprintf(stderr, "pcap_set_datalinke(DLT_EN10MB): %s\n",
168                         pcap_geterr(l2->pcap));
169                 return -1;
170         }
171         snprintf(pcap_filter, sizeof(pcap_filter),
172                  "ether dst " MACSTR " and ether proto 0x%x",
173                  MAC2STR(l2->own_addr), protocol);
174         if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
175                 fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
176                 return -1;
177         }
178
179         if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
180                 fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
181                 return -1;
182         }
183
184         pcap_freecode(&pcap_fp);
185         /*
186          * When libpcap uses BPF we must enable "immediate mode" to
187          * receive frames right away; otherwise the system may
188          * buffer them for us.
189          */
190         { unsigned int on = 1;
191           if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
192                 fprintf(stderr, "%s: cannot enable immediate mode on "
193                         "interface %s: %s\n",
194                         __func__, l2->ifname, strerror(errno));
195                 /* XXX should we fail? */
196           }
197         }
198
199         eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
200                                  l2_packet_receive, l2, l2->pcap);
201
202         return 0;
203 }
204
205 static void
206 l2_packet_deinit_libpcap(struct l2_packet_data *l2)
207 {
208         if (l2->pcap != NULL) {
209                 eloop_unregister_read_sock(pcap_get_selectable_fd(l2->pcap));
210                 pcap_close(l2->pcap);
211                 l2->pcap = NULL;
212         }
213 }
214
215 static int
216 eth_get(const char *device, u8 ea[ETH_ALEN])
217 {
218         struct if_msghdr *ifm;
219         struct sockaddr_dl *sdl;
220         u_char *p, *buf;
221         size_t len;
222         int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };
223
224         if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
225                 return -1;
226         if ((buf = malloc(len)) == NULL)
227                 return -1;
228         if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
229                 free(buf);
230                 return -1;
231         }
232         for (p = buf; p < buf + len; p += ifm->ifm_msglen) {
233                 ifm = (struct if_msghdr *)p;
234                 sdl = (struct sockaddr_dl *)(ifm + 1);
235                 if (ifm->ifm_type != RTM_IFINFO ||
236                     (ifm->ifm_addrs & RTA_IFP) == 0)
237                         continue;
238                 if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
239                     memcmp(sdl->sdl_data, device, sdl->sdl_nlen) != 0)
240                         continue;
241                 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
242                 break;
243         }
244         free(buf);
245
246         if (p >= buf + len) {
247                 errno = ESRCH;
248                 return -1;
249         }
250         return 0;
251 }
252
253 struct l2_packet_data *
254 l2_packet_init(const char *ifname, const u8 *own_addr, unsigned short protocol,
255         void (*rx_callback)(void *ctx, const u8 *src_addr,
256                             const u8 *buf, size_t len),
257         void *rx_callback_ctx, int l2_hdr)
258 {
259         struct l2_packet_data *l2;
260
261         l2 = malloc(sizeof(struct l2_packet_data));
262         if (l2 == NULL)
263                 return NULL;
264         memset(l2, 0, sizeof(*l2));
265         strncpy(l2->ifname, ifname, sizeof(l2->ifname));
266         l2->rx_callback = rx_callback;
267         l2->rx_callback_ctx = rx_callback_ctx;
268         l2->l2_hdr = l2_hdr;
269
270         if (eth_get(l2->ifname, l2->own_addr) < 0) {
271                 fprintf(stderr, "Failed to get link-level address for "
272                         "interface '%s'.\n", l2->ifname);
273                 free(l2);
274                 return NULL;
275         }
276
277         if (l2_packet_init_libpcap(l2, protocol) != 0) {
278                 free(l2);
279                 return NULL;
280         }
281         return l2;
282 }
283
284 void
285 l2_packet_deinit(struct l2_packet_data *l2)
286 {
287         if (l2 != NULL) {
288                 l2_packet_deinit_libpcap(l2);
289                 free(l2);
290         }
291 }