gas.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Generic advertisement service (GAS) (IEEE 802.11u)
  3. * Copyright (c) 2009, Atheros Communications
  4. * Copyright (c) 2011-2012, Qualcomm Atheros
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "ieee802_11_defs.h"
  12. #include "gas.h"
  13. static struct wpabuf *
  14. gas_build_req(u8 action, u8 dialog_token, size_t size)
  15. {
  16. struct wpabuf *buf;
  17. buf = wpabuf_alloc(100 + size);
  18. if (buf == NULL)
  19. return NULL;
  20. wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
  21. wpabuf_put_u8(buf, action);
  22. wpabuf_put_u8(buf, dialog_token);
  23. return buf;
  24. }
  25. struct wpabuf * gas_build_initial_req(u8 dialog_token, size_t size)
  26. {
  27. return gas_build_req(WLAN_PA_GAS_INITIAL_REQ, dialog_token,
  28. size);
  29. }
  30. struct wpabuf * gas_build_comeback_req(u8 dialog_token)
  31. {
  32. return gas_build_req(WLAN_PA_GAS_COMEBACK_REQ, dialog_token, 0);
  33. }
  34. static struct wpabuf *
  35. gas_build_resp(u8 action, u8 dialog_token, u16 status_code, u8 frag_id,
  36. u8 more, u16 comeback_delay, size_t size)
  37. {
  38. struct wpabuf *buf;
  39. buf = wpabuf_alloc(100 + size);
  40. if (buf == NULL)
  41. return NULL;
  42. wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
  43. wpabuf_put_u8(buf, action);
  44. wpabuf_put_u8(buf, dialog_token);
  45. wpabuf_put_le16(buf, status_code);
  46. if (action == WLAN_PA_GAS_COMEBACK_RESP)
  47. wpabuf_put_u8(buf, frag_id | (more ? 0x80 : 0));
  48. wpabuf_put_le16(buf, comeback_delay);
  49. return buf;
  50. }
  51. struct wpabuf *
  52. gas_build_initial_resp(u8 dialog_token, u16 status_code, u16 comeback_delay,
  53. size_t size)
  54. {
  55. return gas_build_resp(WLAN_PA_GAS_INITIAL_RESP, dialog_token,
  56. status_code, 0, 0, comeback_delay, size);
  57. }
  58. static struct wpabuf *
  59. gas_build_comeback_resp(u8 dialog_token, u16 status_code, u8 frag_id, u8 more,
  60. u16 comeback_delay, size_t size)
  61. {
  62. return gas_build_resp(WLAN_PA_GAS_COMEBACK_RESP, dialog_token,
  63. status_code, frag_id, more, comeback_delay,
  64. size);
  65. }
  66. /**
  67. * gas_add_adv_proto_anqp - Add an Advertisement Protocol element
  68. * @buf: Buffer to which the element is added
  69. * @query_resp_len_limit: Query Response Length Limit in units of 256 octets
  70. * @pame_bi: Pre-Association Message Exchange BSSID Independent (0/1)
  71. *
  72. *
  73. * @query_resp_len_limit is 0 for request and 1-0x7f for response. 0x7f means
  74. * that the maximum limit is determined by the maximum allowable number of
  75. * fragments in the GAS Query Response Fragment ID.
  76. */
  77. static void gas_add_adv_proto_anqp(struct wpabuf *buf, u8 query_resp_len_limit,
  78. u8 pame_bi)
  79. {
  80. /* Advertisement Protocol IE */
  81. wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
  82. wpabuf_put_u8(buf, 2); /* Length */
  83. wpabuf_put_u8(buf, (query_resp_len_limit & 0x7f) |
  84. (pame_bi ? 0x80 : 0));
  85. /* Advertisement Protocol */
  86. wpabuf_put_u8(buf, ACCESS_NETWORK_QUERY_PROTOCOL);
  87. }
  88. struct wpabuf * gas_anqp_build_initial_req(u8 dialog_token, size_t size)
  89. {
  90. struct wpabuf *buf;
  91. buf = gas_build_initial_req(dialog_token, 4 + size);
  92. if (buf == NULL)
  93. return NULL;
  94. gas_add_adv_proto_anqp(buf, 0, 0);
  95. wpabuf_put(buf, 2); /* Query Request Length to be filled */
  96. return buf;
  97. }
  98. struct wpabuf * gas_anqp_build_initial_resp(u8 dialog_token, u16 status_code,
  99. u16 comeback_delay, size_t size)
  100. {
  101. struct wpabuf *buf;
  102. buf = gas_build_initial_resp(dialog_token, status_code, comeback_delay,
  103. 4 + size);
  104. if (buf == NULL)
  105. return NULL;
  106. gas_add_adv_proto_anqp(buf, 0x7f, 0);
  107. wpabuf_put(buf, 2); /* Query Response Length to be filled */
  108. return buf;
  109. }
  110. struct wpabuf * gas_anqp_build_initial_resp_buf(u8 dialog_token,
  111. u16 status_code,
  112. u16 comeback_delay,
  113. struct wpabuf *payload)
  114. {
  115. struct wpabuf *buf;
  116. buf = gas_anqp_build_initial_resp(dialog_token, status_code,
  117. comeback_delay,
  118. payload ? wpabuf_len(payload) : 0);
  119. if (buf == NULL)
  120. return NULL;
  121. if (payload)
  122. wpabuf_put_buf(buf, payload);
  123. gas_anqp_set_len(buf);
  124. return buf;
  125. }
  126. struct wpabuf * gas_anqp_build_comeback_resp(u8 dialog_token, u16 status_code,
  127. u8 frag_id, u8 more,
  128. u16 comeback_delay, size_t size)
  129. {
  130. struct wpabuf *buf;
  131. buf = gas_build_comeback_resp(dialog_token, status_code,
  132. frag_id, more, comeback_delay, 4 + size);
  133. if (buf == NULL)
  134. return NULL;
  135. gas_add_adv_proto_anqp(buf, 0x7f, 0);
  136. wpabuf_put(buf, 2); /* Query Response Length to be filled */
  137. return buf;
  138. }
  139. struct wpabuf * gas_anqp_build_comeback_resp_buf(u8 dialog_token,
  140. u16 status_code,
  141. u8 frag_id, u8 more,
  142. u16 comeback_delay,
  143. struct wpabuf *payload)
  144. {
  145. struct wpabuf *buf;
  146. buf = gas_anqp_build_comeback_resp(dialog_token, status_code, frag_id,
  147. more, comeback_delay,
  148. payload ? wpabuf_len(payload) : 0);
  149. if (buf == NULL)
  150. return NULL;
  151. if (payload)
  152. wpabuf_put_buf(buf, payload);
  153. gas_anqp_set_len(buf);
  154. return buf;
  155. }
  156. /**
  157. * gas_anqp_set_len - Set Query Request/Response Length
  158. * @buf: GAS message
  159. *
  160. * This function is used to update the Query Request/Response Length field once
  161. * the payload has been filled.
  162. */
  163. void gas_anqp_set_len(struct wpabuf *buf)
  164. {
  165. u8 action;
  166. size_t offset;
  167. u8 *len;
  168. if (buf == NULL || wpabuf_len(buf) < 2)
  169. return;
  170. action = *(wpabuf_head_u8(buf) + 1);
  171. switch (action) {
  172. case WLAN_PA_GAS_INITIAL_REQ:
  173. offset = 3 + 4;
  174. break;
  175. case WLAN_PA_GAS_INITIAL_RESP:
  176. offset = 7 + 4;
  177. break;
  178. case WLAN_PA_GAS_COMEBACK_RESP:
  179. offset = 8 + 4;
  180. break;
  181. default:
  182. return;
  183. }
  184. if (wpabuf_len(buf) < offset + 2)
  185. return;
  186. len = wpabuf_mhead_u8(buf) + offset;
  187. WPA_PUT_LE16(len, (u8 *) wpabuf_put(buf, 0) - len - 2);
  188. }
  189. /**
  190. * gas_anqp_add_element - Add ANQP element header
  191. * @buf: GAS message
  192. * @info_id: ANQP Info ID
  193. * Returns: Pointer to the Length field for gas_anqp_set_element_len()
  194. */
  195. u8 * gas_anqp_add_element(struct wpabuf *buf, u16 info_id)
  196. {
  197. wpabuf_put_le16(buf, info_id);
  198. return wpabuf_put(buf, 2); /* Length to be filled */
  199. }
  200. /**
  201. * gas_anqp_set_element_len - Update ANQP element Length field
  202. * @buf: GAS message
  203. * @len_pos: Length field position from gas_anqp_add_element()
  204. *
  205. * This function is called after the ANQP element payload has been added to the
  206. * buffer.
  207. */
  208. void gas_anqp_set_element_len(struct wpabuf *buf, u8 *len_pos)
  209. {
  210. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(buf, 0) - len_pos - 2);
  211. }