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