1 // SPDX-License-Identifier: GPL-2.0-or-later
9 #include <linux/compiler.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/list.h>
13 #include <linux/skbuff.h>
14 #include <net/netlink.h>
15 #include <linux/moduleparam.h>
16 #include <linux/connector.h>
17 #include <linux/slab.h>
18 #include <linux/mutex.h>
19 #include <linux/proc_fs.h>
20 #include <linux/spinlock.h>
24 MODULE_LICENSE("GPL");
26 MODULE_DESCRIPTION("Generic userspace <-> kernelspace connector.");
27 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_CONNECTOR);
29 static struct cn_dev cdev;
31 static int cn_already_initialized;
34 * Sends mult (multiple) cn_msg at a time.
36 * msg->seq and msg->ack are used to determine message genealogy.
37 * When someone sends message it puts there locally unique sequence
38 * and random acknowledge numbers. Sequence number may be copied into
39 * nlmsghdr->nlmsg_seq too.
41 * Sequence number is incremented with each message to be sent.
43 * If we expect a reply to our message then the sequence number in
44 * received message MUST be the same as in original message, and
45 * acknowledge number MUST be the same + 1.
47 * If we receive a message and its sequence number is not equal to the
48 * one we are expecting then it is a new message.
50 * If we receive a message and its sequence number is the same as one
51 * we are expecting but it's acknowledgement number is not equal to
52 * the acknowledgement number in the original message + 1, then it is
55 * If msg->len != len, then additional cn_msg messages are expected following
58 * The message is sent to, the portid if given, the group if given, both if
59 * both, or if both are zero then the group is looked up and sent there.
61 int cn_netlink_send_mult(struct cn_msg *msg, u16 len, u32 portid, u32 __group,
62 gfp_t gfp_mask, netlink_filter_fn filter,
65 struct cn_callback_entry *__cbq;
70 struct cn_dev *dev = &cdev;
74 if (portid || __group) {
77 spin_lock_bh(&dev->cbdev->queue_lock);
78 list_for_each_entry(__cbq, &dev->cbdev->queue_list,
80 if (cn_cb_equal(&__cbq->id.id, &msg->id)) {
86 spin_unlock_bh(&dev->cbdev->queue_lock);
92 if (!portid && !netlink_has_listeners(dev->nls, group))
95 size = sizeof(*msg) + len;
97 skb = nlmsg_new(size, gfp_mask);
101 nlh = nlmsg_put(skb, 0, msg->seq, NLMSG_DONE, size, 0);
107 data = nlmsg_data(nlh);
109 memcpy(data, msg, size);
111 NETLINK_CB(skb).dst_group = group;
114 return netlink_broadcast_filtered(dev->nls, skb, portid, group,
116 (void *)filter_data);
117 return netlink_unicast(dev->nls, skb, portid,
118 !gfpflags_allow_blocking(gfp_mask));
120 EXPORT_SYMBOL_GPL(cn_netlink_send_mult);
122 /* same as cn_netlink_send_mult except msg->len is used for len */
123 int cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __group,
126 return cn_netlink_send_mult(msg, msg->len, portid, __group, gfp_mask,
129 EXPORT_SYMBOL_GPL(cn_netlink_send);
132 * Callback helper - queues work and setup destructor for given data.
134 static int cn_call_callback(struct sk_buff *skb)
136 struct nlmsghdr *nlh;
137 struct cn_callback_entry *i, *cbq = NULL;
138 struct cn_dev *dev = &cdev;
139 struct cn_msg *msg = nlmsg_data(nlmsg_hdr(skb));
140 struct netlink_skb_parms *nsp = &NETLINK_CB(skb);
143 /* verify msg->len is within skb */
144 nlh = nlmsg_hdr(skb);
145 if (nlh->nlmsg_len < NLMSG_HDRLEN + sizeof(struct cn_msg) + msg->len)
148 spin_lock_bh(&dev->cbdev->queue_lock);
149 list_for_each_entry(i, &dev->cbdev->queue_list, callback_entry) {
150 if (cn_cb_equal(&i->id.id, &msg->id)) {
151 refcount_inc(&i->refcnt);
156 spin_unlock_bh(&dev->cbdev->queue_lock);
159 cbq->callback(msg, nsp);
161 cn_queue_release_callback(cbq);
169 * Allow non-root access for NETLINK_CONNECTOR family having CN_IDX_PROC
172 static int cn_bind(struct net *net, int group)
174 unsigned long groups = (unsigned long) group;
176 if (ns_capable(net->user_ns, CAP_NET_ADMIN))
179 if (test_bit(CN_IDX_PROC - 1, &groups))
185 static void cn_release(struct sock *sk, unsigned long *groups)
187 if (groups && test_bit(CN_IDX_PROC - 1, groups)) {
188 kfree(sk->sk_user_data);
189 sk->sk_user_data = NULL;
194 * Main netlink receiving function.
196 * It checks skb, netlink header and msg sizes, and calls callback helper.
198 static void cn_rx_skb(struct sk_buff *skb)
200 struct nlmsghdr *nlh;
203 if (skb->len >= NLMSG_HDRLEN) {
204 nlh = nlmsg_hdr(skb);
205 len = nlmsg_len(nlh);
207 if (len < (int)sizeof(struct cn_msg) ||
208 skb->len < nlh->nlmsg_len ||
209 len > CONNECTOR_MAX_MSG_SIZE)
212 err = cn_call_callback(skb_get(skb));
219 * Callback add routing - adds callback with given ID and name.
220 * If there is registered callback with the same ID it will not be added.
224 int cn_add_callback(const struct cb_id *id, const char *name,
225 void (*callback)(struct cn_msg *,
226 struct netlink_skb_parms *))
228 struct cn_dev *dev = &cdev;
230 if (!cn_already_initialized)
233 return cn_queue_add_callback(dev->cbdev, name, id, callback);
235 EXPORT_SYMBOL_GPL(cn_add_callback);
238 * Callback remove routing - removes callback
240 * If there is no registered callback with given
241 * ID nothing happens.
243 * May sleep while waiting for reference counter to become zero.
245 void cn_del_callback(const struct cb_id *id)
247 struct cn_dev *dev = &cdev;
249 cn_queue_del_callback(dev->cbdev, id);
251 EXPORT_SYMBOL_GPL(cn_del_callback);
253 static int __maybe_unused cn_proc_show(struct seq_file *m, void *v)
255 struct cn_queue_dev *dev = cdev.cbdev;
256 struct cn_callback_entry *cbq;
258 seq_printf(m, "Name ID\n");
260 spin_lock_bh(&dev->queue_lock);
262 list_for_each_entry(cbq, &dev->queue_list, callback_entry) {
263 seq_printf(m, "%-15s %u:%u\n",
269 spin_unlock_bh(&dev->queue_lock);
274 static int cn_init(void)
276 struct cn_dev *dev = &cdev;
277 struct netlink_kernel_cfg cfg = {
278 .groups = CN_NETLINK_USERS + 0xf,
280 .flags = NL_CFG_F_NONROOT_RECV,
282 .release = cn_release,
285 dev->nls = netlink_kernel_create(&init_net, NETLINK_CONNECTOR, &cfg);
289 dev->cbdev = cn_queue_alloc_dev("cqueue", dev->nls);
291 netlink_kernel_release(dev->nls);
295 cn_already_initialized = 1;
297 proc_create_single("connector", S_IRUGO, init_net.proc_net, cn_proc_show);
302 static void cn_fini(void)
304 struct cn_dev *dev = &cdev;
306 cn_already_initialized = 0;
308 remove_proc_entry("connector", init_net.proc_net);
310 cn_queue_free_dev(dev->cbdev);
311 netlink_kernel_release(dev->nls);
314 subsys_initcall(cn_init);
315 module_exit(cn_fini);