Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 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 | * |
Samuel Ortiz | 9e58095 | 2014-10-14 02:19:46 +0200 | [diff] [blame] | 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 | * |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 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 |
Jeff Kirsher | 98b32de | 2013-12-06 08:56:16 -0800 | [diff] [blame] | 25 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 26 | */ |
| 27 | |
Samuel Ortiz | 52858b5 | 2011-12-14 16:43:05 +0100 | [diff] [blame] | 28 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 29 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 30 | #include <net/genetlink.h> |
| 31 | #include <linux/nfc.h> |
| 32 | #include <linux/slab.h> |
| 33 | |
| 34 | #include "nfc.h" |
Samuel Ortiz | 30cc458 | 2013-04-26 11:49:40 +0200 | [diff] [blame] | 35 | #include "llcp.h" |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 36 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 37 | static const struct genl_multicast_group nfc_genl_mcgrps[] = { |
| 38 | { .name = NFC_GENL_MCAST_EVENT_NAME, }, |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 39 | }; |
| 40 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 41 | static struct genl_family nfc_genl_family; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 42 | static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = { |
| 43 | [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 }, |
| 44 | [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING, |
| 45 | .len = NFC_DEVICE_NAME_MAXSIZE }, |
| 46 | [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 }, |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 47 | [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 }, |
| 48 | [NFC_ATTR_RF_MODE] = { .type = NLA_U8 }, |
Samuel Ortiz | c970a1a | 2012-03-05 01:03:34 +0100 | [diff] [blame] | 49 | [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 }, |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 50 | [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 }, |
| 51 | [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 }, |
Thierry Escande | 8af362d | 2013-02-15 10:42:52 +0100 | [diff] [blame] | 52 | [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 }, |
| 53 | [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 }, |
| 54 | [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 }, |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 55 | [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED }, |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 56 | [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING, |
| 57 | .len = NFC_FIRMWARE_NAME_MAXSIZE }, |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 58 | [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY }, |
Christophe Ricard | 29e7692 | 2015-08-19 21:26:43 +0200 | [diff] [blame] | 59 | [NFC_ATTR_VENDOR_DATA] = { .type = NLA_BINARY }, |
| 60 | |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = { |
Kees Cook | fe9c842 | 2018-02-14 15:45:07 -0800 | [diff] [blame] | 64 | [NFC_SDP_ATTR_URI] = { .type = NLA_STRING, |
| 65 | .len = U8_MAX - 4 }, |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 66 | [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 }, |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 70 | struct netlink_callback *cb, int flags) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 71 | { |
| 72 | void *hdr; |
| 73 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 74 | hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 75 | &nfc_genl_family, flags, NFC_CMD_GET_TARGET); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 76 | if (!hdr) |
| 77 | return -EMSGSIZE; |
| 78 | |
Michal Kubecek | 0a833c2 | 2017-11-15 13:09:32 +0100 | [diff] [blame] | 79 | genl_dump_check_consistent(cb, hdr); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 80 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 81 | if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) || |
| 82 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) || |
| 83 | nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) || |
| 84 | nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res)) |
| 85 | goto nla_put_failure; |
| 86 | if (target->nfcid1_len > 0 && |
| 87 | nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len, |
| 88 | target->nfcid1)) |
| 89 | goto nla_put_failure; |
| 90 | if (target->sensb_res_len > 0 && |
| 91 | nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len, |
| 92 | target->sensb_res)) |
| 93 | goto nla_put_failure; |
| 94 | if (target->sensf_res_len > 0 && |
| 95 | nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len, |
| 96 | target->sensf_res)) |
| 97 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 98 | |
Mark A. Greer | f5f6872 | 2014-01-14 17:52:11 -0700 | [diff] [blame] | 99 | if (target->is_iso15693) { |
| 100 | if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID, |
| 101 | target->iso15693_dsfid) || |
| 102 | nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID, |
| 103 | sizeof(target->iso15693_uid), target->iso15693_uid)) |
| 104 | goto nla_put_failure; |
| 105 | } |
| 106 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 107 | genlmsg_end(msg, hdr); |
| 108 | return 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 109 | |
| 110 | nla_put_failure: |
| 111 | genlmsg_cancel(msg, hdr); |
| 112 | return -EMSGSIZE; |
| 113 | } |
| 114 | |
| 115 | static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb) |
| 116 | { |
Johannes Berg | c90c39d | 2016-10-24 14:40:01 +0200 | [diff] [blame] | 117 | struct nlattr **attrbuf = genl_family_attrbuf(&nfc_genl_family); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 118 | struct nfc_dev *dev; |
| 119 | int rc; |
| 120 | u32 idx; |
| 121 | |
| 122 | rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize, |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 123 | attrbuf, nfc_genl_family.maxattr, nfc_genl_policy, |
| 124 | NULL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 125 | if (rc < 0) |
| 126 | return ERR_PTR(rc); |
| 127 | |
Johannes Berg | c90c39d | 2016-10-24 14:40:01 +0200 | [diff] [blame] | 128 | if (!attrbuf[NFC_ATTR_DEVICE_INDEX]) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 129 | return ERR_PTR(-EINVAL); |
| 130 | |
Johannes Berg | c90c39d | 2016-10-24 14:40:01 +0200 | [diff] [blame] | 131 | idx = nla_get_u32(attrbuf[NFC_ATTR_DEVICE_INDEX]); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 132 | |
| 133 | dev = nfc_get_device(idx); |
| 134 | if (!dev) |
| 135 | return ERR_PTR(-ENODEV); |
| 136 | |
| 137 | return dev; |
| 138 | } |
| 139 | |
| 140 | static int nfc_genl_dump_targets(struct sk_buff *skb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 141 | struct netlink_callback *cb) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 142 | { |
| 143 | int i = cb->args[0]; |
| 144 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 145 | int rc; |
| 146 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 147 | if (!dev) { |
| 148 | dev = __get_device_from_cb(cb); |
| 149 | if (IS_ERR(dev)) |
| 150 | return PTR_ERR(dev); |
| 151 | |
| 152 | cb->args[1] = (long) dev; |
| 153 | } |
| 154 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 155 | device_lock(&dev->dev); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 156 | |
| 157 | cb->seq = dev->targets_generation; |
| 158 | |
| 159 | while (i < dev->n_targets) { |
| 160 | rc = nfc_genl_send_target(skb, &dev->targets[i], cb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 161 | NLM_F_MULTI); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 162 | if (rc < 0) |
| 163 | break; |
| 164 | |
| 165 | i++; |
| 166 | } |
| 167 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 168 | device_unlock(&dev->dev); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 169 | |
| 170 | cb->args[0] = i; |
| 171 | |
| 172 | return skb->len; |
| 173 | } |
| 174 | |
| 175 | static int nfc_genl_dump_targets_done(struct netlink_callback *cb) |
| 176 | { |
| 177 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 178 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 179 | if (dev) |
| 180 | nfc_put_device(dev); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | int nfc_genl_targets_found(struct nfc_dev *dev) |
| 186 | { |
| 187 | struct sk_buff *msg; |
| 188 | void *hdr; |
| 189 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 190 | dev->genl_data.poll_req_portid = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 191 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 192 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 193 | if (!msg) |
| 194 | return -ENOMEM; |
| 195 | |
| 196 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 197 | NFC_EVENT_TARGETS_FOUND); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 198 | if (!hdr) |
| 199 | goto free_msg; |
| 200 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 201 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 202 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 203 | |
| 204 | genlmsg_end(msg, hdr); |
| 205 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 206 | return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 207 | |
| 208 | nla_put_failure: |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 209 | free_msg: |
| 210 | nlmsg_free(msg); |
| 211 | return -EMSGSIZE; |
| 212 | } |
| 213 | |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 214 | int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx) |
| 215 | { |
| 216 | struct sk_buff *msg; |
| 217 | void *hdr; |
| 218 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 219 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 220 | if (!msg) |
| 221 | return -ENOMEM; |
| 222 | |
| 223 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 224 | NFC_EVENT_TARGET_LOST); |
| 225 | if (!hdr) |
| 226 | goto free_msg; |
| 227 | |
John W. Linville | 59ef43e | 2012-04-18 14:17:13 -0400 | [diff] [blame] | 228 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
| 229 | nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) |
| 230 | goto nla_put_failure; |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 231 | |
| 232 | genlmsg_end(msg, hdr); |
| 233 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 234 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 235 | |
| 236 | return 0; |
| 237 | |
| 238 | nla_put_failure: |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 239 | free_msg: |
| 240 | nlmsg_free(msg); |
| 241 | return -EMSGSIZE; |
| 242 | } |
| 243 | |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 244 | int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol) |
| 245 | { |
| 246 | struct sk_buff *msg; |
| 247 | void *hdr; |
| 248 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 249 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 250 | if (!msg) |
| 251 | return -ENOMEM; |
| 252 | |
| 253 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 254 | NFC_EVENT_TM_ACTIVATED); |
| 255 | if (!hdr) |
| 256 | goto free_msg; |
| 257 | |
| 258 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 259 | goto nla_put_failure; |
| 260 | if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol)) |
| 261 | goto nla_put_failure; |
| 262 | |
| 263 | genlmsg_end(msg, hdr); |
| 264 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 265 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 266 | |
| 267 | return 0; |
| 268 | |
| 269 | nla_put_failure: |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 270 | free_msg: |
| 271 | nlmsg_free(msg); |
| 272 | return -EMSGSIZE; |
| 273 | } |
| 274 | |
| 275 | int nfc_genl_tm_deactivated(struct nfc_dev *dev) |
| 276 | { |
| 277 | struct sk_buff *msg; |
| 278 | void *hdr; |
| 279 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 280 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 281 | if (!msg) |
| 282 | return -ENOMEM; |
| 283 | |
| 284 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 285 | NFC_EVENT_TM_DEACTIVATED); |
| 286 | if (!hdr) |
| 287 | goto free_msg; |
| 288 | |
| 289 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 290 | goto nla_put_failure; |
| 291 | |
| 292 | genlmsg_end(msg, hdr); |
| 293 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 294 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 295 | |
| 296 | return 0; |
| 297 | |
| 298 | nla_put_failure: |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 299 | free_msg: |
| 300 | nlmsg_free(msg); |
| 301 | return -EMSGSIZE; |
| 302 | } |
| 303 | |
OGAWA Hirofumi | 85a2566 | 2017-02-04 10:15:55 +0900 | [diff] [blame] | 304 | static int nfc_genl_setup_device_added(struct nfc_dev *dev, struct sk_buff *msg) |
| 305 | { |
| 306 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
| 307 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 308 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || |
| 309 | nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) || |
| 310 | nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode)) |
| 311 | return -1; |
| 312 | return 0; |
| 313 | } |
| 314 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 315 | int nfc_genl_device_added(struct nfc_dev *dev) |
| 316 | { |
| 317 | struct sk_buff *msg; |
| 318 | void *hdr; |
| 319 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 320 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 321 | if (!msg) |
| 322 | return -ENOMEM; |
| 323 | |
| 324 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 325 | NFC_EVENT_DEVICE_ADDED); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 326 | if (!hdr) |
| 327 | goto free_msg; |
| 328 | |
OGAWA Hirofumi | 85a2566 | 2017-02-04 10:15:55 +0900 | [diff] [blame] | 329 | if (nfc_genl_setup_device_added(dev, msg)) |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 330 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 331 | |
| 332 | genlmsg_end(msg, hdr); |
| 333 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 334 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 335 | |
| 336 | return 0; |
| 337 | |
| 338 | nla_put_failure: |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 339 | free_msg: |
| 340 | nlmsg_free(msg); |
| 341 | return -EMSGSIZE; |
| 342 | } |
| 343 | |
| 344 | int nfc_genl_device_removed(struct nfc_dev *dev) |
| 345 | { |
| 346 | struct sk_buff *msg; |
| 347 | void *hdr; |
| 348 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 349 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 350 | if (!msg) |
| 351 | return -ENOMEM; |
| 352 | |
| 353 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 354 | NFC_EVENT_DEVICE_REMOVED); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 355 | if (!hdr) |
| 356 | goto free_msg; |
| 357 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 358 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 359 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 360 | |
| 361 | genlmsg_end(msg, hdr); |
| 362 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 363 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 364 | |
| 365 | return 0; |
| 366 | |
| 367 | nla_put_failure: |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 368 | free_msg: |
| 369 | nlmsg_free(msg); |
| 370 | return -EMSGSIZE; |
| 371 | } |
| 372 | |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 373 | int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list) |
| 374 | { |
| 375 | struct sk_buff *msg; |
| 376 | struct nlattr *sdp_attr, *uri_attr; |
| 377 | struct nfc_llcp_sdp_tlv *sdres; |
| 378 | struct hlist_node *n; |
| 379 | void *hdr; |
| 380 | int rc = -EMSGSIZE; |
| 381 | int i; |
| 382 | |
| 383 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 384 | if (!msg) |
| 385 | return -ENOMEM; |
| 386 | |
| 387 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 388 | NFC_EVENT_LLC_SDRES); |
| 389 | if (!hdr) |
| 390 | goto free_msg; |
| 391 | |
| 392 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 393 | goto nla_put_failure; |
| 394 | |
| 395 | sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP); |
| 396 | if (sdp_attr == NULL) { |
| 397 | rc = -ENOMEM; |
| 398 | goto nla_put_failure; |
| 399 | } |
| 400 | |
| 401 | i = 1; |
| 402 | hlist_for_each_entry_safe(sdres, n, sdres_list, node) { |
| 403 | pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap); |
| 404 | |
| 405 | uri_attr = nla_nest_start(msg, i++); |
| 406 | if (uri_attr == NULL) { |
| 407 | rc = -ENOMEM; |
| 408 | goto nla_put_failure; |
| 409 | } |
| 410 | |
| 411 | if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap)) |
| 412 | goto nla_put_failure; |
| 413 | |
| 414 | if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri)) |
| 415 | goto nla_put_failure; |
| 416 | |
| 417 | nla_nest_end(msg, uri_attr); |
| 418 | |
| 419 | hlist_del(&sdres->node); |
| 420 | |
| 421 | nfc_llcp_free_sdp_tlv(sdres); |
| 422 | } |
| 423 | |
| 424 | nla_nest_end(msg, sdp_attr); |
| 425 | |
| 426 | genlmsg_end(msg, hdr); |
| 427 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 428 | return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 429 | |
| 430 | nla_put_failure: |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 431 | free_msg: |
| 432 | nlmsg_free(msg); |
| 433 | |
| 434 | nfc_llcp_free_sdp_tlv_list(sdres_list); |
| 435 | |
| 436 | return rc; |
| 437 | } |
| 438 | |
Samuel Ortiz | 2757c372 | 2013-05-10 15:47:37 +0200 | [diff] [blame] | 439 | int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type) |
| 440 | { |
| 441 | struct sk_buff *msg; |
| 442 | void *hdr; |
| 443 | |
| 444 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 445 | if (!msg) |
| 446 | return -ENOMEM; |
| 447 | |
| 448 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 449 | NFC_EVENT_SE_ADDED); |
| 450 | if (!hdr) |
| 451 | goto free_msg; |
| 452 | |
| 453 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 454 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) || |
| 455 | nla_put_u8(msg, NFC_ATTR_SE_TYPE, type)) |
| 456 | goto nla_put_failure; |
| 457 | |
| 458 | genlmsg_end(msg, hdr); |
| 459 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 460 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | 2757c372 | 2013-05-10 15:47:37 +0200 | [diff] [blame] | 461 | |
| 462 | return 0; |
| 463 | |
| 464 | nla_put_failure: |
Samuel Ortiz | 2757c372 | 2013-05-10 15:47:37 +0200 | [diff] [blame] | 465 | free_msg: |
| 466 | nlmsg_free(msg); |
| 467 | return -EMSGSIZE; |
| 468 | } |
| 469 | |
| 470 | int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx) |
| 471 | { |
| 472 | struct sk_buff *msg; |
| 473 | void *hdr; |
| 474 | |
| 475 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 476 | if (!msg) |
| 477 | return -ENOMEM; |
| 478 | |
| 479 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 480 | NFC_EVENT_SE_REMOVED); |
| 481 | if (!hdr) |
| 482 | goto free_msg; |
| 483 | |
| 484 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 485 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx)) |
| 486 | goto nla_put_failure; |
| 487 | |
| 488 | genlmsg_end(msg, hdr); |
| 489 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 490 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | 2757c372 | 2013-05-10 15:47:37 +0200 | [diff] [blame] | 491 | |
| 492 | return 0; |
| 493 | |
| 494 | nla_put_failure: |
Samuel Ortiz | 2757c372 | 2013-05-10 15:47:37 +0200 | [diff] [blame] | 495 | free_msg: |
| 496 | nlmsg_free(msg); |
| 497 | return -EMSGSIZE; |
| 498 | } |
| 499 | |
Christophe Ricard | 447b27c | 2015-02-01 22:26:16 +0100 | [diff] [blame] | 500 | int nfc_genl_se_transaction(struct nfc_dev *dev, u8 se_idx, |
| 501 | struct nfc_evt_transaction *evt_transaction) |
| 502 | { |
| 503 | struct nfc_se *se; |
| 504 | struct sk_buff *msg; |
| 505 | void *hdr; |
| 506 | |
| 507 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 508 | if (!msg) |
| 509 | return -ENOMEM; |
| 510 | |
| 511 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 512 | NFC_EVENT_SE_TRANSACTION); |
| 513 | if (!hdr) |
| 514 | goto free_msg; |
| 515 | |
| 516 | se = nfc_find_se(dev, se_idx); |
| 517 | if (!se) |
| 518 | goto free_msg; |
| 519 | |
| 520 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 521 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) || |
| 522 | nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type) || |
| 523 | nla_put(msg, NFC_ATTR_SE_AID, evt_transaction->aid_len, |
| 524 | evt_transaction->aid) || |
| 525 | nla_put(msg, NFC_ATTR_SE_PARAMS, evt_transaction->params_len, |
| 526 | evt_transaction->params)) |
| 527 | goto nla_put_failure; |
| 528 | |
| 529 | /* evt_transaction is no more used */ |
| 530 | devm_kfree(&dev->dev, evt_transaction); |
| 531 | |
| 532 | genlmsg_end(msg, hdr); |
| 533 | |
| 534 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
| 535 | |
| 536 | return 0; |
| 537 | |
| 538 | nla_put_failure: |
Christophe Ricard | 447b27c | 2015-02-01 22:26:16 +0100 | [diff] [blame] | 539 | free_msg: |
| 540 | /* evt_transaction is no more used */ |
| 541 | devm_kfree(&dev->dev, evt_transaction); |
| 542 | nlmsg_free(msg); |
| 543 | return -EMSGSIZE; |
| 544 | } |
| 545 | |
Christophe Ricard | 9afec6d | 2015-12-23 23:45:18 +0100 | [diff] [blame] | 546 | int nfc_genl_se_connectivity(struct nfc_dev *dev, u8 se_idx) |
| 547 | { |
| 548 | struct nfc_se *se; |
| 549 | struct sk_buff *msg; |
| 550 | void *hdr; |
| 551 | |
| 552 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 553 | if (!msg) |
| 554 | return -ENOMEM; |
| 555 | |
| 556 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 557 | NFC_EVENT_SE_CONNECTIVITY); |
| 558 | if (!hdr) |
| 559 | goto free_msg; |
| 560 | |
| 561 | se = nfc_find_se(dev, se_idx); |
| 562 | if (!se) |
| 563 | goto free_msg; |
| 564 | |
| 565 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 566 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) || |
| 567 | nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type)) |
| 568 | goto nla_put_failure; |
| 569 | |
| 570 | genlmsg_end(msg, hdr); |
| 571 | |
| 572 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
| 573 | |
| 574 | return 0; |
| 575 | |
| 576 | nla_put_failure: |
Christophe Ricard | 9afec6d | 2015-12-23 23:45:18 +0100 | [diff] [blame] | 577 | free_msg: |
| 578 | nlmsg_free(msg); |
| 579 | return -EMSGSIZE; |
| 580 | } |
| 581 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 582 | static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 583 | u32 portid, u32 seq, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 584 | struct netlink_callback *cb, |
| 585 | int flags) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 586 | { |
| 587 | void *hdr; |
| 588 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 589 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 590 | NFC_CMD_GET_DEVICE); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 591 | if (!hdr) |
| 592 | return -EMSGSIZE; |
| 593 | |
| 594 | if (cb) |
Michal Kubecek | 0a833c2 | 2017-11-15 13:09:32 +0100 | [diff] [blame] | 595 | genl_dump_check_consistent(cb, hdr); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 596 | |
OGAWA Hirofumi | 85a2566 | 2017-02-04 10:15:55 +0900 | [diff] [blame] | 597 | if (nfc_genl_setup_device_added(dev, msg)) |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 598 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 599 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 600 | genlmsg_end(msg, hdr); |
| 601 | return 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 602 | |
| 603 | nla_put_failure: |
| 604 | genlmsg_cancel(msg, hdr); |
| 605 | return -EMSGSIZE; |
| 606 | } |
| 607 | |
| 608 | static int nfc_genl_dump_devices(struct sk_buff *skb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 609 | struct netlink_callback *cb) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 610 | { |
| 611 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 612 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 613 | bool first_call = false; |
| 614 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 615 | if (!iter) { |
| 616 | first_call = true; |
| 617 | iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); |
| 618 | if (!iter) |
| 619 | return -ENOMEM; |
| 620 | cb->args[0] = (long) iter; |
| 621 | } |
| 622 | |
| 623 | mutex_lock(&nfc_devlist_mutex); |
| 624 | |
| 625 | cb->seq = nfc_devlist_generation; |
| 626 | |
| 627 | if (first_call) { |
| 628 | nfc_device_iter_init(iter); |
| 629 | dev = nfc_device_iter_next(iter); |
| 630 | } |
| 631 | |
| 632 | while (dev) { |
| 633 | int rc; |
| 634 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 635 | rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 636 | cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 637 | if (rc < 0) |
| 638 | break; |
| 639 | |
| 640 | dev = nfc_device_iter_next(iter); |
| 641 | } |
| 642 | |
| 643 | mutex_unlock(&nfc_devlist_mutex); |
| 644 | |
| 645 | cb->args[1] = (long) dev; |
| 646 | |
| 647 | return skb->len; |
| 648 | } |
| 649 | |
| 650 | static int nfc_genl_dump_devices_done(struct netlink_callback *cb) |
| 651 | { |
| 652 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 653 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 654 | nfc_device_iter_exit(iter); |
| 655 | kfree(iter); |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 660 | int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 661 | u8 comm_mode, u8 rf_mode) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 662 | { |
| 663 | struct sk_buff *msg; |
| 664 | void *hdr; |
| 665 | |
| 666 | pr_debug("DEP link is up\n"); |
| 667 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 668 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 669 | if (!msg) |
| 670 | return -ENOMEM; |
| 671 | |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 672 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 673 | if (!hdr) |
| 674 | goto free_msg; |
| 675 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 676 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 677 | goto nla_put_failure; |
| 678 | if (rf_mode == NFC_RF_INITIATOR && |
| 679 | nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) |
| 680 | goto nla_put_failure; |
| 681 | if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) || |
| 682 | nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode)) |
| 683 | goto nla_put_failure; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 684 | |
| 685 | genlmsg_end(msg, hdr); |
| 686 | |
| 687 | dev->dep_link_up = true; |
| 688 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 689 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 690 | |
| 691 | return 0; |
| 692 | |
| 693 | nla_put_failure: |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 694 | free_msg: |
| 695 | nlmsg_free(msg); |
| 696 | return -EMSGSIZE; |
| 697 | } |
| 698 | |
| 699 | int nfc_genl_dep_link_down_event(struct nfc_dev *dev) |
| 700 | { |
| 701 | struct sk_buff *msg; |
| 702 | void *hdr; |
| 703 | |
| 704 | pr_debug("DEP link is down\n"); |
| 705 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 706 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 707 | if (!msg) |
| 708 | return -ENOMEM; |
| 709 | |
| 710 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 711 | NFC_CMD_DEP_LINK_DOWN); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 712 | if (!hdr) |
| 713 | goto free_msg; |
| 714 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 715 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 716 | goto nla_put_failure; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 717 | |
| 718 | genlmsg_end(msg, hdr); |
| 719 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 720 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 721 | |
| 722 | return 0; |
| 723 | |
| 724 | nla_put_failure: |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 725 | free_msg: |
| 726 | nlmsg_free(msg); |
| 727 | return -EMSGSIZE; |
| 728 | } |
| 729 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 730 | static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info) |
| 731 | { |
| 732 | struct sk_buff *msg; |
| 733 | struct nfc_dev *dev; |
| 734 | u32 idx; |
| 735 | int rc = -ENOBUFS; |
| 736 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 737 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 738 | return -EINVAL; |
| 739 | |
| 740 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 741 | |
| 742 | dev = nfc_get_device(idx); |
| 743 | if (!dev) |
| 744 | return -ENODEV; |
| 745 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 746 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 747 | if (!msg) { |
| 748 | rc = -ENOMEM; |
| 749 | goto out_putdev; |
| 750 | } |
| 751 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 752 | rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 753 | NULL, 0); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 754 | if (rc < 0) |
| 755 | goto out_free; |
| 756 | |
| 757 | nfc_put_device(dev); |
| 758 | |
| 759 | return genlmsg_reply(msg, info); |
| 760 | |
| 761 | out_free: |
| 762 | nlmsg_free(msg); |
| 763 | out_putdev: |
| 764 | nfc_put_device(dev); |
| 765 | return rc; |
| 766 | } |
| 767 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 768 | static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info) |
| 769 | { |
| 770 | struct nfc_dev *dev; |
| 771 | int rc; |
| 772 | u32 idx; |
| 773 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 774 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 775 | return -EINVAL; |
| 776 | |
| 777 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 778 | |
| 779 | dev = nfc_get_device(idx); |
| 780 | if (!dev) |
| 781 | return -ENODEV; |
| 782 | |
| 783 | rc = nfc_dev_up(dev); |
| 784 | |
| 785 | nfc_put_device(dev); |
| 786 | return rc; |
| 787 | } |
| 788 | |
| 789 | static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info) |
| 790 | { |
| 791 | struct nfc_dev *dev; |
| 792 | int rc; |
| 793 | u32 idx; |
| 794 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 795 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 796 | return -EINVAL; |
| 797 | |
| 798 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 799 | |
| 800 | dev = nfc_get_device(idx); |
| 801 | if (!dev) |
| 802 | return -ENODEV; |
| 803 | |
| 804 | rc = nfc_dev_down(dev); |
| 805 | |
| 806 | nfc_put_device(dev); |
| 807 | return rc; |
| 808 | } |
| 809 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 810 | static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info) |
| 811 | { |
| 812 | struct nfc_dev *dev; |
| 813 | int rc; |
| 814 | u32 idx; |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 815 | u32 im_protocols = 0, tm_protocols = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 816 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 817 | pr_debug("Poll start\n"); |
| 818 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 819 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 820 | ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] && |
| 821 | !info->attrs[NFC_ATTR_PROTOCOLS]) && |
Szymon Janc | 0f45077 | 2012-10-17 15:23:39 +0200 | [diff] [blame] | 822 | !info->attrs[NFC_ATTR_TM_PROTOCOLS])) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 823 | return -EINVAL; |
| 824 | |
| 825 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 826 | |
| 827 | if (info->attrs[NFC_ATTR_TM_PROTOCOLS]) |
| 828 | tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 829 | |
| 830 | if (info->attrs[NFC_ATTR_IM_PROTOCOLS]) |
| 831 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]); |
Samuel Ortiz | 5e50ee3 | 2012-05-31 11:48:58 +0200 | [diff] [blame] | 832 | else if (info->attrs[NFC_ATTR_PROTOCOLS]) |
| 833 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 834 | |
| 835 | dev = nfc_get_device(idx); |
| 836 | if (!dev) |
| 837 | return -ENODEV; |
| 838 | |
| 839 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 840 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 841 | rc = nfc_start_poll(dev, im_protocols, tm_protocols); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 842 | if (!rc) |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 843 | dev->genl_data.poll_req_portid = info->snd_portid; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 844 | |
| 845 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 846 | |
| 847 | nfc_put_device(dev); |
| 848 | return rc; |
| 849 | } |
| 850 | |
| 851 | static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info) |
| 852 | { |
| 853 | struct nfc_dev *dev; |
| 854 | int rc; |
| 855 | u32 idx; |
| 856 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 857 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 858 | return -EINVAL; |
| 859 | |
| 860 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 861 | |
| 862 | dev = nfc_get_device(idx); |
| 863 | if (!dev) |
| 864 | return -ENODEV; |
| 865 | |
Samuel Ortiz | a831b91 | 2012-06-28 16:41:57 +0200 | [diff] [blame] | 866 | device_lock(&dev->dev); |
| 867 | |
| 868 | if (!dev->polling) { |
| 869 | device_unlock(&dev->dev); |
| 870 | return -EINVAL; |
| 871 | } |
| 872 | |
| 873 | device_unlock(&dev->dev); |
| 874 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 875 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 876 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 877 | if (dev->genl_data.poll_req_portid != info->snd_portid) { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 878 | rc = -EBUSY; |
| 879 | goto out; |
| 880 | } |
| 881 | |
| 882 | rc = nfc_stop_poll(dev); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 883 | dev->genl_data.poll_req_portid = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 884 | |
| 885 | out: |
| 886 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 887 | nfc_put_device(dev); |
| 888 | return rc; |
| 889 | } |
| 890 | |
Christophe Ricard | 3682f49 | 2014-12-02 21:27:50 +0100 | [diff] [blame] | 891 | static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info) |
| 892 | { |
| 893 | struct nfc_dev *dev; |
| 894 | u32 device_idx, target_idx, protocol; |
| 895 | int rc; |
| 896 | |
Mateusz Jurczyk | a0323b9 | 2017-05-24 12:42:26 +0200 | [diff] [blame] | 897 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 898 | !info->attrs[NFC_ATTR_TARGET_INDEX] || |
| 899 | !info->attrs[NFC_ATTR_PROTOCOLS]) |
Christophe Ricard | 3682f49 | 2014-12-02 21:27:50 +0100 | [diff] [blame] | 900 | return -EINVAL; |
| 901 | |
| 902 | device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 903 | |
| 904 | dev = nfc_get_device(device_idx); |
| 905 | if (!dev) |
| 906 | return -ENODEV; |
| 907 | |
| 908 | target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); |
| 909 | protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); |
| 910 | |
Christophe Ricard | 96d4581 | 2015-10-25 22:54:43 +0100 | [diff] [blame] | 911 | nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP); |
Christophe Ricard | 3682f49 | 2014-12-02 21:27:50 +0100 | [diff] [blame] | 912 | rc = nfc_activate_target(dev, target_idx, protocol); |
| 913 | |
| 914 | nfc_put_device(dev); |
Andy Shevchenko | 3267183 | 2017-03-22 21:20:58 +0200 | [diff] [blame] | 915 | return rc; |
Christophe Ricard | 3682f49 | 2014-12-02 21:27:50 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Mark Greer | 4d63adf | 2017-06-15 20:34:22 -0700 | [diff] [blame] | 918 | static int nfc_genl_deactivate_target(struct sk_buff *skb, |
| 919 | struct genl_info *info) |
| 920 | { |
| 921 | struct nfc_dev *dev; |
| 922 | u32 device_idx, target_idx; |
| 923 | int rc; |
| 924 | |
| 925 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 926 | return -EINVAL; |
| 927 | |
| 928 | device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 929 | |
| 930 | dev = nfc_get_device(device_idx); |
| 931 | if (!dev) |
| 932 | return -ENODEV; |
| 933 | |
| 934 | target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); |
| 935 | |
| 936 | rc = nfc_deactivate_target(dev, target_idx, NFC_TARGET_MODE_SLEEP); |
| 937 | |
| 938 | nfc_put_device(dev); |
| 939 | return rc; |
| 940 | } |
| 941 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 942 | static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info) |
| 943 | { |
| 944 | struct nfc_dev *dev; |
| 945 | int rc, tgt_idx; |
| 946 | u32 idx; |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 947 | u8 comm; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 948 | |
| 949 | pr_debug("DEP link up\n"); |
| 950 | |
| 951 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 952 | !info->attrs[NFC_ATTR_COMM_MODE]) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 953 | return -EINVAL; |
| 954 | |
| 955 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 956 | if (!info->attrs[NFC_ATTR_TARGET_INDEX]) |
| 957 | tgt_idx = NFC_TARGET_IDX_ANY; |
| 958 | else |
| 959 | tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); |
| 960 | |
| 961 | comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 962 | |
| 963 | if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE) |
| 964 | return -EINVAL; |
| 965 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 966 | dev = nfc_get_device(idx); |
| 967 | if (!dev) |
| 968 | return -ENODEV; |
| 969 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 970 | rc = nfc_dep_link_up(dev, tgt_idx, comm); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 971 | |
| 972 | nfc_put_device(dev); |
| 973 | |
| 974 | return rc; |
| 975 | } |
| 976 | |
| 977 | static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info) |
| 978 | { |
| 979 | struct nfc_dev *dev; |
| 980 | int rc; |
| 981 | u32 idx; |
| 982 | |
| 983 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 984 | return -EINVAL; |
| 985 | |
| 986 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 987 | |
| 988 | dev = nfc_get_device(idx); |
| 989 | if (!dev) |
| 990 | return -ENODEV; |
| 991 | |
| 992 | rc = nfc_dep_link_down(dev); |
| 993 | |
| 994 | nfc_put_device(dev); |
| 995 | return rc; |
| 996 | } |
| 997 | |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 998 | static int nfc_genl_send_params(struct sk_buff *msg, |
| 999 | struct nfc_llcp_local *local, |
| 1000 | u32 portid, u32 seq) |
| 1001 | { |
| 1002 | void *hdr; |
| 1003 | |
| 1004 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0, |
| 1005 | NFC_CMD_LLC_GET_PARAMS); |
| 1006 | if (!hdr) |
| 1007 | return -EMSGSIZE; |
| 1008 | |
| 1009 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) || |
| 1010 | nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) || |
| 1011 | nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) || |
| 1012 | nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux))) |
| 1013 | goto nla_put_failure; |
| 1014 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1015 | genlmsg_end(msg, hdr); |
| 1016 | return 0; |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 1017 | |
| 1018 | nla_put_failure: |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 1019 | genlmsg_cancel(msg, hdr); |
| 1020 | return -EMSGSIZE; |
| 1021 | } |
| 1022 | |
| 1023 | static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info) |
| 1024 | { |
| 1025 | struct nfc_dev *dev; |
| 1026 | struct nfc_llcp_local *local; |
| 1027 | int rc = 0; |
| 1028 | struct sk_buff *msg = NULL; |
| 1029 | u32 idx; |
| 1030 | |
| 1031 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 1032 | return -EINVAL; |
| 1033 | |
| 1034 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1035 | |
| 1036 | dev = nfc_get_device(idx); |
| 1037 | if (!dev) |
| 1038 | return -ENODEV; |
| 1039 | |
| 1040 | device_lock(&dev->dev); |
| 1041 | |
| 1042 | local = nfc_llcp_find_local(dev); |
| 1043 | if (!local) { |
| 1044 | rc = -ENODEV; |
| 1045 | goto exit; |
| 1046 | } |
| 1047 | |
| 1048 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1049 | if (!msg) { |
| 1050 | rc = -ENOMEM; |
| 1051 | goto exit; |
| 1052 | } |
| 1053 | |
| 1054 | rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq); |
| 1055 | |
| 1056 | exit: |
| 1057 | device_unlock(&dev->dev); |
| 1058 | |
| 1059 | nfc_put_device(dev); |
| 1060 | |
| 1061 | if (rc < 0) { |
| 1062 | if (msg) |
| 1063 | nlmsg_free(msg); |
| 1064 | |
| 1065 | return rc; |
| 1066 | } |
| 1067 | |
| 1068 | return genlmsg_reply(msg, info); |
| 1069 | } |
| 1070 | |
| 1071 | static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info) |
| 1072 | { |
| 1073 | struct nfc_dev *dev; |
| 1074 | struct nfc_llcp_local *local; |
| 1075 | u8 rw = 0; |
| 1076 | u16 miux = 0; |
| 1077 | u32 idx; |
| 1078 | int rc = 0; |
| 1079 | |
| 1080 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 1081 | (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] && |
| 1082 | !info->attrs[NFC_ATTR_LLC_PARAM_RW] && |
| 1083 | !info->attrs[NFC_ATTR_LLC_PARAM_MIUX])) |
| 1084 | return -EINVAL; |
| 1085 | |
| 1086 | if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) { |
| 1087 | rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]); |
| 1088 | |
| 1089 | if (rw > LLCP_MAX_RW) |
| 1090 | return -EINVAL; |
| 1091 | } |
| 1092 | |
| 1093 | if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) { |
| 1094 | miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]); |
| 1095 | |
| 1096 | if (miux > LLCP_MAX_MIUX) |
| 1097 | return -EINVAL; |
| 1098 | } |
| 1099 | |
| 1100 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1101 | |
| 1102 | dev = nfc_get_device(idx); |
| 1103 | if (!dev) |
| 1104 | return -ENODEV; |
| 1105 | |
| 1106 | device_lock(&dev->dev); |
| 1107 | |
| 1108 | local = nfc_llcp_find_local(dev); |
| 1109 | if (!local) { |
| 1110 | nfc_put_device(dev); |
| 1111 | rc = -ENODEV; |
| 1112 | goto exit; |
| 1113 | } |
| 1114 | |
| 1115 | if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) { |
| 1116 | if (dev->dep_link_up) { |
| 1117 | rc = -EINPROGRESS; |
| 1118 | goto exit; |
| 1119 | } |
| 1120 | |
| 1121 | local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]); |
| 1122 | } |
| 1123 | |
| 1124 | if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) |
| 1125 | local->rw = rw; |
| 1126 | |
| 1127 | if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) |
| 1128 | local->miux = cpu_to_be16(miux); |
| 1129 | |
| 1130 | exit: |
| 1131 | device_unlock(&dev->dev); |
| 1132 | |
| 1133 | nfc_put_device(dev); |
| 1134 | |
| 1135 | return rc; |
| 1136 | } |
| 1137 | |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 1138 | static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info) |
| 1139 | { |
| 1140 | struct nfc_dev *dev; |
| 1141 | struct nfc_llcp_local *local; |
| 1142 | struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1]; |
| 1143 | u32 idx; |
| 1144 | u8 tid; |
| 1145 | char *uri; |
| 1146 | int rc = 0, rem; |
| 1147 | size_t uri_len, tlvs_len; |
| 1148 | struct hlist_head sdreq_list; |
| 1149 | struct nfc_llcp_sdp_tlv *sdreq; |
| 1150 | |
| 1151 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 1152 | !info->attrs[NFC_ATTR_LLC_SDP]) |
| 1153 | return -EINVAL; |
| 1154 | |
| 1155 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1156 | |
| 1157 | dev = nfc_get_device(idx); |
Julia Lawall | 9abebb8 | 2015-10-17 11:32:19 +0200 | [diff] [blame] | 1158 | if (!dev) |
| 1159 | return -ENODEV; |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 1160 | |
| 1161 | device_lock(&dev->dev); |
| 1162 | |
| 1163 | if (dev->dep_link_up == false) { |
| 1164 | rc = -ENOLINK; |
| 1165 | goto exit; |
| 1166 | } |
| 1167 | |
| 1168 | local = nfc_llcp_find_local(dev); |
| 1169 | if (!local) { |
| 1170 | nfc_put_device(dev); |
| 1171 | rc = -ENODEV; |
| 1172 | goto exit; |
| 1173 | } |
| 1174 | |
| 1175 | INIT_HLIST_HEAD(&sdreq_list); |
| 1176 | |
| 1177 | tlvs_len = 0; |
| 1178 | |
| 1179 | nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) { |
| 1180 | rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr, |
Johannes Berg | fe52145 | 2017-04-12 14:34:08 +0200 | [diff] [blame] | 1181 | nfc_sdp_genl_policy, info->extack); |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 1182 | |
| 1183 | if (rc != 0) { |
| 1184 | rc = -EINVAL; |
| 1185 | goto exit; |
| 1186 | } |
| 1187 | |
| 1188 | if (!sdp_attrs[NFC_SDP_ATTR_URI]) |
| 1189 | continue; |
| 1190 | |
| 1191 | uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]); |
| 1192 | if (uri_len == 0) |
| 1193 | continue; |
| 1194 | |
| 1195 | uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]); |
| 1196 | if (uri == NULL || *uri == 0) |
| 1197 | continue; |
| 1198 | |
| 1199 | tid = local->sdreq_next_tid++; |
| 1200 | |
| 1201 | sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len); |
| 1202 | if (sdreq == NULL) { |
| 1203 | rc = -ENOMEM; |
| 1204 | goto exit; |
| 1205 | } |
| 1206 | |
| 1207 | tlvs_len += sdreq->tlv_len; |
| 1208 | |
| 1209 | hlist_add_head(&sdreq->node, &sdreq_list); |
| 1210 | } |
| 1211 | |
| 1212 | if (hlist_empty(&sdreq_list)) { |
| 1213 | rc = -EINVAL; |
| 1214 | goto exit; |
| 1215 | } |
| 1216 | |
| 1217 | rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len); |
| 1218 | exit: |
| 1219 | device_unlock(&dev->dev); |
| 1220 | |
| 1221 | nfc_put_device(dev); |
| 1222 | |
| 1223 | return rc; |
| 1224 | } |
| 1225 | |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 1226 | static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info) |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1227 | { |
| 1228 | struct nfc_dev *dev; |
| 1229 | int rc; |
| 1230 | u32 idx; |
| 1231 | char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1]; |
| 1232 | |
| 1233 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 1234 | return -EINVAL; |
| 1235 | |
| 1236 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1237 | |
| 1238 | dev = nfc_get_device(idx); |
| 1239 | if (!dev) |
| 1240 | return -ENODEV; |
| 1241 | |
| 1242 | nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME], |
| 1243 | sizeof(firmware_name)); |
| 1244 | |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 1245 | rc = nfc_fw_download(dev, firmware_name); |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1246 | |
| 1247 | nfc_put_device(dev); |
| 1248 | return rc; |
| 1249 | } |
| 1250 | |
Eric Lapuyade | 352a5f5 | 2013-07-19 14:57:55 +0200 | [diff] [blame] | 1251 | int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name, |
| 1252 | u32 result) |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1253 | { |
| 1254 | struct sk_buff *msg; |
| 1255 | void *hdr; |
| 1256 | |
| 1257 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1258 | if (!msg) |
| 1259 | return -ENOMEM; |
| 1260 | |
| 1261 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 1262 | NFC_CMD_FW_DOWNLOAD); |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1263 | if (!hdr) |
| 1264 | goto free_msg; |
| 1265 | |
| 1266 | if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) || |
Eric Lapuyade | 352a5f5 | 2013-07-19 14:57:55 +0200 | [diff] [blame] | 1267 | nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) || |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1268 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 1269 | goto nla_put_failure; |
| 1270 | |
| 1271 | genlmsg_end(msg, hdr); |
| 1272 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1273 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1274 | |
| 1275 | return 0; |
| 1276 | |
| 1277 | nla_put_failure: |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1278 | free_msg: |
| 1279 | nlmsg_free(msg); |
| 1280 | return -EMSGSIZE; |
| 1281 | } |
| 1282 | |
Samuel Ortiz | be08565 | 2013-05-10 17:07:32 +0200 | [diff] [blame] | 1283 | static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info) |
| 1284 | { |
| 1285 | struct nfc_dev *dev; |
| 1286 | int rc; |
| 1287 | u32 idx, se_idx; |
| 1288 | |
| 1289 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 1290 | !info->attrs[NFC_ATTR_SE_INDEX]) |
| 1291 | return -EINVAL; |
| 1292 | |
| 1293 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1294 | se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); |
| 1295 | |
| 1296 | dev = nfc_get_device(idx); |
| 1297 | if (!dev) |
| 1298 | return -ENODEV; |
| 1299 | |
| 1300 | rc = nfc_enable_se(dev, se_idx); |
| 1301 | |
| 1302 | nfc_put_device(dev); |
| 1303 | return rc; |
| 1304 | } |
| 1305 | |
| 1306 | static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info) |
| 1307 | { |
| 1308 | struct nfc_dev *dev; |
| 1309 | int rc; |
| 1310 | u32 idx, se_idx; |
| 1311 | |
| 1312 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 1313 | !info->attrs[NFC_ATTR_SE_INDEX]) |
| 1314 | return -EINVAL; |
| 1315 | |
| 1316 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1317 | se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); |
| 1318 | |
| 1319 | dev = nfc_get_device(idx); |
| 1320 | if (!dev) |
| 1321 | return -ENODEV; |
| 1322 | |
| 1323 | rc = nfc_disable_se(dev, se_idx); |
| 1324 | |
| 1325 | nfc_put_device(dev); |
| 1326 | return rc; |
| 1327 | } |
| 1328 | |
Samuel Ortiz | ac22ac4 | 2013-07-24 18:10:50 +0200 | [diff] [blame] | 1329 | static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev, |
| 1330 | u32 portid, u32 seq, |
| 1331 | struct netlink_callback *cb, |
| 1332 | int flags) |
| 1333 | { |
| 1334 | void *hdr; |
| 1335 | struct nfc_se *se, *n; |
| 1336 | |
| 1337 | list_for_each_entry_safe(se, n, &dev->secure_elements, list) { |
| 1338 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, |
| 1339 | NFC_CMD_GET_SE); |
| 1340 | if (!hdr) |
| 1341 | goto nla_put_failure; |
| 1342 | |
| 1343 | if (cb) |
Michal Kubecek | 0a833c2 | 2017-11-15 13:09:32 +0100 | [diff] [blame] | 1344 | genl_dump_check_consistent(cb, hdr); |
Samuel Ortiz | ac22ac4 | 2013-07-24 18:10:50 +0200 | [diff] [blame] | 1345 | |
| 1346 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 1347 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) || |
| 1348 | nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type)) |
| 1349 | goto nla_put_failure; |
| 1350 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1351 | genlmsg_end(msg, hdr); |
Samuel Ortiz | ac22ac4 | 2013-07-24 18:10:50 +0200 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | return 0; |
| 1355 | |
| 1356 | nla_put_failure: |
| 1357 | genlmsg_cancel(msg, hdr); |
| 1358 | return -EMSGSIZE; |
| 1359 | } |
| 1360 | |
| 1361 | static int nfc_genl_dump_ses(struct sk_buff *skb, |
| 1362 | struct netlink_callback *cb) |
| 1363 | { |
| 1364 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 1365 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 1366 | bool first_call = false; |
| 1367 | |
| 1368 | if (!iter) { |
| 1369 | first_call = true; |
| 1370 | iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); |
| 1371 | if (!iter) |
| 1372 | return -ENOMEM; |
| 1373 | cb->args[0] = (long) iter; |
| 1374 | } |
| 1375 | |
| 1376 | mutex_lock(&nfc_devlist_mutex); |
| 1377 | |
| 1378 | cb->seq = nfc_devlist_generation; |
| 1379 | |
| 1380 | if (first_call) { |
| 1381 | nfc_device_iter_init(iter); |
| 1382 | dev = nfc_device_iter_next(iter); |
| 1383 | } |
| 1384 | |
| 1385 | while (dev) { |
| 1386 | int rc; |
| 1387 | |
| 1388 | rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid, |
| 1389 | cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); |
| 1390 | if (rc < 0) |
| 1391 | break; |
| 1392 | |
| 1393 | dev = nfc_device_iter_next(iter); |
| 1394 | } |
| 1395 | |
| 1396 | mutex_unlock(&nfc_devlist_mutex); |
| 1397 | |
| 1398 | cb->args[1] = (long) dev; |
| 1399 | |
| 1400 | return skb->len; |
| 1401 | } |
| 1402 | |
| 1403 | static int nfc_genl_dump_ses_done(struct netlink_callback *cb) |
| 1404 | { |
| 1405 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 1406 | |
| 1407 | nfc_device_iter_exit(iter); |
| 1408 | kfree(iter); |
| 1409 | |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
Christophe Ricard | cd96db6 | 2014-12-02 21:27:51 +0100 | [diff] [blame] | 1413 | static int nfc_se_io(struct nfc_dev *dev, u32 se_idx, |
| 1414 | u8 *apdu, size_t apdu_length, |
| 1415 | se_io_cb_t cb, void *cb_context) |
| 1416 | { |
| 1417 | struct nfc_se *se; |
| 1418 | int rc; |
| 1419 | |
| 1420 | pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx); |
| 1421 | |
| 1422 | device_lock(&dev->dev); |
| 1423 | |
| 1424 | if (!device_is_registered(&dev->dev)) { |
| 1425 | rc = -ENODEV; |
| 1426 | goto error; |
| 1427 | } |
| 1428 | |
| 1429 | if (!dev->dev_up) { |
| 1430 | rc = -ENODEV; |
| 1431 | goto error; |
| 1432 | } |
| 1433 | |
| 1434 | if (!dev->ops->se_io) { |
| 1435 | rc = -EOPNOTSUPP; |
| 1436 | goto error; |
| 1437 | } |
| 1438 | |
| 1439 | se = nfc_find_se(dev, se_idx); |
| 1440 | if (!se) { |
| 1441 | rc = -EINVAL; |
| 1442 | goto error; |
| 1443 | } |
| 1444 | |
| 1445 | if (se->state != NFC_SE_ENABLED) { |
| 1446 | rc = -ENODEV; |
| 1447 | goto error; |
| 1448 | } |
| 1449 | |
| 1450 | rc = dev->ops->se_io(dev, se_idx, apdu, |
| 1451 | apdu_length, cb, cb_context); |
| 1452 | |
| 1453 | error: |
| 1454 | device_unlock(&dev->dev); |
| 1455 | return rc; |
| 1456 | } |
| 1457 | |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 1458 | struct se_io_ctx { |
| 1459 | u32 dev_idx; |
| 1460 | u32 se_idx; |
| 1461 | }; |
| 1462 | |
Samuel Ortiz | ddc1a70 | 2013-10-07 14:18:44 +0200 | [diff] [blame] | 1463 | static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err) |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 1464 | { |
| 1465 | struct se_io_ctx *ctx = context; |
| 1466 | struct sk_buff *msg; |
| 1467 | void *hdr; |
| 1468 | |
| 1469 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1470 | if (!msg) { |
| 1471 | kfree(ctx); |
| 1472 | return; |
| 1473 | } |
| 1474 | |
| 1475 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 1476 | NFC_CMD_SE_IO); |
| 1477 | if (!hdr) |
| 1478 | goto free_msg; |
| 1479 | |
| 1480 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) || |
| 1481 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) || |
| 1482 | nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu)) |
| 1483 | goto nla_put_failure; |
| 1484 | |
| 1485 | genlmsg_end(msg, hdr); |
| 1486 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1487 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 1488 | |
| 1489 | kfree(ctx); |
| 1490 | |
| 1491 | return; |
| 1492 | |
| 1493 | nla_put_failure: |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 1494 | free_msg: |
| 1495 | nlmsg_free(msg); |
| 1496 | kfree(ctx); |
| 1497 | |
| 1498 | return; |
| 1499 | } |
| 1500 | |
| 1501 | static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info) |
| 1502 | { |
| 1503 | struct nfc_dev *dev; |
| 1504 | struct se_io_ctx *ctx; |
| 1505 | u32 dev_idx, se_idx; |
| 1506 | u8 *apdu; |
| 1507 | size_t apdu_len; |
| 1508 | |
| 1509 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 1510 | !info->attrs[NFC_ATTR_SE_INDEX] || |
| 1511 | !info->attrs[NFC_ATTR_SE_APDU]) |
| 1512 | return -EINVAL; |
| 1513 | |
| 1514 | dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1515 | se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); |
| 1516 | |
| 1517 | dev = nfc_get_device(dev_idx); |
| 1518 | if (!dev) |
| 1519 | return -ENODEV; |
| 1520 | |
| 1521 | if (!dev->ops || !dev->ops->se_io) |
| 1522 | return -ENOTSUPP; |
| 1523 | |
| 1524 | apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]); |
| 1525 | if (apdu_len == 0) |
| 1526 | return -EINVAL; |
| 1527 | |
| 1528 | apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]); |
| 1529 | if (!apdu) |
| 1530 | return -EINVAL; |
| 1531 | |
| 1532 | ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL); |
| 1533 | if (!ctx) |
| 1534 | return -ENOMEM; |
| 1535 | |
| 1536 | ctx->dev_idx = dev_idx; |
| 1537 | ctx->se_idx = se_idx; |
| 1538 | |
Christophe Ricard | cd96db6 | 2014-12-02 21:27:51 +0100 | [diff] [blame] | 1539 | return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx); |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 1540 | } |
| 1541 | |
Samuel Ortiz | 9e58095 | 2014-10-14 02:19:46 +0200 | [diff] [blame] | 1542 | static int nfc_genl_vendor_cmd(struct sk_buff *skb, |
| 1543 | struct genl_info *info) |
| 1544 | { |
| 1545 | struct nfc_dev *dev; |
| 1546 | struct nfc_vendor_cmd *cmd; |
| 1547 | u32 dev_idx, vid, subcmd; |
| 1548 | u8 *data; |
| 1549 | size_t data_len; |
Christophe Ricard | 29e7692 | 2015-08-19 21:26:43 +0200 | [diff] [blame] | 1550 | int i, err; |
Samuel Ortiz | 9e58095 | 2014-10-14 02:19:46 +0200 | [diff] [blame] | 1551 | |
| 1552 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 1553 | !info->attrs[NFC_ATTR_VENDOR_ID] || |
| 1554 | !info->attrs[NFC_ATTR_VENDOR_SUBCMD]) |
| 1555 | return -EINVAL; |
| 1556 | |
| 1557 | dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1558 | vid = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_ID]); |
| 1559 | subcmd = nla_get_u32(info->attrs[NFC_ATTR_VENDOR_SUBCMD]); |
| 1560 | |
| 1561 | dev = nfc_get_device(dev_idx); |
| 1562 | if (!dev || !dev->vendor_cmds || !dev->n_vendor_cmds) |
| 1563 | return -ENODEV; |
| 1564 | |
Christophe Ricard | fe202fe | 2015-08-14 22:33:40 +0200 | [diff] [blame] | 1565 | if (info->attrs[NFC_ATTR_VENDOR_DATA]) { |
| 1566 | data = nla_data(info->attrs[NFC_ATTR_VENDOR_DATA]); |
Samuel Ortiz | 9e58095 | 2014-10-14 02:19:46 +0200 | [diff] [blame] | 1567 | data_len = nla_len(info->attrs[NFC_ATTR_VENDOR_DATA]); |
| 1568 | if (data_len == 0) |
| 1569 | return -EINVAL; |
| 1570 | } else { |
Christophe Ricard | adca3c3 | 2015-08-17 08:33:43 +0200 | [diff] [blame] | 1571 | data = NULL; |
Samuel Ortiz | 9e58095 | 2014-10-14 02:19:46 +0200 | [diff] [blame] | 1572 | data_len = 0; |
| 1573 | } |
| 1574 | |
| 1575 | for (i = 0; i < dev->n_vendor_cmds; i++) { |
| 1576 | cmd = &dev->vendor_cmds[i]; |
| 1577 | |
| 1578 | if (cmd->vendor_id != vid || cmd->subcmd != subcmd) |
| 1579 | continue; |
| 1580 | |
Christophe Ricard | 29e7692 | 2015-08-19 21:26:43 +0200 | [diff] [blame] | 1581 | dev->cur_cmd_info = info; |
| 1582 | err = cmd->doit(dev, data, data_len); |
| 1583 | dev->cur_cmd_info = NULL; |
| 1584 | return err; |
Samuel Ortiz | 9e58095 | 2014-10-14 02:19:46 +0200 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | return -EOPNOTSUPP; |
| 1588 | } |
| 1589 | |
Christophe Ricard | 29e7692 | 2015-08-19 21:26:43 +0200 | [diff] [blame] | 1590 | /* message building helper */ |
| 1591 | static inline void *nfc_hdr_put(struct sk_buff *skb, u32 portid, u32 seq, |
| 1592 | int flags, u8 cmd) |
| 1593 | { |
| 1594 | /* since there is no private header just add the generic one */ |
| 1595 | return genlmsg_put(skb, portid, seq, &nfc_genl_family, flags, cmd); |
| 1596 | } |
| 1597 | |
| 1598 | static struct sk_buff * |
| 1599 | __nfc_alloc_vendor_cmd_skb(struct nfc_dev *dev, int approxlen, |
| 1600 | u32 portid, u32 seq, |
| 1601 | enum nfc_attrs attr, |
| 1602 | u32 oui, u32 subcmd, gfp_t gfp) |
| 1603 | { |
| 1604 | struct sk_buff *skb; |
| 1605 | void *hdr; |
| 1606 | |
| 1607 | skb = nlmsg_new(approxlen + 100, gfp); |
| 1608 | if (!skb) |
| 1609 | return NULL; |
| 1610 | |
| 1611 | hdr = nfc_hdr_put(skb, portid, seq, 0, NFC_CMD_VENDOR); |
| 1612 | if (!hdr) { |
| 1613 | kfree_skb(skb); |
| 1614 | return NULL; |
| 1615 | } |
| 1616 | |
| 1617 | if (nla_put_u32(skb, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 1618 | goto nla_put_failure; |
| 1619 | if (nla_put_u32(skb, NFC_ATTR_VENDOR_ID, oui)) |
| 1620 | goto nla_put_failure; |
| 1621 | if (nla_put_u32(skb, NFC_ATTR_VENDOR_SUBCMD, subcmd)) |
| 1622 | goto nla_put_failure; |
| 1623 | |
| 1624 | ((void **)skb->cb)[0] = dev; |
| 1625 | ((void **)skb->cb)[1] = hdr; |
| 1626 | |
| 1627 | return skb; |
| 1628 | |
| 1629 | nla_put_failure: |
| 1630 | kfree_skb(skb); |
| 1631 | return NULL; |
| 1632 | } |
| 1633 | |
| 1634 | struct sk_buff *__nfc_alloc_vendor_cmd_reply_skb(struct nfc_dev *dev, |
| 1635 | enum nfc_attrs attr, |
| 1636 | u32 oui, u32 subcmd, |
| 1637 | int approxlen) |
| 1638 | { |
| 1639 | if (WARN_ON(!dev->cur_cmd_info)) |
| 1640 | return NULL; |
| 1641 | |
| 1642 | return __nfc_alloc_vendor_cmd_skb(dev, approxlen, |
| 1643 | dev->cur_cmd_info->snd_portid, |
| 1644 | dev->cur_cmd_info->snd_seq, attr, |
| 1645 | oui, subcmd, GFP_KERNEL); |
| 1646 | } |
| 1647 | EXPORT_SYMBOL(__nfc_alloc_vendor_cmd_reply_skb); |
| 1648 | |
| 1649 | int nfc_vendor_cmd_reply(struct sk_buff *skb) |
| 1650 | { |
| 1651 | struct nfc_dev *dev = ((void **)skb->cb)[0]; |
| 1652 | void *hdr = ((void **)skb->cb)[1]; |
| 1653 | |
| 1654 | /* clear CB data for netlink core to own from now on */ |
| 1655 | memset(skb->cb, 0, sizeof(skb->cb)); |
| 1656 | |
| 1657 | if (WARN_ON(!dev->cur_cmd_info)) { |
| 1658 | kfree_skb(skb); |
| 1659 | return -EINVAL; |
| 1660 | } |
| 1661 | |
| 1662 | genlmsg_end(skb, hdr); |
| 1663 | return genlmsg_reply(skb, dev->cur_cmd_info); |
| 1664 | } |
| 1665 | EXPORT_SYMBOL(nfc_vendor_cmd_reply); |
| 1666 | |
Johannes Berg | 4534de8 | 2013-11-14 17:14:46 +0100 | [diff] [blame] | 1667 | static const struct genl_ops nfc_genl_ops[] = { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1668 | { |
| 1669 | .cmd = NFC_CMD_GET_DEVICE, |
| 1670 | .doit = nfc_genl_get_device, |
| 1671 | .dumpit = nfc_genl_dump_devices, |
| 1672 | .done = nfc_genl_dump_devices_done, |
| 1673 | .policy = nfc_genl_policy, |
| 1674 | }, |
| 1675 | { |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 1676 | .cmd = NFC_CMD_DEV_UP, |
| 1677 | .doit = nfc_genl_dev_up, |
| 1678 | .policy = nfc_genl_policy, |
| 1679 | }, |
| 1680 | { |
| 1681 | .cmd = NFC_CMD_DEV_DOWN, |
| 1682 | .doit = nfc_genl_dev_down, |
| 1683 | .policy = nfc_genl_policy, |
| 1684 | }, |
| 1685 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1686 | .cmd = NFC_CMD_START_POLL, |
| 1687 | .doit = nfc_genl_start_poll, |
| 1688 | .policy = nfc_genl_policy, |
| 1689 | }, |
| 1690 | { |
| 1691 | .cmd = NFC_CMD_STOP_POLL, |
| 1692 | .doit = nfc_genl_stop_poll, |
| 1693 | .policy = nfc_genl_policy, |
| 1694 | }, |
| 1695 | { |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 1696 | .cmd = NFC_CMD_DEP_LINK_UP, |
| 1697 | .doit = nfc_genl_dep_link_up, |
| 1698 | .policy = nfc_genl_policy, |
| 1699 | }, |
| 1700 | { |
| 1701 | .cmd = NFC_CMD_DEP_LINK_DOWN, |
| 1702 | .doit = nfc_genl_dep_link_down, |
| 1703 | .policy = nfc_genl_policy, |
| 1704 | }, |
| 1705 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1706 | .cmd = NFC_CMD_GET_TARGET, |
| 1707 | .dumpit = nfc_genl_dump_targets, |
| 1708 | .done = nfc_genl_dump_targets_done, |
| 1709 | .policy = nfc_genl_policy, |
| 1710 | }, |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 1711 | { |
| 1712 | .cmd = NFC_CMD_LLC_GET_PARAMS, |
| 1713 | .doit = nfc_genl_llc_get_params, |
| 1714 | .policy = nfc_genl_policy, |
| 1715 | }, |
| 1716 | { |
| 1717 | .cmd = NFC_CMD_LLC_SET_PARAMS, |
| 1718 | .doit = nfc_genl_llc_set_params, |
| 1719 | .policy = nfc_genl_policy, |
| 1720 | }, |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 1721 | { |
| 1722 | .cmd = NFC_CMD_LLC_SDREQ, |
| 1723 | .doit = nfc_genl_llc_sdreq, |
| 1724 | .policy = nfc_genl_policy, |
| 1725 | }, |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1726 | { |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 1727 | .cmd = NFC_CMD_FW_DOWNLOAD, |
| 1728 | .doit = nfc_genl_fw_download, |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1729 | .policy = nfc_genl_policy, |
| 1730 | }, |
Samuel Ortiz | be08565 | 2013-05-10 17:07:32 +0200 | [diff] [blame] | 1731 | { |
| 1732 | .cmd = NFC_CMD_ENABLE_SE, |
| 1733 | .doit = nfc_genl_enable_se, |
| 1734 | .policy = nfc_genl_policy, |
| 1735 | }, |
| 1736 | { |
| 1737 | .cmd = NFC_CMD_DISABLE_SE, |
| 1738 | .doit = nfc_genl_disable_se, |
| 1739 | .policy = nfc_genl_policy, |
| 1740 | }, |
Samuel Ortiz | ac22ac4 | 2013-07-24 18:10:50 +0200 | [diff] [blame] | 1741 | { |
| 1742 | .cmd = NFC_CMD_GET_SE, |
| 1743 | .dumpit = nfc_genl_dump_ses, |
| 1744 | .done = nfc_genl_dump_ses_done, |
| 1745 | .policy = nfc_genl_policy, |
| 1746 | }, |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 1747 | { |
| 1748 | .cmd = NFC_CMD_SE_IO, |
| 1749 | .doit = nfc_genl_se_io, |
| 1750 | .policy = nfc_genl_policy, |
| 1751 | }, |
Christophe Ricard | 3682f49 | 2014-12-02 21:27:50 +0100 | [diff] [blame] | 1752 | { |
| 1753 | .cmd = NFC_CMD_ACTIVATE_TARGET, |
| 1754 | .doit = nfc_genl_activate_target, |
| 1755 | .policy = nfc_genl_policy, |
| 1756 | }, |
Samuel Ortiz | 9e58095 | 2014-10-14 02:19:46 +0200 | [diff] [blame] | 1757 | { |
| 1758 | .cmd = NFC_CMD_VENDOR, |
| 1759 | .doit = nfc_genl_vendor_cmd, |
| 1760 | .policy = nfc_genl_policy, |
| 1761 | }, |
Mark Greer | 4d63adf | 2017-06-15 20:34:22 -0700 | [diff] [blame] | 1762 | { |
| 1763 | .cmd = NFC_CMD_DEACTIVATE_TARGET, |
| 1764 | .doit = nfc_genl_deactivate_target, |
| 1765 | .policy = nfc_genl_policy, |
| 1766 | }, |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1767 | }; |
| 1768 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 1769 | static struct genl_family nfc_genl_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1770 | .hdrsize = 0, |
| 1771 | .name = NFC_GENL_NAME, |
| 1772 | .version = NFC_GENL_VERSION, |
| 1773 | .maxattr = NFC_ATTR_MAX, |
| 1774 | .module = THIS_MODULE, |
| 1775 | .ops = nfc_genl_ops, |
| 1776 | .n_ops = ARRAY_SIZE(nfc_genl_ops), |
| 1777 | .mcgrps = nfc_genl_mcgrps, |
| 1778 | .n_mcgrps = ARRAY_SIZE(nfc_genl_mcgrps), |
| 1779 | }; |
| 1780 | |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1781 | |
| 1782 | struct urelease_work { |
| 1783 | struct work_struct w; |
Richard Weinberger | 65bc4f9 | 2015-04-13 00:52:36 +0200 | [diff] [blame] | 1784 | u32 portid; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1785 | }; |
| 1786 | |
| 1787 | static void nfc_urelease_event_work(struct work_struct *work) |
| 1788 | { |
| 1789 | struct urelease_work *w = container_of(work, struct urelease_work, w); |
| 1790 | struct class_dev_iter iter; |
| 1791 | struct nfc_dev *dev; |
| 1792 | |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1793 | pr_debug("portid %d\n", w->portid); |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1794 | |
| 1795 | mutex_lock(&nfc_devlist_mutex); |
| 1796 | |
| 1797 | nfc_device_iter_init(&iter); |
| 1798 | dev = nfc_device_iter_next(&iter); |
| 1799 | |
| 1800 | while (dev) { |
| 1801 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 1802 | |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1803 | if (dev->genl_data.poll_req_portid == w->portid) { |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1804 | nfc_stop_poll(dev); |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1805 | dev->genl_data.poll_req_portid = 0; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 1809 | |
| 1810 | dev = nfc_device_iter_next(&iter); |
| 1811 | } |
| 1812 | |
| 1813 | nfc_device_iter_exit(&iter); |
| 1814 | |
| 1815 | mutex_unlock(&nfc_devlist_mutex); |
| 1816 | |
| 1817 | kfree(w); |
| 1818 | } |
| 1819 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1820 | static int nfc_genl_rcv_nl_event(struct notifier_block *this, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 1821 | unsigned long event, void *ptr) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1822 | { |
| 1823 | struct netlink_notify *n = ptr; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1824 | struct urelease_work *w; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1825 | |
| 1826 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC) |
| 1827 | goto out; |
| 1828 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1829 | pr_debug("NETLINK_URELEASE event from id %d\n", n->portid); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1830 | |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1831 | w = kmalloc(sizeof(*w), GFP_ATOMIC); |
| 1832 | if (w) { |
| 1833 | INIT_WORK((struct work_struct *) w, nfc_urelease_event_work); |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1834 | w->portid = n->portid; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1835 | schedule_work((struct work_struct *) w); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1836 | } |
| 1837 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1838 | out: |
| 1839 | return NOTIFY_DONE; |
| 1840 | } |
| 1841 | |
| 1842 | void nfc_genl_data_init(struct nfc_genl_data *genl_data) |
| 1843 | { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1844 | genl_data->poll_req_portid = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1845 | mutex_init(&genl_data->genl_data_mutex); |
| 1846 | } |
| 1847 | |
| 1848 | void nfc_genl_data_exit(struct nfc_genl_data *genl_data) |
| 1849 | { |
| 1850 | mutex_destroy(&genl_data->genl_data_mutex); |
| 1851 | } |
| 1852 | |
| 1853 | static struct notifier_block nl_notifier = { |
| 1854 | .notifier_call = nfc_genl_rcv_nl_event, |
| 1855 | }; |
| 1856 | |
| 1857 | /** |
| 1858 | * nfc_genl_init() - Initialize netlink interface |
| 1859 | * |
| 1860 | * This initialization function registers the nfc netlink family. |
| 1861 | */ |
| 1862 | int __init nfc_genl_init(void) |
| 1863 | { |
| 1864 | int rc; |
| 1865 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1866 | rc = genl_register_family(&nfc_genl_family); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1867 | if (rc) |
| 1868 | return rc; |
| 1869 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1870 | netlink_register_notifier(&nl_notifier); |
| 1871 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1872 | return 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | /** |
| 1876 | * nfc_genl_exit() - Deinitialize netlink interface |
| 1877 | * |
| 1878 | * This exit function unregisters the nfc netlink family. |
| 1879 | */ |
| 1880 | void nfc_genl_exit(void) |
| 1881 | { |
| 1882 | netlink_unregister_notifier(&nl_notifier); |
| 1883 | genl_unregister_family(&nfc_genl_family); |
| 1884 | } |