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