Sync usage with man page.
[netbsd-mini2440.git] / dist / wpa / hostapd / driver_wired.c
blobab970712892feea1ab910612865c4ad04814f0e7
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>
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
11 * license.
13 * See README and COPYING for more details.
16 #include "includes.h"
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>
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 */
31 #include "hostapd.h"
32 #include "ieee802_1x.h"
33 #include "eloop.h"
34 #include "sta_info.h"
35 #include "driver.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. */
55 struct dhcp_message {
56 u_int8_t op;
57 u_int8_t htype;
58 u_int8_t hlen;
59 u_int8_t hops;
60 u_int32_t xid;
61 u_int16_t secs;
62 u_int16_t flags;
63 u_int32_t ciaddr;
64 u_int32_t yiaddr;
65 u_int32_t siaddr;
66 u_int32_t giaddr;
67 u_int8_t chaddr[16];
68 u_int8_t sname[64];
69 u_int8_t file[128];
70 u_int32_t cookie;
71 u_int8_t options[308]; /* 312 - cookie */
75 static void wired_possible_new_sta(struct hostapd_data *hapd, u8 *addr)
77 struct sta_info *sta;
79 sta = ap_get_sta(hapd, addr);
80 if (sta)
81 return;
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);
86 if (sta) {
87 hostapd_new_assoc_sta(hapd, sta, 0);
88 accounting_sta_get_id(hapd, sta);
89 } else {
90 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
91 MAC2STR(addr));
96 static void handle_data(struct hostapd_data *hapd, unsigned char *buf,
97 size_t len)
99 struct ieee8023_hdr *hdr;
100 u8 *pos, *sa;
101 size_t left;
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;
111 hdr = (struct ieee8023_hdr *) buf;
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);
119 pos = (u8 *) (hdr + 1);
120 left = len - sizeof(*hdr);
122 ieee802_1x_receive(hapd, sa, pos, left);
123 break;
125 default:
126 wpa_printf(MSG_DEBUG, "Unknown ethertype 0x%04x in data frame",
127 ntohs(hdr->ethertype));
128 break;
133 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
135 struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
136 int len;
137 unsigned char buf[3000];
139 len = recv(sock, buf, sizeof(buf), 0);
140 if (len < 0) {
141 perror("recv");
142 return;
145 handle_data(hapd, buf, len);
149 static void handle_dhcp(int sock, void *eloop_ctx, void *sock_ctx)
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;
157 len = recv(sock, buf, sizeof(buf), 0);
158 if (len < 0) {
159 perror("recv");
160 return;
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;
169 msg = (struct dhcp_message *) buf;
170 mac_address = (u8 *) &(msg->chaddr);
172 wpa_printf(MSG_MSGDUMP, "Got DHCP broadcast packet from " MACSTR,
173 MAC2STR(mac_address));
175 wired_possible_new_sta(hapd, mac_address);
179 static int wired_init_sockets(struct wired_driver_data *drv)
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;
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;
195 if (eloop_register_read_sock(drv->sock, handle_read, hapd, NULL)) {
196 printf("Could not register read socket\n");
197 return -1;
200 memset(&ifr, 0, sizeof(ifr));
201 os_strlcpy(ifr.ifr_name, hapd->conf->iface, sizeof(ifr.ifr_name));
202 if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
203 perror("ioctl(SIOCGIFINDEX)");
204 return -1;
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);
214 if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
215 perror("bind");
216 return -1;
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);
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;
232 memset(&ifr, 0, sizeof(ifr));
233 os_strlcpy(ifr.ifr_name, hapd->conf->iface, sizeof(ifr.ifr_name));
234 if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
235 perror("ioctl(SIOCGIFHWADDR)");
236 return -1;
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;
244 memcpy(hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
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;
252 if (eloop_register_read_sock(drv->dhcp_sock, handle_dhcp, hapd, NULL))
254 printf("Could not register read socket\n");
255 return -1;
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;
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;
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;
274 memset(&ifr, 0, sizeof(ifr));
275 os_strlcpy(ifr.ifr_ifrn.ifrn_name, hapd->conf->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;
282 if (bind(drv->dhcp_sock, (struct sockaddr *) &addr2,
283 sizeof(struct sockaddr)) == -1) {
284 perror("bind");
285 return -1;
288 return 0;
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)
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;
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;
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);
316 pos = (u8 *) (hdr + 1);
317 memcpy(pos, data, data_len);
319 res = send(drv->sock, (u8 *) hdr, len, 0);
320 free(hdr);
322 if (res < 0) {
323 perror("wired_send_eapol: send");
324 printf("wired_send_eapol - packet len: %lu - failed\n",
325 (unsigned long) len);
328 return res;
332 static void * wired_driver_init(struct hostapd_data *hapd)
334 struct wired_driver_data *drv;
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;
342 drv->hapd = hapd;
343 drv->use_pae_group_addr = hapd->conf->use_pae_group_addr;
345 if (wired_init_sockets(drv)) {
346 free(drv);
347 return NULL;
350 return drv;
354 static void wired_driver_deinit(void *priv)
356 struct wired_driver_data *drv = priv;
358 if (drv->sock >= 0)
359 close(drv->sock);
361 if (drv->dhcp_sock >= 0)
362 close(drv->dhcp_sock);
364 free(drv);
368 const struct wpa_driver_ops wpa_driver_wired_ops = {
369 .name = "wired",
370 .init = wired_driver_init,
371 .deinit = wired_driver_deinit,
372 .send_eapol = wired_send_eapol,