l2cap.c 43 KB

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