3 * Generic messaging interface between userspace and driver/device
6 * Copyright (C) 2007-2008 Intel Corporation <linux-wimax@intel.com>
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * This implements a direct communication channel between user space and
25 * the driver/device, by which free form messages can be sent back and
28 * This is intended for device-specific features, vendor quirks, etc.
30 * See include/net/wimax.h
32 * GENERIC NETLINK ENCODING AND CAPACITY
34 * A destination "pipe name" is added to each message; it is up to the
35 * drivers to assign or use those names (if using them at all).
37 * Messages are encoded as a binary netlink attribute using nla_put()
38 * using type NLA_UNSPEC (as some versions of libnl still in
39 * deployment don't yet understand NLA_BINARY).
41 * The maximum capacity of this transport is PAGESIZE per message (so
42 * the actual payload will be bit smaller depending on the
43 * netlink/generic netlink attributes and headers).
45 * RECEPTION OF MESSAGES
47 * When a message is received from user space, it is passed verbatim
48 * to the driver calling wimax_dev->op_msg_from_user(). The return
49 * value from this function is passed back to user space as an ack
50 * over the generic netlink protocol.
52 * The stack doesn't do any processing or interpretation of these
57 * Messages can be sent with wimax_msg().
59 * If the message delivery needs to happen on a different context to
60 * that of its creation, wimax_msg_alloc() can be used to get a
61 * pointer to the message that can be delivered later on with
66 * wimax_gnl_doit_msg_from_user() Process a message from user space
67 * wimax_dev_get_by_genl_info()
68 * wimax_dev->op_msg_from_user() Delivery of message to the driver
70 * wimax_msg() Send a message to user space
74 #include <linux/device.h>
75 #include <net/genetlink.h>
76 #include <linux/netdevice.h>
77 #include <linux/wimax.h>
78 #include <linux/security.h>
79 #include "wimax-internal.h"
82 #define D_SUBMODULE op_msg
83 #include "debug-levels.h"
87 * wimax_msg_alloc - Create a new skb for sending a message to userspace
89 * @wimax_dev: WiMAX device descriptor
90 * @pipe_name: "named pipe" the message will be sent to
91 * @msg: pointer to the message data to send
92 * @size: size of the message to send (in bytes), including the header.
93 * @gfp_flags: flags for memory allocation.
95 * Returns: %0 if ok, negative errno code on error
99 * Allocates an skb that will contain the message to send to user
100 * space over the messaging pipe and initializes it, copying the
103 * Once this call is done, you can deliver it with
108 * Don't use skb_push()/skb_pull()/skb_reserve() on the skb, as
109 * wimax_msg_send() depends on skb->data being placed at the
110 * beginning of the user message.
112 struct sk_buff
*wimax_msg_alloc(struct wimax_dev
*wimax_dev
,
113 const char *pipe_name
,
114 const void *msg
, size_t size
,
118 struct device
*dev
= wimax_dev
->net_dev
->dev
.parent
;
123 msg_size
= nla_total_size(size
)
124 + nla_total_size(sizeof(u32
))
125 + (pipe_name
? nla_total_size(strlen(pipe_name
)) : 0);
127 skb
= genlmsg_new(msg_size
, gfp_flags
);
130 genl_msg
= genlmsg_put(skb
, 0, 0, &wimax_gnl_family
,
131 0, WIMAX_GNL_OP_MSG_TO_USER
);
132 if (genl_msg
== NULL
) {
133 dev_err(dev
, "no memory to create generic netlink message\n");
134 goto error_genlmsg_put
;
136 result
= nla_put_u32(skb
, WIMAX_GNL_MSG_IFIDX
,
137 wimax_dev
->net_dev
->ifindex
);
139 dev_err(dev
, "no memory to add ifindex attribute\n");
143 result
= nla_put_string(skb
, WIMAX_GNL_MSG_PIPE_NAME
,
146 dev_err(dev
, "no memory to add pipe_name attribute\n");
150 result
= nla_put(skb
, WIMAX_GNL_MSG_DATA
, size
, msg
);
152 dev_err(dev
, "no memory to add payload in attribute\n");
155 genlmsg_end(skb
, genl_msg
);
162 return ERR_PTR(result
);
165 EXPORT_SYMBOL_GPL(wimax_msg_alloc
);
169 * wimax_msg_data_len - Return a pointer and size of a message's payload
171 * @msg: Pointer to a message created with wimax_msg_alloc()
172 * @size: Pointer to where to store the message's size
174 * Returns the pointer to the message data.
176 const void *wimax_msg_data_len(struct sk_buff
*msg
, size_t *size
)
178 struct nlmsghdr
*nlh
= (void *) msg
->head
;
181 nla
= nlmsg_find_attr(nlh
, sizeof(struct genlmsghdr
),
184 printk(KERN_ERR
"Cannot find attribute WIMAX_GNL_MSG_DATA\n");
187 *size
= nla_len(nla
);
188 return nla_data(nla
);
190 EXPORT_SYMBOL_GPL(wimax_msg_data_len
);
194 * wimax_msg_data - Return a pointer to a message's payload
196 * @msg: Pointer to a message created with wimax_msg_alloc()
198 const void *wimax_msg_data(struct sk_buff
*msg
)
200 struct nlmsghdr
*nlh
= (void *) msg
->head
;
203 nla
= nlmsg_find_attr(nlh
, sizeof(struct genlmsghdr
),
206 printk(KERN_ERR
"Cannot find attribute WIMAX_GNL_MSG_DATA\n");
209 return nla_data(nla
);
211 EXPORT_SYMBOL_GPL(wimax_msg_data
);
215 * wimax_msg_len - Return a message's payload length
217 * @msg: Pointer to a message created with wimax_msg_alloc()
219 ssize_t
wimax_msg_len(struct sk_buff
*msg
)
221 struct nlmsghdr
*nlh
= (void *) msg
->head
;
224 nla
= nlmsg_find_attr(nlh
, sizeof(struct genlmsghdr
),
227 printk(KERN_ERR
"Cannot find attribute WIMAX_GNL_MSG_DATA\n");
232 EXPORT_SYMBOL_GPL(wimax_msg_len
);
236 * wimax_msg_send - Send a pre-allocated message to user space
238 * @wimax_dev: WiMAX device descriptor
240 * @skb: &struct sk_buff returned by wimax_msg_alloc(). Note the
241 * ownership of @skb is transferred to this function.
243 * Returns: 0 if ok, < 0 errno code on error
247 * Sends a free-form message that was preallocated with
248 * wimax_msg_alloc() and filled up.
250 * Assumes that once you pass an skb to this function for sending, it
251 * owns it and will release it when done (on success).
255 * Don't use skb_push()/skb_pull()/skb_reserve() on the skb, as
256 * wimax_msg_send() depends on skb->data being placed at the
257 * beginning of the user message.
259 int wimax_msg_send(struct wimax_dev
*wimax_dev
, struct sk_buff
*skb
)
262 struct device
*dev
= wimax_dev
->net_dev
->dev
.parent
;
263 void *msg
= skb
->data
;
264 size_t size
= skb
->len
;
267 d_printf(1, dev
, "CTX: wimax msg, %zu bytes\n", size
);
268 d_dump(2, dev
, msg
, size
);
269 result
= genlmsg_multicast(skb
, 0, wimax_gnl_mcg
.id
, GFP_KERNEL
);
270 d_printf(1, dev
, "CTX: genl multicast result %d\n", result
);
271 if (result
== -ESRCH
) /* Nobody connected, ignore it */
272 result
= 0; /* btw, the skb is freed already */
275 EXPORT_SYMBOL_GPL(wimax_msg_send
);
279 * wimax_msg - Send a message to user space
281 * @wimax_dev: WiMAX device descriptor (properly referenced)
282 * @pipe_name: "named pipe" the message will be sent to
283 * @buf: pointer to the message to send.
284 * @size: size of the buffer pointed to by @buf (in bytes).
285 * @gfp_flags: flags for memory allocation.
287 * Returns: %0 if ok, negative errno code on error.
291 * Sends a free-form message to user space on the device @wimax_dev.
295 * Once the @skb is given to this function, who will own it and will
296 * release it when done (unless it returns error).
298 int wimax_msg(struct wimax_dev
*wimax_dev
, const char *pipe_name
,
299 const void *buf
, size_t size
, gfp_t gfp_flags
)
301 int result
= -ENOMEM
;
304 skb
= wimax_msg_alloc(wimax_dev
, pipe_name
, buf
, size
, gfp_flags
);
307 result
= wimax_msg_send(wimax_dev
, skb
);
311 EXPORT_SYMBOL_GPL(wimax_msg
);
315 struct nla_policy wimax_gnl_msg_policy
[WIMAX_GNL_ATTR_MAX
+ 1] = {
316 [WIMAX_GNL_MSG_IFIDX
] = {
319 [WIMAX_GNL_MSG_DATA
] = {
320 .type
= NLA_UNSPEC
, /* libnl doesn't grok BINARY yet */
326 * Relays a message from user space to the driver
328 * The skb is passed to the driver-specific function with the netlink
329 * and generic netlink headers already stripped.
331 * This call will block while handling/relaying the message.
334 int wimax_gnl_doit_msg_from_user(struct sk_buff
*skb
, struct genl_info
*info
)
337 struct wimax_dev
*wimax_dev
;
339 struct nlmsghdr
*nlh
= info
->nlhdr
;
345 d_fnstart(3, NULL
, "(skb %p info %p)\n", skb
, info
);
347 if (info
->attrs
[WIMAX_GNL_MSG_IFIDX
] == NULL
) {
348 printk(KERN_ERR
"WIMAX_GNL_MSG_FROM_USER: can't find IFIDX "
350 goto error_no_wimax_dev
;
352 ifindex
= nla_get_u32(info
->attrs
[WIMAX_GNL_MSG_IFIDX
]);
353 wimax_dev
= wimax_dev_get_by_genl_info(info
, ifindex
);
354 if (wimax_dev
== NULL
)
355 goto error_no_wimax_dev
;
356 dev
= wimax_dev_to_dev(wimax_dev
);
358 /* Unpack arguments */
360 if (info
->attrs
[WIMAX_GNL_MSG_DATA
] == NULL
) {
361 dev_err(dev
, "WIMAX_GNL_MSG_FROM_USER: can't find MSG_DATA "
365 msg_buf
= nla_data(info
->attrs
[WIMAX_GNL_MSG_DATA
]);
366 msg_len
= nla_len(info
->attrs
[WIMAX_GNL_MSG_DATA
]);
368 if (info
->attrs
[WIMAX_GNL_MSG_PIPE_NAME
] == NULL
)
371 struct nlattr
*attr
= info
->attrs
[WIMAX_GNL_MSG_PIPE_NAME
];
372 size_t attr_len
= nla_len(attr
);
373 /* libnl-1.1 does not yet support NLA_NUL_STRING */
375 pipe_name
= kstrndup(nla_data(attr
), attr_len
+ 1, GFP_KERNEL
);
376 if (pipe_name
== NULL
)
378 pipe_name
[attr_len
] = 0;
380 mutex_lock(&wimax_dev
->mutex
);
381 result
= wimax_dev_is_ready(wimax_dev
);
383 goto error_not_ready
;
385 if (wimax_dev
->op_msg_from_user
== NULL
)
389 "CRX: nlmsghdr len %u type %u flags 0x%04x seq 0x%x pid %u\n",
390 nlh
->nlmsg_len
, nlh
->nlmsg_type
, nlh
->nlmsg_flags
,
391 nlh
->nlmsg_seq
, nlh
->nlmsg_pid
);
392 d_printf(1, dev
, "CRX: wimax message %zu bytes\n", msg_len
);
393 d_dump(2, dev
, msg_buf
, msg_len
);
395 result
= wimax_dev
->op_msg_from_user(wimax_dev
, pipe_name
,
396 msg_buf
, msg_len
, info
);
399 mutex_unlock(&wimax_dev
->mutex
);
403 dev_put(wimax_dev
->net_dev
);
405 d_fnend(3, NULL
, "(skb %p info %p) = %d\n", skb
, info
, result
);
411 * Generic Netlink glue
414 struct genl_ops wimax_gnl_msg_from_user
= {
415 .cmd
= WIMAX_GNL_OP_MSG_FROM_USER
,
416 .flags
= GENL_ADMIN_PERM
,
417 .policy
= wimax_gnl_msg_policy
,
418 .doit
= wimax_gnl_doit_msg_from_user
,