caif_socket.c 26.4 KB
Newer Older
1 2
/*
 * Copyright (C) ST-Ericsson AB 2010
3
 * Author:	Sjur Brendeland
4 5 6
 * License terms: GNU General Public License (GPL) version 2
 */

J
Joe Perches 已提交
7 8
#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__

9 10 11 12 13 14 15 16 17 18 19
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <linux/mutex.h>
#include <linux/list.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/tcp.h>
#include <linux/uaccess.h>
20
#include <linux/debugfs.h>
21
#include <linux/caif/caif_socket.h>
22
#include <linux/pkt_sched.h>
23 24
#include <net/sock.h>
#include <net/tcp_states.h>
25 26 27 28 29
#include <net/caif/caif_layer.h>
#include <net/caif/caif_dev.h>
#include <net/caif/cfpkt.h>

MODULE_LICENSE("GPL");
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
MODULE_ALIAS_NETPROTO(AF_CAIF);

/*
 * CAIF state is re-using the TCP socket states.
 * caif_states stored in sk_state reflect the state as reported by
 * the CAIF stack, while sk_socket->state is the state of the socket.
 */
enum caif_states {
	CAIF_CONNECTED		= TCP_ESTABLISHED,
	CAIF_CONNECTING	= TCP_SYN_SENT,
	CAIF_DISCONNECTED	= TCP_CLOSE
};

#define TX_FLOW_ON_BIT	1
#define RX_FLOW_ON_BIT	2
45 46

struct caifsock {
47
	struct sock sk; /* must be first member */
48 49 50
	struct cflayer layer;
	u32 flow_state;
	struct caif_connect_request conn_req;
51
	struct mutex readlock;
52
	struct dentry *debugfs_socket_dir;
53
	int headroom, tailroom, maxframe;
54 55
};

56 57 58 59 60 61 62 63 64 65 66
static int rx_flow_is_on(struct caifsock *cf_sk)
{
	return test_bit(RX_FLOW_ON_BIT,
			(void *) &cf_sk->flow_state);
}

static int tx_flow_is_on(struct caifsock *cf_sk)
{
	return test_bit(TX_FLOW_ON_BIT,
			(void *) &cf_sk->flow_state);
}
67

68
static void set_rx_flow_off(struct caifsock *cf_sk)
69
{
70 71 72
	 clear_bit(RX_FLOW_ON_BIT,
		 (void *) &cf_sk->flow_state);
}
73

74 75 76 77 78
static void set_rx_flow_on(struct caifsock *cf_sk)
{
	 set_bit(RX_FLOW_ON_BIT,
			(void *) &cf_sk->flow_state);
}
79

80 81 82 83 84
static void set_tx_flow_off(struct caifsock *cf_sk)
{
	 clear_bit(TX_FLOW_ON_BIT,
		(void *) &cf_sk->flow_state);
}
85

86 87 88 89 90
static void set_tx_flow_on(struct caifsock *cf_sk)
{
	 set_bit(TX_FLOW_ON_BIT,
		(void *) &cf_sk->flow_state);
}
91

92 93 94 95 96 97
static void caif_read_lock(struct sock *sk)
{
	struct caifsock *cf_sk;
	cf_sk = container_of(sk, struct caifsock, sk);
	mutex_lock(&cf_sk->readlock);
}
98

99 100 101 102 103 104
static void caif_read_unlock(struct sock *sk)
{
	struct caifsock *cf_sk;
	cf_sk = container_of(sk, struct caifsock, sk);
	mutex_unlock(&cf_sk->readlock);
}
105

106
static int sk_rcvbuf_lowwater(struct caifsock *cf_sk)
107 108 109 110
{
	/* A quarter of full buffer is used a low water mark */
	return cf_sk->sk.sk_rcvbuf / 4;
}
111

112
static void caif_flow_ctrl(struct sock *sk, int mode)
113 114 115
{
	struct caifsock *cf_sk;
	cf_sk = container_of(sk, struct caifsock, sk);
116
	if (cf_sk->layer.dn && cf_sk->layer.dn->modemcmd)
117 118
		cf_sk->layer.dn->modemcmd(cf_sk->layer.dn, mode);
}
119

120 121 122 123
/*
 * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
 * not dropped, but CAIF is sending flow off instead.
 */
124
static void caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
125 126 127 128 129
{
	int err;
	unsigned long flags;
	struct sk_buff_head *list = &sk->sk_receive_queue;
	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
130
	bool queued = false;
131

132
	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
133
		(unsigned int)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) {
134 135 136
		net_dbg_ratelimited("sending flow OFF (queue len = %d %d)\n",
				    atomic_read(&cf_sk->sk.sk_rmem_alloc),
				    sk_rcvbuf_lowwater(cf_sk));
137
		set_rx_flow_off(cf_sk);
138
		caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
139
	}
140

141 142
	err = sk_filter(sk, skb);
	if (err)
143 144
		goto out;

145
	if (!sk_rmem_schedule(sk, skb, skb->truesize) && rx_flow_is_on(cf_sk)) {
146
		set_rx_flow_off(cf_sk);
147
		net_dbg_ratelimited("sending flow OFF due to rmem_schedule\n");
148
		caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
149 150 151 152
	}
	skb->dev = NULL;
	skb_set_owner_r(skb, sk);
	spin_lock_irqsave(&list->lock, flags);
153 154
	queued = !sock_flag(sk, SOCK_DEAD);
	if (queued)
155 156
		__skb_queue_tail(list, skb);
	spin_unlock_irqrestore(&list->lock, flags);
157 158
out:
	if (queued)
159
		sk->sk_data_ready(sk);
160 161
	else
		kfree_skb(skb);
162 163
}

164 165
/* Packet Receive Callback function called from CAIF Stack */
static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt)
166 167
{
	struct caifsock *cf_sk;
168
	struct sk_buff *skb;
169 170

	cf_sk = container_of(layr, struct caifsock, layer);
171 172 173
	skb = cfpkt_tonative(pkt);

	if (unlikely(cf_sk->sk.sk_state != CAIF_CONNECTED)) {
174
		kfree_skb(skb);
175 176 177 178 179
		return 0;
	}
	caif_queue_rcv_skb(&cf_sk->sk, skb);
	return 0;
}
180

181 182 183 184 185 186 187 188 189 190 191 192
static void cfsk_hold(struct cflayer *layr)
{
	struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
	sock_hold(&cf_sk->sk);
}

static void cfsk_put(struct cflayer *layr)
{
	struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
	sock_put(&cf_sk->sk);
}

193 194
/* Packet Control Callback function called from CAIF */
static void caif_ctrl_cb(struct cflayer *layr,
195 196
			 enum caif_ctrlcmd flow,
			 int phyid)
197 198
{
	struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
199 200
	switch (flow) {
	case CAIF_CTRLCMD_FLOW_ON_IND:
201 202 203
		/* OK from modem to start sending again */
		set_tx_flow_on(cf_sk);
		cf_sk->sk.sk_state_change(&cf_sk->sk);
204 205 206
		break;

	case CAIF_CTRLCMD_FLOW_OFF_IND:
207 208 209
		/* Modem asks us to shut up */
		set_tx_flow_off(cf_sk);
		cf_sk->sk.sk_state_change(&cf_sk->sk);
210 211 212
		break;

	case CAIF_CTRLCMD_INIT_RSP:
213
		/* We're now connected */
214 215
		caif_client_register_refcnt(&cf_sk->layer,
						cfsk_hold, cfsk_put);
216 217
		cf_sk->sk.sk_state = CAIF_CONNECTED;
		set_tx_flow_on(cf_sk);
218
		cf_sk->sk.sk_shutdown = 0;
219
		cf_sk->sk.sk_state_change(&cf_sk->sk);
220 221 222
		break;

	case CAIF_CTRLCMD_DEINIT_RSP:
223 224 225
		/* We're now disconnected */
		cf_sk->sk.sk_state = CAIF_DISCONNECTED;
		cf_sk->sk.sk_state_change(&cf_sk->sk);
226 227 228
		break;

	case CAIF_CTRLCMD_INIT_FAIL_RSP:
229 230 231 232 233 234 235 236 237 238
		/* Connect request failed */
		cf_sk->sk.sk_err = ECONNREFUSED;
		cf_sk->sk.sk_state = CAIF_DISCONNECTED;
		cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
		/*
		 * Socket "standards" seems to require POLLOUT to
		 * be set at connect failure.
		 */
		set_tx_flow_on(cf_sk);
		cf_sk->sk.sk_state_change(&cf_sk->sk);
239 240 241
		break;

	case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
242 243 244 245 246
		/* Modem has closed this connection, or device is down. */
		cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
		cf_sk->sk.sk_err = ECONNRESET;
		set_rx_flow_on(cf_sk);
		cf_sk->sk.sk_error_report(&cf_sk->sk);
247 248 249
		break;

	default:
J
Joe Perches 已提交
250
		pr_debug("Unexpected flow command %d\n", flow);
251 252 253
	}
}

254
static void caif_check_flow_release(struct sock *sk)
255
{
256
	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
257

258 259
	if (rx_flow_is_on(cf_sk))
		return;
260

261 262
	if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) {
			set_rx_flow_on(cf_sk);
263
			caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_ON_REQ);
264 265
	}
}
266

267
/*
268 269
 * Copied from unix_dgram_recvmsg, but removed credit checks,
 * changed locking, address handling and added MSG_TRUNC.
270
 */
271 272
static int caif_seqpkt_recvmsg(struct socket *sock, struct msghdr *m,
			       size_t len, int flags)
273 274 275 276

{
	struct sock *sk = sock->sk;
	struct sk_buff *skb;
277 278
	int ret;
	int copylen;
279

280
	ret = -EOPNOTSUPP;
281
	if (flags & MSG_OOB)
282
		goto read_error;
283

284 285
	skb = skb_recv_datagram(sk, flags, 0 , &ret);
	if (!skb)
286
		goto read_error;
287 288 289 290
	copylen = skb->len;
	if (len < copylen) {
		m->msg_flags |= MSG_TRUNC;
		copylen = len;
291 292
	}

293
	ret = skb_copy_datagram_msg(skb, 0, m, copylen);
294
	if (ret)
295
		goto out_free;
296

297 298
	ret = (flags & MSG_TRUNC) ? skb->len : copylen;
out_free:
299 300
	skb_free_datagram(sk, skb);
	caif_check_flow_release(sk);
301
	return ret;
302

303 304 305
read_error:
	return ret;
}
306 307


308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
/* Copied from unix_stream_wait_data, identical except for lock call. */
static long caif_stream_data_wait(struct sock *sk, long timeo)
{
	DEFINE_WAIT(wait);
	lock_sock(sk);

	for (;;) {
		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);

		if (!skb_queue_empty(&sk->sk_receive_queue) ||
			sk->sk_err ||
			sk->sk_state != CAIF_CONNECTED ||
			sock_flag(sk, SOCK_DEAD) ||
			(sk->sk_shutdown & RCV_SHUTDOWN) ||
			signal_pending(current) ||
			!timeo)
			break;
325

326 327 328 329
		set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
		release_sock(sk);
		timeo = schedule_timeout(timeo);
		lock_sock(sk);
330 331 332 333

		if (sock_flag(sk, SOCK_DEAD))
			break;

334
		clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
335 336
	}

337 338 339 340
	finish_wait(sk_sleep(sk), &wait);
	release_sock(sk);
	return timeo;
}
341 342


343 344 345 346
/*
 * Copied from unix_stream_recvmsg, but removed credit checks,
 * changed locking calls, changed address handling.
 */
347 348
static int caif_stream_recvmsg(struct socket *sock, struct msghdr *msg,
			       size_t size, int flags)
349 350 351 352 353 354
{
	struct sock *sk = sock->sk;
	int copied = 0;
	int target;
	int err = 0;
	long timeo;
355

356 357 358
	err = -EOPNOTSUPP;
	if (flags&MSG_OOB)
		goto out;
359

360 361 362 363 364 365 366
	/*
	 * Lock the socket to prevent queue disordering
	 * while sleeps in memcpy_tomsg
	 */
	err = -EAGAIN;
	if (sk->sk_state == CAIF_CONNECTING)
		goto out;
367

368 369 370
	caif_read_lock(sk);
	target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
	timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
371

372 373 374
	do {
		int chunk;
		struct sk_buff *skb;
375

376
		lock_sock(sk);
377 378 379 380
		if (sock_flag(sk, SOCK_DEAD)) {
			err = -ECONNRESET;
			goto unlock;
		}
381 382
		skb = skb_dequeue(&sk->sk_receive_queue);
		caif_check_flow_release(sk);
383

384 385 386 387 388 389 390 391 392 393 394 395
		if (skb == NULL) {
			if (copied >= target)
				goto unlock;
			/*
			 *	POSIX 1003.1g mandates this order.
			 */
			err = sock_error(sk);
			if (err)
				goto unlock;
			err = -ECONNRESET;
			if (sk->sk_shutdown & RCV_SHUTDOWN)
				goto unlock;
396

397 398 399 400 401
			err = -EPIPE;
			if (sk->sk_state != CAIF_CONNECTED)
				goto unlock;
			if (sock_flag(sk, SOCK_DEAD))
				goto unlock;
402

403
			release_sock(sk);
404

405 406 407
			err = -EAGAIN;
			if (!timeo)
				break;
408

409
			caif_read_unlock(sk);
410

411
			timeo = caif_stream_data_wait(sk, timeo);
412

413 414 415 416 417 418 419 420 421
			if (signal_pending(current)) {
				err = sock_intr_errno(timeo);
				goto out;
			}
			caif_read_lock(sk);
			continue;
unlock:
			release_sock(sk);
			break;
422
		}
423 424
		release_sock(sk);
		chunk = min_t(unsigned int, skb->len, size);
A
Al Viro 已提交
425
		if (memcpy_to_msg(msg, skb->data, chunk)) {
426 427 428 429 430 431 432
			skb_queue_head(&sk->sk_receive_queue, skb);
			if (copied == 0)
				copied = -EFAULT;
			break;
		}
		copied += chunk;
		size -= chunk;
433

434 435 436
		/* Mark read part of skb as used */
		if (!(flags & MSG_PEEK)) {
			skb_pull(skb, chunk);
437

438 439 440 441 442 443
			/* put the skb back if we didn't use it up. */
			if (skb->len) {
				skb_queue_head(&sk->sk_receive_queue, skb);
				break;
			}
			kfree_skb(skb);
444 445

		} else {
446 447 448 449 450 451
			/*
			 * It is questionable, see note in unix_dgram_recvmsg.
			 */
			/* put message back and return */
			skb_queue_head(&sk->sk_receive_queue, skb);
			break;
452
		}
453 454
	} while (size);
	caif_read_unlock(sk);
455

456 457 458
out:
	return copied ? : err;
}
459

460 461 462 463 464
/*
 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
 * CAIF flow-on and sock_writable.
 */
static long caif_wait_for_flow_on(struct caifsock *cf_sk,
465
				  int wait_writeable, long timeo, int *err)
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
{
	struct sock *sk = &cf_sk->sk;
	DEFINE_WAIT(wait);
	for (;;) {
		*err = 0;
		if (tx_flow_is_on(cf_sk) &&
			(!wait_writeable || sock_writeable(&cf_sk->sk)))
			break;
		*err = -ETIMEDOUT;
		if (!timeo)
			break;
		*err = -ERESTARTSYS;
		if (signal_pending(current))
			break;
		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
		*err = -ECONNRESET;
		if (sk->sk_shutdown & SHUTDOWN_MASK)
			break;
		*err = -sk->sk_err;
		if (sk->sk_err)
			break;
		*err = -EPIPE;
		if (cf_sk->sk.sk_state != CAIF_CONNECTED)
			break;
		timeo = schedule_timeout(timeo);
491
	}
492 493 494
	finish_wait(sk_sleep(sk), &wait);
	return timeo;
}
495

496 497 498 499 500 501 502 503
/*
 * Transmit a SKB. The device may temporarily request re-transmission
 * by returning EAGAIN.
 */
static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
			int noblock, long timeo)
{
	struct cfpkt *pkt;
504

505
	pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
506
	memset(skb->cb, 0, sizeof(struct caif_payload_info));
507
	cfpkt_set_prio(pkt, cf_sk->sk.sk_priority);
508

509 510
	if (cf_sk->layer.dn == NULL) {
		kfree_skb(skb);
511
		return -EINVAL;
512
	}
513

514
	return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
515
}
516

517
/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
518 519
static int caif_seqpkt_sendmsg(struct socket *sock, struct msghdr *msg,
			       size_t len)
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
{
	struct sock *sk = sock->sk;
	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
	int buffer_size;
	int ret = 0;
	struct sk_buff *skb = NULL;
	int noblock;
	long timeo;
	caif_assert(cf_sk);
	ret = sock_error(sk);
	if (ret)
		goto err;

	ret = -EOPNOTSUPP;
	if (msg->msg_flags&MSG_OOB)
		goto err;

	ret = -EOPNOTSUPP;
	if (msg->msg_namelen)
		goto err;

	ret = -EINVAL;
A
Al Viro 已提交
542
	if (unlikely(msg->msg_iter.iov->iov_base == NULL))
543 544 545 546 547 548 549
		goto err;
	noblock = msg->msg_flags & MSG_DONTWAIT;

	timeo = sock_sndtimeo(sk, noblock);
	timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
				1, timeo, &ret);

550 551
	if (ret)
		goto err;
552 553 554 555 556 557
	ret = -EPIPE;
	if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
		sock_flag(sk, SOCK_DEAD) ||
		(sk->sk_shutdown & RCV_SHUTDOWN))
		goto err;

558 559 560 561 562 563 564
	/* Error if trying to write more than maximum frame size. */
	ret = -EMSGSIZE;
	if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM)
		goto err;

	buffer_size = len + cf_sk->headroom + cf_sk->tailroom;

565 566
	ret = -ENOMEM;
	skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
567 568

	if (!skb || skb_tailroom(skb) < buffer_size)
569
		goto err;
570 571

	skb_reserve(skb, cf_sk->headroom);
572

A
Al Viro 已提交
573
	ret = memcpy_from_msg(skb_put(skb, len), msg, len);
574 575 576 577 578

	if (ret)
		goto err;
	ret = transmit_skb(skb, cf_sk, noblock, timeo);
	if (ret < 0)
579 580 581
		/* skb is already freed */
		return ret;

582 583 584 585 586
	return len;
err:
	kfree_skb(skb);
	return ret;
}
587

588 589 590 591 592
/*
 * Copied from unix_stream_sendmsg and adapted to CAIF:
 * Changed removed permission handling and added waiting for flow on
 * and other minor adaptations.
 */
593 594
static int caif_stream_sendmsg(struct socket *sock, struct msghdr *msg,
			       size_t len)
595 596 597 598 599 600 601
{
	struct sock *sk = sock->sk;
	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
	int err, size;
	struct sk_buff *skb;
	int sent = 0;
	long timeo;
602

603 604 605
	err = -EOPNOTSUPP;
	if (unlikely(msg->msg_flags&MSG_OOB))
		goto out_err;
606

607 608
	if (unlikely(msg->msg_namelen))
		goto out_err;
609

610 611
	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
	timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
612

613 614
	if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
		goto pipe_err;
615

616
	while (sent < len) {
617

618
		size = len-sent;
619

620 621
		if (size > cf_sk->maxframe)
			size = cf_sk->maxframe;
622

623 624 625
		/* If size is more than half of sndbuf, chop up message */
		if (size > ((sk->sk_sndbuf >> 1) - 64))
			size = (sk->sk_sndbuf >> 1) - 64;
626

627 628
		if (size > SKB_MAX_ALLOC)
			size = SKB_MAX_ALLOC;
629

630
		skb = sock_alloc_send_skb(sk,
631 632
					size + cf_sk->headroom +
					cf_sk->tailroom,
633 634 635 636
					msg->msg_flags&MSG_DONTWAIT,
					&err);
		if (skb == NULL)
			goto out_err;
637

638
		skb_reserve(skb, cf_sk->headroom);
639 640 641 642 643 644 645 646
		/*
		 *	If you pass two values to the sock_alloc_send_skb
		 *	it tries to grab the large buffer with GFP_NOFS
		 *	(which can fail easily), and if it fails grab the
		 *	fallback size buffer which is under a page and will
		 *	succeed. [Alan]
		 */
		size = min_t(int, size, skb_tailroom(skb));
647

A
Al Viro 已提交
648
		err = memcpy_from_msg(skb_put(skb, size), msg, size);
649 650 651 652 653 654
		if (err) {
			kfree_skb(skb);
			goto out_err;
		}
		err = transmit_skb(skb, cf_sk,
				msg->msg_flags&MSG_DONTWAIT, timeo);
655 656
		if (err < 0)
			/* skb is already freed */
657
			goto pipe_err;
658

659
		sent += size;
660 661
	}

662
	return sent;
663

664 665 666 667 668 669
pipe_err:
	if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
		send_sig(SIGPIPE, current, 0);
	err = -EPIPE;
out_err:
	return sent ? : err;
670 671 672
}

static int setsockopt(struct socket *sock,
673
		      int lvl, int opt, char __user *ov, unsigned int ol)
674 675 676
{
	struct sock *sk = sock->sk;
	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
677
	int linksel;
678

679
	if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
680
		return -ENOPROTOOPT;
681

682 683
	switch (opt) {
	case CAIFSO_LINK_SELECT:
684
		if (ol < sizeof(int))
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
			return -EINVAL;
		if (lvl != SOL_CAIF)
			goto bad_sol;
		if (copy_from_user(&linksel, ov, sizeof(int)))
			return -EINVAL;
		lock_sock(&(cf_sk->sk));
		cf_sk->conn_req.link_selector = linksel;
		release_sock(&cf_sk->sk);
		return 0;

	case CAIFSO_REQ_PARAM:
		if (lvl != SOL_CAIF)
			goto bad_sol;
		if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
			return -ENOPROTOOPT;
		lock_sock(&(cf_sk->sk));
701 702
		if (ol > sizeof(cf_sk->conn_req.param.data) ||
			copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) {
703 704 705
			release_sock(&cf_sk->sk);
			return -EINVAL;
		}
706
		cf_sk->conn_req.param.size = ol;
707 708 709 710
		release_sock(&cf_sk->sk);
		return 0;

	default:
711
		return -ENOPROTOOPT;
712 713 714 715 716 717 718 719
	}

	return 0;
bad_sol:
	return -ENOPROTOOPT;

}

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
/*
 * caif_connect() - Connect a CAIF Socket
 * Copied and modified af_irda.c:irda_connect().
 *
 * Note : by consulting "errno", the user space caller may learn the cause
 * of the failure. Most of them are visible in the function, others may come
 * from subroutines called and are listed here :
 *  o -EAFNOSUPPORT: bad socket family or type.
 *  o -ESOCKTNOSUPPORT: bad socket type or protocol
 *  o -EINVAL: bad socket address, or CAIF link type
 *  o -ECONNREFUSED: remote end refused the connection.
 *  o -EINPROGRESS: connect request sent but timed out (or non-blocking)
 *  o -EISCONN: already connected.
 *  o -ETIMEDOUT: Connection timed out (send timeout)
 *  o -ENODEV: No link layer to send request
 *  o -ECONNRESET: Received Shutdown indication or lost link layer
 *  o -ENOMEM: Out of memory
 *
 *  State Strategy:
 *  o sk_state: holds the CAIF_* protocol state, it's updated by
 *	caif_ctrl_cb.
 *  o sock->state: holds the SS_* socket state and is updated by connect and
 *	disconnect.
 */
static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
			int addr_len, int flags)
746 747
{
	struct sock *sk = sock->sk;
748 749 750
	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
	long timeo;
	int err;
751
	int ifindex, headroom, tailroom;
E
Eric Dumazet 已提交
752
	unsigned int mtu;
753 754
	struct net_device *dev;

755
	lock_sock(sk);
756

757 758
	err = -EAFNOSUPPORT;
	if (uaddr->sa_family != AF_CAIF)
759 760
		goto out;

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
	switch (sock->state) {
	case SS_UNCONNECTED:
		/* Normal case, a fresh connect */
		caif_assert(sk->sk_state == CAIF_DISCONNECTED);
		break;
	case SS_CONNECTING:
		switch (sk->sk_state) {
		case CAIF_CONNECTED:
			sock->state = SS_CONNECTED;
			err = -EISCONN;
			goto out;
		case CAIF_DISCONNECTED:
			/* Reconnect allowed */
			break;
		case CAIF_CONNECTING:
			err = -EALREADY;
			if (flags & O_NONBLOCK)
				goto out;
			goto wait_connect;
		}
		break;
	case SS_CONNECTED:
		caif_assert(sk->sk_state == CAIF_CONNECTED ||
				sk->sk_state == CAIF_DISCONNECTED);
		if (sk->sk_shutdown & SHUTDOWN_MASK) {
			/* Allow re-connect after SHUTDOWN_IND */
787
			caif_disconnect_client(sock_net(sk), &cf_sk->layer);
788
			caif_free_client(&cf_sk->layer);
789 790 791 792 793 794 795 796 797
			break;
		}
		/* No reconnect on a seqpacket socket */
		err = -EISCONN;
		goto out;
	case SS_DISCONNECTING:
	case SS_FREE:
		caif_assert(1); /*Should never happen */
		break;
798
	}
799 800 801
	sk->sk_state = CAIF_DISCONNECTED;
	sock->state = SS_UNCONNECTED;
	sk_stream_kill_queues(&cf_sk->sk);
802

803
	err = -EINVAL;
804
	if (addr_len != sizeof(struct sockaddr_caif))
805
		goto out;
806

807
	memcpy(&cf_sk->conn_req.sockaddr, uaddr,
808 809
		sizeof(struct sockaddr_caif));

810 811 812 813
	/* Move to connecting socket, start sending Connect Requests */
	sock->state = SS_CONNECTING;
	sk->sk_state = CAIF_CONNECTING;

814
	/* Check priority value comming from socket */
815 816 817 818 819 820 821 822 823 824 825
	/* if priority value is out of range it will be ajusted */
	if (cf_sk->sk.sk_priority > CAIF_PRIO_MAX)
		cf_sk->conn_req.priority = CAIF_PRIO_MAX;
	else if (cf_sk->sk.sk_priority < CAIF_PRIO_MIN)
		cf_sk->conn_req.priority = CAIF_PRIO_MIN;
	else
		cf_sk->conn_req.priority = cf_sk->sk.sk_priority;

	/*ifindex = id of the interface.*/
	cf_sk->conn_req.ifindex = cf_sk->sk.sk_bound_dev_if;

826
	cf_sk->layer.receive = caif_sktrecv_cb;
827

828
	err = caif_connect_client(sock_net(sk), &cf_sk->conn_req,
829
				&cf_sk->layer, &ifindex, &headroom, &tailroom);
830

831 832 833 834
	if (err < 0) {
		cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
		cf_sk->sk.sk_state = CAIF_DISCONNECTED;
		goto out;
835
	}
E
Eric Dumazet 已提交
836 837 838 839 840 841 842 843

	err = -ENODEV;
	rcu_read_lock();
	dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
	if (!dev) {
		rcu_read_unlock();
		goto out;
	}
844
	cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
E
Eric Dumazet 已提交
845 846 847
	mtu = dev->mtu;
	rcu_read_unlock();

848
	cf_sk->tailroom = tailroom;
E
Eric Dumazet 已提交
849
	cf_sk->maxframe = mtu - (headroom + tailroom);
850
	if (cf_sk->maxframe < 1) {
J
Joe Perches 已提交
851
		pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
852
		err = -ENODEV;
853 854
		goto out;
	}
855

856 857
	err = -EINPROGRESS;
wait_connect:
858

859 860
	if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
		goto out;
861

862 863 864
	timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);

	release_sock(sk);
865 866
	err = -ERESTARTSYS;
	timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
867 868 869
			sk->sk_state != CAIF_CONNECTING,
			timeo);
	lock_sock(sk);
870
	if (timeo < 0)
871
		goto out; /* -ERESTARTSYS */
872

873 874 875
	err = -ETIMEDOUT;
	if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
		goto out;
876 877 878 879 880 881 882 883 884
	if (sk->sk_state != CAIF_CONNECTED) {
		sock->state = SS_UNCONNECTED;
		err = sock_error(sk);
		if (!err)
			err = -ECONNREFUSED;
		goto out;
	}
	sock->state = SS_CONNECTED;
	err = 0;
885
out:
886 887
	release_sock(sk);
	return err;
888 889
}

890 891 892 893 894
/*
 * caif_release() - Disconnect a CAIF Socket
 * Copied and modified af_irda.c:irda_release().
 */
static int caif_release(struct socket *sock)
895 896
{
	struct sock *sk = sock->sk;
897
	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
898

899 900
	if (!sk)
		return 0;
901

902
	set_tx_flow_off(cf_sk);
903 904

	/*
905 906 907
	 * Ensure that packets are not queued after this point in time.
	 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
	 * this ensures no packets when sock is dead.
908
	 */
909
	spin_lock_bh(&sk->sk_receive_queue.lock);
910
	sock_set_flag(sk, SOCK_DEAD);
911
	spin_unlock_bh(&sk->sk_receive_queue.lock);
912
	sock->sk = NULL;
913

914
	WARN_ON(IS_ERR(cf_sk->debugfs_socket_dir));
915
	debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
916 917

	lock_sock(&(cf_sk->sk));
918 919
	sk->sk_state = CAIF_DISCONNECTED;
	sk->sk_shutdown = SHUTDOWN_MASK;
920

921
	caif_disconnect_client(sock_net(sk), &cf_sk->layer);
922 923
	cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
	wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP);
924

925 926 927 928
	sock_orphan(sk);
	sk_stream_kill_queues(&cf_sk->sk);
	release_sock(sk);
	sock_put(sk);
929
	return 0;
930
}
931

932 933
/* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
static unsigned int caif_poll(struct file *file,
934
			      struct socket *sock, poll_table *wait)
935 936 937 938
{
	struct sock *sk = sock->sk;
	unsigned int mask;
	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
939

940 941
	sock_poll_wait(file, sk_sleep(sk), wait);
	mask = 0;
942

943 944 945 946 947 948 949
	/* exceptional events? */
	if (sk->sk_err)
		mask |= POLLERR;
	if (sk->sk_shutdown == SHUTDOWN_MASK)
		mask |= POLLHUP;
	if (sk->sk_shutdown & RCV_SHUTDOWN)
		mask |= POLLRDHUP;
950

951 952 953 954
	/* readable? */
	if (!skb_queue_empty(&sk->sk_receive_queue) ||
		(sk->sk_shutdown & RCV_SHUTDOWN))
		mask |= POLLIN | POLLRDNORM;
955 956

	/*
957 958
	 * we set writable also when the other side has shut down the
	 * connection. This prevents stuck sockets.
959
	 */
960 961 962 963
	if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;

	return mask;
964 965
}

966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
static const struct proto_ops caif_seqpacket_ops = {
	.family = PF_CAIF,
	.owner = THIS_MODULE,
	.release = caif_release,
	.bind = sock_no_bind,
	.connect = caif_connect,
	.socketpair = sock_no_socketpair,
	.accept = sock_no_accept,
	.getname = sock_no_getname,
	.poll = caif_poll,
	.ioctl = sock_no_ioctl,
	.listen = sock_no_listen,
	.shutdown = sock_no_shutdown,
	.setsockopt = setsockopt,
	.getsockopt = sock_no_getsockopt,
	.sendmsg = caif_seqpkt_sendmsg,
	.recvmsg = caif_seqpkt_recvmsg,
	.mmap = sock_no_mmap,
	.sendpage = sock_no_sendpage,
};

static const struct proto_ops caif_stream_ops = {
988 989 990 991 992 993 994 995 996 997 998
	.family = PF_CAIF,
	.owner = THIS_MODULE,
	.release = caif_release,
	.bind = sock_no_bind,
	.connect = caif_connect,
	.socketpair = sock_no_socketpair,
	.accept = sock_no_accept,
	.getname = sock_no_getname,
	.poll = caif_poll,
	.ioctl = sock_no_ioctl,
	.listen = sock_no_listen,
999
	.shutdown = sock_no_shutdown,
1000 1001
	.setsockopt = setsockopt,
	.getsockopt = sock_no_getsockopt,
1002 1003
	.sendmsg = caif_stream_sendmsg,
	.recvmsg = caif_stream_recvmsg,
1004
	.mmap = sock_no_mmap,
1005
	.sendpage = sock_no_sendpage,
1006 1007 1008 1009 1010
};

/* This function is called when a socket is finally destroyed. */
static void caif_sock_destructor(struct sock *sk)
{
1011
	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
1012 1013 1014 1015
	caif_assert(!atomic_read(&sk->sk_wmem_alloc));
	caif_assert(sk_unhashed(sk));
	caif_assert(!sk->sk_socket);
	if (!sock_flag(sk, SOCK_DEAD)) {
1016
		pr_debug("Attempt to release alive CAIF socket: %p\n", sk);
1017 1018
		return;
	}
1019
	sk_stream_kill_queues(&cf_sk->sk);
1020
	caif_free_client(&cf_sk->layer);
1021 1022 1023
}

static int caif_create(struct net *net, struct socket *sock, int protocol,
1024
		       int kern)
1025 1026 1027 1028 1029 1030 1031 1032
{
	struct sock *sk = NULL;
	struct caifsock *cf_sk = NULL;
	static struct proto prot = {.name = "PF_CAIF",
		.owner = THIS_MODULE,
		.obj_size = sizeof(struct caifsock),
	};

1033 1034
	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
		return -EPERM;
1035 1036
	/*
	 * The sock->type specifies the socket type to use.
1037 1038 1039
	 * The CAIF socket is a packet stream in the sense
	 * that it is packet based. CAIF trusts the reliability
	 * of the link, no resending is implemented.
1040
	 */
1041 1042 1043 1044 1045
	if (sock->type == SOCK_SEQPACKET)
		sock->ops = &caif_seqpacket_ops;
	else if (sock->type == SOCK_STREAM)
		sock->ops = &caif_stream_ops;
	else
1046 1047 1048 1049 1050
		return -ESOCKTNOSUPPORT;

	if (protocol < 0 || protocol >= CAIFPROTO_MAX)
		return -EPROTONOSUPPORT;
	/*
1051 1052
	 * Set the socket state to unconnected.	 The socket state
	 * is really not used at all in the net/core or socket.c but the
1053 1054
	 * initialization makes sure that sock->state is not uninitialized.
	 */
1055
	sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot, kern);
1056 1057 1058 1059 1060 1061 1062 1063
	if (!sk)
		return -ENOMEM;

	cf_sk = container_of(sk, struct caifsock, sk);

	/* Store the protocol */
	sk->sk_protocol = (unsigned char) protocol;

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	/* Initialize default priority for well-known cases */
	switch (protocol) {
	case CAIFPROTO_AT:
		sk->sk_priority = TC_PRIO_CONTROL;
		break;
	case CAIFPROTO_RFM:
		sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
		break;
	default:
		sk->sk_priority = TC_PRIO_BESTEFFORT;
	}

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
	/*
	 * Lock in order to try to stop someone from opening the socket
	 * too early.
	 */
	lock_sock(&(cf_sk->sk));

	/* Initialize the nozero default sock structure data. */
	sock_init_data(sock, sk);
	sk->sk_destruct = caif_sock_destructor;

1086 1087 1088 1089
	mutex_init(&cf_sk->readlock); /* single task reading lock */
	cf_sk->layer.ctrlcmd = caif_ctrl_cb;
	cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
	cf_sk->sk.sk_state = CAIF_DISCONNECTED;
1090

1091 1092
	set_tx_flow_off(cf_sk);
	set_rx_flow_on(cf_sk);
1093 1094

	/* Set default options on configuration */
1095
	cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
1096 1097 1098 1099 1100
	cf_sk->conn_req.protocol = protocol;
	release_sock(&cf_sk->sk);
	return 0;
}

1101

1102 1103 1104 1105 1106 1107
static struct net_proto_family caif_family_ops = {
	.family = PF_CAIF,
	.create = caif_create,
	.owner = THIS_MODULE,
};

1108
static int __init caif_sktinit_module(void)
1109
{
1110
	int err = sock_register(&caif_family_ops);
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
	if (!err)
		return err;
	return 0;
}

static void __exit caif_sktexit_module(void)
{
	sock_unregister(PF_CAIF);
}
module_init(caif_sktinit_module);
module_exit(caif_sktexit_module);