l2cap.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. /* L2CAP defaults */
  25. #define L2CAP_DEFAULT_MTU 672
  26. #define L2CAP_DEFAULT_MIN_MTU 48
  27. #define L2CAP_DEFAULT_FLUSH_TO 0xffff
  28. #define L2CAP_DEFAULT_TX_WINDOW 63
  29. #define L2CAP_DEFAULT_MAX_TX 3
  30. #define L2CAP_DEFAULT_RETRANS_TO 2000 /* 2 seconds */
  31. #define L2CAP_DEFAULT_MONITOR_TO 12000 /* 12 seconds */
  32. #define L2CAP_DEFAULT_MAX_PDU_SIZE 1009 /* Sized for 3-DH5 packet */
  33. #define L2CAP_DEFAULT_ACK_TO 200
  34. #define L2CAP_LE_DEFAULT_MTU 23
  35. #define L2CAP_CONN_TIMEOUT (40000) /* 40 seconds */
  36. #define L2CAP_INFO_TIMEOUT (4000) /* 4 seconds */
  37. /* L2CAP socket address */
  38. struct sockaddr_l2 {
  39. sa_family_t l2_family;
  40. __le16 l2_psm;
  41. bdaddr_t l2_bdaddr;
  42. __le16 l2_cid;
  43. };
  44. /* L2CAP socket options */
  45. #define L2CAP_OPTIONS 0x01
  46. struct l2cap_options {
  47. __u16 omtu;
  48. __u16 imtu;
  49. __u16 flush_to;
  50. __u8 mode;
  51. __u8 fcs;
  52. __u8 max_tx;
  53. __u16 txwin_size;
  54. };
  55. #define L2CAP_CONNINFO 0x02
  56. struct l2cap_conninfo {
  57. __u16 hci_handle;
  58. __u8 dev_class[3];
  59. };
  60. #define L2CAP_LM 0x03
  61. #define L2CAP_LM_MASTER 0x0001
  62. #define L2CAP_LM_AUTH 0x0002
  63. #define L2CAP_LM_ENCRYPT 0x0004
  64. #define L2CAP_LM_TRUSTED 0x0008
  65. #define L2CAP_LM_RELIABLE 0x0010
  66. #define L2CAP_LM_SECURE 0x0020
  67. /* L2CAP command codes */
  68. #define L2CAP_COMMAND_REJ 0x01
  69. #define L2CAP_CONN_REQ 0x02
  70. #define L2CAP_CONN_RSP 0x03
  71. #define L2CAP_CONF_REQ 0x04
  72. #define L2CAP_CONF_RSP 0x05
  73. #define L2CAP_DISCONN_REQ 0x06
  74. #define L2CAP_DISCONN_RSP 0x07
  75. #define L2CAP_ECHO_REQ 0x08
  76. #define L2CAP_ECHO_RSP 0x09
  77. #define L2CAP_INFO_REQ 0x0a
  78. #define L2CAP_INFO_RSP 0x0b
  79. #define L2CAP_CONN_PARAM_UPDATE_REQ 0x12
  80. #define L2CAP_CONN_PARAM_UPDATE_RSP 0x13
  81. /* L2CAP feature mask */
  82. #define L2CAP_FEAT_FLOWCTL 0x00000001
  83. #define L2CAP_FEAT_RETRANS 0x00000002
  84. #define L2CAP_FEAT_ERTM 0x00000008
  85. #define L2CAP_FEAT_STREAMING 0x00000010
  86. #define L2CAP_FEAT_FCS 0x00000020
  87. #define L2CAP_FEAT_FIXED_CHAN 0x00000080
  88. /* L2CAP checksum option */
  89. #define L2CAP_FCS_NONE 0x00
  90. #define L2CAP_FCS_CRC16 0x01
  91. /* L2CAP Control Field bit masks */
  92. #define L2CAP_CTRL_SAR 0xC000
  93. #define L2CAP_CTRL_REQSEQ 0x3F00
  94. #define L2CAP_CTRL_TXSEQ 0x007E
  95. #define L2CAP_CTRL_RETRANS 0x0080
  96. #define L2CAP_CTRL_FINAL 0x0080
  97. #define L2CAP_CTRL_POLL 0x0010
  98. #define L2CAP_CTRL_SUPERVISE 0x000C
  99. #define L2CAP_CTRL_FRAME_TYPE 0x0001 /* I- or S-Frame */
  100. #define L2CAP_CTRL_TXSEQ_SHIFT 1
  101. #define L2CAP_CTRL_REQSEQ_SHIFT 8
  102. #define L2CAP_CTRL_SAR_SHIFT 14
  103. /* L2CAP Supervisory Function */
  104. #define L2CAP_SUPER_RCV_READY 0x0000
  105. #define L2CAP_SUPER_REJECT 0x0004
  106. #define L2CAP_SUPER_RCV_NOT_READY 0x0008
  107. #define L2CAP_SUPER_SELECT_REJECT 0x000C
  108. /* L2CAP Segmentation and Reassembly */
  109. #define L2CAP_SDU_UNSEGMENTED 0x0000
  110. #define L2CAP_SDU_START 0x4000
  111. #define L2CAP_SDU_END 0x8000
  112. #define L2CAP_SDU_CONTINUE 0xC000
  113. /* L2CAP structures */
  114. struct l2cap_hdr {
  115. __le16 len;
  116. __le16 cid;
  117. } __packed;
  118. #define L2CAP_HDR_SIZE 4
  119. struct l2cap_cmd_hdr {
  120. __u8 code;
  121. __u8 ident;
  122. __le16 len;
  123. } __packed;
  124. #define L2CAP_CMD_HDR_SIZE 4
  125. struct l2cap_cmd_rej {
  126. __le16 reason;
  127. } __packed;
  128. struct l2cap_conn_req {
  129. __le16 psm;
  130. __le16 scid;
  131. } __packed;
  132. struct l2cap_conn_rsp {
  133. __le16 dcid;
  134. __le16 scid;
  135. __le16 result;
  136. __le16 status;
  137. } __packed;
  138. /* channel indentifier */
  139. #define L2CAP_CID_SIGNALING 0x0001
  140. #define L2CAP_CID_CONN_LESS 0x0002
  141. #define L2CAP_CID_LE_DATA 0x0004
  142. #define L2CAP_CID_LE_SIGNALING 0x0005
  143. #define L2CAP_CID_SMP 0x0006
  144. #define L2CAP_CID_DYN_START 0x0040
  145. #define L2CAP_CID_DYN_END 0xffff
  146. /* connect result */
  147. #define L2CAP_CR_SUCCESS 0x0000
  148. #define L2CAP_CR_PEND 0x0001
  149. #define L2CAP_CR_BAD_PSM 0x0002
  150. #define L2CAP_CR_SEC_BLOCK 0x0003
  151. #define L2CAP_CR_NO_MEM 0x0004
  152. /* connect status */
  153. #define L2CAP_CS_NO_INFO 0x0000
  154. #define L2CAP_CS_AUTHEN_PEND 0x0001
  155. #define L2CAP_CS_AUTHOR_PEND 0x0002
  156. struct l2cap_conf_req {
  157. __le16 dcid;
  158. __le16 flags;
  159. __u8 data[0];
  160. } __packed;
  161. struct l2cap_conf_rsp {
  162. __le16 scid;
  163. __le16 flags;
  164. __le16 result;
  165. __u8 data[0];
  166. } __packed;
  167. #define L2CAP_CONF_SUCCESS 0x0000
  168. #define L2CAP_CONF_UNACCEPT 0x0001
  169. #define L2CAP_CONF_REJECT 0x0002
  170. #define L2CAP_CONF_UNKNOWN 0x0003
  171. struct l2cap_conf_opt {
  172. __u8 type;
  173. __u8 len;
  174. __u8 val[0];
  175. } __packed;
  176. #define L2CAP_CONF_OPT_SIZE 2
  177. #define L2CAP_CONF_HINT 0x80
  178. #define L2CAP_CONF_MASK 0x7f
  179. #define L2CAP_CONF_MTU 0x01
  180. #define L2CAP_CONF_FLUSH_TO 0x02
  181. #define L2CAP_CONF_QOS 0x03
  182. #define L2CAP_CONF_RFC 0x04
  183. #define L2CAP_CONF_FCS 0x05
  184. #define L2CAP_CONF_MAX_SIZE 22
  185. struct l2cap_conf_rfc {
  186. __u8 mode;
  187. __u8 txwin_size;
  188. __u8 max_transmit;
  189. __le16 retrans_timeout;
  190. __le16 monitor_timeout;
  191. __le16 max_pdu_size;
  192. } __packed;
  193. #define L2CAP_MODE_BASIC 0x00
  194. #define L2CAP_MODE_RETRANS 0x01
  195. #define L2CAP_MODE_FLOWCTL 0x02
  196. #define L2CAP_MODE_ERTM 0x03
  197. #define L2CAP_MODE_STREAMING 0x04
  198. struct l2cap_disconn_req {
  199. __le16 dcid;
  200. __le16 scid;
  201. } __packed;
  202. struct l2cap_disconn_rsp {
  203. __le16 dcid;
  204. __le16 scid;
  205. } __packed;
  206. struct l2cap_info_req {
  207. __le16 type;
  208. } __packed;
  209. struct l2cap_info_rsp {
  210. __le16 type;
  211. __le16 result;
  212. __u8 data[0];
  213. } __packed;
  214. /* info type */
  215. #define L2CAP_IT_CL_MTU 0x0001
  216. #define L2CAP_IT_FEAT_MASK 0x0002
  217. #define L2CAP_IT_FIXED_CHAN 0x0003
  218. /* info result */
  219. #define L2CAP_IR_SUCCESS 0x0000
  220. #define L2CAP_IR_NOTSUPP 0x0001
  221. struct l2cap_conn_param_update_req {
  222. __le16 min;
  223. __le16 max;
  224. __le16 latency;
  225. __le16 to_multiplier;
  226. } __packed;
  227. struct l2cap_conn_param_update_rsp {
  228. __le16 result;
  229. } __packed;
  230. /* Connection Parameters result */
  231. #define L2CAP_CONN_PARAM_ACCEPTED 0x0000
  232. #define L2CAP_CONN_PARAM_REJECTED 0x0001
  233. /* ----- L2CAP channels and connections ----- */
  234. struct srej_list {
  235. __u8 tx_seq;
  236. struct list_head list;
  237. };
  238. struct l2cap_chan {
  239. struct sock *sk;
  240. struct l2cap_conn *conn;
  241. __u8 state;
  242. atomic_t refcnt;
  243. __le16 psm;
  244. __u16 dcid;
  245. __u16 scid;
  246. __u16 imtu;
  247. __u16 omtu;
  248. __u16 flush_to;
  249. __u8 mode;
  250. __u8 chan_type;
  251. __le16 sport;
  252. __u8 sec_level;
  253. __u8 role_switch;
  254. __u8 force_reliable;
  255. __u8 flushable;
  256. __u8 force_active;
  257. __u8 ident;
  258. __u8 conf_req[64];
  259. __u8 conf_len;
  260. __u8 num_conf_req;
  261. __u8 num_conf_rsp;
  262. __u8 fcs;
  263. __u8 tx_win;
  264. __u8 max_tx;
  265. __u16 retrans_timeout;
  266. __u16 monitor_timeout;
  267. __u16 mps;
  268. unsigned long conf_state;
  269. unsigned long conn_state;
  270. __u8 next_tx_seq;
  271. __u8 expected_ack_seq;
  272. __u8 expected_tx_seq;
  273. __u8 buffer_seq;
  274. __u8 buffer_seq_srej;
  275. __u8 srej_save_reqseq;
  276. __u8 frames_sent;
  277. __u8 unacked_frames;
  278. __u8 retry_count;
  279. __u8 num_acked;
  280. __u16 sdu_len;
  281. __u16 partial_sdu_len;
  282. struct sk_buff *sdu;
  283. __u8 remote_tx_win;
  284. __u8 remote_max_tx;
  285. __u16 remote_mps;
  286. struct timer_list chan_timer;
  287. struct timer_list retrans_timer;
  288. struct timer_list monitor_timer;
  289. struct timer_list ack_timer;
  290. struct sk_buff *tx_send_head;
  291. struct sk_buff_head tx_q;
  292. struct sk_buff_head srej_q;
  293. struct list_head srej_l;
  294. struct list_head list;
  295. struct list_head global_l;
  296. void *data;
  297. struct l2cap_ops *ops;
  298. };
  299. struct l2cap_ops {
  300. char *name;
  301. struct l2cap_chan *(*new_connection) (void *data);
  302. int (*recv) (void *data, struct sk_buff *skb);
  303. void (*close) (void *data);
  304. void (*state_change) (void *data, int state);
  305. };
  306. struct l2cap_conn {
  307. struct hci_conn *hcon;
  308. bdaddr_t *dst;
  309. bdaddr_t *src;
  310. unsigned int mtu;
  311. __u32 feat_mask;
  312. __u8 info_state;
  313. __u8 info_ident;
  314. struct timer_list info_timer;
  315. spinlock_t lock;
  316. struct sk_buff *rx_skb;
  317. __u32 rx_len;
  318. __u8 tx_ident;
  319. __u8 disc_reason;
  320. __u8 preq[7]; /* SMP Pairing Request */
  321. __u8 prsp[7]; /* SMP Pairing Response */
  322. __u8 prnd[16]; /* SMP Pairing Random */
  323. __u8 pcnf[16]; /* SMP Pairing Confirm */
  324. __u8 tk[16]; /* SMP Temporary Key */
  325. __u8 smp_key_size;
  326. struct timer_list security_timer;
  327. struct list_head chan_l;
  328. rwlock_t chan_lock;
  329. };
  330. #define L2CAP_INFO_CL_MTU_REQ_SENT 0x01
  331. #define L2CAP_INFO_FEAT_MASK_REQ_SENT 0x04
  332. #define L2CAP_INFO_FEAT_MASK_REQ_DONE 0x08
  333. #define L2CAP_CHAN_RAW 1
  334. #define L2CAP_CHAN_CONN_LESS 2
  335. #define L2CAP_CHAN_CONN_ORIENTED 3
  336. /* ----- L2CAP socket info ----- */
  337. #define l2cap_pi(sk) ((struct l2cap_pinfo *) sk)
  338. struct l2cap_pinfo {
  339. struct bt_sock bt;
  340. struct l2cap_chan *chan;
  341. struct sk_buff *rx_busy_skb;
  342. };
  343. enum {
  344. CONF_REQ_SENT,
  345. CONF_INPUT_DONE,
  346. CONF_OUTPUT_DONE,
  347. CONF_MTU_DONE,
  348. CONF_MODE_DONE,
  349. CONF_CONNECT_PEND,
  350. CONF_NO_FCS_RECV,
  351. CONF_STATE2_DEVICE,
  352. };
  353. #define L2CAP_CONF_MAX_CONF_REQ 2
  354. #define L2CAP_CONF_MAX_CONF_RSP 2
  355. enum {
  356. CONN_SAR_SDU,
  357. CONN_SREJ_SENT,
  358. CONN_WAIT_F,
  359. CONN_SREJ_ACT,
  360. CONN_SEND_PBIT,
  361. CONN_REMOTE_BUSY,
  362. CONN_LOCAL_BUSY,
  363. CONN_REJ_ACT,
  364. CONN_SEND_FBIT,
  365. CONN_RNR_SENT,
  366. };
  367. #define __set_chan_timer(c, t) l2cap_set_timer(c, &c->chan_timer, (t))
  368. #define __clear_chan_timer(c) l2cap_clear_timer(c, &c->chan_timer)
  369. #define __set_retrans_timer(c) l2cap_set_timer(c, &c->retrans_timer, \
  370. L2CAP_DEFAULT_RETRANS_TO);
  371. #define __clear_retrans_timer(c) l2cap_clear_timer(c, &c->retrans_timer)
  372. #define __set_monitor_timer(c) l2cap_set_timer(c, &c->monitor_timer, \
  373. L2CAP_DEFAULT_MONITOR_TO);
  374. #define __clear_monitor_timer(c) l2cap_clear_timer(c, &c->monitor_timer)
  375. #define __set_ack_timer(c) l2cap_set_timer(c, &chan->ack_timer, \
  376. L2CAP_DEFAULT_ACK_TO);
  377. #define __clear_ack_timer(c) l2cap_clear_timer(c, &c->ack_timer)
  378. static inline int l2cap_tx_window_full(struct l2cap_chan *ch)
  379. {
  380. int sub;
  381. sub = (ch->next_tx_seq - ch->expected_ack_seq) % 64;
  382. if (sub < 0)
  383. sub += 64;
  384. return sub == ch->remote_tx_win;
  385. }
  386. #define __get_txseq(ctrl) (((ctrl) & L2CAP_CTRL_TXSEQ) >> 1)
  387. #define __get_reqseq(ctrl) (((ctrl) & L2CAP_CTRL_REQSEQ) >> 8)
  388. #define __is_iframe(ctrl) (!((ctrl) & L2CAP_CTRL_FRAME_TYPE))
  389. #define __is_sframe(ctrl) ((ctrl) & L2CAP_CTRL_FRAME_TYPE)
  390. #define __is_sar_start(ctrl) (((ctrl) & L2CAP_CTRL_SAR) == L2CAP_SDU_START)
  391. extern int disable_ertm;
  392. int l2cap_init_sockets(void);
  393. void l2cap_cleanup_sockets(void);
  394. void __l2cap_connect_rsp_defer(struct l2cap_chan *chan);
  395. int __l2cap_wait_ack(struct sock *sk);
  396. int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm);
  397. int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid);
  398. struct l2cap_chan *l2cap_chan_create(struct sock *sk);
  399. void l2cap_chan_close(struct l2cap_chan *chan, int reason);
  400. void l2cap_chan_destroy(struct l2cap_chan *chan);
  401. int l2cap_chan_connect(struct l2cap_chan *chan);
  402. int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len);
  403. void l2cap_chan_busy(struct l2cap_chan *chan, int busy);
  404. #endif /* __L2CAP_H */