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