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