Linux Audio

Check our new training course

Loading...
v6.13.7
  1/* License: GPL */
  2
  3#include <linux/filter.h>
  4#include <linux/mutex.h>
  5#include <linux/socket.h>
  6#include <linux/skbuff.h>
  7#include <net/netlink.h>
  8#include <net/net_namespace.h>
  9#include <linux/module.h>
 
 10#include <net/sock.h>
 11#include <linux/kernel.h>
 12#include <linux/tcp.h>
 13#include <linux/workqueue.h>
 14#include <linux/nospec.h>
 15#include <linux/cookie.h>
 16#include <linux/inet_diag.h>
 17#include <linux/sock_diag.h>
 18
 19static const struct sock_diag_handler __rcu *sock_diag_handlers[AF_MAX];
 20
 21static const struct sock_diag_inet_compat __rcu *inet_rcv_compat;
 22
 23static struct workqueue_struct *broadcast_wq;
 24
 25DEFINE_COOKIE(sock_cookie);
 26
 27u64 __sock_gen_cookie(struct sock *sk)
 28{
 29	u64 res = atomic64_read(&sk->sk_cookie);
 30
 31	if (!res) {
 32		u64 new = gen_cookie_next(&sock_cookie);
 33
 34		atomic64_cmpxchg(&sk->sk_cookie, res, new);
 35
 36		/* Another thread might have changed sk_cookie before us. */
 37		res = atomic64_read(&sk->sk_cookie);
 38	}
 39	return res;
 40}
 41
 42int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie)
 43{
 44	u64 res;
 45
 46	if (cookie[0] == INET_DIAG_NOCOOKIE && cookie[1] == INET_DIAG_NOCOOKIE)
 47		return 0;
 48
 49	res = sock_gen_cookie(sk);
 50	if ((u32)res != cookie[0] || (u32)(res >> 32) != cookie[1])
 51		return -ESTALE;
 52
 53	return 0;
 54}
 55EXPORT_SYMBOL_GPL(sock_diag_check_cookie);
 56
 57void sock_diag_save_cookie(struct sock *sk, __u32 *cookie)
 58{
 59	u64 res = sock_gen_cookie(sk);
 60
 61	cookie[0] = (u32)res;
 62	cookie[1] = (u32)(res >> 32);
 63}
 64EXPORT_SYMBOL_GPL(sock_diag_save_cookie);
 65
 66int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
 67{
 68	u32 mem[SK_MEMINFO_VARS];
 69
 70	sk_get_meminfo(sk, mem);
 71
 72	return nla_put(skb, attrtype, sizeof(mem), &mem);
 73}
 74EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
 75
 76int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
 77			     struct sk_buff *skb, int attrtype)
 78{
 79	struct sock_fprog_kern *fprog;
 80	struct sk_filter *filter;
 81	struct nlattr *attr;
 82	unsigned int flen;
 83	int err = 0;
 84
 85	if (!may_report_filterinfo) {
 86		nla_reserve(skb, attrtype, 0);
 87		return 0;
 88	}
 89
 90	rcu_read_lock();
 91	filter = rcu_dereference(sk->sk_filter);
 92	if (!filter)
 93		goto out;
 94
 95	fprog = filter->prog->orig_prog;
 96	if (!fprog)
 97		goto out;
 98
 99	flen = bpf_classic_proglen(fprog);
100
101	attr = nla_reserve(skb, attrtype, flen);
102	if (attr == NULL) {
103		err = -EMSGSIZE;
104		goto out;
105	}
106
107	memcpy(nla_data(attr), fprog->filter, flen);
108out:
109	rcu_read_unlock();
110	return err;
111}
112EXPORT_SYMBOL(sock_diag_put_filterinfo);
113
114struct broadcast_sk {
115	struct sock *sk;
116	struct work_struct work;
117};
118
119static size_t sock_diag_nlmsg_size(void)
120{
121	return NLMSG_ALIGN(sizeof(struct inet_diag_msg)
122	       + nla_total_size(sizeof(u8)) /* INET_DIAG_PROTOCOL */
123	       + nla_total_size_64bit(sizeof(struct tcp_info))); /* INET_DIAG_INFO */
124}
125
126static const struct sock_diag_handler *sock_diag_lock_handler(int family)
127{
128	const struct sock_diag_handler *handler;
129
130	rcu_read_lock();
131	handler = rcu_dereference(sock_diag_handlers[family]);
132	if (handler && !try_module_get(handler->owner))
133		handler = NULL;
134	rcu_read_unlock();
135
136	return handler;
137}
138
139static void sock_diag_unlock_handler(const struct sock_diag_handler *handler)
140{
141	module_put(handler->owner);
142}
143
144static void sock_diag_broadcast_destroy_work(struct work_struct *work)
145{
146	struct broadcast_sk *bsk =
147		container_of(work, struct broadcast_sk, work);
148	struct sock *sk = bsk->sk;
149	const struct sock_diag_handler *hndl;
150	struct sk_buff *skb;
151	const enum sknetlink_groups group = sock_diag_destroy_group(sk);
152	int err = -1;
153
154	WARN_ON(group == SKNLGRP_NONE);
155
156	skb = nlmsg_new(sock_diag_nlmsg_size(), GFP_KERNEL);
157	if (!skb)
158		goto out;
159
160	hndl = sock_diag_lock_handler(sk->sk_family);
161	if (hndl) {
162		if (hndl->get_info)
163			err = hndl->get_info(skb, sk);
164		sock_diag_unlock_handler(hndl);
165	}
166	if (!err)
167		nlmsg_multicast(sock_net(sk)->diag_nlsk, skb, 0, group,
168				GFP_KERNEL);
169	else
170		kfree_skb(skb);
171out:
172	sk_destruct(sk);
173	kfree(bsk);
174}
175
176void sock_diag_broadcast_destroy(struct sock *sk)
177{
178	/* Note, this function is often called from an interrupt context. */
179	struct broadcast_sk *bsk =
180		kmalloc(sizeof(struct broadcast_sk), GFP_ATOMIC);
181	if (!bsk)
182		return sk_destruct(sk);
183	bsk->sk = sk;
184	INIT_WORK(&bsk->work, sock_diag_broadcast_destroy_work);
185	queue_work(broadcast_wq, &bsk->work);
186}
 
187
188void sock_diag_register_inet_compat(const struct sock_diag_inet_compat *ptr)
189{
190	xchg(&inet_rcv_compat, RCU_INITIALIZER(ptr));
 
 
191}
192EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
193
194void sock_diag_unregister_inet_compat(const struct sock_diag_inet_compat *ptr)
195{
196	const struct sock_diag_inet_compat *old;
197
198	old = unrcu_pointer(xchg(&inet_rcv_compat, NULL));
199	WARN_ON_ONCE(old != ptr);
200}
201EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
202
203int sock_diag_register(const struct sock_diag_handler *hndl)
204{
205	int family = hndl->family;
206
207	if (family >= AF_MAX)
208		return -EINVAL;
209
210	return !cmpxchg((const struct sock_diag_handler **)
211				&sock_diag_handlers[family],
212			NULL, hndl) ? 0 : -EBUSY;
 
 
 
 
 
213}
214EXPORT_SYMBOL_GPL(sock_diag_register);
215
216void sock_diag_unregister(const struct sock_diag_handler *hndl)
217{
218	int family = hndl->family;
219
220	if (family >= AF_MAX)
221		return;
222
223	xchg((const struct sock_diag_handler **)&sock_diag_handlers[family],
224	     NULL);
 
 
225}
226EXPORT_SYMBOL_GPL(sock_diag_unregister);
227
228static int __sock_diag_cmd(struct sk_buff *skb, struct nlmsghdr *nlh)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229{
230	int err;
231	struct sock_diag_req *req = nlmsg_data(nlh);
232	const struct sock_diag_handler *hndl;
233
234	if (nlmsg_len(nlh) < sizeof(*req))
235		return -EINVAL;
236
237	if (req->sdiag_family >= AF_MAX)
238		return -EINVAL;
239	req->sdiag_family = array_index_nospec(req->sdiag_family, AF_MAX);
240
241	if (!rcu_access_pointer(sock_diag_handlers[req->sdiag_family]))
242		sock_load_diag_module(req->sdiag_family, 0);
243
244	hndl = sock_diag_lock_handler(req->sdiag_family);
245	if (hndl == NULL)
246		return -ENOENT;
247
248	if (nlh->nlmsg_type == SOCK_DIAG_BY_FAMILY)
249		err = hndl->dump(skb, nlh);
250	else if (nlh->nlmsg_type == SOCK_DESTROY && hndl->destroy)
251		err = hndl->destroy(skb, nlh);
252	else
253		err = -EOPNOTSUPP;
254	sock_diag_unlock_handler(hndl);
255
256	return err;
257}
258
259static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
260			     struct netlink_ext_ack *extack)
261{
262	const struct sock_diag_inet_compat *ptr;
263	int ret;
264
265	switch (nlh->nlmsg_type) {
266	case TCPDIAG_GETSOCK:
267	case DCCPDIAG_GETSOCK:
268
269		if (!rcu_access_pointer(inet_rcv_compat))
270			sock_load_diag_module(AF_INET, 0);
271
272		rcu_read_lock();
273		ptr = rcu_dereference(inet_rcv_compat);
274		if (ptr && !try_module_get(ptr->owner))
275			ptr = NULL;
276		rcu_read_unlock();
277
278		ret = -EOPNOTSUPP;
279		if (ptr) {
280			ret = ptr->fn(skb, nlh);
281			module_put(ptr->owner);
282		}
283
284		return ret;
285	case SOCK_DIAG_BY_FAMILY:
286	case SOCK_DESTROY:
287		return __sock_diag_cmd(skb, nlh);
288	default:
289		return -EINVAL;
290	}
291}
292
 
 
293static void sock_diag_rcv(struct sk_buff *skb)
294{
 
295	netlink_rcv_skb(skb, &sock_diag_rcv_msg);
 
296}
297
298static int sock_diag_bind(struct net *net, int group)
299{
300	switch (group) {
301	case SKNLGRP_INET_TCP_DESTROY:
302	case SKNLGRP_INET_UDP_DESTROY:
303		if (!rcu_access_pointer(sock_diag_handlers[AF_INET]))
304			sock_load_diag_module(AF_INET, 0);
305		break;
306	case SKNLGRP_INET6_TCP_DESTROY:
307	case SKNLGRP_INET6_UDP_DESTROY:
308		if (!rcu_access_pointer(sock_diag_handlers[AF_INET6]))
309			sock_load_diag_module(AF_INET6, 0);
310		break;
311	}
312	return 0;
313}
314
315int sock_diag_destroy(struct sock *sk, int err)
316{
317	if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
318		return -EPERM;
319
320	if (!sk->sk_prot->diag_destroy)
321		return -EOPNOTSUPP;
322
323	return sk->sk_prot->diag_destroy(sk, err);
324}
325EXPORT_SYMBOL_GPL(sock_diag_destroy);
326
327static int __net_init diag_net_init(struct net *net)
328{
329	struct netlink_kernel_cfg cfg = {
330		.groups	= SKNLGRP_MAX,
331		.input	= sock_diag_rcv,
332		.bind	= sock_diag_bind,
333		.flags	= NL_CFG_F_NONROOT_RECV,
334	};
335
336	net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
337	return net->diag_nlsk == NULL ? -ENOMEM : 0;
338}
339
340static void __net_exit diag_net_exit(struct net *net)
341{
342	netlink_kernel_release(net->diag_nlsk);
343	net->diag_nlsk = NULL;
344}
345
346static struct pernet_operations diag_net_ops = {
347	.init = diag_net_init,
348	.exit = diag_net_exit,
349};
350
351static int __init sock_diag_init(void)
352{
353	broadcast_wq = alloc_workqueue("sock_diag_events", 0, 0);
354	BUG_ON(!broadcast_wq);
355	return register_pernet_subsys(&diag_net_ops);
356}
357device_initcall(sock_diag_init);
v3.5.6
 
 
 
  1#include <linux/mutex.h>
  2#include <linux/socket.h>
  3#include <linux/skbuff.h>
  4#include <net/netlink.h>
  5#include <net/net_namespace.h>
  6#include <linux/module.h>
  7#include <linux/rtnetlink.h>
  8#include <net/sock.h>
  9
 
 
 
 
 10#include <linux/inet_diag.h>
 11#include <linux/sock_diag.h>
 12
 13static const struct sock_diag_handler *sock_diag_handlers[AF_MAX];
 14static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
 15static DEFINE_MUTEX(sock_diag_table_mutex);
 16
 17int sock_diag_check_cookie(void *sk, __u32 *cookie)
 18{
 19	if ((cookie[0] != INET_DIAG_NOCOOKIE ||
 20	     cookie[1] != INET_DIAG_NOCOOKIE) &&
 21	    ((u32)(unsigned long)sk != cookie[0] ||
 22	     (u32)((((unsigned long)sk) >> 31) >> 1) != cookie[1]))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 23		return -ESTALE;
 24	else
 25		return 0;
 26}
 27EXPORT_SYMBOL_GPL(sock_diag_check_cookie);
 28
 29void sock_diag_save_cookie(void *sk, __u32 *cookie)
 30{
 31	cookie[0] = (u32)(unsigned long)sk;
 32	cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
 
 
 33}
 34EXPORT_SYMBOL_GPL(sock_diag_save_cookie);
 35
 36int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
 37{
 38	__u32 *mem;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 39
 40	mem = RTA_DATA(__RTA_PUT(skb, attrtype, SK_MEMINFO_VARS * sizeof(__u32)));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 41
 42	mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk);
 43	mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf;
 44	mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk);
 45	mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf;
 46	mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
 47	mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued;
 48	mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 49
 50	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 51
 52rtattr_failure:
 53	return -EMSGSIZE;
 
 
 
 
 
 
 
 
 54}
 55EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
 56
 57void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
 58{
 59	mutex_lock(&sock_diag_table_mutex);
 60	inet_rcv_compat = fn;
 61	mutex_unlock(&sock_diag_table_mutex);
 62}
 63EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
 64
 65void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
 66{
 67	mutex_lock(&sock_diag_table_mutex);
 68	inet_rcv_compat = NULL;
 69	mutex_unlock(&sock_diag_table_mutex);
 
 70}
 71EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
 72
 73int sock_diag_register(const struct sock_diag_handler *hndl)
 74{
 75	int err = 0;
 76
 77	if (hndl->family >= AF_MAX)
 78		return -EINVAL;
 79
 80	mutex_lock(&sock_diag_table_mutex);
 81	if (sock_diag_handlers[hndl->family])
 82		err = -EBUSY;
 83	else
 84		sock_diag_handlers[hndl->family] = hndl;
 85	mutex_unlock(&sock_diag_table_mutex);
 86
 87	return err;
 88}
 89EXPORT_SYMBOL_GPL(sock_diag_register);
 90
 91void sock_diag_unregister(const struct sock_diag_handler *hnld)
 92{
 93	int family = hnld->family;
 94
 95	if (family >= AF_MAX)
 96		return;
 97
 98	mutex_lock(&sock_diag_table_mutex);
 99	BUG_ON(sock_diag_handlers[family] != hnld);
100	sock_diag_handlers[family] = NULL;
101	mutex_unlock(&sock_diag_table_mutex);
102}
103EXPORT_SYMBOL_GPL(sock_diag_unregister);
104
105static const inline struct sock_diag_handler *sock_diag_lock_handler(int family)
106{
107	if (sock_diag_handlers[family] == NULL)
108		request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
109				NETLINK_SOCK_DIAG, family);
110
111	mutex_lock(&sock_diag_table_mutex);
112	return sock_diag_handlers[family];
113}
114
115static inline void sock_diag_unlock_handler(const struct sock_diag_handler *h)
116{
117	mutex_unlock(&sock_diag_table_mutex);
118}
119
120static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
121{
122	int err;
123	struct sock_diag_req *req = NLMSG_DATA(nlh);
124	const struct sock_diag_handler *hndl;
125
126	if (nlmsg_len(nlh) < sizeof(*req))
127		return -EINVAL;
128
 
 
 
 
 
 
 
129	hndl = sock_diag_lock_handler(req->sdiag_family);
130	if (hndl == NULL)
131		err = -ENOENT;
 
 
 
 
 
132	else
133		err = hndl->dump(skb, nlh);
134	sock_diag_unlock_handler(hndl);
135
136	return err;
137}
138
139static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
 
140{
 
141	int ret;
142
143	switch (nlh->nlmsg_type) {
144	case TCPDIAG_GETSOCK:
145	case DCCPDIAG_GETSOCK:
146		if (inet_rcv_compat == NULL)
147			request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
148					NETLINK_SOCK_DIAG, AF_INET);
149
150		mutex_lock(&sock_diag_table_mutex);
151		if (inet_rcv_compat != NULL)
152			ret = inet_rcv_compat(skb, nlh);
153		else
154			ret = -EOPNOTSUPP;
155		mutex_unlock(&sock_diag_table_mutex);
 
 
 
 
 
156
157		return ret;
158	case SOCK_DIAG_BY_FAMILY:
159		return __sock_diag_rcv_msg(skb, nlh);
 
160	default:
161		return -EINVAL;
162	}
163}
164
165static DEFINE_MUTEX(sock_diag_mutex);
166
167static void sock_diag_rcv(struct sk_buff *skb)
168{
169	mutex_lock(&sock_diag_mutex);
170	netlink_rcv_skb(skb, &sock_diag_rcv_msg);
171	mutex_unlock(&sock_diag_mutex);
172}
173
174struct sock *sock_diag_nlsk;
175EXPORT_SYMBOL_GPL(sock_diag_nlsk);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
177static int __init sock_diag_init(void)
178{
179	sock_diag_nlsk = netlink_kernel_create(&init_net, NETLINK_SOCK_DIAG, 0,
180					sock_diag_rcv, NULL, THIS_MODULE);
181	return sock_diag_nlsk == NULL ? -ENOMEM : 0;
 
 
 
 
182}
 
183
184static void __exit sock_diag_exit(void)
185{
186	netlink_kernel_release(sock_diag_nlsk);
 
 
 
 
 
 
 
 
187}
188
189module_init(sock_diag_init);
190module_exit(sock_diag_exit);
191MODULE_LICENSE("GPL");
192MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);