llcp_core.c 35 KB

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