]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/connector/connector.c
memblock: replace free_bootmem_late with memblock_free_late
[mirror_ubuntu-jammy-kernel.git] / drivers / connector / connector.c
CommitLineData
7672d0b5 1/*
f3c48ecc 2 * connector.c
1a5645bc 3 *
acb9c1b2 4 * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
7672d0b5 5 * All rights reserved.
1a5645bc 6 *
7672d0b5
EP
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
d2af686c 22#include <linux/compiler.h>
7672d0b5
EP
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/list.h>
26#include <linux/skbuff.h>
9631d79e 27#include <net/netlink.h>
7672d0b5
EP
28#include <linux/moduleparam.h>
29#include <linux/connector.h>
5a0e3ad6 30#include <linux/slab.h>
8ed965d6 31#include <linux/mutex.h>
a0a61a60
LZ
32#include <linux/proc_fs.h>
33#include <linux/spinlock.h>
7672d0b5
EP
34
35#include <net/sock.h>
36
37MODULE_LICENSE("GPL");
acb9c1b2 38MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
7672d0b5 39MODULE_DESCRIPTION("Generic userspace <-> kernelspace connector.");
3700c3c2 40MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_CONNECTOR);
7672d0b5 41
7672d0b5
EP
42static struct cn_dev cdev;
43
78374676 44static int cn_already_initialized;
7672d0b5
EP
45
46/*
34470e0b
DF
47 * Sends mult (multiple) cn_msg at a time.
48 *
7672d0b5
EP
49 * msg->seq and msg->ack are used to determine message genealogy.
50 * When someone sends message it puts there locally unique sequence
51 * and random acknowledge numbers. Sequence number may be copied into
52 * nlmsghdr->nlmsg_seq too.
53 *
54 * Sequence number is incremented with each message to be sent.
55 *
ac8f7330 56 * If we expect a reply to our message then the sequence number in
7672d0b5
EP
57 * received message MUST be the same as in original message, and
58 * acknowledge number MUST be the same + 1.
59 *
60 * If we receive a message and its sequence number is not equal to the
61 * one we are expecting then it is a new message.
62 *
63 * If we receive a message and its sequence number is the same as one
64 * we are expecting but it's acknowledgement number is not equal to
65 * the acknowledgement number in the original message + 1, then it is
66 * a new message.
67 *
34470e0b
DF
68 * If msg->len != len, then additional cn_msg messages are expected following
69 * the first msg.
70 *
ac8f7330
DF
71 * The message is sent to, the portid if given, the group if given, both if
72 * both, or if both are zero then the group is looked up and sent there.
7672d0b5 73 */
34470e0b 74int cn_netlink_send_mult(struct cn_msg *msg, u16 len, u32 portid, u32 __group,
ac8f7330 75 gfp_t gfp_mask)
7672d0b5
EP
76{
77 struct cn_callback_entry *__cbq;
78 unsigned int size;
79 struct sk_buff *skb;
80 struct nlmsghdr *nlh;
81 struct cn_msg *data;
82 struct cn_dev *dev = &cdev;
83 u32 group = 0;
84 int found = 0;
85
ac8f7330
DF
86 if (portid || __group) {
87 group = __group;
88 } else {
7672d0b5
EP
89 spin_lock_bh(&dev->cbdev->queue_lock);
90 list_for_each_entry(__cbq, &dev->cbdev->queue_list,
91 callback_entry) {
acd042bb 92 if (cn_cb_equal(&__cbq->id.id, &msg->id)) {
7672d0b5
EP
93 found = 1;
94 group = __cbq->group;
fd00eecc 95 break;
7672d0b5
EP
96 }
97 }
98 spin_unlock_bh(&dev->cbdev->queue_lock);
99
100 if (!found)
101 return -ENODEV;
7672d0b5
EP
102 }
103
ac8f7330 104 if (!portid && !netlink_has_listeners(dev->nls, group))
b191ba0d
EP
105 return -ESRCH;
106
34470e0b 107 size = sizeof(*msg) + len;
7672d0b5 108
9631d79e 109 skb = nlmsg_new(size, gfp_mask);
7672d0b5
EP
110 if (!skb)
111 return -ENOMEM;
112
9631d79e 113 nlh = nlmsg_put(skb, 0, msg->seq, NLMSG_DONE, size, 0);
85c93166
JMC
114 if (!nlh) {
115 kfree_skb(skb);
116 return -EMSGSIZE;
117 }
7672d0b5 118
85c93166 119 data = nlmsg_data(nlh);
7672d0b5 120
ac73bf50 121 memcpy(data, msg, size);
7672d0b5
EP
122
123 NETLINK_CB(skb).dst_group = group;
124
ac8f7330
DF
125 if (group)
126 return netlink_broadcast(dev->nls, skb, portid, group,
127 gfp_mask);
d0164adc
MG
128 return netlink_unicast(dev->nls, skb, portid,
129 !gfpflags_allow_blocking(gfp_mask));
7672d0b5 130}
34470e0b
DF
131EXPORT_SYMBOL_GPL(cn_netlink_send_mult);
132
133/* same as cn_netlink_send_mult except msg->len is used for len */
134int cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __group,
135 gfp_t gfp_mask)
136{
137 return cn_netlink_send_mult(msg, msg->len, portid, __group, gfp_mask);
138}
deb0e9b2 139EXPORT_SYMBOL_GPL(cn_netlink_send);
7672d0b5
EP
140
141/*
142 * Callback helper - queues work and setup destructor for given data.
143 */
f1489cfb 144static int cn_call_callback(struct sk_buff *skb)
7672d0b5 145{
a30cfa47 146 struct nlmsghdr *nlh;
04f482fa 147 struct cn_callback_entry *i, *cbq = NULL;
7672d0b5 148 struct cn_dev *dev = &cdev;
9631d79e 149 struct cn_msg *msg = nlmsg_data(nlmsg_hdr(skb));
04f482fa 150 struct netlink_skb_parms *nsp = &NETLINK_CB(skb);
acd042bb 151 int err = -ENODEV;
7672d0b5 152
a30cfa47
DF
153 /* verify msg->len is within skb */
154 nlh = nlmsg_hdr(skb);
155 if (nlh->nlmsg_len < NLMSG_HDRLEN + sizeof(struct cn_msg) + msg->len)
156 return -EINVAL;
157
7672d0b5 158 spin_lock_bh(&dev->cbdev->queue_lock);
04f482fa
PM
159 list_for_each_entry(i, &dev->cbdev->queue_list, callback_entry) {
160 if (cn_cb_equal(&i->id.id, &msg->id)) {
e65f7ee3 161 refcount_inc(&i->refcnt);
04f482fa 162 cbq = i;
7672d0b5
EP
163 break;
164 }
165 }
166 spin_unlock_bh(&dev->cbdev->queue_lock);
167
04f482fa
PM
168 if (cbq != NULL) {
169 cbq->callback(msg, nsp);
170 kfree_skb(skb);
171 cn_queue_release_callback(cbq);
0e087858 172 err = 0;
04f482fa
PM
173 }
174
acd042bb 175 return err;
7672d0b5
EP
176}
177
7672d0b5
EP
178/*
179 * Main netlink receiving function.
180 *
00f5e06c 181 * It checks skb, netlink header and msg sizes, and calls callback helper.
7672d0b5 182 */
55285bf0 183static void cn_rx_skb(struct sk_buff *skb)
7672d0b5
EP
184{
185 struct nlmsghdr *nlh;
162b2bed 186 int len, err;
7672d0b5 187
9631d79e 188 if (skb->len >= NLMSG_HDRLEN) {
b529ccf2 189 nlh = nlmsg_hdr(skb);
162b2bed 190 len = nlmsg_len(nlh);
7672d0b5 191
162b2bed 192 if (len < (int)sizeof(struct cn_msg) ||
7672d0b5 193 skb->len < nlh->nlmsg_len ||
55285bf0 194 len > CONNECTOR_MAX_MSG_SIZE)
6cf92e98 195 return;
7672d0b5 196
55285bf0 197 err = cn_call_callback(skb_get(skb));
7672d0b5
EP
198 if (err < 0)
199 kfree_skb(skb);
200 }
7672d0b5
EP
201}
202
7672d0b5
EP
203/*
204 * Callback add routing - adds callback with given ID and name.
205 * If there is registered callback with the same ID it will not be added.
206 *
207 * May sleep.
208 */
008536e8 209int cn_add_callback(struct cb_id *id, const char *name,
f3c48ecc
VI
210 void (*callback)(struct cn_msg *,
211 struct netlink_skb_parms *))
7672d0b5
EP
212{
213 int err;
214 struct cn_dev *dev = &cdev;
7672d0b5 215
d6cc7f1a
EP
216 if (!cn_already_initialized)
217 return -EAGAIN;
218
acd042bb
EP
219 err = cn_queue_add_callback(dev->cbdev, name, id, callback);
220 if (err)
7672d0b5 221 return err;
7672d0b5 222
7672d0b5
EP
223 return 0;
224}
deb0e9b2 225EXPORT_SYMBOL_GPL(cn_add_callback);
7672d0b5
EP
226
227/*
228 * Callback remove routing - removes callback
229 * with given ID.
230 * If there is no registered callback with given
231 * ID nothing happens.
232 *
233 * May sleep while waiting for reference counter to become zero.
234 */
235void cn_del_callback(struct cb_id *id)
236{
237 struct cn_dev *dev = &cdev;
238
239 cn_queue_del_callback(dev->cbdev, id);
7672d0b5 240}
deb0e9b2 241EXPORT_SYMBOL_GPL(cn_del_callback);
7672d0b5 242
d2af686c 243static int __maybe_unused cn_proc_show(struct seq_file *m, void *v)
a0a61a60
LZ
244{
245 struct cn_queue_dev *dev = cdev.cbdev;
246 struct cn_callback_entry *cbq;
247
248 seq_printf(m, "Name ID\n");
249
250 spin_lock_bh(&dev->queue_lock);
251
252 list_for_each_entry(cbq, &dev->queue_list, callback_entry) {
253 seq_printf(m, "%-15s %u:%u\n",
254 cbq->id.name,
255 cbq->id.id.idx,
256 cbq->id.id.val);
257 }
258
259 spin_unlock_bh(&dev->queue_lock);
260
261 return 0;
262}
263
a31f2d17
PNA
264static struct cn_dev cdev = {
265 .input = cn_rx_skb,
266};
267
0fe763c5 268static int cn_init(void)
7672d0b5
EP
269{
270 struct cn_dev *dev = &cdev;
a31f2d17
PNA
271 struct netlink_kernel_cfg cfg = {
272 .groups = CN_NETLINK_USERS + 0xf,
273 .input = dev->input,
274 };
7672d0b5 275
9f00d977 276 dev->nls = netlink_kernel_create(&init_net, NETLINK_CONNECTOR, &cfg);
7672d0b5
EP
277 if (!dev->nls)
278 return -EIO;
279
280 dev->cbdev = cn_queue_alloc_dev("cqueue", dev->nls);
281 if (!dev->cbdev) {
b7c6ba6e 282 netlink_kernel_release(dev->nls);
7672d0b5
EP
283 return -EINVAL;
284 }
1a5645bc 285
d6cc7f1a 286 cn_already_initialized = 1;
7672d0b5 287
3f3942ac 288 proc_create_single("connector", S_IRUGO, init_net.proc_net, cn_proc_show);
a0a61a60 289
7672d0b5
EP
290 return 0;
291}
292
0fe763c5 293static void cn_fini(void)
7672d0b5
EP
294{
295 struct cn_dev *dev = &cdev;
296
297 cn_already_initialized = 0;
298
ece31ffd 299 remove_proc_entry("connector", init_net.proc_net);
a0a61a60 300
7672d0b5 301 cn_queue_free_dev(dev->cbdev);
b7c6ba6e 302 netlink_kernel_release(dev->nls);
7672d0b5
EP
303}
304
d6cc7f1a 305subsys_initcall(cn_init);
7672d0b5 306module_exit(cn_fini);