l2cap.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (C) 2000-2001 Qualcomm Incorporated
  4. Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License version 2 as
  7. published by the Free Software Foundation;
  8. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  9. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  10. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  11. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  12. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  13. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  17. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  18. SOFTWARE IS DISCLAIMED.
  19. */
  20. #ifndef __L2CAP_H
  21. #define __L2CAP_H
  22. /* L2CAP defaults */
  23. #define L2CAP_DEFAULT_MTU 672
  24. #define L2CAP_DEFAULT_MIN_MTU 48
  25. #define L2CAP_DEFAULT_FLUSH_TO 0xffff
  26. #define L2CAP_DEFAULT_TX_WINDOW 63
  27. #define L2CAP_DEFAULT_NUM_TO_ACK (L2CAP_DEFAULT_TX_WINDOW/5)
  28. #define L2CAP_DEFAULT_MAX_TX 3
  29. #define L2CAP_DEFAULT_RETRANS_TO 1000 /* 1 second */
  30. #define L2CAP_DEFAULT_MONITOR_TO 12000 /* 12 seconds */
  31. #define L2CAP_DEFAULT_MAX_PDU_SIZE 672
  32. #define L2CAP_CONN_TIMEOUT (40000) /* 40 seconds */
  33. #define L2CAP_INFO_TIMEOUT (4000) /* 4 seconds */
  34. /* L2CAP socket address */
  35. struct sockaddr_l2 {
  36. sa_family_t l2_family;
  37. __le16 l2_psm;
  38. bdaddr_t l2_bdaddr;
  39. __le16 l2_cid;
  40. };
  41. /* L2CAP socket options */
  42. #define L2CAP_OPTIONS 0x01
  43. struct l2cap_options {
  44. __u16 omtu;
  45. __u16 imtu;
  46. __u16 flush_to;
  47. __u8 mode;
  48. __u8 fcs;
  49. };
  50. #define L2CAP_CONNINFO 0x02
  51. struct l2cap_conninfo {
  52. __u16 hci_handle;
  53. __u8 dev_class[3];
  54. };
  55. #define L2CAP_LM 0x03
  56. #define L2CAP_LM_MASTER 0x0001
  57. #define L2CAP_LM_AUTH 0x0002
  58. #define L2CAP_LM_ENCRYPT 0x0004
  59. #define L2CAP_LM_TRUSTED 0x0008
  60. #define L2CAP_LM_RELIABLE 0x0010
  61. #define L2CAP_LM_SECURE 0x0020
  62. /* L2CAP command codes */
  63. #define L2CAP_COMMAND_REJ 0x01
  64. #define L2CAP_CONN_REQ 0x02
  65. #define L2CAP_CONN_RSP 0x03
  66. #define L2CAP_CONF_REQ 0x04
  67. #define L2CAP_CONF_RSP 0x05
  68. #define L2CAP_DISCONN_REQ 0x06
  69. #define L2CAP_DISCONN_RSP 0x07
  70. #define L2CAP_ECHO_REQ 0x08
  71. #define L2CAP_ECHO_RSP 0x09
  72. #define L2CAP_INFO_REQ 0x0a
  73. #define L2CAP_INFO_RSP 0x0b
  74. /* L2CAP feature mask */
  75. #define L2CAP_FEAT_FLOWCTL 0x00000001
  76. #define L2CAP_FEAT_RETRANS 0x00000002
  77. #define L2CAP_FEAT_ERTM 0x00000008
  78. #define L2CAP_FEAT_STREAMING 0x00000010
  79. #define L2CAP_FEAT_FCS 0x00000020
  80. #define L2CAP_FEAT_FIXED_CHAN 0x00000080
  81. /* L2CAP checksum option */
  82. #define L2CAP_FCS_NONE 0x00
  83. #define L2CAP_FCS_CRC16 0x01
  84. /* L2CAP Control Field bit masks */
  85. #define L2CAP_CTRL_SAR 0xC000
  86. #define L2CAP_CTRL_REQSEQ 0x3F00
  87. #define L2CAP_CTRL_TXSEQ 0x007E
  88. #define L2CAP_CTRL_RETRANS 0x0080
  89. #define L2CAP_CTRL_FINAL 0x0080
  90. #define L2CAP_CTRL_POLL 0x0010
  91. #define L2CAP_CTRL_SUPERVISE 0x000C
  92. #define L2CAP_CTRL_FRAME_TYPE 0x0001 /* I- or S-Frame */
  93. #define L2CAP_CTRL_TXSEQ_SHIFT 1
  94. #define L2CAP_CTRL_REQSEQ_SHIFT 8
  95. #define L2CAP_CTRL_SAR_SHIFT 14
  96. /* L2CAP Supervisory Function */
  97. #define L2CAP_SUPER_RCV_READY 0x0000
  98. #define L2CAP_SUPER_REJECT 0x0004
  99. #define L2CAP_SUPER_RCV_NOT_READY 0x0008
  100. #define L2CAP_SUPER_SELECT_REJECT 0x000C
  101. /* L2CAP Segmentation and Reassembly */
  102. #define L2CAP_SDU_UNSEGMENTED 0x0000
  103. #define L2CAP_SDU_START 0x4000
  104. #define L2CAP_SDU_END 0x8000
  105. #define L2CAP_SDU_CONTINUE 0xC000
  106. /* L2CAP structures */
  107. struct l2cap_hdr {
  108. __le16 len;
  109. __le16 cid;
  110. } __attribute__ ((packed));
  111. #define L2CAP_HDR_SIZE 4
  112. struct l2cap_cmd_hdr {
  113. __u8 code;
  114. __u8 ident;
  115. __le16 len;
  116. } __attribute__ ((packed));
  117. #define L2CAP_CMD_HDR_SIZE 4
  118. struct l2cap_cmd_rej {
  119. __le16 reason;
  120. } __attribute__ ((packed));
  121. struct l2cap_conn_req {
  122. __le16 psm;
  123. __le16 scid;
  124. } __attribute__ ((packed));
  125. struct l2cap_conn_rsp {
  126. __le16 dcid;
  127. __le16 scid;
  128. __le16 result;
  129. __le16 status;
  130. } __attribute__ ((packed));
  131. /* channel indentifier */
  132. #define L2CAP_CID_SIGNALING 0x0001
  133. #define L2CAP_CID_CONN_LESS 0x0002
  134. #define L2CAP_CID_DYN_START 0x0040
  135. #define L2CAP_CID_DYN_END 0xffff
  136. /* connect result */
  137. #define L2CAP_CR_SUCCESS 0x0000
  138. #define L2CAP_CR_PEND 0x0001
  139. #define L2CAP_CR_BAD_PSM 0x0002
  140. #define L2CAP_CR_SEC_BLOCK 0x0003
  141. #define L2CAP_CR_NO_MEM 0x0004
  142. /* connect status */
  143. #define L2CAP_CS_NO_INFO 0x0000
  144. #define L2CAP_CS_AUTHEN_PEND 0x0001
  145. #define L2CAP_CS_AUTHOR_PEND 0x0002
  146. struct l2cap_conf_req {
  147. __le16 dcid;
  148. __le16 flags;
  149. __u8 data[0];
  150. } __attribute__ ((packed));
  151. struct l2cap_conf_rsp {
  152. __le16 scid;
  153. __le16 flags;
  154. __le16 result;
  155. __u8 data[0];
  156. } __attribute__ ((packed));
  157. #define L2CAP_CONF_SUCCESS 0x0000
  158. #define L2CAP_CONF_UNACCEPT 0x0001
  159. #define L2CAP_CONF_REJECT 0x0002
  160. #define L2CAP_CONF_UNKNOWN 0x0003
  161. struct l2cap_conf_opt {
  162. __u8 type;
  163. __u8 len;
  164. __u8 val[0];
  165. } __attribute__ ((packed));
  166. #define L2CAP_CONF_OPT_SIZE 2
  167. #define L2CAP_CONF_HINT 0x80
  168. #define L2CAP_CONF_MASK 0x7f
  169. #define L2CAP_CONF_MTU 0x01
  170. #define L2CAP_CONF_FLUSH_TO 0x02
  171. #define L2CAP_CONF_QOS 0x03
  172. #define L2CAP_CONF_RFC 0x04
  173. #define L2CAP_CONF_FCS 0x05
  174. #define L2CAP_CONF_MAX_SIZE 22
  175. struct l2cap_conf_rfc {
  176. __u8 mode;
  177. __u8 txwin_size;
  178. __u8 max_transmit;
  179. __le16 retrans_timeout;
  180. __le16 monitor_timeout;
  181. __le16 max_pdu_size;
  182. } __attribute__ ((packed));
  183. #define L2CAP_MODE_BASIC 0x00
  184. #define L2CAP_MODE_RETRANS 0x01
  185. #define L2CAP_MODE_FLOWCTL 0x02
  186. #define L2CAP_MODE_ERTM 0x03
  187. #define L2CAP_MODE_STREAMING 0x04
  188. struct l2cap_disconn_req {
  189. __le16 dcid;
  190. __le16 scid;
  191. } __attribute__ ((packed));
  192. struct l2cap_disconn_rsp {
  193. __le16 dcid;
  194. __le16 scid;
  195. } __attribute__ ((packed));
  196. struct l2cap_info_req {
  197. __le16 type;
  198. } __attribute__ ((packed));
  199. struct l2cap_info_rsp {
  200. __le16 type;
  201. __le16 result;
  202. __u8 data[0];
  203. } __attribute__ ((packed));
  204. /* info type */
  205. #define L2CAP_IT_CL_MTU 0x0001
  206. #define L2CAP_IT_FEAT_MASK 0x0002
  207. #define L2CAP_IT_FIXED_CHAN 0x0003
  208. /* info result */
  209. #define L2CAP_IR_SUCCESS 0x0000
  210. #define L2CAP_IR_NOTSUPP 0x0001
  211. /* ----- L2CAP connections ----- */
  212. struct l2cap_chan_list {
  213. struct sock *head;
  214. rwlock_t lock;
  215. long num;
  216. };
  217. struct l2cap_conn {
  218. struct hci_conn *hcon;
  219. bdaddr_t *dst;
  220. bdaddr_t *src;
  221. unsigned int mtu;
  222. __u32 feat_mask;
  223. __u8 info_state;
  224. __u8 info_ident;
  225. struct timer_list info_timer;
  226. spinlock_t lock;
  227. struct sk_buff *rx_skb;
  228. __u32 rx_len;
  229. __u8 rx_ident;
  230. __u8 tx_ident;
  231. __u8 disc_reason;
  232. struct l2cap_chan_list chan_list;
  233. };
  234. #define L2CAP_INFO_CL_MTU_REQ_SENT 0x01
  235. #define L2CAP_INFO_FEAT_MASK_REQ_SENT 0x04
  236. #define L2CAP_INFO_FEAT_MASK_REQ_DONE 0x08
  237. /* ----- L2CAP channel and socket info ----- */
  238. #define l2cap_pi(sk) ((struct l2cap_pinfo *) sk)
  239. #define TX_QUEUE(sk) (&l2cap_pi(sk)->tx_queue)
  240. #define SREJ_QUEUE(sk) (&l2cap_pi(sk)->srej_queue)
  241. #define SREJ_LIST(sk) (&l2cap_pi(sk)->srej_l.list)
  242. struct srej_list {
  243. __u8 tx_seq;
  244. struct list_head list;
  245. };
  246. struct l2cap_pinfo {
  247. struct bt_sock bt;
  248. __le16 psm;
  249. __u16 dcid;
  250. __u16 scid;
  251. __u16 imtu;
  252. __u16 omtu;
  253. __u16 flush_to;
  254. __u8 mode;
  255. __u8 num_conf_req;
  256. __u8 num_conf_rsp;
  257. __u8 fcs;
  258. __u8 sec_level;
  259. __u8 role_switch;
  260. __u8 force_reliable;
  261. __u8 conf_req[64];
  262. __u8 conf_len;
  263. __u8 conf_state;
  264. __u8 conn_state;
  265. __u8 next_tx_seq;
  266. __u8 expected_ack_seq;
  267. __u8 req_seq;
  268. __u8 expected_tx_seq;
  269. __u8 buffer_seq;
  270. __u8 buffer_seq_srej;
  271. __u8 srej_save_reqseq;
  272. __u8 unacked_frames;
  273. __u8 retry_count;
  274. __u8 num_to_ack;
  275. __u16 sdu_len;
  276. __u16 partial_sdu_len;
  277. struct sk_buff *sdu;
  278. __u8 ident;
  279. __u8 remote_tx_win;
  280. __u8 remote_max_tx;
  281. __u16 retrans_timeout;
  282. __u16 monitor_timeout;
  283. __u16 max_pdu_size;
  284. __le16 sport;
  285. struct timer_list retrans_timer;
  286. struct timer_list monitor_timer;
  287. struct sk_buff_head tx_queue;
  288. struct sk_buff_head srej_queue;
  289. struct srej_list srej_l;
  290. struct l2cap_conn *conn;
  291. struct sock *next_c;
  292. struct sock *prev_c;
  293. };
  294. #define L2CAP_CONF_REQ_SENT 0x01
  295. #define L2CAP_CONF_INPUT_DONE 0x02
  296. #define L2CAP_CONF_OUTPUT_DONE 0x04
  297. #define L2CAP_CONF_MTU_DONE 0x08
  298. #define L2CAP_CONF_MODE_DONE 0x10
  299. #define L2CAP_CONF_CONNECT_PEND 0x20
  300. #define L2CAP_CONF_NO_FCS_RECV 0x40
  301. #define L2CAP_CONF_STATE2_DEVICE 0x80
  302. #define L2CAP_CONF_MAX_CONF_REQ 2
  303. #define L2CAP_CONF_MAX_CONF_RSP 2
  304. #define L2CAP_CONN_SAR_SDU 0x01
  305. #define L2CAP_CONN_SREJ_SENT 0x02
  306. #define L2CAP_CONN_WAIT_F 0x04
  307. #define L2CAP_CONN_SREJ_ACT 0x08
  308. #define L2CAP_CONN_SEND_PBIT 0x10
  309. #define L2CAP_CONN_REMOTE_BUSY 0x20
  310. #define L2CAP_CONN_LOCAL_BUSY 0x40
  311. #define __mod_retrans_timer() mod_timer(&l2cap_pi(sk)->retrans_timer, \
  312. jiffies + msecs_to_jiffies(L2CAP_DEFAULT_RETRANS_TO));
  313. #define __mod_monitor_timer() mod_timer(&l2cap_pi(sk)->monitor_timer, \
  314. jiffies + msecs_to_jiffies(L2CAP_DEFAULT_MONITOR_TO));
  315. static inline int l2cap_tx_window_full(struct sock *sk)
  316. {
  317. struct l2cap_pinfo *pi = l2cap_pi(sk);
  318. int sub;
  319. sub = (pi->next_tx_seq - pi->expected_ack_seq) % 64;
  320. if (sub < 0)
  321. sub += 64;
  322. return (sub == pi->remote_tx_win);
  323. }
  324. #define __get_txseq(ctrl) ((ctrl) & L2CAP_CTRL_TXSEQ) >> 1
  325. #define __get_reqseq(ctrl) ((ctrl) & L2CAP_CTRL_REQSEQ) >> 8
  326. #define __is_iframe(ctrl) !((ctrl) & L2CAP_CTRL_FRAME_TYPE)
  327. #define __is_sframe(ctrl) (ctrl) & L2CAP_CTRL_FRAME_TYPE
  328. #define __is_sar_start(ctrl) ((ctrl) & L2CAP_CTRL_SAR) == L2CAP_SDU_START
  329. void l2cap_load(void);
  330. #endif /* __L2CAP_H */