netlink.c 39 KB

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