iapp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 extenting 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. void iapp_new_station(struct iapp_data *iapp, struct sta_info *sta)
  199. {
  200. struct ieee80211_mgmt *assoc;
  201. u16 seq;
  202. if (iapp == NULL)
  203. return;
  204. assoc = sta->last_assoc_req;
  205. seq = assoc ? WLAN_GET_SEQ_SEQ(le_to_host16(assoc->seq_ctrl)) : 0;
  206. /* IAPP-ADD.request(MAC Address, Sequence Number, Timeout) */
  207. hostapd_logger(iapp->hapd, sta->addr, HOSTAPD_MODULE_IAPP,
  208. HOSTAPD_LEVEL_DEBUG, "IAPP-ADD.request(seq=%d)", seq);
  209. iapp_send_layer2_update(iapp, sta->addr);
  210. iapp_send_add(iapp, sta->addr, seq);
  211. if (assoc && WLAN_FC_GET_STYPE(le_to_host16(assoc->frame_control)) ==
  212. WLAN_FC_STYPE_REASSOC_REQ) {
  213. /* IAPP-MOVE.request(MAC Address, Sequence Number, Old AP,
  214. * Context Block, Timeout)
  215. */
  216. /* TODO: Send IAPP-MOVE to the old AP; Map Old AP BSSID to
  217. * IP address */
  218. }
  219. }
  220. static void iapp_process_add_notify(struct iapp_data *iapp,
  221. struct sockaddr_in *from,
  222. struct iapp_hdr *hdr, int len)
  223. {
  224. struct iapp_add_notify *add = (struct iapp_add_notify *) (hdr + 1);
  225. struct sta_info *sta;
  226. if (len != sizeof(*add)) {
  227. printf("Invalid IAPP-ADD packet length %d (expected %lu)\n",
  228. len, (unsigned long) sizeof(*add));
  229. return;
  230. }
  231. sta = ap_get_sta(iapp->hapd, add->mac_addr);
  232. /* IAPP-ADD.indication(MAC Address, Sequence Number) */
  233. hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
  234. HOSTAPD_LEVEL_INFO,
  235. "Received IAPP ADD-notify (seq# %d) from %s:%d%s",
  236. be_to_host16(add->seq_num),
  237. inet_ntoa(from->sin_addr), ntohs(from->sin_port),
  238. sta ? "" : " (STA not found)");
  239. if (!sta)
  240. return;
  241. /* TODO: could use seq_num to try to determine whether last association
  242. * to this AP is newer than the one advertised in IAPP-ADD. Although,
  243. * this is not really a reliable verification. */
  244. hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
  245. HOSTAPD_LEVEL_DEBUG,
  246. "Removing STA due to IAPP ADD-notify");
  247. sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
  248. eloop_cancel_timeout(ap_handle_timer, iapp->hapd, sta);
  249. eloop_register_timeout(0, 0, ap_handle_timer, iapp->hapd, sta);
  250. sta->timeout_next = STA_REMOVE;
  251. }
  252. static void iapp_receive_udp(int sock, void *eloop_ctx, void *sock_ctx)
  253. {
  254. struct iapp_data *iapp = eloop_ctx;
  255. int len, hlen;
  256. unsigned char buf[128];
  257. struct sockaddr_in from;
  258. socklen_t fromlen;
  259. struct iapp_hdr *hdr;
  260. /* Handle incoming IAPP frames (over UDP/IP) */
  261. fromlen = sizeof(from);
  262. len = recvfrom(iapp->udp_sock, buf, sizeof(buf), 0,
  263. (struct sockaddr *) &from, &fromlen);
  264. if (len < 0) {
  265. perror("recvfrom");
  266. return;
  267. }
  268. if (from.sin_addr.s_addr == iapp->own.s_addr)
  269. return; /* ignore own IAPP messages */
  270. hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
  271. HOSTAPD_LEVEL_DEBUG,
  272. "Received %d byte IAPP frame from %s%s\n",
  273. len, inet_ntoa(from.sin_addr),
  274. len < (int) sizeof(*hdr) ? " (too short)" : "");
  275. if (len < (int) sizeof(*hdr))
  276. return;
  277. hdr = (struct iapp_hdr *) buf;
  278. hlen = be_to_host16(hdr->length);
  279. hostapd_logger(iapp->hapd, NULL, HOSTAPD_MODULE_IAPP,
  280. HOSTAPD_LEVEL_DEBUG,
  281. "RX: version=%d command=%d id=%d len=%d\n",
  282. hdr->version, hdr->command,
  283. be_to_host16(hdr->identifier), hlen);
  284. if (hdr->version != IAPP_VERSION) {
  285. printf("Dropping IAPP frame with unknown version %d\n",
  286. hdr->version);
  287. return;
  288. }
  289. if (hlen > len) {
  290. printf("Underflow IAPP frame (hlen=%d len=%d)\n", hlen, len);
  291. return;
  292. }
  293. if (hlen < len) {
  294. printf("Ignoring %d extra bytes from IAPP frame\n",
  295. len - hlen);
  296. len = hlen;
  297. }
  298. switch (hdr->command) {
  299. case IAPP_CMD_ADD_notify:
  300. iapp_process_add_notify(iapp, &from, hdr, hlen - sizeof(*hdr));
  301. break;
  302. case IAPP_CMD_MOVE_notify:
  303. /* TODO: MOVE is using TCP; so move this to TCP handler once it
  304. * is implemented.. */
  305. /* IAPP-MOVE.indication(MAC Address, New BSSID,
  306. * Sequence Number, AP Address, Context Block) */
  307. /* TODO: process */
  308. break;
  309. default:
  310. printf("Unknown IAPP command %d\n", hdr->command);
  311. break;
  312. }
  313. }
  314. struct iapp_data * iapp_init(struct hostapd_data *hapd, const char *iface)
  315. {
  316. struct ifreq ifr;
  317. struct sockaddr_ll addr;
  318. int ifindex;
  319. struct sockaddr_in *paddr, uaddr;
  320. struct iapp_data *iapp;
  321. struct ip_mreqn mreq;
  322. iapp = os_zalloc(sizeof(*iapp));
  323. if (iapp == NULL)
  324. return NULL;
  325. iapp->hapd = hapd;
  326. iapp->udp_sock = iapp->packet_sock = -1;
  327. /* TODO:
  328. * open socket for sending and receiving IAPP frames over TCP
  329. */
  330. iapp->udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
  331. if (iapp->udp_sock < 0) {
  332. perror("socket[PF_INET,SOCK_DGRAM]");
  333. iapp_deinit(iapp);
  334. return NULL;
  335. }
  336. os_memset(&ifr, 0, sizeof(ifr));
  337. os_strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
  338. if (ioctl(iapp->udp_sock, SIOCGIFINDEX, &ifr) != 0) {
  339. perror("ioctl(SIOCGIFINDEX)");
  340. iapp_deinit(iapp);
  341. return NULL;
  342. }
  343. ifindex = ifr.ifr_ifindex;
  344. if (ioctl(iapp->udp_sock, SIOCGIFADDR, &ifr) != 0) {
  345. perror("ioctl(SIOCGIFADDR)");
  346. iapp_deinit(iapp);
  347. return NULL;
  348. }
  349. paddr = (struct sockaddr_in *) &ifr.ifr_addr;
  350. if (paddr->sin_family != AF_INET) {
  351. printf("Invalid address family %i (SIOCGIFADDR)\n",
  352. paddr->sin_family);
  353. iapp_deinit(iapp);
  354. return NULL;
  355. }
  356. iapp->own.s_addr = paddr->sin_addr.s_addr;
  357. if (ioctl(iapp->udp_sock, SIOCGIFBRDADDR, &ifr) != 0) {
  358. perror("ioctl(SIOCGIFBRDADDR)");
  359. iapp_deinit(iapp);
  360. return NULL;
  361. }
  362. paddr = (struct sockaddr_in *) &ifr.ifr_addr;
  363. if (paddr->sin_family != AF_INET) {
  364. printf("Invalid address family %i (SIOCGIFBRDADDR)\n",
  365. paddr->sin_family);
  366. iapp_deinit(iapp);
  367. return NULL;
  368. }
  369. inet_aton(IAPP_MULTICAST, &iapp->multicast);
  370. os_memset(&uaddr, 0, sizeof(uaddr));
  371. uaddr.sin_family = AF_INET;
  372. uaddr.sin_port = htons(IAPP_UDP_PORT);
  373. if (bind(iapp->udp_sock, (struct sockaddr *) &uaddr,
  374. sizeof(uaddr)) < 0) {
  375. perror("bind[UDP]");
  376. iapp_deinit(iapp);
  377. return NULL;
  378. }
  379. os_memset(&mreq, 0, sizeof(mreq));
  380. mreq.imr_multiaddr = iapp->multicast;
  381. mreq.imr_address.s_addr = INADDR_ANY;
  382. mreq.imr_ifindex = 0;
  383. if (setsockopt(iapp->udp_sock, SOL_IP, IP_ADD_MEMBERSHIP, &mreq,
  384. sizeof(mreq)) < 0) {
  385. perror("setsockopt[UDP,IP_ADD_MEMBERSHIP]");
  386. iapp_deinit(iapp);
  387. return NULL;
  388. }
  389. iapp->packet_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  390. if (iapp->packet_sock < 0) {
  391. perror("socket[PF_PACKET,SOCK_RAW]");
  392. iapp_deinit(iapp);
  393. return NULL;
  394. }
  395. os_memset(&addr, 0, sizeof(addr));
  396. addr.sll_family = AF_PACKET;
  397. addr.sll_ifindex = ifindex;
  398. if (bind(iapp->packet_sock, (struct sockaddr *) &addr,
  399. sizeof(addr)) < 0) {
  400. perror("bind[PACKET]");
  401. iapp_deinit(iapp);
  402. return NULL;
  403. }
  404. if (eloop_register_read_sock(iapp->udp_sock, iapp_receive_udp,
  405. iapp, NULL)) {
  406. printf("Could not register read socket for IAPP.\n");
  407. iapp_deinit(iapp);
  408. return NULL;
  409. }
  410. printf("IEEE 802.11F (IAPP) using interface %s\n", iface);
  411. /* TODO: For levels 2 and 3: send RADIUS Initiate-Request, receive
  412. * RADIUS Initiate-Accept or Initiate-Reject. IAPP port should actually
  413. * be openned only after receiving Initiate-Accept. If Initiate-Reject
  414. * is received, IAPP is not started. */
  415. return iapp;
  416. }
  417. void iapp_deinit(struct iapp_data *iapp)
  418. {
  419. struct ip_mreqn mreq;
  420. if (iapp == NULL)
  421. return;
  422. if (iapp->udp_sock >= 0) {
  423. os_memset(&mreq, 0, sizeof(mreq));
  424. mreq.imr_multiaddr = iapp->multicast;
  425. mreq.imr_address.s_addr = INADDR_ANY;
  426. mreq.imr_ifindex = 0;
  427. if (setsockopt(iapp->udp_sock, SOL_IP, IP_DROP_MEMBERSHIP,
  428. &mreq, sizeof(mreq)) < 0) {
  429. perror("setsockopt[UDP,IP_DEL_MEMBERSHIP]");
  430. }
  431. eloop_unregister_read_sock(iapp->udp_sock);
  432. close(iapp->udp_sock);
  433. }
  434. if (iapp->packet_sock >= 0) {
  435. eloop_unregister_read_sock(iapp->packet_sock);
  436. close(iapp->packet_sock);
  437. }
  438. os_free(iapp);
  439. }
  440. int iapp_reconfig(struct hostapd_data *hapd, struct hostapd_config *oldconf,
  441. struct hostapd_bss_config *oldbss)
  442. {
  443. if (hapd->conf->ieee802_11f != oldbss->ieee802_11f ||
  444. os_strcmp(hapd->conf->iapp_iface, oldbss->iapp_iface) != 0) {
  445. iapp_deinit(hapd->iapp);
  446. hapd->iapp = NULL;
  447. if (hapd->conf->ieee802_11f) {
  448. hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface);
  449. if (hapd->iapp == NULL)
  450. return -1;
  451. }
  452. }
  453. return 0;
  454. }