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