]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/ap/iapp.c
Rename some src/ap files to avoid duplicate file names
[thirdparty/hostap.git] / src / ap / iapp.c
1 /*
2 * hostapd / IEEE 802.11F-2003 Inter-Access Point Protocol (IAPP)
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
4 *
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.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 *
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.
18 */
19
20 /* TODO:
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
31 * used here?
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
38 */
39
40 #include "utils/includes.h"
41 #include <net/if.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 */
48
49 #include "utils/common.h"
50 #include "utils/eloop.h"
51 #include "hostapd.h"
52 #include "ap_config.h"
53 #include "ieee802_11.h"
54 #include "sta_info.h"
55 #include "iapp.h"
56
57
58 #define IAPP_MULTICAST "224.0.1.178"
59 #define IAPP_UDP_PORT 3517
60 #define IAPP_TCP_PORT 3517
61
62 struct iapp_hdr {
63 u8 version;
64 u8 command;
65 be16 identifier;
66 be16 length;
67 /* followed by length-6 octets of data */
68 } __attribute__ ((packed));
69
70 #define IAPP_VERSION 0
71
72 enum IAPP_COMMAND {
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,
80 };
81
82
83 /* ADD-notify - multicast UDP on the local LAN */
84 struct iapp_add_notify {
85 u8 addr_len; /* ETH_ALEN */
86 u8 reserved;
87 u8 mac_addr[ETH_ALEN];
88 be16 seq_num;
89 } __attribute__ ((packed));
90
91
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 */
96 be16 len; /* 6 */
97 u8 dsap; /* null DSAP address */
98 u8 ssap; /* null SSAP address, CR=Response */
99 u8 control;
100 u8 xid_info[3];
101 } __attribute__ ((packed));
102
103
104 /* MOVE-notify - unicast TCP */
105 struct iapp_move_notify {
106 u8 addr_len; /* ETH_ALEN */
107 u8 reserved;
108 u8 mac_addr[ETH_ALEN];
109 u16 seq_num;
110 u16 ctx_block_len;
111 /* followed by ctx_block_len bytes */
112 } __attribute__ ((packed));
113
114
115 /* MOVE-response - unicast TCP */
116 struct iapp_move_response {
117 u8 addr_len; /* ETH_ALEN */
118 u8 status;
119 u8 mac_addr[ETH_ALEN];
120 u16 seq_num;
121 u16 ctx_block_len;
122 /* followed by ctx_block_len bytes */
123 } __attribute__ ((packed));
124
125 enum {
126 IAPP_MOVE_SUCCESSFUL = 0,
127 IAPP_MOVE_DENIED = 1,
128 IAPP_MOVE_STALE_MOVE = 2,
129 };
130
131
132 /* CACHE-notify */
133 struct iapp_cache_notify {
134 u8 addr_len; /* ETH_ALEN */
135 u8 reserved;
136 u8 mac_addr[ETH_ALEN];
137 u16 seq_num;
138 u8 current_ap[ETH_ALEN];
139 u16 ctx_block_len;
140 /* ctx_block_len bytes of context block followed by 16-bit context
141 * timeout */
142 } __attribute__ ((packed));
143
144
145 /* CACHE-response - unicast TCP */
146 struct iapp_cache_response {
147 u8 addr_len; /* ETH_ALEN */
148 u8 status;
149 u8 mac_addr[ETH_ALEN];
150 u16 seq_num;
151 } __attribute__ ((packed));
152
153 enum {
154 IAPP_CACHE_SUCCESSFUL = 0,
155 IAPP_CACHE_STALE_CACHE = 1,
156 };
157
158
159 /* Send-Security-Block - unicast TCP */
160 struct iapp_send_security_block {
161 u8 iv[8];
162 u16 sec_block_len;
163 /* followed by sec_block_len bytes of security block */
164 } __attribute__ ((packed));
165
166
167 /* ACK-Security-Block - unicast TCP */
168 struct iapp_ack_security_block {
169 u8 iv[8];
170 u8 new_ap_ack_authenticator[48];
171 } __attribute__ ((packed));
172
173
174 struct iapp_data {
175 struct hostapd_data *hapd;
176 u16 identifier; /* next IAPP identifier */
177 struct in_addr own, multicast;
178 int udp_sock;
179 int packet_sock;
180 };
181
182
183 static void iapp_send_add(struct iapp_data *iapp, u8 *mac_addr, u16 seq_num)
184 {
185 char buf[128];
186 struct iapp_hdr *hdr;
187 struct iapp_add_notify *add;
188 struct sockaddr_in addr;
189
190 /* Send IAPP ADD-notify to remove possible association from other APs
191 */
192
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));
198
199 add = (struct iapp_add_notify *) (hdr + 1);
200 add->addr_len = ETH_ALEN;
201 add->reserved = 0;
202 os_memcpy(add->mac_addr, mac_addr, ETH_ALEN);
203
204 add->seq_num = host_to_be16(seq_num);
205
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]");
213 }
214
215
216 static void iapp_send_layer2_update(struct iapp_data *iapp, u8 *addr)
217 {
218 struct iapp_layer2_update msg;
219
220 /* Send Level 2 Update Frame to update forwarding tables in layer 2
221 * bridge devices */
222
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 */
225
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? */
237
238 if (send(iapp->packet_sock, &msg, sizeof(msg), 0) < 0)
239 perror("send[L2 Update]");
240 }
241
242
243 /**
244 * iapp_new_station - IAPP processing for a new STA
245 * @iapp: IAPP data
246 * @sta: The associated station
247 */
248 void iapp_new_station(struct iapp_data *iapp, struct sta_info *sta)
249 {
250 struct ieee80211_mgmt *assoc;
251 u16 seq;
252
253 if (iapp == NULL)
254 return;
255
256 assoc = sta->last_assoc_req;
257 seq = assoc ? WLAN_GET_SEQ_SEQ(le_to_host16(assoc->seq_ctrl)) : 0;
258
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);
264
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)
269 */
270 /* TODO: Send IAPP-MOVE to the old AP; Map Old AP BSSID to
271 * IP address */
272 }
273 }
274
275
276 static void iapp_process_add_notify(struct iapp_data *iapp,
277 struct sockaddr_in *from,
278 struct iapp_hdr *hdr, int len)
279 {
280 struct iapp_add_notify *add = (struct iapp_add_notify *) (hdr + 1);
281 struct sta_info *sta;
282
283 if (len != sizeof(*add)) {
284 printf("Invalid IAPP-ADD packet length %d (expected %lu)\n",
285 len, (unsigned long) sizeof(*add));
286 return;
287 }
288
289 sta = ap_get_sta(iapp->hapd, add->mac_addr);
290
291 /* IAPP-ADD.indication(MAC Address, Sequence Number) */
292 hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
293 HOSTAPD_LEVEL_INFO,
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)");
298
299 if (!sta)
300 return;
301
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. */
305
306 hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
307 HOSTAPD_LEVEL_DEBUG,
308 "Removing STA due to IAPP ADD-notify");
309 ap_sta_disconnect(iapp->hapd, sta, NULL, 0);
310 }
311
312
313 /**
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
318 */
319 static void iapp_receive_udp(int sock, void *eloop_ctx, void *sock_ctx)
320 {
321 struct iapp_data *iapp = eloop_ctx;
322 int len, hlen;
323 unsigned char buf[128];
324 struct sockaddr_in from;
325 socklen_t fromlen;
326 struct iapp_hdr *hdr;
327
328 /* Handle incoming IAPP frames (over UDP/IP) */
329
330 fromlen = sizeof(from);
331 len = recvfrom(iapp->udp_sock, buf, sizeof(buf), 0,
332 (struct sockaddr *) &from, &fromlen);
333 if (len < 0) {
334 perror("recvfrom");
335 return;
336 }
337
338 if (from.sin_addr.s_addr == iapp->own.s_addr)
339 return; /* ignore own IAPP messages */
340
341 hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
342 HOSTAPD_LEVEL_DEBUG,
343 "Received %d byte IAPP frame from %s%s\n",
344 len, inet_ntoa(from.sin_addr),
345 len < (int) sizeof(*hdr) ? " (too short)" : "");
346
347 if (len < (int) sizeof(*hdr))
348 return;
349
350 hdr = (struct iapp_hdr *) buf;
351 hlen = be_to_host16(hdr->length);
352 hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
353 HOSTAPD_LEVEL_DEBUG,
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",
359 hdr->version);
360 return;
361 }
362 if (hlen > len) {
363 printf("Underflow IAPP frame (hlen=%d len=%d)\n", hlen, len);
364 return;
365 }
366 if (hlen < len) {
367 printf("Ignoring %d extra bytes from IAPP frame\n",
368 len - hlen);
369 len = hlen;
370 }
371
372 switch (hdr->command) {
373 case IAPP_CMD_ADD_notify:
374 iapp_process_add_notify(iapp, &from, hdr, hlen - sizeof(*hdr));
375 break;
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) */
381 /* TODO: process */
382 break;
383 default:
384 printf("Unknown IAPP command %d\n", hdr->command);
385 break;
386 }
387 }
388
389
390 struct iapp_data * iapp_init(struct hostapd_data *hapd, const char *iface)
391 {
392 struct ifreq ifr;
393 struct sockaddr_ll addr;
394 int ifindex;
395 struct sockaddr_in *paddr, uaddr;
396 struct iapp_data *iapp;
397 struct ip_mreqn mreq;
398
399 iapp = os_zalloc(sizeof(*iapp));
400 if (iapp == NULL)
401 return NULL;
402 iapp->hapd = hapd;
403 iapp->udp_sock = iapp->packet_sock = -1;
404
405 /* TODO:
406 * open socket for sending and receiving IAPP frames over TCP
407 */
408
409 iapp->udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
410 if (iapp->udp_sock < 0) {
411 perror("socket[PF_INET,SOCK_DGRAM]");
412 iapp_deinit(iapp);
413 return NULL;
414 }
415
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)");
420 iapp_deinit(iapp);
421 return NULL;
422 }
423 ifindex = ifr.ifr_ifindex;
424
425 if (ioctl(iapp->udp_sock, SIOCGIFADDR, &ifr) != 0) {
426 perror("ioctl(SIOCGIFADDR)");
427 iapp_deinit(iapp);
428 return NULL;
429 }
430 paddr = (struct sockaddr_in *) &ifr.ifr_addr;
431 if (paddr->sin_family != AF_INET) {
432 printf("Invalid address family %i (SIOCGIFADDR)\n",
433 paddr->sin_family);
434 iapp_deinit(iapp);
435 return NULL;
436 }
437 iapp->own.s_addr = paddr->sin_addr.s_addr;
438
439 if (ioctl(iapp->udp_sock, SIOCGIFBRDADDR, &ifr) != 0) {
440 perror("ioctl(SIOCGIFBRDADDR)");
441 iapp_deinit(iapp);
442 return NULL;
443 }
444 paddr = (struct sockaddr_in *) &ifr.ifr_addr;
445 if (paddr->sin_family != AF_INET) {
446 printf("Invalid address family %i (SIOCGIFBRDADDR)\n",
447 paddr->sin_family);
448 iapp_deinit(iapp);
449 return NULL;
450 }
451 inet_aton(IAPP_MULTICAST, &iapp->multicast);
452
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) {
458 perror("bind[UDP]");
459 iapp_deinit(iapp);
460 return NULL;
461 }
462
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,
468 sizeof(mreq)) < 0) {
469 perror("setsockopt[UDP,IP_ADD_MEMBERSHIP]");
470 iapp_deinit(iapp);
471 return NULL;
472 }
473
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]");
477 iapp_deinit(iapp);
478 return NULL;
479 }
480
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,
485 sizeof(addr)) < 0) {
486 perror("bind[PACKET]");
487 iapp_deinit(iapp);
488 return NULL;
489 }
490
491 if (eloop_register_read_sock(iapp->udp_sock, iapp_receive_udp,
492 iapp, NULL)) {
493 printf("Could not register read socket for IAPP.\n");
494 iapp_deinit(iapp);
495 return NULL;
496 }
497
498 printf("IEEE 802.11F (IAPP) using interface %s\n", iface);
499
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. */
504
505 return iapp;
506 }
507
508
509 void iapp_deinit(struct iapp_data *iapp)
510 {
511 struct ip_mreqn mreq;
512
513 if (iapp == NULL)
514 return;
515
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]");
524 }
525
526 eloop_unregister_read_sock(iapp->udp_sock);
527 close(iapp->udp_sock);
528 }
529 if (iapp->packet_sock >= 0) {
530 eloop_unregister_read_sock(iapp->packet_sock);
531 close(iapp->packet_sock);
532 }
533 os_free(iapp);
534 }