Remove some unnecessary casts.
[dragonfly.git] / contrib / hostapd-0.5.8 / iapp.c
blob5e027fb06bac09d82a9f9dc78171cfff67420cff
1 /*
2 * hostapd / IEEE 802.11F-2003 Inter-Access Point Protocol (IAPP)
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
14 * Note: IEEE 802.11F-2003 was a experimental use specification. It has expired
15 * and IEEE has withdrawn it. In other words, it is likely better to look at
16 * using some other mechanism for AP-to-AP communication than extenting the
17 * implementation here.
20 /* TODO:
21 * Level 1: no administrative or security support
22 * (e.g., static BSSID to IP address mapping in each AP)
23 * Level 2: support for dynamic mapping of BSSID to IP address
24 * Level 3: support for encryption and authentication of IAPP messages
25 * - add support for MOVE-notify and MOVE-response (this requires support for
26 * finding out IP address for previous AP using RADIUS)
27 * - add support for Send- and ACK-Security-Block to speedup IEEE 802.1X during
28 * reassociation to another AP
29 * - implement counters etc. for IAPP MIB
30 * - verify endianness of fields in IAPP messages; are they big-endian as
31 * used here?
32 * - RADIUS connection for AP registration and BSSID to IP address mapping
33 * - TCP connection for IAPP MOVE, CACHE
34 * - broadcast ESP for IAPP ADD-notify
35 * - ESP for IAPP MOVE messages
36 * - security block sending/processing
37 * - IEEE 802.11 context transfer
40 #include "includes.h"
41 #include <net/if.h>
42 #include <sys/ioctl.h>
43 #ifdef USE_KERNEL_HEADERS
44 #include <linux/if_packet.h>
45 #else /* USE_KERNEL_HEADERS */
46 #include <netpacket/packet.h>
47 #endif /* USE_KERNEL_HEADERS */
49 #include "hostapd.h"
50 #include "ieee802_11.h"
51 #include "iapp.h"
52 #include "eloop.h"
53 #include "sta_info.h"
56 #define IAPP_MULTICAST "224.0.1.178"
57 #define IAPP_UDP_PORT 3517
58 #define IAPP_TCP_PORT 3517
60 struct iapp_hdr {
61 u8 version;
62 u8 command;
63 u16 identifier;
64 u16 length;
65 /* followed by length-6 octets of data */
66 } __attribute__ ((packed));
68 #define IAPP_VERSION 0
70 enum IAPP_COMMAND {
71 IAPP_CMD_ADD_notify = 0,
72 IAPP_CMD_MOVE_notify = 1,
73 IAPP_CMD_MOVE_response = 2,
74 IAPP_CMD_Send_Security_Block = 3,
75 IAPP_CMD_ACK_Security_Block = 4,
76 IAPP_CMD_CACHE_notify = 5,
77 IAPP_CMD_CACHE_response = 6,
81 /* ADD-notify - multicast UDP on the local LAN */
82 struct iapp_add_notify {
83 u8 addr_len; /* ETH_ALEN */
84 u8 reserved;
85 u8 mac_addr[ETH_ALEN];
86 u16 seq_num;
87 } __attribute__ ((packed));
90 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
91 struct iapp_layer2_update {
92 u8 da[ETH_ALEN]; /* broadcast */
93 u8 sa[ETH_ALEN]; /* STA addr */
94 u16 len; /* 6 */
95 u8 dsap; /* null DSAP address */
96 u8 ssap; /* null SSAP address, CR=Response */
97 u8 control;
98 u8 xid_info[3];
99 } __attribute__ ((packed));
102 /* MOVE-notify - unicast TCP */
103 struct iapp_move_notify {
104 u8 addr_len; /* ETH_ALEN */
105 u8 reserved;
106 u8 mac_addr[ETH_ALEN];
107 u16 seq_num;
108 u16 ctx_block_len;
109 /* followed by ctx_block_len bytes */
110 } __attribute__ ((packed));
113 /* MOVE-response - unicast TCP */
114 struct iapp_move_response {
115 u8 addr_len; /* ETH_ALEN */
116 u8 status;
117 u8 mac_addr[ETH_ALEN];
118 u16 seq_num;
119 u16 ctx_block_len;
120 /* followed by ctx_block_len bytes */
121 } __attribute__ ((packed));
123 enum {
124 IAPP_MOVE_SUCCESSFUL = 0,
125 IAPP_MOVE_DENIED = 1,
126 IAPP_MOVE_STALE_MOVE = 2,
130 /* CACHE-notify */
131 struct iapp_cache_notify {
132 u8 addr_len; /* ETH_ALEN */
133 u8 reserved;
134 u8 mac_addr[ETH_ALEN];
135 u16 seq_num;
136 u8 current_ap[ETH_ALEN];
137 u16 ctx_block_len;
138 /* ctx_block_len bytes of context block followed by 16-bit context
139 * timeout */
140 } __attribute__ ((packed));
143 /* CACHE-response - unicast TCP */
144 struct iapp_cache_response {
145 u8 addr_len; /* ETH_ALEN */
146 u8 status;
147 u8 mac_addr[ETH_ALEN];
148 u16 seq_num;
149 } __attribute__ ((packed));
151 enum {
152 IAPP_CACHE_SUCCESSFUL = 0,
153 IAPP_CACHE_STALE_CACHE = 1,
157 /* Send-Security-Block - unicast TCP */
158 struct iapp_send_security_block {
159 u8 iv[8];
160 u16 sec_block_len;
161 /* followed by sec_block_len bytes of security block */
162 } __attribute__ ((packed));
165 /* ACK-Security-Block - unicast TCP */
166 struct iapp_ack_security_block {
167 u8 iv[8];
168 u8 new_ap_ack_authenticator[48];
169 } __attribute__ ((packed));
172 struct iapp_data {
173 struct hostapd_data *hapd;
174 u16 identifier; /* next IAPP identifier */
175 struct in_addr own, multicast;
176 int udp_sock;
177 int packet_sock;
181 static void iapp_send_add(struct iapp_data *iapp, u8 *mac_addr, u16 seq_num)
183 char buf[128];
184 struct iapp_hdr *hdr;
185 struct iapp_add_notify *add;
186 struct sockaddr_in addr;
188 /* Send IAPP ADD-notify to remove possible association from other APs
191 hdr = (struct iapp_hdr *) buf;
192 hdr->version = IAPP_VERSION;
193 hdr->command = IAPP_CMD_ADD_notify;
194 hdr->identifier = host_to_be16(iapp->identifier++);
195 hdr->length = host_to_be16(sizeof(*hdr) + sizeof(*add));
197 add = (struct iapp_add_notify *) (hdr + 1);
198 add->addr_len = ETH_ALEN;
199 add->reserved = 0;
200 memcpy(add->mac_addr, mac_addr, ETH_ALEN);
202 add->seq_num = host_to_be16(seq_num);
204 memset(&addr, 0, sizeof(addr));
205 addr.sin_family = AF_INET;
206 addr.sin_addr.s_addr = iapp->multicast.s_addr;
207 addr.sin_port = htons(IAPP_UDP_PORT);
208 if (sendto(iapp->udp_sock, buf, (char *) (add + 1) - buf, 0,
209 (struct sockaddr *) &addr, sizeof(addr)) < 0)
210 perror("sendto[IAPP-ADD]");
214 static void iapp_send_layer2_update(struct iapp_data *iapp, u8 *addr)
216 struct iapp_layer2_update msg;
218 /* Send Level 2 Update Frame to update forwarding tables in layer 2
219 * bridge devices */
221 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
222 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
224 memset(msg.da, 0xff, ETH_ALEN);
225 memcpy(msg.sa, addr, ETH_ALEN);
226 msg.len = host_to_be16(6);
227 msg.dsap = 0; /* NULL DSAP address */
228 msg.ssap = 0x01; /* NULL SSAP address, CR Bit: Response */
229 msg.control = 0xaf; /* XID response lsb.1111F101.
230 * F=0 (no poll command; unsolicited frame) */
231 msg.xid_info[0] = 0x81; /* XID format identifier */
232 msg.xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
233 msg.xid_info[2] = 1 << 1; /* XID sender's receive window size (RW)
234 * FIX: what is correct RW with 802.11? */
236 if (send(iapp->packet_sock, &msg, sizeof(msg), 0) < 0)
237 perror("send[L2 Update]");
241 void iapp_new_station(struct iapp_data *iapp, struct sta_info *sta)
243 struct ieee80211_mgmt *assoc;
244 u16 seq;
246 if (iapp == NULL)
247 return;
249 assoc = sta->last_assoc_req;
250 seq = assoc ? WLAN_GET_SEQ_SEQ(le_to_host16(assoc->seq_ctrl)) : 0;
252 /* IAPP-ADD.request(MAC Address, Sequence Number, Timeout) */
253 hostapd_logger(iapp->hapd, sta->addr, HOSTAPD_MODULE_IAPP,
254 HOSTAPD_LEVEL_DEBUG, "IAPP-ADD.request(seq=%d)", seq);
255 iapp_send_layer2_update(iapp, sta->addr);
256 iapp_send_add(iapp, sta->addr, seq);
258 if (assoc && WLAN_FC_GET_STYPE(le_to_host16(assoc->frame_control)) ==
259 WLAN_FC_STYPE_REASSOC_REQ) {
260 /* IAPP-MOVE.request(MAC Address, Sequence Number, Old AP,
261 * Context Block, Timeout)
263 /* TODO: Send IAPP-MOVE to the old AP; Map Old AP BSSID to
264 * IP address */
269 static void iapp_process_add_notify(struct iapp_data *iapp,
270 struct sockaddr_in *from,
271 struct iapp_hdr *hdr, int len)
273 struct iapp_add_notify *add = (struct iapp_add_notify *) (hdr + 1);
274 struct sta_info *sta;
276 if (len != sizeof(*add)) {
277 printf("Invalid IAPP-ADD packet length %d (expected %lu)\n",
278 len, (unsigned long) sizeof(*add));
279 return;
282 sta = ap_get_sta(iapp->hapd, add->mac_addr);
284 /* IAPP-ADD.indication(MAC Address, Sequence Number) */
285 hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
286 HOSTAPD_LEVEL_INFO,
287 "Received IAPP ADD-notify (seq# %d) from %s:%d%s",
288 be_to_host16(add->seq_num),
289 inet_ntoa(from->sin_addr), ntohs(from->sin_port),
290 sta ? "" : " (STA not found)");
292 if (!sta)
293 return;
295 /* TODO: could use seq_num to try to determine whether last association
296 * to this AP is newer than the one advertised in IAPP-ADD. Although,
297 * this is not really a reliable verification. */
299 hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
300 HOSTAPD_LEVEL_DEBUG,
301 "Removing STA due to IAPP ADD-notify");
302 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
303 eloop_cancel_timeout(ap_handle_timer, iapp->hapd, sta);
304 eloop_register_timeout(0, 0, ap_handle_timer, iapp->hapd, sta);
305 sta->timeout_next = STA_REMOVE;
309 static void iapp_receive_udp(int sock, void *eloop_ctx, void *sock_ctx)
311 struct iapp_data *iapp = eloop_ctx;
312 int len, hlen;
313 unsigned char buf[128];
314 struct sockaddr_in from;
315 socklen_t fromlen;
316 struct iapp_hdr *hdr;
318 /* Handle incoming IAPP frames (over UDP/IP) */
320 fromlen = sizeof(from);
321 len = recvfrom(iapp->udp_sock, buf, sizeof(buf), 0,
322 (struct sockaddr *) &from, &fromlen);
323 if (len < 0) {
324 perror("recvfrom");
325 return;
328 if (from.sin_addr.s_addr == iapp->own.s_addr)
329 return; /* ignore own IAPP messages */
331 hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
332 HOSTAPD_LEVEL_DEBUG,
333 "Received %d byte IAPP frame from %s%s\n",
334 len, inet_ntoa(from.sin_addr),
335 len < (int) sizeof(*hdr) ? " (too short)" : "");
337 if (len < (int) sizeof(*hdr))
338 return;
340 hdr = (struct iapp_hdr *) buf;
341 hlen = be_to_host16(hdr->length);
342 hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
343 HOSTAPD_LEVEL_DEBUG,
344 "RX: version=%d command=%d id=%d len=%d\n",
345 hdr->version, hdr->command,
346 be_to_host16(hdr->identifier), hlen);
347 if (hdr->version != IAPP_VERSION) {
348 printf("Dropping IAPP frame with unknown version %d\n",
349 hdr->version);
350 return;
352 if (hlen > len) {
353 printf("Underflow IAPP frame (hlen=%d len=%d)\n", hlen, len);
354 return;
356 if (hlen < len) {
357 printf("Ignoring %d extra bytes from IAPP frame\n",
358 len - hlen);
359 len = hlen;
362 switch (hdr->command) {
363 case IAPP_CMD_ADD_notify:
364 iapp_process_add_notify(iapp, &from, hdr, hlen - sizeof(*hdr));
365 break;
366 case IAPP_CMD_MOVE_notify:
367 /* TODO: MOVE is using TCP; so move this to TCP handler once it
368 * is implemented.. */
369 /* IAPP-MOVE.indication(MAC Address, New BSSID,
370 * Sequence Number, AP Address, Context Block) */
371 /* TODO: process */
372 break;
373 default:
374 printf("Unknown IAPP command %d\n", hdr->command);
375 break;
380 struct iapp_data * iapp_init(struct hostapd_data *hapd, const char *iface)
382 struct ifreq ifr;
383 struct sockaddr_ll addr;
384 int ifindex;
385 struct sockaddr_in *paddr, uaddr;
386 struct iapp_data *iapp;
387 struct ip_mreqn mreq;
389 iapp = wpa_zalloc(sizeof(*iapp));
390 if (iapp == NULL)
391 return NULL;
392 iapp->hapd = hapd;
393 iapp->udp_sock = iapp->packet_sock = -1;
395 /* TODO:
396 * open socket for sending and receiving IAPP frames over TCP
399 iapp->udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
400 if (iapp->udp_sock < 0) {
401 perror("socket[PF_INET,SOCK_DGRAM]");
402 iapp_deinit(iapp);
403 return NULL;
406 memset(&ifr, 0, sizeof(ifr));
407 strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
408 if (ioctl(iapp->udp_sock, SIOCGIFINDEX, &ifr) != 0) {
409 perror("ioctl(SIOCGIFINDEX)");
410 iapp_deinit(iapp);
411 return NULL;
413 ifindex = ifr.ifr_ifindex;
415 if (ioctl(iapp->udp_sock, SIOCGIFADDR, &ifr) != 0) {
416 perror("ioctl(SIOCGIFADDR)");
417 iapp_deinit(iapp);
418 return NULL;
420 paddr = (struct sockaddr_in *) &ifr.ifr_addr;
421 if (paddr->sin_family != AF_INET) {
422 printf("Invalid address family %i (SIOCGIFADDR)\n",
423 paddr->sin_family);
424 iapp_deinit(iapp);
425 return NULL;
427 iapp->own.s_addr = paddr->sin_addr.s_addr;
429 if (ioctl(iapp->udp_sock, SIOCGIFBRDADDR, &ifr) != 0) {
430 perror("ioctl(SIOCGIFBRDADDR)");
431 iapp_deinit(iapp);
432 return NULL;
434 paddr = (struct sockaddr_in *) &ifr.ifr_addr;
435 if (paddr->sin_family != AF_INET) {
436 printf("Invalid address family %i (SIOCGIFBRDADDR)\n",
437 paddr->sin_family);
438 iapp_deinit(iapp);
439 return NULL;
441 inet_aton(IAPP_MULTICAST, &iapp->multicast);
443 memset(&uaddr, 0, sizeof(uaddr));
444 uaddr.sin_family = AF_INET;
445 uaddr.sin_port = htons(IAPP_UDP_PORT);
446 if (bind(iapp->udp_sock, (struct sockaddr *) &uaddr,
447 sizeof(uaddr)) < 0) {
448 perror("bind[UDP]");
449 iapp_deinit(iapp);
450 return NULL;
453 memset(&mreq, 0, sizeof(mreq));
454 mreq.imr_multiaddr = iapp->multicast;
455 mreq.imr_address.s_addr = INADDR_ANY;
456 mreq.imr_ifindex = 0;
457 if (setsockopt(iapp->udp_sock, SOL_IP, IP_ADD_MEMBERSHIP, &mreq,
458 sizeof(mreq)) < 0) {
459 perror("setsockopt[UDP,IP_ADD_MEMBERSHIP]");
460 iapp_deinit(iapp);
461 return NULL;
464 iapp->packet_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
465 if (iapp->packet_sock < 0) {
466 perror("socket[PF_PACKET,SOCK_RAW]");
467 iapp_deinit(iapp);
468 return NULL;
471 memset(&addr, 0, sizeof(addr));
472 addr.sll_family = AF_PACKET;
473 addr.sll_ifindex = ifindex;
474 if (bind(iapp->packet_sock, (struct sockaddr *) &addr,
475 sizeof(addr)) < 0) {
476 perror("bind[PACKET]");
477 iapp_deinit(iapp);
478 return NULL;
481 if (eloop_register_read_sock(iapp->udp_sock, iapp_receive_udp,
482 iapp, NULL)) {
483 printf("Could not register read socket for IAPP.\n");
484 iapp_deinit(iapp);
485 return NULL;
488 printf("IEEE 802.11F (IAPP) using interface %s\n", iface);
490 /* TODO: For levels 2 and 3: send RADIUS Initiate-Request, receive
491 * RADIUS Initiate-Accept or Initiate-Reject. IAPP port should actually
492 * be openned only after receiving Initiate-Accept. If Initiate-Reject
493 * is received, IAPP is not started. */
495 return iapp;
499 void iapp_deinit(struct iapp_data *iapp)
501 struct ip_mreqn mreq;
503 if (iapp == NULL)
504 return;
506 if (iapp->udp_sock >= 0) {
507 memset(&mreq, 0, sizeof(mreq));
508 mreq.imr_multiaddr = iapp->multicast;
509 mreq.imr_address.s_addr = INADDR_ANY;
510 mreq.imr_ifindex = 0;
511 if (setsockopt(iapp->udp_sock, SOL_IP, IP_DROP_MEMBERSHIP,
512 &mreq, sizeof(mreq)) < 0) {
513 perror("setsockopt[UDP,IP_DEL_MEMBERSHIP]");
516 eloop_unregister_read_sock(iapp->udp_sock);
517 close(iapp->udp_sock);
519 if (iapp->packet_sock >= 0) {
520 eloop_unregister_read_sock(iapp->packet_sock);
521 close(iapp->packet_sock);
523 free(iapp);
526 int iapp_reconfig(struct hostapd_data *hapd, struct hostapd_config *oldconf,
527 struct hostapd_bss_config *oldbss)
529 if (hapd->conf->ieee802_11f != oldbss->ieee802_11f ||
530 strcmp(hapd->conf->iapp_iface, oldbss->iapp_iface) != 0) {
532 iapp_deinit(hapd->iapp);
533 hapd->iapp = NULL;
535 if (hapd->conf->ieee802_11f) {
536 hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface);
538 if (hapd->iapp == NULL)
539 return -1;
543 return 0;