iapp.c 14 KB

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