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