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
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 extending the
17 * implementation here.
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
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
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 */
52 #include "ieee802_11.h"
58 #define IAPP_MULTICAST "224.0.1.178"
59 #define IAPP_UDP_PORT 3517
60 #define IAPP_TCP_PORT 3517
67 /* followed by length-6 octets of data */
68 } __attribute__ ((packed));
70 #define IAPP_VERSION 0
73 IAPP_CMD_ADD_notify = 0,
74 IAPP_CMD_MOVE_notify = 1,
75 IAPP_CMD_MOVE_response = 2,
76 IAPP_CMD_Send_Security_Block = 3,
77 IAPP_CMD_ACK_Security_Block = 4,
78 IAPP_CMD_CACHE_notify = 5,
79 IAPP_CMD_CACHE_response = 6,
83 /* ADD-notify - multicast UDP on the local LAN */
84 struct iapp_add_notify {
85 u8 addr_len; /* ETH_ALEN */
87 u8 mac_addr[ETH_ALEN];
89 } __attribute__ ((packed));
92 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
93 struct iapp_layer2_update {
94 u8 da[ETH_ALEN]; /* broadcast */
95 u8 sa[ETH_ALEN]; /* STA addr */
97 u8 dsap; /* null DSAP address */
98 u8 ssap; /* null SSAP address, CR=Response */
101 } __attribute__ ((packed));
104 /* MOVE-notify - unicast TCP */
105 struct iapp_move_notify {
106 u8 addr_len; /* ETH_ALEN */
108 u8 mac_addr[ETH_ALEN];
111 /* followed by ctx_block_len bytes */
112 } __attribute__ ((packed));
115 /* MOVE-response - unicast TCP */
116 struct iapp_move_response {
117 u8 addr_len; /* ETH_ALEN */
119 u8 mac_addr[ETH_ALEN];
122 /* followed by ctx_block_len bytes */
123 } __attribute__ ((packed));
126 IAPP_MOVE_SUCCESSFUL = 0,
127 IAPP_MOVE_DENIED = 1,
128 IAPP_MOVE_STALE_MOVE = 2,
133 struct iapp_cache_notify {
134 u8 addr_len; /* ETH_ALEN */
136 u8 mac_addr[ETH_ALEN];
138 u8 current_ap[ETH_ALEN];
140 /* ctx_block_len bytes of context block followed by 16-bit context
142 } __attribute__ ((packed));
145 /* CACHE-response - unicast TCP */
146 struct iapp_cache_response {
147 u8 addr_len; /* ETH_ALEN */
149 u8 mac_addr[ETH_ALEN];
151 } __attribute__ ((packed));
154 IAPP_CACHE_SUCCESSFUL = 0,
155 IAPP_CACHE_STALE_CACHE = 1,
159 /* Send-Security-Block - unicast TCP */
160 struct iapp_send_security_block {
163 /* followed by sec_block_len bytes of security block */
164 } __attribute__ ((packed));
167 /* ACK-Security-Block - unicast TCP */
168 struct iapp_ack_security_block {
170 u8 new_ap_ack_authenticator[48];
171 } __attribute__ ((packed));
175 struct hostapd_data *hapd;
176 u16 identifier; /* next IAPP identifier */
177 struct in_addr own, multicast;
183 static void iapp_send_add(struct iapp_data *iapp, u8 *mac_addr, u16 seq_num)
186 struct iapp_hdr *hdr;
187 struct iapp_add_notify *add;
188 struct sockaddr_in addr;
190 /* Send IAPP ADD-notify to remove possible association from other APs
193 hdr = (struct iapp_hdr *) buf;
194 hdr->version = IAPP_VERSION;
195 hdr->command = IAPP_CMD_ADD_notify;
196 hdr->identifier = host_to_be16(iapp->identifier++);
197 hdr->length = host_to_be16(sizeof(*hdr) + sizeof(*add));
199 add = (struct iapp_add_notify *) (hdr + 1);
200 add->addr_len = ETH_ALEN;
202 os_memcpy(add->mac_addr, mac_addr, ETH_ALEN);
204 add->seq_num = host_to_be16(seq_num);
206 os_memset(&addr, 0, sizeof(addr));
207 addr.sin_family = AF_INET;
208 addr.sin_addr.s_addr = iapp->multicast.s_addr;
209 addr.sin_port = htons(IAPP_UDP_PORT);
210 if (sendto(iapp->udp_sock, buf, (char *) (add + 1) - buf, 0,
211 (struct sockaddr *) &addr, sizeof(addr)) < 0)
212 perror("sendto[IAPP-ADD]");
216 static void iapp_send_layer2_update(struct iapp_data *iapp, u8 *addr)
218 struct iapp_layer2_update msg;
220 /* Send Level 2 Update Frame to update forwarding tables in layer 2
223 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
224 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
226 os_memset(msg.da, 0xff, ETH_ALEN);
227 os_memcpy(msg.sa, addr, ETH_ALEN);
228 msg.len = host_to_be16(6);
229 msg.dsap = 0; /* NULL DSAP address */
230 msg.ssap = 0x01; /* NULL SSAP address, CR Bit: Response */
231 msg.control = 0xaf; /* XID response lsb.1111F101.
232 * F=0 (no poll command; unsolicited frame) */
233 msg.xid_info[0] = 0x81; /* XID format identifier */
234 msg.xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
235 msg.xid_info[2] = 1 << 1; /* XID sender's receive window size (RW)
236 * FIX: what is correct RW with 802.11? */
238 if (send(iapp->packet_sock, &msg, sizeof(msg), 0) < 0)
239 perror("send[L2 Update]");
244 * iapp_new_station - IAPP processing for a new STA
246 * @sta: The associated station
248 void iapp_new_station(struct iapp_data *iapp, struct sta_info *sta)
250 struct ieee80211_mgmt *assoc;
256 assoc = sta->last_assoc_req;
257 seq = assoc ? WLAN_GET_SEQ_SEQ(le_to_host16(assoc->seq_ctrl)) : 0;
259 /* IAPP-ADD.request(MAC Address, Sequence Number, Timeout) */
260 hostapd_logger(iapp->hapd, sta->addr, HOSTAPD_MODULE_IAPP,
261 HOSTAPD_LEVEL_DEBUG, "IAPP-ADD.request(seq=%d)", seq);
262 iapp_send_layer2_update(iapp, sta->addr);
263 iapp_send_add(iapp, sta->addr, seq);
265 if (assoc && WLAN_FC_GET_STYPE(le_to_host16(assoc->frame_control)) ==
266 WLAN_FC_STYPE_REASSOC_REQ) {
267 /* IAPP-MOVE.request(MAC Address, Sequence Number, Old AP,
268 * Context Block, Timeout)
270 /* TODO: Send IAPP-MOVE to the old AP; Map Old AP BSSID to
276 static void iapp_process_add_notify(struct iapp_data *iapp,
277 struct sockaddr_in *from,
278 struct iapp_hdr *hdr, int len)
280 struct iapp_add_notify *add = (struct iapp_add_notify *) (hdr + 1);
281 struct sta_info *sta;
283 if (len != sizeof(*add)) {
284 printf("Invalid IAPP-ADD packet length %d (expected %lu)\n",
285 len, (unsigned long) sizeof(*add));
289 sta = ap_get_sta(iapp->hapd, add->mac_addr);
291 /* IAPP-ADD.indication(MAC Address, Sequence Number) */
292 hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
294 "Received IAPP ADD-notify (seq# %d) from %s:%d%s",
295 be_to_host16(add->seq_num),
296 inet_ntoa(from->sin_addr), ntohs(from->sin_port),
297 sta ? "" : " (STA not found)");
302 /* TODO: could use seq_num to try to determine whether last association
303 * to this AP is newer than the one advertised in IAPP-ADD. Although,
304 * this is not really a reliable verification. */
306 hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
308 "Removing STA due to IAPP ADD-notify");
309 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
310 eloop_cancel_timeout(ap_handle_timer, iapp->hapd, sta);
311 eloop_register_timeout(0, 0, ap_handle_timer, iapp->hapd, sta);
312 sta->timeout_next = STA_REMOVE;
317 * iapp_receive_udp - Process IAPP UDP frames
318 * @sock: File descriptor for the socket
319 * @eloop_ctx: IAPP data (struct iapp_data *)
320 * @sock_ctx: Not used
322 static void iapp_receive_udp(int sock, void *eloop_ctx, void *sock_ctx)
324 struct iapp_data *iapp = eloop_ctx;
326 unsigned char buf[128];
327 struct sockaddr_in from;
329 struct iapp_hdr *hdr;
331 /* Handle incoming IAPP frames (over UDP/IP) */
333 fromlen = sizeof(from);
334 len = recvfrom(iapp->udp_sock, buf, sizeof(buf), 0,
335 (struct sockaddr *) &from, &fromlen);
341 if (from.sin_addr.s_addr == iapp->own.s_addr)
342 return; /* ignore own IAPP messages */
344 hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
346 "Received %d byte IAPP frame from %s%s\n",
347 len, inet_ntoa(from.sin_addr),
348 len < (int) sizeof(*hdr) ? " (too short)" : "");
350 if (len < (int) sizeof(*hdr))
353 hdr = (struct iapp_hdr *) buf;
354 hlen = be_to_host16(hdr->length);
355 hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
357 "RX: version=%d command=%d id=%d len=%d\n",
358 hdr->version, hdr->command,
359 be_to_host16(hdr->identifier), hlen);
360 if (hdr->version != IAPP_VERSION) {
361 printf("Dropping IAPP frame with unknown version %d\n",
366 printf("Underflow IAPP frame (hlen=%d len=%d)\n", hlen, len);
370 printf("Ignoring %d extra bytes from IAPP frame\n",
375 switch (hdr->command) {
376 case IAPP_CMD_ADD_notify:
377 iapp_process_add_notify(iapp, &from, hdr, hlen - sizeof(*hdr));
379 case IAPP_CMD_MOVE_notify:
380 /* TODO: MOVE is using TCP; so move this to TCP handler once it
381 * is implemented.. */
382 /* IAPP-MOVE.indication(MAC Address, New BSSID,
383 * Sequence Number, AP Address, Context Block) */
387 printf("Unknown IAPP command %d\n", hdr->command);
393 struct iapp_data * iapp_init(struct hostapd_data *hapd, const char *iface)
396 struct sockaddr_ll addr;
398 struct sockaddr_in *paddr, uaddr;
399 struct iapp_data *iapp;
400 struct ip_mreqn mreq;
402 iapp = os_zalloc(sizeof(*iapp));
406 iapp->udp_sock = iapp->packet_sock = -1;
409 * open socket for sending and receiving IAPP frames over TCP
412 iapp->udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
413 if (iapp->udp_sock < 0) {
414 perror("socket[PF_INET,SOCK_DGRAM]");
419 os_memset(&ifr, 0, sizeof(ifr));
420 os_strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
421 if (ioctl(iapp->udp_sock, SIOCGIFINDEX, &ifr) != 0) {
422 perror("ioctl(SIOCGIFINDEX)");
426 ifindex = ifr.ifr_ifindex;
428 if (ioctl(iapp->udp_sock, SIOCGIFADDR, &ifr) != 0) {
429 perror("ioctl(SIOCGIFADDR)");
433 paddr = (struct sockaddr_in *) &ifr.ifr_addr;
434 if (paddr->sin_family != AF_INET) {
435 printf("Invalid address family %i (SIOCGIFADDR)\n",
440 iapp->own.s_addr = paddr->sin_addr.s_addr;
442 if (ioctl(iapp->udp_sock, SIOCGIFBRDADDR, &ifr) != 0) {
443 perror("ioctl(SIOCGIFBRDADDR)");
447 paddr = (struct sockaddr_in *) &ifr.ifr_addr;
448 if (paddr->sin_family != AF_INET) {
449 printf("Invalid address family %i (SIOCGIFBRDADDR)\n",
454 inet_aton(IAPP_MULTICAST, &iapp->multicast);
456 os_memset(&uaddr, 0, sizeof(uaddr));
457 uaddr.sin_family = AF_INET;
458 uaddr.sin_port = htons(IAPP_UDP_PORT);
459 if (bind(iapp->udp_sock, (struct sockaddr *) &uaddr,
460 sizeof(uaddr)) < 0) {
466 os_memset(&mreq, 0, sizeof(mreq));
467 mreq.imr_multiaddr = iapp->multicast;
468 mreq.imr_address.s_addr = INADDR_ANY;
469 mreq.imr_ifindex = 0;
470 if (setsockopt(iapp->udp_sock, SOL_IP, IP_ADD_MEMBERSHIP, &mreq,
472 perror("setsockopt[UDP,IP_ADD_MEMBERSHIP]");
477 iapp->packet_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
478 if (iapp->packet_sock < 0) {
479 perror("socket[PF_PACKET,SOCK_RAW]");
484 os_memset(&addr, 0, sizeof(addr));
485 addr.sll_family = AF_PACKET;
486 addr.sll_ifindex = ifindex;
487 if (bind(iapp->packet_sock, (struct sockaddr *) &addr,
489 perror("bind[PACKET]");
494 if (eloop_register_read_sock(iapp->udp_sock, iapp_receive_udp,
496 printf("Could not register read socket for IAPP.\n");
501 printf("IEEE 802.11F (IAPP) using interface %s\n", iface);
503 /* TODO: For levels 2 and 3: send RADIUS Initiate-Request, receive
504 * RADIUS Initiate-Accept or Initiate-Reject. IAPP port should actually
505 * be openned only after receiving Initiate-Accept. If Initiate-Reject
506 * is received, IAPP is not started. */
512 void iapp_deinit(struct iapp_data *iapp)
514 struct ip_mreqn mreq;
519 if (iapp->udp_sock >= 0) {
520 os_memset(&mreq, 0, sizeof(mreq));
521 mreq.imr_multiaddr = iapp->multicast;
522 mreq.imr_address.s_addr = INADDR_ANY;
523 mreq.imr_ifindex = 0;
524 if (setsockopt(iapp->udp_sock, SOL_IP, IP_DROP_MEMBERSHIP,
525 &mreq, sizeof(mreq)) < 0) {
526 perror("setsockopt[UDP,IP_DEL_MEMBERSHIP]");
529 eloop_unregister_read_sock(iapp->udp_sock);
530 close(iapp->udp_sock);
532 if (iapp->packet_sock >= 0) {
533 eloop_unregister_read_sock(iapp->packet_sock);
534 close(iapp->packet_sock);