“ad1081210f3c91874f9fe9b48c3934c7db9714b7”上不存在“README.md”
sock_diag.c 8.2 KB
Newer Older
1 2
/* License: GPL */

3 4 5 6 7 8
#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>
9
#include <net/sock.h>
10 11 12
#include <linux/kernel.h>
#include <linux/tcp.h>
#include <linux/workqueue.h>
13 14 15 16

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

17
static const struct sock_diag_handler *sock_diag_handlers[AF_MAX];
18 19
static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
static DEFINE_MUTEX(sock_diag_table_mutex);
20
static struct workqueue_struct *broadcast_wq;
21

22
static u64 sock_gen_cookie(struct sock *sk)
23
{
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	while (1) {
		u64 res = atomic64_read(&sk->sk_cookie);

		if (res)
			return res;
		res = atomic64_inc_return(&sock_net(sk)->cookie_gen);
		atomic64_cmpxchg(&sk->sk_cookie, 0, res);
	}
}

int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie)
{
	u64 res;

	if (cookie[0] == INET_DIAG_NOCOOKIE && cookie[1] == INET_DIAG_NOCOOKIE)
39
		return 0;
40 41 42 43 44 45

	res = sock_gen_cookie(sk);
	if ((u32)res != cookie[0] || (u32)(res >> 32) != cookie[1])
		return -ESTALE;

	return 0;
46 47 48
}
EXPORT_SYMBOL_GPL(sock_diag_check_cookie);

49
void sock_diag_save_cookie(struct sock *sk, __u32 *cookie)
50
{
51 52 53 54
	u64 res = sock_gen_cookie(sk);

	cookie[0] = (u32)res;
	cookie[1] = (u32)(res >> 32);
55 56 57
}
EXPORT_SYMBOL_GPL(sock_diag_save_cookie);

58 59
int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
{
60
	u32 mem[SK_MEMINFO_VARS];
61 62 63 64 65 66 67 68

	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);
69
	mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len;
70
	mem[SK_MEMINFO_DROPS] = atomic_read(&sk->sk_drops);
71

72
	return nla_put(skb, attrtype, sizeof(mem), &mem);
73 74 75
}
EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);

76
int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
77 78
			     struct sk_buff *skb, int attrtype)
{
79
	struct sock_fprog_kern *fprog;
80
	struct sk_filter *filter;
81 82
	struct nlattr *attr;
	unsigned int flen;
83 84
	int err = 0;

85
	if (!may_report_filterinfo) {
86 87 88 89 90 91
		nla_reserve(skb, attrtype, 0);
		return 0;
	}

	rcu_read_lock();
	filter = rcu_dereference(sk->sk_filter);
92 93
	if (!filter)
		goto out;
94

95
	fprog = filter->prog->orig_prog;
96 97 98
	if (!fprog)
		goto out;

99
	flen = bpf_classic_proglen(fprog);
100 101

	attr = nla_reserve(skb, attrtype, flen);
102 103 104 105 106
	if (attr == NULL) {
		err = -EMSGSIZE;
		goto out;
	}

107
	memcpy(nla_data(attr), fprog->filter, flen);
108 109 110 111 112 113
out:
	rcu_read_unlock();
	return err;
}
EXPORT_SYMBOL(sock_diag_put_filterinfo);

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
struct broadcast_sk {
	struct sock *sk;
	struct work_struct work;
};

static size_t sock_diag_nlmsg_size(void)
{
	return NLMSG_ALIGN(sizeof(struct inet_diag_msg)
	       + nla_total_size(sizeof(u8)) /* INET_DIAG_PROTOCOL */
	       + nla_total_size(sizeof(struct tcp_info))); /* INET_DIAG_INFO */
}

static void sock_diag_broadcast_destroy_work(struct work_struct *work)
{
	struct broadcast_sk *bsk =
		container_of(work, struct broadcast_sk, work);
	struct sock *sk = bsk->sk;
	const struct sock_diag_handler *hndl;
	struct sk_buff *skb;
	const enum sknetlink_groups group = sock_diag_destroy_group(sk);
	int err = -1;

	WARN_ON(group == SKNLGRP_NONE);

	skb = nlmsg_new(sock_diag_nlmsg_size(), GFP_KERNEL);
	if (!skb)
		goto out;

	mutex_lock(&sock_diag_table_mutex);
	hndl = sock_diag_handlers[sk->sk_family];
	if (hndl && hndl->get_info)
		err = hndl->get_info(skb, sk);
	mutex_unlock(&sock_diag_table_mutex);

	if (!err)
		nlmsg_multicast(sock_net(sk)->diag_nlsk, skb, 0, group,
				GFP_KERNEL);
	else
		kfree_skb(skb);
out:
	sk_destruct(sk);
	kfree(bsk);
}

void sock_diag_broadcast_destroy(struct sock *sk)
{
	/* Note, this function is often called from an interrupt context. */
	struct broadcast_sk *bsk =
		kmalloc(sizeof(struct broadcast_sk), GFP_ATOMIC);
	if (!bsk)
		return sk_destruct(sk);
	bsk->sk = sk;
	INIT_WORK(&bsk->work, sock_diag_broadcast_destroy_work);
	queue_work(broadcast_wq, &bsk->work);
}

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
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);

186
int sock_diag_register(const struct sock_diag_handler *hndl)
187 188 189
{
	int err = 0;

190
	if (hndl->family >= AF_MAX)
191 192 193 194 195 196 197 198 199 200 201 202 203
		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);

204
void sock_diag_unregister(const struct sock_diag_handler *hnld)
205 206 207
{
	int family = hnld->family;

208
	if (family >= AF_MAX)
209 210 211 212 213 214 215 216 217
		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);

218
static int __sock_diag_cmd(struct sk_buff *skb, struct nlmsghdr *nlh)
219 220
{
	int err;
221
	struct sock_diag_req *req = nlmsg_data(nlh);
222
	const struct sock_diag_handler *hndl;
223 224 225 226

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

227 228 229
	if (req->sdiag_family >= AF_MAX)
		return -EINVAL;

230 231 232 233 234 235
	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];
236 237
	if (hndl == NULL)
		err = -ENOENT;
238
	else if (nlh->nlmsg_type == SOCK_DIAG_BY_FAMILY)
239
		err = hndl->dump(skb, nlh);
240 241 242 243
	else if (nlh->nlmsg_type == SOCK_DESTROY && hndl->destroy)
		err = hndl->destroy(skb, nlh);
	else
		err = -EOPNOTSUPP;
244
	mutex_unlock(&sock_diag_table_mutex);
245 246 247 248 249 250 251 252 253 254 255 256 257

	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,
258
					NETLINK_SOCK_DIAG, AF_INET);
259 260 261 262 263 264 265 266 267 268

		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:
269 270
	case SOCK_DESTROY:
		return __sock_diag_cmd(skb, nlh);
271 272 273 274 275 276 277 278 279 280 281 282 283 284
	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);
}

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
static int sock_diag_bind(struct net *net, int group)
{
	switch (group) {
	case SKNLGRP_INET_TCP_DESTROY:
	case SKNLGRP_INET_UDP_DESTROY:
		if (!sock_diag_handlers[AF_INET])
			request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
				       NETLINK_SOCK_DIAG, AF_INET);
		break;
	case SKNLGRP_INET6_TCP_DESTROY:
	case SKNLGRP_INET6_UDP_DESTROY:
		if (!sock_diag_handlers[AF_INET6])
			request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
				       NETLINK_SOCK_DIAG, AF_INET);
		break;
	}
	return 0;
}

304 305 306 307 308 309 310 311 312 313 314 315
int sock_diag_destroy(struct sock *sk, int err)
{
	if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
		return -EPERM;

	if (!sk->sk_prot->diag_destroy)
		return -EOPNOTSUPP;

	return sk->sk_prot->diag_destroy(sk, err);
}
EXPORT_SYMBOL_GPL(sock_diag_destroy);

316
static int __net_init diag_net_init(struct net *net)
317
{
318
	struct netlink_kernel_cfg cfg = {
319
		.groups	= SKNLGRP_MAX,
320
		.input	= sock_diag_rcv,
321 322
		.bind	= sock_diag_bind,
		.flags	= NL_CFG_F_NONROOT_RECV,
323 324
	};

325
	net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	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)
{
342 343
	broadcast_wq = alloc_workqueue("sock_diag_events", 0, 0);
	BUG_ON(!broadcast_wq);
344
	return register_pernet_subsys(&diag_net_ops);
345
}
346
device_initcall(sock_diag_init);
新手
引导
客服 返回
顶部