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>
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.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
17 #include <sys/ioctl.h>
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>
25 #else /* USE_KERNEL_HEADERS */
26 #include <net/if_arp.h>
28 #include <netpacket/packet.h>
29 #endif /* USE_KERNEL_HEADERS */
32 #include "ieee802_1x.h"
36 #include "accounting.h"
39 struct wired_driver_data {
40 struct hostapd_data *hapd;
42 int sock; /* raw packet socket for driver access */
43 int dhcp_sock; /* socket for dhcp packets */
44 int use_pae_group_addr;
48 #define WIRED_EAPOL_MULTICAST_GROUP {0x01,0x80,0xc2,0x00,0x00,0x03}
51 /* TODO: detecting new devices should eventually be changed from using DHCP
52 * snooping to trigger on any packet from a new layer 2 MAC address, e.g.,
53 * based on ebtables, etc. */
71 u_int8_t options[308]; /* 312 - cookie */
75 static void wired_possible_new_sta(struct hostapd_data *hapd, u8 *addr)
79 sta = ap_get_sta(hapd, addr);
83 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
84 " - adding a new STA", MAC2STR(addr));
85 sta = ap_sta_add(hapd, addr);
87 hostapd_new_assoc_sta(hapd, sta, 0);
89 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
95 static void handle_data(struct hostapd_data *hapd, unsigned char *buf,
98 struct ieee8023_hdr *hdr;
102 /* must contain at least ieee8023_hdr 6 byte source, 6 byte dest,
103 * 2 byte ethertype */
105 wpa_printf(MSG_MSGDUMP, "handle_data: too short (%lu)",
106 (unsigned long) len);
110 hdr = (struct ieee8023_hdr *) buf;
112 switch (ntohs(hdr->ethertype)) {
114 wpa_printf(MSG_MSGDUMP, "Received EAPOL packet");
116 wired_possible_new_sta(hapd, sa);
118 pos = (u8 *) (hdr + 1);
119 left = len - sizeof(*hdr);
121 ieee802_1x_receive(hapd, sa, pos, left);
125 wpa_printf(MSG_DEBUG, "Unknown ethertype 0x%04x in data frame",
126 ntohs(hdr->ethertype));
132 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
134 struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
136 unsigned char buf[3000];
138 len = recv(sock, buf, sizeof(buf), 0);
144 handle_data(hapd, buf, len);
148 static void handle_dhcp(int sock, void *eloop_ctx, void *sock_ctx)
150 struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
152 unsigned char buf[3000];
153 struct dhcp_message *msg;
156 len = recv(sock, buf, sizeof(buf), 0);
162 /* must contain at least dhcp_message->chaddr */
164 wpa_printf(MSG_MSGDUMP, "handle_dhcp: too short (%d)", len);
168 msg = (struct dhcp_message *) buf;
169 mac_address = (u8 *) &(msg->chaddr);
171 wpa_printf(MSG_MSGDUMP, "Got DHCP broadcast packet from " MACSTR,
172 MAC2STR(mac_address));
174 wired_possible_new_sta(hapd, mac_address);
178 static int wired_init_sockets(struct wired_driver_data *drv)
180 struct hostapd_data *hapd = drv->hapd;
182 struct sockaddr_ll addr;
183 struct sockaddr_in addr2;
184 struct packet_mreq mreq;
185 u8 multicastgroup_eapol[6] = WIRED_EAPOL_MULTICAST_GROUP;
188 drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_PAE));
190 perror("socket[PF_PACKET,SOCK_RAW]");
194 if (eloop_register_read_sock(drv->sock, handle_read, hapd, NULL)) {
195 printf("Could not register read socket\n");
199 memset(&ifr, 0, sizeof(ifr));
200 os_strlcpy(ifr.ifr_name, hapd->conf->iface, sizeof(ifr.ifr_name));
201 if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
202 perror("ioctl(SIOCGIFINDEX)");
207 memset(&addr, 0, sizeof(addr));
208 addr.sll_family = AF_PACKET;
209 addr.sll_ifindex = ifr.ifr_ifindex;
210 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
213 if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
218 /* filter multicast address */
219 memset(&mreq, 0, sizeof(mreq));
220 mreq.mr_ifindex = ifr.ifr_ifindex;
221 mreq.mr_type = PACKET_MR_MULTICAST;
223 memcpy(mreq.mr_address, multicastgroup_eapol, mreq.mr_alen);
225 if (setsockopt(drv->sock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq,
227 perror("setsockopt[SOL_SOCKET,PACKET_ADD_MEMBERSHIP]");
231 memset(&ifr, 0, sizeof(ifr));
232 os_strlcpy(ifr.ifr_name, hapd->conf->iface, sizeof(ifr.ifr_name));
233 if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
234 perror("ioctl(SIOCGIFHWADDR)");
238 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
239 printf("Invalid HW-addr family 0x%04x\n",
240 ifr.ifr_hwaddr.sa_family);
243 memcpy(hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
245 /* setup dhcp listen socket for sta detection */
246 if ((drv->dhcp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
247 perror("socket call failed for dhcp");
251 if (eloop_register_read_sock(drv->dhcp_sock, handle_dhcp, hapd, NULL))
253 printf("Could not register read socket\n");
257 memset(&addr2, 0, sizeof(addr2));
258 addr2.sin_family = AF_INET;
259 addr2.sin_port = htons(67);
260 addr2.sin_addr.s_addr = INADDR_ANY;
262 if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_REUSEADDR, (char *) &n,
264 perror("setsockopt[SOL_SOCKET,SO_REUSEADDR]");
267 if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_BROADCAST, (char *) &n,
269 perror("setsockopt[SOL_SOCKET,SO_BROADCAST]");
273 memset(&ifr, 0, sizeof(ifr));
274 os_strlcpy(ifr.ifr_ifrn.ifrn_name, hapd->conf->iface, IFNAMSIZ);
275 if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_BINDTODEVICE,
276 (char *) &ifr, sizeof(ifr)) < 0) {
277 perror("setsockopt[SOL_SOCKET,SO_BINDTODEVICE]");
281 if (bind(drv->dhcp_sock, (struct sockaddr *) &addr2,
282 sizeof(struct sockaddr)) == -1) {
291 static int wired_send_eapol(void *priv, const u8 *addr,
292 const u8 *data, size_t data_len, int encrypt,
295 struct wired_driver_data *drv = priv;
296 u8 pae_group_addr[ETH_ALEN] = WIRED_EAPOL_MULTICAST_GROUP;
297 struct ieee8023_hdr *hdr;
302 len = sizeof(*hdr) + data_len;
303 hdr = os_zalloc(len);
305 printf("malloc() failed for wired_send_eapol(len=%lu)\n",
306 (unsigned long) len);
310 memcpy(hdr->dest, drv->use_pae_group_addr ? pae_group_addr : addr,
312 memcpy(hdr->src, own_addr, ETH_ALEN);
313 hdr->ethertype = htons(ETH_P_PAE);
315 pos = (u8 *) (hdr + 1);
316 memcpy(pos, data, data_len);
318 res = send(drv->sock, (u8 *) hdr, len, 0);
322 perror("wired_send_eapol: send");
323 printf("wired_send_eapol - packet len: %lu - failed\n",
324 (unsigned long) len);
331 static void * wired_driver_init(struct hostapd_data *hapd)
333 struct wired_driver_data *drv;
335 drv = os_zalloc(sizeof(struct wired_driver_data));
337 printf("Could not allocate memory for wired driver data\n");
342 drv->use_pae_group_addr = hapd->conf->use_pae_group_addr;
344 if (wired_init_sockets(drv)) {
353 static void wired_driver_deinit(void *priv)
355 struct wired_driver_data *drv = priv;
360 if (drv->dhcp_sock >= 0)
361 close(drv->dhcp_sock);
367 const struct wpa_driver_ops wpa_driver_wired_ops = {
369 .init = wired_driver_init,
370 .deinit = wired_driver_deinit,
371 .send_eapol = wired_send_eapol,