iapp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. /* TODO:
  20. * Level 1: no administrative or security support
  21. * (e.g., static BSSID to IP address mapping in each AP)
  22. * Level 2: support for dynamic mapping of BSSID to IP address
  23. * Level 3: support for encryption and authentication of IAPP messages
  24. * - add support for MOVE-notify and MOVE-response (this requires support for
  25. * finding out IP address for previous AP using RADIUS)
  26. * - add support for Send- and ACK-Security-Block to speedup IEEE 802.1X during
  27. * reassociation to another AP
  28. * - implement counters etc. for IAPP MIB
  29. * - verify endianness of fields in IAPP messages; are they big-endian as
  30. * used here?
  31. * - RADIUS connection for AP registration and BSSID to IP address mapping
  32. * - TCP connection for IAPP MOVE, CACHE
  33. * - broadcast ESP for IAPP ADD-notify
  34. * - ESP for IAPP MOVE messages
  35. * - security block sending/processing
  36. * - IEEE 802.11 context transfer
  37. */
  38. #include "includes.h"
  39. #include <net/if.h>
  40. #include <sys/ioctl.h>
  41. #ifdef USE_KERNEL_HEADERS
  42. #include <linux/if_packet.h>
  43. #else /* USE_KERNEL_HEADERS */
  44. #include <netpacket/packet.h>
  45. #endif /* USE_KERNEL_HEADERS */
  46. #include "hostapd.h"
  47. #include "ieee802_11.h"
  48. #include "iapp.h"
  49. #include "eloop.h"
  50. #include "sta_info.h"
  51. #define IAPP_MULTICAST "224.0.1.178"
  52. #define IAPP_UDP_PORT 3517
  53. #define IAPP_TCP_PORT 3517
  54. struct iapp_hdr {
  55. u8 version;
  56. u8 command;
  57. be16 identifier;
  58. be16 length;
  59. /* followed by length-6 octets of data */
  60. } __attribute__ ((packed));
  61. #define IAPP_VERSION 0
  62. enum IAPP_COMMAND {
  63. IAPP_CMD_ADD_notify = 0,
  64. IAPP_CMD_MOVE_notify = 1,
  65. IAPP_CMD_MOVE_response = 2,
  66. IAPP_CMD_Send_Security_Block = 3,
  67. IAPP_CMD_ACK_Security_Block = 4,
  68. IAPP_CMD_CACHE_notify = 5,
  69. IAPP_CMD_CACHE_response = 6,
  70. };
  71. /* ADD-notify - multicast UDP on the local LAN */
  72. struct iapp_add_notify {
  73. u8 addr_len; /* ETH_ALEN */
  74. u8 reserved;
  75. u8 mac_addr[ETH_ALEN];
  76. be16 seq_num;
  77. } __attribute__ ((packed));
  78. /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
  79. struct iapp_layer2_update {
  80. u8 da[ETH_ALEN]; /* broadcast */
  81. u8 sa[ETH_ALEN]; /* STA addr */
  82. be16 len; /* 6 */
  83. u8 dsap; /* null DSAP address */
  84. u8 ssap; /* null SSAP address, CR=Response */
  85. u8 control;
  86. u8 xid_info[3];
  87. } __attribute__ ((packed));
  88. /* MOVE-notify - unicast TCP */
  89. struct iapp_move_notify {
  90. u8 addr_len; /* ETH_ALEN */
  91. u8 reserved;
  92. u8 mac_addr[ETH_ALEN];
  93. u16 seq_num;
  94. u16 ctx_block_len;
  95. /* followed by ctx_block_len bytes */
  96. } __attribute__ ((packed));
  97. /* MOVE-response - unicast TCP */
  98. struct iapp_move_response {
  99. u8 addr_len; /* ETH_ALEN */
  100. u8 status;
  101. u8 mac_addr[ETH_ALEN];
  102. u16 seq_num;
  103. u16 ctx_block_len;
  104. /* followed by ctx_block_len bytes */
  105. } __attribute__ ((packed));
  106. enum {
  107. IAPP_MOVE_SUCCESSFUL = 0,
  108. IAPP_MOVE_DENIED = 1,
  109. IAPP_MOVE_STALE_MOVE = 2,
  110. };
  111. /* CACHE-notify */
  112. struct iapp_cache_notify {
  113. u8 addr_len; /* ETH_ALEN */
  114. u8 reserved;
  115. u8 mac_addr[ETH_ALEN];
  116. u16 seq_num;
  117. u8 current_ap[ETH_ALEN];
  118. u16 ctx_block_len;
  119. /* ctx_block_len bytes of context block followed by 16-bit context
  120. * timeout */
  121. } __attribute__ ((packed));
  122. /* CACHE-response - unicast TCP */
  123. struct iapp_cache_response {
  124. u8 addr_len; /* ETH_ALEN */
  125. u8 status;
  126. u8 mac_addr[ETH_ALEN];
  127. u16 seq_num;
  128. } __attribute__ ((packed));
  129. enum {
  130. IAPP_CACHE_SUCCESSFUL = 0,
  131. IAPP_CACHE_STALE_CACHE = 1,
  132. };
  133. /* Send-Security-Block - unicast TCP */
  134. struct iapp_send_security_block {
  135. u8 iv[8];
  136. u16 sec_block_len;
  137. /* followed by sec_block_len bytes of security block */
  138. } __attribute__ ((packed));
  139. /* ACK-Security-Block - unicast TCP */
  140. struct iapp_ack_security_block {
  141. u8 iv[8];
  142. u8 new_ap_ack_authenticator[48];
  143. } __attribute__ ((packed));
  144. struct iapp_data {
  145. struct hostapd_data *hapd;
  146. u16 identifier; /* next IAPP identifier */
  147. struct in_addr own, multicast;
  148. int udp_sock;
  149. int packet_sock;
  150. };
  151. static void iapp_send_add(struct iapp_data *iapp, u8 *mac_addr, u16 seq_num)
  152. {
  153. char buf[128];
  154. struct iapp_hdr *hdr;
  155. struct iapp_add_notify *add;
  156. struct sockaddr_in addr;
  157. /* Send IAPP ADD-notify to remove possible association from other APs
  158. */
  159. hdr = (struct iapp_hdr *) buf;
  160. hdr->version = IAPP_VERSION;
  161. hdr->command = IAPP_CMD_ADD_notify;
  162. hdr->identifier = host_to_be16(iapp->identifier++);
  163. hdr->length = host_to_be16(sizeof(*hdr) + sizeof(*add));
  164. add = (struct iapp_add_notify *) (hdr + 1);
  165. add->addr_len = ETH_ALEN;
  166. add->reserved = 0;
  167. os_memcpy(add->mac_addr, mac_addr, ETH_ALEN);
  168. add->seq_num = host_to_be16(seq_num);
  169. os_memset(&addr, 0, sizeof(addr));
  170. addr.sin_family = AF_INET;
  171. addr.sin_addr.s_addr = iapp->multicast.s_addr;
  172. addr.sin_port = htons(IAPP_UDP_PORT);
  173. if (sendto(iapp->udp_sock, buf, (char *) (add + 1) - buf, 0,
  174. (struct sockaddr *) &addr, sizeof(addr)) < 0)
  175. perror("sendto[IAPP-ADD]");
  176. }
  177. static void iapp_send_layer2_update(struct iapp_data *iapp, u8 *addr)
  178. {
  179. struct iapp_layer2_update msg;
  180. /* Send Level 2 Update Frame to update forwarding tables in layer 2
  181. * bridge devices */
  182. /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
  183. * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
  184. os_memset(msg.da, 0xff, ETH_ALEN);
  185. os_memcpy(msg.sa, addr, ETH_ALEN);
  186. msg.len = host_to_be16(6);
  187. msg.dsap = 0; /* NULL DSAP address */
  188. msg.ssap = 0x01; /* NULL SSAP address, CR Bit: Response */
  189. msg.control = 0xaf; /* XID response lsb.1111F101.
  190. * F=0 (no poll command; unsolicited frame) */
  191. msg.xid_info[0] = 0x81; /* XID format identifier */
  192. msg.xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
  193. msg.xid_info[2] = 1 << 1; /* XID sender's receive window size (RW)
  194. * FIX: what is correct RW with 802.11? */
  195. if (send(iapp->packet_sock, &msg, sizeof(msg), 0) < 0)
  196. perror("send[L2 Update]");
  197. }
  198. /**
  199. * iapp_new_station - IAPP processing for a new STA
  200. * @iapp: IAPP data
  201. * @sta: The associated station
  202. */
  203. void iapp_new_station(struct iapp_data *iapp, struct sta_info *sta)
  204. {
  205. struct ieee80211_mgmt *assoc;
  206. u16 seq;
  207. if (iapp == NULL)
  208. return;
  209. assoc = sta->last_assoc_req;
  210. seq = assoc ? WLAN_GET_SEQ_SEQ(le_to_host16(assoc->seq_ctrl)) : 0;
  211. /* IAPP-ADD.request(MAC Address, Sequence Number, Timeout) */
  212. hostapd_logger(iapp->hapd, sta->addr, HOSTAPD_MODULE_IAPP,
  213. HOSTAPD_LEVEL_DEBUG, "IAPP-ADD.request(seq=%d)", seq);
  214. iapp_send_layer2_update(iapp, sta->addr);
  215. iapp_send_add(iapp, sta->addr, seq);
  216. if (assoc && WLAN_FC_GET_STYPE(le_to_host16(assoc->frame_control)) ==
  217. WLAN_FC_STYPE_REASSOC_REQ) {
  218. /* IAPP-MOVE.request(MAC Address, Sequence Number, Old AP,
  219. * Context Block, Timeout)
  220. */
  221. /* TODO: Send IAPP-MOVE to the old AP; Map Old AP BSSID to
  222. * IP address */
  223. }
  224. }
  225. static void iapp_process_add_notify(struct iapp_data *iapp,
  226. struct sockaddr_in *from,
  227. struct iapp_hdr *hdr, int len)
  228. {
  229. struct iapp_add_notify *add = (struct iapp_add_notify *) (hdr + 1);
  230. struct sta_info *sta;
  231. if (len != sizeof(*add)) {
  232. printf("Invalid IAPP-ADD packet length %d (expected %lu)\n",
  233. len, (unsigned long) sizeof(*add));
  234. return;
  235. }
  236. sta = ap_get_sta(iapp->hapd, add->mac_addr);
  237. /* IAPP-ADD.indication(MAC Address, Sequence Number) */
  238. hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
  239. HOSTAPD_LEVEL_INFO,
  240. "Received IAPP ADD-notify (seq# %d) from %s:%d%s",
  241. be_to_host16(add->seq_num),
  242. inet_ntoa(from->sin_addr), ntohs(from->sin_port),
  243. sta ? "" : " (STA not found)");
  244. if (!sta)
  245. return;
  246. /* TODO: could use seq_num to try to determine whether last association
  247. * to this AP is newer than the one advertised in IAPP-ADD. Although,
  248. * this is not really a reliable verification. */
  249. hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
  250. HOSTAPD_LEVEL_DEBUG,
  251. "Removing STA due to IAPP ADD-notify");
  252. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
  253. eloop_cancel_timeout(ap_handle_timer, iapp->hapd, sta);
  254. eloop_register_timeout(0, 0, ap_handle_timer, iapp->hapd, sta);
  255. sta->timeout_next = STA_REMOVE;
  256. }
  257. /**
  258. * iapp_receive_udp - Process IAPP UDP frames
  259. * @sock: File descriptor for the socket
  260. * @eloop_ctx: IAPP data (struct iapp_data *)
  261. * @sock_ctx: Not used
  262. */
  263. static void iapp_receive_udp(int sock, void *eloop_ctx, void *sock_ctx)
  264. {
  265. struct iapp_data *iapp = eloop_ctx;
  266. int len, hlen;
  267. unsigned char buf[128];
  268. struct sockaddr_in from;
  269. socklen_t fromlen;
  270. struct iapp_hdr *hdr;
  271. /* Handle incoming IAPP frames (over UDP/IP) */
  272. fromlen = sizeof(from);
  273. len = recvfrom(iapp->udp_sock, buf, sizeof(buf), 0,
  274. (struct sockaddr *) &from, &fromlen);
  275. if (len < 0) {
  276. perror("recvfrom");
  277. return;
  278. }
  279. if (from.sin_addr.s_addr == iapp->own.s_addr)
  280. return; /* ignore own IAPP messages */
  281. hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
  282. HOSTAPD_LEVEL_DEBUG,
  283. "Received %d byte IAPP frame from %s%s\n",
  284. len, inet_ntoa(from.sin_addr),
  285. len < (int) sizeof(*hdr) ? " (too short)" : "");
  286. if (len < (int) sizeof(*hdr))
  287. return;
  288. hdr = (struct iapp_hdr *) buf;
  289. hlen = be_to_host16(hdr->length);
  290. hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
  291. HOSTAPD_LEVEL_DEBUG,
  292. "RX: version=%d command=%d id=%d len=%d\n",
  293. hdr->version, hdr->command,
  294. be_to_host16(hdr->identifier), hlen);
  295. if (hdr->version != IAPP_VERSION) {
  296. printf("Dropping IAPP frame with unknown version %d\n",
  297. hdr->version);
  298. return;
  299. }
  300. if (hlen > len) {
  301. printf("Underflow IAPP frame (hlen=%d len=%d)\n", hlen, len);
  302. return;
  303. }
  304. if (hlen < len) {
  305. printf("Ignoring %d extra bytes from IAPP frame\n",
  306. len - hlen);
  307. len = hlen;
  308. }
  309. switch (hdr->command) {
  310. case IAPP_CMD_ADD_notify:
  311. iapp_process_add_notify(iapp, &from, hdr, hlen - sizeof(*hdr));
  312. break;
  313. case IAPP_CMD_MOVE_notify:
  314. /* TODO: MOVE is using TCP; so move this to TCP handler once it
  315. * is implemented.. */
  316. /* IAPP-MOVE.indication(MAC Address, New BSSID,
  317. * Sequence Number, AP Address, Context Block) */
  318. /* TODO: process */
  319. break;
  320. default:
  321. printf("Unknown IAPP command %d\n", hdr->command);
  322. break;
  323. }
  324. }
  325. struct iapp_data * iapp_init(struct hostapd_data *hapd, const char *iface)
  326. {
  327. struct ifreq ifr;
  328. struct sockaddr_ll addr;
  329. int ifindex;
  330. struct sockaddr_in *paddr, uaddr;
  331. struct iapp_data *iapp;
  332. struct ip_mreqn mreq;
  333. iapp = os_zalloc(sizeof(*iapp));
  334. if (iapp == NULL)
  335. return NULL;
  336. iapp->hapd = hapd;
  337. iapp->udp_sock = iapp->packet_sock = -1;
  338. /* TODO:
  339. * open socket for sending and receiving IAPP frames over TCP
  340. */
  341. iapp->udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
  342. if (iapp->udp_sock < 0) {
  343. perror("socket[PF_INET,SOCK_DGRAM]");
  344. iapp_deinit(iapp);
  345. return NULL;
  346. }
  347. os_memset(&ifr, 0, sizeof(ifr));
  348. os_strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
  349. if (ioctl(iapp->udp_sock, SIOCGIFINDEX, &ifr) != 0) {
  350. perror("ioctl(SIOCGIFINDEX)");
  351. iapp_deinit(iapp);
  352. return NULL;
  353. }
  354. ifindex = ifr.ifr_ifindex;
  355. if (ioctl(iapp->udp_sock, SIOCGIFADDR, &ifr) != 0) {
  356. perror("ioctl(SIOCGIFADDR)");
  357. iapp_deinit(iapp);
  358. return NULL;
  359. }
  360. paddr = (struct sockaddr_in *) &ifr.ifr_addr;
  361. if (paddr->sin_family != AF_INET) {
  362. printf("Invalid address family %i (SIOCGIFADDR)\n",
  363. paddr->sin_family);
  364. iapp_deinit(iapp);
  365. return NULL;
  366. }
  367. iapp->own.s_addr = paddr->sin_addr.s_addr;
  368. if (ioctl(iapp->udp_sock, SIOCGIFBRDADDR, &ifr) != 0) {
  369. perror("ioctl(SIOCGIFBRDADDR)");
  370. iapp_deinit(iapp);
  371. return NULL;
  372. }
  373. paddr = (struct sockaddr_in *) &ifr.ifr_addr;
  374. if (paddr->sin_family != AF_INET) {
  375. printf("Invalid address family %i (SIOCGIFBRDADDR)\n",
  376. paddr->sin_family);
  377. iapp_deinit(iapp);
  378. return NULL;
  379. }
  380. inet_aton(IAPP_MULTICAST, &iapp->multicast);
  381. os_memset(&uaddr, 0, sizeof(uaddr));
  382. uaddr.sin_family = AF_INET;
  383. uaddr.sin_port = htons(IAPP_UDP_PORT);
  384. if (bind(iapp->udp_sock, (struct sockaddr *) &uaddr,
  385. sizeof(uaddr)) < 0) {
  386. perror("bind[UDP]");
  387. iapp_deinit(iapp);
  388. return NULL;
  389. }
  390. os_memset(&mreq, 0, sizeof(mreq));
  391. mreq.imr_multiaddr = iapp->multicast;
  392. mreq.imr_address.s_addr = INADDR_ANY;
  393. mreq.imr_ifindex = 0;
  394. if (setsockopt(iapp->udp_sock, SOL_IP, IP_ADD_MEMBERSHIP, &mreq,
  395. sizeof(mreq)) < 0) {
  396. perror("setsockopt[UDP,IP_ADD_MEMBERSHIP]");
  397. iapp_deinit(iapp);
  398. return NULL;
  399. }
  400. iapp->packet_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  401. if (iapp->packet_sock < 0) {
  402. perror("socket[PF_PACKET,SOCK_RAW]");
  403. iapp_deinit(iapp);
  404. return NULL;
  405. }
  406. os_memset(&addr, 0, sizeof(addr));
  407. addr.sll_family = AF_PACKET;
  408. addr.sll_ifindex = ifindex;
  409. if (bind(iapp->packet_sock, (struct sockaddr *) &addr,
  410. sizeof(addr)) < 0) {
  411. perror("bind[PACKET]");
  412. iapp_deinit(iapp);
  413. return NULL;
  414. }
  415. if (eloop_register_read_sock(iapp->udp_sock, iapp_receive_udp,
  416. iapp, NULL)) {
  417. printf("Could not register read socket for IAPP.\n");
  418. iapp_deinit(iapp);
  419. return NULL;
  420. }
  421. printf("IEEE 802.11F (IAPP) using interface %s\n", iface);
  422. /* TODO: For levels 2 and 3: send RADIUS Initiate-Request, receive
  423. * RADIUS Initiate-Accept or Initiate-Reject. IAPP port should actually
  424. * be openned only after receiving Initiate-Accept. If Initiate-Reject
  425. * is received, IAPP is not started. */
  426. return iapp;
  427. }
  428. void iapp_deinit(struct iapp_data *iapp)
  429. {
  430. struct ip_mreqn mreq;
  431. if (iapp == NULL)
  432. return;
  433. if (iapp->udp_sock >= 0) {
  434. os_memset(&mreq, 0, sizeof(mreq));
  435. mreq.imr_multiaddr = iapp->multicast;
  436. mreq.imr_address.s_addr = INADDR_ANY;
  437. mreq.imr_ifindex = 0;
  438. if (setsockopt(iapp->udp_sock, SOL_IP, IP_DROP_MEMBERSHIP,
  439. &mreq, sizeof(mreq)) < 0) {
  440. perror("setsockopt[UDP,IP_DEL_MEMBERSHIP]");
  441. }
  442. eloop_unregister_read_sock(iapp->udp_sock);
  443. close(iapp->udp_sock);
  444. }
  445. if (iapp->packet_sock >= 0) {
  446. eloop_unregister_read_sock(iapp->packet_sock);
  447. close(iapp->packet_sock);
  448. }
  449. os_free(iapp);
  450. }
  451. int iapp_reconfig(struct hostapd_data *hapd, struct hostapd_config *oldconf,
  452. struct hostapd_bss_config *oldbss)
  453. {
  454. if (hapd->conf->ieee802_11f != oldbss->ieee802_11f ||
  455. os_strcmp(hapd->conf->iapp_iface, oldbss->iapp_iface) != 0) {
  456. iapp_deinit(hapd->iapp);
  457. hapd->iapp = NULL;
  458. if (hapd->conf->ieee802_11f) {
  459. hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface);
  460. if (hapd->iapp == NULL)
  461. return -1;
  462. }
  463. }
  464. return 0;
  465. }