2 * hostapd / IEEE 802.11F-2003 Inter-Access Point Protocol (IAPP)
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
8 * Note: IEEE 802.11F-2003 was a experimental use specification. It has expired
9 * and IEEE has withdrawn it. In other words, it is likely better to look at
10 * using some other mechanism for AP-to-AP communication than extending the
11 * implementation here.
15 * Level 1: no administrative or security support
16 * (e.g., static BSSID to IP address mapping in each AP)
17 * Level 2: support for dynamic mapping of BSSID to IP address
18 * Level 3: support for encryption and authentication of IAPP messages
19 * - add support for MOVE-notify and MOVE-response (this requires support for
20 * finding out IP address for previous AP using RADIUS)
21 * - add support for Send- and ACK-Security-Block to speedup IEEE 802.1X during
22 * reassociation to another AP
23 * - implement counters etc. for IAPP MIB
24 * - verify endianness of fields in IAPP messages; are they big-endian as
26 * - RADIUS connection for AP registration and BSSID to IP address mapping
27 * - TCP connection for IAPP MOVE, CACHE
28 * - broadcast ESP for IAPP ADD-notify
29 * - ESP for IAPP MOVE messages
30 * - security block sending/processing
31 * - IEEE 802.11 context transfer
34 #include "utils/includes.h"
36 #include <sys/ioctl.h>
37 #include <netpacket/packet.h>
39 #include "utils/common.h"
40 #include "utils/eloop.h"
41 #include "common/ieee802_11_defs.h"
43 #include "ap_config.h"
44 #include "ieee802_11.h"
49 #define IAPP_MULTICAST "224.0.1.178"
50 #define IAPP_UDP_PORT 3517
51 #define IAPP_TCP_PORT 3517
58 /* followed by length-6 octets of data */
59 } __attribute__ ((packed));
61 #define IAPP_VERSION 0
64 IAPP_CMD_ADD_notify = 0,
65 IAPP_CMD_MOVE_notify = 1,
66 IAPP_CMD_MOVE_response = 2,
67 IAPP_CMD_Send_Security_Block = 3,
68 IAPP_CMD_ACK_Security_Block = 4,
69 IAPP_CMD_CACHE_notify = 5,
70 IAPP_CMD_CACHE_response = 6,
74 /* ADD-notify - multicast UDP on the local LAN */
75 struct iapp_add_notify {
76 u8 addr_len; /* ETH_ALEN */
78 u8 mac_addr[ETH_ALEN];
80 } __attribute__ ((packed));
83 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
84 struct iapp_layer2_update {
85 u8 da[ETH_ALEN]; /* broadcast */
86 u8 sa[ETH_ALEN]; /* STA addr */
88 u8 dsap; /* null DSAP address */
89 u8 ssap; /* null SSAP address, CR=Response */
92 } __attribute__ ((packed));
95 /* MOVE-notify - unicast TCP */
96 struct iapp_move_notify {
97 u8 addr_len; /* ETH_ALEN */
99 u8 mac_addr[ETH_ALEN];
102 /* followed by ctx_block_len bytes */
103 } __attribute__ ((packed));
106 /* MOVE-response - unicast TCP */
107 struct iapp_move_response {
108 u8 addr_len; /* ETH_ALEN */
110 u8 mac_addr[ETH_ALEN];
113 /* followed by ctx_block_len bytes */
114 } __attribute__ ((packed));
117 IAPP_MOVE_SUCCESSFUL = 0,
118 IAPP_MOVE_DENIED = 1,
119 IAPP_MOVE_STALE_MOVE = 2,
124 struct iapp_cache_notify {
125 u8 addr_len; /* ETH_ALEN */
127 u8 mac_addr[ETH_ALEN];
129 u8 current_ap[ETH_ALEN];
131 /* ctx_block_len bytes of context block followed by 16-bit context
133 } __attribute__ ((packed));
136 /* CACHE-response - unicast TCP */
137 struct iapp_cache_response {
138 u8 addr_len; /* ETH_ALEN */
140 u8 mac_addr[ETH_ALEN];
142 } __attribute__ ((packed));
145 IAPP_CACHE_SUCCESSFUL = 0,
146 IAPP_CACHE_STALE_CACHE = 1,
150 /* Send-Security-Block - unicast TCP */
151 struct iapp_send_security_block {
154 /* followed by sec_block_len bytes of security block */
155 } __attribute__ ((packed));
158 /* ACK-Security-Block - unicast TCP */
159 struct iapp_ack_security_block {
161 u8 new_ap_ack_authenticator[48];
162 } __attribute__ ((packed));
166 struct hostapd_data *hapd;
167 u16 identifier; /* next IAPP identifier */
168 struct in_addr own, multicast;
174 static void iapp_send_add(struct iapp_data *iapp, u8 *mac_addr, u16 seq_num)
177 struct iapp_hdr *hdr;
178 struct iapp_add_notify *add;
179 struct sockaddr_in addr;
181 /* Send IAPP ADD-notify to remove possible association from other APs
184 hdr = (struct iapp_hdr *) buf;
185 hdr->version = IAPP_VERSION;
186 hdr->command = IAPP_CMD_ADD_notify;
187 hdr->identifier = host_to_be16(iapp->identifier++);
188 hdr->length = host_to_be16(sizeof(*hdr) + sizeof(*add));
190 add = (struct iapp_add_notify *) (hdr + 1);
191 add->addr_len = ETH_ALEN;
193 os_memcpy(add->mac_addr, mac_addr, ETH_ALEN);
195 add->seq_num = host_to_be16(seq_num);
197 os_memset(&addr, 0, sizeof(addr));
198 addr.sin_family = AF_INET;
199 addr.sin_addr.s_addr = iapp->multicast.s_addr;
200 addr.sin_port = htons(IAPP_UDP_PORT);
201 if (sendto(iapp->udp_sock, buf, (char *) (add + 1) - buf, 0,
202 (struct sockaddr *) &addr, sizeof(addr)) < 0)
203 wpa_printf(MSG_INFO, "sendto[IAPP-ADD]: %s", strerror(errno));
207 static void iapp_send_layer2_update(struct iapp_data *iapp, u8 *addr)
209 struct iapp_layer2_update msg;
211 /* Send Level 2 Update Frame to update forwarding tables in layer 2
214 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
215 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
217 os_memset(msg.da, 0xff, ETH_ALEN);
218 os_memcpy(msg.sa, addr, ETH_ALEN);
219 msg.len = host_to_be16(6);
220 msg.dsap = 0; /* NULL DSAP address */
221 msg.ssap = 0x01; /* NULL SSAP address, CR Bit: Response */
222 msg.control = 0xaf; /* XID response lsb.1111F101.
223 * F=0 (no poll command; unsolicited frame) */
224 msg.xid_info[0] = 0x81; /* XID format identifier */
225 msg.xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
226 msg.xid_info[2] = 1 << 1; /* XID sender's receive window size (RW)
227 * FIX: what is correct RW with 802.11? */
229 if (send(iapp->packet_sock, &msg, sizeof(msg), 0) < 0)
230 wpa_printf(MSG_INFO, "send[L2 Update]: %s", strerror(errno));
235 * iapp_new_station - IAPP processing for a new STA
237 * @sta: The associated station
239 void iapp_new_station(struct iapp_data *iapp, struct sta_info *sta)
241 u16 seq = 0; /* TODO */
246 /* IAPP-ADD.request(MAC Address, Sequence Number, Timeout) */
247 hostapd_logger(iapp->hapd, sta->addr, HOSTAPD_MODULE_IAPP,
248 HOSTAPD_LEVEL_DEBUG, "IAPP-ADD.request(seq=%d)", seq);
249 iapp_send_layer2_update(iapp, sta->addr);
250 iapp_send_add(iapp, sta->addr, seq);
252 /* TODO: If this was reassociation:
253 * IAPP-MOVE.request(MAC Address, Sequence Number, Old AP,
254 * Context Block, Timeout)
255 * TODO: Send IAPP-MOVE to the old AP; Map Old AP BSSID to
260 static void iapp_process_add_notify(struct iapp_data *iapp,
261 struct sockaddr_in *from,
262 struct iapp_hdr *hdr, int len)
264 struct iapp_add_notify *add = (struct iapp_add_notify *) (hdr + 1);
265 struct sta_info *sta;
267 if (len != sizeof(*add)) {
268 wpa_printf(MSG_INFO, "Invalid IAPP-ADD packet length %d (expected %lu)",
269 len, (unsigned long) sizeof(*add));
273 sta = ap_get_sta(iapp->hapd, add->mac_addr);
275 /* IAPP-ADD.indication(MAC Address, Sequence Number) */
276 hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
278 "Received IAPP ADD-notify (seq# %d) from %s:%d%s",
279 be_to_host16(add->seq_num),
280 inet_ntoa(from->sin_addr), ntohs(from->sin_port),
281 sta ? "" : " (STA not found)");
286 /* TODO: could use seq_num to try to determine whether last association
287 * to this AP is newer than the one advertised in IAPP-ADD. Although,
288 * this is not really a reliable verification. */
290 hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
292 "Removing STA due to IAPP ADD-notify");
293 ap_sta_disconnect(iapp->hapd, sta, NULL, 0);
298 * iapp_receive_udp - Process IAPP UDP frames
299 * @sock: File descriptor for the socket
300 * @eloop_ctx: IAPP data (struct iapp_data *)
301 * @sock_ctx: Not used
303 static void iapp_receive_udp(int sock, void *eloop_ctx, void *sock_ctx)
305 struct iapp_data *iapp = eloop_ctx;
307 unsigned char buf[128];
308 struct sockaddr_in from;
310 struct iapp_hdr *hdr;
312 /* Handle incoming IAPP frames (over UDP/IP) */
314 fromlen = sizeof(from);
315 len = recvfrom(iapp->udp_sock, buf, sizeof(buf), 0,
316 (struct sockaddr *) &from, &fromlen);
318 wpa_printf(MSG_INFO, "iapp_receive_udp - recvfrom: %s",
323 if (from.sin_addr.s_addr == iapp->own.s_addr)
324 return; /* ignore own IAPP messages */
326 hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
328 "Received %d byte IAPP frame from %s%s\n",
329 len, inet_ntoa(from.sin_addr),
330 len < (int) sizeof(*hdr) ? " (too short)" : "");
332 if (len < (int) sizeof(*hdr))
335 hdr = (struct iapp_hdr *) buf;
336 hlen = be_to_host16(hdr->length);
337 hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
339 "RX: version=%d command=%d id=%d len=%d\n",
340 hdr->version, hdr->command,
341 be_to_host16(hdr->identifier), hlen);
342 if (hdr->version != IAPP_VERSION) {
343 wpa_printf(MSG_INFO, "Dropping IAPP frame with unknown version %d",
348 wpa_printf(MSG_INFO, "Underflow IAPP frame (hlen=%d len=%d)",
353 wpa_printf(MSG_INFO, "Ignoring %d extra bytes from IAPP frame",
358 switch (hdr->command) {
359 case IAPP_CMD_ADD_notify:
360 iapp_process_add_notify(iapp, &from, hdr, len - sizeof(*hdr));
362 case IAPP_CMD_MOVE_notify:
363 /* TODO: MOVE is using TCP; so move this to TCP handler once it
364 * is implemented.. */
365 /* IAPP-MOVE.indication(MAC Address, New BSSID,
366 * Sequence Number, AP Address, Context Block) */
370 wpa_printf(MSG_INFO, "Unknown IAPP command %d", hdr->command);
376 struct iapp_data * iapp_init(struct hostapd_data *hapd, const char *iface)
379 struct sockaddr_ll addr;
381 struct sockaddr_in *paddr, uaddr;
382 struct iapp_data *iapp;
383 struct ip_mreqn mreq;
386 iapp = os_zalloc(sizeof(*iapp));
390 iapp->udp_sock = iapp->packet_sock = -1;
393 * open socket for sending and receiving IAPP frames over TCP
396 iapp->udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
397 if (iapp->udp_sock < 0) {
398 wpa_printf(MSG_INFO, "iapp_init - socket[PF_INET,SOCK_DGRAM]: %s",
404 os_memset(&ifr, 0, sizeof(ifr));
405 os_strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
406 if (ioctl(iapp->udp_sock, SIOCGIFINDEX, &ifr) != 0) {
407 wpa_printf(MSG_INFO, "iapp_init - ioctl(SIOCGIFINDEX): %s",
412 ifindex = ifr.ifr_ifindex;
414 if (ioctl(iapp->udp_sock, SIOCGIFADDR, &ifr) != 0) {
415 wpa_printf(MSG_INFO, "iapp_init - ioctl(SIOCGIFADDR): %s",
420 paddr = (struct sockaddr_in *) &ifr.ifr_addr;
421 if (paddr->sin_family != AF_INET) {
422 wpa_printf(MSG_INFO, "IAPP: Invalid address family %i (SIOCGIFADDR)",
427 iapp->own.s_addr = paddr->sin_addr.s_addr;
429 if (ioctl(iapp->udp_sock, SIOCGIFBRDADDR, &ifr) != 0) {
430 wpa_printf(MSG_INFO, "iapp_init - ioctl(SIOCGIFBRDADDR): %s",
435 paddr = (struct sockaddr_in *) &ifr.ifr_addr;
436 if (paddr->sin_family != AF_INET) {
437 wpa_printf(MSG_INFO, "Invalid address family %i (SIOCGIFBRDADDR)",
442 inet_aton(IAPP_MULTICAST, &iapp->multicast);
444 os_memset(&uaddr, 0, sizeof(uaddr));
445 uaddr.sin_family = AF_INET;
446 uaddr.sin_port = htons(IAPP_UDP_PORT);
448 if (setsockopt(iapp->udp_sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
449 sizeof(reuseaddr)) < 0) {
451 "iapp_init - setsockopt[UDP,SO_REUSEADDR]: %s",
454 * Ignore this and try to continue. This is fine for single
455 * BSS cases, but may fail if multiple BSSes enable IAPP.
459 if (bind(iapp->udp_sock, (struct sockaddr *) &uaddr,
460 sizeof(uaddr)) < 0) {
461 wpa_printf(MSG_INFO, "iapp_init - bind[UDP]: %s",
467 os_memset(&mreq, 0, sizeof(mreq));
468 mreq.imr_multiaddr = iapp->multicast;
469 mreq.imr_address.s_addr = INADDR_ANY;
470 mreq.imr_ifindex = 0;
471 if (setsockopt(iapp->udp_sock, SOL_IP, IP_ADD_MEMBERSHIP, &mreq,
473 wpa_printf(MSG_INFO, "iapp_init - setsockopt[UDP,IP_ADD_MEMBERSHIP]: %s",
479 iapp->packet_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
480 if (iapp->packet_sock < 0) {
481 wpa_printf(MSG_INFO, "iapp_init - socket[PF_PACKET,SOCK_RAW]: %s",
487 os_memset(&addr, 0, sizeof(addr));
488 addr.sll_family = AF_PACKET;
489 addr.sll_ifindex = ifindex;
490 if (bind(iapp->packet_sock, (struct sockaddr *) &addr,
492 wpa_printf(MSG_INFO, "iapp_init - bind[PACKET]: %s",
498 if (eloop_register_read_sock(iapp->udp_sock, iapp_receive_udp,
500 wpa_printf(MSG_INFO, "Could not register read socket for IAPP");
505 wpa_printf(MSG_INFO, "IEEE 802.11F (IAPP) using interface %s", iface);
507 /* TODO: For levels 2 and 3: send RADIUS Initiate-Request, receive
508 * RADIUS Initiate-Accept or Initiate-Reject. IAPP port should actually
509 * be openned only after receiving Initiate-Accept. If Initiate-Reject
510 * is received, IAPP is not started. */
516 void iapp_deinit(struct iapp_data *iapp)
518 struct ip_mreqn mreq;
523 if (iapp->udp_sock >= 0) {
524 os_memset(&mreq, 0, sizeof(mreq));
525 mreq.imr_multiaddr = iapp->multicast;
526 mreq.imr_address.s_addr = INADDR_ANY;
527 mreq.imr_ifindex = 0;
528 if (setsockopt(iapp->udp_sock, SOL_IP, IP_DROP_MEMBERSHIP,
529 &mreq, sizeof(mreq)) < 0) {
530 wpa_printf(MSG_INFO, "iapp_deinit - setsockopt[UDP,IP_DEL_MEMBERSHIP]: %s",
534 eloop_unregister_read_sock(iapp->udp_sock);
535 close(iapp->udp_sock);
537 if (iapp->packet_sock >= 0) {
538 eloop_unregister_read_sock(iapp->packet_sock);
539 close(iapp->packet_sock);