hostapd: Update vendor branch to 0.6.10
[dragonfly.git] / contrib / hostapd / hostapd / driver_wired.c
1 /*
2  * hostapd / Kernel driver communication for wired (Ethernet) drivers
3  * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2004, Gunter Burchardt <tira@isx.de>
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
16 #include "includes.h"
17 #include <sys/ioctl.h>
18
19 #ifdef USE_KERNEL_HEADERS
20 #include <asm/types.h>
21 #include <linux/if_packet.h>
22 #include <linux/if_ether.h>   /* The L2 protocols */
23 #include <linux/if_arp.h>
24 #include <linux/if.h>
25 #else /* USE_KERNEL_HEADERS */
26 #include <net/if_arp.h>
27 #include <net/if.h>
28 #include <netpacket/packet.h>
29 #endif /* USE_KERNEL_HEADERS */
30
31 #include "hostapd.h"
32 #include "ieee802_1x.h"
33 #include "eloop.h"
34 #include "sta_info.h"
35 #include "driver.h"
36 #include "accounting.h"
37
38
39 struct wired_driver_data {
40         struct hostapd_data *hapd;
41
42         int sock; /* raw packet socket for driver access */
43         int dhcp_sock; /* socket for dhcp packets */
44         int use_pae_group_addr;
45 };
46
47
48 #define WIRED_EAPOL_MULTICAST_GROUP     {0x01,0x80,0xc2,0x00,0x00,0x03}
49
50
51 /* TODO: detecting new devices should eventually be changed from using DHCP
52  * snooping to trigger on any packet from a new layer 2 MAC address, e.g.,
53  * based on ebtables, etc. */
54
55 struct dhcp_message {
56         u_int8_t op;
57         u_int8_t htype;
58         u_int8_t hlen;
59         u_int8_t hops;
60         u_int32_t xid;
61         u_int16_t secs;
62         u_int16_t flags;
63         u_int32_t ciaddr;
64         u_int32_t yiaddr;
65         u_int32_t siaddr;
66         u_int32_t giaddr;
67         u_int8_t chaddr[16];
68         u_int8_t sname[64];
69         u_int8_t file[128];
70         u_int32_t cookie;
71         u_int8_t options[308]; /* 312 - cookie */
72 };
73
74
75 static void wired_possible_new_sta(struct hostapd_data *hapd, u8 *addr)
76 {
77         struct sta_info *sta;
78
79         sta = ap_get_sta(hapd, addr);
80         if (sta)
81                 return;
82
83         wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
84                    " - adding a new STA", MAC2STR(addr));
85         sta = ap_sta_add(hapd, addr);
86         if (sta) {
87                 hostapd_new_assoc_sta(hapd, sta, 0);
88         } else {
89                 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
90                            MAC2STR(addr));
91         }
92 }
93
94
95 static void handle_data(struct hostapd_data *hapd, unsigned char *buf,
96                         size_t len)
97 {
98         struct ieee8023_hdr *hdr;
99         u8 *pos, *sa;
100         size_t left;
101
102         /* must contain at least ieee8023_hdr 6 byte source, 6 byte dest,
103          * 2 byte ethertype */
104         if (len < 14) {
105                 wpa_printf(MSG_MSGDUMP, "handle_data: too short (%lu)",
106                            (unsigned long) len);
107                 return;
108         }
109
110         hdr = (struct ieee8023_hdr *) buf;
111
112         switch (ntohs(hdr->ethertype)) {
113                 case ETH_P_PAE:
114                         wpa_printf(MSG_MSGDUMP, "Received EAPOL packet");
115                         sa = hdr->src;
116                         wired_possible_new_sta(hapd, sa);
117
118                         pos = (u8 *) (hdr + 1);
119                         left = len - sizeof(*hdr);
120
121                         ieee802_1x_receive(hapd, sa, pos, left);
122                 break;
123
124         default:
125                 wpa_printf(MSG_DEBUG, "Unknown ethertype 0x%04x in data frame",
126                            ntohs(hdr->ethertype));
127                 break;
128         }
129 }
130
131
132 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
133 {
134         struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
135         int len;
136         unsigned char buf[3000];
137
138         len = recv(sock, buf, sizeof(buf), 0);
139         if (len < 0) {
140                 perror("recv");
141                 return;
142         }
143         
144         handle_data(hapd, buf, len);
145 }
146
147
148 static void handle_dhcp(int sock, void *eloop_ctx, void *sock_ctx)
149 {
150         struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
151         int len;
152         unsigned char buf[3000];
153         struct dhcp_message *msg;
154         u8 *mac_address;
155
156         len = recv(sock, buf, sizeof(buf), 0);
157         if (len < 0) {
158                 perror("recv"); 
159                 return;
160         }
161
162         /* must contain at least dhcp_message->chaddr */
163         if (len < 44) {
164                 wpa_printf(MSG_MSGDUMP, "handle_dhcp: too short (%d)", len);
165                 return;
166         }
167         
168         msg = (struct dhcp_message *) buf;
169         mac_address = (u8 *) &(msg->chaddr);
170         
171         wpa_printf(MSG_MSGDUMP, "Got DHCP broadcast packet from " MACSTR,
172                    MAC2STR(mac_address));
173
174         wired_possible_new_sta(hapd, mac_address);
175 }
176
177
178 static int wired_init_sockets(struct wired_driver_data *drv)
179 {
180         struct hostapd_data *hapd = drv->hapd;
181         struct ifreq ifr;
182         struct sockaddr_ll addr;
183         struct sockaddr_in addr2;
184         struct packet_mreq mreq;
185         u8 multicastgroup_eapol[6] = WIRED_EAPOL_MULTICAST_GROUP;
186         int n = 1;
187
188         drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_PAE));
189         if (drv->sock < 0) {
190                 perror("socket[PF_PACKET,SOCK_RAW]");
191                 return -1;
192         }
193
194         if (eloop_register_read_sock(drv->sock, handle_read, hapd, NULL)) {
195                 printf("Could not register read socket\n");
196                 return -1;
197         }
198
199         memset(&ifr, 0, sizeof(ifr));
200         os_strlcpy(ifr.ifr_name, hapd->conf->iface, sizeof(ifr.ifr_name));
201         if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
202                 perror("ioctl(SIOCGIFINDEX)");
203                 return -1;
204         }
205
206         
207         memset(&addr, 0, sizeof(addr));
208         addr.sll_family = AF_PACKET;
209         addr.sll_ifindex = ifr.ifr_ifindex;
210         wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
211                    addr.sll_ifindex);
212
213         if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
214                 perror("bind");
215                 return -1;
216         }
217
218         /* filter multicast address */
219         memset(&mreq, 0, sizeof(mreq));
220         mreq.mr_ifindex = ifr.ifr_ifindex;
221         mreq.mr_type = PACKET_MR_MULTICAST;
222         mreq.mr_alen = 6;
223         memcpy(mreq.mr_address, multicastgroup_eapol, mreq.mr_alen);
224
225         if (setsockopt(drv->sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq,
226                        sizeof(mreq)) < 0) {
227                 perror("setsockopt[SOL_SOCKET,PACKET_ADD_MEMBERSHIP]");
228                 return -1;
229         }
230
231         memset(&ifr, 0, sizeof(ifr));
232         os_strlcpy(ifr.ifr_name, hapd->conf->iface, sizeof(ifr.ifr_name));
233         if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
234                 perror("ioctl(SIOCGIFHWADDR)");
235                 return -1;
236         }
237
238         if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
239                 printf("Invalid HW-addr family 0x%04x\n",
240                        ifr.ifr_hwaddr.sa_family);
241                 return -1;
242         }
243         memcpy(hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
244
245         /* setup dhcp listen socket for sta detection */
246         if ((drv->dhcp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
247                 perror("socket call failed for dhcp");
248                 return -1;
249         }
250
251         if (eloop_register_read_sock(drv->dhcp_sock, handle_dhcp, hapd, NULL))
252         {
253                 printf("Could not register read socket\n");
254                 return -1;
255         }
256         
257         memset(&addr2, 0, sizeof(addr2));
258         addr2.sin_family = AF_INET;
259         addr2.sin_port = htons(67);
260         addr2.sin_addr.s_addr = INADDR_ANY;
261
262         if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_REUSEADDR, (char *) &n,
263                        sizeof(n)) == -1) {
264                 perror("setsockopt[SOL_SOCKET,SO_REUSEADDR]");
265                 return -1;
266         }
267         if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_BROADCAST, (char *) &n,
268                        sizeof(n)) == -1) {
269                 perror("setsockopt[SOL_SOCKET,SO_BROADCAST]");
270                 return -1;
271         }
272
273         memset(&ifr, 0, sizeof(ifr));
274         os_strlcpy(ifr.ifr_ifrn.ifrn_name, hapd->conf->iface, IFNAMSIZ);
275         if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_BINDTODEVICE,
276                        (char *) &ifr, sizeof(ifr)) < 0) {
277                 perror("setsockopt[SOL_SOCKET,SO_BINDTODEVICE]");
278                 return -1;
279         }
280
281         if (bind(drv->dhcp_sock, (struct sockaddr *) &addr2,
282                  sizeof(struct sockaddr)) == -1) {
283                 perror("bind");
284                 return -1;
285         }
286
287         return 0;
288 }
289
290
291 static int wired_send_eapol(void *priv, const u8 *addr,
292                             const u8 *data, size_t data_len, int encrypt,
293                             const u8 *own_addr)
294 {
295         struct wired_driver_data *drv = priv;
296         u8 pae_group_addr[ETH_ALEN] = WIRED_EAPOL_MULTICAST_GROUP;
297         struct ieee8023_hdr *hdr;
298         size_t len;
299         u8 *pos;
300         int res;
301
302         len = sizeof(*hdr) + data_len;
303         hdr = os_zalloc(len);
304         if (hdr == NULL) {
305                 printf("malloc() failed for wired_send_eapol(len=%lu)\n",
306                        (unsigned long) len);
307                 return -1;
308         }
309
310         memcpy(hdr->dest, drv->use_pae_group_addr ? pae_group_addr : addr,
311                ETH_ALEN);
312         memcpy(hdr->src, own_addr, ETH_ALEN);
313         hdr->ethertype = htons(ETH_P_PAE);
314
315         pos = (u8 *) (hdr + 1);
316         memcpy(pos, data, data_len);
317
318         res = send(drv->sock, (u8 *) hdr, len, 0);
319         free(hdr);
320
321         if (res < 0) {
322                 perror("wired_send_eapol: send");
323                 printf("wired_send_eapol - packet len: %lu - failed\n",
324                        (unsigned long) len);
325         }
326
327         return res;
328 }
329
330
331 static void * wired_driver_init(struct hostapd_data *hapd)
332 {
333         struct wired_driver_data *drv;
334
335         drv = os_zalloc(sizeof(struct wired_driver_data));
336         if (drv == NULL) {
337                 printf("Could not allocate memory for wired driver data\n");
338                 return NULL;
339         }
340
341         drv->hapd = hapd;
342         drv->use_pae_group_addr = hapd->conf->use_pae_group_addr;
343
344         if (wired_init_sockets(drv)) {
345                 free(drv);
346                 return NULL;
347         }
348
349         return drv;
350 }
351
352
353 static void wired_driver_deinit(void *priv)
354 {
355         struct wired_driver_data *drv = priv;
356
357         if (drv->sock >= 0)
358                 close(drv->sock);
359         
360         if (drv->dhcp_sock >= 0)
361                 close(drv->dhcp_sock);
362
363         free(drv);
364 }
365
366
367 const struct wpa_driver_ops wpa_driver_wired_ops = {
368         .name = "wired",
369         .init = wired_driver_init,
370         .deinit = wired_driver_deinit,
371         .send_eapol = wired_send_eapol,
372 };