l2cap.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (C) 2000-2001 Qualcomm Incorporated
  4. Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
  5. Copyright (C) 2010 Google Inc.
  6. Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License version 2 as
  9. published by the Free Software Foundation;
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  11. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  13. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  14. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  15. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  19. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  20. SOFTWARE IS DISCLAIMED.
  21. */
  22. #ifndef __L2CAP_H
  23. #define __L2CAP_H
  24. #include <linux/unaligned.h>
  25. #include <linux/atomic.h>
  26. /* L2CAP defaults */
  27. #define L2CAP_DEFAULT_MTU 672
  28. #define L2CAP_DEFAULT_MIN_MTU 48
  29. #define L2CAP_DEFAULT_FLUSH_TO 0xFFFF
  30. #define L2CAP_EFS_DEFAULT_FLUSH_TO 0xFFFFFFFF
  31. #define L2CAP_DEFAULT_TX_WINDOW 63
  32. #define L2CAP_DEFAULT_EXT_WINDOW 0x3FFF
  33. #define L2CAP_DEFAULT_MAX_TX 3
  34. #define L2CAP_DEFAULT_RETRANS_TO 2000 /* 2 seconds */
  35. #define L2CAP_DEFAULT_MONITOR_TO 12000 /* 12 seconds */
  36. #define L2CAP_DEFAULT_MAX_PDU_SIZE 1492 /* Sized for AMP packet */
  37. #define L2CAP_DEFAULT_ACK_TO 200
  38. #define L2CAP_DEFAULT_MAX_SDU_SIZE 0xFFFF
  39. #define L2CAP_DEFAULT_SDU_ITIME 0xFFFFFFFF
  40. #define L2CAP_DEFAULT_ACC_LAT 0xFFFFFFFF
  41. #define L2CAP_BREDR_MAX_PAYLOAD 1019 /* 3-DH5 packet */
  42. #define L2CAP_LE_MIN_MTU 23
  43. #define L2CAP_ECRED_CONN_SCID_MAX 5
  44. #define L2CAP_DISC_TIMEOUT msecs_to_jiffies(100)
  45. #define L2CAP_DISC_REJ_TIMEOUT msecs_to_jiffies(5000)
  46. #define L2CAP_ENC_TIMEOUT msecs_to_jiffies(5000)
  47. #define L2CAP_CONN_TIMEOUT msecs_to_jiffies(40000)
  48. #define L2CAP_INFO_TIMEOUT msecs_to_jiffies(4000)
  49. #define L2CAP_MOVE_TIMEOUT msecs_to_jiffies(4000)
  50. #define L2CAP_MOVE_ERTX_TIMEOUT msecs_to_jiffies(60000)
  51. #define L2CAP_WAIT_ACK_POLL_PERIOD msecs_to_jiffies(200)
  52. #define L2CAP_WAIT_ACK_TIMEOUT msecs_to_jiffies(10000)
  53. /* L2CAP socket address */
  54. struct sockaddr_l2 {
  55. sa_family_t l2_family;
  56. __le16 l2_psm;
  57. bdaddr_t l2_bdaddr;
  58. __le16 l2_cid;
  59. __u8 l2_bdaddr_type;
  60. };
  61. /* L2CAP socket options */
  62. #define L2CAP_OPTIONS 0x01
  63. struct l2cap_options {
  64. __u16 omtu;
  65. __u16 imtu;
  66. __u16 flush_to;
  67. __u8 mode;
  68. __u8 fcs;
  69. __u8 max_tx;
  70. __u16 txwin_size;
  71. };
  72. #define L2CAP_CONNINFO 0x02
  73. struct l2cap_conninfo {
  74. __u16 hci_handle;
  75. __u8 dev_class[3];
  76. };
  77. #define L2CAP_LM 0x03
  78. #define L2CAP_LM_MASTER 0x0001
  79. #define L2CAP_LM_AUTH 0x0002
  80. #define L2CAP_LM_ENCRYPT 0x0004
  81. #define L2CAP_LM_TRUSTED 0x0008
  82. #define L2CAP_LM_RELIABLE 0x0010
  83. #define L2CAP_LM_SECURE 0x0020
  84. #define L2CAP_LM_FIPS 0x0040
  85. /* L2CAP command codes */
  86. #define L2CAP_COMMAND_REJ 0x01
  87. #define L2CAP_CONN_REQ 0x02
  88. #define L2CAP_CONN_RSP 0x03
  89. #define L2CAP_CONF_REQ 0x04
  90. #define L2CAP_CONF_RSP 0x05
  91. #define L2CAP_DISCONN_REQ 0x06
  92. #define L2CAP_DISCONN_RSP 0x07
  93. #define L2CAP_ECHO_REQ 0x08
  94. #define L2CAP_ECHO_RSP 0x09
  95. #define L2CAP_INFO_REQ 0x0a
  96. #define L2CAP_INFO_RSP 0x0b
  97. #define L2CAP_CONN_PARAM_UPDATE_REQ 0x12
  98. #define L2CAP_CONN_PARAM_UPDATE_RSP 0x13
  99. #define L2CAP_LE_CONN_REQ 0x14
  100. #define L2CAP_LE_CONN_RSP 0x15
  101. #define L2CAP_LE_CREDITS 0x16
  102. #define L2CAP_ECRED_CONN_REQ 0x17
  103. #define L2CAP_ECRED_CONN_RSP 0x18
  104. #define L2CAP_ECRED_RECONF_REQ 0x19
  105. #define L2CAP_ECRED_RECONF_RSP 0x1a
  106. /* L2CAP extended feature mask */
  107. #define L2CAP_FEAT_FLOWCTL 0x00000001
  108. #define L2CAP_FEAT_RETRANS 0x00000002
  109. #define L2CAP_FEAT_BIDIR_QOS 0x00000004
  110. #define L2CAP_FEAT_ERTM 0x00000008
  111. #define L2CAP_FEAT_STREAMING 0x00000010
  112. #define L2CAP_FEAT_FCS 0x00000020
  113. #define L2CAP_FEAT_EXT_FLOW 0x00000040
  114. #define L2CAP_FEAT_FIXED_CHAN 0x00000080
  115. #define L2CAP_FEAT_EXT_WINDOW 0x00000100
  116. #define L2CAP_FEAT_UCD 0x00000200
  117. /* L2CAP checksum option */
  118. #define L2CAP_FCS_NONE 0x00
  119. #define L2CAP_FCS_CRC16 0x01
  120. /* L2CAP fixed channels */
  121. #define L2CAP_FC_SIG_BREDR 0x02
  122. #define L2CAP_FC_CONNLESS 0x04
  123. #define L2CAP_FC_ATT 0x10
  124. #define L2CAP_FC_SIG_LE 0x20
  125. #define L2CAP_FC_SMP_LE 0x40
  126. #define L2CAP_FC_SMP_BREDR 0x80
  127. /* L2CAP Control Field bit masks */
  128. #define L2CAP_CTRL_SAR 0xC000
  129. #define L2CAP_CTRL_REQSEQ 0x3F00
  130. #define L2CAP_CTRL_TXSEQ 0x007E
  131. #define L2CAP_CTRL_SUPERVISE 0x000C
  132. #define L2CAP_CTRL_RETRANS 0x0080
  133. #define L2CAP_CTRL_FINAL 0x0080
  134. #define L2CAP_CTRL_POLL 0x0010
  135. #define L2CAP_CTRL_FRAME_TYPE 0x0001 /* I- or S-Frame */
  136. #define L2CAP_CTRL_TXSEQ_SHIFT 1
  137. #define L2CAP_CTRL_SUPER_SHIFT 2
  138. #define L2CAP_CTRL_POLL_SHIFT 4
  139. #define L2CAP_CTRL_FINAL_SHIFT 7
  140. #define L2CAP_CTRL_REQSEQ_SHIFT 8
  141. #define L2CAP_CTRL_SAR_SHIFT 14
  142. /* L2CAP Extended Control Field bit mask */
  143. #define L2CAP_EXT_CTRL_TXSEQ 0xFFFC0000
  144. #define L2CAP_EXT_CTRL_SAR 0x00030000
  145. #define L2CAP_EXT_CTRL_SUPERVISE 0x00030000
  146. #define L2CAP_EXT_CTRL_REQSEQ 0x0000FFFC
  147. #define L2CAP_EXT_CTRL_POLL 0x00040000
  148. #define L2CAP_EXT_CTRL_FINAL 0x00000002
  149. #define L2CAP_EXT_CTRL_FRAME_TYPE 0x00000001 /* I- or S-Frame */
  150. #define L2CAP_EXT_CTRL_FINAL_SHIFT 1
  151. #define L2CAP_EXT_CTRL_REQSEQ_SHIFT 2
  152. #define L2CAP_EXT_CTRL_SAR_SHIFT 16
  153. #define L2CAP_EXT_CTRL_SUPER_SHIFT 16
  154. #define L2CAP_EXT_CTRL_POLL_SHIFT 18
  155. #define L2CAP_EXT_CTRL_TXSEQ_SHIFT 18
  156. /* L2CAP Supervisory Function */
  157. #define L2CAP_SUPER_RR 0x00
  158. #define L2CAP_SUPER_REJ 0x01
  159. #define L2CAP_SUPER_RNR 0x02
  160. #define L2CAP_SUPER_SREJ 0x03
  161. /* L2CAP Segmentation and Reassembly */
  162. #define L2CAP_SAR_UNSEGMENTED 0x00
  163. #define L2CAP_SAR_START 0x01
  164. #define L2CAP_SAR_END 0x02
  165. #define L2CAP_SAR_CONTINUE 0x03
  166. /* L2CAP Command rej. reasons */
  167. #define L2CAP_REJ_NOT_UNDERSTOOD 0x0000
  168. #define L2CAP_REJ_MTU_EXCEEDED 0x0001
  169. #define L2CAP_REJ_INVALID_CID 0x0002
  170. /* L2CAP structures */
  171. struct l2cap_hdr {
  172. __le16 len;
  173. __le16 cid;
  174. } __packed;
  175. #define L2CAP_LEN_SIZE 2
  176. #define L2CAP_HDR_SIZE 4
  177. #define L2CAP_ENH_HDR_SIZE 6
  178. #define L2CAP_EXT_HDR_SIZE 8
  179. #define L2CAP_FCS_SIZE 2
  180. #define L2CAP_SDULEN_SIZE 2
  181. #define L2CAP_PSMLEN_SIZE 2
  182. #define L2CAP_ENH_CTRL_SIZE 2
  183. #define L2CAP_EXT_CTRL_SIZE 4
  184. struct l2cap_cmd_hdr {
  185. __u8 code;
  186. __u8 ident;
  187. __le16 len;
  188. } __packed;
  189. #define L2CAP_CMD_HDR_SIZE 4
  190. struct l2cap_cmd_rej_unk {
  191. __le16 reason;
  192. } __packed;
  193. struct l2cap_cmd_rej_mtu {
  194. __le16 reason;
  195. __le16 max_mtu;
  196. } __packed;
  197. struct l2cap_cmd_rej_cid {
  198. __le16 reason;
  199. __le16 scid;
  200. __le16 dcid;
  201. } __packed;
  202. struct l2cap_conn_req {
  203. __le16 psm;
  204. __le16 scid;
  205. } __packed;
  206. struct l2cap_conn_rsp {
  207. __le16 dcid;
  208. __le16 scid;
  209. __le16 result;
  210. __le16 status;
  211. } __packed;
  212. /* protocol/service multiplexer (PSM) */
  213. #define L2CAP_PSM_SDP 0x0001
  214. #define L2CAP_PSM_RFCOMM 0x0003
  215. #define L2CAP_PSM_3DSP 0x0021
  216. #define L2CAP_PSM_IPSP 0x0023 /* 6LoWPAN */
  217. #define L2CAP_PSM_DYN_START 0x1001
  218. #define L2CAP_PSM_DYN_END 0xffff
  219. #define L2CAP_PSM_AUTO_END 0x10ff
  220. #define L2CAP_PSM_LE_DYN_START 0x0080
  221. #define L2CAP_PSM_LE_DYN_END 0x00ff
  222. /* channel identifier */
  223. #define L2CAP_CID_SIGNALING 0x0001
  224. #define L2CAP_CID_CONN_LESS 0x0002
  225. #define L2CAP_CID_ATT 0x0004
  226. #define L2CAP_CID_LE_SIGNALING 0x0005
  227. #define L2CAP_CID_SMP 0x0006
  228. #define L2CAP_CID_SMP_BREDR 0x0007
  229. #define L2CAP_CID_DYN_START 0x0040
  230. #define L2CAP_CID_DYN_END 0xffff
  231. #define L2CAP_CID_LE_DYN_END 0x007f
  232. /* connect/create channel results */
  233. #define L2CAP_CR_SUCCESS 0x0000
  234. #define L2CAP_CR_PEND 0x0001
  235. #define L2CAP_CR_BAD_PSM 0x0002
  236. #define L2CAP_CR_SEC_BLOCK 0x0003
  237. #define L2CAP_CR_NO_MEM 0x0004
  238. #define L2CAP_CR_INVALID_SCID 0x0006
  239. #define L2CAP_CR_SCID_IN_USE 0x0007
  240. /* credit based connect results */
  241. #define L2CAP_CR_LE_SUCCESS 0x0000
  242. #define L2CAP_CR_LE_BAD_PSM 0x0002
  243. #define L2CAP_CR_LE_NO_MEM 0x0004
  244. #define L2CAP_CR_LE_AUTHENTICATION 0x0005
  245. #define L2CAP_CR_LE_AUTHORIZATION 0x0006
  246. #define L2CAP_CR_LE_BAD_KEY_SIZE 0x0007
  247. #define L2CAP_CR_LE_ENCRYPTION 0x0008
  248. #define L2CAP_CR_LE_INVALID_SCID 0x0009
  249. #define L2CAP_CR_LE_SCID_IN_USE 0x000A
  250. #define L2CAP_CR_LE_UNACCEPT_PARAMS 0x000B
  251. #define L2CAP_CR_LE_INVALID_PARAMS 0x000C
  252. /* connect/create channel status */
  253. #define L2CAP_CS_NO_INFO 0x0000
  254. #define L2CAP_CS_AUTHEN_PEND 0x0001
  255. #define L2CAP_CS_AUTHOR_PEND 0x0002
  256. struct l2cap_conf_req {
  257. __le16 dcid;
  258. __le16 flags;
  259. __u8 data[];
  260. } __packed;
  261. struct l2cap_conf_rsp {
  262. __le16 scid;
  263. __le16 flags;
  264. __le16 result;
  265. __u8 data[];
  266. } __packed;
  267. #define L2CAP_CONF_SUCCESS 0x0000
  268. #define L2CAP_CONF_UNACCEPT 0x0001
  269. #define L2CAP_CONF_REJECT 0x0002
  270. #define L2CAP_CONF_UNKNOWN 0x0003
  271. #define L2CAP_CONF_PENDING 0x0004
  272. #define L2CAP_CONF_EFS_REJECT 0x0005
  273. /* configuration req/rsp continuation flag */
  274. #define L2CAP_CONF_FLAG_CONTINUATION 0x0001
  275. struct l2cap_conf_opt {
  276. __u8 type;
  277. __u8 len;
  278. __u8 val[];
  279. } __packed;
  280. #define L2CAP_CONF_OPT_SIZE 2
  281. #define L2CAP_CONF_HINT 0x80
  282. #define L2CAP_CONF_MASK 0x7f
  283. #define L2CAP_CONF_MTU 0x01
  284. #define L2CAP_CONF_FLUSH_TO 0x02
  285. #define L2CAP_CONF_QOS 0x03
  286. #define L2CAP_CONF_RFC 0x04
  287. #define L2CAP_CONF_FCS 0x05
  288. #define L2CAP_CONF_EFS 0x06
  289. #define L2CAP_CONF_EWS 0x07
  290. #define L2CAP_CONF_MAX_SIZE 22
  291. struct l2cap_conf_rfc {
  292. __u8 mode;
  293. __u8 txwin_size;
  294. __u8 max_transmit;
  295. __le16 retrans_timeout;
  296. __le16 monitor_timeout;
  297. __le16 max_pdu_size;
  298. } __packed;
  299. #define L2CAP_MODE_BASIC 0x00
  300. #define L2CAP_MODE_RETRANS 0x01
  301. #define L2CAP_MODE_FLOWCTL 0x02
  302. #define L2CAP_MODE_ERTM 0x03
  303. #define L2CAP_MODE_STREAMING 0x04
  304. /* Unlike the above this one doesn't actually map to anything that would
  305. * ever be sent over the air. Therefore, use a value that's unlikely to
  306. * ever be used in the BR/EDR configuration phase.
  307. */
  308. #define L2CAP_MODE_LE_FLOWCTL 0x80
  309. #define L2CAP_MODE_EXT_FLOWCTL 0x81
  310. struct l2cap_conf_efs {
  311. __u8 id;
  312. __u8 stype;
  313. __le16 msdu;
  314. __le32 sdu_itime;
  315. __le32 acc_lat;
  316. __le32 flush_to;
  317. } __packed;
  318. #define L2CAP_SERV_NOTRAFIC 0x00
  319. #define L2CAP_SERV_BESTEFFORT 0x01
  320. #define L2CAP_SERV_GUARANTEED 0x02
  321. #define L2CAP_BESTEFFORT_ID 0x01
  322. struct l2cap_disconn_req {
  323. __le16 dcid;
  324. __le16 scid;
  325. } __packed;
  326. struct l2cap_disconn_rsp {
  327. __le16 dcid;
  328. __le16 scid;
  329. } __packed;
  330. struct l2cap_info_req {
  331. __le16 type;
  332. } __packed;
  333. struct l2cap_info_rsp {
  334. __le16 type;
  335. __le16 result;
  336. __u8 data[];
  337. } __packed;
  338. #define L2CAP_MR_SUCCESS 0x0000
  339. #define L2CAP_MR_PEND 0x0001
  340. #define L2CAP_MR_BAD_ID 0x0002
  341. #define L2CAP_MR_SAME_ID 0x0003
  342. #define L2CAP_MR_NOT_SUPP 0x0004
  343. #define L2CAP_MR_COLLISION 0x0005
  344. #define L2CAP_MR_NOT_ALLOWED 0x0006
  345. struct l2cap_move_chan_cfm {
  346. __le16 icid;
  347. __le16 result;
  348. } __packed;
  349. #define L2CAP_MC_CONFIRMED 0x0000
  350. #define L2CAP_MC_UNCONFIRMED 0x0001
  351. struct l2cap_move_chan_cfm_rsp {
  352. __le16 icid;
  353. } __packed;
  354. /* info type */
  355. #define L2CAP_IT_CL_MTU 0x0001
  356. #define L2CAP_IT_FEAT_MASK 0x0002
  357. #define L2CAP_IT_FIXED_CHAN 0x0003
  358. /* info result */
  359. #define L2CAP_IR_SUCCESS 0x0000
  360. #define L2CAP_IR_NOTSUPP 0x0001
  361. struct l2cap_conn_param_update_req {
  362. __le16 min;
  363. __le16 max;
  364. __le16 latency;
  365. __le16 to_multiplier;
  366. } __packed;
  367. struct l2cap_conn_param_update_rsp {
  368. __le16 result;
  369. } __packed;
  370. /* Connection Parameters result */
  371. #define L2CAP_CONN_PARAM_ACCEPTED 0x0000
  372. #define L2CAP_CONN_PARAM_REJECTED 0x0001
  373. struct l2cap_le_conn_req {
  374. __le16 psm;
  375. __le16 scid;
  376. __le16 mtu;
  377. __le16 mps;
  378. __le16 credits;
  379. } __packed;
  380. struct l2cap_le_conn_rsp {
  381. __le16 dcid;
  382. __le16 mtu;
  383. __le16 mps;
  384. __le16 credits;
  385. __le16 result;
  386. } __packed;
  387. struct l2cap_le_credits {
  388. __le16 cid;
  389. __le16 credits;
  390. } __packed;
  391. #define L2CAP_ECRED_MIN_MTU 64
  392. #define L2CAP_ECRED_MIN_MPS 64
  393. #define L2CAP_ECRED_MAX_CID 5
  394. struct l2cap_ecred_conn_req {
  395. /* New members must be added within the struct_group() macro below. */
  396. __struct_group(l2cap_ecred_conn_req_hdr, hdr, __packed,
  397. __le16 psm;
  398. __le16 mtu;
  399. __le16 mps;
  400. __le16 credits;
  401. );
  402. __le16 scid[];
  403. } __packed;
  404. struct l2cap_ecred_conn_rsp {
  405. /* New members must be added within the struct_group() macro below. */
  406. struct_group_tagged(l2cap_ecred_conn_rsp_hdr, hdr,
  407. __le16 mtu;
  408. __le16 mps;
  409. __le16 credits;
  410. __le16 result;
  411. );
  412. __le16 dcid[];
  413. };
  414. struct l2cap_ecred_reconf_req {
  415. __le16 mtu;
  416. __le16 mps;
  417. __le16 scid[];
  418. } __packed;
  419. #define L2CAP_RECONF_SUCCESS 0x0000
  420. #define L2CAP_RECONF_INVALID_MTU 0x0001
  421. #define L2CAP_RECONF_INVALID_MPS 0x0002
  422. #define L2CAP_RECONF_INVALID_CID 0x0003
  423. #define L2CAP_RECONF_INVALID_PARAMS 0x0004
  424. struct l2cap_ecred_reconf_rsp {
  425. __le16 result;
  426. } __packed;
  427. /* ----- L2CAP channels and connections ----- */
  428. struct l2cap_seq_list {
  429. __u16 head;
  430. __u16 tail;
  431. __u16 mask;
  432. __u16 *list;
  433. };
  434. #define L2CAP_SEQ_LIST_CLEAR 0xFFFF
  435. #define L2CAP_SEQ_LIST_TAIL 0x8000
  436. struct l2cap_chan {
  437. struct l2cap_conn *conn;
  438. struct kref kref;
  439. atomic_t nesting;
  440. __u8 state;
  441. bdaddr_t dst;
  442. __u8 dst_type;
  443. bdaddr_t src;
  444. __u8 src_type;
  445. __le16 psm;
  446. __le16 sport;
  447. __u16 dcid;
  448. __u16 scid;
  449. __u16 imtu;
  450. __u16 omtu;
  451. __u16 flush_to;
  452. __u8 mode;
  453. __u8 chan_type;
  454. __u8 chan_policy;
  455. __u8 sec_level;
  456. __u8 ident;
  457. __u8 conf_req[64];
  458. __u8 conf_len;
  459. __u8 num_conf_req;
  460. __u8 num_conf_rsp;
  461. __u8 fcs;
  462. __u16 tx_win;
  463. __u16 tx_win_max;
  464. __u16 ack_win;
  465. __u8 max_tx;
  466. __u16 retrans_timeout;
  467. __u16 monitor_timeout;
  468. __u16 mps;
  469. __u16 tx_credits;
  470. __u16 rx_credits;
  471. /* estimated available receive buffer space or -1 if unknown */
  472. ssize_t rx_avail;
  473. __u8 tx_state;
  474. __u8 rx_state;
  475. unsigned long conf_state;
  476. unsigned long conn_state;
  477. unsigned long flags;
  478. __u16 next_tx_seq;
  479. __u16 expected_ack_seq;
  480. __u16 expected_tx_seq;
  481. __u16 buffer_seq;
  482. __u16 srej_save_reqseq;
  483. __u16 last_acked_seq;
  484. __u16 frames_sent;
  485. __u16 unacked_frames;
  486. __u8 retry_count;
  487. __u16 sdu_len;
  488. struct sk_buff *sdu;
  489. struct sk_buff *sdu_last_frag;
  490. __u16 remote_tx_win;
  491. __u8 remote_max_tx;
  492. __u16 remote_mps;
  493. __u8 local_id;
  494. __u8 local_stype;
  495. __u16 local_msdu;
  496. __u32 local_sdu_itime;
  497. __u32 local_acc_lat;
  498. __u32 local_flush_to;
  499. __u8 remote_id;
  500. __u8 remote_stype;
  501. __u16 remote_msdu;
  502. __u32 remote_sdu_itime;
  503. __u32 remote_acc_lat;
  504. __u32 remote_flush_to;
  505. struct delayed_work chan_timer;
  506. struct delayed_work retrans_timer;
  507. struct delayed_work monitor_timer;
  508. struct delayed_work ack_timer;
  509. struct sk_buff *tx_send_head;
  510. struct sk_buff_head tx_q;
  511. struct sk_buff_head srej_q;
  512. struct l2cap_seq_list srej_list;
  513. struct l2cap_seq_list retrans_list;
  514. struct list_head list;
  515. struct list_head global_l;
  516. void *data;
  517. const struct l2cap_ops *ops;
  518. struct mutex lock;
  519. };
  520. struct l2cap_ops {
  521. char *name;
  522. struct l2cap_chan *(*new_connection) (struct l2cap_chan *chan);
  523. int (*recv) (struct l2cap_chan * chan,
  524. struct sk_buff *skb);
  525. void (*teardown) (struct l2cap_chan *chan, int err);
  526. void (*close) (struct l2cap_chan *chan);
  527. void (*state_change) (struct l2cap_chan *chan,
  528. int state, int err);
  529. void (*ready) (struct l2cap_chan *chan);
  530. void (*defer) (struct l2cap_chan *chan);
  531. void (*resume) (struct l2cap_chan *chan);
  532. void (*suspend) (struct l2cap_chan *chan);
  533. void (*set_shutdown) (struct l2cap_chan *chan);
  534. long (*get_sndtimeo) (struct l2cap_chan *chan);
  535. struct pid *(*get_peer_pid) (struct l2cap_chan *chan);
  536. struct sk_buff *(*alloc_skb) (struct l2cap_chan *chan,
  537. unsigned long hdr_len,
  538. unsigned long len, int nb);
  539. int (*filter) (struct l2cap_chan * chan,
  540. struct sk_buff *skb);
  541. };
  542. struct l2cap_conn {
  543. struct hci_conn *hcon;
  544. struct hci_chan *hchan;
  545. unsigned int mtu;
  546. __u32 feat_mask;
  547. __u8 remote_fixed_chan;
  548. __u8 local_fixed_chan;
  549. __u8 info_state;
  550. __u8 info_ident;
  551. struct delayed_work info_timer;
  552. struct sk_buff *rx_skb;
  553. __u32 rx_len;
  554. struct ida tx_ida;
  555. __u8 tx_ident;
  556. struct sk_buff_head pending_rx;
  557. struct work_struct pending_rx_work;
  558. struct delayed_work id_addr_timer;
  559. __u8 disc_reason;
  560. struct l2cap_chan *smp;
  561. struct list_head chan_l;
  562. struct mutex lock;
  563. struct kref ref;
  564. struct list_head users;
  565. };
  566. struct l2cap_user {
  567. struct list_head list;
  568. int (*probe) (struct l2cap_conn *conn, struct l2cap_user *user);
  569. void (*remove) (struct l2cap_conn *conn, struct l2cap_user *user);
  570. };
  571. #define L2CAP_INFO_CL_MTU_REQ_SENT 0x01
  572. #define L2CAP_INFO_FEAT_MASK_REQ_SENT 0x04
  573. #define L2CAP_INFO_FEAT_MASK_REQ_DONE 0x08
  574. #define L2CAP_CHAN_RAW 1
  575. #define L2CAP_CHAN_CONN_LESS 2
  576. #define L2CAP_CHAN_CONN_ORIENTED 3
  577. #define L2CAP_CHAN_FIXED 4
  578. /* ----- L2CAP socket info ----- */
  579. #define l2cap_pi(sk) ((struct l2cap_pinfo *) sk)
  580. struct l2cap_rx_busy {
  581. struct list_head list;
  582. struct sk_buff *skb;
  583. };
  584. struct l2cap_pinfo {
  585. struct bt_sock bt;
  586. struct l2cap_chan *chan;
  587. struct list_head rx_busy;
  588. };
  589. enum {
  590. CONF_REQ_SENT,
  591. CONF_INPUT_DONE,
  592. CONF_OUTPUT_DONE,
  593. CONF_MTU_DONE,
  594. CONF_MODE_DONE,
  595. CONF_CONNECT_PEND,
  596. CONF_RECV_NO_FCS,
  597. CONF_STATE2_DEVICE,
  598. CONF_EWS_RECV,
  599. CONF_LOC_CONF_PEND,
  600. CONF_REM_CONF_PEND,
  601. CONF_NOT_COMPLETE,
  602. };
  603. #define L2CAP_CONF_MAX_CONF_REQ 2
  604. #define L2CAP_CONF_MAX_CONF_RSP 2
  605. enum {
  606. CONN_SREJ_SENT,
  607. CONN_WAIT_F,
  608. CONN_SREJ_ACT,
  609. CONN_SEND_PBIT,
  610. CONN_REMOTE_BUSY,
  611. CONN_LOCAL_BUSY,
  612. CONN_REJ_ACT,
  613. CONN_SEND_FBIT,
  614. CONN_RNR_SENT,
  615. };
  616. /* Definitions for flags in l2cap_chan */
  617. enum {
  618. FLAG_ROLE_SWITCH,
  619. FLAG_FORCE_ACTIVE,
  620. FLAG_FORCE_RELIABLE,
  621. FLAG_FLUSHABLE,
  622. FLAG_EXT_CTRL,
  623. FLAG_EFS_ENABLE,
  624. FLAG_DEFER_SETUP,
  625. FLAG_LE_CONN_REQ_SENT,
  626. FLAG_ECRED_CONN_REQ_SENT,
  627. FLAG_PENDING_SECURITY,
  628. FLAG_HOLD_HCI_CONN,
  629. };
  630. /* Lock nesting levels for L2CAP channels. We need these because lockdep
  631. * otherwise considers all channels equal and will e.g. complain about a
  632. * connection oriented channel triggering SMP procedures or a listening
  633. * channel creating and locking a child channel.
  634. */
  635. enum {
  636. L2CAP_NESTING_SMP,
  637. L2CAP_NESTING_NORMAL,
  638. L2CAP_NESTING_PARENT,
  639. };
  640. enum {
  641. L2CAP_TX_STATE_XMIT,
  642. L2CAP_TX_STATE_WAIT_F,
  643. };
  644. enum {
  645. L2CAP_RX_STATE_RECV,
  646. L2CAP_RX_STATE_SREJ_SENT,
  647. L2CAP_RX_STATE_MOVE,
  648. L2CAP_RX_STATE_WAIT_P,
  649. L2CAP_RX_STATE_WAIT_F,
  650. };
  651. enum {
  652. L2CAP_TXSEQ_EXPECTED,
  653. L2CAP_TXSEQ_EXPECTED_SREJ,
  654. L2CAP_TXSEQ_UNEXPECTED,
  655. L2CAP_TXSEQ_UNEXPECTED_SREJ,
  656. L2CAP_TXSEQ_DUPLICATE,
  657. L2CAP_TXSEQ_DUPLICATE_SREJ,
  658. L2CAP_TXSEQ_INVALID,
  659. L2CAP_TXSEQ_INVALID_IGNORE,
  660. };
  661. enum {
  662. L2CAP_EV_DATA_REQUEST,
  663. L2CAP_EV_LOCAL_BUSY_DETECTED,
  664. L2CAP_EV_LOCAL_BUSY_CLEAR,
  665. L2CAP_EV_RECV_REQSEQ_AND_FBIT,
  666. L2CAP_EV_RECV_FBIT,
  667. L2CAP_EV_RETRANS_TO,
  668. L2CAP_EV_MONITOR_TO,
  669. L2CAP_EV_EXPLICIT_POLL,
  670. L2CAP_EV_RECV_IFRAME,
  671. L2CAP_EV_RECV_RR,
  672. L2CAP_EV_RECV_REJ,
  673. L2CAP_EV_RECV_RNR,
  674. L2CAP_EV_RECV_SREJ,
  675. L2CAP_EV_RECV_FRAME,
  676. };
  677. enum {
  678. L2CAP_MOVE_ROLE_NONE,
  679. L2CAP_MOVE_ROLE_INITIATOR,
  680. L2CAP_MOVE_ROLE_RESPONDER,
  681. };
  682. enum {
  683. L2CAP_MOVE_STABLE,
  684. L2CAP_MOVE_WAIT_REQ,
  685. L2CAP_MOVE_WAIT_RSP,
  686. L2CAP_MOVE_WAIT_RSP_SUCCESS,
  687. L2CAP_MOVE_WAIT_CONFIRM,
  688. L2CAP_MOVE_WAIT_CONFIRM_RSP,
  689. L2CAP_MOVE_WAIT_LOGICAL_COMP,
  690. L2CAP_MOVE_WAIT_LOGICAL_CFM,
  691. L2CAP_MOVE_WAIT_LOCAL_BUSY,
  692. L2CAP_MOVE_WAIT_PREPARE,
  693. };
  694. void l2cap_chan_hold(struct l2cap_chan *c);
  695. struct l2cap_chan *l2cap_chan_hold_unless_zero(struct l2cap_chan *c);
  696. void l2cap_chan_put(struct l2cap_chan *c);
  697. static inline void l2cap_chan_lock(struct l2cap_chan *chan)
  698. {
  699. mutex_lock_nested(&chan->lock, atomic_read(&chan->nesting));
  700. }
  701. static inline void l2cap_chan_unlock(struct l2cap_chan *chan)
  702. {
  703. mutex_unlock(&chan->lock);
  704. }
  705. static inline void l2cap_set_timer(struct l2cap_chan *chan,
  706. struct delayed_work *work, long timeout)
  707. {
  708. BT_DBG("chan %p state %s timeout %ld", chan,
  709. state_to_string(chan->state), timeout);
  710. /* If delayed work cancelled do not hold(chan)
  711. since it is already done with previous set_timer */
  712. if (!cancel_delayed_work(work))
  713. l2cap_chan_hold(chan);
  714. schedule_delayed_work(work, timeout);
  715. }
  716. static inline bool l2cap_clear_timer(struct l2cap_chan *chan,
  717. struct delayed_work *work)
  718. {
  719. bool ret;
  720. /* put(chan) if delayed work cancelled otherwise it
  721. is done in delayed work function */
  722. ret = cancel_delayed_work(work);
  723. if (ret)
  724. l2cap_chan_put(chan);
  725. return ret;
  726. }
  727. #define __set_chan_timer(c, t) l2cap_set_timer(c, &c->chan_timer, (t))
  728. #define __clear_chan_timer(c) l2cap_clear_timer(c, &c->chan_timer)
  729. #define __clear_retrans_timer(c) l2cap_clear_timer(c, &c->retrans_timer)
  730. #define __clear_monitor_timer(c) l2cap_clear_timer(c, &c->monitor_timer)
  731. #define __set_ack_timer(c) l2cap_set_timer(c, &chan->ack_timer, \
  732. msecs_to_jiffies(L2CAP_DEFAULT_ACK_TO));
  733. #define __clear_ack_timer(c) l2cap_clear_timer(c, &c->ack_timer)
  734. static inline int __seq_offset(struct l2cap_chan *chan, __u16 seq1, __u16 seq2)
  735. {
  736. if (seq1 >= seq2)
  737. return seq1 - seq2;
  738. else
  739. return chan->tx_win_max + 1 - seq2 + seq1;
  740. }
  741. static inline __u16 __next_seq(struct l2cap_chan *chan, __u16 seq)
  742. {
  743. return (seq + 1) % (chan->tx_win_max + 1);
  744. }
  745. static inline struct l2cap_chan *l2cap_chan_no_new_connection(struct l2cap_chan *chan)
  746. {
  747. return NULL;
  748. }
  749. static inline int l2cap_chan_no_recv(struct l2cap_chan *chan, struct sk_buff *skb)
  750. {
  751. return -ENOSYS;
  752. }
  753. static inline struct sk_buff *l2cap_chan_no_alloc_skb(struct l2cap_chan *chan,
  754. unsigned long hdr_len,
  755. unsigned long len, int nb)
  756. {
  757. return ERR_PTR(-ENOSYS);
  758. }
  759. static inline void l2cap_chan_no_teardown(struct l2cap_chan *chan, int err)
  760. {
  761. }
  762. static inline void l2cap_chan_no_close(struct l2cap_chan *chan)
  763. {
  764. }
  765. static inline void l2cap_chan_no_ready(struct l2cap_chan *chan)
  766. {
  767. }
  768. static inline void l2cap_chan_no_state_change(struct l2cap_chan *chan,
  769. int state, int err)
  770. {
  771. }
  772. static inline void l2cap_chan_no_defer(struct l2cap_chan *chan)
  773. {
  774. }
  775. static inline void l2cap_chan_no_suspend(struct l2cap_chan *chan)
  776. {
  777. }
  778. static inline void l2cap_chan_no_resume(struct l2cap_chan *chan)
  779. {
  780. }
  781. static inline void l2cap_chan_no_set_shutdown(struct l2cap_chan *chan)
  782. {
  783. }
  784. static inline long l2cap_chan_no_get_sndtimeo(struct l2cap_chan *chan)
  785. {
  786. return 0;
  787. }
  788. extern bool disable_ertm;
  789. extern bool enable_ecred;
  790. int l2cap_init_sockets(void);
  791. void l2cap_cleanup_sockets(void);
  792. bool l2cap_is_socket(struct socket *sock);
  793. void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan);
  794. void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan);
  795. void __l2cap_connect_rsp_defer(struct l2cap_chan *chan);
  796. int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm);
  797. int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid);
  798. struct l2cap_chan *l2cap_chan_create(void);
  799. void l2cap_chan_close(struct l2cap_chan *chan, int reason);
  800. int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
  801. bdaddr_t *dst, u8 dst_type, u16 timeout);
  802. int l2cap_chan_reconfigure(struct l2cap_chan *chan, __u16 mtu);
  803. int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len,
  804. const struct sockcm_cookie *sockc);
  805. void l2cap_chan_busy(struct l2cap_chan *chan, int busy);
  806. void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail);
  807. int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator);
  808. void l2cap_chan_set_defaults(struct l2cap_chan *chan);
  809. int l2cap_ertm_init(struct l2cap_chan *chan);
  810. void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan);
  811. void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan);
  812. typedef void (*l2cap_chan_func_t)(struct l2cap_chan *chan, void *data);
  813. void l2cap_chan_list(struct l2cap_conn *conn, l2cap_chan_func_t func,
  814. void *data);
  815. void l2cap_chan_del(struct l2cap_chan *chan, int err);
  816. void l2cap_send_conn_req(struct l2cap_chan *chan);
  817. struct l2cap_conn *l2cap_conn_get(struct l2cap_conn *conn);
  818. struct l2cap_conn *l2cap_conn_hold_unless_zero(struct l2cap_conn *conn);
  819. void l2cap_conn_put(struct l2cap_conn *conn);
  820. int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user);
  821. void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user);
  822. #endif /* __L2CAP_H */