Merge driver ops set_wps_beacon_ie and set_wps_probe_resp_ie
[libeap.git] / 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 "common.h"
50 #include "hostapd.h"
51 #include "config.h"
52 #include "ieee802_11.h"
53 #include "iapp.h"
54 #include "eloop.h"
55 #include "sta_info.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         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
310         eloop_cancel_timeout(ap_handle_timer, iapp->hapd, sta);
311         eloop_register_timeout(0, 0, ap_handle_timer, iapp->hapd, sta);
312         sta->timeout_next = STA_REMOVE;
313 }
314
315
316 /**
317  * iapp_receive_udp - Process IAPP UDP frames
318  * @sock: File descriptor for the socket
319  * @eloop_ctx: IAPP data (struct iapp_data *)
320  * @sock_ctx: Not used
321  */
322 static void iapp_receive_udp(int sock, void *eloop_ctx, void *sock_ctx)
323 {
324         struct iapp_data *iapp = eloop_ctx;
325         int len, hlen;
326         unsigned char buf[128];
327         struct sockaddr_in from;
328         socklen_t fromlen;
329         struct iapp_hdr *hdr;
330
331         /* Handle incoming IAPP frames (over UDP/IP) */
332
333         fromlen = sizeof(from);
334         len = recvfrom(iapp->udp_sock, buf, sizeof(buf), 0,
335                        (struct sockaddr *) &from, &fromlen);
336         if (len < 0) {
337                 perror("recvfrom");
338                 return;
339         }
340
341         if (from.sin_addr.s_addr == iapp->own.s_addr)
342                 return; /* ignore own IAPP messages */
343
344         hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
345                        HOSTAPD_LEVEL_DEBUG,
346                        "Received %d byte IAPP frame from %s%s\n",
347                        len, inet_ntoa(from.sin_addr),
348                        len < (int) sizeof(*hdr) ? " (too short)" : "");
349
350         if (len < (int) sizeof(*hdr))
351                 return;
352
353         hdr = (struct iapp_hdr *) buf;
354         hlen = be_to_host16(hdr->length);
355         hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
356                        HOSTAPD_LEVEL_DEBUG,
357                        "RX: version=%d command=%d id=%d len=%d\n",
358                        hdr->version, hdr->command,
359                        be_to_host16(hdr->identifier), hlen);
360         if (hdr->version != IAPP_VERSION) {
361                 printf("Dropping IAPP frame with unknown version %d\n",
362                        hdr->version);
363                 return;
364         }
365         if (hlen > len) {
366                 printf("Underflow IAPP frame (hlen=%d len=%d)\n", hlen, len);
367                 return;
368         }
369         if (hlen < len) {
370                 printf("Ignoring %d extra bytes from IAPP frame\n",
371                        len - hlen);
372                 len = hlen;
373         }
374
375         switch (hdr->command) {
376         case IAPP_CMD_ADD_notify:
377                 iapp_process_add_notify(iapp, &from, hdr, hlen - sizeof(*hdr));
378                 break;
379         case IAPP_CMD_MOVE_notify:
380                 /* TODO: MOVE is using TCP; so move this to TCP handler once it
381                  * is implemented.. */
382                 /* IAPP-MOVE.indication(MAC Address, New BSSID,
383                  * Sequence Number, AP Address, Context Block) */
384                 /* TODO: process */
385                 break;
386         default:
387                 printf("Unknown IAPP command %d\n", hdr->command);
388                 break;
389         }
390 }
391
392
393 struct iapp_data * iapp_init(struct hostapd_data *hapd, const char *iface)
394 {
395         struct ifreq ifr;
396         struct sockaddr_ll addr;
397         int ifindex;
398         struct sockaddr_in *paddr, uaddr;
399         struct iapp_data *iapp;
400         struct ip_mreqn mreq;
401
402         iapp = os_zalloc(sizeof(*iapp));
403         if (iapp == NULL)
404                 return NULL;
405         iapp->hapd = hapd;
406         iapp->udp_sock = iapp->packet_sock = -1;
407
408         /* TODO:
409          * open socket for sending and receiving IAPP frames over TCP
410          */
411
412         iapp->udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
413         if (iapp->udp_sock < 0) {
414                 perror("socket[PF_INET,SOCK_DGRAM]");
415                 iapp_deinit(iapp);
416                 return NULL;
417         }
418
419         os_memset(&ifr, 0, sizeof(ifr));
420         os_strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
421         if (ioctl(iapp->udp_sock, SIOCGIFINDEX, &ifr) != 0) {
422                 perror("ioctl(SIOCGIFINDEX)");
423                 iapp_deinit(iapp);
424                 return NULL;
425         }
426         ifindex = ifr.ifr_ifindex;
427
428         if (ioctl(iapp->udp_sock, SIOCGIFADDR, &ifr) != 0) {
429                 perror("ioctl(SIOCGIFADDR)");
430                 iapp_deinit(iapp);
431                 return NULL;
432         }
433         paddr = (struct sockaddr_in *) &ifr.ifr_addr;
434         if (paddr->sin_family != AF_INET) {
435                 printf("Invalid address family %i (SIOCGIFADDR)\n",
436                        paddr->sin_family);
437                 iapp_deinit(iapp);
438                 return NULL;
439         }
440         iapp->own.s_addr = paddr->sin_addr.s_addr;
441
442         if (ioctl(iapp->udp_sock, SIOCGIFBRDADDR, &ifr) != 0) {
443                 perror("ioctl(SIOCGIFBRDADDR)");
444                 iapp_deinit(iapp);
445                 return NULL;
446         }
447         paddr = (struct sockaddr_in *) &ifr.ifr_addr;
448         if (paddr->sin_family != AF_INET) {
449                 printf("Invalid address family %i (SIOCGIFBRDADDR)\n",
450                        paddr->sin_family);
451                 iapp_deinit(iapp);
452                 return NULL;
453         }
454         inet_aton(IAPP_MULTICAST, &iapp->multicast);
455
456         os_memset(&uaddr, 0, sizeof(uaddr));
457         uaddr.sin_family = AF_INET;
458         uaddr.sin_port = htons(IAPP_UDP_PORT);
459         if (bind(iapp->udp_sock, (struct sockaddr *) &uaddr,
460                  sizeof(uaddr)) < 0) {
461                 perror("bind[UDP]");
462                 iapp_deinit(iapp);
463                 return NULL;
464         }
465
466         os_memset(&mreq, 0, sizeof(mreq));
467         mreq.imr_multiaddr = iapp->multicast;
468         mreq.imr_address.s_addr = INADDR_ANY;
469         mreq.imr_ifindex = 0;
470         if (setsockopt(iapp->udp_sock, SOL_IP, IP_ADD_MEMBERSHIP, &mreq,
471                        sizeof(mreq)) < 0) {
472                 perror("setsockopt[UDP,IP_ADD_MEMBERSHIP]");
473                 iapp_deinit(iapp);
474                 return NULL;
475         }
476
477         iapp->packet_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
478         if (iapp->packet_sock < 0) {
479                 perror("socket[PF_PACKET,SOCK_RAW]");
480                 iapp_deinit(iapp);
481                 return NULL;
482         }
483
484         os_memset(&addr, 0, sizeof(addr));
485         addr.sll_family = AF_PACKET;
486         addr.sll_ifindex = ifindex;
487         if (bind(iapp->packet_sock, (struct sockaddr *) &addr,
488                  sizeof(addr)) < 0) {
489                 perror("bind[PACKET]");
490                 iapp_deinit(iapp);
491                 return NULL;
492         }
493
494         if (eloop_register_read_sock(iapp->udp_sock, iapp_receive_udp,
495                                      iapp, NULL)) {
496                 printf("Could not register read socket for IAPP.\n");
497                 iapp_deinit(iapp);
498                 return NULL;
499         }
500
501         printf("IEEE 802.11F (IAPP) using interface %s\n", iface);
502
503         /* TODO: For levels 2 and 3: send RADIUS Initiate-Request, receive
504          * RADIUS Initiate-Accept or Initiate-Reject. IAPP port should actually
505          * be openned only after receiving Initiate-Accept. If Initiate-Reject
506          * is received, IAPP is not started. */
507
508         return iapp;
509 }
510
511
512 void iapp_deinit(struct iapp_data *iapp)
513 {
514         struct ip_mreqn mreq;
515
516         if (iapp == NULL)
517                 return;
518
519         if (iapp->udp_sock >= 0) {
520                 os_memset(&mreq, 0, sizeof(mreq));
521                 mreq.imr_multiaddr = iapp->multicast;
522                 mreq.imr_address.s_addr = INADDR_ANY;
523                 mreq.imr_ifindex = 0;
524                 if (setsockopt(iapp->udp_sock, SOL_IP, IP_DROP_MEMBERSHIP,
525                                &mreq, sizeof(mreq)) < 0) {
526                         perror("setsockopt[UDP,IP_DEL_MEMBERSHIP]");
527                 }
528
529                 eloop_unregister_read_sock(iapp->udp_sock);
530                 close(iapp->udp_sock);
531         }
532         if (iapp->packet_sock >= 0) {
533                 eloop_unregister_read_sock(iapp->packet_sock);
534                 close(iapp->packet_sock);
535         }
536         os_free(iapp);
537 }