bsd: Get rid of printf size_t warnings with 64-bit builds
[wpasupplicant] / hostapd / 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 "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 "hostapd.h"
50 #include "config.h"
51 #include "ieee802_11.h"
52 #include "iapp.h"
53 #include "eloop.h"
54 #include "sta_info.h"
55
56
57 #define IAPP_MULTICAST "224.0.1.178"
58 #define IAPP_UDP_PORT 3517
59 #define IAPP_TCP_PORT 3517
60
61 struct iapp_hdr {
62         u8 version;
63         u8 command;
64         be16 identifier;
65         be16 length;
66         /* followed by length-6 octets of data */
67 } __attribute__ ((packed));
68
69 #define IAPP_VERSION 0
70
71 enum IAPP_COMMAND {
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,
79 };
80
81
82 /* ADD-notify - multicast UDP on the local LAN */
83 struct iapp_add_notify {
84         u8 addr_len; /* ETH_ALEN */
85         u8 reserved;
86         u8 mac_addr[ETH_ALEN];
87         be16 seq_num;
88 } __attribute__ ((packed));
89
90
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 */
95         be16 len; /* 6 */
96         u8 dsap; /* null DSAP address */
97         u8 ssap; /* null SSAP address, CR=Response */
98         u8 control;
99         u8 xid_info[3];
100 } __attribute__ ((packed));
101
102
103 /* MOVE-notify - unicast TCP */
104 struct iapp_move_notify {
105         u8 addr_len; /* ETH_ALEN */
106         u8 reserved;
107         u8 mac_addr[ETH_ALEN];
108         u16 seq_num;
109         u16 ctx_block_len;
110         /* followed by ctx_block_len bytes */
111 } __attribute__ ((packed));
112
113
114 /* MOVE-response - unicast TCP */
115 struct iapp_move_response {
116         u8 addr_len; /* ETH_ALEN */
117         u8 status;
118         u8 mac_addr[ETH_ALEN];
119         u16 seq_num;
120         u16 ctx_block_len;
121         /* followed by ctx_block_len bytes */
122 } __attribute__ ((packed));
123
124 enum {
125         IAPP_MOVE_SUCCESSFUL = 0,
126         IAPP_MOVE_DENIED = 1,
127         IAPP_MOVE_STALE_MOVE = 2,
128 };
129
130
131 /* CACHE-notify */
132 struct iapp_cache_notify {
133         u8 addr_len; /* ETH_ALEN */
134         u8 reserved;
135         u8 mac_addr[ETH_ALEN];
136         u16 seq_num;
137         u8 current_ap[ETH_ALEN];
138         u16 ctx_block_len;
139         /* ctx_block_len bytes of context block followed by 16-bit context
140          * timeout */
141 } __attribute__ ((packed));
142
143
144 /* CACHE-response - unicast TCP */
145 struct iapp_cache_response {
146         u8 addr_len; /* ETH_ALEN */
147         u8 status;
148         u8 mac_addr[ETH_ALEN];
149         u16 seq_num;
150 } __attribute__ ((packed));
151
152 enum {
153         IAPP_CACHE_SUCCESSFUL = 0,
154         IAPP_CACHE_STALE_CACHE = 1,
155 };
156
157
158 /* Send-Security-Block - unicast TCP */
159 struct iapp_send_security_block {
160         u8 iv[8];
161         u16 sec_block_len;
162         /* followed by sec_block_len bytes of security block */
163 } __attribute__ ((packed));
164
165
166 /* ACK-Security-Block - unicast TCP */
167 struct iapp_ack_security_block {
168         u8 iv[8];
169         u8 new_ap_ack_authenticator[48];
170 } __attribute__ ((packed));
171
172
173 struct iapp_data {
174         struct hostapd_data *hapd;
175         u16 identifier; /* next IAPP identifier */
176         struct in_addr own, multicast;
177         int udp_sock;
178         int packet_sock;
179 };
180
181
182 static void iapp_send_add(struct iapp_data *iapp, u8 *mac_addr, u16 seq_num)
183 {
184         char buf[128];
185         struct iapp_hdr *hdr;
186         struct iapp_add_notify *add;
187         struct sockaddr_in addr;
188
189         /* Send IAPP ADD-notify to remove possible association from other APs
190          */
191
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));
197
198         add = (struct iapp_add_notify *) (hdr + 1);
199         add->addr_len = ETH_ALEN;
200         add->reserved = 0;
201         os_memcpy(add->mac_addr, mac_addr, ETH_ALEN);
202
203         add->seq_num = host_to_be16(seq_num);
204         
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]");
212 }
213
214
215 static void iapp_send_layer2_update(struct iapp_data *iapp, u8 *addr)
216 {
217         struct iapp_layer2_update msg;
218
219         /* Send Level 2 Update Frame to update forwarding tables in layer 2
220          * bridge devices */
221
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 */
224
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? */
236
237         if (send(iapp->packet_sock, &msg, sizeof(msg), 0) < 0)
238                 perror("send[L2 Update]");
239 }
240
241
242 /**
243  * iapp_new_station - IAPP processing for a new STA
244  * @iapp: IAPP data
245  * @sta: The associated station
246  */
247 void iapp_new_station(struct iapp_data *iapp, struct sta_info *sta)
248 {
249         struct ieee80211_mgmt *assoc;
250         u16 seq;
251
252         if (iapp == NULL)
253                 return;
254
255         assoc = sta->last_assoc_req;
256         seq = assoc ? WLAN_GET_SEQ_SEQ(le_to_host16(assoc->seq_ctrl)) : 0;
257
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);
263
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)
268                  */
269                 /* TODO: Send IAPP-MOVE to the old AP; Map Old AP BSSID to
270                  * IP address */
271         }
272 }
273
274
275 static void iapp_process_add_notify(struct iapp_data *iapp,
276                                     struct sockaddr_in *from,
277                                     struct iapp_hdr *hdr, int len)
278 {
279         struct iapp_add_notify *add = (struct iapp_add_notify *) (hdr + 1);
280         struct sta_info *sta;
281
282         if (len != sizeof(*add)) {
283                 printf("Invalid IAPP-ADD packet length %d (expected %lu)\n",
284                        len, (unsigned long) sizeof(*add));
285                 return;
286         }
287
288         sta = ap_get_sta(iapp->hapd, add->mac_addr);
289
290         /* IAPP-ADD.indication(MAC Address, Sequence Number) */
291         hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
292                        HOSTAPD_LEVEL_INFO,
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)");
297
298         if (!sta)
299                 return;
300
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. */
304
305         hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
306                        HOSTAPD_LEVEL_DEBUG,
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;
312 }
313
314
315 /**
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
320  */
321 static void iapp_receive_udp(int sock, void *eloop_ctx, void *sock_ctx)
322 {
323         struct iapp_data *iapp = eloop_ctx;
324         int len, hlen;
325         unsigned char buf[128];
326         struct sockaddr_in from;
327         socklen_t fromlen;
328         struct iapp_hdr *hdr;
329
330         /* Handle incoming IAPP frames (over UDP/IP) */
331
332         fromlen = sizeof(from);
333         len = recvfrom(iapp->udp_sock, buf, sizeof(buf), 0,
334                        (struct sockaddr *) &from, &fromlen);
335         if (len < 0) {
336                 perror("recvfrom");
337                 return;
338         }
339
340         if (from.sin_addr.s_addr == iapp->own.s_addr)
341                 return; /* ignore own IAPP messages */
342
343         hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
344                        HOSTAPD_LEVEL_DEBUG,
345                        "Received %d byte IAPP frame from %s%s\n",
346                        len, inet_ntoa(from.sin_addr),
347                        len < (int) sizeof(*hdr) ? " (too short)" : "");
348
349         if (len < (int) sizeof(*hdr))
350                 return;
351
352         hdr = (struct iapp_hdr *) buf;
353         hlen = be_to_host16(hdr->length);
354         hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
355                        HOSTAPD_LEVEL_DEBUG,
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",
361                        hdr->version);
362                 return;
363         }
364         if (hlen > len) {
365                 printf("Underflow IAPP frame (hlen=%d len=%d)\n", hlen, len);
366                 return;
367         }
368         if (hlen < len) {
369                 printf("Ignoring %d extra bytes from IAPP frame\n",
370                        len - hlen);
371                 len = hlen;
372         }
373
374         switch (hdr->command) {
375         case IAPP_CMD_ADD_notify:
376                 iapp_process_add_notify(iapp, &from, hdr, hlen - sizeof(*hdr));
377                 break;
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) */
383                 /* TODO: process */
384                 break;
385         default:
386                 printf("Unknown IAPP command %d\n", hdr->command);
387                 break;
388         }
389 }
390
391
392 struct iapp_data * iapp_init(struct hostapd_data *hapd, const char *iface)
393 {
394         struct ifreq ifr;
395         struct sockaddr_ll addr;
396         int ifindex;
397         struct sockaddr_in *paddr, uaddr;
398         struct iapp_data *iapp;
399         struct ip_mreqn mreq;
400
401         iapp = os_zalloc(sizeof(*iapp));
402         if (iapp == NULL)
403                 return NULL;
404         iapp->hapd = hapd;
405         iapp->udp_sock = iapp->packet_sock = -1;
406
407         /* TODO:
408          * open socket for sending and receiving IAPP frames over TCP
409          */
410
411         iapp->udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
412         if (iapp->udp_sock < 0) {
413                 perror("socket[PF_INET,SOCK_DGRAM]");
414                 iapp_deinit(iapp);
415                 return NULL;
416         }
417
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)");
422                 iapp_deinit(iapp);
423                 return NULL;
424         }
425         ifindex = ifr.ifr_ifindex;
426
427         if (ioctl(iapp->udp_sock, SIOCGIFADDR, &ifr) != 0) {
428                 perror("ioctl(SIOCGIFADDR)");
429                 iapp_deinit(iapp);
430                 return NULL;
431         }
432         paddr = (struct sockaddr_in *) &ifr.ifr_addr;
433         if (paddr->sin_family != AF_INET) {
434                 printf("Invalid address family %i (SIOCGIFADDR)\n",
435                        paddr->sin_family);
436                 iapp_deinit(iapp);
437                 return NULL;
438         }
439         iapp->own.s_addr = paddr->sin_addr.s_addr;
440
441         if (ioctl(iapp->udp_sock, SIOCGIFBRDADDR, &ifr) != 0) {
442                 perror("ioctl(SIOCGIFBRDADDR)");
443                 iapp_deinit(iapp);
444                 return NULL;
445         }
446         paddr = (struct sockaddr_in *) &ifr.ifr_addr;
447         if (paddr->sin_family != AF_INET) {
448                 printf("Invalid address family %i (SIOCGIFBRDADDR)\n",
449                        paddr->sin_family);
450                 iapp_deinit(iapp);
451                 return NULL;
452         }
453         inet_aton(IAPP_MULTICAST, &iapp->multicast);
454
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) {
460                 perror("bind[UDP]");
461                 iapp_deinit(iapp);
462                 return NULL;
463         }
464
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,
470                        sizeof(mreq)) < 0) {
471                 perror("setsockopt[UDP,IP_ADD_MEMBERSHIP]");
472                 iapp_deinit(iapp);
473                 return NULL;
474         }
475
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]");
479                 iapp_deinit(iapp);
480                 return NULL;
481         }
482
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,
487                  sizeof(addr)) < 0) {
488                 perror("bind[PACKET]");
489                 iapp_deinit(iapp);
490                 return NULL;
491         }
492
493         if (eloop_register_read_sock(iapp->udp_sock, iapp_receive_udp,
494                                      iapp, NULL)) {
495                 printf("Could not register read socket for IAPP.\n");
496                 iapp_deinit(iapp);
497                 return NULL;
498         }
499
500         printf("IEEE 802.11F (IAPP) using interface %s\n", iface);
501
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. */
506
507         return iapp;
508 }
509
510
511 void iapp_deinit(struct iapp_data *iapp)
512 {
513         struct ip_mreqn mreq;
514
515         if (iapp == NULL)
516                 return;
517
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]");
526                 }
527
528                 eloop_unregister_read_sock(iapp->udp_sock);
529                 close(iapp->udp_sock);
530         }
531         if (iapp->packet_sock >= 0) {
532                 eloop_unregister_read_sock(iapp->packet_sock);
533                 close(iapp->packet_sock);
534         }
535         os_free(iapp);
536 }
537
538 int iapp_reconfig(struct hostapd_data *hapd, struct hostapd_config *oldconf,
539                   struct hostapd_bss_config *oldbss)
540 {
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);
544                 hapd->iapp = NULL;
545
546                 if (hapd->conf->ieee802_11f) {
547                         hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface);
548                         if (hapd->iapp == NULL)
549                                 return -1;
550                 }
551         }
552
553         return 0;
554 }