]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/wpa/l2_packet.c
This commit was generated by cvs2svn to compensate for changes in r152069,
[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
35 #include "common.h"
36 #include "eloop.h"
37 #include "l2_packet.h"
38
39 struct l2_packet_data {
40         pcap_t *pcap;
41         char ifname[100];
42         u8 own_addr[ETH_ALEN];
43         void (*rx_callback)(void *ctx, unsigned char *src_addr,
44                             unsigned char *buf, size_t len);
45         void *rx_callback_ctx;
46         int rx_l2_hdr; /* whether to include layer 2 (Ethernet) header in calls
47                         * to rx_callback */
48 };
49
50 int
51 l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
52 {
53         memcpy(addr, l2->own_addr, ETH_ALEN);
54         return 0;
55 }
56
57 void
58 l2_packet_set_rx_l2_hdr(struct l2_packet_data *l2, int rx_l2_hdr)
59 {
60         l2->rx_l2_hdr = rx_l2_hdr;
61 }
62
63 int
64 l2_packet_send(struct l2_packet_data *l2, u8 *buf, size_t len)
65 {
66         return pcap_inject(l2->pcap, buf, len);
67 }
68
69
70 static void
71 l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
72 {
73         struct l2_packet_data *l2 = eloop_ctx;
74         pcap_t *pcap = sock_ctx;
75         struct pcap_pkthdr hdr;
76         const u_char *packet;
77         struct l2_ethhdr *ethhdr;
78         unsigned char *buf;
79         size_t len;
80
81         packet = pcap_next(pcap, &hdr);
82
83         if (packet == NULL || hdr.caplen < sizeof(*ethhdr))
84                 return;
85
86         ethhdr = (struct l2_ethhdr *) packet;
87         if (l2->rx_l2_hdr) {
88                 buf = (unsigned char *) ethhdr;
89                 len = hdr.caplen;
90         } else {
91                 buf = (unsigned char *) (ethhdr + 1);
92                 len = hdr.caplen - sizeof(*ethhdr);
93         }
94         l2->rx_callback(l2->rx_callback_ctx, ethhdr->h_source, buf, len);
95 }
96
97 static int
98 l2_packet_init_libpcap(struct l2_packet_data *l2, unsigned short protocol)
99 {
100         bpf_u_int32 pcap_maskp, pcap_netp;
101         char pcap_filter[100], pcap_err[PCAP_ERRBUF_SIZE];
102         struct bpf_program pcap_fp;
103
104         pcap_lookupnet(l2->ifname, &pcap_netp, &pcap_maskp, pcap_err);
105         l2->pcap = pcap_open_live(l2->ifname, 2500, 0, 10, pcap_err);
106         if (l2->pcap == NULL) {
107                 fprintf(stderr, "pcap_open_live: %s\n", pcap_err);
108                 fprintf(stderr, "ifname='%s'\n", l2->ifname);
109                 return -1;
110         }
111         if (pcap_datalink(l2->pcap) != DLT_EN10MB &&
112             pcap_set_datalink(l2->pcap, DLT_EN10MB) < 0) {
113                 fprintf(stderr, "pcap_set_datalinke(DLT_EN10MB): %s\n",
114                         pcap_geterr(l2->pcap));
115                 return -1;
116         }
117         snprintf(pcap_filter, sizeof(pcap_filter),
118                  "ether dst " MACSTR " and ether proto 0x%x",
119                  MAC2STR(l2->own_addr), protocol);
120         if (pcap_compile(l2->pcap, &pcap_fp, pcap_filter, 1, pcap_netp) < 0) {
121                 fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(l2->pcap));
122                 return -1;
123         }
124
125         if (pcap_setfilter(l2->pcap, &pcap_fp) < 0) {
126                 fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(l2->pcap));
127                 return -1;
128         }
129
130         pcap_freecode(&pcap_fp);
131         /*
132          * When libpcap uses BPF we must enable "immediate mode" to
133          * receive frames right away; otherwise the system may
134          * buffer them for us.
135          */
136         { unsigned int on = 1;
137           if (ioctl(pcap_fileno(l2->pcap), BIOCIMMEDIATE, &on) < 0) {
138                 fprintf(stderr, "%s: cannot enable immediate mode on "
139                         "interface %s: %s\n",
140                         __func__, l2->ifname, strerror(errno));
141                 /* XXX should we fail? */
142           }
143         }
144
145         eloop_register_read_sock(pcap_get_selectable_fd(l2->pcap),
146                                  l2_packet_receive, l2, l2->pcap);
147
148         return 0;
149 }
150
151 static void
152 l2_packet_deinit_libpcap(struct l2_packet_data *l2)
153 {
154         if (l2->pcap != NULL) {
155                 eloop_unregister_read_sock(pcap_get_selectable_fd(l2->pcap));
156                 pcap_close(l2->pcap);
157                 l2->pcap = NULL;
158         }
159 }
160
161 static int
162 eth_get(const char *device, u8 ea[ETH_ALEN])
163 {
164         struct if_msghdr *ifm;
165         struct sockaddr_dl *sdl;
166         u_char *p, *buf;
167         size_t len;
168         int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };
169
170         if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
171                 return -1;
172         if ((buf = malloc(len)) == NULL)
173                 return -1;
174         if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
175                 free(buf);
176                 return -1;
177         }
178         for (p = buf; p < buf + len; p += ifm->ifm_msglen) {
179                 ifm = (struct if_msghdr *)p;
180                 sdl = (struct sockaddr_dl *)(ifm + 1);
181                 if (ifm->ifm_type != RTM_IFINFO ||
182                     (ifm->ifm_addrs & RTA_IFP) == 0)
183                         continue;
184                 if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
185                     memcmp(sdl->sdl_data, device, sdl->sdl_nlen) != 0)
186                         continue;
187                 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
188                 break;
189         }
190         free(buf);
191
192         if (p >= buf + len) {
193                 errno = ESRCH;
194                 return -1;
195         }
196         return 0;
197 }
198
199 struct l2_packet_data *
200 l2_packet_init(const char *ifname, const u8 *own_addr, unsigned short protocol,
201         void (*rx_callback)(void *ctx, unsigned char *src_addr,
202                             unsigned char *buf, size_t len),
203         void *rx_callback_ctx)
204 {
205         struct l2_packet_data *l2;
206
207         l2 = malloc(sizeof(struct l2_packet_data));
208         if (l2 == NULL)
209                 return NULL;
210         memset(l2, 0, sizeof(*l2));
211         strncpy(l2->ifname, ifname, sizeof(l2->ifname));
212         l2->rx_callback = rx_callback;
213         l2->rx_callback_ctx = rx_callback_ctx;
214
215         if (eth_get(l2->ifname, l2->own_addr) < 0) {
216                 fprintf(stderr, "Failed to get link-level address for "
217                         "interface '%s'.\n", l2->ifname);
218                 free(l2);
219                 return NULL;
220         }
221
222         if (l2_packet_init_libpcap(l2, protocol) != 0) {
223                 free(l2);
224                 return NULL;
225         }
226         return l2;
227 }
228
229 void
230 l2_packet_deinit(struct l2_packet_data *l2)
231 {
232         if (l2 != NULL) {
233                 l2_packet_deinit_libpcap(l2);
234                 free(l2);
235         }
236 }