bt-l2cap.c 43 KB

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