l2cap.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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 <asm/unaligned.h>
  25. /* L2CAP defaults */
  26. #define L2CAP_DEFAULT_MTU 672
  27. #define L2CAP_DEFAULT_MIN_MTU 48
  28. #define L2CAP_DEFAULT_FLUSH_TO 0xffff
  29. #define L2CAP_DEFAULT_TX_WINDOW 63
  30. #define L2CAP_DEFAULT_EXT_WINDOW 0x3FFF
  31. #define L2CAP_DEFAULT_MAX_TX 3
  32. #define L2CAP_DEFAULT_RETRANS_TO 2000 /* 2 seconds */
  33. #define L2CAP_DEFAULT_MONITOR_TO 12000 /* 12 seconds */
  34. #define L2CAP_DEFAULT_MAX_PDU_SIZE 1009 /* Sized for 3-DH5 packet */
  35. #define L2CAP_DEFAULT_ACK_TO 200
  36. #define L2CAP_LE_DEFAULT_MTU 23
  37. #define L2CAP_DEFAULT_MAX_SDU_SIZE 0xFFFF
  38. #define L2CAP_DEFAULT_SDU_ITIME 0xFFFFFFFF
  39. #define L2CAP_DEFAULT_ACC_LAT 0xFFFFFFFF
  40. #define L2CAP_DISC_TIMEOUT msecs_to_jiffies(100)
  41. #define L2CAP_DISC_REJ_TIMEOUT msecs_to_jiffies(5000)
  42. #define L2CAP_ENC_TIMEOUT msecs_to_jiffies(5000)
  43. #define L2CAP_CONN_TIMEOUT msecs_to_jiffies(40000)
  44. #define L2CAP_INFO_TIMEOUT msecs_to_jiffies(4000)
  45. /* L2CAP socket address */
  46. struct sockaddr_l2 {
  47. sa_family_t l2_family;
  48. __le16 l2_psm;
  49. bdaddr_t l2_bdaddr;
  50. __le16 l2_cid;
  51. };
  52. /* L2CAP socket options */
  53. #define L2CAP_OPTIONS 0x01
  54. struct l2cap_options {
  55. __u16 omtu;
  56. __u16 imtu;
  57. __u16 flush_to;
  58. __u8 mode;
  59. __u8 fcs;
  60. __u8 max_tx;
  61. __u16 txwin_size;
  62. };
  63. #define L2CAP_CONNINFO 0x02
  64. struct l2cap_conninfo {
  65. __u16 hci_handle;
  66. __u8 dev_class[3];
  67. };
  68. #define L2CAP_LM 0x03
  69. #define L2CAP_LM_MASTER 0x0001
  70. #define L2CAP_LM_AUTH 0x0002
  71. #define L2CAP_LM_ENCRYPT 0x0004
  72. #define L2CAP_LM_TRUSTED 0x0008
  73. #define L2CAP_LM_RELIABLE 0x0010
  74. #define L2CAP_LM_SECURE 0x0020
  75. /* L2CAP command codes */
  76. #define L2CAP_COMMAND_REJ 0x01
  77. #define L2CAP_CONN_REQ 0x02
  78. #define L2CAP_CONN_RSP 0x03
  79. #define L2CAP_CONF_REQ 0x04
  80. #define L2CAP_CONF_RSP 0x05
  81. #define L2CAP_DISCONN_REQ 0x06
  82. #define L2CAP_DISCONN_RSP 0x07
  83. #define L2CAP_ECHO_REQ 0x08
  84. #define L2CAP_ECHO_RSP 0x09
  85. #define L2CAP_INFO_REQ 0x0a
  86. #define L2CAP_INFO_RSP 0x0b
  87. #define L2CAP_CREATE_CHAN_REQ 0x0c
  88. #define L2CAP_CREATE_CHAN_RSP 0x0d
  89. #define L2CAP_MOVE_CHAN_REQ 0x0e
  90. #define L2CAP_MOVE_CHAN_RSP 0x0f
  91. #define L2CAP_MOVE_CHAN_CFM 0x10
  92. #define L2CAP_MOVE_CHAN_CFM_RSP 0x11
  93. #define L2CAP_CONN_PARAM_UPDATE_REQ 0x12
  94. #define L2CAP_CONN_PARAM_UPDATE_RSP 0x13
  95. /* L2CAP extended feature mask */
  96. #define L2CAP_FEAT_FLOWCTL 0x00000001
  97. #define L2CAP_FEAT_RETRANS 0x00000002
  98. #define L2CAP_FEAT_BIDIR_QOS 0x00000004
  99. #define L2CAP_FEAT_ERTM 0x00000008
  100. #define L2CAP_FEAT_STREAMING 0x00000010
  101. #define L2CAP_FEAT_FCS 0x00000020
  102. #define L2CAP_FEAT_EXT_FLOW 0x00000040
  103. #define L2CAP_FEAT_FIXED_CHAN 0x00000080
  104. #define L2CAP_FEAT_EXT_WINDOW 0x00000100
  105. #define L2CAP_FEAT_UCD 0x00000200
  106. /* L2CAP checksum option */
  107. #define L2CAP_FCS_NONE 0x00
  108. #define L2CAP_FCS_CRC16 0x01
  109. /* L2CAP fixed channels */
  110. #define L2CAP_FC_L2CAP 0x02
  111. #define L2CAP_FC_A2MP 0x08
  112. /* L2CAP Control Field bit masks */
  113. #define L2CAP_CTRL_SAR 0xC000
  114. #define L2CAP_CTRL_REQSEQ 0x3F00
  115. #define L2CAP_CTRL_TXSEQ 0x007E
  116. #define L2CAP_CTRL_SUPERVISE 0x000C
  117. #define L2CAP_CTRL_RETRANS 0x0080
  118. #define L2CAP_CTRL_FINAL 0x0080
  119. #define L2CAP_CTRL_POLL 0x0010
  120. #define L2CAP_CTRL_FRAME_TYPE 0x0001 /* I- or S-Frame */
  121. #define L2CAP_CTRL_TXSEQ_SHIFT 1
  122. #define L2CAP_CTRL_SUPER_SHIFT 2
  123. #define L2CAP_CTRL_REQSEQ_SHIFT 8
  124. #define L2CAP_CTRL_SAR_SHIFT 14
  125. /* L2CAP Extended Control Field bit mask */
  126. #define L2CAP_EXT_CTRL_TXSEQ 0xFFFC0000
  127. #define L2CAP_EXT_CTRL_SAR 0x00030000
  128. #define L2CAP_EXT_CTRL_SUPERVISE 0x00030000
  129. #define L2CAP_EXT_CTRL_REQSEQ 0x0000FFFC
  130. #define L2CAP_EXT_CTRL_POLL 0x00040000
  131. #define L2CAP_EXT_CTRL_FINAL 0x00000002
  132. #define L2CAP_EXT_CTRL_FRAME_TYPE 0x00000001 /* I- or S-Frame */
  133. #define L2CAP_EXT_CTRL_REQSEQ_SHIFT 2
  134. #define L2CAP_EXT_CTRL_SAR_SHIFT 16
  135. #define L2CAP_EXT_CTRL_SUPER_SHIFT 16
  136. #define L2CAP_EXT_CTRL_TXSEQ_SHIFT 18
  137. /* L2CAP Supervisory Function */
  138. #define L2CAP_SUPER_RR 0x00
  139. #define L2CAP_SUPER_REJ 0x01
  140. #define L2CAP_SUPER_RNR 0x02
  141. #define L2CAP_SUPER_SREJ 0x03
  142. /* L2CAP Segmentation and Reassembly */
  143. #define L2CAP_SAR_UNSEGMENTED 0x00
  144. #define L2CAP_SAR_START 0x01
  145. #define L2CAP_SAR_END 0x02
  146. #define L2CAP_SAR_CONTINUE 0x03
  147. /* L2CAP Command rej. reasons */
  148. #define L2CAP_REJ_NOT_UNDERSTOOD 0x0000
  149. #define L2CAP_REJ_MTU_EXCEEDED 0x0001
  150. #define L2CAP_REJ_INVALID_CID 0x0002
  151. /* L2CAP structures */
  152. struct l2cap_hdr {
  153. __le16 len;
  154. __le16 cid;
  155. } __packed;
  156. #define L2CAP_HDR_SIZE 4
  157. #define L2CAP_ENH_HDR_SIZE 6
  158. #define L2CAP_EXT_HDR_SIZE 8
  159. #define L2CAP_FCS_SIZE 2
  160. #define L2CAP_SDULEN_SIZE 2
  161. #define L2CAP_PSMLEN_SIZE 2
  162. struct l2cap_cmd_hdr {
  163. __u8 code;
  164. __u8 ident;
  165. __le16 len;
  166. } __packed;
  167. #define L2CAP_CMD_HDR_SIZE 4
  168. struct l2cap_cmd_rej_unk {
  169. __le16 reason;
  170. } __packed;
  171. struct l2cap_cmd_rej_mtu {
  172. __le16 reason;
  173. __le16 max_mtu;
  174. } __packed;
  175. struct l2cap_cmd_rej_cid {
  176. __le16 reason;
  177. __le16 scid;
  178. __le16 dcid;
  179. } __packed;
  180. struct l2cap_conn_req {
  181. __le16 psm;
  182. __le16 scid;
  183. } __packed;
  184. struct l2cap_conn_rsp {
  185. __le16 dcid;
  186. __le16 scid;
  187. __le16 result;
  188. __le16 status;
  189. } __packed;
  190. /* channel indentifier */
  191. #define L2CAP_CID_SIGNALING 0x0001
  192. #define L2CAP_CID_CONN_LESS 0x0002
  193. #define L2CAP_CID_LE_DATA 0x0004
  194. #define L2CAP_CID_LE_SIGNALING 0x0005
  195. #define L2CAP_CID_SMP 0x0006
  196. #define L2CAP_CID_DYN_START 0x0040
  197. #define L2CAP_CID_DYN_END 0xffff
  198. /* connect/create channel results */
  199. #define L2CAP_CR_SUCCESS 0x0000
  200. #define L2CAP_CR_PEND 0x0001
  201. #define L2CAP_CR_BAD_PSM 0x0002
  202. #define L2CAP_CR_SEC_BLOCK 0x0003
  203. #define L2CAP_CR_NO_MEM 0x0004
  204. #define L2CAP_CR_BAD_AMP 0x0005
  205. /* connect/create channel status */
  206. #define L2CAP_CS_NO_INFO 0x0000
  207. #define L2CAP_CS_AUTHEN_PEND 0x0001
  208. #define L2CAP_CS_AUTHOR_PEND 0x0002
  209. struct l2cap_conf_req {
  210. __le16 dcid;
  211. __le16 flags;
  212. __u8 data[0];
  213. } __packed;
  214. struct l2cap_conf_rsp {
  215. __le16 scid;
  216. __le16 flags;
  217. __le16 result;
  218. __u8 data[0];
  219. } __packed;
  220. #define L2CAP_CONF_SUCCESS 0x0000
  221. #define L2CAP_CONF_UNACCEPT 0x0001
  222. #define L2CAP_CONF_REJECT 0x0002
  223. #define L2CAP_CONF_UNKNOWN 0x0003
  224. #define L2CAP_CONF_PENDING 0x0004
  225. #define L2CAP_CONF_EFS_REJECT 0x0005
  226. struct l2cap_conf_opt {
  227. __u8 type;
  228. __u8 len;
  229. __u8 val[0];
  230. } __packed;
  231. #define L2CAP_CONF_OPT_SIZE 2
  232. #define L2CAP_CONF_HINT 0x80
  233. #define L2CAP_CONF_MASK 0x7f
  234. #define L2CAP_CONF_MTU 0x01
  235. #define L2CAP_CONF_FLUSH_TO 0x02
  236. #define L2CAP_CONF_QOS 0x03
  237. #define L2CAP_CONF_RFC 0x04
  238. #define L2CAP_CONF_FCS 0x05
  239. #define L2CAP_CONF_EFS 0x06
  240. #define L2CAP_CONF_EWS 0x07
  241. #define L2CAP_CONF_MAX_SIZE 22
  242. struct l2cap_conf_rfc {
  243. __u8 mode;
  244. __u8 txwin_size;
  245. __u8 max_transmit;
  246. __le16 retrans_timeout;
  247. __le16 monitor_timeout;
  248. __le16 max_pdu_size;
  249. } __packed;
  250. #define L2CAP_MODE_BASIC 0x00
  251. #define L2CAP_MODE_RETRANS 0x01
  252. #define L2CAP_MODE_FLOWCTL 0x02
  253. #define L2CAP_MODE_ERTM 0x03
  254. #define L2CAP_MODE_STREAMING 0x04
  255. struct l2cap_conf_efs {
  256. __u8 id;
  257. __u8 stype;
  258. __le16 msdu;
  259. __le32 sdu_itime;
  260. __le32 acc_lat;
  261. __le32 flush_to;
  262. } __packed;
  263. #define L2CAP_SERV_NOTRAFIC 0x00
  264. #define L2CAP_SERV_BESTEFFORT 0x01
  265. #define L2CAP_SERV_GUARANTEED 0x02
  266. #define L2CAP_BESTEFFORT_ID 0x01
  267. struct l2cap_disconn_req {
  268. __le16 dcid;
  269. __le16 scid;
  270. } __packed;
  271. struct l2cap_disconn_rsp {
  272. __le16 dcid;
  273. __le16 scid;
  274. } __packed;
  275. struct l2cap_info_req {
  276. __le16 type;
  277. } __packed;
  278. struct l2cap_info_rsp {
  279. __le16 type;
  280. __le16 result;
  281. __u8 data[0];
  282. } __packed;
  283. struct l2cap_create_chan_req {
  284. __le16 psm;
  285. __le16 scid;
  286. __u8 amp_id;
  287. } __packed;
  288. struct l2cap_create_chan_rsp {
  289. __le16 dcid;
  290. __le16 scid;
  291. __le16 result;
  292. __le16 status;
  293. } __packed;
  294. struct l2cap_move_chan_req {
  295. __le16 icid;
  296. __u8 dest_amp_id;
  297. } __packed;
  298. struct l2cap_move_chan_rsp {
  299. __le16 icid;
  300. __le16 result;
  301. } __packed;
  302. #define L2CAP_MR_SUCCESS 0x0000
  303. #define L2CAP_MR_PEND 0x0001
  304. #define L2CAP_MR_BAD_ID 0x0002
  305. #define L2CAP_MR_SAME_ID 0x0003
  306. #define L2CAP_MR_NOT_SUPP 0x0004
  307. #define L2CAP_MR_COLLISION 0x0005
  308. #define L2CAP_MR_NOT_ALLOWED 0x0006
  309. struct l2cap_move_chan_cfm {
  310. __le16 icid;
  311. __le16 result;
  312. } __packed;
  313. #define L2CAP_MC_CONFIRMED 0x0000
  314. #define L2CAP_MC_UNCONFIRMED 0x0001
  315. struct l2cap_move_chan_cfm_rsp {
  316. __le16 icid;
  317. } __packed;
  318. /* info type */
  319. #define L2CAP_IT_CL_MTU 0x0001
  320. #define L2CAP_IT_FEAT_MASK 0x0002
  321. #define L2CAP_IT_FIXED_CHAN 0x0003
  322. /* info result */
  323. #define L2CAP_IR_SUCCESS 0x0000
  324. #define L2CAP_IR_NOTSUPP 0x0001
  325. struct l2cap_conn_param_update_req {
  326. __le16 min;
  327. __le16 max;
  328. __le16 latency;
  329. __le16 to_multiplier;
  330. } __packed;
  331. struct l2cap_conn_param_update_rsp {
  332. __le16 result;
  333. } __packed;
  334. /* Connection Parameters result */
  335. #define L2CAP_CONN_PARAM_ACCEPTED 0x0000
  336. #define L2CAP_CONN_PARAM_REJECTED 0x0001
  337. /* ----- L2CAP channels and connections ----- */
  338. struct srej_list {
  339. __u16 tx_seq;
  340. struct list_head list;
  341. };
  342. struct l2cap_chan {
  343. struct sock *sk;
  344. struct l2cap_conn *conn;
  345. __u8 state;
  346. atomic_t refcnt;
  347. __le16 psm;
  348. __u16 dcid;
  349. __u16 scid;
  350. __u16 imtu;
  351. __u16 omtu;
  352. __u16 flush_to;
  353. __u8 mode;
  354. __u8 chan_type;
  355. __u8 chan_policy;
  356. __le16 sport;
  357. __u8 sec_level;
  358. __u8 ident;
  359. __u8 conf_req[64];
  360. __u8 conf_len;
  361. __u8 num_conf_req;
  362. __u8 num_conf_rsp;
  363. __u8 fcs;
  364. __u16 tx_win;
  365. __u16 tx_win_max;
  366. __u8 max_tx;
  367. __u16 retrans_timeout;
  368. __u16 monitor_timeout;
  369. __u16 mps;
  370. unsigned long conf_state;
  371. unsigned long conn_state;
  372. unsigned long flags;
  373. __u16 next_tx_seq;
  374. __u16 expected_ack_seq;
  375. __u16 expected_tx_seq;
  376. __u16 buffer_seq;
  377. __u16 buffer_seq_srej;
  378. __u16 srej_save_reqseq;
  379. __u16 frames_sent;
  380. __u16 unacked_frames;
  381. __u8 retry_count;
  382. __u8 num_acked;
  383. __u16 sdu_len;
  384. struct sk_buff *sdu;
  385. struct sk_buff *sdu_last_frag;
  386. __u16 remote_tx_win;
  387. __u8 remote_max_tx;
  388. __u16 remote_mps;
  389. __u8 local_id;
  390. __u8 local_stype;
  391. __u16 local_msdu;
  392. __u32 local_sdu_itime;
  393. __u32 local_acc_lat;
  394. __u32 local_flush_to;
  395. __u8 remote_id;
  396. __u8 remote_stype;
  397. __u16 remote_msdu;
  398. __u32 remote_sdu_itime;
  399. __u32 remote_acc_lat;
  400. __u32 remote_flush_to;
  401. struct delayed_work chan_timer;
  402. struct delayed_work retrans_timer;
  403. struct delayed_work monitor_timer;
  404. struct delayed_work ack_timer;
  405. struct sk_buff *tx_send_head;
  406. struct sk_buff_head tx_q;
  407. struct sk_buff_head srej_q;
  408. struct list_head srej_l;
  409. struct list_head list;
  410. struct list_head global_l;
  411. void *data;
  412. struct l2cap_ops *ops;
  413. struct mutex lock;
  414. };
  415. struct l2cap_ops {
  416. char *name;
  417. struct l2cap_chan *(*new_connection) (void *data);
  418. int (*recv) (void *data, struct sk_buff *skb);
  419. void (*close) (void *data);
  420. void (*state_change) (void *data, int state);
  421. struct sk_buff *(*alloc_skb) (struct l2cap_chan *chan,
  422. unsigned long len, int nb, int *err);
  423. };
  424. struct l2cap_conn {
  425. struct hci_conn *hcon;
  426. struct hci_chan *hchan;
  427. bdaddr_t *dst;
  428. bdaddr_t *src;
  429. unsigned int mtu;
  430. __u32 feat_mask;
  431. __u8 fixed_chan_mask;
  432. __u8 info_state;
  433. __u8 info_ident;
  434. struct delayed_work info_timer;
  435. spinlock_t lock;
  436. struct sk_buff *rx_skb;
  437. __u32 rx_len;
  438. __u8 tx_ident;
  439. __u8 disc_reason;
  440. struct delayed_work security_timer;
  441. struct smp_chan *smp_chan;
  442. struct list_head chan_l;
  443. struct mutex chan_lock;
  444. };
  445. #define L2CAP_INFO_CL_MTU_REQ_SENT 0x01
  446. #define L2CAP_INFO_FEAT_MASK_REQ_SENT 0x04
  447. #define L2CAP_INFO_FEAT_MASK_REQ_DONE 0x08
  448. #define L2CAP_CHAN_RAW 1
  449. #define L2CAP_CHAN_CONN_LESS 2
  450. #define L2CAP_CHAN_CONN_ORIENTED 3
  451. /* ----- L2CAP socket info ----- */
  452. #define l2cap_pi(sk) ((struct l2cap_pinfo *) sk)
  453. struct l2cap_pinfo {
  454. struct bt_sock bt;
  455. struct l2cap_chan *chan;
  456. struct sk_buff *rx_busy_skb;
  457. };
  458. enum {
  459. CONF_REQ_SENT,
  460. CONF_INPUT_DONE,
  461. CONF_OUTPUT_DONE,
  462. CONF_MTU_DONE,
  463. CONF_MODE_DONE,
  464. CONF_CONNECT_PEND,
  465. CONF_NO_FCS_RECV,
  466. CONF_STATE2_DEVICE,
  467. CONF_EWS_RECV,
  468. CONF_LOC_CONF_PEND,
  469. CONF_REM_CONF_PEND,
  470. };
  471. #define L2CAP_CONF_MAX_CONF_REQ 2
  472. #define L2CAP_CONF_MAX_CONF_RSP 2
  473. enum {
  474. CONN_SREJ_SENT,
  475. CONN_WAIT_F,
  476. CONN_SREJ_ACT,
  477. CONN_SEND_PBIT,
  478. CONN_REMOTE_BUSY,
  479. CONN_LOCAL_BUSY,
  480. CONN_REJ_ACT,
  481. CONN_SEND_FBIT,
  482. CONN_RNR_SENT,
  483. };
  484. /* Definitions for flags in l2cap_chan */
  485. enum {
  486. FLAG_ROLE_SWITCH,
  487. FLAG_FORCE_ACTIVE,
  488. FLAG_FORCE_RELIABLE,
  489. FLAG_FLUSHABLE,
  490. FLAG_EXT_CTRL,
  491. FLAG_EFS_ENABLE,
  492. };
  493. static inline void l2cap_chan_hold(struct l2cap_chan *c)
  494. {
  495. atomic_inc(&c->refcnt);
  496. }
  497. static inline void l2cap_chan_put(struct l2cap_chan *c)
  498. {
  499. if (atomic_dec_and_test(&c->refcnt))
  500. kfree(c);
  501. }
  502. static inline void l2cap_chan_lock(struct l2cap_chan *chan)
  503. {
  504. mutex_lock(&chan->lock);
  505. }
  506. static inline void l2cap_chan_unlock(struct l2cap_chan *chan)
  507. {
  508. mutex_unlock(&chan->lock);
  509. }
  510. static inline void l2cap_set_timer(struct l2cap_chan *chan,
  511. struct delayed_work *work, long timeout)
  512. {
  513. BT_DBG("chan %p state %s timeout %ld", chan,
  514. state_to_string(chan->state), timeout);
  515. if (!cancel_delayed_work(work))
  516. l2cap_chan_hold(chan);
  517. schedule_delayed_work(work, timeout);
  518. }
  519. static inline bool l2cap_clear_timer(struct l2cap_chan *chan,
  520. struct delayed_work *work)
  521. {
  522. bool ret;
  523. ret = cancel_delayed_work(work);
  524. if (ret)
  525. l2cap_chan_put(chan);
  526. return ret;
  527. }
  528. #define __set_chan_timer(c, t) l2cap_set_timer(c, &c->chan_timer, (t))
  529. #define __clear_chan_timer(c) l2cap_clear_timer(c, &c->chan_timer)
  530. #define __set_retrans_timer(c) l2cap_set_timer(c, &c->retrans_timer, \
  531. msecs_to_jiffies(L2CAP_DEFAULT_RETRANS_TO));
  532. #define __clear_retrans_timer(c) l2cap_clear_timer(c, &c->retrans_timer)
  533. #define __set_monitor_timer(c) l2cap_set_timer(c, &c->monitor_timer, \
  534. msecs_to_jiffies(L2CAP_DEFAULT_MONITOR_TO));
  535. #define __clear_monitor_timer(c) l2cap_clear_timer(c, &c->monitor_timer)
  536. #define __set_ack_timer(c) l2cap_set_timer(c, &chan->ack_timer, \
  537. msecs_to_jiffies(L2CAP_DEFAULT_ACK_TO));
  538. #define __clear_ack_timer(c) l2cap_clear_timer(c, &c->ack_timer)
  539. static inline int __seq_offset(struct l2cap_chan *chan, __u16 seq1, __u16 seq2)
  540. {
  541. int offset;
  542. offset = (seq1 - seq2) % (chan->tx_win_max + 1);
  543. if (offset < 0)
  544. offset += (chan->tx_win_max + 1);
  545. return offset;
  546. }
  547. static inline __u16 __next_seq(struct l2cap_chan *chan, __u16 seq)
  548. {
  549. return (seq + 1) % (chan->tx_win_max + 1);
  550. }
  551. static inline int l2cap_tx_window_full(struct l2cap_chan *ch)
  552. {
  553. int sub;
  554. sub = (ch->next_tx_seq - ch->expected_ack_seq) % 64;
  555. if (sub < 0)
  556. sub += 64;
  557. return sub == ch->remote_tx_win;
  558. }
  559. static inline __u16 __get_reqseq(struct l2cap_chan *chan, __u32 ctrl)
  560. {
  561. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  562. return (ctrl & L2CAP_EXT_CTRL_REQSEQ) >>
  563. L2CAP_EXT_CTRL_REQSEQ_SHIFT;
  564. else
  565. return (ctrl & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT;
  566. }
  567. static inline __u32 __set_reqseq(struct l2cap_chan *chan, __u32 reqseq)
  568. {
  569. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  570. return (reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT) &
  571. L2CAP_EXT_CTRL_REQSEQ;
  572. else
  573. return (reqseq << L2CAP_CTRL_REQSEQ_SHIFT) & L2CAP_CTRL_REQSEQ;
  574. }
  575. static inline __u16 __get_txseq(struct l2cap_chan *chan, __u32 ctrl)
  576. {
  577. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  578. return (ctrl & L2CAP_EXT_CTRL_TXSEQ) >>
  579. L2CAP_EXT_CTRL_TXSEQ_SHIFT;
  580. else
  581. return (ctrl & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT;
  582. }
  583. static inline __u32 __set_txseq(struct l2cap_chan *chan, __u32 txseq)
  584. {
  585. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  586. return (txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT) &
  587. L2CAP_EXT_CTRL_TXSEQ;
  588. else
  589. return (txseq << L2CAP_CTRL_TXSEQ_SHIFT) & L2CAP_CTRL_TXSEQ;
  590. }
  591. static inline bool __is_sframe(struct l2cap_chan *chan, __u32 ctrl)
  592. {
  593. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  594. return ctrl & L2CAP_EXT_CTRL_FRAME_TYPE;
  595. else
  596. return ctrl & L2CAP_CTRL_FRAME_TYPE;
  597. }
  598. static inline __u32 __set_sframe(struct l2cap_chan *chan)
  599. {
  600. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  601. return L2CAP_EXT_CTRL_FRAME_TYPE;
  602. else
  603. return L2CAP_CTRL_FRAME_TYPE;
  604. }
  605. static inline __u8 __get_ctrl_sar(struct l2cap_chan *chan, __u32 ctrl)
  606. {
  607. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  608. return (ctrl & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT;
  609. else
  610. return (ctrl & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT;
  611. }
  612. static inline __u32 __set_ctrl_sar(struct l2cap_chan *chan, __u32 sar)
  613. {
  614. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  615. return (sar << L2CAP_EXT_CTRL_SAR_SHIFT) & L2CAP_EXT_CTRL_SAR;
  616. else
  617. return (sar << L2CAP_CTRL_SAR_SHIFT) & L2CAP_CTRL_SAR;
  618. }
  619. static inline bool __is_sar_start(struct l2cap_chan *chan, __u32 ctrl)
  620. {
  621. return __get_ctrl_sar(chan, ctrl) == L2CAP_SAR_START;
  622. }
  623. static inline __u32 __get_sar_mask(struct l2cap_chan *chan)
  624. {
  625. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  626. return L2CAP_EXT_CTRL_SAR;
  627. else
  628. return L2CAP_CTRL_SAR;
  629. }
  630. static inline __u8 __get_ctrl_super(struct l2cap_chan *chan, __u32 ctrl)
  631. {
  632. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  633. return (ctrl & L2CAP_EXT_CTRL_SUPERVISE) >>
  634. L2CAP_EXT_CTRL_SUPER_SHIFT;
  635. else
  636. return (ctrl & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT;
  637. }
  638. static inline __u32 __set_ctrl_super(struct l2cap_chan *chan, __u32 super)
  639. {
  640. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  641. return (super << L2CAP_EXT_CTRL_SUPER_SHIFT) &
  642. L2CAP_EXT_CTRL_SUPERVISE;
  643. else
  644. return (super << L2CAP_CTRL_SUPER_SHIFT) &
  645. L2CAP_CTRL_SUPERVISE;
  646. }
  647. static inline __u32 __set_ctrl_final(struct l2cap_chan *chan)
  648. {
  649. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  650. return L2CAP_EXT_CTRL_FINAL;
  651. else
  652. return L2CAP_CTRL_FINAL;
  653. }
  654. static inline bool __is_ctrl_final(struct l2cap_chan *chan, __u32 ctrl)
  655. {
  656. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  657. return ctrl & L2CAP_EXT_CTRL_FINAL;
  658. else
  659. return ctrl & L2CAP_CTRL_FINAL;
  660. }
  661. static inline __u32 __set_ctrl_poll(struct l2cap_chan *chan)
  662. {
  663. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  664. return L2CAP_EXT_CTRL_POLL;
  665. else
  666. return L2CAP_CTRL_POLL;
  667. }
  668. static inline bool __is_ctrl_poll(struct l2cap_chan *chan, __u32 ctrl)
  669. {
  670. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  671. return ctrl & L2CAP_EXT_CTRL_POLL;
  672. else
  673. return ctrl & L2CAP_CTRL_POLL;
  674. }
  675. static inline __u32 __get_control(struct l2cap_chan *chan, void *p)
  676. {
  677. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  678. return get_unaligned_le32(p);
  679. else
  680. return get_unaligned_le16(p);
  681. }
  682. static inline void __put_control(struct l2cap_chan *chan, __u32 control,
  683. void *p)
  684. {
  685. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  686. return put_unaligned_le32(control, p);
  687. else
  688. return put_unaligned_le16(control, p);
  689. }
  690. static inline __u8 __ctrl_size(struct l2cap_chan *chan)
  691. {
  692. if (test_bit(FLAG_EXT_CTRL, &chan->flags))
  693. return L2CAP_EXT_HDR_SIZE - L2CAP_HDR_SIZE;
  694. else
  695. return L2CAP_ENH_HDR_SIZE - L2CAP_HDR_SIZE;
  696. }
  697. extern bool disable_ertm;
  698. int l2cap_init_sockets(void);
  699. void l2cap_cleanup_sockets(void);
  700. void __l2cap_connect_rsp_defer(struct l2cap_chan *chan);
  701. int __l2cap_wait_ack(struct sock *sk);
  702. int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm);
  703. int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid);
  704. struct l2cap_chan *l2cap_chan_create(struct sock *sk);
  705. void l2cap_chan_close(struct l2cap_chan *chan, int reason);
  706. void l2cap_chan_destroy(struct l2cap_chan *chan);
  707. int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
  708. bdaddr_t *dst);
  709. int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len,
  710. u32 priority);
  711. void l2cap_chan_busy(struct l2cap_chan *chan, int busy);
  712. int l2cap_chan_check_security(struct l2cap_chan *chan);
  713. #endif /* __L2CAP_H */