socket.c 18.0 KB
Newer Older
R
Remi Denis-Courmont 已提交
1 2 3 4 5 6 7
/*
 * File: socket.c
 *
 * Phonet sockets
 *
 * Copyright (C) 2008 Nokia Corporation.
 *
8 9
 * Authors: Sakari Ailus <sakari.ailus@nokia.com>
 *          Rémi Denis-Courmont
R
Remi Denis-Courmont 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

26
#include <linux/gfp.h>
R
Remi Denis-Courmont 已提交
27 28
#include <linux/kernel.h>
#include <linux/net.h>
29
#include <linux/poll.h>
R
Remi Denis-Courmont 已提交
30 31 32 33
#include <net/sock.h>
#include <net/tcp_states.h>

#include <linux/phonet.h>
34
#include <linux/export.h>
R
Remi Denis-Courmont 已提交
35
#include <net/phonet/phonet.h>
36
#include <net/phonet/pep.h>
R
Remi Denis-Courmont 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49
#include <net/phonet/pn_dev.h>

static int pn_socket_release(struct socket *sock)
{
	struct sock *sk = sock->sk;

	if (sk) {
		sock->sk = NULL;
		sk->sk_prot->close(sk, 0);
	}
	return 0;
}

50 51 52 53
#define PN_HASHSIZE	16
#define PN_HASHMASK	(PN_HASHSIZE-1)


R
Remi Denis-Courmont 已提交
54
static struct  {
55
	struct hlist_head hlist[PN_HASHSIZE];
56
	struct mutex lock;
57 58 59 60
} pnsocks;

void __init pn_sock_init(void)
{
61
	unsigned int i;
62 63 64

	for (i = 0; i < PN_HASHSIZE; i++)
		INIT_HLIST_HEAD(pnsocks.hlist + i);
65
	mutex_init(&pnsocks.lock);
66 67 68 69 70 71
}

static struct hlist_head *pn_hash_list(u16 obj)
{
	return pnsocks.hlist + (obj & PN_HASHMASK);
}
R
Remi Denis-Courmont 已提交
72 73 74 75 76

/*
 * Find address based on socket address, match only certain fields.
 * Also grab sock if it was found. Remember to sock_put it later.
 */
77
struct sock *pn_find_sock_by_sa(struct net *net, const struct sockaddr_pn *spn)
R
Remi Denis-Courmont 已提交
78 79 80 81 82 83
{
	struct hlist_node *node;
	struct sock *sknode;
	struct sock *rval = NULL;
	u16 obj = pn_sockaddr_get_object(spn);
	u8 res = spn->spn_resource;
84
	struct hlist_head *hlist = pn_hash_list(obj);
R
Remi Denis-Courmont 已提交
85

86 87
	rcu_read_lock();
	sk_for_each_rcu(sknode, node, hlist) {
R
Remi Denis-Courmont 已提交
88 89 90
		struct pn_sock *pn = pn_sk(sknode);
		BUG_ON(!pn->sobject); /* unbound socket */

91 92
		if (!net_eq(sock_net(sknode), net))
			continue;
R
Remi Denis-Courmont 已提交
93 94 95 96 97 98 99 100 101
		if (pn_port(obj)) {
			/* Look up socket by port */
			if (pn_port(pn->sobject) != pn_port(obj))
				continue;
		} else {
			/* If port is zero, look up by resource */
			if (pn->resource != res)
				continue;
		}
102 103
		if (pn_addr(pn->sobject) &&
		    pn_addr(pn->sobject) != pn_addr(obj))
R
Remi Denis-Courmont 已提交
104 105 106 107 108 109
			continue;

		rval = sknode;
		sock_hold(sknode);
		break;
	}
110
	rcu_read_unlock();
R
Remi Denis-Courmont 已提交
111 112

	return rval;
113 114 115 116 117
}

/* Deliver a broadcast packet (only in bottom-half) */
void pn_deliver_sock_broadcast(struct net *net, struct sk_buff *skb)
{
118
	struct hlist_head *hlist = pnsocks.hlist;
119
	unsigned int h;
120

121
	rcu_read_lock();
122 123 124
	for (h = 0; h < PN_HASHSIZE; h++) {
		struct hlist_node *node;
		struct sock *sknode;
125

126 127
		sk_for_each(sknode, node, hlist) {
			struct sk_buff *clone;
R
Remi Denis-Courmont 已提交
128

129 130 131 132 133 134 135 136 137 138
			if (!net_eq(sock_net(sknode), net))
				continue;
			if (!sock_flag(sknode, SOCK_BROADCAST))
				continue;

			clone = skb_clone(skb, GFP_ATOMIC);
			if (clone) {
				sock_hold(sknode);
				sk_receive_skb(sknode, clone, 0);
			}
139
		}
140
		hlist++;
141
	}
142
	rcu_read_unlock();
R
Remi Denis-Courmont 已提交
143 144 145 146
}

void pn_sock_hash(struct sock *sk)
{
147 148
	struct hlist_head *hlist = pn_hash_list(pn_sk(sk)->sobject);

149 150 151
	mutex_lock(&pnsocks.lock);
	sk_add_node_rcu(sk, hlist);
	mutex_unlock(&pnsocks.lock);
R
Remi Denis-Courmont 已提交
152 153 154 155 156
}
EXPORT_SYMBOL(pn_sock_hash);

void pn_sock_unhash(struct sock *sk)
{
157 158 159
	mutex_lock(&pnsocks.lock);
	sk_del_node_init_rcu(sk);
	mutex_unlock(&pnsocks.lock);
160
	pn_sock_unbind_all_res(sk);
161
	synchronize_rcu();
R
Remi Denis-Courmont 已提交
162 163 164
}
EXPORT_SYMBOL(pn_sock_unhash);

165 166
static DEFINE_MUTEX(port_mutex);

R
Remi Denis-Courmont 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
static int pn_socket_bind(struct socket *sock, struct sockaddr *addr, int len)
{
	struct sock *sk = sock->sk;
	struct pn_sock *pn = pn_sk(sk);
	struct sockaddr_pn *spn = (struct sockaddr_pn *)addr;
	int err;
	u16 handle;
	u8 saddr;

	if (sk->sk_prot->bind)
		return sk->sk_prot->bind(sk, addr, len);

	if (len < sizeof(struct sockaddr_pn))
		return -EINVAL;
	if (spn->spn_family != AF_PHONET)
		return -EAFNOSUPPORT;

	handle = pn_sockaddr_get_object((struct sockaddr_pn *)addr);
	saddr = pn_addr(handle);
186
	if (saddr && phonet_address_lookup(sock_net(sk), saddr))
R
Remi Denis-Courmont 已提交
187 188 189 190 191 192 193
		return -EADDRNOTAVAIL;

	lock_sock(sk);
	if (sk->sk_state != TCP_CLOSE || pn_port(pn->sobject)) {
		err = -EINVAL; /* attempt to rebind */
		goto out;
	}
194 195
	WARN_ON(sk_hashed(sk));
	mutex_lock(&port_mutex);
R
Remi Denis-Courmont 已提交
196 197
	err = sk->sk_prot->get_port(sk, pn_port(handle));
	if (err)
198
		goto out_port;
R
Remi Denis-Courmont 已提交
199 200 201 202 203 204 205

	/* get_port() sets the port, bind() sets the address if applicable */
	pn->sobject = pn_object(saddr, pn_port(pn->sobject));
	pn->resource = spn->spn_resource;

	/* Enable RX on the socket */
	sk->sk_prot->hash(sk);
206 207
out_port:
	mutex_unlock(&port_mutex);
R
Remi Denis-Courmont 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
out:
	release_sock(sk);
	return err;
}

static int pn_socket_autobind(struct socket *sock)
{
	struct sockaddr_pn sa;
	int err;

	memset(&sa, 0, sizeof(sa));
	sa.spn_family = AF_PHONET;
	err = pn_socket_bind(sock, (struct sockaddr *)&sa,
				sizeof(struct sockaddr_pn));
	if (err != -EINVAL)
		return err;
	BUG_ON(!pn_port(pn_sk(sock->sk)->sobject));
	return 0; /* socket was already bound */
}

228 229 230 231
static int pn_socket_connect(struct socket *sock, struct sockaddr *addr,
		int len, int flags)
{
	struct sock *sk = sock->sk;
232
	struct pn_sock *pn = pn_sk(sk);
233
	struct sockaddr_pn *spn = (struct sockaddr_pn *)addr;
234 235
	struct task_struct *tsk = current;
	long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
236 237
	int err;

238 239
	if (pn_socket_autobind(sock))
		return -ENOBUFS;
240 241 242 243 244 245 246 247 248
	if (len < sizeof(struct sockaddr_pn))
		return -EINVAL;
	if (spn->spn_family != AF_PHONET)
		return -EAFNOSUPPORT;

	lock_sock(sk);

	switch (sock->state) {
	case SS_UNCONNECTED:
249
		if (sk->sk_state != TCP_CLOSE) {
250 251 252 253
			err = -EISCONN;
			goto out;
		}
		break;
254 255 256 257 258 259
	case SS_CONNECTING:
		err = -EALREADY;
		goto out;
	default:
		err = -EISCONN;
		goto out;
260 261
	}

262 263
	pn->dobject = pn_sockaddr_get_object(spn);
	pn->resource = pn_sockaddr_get_resource(spn);
264
	sock->state = SS_CONNECTING;
265

266
	err = sk->sk_prot->connect(sk, addr, len);
267
	if (err) {
268
		sock->state = SS_UNCONNECTED;
269
		pn->dobject = 0;
270 271 272
		goto out;
	}

273 274
	while (sk->sk_state == TCP_SYN_SENT) {
		DEFINE_WAIT(wait);
275

276 277 278 279 280 281 282 283
		if (!timeo) {
			err = -EINPROGRESS;
			goto out;
		}
		if (signal_pending(tsk)) {
			err = sock_intr_errno(timeo);
			goto out;
		}
284

285 286 287 288 289 290
		prepare_to_wait_exclusive(sk_sleep(sk), &wait,
						TASK_INTERRUPTIBLE);
		release_sock(sk);
		timeo = schedule_timeout(timeo);
		lock_sock(sk);
		finish_wait(sk_sleep(sk), &wait);
291 292
	}

293 294 295 296 297 298 299
	if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED))
		err = 0;
	else if (sk->sk_state == TCP_CLOSE_WAIT)
		err = -ECONNRESET;
	else
		err = -ECONNREFUSED;
	sock->state = err ? SS_UNCONNECTED : SS_CONNECTED;
300 301 302 303 304
out:
	release_sock(sk);
	return err;
}

305 306 307 308 309 310 311
static int pn_socket_accept(struct socket *sock, struct socket *newsock,
				int flags)
{
	struct sock *sk = sock->sk;
	struct sock *newsk;
	int err;

312 313 314
	if (unlikely(sk->sk_state != TCP_LISTEN))
		return -EINVAL;

315 316 317 318 319 320 321 322 323 324 325
	newsk = sk->sk_prot->accept(sk, flags, &err);
	if (!newsk)
		return err;

	lock_sock(newsk);
	sock_graft(newsk, newsock);
	newsock->state = SS_CONNECTED;
	release_sock(newsk);
	return 0;
}

R
Remi Denis-Courmont 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
static int pn_socket_getname(struct socket *sock, struct sockaddr *addr,
				int *sockaddr_len, int peer)
{
	struct sock *sk = sock->sk;
	struct pn_sock *pn = pn_sk(sk);

	memset(addr, 0, sizeof(struct sockaddr_pn));
	addr->sa_family = AF_PHONET;
	if (!peer) /* Race with bind() here is userland's problem. */
		pn_sockaddr_set_object((struct sockaddr_pn *)addr,
					pn->sobject);

	*sockaddr_len = sizeof(struct sockaddr_pn);
	return 0;
}

342 343 344 345 346 347 348
static unsigned int pn_socket_poll(struct file *file, struct socket *sock,
					poll_table *wait)
{
	struct sock *sk = sock->sk;
	struct pep_sock *pn = pep_sk(sk);
	unsigned int mask = 0;

349
	poll_wait(file, sk_sleep(sk), wait);
350

351
	if (sk->sk_state == TCP_CLOSE)
352 353 354
		return POLLERR;
	if (!skb_queue_empty(&sk->sk_receive_queue))
		mask |= POLLIN | POLLRDNORM;
355 356 357
	if (!skb_queue_empty(&pn->ctrlreq_queue))
		mask |= POLLPRI;
	if (!mask && sk->sk_state == TCP_CLOSE_WAIT)
358 359
		return POLLHUP;

360 361 362
	if (sk->sk_state == TCP_ESTABLISHED &&
		atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf &&
		atomic_read(&pn->tx_credits))
363 364 365 366 367
		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;

	return mask;
}

R
Remi Denis-Courmont 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
static int pn_socket_ioctl(struct socket *sock, unsigned int cmd,
				unsigned long arg)
{
	struct sock *sk = sock->sk;
	struct pn_sock *pn = pn_sk(sk);

	if (cmd == SIOCPNGETOBJECT) {
		struct net_device *dev;
		u16 handle;
		u8 saddr;

		if (get_user(handle, (__u16 __user *)arg))
			return -EFAULT;

		lock_sock(sk);
		if (sk->sk_bound_dev_if)
			dev = dev_get_by_index(sock_net(sk),
						sk->sk_bound_dev_if);
		else
			dev = phonet_device_get(sock_net(sk));
		if (dev && (dev->flags & IFF_UP))
			saddr = phonet_address_get(dev, pn_addr(handle));
		else
			saddr = PN_NO_ADDR;
		release_sock(sk);

		if (dev)
			dev_put(dev);
		if (saddr == PN_NO_ADDR)
			return -EHOSTUNREACH;

		handle = pn_object(saddr, pn_port(pn->sobject));
		return put_user(handle, (__u16 __user *)arg);
	}

	return sk->sk_prot->ioctl(sk, cmd, arg);
}

406 407 408 409 410 411 412 413 414
static int pn_socket_listen(struct socket *sock, int backlog)
{
	struct sock *sk = sock->sk;
	int err = 0;

	if (pn_socket_autobind(sock))
		return -ENOBUFS;

	lock_sock(sk);
415
	if (sock->state != SS_UNCONNECTED) {
416 417 418 419
		err = -EINVAL;
		goto out;
	}

420 421 422 423
	if (sk->sk_state != TCP_LISTEN) {
		sk->sk_state = TCP_LISTEN;
		sk->sk_ack_backlog = 0;
	}
424 425 426 427 428 429
	sk->sk_max_ack_backlog = backlog;
out:
	release_sock(sk);
	return err;
}

R
Remi Denis-Courmont 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
static int pn_socket_sendmsg(struct kiocb *iocb, struct socket *sock,
				struct msghdr *m, size_t total_len)
{
	struct sock *sk = sock->sk;

	if (pn_socket_autobind(sock))
		return -EAGAIN;

	return sk->sk_prot->sendmsg(iocb, sk, m, total_len);
}

const struct proto_ops phonet_dgram_ops = {
	.family		= AF_PHONET,
	.owner		= THIS_MODULE,
	.release	= pn_socket_release,
	.bind		= pn_socket_bind,
	.connect	= sock_no_connect,
	.socketpair	= sock_no_socketpair,
	.accept		= sock_no_accept,
	.getname	= pn_socket_getname,
	.poll		= datagram_poll,
	.ioctl		= pn_socket_ioctl,
	.listen		= sock_no_listen,
	.shutdown	= sock_no_shutdown,
	.setsockopt	= sock_no_setsockopt,
	.getsockopt	= sock_no_getsockopt,
#ifdef CONFIG_COMPAT
	.compat_setsockopt = sock_no_setsockopt,
	.compat_getsockopt = sock_no_getsockopt,
#endif
	.sendmsg	= pn_socket_sendmsg,
	.recvmsg	= sock_common_recvmsg,
	.mmap		= sock_no_mmap,
	.sendpage	= sock_no_sendpage,
};

466 467 468 469 470
const struct proto_ops phonet_stream_ops = {
	.family		= AF_PHONET,
	.owner		= THIS_MODULE,
	.release	= pn_socket_release,
	.bind		= pn_socket_bind,
471
	.connect	= pn_socket_connect,
472 473 474 475 476 477 478
	.socketpair	= sock_no_socketpair,
	.accept		= pn_socket_accept,
	.getname	= pn_socket_getname,
	.poll		= pn_socket_poll,
	.ioctl		= pn_socket_ioctl,
	.listen		= pn_socket_listen,
	.shutdown	= sock_no_shutdown,
479 480
	.setsockopt	= sock_common_setsockopt,
	.getsockopt	= sock_common_getsockopt,
481
#ifdef CONFIG_COMPAT
482 483
	.compat_setsockopt = compat_sock_common_setsockopt,
	.compat_getsockopt = compat_sock_common_getsockopt,
484 485 486 487 488 489 490 491
#endif
	.sendmsg	= pn_socket_sendmsg,
	.recvmsg	= sock_common_recvmsg,
	.mmap		= sock_no_mmap,
	.sendpage	= sock_no_sendpage,
};
EXPORT_SYMBOL(phonet_stream_ops);

R
Remi Denis-Courmont 已提交
492 493 494 495
/* allocate port for a socket */
int pn_sock_get_port(struct sock *sk, unsigned short sport)
{
	static int port_cur;
496
	struct net *net = sock_net(sk);
R
Remi Denis-Courmont 已提交
497 498 499 500 501 502
	struct pn_sock *pn = pn_sk(sk);
	struct sockaddr_pn try_sa;
	struct sock *tmpsk;

	memset(&try_sa, 0, sizeof(struct sockaddr_pn));
	try_sa.spn_family = AF_PHONET;
503
	WARN_ON(!mutex_is_locked(&port_mutex));
R
Remi Denis-Courmont 已提交
504 505
	if (!sport) {
		/* search free port */
506
		int port, pmin, pmax;
R
Remi Denis-Courmont 已提交
507

508
		phonet_get_local_port_range(&pmin, &pmax);
R
Remi Denis-Courmont 已提交
509 510 511 512 513 514
		for (port = pmin; port <= pmax; port++) {
			port_cur++;
			if (port_cur < pmin || port_cur > pmax)
				port_cur = pmin;

			pn_sockaddr_set_port(&try_sa, port_cur);
515
			tmpsk = pn_find_sock_by_sa(net, &try_sa);
R
Remi Denis-Courmont 已提交
516 517 518 519 520 521 522 523 524
			if (tmpsk == NULL) {
				sport = port_cur;
				goto found;
			} else
				sock_put(tmpsk);
		}
	} else {
		/* try to find specific port */
		pn_sockaddr_set_port(&try_sa, sport);
525
		tmpsk = pn_find_sock_by_sa(net, &try_sa);
R
Remi Denis-Courmont 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539
		if (tmpsk == NULL)
			/* No sock there! We can use that port... */
			goto found;
		else
			sock_put(tmpsk);
	}
	/* the port must be in use already */
	return -EADDRINUSE;

found:
	pn->sobject = pn_object(pn_addr(pn->sobject), sport);
	return 0;
}
EXPORT_SYMBOL(pn_sock_get_port);
540

541
#ifdef CONFIG_PROC_FS
542 543 544
static struct sock *pn_sock_get_idx(struct seq_file *seq, loff_t pos)
{
	struct net *net = seq_file_net(seq);
545
	struct hlist_head *hlist = pnsocks.hlist;
546 547
	struct hlist_node *node;
	struct sock *sknode;
548
	unsigned int h;
549

550
	for (h = 0; h < PN_HASHSIZE; h++) {
551
		sk_for_each_rcu(sknode, node, hlist) {
552 553 554 555 556 557 558
			if (!net_eq(net, sock_net(sknode)))
				continue;
			if (!pos)
				return sknode;
			pos--;
		}
		hlist++;
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
	}
	return NULL;
}

static struct sock *pn_sock_get_next(struct seq_file *seq, struct sock *sk)
{
	struct net *net = seq_file_net(seq);

	do
		sk = sk_next(sk);
	while (sk && !net_eq(net, sock_net(sk)));

	return sk;
}

static void *pn_sock_seq_start(struct seq_file *seq, loff_t *pos)
575
	__acquires(rcu)
576
{
577
	rcu_read_lock();
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
	return *pos ? pn_sock_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
}

static void *pn_sock_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
	struct sock *sk;

	if (v == SEQ_START_TOKEN)
		sk = pn_sock_get_idx(seq, 0);
	else
		sk = pn_sock_get_next(seq, v);
	(*pos)++;
	return sk;
}

static void pn_sock_seq_stop(struct seq_file *seq, void *v)
594
	__releases(rcu)
595
{
596
	rcu_read_unlock();
597 598 599 600 601 602 603 604 605 606 607 608 609 610
}

static int pn_sock_seq_show(struct seq_file *seq, void *v)
{
	int len;

	if (v == SEQ_START_TOKEN)
		seq_printf(seq, "%s%n", "pt  loc  rem rs st tx_queue rx_queue "
			"  uid inode ref pointer drops", &len);
	else {
		struct sock *sk = v;
		struct pn_sock *pn = pn_sk(sk);

		seq_printf(seq, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu "
D
Dan Rosenberg 已提交
611
			"%d %pK %d%n",
612 613
			sk->sk_protocol, pn->sobject, pn->dobject,
			pn->resource, sk->sk_state,
614
			sk_wmem_alloc_get(sk), sk_rmem_alloc_get(sk),
615 616
			from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
			sock_i_ino(sk),
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
			atomic_read(&sk->sk_refcnt), sk,
			atomic_read(&sk->sk_drops), &len);
	}
	seq_printf(seq, "%*s\n", 127 - len, "");
	return 0;
}

static const struct seq_operations pn_sock_seq_ops = {
	.start = pn_sock_seq_start,
	.next = pn_sock_seq_next,
	.stop = pn_sock_seq_stop,
	.show = pn_sock_seq_show,
};

static int pn_sock_open(struct inode *inode, struct file *file)
{
633 634
	return seq_open_net(inode, file, &pn_sock_seq_ops,
				sizeof(struct seq_net_private));
635 636 637 638 639 640 641
}

const struct file_operations pn_sock_seq_fops = {
	.owner = THIS_MODULE,
	.open = pn_sock_open,
	.read = seq_read,
	.llseek = seq_lseek,
642
	.release = seq_release_net,
643
};
644
#endif
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683

static struct  {
	struct sock *sk[256];
} pnres;

/*
 * Find and hold socket based on resource.
 */
struct sock *pn_find_sock_by_res(struct net *net, u8 res)
{
	struct sock *sk;

	if (!net_eq(net, &init_net))
		return NULL;

	rcu_read_lock();
	sk = rcu_dereference(pnres.sk[res]);
	if (sk)
		sock_hold(sk);
	rcu_read_unlock();
	return sk;
}

static DEFINE_MUTEX(resource_mutex);

int pn_sock_bind_res(struct sock *sk, u8 res)
{
	int ret = -EADDRINUSE;

	if (!net_eq(sock_net(sk), &init_net))
		return -ENOIOCTLCMD;
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;
	if (pn_socket_autobind(sk->sk_socket))
		return -EAGAIN;

	mutex_lock(&resource_mutex);
	if (pnres.sk[res] == NULL) {
		sock_hold(sk);
684
		rcu_assign_pointer(pnres.sk[res], sk);
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
		ret = 0;
	}
	mutex_unlock(&resource_mutex);
	return ret;
}

int pn_sock_unbind_res(struct sock *sk, u8 res)
{
	int ret = -ENOENT;

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	mutex_lock(&resource_mutex);
	if (pnres.sk[res] == sk) {
700
		RCU_INIT_POINTER(pnres.sk[res], NULL);
701 702 703 704 705 706 707 708 709 710 711 712 713
		ret = 0;
	}
	mutex_unlock(&resource_mutex);

	if (ret == 0) {
		synchronize_rcu();
		sock_put(sk);
	}
	return ret;
}

void pn_sock_unbind_all_res(struct sock *sk)
{
714
	unsigned int res, match = 0;
715 716 717 718

	mutex_lock(&resource_mutex);
	for (res = 0; res < 256; res++) {
		if (pnres.sk[res] == sk) {
719
			RCU_INIT_POINTER(pnres.sk[res], NULL);
720 721 722 723 724 725
			match++;
		}
	}
	mutex_unlock(&resource_mutex);

	while (match > 0) {
726
		__sock_put(sk);
727 728
		match--;
	}
729
	/* Caller is responsible for RCU sync before final sock_put() */
730
}
731 732 733 734 735

#ifdef CONFIG_PROC_FS
static struct sock **pn_res_get_idx(struct seq_file *seq, loff_t pos)
{
	struct net *net = seq_file_net(seq);
736
	unsigned int i;
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753

	if (!net_eq(net, &init_net))
		return NULL;

	for (i = 0; i < 256; i++) {
		if (pnres.sk[i] == NULL)
			continue;
		if (!pos)
			return pnres.sk + i;
		pos--;
	}
	return NULL;
}

static struct sock **pn_res_get_next(struct seq_file *seq, struct sock **sk)
{
	struct net *net = seq_file_net(seq);
754
	unsigned int i;
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799

	BUG_ON(!net_eq(net, &init_net));

	for (i = (sk - pnres.sk) + 1; i < 256; i++)
		if (pnres.sk[i])
			return pnres.sk + i;
	return NULL;
}

static void *pn_res_seq_start(struct seq_file *seq, loff_t *pos)
	__acquires(resource_mutex)
{
	mutex_lock(&resource_mutex);
	return *pos ? pn_res_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
}

static void *pn_res_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
	struct sock **sk;

	if (v == SEQ_START_TOKEN)
		sk = pn_res_get_idx(seq, 0);
	else
		sk = pn_res_get_next(seq, v);
	(*pos)++;
	return sk;
}

static void pn_res_seq_stop(struct seq_file *seq, void *v)
	__releases(resource_mutex)
{
	mutex_unlock(&resource_mutex);
}

static int pn_res_seq_show(struct seq_file *seq, void *v)
{
	int len;

	if (v == SEQ_START_TOKEN)
		seq_printf(seq, "%s%n", "rs   uid inode", &len);
	else {
		struct sock **psk = v;
		struct sock *sk = *psk;

		seq_printf(seq, "%02X %5d %lu%n",
800 801
			   (int) (psk - pnres.sk),
			   from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
D
David S. Miller 已提交
802
			   sock_i_ino(sk), &len);
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
	}
	seq_printf(seq, "%*s\n", 63 - len, "");
	return 0;
}

static const struct seq_operations pn_res_seq_ops = {
	.start = pn_res_seq_start,
	.next = pn_res_seq_next,
	.stop = pn_res_seq_stop,
	.show = pn_res_seq_show,
};

static int pn_res_open(struct inode *inode, struct file *file)
{
	return seq_open_net(inode, file, &pn_res_seq_ops,
				sizeof(struct seq_net_private));
}

const struct file_operations pn_res_seq_fops = {
	.owner = THIS_MODULE,
	.open = pn_res_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = seq_release_net,
};
#endif