You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

iapp.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 "utils/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 "utils/common.h"
  47. #include "utils/eloop.h"
  48. #include "common/ieee802_11_defs.h"
  49. #include "hostapd.h"
  50. #include "ap_config.h"
  51. #include "ieee802_11.h"
  52. #include "sta_info.h"
  53. #include "iapp.h"
  54. #define IAPP_MULTICAST "224.0.1.178"
  55. #define IAPP_UDP_PORT 3517
  56. #define IAPP_TCP_PORT 3517
  57. struct iapp_hdr {
  58. u8 version;
  59. u8 command;
  60. be16 identifier;
  61. be16 length;
  62. /* followed by length-6 octets of data */
  63. } __attribute__ ((packed));
  64. #define IAPP_VERSION 0
  65. enum IAPP_COMMAND {
  66. IAPP_CMD_ADD_notify = 0,
  67. IAPP_CMD_MOVE_notify = 1,
  68. IAPP_CMD_MOVE_response = 2,
  69. IAPP_CMD_Send_Security_Block = 3,
  70. IAPP_CMD_ACK_Security_Block = 4,
  71. IAPP_CMD_CACHE_notify = 5,
  72. IAPP_CMD_CACHE_response = 6,
  73. };
  74. /* ADD-notify - multicast UDP on the local LAN */
  75. struct iapp_add_notify {
  76. u8 addr_len; /* ETH_ALEN */
  77. u8 reserved;
  78. u8 mac_addr[ETH_ALEN];
  79. be16 seq_num;
  80. } __attribute__ ((packed));
  81. /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
  82. struct iapp_layer2_update {
  83. u8 da[ETH_ALEN]; /* broadcast */
  84. u8 sa[ETH_ALEN]; /* STA addr */
  85. be16 len; /* 6 */
  86. u8 dsap; /* null DSAP address */
  87. u8 ssap; /* null SSAP address, CR=Response */
  88. u8 control;
  89. u8 xid_info[3];
  90. } __attribute__ ((packed));
  91. /* MOVE-notify - unicast TCP */
  92. struct iapp_move_notify {
  93. u8 addr_len; /* ETH_ALEN */
  94. u8 reserved;
  95. u8 mac_addr[ETH_ALEN];
  96. u16 seq_num;
  97. u16 ctx_block_len;
  98. /* followed by ctx_block_len bytes */
  99. } __attribute__ ((packed));
  100. /* MOVE-response - unicast TCP */
  101. struct iapp_move_response {
  102. u8 addr_len; /* ETH_ALEN */
  103. u8 status;
  104. u8 mac_addr[ETH_ALEN];
  105. u16 seq_num;
  106. u16 ctx_block_len;
  107. /* followed by ctx_block_len bytes */
  108. } __attribute__ ((packed));
  109. enum {
  110. IAPP_MOVE_SUCCESSFUL = 0,
  111. IAPP_MOVE_DENIED = 1,
  112. IAPP_MOVE_STALE_MOVE = 2,
  113. };
  114. /* CACHE-notify */
  115. struct iapp_cache_notify {
  116. u8 addr_len; /* ETH_ALEN */
  117. u8 reserved;
  118. u8 mac_addr[ETH_ALEN];
  119. u16 seq_num;
  120. u8 current_ap[ETH_ALEN];
  121. u16 ctx_block_len;
  122. /* ctx_block_len bytes of context block followed by 16-bit context
  123. * timeout */
  124. } __attribute__ ((packed));
  125. /* CACHE-response - unicast TCP */
  126. struct iapp_cache_response {
  127. u8 addr_len; /* ETH_ALEN */
  128. u8 status;
  129. u8 mac_addr[ETH_ALEN];
  130. u16 seq_num;
  131. } __attribute__ ((packed));
  132. enum {
  133. IAPP_CACHE_SUCCESSFUL = 0,
  134. IAPP_CACHE_STALE_CACHE = 1,
  135. };
  136. /* Send-Security-Block - unicast TCP */
  137. struct iapp_send_security_block {
  138. u8 iv[8];
  139. u16 sec_block_len;
  140. /* followed by sec_block_len bytes of security block */
  141. } __attribute__ ((packed));
  142. /* ACK-Security-Block - unicast TCP */
  143. struct iapp_ack_security_block {
  144. u8 iv[8];
  145. u8 new_ap_ack_authenticator[48];
  146. } __attribute__ ((packed));
  147. struct iapp_data {
  148. struct hostapd_data *hapd;
  149. u16 identifier; /* next IAPP identifier */
  150. struct in_addr own, multicast;
  151. int udp_sock;
  152. int packet_sock;
  153. };
  154. static void iapp_send_add(struct iapp_data *iapp, u8 *mac_addr, u16 seq_num)
  155. {
  156. char buf[128];
  157. struct iapp_hdr *hdr;
  158. struct iapp_add_notify *add;
  159. struct sockaddr_in addr;
  160. /* Send IAPP ADD-notify to remove possible association from other APs
  161. */
  162. hdr = (struct iapp_hdr *) buf;
  163. hdr->version = IAPP_VERSION;
  164. hdr->command = IAPP_CMD_ADD_notify;
  165. hdr->identifier = host_to_be16(iapp->identifier++);
  166. hdr->length = host_to_be16(sizeof(*hdr) + sizeof(*add));
  167. add = (struct iapp_add_notify *) (hdr + 1);
  168. add->addr_len = ETH_ALEN;
  169. add->reserved = 0;
  170. os_memcpy(add->mac_addr, mac_addr, ETH_ALEN);
  171. add->seq_num = host_to_be16(seq_num);
  172. os_memset(&addr, 0, sizeof(addr));
  173. addr.sin_family = AF_INET;
  174. addr.sin_addr.s_addr = iapp->multicast.s_addr;
  175. addr.sin_port = htons(IAPP_UDP_PORT);
  176. if (sendto(iapp->udp_sock, buf, (char *) (add + 1) - buf, 0,
  177. (struct sockaddr *) &addr, sizeof(addr)) < 0)
  178. perror("sendto[IAPP-ADD]");
  179. }
  180. static void iapp_send_layer2_update(struct iapp_data *iapp, u8 *addr)
  181. {
  182. struct iapp_layer2_update msg;
  183. /* Send Level 2 Update Frame to update forwarding tables in layer 2
  184. * bridge devices */
  185. /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
  186. * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
  187. os_memset(msg.da, 0xff, ETH_ALEN);
  188. os_memcpy(msg.sa, addr, ETH_ALEN);
  189. msg.len = host_to_be16(6);
  190. msg.dsap = 0; /* NULL DSAP address */
  191. msg.ssap = 0x01; /* NULL SSAP address, CR Bit: Response */
  192. msg.control = 0xaf; /* XID response lsb.1111F101.
  193. * F=0 (no poll command; unsolicited frame) */
  194. msg.xid_info[0] = 0x81; /* XID format identifier */
  195. msg.xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
  196. msg.xid_info[2] = 1 << 1; /* XID sender's receive window size (RW)
  197. * FIX: what is correct RW with 802.11? */
  198. if (send(iapp->packet_sock, &msg, sizeof(msg), 0) < 0)
  199. perror("send[L2 Update]");
  200. }
  201. /**
  202. * iapp_new_station - IAPP processing for a new STA
  203. * @iapp: IAPP data
  204. * @sta: The associated station
  205. */
  206. void iapp_new_station(struct iapp_data *iapp, struct sta_info *sta)
  207. {
  208. struct ieee80211_mgmt *assoc;
  209. u16 seq;
  210. if (iapp == NULL)
  211. return;
  212. assoc = sta->last_assoc_req;
  213. seq = assoc ? WLAN_GET_SEQ_SEQ(le_to_host16(assoc->seq_ctrl)) : 0;
  214. /* IAPP-ADD.request(MAC Address, Sequence Number, Timeout) */
  215. hostapd_logger(iapp->hapd, sta->addr, HOSTAPD_MODULE_IAPP,
  216. HOSTAPD_LEVEL_DEBUG, "IAPP-ADD.request(seq=%d)", seq);
  217. iapp_send_layer2_update(iapp, sta->addr);
  218. iapp_send_add(iapp, sta->addr, seq);
  219. if (assoc && WLAN_FC_GET_STYPE(le_to_host16(assoc->frame_control)) ==
  220. WLAN_FC_STYPE_REASSOC_REQ) {
  221. /* IAPP-MOVE.request(MAC Address, Sequence Number, Old AP,
  222. * Context Block, Timeout)
  223. */
  224. /* TODO: Send IAPP-MOVE to the old AP; Map Old AP BSSID to
  225. * IP address */
  226. }
  227. }
  228. static void iapp_process_add_notify(struct iapp_data *iapp,
  229. struct sockaddr_in *from,
  230. struct iapp_hdr *hdr, int len)
  231. {
  232. struct iapp_add_notify *add = (struct iapp_add_notify *) (hdr + 1);
  233. struct sta_info *sta;
  234. if (len != sizeof(*add)) {
  235. printf("Invalid IAPP-ADD packet length %d (expected %lu)\n",
  236. len, (unsigned long) sizeof(*add));
  237. return;
  238. }
  239. sta = ap_get_sta(iapp->hapd, add->mac_addr);
  240. /* IAPP-ADD.indication(MAC Address, Sequence Number) */
  241. hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
  242. HOSTAPD_LEVEL_INFO,
  243. "Received IAPP ADD-notify (seq# %d) from %s:%d%s",
  244. be_to_host16(add->seq_num),
  245. inet_ntoa(from->sin_addr), ntohs(from->sin_port),
  246. sta ? "" : " (STA not found)");
  247. if (!sta)
  248. return;
  249. /* TODO: could use seq_num to try to determine whether last association
  250. * to this AP is newer than the one advertised in IAPP-ADD. Although,
  251. * this is not really a reliable verification. */
  252. hostapd_logger(iapp->hapd, add->mac_addr, HOSTAPD_MODULE_IAPP,
  253. HOSTAPD_LEVEL_DEBUG,
  254. "Removing STA due to IAPP ADD-notify");
  255. ap_sta_disconnect(iapp->hapd, sta, NULL, 0);
  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. }