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