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