llcp_commands.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. * Copyright (C) 2011 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/nfc.h>
  22. #include <net/nfc/nfc.h>
  23. #include "nfc.h"
  24. #include "llcp.h"
  25. static u8 llcp_tlv_length[LLCP_TLV_MAX] = {
  26. 0,
  27. 1, /* VERSION */
  28. 2, /* MIUX */
  29. 2, /* WKS */
  30. 1, /* LTO */
  31. 1, /* RW */
  32. 0, /* SN */
  33. 1, /* OPT */
  34. 0, /* SDREQ */
  35. 2, /* SDRES */
  36. };
  37. static u8 llcp_tlv8(u8 *tlv, u8 type)
  38. {
  39. if (tlv[0] != type || tlv[1] != llcp_tlv_length[tlv[0]])
  40. return 0;
  41. return tlv[2];
  42. }
  43. static u16 llcp_tlv16(u8 *tlv, u8 type)
  44. {
  45. if (tlv[0] != type || tlv[1] != llcp_tlv_length[tlv[0]])
  46. return 0;
  47. return be16_to_cpu(*((__be16 *)(tlv + 2)));
  48. }
  49. static u8 llcp_tlv_version(u8 *tlv)
  50. {
  51. return llcp_tlv8(tlv, LLCP_TLV_VERSION);
  52. }
  53. static u16 llcp_tlv_miux(u8 *tlv)
  54. {
  55. return llcp_tlv16(tlv, LLCP_TLV_MIUX) & 0x7ff;
  56. }
  57. static u16 llcp_tlv_wks(u8 *tlv)
  58. {
  59. return llcp_tlv16(tlv, LLCP_TLV_WKS);
  60. }
  61. static u16 llcp_tlv_lto(u8 *tlv)
  62. {
  63. return llcp_tlv8(tlv, LLCP_TLV_LTO);
  64. }
  65. static u8 llcp_tlv_opt(u8 *tlv)
  66. {
  67. return llcp_tlv8(tlv, LLCP_TLV_OPT);
  68. }
  69. static u8 llcp_tlv_rw(u8 *tlv)
  70. {
  71. return llcp_tlv8(tlv, LLCP_TLV_RW) & 0xf;
  72. }
  73. u8 *nfc_llcp_build_tlv(u8 type, u8 *value, u8 value_length, u8 *tlv_length)
  74. {
  75. u8 *tlv, length;
  76. pr_debug("type %d\n", type);
  77. if (type >= LLCP_TLV_MAX)
  78. return NULL;
  79. length = llcp_tlv_length[type];
  80. if (length == 0 && value_length == 0)
  81. return NULL;
  82. else if (length == 0)
  83. length = value_length;
  84. *tlv_length = 2 + length;
  85. tlv = kzalloc(2 + length, GFP_KERNEL);
  86. if (tlv == NULL)
  87. return tlv;
  88. tlv[0] = type;
  89. tlv[1] = length;
  90. memcpy(tlv + 2, value, length);
  91. return tlv;
  92. }
  93. struct nfc_llcp_sdp_tlv *nfc_llcp_build_sdres_tlv(u8 tid, u8 sap)
  94. {
  95. struct nfc_llcp_sdp_tlv *sdres;
  96. u8 value[2];
  97. sdres = kzalloc(sizeof(struct nfc_llcp_sdp_tlv), GFP_KERNEL);
  98. if (sdres == NULL)
  99. return NULL;
  100. value[0] = tid;
  101. value[1] = sap;
  102. sdres->tlv = nfc_llcp_build_tlv(LLCP_TLV_SDRES, value, 2,
  103. &sdres->tlv_len);
  104. if (sdres->tlv == NULL) {
  105. kfree(sdres);
  106. return NULL;
  107. }
  108. sdres->tid = tid;
  109. sdres->sap = sap;
  110. INIT_HLIST_NODE(&sdres->node);
  111. return sdres;
  112. }
  113. struct nfc_llcp_sdp_tlv *nfc_llcp_build_sdreq_tlv(u8 tid, char *uri,
  114. size_t uri_len)
  115. {
  116. struct nfc_llcp_sdp_tlv *sdreq;
  117. pr_debug("uri: %s, len: %zu\n", uri, uri_len);
  118. /* sdreq->tlv_len is u8, takes uri_len, + 3 for header, + 1 for NULL */
  119. if (WARN_ON_ONCE(uri_len > U8_MAX - 4))
  120. return NULL;
  121. sdreq = kzalloc(sizeof(struct nfc_llcp_sdp_tlv), GFP_KERNEL);
  122. if (sdreq == NULL)
  123. return NULL;
  124. sdreq->tlv_len = uri_len + 3;
  125. if (uri[uri_len - 1] == 0)
  126. sdreq->tlv_len--;
  127. sdreq->tlv = kzalloc(sdreq->tlv_len + 1, GFP_KERNEL);
  128. if (sdreq->tlv == NULL) {
  129. kfree(sdreq);
  130. return NULL;
  131. }
  132. sdreq->tlv[0] = LLCP_TLV_SDREQ;
  133. sdreq->tlv[1] = sdreq->tlv_len - 2;
  134. sdreq->tlv[2] = tid;
  135. sdreq->tid = tid;
  136. sdreq->uri = sdreq->tlv + 3;
  137. memcpy(sdreq->uri, uri, uri_len);
  138. sdreq->time = jiffies;
  139. INIT_HLIST_NODE(&sdreq->node);
  140. return sdreq;
  141. }
  142. void nfc_llcp_free_sdp_tlv(struct nfc_llcp_sdp_tlv *sdp)
  143. {
  144. kfree(sdp->tlv);
  145. kfree(sdp);
  146. }
  147. void nfc_llcp_free_sdp_tlv_list(struct hlist_head *head)
  148. {
  149. struct nfc_llcp_sdp_tlv *sdp;
  150. struct hlist_node *n;
  151. hlist_for_each_entry_safe(sdp, n, head, node) {
  152. hlist_del(&sdp->node);
  153. nfc_llcp_free_sdp_tlv(sdp);
  154. }
  155. }
  156. int nfc_llcp_parse_gb_tlv(struct nfc_llcp_local *local,
  157. u8 *tlv_array, u16 tlv_array_len)
  158. {
  159. u8 *tlv = tlv_array, type, length, offset = 0;
  160. pr_debug("TLV array length %d\n", tlv_array_len);
  161. if (local == NULL)
  162. return -ENODEV;
  163. while (offset < tlv_array_len) {
  164. type = tlv[0];
  165. length = tlv[1];
  166. pr_debug("type 0x%x length %d\n", type, length);
  167. switch (type) {
  168. case LLCP_TLV_VERSION:
  169. local->remote_version = llcp_tlv_version(tlv);
  170. break;
  171. case LLCP_TLV_MIUX:
  172. local->remote_miu = llcp_tlv_miux(tlv) + 128;
  173. break;
  174. case LLCP_TLV_WKS:
  175. local->remote_wks = llcp_tlv_wks(tlv);
  176. break;
  177. case LLCP_TLV_LTO:
  178. local->remote_lto = llcp_tlv_lto(tlv) * 10;
  179. break;
  180. case LLCP_TLV_OPT:
  181. local->remote_opt = llcp_tlv_opt(tlv);
  182. break;
  183. default:
  184. pr_err("Invalid gt tlv value 0x%x\n", type);
  185. break;
  186. }
  187. offset += length + 2;
  188. tlv += length + 2;
  189. }
  190. pr_debug("version 0x%x miu %d lto %d opt 0x%x wks 0x%x\n",
  191. local->remote_version, local->remote_miu,
  192. local->remote_lto, local->remote_opt,
  193. local->remote_wks);
  194. return 0;
  195. }
  196. int nfc_llcp_parse_connection_tlv(struct nfc_llcp_sock *sock,
  197. u8 *tlv_array, u16 tlv_array_len)
  198. {
  199. u8 *tlv = tlv_array, type, length, offset = 0;
  200. pr_debug("TLV array length %d\n", tlv_array_len);
  201. if (sock == NULL)
  202. return -ENOTCONN;
  203. while (offset < tlv_array_len) {
  204. type = tlv[0];
  205. length = tlv[1];
  206. pr_debug("type 0x%x length %d\n", type, length);
  207. switch (type) {
  208. case LLCP_TLV_MIUX:
  209. sock->remote_miu = llcp_tlv_miux(tlv) + 128;
  210. break;
  211. case LLCP_TLV_RW:
  212. sock->remote_rw = llcp_tlv_rw(tlv);
  213. break;
  214. case LLCP_TLV_SN:
  215. break;
  216. default:
  217. pr_err("Invalid gt tlv value 0x%x\n", type);
  218. break;
  219. }
  220. offset += length + 2;
  221. tlv += length + 2;
  222. }
  223. pr_debug("sock %p rw %d miu %d\n", sock,
  224. sock->remote_rw, sock->remote_miu);
  225. return 0;
  226. }
  227. static struct sk_buff *llcp_add_header(struct sk_buff *pdu,
  228. u8 dsap, u8 ssap, u8 ptype)
  229. {
  230. u8 header[2];
  231. pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
  232. header[0] = (u8)((dsap << 2) | (ptype >> 2));
  233. header[1] = (u8)((ptype << 6) | ssap);
  234. pr_debug("header 0x%x 0x%x\n", header[0], header[1]);
  235. memcpy(skb_put(pdu, LLCP_HEADER_SIZE), header, LLCP_HEADER_SIZE);
  236. return pdu;
  237. }
  238. static struct sk_buff *llcp_add_tlv(struct sk_buff *pdu, u8 *tlv,
  239. u8 tlv_length)
  240. {
  241. /* XXX Add an skb length check */
  242. if (tlv == NULL)
  243. return NULL;
  244. memcpy(skb_put(pdu, tlv_length), tlv, tlv_length);
  245. return pdu;
  246. }
  247. static struct sk_buff *llcp_allocate_pdu(struct nfc_llcp_sock *sock,
  248. u8 cmd, u16 size)
  249. {
  250. struct sk_buff *skb;
  251. int err;
  252. if (sock->ssap == 0)
  253. return NULL;
  254. skb = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT,
  255. size + LLCP_HEADER_SIZE, &err);
  256. if (skb == NULL) {
  257. pr_err("Could not allocate PDU\n");
  258. return NULL;
  259. }
  260. skb = llcp_add_header(skb, sock->dsap, sock->ssap, cmd);
  261. return skb;
  262. }
  263. int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock)
  264. {
  265. struct sk_buff *skb;
  266. struct nfc_dev *dev;
  267. struct nfc_llcp_local *local;
  268. pr_debug("Sending DISC\n");
  269. local = sock->local;
  270. if (local == NULL)
  271. return -ENODEV;
  272. dev = sock->dev;
  273. if (dev == NULL)
  274. return -ENODEV;
  275. skb = llcp_allocate_pdu(sock, LLCP_PDU_DISC, 0);
  276. if (skb == NULL)
  277. return -ENOMEM;
  278. skb_queue_tail(&local->tx_queue, skb);
  279. return 0;
  280. }
  281. int nfc_llcp_send_symm(struct nfc_dev *dev)
  282. {
  283. struct sk_buff *skb;
  284. struct nfc_llcp_local *local;
  285. u16 size = 0;
  286. pr_debug("Sending SYMM\n");
  287. local = nfc_llcp_find_local(dev);
  288. if (local == NULL)
  289. return -ENODEV;
  290. size += LLCP_HEADER_SIZE;
  291. size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
  292. skb = alloc_skb(size, GFP_KERNEL);
  293. if (skb == NULL)
  294. return -ENOMEM;
  295. skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
  296. skb = llcp_add_header(skb, 0, 0, LLCP_PDU_SYMM);
  297. __net_timestamp(skb);
  298. nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_TX);
  299. return nfc_data_exchange(dev, local->target_idx, skb,
  300. nfc_llcp_recv, local);
  301. }
  302. int nfc_llcp_send_connect(struct nfc_llcp_sock *sock)
  303. {
  304. struct nfc_llcp_local *local;
  305. struct sk_buff *skb;
  306. u8 *service_name_tlv = NULL, service_name_tlv_length;
  307. u8 *miux_tlv = NULL, miux_tlv_length;
  308. u8 *rw_tlv = NULL, rw_tlv_length, rw;
  309. int err;
  310. u16 size = 0;
  311. __be16 miux;
  312. pr_debug("Sending CONNECT\n");
  313. local = sock->local;
  314. if (local == NULL)
  315. return -ENODEV;
  316. if (sock->service_name != NULL) {
  317. service_name_tlv = nfc_llcp_build_tlv(LLCP_TLV_SN,
  318. sock->service_name,
  319. sock->service_name_len,
  320. &service_name_tlv_length);
  321. size += service_name_tlv_length;
  322. }
  323. /* If the socket parameters are not set, use the local ones */
  324. miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ?
  325. local->miux : sock->miux;
  326. rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw;
  327. miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
  328. &miux_tlv_length);
  329. size += miux_tlv_length;
  330. rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length);
  331. size += rw_tlv_length;
  332. pr_debug("SKB size %d SN length %zu\n", size, sock->service_name_len);
  333. skb = llcp_allocate_pdu(sock, LLCP_PDU_CONNECT, size);
  334. if (skb == NULL) {
  335. err = -ENOMEM;
  336. goto error_tlv;
  337. }
  338. llcp_add_tlv(skb, service_name_tlv, service_name_tlv_length);
  339. llcp_add_tlv(skb, miux_tlv, miux_tlv_length);
  340. llcp_add_tlv(skb, rw_tlv, rw_tlv_length);
  341. skb_queue_tail(&local->tx_queue, skb);
  342. err = 0;
  343. error_tlv:
  344. if (err)
  345. pr_err("error %d\n", err);
  346. kfree(service_name_tlv);
  347. kfree(miux_tlv);
  348. kfree(rw_tlv);
  349. return err;
  350. }
  351. int nfc_llcp_send_cc(struct nfc_llcp_sock *sock)
  352. {
  353. struct nfc_llcp_local *local;
  354. struct sk_buff *skb;
  355. u8 *miux_tlv = NULL, miux_tlv_length;
  356. u8 *rw_tlv = NULL, rw_tlv_length, rw;
  357. int err;
  358. u16 size = 0;
  359. __be16 miux;
  360. pr_debug("Sending CC\n");
  361. local = sock->local;
  362. if (local == NULL)
  363. return -ENODEV;
  364. /* If the socket parameters are not set, use the local ones */
  365. miux = be16_to_cpu(sock->miux) > LLCP_MAX_MIUX ?
  366. local->miux : sock->miux;
  367. rw = sock->rw > LLCP_MAX_RW ? local->rw : sock->rw;
  368. miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0,
  369. &miux_tlv_length);
  370. size += miux_tlv_length;
  371. rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length);
  372. size += rw_tlv_length;
  373. skb = llcp_allocate_pdu(sock, LLCP_PDU_CC, size);
  374. if (skb == NULL) {
  375. err = -ENOMEM;
  376. goto error_tlv;
  377. }
  378. llcp_add_tlv(skb, miux_tlv, miux_tlv_length);
  379. llcp_add_tlv(skb, rw_tlv, rw_tlv_length);
  380. skb_queue_tail(&local->tx_queue, skb);
  381. err = 0;
  382. error_tlv:
  383. if (err)
  384. pr_err("error %d\n", err);
  385. kfree(miux_tlv);
  386. kfree(rw_tlv);
  387. return err;
  388. }
  389. static struct sk_buff *nfc_llcp_allocate_snl(struct nfc_llcp_local *local,
  390. size_t tlv_length)
  391. {
  392. struct sk_buff *skb;
  393. struct nfc_dev *dev;
  394. u16 size = 0;
  395. if (local == NULL)
  396. return ERR_PTR(-ENODEV);
  397. dev = local->dev;
  398. if (dev == NULL)
  399. return ERR_PTR(-ENODEV);
  400. size += LLCP_HEADER_SIZE;
  401. size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
  402. size += tlv_length;
  403. skb = alloc_skb(size, GFP_KERNEL);
  404. if (skb == NULL)
  405. return ERR_PTR(-ENOMEM);
  406. skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
  407. skb = llcp_add_header(skb, LLCP_SAP_SDP, LLCP_SAP_SDP, LLCP_PDU_SNL);
  408. return skb;
  409. }
  410. int nfc_llcp_send_snl_sdres(struct nfc_llcp_local *local,
  411. struct hlist_head *tlv_list, size_t tlvs_len)
  412. {
  413. struct nfc_llcp_sdp_tlv *sdp;
  414. struct hlist_node *n;
  415. struct sk_buff *skb;
  416. skb = nfc_llcp_allocate_snl(local, tlvs_len);
  417. if (IS_ERR(skb))
  418. return PTR_ERR(skb);
  419. hlist_for_each_entry_safe(sdp, n, tlv_list, node) {
  420. memcpy(skb_put(skb, sdp->tlv_len), sdp->tlv, sdp->tlv_len);
  421. hlist_del(&sdp->node);
  422. nfc_llcp_free_sdp_tlv(sdp);
  423. }
  424. skb_queue_tail(&local->tx_queue, skb);
  425. return 0;
  426. }
  427. int nfc_llcp_send_snl_sdreq(struct nfc_llcp_local *local,
  428. struct hlist_head *tlv_list, size_t tlvs_len)
  429. {
  430. struct nfc_llcp_sdp_tlv *sdreq;
  431. struct hlist_node *n;
  432. struct sk_buff *skb;
  433. skb = nfc_llcp_allocate_snl(local, tlvs_len);
  434. if (IS_ERR(skb))
  435. return PTR_ERR(skb);
  436. mutex_lock(&local->sdreq_lock);
  437. if (hlist_empty(&local->pending_sdreqs))
  438. mod_timer(&local->sdreq_timer,
  439. jiffies + msecs_to_jiffies(3 * local->remote_lto));
  440. hlist_for_each_entry_safe(sdreq, n, tlv_list, node) {
  441. pr_debug("tid %d for %s\n", sdreq->tid, sdreq->uri);
  442. memcpy(skb_put(skb, sdreq->tlv_len), sdreq->tlv,
  443. sdreq->tlv_len);
  444. hlist_del(&sdreq->node);
  445. hlist_add_head(&sdreq->node, &local->pending_sdreqs);
  446. }
  447. mutex_unlock(&local->sdreq_lock);
  448. skb_queue_tail(&local->tx_queue, skb);
  449. return 0;
  450. }
  451. int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason)
  452. {
  453. struct sk_buff *skb;
  454. struct nfc_dev *dev;
  455. u16 size = 1; /* Reason code */
  456. pr_debug("Sending DM reason 0x%x\n", reason);
  457. if (local == NULL)
  458. return -ENODEV;
  459. dev = local->dev;
  460. if (dev == NULL)
  461. return -ENODEV;
  462. size += LLCP_HEADER_SIZE;
  463. size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
  464. skb = alloc_skb(size, GFP_KERNEL);
  465. if (skb == NULL)
  466. return -ENOMEM;
  467. skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
  468. skb = llcp_add_header(skb, dsap, ssap, LLCP_PDU_DM);
  469. memcpy(skb_put(skb, 1), &reason, 1);
  470. skb_queue_head(&local->tx_queue, skb);
  471. return 0;
  472. }
  473. int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock,
  474. struct msghdr *msg, size_t len)
  475. {
  476. struct sk_buff *pdu;
  477. struct sock *sk = &sock->sk;
  478. struct nfc_llcp_local *local;
  479. size_t frag_len = 0, remaining_len;
  480. u8 *msg_data, *msg_ptr;
  481. u16 remote_miu;
  482. pr_debug("Send I frame len %zd\n", len);
  483. local = sock->local;
  484. if (local == NULL)
  485. return -ENODEV;
  486. /* Remote is ready but has not acknowledged our frames */
  487. if((sock->remote_ready &&
  488. skb_queue_len(&sock->tx_pending_queue) >= sock->remote_rw &&
  489. skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) {
  490. pr_err("Pending queue is full %d frames\n",
  491. skb_queue_len(&sock->tx_pending_queue));
  492. return -ENOBUFS;
  493. }
  494. /* Remote is not ready and we've been queueing enough frames */
  495. if ((!sock->remote_ready &&
  496. skb_queue_len(&sock->tx_queue) >= 2 * sock->remote_rw)) {
  497. pr_err("Tx queue is full %d frames\n",
  498. skb_queue_len(&sock->tx_queue));
  499. return -ENOBUFS;
  500. }
  501. msg_data = kmalloc(len, GFP_USER | __GFP_NOWARN);
  502. if (msg_data == NULL)
  503. return -ENOMEM;
  504. if (memcpy_from_msg(msg_data, msg, len)) {
  505. kfree(msg_data);
  506. return -EFAULT;
  507. }
  508. remaining_len = len;
  509. msg_ptr = msg_data;
  510. do {
  511. remote_miu = sock->remote_miu > LLCP_MAX_MIU ?
  512. LLCP_DEFAULT_MIU : sock->remote_miu;
  513. frag_len = min_t(size_t, remote_miu, remaining_len);
  514. pr_debug("Fragment %zd bytes remaining %zd",
  515. frag_len, remaining_len);
  516. pdu = llcp_allocate_pdu(sock, LLCP_PDU_I,
  517. frag_len + LLCP_SEQUENCE_SIZE);
  518. if (pdu == NULL) {
  519. kfree(msg_data);
  520. return -ENOMEM;
  521. }
  522. skb_put(pdu, LLCP_SEQUENCE_SIZE);
  523. if (likely(frag_len > 0))
  524. memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len);
  525. skb_queue_tail(&sock->tx_queue, pdu);
  526. lock_sock(sk);
  527. nfc_llcp_queue_i_frames(sock);
  528. release_sock(sk);
  529. remaining_len -= frag_len;
  530. msg_ptr += frag_len;
  531. } while (remaining_len > 0);
  532. kfree(msg_data);
  533. return len;
  534. }
  535. int nfc_llcp_send_ui_frame(struct nfc_llcp_sock *sock, u8 ssap, u8 dsap,
  536. struct msghdr *msg, size_t len)
  537. {
  538. struct sk_buff *pdu;
  539. struct nfc_llcp_local *local;
  540. size_t frag_len = 0, remaining_len;
  541. u8 *msg_ptr, *msg_data;
  542. u16 remote_miu;
  543. int err;
  544. pr_debug("Send UI frame len %zd\n", len);
  545. local = sock->local;
  546. if (local == NULL)
  547. return -ENODEV;
  548. msg_data = kmalloc(len, GFP_USER | __GFP_NOWARN);
  549. if (msg_data == NULL)
  550. return -ENOMEM;
  551. if (memcpy_from_msg(msg_data, msg, len)) {
  552. kfree(msg_data);
  553. return -EFAULT;
  554. }
  555. remaining_len = len;
  556. msg_ptr = msg_data;
  557. do {
  558. remote_miu = sock->remote_miu > LLCP_MAX_MIU ?
  559. local->remote_miu : sock->remote_miu;
  560. frag_len = min_t(size_t, remote_miu, remaining_len);
  561. pr_debug("Fragment %zd bytes remaining %zd",
  562. frag_len, remaining_len);
  563. pdu = nfc_alloc_send_skb(sock->dev, &sock->sk, 0,
  564. frag_len + LLCP_HEADER_SIZE, &err);
  565. if (pdu == NULL) {
  566. pr_err("Could not allocate PDU (error=%d)\n", err);
  567. len -= remaining_len;
  568. if (len == 0)
  569. len = err;
  570. break;
  571. }
  572. pdu = llcp_add_header(pdu, dsap, ssap, LLCP_PDU_UI);
  573. if (likely(frag_len > 0))
  574. memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len);
  575. /* No need to check for the peer RW for UI frames */
  576. skb_queue_tail(&local->tx_queue, pdu);
  577. remaining_len -= frag_len;
  578. msg_ptr += frag_len;
  579. } while (remaining_len > 0);
  580. kfree(msg_data);
  581. return len;
  582. }
  583. int nfc_llcp_send_rr(struct nfc_llcp_sock *sock)
  584. {
  585. struct sk_buff *skb;
  586. struct nfc_llcp_local *local;
  587. pr_debug("Send rr nr %d\n", sock->recv_n);
  588. local = sock->local;
  589. if (local == NULL)
  590. return -ENODEV;
  591. skb = llcp_allocate_pdu(sock, LLCP_PDU_RR, LLCP_SEQUENCE_SIZE);
  592. if (skb == NULL)
  593. return -ENOMEM;
  594. skb_put(skb, LLCP_SEQUENCE_SIZE);
  595. skb->data[2] = sock->recv_n;
  596. skb_queue_head(&local->tx_queue, skb);
  597. return 0;
  598. }