| 
									
										
										
										
											2011-12-06 07:59:52 +00:00
										 |  |  | #include <linux/mutex.h>
 | 
					
						
							|  |  |  | #include <linux/socket.h>
 | 
					
						
							|  |  |  | #include <linux/skbuff.h>
 | 
					
						
							|  |  |  | #include <net/netlink.h>
 | 
					
						
							|  |  |  | #include <net/net_namespace.h>
 | 
					
						
							|  |  |  | #include <linux/module.h>
 | 
					
						
							| 
									
										
										
										
											2011-12-30 00:53:13 +00:00
										 |  |  | #include <net/sock.h>
 | 
					
						
							| 
									
										
										
										
											2011-12-06 07:59:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <linux/inet_diag.h>
 | 
					
						
							|  |  |  | #include <linux/sock_diag.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-24 18:21:07 +00:00
										 |  |  | static const struct sock_diag_handler *sock_diag_handlers[AF_MAX]; | 
					
						
							| 
									
										
										
										
											2011-12-06 07:59:52 +00:00
										 |  |  | static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh); | 
					
						
							|  |  |  | static DEFINE_MUTEX(sock_diag_table_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-15 02:43:44 +00:00
										 |  |  | int sock_diag_check_cookie(void *sk, __u32 *cookie) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if ((cookie[0] != INET_DIAG_NOCOOKIE || | 
					
						
							|  |  |  | 	     cookie[1] != INET_DIAG_NOCOOKIE) && | 
					
						
							|  |  |  | 	    ((u32)(unsigned long)sk != cookie[0] || | 
					
						
							|  |  |  | 	     (u32)((((unsigned long)sk) >> 31) >> 1) != cookie[1])) | 
					
						
							|  |  |  | 		return -ESTALE; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(sock_diag_check_cookie); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void sock_diag_save_cookie(void *sk, __u32 *cookie) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	cookie[0] = (u32)(unsigned long)sk; | 
					
						
							|  |  |  | 	cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(sock_diag_save_cookie); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-30 00:53:13 +00:00
										 |  |  | int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-06-26 23:36:11 +00:00
										 |  |  | 	u32 mem[SK_MEMINFO_VARS]; | 
					
						
							| 
									
										
										
										
											2011-12-30 00:53:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk); | 
					
						
							|  |  |  | 	mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf; | 
					
						
							|  |  |  | 	mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk); | 
					
						
							|  |  |  | 	mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf; | 
					
						
							|  |  |  | 	mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc; | 
					
						
							|  |  |  | 	mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued; | 
					
						
							|  |  |  | 	mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc); | 
					
						
							| 
									
										
										
										
											2012-06-04 03:50:35 +00:00
										 |  |  | 	mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len; | 
					
						
							| 
									
										
										
										
											2011-12-30 00:53:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-26 23:36:11 +00:00
										 |  |  | 	return nla_put(skb, attrtype, sizeof(mem), &mem); | 
					
						
							| 
									
										
										
										
											2011-12-30 00:53:13 +00:00
										 |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(sock_diag_put_meminfo); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 06:53:54 +00:00
										 |  |  | int sock_diag_put_filterinfo(struct user_namespace *user_ns, struct sock *sk, | 
					
						
							|  |  |  | 			     struct sk_buff *skb, int attrtype) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct nlattr *attr; | 
					
						
							|  |  |  | 	struct sk_filter *filter; | 
					
						
							|  |  |  | 	unsigned int len; | 
					
						
							|  |  |  | 	int err = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!ns_capable(user_ns, CAP_NET_ADMIN)) { | 
					
						
							|  |  |  | 		nla_reserve(skb, attrtype, 0); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rcu_read_lock(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	filter = rcu_dereference(sk->sk_filter); | 
					
						
							|  |  |  | 	len = filter ? filter->len * sizeof(struct sock_filter) : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	attr = nla_reserve(skb, attrtype, len); | 
					
						
							|  |  |  | 	if (attr == NULL) { | 
					
						
							|  |  |  | 		err = -EMSGSIZE; | 
					
						
							|  |  |  | 		goto out; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (filter) | 
					
						
							|  |  |  | 		memcpy(nla_data(attr), filter->insns, len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | out: | 
					
						
							|  |  |  | 	rcu_read_unlock(); | 
					
						
							|  |  |  | 	return err; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL(sock_diag_put_filterinfo); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-06 07:59:52 +00:00
										 |  |  | void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	mutex_lock(&sock_diag_table_mutex); | 
					
						
							|  |  |  | 	inet_rcv_compat = fn; | 
					
						
							|  |  |  | 	mutex_unlock(&sock_diag_table_mutex); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	mutex_lock(&sock_diag_table_mutex); | 
					
						
							|  |  |  | 	inet_rcv_compat = NULL; | 
					
						
							|  |  |  | 	mutex_unlock(&sock_diag_table_mutex); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-24 18:21:07 +00:00
										 |  |  | int sock_diag_register(const struct sock_diag_handler *hndl) | 
					
						
							| 
									
										
										
										
											2011-12-06 07:59:52 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int err = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-07 20:49:38 +00:00
										 |  |  | 	if (hndl->family >= AF_MAX) | 
					
						
							| 
									
										
										
										
											2011-12-06 07:59:52 +00:00
										 |  |  | 		return -EINVAL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mutex_lock(&sock_diag_table_mutex); | 
					
						
							|  |  |  | 	if (sock_diag_handlers[hndl->family]) | 
					
						
							|  |  |  | 		err = -EBUSY; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		sock_diag_handlers[hndl->family] = hndl; | 
					
						
							|  |  |  | 	mutex_unlock(&sock_diag_table_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return err; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(sock_diag_register); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-24 18:21:07 +00:00
										 |  |  | void sock_diag_unregister(const struct sock_diag_handler *hnld) | 
					
						
							| 
									
										
										
										
											2011-12-06 07:59:52 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int family = hnld->family; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-07 20:49:38 +00:00
										 |  |  | 	if (family >= AF_MAX) | 
					
						
							| 
									
										
										
										
											2011-12-06 07:59:52 +00:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mutex_lock(&sock_diag_table_mutex); | 
					
						
							|  |  |  | 	BUG_ON(sock_diag_handlers[family] != hnld); | 
					
						
							|  |  |  | 	sock_diag_handlers[family] = NULL; | 
					
						
							|  |  |  | 	mutex_unlock(&sock_diag_table_mutex); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(sock_diag_unregister); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int err; | 
					
						
							| 
									
										
										
										
											2012-06-26 23:36:11 +00:00
										 |  |  | 	struct sock_diag_req *req = nlmsg_data(nlh); | 
					
						
							| 
									
										
										
										
											2012-04-24 18:21:07 +00:00
										 |  |  | 	const struct sock_diag_handler *hndl; | 
					
						
							| 
									
										
										
										
											2011-12-06 07:59:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (nlmsg_len(nlh) < sizeof(*req)) | 
					
						
							|  |  |  | 		return -EINVAL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-23 01:13:47 +00:00
										 |  |  | 	if (req->sdiag_family >= AF_MAX) | 
					
						
							|  |  |  | 		return -EINVAL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-23 01:13:48 +00:00
										 |  |  | 	if (sock_diag_handlers[req->sdiag_family] == NULL) | 
					
						
							|  |  |  | 		request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, | 
					
						
							|  |  |  | 				NETLINK_SOCK_DIAG, req->sdiag_family); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mutex_lock(&sock_diag_table_mutex); | 
					
						
							|  |  |  | 	hndl = sock_diag_handlers[req->sdiag_family]; | 
					
						
							| 
									
										
										
										
											2011-12-06 07:59:52 +00:00
										 |  |  | 	if (hndl == NULL) | 
					
						
							|  |  |  | 		err = -ENOENT; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		err = hndl->dump(skb, nlh); | 
					
						
							| 
									
										
										
										
											2013-02-23 01:13:48 +00:00
										 |  |  | 	mutex_unlock(&sock_diag_table_mutex); | 
					
						
							| 
									
										
										
										
											2011-12-06 07:59:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return err; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (nlh->nlmsg_type) { | 
					
						
							|  |  |  | 	case TCPDIAG_GETSOCK: | 
					
						
							|  |  |  | 	case DCCPDIAG_GETSOCK: | 
					
						
							|  |  |  | 		if (inet_rcv_compat == NULL) | 
					
						
							|  |  |  | 			request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, | 
					
						
							| 
									
										
										
										
											2011-12-15 02:43:27 +00:00
										 |  |  | 					NETLINK_SOCK_DIAG, AF_INET); | 
					
						
							| 
									
										
										
										
											2011-12-06 07:59:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		mutex_lock(&sock_diag_table_mutex); | 
					
						
							|  |  |  | 		if (inet_rcv_compat != NULL) | 
					
						
							|  |  |  | 			ret = inet_rcv_compat(skb, nlh); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			ret = -EOPNOTSUPP; | 
					
						
							|  |  |  | 		mutex_unlock(&sock_diag_table_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return ret; | 
					
						
							|  |  |  | 	case SOCK_DIAG_BY_FAMILY: | 
					
						
							|  |  |  | 		return __sock_diag_rcv_msg(skb, nlh); | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		return -EINVAL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static DEFINE_MUTEX(sock_diag_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void sock_diag_rcv(struct sk_buff *skb) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	mutex_lock(&sock_diag_mutex); | 
					
						
							|  |  |  | 	netlink_rcv_skb(skb, &sock_diag_rcv_msg); | 
					
						
							|  |  |  | 	mutex_unlock(&sock_diag_mutex); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-16 04:28:49 +00:00
										 |  |  | static int __net_init diag_net_init(struct net *net) | 
					
						
							| 
									
										
										
										
											2011-12-06 07:59:52 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-06-29 06:15:21 +00:00
										 |  |  | 	struct netlink_kernel_cfg cfg = { | 
					
						
							|  |  |  | 		.input	= sock_diag_rcv, | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-08 02:53:54 +00:00
										 |  |  | 	net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg); | 
					
						
							| 
									
										
										
										
											2012-07-16 04:28:49 +00:00
										 |  |  | 	return net->diag_nlsk == NULL ? -ENOMEM : 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void __net_exit diag_net_exit(struct net *net) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	netlink_kernel_release(net->diag_nlsk); | 
					
						
							|  |  |  | 	net->diag_nlsk = NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct pernet_operations diag_net_ops = { | 
					
						
							|  |  |  | 	.init = diag_net_init, | 
					
						
							|  |  |  | 	.exit = diag_net_exit, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int __init sock_diag_init(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return register_pernet_subsys(&diag_net_ops); | 
					
						
							| 
									
										
										
										
											2011-12-06 07:59:52 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void __exit sock_diag_exit(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-07-16 04:28:49 +00:00
										 |  |  | 	unregister_pernet_subsys(&diag_net_ops); | 
					
						
							| 
									
										
										
										
											2011-12-06 07:59:52 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module_init(sock_diag_init); | 
					
						
							|  |  |  | module_exit(sock_diag_exit); | 
					
						
							|  |  |  | MODULE_LICENSE("GPL"); | 
					
						
							|  |  |  | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG); |