iapp.c 15 KB

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