bt-l2cap.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. /*
  2. * QEMU Bluetooth L2CAP logic.
  3. *
  4. * Copyright (C) 2008 Andrzej Zaborowski <balrog@zabor.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu-common.h"
  20. #include "qemu-timer.h"
  21. #include "bt.h"
  22. #define L2CAP_CID_MAX 0x100 /* Between 0x40 and 0x10000 */
  23. struct l2cap_instance_s {
  24. struct bt_link_s *link;
  25. struct bt_l2cap_device_s *dev;
  26. int role;
  27. uint8_t frame_in[65535 + L2CAP_HDR_SIZE] __attribute__ ((aligned (4)));
  28. int frame_in_len;
  29. uint8_t frame_out[65535 + L2CAP_HDR_SIZE] __attribute__ ((aligned (4)));
  30. int frame_out_len;
  31. /* Signalling channel timers. They exist per-request but we can make
  32. * sure we have no more than one outstanding request at any time. */
  33. QEMUTimer *rtx;
  34. QEMUTimer *ertx;
  35. int last_id;
  36. int next_id;
  37. struct l2cap_chan_s {
  38. struct bt_l2cap_conn_params_s params;
  39. void (*frame_in)(struct l2cap_chan_s *chan, uint16_t cid,
  40. const l2cap_hdr *hdr, int len);
  41. int mps;
  42. int min_mtu;
  43. struct l2cap_instance_s *l2cap;
  44. /* Only allocated channels */
  45. uint16_t remote_cid;
  46. #define L2CAP_CFG_INIT 2
  47. #define L2CAP_CFG_ACC 1
  48. int config_req_id; /* TODO: handle outgoing requests generically */
  49. int config;
  50. /* Only connection-oriented channels. Note: if we allow the tx and
  51. * rx traffic to be in different modes at any time, we need two. */
  52. int mode;
  53. /* Only flow-controlled, connection-oriented channels */
  54. uint8_t sdu[65536]; /* TODO: dynamically allocate */
  55. int len_cur, len_total;
  56. int rexmit;
  57. int monitor_timeout;
  58. QEMUTimer *monitor_timer;
  59. QEMUTimer *retransmission_timer;
  60. } *cid[L2CAP_CID_MAX];
  61. /* The channel state machine states map as following:
  62. * CLOSED -> !cid[N]
  63. * WAIT_CONNECT -> never occurs
  64. * WAIT_CONNECT_RSP -> never occurs
  65. * CONFIG -> cid[N] && config < 3
  66. * WAIT_CONFIG -> never occurs, cid[N] && config == 0 && !config_r
  67. * WAIT_SEND_CONFIG -> never occurs, cid[N] && config == 1 && !config_r
  68. * WAIT_CONFIG_REQ_RSP -> cid[N] && config == 0 && config_req_id
  69. * WAIT_CONFIG_RSP -> cid[N] && config == 1 && config_req_id
  70. * WAIT_CONFIG_REQ -> cid[N] && config == 2
  71. * OPEN -> cid[N] && config == 3
  72. * WAIT_DISCONNECT -> never occurs
  73. */
  74. struct l2cap_chan_s signalling_ch;
  75. struct l2cap_chan_s group_ch;
  76. };
  77. struct slave_l2cap_instance_s {
  78. struct bt_link_s link; /* Underlying logical link (ACL) */
  79. struct l2cap_instance_s l2cap;
  80. };
  81. struct bt_l2cap_psm_s {
  82. int psm;
  83. int min_mtu;
  84. int (*new_channel)(struct bt_l2cap_device_s *device,
  85. struct bt_l2cap_conn_params_s *params);
  86. struct bt_l2cap_psm_s *next;
  87. };
  88. static const uint16_t l2cap_fcs16_table[256] = {
  89. 0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
  90. 0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
  91. 0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
  92. 0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
  93. 0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
  94. 0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
  95. 0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
  96. 0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
  97. 0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
  98. 0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
  99. 0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
  100. 0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
  101. 0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
  102. 0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
  103. 0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
  104. 0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
  105. 0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
  106. 0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
  107. 0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
  108. 0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
  109. 0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
  110. 0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
  111. 0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
  112. 0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
  113. 0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
  114. 0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
  115. 0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
  116. 0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
  117. 0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
  118. 0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
  119. 0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
  120. 0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040,
  121. };
  122. static uint16_t l2cap_fcs16(const uint8_t *message, int len)
  123. {
  124. uint16_t fcs = 0x0000;
  125. while (len --)
  126. #if 0
  127. {
  128. int i;
  129. fcs ^= *message ++;
  130. for (i = 8; i; -- i)
  131. if (fcs & 1)
  132. fcs = (fcs >> 1) ^ 0xa001;
  133. else
  134. fcs = (fcs >> 1);
  135. }
  136. #else
  137. fcs = (fcs >> 8) ^ l2cap_fcs16_table[(fcs ^ *message ++) & 0xff];
  138. #endif
  139. return fcs;
  140. }
  141. /* L2CAP layer logic (protocol) */
  142. static void l2cap_retransmission_timer_update(struct l2cap_chan_s *ch)
  143. {
  144. #if 0
  145. if (ch->mode != L2CAP_MODE_BASIC && ch->rexmit)
  146. qemu_mod_timer(ch->retransmission_timer);
  147. else
  148. qemu_del_timer(ch->retransmission_timer);
  149. #endif
  150. }
  151. static void l2cap_monitor_timer_update(struct l2cap_chan_s *ch)
  152. {
  153. #if 0
  154. if (ch->mode != L2CAP_MODE_BASIC && !ch->rexmit)
  155. qemu_mod_timer(ch->monitor_timer);
  156. else
  157. qemu_del_timer(ch->monitor_timer);
  158. #endif
  159. }
  160. static void l2cap_command_reject(struct l2cap_instance_s *l2cap, int id,
  161. uint16_t reason, const void *data, int plen)
  162. {
  163. uint8_t *pkt;
  164. l2cap_cmd_hdr *hdr;
  165. l2cap_cmd_rej *params;
  166. uint16_t len;
  167. reason = cpu_to_le16(reason);
  168. len = cpu_to_le16(L2CAP_CMD_REJ_SIZE + plen);
  169. pkt = l2cap->signalling_ch.params.sdu_out(&l2cap->signalling_ch.params,
  170. L2CAP_CMD_HDR_SIZE + L2CAP_CMD_REJ_SIZE + plen);
  171. hdr = (void *) (pkt + 0);
  172. params = (void *) (pkt + L2CAP_CMD_HDR_SIZE);
  173. hdr->code = L2CAP_COMMAND_REJ;
  174. hdr->ident = id;
  175. memcpy(&hdr->len, &len, sizeof(hdr->len));
  176. memcpy(&params->reason, &reason, sizeof(reason));
  177. if (plen)
  178. memcpy(pkt + L2CAP_CMD_HDR_SIZE + L2CAP_CMD_REJ_SIZE, data, plen);
  179. l2cap->signalling_ch.params.sdu_submit(&l2cap->signalling_ch.params);
  180. }
  181. static void l2cap_command_reject_cid(struct l2cap_instance_s *l2cap, int id,
  182. uint16_t reason, uint16_t dcid, uint16_t scid)
  183. {
  184. l2cap_cmd_rej_cid params = {
  185. .dcid = dcid,
  186. .scid = scid,
  187. };
  188. l2cap_command_reject(l2cap, id, reason, &params, L2CAP_CMD_REJ_CID_SIZE);
  189. }
  190. static void l2cap_connection_response(struct l2cap_instance_s *l2cap,
  191. int dcid, int scid, int result, int status)
  192. {
  193. uint8_t *pkt;
  194. l2cap_cmd_hdr *hdr;
  195. l2cap_conn_rsp *params;
  196. pkt = l2cap->signalling_ch.params.sdu_out(&l2cap->signalling_ch.params,
  197. L2CAP_CMD_HDR_SIZE + L2CAP_CONN_RSP_SIZE);
  198. hdr = (void *) (pkt + 0);
  199. params = (void *) (pkt + L2CAP_CMD_HDR_SIZE);
  200. hdr->code = L2CAP_CONN_RSP;
  201. hdr->ident = l2cap->last_id;
  202. hdr->len = cpu_to_le16(L2CAP_CONN_RSP_SIZE);
  203. params->dcid = cpu_to_le16(dcid);
  204. params->scid = cpu_to_le16(scid);
  205. params->result = cpu_to_le16(result);
  206. params->status = cpu_to_le16(status);
  207. l2cap->signalling_ch.params.sdu_submit(&l2cap->signalling_ch.params);
  208. }
  209. static void l2cap_configuration_request(struct l2cap_instance_s *l2cap,
  210. int dcid, int flag, const uint8_t *data, int len)
  211. {
  212. uint8_t *pkt;
  213. l2cap_cmd_hdr *hdr;
  214. l2cap_conf_req *params;
  215. pkt = l2cap->signalling_ch.params.sdu_out(&l2cap->signalling_ch.params,
  216. L2CAP_CMD_HDR_SIZE + L2CAP_CONF_REQ_SIZE(len));
  217. hdr = (void *) (pkt + 0);
  218. params = (void *) (pkt + L2CAP_CMD_HDR_SIZE);
  219. /* TODO: unify the id sequencing */
  220. l2cap->last_id = l2cap->next_id;
  221. l2cap->next_id = l2cap->next_id == 255 ? 1 : l2cap->next_id + 1;
  222. hdr->code = L2CAP_CONF_REQ;
  223. hdr->ident = l2cap->last_id;
  224. hdr->len = cpu_to_le16(L2CAP_CONF_REQ_SIZE(len));
  225. params->dcid = cpu_to_le16(dcid);
  226. params->flags = cpu_to_le16(flag);
  227. if (len)
  228. memcpy(params->data, data, len);
  229. l2cap->signalling_ch.params.sdu_submit(&l2cap->signalling_ch.params);
  230. }
  231. static void l2cap_configuration_response(struct l2cap_instance_s *l2cap,
  232. int scid, int flag, int result, const uint8_t *data, int len)
  233. {
  234. uint8_t *pkt;
  235. l2cap_cmd_hdr *hdr;
  236. l2cap_conf_rsp *params;
  237. pkt = l2cap->signalling_ch.params.sdu_out(&l2cap->signalling_ch.params,
  238. L2CAP_CMD_HDR_SIZE + L2CAP_CONF_RSP_SIZE(len));
  239. hdr = (void *) (pkt + 0);
  240. params = (void *) (pkt + L2CAP_CMD_HDR_SIZE);
  241. hdr->code = L2CAP_CONF_RSP;
  242. hdr->ident = l2cap->last_id;
  243. hdr->len = cpu_to_le16(L2CAP_CONF_RSP_SIZE(len));
  244. params->scid = cpu_to_le16(scid);
  245. params->flags = cpu_to_le16(flag);
  246. params->result = cpu_to_le16(result);
  247. if (len)
  248. memcpy(params->data, data, len);
  249. l2cap->signalling_ch.params.sdu_submit(&l2cap->signalling_ch.params);
  250. }
  251. static void l2cap_disconnection_response(struct l2cap_instance_s *l2cap,
  252. int dcid, int scid)
  253. {
  254. uint8_t *pkt;
  255. l2cap_cmd_hdr *hdr;
  256. l2cap_disconn_rsp *params;
  257. pkt = l2cap->signalling_ch.params.sdu_out(&l2cap->signalling_ch.params,
  258. L2CAP_CMD_HDR_SIZE + L2CAP_DISCONN_RSP_SIZE);
  259. hdr = (void *) (pkt + 0);
  260. params = (void *) (pkt + L2CAP_CMD_HDR_SIZE);
  261. hdr->code = L2CAP_DISCONN_RSP;
  262. hdr->ident = l2cap->last_id;
  263. hdr->len = cpu_to_le16(L2CAP_DISCONN_RSP_SIZE);
  264. params->dcid = cpu_to_le16(dcid);
  265. params->scid = cpu_to_le16(scid);
  266. l2cap->signalling_ch.params.sdu_submit(&l2cap->signalling_ch.params);
  267. }
  268. static void l2cap_echo_response(struct l2cap_instance_s *l2cap,
  269. const uint8_t *data, int len)
  270. {
  271. uint8_t *pkt;
  272. l2cap_cmd_hdr *hdr;
  273. uint8_t *params;
  274. pkt = l2cap->signalling_ch.params.sdu_out(&l2cap->signalling_ch.params,
  275. L2CAP_CMD_HDR_SIZE + len);
  276. hdr = (void *) (pkt + 0);
  277. params = (void *) (pkt + L2CAP_CMD_HDR_SIZE);
  278. hdr->code = L2CAP_ECHO_RSP;
  279. hdr->ident = l2cap->last_id;
  280. hdr->len = cpu_to_le16(len);
  281. memcpy(params, data, len);
  282. l2cap->signalling_ch.params.sdu_submit(&l2cap->signalling_ch.params);
  283. }
  284. static void l2cap_info_response(struct l2cap_instance_s *l2cap, int type,
  285. int result, const uint8_t *data, int len)
  286. {
  287. uint8_t *pkt;
  288. l2cap_cmd_hdr *hdr;
  289. l2cap_info_rsp *params;
  290. pkt = l2cap->signalling_ch.params.sdu_out(&l2cap->signalling_ch.params,
  291. L2CAP_CMD_HDR_SIZE + L2CAP_INFO_RSP_SIZE + len);
  292. hdr = (void *) (pkt + 0);
  293. params = (void *) (pkt + L2CAP_CMD_HDR_SIZE);
  294. hdr->code = L2CAP_INFO_RSP;
  295. hdr->ident = l2cap->last_id;
  296. hdr->len = cpu_to_le16(L2CAP_INFO_RSP_SIZE + len);
  297. params->type = cpu_to_le16(type);
  298. params->result = cpu_to_le16(result);
  299. if (len)
  300. memcpy(params->data, data, len);
  301. l2cap->signalling_ch.params.sdu_submit(&l2cap->signalling_ch.params);
  302. }
  303. static uint8_t *l2cap_bframe_out(struct bt_l2cap_conn_params_s *parm, int len);
  304. static void l2cap_bframe_submit(struct bt_l2cap_conn_params_s *parms);
  305. #if 0
  306. static uint8_t *l2cap_iframe_out(struct bt_l2cap_conn_params_s *parm, int len);
  307. static void l2cap_iframe_submit(struct bt_l2cap_conn_params_s *parm);
  308. #endif
  309. static void l2cap_bframe_in(struct l2cap_chan_s *ch, uint16_t cid,
  310. const l2cap_hdr *hdr, int len);
  311. static void l2cap_iframe_in(struct l2cap_chan_s *ch, uint16_t cid,
  312. const l2cap_hdr *hdr, int len);
  313. static int l2cap_cid_new(struct l2cap_instance_s *l2cap)
  314. {
  315. int i;
  316. for (i = L2CAP_CID_ALLOC; i < L2CAP_CID_MAX; i ++)
  317. if (!l2cap->cid[i])
  318. return i;
  319. return L2CAP_CID_INVALID;
  320. }
  321. static inline struct bt_l2cap_psm_s *l2cap_psm(
  322. struct bt_l2cap_device_s *device, int psm)
  323. {
  324. struct bt_l2cap_psm_s *ret = device->first_psm;
  325. while (ret && ret->psm != psm)
  326. ret = ret->next;
  327. return ret;
  328. }
  329. static struct l2cap_chan_s *l2cap_channel_open(struct l2cap_instance_s *l2cap,
  330. int psm, int source_cid)
  331. {
  332. struct l2cap_chan_s *ch = NULL;
  333. struct bt_l2cap_psm_s *psm_info;
  334. int result, status;
  335. int cid = l2cap_cid_new(l2cap);
  336. if (cid) {
  337. /* See what the channel is to be used for.. */
  338. psm_info = l2cap_psm(l2cap->dev, psm);
  339. if (psm_info) {
  340. /* Device supports this use-case. */
  341. ch = qemu_mallocz(sizeof(*ch));
  342. ch->params.sdu_out = l2cap_bframe_out;
  343. ch->params.sdu_submit = l2cap_bframe_submit;
  344. ch->frame_in = l2cap_bframe_in;
  345. ch->mps = 65536;
  346. ch->min_mtu = MAX(48, psm_info->min_mtu);
  347. ch->params.remote_mtu = MAX(672, ch->min_mtu);
  348. ch->remote_cid = source_cid;
  349. ch->mode = L2CAP_MODE_BASIC;
  350. ch->l2cap = l2cap;
  351. /* Does it feel like opening yet another channel though? */
  352. if (!psm_info->new_channel(l2cap->dev, &ch->params)) {
  353. l2cap->cid[cid] = ch;
  354. result = L2CAP_CR_SUCCESS;
  355. status = L2CAP_CS_NO_INFO;
  356. } else {
  357. qemu_free(ch);
  358. result = L2CAP_CR_NO_MEM;
  359. status = L2CAP_CS_NO_INFO;
  360. }
  361. } else {
  362. result = L2CAP_CR_BAD_PSM;
  363. status = L2CAP_CS_NO_INFO;
  364. }
  365. } else {
  366. result = L2CAP_CR_NO_MEM;
  367. status = L2CAP_CS_NO_INFO;
  368. }
  369. l2cap_connection_response(l2cap, cid, source_cid, result, status);
  370. return ch;
  371. }
  372. static void l2cap_channel_close(struct l2cap_instance_s *l2cap,
  373. int cid, int source_cid)
  374. {
  375. struct l2cap_chan_s *ch = NULL;
  376. /* According to Volume 3, section 6.1.1, pg 1048 of BT Core V2.0, a
  377. * connection in CLOSED state still responds with a L2CAP_DisconnectRsp
  378. * message on an L2CAP_DisconnectReq event. */
  379. if (unlikely(cid < L2CAP_CID_ALLOC)) {
  380. l2cap_command_reject_cid(l2cap, l2cap->last_id, L2CAP_REJ_CID_INVAL,
  381. cid, source_cid);
  382. return;
  383. }
  384. if (likely(cid >= L2CAP_CID_ALLOC && cid < L2CAP_CID_MAX))
  385. ch = l2cap->cid[cid];
  386. if (likely(ch)) {
  387. if (ch->remote_cid != source_cid) {
  388. fprintf(stderr, "%s: Ignoring a Disconnection Request with the "
  389. "invalid SCID %04x.\n", __FUNCTION__, source_cid);
  390. return;
  391. }
  392. l2cap->cid[cid] = NULL;
  393. ch->params.close(ch->params.opaque);
  394. qemu_free(ch);
  395. }
  396. l2cap_disconnection_response(l2cap, cid, source_cid);
  397. }
  398. static void l2cap_channel_config_null(struct l2cap_instance_s *l2cap,
  399. struct l2cap_chan_s *ch)
  400. {
  401. l2cap_configuration_request(l2cap, ch->remote_cid, 0, NULL, 0);
  402. ch->config_req_id = l2cap->last_id;
  403. ch->config &= ~L2CAP_CFG_INIT;
  404. }
  405. static void l2cap_channel_config_req_event(struct l2cap_instance_s *l2cap,
  406. struct l2cap_chan_s *ch)
  407. {
  408. /* Use all default channel options and terminate negotiation. */
  409. l2cap_channel_config_null(l2cap, ch);
  410. }
  411. static int l2cap_channel_config(struct l2cap_instance_s *l2cap,
  412. struct l2cap_chan_s *ch, int flag,
  413. const uint8_t *data, int len)
  414. {
  415. l2cap_conf_opt *opt;
  416. l2cap_conf_opt_qos *qos;
  417. uint32_t val;
  418. uint8_t rsp[len];
  419. int result = L2CAP_CONF_SUCCESS;
  420. data = memcpy(rsp, data, len);
  421. while (len) {
  422. opt = (void *) data;
  423. if (len < L2CAP_CONF_OPT_SIZE ||
  424. len < L2CAP_CONF_OPT_SIZE + opt->len) {
  425. result = L2CAP_CONF_REJECT;
  426. break;
  427. }
  428. data += L2CAP_CONF_OPT_SIZE + opt->len;
  429. len -= L2CAP_CONF_OPT_SIZE + opt->len;
  430. switch (opt->type & 0x7f) {
  431. case L2CAP_CONF_MTU:
  432. if (opt->len != 2) {
  433. result = L2CAP_CONF_REJECT;
  434. break;
  435. }
  436. /* MTU */
  437. val = le16_to_cpup((void *) opt->val);
  438. if (val < ch->min_mtu) {
  439. cpu_to_le16w((void *) opt->val, ch->min_mtu);
  440. result = L2CAP_CONF_UNACCEPT;
  441. break;
  442. }
  443. ch->params.remote_mtu = val;
  444. break;
  445. case L2CAP_CONF_FLUSH_TO:
  446. if (opt->len != 2) {
  447. result = L2CAP_CONF_REJECT;
  448. break;
  449. }
  450. /* Flush Timeout */
  451. val = le16_to_cpup((void *) opt->val);
  452. if (val < 0x0001) {
  453. opt->val[0] = 0xff;
  454. opt->val[1] = 0xff;
  455. result = L2CAP_CONF_UNACCEPT;
  456. break;
  457. }
  458. break;
  459. case L2CAP_CONF_QOS:
  460. if (opt->len != L2CAP_CONF_OPT_QOS_SIZE) {
  461. result = L2CAP_CONF_REJECT;
  462. break;
  463. }
  464. qos = (void *) opt->val;
  465. /* Flags */
  466. val = qos->flags;
  467. if (val) {
  468. qos->flags = 0;
  469. result = L2CAP_CONF_UNACCEPT;
  470. }
  471. /* Service type */
  472. val = qos->service_type;
  473. if (val != L2CAP_CONF_QOS_BEST_EFFORT &&
  474. val != L2CAP_CONF_QOS_NO_TRAFFIC) {
  475. qos->service_type = L2CAP_CONF_QOS_BEST_EFFORT;
  476. result = L2CAP_CONF_UNACCEPT;
  477. }
  478. if (val != L2CAP_CONF_QOS_NO_TRAFFIC) {
  479. /* XXX: These values should possibly be calculated
  480. * based on LM / baseband properties also. */
  481. /* Token rate */
  482. val = le32_to_cpu(qos->token_rate);
  483. if (val == L2CAP_CONF_QOS_WILDCARD)
  484. qos->token_rate = cpu_to_le32(0x100000);
  485. /* Token bucket size */
  486. val = le32_to_cpu(qos->token_bucket_size);
  487. if (val == L2CAP_CONF_QOS_WILDCARD)
  488. qos->token_bucket_size = cpu_to_le32(65500);
  489. /* Any Peak bandwidth value is correct to return as-is */
  490. /* Any Access latency value is correct to return as-is */
  491. /* Any Delay variation value is correct to return as-is */
  492. }
  493. break;
  494. case L2CAP_CONF_RFC:
  495. if (opt->len != 9) {
  496. result = L2CAP_CONF_REJECT;
  497. break;
  498. }
  499. /* Mode */
  500. val = opt->val[0];
  501. switch (val) {
  502. case L2CAP_MODE_BASIC:
  503. ch->mode = val;
  504. ch->frame_in = l2cap_bframe_in;
  505. /* All other parameters shall be ignored */
  506. break;
  507. case L2CAP_MODE_RETRANS:
  508. case L2CAP_MODE_FLOWCTL:
  509. ch->mode = val;
  510. ch->frame_in = l2cap_iframe_in;
  511. /* Note: most of these parameters refer to incoming traffic
  512. * so we don't need to save them as long as we can accept
  513. * incoming PDUs at any values of the parameters. */
  514. /* TxWindow size */
  515. val = opt->val[1];
  516. if (val < 1 || val > 32) {
  517. opt->val[1] = 32;
  518. result = L2CAP_CONF_UNACCEPT;
  519. break;
  520. }
  521. /* MaxTransmit */
  522. val = opt->val[2];
  523. if (val < 1) {
  524. opt->val[2] = 1;
  525. result = L2CAP_CONF_UNACCEPT;
  526. break;
  527. }
  528. /* Remote Retransmission time-out shouldn't affect local
  529. * operation (?) */
  530. /* The Monitor time-out drives the local Monitor timer (?),
  531. * so save the value. */
  532. val = (opt->val[6] << 8) | opt->val[5];
  533. if (val < 30) {
  534. opt->val[5] = 100 & 0xff;
  535. opt->val[6] = 100 >> 8;
  536. result = L2CAP_CONF_UNACCEPT;
  537. break;
  538. }
  539. ch->monitor_timeout = val;
  540. l2cap_monitor_timer_update(ch);
  541. /* MPS */
  542. val = (opt->val[8] << 8) | opt->val[7];
  543. if (val < ch->min_mtu) {
  544. opt->val[7] = ch->min_mtu & 0xff;
  545. opt->val[8] = ch->min_mtu >> 8;
  546. result = L2CAP_CONF_UNACCEPT;
  547. break;
  548. }
  549. ch->mps = val;
  550. break;
  551. default:
  552. result = L2CAP_CONF_UNACCEPT;
  553. break;
  554. }
  555. break;
  556. default:
  557. if (!(opt->type >> 7))
  558. result = L2CAP_CONF_UNKNOWN;
  559. break;
  560. }
  561. if (result != L2CAP_CONF_SUCCESS)
  562. break; /* XXX: should continue? */
  563. }
  564. l2cap_configuration_response(l2cap, ch->remote_cid,
  565. flag, result, rsp, len);
  566. return result == L2CAP_CONF_SUCCESS && !flag;
  567. }
  568. static void l2cap_channel_config_req_msg(struct l2cap_instance_s *l2cap,
  569. int flag, int cid, const uint8_t *data, int len)
  570. {
  571. struct l2cap_chan_s *ch;
  572. if (unlikely(cid >= L2CAP_CID_MAX || !l2cap->cid[cid])) {
  573. l2cap_command_reject_cid(l2cap, l2cap->last_id, L2CAP_REJ_CID_INVAL,
  574. cid, 0x0000);
  575. return;
  576. }
  577. ch = l2cap->cid[cid];
  578. /* From OPEN go to WAIT_CONFIG_REQ and from WAIT_CONFIG_REQ_RSP to
  579. * WAIT_CONFIG_REQ_RSP. This is assuming the transition chart for OPEN
  580. * on pg 1053, section 6.1.5, volume 3 of BT Core V2.0 has a mistake
  581. * and on options-acceptable we go back to OPEN and otherwise to
  582. * WAIT_CONFIG_REQ and not the other way. */
  583. ch->config &= ~L2CAP_CFG_ACC;
  584. if (l2cap_channel_config(l2cap, ch, flag, data, len))
  585. /* Go to OPEN or WAIT_CONFIG_RSP */
  586. ch->config |= L2CAP_CFG_ACC;
  587. /* TODO: if the incoming traffic flow control or retransmission mode
  588. * changed then we probably need to also generate the
  589. * ConfigureChannel_Req event and set the outgoing traffic to the same
  590. * mode. */
  591. if (!(ch->config & L2CAP_CFG_INIT) && (ch->config & L2CAP_CFG_ACC) &&
  592. !ch->config_req_id)
  593. l2cap_channel_config_req_event(l2cap, ch);
  594. }
  595. static int l2cap_channel_config_rsp_msg(struct l2cap_instance_s *l2cap,
  596. int result, int flag, int cid, const uint8_t *data, int len)
  597. {
  598. struct l2cap_chan_s *ch;
  599. if (unlikely(cid >= L2CAP_CID_MAX || !l2cap->cid[cid])) {
  600. l2cap_command_reject_cid(l2cap, l2cap->last_id, L2CAP_REJ_CID_INVAL,
  601. cid, 0x0000);
  602. return 0;
  603. }
  604. ch = l2cap->cid[cid];
  605. if (ch->config_req_id != l2cap->last_id)
  606. return 1;
  607. ch->config_req_id = 0;
  608. if (result == L2CAP_CONF_SUCCESS) {
  609. if (!flag)
  610. ch->config |= L2CAP_CFG_INIT;
  611. else
  612. l2cap_channel_config_null(l2cap, ch);
  613. } else
  614. /* Retry until we succeed */
  615. l2cap_channel_config_req_event(l2cap, ch);
  616. return 0;
  617. }
  618. static void l2cap_channel_open_req_msg(struct l2cap_instance_s *l2cap,
  619. int psm, int source_cid)
  620. {
  621. struct l2cap_chan_s *ch = l2cap_channel_open(l2cap, psm, source_cid);
  622. if (!ch)
  623. return;
  624. /* Optional */
  625. if (!(ch->config & L2CAP_CFG_INIT) && !ch->config_req_id)
  626. l2cap_channel_config_req_event(l2cap, ch);
  627. }
  628. static void l2cap_info(struct l2cap_instance_s *l2cap, int type)
  629. {
  630. uint8_t data[4];
  631. int len = 0;
  632. int result = L2CAP_IR_SUCCESS;
  633. switch (type) {
  634. case L2CAP_IT_CL_MTU:
  635. data[len ++] = l2cap->group_ch.mps & 0xff;
  636. data[len ++] = l2cap->group_ch.mps >> 8;
  637. break;
  638. case L2CAP_IT_FEAT_MASK:
  639. /* (Prematurely) report Flow control and Retransmission modes. */
  640. data[len ++] = 0x03;
  641. data[len ++] = 0x00;
  642. data[len ++] = 0x00;
  643. data[len ++] = 0x00;
  644. break;
  645. default:
  646. result = L2CAP_IR_NOTSUPP;
  647. }
  648. l2cap_info_response(l2cap, type, result, data, len);
  649. }
  650. static void l2cap_command(struct l2cap_instance_s *l2cap, int code, int id,
  651. const uint8_t *params, int len)
  652. {
  653. int err;
  654. #if 0
  655. /* TODO: do the IDs really have to be in sequence? */
  656. if (!id || (id != l2cap->last_id && id != l2cap->next_id)) {
  657. fprintf(stderr, "%s: out of sequence command packet ignored.\n",
  658. __FUNCTION__);
  659. return;
  660. }
  661. #else
  662. l2cap->next_id = id;
  663. #endif
  664. if (id == l2cap->next_id) {
  665. l2cap->last_id = l2cap->next_id;
  666. l2cap->next_id = l2cap->next_id == 255 ? 1 : l2cap->next_id + 1;
  667. } else {
  668. /* TODO: Need to re-send the same response, without re-executing
  669. * the corresponding command! */
  670. }
  671. switch (code) {
  672. case L2CAP_COMMAND_REJ:
  673. if (unlikely(len != 2 && len != 4 && len != 6)) {
  674. err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
  675. goto reject;
  676. }
  677. /* We never issue commands other than Command Reject currently. */
  678. fprintf(stderr, "%s: stray Command Reject (%02x, %04x) "
  679. "packet, ignoring.\n", __FUNCTION__, id,
  680. le16_to_cpu(((l2cap_cmd_rej *) params)->reason));
  681. break;
  682. case L2CAP_CONN_REQ:
  683. if (unlikely(len != L2CAP_CONN_REQ_SIZE)) {
  684. err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
  685. goto reject;
  686. }
  687. l2cap_channel_open_req_msg(l2cap,
  688. le16_to_cpu(((l2cap_conn_req *) params)->psm),
  689. le16_to_cpu(((l2cap_conn_req *) params)->scid));
  690. break;
  691. case L2CAP_CONN_RSP:
  692. if (unlikely(len != L2CAP_CONN_RSP_SIZE)) {
  693. err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
  694. goto reject;
  695. }
  696. /* We never issue Connection Requests currently. TODO */
  697. fprintf(stderr, "%s: unexpected Connection Response (%02x) "
  698. "packet, ignoring.\n", __FUNCTION__, id);
  699. break;
  700. case L2CAP_CONF_REQ:
  701. if (unlikely(len < L2CAP_CONF_REQ_SIZE(0))) {
  702. err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
  703. goto reject;
  704. }
  705. l2cap_channel_config_req_msg(l2cap,
  706. le16_to_cpu(((l2cap_conf_req *) params)->flags) & 1,
  707. le16_to_cpu(((l2cap_conf_req *) params)->dcid),
  708. ((l2cap_conf_req *) params)->data,
  709. len - L2CAP_CONF_REQ_SIZE(0));
  710. break;
  711. case L2CAP_CONF_RSP:
  712. if (unlikely(len < L2CAP_CONF_RSP_SIZE(0))) {
  713. err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
  714. goto reject;
  715. }
  716. if (l2cap_channel_config_rsp_msg(l2cap,
  717. le16_to_cpu(((l2cap_conf_rsp *) params)->result),
  718. le16_to_cpu(((l2cap_conf_rsp *) params)->flags) & 1,
  719. le16_to_cpu(((l2cap_conf_rsp *) params)->scid),
  720. ((l2cap_conf_rsp *) params)->data,
  721. len - L2CAP_CONF_RSP_SIZE(0)))
  722. fprintf(stderr, "%s: unexpected Configure Response (%02x) "
  723. "packet, ignoring.\n", __FUNCTION__, id);
  724. break;
  725. case L2CAP_DISCONN_REQ:
  726. if (unlikely(len != L2CAP_DISCONN_REQ_SIZE)) {
  727. err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
  728. goto reject;
  729. }
  730. l2cap_channel_close(l2cap,
  731. le16_to_cpu(((l2cap_disconn_req *) params)->dcid),
  732. le16_to_cpu(((l2cap_disconn_req *) params)->scid));
  733. break;
  734. case L2CAP_DISCONN_RSP:
  735. if (unlikely(len != L2CAP_DISCONN_RSP_SIZE)) {
  736. err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
  737. goto reject;
  738. }
  739. /* We never issue Disconnection Requests currently. TODO */
  740. fprintf(stderr, "%s: unexpected Disconnection Response (%02x) "
  741. "packet, ignoring.\n", __FUNCTION__, id);
  742. break;
  743. case L2CAP_ECHO_REQ:
  744. l2cap_echo_response(l2cap, params, len);
  745. break;
  746. case L2CAP_ECHO_RSP:
  747. /* We never issue Echo Requests currently. TODO */
  748. fprintf(stderr, "%s: unexpected Echo Response (%02x) "
  749. "packet, ignoring.\n", __FUNCTION__, id);
  750. break;
  751. case L2CAP_INFO_REQ:
  752. if (unlikely(len != L2CAP_INFO_REQ_SIZE)) {
  753. err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
  754. goto reject;
  755. }
  756. l2cap_info(l2cap, le16_to_cpu(((l2cap_info_req *) params)->type));
  757. break;
  758. case L2CAP_INFO_RSP:
  759. if (unlikely(len != L2CAP_INFO_RSP_SIZE)) {
  760. err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
  761. goto reject;
  762. }
  763. /* We never issue Information Requests currently. TODO */
  764. fprintf(stderr, "%s: unexpected Information Response (%02x) "
  765. "packet, ignoring.\n", __FUNCTION__, id);
  766. break;
  767. default:
  768. err = L2CAP_REJ_CMD_NOT_UNDERSTOOD;
  769. reject:
  770. l2cap_command_reject(l2cap, id, err, 0, 0);
  771. break;
  772. }
  773. }
  774. static void l2cap_rexmit_enable(struct l2cap_chan_s *ch, int enable)
  775. {
  776. ch->rexmit = enable;
  777. l2cap_retransmission_timer_update(ch);
  778. l2cap_monitor_timer_update(ch);
  779. }
  780. /* Command frame SDU */
  781. static void l2cap_cframe_in(void *opaque, const uint8_t *data, int len)
  782. {
  783. struct l2cap_instance_s *l2cap = opaque;
  784. const l2cap_cmd_hdr *hdr;
  785. int clen;
  786. while (len) {
  787. hdr = (void *) data;
  788. if (len < L2CAP_CMD_HDR_SIZE)
  789. /* TODO: signal an error */
  790. return;
  791. len -= L2CAP_CMD_HDR_SIZE;
  792. data += L2CAP_CMD_HDR_SIZE;
  793. clen = le16_to_cpu(hdr->len);
  794. if (len < clen) {
  795. l2cap_command_reject(l2cap, hdr->ident,
  796. L2CAP_REJ_CMD_NOT_UNDERSTOOD, 0, 0);
  797. break;
  798. }
  799. l2cap_command(l2cap, hdr->code, hdr->ident, data, clen);
  800. len -= clen;
  801. data += clen;
  802. }
  803. }
  804. /* Group frame SDU */
  805. static void l2cap_gframe_in(void *opaque, const uint8_t *data, int len)
  806. {
  807. }
  808. /* Supervisory frame */
  809. static void l2cap_sframe_in(struct l2cap_chan_s *ch, uint16_t ctrl)
  810. {
  811. }
  812. /* Basic L2CAP mode Information frame */
  813. static void l2cap_bframe_in(struct l2cap_chan_s *ch, uint16_t cid,
  814. const l2cap_hdr *hdr, int len)
  815. {
  816. /* We have a full SDU, no further processing */
  817. ch->params.sdu_in(ch->params.opaque, hdr->data, len);
  818. }
  819. /* Flow Control and Retransmission mode frame */
  820. static void l2cap_iframe_in(struct l2cap_chan_s *ch, uint16_t cid,
  821. const l2cap_hdr *hdr, int len)
  822. {
  823. uint16_t fcs = le16_to_cpup((void *) (hdr->data + len - 2));
  824. if (len < 4)
  825. goto len_error;
  826. if (l2cap_fcs16((const uint8_t *) hdr, L2CAP_HDR_SIZE + len - 2) != fcs)
  827. goto fcs_error;
  828. if ((hdr->data[0] >> 7) == ch->rexmit)
  829. l2cap_rexmit_enable(ch, !(hdr->data[0] >> 7));
  830. if (hdr->data[0] & 1) {
  831. if (len != 4) {
  832. /* TODO: Signal an error? */
  833. return;
  834. }
  835. return l2cap_sframe_in(ch, le16_to_cpup((void *) hdr->data));
  836. }
  837. switch (hdr->data[1] >> 6) { /* SAR */
  838. case L2CAP_SAR_NO_SEG:
  839. if (ch->len_total)
  840. goto seg_error;
  841. if (len - 4 > ch->mps)
  842. goto len_error;
  843. return ch->params.sdu_in(ch->params.opaque, hdr->data + 2, len - 4);
  844. case L2CAP_SAR_START:
  845. if (ch->len_total || len < 6)
  846. goto seg_error;
  847. if (len - 6 > ch->mps)
  848. goto len_error;
  849. ch->len_total = le16_to_cpup((void *) (hdr->data + 2));
  850. if (len >= 6 + ch->len_total)
  851. goto seg_error;
  852. ch->len_cur = len - 6;
  853. memcpy(ch->sdu, hdr->data + 4, ch->len_cur);
  854. break;
  855. case L2CAP_SAR_END:
  856. if (!ch->len_total || ch->len_cur + len - 4 < ch->len_total)
  857. goto seg_error;
  858. if (len - 4 > ch->mps)
  859. goto len_error;
  860. memcpy(ch->sdu + ch->len_cur, hdr->data + 2, len - 4);
  861. return ch->params.sdu_in(ch->params.opaque, ch->sdu, ch->len_total);
  862. case L2CAP_SAR_CONT:
  863. if (!ch->len_total || ch->len_cur + len - 4 >= ch->len_total)
  864. goto seg_error;
  865. if (len - 4 > ch->mps)
  866. goto len_error;
  867. memcpy(ch->sdu + ch->len_cur, hdr->data + 2, len - 4);
  868. ch->len_cur += len - 4;
  869. break;
  870. seg_error:
  871. len_error: /* TODO */
  872. fcs_error: /* TODO */
  873. ch->len_cur = 0;
  874. ch->len_total = 0;
  875. break;
  876. }
  877. }
  878. static void l2cap_frame_in(struct l2cap_instance_s *l2cap,
  879. const l2cap_hdr *frame)
  880. {
  881. uint16_t cid = le16_to_cpu(frame->cid);
  882. uint16_t len = le16_to_cpu(frame->len);
  883. if (unlikely(cid >= L2CAP_CID_MAX || !l2cap->cid[cid])) {
  884. fprintf(stderr, "%s: frame addressed to a non-existent L2CAP "
  885. "channel %04x received.\n", __FUNCTION__, cid);
  886. return;
  887. }
  888. l2cap->cid[cid]->frame_in(l2cap->cid[cid], cid, frame, len);
  889. }
  890. /* "Recombination" */
  891. static void l2cap_pdu_in(struct l2cap_instance_s *l2cap,
  892. const uint8_t *data, int len)
  893. {
  894. const l2cap_hdr *hdr = (void *) l2cap->frame_in;
  895. if (unlikely(len + l2cap->frame_in_len > sizeof(l2cap->frame_in))) {
  896. if (l2cap->frame_in_len < sizeof(l2cap->frame_in)) {
  897. memcpy(l2cap->frame_in + l2cap->frame_in_len, data,
  898. sizeof(l2cap->frame_in) - l2cap->frame_in_len);
  899. l2cap->frame_in_len = sizeof(l2cap->frame_in);
  900. /* TODO: truncate */
  901. l2cap_frame_in(l2cap, hdr);
  902. }
  903. return;
  904. }
  905. memcpy(l2cap->frame_in + l2cap->frame_in_len, data, len);
  906. l2cap->frame_in_len += len;
  907. if (len >= L2CAP_HDR_SIZE)
  908. if (len >= L2CAP_HDR_SIZE + le16_to_cpu(hdr->len))
  909. l2cap_frame_in(l2cap, hdr);
  910. /* There is never a start of a new PDU in the same ACL packet, so
  911. * no need to memmove the remaining payload and loop. */
  912. }
  913. static inline uint8_t *l2cap_pdu_out(struct l2cap_instance_s *l2cap,
  914. uint16_t cid, uint16_t len)
  915. {
  916. l2cap_hdr *hdr = (void *) l2cap->frame_out;
  917. l2cap->frame_out_len = len + L2CAP_HDR_SIZE;
  918. hdr->cid = cpu_to_le16(cid);
  919. hdr->len = cpu_to_le16(len);
  920. return l2cap->frame_out + L2CAP_HDR_SIZE;
  921. }
  922. static inline void l2cap_pdu_submit(struct l2cap_instance_s *l2cap)
  923. {
  924. /* TODO: Fragmentation */
  925. (l2cap->role ?
  926. l2cap->link->slave->lmp_acl_data : l2cap->link->host->lmp_acl_resp)
  927. (l2cap->link, l2cap->frame_out, 1, l2cap->frame_out_len);
  928. }
  929. static uint8_t *l2cap_bframe_out(struct bt_l2cap_conn_params_s *parm, int len)
  930. {
  931. struct l2cap_chan_s *chan = (struct l2cap_chan_s *) parm;
  932. if (len > chan->params.remote_mtu) {
  933. fprintf(stderr, "%s: B-Frame for CID %04x longer than %i octets.\n",
  934. __FUNCTION__,
  935. chan->remote_cid, chan->params.remote_mtu);
  936. exit(-1);
  937. }
  938. return l2cap_pdu_out(chan->l2cap, chan->remote_cid, len);
  939. }
  940. static void l2cap_bframe_submit(struct bt_l2cap_conn_params_s *parms)
  941. {
  942. struct l2cap_chan_s *chan = (struct l2cap_chan_s *) parms;
  943. return l2cap_pdu_submit(chan->l2cap);
  944. }
  945. #if 0
  946. /* Stub: Only used if an emulated device requests outgoing flow control */
  947. static uint8_t *l2cap_iframe_out(struct bt_l2cap_conn_params_s *parm, int len)
  948. {
  949. struct l2cap_chan_s *chan = (struct l2cap_chan_s *) parm;
  950. if (len > chan->params.remote_mtu) {
  951. /* TODO: slice into segments and queue each segment as a separate
  952. * I-Frame in a FIFO of I-Frames, local to the CID. */
  953. } else {
  954. /* TODO: add to the FIFO of I-Frames, local to the CID. */
  955. /* Possibly we need to return a pointer to a contiguous buffer
  956. * for now and then memcpy from it into FIFOs in l2cap_iframe_submit
  957. * while segmenting at the same time. */
  958. }
  959. return 0;
  960. }
  961. static void l2cap_iframe_submit(struct bt_l2cap_conn_params_s *parm)
  962. {
  963. /* TODO: If flow control indicates clear to send, start submitting the
  964. * invidual I-Frames from the FIFO, but don't remove them from there.
  965. * Kick the appropriate timer until we get an S-Frame, and only then
  966. * remove from FIFO or resubmit and re-kick the timer if the timer
  967. * expired. */
  968. }
  969. #endif
  970. static void l2cap_init(struct l2cap_instance_s *l2cap,
  971. struct bt_link_s *link, int role)
  972. {
  973. l2cap->link = link;
  974. l2cap->role = role;
  975. l2cap->dev = (struct bt_l2cap_device_s *)
  976. (role ? link->host : link->slave);
  977. l2cap->next_id = 1;
  978. /* Establish the signalling channel */
  979. l2cap->signalling_ch.params.sdu_in = l2cap_cframe_in;
  980. l2cap->signalling_ch.params.sdu_out = l2cap_bframe_out;
  981. l2cap->signalling_ch.params.sdu_submit = l2cap_bframe_submit;
  982. l2cap->signalling_ch.params.opaque = l2cap;
  983. l2cap->signalling_ch.params.remote_mtu = 48;
  984. l2cap->signalling_ch.remote_cid = L2CAP_CID_SIGNALLING;
  985. l2cap->signalling_ch.frame_in = l2cap_bframe_in;
  986. l2cap->signalling_ch.mps = 65536;
  987. l2cap->signalling_ch.min_mtu = 48;
  988. l2cap->signalling_ch.mode = L2CAP_MODE_BASIC;
  989. l2cap->signalling_ch.l2cap = l2cap;
  990. l2cap->cid[L2CAP_CID_SIGNALLING] = &l2cap->signalling_ch;
  991. /* Establish the connection-less data channel */
  992. l2cap->group_ch.params.sdu_in = l2cap_gframe_in;
  993. l2cap->group_ch.params.opaque = l2cap;
  994. l2cap->group_ch.frame_in = l2cap_bframe_in;
  995. l2cap->group_ch.mps = 65533;
  996. l2cap->group_ch.l2cap = l2cap;
  997. l2cap->group_ch.remote_cid = L2CAP_CID_INVALID;
  998. l2cap->cid[L2CAP_CID_GROUP] = &l2cap->group_ch;
  999. }
  1000. static void l2cap_teardown(struct l2cap_instance_s *l2cap, int send_disconnect)
  1001. {
  1002. int cid;
  1003. /* Don't send DISCONNECT if we are currently handling a DISCONNECT
  1004. * sent from the other side. */
  1005. if (send_disconnect) {
  1006. if (l2cap->role)
  1007. l2cap->dev->device.lmp_disconnect_slave(l2cap->link);
  1008. /* l2cap->link is invalid from now on. */
  1009. else
  1010. l2cap->dev->device.lmp_disconnect_master(l2cap->link);
  1011. }
  1012. for (cid = L2CAP_CID_ALLOC; cid < L2CAP_CID_MAX; cid ++)
  1013. if (l2cap->cid[cid]) {
  1014. l2cap->cid[cid]->params.close(l2cap->cid[cid]->params.opaque);
  1015. qemu_free(l2cap->cid[cid]);
  1016. }
  1017. if (l2cap->role)
  1018. qemu_free(l2cap);
  1019. else
  1020. qemu_free(l2cap->link);
  1021. }
  1022. /* L2CAP glue to lower layers in bluetooth stack (LMP) */
  1023. static void l2cap_lmp_connection_request(struct bt_link_s *link)
  1024. {
  1025. struct bt_l2cap_device_s *dev = (struct bt_l2cap_device_s *) link->slave;
  1026. struct slave_l2cap_instance_s *l2cap;
  1027. /* Always accept - we only get called if (dev->device->page_scan). */
  1028. l2cap = qemu_mallocz(sizeof(struct slave_l2cap_instance_s));
  1029. l2cap->link.slave = &dev->device;
  1030. l2cap->link.host = link->host;
  1031. l2cap_init(&l2cap->l2cap, &l2cap->link, 0);
  1032. /* Always at the end */
  1033. link->host->reject_reason = 0;
  1034. link->host->lmp_connection_complete(&l2cap->link);
  1035. }
  1036. /* Stub */
  1037. static void l2cap_lmp_connection_complete(struct bt_link_s *link)
  1038. {
  1039. struct bt_l2cap_device_s *dev = (struct bt_l2cap_device_s *) link->host;
  1040. struct l2cap_instance_s *l2cap;
  1041. if (dev->device.reject_reason) {
  1042. /* Signal to upper layer */
  1043. return;
  1044. }
  1045. l2cap = qemu_mallocz(sizeof(struct l2cap_instance_s));
  1046. l2cap_init(l2cap, link, 1);
  1047. link->acl_mode = acl_active;
  1048. /* Signal to upper layer */
  1049. }
  1050. /* Stub */
  1051. static void l2cap_lmp_disconnect_host(struct bt_link_s *link)
  1052. {
  1053. struct bt_l2cap_device_s *dev = (struct bt_l2cap_device_s *) link->host;
  1054. struct l2cap_instance_s *l2cap =
  1055. /* TODO: Retrieve from upper layer */ (void *) dev;
  1056. /* Signal to upper layer */
  1057. l2cap_teardown(l2cap, 0);
  1058. }
  1059. static void l2cap_lmp_disconnect_slave(struct bt_link_s *link)
  1060. {
  1061. struct slave_l2cap_instance_s *l2cap =
  1062. (struct slave_l2cap_instance_s *) link;
  1063. l2cap_teardown(&l2cap->l2cap, 0);
  1064. }
  1065. static void l2cap_lmp_acl_data_slave(struct bt_link_s *link,
  1066. const uint8_t *data, int start, int len)
  1067. {
  1068. struct slave_l2cap_instance_s *l2cap =
  1069. (struct slave_l2cap_instance_s *) link;
  1070. if (start)
  1071. l2cap->l2cap.frame_in_len = 0;
  1072. l2cap_pdu_in(&l2cap->l2cap, data, len);
  1073. }
  1074. /* Stub */
  1075. static void l2cap_lmp_acl_data_host(struct bt_link_s *link,
  1076. const uint8_t *data, int start, int len)
  1077. {
  1078. struct bt_l2cap_device_s *dev = (struct bt_l2cap_device_s *) link->host;
  1079. struct l2cap_instance_s *l2cap =
  1080. /* TODO: Retrieve from upper layer */ (void *) dev;
  1081. if (start)
  1082. l2cap->frame_in_len = 0;
  1083. l2cap_pdu_in(l2cap, data, len);
  1084. }
  1085. static void l2cap_dummy_destroy(struct bt_device_s *dev)
  1086. {
  1087. struct bt_l2cap_device_s *l2cap_dev = (struct bt_l2cap_device_s *) dev;
  1088. bt_l2cap_device_done(l2cap_dev);
  1089. }
  1090. void bt_l2cap_device_init(struct bt_l2cap_device_s *dev,
  1091. struct bt_scatternet_s *net)
  1092. {
  1093. bt_device_init(&dev->device, net);
  1094. dev->device.lmp_connection_request = l2cap_lmp_connection_request;
  1095. dev->device.lmp_connection_complete = l2cap_lmp_connection_complete;
  1096. dev->device.lmp_disconnect_master = l2cap_lmp_disconnect_host;
  1097. dev->device.lmp_disconnect_slave = l2cap_lmp_disconnect_slave;
  1098. dev->device.lmp_acl_data = l2cap_lmp_acl_data_slave;
  1099. dev->device.lmp_acl_resp = l2cap_lmp_acl_data_host;
  1100. dev->device.handle_destroy = l2cap_dummy_destroy;
  1101. }
  1102. void bt_l2cap_device_done(struct bt_l2cap_device_s *dev)
  1103. {
  1104. bt_device_done(&dev->device);
  1105. /* Should keep a list of all instances and go through it and
  1106. * invoke l2cap_teardown() for each. */
  1107. }
  1108. void bt_l2cap_psm_register(struct bt_l2cap_device_s *dev, int psm, int min_mtu,
  1109. int (*new_channel)(struct bt_l2cap_device_s *dev,
  1110. struct bt_l2cap_conn_params_s *params))
  1111. {
  1112. struct bt_l2cap_psm_s *new_psm = l2cap_psm(dev, psm);
  1113. if (new_psm) {
  1114. fprintf(stderr, "%s: PSM %04x already registered for device `%s'.\n",
  1115. __FUNCTION__, psm, dev->device.lmp_name);
  1116. exit(-1);
  1117. }
  1118. new_psm = qemu_mallocz(sizeof(*new_psm));
  1119. new_psm->psm = psm;
  1120. new_psm->min_mtu = min_mtu;
  1121. new_psm->new_channel = new_channel;
  1122. new_psm->next = dev->first_psm;
  1123. dev->first_psm = new_psm;
  1124. }