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 ap_sta_disconnect(iapp->hapd, sta, NULL, 0);
314 * iapp_receive_udp - Process IAPP UDP frames
315 * @sock: File descriptor for the socket
316 * @eloop_ctx: IAPP data (struct iapp_data *)
317 * @sock_ctx: Not used
319 static void iapp_receive_udp(int sock, void *eloop_ctx, void *sock_ctx)
321 struct iapp_data *iapp = eloop_ctx;
323 unsigned char buf[128];
324 struct sockaddr_in from;
326 struct iapp_hdr *hdr;
328 /* Handle incoming IAPP frames (over UDP/IP) */
330 fromlen = sizeof(from);
331 len = recvfrom(iapp->udp_sock, buf, sizeof(buf), 0,
332 (struct sockaddr *) &from, &fromlen);
338 if (from.sin_addr.s_addr == iapp->own.s_addr)
339 return; /* ignore own IAPP messages */
341 hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
343 "Received %d byte IAPP frame from %s%s\n",
344 len, inet_ntoa(from.sin_addr),
345 len < (int) sizeof(*hdr) ? " (too short)" : "");
347 if (len < (int) sizeof(*hdr))
350 hdr = (struct iapp_hdr *) buf;
351 hlen = be_to_host16(hdr->length);
352 hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
354 "RX: version=%d command=%d id=%d len=%d\n",
355 hdr->version, hdr->command,
356 be_to_host16(hdr->identifier), hlen);
357 if (hdr->version != IAPP_VERSION) {
358 printf("Dropping IAPP frame with unknown version %d\n",
363 printf("Underflow IAPP frame (hlen=%d len=%d)\n", hlen, len);
367 printf("Ignoring %d extra bytes from IAPP frame\n",
372 switch (hdr->command) {
373 case IAPP_CMD_ADD_notify:
374 iapp_process_add_notify(iapp, &from, hdr, hlen - sizeof(*hdr));
376 case IAPP_CMD_MOVE_notify:
377 /* TODO: MOVE is using TCP; so move this to TCP handler once it
378 * is implemented.. */
379 /* IAPP-MOVE.indication(MAC Address, New BSSID,
380 * Sequence Number, AP Address, Context Block) */
384 printf("Unknown IAPP command %d\n", hdr->command);
390 struct iapp_data * iapp_init(struct hostapd_data *hapd, const char *iface)
393 struct sockaddr_ll addr;
395 struct sockaddr_in *paddr, uaddr;
396 struct iapp_data *iapp;
397 struct ip_mreqn mreq;
399 iapp = os_zalloc(sizeof(*iapp));
403 iapp->udp_sock = iapp->packet_sock = -1;
406 * open socket for sending and receiving IAPP frames over TCP
409 iapp->udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
410 if (iapp->udp_sock < 0) {
411 perror("socket[PF_INET,SOCK_DGRAM]");
416 os_memset(&ifr, 0, sizeof(ifr));
417 os_strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
418 if (ioctl(iapp->udp_sock, SIOCGIFINDEX, &ifr) != 0) {
419 perror("ioctl(SIOCGIFINDEX)");
423 ifindex = ifr.ifr_ifindex;
425 if (ioctl(iapp->udp_sock, SIOCGIFADDR, &ifr) != 0) {
426 perror("ioctl(SIOCGIFADDR)");
430 paddr = (struct sockaddr_in *) &ifr.ifr_addr;
431 if (paddr->sin_family != AF_INET) {
432 printf("Invalid address family %i (SIOCGIFADDR)\n",
437 iapp->own.s_addr = paddr->sin_addr.s_addr;
439 if (ioctl(iapp->udp_sock, SIOCGIFBRDADDR, &ifr) != 0) {
440 perror("ioctl(SIOCGIFBRDADDR)");
444 paddr = (struct sockaddr_in *) &ifr.ifr_addr;
445 if (paddr->sin_family != AF_INET) {
446 printf("Invalid address family %i (SIOCGIFBRDADDR)\n",
451 inet_aton(IAPP_MULTICAST, &iapp->multicast);
453 os_memset(&uaddr, 0, sizeof(uaddr));
454 uaddr.sin_family = AF_INET;
455 uaddr.sin_port = htons(IAPP_UDP_PORT);
456 if (bind(iapp->udp_sock, (struct sockaddr *) &uaddr,
457 sizeof(uaddr)) < 0) {
463 os_memset(&mreq, 0, sizeof(mreq));
464 mreq.imr_multiaddr = iapp->multicast;
465 mreq.imr_address.s_addr = INADDR_ANY;
466 mreq.imr_ifindex = 0;
467 if (setsockopt(iapp->udp_sock, SOL_IP, IP_ADD_MEMBERSHIP, &mreq,
469 perror("setsockopt[UDP,IP_ADD_MEMBERSHIP]");
474 iapp->packet_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
475 if (iapp->packet_sock < 0) {
476 perror("socket[PF_PACKET,SOCK_RAW]");
481 os_memset(&addr, 0, sizeof(addr));
482 addr.sll_family = AF_PACKET;
483 addr.sll_ifindex = ifindex;
484 if (bind(iapp->packet_sock, (struct sockaddr *) &addr,
486 perror("bind[PACKET]");
491 if (eloop_register_read_sock(iapp->udp_sock, iapp_receive_udp,
493 printf("Could not register read socket for IAPP.\n");
498 printf("IEEE 802.11F (IAPP) using interface %s\n", iface);
500 /* TODO: For levels 2 and 3: send RADIUS Initiate-Request, receive
501 * RADIUS Initiate-Accept or Initiate-Reject. IAPP port should actually
502 * be openned only after receiving Initiate-Accept. If Initiate-Reject
503 * is received, IAPP is not started. */
509 void iapp_deinit(struct iapp_data *iapp)
511 struct ip_mreqn mreq;
516 if (iapp->udp_sock >= 0) {
517 os_memset(&mreq, 0, sizeof(mreq));
518 mreq.imr_multiaddr = iapp->multicast;
519 mreq.imr_address.s_addr = INADDR_ANY;
520 mreq.imr_ifindex = 0;
521 if (setsockopt(iapp->udp_sock, SOL_IP, IP_DROP_MEMBERSHIP,
522 &mreq, sizeof(mreq)) < 0) {
523 perror("setsockopt[UDP,IP_DEL_MEMBERSHIP]");
526 eloop_unregister_read_sock(iapp->udp_sock);
527 close(iapp->udp_sock);
529 if (iapp->packet_sock >= 0) {
530 eloop_unregister_read_sock(iapp->packet_sock);
531 close(iapp->packet_sock);