l2cap_sock.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (C) 2000-2001 Qualcomm Incorporated
  4. Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
  5. Copyright (C) 2010 Google Inc.
  6. Copyright (C) 2011 ProFUSION Embedded Systems
  7. Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License version 2 as
  10. published by the Free Software Foundation;
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  12. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  14. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  15. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  16. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  20. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  21. SOFTWARE IS DISCLAIMED.
  22. */
  23. /* Bluetooth L2CAP sockets. */
  24. #include <linux/module.h>
  25. #include <linux/export.h>
  26. #include <net/bluetooth/bluetooth.h>
  27. #include <net/bluetooth/hci_core.h>
  28. #include <net/bluetooth/l2cap.h>
  29. #include "smp.h"
  30. static struct bt_sock_list l2cap_sk_list = {
  31. .lock = __RW_LOCK_UNLOCKED(l2cap_sk_list.lock)
  32. };
  33. static const struct proto_ops l2cap_sock_ops;
  34. static void l2cap_sock_init(struct sock *sk, struct sock *parent);
  35. static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
  36. int proto, gfp_t prio, int kern);
  37. bool l2cap_is_socket(struct socket *sock)
  38. {
  39. return sock && sock->ops == &l2cap_sock_ops;
  40. }
  41. EXPORT_SYMBOL(l2cap_is_socket);
  42. static int l2cap_validate_bredr_psm(u16 psm)
  43. {
  44. /* PSM must be odd and lsb of upper byte must be 0 */
  45. if ((psm & 0x0101) != 0x0001)
  46. return -EINVAL;
  47. /* Restrict usage of well-known PSMs */
  48. if (psm < L2CAP_PSM_DYN_START && !capable(CAP_NET_BIND_SERVICE))
  49. return -EACCES;
  50. return 0;
  51. }
  52. static int l2cap_validate_le_psm(u16 psm)
  53. {
  54. /* Valid LE_PSM ranges are defined only until 0x00ff */
  55. if (psm > L2CAP_PSM_LE_DYN_END)
  56. return -EINVAL;
  57. /* Restrict fixed, SIG assigned PSM values to CAP_NET_BIND_SERVICE */
  58. if (psm < L2CAP_PSM_LE_DYN_START && !capable(CAP_NET_BIND_SERVICE))
  59. return -EACCES;
  60. return 0;
  61. }
  62. static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
  63. {
  64. struct sock *sk = sock->sk;
  65. struct l2cap_chan *chan = l2cap_pi(sk)->chan;
  66. struct sockaddr_l2 la;
  67. int len, err = 0;
  68. BT_DBG("sk %p", sk);
  69. if (!addr || addr->sa_family != AF_BLUETOOTH)
  70. return -EINVAL;
  71. memset(&la, 0, sizeof(la));
  72. len = min_t(unsigned int, sizeof(la), alen);
  73. memcpy(&la, addr, len);
  74. if (la.l2_cid && la.l2_psm)
  75. return -EINVAL;
  76. if (!bdaddr_type_is_valid(la.l2_bdaddr_type))
  77. return -EINVAL;
  78. if (bdaddr_type_is_le(la.l2_bdaddr_type)) {
  79. /* We only allow ATT user space socket */
  80. if (la.l2_cid &&
  81. la.l2_cid != cpu_to_le16(L2CAP_CID_ATT))
  82. return -EINVAL;
  83. }
  84. lock_sock(sk);
  85. if (sk->sk_state != BT_OPEN) {
  86. err = -EBADFD;
  87. goto done;
  88. }
  89. if (la.l2_psm) {
  90. __u16 psm = __le16_to_cpu(la.l2_psm);
  91. if (la.l2_bdaddr_type == BDADDR_BREDR)
  92. err = l2cap_validate_bredr_psm(psm);
  93. else
  94. err = l2cap_validate_le_psm(psm);
  95. if (err)
  96. goto done;
  97. }
  98. bacpy(&chan->src, &la.l2_bdaddr);
  99. chan->src_type = la.l2_bdaddr_type;
  100. if (la.l2_cid)
  101. err = l2cap_add_scid(chan, __le16_to_cpu(la.l2_cid));
  102. else
  103. err = l2cap_add_psm(chan, &la.l2_bdaddr, la.l2_psm);
  104. if (err < 0)
  105. goto done;
  106. switch (chan->chan_type) {
  107. case L2CAP_CHAN_CONN_LESS:
  108. if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_3DSP)
  109. chan->sec_level = BT_SECURITY_SDP;
  110. break;
  111. case L2CAP_CHAN_CONN_ORIENTED:
  112. if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_SDP ||
  113. __le16_to_cpu(la.l2_psm) == L2CAP_PSM_RFCOMM)
  114. chan->sec_level = BT_SECURITY_SDP;
  115. break;
  116. case L2CAP_CHAN_RAW:
  117. chan->sec_level = BT_SECURITY_SDP;
  118. break;
  119. case L2CAP_CHAN_FIXED:
  120. /* Fixed channels default to the L2CAP core not holding a
  121. * hci_conn reference for them. For fixed channels mapping to
  122. * L2CAP sockets we do want to hold a reference so set the
  123. * appropriate flag to request it.
  124. */
  125. set_bit(FLAG_HOLD_HCI_CONN, &chan->flags);
  126. break;
  127. }
  128. if (chan->psm && bdaddr_type_is_le(chan->src_type))
  129. chan->mode = L2CAP_MODE_LE_FLOWCTL;
  130. chan->state = BT_BOUND;
  131. sk->sk_state = BT_BOUND;
  132. done:
  133. release_sock(sk);
  134. return err;
  135. }
  136. static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr,
  137. int alen, int flags)
  138. {
  139. struct sock *sk = sock->sk;
  140. struct l2cap_chan *chan = l2cap_pi(sk)->chan;
  141. struct sockaddr_l2 la;
  142. int len, err = 0;
  143. BT_DBG("sk %p", sk);
  144. if (!addr || alen < sizeof(addr->sa_family) ||
  145. addr->sa_family != AF_BLUETOOTH)
  146. return -EINVAL;
  147. memset(&la, 0, sizeof(la));
  148. len = min_t(unsigned int, sizeof(la), alen);
  149. memcpy(&la, addr, len);
  150. if (la.l2_cid && la.l2_psm)
  151. return -EINVAL;
  152. if (!bdaddr_type_is_valid(la.l2_bdaddr_type))
  153. return -EINVAL;
  154. /* Check that the socket wasn't bound to something that
  155. * conflicts with the address given to connect(). If chan->src
  156. * is BDADDR_ANY it means bind() was never used, in which case
  157. * chan->src_type and la.l2_bdaddr_type do not need to match.
  158. */
  159. if (chan->src_type == BDADDR_BREDR && bacmp(&chan->src, BDADDR_ANY) &&
  160. bdaddr_type_is_le(la.l2_bdaddr_type)) {
  161. /* Old user space versions will try to incorrectly bind
  162. * the ATT socket using BDADDR_BREDR. We need to accept
  163. * this and fix up the source address type only when
  164. * both the source CID and destination CID indicate
  165. * ATT. Anything else is an invalid combination.
  166. */
  167. if (chan->scid != L2CAP_CID_ATT ||
  168. la.l2_cid != cpu_to_le16(L2CAP_CID_ATT))
  169. return -EINVAL;
  170. /* We don't have the hdev available here to make a
  171. * better decision on random vs public, but since all
  172. * user space versions that exhibit this issue anyway do
  173. * not support random local addresses assuming public
  174. * here is good enough.
  175. */
  176. chan->src_type = BDADDR_LE_PUBLIC;
  177. }
  178. if (chan->src_type != BDADDR_BREDR && la.l2_bdaddr_type == BDADDR_BREDR)
  179. return -EINVAL;
  180. if (bdaddr_type_is_le(la.l2_bdaddr_type)) {
  181. /* We only allow ATT user space socket */
  182. if (la.l2_cid &&
  183. la.l2_cid != cpu_to_le16(L2CAP_CID_ATT))
  184. return -EINVAL;
  185. }
  186. if (chan->psm && bdaddr_type_is_le(chan->src_type))
  187. chan->mode = L2CAP_MODE_LE_FLOWCTL;
  188. err = l2cap_chan_connect(chan, la.l2_psm, __le16_to_cpu(la.l2_cid),
  189. &la.l2_bdaddr, la.l2_bdaddr_type);
  190. if (err)
  191. return err;
  192. lock_sock(sk);
  193. err = bt_sock_wait_state(sk, BT_CONNECTED,
  194. sock_sndtimeo(sk, flags & O_NONBLOCK));
  195. release_sock(sk);
  196. return err;
  197. }
  198. static int l2cap_sock_listen(struct socket *sock, int backlog)
  199. {
  200. struct sock *sk = sock->sk;
  201. struct l2cap_chan *chan = l2cap_pi(sk)->chan;
  202. int err = 0;
  203. BT_DBG("sk %p backlog %d", sk, backlog);
  204. lock_sock(sk);
  205. if (sk->sk_state != BT_BOUND) {
  206. err = -EBADFD;
  207. goto done;
  208. }
  209. if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM) {
  210. err = -EINVAL;
  211. goto done;
  212. }
  213. switch (chan->mode) {
  214. case L2CAP_MODE_BASIC:
  215. case L2CAP_MODE_LE_FLOWCTL:
  216. break;
  217. case L2CAP_MODE_ERTM:
  218. case L2CAP_MODE_STREAMING:
  219. if (!disable_ertm)
  220. break;
  221. /* fall through */
  222. default:
  223. err = -EOPNOTSUPP;
  224. goto done;
  225. }
  226. sk->sk_max_ack_backlog = backlog;
  227. sk->sk_ack_backlog = 0;
  228. /* Listening channels need to use nested locking in order not to
  229. * cause lockdep warnings when the created child channels end up
  230. * being locked in the same thread as the parent channel.
  231. */
  232. atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
  233. chan->state = BT_LISTEN;
  234. sk->sk_state = BT_LISTEN;
  235. done:
  236. release_sock(sk);
  237. return err;
  238. }
  239. static int l2cap_sock_accept(struct socket *sock, struct socket *newsock,
  240. int flags)
  241. {
  242. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  243. struct sock *sk = sock->sk, *nsk;
  244. long timeo;
  245. int err = 0;
  246. lock_sock_nested(sk, L2CAP_NESTING_PARENT);
  247. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  248. BT_DBG("sk %p timeo %ld", sk, timeo);
  249. /* Wait for an incoming connection. (wake-one). */
  250. add_wait_queue_exclusive(sk_sleep(sk), &wait);
  251. while (1) {
  252. if (sk->sk_state != BT_LISTEN) {
  253. err = -EBADFD;
  254. break;
  255. }
  256. nsk = bt_accept_dequeue(sk, newsock);
  257. if (nsk)
  258. break;
  259. if (!timeo) {
  260. err = -EAGAIN;
  261. break;
  262. }
  263. if (signal_pending(current)) {
  264. err = sock_intr_errno(timeo);
  265. break;
  266. }
  267. release_sock(sk);
  268. timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
  269. lock_sock_nested(sk, L2CAP_NESTING_PARENT);
  270. }
  271. remove_wait_queue(sk_sleep(sk), &wait);
  272. if (err)
  273. goto done;
  274. newsock->state = SS_CONNECTED;
  275. BT_DBG("new socket %p", nsk);
  276. done:
  277. release_sock(sk);
  278. return err;
  279. }
  280. static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr,
  281. int *len, int peer)
  282. {
  283. struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
  284. struct sock *sk = sock->sk;
  285. struct l2cap_chan *chan = l2cap_pi(sk)->chan;
  286. BT_DBG("sock %p, sk %p", sock, sk);
  287. if (peer && sk->sk_state != BT_CONNECTED &&
  288. sk->sk_state != BT_CONNECT && sk->sk_state != BT_CONNECT2 &&
  289. sk->sk_state != BT_CONFIG)
  290. return -ENOTCONN;
  291. memset(la, 0, sizeof(struct sockaddr_l2));
  292. addr->sa_family = AF_BLUETOOTH;
  293. *len = sizeof(struct sockaddr_l2);
  294. la->l2_psm = chan->psm;
  295. if (peer) {
  296. bacpy(&la->l2_bdaddr, &chan->dst);
  297. la->l2_cid = cpu_to_le16(chan->dcid);
  298. la->l2_bdaddr_type = chan->dst_type;
  299. } else {
  300. bacpy(&la->l2_bdaddr, &chan->src);
  301. la->l2_cid = cpu_to_le16(chan->scid);
  302. la->l2_bdaddr_type = chan->src_type;
  303. }
  304. return 0;
  305. }
  306. static int l2cap_sock_getsockopt_old(struct socket *sock, int optname,
  307. char __user *optval, int __user *optlen)
  308. {
  309. struct sock *sk = sock->sk;
  310. struct l2cap_chan *chan = l2cap_pi(sk)->chan;
  311. struct l2cap_options opts;
  312. struct l2cap_conninfo cinfo;
  313. int len, err = 0;
  314. u32 opt;
  315. BT_DBG("sk %p", sk);
  316. if (get_user(len, optlen))
  317. return -EFAULT;
  318. lock_sock(sk);
  319. switch (optname) {
  320. case L2CAP_OPTIONS:
  321. /* LE sockets should use BT_SNDMTU/BT_RCVMTU, but since
  322. * legacy ATT code depends on getsockopt for
  323. * L2CAP_OPTIONS we need to let this pass.
  324. */
  325. if (bdaddr_type_is_le(chan->src_type) &&
  326. chan->scid != L2CAP_CID_ATT) {
  327. err = -EINVAL;
  328. break;
  329. }
  330. memset(&opts, 0, sizeof(opts));
  331. opts.imtu = chan->imtu;
  332. opts.omtu = chan->omtu;
  333. opts.flush_to = chan->flush_to;
  334. opts.mode = chan->mode;
  335. opts.fcs = chan->fcs;
  336. opts.max_tx = chan->max_tx;
  337. opts.txwin_size = chan->tx_win;
  338. len = min_t(unsigned int, len, sizeof(opts));
  339. if (copy_to_user(optval, (char *) &opts, len))
  340. err = -EFAULT;
  341. break;
  342. case L2CAP_LM:
  343. switch (chan->sec_level) {
  344. case BT_SECURITY_LOW:
  345. opt = L2CAP_LM_AUTH;
  346. break;
  347. case BT_SECURITY_MEDIUM:
  348. opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT;
  349. break;
  350. case BT_SECURITY_HIGH:
  351. opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
  352. L2CAP_LM_SECURE;
  353. break;
  354. case BT_SECURITY_FIPS:
  355. opt = L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT |
  356. L2CAP_LM_SECURE | L2CAP_LM_FIPS;
  357. break;
  358. default:
  359. opt = 0;
  360. break;
  361. }
  362. if (test_bit(FLAG_ROLE_SWITCH, &chan->flags))
  363. opt |= L2CAP_LM_MASTER;
  364. if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
  365. opt |= L2CAP_LM_RELIABLE;
  366. if (put_user(opt, (u32 __user *) optval))
  367. err = -EFAULT;
  368. break;
  369. case L2CAP_CONNINFO:
  370. if (sk->sk_state != BT_CONNECTED &&
  371. !(sk->sk_state == BT_CONNECT2 &&
  372. test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
  373. err = -ENOTCONN;
  374. break;
  375. }
  376. memset(&cinfo, 0, sizeof(cinfo));
  377. cinfo.hci_handle = chan->conn->hcon->handle;
  378. memcpy(cinfo.dev_class, chan->conn->hcon->dev_class, 3);
  379. len = min_t(unsigned int, len, sizeof(cinfo));
  380. if (copy_to_user(optval, (char *) &cinfo, len))
  381. err = -EFAULT;
  382. break;
  383. default:
  384. err = -ENOPROTOOPT;
  385. break;
  386. }
  387. release_sock(sk);
  388. return err;
  389. }
  390. static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname,
  391. char __user *optval, int __user *optlen)
  392. {
  393. struct sock *sk = sock->sk;
  394. struct l2cap_chan *chan = l2cap_pi(sk)->chan;
  395. struct bt_security sec;
  396. struct bt_power pwr;
  397. int len, err = 0;
  398. BT_DBG("sk %p", sk);
  399. if (level == SOL_L2CAP)
  400. return l2cap_sock_getsockopt_old(sock, optname, optval, optlen);
  401. if (level != SOL_BLUETOOTH)
  402. return -ENOPROTOOPT;
  403. if (get_user(len, optlen))
  404. return -EFAULT;
  405. lock_sock(sk);
  406. switch (optname) {
  407. case BT_SECURITY:
  408. if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
  409. chan->chan_type != L2CAP_CHAN_FIXED &&
  410. chan->chan_type != L2CAP_CHAN_RAW) {
  411. err = -EINVAL;
  412. break;
  413. }
  414. memset(&sec, 0, sizeof(sec));
  415. if (chan->conn) {
  416. sec.level = chan->conn->hcon->sec_level;
  417. if (sk->sk_state == BT_CONNECTED)
  418. sec.key_size = chan->conn->hcon->enc_key_size;
  419. } else {
  420. sec.level = chan->sec_level;
  421. }
  422. len = min_t(unsigned int, len, sizeof(sec));
  423. if (copy_to_user(optval, (char *) &sec, len))
  424. err = -EFAULT;
  425. break;
  426. case BT_DEFER_SETUP:
  427. if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
  428. err = -EINVAL;
  429. break;
  430. }
  431. if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
  432. (u32 __user *) optval))
  433. err = -EFAULT;
  434. break;
  435. case BT_FLUSHABLE:
  436. if (put_user(test_bit(FLAG_FLUSHABLE, &chan->flags),
  437. (u32 __user *) optval))
  438. err = -EFAULT;
  439. break;
  440. case BT_POWER:
  441. if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM
  442. && sk->sk_type != SOCK_RAW) {
  443. err = -EINVAL;
  444. break;
  445. }
  446. pwr.force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
  447. len = min_t(unsigned int, len, sizeof(pwr));
  448. if (copy_to_user(optval, (char *) &pwr, len))
  449. err = -EFAULT;
  450. break;
  451. case BT_CHANNEL_POLICY:
  452. if (put_user(chan->chan_policy, (u32 __user *) optval))
  453. err = -EFAULT;
  454. break;
  455. case BT_SNDMTU:
  456. if (!bdaddr_type_is_le(chan->src_type)) {
  457. err = -EINVAL;
  458. break;
  459. }
  460. if (sk->sk_state != BT_CONNECTED) {
  461. err = -ENOTCONN;
  462. break;
  463. }
  464. if (put_user(chan->omtu, (u16 __user *) optval))
  465. err = -EFAULT;
  466. break;
  467. case BT_RCVMTU:
  468. if (!bdaddr_type_is_le(chan->src_type)) {
  469. err = -EINVAL;
  470. break;
  471. }
  472. if (put_user(chan->imtu, (u16 __user *) optval))
  473. err = -EFAULT;
  474. break;
  475. default:
  476. err = -ENOPROTOOPT;
  477. break;
  478. }
  479. release_sock(sk);
  480. return err;
  481. }
  482. static bool l2cap_valid_mtu(struct l2cap_chan *chan, u16 mtu)
  483. {
  484. switch (chan->scid) {
  485. case L2CAP_CID_ATT:
  486. if (mtu < L2CAP_LE_MIN_MTU)
  487. return false;
  488. break;
  489. default:
  490. if (mtu < L2CAP_DEFAULT_MIN_MTU)
  491. return false;
  492. }
  493. return true;
  494. }
  495. static int l2cap_sock_setsockopt_old(struct socket *sock, int optname,
  496. char __user *optval, unsigned int optlen)
  497. {
  498. struct sock *sk = sock->sk;
  499. struct l2cap_chan *chan = l2cap_pi(sk)->chan;
  500. struct l2cap_options opts;
  501. int len, err = 0;
  502. u32 opt;
  503. BT_DBG("sk %p", sk);
  504. lock_sock(sk);
  505. switch (optname) {
  506. case L2CAP_OPTIONS:
  507. if (bdaddr_type_is_le(chan->src_type)) {
  508. err = -EINVAL;
  509. break;
  510. }
  511. if (sk->sk_state == BT_CONNECTED) {
  512. err = -EINVAL;
  513. break;
  514. }
  515. opts.imtu = chan->imtu;
  516. opts.omtu = chan->omtu;
  517. opts.flush_to = chan->flush_to;
  518. opts.mode = chan->mode;
  519. opts.fcs = chan->fcs;
  520. opts.max_tx = chan->max_tx;
  521. opts.txwin_size = chan->tx_win;
  522. len = min_t(unsigned int, sizeof(opts), optlen);
  523. if (copy_from_user((char *) &opts, optval, len)) {
  524. err = -EFAULT;
  525. break;
  526. }
  527. if (opts.txwin_size > L2CAP_DEFAULT_EXT_WINDOW) {
  528. err = -EINVAL;
  529. break;
  530. }
  531. if (!l2cap_valid_mtu(chan, opts.imtu)) {
  532. err = -EINVAL;
  533. break;
  534. }
  535. chan->mode = opts.mode;
  536. switch (chan->mode) {
  537. case L2CAP_MODE_LE_FLOWCTL:
  538. break;
  539. case L2CAP_MODE_BASIC:
  540. clear_bit(CONF_STATE2_DEVICE, &chan->conf_state);
  541. break;
  542. case L2CAP_MODE_ERTM:
  543. case L2CAP_MODE_STREAMING:
  544. if (!disable_ertm)
  545. break;
  546. /* fall through */
  547. default:
  548. err = -EINVAL;
  549. break;
  550. }
  551. chan->imtu = opts.imtu;
  552. chan->omtu = opts.omtu;
  553. chan->fcs = opts.fcs;
  554. chan->max_tx = opts.max_tx;
  555. chan->tx_win = opts.txwin_size;
  556. chan->flush_to = opts.flush_to;
  557. break;
  558. case L2CAP_LM:
  559. if (get_user(opt, (u32 __user *) optval)) {
  560. err = -EFAULT;
  561. break;
  562. }
  563. if (opt & L2CAP_LM_FIPS) {
  564. err = -EINVAL;
  565. break;
  566. }
  567. if (opt & L2CAP_LM_AUTH)
  568. chan->sec_level = BT_SECURITY_LOW;
  569. if (opt & L2CAP_LM_ENCRYPT)
  570. chan->sec_level = BT_SECURITY_MEDIUM;
  571. if (opt & L2CAP_LM_SECURE)
  572. chan->sec_level = BT_SECURITY_HIGH;
  573. if (opt & L2CAP_LM_MASTER)
  574. set_bit(FLAG_ROLE_SWITCH, &chan->flags);
  575. else
  576. clear_bit(FLAG_ROLE_SWITCH, &chan->flags);
  577. if (opt & L2CAP_LM_RELIABLE)
  578. set_bit(FLAG_FORCE_RELIABLE, &chan->flags);
  579. else
  580. clear_bit(FLAG_FORCE_RELIABLE, &chan->flags);
  581. break;
  582. default:
  583. err = -ENOPROTOOPT;
  584. break;
  585. }
  586. release_sock(sk);
  587. return err;
  588. }
  589. static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
  590. char __user *optval, unsigned int optlen)
  591. {
  592. struct sock *sk = sock->sk;
  593. struct l2cap_chan *chan = l2cap_pi(sk)->chan;
  594. struct bt_security sec;
  595. struct bt_power pwr;
  596. struct l2cap_conn *conn;
  597. int len, err = 0;
  598. u32 opt;
  599. BT_DBG("sk %p", sk);
  600. if (level == SOL_L2CAP)
  601. return l2cap_sock_setsockopt_old(sock, optname, optval, optlen);
  602. if (level != SOL_BLUETOOTH)
  603. return -ENOPROTOOPT;
  604. lock_sock(sk);
  605. switch (optname) {
  606. case BT_SECURITY:
  607. if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
  608. chan->chan_type != L2CAP_CHAN_FIXED &&
  609. chan->chan_type != L2CAP_CHAN_RAW) {
  610. err = -EINVAL;
  611. break;
  612. }
  613. sec.level = BT_SECURITY_LOW;
  614. len = min_t(unsigned int, sizeof(sec), optlen);
  615. if (copy_from_user((char *) &sec, optval, len)) {
  616. err = -EFAULT;
  617. break;
  618. }
  619. if (sec.level < BT_SECURITY_LOW ||
  620. sec.level > BT_SECURITY_FIPS) {
  621. err = -EINVAL;
  622. break;
  623. }
  624. chan->sec_level = sec.level;
  625. if (!chan->conn)
  626. break;
  627. conn = chan->conn;
  628. /*change security for LE channels */
  629. if (chan->scid == L2CAP_CID_ATT) {
  630. if (smp_conn_security(conn->hcon, sec.level))
  631. break;
  632. set_bit(FLAG_PENDING_SECURITY, &chan->flags);
  633. sk->sk_state = BT_CONFIG;
  634. chan->state = BT_CONFIG;
  635. /* or for ACL link */
  636. } else if ((sk->sk_state == BT_CONNECT2 &&
  637. test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) ||
  638. sk->sk_state == BT_CONNECTED) {
  639. if (!l2cap_chan_check_security(chan, true))
  640. set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
  641. else
  642. sk->sk_state_change(sk);
  643. } else {
  644. err = -EINVAL;
  645. }
  646. break;
  647. case BT_DEFER_SETUP:
  648. if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
  649. err = -EINVAL;
  650. break;
  651. }
  652. if (get_user(opt, (u32 __user *) optval)) {
  653. err = -EFAULT;
  654. break;
  655. }
  656. if (opt) {
  657. set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
  658. set_bit(FLAG_DEFER_SETUP, &chan->flags);
  659. } else {
  660. clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
  661. clear_bit(FLAG_DEFER_SETUP, &chan->flags);
  662. }
  663. break;
  664. case BT_FLUSHABLE:
  665. if (get_user(opt, (u32 __user *) optval)) {
  666. err = -EFAULT;
  667. break;
  668. }
  669. if (opt > BT_FLUSHABLE_ON) {
  670. err = -EINVAL;
  671. break;
  672. }
  673. if (opt == BT_FLUSHABLE_OFF) {
  674. conn = chan->conn;
  675. /* proceed further only when we have l2cap_conn and
  676. No Flush support in the LM */
  677. if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
  678. err = -EINVAL;
  679. break;
  680. }
  681. }
  682. if (opt)
  683. set_bit(FLAG_FLUSHABLE, &chan->flags);
  684. else
  685. clear_bit(FLAG_FLUSHABLE, &chan->flags);
  686. break;
  687. case BT_POWER:
  688. if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED &&
  689. chan->chan_type != L2CAP_CHAN_RAW) {
  690. err = -EINVAL;
  691. break;
  692. }
  693. pwr.force_active = BT_POWER_FORCE_ACTIVE_ON;
  694. len = min_t(unsigned int, sizeof(pwr), optlen);
  695. if (copy_from_user((char *) &pwr, optval, len)) {
  696. err = -EFAULT;
  697. break;
  698. }
  699. if (pwr.force_active)
  700. set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
  701. else
  702. clear_bit(FLAG_FORCE_ACTIVE, &chan->flags);
  703. break;
  704. case BT_CHANNEL_POLICY:
  705. if (get_user(opt, (u32 __user *) optval)) {
  706. err = -EFAULT;
  707. break;
  708. }
  709. if (opt > BT_CHANNEL_POLICY_AMP_PREFERRED) {
  710. err = -EINVAL;
  711. break;
  712. }
  713. if (chan->mode != L2CAP_MODE_ERTM &&
  714. chan->mode != L2CAP_MODE_STREAMING) {
  715. err = -EOPNOTSUPP;
  716. break;
  717. }
  718. chan->chan_policy = (u8) opt;
  719. if (sk->sk_state == BT_CONNECTED &&
  720. chan->move_role == L2CAP_MOVE_ROLE_NONE)
  721. l2cap_move_start(chan);
  722. break;
  723. case BT_SNDMTU:
  724. if (!bdaddr_type_is_le(chan->src_type)) {
  725. err = -EINVAL;
  726. break;
  727. }
  728. /* Setting is not supported as it's the remote side that
  729. * decides this.
  730. */
  731. err = -EPERM;
  732. break;
  733. case BT_RCVMTU:
  734. if (!bdaddr_type_is_le(chan->src_type)) {
  735. err = -EINVAL;
  736. break;
  737. }
  738. if (sk->sk_state == BT_CONNECTED) {
  739. err = -EISCONN;
  740. break;
  741. }
  742. if (get_user(opt, (u16 __user *) optval)) {
  743. err = -EFAULT;
  744. break;
  745. }
  746. chan->imtu = opt;
  747. break;
  748. default:
  749. err = -ENOPROTOOPT;
  750. break;
  751. }
  752. release_sock(sk);
  753. return err;
  754. }
  755. static int l2cap_sock_sendmsg(struct socket *sock, struct msghdr *msg,
  756. size_t len)
  757. {
  758. struct sock *sk = sock->sk;
  759. struct l2cap_chan *chan = l2cap_pi(sk)->chan;
  760. int err;
  761. BT_DBG("sock %p, sk %p", sock, sk);
  762. err = sock_error(sk);
  763. if (err)
  764. return err;
  765. if (msg->msg_flags & MSG_OOB)
  766. return -EOPNOTSUPP;
  767. if (sk->sk_state != BT_CONNECTED)
  768. return -ENOTCONN;
  769. lock_sock(sk);
  770. err = bt_sock_wait_ready(sk, msg->msg_flags);
  771. release_sock(sk);
  772. if (err)
  773. return err;
  774. l2cap_chan_lock(chan);
  775. err = l2cap_chan_send(chan, msg, len);
  776. l2cap_chan_unlock(chan);
  777. return err;
  778. }
  779. static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg,
  780. size_t len, int flags)
  781. {
  782. struct sock *sk = sock->sk;
  783. struct l2cap_pinfo *pi = l2cap_pi(sk);
  784. int err;
  785. lock_sock(sk);
  786. if (sk->sk_state == BT_CONNECT2 && test_bit(BT_SK_DEFER_SETUP,
  787. &bt_sk(sk)->flags)) {
  788. if (bdaddr_type_is_le(pi->chan->src_type)) {
  789. sk->sk_state = BT_CONNECTED;
  790. pi->chan->state = BT_CONNECTED;
  791. __l2cap_le_connect_rsp_defer(pi->chan);
  792. } else {
  793. sk->sk_state = BT_CONFIG;
  794. pi->chan->state = BT_CONFIG;
  795. __l2cap_connect_rsp_defer(pi->chan);
  796. }
  797. err = 0;
  798. goto done;
  799. }
  800. release_sock(sk);
  801. if (sock->type == SOCK_STREAM)
  802. err = bt_sock_stream_recvmsg(sock, msg, len, flags);
  803. else
  804. err = bt_sock_recvmsg(sock, msg, len, flags);
  805. if (pi->chan->mode != L2CAP_MODE_ERTM)
  806. return err;
  807. /* Attempt to put pending rx data in the socket buffer */
  808. lock_sock(sk);
  809. if (!test_bit(CONN_LOCAL_BUSY, &pi->chan->conn_state))
  810. goto done;
  811. if (pi->rx_busy_skb) {
  812. if (!__sock_queue_rcv_skb(sk, pi->rx_busy_skb))
  813. pi->rx_busy_skb = NULL;
  814. else
  815. goto done;
  816. }
  817. /* Restore data flow when half of the receive buffer is
  818. * available. This avoids resending large numbers of
  819. * frames.
  820. */
  821. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf >> 1)
  822. l2cap_chan_busy(pi->chan, 0);
  823. done:
  824. release_sock(sk);
  825. return err;
  826. }
  827. /* Kill socket (only if zapped and orphan)
  828. * Must be called on unlocked socket.
  829. */
  830. static void l2cap_sock_kill(struct sock *sk)
  831. {
  832. if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
  833. return;
  834. BT_DBG("sk %p state %s", sk, state_to_string(sk->sk_state));
  835. /* Kill poor orphan */
  836. l2cap_chan_put(l2cap_pi(sk)->chan);
  837. sock_set_flag(sk, SOCK_DEAD);
  838. sock_put(sk);
  839. }
  840. static int __l2cap_wait_ack(struct sock *sk, struct l2cap_chan *chan)
  841. {
  842. DECLARE_WAITQUEUE(wait, current);
  843. int err = 0;
  844. int timeo = L2CAP_WAIT_ACK_POLL_PERIOD;
  845. /* Timeout to prevent infinite loop */
  846. unsigned long timeout = jiffies + L2CAP_WAIT_ACK_TIMEOUT;
  847. add_wait_queue(sk_sleep(sk), &wait);
  848. set_current_state(TASK_INTERRUPTIBLE);
  849. do {
  850. BT_DBG("Waiting for %d ACKs, timeout %04d ms",
  851. chan->unacked_frames, time_after(jiffies, timeout) ? 0 :
  852. jiffies_to_msecs(timeout - jiffies));
  853. if (!timeo)
  854. timeo = L2CAP_WAIT_ACK_POLL_PERIOD;
  855. if (signal_pending(current)) {
  856. err = sock_intr_errno(timeo);
  857. break;
  858. }
  859. release_sock(sk);
  860. timeo = schedule_timeout(timeo);
  861. lock_sock(sk);
  862. set_current_state(TASK_INTERRUPTIBLE);
  863. err = sock_error(sk);
  864. if (err)
  865. break;
  866. if (time_after(jiffies, timeout)) {
  867. err = -ENOLINK;
  868. break;
  869. }
  870. } while (chan->unacked_frames > 0 &&
  871. chan->state == BT_CONNECTED);
  872. set_current_state(TASK_RUNNING);
  873. remove_wait_queue(sk_sleep(sk), &wait);
  874. return err;
  875. }
  876. static int l2cap_sock_shutdown(struct socket *sock, int how)
  877. {
  878. struct sock *sk = sock->sk;
  879. struct l2cap_chan *chan;
  880. struct l2cap_conn *conn;
  881. int err = 0;
  882. BT_DBG("sock %p, sk %p", sock, sk);
  883. if (!sk)
  884. return 0;
  885. lock_sock(sk);
  886. if (sk->sk_shutdown)
  887. goto shutdown_already;
  888. BT_DBG("Handling sock shutdown");
  889. /* prevent sk structure from being freed whilst unlocked */
  890. sock_hold(sk);
  891. chan = l2cap_pi(sk)->chan;
  892. /* prevent chan structure from being freed whilst unlocked */
  893. l2cap_chan_hold(chan);
  894. BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
  895. if (chan->mode == L2CAP_MODE_ERTM &&
  896. chan->unacked_frames > 0 &&
  897. chan->state == BT_CONNECTED) {
  898. err = __l2cap_wait_ack(sk, chan);
  899. /* After waiting for ACKs, check whether shutdown
  900. * has already been actioned to close the L2CAP
  901. * link such as by l2cap_disconnection_req().
  902. */
  903. if (sk->sk_shutdown)
  904. goto has_shutdown;
  905. }
  906. sk->sk_shutdown = SHUTDOWN_MASK;
  907. release_sock(sk);
  908. l2cap_chan_lock(chan);
  909. conn = chan->conn;
  910. if (conn)
  911. /* prevent conn structure from being freed */
  912. l2cap_conn_get(conn);
  913. l2cap_chan_unlock(chan);
  914. if (conn)
  915. /* mutex lock must be taken before l2cap_chan_lock() */
  916. mutex_lock(&conn->chan_lock);
  917. l2cap_chan_lock(chan);
  918. l2cap_chan_close(chan, 0);
  919. l2cap_chan_unlock(chan);
  920. if (conn) {
  921. mutex_unlock(&conn->chan_lock);
  922. l2cap_conn_put(conn);
  923. }
  924. lock_sock(sk);
  925. if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
  926. !(current->flags & PF_EXITING))
  927. err = bt_sock_wait_state(sk, BT_CLOSED,
  928. sk->sk_lingertime);
  929. has_shutdown:
  930. l2cap_chan_put(chan);
  931. sock_put(sk);
  932. shutdown_already:
  933. if (!err && sk->sk_err)
  934. err = -sk->sk_err;
  935. release_sock(sk);
  936. BT_DBG("Sock shutdown complete err: %d", err);
  937. return err;
  938. }
  939. static int l2cap_sock_release(struct socket *sock)
  940. {
  941. struct sock *sk = sock->sk;
  942. int err;
  943. BT_DBG("sock %p, sk %p", sock, sk);
  944. if (!sk)
  945. return 0;
  946. bt_sock_unlink(&l2cap_sk_list, sk);
  947. err = l2cap_sock_shutdown(sock, 2);
  948. sock_orphan(sk);
  949. l2cap_sock_kill(sk);
  950. return err;
  951. }
  952. static void l2cap_sock_cleanup_listen(struct sock *parent)
  953. {
  954. struct sock *sk;
  955. BT_DBG("parent %p state %s", parent,
  956. state_to_string(parent->sk_state));
  957. /* Close not yet accepted channels */
  958. while ((sk = bt_accept_dequeue(parent, NULL))) {
  959. struct l2cap_chan *chan = l2cap_pi(sk)->chan;
  960. BT_DBG("child chan %p state %s", chan,
  961. state_to_string(chan->state));
  962. l2cap_chan_lock(chan);
  963. __clear_chan_timer(chan);
  964. l2cap_chan_close(chan, ECONNRESET);
  965. l2cap_chan_unlock(chan);
  966. l2cap_sock_kill(sk);
  967. }
  968. }
  969. static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)
  970. {
  971. struct sock *sk, *parent = chan->data;
  972. lock_sock(parent);
  973. /* Check for backlog size */
  974. if (sk_acceptq_is_full(parent)) {
  975. BT_DBG("backlog full %d", parent->sk_ack_backlog);
  976. release_sock(parent);
  977. return NULL;
  978. }
  979. sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP,
  980. GFP_ATOMIC, 0);
  981. if (!sk) {
  982. release_sock(parent);
  983. return NULL;
  984. }
  985. bt_sock_reclassify_lock(sk, BTPROTO_L2CAP);
  986. l2cap_sock_init(sk, parent);
  987. bt_accept_enqueue(parent, sk);
  988. release_sock(parent);
  989. return l2cap_pi(sk)->chan;
  990. }
  991. static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
  992. {
  993. struct sock *sk = chan->data;
  994. int err;
  995. lock_sock(sk);
  996. if (l2cap_pi(sk)->rx_busy_skb) {
  997. err = -ENOMEM;
  998. goto done;
  999. }
  1000. if (chan->mode != L2CAP_MODE_ERTM &&
  1001. chan->mode != L2CAP_MODE_STREAMING) {
  1002. /* Even if no filter is attached, we could potentially
  1003. * get errors from security modules, etc.
  1004. */
  1005. err = sk_filter(sk, skb);
  1006. if (err)
  1007. goto done;
  1008. }
  1009. err = __sock_queue_rcv_skb(sk, skb);
  1010. /* For ERTM, handle one skb that doesn't fit into the recv
  1011. * buffer. This is important to do because the data frames
  1012. * have already been acked, so the skb cannot be discarded.
  1013. *
  1014. * Notify the l2cap core that the buffer is full, so the
  1015. * LOCAL_BUSY state is entered and no more frames are
  1016. * acked and reassembled until there is buffer space
  1017. * available.
  1018. */
  1019. if (err < 0 && chan->mode == L2CAP_MODE_ERTM) {
  1020. l2cap_pi(sk)->rx_busy_skb = skb;
  1021. l2cap_chan_busy(chan, 1);
  1022. err = 0;
  1023. }
  1024. done:
  1025. release_sock(sk);
  1026. return err;
  1027. }
  1028. static void l2cap_sock_close_cb(struct l2cap_chan *chan)
  1029. {
  1030. struct sock *sk = chan->data;
  1031. l2cap_sock_kill(sk);
  1032. }
  1033. static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err)
  1034. {
  1035. struct sock *sk = chan->data;
  1036. struct sock *parent;
  1037. BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
  1038. /* This callback can be called both for server (BT_LISTEN)
  1039. * sockets as well as "normal" ones. To avoid lockdep warnings
  1040. * with child socket locking (through l2cap_sock_cleanup_listen)
  1041. * we need separation into separate nesting levels. The simplest
  1042. * way to accomplish this is to inherit the nesting level used
  1043. * for the channel.
  1044. */
  1045. lock_sock_nested(sk, atomic_read(&chan->nesting));
  1046. parent = bt_sk(sk)->parent;
  1047. sock_set_flag(sk, SOCK_ZAPPED);
  1048. switch (chan->state) {
  1049. case BT_OPEN:
  1050. case BT_BOUND:
  1051. case BT_CLOSED:
  1052. break;
  1053. case BT_LISTEN:
  1054. l2cap_sock_cleanup_listen(sk);
  1055. sk->sk_state = BT_CLOSED;
  1056. chan->state = BT_CLOSED;
  1057. break;
  1058. default:
  1059. sk->sk_state = BT_CLOSED;
  1060. chan->state = BT_CLOSED;
  1061. sk->sk_err = err;
  1062. if (parent) {
  1063. bt_accept_unlink(sk);
  1064. parent->sk_data_ready(parent);
  1065. } else {
  1066. sk->sk_state_change(sk);
  1067. }
  1068. break;
  1069. }
  1070. release_sock(sk);
  1071. }
  1072. static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state,
  1073. int err)
  1074. {
  1075. struct sock *sk = chan->data;
  1076. sk->sk_state = state;
  1077. if (err)
  1078. sk->sk_err = err;
  1079. }
  1080. static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,
  1081. unsigned long hdr_len,
  1082. unsigned long len, int nb)
  1083. {
  1084. struct sock *sk = chan->data;
  1085. struct sk_buff *skb;
  1086. int err;
  1087. l2cap_chan_unlock(chan);
  1088. skb = bt_skb_send_alloc(sk, hdr_len + len, nb, &err);
  1089. l2cap_chan_lock(chan);
  1090. if (!skb)
  1091. return ERR_PTR(err);
  1092. skb->priority = sk->sk_priority;
  1093. bt_cb(skb)->l2cap.chan = chan;
  1094. return skb;
  1095. }
  1096. static void l2cap_sock_ready_cb(struct l2cap_chan *chan)
  1097. {
  1098. struct sock *sk = chan->data;
  1099. struct sock *parent;
  1100. lock_sock(sk);
  1101. parent = bt_sk(sk)->parent;
  1102. BT_DBG("sk %p, parent %p", sk, parent);
  1103. sk->sk_state = BT_CONNECTED;
  1104. sk->sk_state_change(sk);
  1105. if (parent)
  1106. parent->sk_data_ready(parent);
  1107. release_sock(sk);
  1108. }
  1109. static void l2cap_sock_defer_cb(struct l2cap_chan *chan)
  1110. {
  1111. struct sock *parent, *sk = chan->data;
  1112. lock_sock(sk);
  1113. parent = bt_sk(sk)->parent;
  1114. if (parent)
  1115. parent->sk_data_ready(parent);
  1116. release_sock(sk);
  1117. }
  1118. static void l2cap_sock_resume_cb(struct l2cap_chan *chan)
  1119. {
  1120. struct sock *sk = chan->data;
  1121. if (test_and_clear_bit(FLAG_PENDING_SECURITY, &chan->flags)) {
  1122. sk->sk_state = BT_CONNECTED;
  1123. chan->state = BT_CONNECTED;
  1124. }
  1125. clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
  1126. sk->sk_state_change(sk);
  1127. }
  1128. static void l2cap_sock_set_shutdown_cb(struct l2cap_chan *chan)
  1129. {
  1130. struct sock *sk = chan->data;
  1131. lock_sock(sk);
  1132. sk->sk_shutdown = SHUTDOWN_MASK;
  1133. release_sock(sk);
  1134. }
  1135. static long l2cap_sock_get_sndtimeo_cb(struct l2cap_chan *chan)
  1136. {
  1137. struct sock *sk = chan->data;
  1138. return sk->sk_sndtimeo;
  1139. }
  1140. static void l2cap_sock_suspend_cb(struct l2cap_chan *chan)
  1141. {
  1142. struct sock *sk = chan->data;
  1143. set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
  1144. sk->sk_state_change(sk);
  1145. }
  1146. static const struct l2cap_ops l2cap_chan_ops = {
  1147. .name = "L2CAP Socket Interface",
  1148. .new_connection = l2cap_sock_new_connection_cb,
  1149. .recv = l2cap_sock_recv_cb,
  1150. .close = l2cap_sock_close_cb,
  1151. .teardown = l2cap_sock_teardown_cb,
  1152. .state_change = l2cap_sock_state_change_cb,
  1153. .ready = l2cap_sock_ready_cb,
  1154. .defer = l2cap_sock_defer_cb,
  1155. .resume = l2cap_sock_resume_cb,
  1156. .suspend = l2cap_sock_suspend_cb,
  1157. .set_shutdown = l2cap_sock_set_shutdown_cb,
  1158. .get_sndtimeo = l2cap_sock_get_sndtimeo_cb,
  1159. .alloc_skb = l2cap_sock_alloc_skb_cb,
  1160. };
  1161. static void l2cap_sock_destruct(struct sock *sk)
  1162. {
  1163. BT_DBG("sk %p", sk);
  1164. if (l2cap_pi(sk)->chan)
  1165. l2cap_chan_put(l2cap_pi(sk)->chan);
  1166. if (l2cap_pi(sk)->rx_busy_skb) {
  1167. kfree_skb(l2cap_pi(sk)->rx_busy_skb);
  1168. l2cap_pi(sk)->rx_busy_skb = NULL;
  1169. }
  1170. skb_queue_purge(&sk->sk_receive_queue);
  1171. skb_queue_purge(&sk->sk_write_queue);
  1172. }
  1173. static void l2cap_skb_msg_name(struct sk_buff *skb, void *msg_name,
  1174. int *msg_namelen)
  1175. {
  1176. DECLARE_SOCKADDR(struct sockaddr_l2 *, la, msg_name);
  1177. memset(la, 0, sizeof(struct sockaddr_l2));
  1178. la->l2_family = AF_BLUETOOTH;
  1179. la->l2_psm = bt_cb(skb)->l2cap.psm;
  1180. bacpy(&la->l2_bdaddr, &bt_cb(skb)->l2cap.bdaddr);
  1181. *msg_namelen = sizeof(struct sockaddr_l2);
  1182. }
  1183. static void l2cap_sock_init(struct sock *sk, struct sock *parent)
  1184. {
  1185. struct l2cap_chan *chan = l2cap_pi(sk)->chan;
  1186. BT_DBG("sk %p", sk);
  1187. if (parent) {
  1188. struct l2cap_chan *pchan = l2cap_pi(parent)->chan;
  1189. sk->sk_type = parent->sk_type;
  1190. bt_sk(sk)->flags = bt_sk(parent)->flags;
  1191. chan->chan_type = pchan->chan_type;
  1192. chan->imtu = pchan->imtu;
  1193. chan->omtu = pchan->omtu;
  1194. chan->conf_state = pchan->conf_state;
  1195. chan->mode = pchan->mode;
  1196. chan->fcs = pchan->fcs;
  1197. chan->max_tx = pchan->max_tx;
  1198. chan->tx_win = pchan->tx_win;
  1199. chan->tx_win_max = pchan->tx_win_max;
  1200. chan->sec_level = pchan->sec_level;
  1201. chan->flags = pchan->flags;
  1202. chan->tx_credits = pchan->tx_credits;
  1203. chan->rx_credits = pchan->rx_credits;
  1204. if (chan->chan_type == L2CAP_CHAN_FIXED) {
  1205. chan->scid = pchan->scid;
  1206. chan->dcid = pchan->scid;
  1207. }
  1208. security_sk_clone(parent, sk);
  1209. } else {
  1210. switch (sk->sk_type) {
  1211. case SOCK_RAW:
  1212. chan->chan_type = L2CAP_CHAN_RAW;
  1213. break;
  1214. case SOCK_DGRAM:
  1215. chan->chan_type = L2CAP_CHAN_CONN_LESS;
  1216. bt_sk(sk)->skb_msg_name = l2cap_skb_msg_name;
  1217. break;
  1218. case SOCK_SEQPACKET:
  1219. case SOCK_STREAM:
  1220. chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
  1221. break;
  1222. }
  1223. chan->imtu = L2CAP_DEFAULT_MTU;
  1224. chan->omtu = 0;
  1225. if (!disable_ertm && sk->sk_type == SOCK_STREAM) {
  1226. chan->mode = L2CAP_MODE_ERTM;
  1227. set_bit(CONF_STATE2_DEVICE, &chan->conf_state);
  1228. } else {
  1229. chan->mode = L2CAP_MODE_BASIC;
  1230. }
  1231. l2cap_chan_set_defaults(chan);
  1232. }
  1233. /* Default config options */
  1234. chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
  1235. chan->data = sk;
  1236. chan->ops = &l2cap_chan_ops;
  1237. }
  1238. static struct proto l2cap_proto = {
  1239. .name = "L2CAP",
  1240. .owner = THIS_MODULE,
  1241. .obj_size = sizeof(struct l2cap_pinfo)
  1242. };
  1243. static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
  1244. int proto, gfp_t prio, int kern)
  1245. {
  1246. struct sock *sk;
  1247. struct l2cap_chan *chan;
  1248. sk = sk_alloc(net, PF_BLUETOOTH, prio, &l2cap_proto, kern);
  1249. if (!sk)
  1250. return NULL;
  1251. sock_init_data(sock, sk);
  1252. INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
  1253. sk->sk_destruct = l2cap_sock_destruct;
  1254. sk->sk_sndtimeo = L2CAP_CONN_TIMEOUT;
  1255. sock_reset_flag(sk, SOCK_ZAPPED);
  1256. sk->sk_protocol = proto;
  1257. sk->sk_state = BT_OPEN;
  1258. chan = l2cap_chan_create();
  1259. if (!chan) {
  1260. sk_free(sk);
  1261. return NULL;
  1262. }
  1263. l2cap_chan_hold(chan);
  1264. l2cap_pi(sk)->chan = chan;
  1265. return sk;
  1266. }
  1267. static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol,
  1268. int kern)
  1269. {
  1270. struct sock *sk;
  1271. BT_DBG("sock %p", sock);
  1272. sock->state = SS_UNCONNECTED;
  1273. if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM &&
  1274. sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
  1275. return -ESOCKTNOSUPPORT;
  1276. if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
  1277. return -EPERM;
  1278. sock->ops = &l2cap_sock_ops;
  1279. sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
  1280. if (!sk)
  1281. return -ENOMEM;
  1282. l2cap_sock_init(sk, NULL);
  1283. bt_sock_link(&l2cap_sk_list, sk);
  1284. return 0;
  1285. }
  1286. static const struct proto_ops l2cap_sock_ops = {
  1287. .family = PF_BLUETOOTH,
  1288. .owner = THIS_MODULE,
  1289. .release = l2cap_sock_release,
  1290. .bind = l2cap_sock_bind,
  1291. .connect = l2cap_sock_connect,
  1292. .listen = l2cap_sock_listen,
  1293. .accept = l2cap_sock_accept,
  1294. .getname = l2cap_sock_getname,
  1295. .sendmsg = l2cap_sock_sendmsg,
  1296. .recvmsg = l2cap_sock_recvmsg,
  1297. .poll = bt_sock_poll,
  1298. .ioctl = bt_sock_ioctl,
  1299. .mmap = sock_no_mmap,
  1300. .socketpair = sock_no_socketpair,
  1301. .shutdown = l2cap_sock_shutdown,
  1302. .setsockopt = l2cap_sock_setsockopt,
  1303. .getsockopt = l2cap_sock_getsockopt
  1304. };
  1305. static const struct net_proto_family l2cap_sock_family_ops = {
  1306. .family = PF_BLUETOOTH,
  1307. .owner = THIS_MODULE,
  1308. .create = l2cap_sock_create,
  1309. };
  1310. int __init l2cap_init_sockets(void)
  1311. {
  1312. int err;
  1313. BUILD_BUG_ON(sizeof(struct sockaddr_l2) > sizeof(struct sockaddr));
  1314. err = proto_register(&l2cap_proto, 0);
  1315. if (err < 0)
  1316. return err;
  1317. err = bt_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops);
  1318. if (err < 0) {
  1319. BT_ERR("L2CAP socket registration failed");
  1320. goto error;
  1321. }
  1322. err = bt_procfs_init(&init_net, "l2cap", &l2cap_sk_list,
  1323. NULL);
  1324. if (err < 0) {
  1325. BT_ERR("Failed to create L2CAP proc file");
  1326. bt_sock_unregister(BTPROTO_L2CAP);
  1327. goto error;
  1328. }
  1329. BT_INFO("L2CAP socket layer initialized");
  1330. return 0;
  1331. error:
  1332. proto_unregister(&l2cap_proto);
  1333. return err;
  1334. }
  1335. void l2cap_cleanup_sockets(void)
  1336. {
  1337. bt_procfs_cleanup(&init_net, "l2cap");
  1338. bt_sock_unregister(BTPROTO_L2CAP);
  1339. proto_unregister(&l2cap_proto);
  1340. }