^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Linux WiMAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Generic messaging interface between userspace and driver/device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2007-2008 Intel Corporation <linux-wimax@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * This implements a direct communication channel between user space and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * the driver/device, by which free form messages can be sent back and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * forth.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * This is intended for device-specific features, vendor quirks, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * See include/net/wimax.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * GENERIC NETLINK ENCODING AND CAPACITY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * A destination "pipe name" is added to each message; it is up to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * drivers to assign or use those names (if using them at all).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Messages are encoded as a binary netlink attribute using nla_put()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * using type NLA_UNSPEC (as some versions of libnl still in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * deployment don't yet understand NLA_BINARY).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * The maximum capacity of this transport is PAGESIZE per message (so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * the actual payload will be bit smaller depending on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * netlink/generic netlink attributes and headers).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * RECEPTION OF MESSAGES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * When a message is received from user space, it is passed verbatim
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * to the driver calling wimax_dev->op_msg_from_user(). The return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * value from this function is passed back to user space as an ack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * over the generic netlink protocol.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * The stack doesn't do any processing or interpretation of these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * messages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * SENDING MESSAGES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * Messages can be sent with wimax_msg().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * If the message delivery needs to happen on a different context to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * that of its creation, wimax_msg_alloc() can be used to get a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * pointer to the message that can be delivered later on with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * wimax_msg_send().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * ROADMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * wimax_gnl_doit_msg_from_user() Process a message from user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * wimax_dev_get_by_genl_info()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * wimax_dev->op_msg_from_user() Delivery of message to the driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * wimax_msg() Send a message to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * wimax_msg_alloc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * wimax_msg_send()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #include <net/genetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #include <linux/wimax.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #include "wimax-internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #define D_SUBMODULE op_msg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #include "debug-levels.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * wimax_msg_alloc - Create a new skb for sending a message to userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * @wimax_dev: WiMAX device descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * @pipe_name: "named pipe" the message will be sent to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * @msg: pointer to the message data to send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * @size: size of the message to send (in bytes), including the header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * @gfp_flags: flags for memory allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * Returns: %0 if ok, negative errno code on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * Allocates an skb that will contain the message to send to user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * space over the messaging pipe and initializes it, copying the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * Once this call is done, you can deliver it with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * wimax_msg_send().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * IMPORTANT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * Don't use skb_push()/skb_pull()/skb_reserve() on the skb, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * wimax_msg_send() depends on skb->data being placed at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * beginning of the user message.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * Unlike other WiMAX stack calls, this call can be used way early,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * even before wimax_dev_add() is called, as long as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * wimax_dev->net_dev pointer is set to point to a proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * net_dev. This is so that drivers can use it early in case they need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * to send stuff around or communicate with user space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct sk_buff *wimax_msg_alloc(struct wimax_dev *wimax_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) const char *pipe_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) const void *msg, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) gfp_t gfp_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct device *dev = wimax_dev_to_dev(wimax_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) size_t msg_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) void *genl_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) msg_size = nla_total_size(size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) + nla_total_size(sizeof(u32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) + (pipe_name ? nla_total_size(strlen(pipe_name)) : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) result = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) skb = genlmsg_new(msg_size, gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) goto error_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) genl_msg = genlmsg_put(skb, 0, 0, &wimax_gnl_family,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 0, WIMAX_GNL_OP_MSG_TO_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (genl_msg == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) dev_err(dev, "no memory to create generic netlink message\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) goto error_genlmsg_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) result = nla_put_u32(skb, WIMAX_GNL_MSG_IFIDX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) wimax_dev->net_dev->ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (result < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) dev_err(dev, "no memory to add ifindex attribute\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) goto error_nla_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (pipe_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) result = nla_put_string(skb, WIMAX_GNL_MSG_PIPE_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) pipe_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (result < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) dev_err(dev, "no memory to add pipe_name attribute\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) goto error_nla_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) result = nla_put(skb, WIMAX_GNL_MSG_DATA, size, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (result < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) dev_err(dev, "no memory to add payload (msg %p size %zu) in "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) "attribute: %d\n", msg, size, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) goto error_nla_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) genlmsg_end(skb, genl_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) error_nla_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) error_genlmsg_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) error_new:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) nlmsg_free(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return ERR_PTR(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) EXPORT_SYMBOL_GPL(wimax_msg_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * wimax_msg_data_len - Return a pointer and size of a message's payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * @msg: Pointer to a message created with wimax_msg_alloc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * @size: Pointer to where to store the message's size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * Returns the pointer to the message data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) const void *wimax_msg_data_len(struct sk_buff *msg, size_t *size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct nlmsghdr *nlh = (void *) msg->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) nla = nlmsg_find_attr(nlh, sizeof(struct genlmsghdr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) WIMAX_GNL_MSG_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (nla == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) pr_err("Cannot find attribute WIMAX_GNL_MSG_DATA\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) *size = nla_len(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return nla_data(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) EXPORT_SYMBOL_GPL(wimax_msg_data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * wimax_msg_data - Return a pointer to a message's payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * @msg: Pointer to a message created with wimax_msg_alloc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) const void *wimax_msg_data(struct sk_buff *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct nlmsghdr *nlh = (void *) msg->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) nla = nlmsg_find_attr(nlh, sizeof(struct genlmsghdr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) WIMAX_GNL_MSG_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (nla == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) pr_err("Cannot find attribute WIMAX_GNL_MSG_DATA\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return nla_data(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) EXPORT_SYMBOL_GPL(wimax_msg_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * wimax_msg_len - Return a message's payload length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * @msg: Pointer to a message created with wimax_msg_alloc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) ssize_t wimax_msg_len(struct sk_buff *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct nlmsghdr *nlh = (void *) msg->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) nla = nlmsg_find_attr(nlh, sizeof(struct genlmsghdr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) WIMAX_GNL_MSG_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (nla == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) pr_err("Cannot find attribute WIMAX_GNL_MSG_DATA\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return nla_len(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) EXPORT_SYMBOL_GPL(wimax_msg_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * wimax_msg_send - Send a pre-allocated message to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * @wimax_dev: WiMAX device descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * @skb: &struct sk_buff returned by wimax_msg_alloc(). Note the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * ownership of @skb is transferred to this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * Returns: 0 if ok, < 0 errno code on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * Sends a free-form message that was preallocated with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * wimax_msg_alloc() and filled up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * Assumes that once you pass an skb to this function for sending, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * owns it and will release it when done (on success).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * IMPORTANT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * Don't use skb_push()/skb_pull()/skb_reserve() on the skb, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * wimax_msg_send() depends on skb->data being placed at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * beginning of the user message.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * Unlike other WiMAX stack calls, this call can be used way early,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * even before wimax_dev_add() is called, as long as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * wimax_dev->net_dev pointer is set to point to a proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * net_dev. This is so that drivers can use it early in case they need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * to send stuff around or communicate with user space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int wimax_msg_send(struct wimax_dev *wimax_dev, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) struct device *dev = wimax_dev_to_dev(wimax_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) void *msg = skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) size_t size = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) might_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) d_printf(1, dev, "CTX: wimax msg, %zu bytes\n", size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) d_dump(2, dev, msg, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) genlmsg_multicast(&wimax_gnl_family, skb, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) d_printf(1, dev, "CTX: genl multicast done\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) EXPORT_SYMBOL_GPL(wimax_msg_send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * wimax_msg - Send a message to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * @wimax_dev: WiMAX device descriptor (properly referenced)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * @pipe_name: "named pipe" the message will be sent to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * @buf: pointer to the message to send.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * @size: size of the buffer pointed to by @buf (in bytes).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * @gfp_flags: flags for memory allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * Returns: %0 if ok, negative errno code on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * Sends a free-form message to user space on the device @wimax_dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * NOTES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * Once the @skb is given to this function, who will own it and will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * release it when done (unless it returns error).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) int wimax_msg(struct wimax_dev *wimax_dev, const char *pipe_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) const void *buf, size_t size, gfp_t gfp_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) int result = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) skb = wimax_msg_alloc(wimax_dev, pipe_name, buf, size, gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (IS_ERR(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) result = PTR_ERR(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) result = wimax_msg_send(wimax_dev, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) EXPORT_SYMBOL_GPL(wimax_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * Relays a message from user space to the driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * The skb is passed to the driver-specific function with the netlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * and generic netlink headers already stripped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * This call will block while handling/relaying the message.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) int wimax_gnl_doit_msg_from_user(struct sk_buff *skb, struct genl_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) int result, ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct wimax_dev *wimax_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) struct nlmsghdr *nlh = info->nlhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) char *pipe_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) void *msg_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) size_t msg_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) might_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) d_fnstart(3, NULL, "(skb %p info %p)\n", skb, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) result = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (info->attrs[WIMAX_GNL_MSG_IFIDX] == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) pr_err("WIMAX_GNL_MSG_FROM_USER: can't find IFIDX attribute\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) goto error_no_wimax_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) ifindex = nla_get_u32(info->attrs[WIMAX_GNL_MSG_IFIDX]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) wimax_dev = wimax_dev_get_by_genl_info(info, ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (wimax_dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) goto error_no_wimax_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) dev = wimax_dev_to_dev(wimax_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) /* Unpack arguments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) result = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (info->attrs[WIMAX_GNL_MSG_DATA] == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) dev_err(dev, "WIMAX_GNL_MSG_FROM_USER: can't find MSG_DATA "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) "attribute\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) goto error_no_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) msg_buf = nla_data(info->attrs[WIMAX_GNL_MSG_DATA]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) msg_len = nla_len(info->attrs[WIMAX_GNL_MSG_DATA]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (info->attrs[WIMAX_GNL_MSG_PIPE_NAME] == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) pipe_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) struct nlattr *attr = info->attrs[WIMAX_GNL_MSG_PIPE_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) size_t attr_len = nla_len(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) /* libnl-1.1 does not yet support NLA_NUL_STRING */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) result = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) pipe_name = kstrndup(nla_data(attr), attr_len + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (pipe_name == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) goto error_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) pipe_name[attr_len] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) mutex_lock(&wimax_dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) result = wimax_dev_is_ready(wimax_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (result == -ENOMEDIUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (result < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) goto error_not_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) result = -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (wimax_dev->op_msg_from_user == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) goto error_noop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) d_printf(1, dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) "CRX: nlmsghdr len %u type %u flags 0x%04x seq 0x%x pid %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) nlh->nlmsg_len, nlh->nlmsg_type, nlh->nlmsg_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) nlh->nlmsg_seq, nlh->nlmsg_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) d_printf(1, dev, "CRX: wimax message %zu bytes\n", msg_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) d_dump(2, dev, msg_buf, msg_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) result = wimax_dev->op_msg_from_user(wimax_dev, pipe_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) msg_buf, msg_len, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) error_noop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) error_not_ready:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) mutex_unlock(&wimax_dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) error_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) kfree(pipe_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) error_no_data:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) dev_put(wimax_dev->net_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) error_no_wimax_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }