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
40 #include "utils/includes.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 "utils/common.h"
50 #include "utils/eloop.h"
51 #include "common/ieee802_11_defs.h"
53 #include "ap_config.h"
54 #include "ieee802_11.h"
59 #define IAPP_MULTICAST "224.0.1.178"
60 #define IAPP_UDP_PORT 3517
61 #define IAPP_TCP_PORT 3517
68 /* followed by length-6 octets of data */
69 } __attribute__ ((packed
));
71 #define IAPP_VERSION 0
74 IAPP_CMD_ADD_notify
= 0,
75 IAPP_CMD_MOVE_notify
= 1,
76 IAPP_CMD_MOVE_response
= 2,
77 IAPP_CMD_Send_Security_Block
= 3,
78 IAPP_CMD_ACK_Security_Block
= 4,
79 IAPP_CMD_CACHE_notify
= 5,
80 IAPP_CMD_CACHE_response
= 6,
84 /* ADD-notify - multicast UDP on the local LAN */
85 struct iapp_add_notify
{
86 u8 addr_len
; /* ETH_ALEN */
88 u8 mac_addr
[ETH_ALEN
];
90 } __attribute__ ((packed
));
93 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
94 struct iapp_layer2_update
{
95 u8 da
[ETH_ALEN
]; /* broadcast */
96 u8 sa
[ETH_ALEN
]; /* STA addr */
98 u8 dsap
; /* null DSAP address */
99 u8 ssap
; /* null SSAP address, CR=Response */
102 } __attribute__ ((packed
));
105 /* MOVE-notify - unicast TCP */
106 struct iapp_move_notify
{
107 u8 addr_len
; /* ETH_ALEN */
109 u8 mac_addr
[ETH_ALEN
];
112 /* followed by ctx_block_len bytes */
113 } __attribute__ ((packed
));
116 /* MOVE-response - unicast TCP */
117 struct iapp_move_response
{
118 u8 addr_len
; /* ETH_ALEN */
120 u8 mac_addr
[ETH_ALEN
];
123 /* followed by ctx_block_len bytes */
124 } __attribute__ ((packed
));
127 IAPP_MOVE_SUCCESSFUL
= 0,
128 IAPP_MOVE_DENIED
= 1,
129 IAPP_MOVE_STALE_MOVE
= 2,
134 struct iapp_cache_notify
{
135 u8 addr_len
; /* ETH_ALEN */
137 u8 mac_addr
[ETH_ALEN
];
139 u8 current_ap
[ETH_ALEN
];
141 /* ctx_block_len bytes of context block followed by 16-bit context
143 } __attribute__ ((packed
));
146 /* CACHE-response - unicast TCP */
147 struct iapp_cache_response
{
148 u8 addr_len
; /* ETH_ALEN */
150 u8 mac_addr
[ETH_ALEN
];
152 } __attribute__ ((packed
));
155 IAPP_CACHE_SUCCESSFUL
= 0,
156 IAPP_CACHE_STALE_CACHE
= 1,
160 /* Send-Security-Block - unicast TCP */
161 struct iapp_send_security_block
{
164 /* followed by sec_block_len bytes of security block */
165 } __attribute__ ((packed
));
168 /* ACK-Security-Block - unicast TCP */
169 struct iapp_ack_security_block
{
171 u8 new_ap_ack_authenticator
[48];
172 } __attribute__ ((packed
));
176 struct hostapd_data
*hapd
;
177 u16 identifier
; /* next IAPP identifier */
178 struct in_addr own
, multicast
;
184 static void iapp_send_add(struct iapp_data
*iapp
, u8
*mac_addr
, u16 seq_num
)
187 struct iapp_hdr
*hdr
;
188 struct iapp_add_notify
*add
;
189 struct sockaddr_in addr
;
191 /* Send IAPP ADD-notify to remove possible association from other APs
194 hdr
= (struct iapp_hdr
*) buf
;
195 hdr
->version
= IAPP_VERSION
;
196 hdr
->command
= IAPP_CMD_ADD_notify
;
197 hdr
->identifier
= host_to_be16(iapp
->identifier
++);
198 hdr
->length
= host_to_be16(sizeof(*hdr
) + sizeof(*add
));
200 add
= (struct iapp_add_notify
*) (hdr
+ 1);
201 add
->addr_len
= ETH_ALEN
;
203 os_memcpy(add
->mac_addr
, mac_addr
, ETH_ALEN
);
205 add
->seq_num
= host_to_be16(seq_num
);
207 os_memset(&addr
, 0, sizeof(addr
));
208 addr
.sin_family
= AF_INET
;
209 addr
.sin_addr
.s_addr
= iapp
->multicast
.s_addr
;
210 addr
.sin_port
= htons(IAPP_UDP_PORT
);
211 if (sendto(iapp
->udp_sock
, buf
, (char *) (add
+ 1) - buf
, 0,
212 (struct sockaddr
*) &addr
, sizeof(addr
)) < 0)
213 perror("sendto[IAPP-ADD]");
217 static void iapp_send_layer2_update(struct iapp_data
*iapp
, u8
*addr
)
219 struct iapp_layer2_update msg
;
221 /* Send Level 2 Update Frame to update forwarding tables in layer 2
224 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
225 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
227 os_memset(msg
.da
, 0xff, ETH_ALEN
);
228 os_memcpy(msg
.sa
, addr
, ETH_ALEN
);
229 msg
.len
= host_to_be16(6);
230 msg
.dsap
= 0; /* NULL DSAP address */
231 msg
.ssap
= 0x01; /* NULL SSAP address, CR Bit: Response */
232 msg
.control
= 0xaf; /* XID response lsb.1111F101.
233 * F=0 (no poll command; unsolicited frame) */
234 msg
.xid_info
[0] = 0x81; /* XID format identifier */
235 msg
.xid_info
[1] = 1; /* LLC types/classes: Type 1 LLC */
236 msg
.xid_info
[2] = 1 << 1; /* XID sender's receive window size (RW)
237 * FIX: what is correct RW with 802.11? */
239 if (send(iapp
->packet_sock
, &msg
, sizeof(msg
), 0) < 0)
240 perror("send[L2 Update]");
245 * iapp_new_station - IAPP processing for a new STA
247 * @sta: The associated station
249 void iapp_new_station(struct iapp_data
*iapp
, struct sta_info
*sta
)
251 struct ieee80211_mgmt
*assoc
;
257 assoc
= sta
->last_assoc_req
;
258 seq
= assoc
? WLAN_GET_SEQ_SEQ(le_to_host16(assoc
->seq_ctrl
)) : 0;
260 /* IAPP-ADD.request(MAC Address, Sequence Number, Timeout) */
261 hostapd_logger(iapp
->hapd
, sta
->addr
, HOSTAPD_MODULE_IAPP
,
262 HOSTAPD_LEVEL_DEBUG
, "IAPP-ADD.request(seq=%d)", seq
);
263 iapp_send_layer2_update(iapp
, sta
->addr
);
264 iapp_send_add(iapp
, sta
->addr
, seq
);
266 if (assoc
&& WLAN_FC_GET_STYPE(le_to_host16(assoc
->frame_control
)) ==
267 WLAN_FC_STYPE_REASSOC_REQ
) {
268 /* IAPP-MOVE.request(MAC Address, Sequence Number, Old AP,
269 * Context Block, Timeout)
271 /* TODO: Send IAPP-MOVE to the old AP; Map Old AP BSSID to
277 static void iapp_process_add_notify(struct iapp_data
*iapp
,
278 struct sockaddr_in
*from
,
279 struct iapp_hdr
*hdr
, int len
)
281 struct iapp_add_notify
*add
= (struct iapp_add_notify
*) (hdr
+ 1);
282 struct sta_info
*sta
;
284 if (len
!= sizeof(*add
)) {
285 printf("Invalid IAPP-ADD packet length %d (expected %lu)\n",
286 len
, (unsigned long) sizeof(*add
));
290 sta
= ap_get_sta(iapp
->hapd
, add
->mac_addr
);
292 /* IAPP-ADD.indication(MAC Address, Sequence Number) */
293 hostapd_logger(iapp
->hapd
, add
->mac_addr
, HOSTAPD_MODULE_IAPP
,
295 "Received IAPP ADD-notify (seq# %d) from %s:%d%s",
296 be_to_host16(add
->seq_num
),
297 inet_ntoa(from
->sin_addr
), ntohs(from
->sin_port
),
298 sta
? "" : " (STA not found)");
303 /* TODO: could use seq_num to try to determine whether last association
304 * to this AP is newer than the one advertised in IAPP-ADD. Although,
305 * this is not really a reliable verification. */
307 hostapd_logger(iapp
->hapd
, add
->mac_addr
, HOSTAPD_MODULE_IAPP
,
309 "Removing STA due to IAPP ADD-notify");
310 ap_sta_disconnect(iapp
->hapd
, sta
, NULL
, 0);
315 * iapp_receive_udp - Process IAPP UDP frames
316 * @sock: File descriptor for the socket
317 * @eloop_ctx: IAPP data (struct iapp_data *)
318 * @sock_ctx: Not used
320 static void iapp_receive_udp(int sock
, void *eloop_ctx
, void *sock_ctx
)
322 struct iapp_data
*iapp
= eloop_ctx
;
324 unsigned char buf
[128];
325 struct sockaddr_in from
;
327 struct iapp_hdr
*hdr
;
329 /* Handle incoming IAPP frames (over UDP/IP) */
331 fromlen
= sizeof(from
);
332 len
= recvfrom(iapp
->udp_sock
, buf
, sizeof(buf
), 0,
333 (struct sockaddr
*) &from
, &fromlen
);
339 if (from
.sin_addr
.s_addr
== iapp
->own
.s_addr
)
340 return; /* ignore own IAPP messages */
342 hostapd_logger(iapp
->hapd
, NULL
, HOSTAPD_MODULE_IAPP
,
344 "Received %d byte IAPP frame from %s%s\n",
345 len
, inet_ntoa(from
.sin_addr
),
346 len
< (int) sizeof(*hdr
) ? " (too short)" : "");
348 if (len
< (int) sizeof(*hdr
))
351 hdr
= (struct iapp_hdr
*) buf
;
352 hlen
= be_to_host16(hdr
->length
);
353 hostapd_logger(iapp
->hapd
, NULL
, HOSTAPD_MODULE_IAPP
,
355 "RX: version=%d command=%d id=%d len=%d\n",
356 hdr
->version
, hdr
->command
,
357 be_to_host16(hdr
->identifier
), hlen
);
358 if (hdr
->version
!= IAPP_VERSION
) {
359 printf("Dropping IAPP frame with unknown version %d\n",
364 printf("Underflow IAPP frame (hlen=%d len=%d)\n", hlen
, len
);
368 printf("Ignoring %d extra bytes from IAPP frame\n",
373 switch (hdr
->command
) {
374 case IAPP_CMD_ADD_notify
:
375 iapp_process_add_notify(iapp
, &from
, hdr
, hlen
- sizeof(*hdr
));
377 case IAPP_CMD_MOVE_notify
:
378 /* TODO: MOVE is using TCP; so move this to TCP handler once it
379 * is implemented.. */
380 /* IAPP-MOVE.indication(MAC Address, New BSSID,
381 * Sequence Number, AP Address, Context Block) */
385 printf("Unknown IAPP command %d\n", hdr
->command
);
391 struct iapp_data
* iapp_init(struct hostapd_data
*hapd
, const char *iface
)
394 struct sockaddr_ll addr
;
396 struct sockaddr_in
*paddr
, uaddr
;
397 struct iapp_data
*iapp
;
398 struct ip_mreqn mreq
;
400 iapp
= os_zalloc(sizeof(*iapp
));
404 iapp
->udp_sock
= iapp
->packet_sock
= -1;
407 * open socket for sending and receiving IAPP frames over TCP
410 iapp
->udp_sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
411 if (iapp
->udp_sock
< 0) {
412 perror("socket[PF_INET,SOCK_DGRAM]");
417 os_memset(&ifr
, 0, sizeof(ifr
));
418 os_strlcpy(ifr
.ifr_name
, iface
, sizeof(ifr
.ifr_name
));
419 if (ioctl(iapp
->udp_sock
, SIOCGIFINDEX
, &ifr
) != 0) {
420 perror("ioctl(SIOCGIFINDEX)");
424 ifindex
= ifr
.ifr_ifindex
;
426 if (ioctl(iapp
->udp_sock
, SIOCGIFADDR
, &ifr
) != 0) {
427 perror("ioctl(SIOCGIFADDR)");
431 paddr
= (struct sockaddr_in
*) &ifr
.ifr_addr
;
432 if (paddr
->sin_family
!= AF_INET
) {
433 printf("Invalid address family %i (SIOCGIFADDR)\n",
438 iapp
->own
.s_addr
= paddr
->sin_addr
.s_addr
;
440 if (ioctl(iapp
->udp_sock
, SIOCGIFBRDADDR
, &ifr
) != 0) {
441 perror("ioctl(SIOCGIFBRDADDR)");
445 paddr
= (struct sockaddr_in
*) &ifr
.ifr_addr
;
446 if (paddr
->sin_family
!= AF_INET
) {
447 printf("Invalid address family %i (SIOCGIFBRDADDR)\n",
452 inet_aton(IAPP_MULTICAST
, &iapp
->multicast
);
454 os_memset(&uaddr
, 0, sizeof(uaddr
));
455 uaddr
.sin_family
= AF_INET
;
456 uaddr
.sin_port
= htons(IAPP_UDP_PORT
);
457 if (bind(iapp
->udp_sock
, (struct sockaddr
*) &uaddr
,
458 sizeof(uaddr
)) < 0) {
464 os_memset(&mreq
, 0, sizeof(mreq
));
465 mreq
.imr_multiaddr
= iapp
->multicast
;
466 mreq
.imr_address
.s_addr
= INADDR_ANY
;
467 mreq
.imr_ifindex
= 0;
468 if (setsockopt(iapp
->udp_sock
, SOL_IP
, IP_ADD_MEMBERSHIP
, &mreq
,
470 perror("setsockopt[UDP,IP_ADD_MEMBERSHIP]");
475 iapp
->packet_sock
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
476 if (iapp
->packet_sock
< 0) {
477 perror("socket[PF_PACKET,SOCK_RAW]");
482 os_memset(&addr
, 0, sizeof(addr
));
483 addr
.sll_family
= AF_PACKET
;
484 addr
.sll_ifindex
= ifindex
;
485 if (bind(iapp
->packet_sock
, (struct sockaddr
*) &addr
,
487 perror("bind[PACKET]");
492 if (eloop_register_read_sock(iapp
->udp_sock
, iapp_receive_udp
,
494 printf("Could not register read socket for IAPP.\n");
499 printf("IEEE 802.11F (IAPP) using interface %s\n", iface
);
501 /* TODO: For levels 2 and 3: send RADIUS Initiate-Request, receive
502 * RADIUS Initiate-Accept or Initiate-Reject. IAPP port should actually
503 * be openned only after receiving Initiate-Accept. If Initiate-Reject
504 * is received, IAPP is not started. */
510 void iapp_deinit(struct iapp_data
*iapp
)
512 struct ip_mreqn mreq
;
517 if (iapp
->udp_sock
>= 0) {
518 os_memset(&mreq
, 0, sizeof(mreq
));
519 mreq
.imr_multiaddr
= iapp
->multicast
;
520 mreq
.imr_address
.s_addr
= INADDR_ANY
;
521 mreq
.imr_ifindex
= 0;
522 if (setsockopt(iapp
->udp_sock
, SOL_IP
, IP_DROP_MEMBERSHIP
,
523 &mreq
, sizeof(mreq
)) < 0) {
524 perror("setsockopt[UDP,IP_DEL_MEMBERSHIP]");
527 eloop_unregister_read_sock(iapp
->udp_sock
);
528 close(iapp
->udp_sock
);
530 if (iapp
->packet_sock
>= 0) {
531 eloop_unregister_read_sock(iapp
->packet_sock
);
532 close(iapp
->packet_sock
);