llcp_core.c 35 KB

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