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>
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.
11 * Alternatively, this software may be distributed under the terms of BSD
14 * See README and COPYING for more details.
22 #include <sys/ioctl.h>
23 #include <netinet/in.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
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>
34 #else /* USE_KERNEL_HEADERS */
35 #include <net/if_arp.h>
37 #include <netpacket/packet.h>
38 #endif /* USE_KERNEL_HEADERS */
41 #include "ieee802_1x.h"
45 #include "accounting.h"
48 struct wired_driver_data {
49 struct driver_ops ops;
50 struct hostapd_data *hapd;
52 int sock; /* raw packet socket for driver access */
53 int dhcp_sock; /* socket for dhcp packets */
54 int use_pae_group_addr;
57 static const struct driver_ops wired_driver_ops;
60 #define WIRED_EAPOL_MULTICAST_GROUP {0x01,0x80,0xc2,0x00,0x00,0x03}
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. */
83 u_int8_t options[308]; /* 312 - cookie */
87 static void wired_possible_new_sta(struct hostapd_data *hapd, u8 *addr)
91 sta = ap_get_sta(hapd, addr);
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);
99 hostapd_new_assoc_sta(hapd, sta, 0);
100 accounting_sta_get_id(hapd, sta);
102 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "Failed to add STA entry "
103 "for " MACSTR "\n", MAC2STR(addr));
108 static void handle_data(struct hostapd_data *hapd, char *buf, size_t len)
110 struct ieee8023_hdr *hdr;
114 /* must contain at least ieee8023_hdr 6 byte source, 6 byte dest,
115 * 2 byte ethertype */
117 HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE, "handle_data: too short "
118 "(%lu)\n", (unsigned long) len);
122 hdr = (struct ieee8023_hdr *) buf;
124 switch (ntohs(hdr->ethertype)) {
126 HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE,
127 "Received EAPOL packet\n");
129 wired_possible_new_sta(hapd, sa);
131 pos = (u8 *) (hdr + 1);
132 left = len - sizeof(*hdr);
134 ieee802_1x_receive(hapd, sa, pos, left);
138 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
139 "Unknown ethertype 0x%04x in data frame\n",
140 ntohs(hdr->ethertype));
146 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
148 struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
150 unsigned char buf[3000];
152 len = recv(sock, buf, sizeof(buf), 0);
158 handle_data(hapd, buf, len);
162 static void handle_dhcp(int sock, void *eloop_ctx, void *sock_ctx)
164 struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
166 unsigned char buf[3000];
167 struct dhcp_message *msg;
170 len = recv(sock, buf, sizeof(buf), 0);
176 /* must contain at least dhcp_message->chaddr */
178 HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE, "handle_dhcp: too short "
183 msg = (struct dhcp_message *) buf;
184 mac_address = (u8 *) &(msg->chaddr);
186 HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE,
187 "Got DHCP broadcast packet from " MACSTR "\n",
188 MAC2STR(mac_address));
190 wired_possible_new_sta(hapd, mac_address);
194 static int wired_init_sockets(struct wired_driver_data *drv)
196 struct hostapd_data *hapd = drv->hapd;
198 struct sockaddr_ll addr;
199 struct sockaddr_in addr2;
200 struct packet_mreq mreq;
201 u8 multicastgroup_eapol[6] = WIRED_EAPOL_MULTICAST_GROUP;
204 drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_PAE));
206 perror("socket[PF_PACKET,SOCK_RAW]");
210 if (eloop_register_read_sock(drv->sock, handle_read, hapd, NULL)) {
211 printf("Could not register read socket\n");
215 memset(&ifr, 0, sizeof(ifr));
216 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s",
218 if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
219 perror("ioctl(SIOCGIFINDEX)");
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",
231 if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
236 /* filter multicast address */
237 memset(&mreq, 0, sizeof(mreq));
238 mreq.mr_ifindex = ifr.ifr_ifindex;
239 mreq.mr_type = PACKET_MR_MULTICAST;
241 memcpy(mreq.mr_address, multicastgroup_eapol, mreq.mr_alen);
243 if (setsockopt(drv->sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq,
245 perror("setsockopt[SOL_SOCKET,PACKET_ADD_MEMBERSHIP]");
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)");
256 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
257 printf("Invalid HW-addr family 0x%04x\n",
258 ifr.ifr_hwaddr.sa_family);
261 memcpy(hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
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");
269 if (eloop_register_read_sock(drv->dhcp_sock, handle_dhcp, hapd, NULL))
271 printf("Could not register read socket\n");
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;
280 if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_REUSEADDR, (char *) &n,
282 perror("setsockopt[SOL_SOCKET,SO_REUSEADDR]");
285 if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_BROADCAST, (char *) &n,
287 perror("setsockopt[SOL_SOCKET,SO_BROADCAST]");
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]");
299 if (bind(drv->dhcp_sock, (struct sockaddr *) &addr2,
300 sizeof(struct sockaddr)) == -1) {
309 static int wired_send_eapol(void *priv, u8 *addr,
310 u8 *data, size_t data_len, int encrypt)
312 struct wired_driver_data *drv = priv;
313 u8 pae_group_addr[ETH_ALEN] = WIRED_EAPOL_MULTICAST_GROUP;
314 struct ieee8023_hdr *hdr;
319 len = sizeof(*hdr) + data_len;
322 printf("malloc() failed for wired_send_eapol(len=%lu)\n",
323 (unsigned long) len);
328 memcpy(hdr->dest, drv->use_pae_group_addr ? pae_group_addr : addr,
330 memcpy(hdr->src, drv->hapd->own_addr, ETH_ALEN);
331 hdr->ethertype = htons(ETH_P_PAE);
333 pos = (u8 *) (hdr + 1);
334 memcpy(pos, data, data_len);
336 res = send(drv->sock, (u8 *) hdr, len, 0);
340 perror("wired_send_eapol: send");
341 printf("wired_send_eapol - packet len: %lu - failed\n",
342 (unsigned long) len);
349 static int wired_driver_init(struct hostapd_data *hapd)
351 struct wired_driver_data *drv;
353 drv = malloc(sizeof(struct wired_driver_data));
355 printf("Could not allocate memory for wired driver data\n");
359 memset(drv, 0, sizeof(*drv));
360 drv->ops = wired_driver_ops;
362 drv->use_pae_group_addr = hapd->conf->use_pae_group_addr;
364 if (wired_init_sockets(drv))
367 hapd->driver = &drv->ops;
372 static void wired_driver_deinit(void *priv)
374 struct wired_driver_data *drv = priv;
376 drv->hapd->driver = NULL;
381 if (drv->dhcp_sock >= 0)
382 close(drv->dhcp_sock);
388 static const struct driver_ops wired_driver_ops = {
390 .init = wired_driver_init,
391 .deinit = wired_driver_deinit,
392 .send_eapol = wired_send_eapol,
395 void wired_driver_register(void)
397 driver_register(wired_driver_ops.name, &wired_driver_ops);