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