netlink.c 39 KB

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