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