netlink.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745
  1. /*
  2. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  3. *
  4. * Authors:
  5. * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
  6. * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
  7. *
  8. * Vendor commands implementation based on net/wireless/nl80211.c
  9. * which is:
  10. *
  11. * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
  12. * Copyright 2013-2014 Intel Mobile Communications GmbH
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  26. */
  27. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  28. #include <net/genetlink.h>
  29. #include <linux/nfc.h>
  30. #include <linux/slab.h>
  31. #include "nfc.h"
  32. #include "llcp.h"
  33. static const struct genl_multicast_group nfc_genl_mcgrps[] = {
  34. { .name = NFC_GENL_MCAST_EVENT_NAME, },
  35. };
  36. static struct genl_family nfc_genl_family = {
  37. .id = GENL_ID_GENERATE,
  38. .hdrsize = 0,
  39. .name = NFC_GENL_NAME,
  40. .version = NFC_GENL_VERSION,
  41. .maxattr = NFC_ATTR_MAX,
  42. };
  43. static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
  44. [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
  45. [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
  46. .len = NFC_DEVICE_NAME_MAXSIZE },
  47. [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
  48. [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
  49. [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
  50. [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
  51. [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
  52. [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
  53. [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 },
  54. [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 },
  55. [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 },
  56. [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
  57. [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
  58. .len = NFC_FIRMWARE_NAME_MAXSIZE },
  59. [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
  60. };
  61. static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
  62. [NFC_SDP_ATTR_URI] = { .type = NLA_STRING },
  63. [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 },
  64. };
  65. static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
  66. struct netlink_callback *cb, int flags)
  67. {
  68. void *hdr;
  69. hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  70. &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
  71. if (!hdr)
  72. return -EMSGSIZE;
  73. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  74. if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
  75. nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
  76. nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
  77. nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
  78. goto nla_put_failure;
  79. if (target->nfcid1_len > 0 &&
  80. nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
  81. target->nfcid1))
  82. goto nla_put_failure;
  83. if (target->sensb_res_len > 0 &&
  84. nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
  85. target->sensb_res))
  86. goto nla_put_failure;
  87. if (target->sensf_res_len > 0 &&
  88. nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
  89. target->sensf_res))
  90. goto nla_put_failure;
  91. if (target->is_iso15693) {
  92. if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID,
  93. target->iso15693_dsfid) ||
  94. nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID,
  95. sizeof(target->iso15693_uid), target->iso15693_uid))
  96. goto nla_put_failure;
  97. }
  98. genlmsg_end(msg, hdr);
  99. return 0;
  100. nla_put_failure:
  101. genlmsg_cancel(msg, hdr);
  102. return -EMSGSIZE;
  103. }
  104. static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
  105. {
  106. struct nfc_dev *dev;
  107. int rc;
  108. u32 idx;
  109. rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
  110. nfc_genl_family.attrbuf,
  111. nfc_genl_family.maxattr,
  112. nfc_genl_policy);
  113. if (rc < 0)
  114. return ERR_PTR(rc);
  115. if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
  116. return ERR_PTR(-EINVAL);
  117. idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
  118. dev = nfc_get_device(idx);
  119. if (!dev)
  120. return ERR_PTR(-ENODEV);
  121. return dev;
  122. }
  123. static int nfc_genl_dump_targets(struct sk_buff *skb,
  124. struct netlink_callback *cb)
  125. {
  126. int i = cb->args[0];
  127. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  128. int rc;
  129. if (!dev) {
  130. dev = __get_device_from_cb(cb);
  131. if (IS_ERR(dev))
  132. return PTR_ERR(dev);
  133. cb->args[1] = (long) dev;
  134. }
  135. device_lock(&dev->dev);
  136. cb->seq = dev->targets_generation;
  137. while (i < dev->n_targets) {
  138. rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
  139. NLM_F_MULTI);
  140. if (rc < 0)
  141. break;
  142. i++;
  143. }
  144. device_unlock(&dev->dev);
  145. cb->args[0] = i;
  146. return skb->len;
  147. }
  148. static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
  149. {
  150. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  151. if (dev)
  152. nfc_put_device(dev);
  153. return 0;
  154. }
  155. int nfc_genl_targets_found(struct nfc_dev *dev)
  156. {
  157. struct sk_buff *msg;
  158. void *hdr;
  159. dev->genl_data.poll_req_portid = 0;
  160. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  161. if (!msg)
  162. return -ENOMEM;
  163. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  164. NFC_EVENT_TARGETS_FOUND);
  165. if (!hdr)
  166. goto free_msg;
  167. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  168. goto nla_put_failure;
  169. genlmsg_end(msg, hdr);
  170. return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
  171. nla_put_failure:
  172. genlmsg_cancel(msg, hdr);
  173. free_msg:
  174. nlmsg_free(msg);
  175. return -EMSGSIZE;
  176. }
  177. int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
  178. {
  179. struct sk_buff *msg;
  180. void *hdr;
  181. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  182. if (!msg)
  183. return -ENOMEM;
  184. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  185. NFC_EVENT_TARGET_LOST);
  186. if (!hdr)
  187. goto free_msg;
  188. if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
  189. nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
  190. goto nla_put_failure;
  191. genlmsg_end(msg, hdr);
  192. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  193. return 0;
  194. nla_put_failure:
  195. genlmsg_cancel(msg, hdr);
  196. free_msg:
  197. nlmsg_free(msg);
  198. return -EMSGSIZE;
  199. }
  200. int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
  201. {
  202. struct sk_buff *msg;
  203. void *hdr;
  204. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  205. if (!msg)
  206. return -ENOMEM;
  207. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  208. NFC_EVENT_TM_ACTIVATED);
  209. if (!hdr)
  210. goto free_msg;
  211. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  212. goto nla_put_failure;
  213. if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
  214. goto nla_put_failure;
  215. genlmsg_end(msg, hdr);
  216. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  217. return 0;
  218. nla_put_failure:
  219. genlmsg_cancel(msg, hdr);
  220. free_msg:
  221. nlmsg_free(msg);
  222. return -EMSGSIZE;
  223. }
  224. int nfc_genl_tm_deactivated(struct nfc_dev *dev)
  225. {
  226. struct sk_buff *msg;
  227. void *hdr;
  228. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  229. if (!msg)
  230. return -ENOMEM;
  231. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  232. NFC_EVENT_TM_DEACTIVATED);
  233. if (!hdr)
  234. goto free_msg;
  235. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  236. goto nla_put_failure;
  237. genlmsg_end(msg, hdr);
  238. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  239. return 0;
  240. nla_put_failure:
  241. genlmsg_cancel(msg, hdr);
  242. free_msg:
  243. nlmsg_free(msg);
  244. return -EMSGSIZE;
  245. }
  246. int nfc_genl_device_added(struct nfc_dev *dev)
  247. {
  248. struct sk_buff *msg;
  249. void *hdr;
  250. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  251. if (!msg)
  252. return -ENOMEM;
  253. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  254. NFC_EVENT_DEVICE_ADDED);
  255. if (!hdr)
  256. goto free_msg;
  257. if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
  258. nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  259. nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
  260. nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
  261. goto nla_put_failure;
  262. genlmsg_end(msg, hdr);
  263. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  264. return 0;
  265. nla_put_failure:
  266. genlmsg_cancel(msg, hdr);
  267. free_msg:
  268. nlmsg_free(msg);
  269. return -EMSGSIZE;
  270. }
  271. int nfc_genl_device_removed(struct nfc_dev *dev)
  272. {
  273. struct sk_buff *msg;
  274. void *hdr;
  275. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  276. if (!msg)
  277. return -ENOMEM;
  278. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  279. NFC_EVENT_DEVICE_REMOVED);
  280. if (!hdr)
  281. goto free_msg;
  282. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  283. goto nla_put_failure;
  284. genlmsg_end(msg, hdr);
  285. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  286. return 0;
  287. nla_put_failure:
  288. genlmsg_cancel(msg, hdr);
  289. free_msg:
  290. nlmsg_free(msg);
  291. return -EMSGSIZE;
  292. }
  293. int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
  294. {
  295. struct sk_buff *msg;
  296. struct nlattr *sdp_attr, *uri_attr;
  297. struct nfc_llcp_sdp_tlv *sdres;
  298. struct hlist_node *n;
  299. void *hdr;
  300. int rc = -EMSGSIZE;
  301. int i;
  302. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  303. if (!msg)
  304. return -ENOMEM;
  305. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  306. NFC_EVENT_LLC_SDRES);
  307. if (!hdr)
  308. goto free_msg;
  309. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  310. goto nla_put_failure;
  311. sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP);
  312. if (sdp_attr == NULL) {
  313. rc = -ENOMEM;
  314. goto nla_put_failure;
  315. }
  316. i = 1;
  317. hlist_for_each_entry_safe(sdres, n, sdres_list, node) {
  318. pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap);
  319. uri_attr = nla_nest_start(msg, i++);
  320. if (uri_attr == NULL) {
  321. rc = -ENOMEM;
  322. goto nla_put_failure;
  323. }
  324. if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap))
  325. goto nla_put_failure;
  326. if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri))
  327. goto nla_put_failure;
  328. nla_nest_end(msg, uri_attr);
  329. hlist_del(&sdres->node);
  330. nfc_llcp_free_sdp_tlv(sdres);
  331. }
  332. nla_nest_end(msg, sdp_attr);
  333. genlmsg_end(msg, hdr);
  334. return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
  335. nla_put_failure:
  336. genlmsg_cancel(msg, hdr);
  337. free_msg:
  338. nlmsg_free(msg);
  339. nfc_llcp_free_sdp_tlv_list(sdres_list);
  340. return rc;
  341. }
  342. int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
  343. {
  344. struct sk_buff *msg;
  345. void *hdr;
  346. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  347. if (!msg)
  348. return -ENOMEM;
  349. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  350. NFC_EVENT_SE_ADDED);
  351. if (!hdr)
  352. goto free_msg;
  353. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  354. nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
  355. nla_put_u8(msg, NFC_ATTR_SE_TYPE, type))
  356. goto nla_put_failure;
  357. genlmsg_end(msg, hdr);
  358. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  359. return 0;
  360. nla_put_failure:
  361. genlmsg_cancel(msg, hdr);
  362. free_msg:
  363. nlmsg_free(msg);
  364. return -EMSGSIZE;
  365. }
  366. int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
  367. {
  368. struct sk_buff *msg;
  369. void *hdr;
  370. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  371. if (!msg)
  372. return -ENOMEM;
  373. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  374. NFC_EVENT_SE_REMOVED);
  375. if (!hdr)
  376. goto free_msg;
  377. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  378. nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx))
  379. goto nla_put_failure;
  380. genlmsg_end(msg, hdr);
  381. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  382. return 0;
  383. nla_put_failure:
  384. genlmsg_cancel(msg, hdr);
  385. free_msg:
  386. nlmsg_free(msg);
  387. return -EMSGSIZE;
  388. }
  389. int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx,
  390. struct nfc_evt_transaction *evt_transaction)
  391. {
  392. struct nfc_se *se;
  393. struct sk_buff *msg;
  394. void *hdr;
  395. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  396. if (!msg)
  397. return -ENOMEM;
  398. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  399. NFC_EVENT_SE_TRANSACTION);
  400. if (!hdr)
  401. goto free_msg;
  402. se = nfc_find_se(dev, se_idx);
  403. if (!se)
  404. goto free_msg;
  405. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  406. nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) ||
  407. nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) ||
  408. nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len,
  409. evt_transaction->aid) ||
  410. nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len,
  411. evt_transaction->params))
  412. goto nla_put_failure;
  413. /* evt_transaction is no more used */
  414. devm_kfree(&dev->dev, evt_transaction);
  415. genlmsg_end(msg, hdr);
  416. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  417. return 0;
  418. nla_put_failure:
  419. genlmsg_cancel(msg, hdr);
  420. free_msg:
  421. /* evt_transaction is no more used */
  422. devm_kfree(&dev->dev, evt_transaction);
  423. nlmsg_free(msg);
  424. return -EMSGSIZE;
  425. }
  426. static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
  427. u32 portid, u32 seq,
  428. struct netlink_callback *cb,
  429. int flags)
  430. {
  431. void *hdr;
  432. hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
  433. NFC_CMD_GET_DEVICE);
  434. if (!hdr)
  435. return -EMSGSIZE;
  436. if (cb)
  437. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  438. if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
  439. nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  440. nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
  441. nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
  442. nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
  443. goto nla_put_failure;
  444. genlmsg_end(msg, hdr);
  445. return 0;
  446. nla_put_failure:
  447. genlmsg_cancel(msg, hdr);
  448. return -EMSGSIZE;
  449. }
  450. static int nfc_genl_dump_devices(struct sk_buff *skb,
  451. struct netlink_callback *cb)
  452. {
  453. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  454. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  455. bool first_call = false;
  456. if (!iter) {
  457. first_call = true;
  458. iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
  459. if (!iter)
  460. return -ENOMEM;
  461. cb->args[0] = (long) iter;
  462. }
  463. mutex_lock(&nfc_devlist_mutex);
  464. cb->seq = nfc_devlist_generation;
  465. if (first_call) {
  466. nfc_device_iter_init(iter);
  467. dev = nfc_device_iter_next(iter);
  468. }
  469. while (dev) {
  470. int rc;
  471. rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
  472. cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
  473. if (rc < 0)
  474. break;
  475. dev = nfc_device_iter_next(iter);
  476. }
  477. mutex_unlock(&nfc_devlist_mutex);
  478. cb->args[1] = (long) dev;
  479. return skb->len;
  480. }
  481. static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
  482. {
  483. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  484. nfc_device_iter_exit(iter);
  485. kfree(iter);
  486. return 0;
  487. }
  488. int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
  489. u8 comm_mode, u8 rf_mode)
  490. {
  491. struct sk_buff *msg;
  492. void *hdr;
  493. pr_debug("DEP link is up\n");
  494. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  495. if (!msg)
  496. return -ENOMEM;
  497. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
  498. if (!hdr)
  499. goto free_msg;
  500. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  501. goto nla_put_failure;
  502. if (rf_mode == NFC_RF_INITIATOR &&
  503. nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
  504. goto nla_put_failure;
  505. if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
  506. nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
  507. goto nla_put_failure;
  508. genlmsg_end(msg, hdr);
  509. dev->dep_link_up = true;
  510. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
  511. return 0;
  512. nla_put_failure:
  513. genlmsg_cancel(msg, hdr);
  514. free_msg:
  515. nlmsg_free(msg);
  516. return -EMSGSIZE;
  517. }
  518. int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
  519. {
  520. struct sk_buff *msg;
  521. void *hdr;
  522. pr_debug("DEP link is down\n");
  523. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  524. if (!msg)
  525. return -ENOMEM;
  526. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  527. NFC_CMD_DEP_LINK_DOWN);
  528. if (!hdr)
  529. goto free_msg;
  530. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  531. goto nla_put_failure;
  532. genlmsg_end(msg, hdr);
  533. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
  534. return 0;
  535. nla_put_failure:
  536. genlmsg_cancel(msg, hdr);
  537. free_msg:
  538. nlmsg_free(msg);
  539. return -EMSGSIZE;
  540. }
  541. static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
  542. {
  543. struct sk_buff *msg;
  544. struct nfc_dev *dev;
  545. u32 idx;
  546. int rc = -ENOBUFS;
  547. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  548. return -EINVAL;
  549. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  550. dev = nfc_get_device(idx);
  551. if (!dev)
  552. return -ENODEV;
  553. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  554. if (!msg) {
  555. rc = -ENOMEM;
  556. goto out_putdev;
  557. }
  558. rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
  559. NULL, 0);
  560. if (rc < 0)
  561. goto out_free;
  562. nfc_put_device(dev);
  563. return genlmsg_reply(msg, info);
  564. out_free:
  565. nlmsg_free(msg);
  566. out_putdev:
  567. nfc_put_device(dev);
  568. return rc;
  569. }
  570. static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
  571. {
  572. struct nfc_dev *dev;
  573. int rc;
  574. u32 idx;
  575. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  576. return -EINVAL;
  577. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  578. dev = nfc_get_device(idx);
  579. if (!dev)
  580. return -ENODEV;
  581. rc = nfc_dev_up(dev);
  582. nfc_put_device(dev);
  583. return rc;
  584. }
  585. static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
  586. {
  587. struct nfc_dev *dev;
  588. int rc;
  589. u32 idx;
  590. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  591. return -EINVAL;
  592. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  593. dev = nfc_get_device(idx);
  594. if (!dev)
  595. return -ENODEV;
  596. rc = nfc_dev_down(dev);
  597. nfc_put_device(dev);
  598. return rc;
  599. }
  600. static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
  601. {
  602. struct nfc_dev *dev;
  603. int rc;
  604. u32 idx;
  605. u32 im_protocols = 0, tm_protocols = 0;
  606. pr_debug("Poll start\n");
  607. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  608. ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
  609. !info->attrs[NFC_ATTR_PROTOCOLS]) &&
  610. !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
  611. return -EINVAL;
  612. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  613. if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
  614. tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
  615. if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
  616. im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
  617. else if (info->attrs[NFC_ATTR_PROTOCOLS])
  618. im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
  619. dev = nfc_get_device(idx);
  620. if (!dev)
  621. return -ENODEV;
  622. mutex_lock(&dev->genl_data.genl_data_mutex);
  623. rc = nfc_start_poll(dev, im_protocols, tm_protocols);
  624. if (!rc)
  625. dev->genl_data.poll_req_portid = info->snd_portid;
  626. mutex_unlock(&dev->genl_data.genl_data_mutex);
  627. nfc_put_device(dev);
  628. return rc;
  629. }
  630. static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
  631. {
  632. struct nfc_dev *dev;
  633. int rc;
  634. u32 idx;
  635. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  636. return -EINVAL;
  637. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  638. dev = nfc_get_device(idx);
  639. if (!dev)
  640. return -ENODEV;
  641. device_lock(&dev->dev);
  642. if (!dev->polling) {
  643. device_unlock(&dev->dev);
  644. return -EINVAL;
  645. }
  646. device_unlock(&dev->dev);
  647. mutex_lock(&dev->genl_data.genl_data_mutex);
  648. if (dev->genl_data.poll_req_portid != info->snd_portid) {
  649. rc = -EBUSY;
  650. goto out;
  651. }
  652. rc = nfc_stop_poll(dev);
  653. dev->genl_data.poll_req_portid = 0;
  654. out:
  655. mutex_unlock(&dev->genl_data.genl_data_mutex);
  656. nfc_put_device(dev);
  657. return rc;
  658. }
  659. static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info)
  660. {
  661. struct nfc_dev *dev;
  662. u32 device_idx, target_idx, protocol;
  663. int rc;
  664. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  665. return -EINVAL;
  666. device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  667. dev = nfc_get_device(device_idx);
  668. if (!dev)
  669. return -ENODEV;
  670. target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
  671. protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
  672. nfc_deactivate_target(dev, target_idx);
  673. rc = nfc_activate_target(dev, target_idx, protocol);
  674. nfc_put_device(dev);
  675. return 0;
  676. }
  677. static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
  678. {
  679. struct nfc_dev *dev;
  680. int rc, tgt_idx;
  681. u32 idx;
  682. u8 comm;
  683. pr_debug("DEP link up\n");
  684. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  685. !info->attrs[NFC_ATTR_COMM_MODE])
  686. return -EINVAL;
  687. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  688. if (!info->attrs[NFC_ATTR_TARGET_INDEX])
  689. tgt_idx = NFC_TARGET_IDX_ANY;
  690. else
  691. tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
  692. comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
  693. if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
  694. return -EINVAL;
  695. dev = nfc_get_device(idx);
  696. if (!dev)
  697. return -ENODEV;
  698. rc = nfc_dep_link_up(dev, tgt_idx, comm);
  699. nfc_put_device(dev);
  700. return rc;
  701. }
  702. static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
  703. {
  704. struct nfc_dev *dev;
  705. int rc;
  706. u32 idx;
  707. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  708. return -EINVAL;
  709. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  710. dev = nfc_get_device(idx);
  711. if (!dev)
  712. return -ENODEV;
  713. rc = nfc_dep_link_down(dev);
  714. nfc_put_device(dev);
  715. return rc;
  716. }
  717. static int nfc_genl_send_params(struct sk_buff *msg,
  718. struct nfc_llcp_local *local,
  719. u32 portid, u32 seq)
  720. {
  721. void *hdr;
  722. hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
  723. NFC_CMD_LLC_GET_PARAMS);
  724. if (!hdr)
  725. return -EMSGSIZE;
  726. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
  727. nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
  728. nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
  729. nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
  730. goto nla_put_failure;
  731. genlmsg_end(msg, hdr);
  732. return 0;
  733. nla_put_failure:
  734. genlmsg_cancel(msg, hdr);
  735. return -EMSGSIZE;
  736. }
  737. static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
  738. {
  739. struct nfc_dev *dev;
  740. struct nfc_llcp_local *local;
  741. int rc = 0;
  742. struct sk_buff *msg = NULL;
  743. u32 idx;
  744. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  745. return -EINVAL;
  746. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  747. dev = nfc_get_device(idx);
  748. if (!dev)
  749. return -ENODEV;
  750. device_lock(&dev->dev);
  751. local = nfc_llcp_find_local(dev);
  752. if (!local) {
  753. rc = -ENODEV;
  754. goto exit;
  755. }
  756. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  757. if (!msg) {
  758. rc = -ENOMEM;
  759. goto exit;
  760. }
  761. rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
  762. exit:
  763. device_unlock(&dev->dev);
  764. nfc_put_device(dev);
  765. if (rc < 0) {
  766. if (msg)
  767. nlmsg_free(msg);
  768. return rc;
  769. }
  770. return genlmsg_reply(msg, info);
  771. }
  772. static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
  773. {
  774. struct nfc_dev *dev;
  775. struct nfc_llcp_local *local;
  776. u8 rw = 0;
  777. u16 miux = 0;
  778. u32 idx;
  779. int rc = 0;
  780. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  781. (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
  782. !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
  783. !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
  784. return -EINVAL;
  785. if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
  786. rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
  787. if (rw > LLCP_MAX_RW)
  788. return -EINVAL;
  789. }
  790. if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
  791. miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
  792. if (miux > LLCP_MAX_MIUX)
  793. return -EINVAL;
  794. }
  795. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  796. dev = nfc_get_device(idx);
  797. if (!dev)
  798. return -ENODEV;
  799. device_lock(&dev->dev);
  800. local = nfc_llcp_find_local(dev);
  801. if (!local) {
  802. nfc_put_device(dev);
  803. rc = -ENODEV;
  804. goto exit;
  805. }
  806. if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
  807. if (dev->dep_link_up) {
  808. rc = -EINPROGRESS;
  809. goto exit;
  810. }
  811. local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
  812. }
  813. if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
  814. local->rw = rw;
  815. if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
  816. local->miux = cpu_to_be16(miux);
  817. exit:
  818. device_unlock(&dev->dev);
  819. nfc_put_device(dev);
  820. return rc;
  821. }
  822. static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info)
  823. {
  824. struct nfc_dev *dev;
  825. struct nfc_llcp_local *local;
  826. struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1];
  827. u32 idx;
  828. u8 tid;
  829. char *uri;
  830. int rc = 0, rem;
  831. size_t uri_len, tlvs_len;
  832. struct hlist_head sdreq_list;
  833. struct nfc_llcp_sdp_tlv *sdreq;
  834. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  835. !info->attrs[NFC_ATTR_LLC_SDP])
  836. return -EINVAL;
  837. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  838. dev = nfc_get_device(idx);
  839. if (!dev) {
  840. rc = -ENODEV;
  841. goto exit;
  842. }
  843. device_lock(&dev->dev);
  844. if (dev->dep_link_up == false) {
  845. rc = -ENOLINK;
  846. goto exit;
  847. }
  848. local = nfc_llcp_find_local(dev);
  849. if (!local) {
  850. nfc_put_device(dev);
  851. rc = -ENODEV;
  852. goto exit;
  853. }
  854. INIT_HLIST_HEAD(&sdreq_list);
  855. tlvs_len = 0;
  856. nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) {
  857. rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr,
  858. nfc_sdp_genl_policy);
  859. if (rc != 0) {
  860. rc = -EINVAL;
  861. goto exit;
  862. }
  863. if (!sdp_attrs[NFC_SDP_ATTR_URI])
  864. continue;
  865. uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]);
  866. if (uri_len == 0)
  867. continue;
  868. uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]);
  869. if (uri == NULL || *uri == 0)
  870. continue;
  871. tid = local->sdreq_next_tid++;
  872. sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len);
  873. if (sdreq == NULL) {
  874. rc = -ENOMEM;
  875. goto exit;
  876. }
  877. tlvs_len += sdreq->tlv_len;
  878. hlist_add_head(&sdreq->node, &sdreq_list);
  879. }
  880. if (hlist_empty(&sdreq_list)) {
  881. rc = -EINVAL;
  882. goto exit;
  883. }
  884. rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len);
  885. exit:
  886. device_unlock(&dev->dev);
  887. nfc_put_device(dev);
  888. return rc;
  889. }
  890. static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info)
  891. {
  892. struct nfc_dev *dev;
  893. int rc;
  894. u32 idx;
  895. char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
  896. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  897. return -EINVAL;
  898. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  899. dev = nfc_get_device(idx);
  900. if (!dev)
  901. return -ENODEV;
  902. nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME],
  903. sizeof(firmware_name));
  904. rc = nfc_fw_download(dev, firmware_name);
  905. nfc_put_device(dev);
  906. return rc;
  907. }
  908. int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
  909. u32 result)
  910. {
  911. struct sk_buff *msg;
  912. void *hdr;
  913. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  914. if (!msg)
  915. return -ENOMEM;
  916. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  917. NFC_CMD_FW_DOWNLOAD);
  918. if (!hdr)
  919. goto free_msg;
  920. if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) ||
  921. nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) ||
  922. nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  923. goto nla_put_failure;
  924. genlmsg_end(msg, hdr);
  925. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  926. return 0;
  927. nla_put_failure:
  928. genlmsg_cancel(msg, hdr);
  929. free_msg:
  930. nlmsg_free(msg);
  931. return -EMSGSIZE;
  932. }
  933. static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info)
  934. {
  935. struct nfc_dev *dev;
  936. int rc;
  937. u32 idx, se_idx;
  938. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  939. !info->attrs[NFC_ATTR_SE_INDEX])
  940. return -EINVAL;
  941. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  942. se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
  943. dev = nfc_get_device(idx);
  944. if (!dev)
  945. return -ENODEV;
  946. rc = nfc_enable_se(dev, se_idx);
  947. nfc_put_device(dev);
  948. return rc;
  949. }
  950. static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info)
  951. {
  952. struct nfc_dev *dev;
  953. int rc;
  954. u32 idx, se_idx;
  955. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  956. !info->attrs[NFC_ATTR_SE_INDEX])
  957. return -EINVAL;
  958. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  959. se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
  960. dev = nfc_get_device(idx);
  961. if (!dev)
  962. return -ENODEV;
  963. rc = nfc_disable_se(dev, se_idx);
  964. nfc_put_device(dev);
  965. return rc;
  966. }
  967. static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev,
  968. u32 portid, u32 seq,
  969. struct netlink_callback *cb,
  970. int flags)
  971. {
  972. void *hdr;
  973. struct nfc_se *se, *n;
  974. list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
  975. hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
  976. NFC_CMD_GET_SE);
  977. if (!hdr)
  978. goto nla_put_failure;
  979. if (cb)
  980. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  981. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  982. nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) ||
  983. nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type))
  984. goto nla_put_failure;
  985. genlmsg_end(msg, hdr);
  986. }
  987. return 0;
  988. nla_put_failure:
  989. genlmsg_cancel(msg, hdr);
  990. return -EMSGSIZE;
  991. }
  992. static int nfc_genl_dump_ses(struct sk_buff *skb,
  993. struct netlink_callback *cb)
  994. {
  995. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  996. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  997. bool first_call = false;
  998. if (!iter) {
  999. first_call = true;
  1000. iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
  1001. if (!iter)
  1002. return -ENOMEM;
  1003. cb->args[0] = (long) iter;
  1004. }
  1005. mutex_lock(&nfc_devlist_mutex);
  1006. cb->seq = nfc_devlist_generation;
  1007. if (first_call) {
  1008. nfc_device_iter_init(iter);
  1009. dev = nfc_device_iter_next(iter);
  1010. }
  1011. while (dev) {
  1012. int rc;
  1013. rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid,
  1014. cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
  1015. if (rc < 0)
  1016. break;
  1017. dev = nfc_device_iter_next(iter);
  1018. }
  1019. mutex_unlock(&nfc_devlist_mutex);
  1020. cb->args[1] = (long) dev;
  1021. return skb->len;
  1022. }
  1023. static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
  1024. {
  1025. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  1026. nfc_device_iter_exit(iter);
  1027. kfree(iter);
  1028. return 0;
  1029. }
  1030. static int nfc_se_io(struct nfc_dev *dev, u32 se_idx,
  1031. u8 *apdu, size_t apdu_length,
  1032. se_io_cb_t cb, void *cb_context)
  1033. {
  1034. struct nfc_se *se;
  1035. int rc;
  1036. pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
  1037. device_lock(&dev->dev);
  1038. if (!device_is_registered(&dev->dev)) {
  1039. rc = -ENODEV;
  1040. goto error;
  1041. }
  1042. if (!dev->dev_up) {
  1043. rc = -ENODEV;
  1044. goto error;
  1045. }
  1046. if (!dev->ops->se_io) {
  1047. rc = -EOPNOTSUPP;
  1048. goto error;
  1049. }
  1050. se = nfc_find_se(dev, se_idx);
  1051. if (!se) {
  1052. rc = -EINVAL;
  1053. goto error;
  1054. }
  1055. if (se->state != NFC_SE_ENABLED) {
  1056. rc = -ENODEV;
  1057. goto error;
  1058. }
  1059. rc = dev->ops->se_io(dev, se_idx, apdu,
  1060. apdu_length, cb, cb_context);
  1061. error:
  1062. device_unlock(&dev->dev);
  1063. return rc;
  1064. }
  1065. struct se_io_ctx {
  1066. u32 dev_idx;
  1067. u32 se_idx;
  1068. };
  1069. static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
  1070. {
  1071. struct se_io_ctx *ctx = context;
  1072. struct sk_buff *msg;
  1073. void *hdr;
  1074. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1075. if (!msg) {
  1076. kfree(ctx);
  1077. return;
  1078. }
  1079. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  1080. NFC_CMD_SE_IO);
  1081. if (!hdr)
  1082. goto free_msg;
  1083. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
  1084. nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
  1085. nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
  1086. goto nla_put_failure;
  1087. genlmsg_end(msg, hdr);
  1088. genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
  1089. kfree(ctx);
  1090. return;
  1091. nla_put_failure:
  1092. genlmsg_cancel(msg, hdr);
  1093. free_msg:
  1094. nlmsg_free(msg);
  1095. kfree(ctx);
  1096. return;
  1097. }
  1098. static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
  1099. {
  1100. struct nfc_dev *dev;
  1101. struct se_io_ctx *ctx;
  1102. u32 dev_idx, se_idx;
  1103. u8 *apdu;
  1104. size_t apdu_len;
  1105. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  1106. !info->attrs[NFC_ATTR_SE_INDEX] ||
  1107. !info->attrs[NFC_ATTR_SE_APDU])
  1108. return -EINVAL;
  1109. dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  1110. se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
  1111. dev = nfc_get_device(dev_idx);
  1112. if (!dev)
  1113. return -ENODEV;
  1114. if (!dev->ops || !dev->ops->se_io)
  1115. return -ENOTSUPP;
  1116. apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
  1117. if (apdu_len == 0)
  1118. return -EINVAL;
  1119. apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
  1120. if (!apdu)
  1121. return -EINVAL;
  1122. ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
  1123. if (!ctx)
  1124. return -ENOMEM;
  1125. ctx->dev_idx = dev_idx;
  1126. ctx->se_idx = se_idx;
  1127. return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
  1128. }
  1129. static int nfc_genl_vendor_cmd(struct sk_buff *skb,
  1130. struct genl_info *info)
  1131. {
  1132. struct nfc_dev *dev;
  1133. struct nfc_vendor_cmd *cmd;
  1134. u32 dev_idx, vid, subcmd;
  1135. u8 *data;
  1136. size_t data_len;
  1137. int i;
  1138. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  1139. !info->attrs[NFC_ATTR_VENDOR_ID] ||
  1140. !info->attrs[NFC_ATTR_VENDOR_SUBCMD])
  1141. return -EINVAL;
  1142. dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  1143. vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]);
  1144. subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]);
  1145. dev = nfc_get_device(dev_idx);
  1146. if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds)
  1147. return -ENODEV;
  1148. data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]);
  1149. if (data) {
  1150. data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]);
  1151. if (data_len == 0)
  1152. return -EINVAL;
  1153. } else {
  1154. data_len = 0;
  1155. }
  1156. for (i = 0; i < dev->n_vendor_cmds; i++) {
  1157. cmd = &dev->vendor_cmds[i];
  1158. if (cmd->vendor_id != vid || cmd->subcmd != subcmd)
  1159. continue;
  1160. return cmd->doit(dev, data, data_len);
  1161. }
  1162. return -EOPNOTSUPP;
  1163. }
  1164. static const struct genl_ops nfc_genl_ops[] = {
  1165. {
  1166. .cmd = NFC_CMD_GET_DEVICE,
  1167. .doit = nfc_genl_get_device,
  1168. .dumpit = nfc_genl_dump_devices,
  1169. .done = nfc_genl_dump_devices_done,
  1170. .policy = nfc_genl_policy,
  1171. },
  1172. {
  1173. .cmd = NFC_CMD_DEV_UP,
  1174. .doit = nfc_genl_dev_up,
  1175. .policy = nfc_genl_policy,
  1176. },
  1177. {
  1178. .cmd = NFC_CMD_DEV_DOWN,
  1179. .doit = nfc_genl_dev_down,
  1180. .policy = nfc_genl_policy,
  1181. },
  1182. {
  1183. .cmd = NFC_CMD_START_POLL,
  1184. .doit = nfc_genl_start_poll,
  1185. .policy = nfc_genl_policy,
  1186. },
  1187. {
  1188. .cmd = NFC_CMD_STOP_POLL,
  1189. .doit = nfc_genl_stop_poll,
  1190. .policy = nfc_genl_policy,
  1191. },
  1192. {
  1193. .cmd = NFC_CMD_DEP_LINK_UP,
  1194. .doit = nfc_genl_dep_link_up,
  1195. .policy = nfc_genl_policy,
  1196. },
  1197. {
  1198. .cmd = NFC_CMD_DEP_LINK_DOWN,
  1199. .doit = nfc_genl_dep_link_down,
  1200. .policy = nfc_genl_policy,
  1201. },
  1202. {
  1203. .cmd = NFC_CMD_GET_TARGET,
  1204. .dumpit = nfc_genl_dump_targets,
  1205. .done = nfc_genl_dump_targets_done,
  1206. .policy = nfc_genl_policy,
  1207. },
  1208. {
  1209. .cmd = NFC_CMD_LLC_GET_PARAMS,
  1210. .doit = nfc_genl_llc_get_params,
  1211. .policy = nfc_genl_policy,
  1212. },
  1213. {
  1214. .cmd = NFC_CMD_LLC_SET_PARAMS,
  1215. .doit = nfc_genl_llc_set_params,
  1216. .policy = nfc_genl_policy,
  1217. },
  1218. {
  1219. .cmd = NFC_CMD_LLC_SDREQ,
  1220. .doit = nfc_genl_llc_sdreq,
  1221. .policy = nfc_genl_policy,
  1222. },
  1223. {
  1224. .cmd = NFC_CMD_FW_DOWNLOAD,
  1225. .doit = nfc_genl_fw_download,
  1226. .policy = nfc_genl_policy,
  1227. },
  1228. {
  1229. .cmd = NFC_CMD_ENABLE_SE,
  1230. .doit = nfc_genl_enable_se,
  1231. .policy = nfc_genl_policy,
  1232. },
  1233. {
  1234. .cmd = NFC_CMD_DISABLE_SE,
  1235. .doit = nfc_genl_disable_se,
  1236. .policy = nfc_genl_policy,
  1237. },
  1238. {
  1239. .cmd = NFC_CMD_GET_SE,
  1240. .dumpit = nfc_genl_dump_ses,
  1241. .done = nfc_genl_dump_ses_done,
  1242. .policy = nfc_genl_policy,
  1243. },
  1244. {
  1245. .cmd = NFC_CMD_SE_IO,
  1246. .doit = nfc_genl_se_io,
  1247. .policy = nfc_genl_policy,
  1248. },
  1249. {
  1250. .cmd = NFC_CMD_ACTIVATE_TARGET,
  1251. .doit = nfc_genl_activate_target,
  1252. .policy = nfc_genl_policy,
  1253. },
  1254. {
  1255. .cmd = NFC_CMD_VENDOR,
  1256. .doit = nfc_genl_vendor_cmd,
  1257. .policy = nfc_genl_policy,
  1258. },
  1259. };
  1260. struct urelease_work {
  1261. struct work_struct w;
  1262. u32 portid;
  1263. };
  1264. static void nfc_urelease_event_work(struct work_struct *work)
  1265. {
  1266. struct urelease_work *w = container_of(work, struct urelease_work, w);
  1267. struct class_dev_iter iter;
  1268. struct nfc_dev *dev;
  1269. pr_debug("portid %d\n", w->portid);
  1270. mutex_lock(&nfc_devlist_mutex);
  1271. nfc_device_iter_init(&iter);
  1272. dev = nfc_device_iter_next(&iter);
  1273. while (dev) {
  1274. mutex_lock(&dev->genl_data.genl_data_mutex);
  1275. if (dev->genl_data.poll_req_portid == w->portid) {
  1276. nfc_stop_poll(dev);
  1277. dev->genl_data.poll_req_portid = 0;
  1278. }
  1279. mutex_unlock(&dev->genl_data.genl_data_mutex);
  1280. dev = nfc_device_iter_next(&iter);
  1281. }
  1282. nfc_device_iter_exit(&iter);
  1283. mutex_unlock(&nfc_devlist_mutex);
  1284. kfree(w);
  1285. }
  1286. static int nfc_genl_rcv_nl_event(struct notifier_block *this,
  1287. unsigned long event, void *ptr)
  1288. {
  1289. struct netlink_notify *n = ptr;
  1290. struct urelease_work *w;
  1291. if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
  1292. goto out;
  1293. pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
  1294. w = kmalloc(sizeof(*w), GFP_ATOMIC);
  1295. if (w) {
  1296. INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
  1297. w->portid = n->portid;
  1298. schedule_work((struct work_struct *) w);
  1299. }
  1300. out:
  1301. return NOTIFY_DONE;
  1302. }
  1303. void nfc_genl_data_init(struct nfc_genl_data *genl_data)
  1304. {
  1305. genl_data->poll_req_portid = 0;
  1306. mutex_init(&genl_data->genl_data_mutex);
  1307. }
  1308. void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
  1309. {
  1310. mutex_destroy(&genl_data->genl_data_mutex);
  1311. }
  1312. static struct notifier_block nl_notifier = {
  1313. .notifier_call = nfc_genl_rcv_nl_event,
  1314. };
  1315. /**
  1316. * nfc_genl_init() - Initialize netlink interface
  1317. *
  1318. * This initialization function registers the nfc netlink family.
  1319. */
  1320. int __init nfc_genl_init(void)
  1321. {
  1322. int rc;
  1323. rc = genl_register_family_with_ops_groups(&nfc_genl_family,
  1324. nfc_genl_ops,
  1325. nfc_genl_mcgrps);
  1326. if (rc)
  1327. return rc;
  1328. netlink_register_notifier(&nl_notifier);
  1329. return 0;
  1330. }
  1331. /**
  1332. * nfc_genl_exit() - Deinitialize netlink interface
  1333. *
  1334. * This exit function unregisters the nfc netlink family.
  1335. */
  1336. void nfc_genl_exit(void)
  1337. {
  1338. netlink_unregister_notifier(&nl_notifier);
  1339. genl_unregister_family(&nfc_genl_family);
  1340. }