sock_diag.c 5.5 KB
Newer Older
1 2 3 4 5 6
#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>
7
#include <net/sock.h>
8 9 10 11

#include <linux/inet_diag.h>
#include <linux/sock_diag.h>

12
static const struct sock_diag_handler *sock_diag_handlers[AF_MAX];
13 14 15
static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
static DEFINE_MUTEX(sock_diag_table_mutex);

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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);

35 36
int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
{
37
	u32 mem[SK_MEMINFO_VARS];
38 39 40 41 42 43 44 45

	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);
E
Eric Dumazet 已提交
46
	mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len;
47

48
	return nla_put(skb, attrtype, sizeof(mem), &mem);
49 50 51
}
EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);

52
int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
53 54
			     struct sk_buff *skb, int attrtype)
{
55
	struct sock_fprog_kern *fprog;
56
	struct sk_filter *filter;
57 58
	struct nlattr *attr;
	unsigned int flen;
59 60
	int err = 0;

61
	if (!may_report_filterinfo) {
62 63 64 65 66 67
		nla_reserve(skb, attrtype, 0);
		return 0;
	}

	rcu_read_lock();
	filter = rcu_dereference(sk->sk_filter);
68 69
	if (!filter)
		goto out;
70

71
	fprog = filter->orig_prog;
72
	flen = bpf_classic_proglen(fprog);
73 74

	attr = nla_reserve(skb, attrtype, flen);
75 76 77 78 79
	if (attr == NULL) {
		err = -EMSGSIZE;
		goto out;
	}

80
	memcpy(nla_data(attr), fprog->filter, flen);
81 82 83 84 85 86
out:
	rcu_read_unlock();
	return err;
}
EXPORT_SYMBOL(sock_diag_put_filterinfo);

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
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);

103
int sock_diag_register(const struct sock_diag_handler *hndl)
104 105 106
{
	int err = 0;

D
Dan Carpenter 已提交
107
	if (hndl->family >= AF_MAX)
108 109 110 111 112 113 114 115 116 117 118 119 120
		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);

121
void sock_diag_unregister(const struct sock_diag_handler *hnld)
122 123 124
{
	int family = hnld->family;

D
Dan Carpenter 已提交
125
	if (family >= AF_MAX)
126 127 128 129 130 131 132 133 134 135 136 137
		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;
138
	struct sock_diag_req *req = nlmsg_data(nlh);
139
	const struct sock_diag_handler *hndl;
140 141 142 143

	if (nlmsg_len(nlh) < sizeof(*req))
		return -EINVAL;

144 145 146
	if (req->sdiag_family >= AF_MAX)
		return -EINVAL;

147 148 149 150 151 152
	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];
153 154 155 156
	if (hndl == NULL)
		err = -ENOENT;
	else
		err = hndl->dump(skb, nlh);
157
	mutex_unlock(&sock_diag_table_mutex);
158 159 160 161 162 163 164 165 166 167 168 169 170

	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,
171
					NETLINK_SOCK_DIAG, AF_INET);
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

		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);
}

A
Andrey Vagin 已提交
197
static int __net_init diag_net_init(struct net *net)
198
{
199 200 201 202
	struct netlink_kernel_cfg cfg = {
		.input	= sock_diag_rcv,
	};

203
	net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
A
Andrey Vagin 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
	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);
221 222 223 224
}

static void __exit sock_diag_exit(void)
{
A
Andrey Vagin 已提交
225
	unregister_pernet_subsys(&diag_net_ops);
226 227 228 229 230 231
}

module_init(sock_diag_init);
module_exit(sock_diag_exit);
MODULE_LICENSE("GPL");
MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);