llcp_core.c 35 KB

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