llcp_core.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  1. /*
  2. * Copyright (C) 2011 Intel Corporation. All rights reserved.
  3. * Copyright (C) 2014 Marvell International Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define pr_fmt(fmt) "llcp: %s: " fmt, __func__
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/list.h>
  22. #include <linux/nfc.h>
  23. #include "nfc.h"
  24. #include "llcp.h"
  25. static u8 llcp_magic[3] = {0x46, 0x66, 0x6d};
  26. static LIST_HEAD(llcp_devices);
  27. static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb);
  28. void nfc_llcp_sock_link(struct llcp_sock_list *l, struct sock *sk)
  29. {
  30. write_lock(&l->lock);
  31. sk_add_node(sk, &l->head);
  32. write_unlock(&l->lock);
  33. }
  34. void nfc_llcp_sock_unlink(struct llcp_sock_list *l, struct sock *sk)
  35. {
  36. write_lock(&l->lock);
  37. sk_del_node_init(sk);
  38. write_unlock(&l->lock);
  39. }
  40. void nfc_llcp_socket_remote_param_init(struct nfc_llcp_sock *sock)
  41. {
  42. sock->remote_rw = LLCP_DEFAULT_RW;
  43. sock->remote_miu = LLCP_MAX_MIU + 1;
  44. }
  45. static void nfc_llcp_socket_purge(struct nfc_llcp_sock *sock)
  46. {
  47. struct nfc_llcp_local *local = sock->local;
  48. struct sk_buff *s, *tmp;
  49. pr_debug("%p\n", &sock->sk);
  50. skb_queue_purge(&sock->tx_queue);
  51. skb_queue_purge(&sock->tx_pending_queue);
  52. if (local == NULL)
  53. return;
  54. /* Search for local pending SKBs that are related to this socket */
  55. skb_queue_walk_safe(&local->tx_queue, s, tmp) {
  56. if (s->sk != &sock->sk)
  57. continue;
  58. skb_unlink(s, &local->tx_queue);
  59. kfree_skb(s);
  60. }
  61. }
  62. static void nfc_llcp_socket_release(struct nfc_llcp_local *local, bool device,
  63. int err)
  64. {
  65. struct sock *sk;
  66. struct hlist_node *tmp;
  67. struct nfc_llcp_sock *llcp_sock;
  68. skb_queue_purge(&local->tx_queue);
  69. write_lock(&local->sockets.lock);
  70. sk_for_each_safe(sk, tmp, &local->sockets.head) {
  71. llcp_sock = nfc_llcp_sock(sk);
  72. bh_lock_sock(sk);
  73. nfc_llcp_socket_purge(llcp_sock);
  74. if (sk->sk_state == LLCP_CONNECTED)
  75. nfc_put_device(llcp_sock->dev);
  76. if (sk->sk_state == LLCP_LISTEN) {
  77. struct nfc_llcp_sock *lsk, *n;
  78. struct sock *accept_sk;
  79. list_for_each_entry_safe(lsk, n,
  80. &llcp_sock->accept_queue,
  81. accept_queue) {
  82. accept_sk = &lsk->sk;
  83. bh_lock_sock(accept_sk);
  84. nfc_llcp_accept_unlink(accept_sk);
  85. if (err)
  86. accept_sk->sk_err = err;
  87. accept_sk->sk_state = LLCP_CLOSED;
  88. accept_sk->sk_state_change(sk);
  89. bh_unlock_sock(accept_sk);
  90. }
  91. }
  92. if (err)
  93. sk->sk_err = err;
  94. sk->sk_state = LLCP_CLOSED;
  95. sk->sk_state_change(sk);
  96. bh_unlock_sock(sk);
  97. sk_del_node_init(sk);
  98. }
  99. write_unlock(&local->sockets.lock);
  100. /* If we still have a device, we keep the RAW sockets alive */
  101. if (device == true)
  102. return;
  103. write_lock(&local->raw_sockets.lock);
  104. sk_for_each_safe(sk, tmp, &local->raw_sockets.head) {
  105. llcp_sock = nfc_llcp_sock(sk);
  106. bh_lock_sock(sk);
  107. nfc_llcp_socket_purge(llcp_sock);
  108. if (err)
  109. sk->sk_err = err;
  110. sk->sk_state = LLCP_CLOSED;
  111. sk->sk_state_change(sk);
  112. bh_unlock_sock(sk);
  113. sk_del_node_init(sk);
  114. }
  115. write_unlock(&local->raw_sockets.lock);
  116. }
  117. struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
  118. {
  119. kref_get(&local->ref);
  120. return local;
  121. }
  122. static void local_cleanup(struct nfc_llcp_local *local)
  123. {
  124. nfc_llcp_socket_release(local, false, ENXIO);
  125. del_timer_sync(&local->link_timer);
  126. skb_queue_purge(&local->tx_queue);
  127. cancel_work_sync(&local->tx_work);
  128. cancel_work_sync(&local->rx_work);
  129. cancel_work_sync(&local->timeout_work);
  130. kfree_skb(local->rx_pending);
  131. del_timer_sync(&local->sdreq_timer);
  132. cancel_work_sync(&local->sdreq_timeout_work);
  133. nfc_llcp_free_sdp_tlv_list(&local->pending_sdreqs);
  134. }
  135. static void local_release(struct kref *ref)
  136. {
  137. struct nfc_llcp_local *local;
  138. local = container_of(ref, struct nfc_llcp_local, ref);
  139. list_del(&local->list);
  140. local_cleanup(local);
  141. kfree(local);
  142. }
  143. int nfc_llcp_local_put(struct nfc_llcp_local *local)
  144. {
  145. if (local == NULL)
  146. return 0;
  147. return kref_put(&local->ref, local_release);
  148. }
  149. static struct nfc_llcp_sock *nfc_llcp_sock_get(struct nfc_llcp_local *local,
  150. u8 ssap, u8 dsap)
  151. {
  152. struct sock *sk;
  153. struct nfc_llcp_sock *llcp_sock, *tmp_sock;
  154. pr_debug("ssap dsap %d %d\n", ssap, dsap);
  155. if (ssap == 0 && dsap == 0)
  156. return NULL;
  157. read_lock(&local->sockets.lock);
  158. llcp_sock = NULL;
  159. sk_for_each(sk, &local->sockets.head) {
  160. tmp_sock = nfc_llcp_sock(sk);
  161. if (tmp_sock->ssap == ssap && tmp_sock->dsap == dsap) {
  162. llcp_sock = tmp_sock;
  163. break;
  164. }
  165. }
  166. read_unlock(&local->sockets.lock);
  167. if (llcp_sock == NULL)
  168. return NULL;
  169. sock_hold(&llcp_sock->sk);
  170. return llcp_sock;
  171. }
  172. static void nfc_llcp_sock_put(struct nfc_llcp_sock *sock)
  173. {
  174. sock_put(&sock->sk);
  175. }
  176. static void nfc_llcp_timeout_work(struct work_struct *work)
  177. {
  178. struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
  179. timeout_work);
  180. nfc_dep_link_down(local->dev);
  181. }
  182. static void nfc_llcp_symm_timer(unsigned long data)
  183. {
  184. struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
  185. pr_err("SYMM timeout\n");
  186. schedule_work(&local->timeout_work);
  187. }
  188. static void nfc_llcp_sdreq_timeout_work(struct work_struct *work)
  189. {
  190. unsigned long time;
  191. HLIST_HEAD(nl_sdres_list);
  192. struct hlist_node *n;
  193. struct nfc_llcp_sdp_tlv *sdp;
  194. struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
  195. sdreq_timeout_work);
  196. mutex_lock(&local->sdreq_lock);
  197. time = jiffies - msecs_to_jiffies(3 * local->remote_lto);
  198. hlist_for_each_entry_safe(sdp, n, &local->pending_sdreqs, node) {
  199. if (time_after(sdp->time, time))
  200. continue;
  201. sdp->sap = LLCP_SDP_UNBOUND;
  202. hlist_del(&sdp->node);
  203. hlist_add_head(&sdp->node, &nl_sdres_list);
  204. }
  205. if (!hlist_empty(&local->pending_sdreqs))
  206. mod_timer(&local->sdreq_timer,
  207. jiffies + msecs_to_jiffies(3 * local->remote_lto));
  208. mutex_unlock(&local->sdreq_lock);
  209. if (!hlist_empty(&nl_sdres_list))
  210. nfc_genl_llc_send_sdres(local->dev, &nl_sdres_list);
  211. }
  212. static void nfc_llcp_sdreq_timer(unsigned long data)
  213. {
  214. struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
  215. schedule_work(&local->sdreq_timeout_work);
  216. }
  217. struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev)
  218. {
  219. struct nfc_llcp_local *local;
  220. list_for_each_entry(local, &llcp_devices, list)
  221. if (local->dev == dev)
  222. return local;
  223. pr_debug("No device found\n");
  224. return NULL;
  225. }
  226. static char *wks[] = {
  227. NULL,
  228. NULL, /* SDP */
  229. "urn:nfc:sn:ip",
  230. "urn:nfc:sn:obex",
  231. "urn:nfc:sn:snep",
  232. };
  233. static int nfc_llcp_wks_sap(char *service_name, size_t service_name_len)
  234. {
  235. int sap, num_wks;
  236. pr_debug("%s\n", service_name);
  237. if (service_name == NULL)
  238. return -EINVAL;
  239. num_wks = ARRAY_SIZE(wks);
  240. for (sap = 0; sap < num_wks; sap++) {
  241. if (wks[sap] == NULL)
  242. continue;
  243. if (strncmp(wks[sap], service_name, service_name_len) == 0)
  244. return sap;
  245. }
  246. return -EINVAL;
  247. }
  248. static
  249. struct nfc_llcp_sock *nfc_llcp_sock_from_sn(struct nfc_llcp_local *local,
  250. u8 *sn, size_t sn_len)
  251. {
  252. struct sock *sk;
  253. struct nfc_llcp_sock *llcp_sock, *tmp_sock;
  254. pr_debug("sn %zd %p\n", sn_len, sn);
  255. if (sn == NULL || sn_len == 0)
  256. return NULL;
  257. read_lock(&local->sockets.lock);
  258. llcp_sock = NULL;
  259. sk_for_each(sk, &local->sockets.head) {
  260. tmp_sock = nfc_llcp_sock(sk);
  261. pr_debug("llcp sock %p\n", tmp_sock);
  262. if (tmp_sock->sk.sk_type == SOCK_STREAM &&
  263. tmp_sock->sk.sk_state != LLCP_LISTEN)
  264. continue;
  265. if (tmp_sock->sk.sk_type == SOCK_DGRAM &&
  266. tmp_sock->sk.sk_state != LLCP_BOUND)
  267. continue;
  268. if (tmp_sock->service_name == NULL ||
  269. tmp_sock->service_name_len == 0)
  270. continue;
  271. if (tmp_sock->service_name_len != sn_len)
  272. continue;
  273. if (memcmp(sn, tmp_sock->service_name, sn_len) == 0) {
  274. llcp_sock = tmp_sock;
  275. break;
  276. }
  277. }
  278. read_unlock(&local->sockets.lock);
  279. pr_debug("Found llcp sock %p\n", llcp_sock);
  280. return llcp_sock;
  281. }
  282. u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
  283. struct nfc_llcp_sock *sock)
  284. {
  285. mutex_lock(&local->sdp_lock);
  286. if (sock->service_name != NULL && sock->service_name_len > 0) {
  287. int ssap = nfc_llcp_wks_sap(sock->service_name,
  288. sock->service_name_len);
  289. if (ssap > 0) {
  290. pr_debug("WKS %d\n", ssap);
  291. /* This is a WKS, let's check if it's free */
  292. if (local->local_wks & BIT(ssap)) {
  293. mutex_unlock(&local->sdp_lock);
  294. return LLCP_SAP_MAX;
  295. }
  296. set_bit(ssap, &local->local_wks);
  297. mutex_unlock(&local->sdp_lock);
  298. return ssap;
  299. }
  300. /*
  301. * Check if there already is a non WKS socket bound
  302. * to this service name.
  303. */
  304. if (nfc_llcp_sock_from_sn(local, sock->service_name,
  305. sock->service_name_len) != NULL) {
  306. mutex_unlock(&local->sdp_lock);
  307. return LLCP_SAP_MAX;
  308. }
  309. mutex_unlock(&local->sdp_lock);
  310. return LLCP_SDP_UNBOUND;
  311. } else if (sock->ssap != 0 && sock->ssap < LLCP_WKS_NUM_SAP) {
  312. if (!test_bit(sock->ssap, &local->local_wks)) {
  313. set_bit(sock->ssap, &local->local_wks);
  314. mutex_unlock(&local->sdp_lock);
  315. return sock->ssap;
  316. }
  317. }
  318. mutex_unlock(&local->sdp_lock);
  319. return LLCP_SAP_MAX;
  320. }
  321. u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local)
  322. {
  323. u8 local_ssap;
  324. mutex_lock(&local->sdp_lock);
  325. local_ssap = find_first_zero_bit(&local->local_sap, LLCP_LOCAL_NUM_SAP);
  326. if (local_ssap == LLCP_LOCAL_NUM_SAP) {
  327. mutex_unlock(&local->sdp_lock);
  328. return LLCP_SAP_MAX;
  329. }
  330. set_bit(local_ssap, &local->local_sap);
  331. mutex_unlock(&local->sdp_lock);
  332. return local_ssap + LLCP_LOCAL_SAP_OFFSET;
  333. }
  334. void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap)
  335. {
  336. u8 local_ssap;
  337. unsigned long *sdp;
  338. if (ssap < LLCP_WKS_NUM_SAP) {
  339. local_ssap = ssap;
  340. sdp = &local->local_wks;
  341. } else if (ssap < LLCP_LOCAL_NUM_SAP) {
  342. atomic_t *client_cnt;
  343. local_ssap = ssap - LLCP_WKS_NUM_SAP;
  344. sdp = &local->local_sdp;
  345. client_cnt = &local->local_sdp_cnt[local_ssap];
  346. pr_debug("%d clients\n", atomic_read(client_cnt));
  347. mutex_lock(&local->sdp_lock);
  348. if (atomic_dec_and_test(client_cnt)) {
  349. struct nfc_llcp_sock *l_sock;
  350. pr_debug("No more clients for SAP %d\n", ssap);
  351. clear_bit(local_ssap, sdp);
  352. /* Find the listening sock and set it back to UNBOUND */
  353. l_sock = nfc_llcp_sock_get(local, ssap, LLCP_SAP_SDP);
  354. if (l_sock) {
  355. l_sock->ssap = LLCP_SDP_UNBOUND;
  356. nfc_llcp_sock_put(l_sock);
  357. }
  358. }
  359. mutex_unlock(&local->sdp_lock);
  360. return;
  361. } else if (ssap < LLCP_MAX_SAP) {
  362. local_ssap = ssap - LLCP_LOCAL_NUM_SAP;
  363. sdp = &local->local_sap;
  364. } else {
  365. return;
  366. }
  367. mutex_lock(&local->sdp_lock);
  368. clear_bit(local_ssap, sdp);
  369. mutex_unlock(&local->sdp_lock);
  370. }
  371. static u8 nfc_llcp_reserve_sdp_ssap(struct nfc_llcp_local *local)
  372. {
  373. u8 ssap;
  374. mutex_lock(&local->sdp_lock);
  375. ssap = find_first_zero_bit(&local->local_sdp, LLCP_SDP_NUM_SAP);
  376. if (ssap == LLCP_SDP_NUM_SAP) {
  377. mutex_unlock(&local->sdp_lock);
  378. return LLCP_SAP_MAX;
  379. }
  380. pr_debug("SDP ssap %d\n", LLCP_WKS_NUM_SAP + ssap);
  381. set_bit(ssap, &local->local_sdp);
  382. mutex_unlock(&local->sdp_lock);
  383. return LLCP_WKS_NUM_SAP + ssap;
  384. }
  385. static int nfc_llcp_build_gb(struct nfc_llcp_local *local)
  386. {
  387. u8 *gb_cur, version, version_length;
  388. u8 lto_length, wks_length, miux_length;
  389. u8 *version_tlv = NULL, *lto_tlv = NULL,
  390. *wks_tlv = NULL, *miux_tlv = NULL;
  391. __be16 wks = cpu_to_be16(local->local_wks);
  392. u8 gb_len = 0;
  393. int ret = 0;
  394. version = LLCP_VERSION_11;
  395. version_tlv = nfc_llcp_build_tlv(LLCP_TLV_VERSION, &version,
  396. 1, &version_length);
  397. if (!version_tlv) {
  398. ret = -ENOMEM;
  399. goto out;
  400. }
  401. gb_len += version_length;
  402. lto_tlv = nfc_llcp_build_tlv(LLCP_TLV_LTO, &local->lto, 1, &lto_length);
  403. if (!lto_tlv) {
  404. ret = -ENOMEM;
  405. goto out;
  406. }
  407. gb_len += lto_length;
  408. pr_debug("Local wks 0x%lx\n", local->local_wks);
  409. wks_tlv = nfc_llcp_build_tlv(LLCP_TLV_WKS, (u8 *)&wks, 2, &wks_length);
  410. if (!wks_tlv) {
  411. ret = -ENOMEM;
  412. goto out;
  413. }
  414. gb_len += wks_length;
  415. miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&local->miux, 0,
  416. &miux_length);
  417. if (!miux_tlv) {
  418. ret = -ENOMEM;
  419. goto out;
  420. }
  421. gb_len += miux_length;
  422. gb_len += ARRAY_SIZE(llcp_magic);
  423. if (gb_len > NFC_MAX_GT_LEN) {
  424. ret = -EINVAL;
  425. goto out;
  426. }
  427. gb_cur = local->gb;
  428. memcpy(gb_cur, llcp_magic, ARRAY_SIZE(llcp_magic));
  429. gb_cur += ARRAY_SIZE(llcp_magic);
  430. memcpy(gb_cur, version_tlv, version_length);
  431. gb_cur += version_length;
  432. memcpy(gb_cur, lto_tlv, lto_length);
  433. gb_cur += lto_length;
  434. memcpy(gb_cur, wks_tlv, wks_length);
  435. gb_cur += wks_length;
  436. memcpy(gb_cur, miux_tlv, miux_length);
  437. gb_cur += miux_length;
  438. local->gb_len = gb_len;
  439. out:
  440. kfree(version_tlv);
  441. kfree(lto_tlv);
  442. kfree(wks_tlv);
  443. kfree(miux_tlv);
  444. return ret;
  445. }
  446. u8 *nfc_llcp_general_bytes(struct nfc_dev *dev, size_t *general_bytes_len)
  447. {
  448. struct nfc_llcp_local *local;
  449. local = nfc_llcp_find_local(dev);
  450. if (local == NULL) {
  451. *general_bytes_len = 0;
  452. return NULL;
  453. }
  454. nfc_llcp_build_gb(local);
  455. *general_bytes_len = local->gb_len;
  456. return local->gb;
  457. }
  458. int nfc_llcp_set_remote_gb(struct nfc_dev *dev, u8 *gb, u8 gb_len)
  459. {
  460. struct nfc_llcp_local *local;
  461. if (gb_len < 3 || gb_len > NFC_MAX_GT_LEN)
  462. return -EINVAL;
  463. local = nfc_llcp_find_local(dev);
  464. if (local == NULL) {
  465. pr_err("No LLCP device\n");
  466. return -ENODEV;
  467. }
  468. memset(local->remote_gb, 0, NFC_MAX_GT_LEN);
  469. memcpy(local->remote_gb, gb, gb_len);
  470. local->remote_gb_len = gb_len;
  471. if (memcmp(local->remote_gb, llcp_magic, 3)) {
  472. pr_err("MAC does not support LLCP\n");
  473. return -EINVAL;
  474. }
  475. return nfc_llcp_parse_gb_tlv(local,
  476. &local->remote_gb[3],
  477. local->remote_gb_len - 3);
  478. }
  479. static u8 nfc_llcp_dsap(struct sk_buff *pdu)
  480. {
  481. return (pdu->data[0] & 0xfc) >> 2;
  482. }
  483. static u8 nfc_llcp_ptype(struct sk_buff *pdu)
  484. {
  485. return ((pdu->data[0] & 0x03) << 2) | ((pdu->data[1] & 0xc0) >> 6);
  486. }
  487. static u8 nfc_llcp_ssap(struct sk_buff *pdu)
  488. {
  489. return pdu->data[1] & 0x3f;
  490. }
  491. static u8 nfc_llcp_ns(struct sk_buff *pdu)
  492. {
  493. return pdu->data[2] >> 4;
  494. }
  495. static u8 nfc_llcp_nr(struct sk_buff *pdu)
  496. {
  497. return pdu->data[2] & 0xf;
  498. }
  499. static void nfc_llcp_set_nrns(struct nfc_llcp_sock *sock, struct sk_buff *pdu)
  500. {
  501. pdu->data[2] = (sock->send_n << 4) | (sock->recv_n);
  502. sock->send_n = (sock->send_n + 1) % 16;
  503. sock->recv_ack_n = (sock->recv_n - 1) % 16;
  504. }
  505. void nfc_llcp_send_to_raw_sock(struct nfc_llcp_local *local,
  506. struct sk_buff *skb, u8 direction)
  507. {
  508. struct sk_buff *skb_copy = NULL, *nskb;
  509. struct sock *sk;
  510. u8 *data;
  511. read_lock(&local->raw_sockets.lock);
  512. sk_for_each(sk, &local->raw_sockets.head) {
  513. if (sk->sk_state != LLCP_BOUND)
  514. continue;
  515. if (skb_copy == NULL) {
  516. skb_copy = __pskb_copy_fclone(skb, NFC_RAW_HEADER_SIZE,
  517. GFP_ATOMIC, true);
  518. if (skb_copy == NULL)
  519. continue;
  520. data = skb_push(skb_copy, NFC_RAW_HEADER_SIZE);
  521. data[0] = local->dev ? local->dev->idx : 0xFF;
  522. data[1] = direction & 0x01;
  523. data[1] |= (RAW_PAYLOAD_LLCP << 1);
  524. }
  525. nskb = skb_clone(skb_copy, GFP_ATOMIC);
  526. if (!nskb)
  527. continue;
  528. if (sock_queue_rcv_skb(sk, nskb))
  529. kfree_skb(nskb);
  530. }
  531. read_unlock(&local->raw_sockets.lock);
  532. kfree_skb(skb_copy);
  533. }
  534. static void nfc_llcp_tx_work(struct work_struct *work)
  535. {
  536. struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
  537. tx_work);
  538. struct sk_buff *skb;
  539. struct sock *sk;
  540. struct nfc_llcp_sock *llcp_sock;
  541. skb = skb_dequeue(&local->tx_queue);
  542. if (skb != NULL) {
  543. sk = skb->sk;
  544. llcp_sock = nfc_llcp_sock(sk);
  545. if (llcp_sock == NULL && nfc_llcp_ptype(skb) == LLCP_PDU_I) {
  546. kfree_skb(skb);
  547. nfc_llcp_send_symm(local->dev);
  548. } else if (llcp_sock && !llcp_sock->remote_ready) {
  549. skb_queue_head(&local->tx_queue, skb);
  550. nfc_llcp_send_symm(local->dev);
  551. } else {
  552. struct sk_buff *copy_skb = NULL;
  553. u8 ptype = nfc_llcp_ptype(skb);
  554. int ret;
  555. pr_debug("Sending pending skb\n");
  556. print_hex_dump_debug("LLCP Tx: ", DUMP_PREFIX_OFFSET,
  557. 16, 1, skb->data, skb->len, true);
  558. if (ptype == LLCP_PDU_DISC && sk != NULL &&
  559. sk->sk_state == LLCP_DISCONNECTING) {
  560. nfc_llcp_sock_unlink(&local->sockets, sk);
  561. sock_orphan(sk);
  562. sock_put(sk);
  563. }
  564. if (ptype == LLCP_PDU_I)
  565. copy_skb = skb_copy(skb, GFP_ATOMIC);
  566. __net_timestamp(skb);
  567. nfc_llcp_send_to_raw_sock(local, skb,
  568. NFC_DIRECTION_TX);
  569. ret = nfc_data_exchange(local->dev, local->target_idx,
  570. skb, nfc_llcp_recv, local);
  571. if (ret) {
  572. kfree_skb(copy_skb);
  573. goto out;
  574. }
  575. if (ptype == LLCP_PDU_I && copy_skb)
  576. skb_queue_tail(&llcp_sock->tx_pending_queue,
  577. copy_skb);
  578. }
  579. } else {
  580. nfc_llcp_send_symm(local->dev);
  581. }
  582. out:
  583. mod_timer(&local->link_timer,
  584. jiffies + msecs_to_jiffies(2 * local->remote_lto));
  585. }
  586. static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
  587. u8 ssap)
  588. {
  589. struct sock *sk;
  590. struct nfc_llcp_sock *llcp_sock;
  591. read_lock(&local->connecting_sockets.lock);
  592. sk_for_each(sk, &local->connecting_sockets.head) {
  593. llcp_sock = nfc_llcp_sock(sk);
  594. if (llcp_sock->ssap == ssap) {
  595. sock_hold(&llcp_sock->sk);
  596. goto out;
  597. }
  598. }
  599. llcp_sock = NULL;
  600. out:
  601. read_unlock(&local->connecting_sockets.lock);
  602. return llcp_sock;
  603. }
  604. static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
  605. u8 *sn, size_t sn_len)
  606. {
  607. struct nfc_llcp_sock *llcp_sock;
  608. llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
  609. if (llcp_sock == NULL)
  610. return NULL;
  611. sock_hold(&llcp_sock->sk);
  612. return llcp_sock;
  613. }
  614. static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
  615. {
  616. u8 *tlv = &skb->data[2], type, length;
  617. size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
  618. while (offset < tlv_array_len) {
  619. type = tlv[0];
  620. length = tlv[1];
  621. pr_debug("type 0x%x length %d\n", type, length);
  622. if (type == LLCP_TLV_SN) {
  623. *sn_len = length;
  624. return &tlv[2];
  625. }
  626. offset += length + 2;
  627. tlv += length + 2;
  628. }
  629. return NULL;
  630. }
  631. static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
  632. struct sk_buff *skb)
  633. {
  634. struct nfc_llcp_sock *llcp_sock;
  635. struct nfc_llcp_ui_cb *ui_cb;
  636. u8 dsap, ssap;
  637. dsap = nfc_llcp_dsap(skb);
  638. ssap = nfc_llcp_ssap(skb);
  639. ui_cb = nfc_llcp_ui_skb_cb(skb);
  640. ui_cb->dsap = dsap;
  641. ui_cb->ssap = ssap;
  642. pr_debug("%d %d\n", dsap, ssap);
  643. /* We're looking for a bound socket, not a client one */
  644. llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
  645. if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM)
  646. return;
  647. /* There is no sequence with UI frames */
  648. skb_pull(skb, LLCP_HEADER_SIZE);
  649. if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
  650. /*
  651. * UI frames will be freed from the socket layer, so we
  652. * need to keep them alive until someone receives them.
  653. */
  654. skb_get(skb);
  655. } else {
  656. pr_err("Receive queue is full\n");
  657. }
  658. nfc_llcp_sock_put(llcp_sock);
  659. }
  660. static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
  661. struct sk_buff *skb)
  662. {
  663. struct sock *new_sk, *parent;
  664. struct nfc_llcp_sock *sock, *new_sock;
  665. u8 dsap, ssap, reason;
  666. dsap = nfc_llcp_dsap(skb);
  667. ssap = nfc_llcp_ssap(skb);
  668. pr_debug("%d %d\n", dsap, ssap);
  669. if (dsap != LLCP_SAP_SDP) {
  670. sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
  671. if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) {
  672. reason = LLCP_DM_NOBOUND;
  673. goto fail;
  674. }
  675. } else {
  676. u8 *sn;
  677. size_t sn_len;
  678. sn = nfc_llcp_connect_sn(skb, &sn_len);
  679. if (sn == NULL) {
  680. reason = LLCP_DM_NOBOUND;
  681. goto fail;
  682. }
  683. pr_debug("Service name length %zu\n", sn_len);
  684. sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
  685. if (sock == NULL) {
  686. reason = LLCP_DM_NOBOUND;
  687. goto fail;
  688. }
  689. }
  690. lock_sock(&sock->sk);
  691. parent = &sock->sk;
  692. if (sk_acceptq_is_full(parent)) {
  693. reason = LLCP_DM_REJ;
  694. release_sock(&sock->sk);
  695. sock_put(&sock->sk);
  696. goto fail;
  697. }
  698. if (sock->ssap == LLCP_SDP_UNBOUND) {
  699. u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
  700. pr_debug("First client, reserving %d\n", ssap);
  701. if (ssap == LLCP_SAP_MAX) {
  702. reason = LLCP_DM_REJ;
  703. release_sock(&sock->sk);
  704. sock_put(&sock->sk);
  705. goto fail;
  706. }
  707. sock->ssap = ssap;
  708. }
  709. new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC, 0);
  710. if (new_sk == NULL) {
  711. reason = LLCP_DM_REJ;
  712. release_sock(&sock->sk);
  713. sock_put(&sock->sk);
  714. goto fail;
  715. }
  716. new_sock = nfc_llcp_sock(new_sk);
  717. new_sock->dev = local->dev;
  718. new_sock->local = nfc_llcp_local_get(local);
  719. new_sock->rw = sock->rw;
  720. new_sock->miux = sock->miux;
  721. new_sock->nfc_protocol = sock->nfc_protocol;
  722. new_sock->dsap = ssap;
  723. new_sock->target_idx = local->target_idx;
  724. new_sock->parent = parent;
  725. new_sock->ssap = sock->ssap;
  726. if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) {
  727. atomic_t *client_count;
  728. pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock);
  729. client_count =
  730. &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];
  731. atomic_inc(client_count);
  732. new_sock->reserved_ssap = sock->ssap;
  733. }
  734. nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
  735. skb->len - LLCP_HEADER_SIZE);
  736. pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
  737. nfc_llcp_sock_link(&local->sockets, new_sk);
  738. nfc_llcp_accept_enqueue(&sock->sk, new_sk);
  739. nfc_get_device(local->dev->idx);
  740. new_sk->sk_state = LLCP_CONNECTED;
  741. /* Wake the listening processes */
  742. parent->sk_data_ready(parent);
  743. /* Send CC */
  744. nfc_llcp_send_cc(new_sock);
  745. release_sock(&sock->sk);
  746. sock_put(&sock->sk);
  747. return;
  748. fail:
  749. /* Send DM */
  750. nfc_llcp_send_dm(local, dsap, ssap, reason);
  751. }
  752. int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
  753. {
  754. int nr_frames = 0;
  755. struct nfc_llcp_local *local = sock->local;
  756. pr_debug("Remote ready %d tx queue len %d remote rw %d",
  757. sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
  758. sock->remote_rw);
  759. /* Try to queue some I frames for transmission */
  760. while (sock->remote_ready &&
  761. skb_queue_len(&sock->tx_pending_queue) < sock->remote_rw) {
  762. struct sk_buff *pdu;
  763. pdu = skb_dequeue(&sock->tx_queue);
  764. if (pdu == NULL)
  765. break;
  766. /* Update N(S)/N(R) */
  767. nfc_llcp_set_nrns(sock, pdu);
  768. skb_queue_tail(&local->tx_queue, pdu);
  769. nr_frames++;
  770. }
  771. return nr_frames;
  772. }
  773. static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
  774. struct sk_buff *skb)
  775. {
  776. struct nfc_llcp_sock *llcp_sock;
  777. struct sock *sk;
  778. u8 dsap, ssap, ptype, ns, nr;
  779. ptype = nfc_llcp_ptype(skb);
  780. dsap = nfc_llcp_dsap(skb);
  781. ssap = nfc_llcp_ssap(skb);
  782. ns = nfc_llcp_ns(skb);
  783. nr = nfc_llcp_nr(skb);
  784. pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
  785. llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
  786. if (llcp_sock == NULL) {
  787. nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
  788. return;
  789. }
  790. sk = &llcp_sock->sk;
  791. lock_sock(sk);
  792. if (sk->sk_state == LLCP_CLOSED) {
  793. release_sock(sk);
  794. nfc_llcp_sock_put(llcp_sock);
  795. }
  796. /* Pass the payload upstream */
  797. if (ptype == LLCP_PDU_I) {
  798. pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
  799. if (ns == llcp_sock->recv_n)
  800. llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
  801. else
  802. pr_err("Received out of sequence I PDU\n");
  803. skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
  804. if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
  805. /*
  806. * I frames will be freed from the socket layer, so we
  807. * need to keep them alive until someone receives them.
  808. */
  809. skb_get(skb);
  810. } else {
  811. pr_err("Receive queue is full\n");
  812. }
  813. }
  814. /* Remove skbs from the pending queue */
  815. if (llcp_sock->send_ack_n != nr) {
  816. struct sk_buff *s, *tmp;
  817. u8 n;
  818. llcp_sock->send_ack_n = nr;
  819. /* Remove and free all skbs until ns == nr */
  820. skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
  821. n = nfc_llcp_ns(s);
  822. skb_unlink(s, &llcp_sock->tx_pending_queue);
  823. kfree_skb(s);
  824. if (n == nr)
  825. break;
  826. }
  827. /* Re-queue the remaining skbs for transmission */
  828. skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
  829. s, tmp) {
  830. skb_unlink(s, &llcp_sock->tx_pending_queue);
  831. skb_queue_head(&local->tx_queue, s);
  832. }
  833. }
  834. if (ptype == LLCP_PDU_RR)
  835. llcp_sock->remote_ready = true;
  836. else if (ptype == LLCP_PDU_RNR)
  837. llcp_sock->remote_ready = false;
  838. if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
  839. nfc_llcp_send_rr(llcp_sock);
  840. release_sock(sk);
  841. nfc_llcp_sock_put(llcp_sock);
  842. }
  843. static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
  844. struct sk_buff *skb)
  845. {
  846. struct nfc_llcp_sock *llcp_sock;
  847. struct sock *sk;
  848. u8 dsap, ssap;
  849. dsap = nfc_llcp_dsap(skb);
  850. ssap = nfc_llcp_ssap(skb);
  851. if ((dsap == 0) && (ssap == 0)) {
  852. pr_debug("Connection termination");
  853. nfc_dep_link_down(local->dev);
  854. return;
  855. }
  856. llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
  857. if (llcp_sock == NULL) {
  858. nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
  859. return;
  860. }
  861. sk = &llcp_sock->sk;
  862. lock_sock(sk);
  863. nfc_llcp_socket_purge(llcp_sock);
  864. if (sk->sk_state == LLCP_CLOSED) {
  865. release_sock(sk);
  866. nfc_llcp_sock_put(llcp_sock);
  867. }
  868. if (sk->sk_state == LLCP_CONNECTED) {
  869. nfc_put_device(local->dev);
  870. sk->sk_state = LLCP_CLOSED;
  871. sk->sk_state_change(sk);
  872. }
  873. nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
  874. release_sock(sk);
  875. nfc_llcp_sock_put(llcp_sock);
  876. }
  877. static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
  878. {
  879. struct nfc_llcp_sock *llcp_sock;
  880. struct sock *sk;
  881. u8 dsap, ssap;
  882. dsap = nfc_llcp_dsap(skb);
  883. ssap = nfc_llcp_ssap(skb);
  884. llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
  885. if (llcp_sock == NULL) {
  886. pr_err("Invalid CC\n");
  887. nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
  888. return;
  889. }
  890. sk = &llcp_sock->sk;
  891. /* Unlink from connecting and link to the client array */
  892. nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
  893. nfc_llcp_sock_link(&local->sockets, sk);
  894. llcp_sock->dsap = ssap;
  895. nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
  896. skb->len - LLCP_HEADER_SIZE);
  897. sk->sk_state = LLCP_CONNECTED;
  898. sk->sk_state_change(sk);
  899. nfc_llcp_sock_put(llcp_sock);
  900. }
  901. static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
  902. {
  903. struct nfc_llcp_sock *llcp_sock;
  904. struct sock *sk;
  905. u8 dsap, ssap, reason;
  906. dsap = nfc_llcp_dsap(skb);
  907. ssap = nfc_llcp_ssap(skb);
  908. reason = skb->data[2];
  909. pr_debug("%d %d reason %d\n", ssap, dsap, reason);
  910. switch (reason) {
  911. case LLCP_DM_NOBOUND:
  912. case LLCP_DM_REJ:
  913. llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
  914. break;
  915. default:
  916. llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
  917. break;
  918. }
  919. if (llcp_sock == NULL) {
  920. pr_debug("Already closed\n");
  921. return;
  922. }
  923. sk = &llcp_sock->sk;
  924. sk->sk_err = ENXIO;
  925. sk->sk_state = LLCP_CLOSED;
  926. sk->sk_state_change(sk);
  927. nfc_llcp_sock_put(llcp_sock);
  928. }
  929. static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
  930. struct sk_buff *skb)
  931. {
  932. struct nfc_llcp_sock *llcp_sock;
  933. u8 dsap, ssap, *tlv, type, length, tid, sap;
  934. u16 tlv_len, offset;
  935. char *service_name;
  936. size_t service_name_len;
  937. struct nfc_llcp_sdp_tlv *sdp;
  938. HLIST_HEAD(llc_sdres_list);
  939. size_t sdres_tlvs_len;
  940. HLIST_HEAD(nl_sdres_list);
  941. dsap = nfc_llcp_dsap(skb);
  942. ssap = nfc_llcp_ssap(skb);
  943. pr_debug("%d %d\n", dsap, ssap);
  944. if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
  945. pr_err("Wrong SNL SAP\n");
  946. return;
  947. }
  948. tlv = &skb->data[LLCP_HEADER_SIZE];
  949. tlv_len = skb->len - LLCP_HEADER_SIZE;
  950. offset = 0;
  951. sdres_tlvs_len = 0;
  952. while (offset < tlv_len) {
  953. type = tlv[0];
  954. length = tlv[1];
  955. switch (type) {
  956. case LLCP_TLV_SDREQ:
  957. tid = tlv[2];
  958. service_name = (char *) &tlv[3];
  959. service_name_len = length - 1;
  960. pr_debug("Looking for %.16s\n", service_name);
  961. if (service_name_len == strlen("urn:nfc:sn:sdp") &&
  962. !strncmp(service_name, "urn:nfc:sn:sdp",
  963. service_name_len)) {
  964. sap = 1;
  965. goto add_snl;
  966. }
  967. llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
  968. service_name_len);
  969. if (!llcp_sock) {
  970. sap = 0;
  971. goto add_snl;
  972. }
  973. /*
  974. * We found a socket but its ssap has not been reserved
  975. * yet. We need to assign it for good and send a reply.
  976. * The ssap will be freed when the socket is closed.
  977. */
  978. if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
  979. atomic_t *client_count;
  980. sap = nfc_llcp_reserve_sdp_ssap(local);
  981. pr_debug("Reserving %d\n", sap);
  982. if (sap == LLCP_SAP_MAX) {
  983. sap = 0;
  984. goto add_snl;
  985. }
  986. client_count =
  987. &local->local_sdp_cnt[sap -
  988. LLCP_WKS_NUM_SAP];
  989. atomic_inc(client_count);
  990. llcp_sock->ssap = sap;
  991. llcp_sock->reserved_ssap = sap;
  992. } else {
  993. sap = llcp_sock->ssap;
  994. }
  995. pr_debug("%p %d\n", llcp_sock, sap);
  996. add_snl:
  997. sdp = nfc_llcp_build_sdres_tlv(tid, sap);
  998. if (sdp == NULL)
  999. goto exit;
  1000. sdres_tlvs_len += sdp->tlv_len;
  1001. hlist_add_head(&sdp->node, &llc_sdres_list);
  1002. break;
  1003. case LLCP_TLV_SDRES:
  1004. mutex_lock(&local->sdreq_lock);
  1005. pr_debug("LLCP_TLV_SDRES: searching tid %d\n", tlv[2]);
  1006. hlist_for_each_entry(sdp, &local->pending_sdreqs, node) {
  1007. if (sdp->tid != tlv[2])
  1008. continue;
  1009. sdp->sap = tlv[3];
  1010. pr_debug("Found: uri=%s, sap=%d\n",
  1011. sdp->uri, sdp->sap);
  1012. hlist_del(&sdp->node);
  1013. hlist_add_head(&sdp->node, &nl_sdres_list);
  1014. break;
  1015. }
  1016. mutex_unlock(&local->sdreq_lock);
  1017. break;
  1018. default:
  1019. pr_err("Invalid SNL tlv value 0x%x\n", type);
  1020. break;
  1021. }
  1022. offset += length + 2;
  1023. tlv += length + 2;
  1024. }
  1025. exit:
  1026. if (!hlist_empty(&nl_sdres_list))
  1027. nfc_genl_llc_send_sdres(local->dev, &nl_sdres_list);
  1028. if (!hlist_empty(&llc_sdres_list))
  1029. nfc_llcp_send_snl_sdres(local, &llc_sdres_list, sdres_tlvs_len);
  1030. }
  1031. static void nfc_llcp_recv_agf(struct nfc_llcp_local *local, struct sk_buff *skb)
  1032. {
  1033. u8 ptype;
  1034. u16 pdu_len;
  1035. struct sk_buff *new_skb;
  1036. if (skb->len <= LLCP_HEADER_SIZE) {
  1037. pr_err("Malformed AGF PDU\n");
  1038. return;
  1039. }
  1040. skb_pull(skb, LLCP_HEADER_SIZE);
  1041. while (skb->len > LLCP_AGF_PDU_HEADER_SIZE) {
  1042. pdu_len = skb->data[0] << 8 | skb->data[1];
  1043. skb_pull(skb, LLCP_AGF_PDU_HEADER_SIZE);
  1044. if (pdu_len < LLCP_HEADER_SIZE || pdu_len > skb->len) {
  1045. pr_err("Malformed AGF PDU\n");
  1046. return;
  1047. }
  1048. ptype = nfc_llcp_ptype(skb);
  1049. if (ptype == LLCP_PDU_SYMM || ptype == LLCP_PDU_AGF)
  1050. goto next;
  1051. new_skb = nfc_alloc_recv_skb(pdu_len, GFP_KERNEL);
  1052. if (new_skb == NULL) {
  1053. pr_err("Could not allocate PDU\n");
  1054. return;
  1055. }
  1056. skb_put_data(new_skb, skb->data, pdu_len);
  1057. nfc_llcp_rx_skb(local, new_skb);
  1058. kfree_skb(new_skb);
  1059. next:
  1060. skb_pull(skb, pdu_len);
  1061. }
  1062. }
  1063. static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb)
  1064. {
  1065. u8 dsap, ssap, ptype;
  1066. ptype = nfc_llcp_ptype(skb);
  1067. dsap = nfc_llcp_dsap(skb);
  1068. ssap = nfc_llcp_ssap(skb);
  1069. pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
  1070. if (ptype != LLCP_PDU_SYMM)
  1071. print_hex_dump_debug("LLCP Rx: ", DUMP_PREFIX_OFFSET, 16, 1,
  1072. skb->data, skb->len, true);
  1073. switch (ptype) {
  1074. case LLCP_PDU_SYMM:
  1075. pr_debug("SYMM\n");
  1076. break;
  1077. case LLCP_PDU_UI:
  1078. pr_debug("UI\n");
  1079. nfc_llcp_recv_ui(local, skb);
  1080. break;
  1081. case LLCP_PDU_CONNECT:
  1082. pr_debug("CONNECT\n");
  1083. nfc_llcp_recv_connect(local, skb);
  1084. break;
  1085. case LLCP_PDU_DISC:
  1086. pr_debug("DISC\n");
  1087. nfc_llcp_recv_disc(local, skb);
  1088. break;
  1089. case LLCP_PDU_CC:
  1090. pr_debug("CC\n");
  1091. nfc_llcp_recv_cc(local, skb);
  1092. break;
  1093. case LLCP_PDU_DM:
  1094. pr_debug("DM\n");
  1095. nfc_llcp_recv_dm(local, skb);
  1096. break;
  1097. case LLCP_PDU_SNL:
  1098. pr_debug("SNL\n");
  1099. nfc_llcp_recv_snl(local, skb);
  1100. break;
  1101. case LLCP_PDU_I:
  1102. case LLCP_PDU_RR:
  1103. case LLCP_PDU_RNR:
  1104. pr_debug("I frame\n");
  1105. nfc_llcp_recv_hdlc(local, skb);
  1106. break;
  1107. case LLCP_PDU_AGF:
  1108. pr_debug("AGF frame\n");
  1109. nfc_llcp_recv_agf(local, skb);
  1110. break;
  1111. }
  1112. }
  1113. static void nfc_llcp_rx_work(struct work_struct *work)
  1114. {
  1115. struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
  1116. rx_work);
  1117. struct sk_buff *skb;
  1118. skb = local->rx_pending;
  1119. if (skb == NULL) {
  1120. pr_debug("No pending SKB\n");
  1121. return;
  1122. }
  1123. __net_timestamp(skb);
  1124. nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_RX);
  1125. nfc_llcp_rx_skb(local, skb);
  1126. schedule_work(&local->tx_work);
  1127. kfree_skb(local->rx_pending);
  1128. local->rx_pending = NULL;
  1129. }
  1130. static void __nfc_llcp_recv(struct nfc_llcp_local *local, struct sk_buff *skb)
  1131. {
  1132. local->rx_pending = skb;
  1133. del_timer(&local->link_timer);
  1134. schedule_work(&local->rx_work);
  1135. }
  1136. void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
  1137. {
  1138. struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
  1139. pr_debug("Received an LLCP PDU\n");
  1140. if (err < 0) {
  1141. pr_err("err %d\n", err);
  1142. return;
  1143. }
  1144. __nfc_llcp_recv(local, skb);
  1145. }
  1146. int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
  1147. {
  1148. struct nfc_llcp_local *local;
  1149. local = nfc_llcp_find_local(dev);
  1150. if (local == NULL) {
  1151. kfree_skb(skb);
  1152. return -ENODEV;
  1153. }
  1154. __nfc_llcp_recv(local, skb);
  1155. return 0;
  1156. }
  1157. void nfc_llcp_mac_is_down(struct nfc_dev *dev)
  1158. {
  1159. struct nfc_llcp_local *local;
  1160. local = nfc_llcp_find_local(dev);
  1161. if (local == NULL)
  1162. return;
  1163. local->remote_miu = LLCP_DEFAULT_MIU;
  1164. local->remote_lto = LLCP_DEFAULT_LTO;
  1165. /* Close and purge all existing sockets */
  1166. nfc_llcp_socket_release(local, true, 0);
  1167. }
  1168. void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
  1169. u8 comm_mode, u8 rf_mode)
  1170. {
  1171. struct nfc_llcp_local *local;
  1172. pr_debug("rf mode %d\n", rf_mode);
  1173. local = nfc_llcp_find_local(dev);
  1174. if (local == NULL)
  1175. return;
  1176. local->target_idx = target_idx;
  1177. local->comm_mode = comm_mode;
  1178. local->rf_mode = rf_mode;
  1179. if (rf_mode == NFC_RF_INITIATOR) {
  1180. pr_debug("Queueing Tx work\n");
  1181. schedule_work(&local->tx_work);
  1182. } else {
  1183. mod_timer(&local->link_timer,
  1184. jiffies + msecs_to_jiffies(local->remote_lto));
  1185. }
  1186. }
  1187. int nfc_llcp_register_device(struct nfc_dev *ndev)
  1188. {
  1189. struct nfc_llcp_local *local;
  1190. local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
  1191. if (local == NULL)
  1192. return -ENOMEM;
  1193. local->dev = ndev;
  1194. INIT_LIST_HEAD(&local->list);
  1195. kref_init(&local->ref);
  1196. mutex_init(&local->sdp_lock);
  1197. init_timer(&local->link_timer);
  1198. local->link_timer.data = (unsigned long) local;
  1199. local->link_timer.function = nfc_llcp_symm_timer;
  1200. skb_queue_head_init(&local->tx_queue);
  1201. INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
  1202. local->rx_pending = NULL;
  1203. INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
  1204. INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
  1205. rwlock_init(&local->sockets.lock);
  1206. rwlock_init(&local->connecting_sockets.lock);
  1207. rwlock_init(&local->raw_sockets.lock);
  1208. local->lto = 150; /* 1500 ms */
  1209. local->rw = LLCP_MAX_RW;
  1210. local->miux = cpu_to_be16(LLCP_MAX_MIUX);
  1211. local->local_wks = 0x1; /* LLC Link Management */
  1212. nfc_llcp_build_gb(local);
  1213. local->remote_miu = LLCP_DEFAULT_MIU;
  1214. local->remote_lto = LLCP_DEFAULT_LTO;
  1215. mutex_init(&local->sdreq_lock);
  1216. INIT_HLIST_HEAD(&local->pending_sdreqs);
  1217. init_timer(&local->sdreq_timer);
  1218. local->sdreq_timer.data = (unsigned long) local;
  1219. local->sdreq_timer.function = nfc_llcp_sdreq_timer;
  1220. INIT_WORK(&local->sdreq_timeout_work, nfc_llcp_sdreq_timeout_work);
  1221. list_add(&local->list, &llcp_devices);
  1222. return 0;
  1223. }
  1224. void nfc_llcp_unregister_device(struct nfc_dev *dev)
  1225. {
  1226. struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
  1227. if (local == NULL) {
  1228. pr_debug("No such device\n");
  1229. return;
  1230. }
  1231. local_cleanup(local);
  1232. nfc_llcp_local_put(local);
  1233. }
  1234. int __init nfc_llcp_init(void)
  1235. {
  1236. return nfc_llcp_sock_init();
  1237. }
  1238. void nfc_llcp_exit(void)
  1239. {
  1240. nfc_llcp_sock_exit();
  1241. }