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