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(KERN_DEBUG, "LLCP Tx: ",
  557. DUMP_PREFIX_OFFSET, 16, 1,
  558. skb->data, skb->len, true);
  559. if (ptype == LLCP_PDU_DISC && sk != NULL &&
  560. sk->sk_state == LLCP_DISCONNECTING) {
  561. nfc_llcp_sock_unlink(&local->sockets, sk);
  562. sock_orphan(sk);
  563. sock_put(sk);
  564. }
  565. if (ptype == LLCP_PDU_I)
  566. copy_skb = skb_copy(skb, GFP_ATOMIC);
  567. __net_timestamp(skb);
  568. nfc_llcp_send_to_raw_sock(local, skb,
  569. NFC_DIRECTION_TX);
  570. ret = nfc_data_exchange(local->dev, local->target_idx,
  571. skb, nfc_llcp_recv, local);
  572. if (ret) {
  573. kfree_skb(copy_skb);
  574. goto out;
  575. }
  576. if (ptype == LLCP_PDU_I && copy_skb)
  577. skb_queue_tail(&llcp_sock->tx_pending_queue,
  578. copy_skb);
  579. }
  580. } else {
  581. nfc_llcp_send_symm(local->dev);
  582. }
  583. out:
  584. mod_timer(&local->link_timer,
  585. jiffies + msecs_to_jiffies(2 * local->remote_lto));
  586. }
  587. static struct nfc_llcp_sock *nfc_llcp_connecting_sock_get(struct nfc_llcp_local *local,
  588. u8 ssap)
  589. {
  590. struct sock *sk;
  591. struct nfc_llcp_sock *llcp_sock;
  592. read_lock(&local->connecting_sockets.lock);
  593. sk_for_each(sk, &local->connecting_sockets.head) {
  594. llcp_sock = nfc_llcp_sock(sk);
  595. if (llcp_sock->ssap == ssap) {
  596. sock_hold(&llcp_sock->sk);
  597. goto out;
  598. }
  599. }
  600. llcp_sock = NULL;
  601. out:
  602. read_unlock(&local->connecting_sockets.lock);
  603. return llcp_sock;
  604. }
  605. static struct nfc_llcp_sock *nfc_llcp_sock_get_sn(struct nfc_llcp_local *local,
  606. u8 *sn, size_t sn_len)
  607. {
  608. struct nfc_llcp_sock *llcp_sock;
  609. llcp_sock = nfc_llcp_sock_from_sn(local, sn, sn_len);
  610. if (llcp_sock == NULL)
  611. return NULL;
  612. sock_hold(&llcp_sock->sk);
  613. return llcp_sock;
  614. }
  615. static u8 *nfc_llcp_connect_sn(struct sk_buff *skb, size_t *sn_len)
  616. {
  617. u8 *tlv = &skb->data[2], type, length;
  618. size_t tlv_array_len = skb->len - LLCP_HEADER_SIZE, offset = 0;
  619. while (offset < tlv_array_len) {
  620. type = tlv[0];
  621. length = tlv[1];
  622. pr_debug("type 0x%x length %d\n", type, length);
  623. if (type == LLCP_TLV_SN) {
  624. *sn_len = length;
  625. return &tlv[2];
  626. }
  627. offset += length + 2;
  628. tlv += length + 2;
  629. }
  630. return NULL;
  631. }
  632. static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
  633. struct sk_buff *skb)
  634. {
  635. struct nfc_llcp_sock *llcp_sock;
  636. struct nfc_llcp_ui_cb *ui_cb;
  637. u8 dsap, ssap;
  638. dsap = nfc_llcp_dsap(skb);
  639. ssap = nfc_llcp_ssap(skb);
  640. ui_cb = nfc_llcp_ui_skb_cb(skb);
  641. ui_cb->dsap = dsap;
  642. ui_cb->ssap = ssap;
  643. pr_debug("%d %d\n", dsap, ssap);
  644. /* We're looking for a bound socket, not a client one */
  645. llcp_sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
  646. if (llcp_sock == NULL || llcp_sock->sk.sk_type != SOCK_DGRAM)
  647. return;
  648. /* There is no sequence with UI frames */
  649. skb_pull(skb, LLCP_HEADER_SIZE);
  650. if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
  651. /*
  652. * UI frames will be freed from the socket layer, so we
  653. * need to keep them alive until someone receives them.
  654. */
  655. skb_get(skb);
  656. } else {
  657. pr_err("Receive queue is full\n");
  658. }
  659. nfc_llcp_sock_put(llcp_sock);
  660. }
  661. static void nfc_llcp_recv_connect(struct nfc_llcp_local *local,
  662. struct sk_buff *skb)
  663. {
  664. struct sock *new_sk, *parent;
  665. struct nfc_llcp_sock *sock, *new_sock;
  666. u8 dsap, ssap, reason;
  667. dsap = nfc_llcp_dsap(skb);
  668. ssap = nfc_llcp_ssap(skb);
  669. pr_debug("%d %d\n", dsap, ssap);
  670. if (dsap != LLCP_SAP_SDP) {
  671. sock = nfc_llcp_sock_get(local, dsap, LLCP_SAP_SDP);
  672. if (sock == NULL || sock->sk.sk_state != LLCP_LISTEN) {
  673. reason = LLCP_DM_NOBOUND;
  674. goto fail;
  675. }
  676. } else {
  677. u8 *sn;
  678. size_t sn_len;
  679. sn = nfc_llcp_connect_sn(skb, &sn_len);
  680. if (sn == NULL) {
  681. reason = LLCP_DM_NOBOUND;
  682. goto fail;
  683. }
  684. pr_debug("Service name length %zu\n", sn_len);
  685. sock = nfc_llcp_sock_get_sn(local, sn, sn_len);
  686. if (sock == NULL) {
  687. reason = LLCP_DM_NOBOUND;
  688. goto fail;
  689. }
  690. }
  691. lock_sock(&sock->sk);
  692. parent = &sock->sk;
  693. if (sk_acceptq_is_full(parent)) {
  694. reason = LLCP_DM_REJ;
  695. release_sock(&sock->sk);
  696. sock_put(&sock->sk);
  697. goto fail;
  698. }
  699. if (sock->ssap == LLCP_SDP_UNBOUND) {
  700. u8 ssap = nfc_llcp_reserve_sdp_ssap(local);
  701. pr_debug("First client, reserving %d\n", ssap);
  702. if (ssap == LLCP_SAP_MAX) {
  703. reason = LLCP_DM_REJ;
  704. release_sock(&sock->sk);
  705. sock_put(&sock->sk);
  706. goto fail;
  707. }
  708. sock->ssap = ssap;
  709. }
  710. new_sk = nfc_llcp_sock_alloc(NULL, parent->sk_type, GFP_ATOMIC, 0);
  711. if (new_sk == NULL) {
  712. reason = LLCP_DM_REJ;
  713. release_sock(&sock->sk);
  714. sock_put(&sock->sk);
  715. goto fail;
  716. }
  717. new_sock = nfc_llcp_sock(new_sk);
  718. new_sock->dev = local->dev;
  719. new_sock->local = nfc_llcp_local_get(local);
  720. new_sock->rw = sock->rw;
  721. new_sock->miux = sock->miux;
  722. new_sock->nfc_protocol = sock->nfc_protocol;
  723. new_sock->dsap = ssap;
  724. new_sock->target_idx = local->target_idx;
  725. new_sock->parent = parent;
  726. new_sock->ssap = sock->ssap;
  727. if (sock->ssap < LLCP_LOCAL_NUM_SAP && sock->ssap >= LLCP_WKS_NUM_SAP) {
  728. atomic_t *client_count;
  729. pr_debug("reserved_ssap %d for %p\n", sock->ssap, new_sock);
  730. client_count =
  731. &local->local_sdp_cnt[sock->ssap - LLCP_WKS_NUM_SAP];
  732. atomic_inc(client_count);
  733. new_sock->reserved_ssap = sock->ssap;
  734. }
  735. nfc_llcp_parse_connection_tlv(new_sock, &skb->data[LLCP_HEADER_SIZE],
  736. skb->len - LLCP_HEADER_SIZE);
  737. pr_debug("new sock %p sk %p\n", new_sock, &new_sock->sk);
  738. nfc_llcp_sock_link(&local->sockets, new_sk);
  739. nfc_llcp_accept_enqueue(&sock->sk, new_sk);
  740. nfc_get_device(local->dev->idx);
  741. new_sk->sk_state = LLCP_CONNECTED;
  742. /* Wake the listening processes */
  743. parent->sk_data_ready(parent);
  744. /* Send CC */
  745. nfc_llcp_send_cc(new_sock);
  746. release_sock(&sock->sk);
  747. sock_put(&sock->sk);
  748. return;
  749. fail:
  750. /* Send DM */
  751. nfc_llcp_send_dm(local, dsap, ssap, reason);
  752. }
  753. int nfc_llcp_queue_i_frames(struct nfc_llcp_sock *sock)
  754. {
  755. int nr_frames = 0;
  756. struct nfc_llcp_local *local = sock->local;
  757. pr_debug("Remote ready %d tx queue len %d remote rw %d",
  758. sock->remote_ready, skb_queue_len(&sock->tx_pending_queue),
  759. sock->remote_rw);
  760. /* Try to queue some I frames for transmission */
  761. while (sock->remote_ready &&
  762. skb_queue_len(&sock->tx_pending_queue) < sock->remote_rw) {
  763. struct sk_buff *pdu;
  764. pdu = skb_dequeue(&sock->tx_queue);
  765. if (pdu == NULL)
  766. break;
  767. /* Update N(S)/N(R) */
  768. nfc_llcp_set_nrns(sock, pdu);
  769. skb_queue_tail(&local->tx_queue, pdu);
  770. nr_frames++;
  771. }
  772. return nr_frames;
  773. }
  774. static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
  775. struct sk_buff *skb)
  776. {
  777. struct nfc_llcp_sock *llcp_sock;
  778. struct sock *sk;
  779. u8 dsap, ssap, ptype, ns, nr;
  780. ptype = nfc_llcp_ptype(skb);
  781. dsap = nfc_llcp_dsap(skb);
  782. ssap = nfc_llcp_ssap(skb);
  783. ns = nfc_llcp_ns(skb);
  784. nr = nfc_llcp_nr(skb);
  785. pr_debug("%d %d R %d S %d\n", dsap, ssap, nr, ns);
  786. llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
  787. if (llcp_sock == NULL) {
  788. nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
  789. return;
  790. }
  791. sk = &llcp_sock->sk;
  792. lock_sock(sk);
  793. if (sk->sk_state == LLCP_CLOSED) {
  794. release_sock(sk);
  795. nfc_llcp_sock_put(llcp_sock);
  796. }
  797. /* Pass the payload upstream */
  798. if (ptype == LLCP_PDU_I) {
  799. pr_debug("I frame, queueing on %p\n", &llcp_sock->sk);
  800. if (ns == llcp_sock->recv_n)
  801. llcp_sock->recv_n = (llcp_sock->recv_n + 1) % 16;
  802. else
  803. pr_err("Received out of sequence I PDU\n");
  804. skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
  805. if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
  806. /*
  807. * I frames will be freed from the socket layer, so we
  808. * need to keep them alive until someone receives them.
  809. */
  810. skb_get(skb);
  811. } else {
  812. pr_err("Receive queue is full\n");
  813. }
  814. }
  815. /* Remove skbs from the pending queue */
  816. if (llcp_sock->send_ack_n != nr) {
  817. struct sk_buff *s, *tmp;
  818. u8 n;
  819. llcp_sock->send_ack_n = nr;
  820. /* Remove and free all skbs until ns == nr */
  821. skb_queue_walk_safe(&llcp_sock->tx_pending_queue, s, tmp) {
  822. n = nfc_llcp_ns(s);
  823. skb_unlink(s, &llcp_sock->tx_pending_queue);
  824. kfree_skb(s);
  825. if (n == nr)
  826. break;
  827. }
  828. /* Re-queue the remaining skbs for transmission */
  829. skb_queue_reverse_walk_safe(&llcp_sock->tx_pending_queue,
  830. s, tmp) {
  831. skb_unlink(s, &llcp_sock->tx_pending_queue);
  832. skb_queue_head(&local->tx_queue, s);
  833. }
  834. }
  835. if (ptype == LLCP_PDU_RR)
  836. llcp_sock->remote_ready = true;
  837. else if (ptype == LLCP_PDU_RNR)
  838. llcp_sock->remote_ready = false;
  839. if (nfc_llcp_queue_i_frames(llcp_sock) == 0 && ptype == LLCP_PDU_I)
  840. nfc_llcp_send_rr(llcp_sock);
  841. release_sock(sk);
  842. nfc_llcp_sock_put(llcp_sock);
  843. }
  844. static void nfc_llcp_recv_disc(struct nfc_llcp_local *local,
  845. struct sk_buff *skb)
  846. {
  847. struct nfc_llcp_sock *llcp_sock;
  848. struct sock *sk;
  849. u8 dsap, ssap;
  850. dsap = nfc_llcp_dsap(skb);
  851. ssap = nfc_llcp_ssap(skb);
  852. if ((dsap == 0) && (ssap == 0)) {
  853. pr_debug("Connection termination");
  854. nfc_dep_link_down(local->dev);
  855. return;
  856. }
  857. llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
  858. if (llcp_sock == NULL) {
  859. nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
  860. return;
  861. }
  862. sk = &llcp_sock->sk;
  863. lock_sock(sk);
  864. nfc_llcp_socket_purge(llcp_sock);
  865. if (sk->sk_state == LLCP_CLOSED) {
  866. release_sock(sk);
  867. nfc_llcp_sock_put(llcp_sock);
  868. }
  869. if (sk->sk_state == LLCP_CONNECTED) {
  870. nfc_put_device(local->dev);
  871. sk->sk_state = LLCP_CLOSED;
  872. sk->sk_state_change(sk);
  873. }
  874. nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_DISC);
  875. release_sock(sk);
  876. nfc_llcp_sock_put(llcp_sock);
  877. }
  878. static void nfc_llcp_recv_cc(struct nfc_llcp_local *local, struct sk_buff *skb)
  879. {
  880. struct nfc_llcp_sock *llcp_sock;
  881. struct sock *sk;
  882. u8 dsap, ssap;
  883. dsap = nfc_llcp_dsap(skb);
  884. ssap = nfc_llcp_ssap(skb);
  885. llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
  886. if (llcp_sock == NULL) {
  887. pr_err("Invalid CC\n");
  888. nfc_llcp_send_dm(local, dsap, ssap, LLCP_DM_NOCONN);
  889. return;
  890. }
  891. sk = &llcp_sock->sk;
  892. /* Unlink from connecting and link to the client array */
  893. nfc_llcp_sock_unlink(&local->connecting_sockets, sk);
  894. nfc_llcp_sock_link(&local->sockets, sk);
  895. llcp_sock->dsap = ssap;
  896. nfc_llcp_parse_connection_tlv(llcp_sock, &skb->data[LLCP_HEADER_SIZE],
  897. skb->len - LLCP_HEADER_SIZE);
  898. sk->sk_state = LLCP_CONNECTED;
  899. sk->sk_state_change(sk);
  900. nfc_llcp_sock_put(llcp_sock);
  901. }
  902. static void nfc_llcp_recv_dm(struct nfc_llcp_local *local, struct sk_buff *skb)
  903. {
  904. struct nfc_llcp_sock *llcp_sock;
  905. struct sock *sk;
  906. u8 dsap, ssap, reason;
  907. dsap = nfc_llcp_dsap(skb);
  908. ssap = nfc_llcp_ssap(skb);
  909. reason = skb->data[2];
  910. pr_debug("%d %d reason %d\n", ssap, dsap, reason);
  911. switch (reason) {
  912. case LLCP_DM_NOBOUND:
  913. case LLCP_DM_REJ:
  914. llcp_sock = nfc_llcp_connecting_sock_get(local, dsap);
  915. break;
  916. default:
  917. llcp_sock = nfc_llcp_sock_get(local, dsap, ssap);
  918. break;
  919. }
  920. if (llcp_sock == NULL) {
  921. pr_debug("Already closed\n");
  922. return;
  923. }
  924. sk = &llcp_sock->sk;
  925. sk->sk_err = ENXIO;
  926. sk->sk_state = LLCP_CLOSED;
  927. sk->sk_state_change(sk);
  928. nfc_llcp_sock_put(llcp_sock);
  929. }
  930. static void nfc_llcp_recv_snl(struct nfc_llcp_local *local,
  931. struct sk_buff *skb)
  932. {
  933. struct nfc_llcp_sock *llcp_sock;
  934. u8 dsap, ssap, *tlv, type, length, tid, sap;
  935. u16 tlv_len, offset;
  936. char *service_name;
  937. size_t service_name_len;
  938. struct nfc_llcp_sdp_tlv *sdp;
  939. HLIST_HEAD(llc_sdres_list);
  940. size_t sdres_tlvs_len;
  941. HLIST_HEAD(nl_sdres_list);
  942. dsap = nfc_llcp_dsap(skb);
  943. ssap = nfc_llcp_ssap(skb);
  944. pr_debug("%d %d\n", dsap, ssap);
  945. if (dsap != LLCP_SAP_SDP || ssap != LLCP_SAP_SDP) {
  946. pr_err("Wrong SNL SAP\n");
  947. return;
  948. }
  949. tlv = &skb->data[LLCP_HEADER_SIZE];
  950. tlv_len = skb->len - LLCP_HEADER_SIZE;
  951. offset = 0;
  952. sdres_tlvs_len = 0;
  953. while (offset < tlv_len) {
  954. type = tlv[0];
  955. length = tlv[1];
  956. switch (type) {
  957. case LLCP_TLV_SDREQ:
  958. tid = tlv[2];
  959. service_name = (char *) &tlv[3];
  960. service_name_len = length - 1;
  961. pr_debug("Looking for %.16s\n", service_name);
  962. if (service_name_len == strlen("urn:nfc:sn:sdp") &&
  963. !strncmp(service_name, "urn:nfc:sn:sdp",
  964. service_name_len)) {
  965. sap = 1;
  966. goto add_snl;
  967. }
  968. llcp_sock = nfc_llcp_sock_from_sn(local, service_name,
  969. service_name_len);
  970. if (!llcp_sock) {
  971. sap = 0;
  972. goto add_snl;
  973. }
  974. /*
  975. * We found a socket but its ssap has not been reserved
  976. * yet. We need to assign it for good and send a reply.
  977. * The ssap will be freed when the socket is closed.
  978. */
  979. if (llcp_sock->ssap == LLCP_SDP_UNBOUND) {
  980. atomic_t *client_count;
  981. sap = nfc_llcp_reserve_sdp_ssap(local);
  982. pr_debug("Reserving %d\n", sap);
  983. if (sap == LLCP_SAP_MAX) {
  984. sap = 0;
  985. goto add_snl;
  986. }
  987. client_count =
  988. &local->local_sdp_cnt[sap -
  989. LLCP_WKS_NUM_SAP];
  990. atomic_inc(client_count);
  991. llcp_sock->ssap = sap;
  992. llcp_sock->reserved_ssap = sap;
  993. } else {
  994. sap = llcp_sock->ssap;
  995. }
  996. pr_debug("%p %d\n", llcp_sock, sap);
  997. add_snl:
  998. sdp = nfc_llcp_build_sdres_tlv(tid, sap);
  999. if (sdp == NULL)
  1000. goto exit;
  1001. sdres_tlvs_len += sdp->tlv_len;
  1002. hlist_add_head(&sdp->node, &llc_sdres_list);
  1003. break;
  1004. case LLCP_TLV_SDRES:
  1005. mutex_lock(&local->sdreq_lock);
  1006. pr_debug("LLCP_TLV_SDRES: searching tid %d\n", tlv[2]);
  1007. hlist_for_each_entry(sdp, &local->pending_sdreqs, node) {
  1008. if (sdp->tid != tlv[2])
  1009. continue;
  1010. sdp->sap = tlv[3];
  1011. pr_debug("Found: uri=%s, sap=%d\n",
  1012. sdp->uri, sdp->sap);
  1013. hlist_del(&sdp->node);
  1014. hlist_add_head(&sdp->node, &nl_sdres_list);
  1015. break;
  1016. }
  1017. mutex_unlock(&local->sdreq_lock);
  1018. break;
  1019. default:
  1020. pr_err("Invalid SNL tlv value 0x%x\n", type);
  1021. break;
  1022. }
  1023. offset += length + 2;
  1024. tlv += length + 2;
  1025. }
  1026. exit:
  1027. if (!hlist_empty(&nl_sdres_list))
  1028. nfc_genl_llc_send_sdres(local->dev, &nl_sdres_list);
  1029. if (!hlist_empty(&llc_sdres_list))
  1030. nfc_llcp_send_snl_sdres(local, &llc_sdres_list, sdres_tlvs_len);
  1031. }
  1032. static void nfc_llcp_recv_agf(struct nfc_llcp_local *local, struct sk_buff *skb)
  1033. {
  1034. u8 ptype;
  1035. u16 pdu_len;
  1036. struct sk_buff *new_skb;
  1037. if (skb->len <= LLCP_HEADER_SIZE) {
  1038. pr_err("Malformed AGF PDU\n");
  1039. return;
  1040. }
  1041. skb_pull(skb, LLCP_HEADER_SIZE);
  1042. while (skb->len > LLCP_AGF_PDU_HEADER_SIZE) {
  1043. pdu_len = skb->data[0] << 8 | skb->data[1];
  1044. skb_pull(skb, LLCP_AGF_PDU_HEADER_SIZE);
  1045. if (pdu_len < LLCP_HEADER_SIZE || pdu_len > skb->len) {
  1046. pr_err("Malformed AGF PDU\n");
  1047. return;
  1048. }
  1049. ptype = nfc_llcp_ptype(skb);
  1050. if (ptype == LLCP_PDU_SYMM || ptype == LLCP_PDU_AGF)
  1051. goto next;
  1052. new_skb = nfc_alloc_recv_skb(pdu_len, GFP_KERNEL);
  1053. if (new_skb == NULL) {
  1054. pr_err("Could not allocate PDU\n");
  1055. return;
  1056. }
  1057. memcpy(skb_put(new_skb, pdu_len), skb->data, pdu_len);
  1058. nfc_llcp_rx_skb(local, new_skb);
  1059. kfree_skb(new_skb);
  1060. next:
  1061. skb_pull(skb, pdu_len);
  1062. }
  1063. }
  1064. static void nfc_llcp_rx_skb(struct nfc_llcp_local *local, struct sk_buff *skb)
  1065. {
  1066. u8 dsap, ssap, ptype;
  1067. ptype = nfc_llcp_ptype(skb);
  1068. dsap = nfc_llcp_dsap(skb);
  1069. ssap = nfc_llcp_ssap(skb);
  1070. pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap);
  1071. if (ptype != LLCP_PDU_SYMM)
  1072. print_hex_dump(KERN_DEBUG, "LLCP Rx: ", DUMP_PREFIX_OFFSET,
  1073. 16, 1, skb->data, skb->len, true);
  1074. switch (ptype) {
  1075. case LLCP_PDU_SYMM:
  1076. pr_debug("SYMM\n");
  1077. break;
  1078. case LLCP_PDU_UI:
  1079. pr_debug("UI\n");
  1080. nfc_llcp_recv_ui(local, skb);
  1081. break;
  1082. case LLCP_PDU_CONNECT:
  1083. pr_debug("CONNECT\n");
  1084. nfc_llcp_recv_connect(local, skb);
  1085. break;
  1086. case LLCP_PDU_DISC:
  1087. pr_debug("DISC\n");
  1088. nfc_llcp_recv_disc(local, skb);
  1089. break;
  1090. case LLCP_PDU_CC:
  1091. pr_debug("CC\n");
  1092. nfc_llcp_recv_cc(local, skb);
  1093. break;
  1094. case LLCP_PDU_DM:
  1095. pr_debug("DM\n");
  1096. nfc_llcp_recv_dm(local, skb);
  1097. break;
  1098. case LLCP_PDU_SNL:
  1099. pr_debug("SNL\n");
  1100. nfc_llcp_recv_snl(local, skb);
  1101. break;
  1102. case LLCP_PDU_I:
  1103. case LLCP_PDU_RR:
  1104. case LLCP_PDU_RNR:
  1105. pr_debug("I frame\n");
  1106. nfc_llcp_recv_hdlc(local, skb);
  1107. break;
  1108. case LLCP_PDU_AGF:
  1109. pr_debug("AGF frame\n");
  1110. nfc_llcp_recv_agf(local, skb);
  1111. break;
  1112. }
  1113. }
  1114. static void nfc_llcp_rx_work(struct work_struct *work)
  1115. {
  1116. struct nfc_llcp_local *local = container_of(work, struct nfc_llcp_local,
  1117. rx_work);
  1118. struct sk_buff *skb;
  1119. skb = local->rx_pending;
  1120. if (skb == NULL) {
  1121. pr_debug("No pending SKB\n");
  1122. return;
  1123. }
  1124. __net_timestamp(skb);
  1125. nfc_llcp_send_to_raw_sock(local, skb, NFC_DIRECTION_RX);
  1126. nfc_llcp_rx_skb(local, skb);
  1127. schedule_work(&local->tx_work);
  1128. kfree_skb(local->rx_pending);
  1129. local->rx_pending = NULL;
  1130. }
  1131. static void __nfc_llcp_recv(struct nfc_llcp_local *local, struct sk_buff *skb)
  1132. {
  1133. local->rx_pending = skb;
  1134. del_timer(&local->link_timer);
  1135. schedule_work(&local->rx_work);
  1136. }
  1137. void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
  1138. {
  1139. struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
  1140. pr_debug("Received an LLCP PDU\n");
  1141. if (err < 0) {
  1142. pr_err("err %d\n", err);
  1143. return;
  1144. }
  1145. __nfc_llcp_recv(local, skb);
  1146. }
  1147. int nfc_llcp_data_received(struct nfc_dev *dev, struct sk_buff *skb)
  1148. {
  1149. struct nfc_llcp_local *local;
  1150. local = nfc_llcp_find_local(dev);
  1151. if (local == NULL) {
  1152. kfree_skb(skb);
  1153. return -ENODEV;
  1154. }
  1155. __nfc_llcp_recv(local, skb);
  1156. return 0;
  1157. }
  1158. void nfc_llcp_mac_is_down(struct nfc_dev *dev)
  1159. {
  1160. struct nfc_llcp_local *local;
  1161. local = nfc_llcp_find_local(dev);
  1162. if (local == NULL)
  1163. return;
  1164. local->remote_miu = LLCP_DEFAULT_MIU;
  1165. local->remote_lto = LLCP_DEFAULT_LTO;
  1166. /* Close and purge all existing sockets */
  1167. nfc_llcp_socket_release(local, true, 0);
  1168. }
  1169. void nfc_llcp_mac_is_up(struct nfc_dev *dev, u32 target_idx,
  1170. u8 comm_mode, u8 rf_mode)
  1171. {
  1172. struct nfc_llcp_local *local;
  1173. pr_debug("rf mode %d\n", rf_mode);
  1174. local = nfc_llcp_find_local(dev);
  1175. if (local == NULL)
  1176. return;
  1177. local->target_idx = target_idx;
  1178. local->comm_mode = comm_mode;
  1179. local->rf_mode = rf_mode;
  1180. if (rf_mode == NFC_RF_INITIATOR) {
  1181. pr_debug("Queueing Tx work\n");
  1182. schedule_work(&local->tx_work);
  1183. } else {
  1184. mod_timer(&local->link_timer,
  1185. jiffies + msecs_to_jiffies(local->remote_lto));
  1186. }
  1187. }
  1188. int nfc_llcp_register_device(struct nfc_dev *ndev)
  1189. {
  1190. struct nfc_llcp_local *local;
  1191. local = kzalloc(sizeof(struct nfc_llcp_local), GFP_KERNEL);
  1192. if (local == NULL)
  1193. return -ENOMEM;
  1194. local->dev = ndev;
  1195. INIT_LIST_HEAD(&local->list);
  1196. kref_init(&local->ref);
  1197. mutex_init(&local->sdp_lock);
  1198. init_timer(&local->link_timer);
  1199. local->link_timer.data = (unsigned long) local;
  1200. local->link_timer.function = nfc_llcp_symm_timer;
  1201. skb_queue_head_init(&local->tx_queue);
  1202. INIT_WORK(&local->tx_work, nfc_llcp_tx_work);
  1203. local->rx_pending = NULL;
  1204. INIT_WORK(&local->rx_work, nfc_llcp_rx_work);
  1205. INIT_WORK(&local->timeout_work, nfc_llcp_timeout_work);
  1206. rwlock_init(&local->sockets.lock);
  1207. rwlock_init(&local->connecting_sockets.lock);
  1208. rwlock_init(&local->raw_sockets.lock);
  1209. local->lto = 150; /* 1500 ms */
  1210. local->rw = LLCP_MAX_RW;
  1211. local->miux = cpu_to_be16(LLCP_MAX_MIUX);
  1212. local->local_wks = 0x1; /* LLC Link Management */
  1213. nfc_llcp_build_gb(local);
  1214. local->remote_miu = LLCP_DEFAULT_MIU;
  1215. local->remote_lto = LLCP_DEFAULT_LTO;
  1216. mutex_init(&local->sdreq_lock);
  1217. INIT_HLIST_HEAD(&local->pending_sdreqs);
  1218. init_timer(&local->sdreq_timer);
  1219. local->sdreq_timer.data = (unsigned long) local;
  1220. local->sdreq_timer.function = nfc_llcp_sdreq_timer;
  1221. INIT_WORK(&local->sdreq_timeout_work, nfc_llcp_sdreq_timeout_work);
  1222. list_add(&local->list, &llcp_devices);
  1223. return 0;
  1224. }
  1225. void nfc_llcp_unregister_device(struct nfc_dev *dev)
  1226. {
  1227. struct nfc_llcp_local *local = nfc_llcp_find_local(dev);
  1228. if (local == NULL) {
  1229. pr_debug("No such device\n");
  1230. return;
  1231. }
  1232. local_cleanup(local);
  1233. nfc_llcp_local_put(local);
  1234. }
  1235. int __init nfc_llcp_init(void)
  1236. {
  1237. return nfc_llcp_sock_init();
  1238. }
  1239. void nfc_llcp_exit(void)
  1240. {
  1241. nfc_llcp_sock_exit();
  1242. }