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 */
51 #include "ieee802_11.h"
57 #define IAPP_MULTICAST "224.0.1.178"
58 #define IAPP_UDP_PORT 3517
59 #define IAPP_TCP_PORT 3517
66 /* followed by length-6 octets of data */
67 } __attribute__ ((packed));
69 #define IAPP_VERSION 0
72 IAPP_CMD_ADD_notify = 0,
73 IAPP_CMD_MOVE_notify = 1,
74 IAPP_CMD_MOVE_response = 2,
75 IAPP_CMD_Send_Security_Block = 3,
76 IAPP_CMD_ACK_Security_Block = 4,
77 IAPP_CMD_CACHE_notify = 5,
78 IAPP_CMD_CACHE_response = 6,
82 /* ADD-notify - multicast UDP on the local LAN */
83 struct iapp_add_notify {
84 u8 addr_len; /* ETH_ALEN */
86 u8 mac_addr[ETH_ALEN];
88 } __attribute__ ((packed));
91 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
92 struct iapp_layer2_update {
93 u8 da[ETH_ALEN]; /* broadcast */
94 u8 sa[ETH_ALEN]; /* STA addr */
96 u8 dsap; /* null DSAP address */
97 u8 ssap; /* null SSAP address, CR=Response */
100 } __attribute__ ((packed));
103 /* MOVE-notify - unicast TCP */
104 struct iapp_move_notify {
105 u8 addr_len; /* ETH_ALEN */
107 u8 mac_addr[ETH_ALEN];
110 /* followed by ctx_block_len bytes */
111 } __attribute__ ((packed));
114 /* MOVE-response - unicast TCP */
115 struct iapp_move_response {
116 u8 addr_len; /* ETH_ALEN */
118 u8 mac_addr[ETH_ALEN];
121 /* followed by ctx_block_len bytes */
122 } __attribute__ ((packed));
125 IAPP_MOVE_SUCCESSFUL = 0,
126 IAPP_MOVE_DENIED = 1,
127 IAPP_MOVE_STALE_MOVE = 2,
132 struct iapp_cache_notify {
133 u8 addr_len; /* ETH_ALEN */
135 u8 mac_addr[ETH_ALEN];
137 u8 current_ap[ETH_ALEN];
139 /* ctx_block_len bytes of context block followed by 16-bit context
141 } __attribute__ ((packed));
144 /* CACHE-response - unicast TCP */
145 struct iapp_cache_response {
146 u8 addr_len; /* ETH_ALEN */
148 u8 mac_addr[ETH_ALEN];
150 } __attribute__ ((packed));
153 IAPP_CACHE_SUCCESSFUL = 0,
154 IAPP_CACHE_STALE_CACHE = 1,
158 /* Send-Security-Block - unicast TCP */
159 struct iapp_send_security_block {
162 /* followed by sec_block_len bytes of security block */
163 } __attribute__ ((packed));
166 /* ACK-Security-Block - unicast TCP */
167 struct iapp_ack_security_block {
169 u8 new_ap_ack_authenticator[48];
170 } __attribute__ ((packed));
174 struct hostapd_data *hapd;
175 u16 identifier; /* next IAPP identifier */
176 struct in_addr own, multicast;
182 static void iapp_send_add(struct iapp_data *iapp, u8 *mac_addr, u16 seq_num)
185 struct iapp_hdr *hdr;
186 struct iapp_add_notify *add;
187 struct sockaddr_in addr;
189 /* Send IAPP ADD-notify to remove possible association from other APs
192 hdr = (struct iapp_hdr *) buf;
193 hdr->version = IAPP_VERSION;
194 hdr->command = IAPP_CMD_ADD_notify;
195 hdr->identifier = host_to_be16(iapp->identifier++);
196 hdr->length = host_to_be16(sizeof(*hdr) + sizeof(*add));
198 add = (struct iapp_add_notify *) (hdr + 1);
199 add->addr_len = ETH_ALEN;
201 os_memcpy(add->mac_addr, mac_addr, ETH_ALEN);
203 add->seq_num = host_to_be16(seq_num);
205 os_memset(&addr, 0, sizeof(addr));
206 addr.sin_family = AF_INET;
207 addr.sin_addr.s_addr = iapp->multicast.s_addr;
208 addr.sin_port = htons(IAPP_UDP_PORT);
209 if (sendto(iapp->udp_sock, buf, (char *) (add + 1) - buf, 0,
210 (struct sockaddr *) &addr, sizeof(addr)) < 0)
211 perror("sendto[IAPP-ADD]");
215 static void iapp_send_layer2_update(struct iapp_data *iapp, u8 *addr)
217 struct iapp_layer2_update msg;
219 /* Send Level 2 Update Frame to update forwarding tables in layer 2
222 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
223 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
225 os_memset(msg.da, 0xff, ETH_ALEN);
226 os_memcpy(msg.sa, addr, ETH_ALEN);
227 msg.len = host_to_be16(6);
228 msg.dsap = 0; /* NULL DSAP address */
229 msg.ssap = 0x01; /* NULL SSAP address, CR Bit: Response */
230 msg.control = 0xaf; /* XID response lsb.1111F101.
231 * F=0 (no poll command; unsolicited frame) */
232 msg.xid_info[0] = 0x81; /* XID format identifier */
233 msg.xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
234 msg.xid_info[2] = 1 << 1; /* XID sender's receive window size (RW)
235 * FIX: what is correct RW with 802.11? */
237 if (send(iapp->packet_sock, &msg, sizeof(msg), 0) < 0)
238 perror("send[L2 Update]");
243 * iapp_new_station - IAPP processing for a new STA
245 * @sta: The associated station
247 void iapp_new_station(struct iapp_data *iapp, struct sta_info *sta)
249 struct ieee80211_mgmt *assoc;
255 assoc = sta->last_assoc_req;
256 seq = assoc ? WLAN_GET_SEQ_SEQ(le_to_host16(assoc->seq_ctrl)) : 0;
258 /* IAPP-ADD.request(MAC Address, Sequence Number, Timeout) */
259 hostapd_logger(iapp->hapd, sta->addr, HOSTAPD_MODULE_IAPP,
260 HOSTAPD_LEVEL_DEBUG, "IAPP-ADD.request(seq=%d)", seq);
261 iapp_send_layer2_update(iapp, sta->addr);
262 iapp_send_add(iapp, sta->addr, seq);
264 if (assoc && WLAN_FC_GET_STYPE(le_to_host16(assoc->frame_control)) ==
265 WLAN_FC_STYPE_REASSOC_REQ) {
266 /* IAPP-MOVE.request(MAC Address, Sequence Number, Old AP,
267 * Context Block, Timeout)
269 /* TODO: Send IAPP-MOVE to the old AP; Map Old AP BSSID to
275 static void iapp_process_add_notify(struct iapp_data *iapp,
276 struct sockaddr_in *from,
277 struct iapp_hdr *hdr, int len)
279 struct iapp_add_notify *add = (struct iapp_add_notify *) (hdr + 1);
280 struct sta_info *sta;
282 if (len != sizeof(*add)) {
283 printf("Invalid IAPP-ADD packet length %d (expected %lu)\n",
284 len, (unsigned long) sizeof(*add));
288 sta = ap_get_sta(iapp->hapd, add->mac_addr);
290 /* IAPP-ADD.indication(MAC Address, Sequence Number) */
291 hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
293 "Received IAPP ADD-notify (seq# %d) from %s:%d%s",
294 be_to_host16(add->seq_num),
295 inet_ntoa(from->sin_addr), ntohs(from->sin_port),
296 sta ? "" : " (STA not found)");
301 /* TODO: could use seq_num to try to determine whether last association
302 * to this AP is newer than the one advertised in IAPP-ADD. Although,
303 * this is not really a reliable verification. */
305 hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
307 "Removing STA due to IAPP ADD-notify");
308 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
309 eloop_cancel_timeout(ap_handle_timer, iapp->hapd, sta);
310 eloop_register_timeout(0, 0, ap_handle_timer, iapp->hapd, sta);
311 sta->timeout_next = STA_REMOVE;
316 * iapp_receive_udp - Process IAPP UDP frames
317 * @sock: File descriptor for the socket
318 * @eloop_ctx: IAPP data (struct iapp_data *)
319 * @sock_ctx: Not used
321 static void iapp_receive_udp(int sock, void *eloop_ctx, void *sock_ctx)
323 struct iapp_data *iapp = eloop_ctx;
325 unsigned char buf[128];
326 struct sockaddr_in from;
328 struct iapp_hdr *hdr;
330 /* Handle incoming IAPP frames (over UDP/IP) */
332 fromlen = sizeof(from);
333 len = recvfrom(iapp->udp_sock, buf, sizeof(buf), 0,
334 (struct sockaddr *) &from, &fromlen);
340 if (from.sin_addr.s_addr == iapp->own.s_addr)
341 return; /* ignore own IAPP messages */
343 hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
345 "Received %d byte IAPP frame from %s%s\n",
346 len, inet_ntoa(from.sin_addr),
347 len < (int) sizeof(*hdr) ? " (too short)" : "");
349 if (len < (int) sizeof(*hdr))
352 hdr = (struct iapp_hdr *) buf;
353 hlen = be_to_host16(hdr->length);
354 hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
356 "RX: version=%d command=%d id=%d len=%d\n",
357 hdr->version, hdr->command,
358 be_to_host16(hdr->identifier), hlen);
359 if (hdr->version != IAPP_VERSION) {
360 printf("Dropping IAPP frame with unknown version %d\n",
365 printf("Underflow IAPP frame (hlen=%d len=%d)\n", hlen, len);
369 printf("Ignoring %d extra bytes from IAPP frame\n",
374 switch (hdr->command) {
375 case IAPP_CMD_ADD_notify:
376 iapp_process_add_notify(iapp, &from, hdr, hlen - sizeof(*hdr));
378 case IAPP_CMD_MOVE_notify:
379 /* TODO: MOVE is using TCP; so move this to TCP handler once it
380 * is implemented.. */
381 /* IAPP-MOVE.indication(MAC Address, New BSSID,
382 * Sequence Number, AP Address, Context Block) */
386 printf("Unknown IAPP command %d\n", hdr->command);
392 struct iapp_data * iapp_init(struct hostapd_data *hapd, const char *iface)
395 struct sockaddr_ll addr;
397 struct sockaddr_in *paddr, uaddr;
398 struct iapp_data *iapp;
399 struct ip_mreqn mreq;
401 iapp = os_zalloc(sizeof(*iapp));
405 iapp->udp_sock = iapp->packet_sock = -1;
408 * open socket for sending and receiving IAPP frames over TCP
411 iapp->udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
412 if (iapp->udp_sock < 0) {
413 perror("socket[PF_INET,SOCK_DGRAM]");
418 os_memset(&ifr, 0, sizeof(ifr));
419 os_strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
420 if (ioctl(iapp->udp_sock, SIOCGIFINDEX, &ifr) != 0) {
421 perror("ioctl(SIOCGIFINDEX)");
425 ifindex = ifr.ifr_ifindex;
427 if (ioctl(iapp->udp_sock, SIOCGIFADDR, &ifr) != 0) {
428 perror("ioctl(SIOCGIFADDR)");
432 paddr = (struct sockaddr_in *) &ifr.ifr_addr;
433 if (paddr->sin_family != AF_INET) {
434 printf("Invalid address family %i (SIOCGIFADDR)\n",
439 iapp->own.s_addr = paddr->sin_addr.s_addr;
441 if (ioctl(iapp->udp_sock, SIOCGIFBRDADDR, &ifr) != 0) {
442 perror("ioctl(SIOCGIFBRDADDR)");
446 paddr = (struct sockaddr_in *) &ifr.ifr_addr;
447 if (paddr->sin_family != AF_INET) {
448 printf("Invalid address family %i (SIOCGIFBRDADDR)\n",
453 inet_aton(IAPP_MULTICAST, &iapp->multicast);
455 os_memset(&uaddr, 0, sizeof(uaddr));
456 uaddr.sin_family = AF_INET;
457 uaddr.sin_port = htons(IAPP_UDP_PORT);
458 if (bind(iapp->udp_sock, (struct sockaddr *) &uaddr,
459 sizeof(uaddr)) < 0) {
465 os_memset(&mreq, 0, sizeof(mreq));
466 mreq.imr_multiaddr = iapp->multicast;
467 mreq.imr_address.s_addr = INADDR_ANY;
468 mreq.imr_ifindex = 0;
469 if (setsockopt(iapp->udp_sock, SOL_IP, IP_ADD_MEMBERSHIP, &mreq,
471 perror("setsockopt[UDP,IP_ADD_MEMBERSHIP]");
476 iapp->packet_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
477 if (iapp->packet_sock < 0) {
478 perror("socket[PF_PACKET,SOCK_RAW]");
483 os_memset(&addr, 0, sizeof(addr));
484 addr.sll_family = AF_PACKET;
485 addr.sll_ifindex = ifindex;
486 if (bind(iapp->packet_sock, (struct sockaddr *) &addr,
488 perror("bind[PACKET]");
493 if (eloop_register_read_sock(iapp->udp_sock, iapp_receive_udp,
495 printf("Could not register read socket for IAPP.\n");
500 printf("IEEE 802.11F (IAPP) using interface %s\n", iface);
502 /* TODO: For levels 2 and 3: send RADIUS Initiate-Request, receive
503 * RADIUS Initiate-Accept or Initiate-Reject. IAPP port should actually
504 * be openned only after receiving Initiate-Accept. If Initiate-Reject
505 * is received, IAPP is not started. */
511 void iapp_deinit(struct iapp_data *iapp)
513 struct ip_mreqn mreq;
518 if (iapp->udp_sock >= 0) {
519 os_memset(&mreq, 0, sizeof(mreq));
520 mreq.imr_multiaddr = iapp->multicast;
521 mreq.imr_address.s_addr = INADDR_ANY;
522 mreq.imr_ifindex = 0;
523 if (setsockopt(iapp->udp_sock, SOL_IP, IP_DROP_MEMBERSHIP,
524 &mreq, sizeof(mreq)) < 0) {
525 perror("setsockopt[UDP,IP_DEL_MEMBERSHIP]");
528 eloop_unregister_read_sock(iapp->udp_sock);
529 close(iapp->udp_sock);
531 if (iapp->packet_sock >= 0) {
532 eloop_unregister_read_sock(iapp->packet_sock);
533 close(iapp->packet_sock);
538 int iapp_reconfig(struct hostapd_data *hapd, struct hostapd_config *oldconf,
539 struct hostapd_bss_config *oldbss)
541 if (hapd->conf->ieee802_11f != oldbss->ieee802_11f ||
542 os_strcmp(hapd->conf->iapp_iface, oldbss->iapp_iface) != 0) {
543 iapp_deinit(hapd->iapp);
546 if (hapd->conf->ieee802_11f) {
547 hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface);
548 if (hapd->iapp == NULL)