datagram.c 18.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 *	SUCS NET3:
 *
 *	Generic datagram handling routines. These are generic for all
 *	protocols. Possibly a generic IP version on top of these would
 *	make sense. Not tonight however 8-).
 *	This is used because UDP, RAW, PACKET, DDP, IPX, AX.25 and
 *	NetROM layer all have identical poll code and mostly
 *	identical recvmsg() code. So we share it here. The poll was
 *	shared before but buried in udp.c so I moved it.
 *
12
 *	Authors:	Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old
L
Linus Torvalds 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 *						     udp.c code)
 *
 *	Fixes:
 *		Alan Cox	:	NULL return from skb_peek_copy()
 *					understood
 *		Alan Cox	:	Rewrote skb_read_datagram to avoid the
 *					skb_peek_copy stuff.
 *		Alan Cox	:	Added support for SOCK_SEQPACKET.
 *					IPX can no longer use the SO_TYPE hack
 *					but AX.25 now works right, and SPX is
 *					feasible.
 *		Alan Cox	:	Fixed write poll of non IP protocol
 *					crash.
 *		Florian  La Roche:	Changed for my new skbuff handling.
 *		Darryl Miles	:	Fixed non-blocking SOCK_SEQPACKET.
 *		Linus Torvalds	:	BSD semantic fixes.
 *		Alan Cox	:	Datagram iovec handling
 *		Darryl Miles	:	Fixed non-blocking SOCK_STREAM.
 *		Alan Cox	:	POSIXisms
 *		Pete Wyckoff    :       Unconnected accept() fix.
 *
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <asm/uaccess.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>
#include <linux/poll.h>
#include <linux/highmem.h>
49
#include <linux/spinlock.h>
50
#include <linux/slab.h>
L
Linus Torvalds 已提交
51 52 53 54

#include <net/protocol.h>
#include <linux/skbuff.h>

55 56 57
#include <net/checksum.h>
#include <net/sock.h>
#include <net/tcp_states.h>
58
#include <trace/events/skb.h>
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67

/*
 *	Is a socket 'connection oriented' ?
 */
static inline int connection_based(struct sock *sk)
{
	return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
}

68
static int receiver_wake_function(wait_queue_t *wait, unsigned int mode, int sync,
69 70 71 72 73 74 75 76 77 78 79
				  void *key)
{
	unsigned long bits = (unsigned long)key;

	/*
	 * Avoid a wakeup if event not interesting for us
	 */
	if (bits && !(bits & (POLLIN | POLLERR)))
		return 0;
	return autoremove_wake_function(wait, mode, sync, key);
}
L
Linus Torvalds 已提交
80 81 82 83 84 85
/*
 * Wait for a packet..
 */
static int wait_for_packet(struct sock *sk, int *err, long *timeo_p)
{
	int error;
86
	DEFINE_WAIT_FUNC(wait, receiver_wake_function);
L
Linus Torvalds 已提交
87

E
Eric Dumazet 已提交
88
	prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

	/* Socket errors? */
	error = sock_error(sk);
	if (error)
		goto out_err;

	if (!skb_queue_empty(&sk->sk_receive_queue))
		goto out;

	/* Socket shut down? */
	if (sk->sk_shutdown & RCV_SHUTDOWN)
		goto out_noerr;

	/* Sequenced packets can come disconnected.
	 * If so we report the problem
	 */
	error = -ENOTCONN;
	if (connection_based(sk) &&
	    !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
		goto out_err;

	/* handle signals */
	if (signal_pending(current))
		goto interrupted;

	error = 0;
	*timeo_p = schedule_timeout(*timeo_p);
out:
E
Eric Dumazet 已提交
117
	finish_wait(sk_sleep(sk), &wait);
L
Linus Torvalds 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130
	return error;
interrupted:
	error = sock_intr_errno(*timeo_p);
out_err:
	*err = error;
	goto out;
out_noerr:
	*err = 0;
	error = 1;
	goto out;
}

/**
131
 *	__skb_recv_datagram - Receive a datagram skbuff
132 133
 *	@sk: socket
 *	@flags: MSG_ flags
134 135
 *	@off: an offset in bytes to peek skb from. Returns an offset
 *	      within an skb where data actually starts
136
 *	@peeked: returns non-zero if this packet has been seen before
137
 *	@err: error code returned
L
Linus Torvalds 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
 *
 *	Get a datagram skbuff, understands the peeking, nonblocking wakeups
 *	and possible races. This replaces identical code in packet, raw and
 *	udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
 *	the long standing peek and read race for datagram sockets. If you
 *	alter this routine remember it must be re-entrant.
 *
 *	This function will lock the socket if a skb is returned, so the caller
 *	needs to unlock the socket in that case (usually by calling
 *	skb_free_datagram)
 *
 *	* It does not lock socket since today. This function is
 *	* free of race conditions. This measure should/can improve
 *	* significantly datagram socket latencies at high loads,
 *	* when data copying to user space takes lots of time.
 *	* (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
 *	*  8) Great win.)
 *	*			                    --ANK (980729)
 *
 *	The order of the tests when we find no data waiting are specified
 *	quite explicitly by POSIX 1003.1g, don't change them without having
 *	the standard around please.
 */
161
struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
162
				    int *peeked, int *off, int *err)
L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170 171 172 173
{
	struct sk_buff *skb;
	long timeo;
	/*
	 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
	 */
	int error = sock_error(sk);

	if (error)
		goto no_packet;

174
	timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
L
Linus Torvalds 已提交
175 176 177 178 179 180

	do {
		/* Again only user level code calls this function, so nothing
		 * interrupt level will suddenly eat the receive_queue.
		 *
		 * Look at current nfs client by the way...
181
		 * However, this function was correct in any case. 8)
L
Linus Torvalds 已提交
182
		 */
183
		unsigned long cpu_flags;
184
		struct sk_buff_head *queue = &sk->sk_receive_queue;
185

186
		spin_lock_irqsave(&queue->lock, cpu_flags);
187
		skb_queue_walk(queue, skb) {
188 189
			*peeked = skb->peeked;
			if (flags & MSG_PEEK) {
190 191
				if (*off >= skb->len && (skb->len || *off ||
							 skb->peeked)) {
192 193 194
					*off -= skb->len;
					continue;
				}
195
				skb->peeked = 1;
L
Linus Torvalds 已提交
196
				atomic_inc(&skb->users);
197
			} else
198
				__skb_unlink(skb, queue);
L
Linus Torvalds 已提交
199

200
			spin_unlock_irqrestore(&queue->lock, cpu_flags);
L
Linus Torvalds 已提交
201
			return skb;
202 203
		}
		spin_unlock_irqrestore(&queue->lock, cpu_flags);
L
Linus Torvalds 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217

		/* User doesn't want to wait */
		error = -EAGAIN;
		if (!timeo)
			goto no_packet;

	} while (!wait_for_packet(sk, err, &timeo));

	return NULL;

no_packet:
	*err = error;
	return NULL;
}
218 219
EXPORT_SYMBOL(__skb_recv_datagram);

220
struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
221 222
				  int noblock, int *err)
{
223
	int peeked, off = 0;
224 225

	return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
226
				   &peeked, &off, err);
227
}
E
Eric Dumazet 已提交
228
EXPORT_SYMBOL(skb_recv_datagram);
L
Linus Torvalds 已提交
229 230 231

void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
{
232
	consume_skb(skb);
233
	sk_mem_reclaim_partial(sk);
L
Linus Torvalds 已提交
234
}
235 236 237 238
EXPORT_SYMBOL(skb_free_datagram);

void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb)
{
239 240
	bool slow;

241 242 243 244 245
	if (likely(atomic_read(&skb->users) == 1))
		smp_rmb();
	else if (likely(!atomic_dec_and_test(&skb->users)))
		return;

246
	slow = lock_sock_fast(sk);
E
Eric Dumazet 已提交
247 248
	skb_orphan(skb);
	sk_mem_reclaim_partial(sk);
249
	unlock_sock_fast(sk, slow);
E
Eric Dumazet 已提交
250

251 252
	/* skb is now orphaned, can be freed outside of locked section */
	__kfree_skb(skb);
253 254
}
EXPORT_SYMBOL(skb_free_datagram_locked);
L
Linus Torvalds 已提交
255

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
/**
 *	skb_kill_datagram - Free a datagram skbuff forcibly
 *	@sk: socket
 *	@skb: datagram skbuff
 *	@flags: MSG_ flags
 *
 *	This function frees a datagram skbuff that was received by
 *	skb_recv_datagram.  The flags argument must match the one
 *	used for skb_recv_datagram.
 *
 *	If the MSG_PEEK flag is set, and the packet is still on the
 *	receive queue of the socket, it will be taken off the queue
 *	before it is freed.
 *
 *	This function currently only disables BH when acquiring the
 *	sk_receive_queue lock.  Therefore it must not be used in a
 *	context where that lock is acquired in an IRQ context.
273 274
 *
 *	It returns 0 if the packet was removed by us.
275 276
 */

277
int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
278
{
279 280
	int err = 0;

281
	if (flags & MSG_PEEK) {
282
		err = -ENOENT;
283 284 285 286
		spin_lock_bh(&sk->sk_receive_queue.lock);
		if (skb == skb_peek(&sk->sk_receive_queue)) {
			__skb_unlink(skb, &sk->sk_receive_queue);
			atomic_dec(&skb->users);
287
			err = 0;
288 289 290 291
		}
		spin_unlock_bh(&sk->sk_receive_queue.lock);
	}

292
	kfree_skb(skb);
E
Eric Dumazet 已提交
293
	atomic_inc(&sk->sk_drops);
294 295
	sk_mem_reclaim_partial(sk);

296
	return err;
297 298 299
}
EXPORT_SYMBOL(skb_kill_datagram);

L
Linus Torvalds 已提交
300 301
/**
 *	skb_copy_datagram_iovec - Copy a datagram to an iovec.
302 303
 *	@skb: buffer to copy
 *	@offset: offset in the buffer to start copying from
304
 *	@to: io vector to copy to
305
 *	@len: amount of data to copy from buffer to iovec
L
Linus Torvalds 已提交
306 307 308 309 310 311
 *
 *	Note: the iovec is modified during the copy.
 */
int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
			    struct iovec *to, int len)
{
312 313
	int start = skb_headlen(skb);
	int i, copy = start - offset;
314
	struct sk_buff *frag_iter;
315

316 317
	trace_skb_copy_datagram_iovec(skb, len);

318 319 320 321 322 323 324 325 326 327
	/* Copy header. */
	if (copy > 0) {
		if (copy > len)
			copy = len;
		if (memcpy_toiovec(to, skb->data + offset, copy))
			goto fault;
		if ((len -= copy) == 0)
			return 0;
		offset += copy;
	}
328

329 330
	/* Copy paged appendix. Hmm... why does this look so complicated? */
	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
331
		int end;
E
Eric Dumazet 已提交
332
		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
L
Linus Torvalds 已提交
333

334
		WARN_ON(start > offset + len);
335

E
Eric Dumazet 已提交
336
		end = start + skb_frag_size(frag);
337 338 339
		if ((copy = end - offset) > 0) {
			int err;
			u8  *vaddr;
340
			struct page *page = skb_frag_page(frag);
L
Linus Torvalds 已提交
341 342 343

			if (copy > len)
				copy = len;
344
			vaddr = kmap(page);
345 346
			err = memcpy_toiovec(to, vaddr + frag->page_offset +
					     offset - start, copy);
347
			kunmap(page);
L
Linus Torvalds 已提交
348 349 350 351 352 353
			if (err)
				goto fault;
			if (!(len -= copy))
				return 0;
			offset += copy;
		}
354
		start = end;
L
Linus Torvalds 已提交
355
	}
356

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
	skb_walk_frags(skb, frag_iter) {
		int end;

		WARN_ON(start > offset + len);

		end = start + frag_iter->len;
		if ((copy = end - offset) > 0) {
			if (copy > len)
				copy = len;
			if (skb_copy_datagram_iovec(frag_iter,
						    offset - start,
						    to, copy))
				goto fault;
			if ((len -= copy) == 0)
				return 0;
			offset += copy;
373
		}
374
		start = end;
L
Linus Torvalds 已提交
375
	}
376 377 378
	if (!len)
		return 0;

L
Linus Torvalds 已提交
379 380 381
fault:
	return -EFAULT;
}
E
Eric Dumazet 已提交
382
EXPORT_SYMBOL(skb_copy_datagram_iovec);
L
Linus Torvalds 已提交
383

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
/**
 *	skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
 *	@skb: buffer to copy
 *	@offset: offset in the buffer to start copying from
 *	@to: io vector to copy to
 *	@to_offset: offset in the io vector to start copying to
 *	@len: amount of data to copy from buffer to iovec
 *
 *	Returns 0 or -EFAULT.
 *	Note: the iovec is not modified during the copy.
 */
int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset,
				  const struct iovec *to, int to_offset,
				  int len)
{
	int start = skb_headlen(skb);
	int i, copy = start - offset;
401
	struct sk_buff *frag_iter;
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

	/* Copy header. */
	if (copy > 0) {
		if (copy > len)
			copy = len;
		if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy))
			goto fault;
		if ((len -= copy) == 0)
			return 0;
		offset += copy;
		to_offset += copy;
	}

	/* Copy paged appendix. Hmm... why does this look so complicated? */
	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
		int end;
E
Eric Dumazet 已提交
418
		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
419 420 421

		WARN_ON(start > offset + len);

E
Eric Dumazet 已提交
422
		end = start + skb_frag_size(frag);
423 424 425
		if ((copy = end - offset) > 0) {
			int err;
			u8  *vaddr;
426
			struct page *page = skb_frag_page(frag);
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

			if (copy > len)
				copy = len;
			vaddr = kmap(page);
			err = memcpy_toiovecend(to, vaddr + frag->page_offset +
						offset - start, to_offset, copy);
			kunmap(page);
			if (err)
				goto fault;
			if (!(len -= copy))
				return 0;
			offset += copy;
			to_offset += copy;
		}
		start = end;
	}

444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	skb_walk_frags(skb, frag_iter) {
		int end;

		WARN_ON(start > offset + len);

		end = start + frag_iter->len;
		if ((copy = end - offset) > 0) {
			if (copy > len)
				copy = len;
			if (skb_copy_datagram_const_iovec(frag_iter,
							  offset - start,
							  to, to_offset,
							  copy))
				goto fault;
			if ((len -= copy) == 0)
				return 0;
			offset += copy;
			to_offset += copy;
462
		}
463
		start = end;
464 465 466 467 468 469 470 471 472
	}
	if (!len)
		return 0;

fault:
	return -EFAULT;
}
EXPORT_SYMBOL(skb_copy_datagram_const_iovec);

473 474 475 476 477
/**
 *	skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
 *	@skb: buffer to copy
 *	@offset: offset in the buffer to start copying to
 *	@from: io vector to copy to
478
 *	@from_offset: offset in the io vector to start copying from
479 480 481
 *	@len: amount of data to copy to buffer from iovec
 *
 *	Returns 0 or -EFAULT.
482
 *	Note: the iovec is not modified during the copy.
483 484
 */
int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
485 486
				 const struct iovec *from, int from_offset,
				 int len)
487 488 489
{
	int start = skb_headlen(skb);
	int i, copy = start - offset;
490
	struct sk_buff *frag_iter;
491 492 493 494 495

	/* Copy header. */
	if (copy > 0) {
		if (copy > len)
			copy = len;
496 497
		if (memcpy_fromiovecend(skb->data + offset, from, from_offset,
					copy))
498 499 500 501
			goto fault;
		if ((len -= copy) == 0)
			return 0;
		offset += copy;
502
		from_offset += copy;
503 504 505 506 507
	}

	/* Copy paged appendix. Hmm... why does this look so complicated? */
	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
		int end;
E
Eric Dumazet 已提交
508
		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
509 510 511

		WARN_ON(start > offset + len);

E
Eric Dumazet 已提交
512
		end = start + skb_frag_size(frag);
513 514 515
		if ((copy = end - offset) > 0) {
			int err;
			u8  *vaddr;
516
			struct page *page = skb_frag_page(frag);
517 518 519 520

			if (copy > len)
				copy = len;
			vaddr = kmap(page);
521 522 523
			err = memcpy_fromiovecend(vaddr + frag->page_offset +
						  offset - start,
						  from, from_offset, copy);
524 525 526 527 528 529 530
			kunmap(page);
			if (err)
				goto fault;

			if (!(len -= copy))
				return 0;
			offset += copy;
531
			from_offset += copy;
532 533 534 535
		}
		start = end;
	}

536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
	skb_walk_frags(skb, frag_iter) {
		int end;

		WARN_ON(start > offset + len);

		end = start + frag_iter->len;
		if ((copy = end - offset) > 0) {
			if (copy > len)
				copy = len;
			if (skb_copy_datagram_from_iovec(frag_iter,
							 offset - start,
							 from,
							 from_offset,
							 copy))
				goto fault;
			if ((len -= copy) == 0)
				return 0;
			offset += copy;
			from_offset += copy;
555
		}
556
		start = end;
557 558 559 560 561 562 563 564 565
	}
	if (!len)
		return 0;

fault:
	return -EFAULT;
}
EXPORT_SYMBOL(skb_copy_datagram_from_iovec);

L
Linus Torvalds 已提交
566 567
static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
				      u8 __user *to, int len,
568
				      __wsum *csump)
L
Linus Torvalds 已提交
569
{
570 571
	int start = skb_headlen(skb);
	int i, copy = start - offset;
572 573
	struct sk_buff *frag_iter;
	int pos = 0;
L
Linus Torvalds 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591

	/* Copy header. */
	if (copy > 0) {
		int err = 0;
		if (copy > len)
			copy = len;
		*csump = csum_and_copy_to_user(skb->data + offset, to, copy,
					       *csump, &err);
		if (err)
			goto fault;
		if ((len -= copy) == 0)
			return 0;
		offset += copy;
		to += copy;
		pos = copy;
	}

	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
592
		int end;
E
Eric Dumazet 已提交
593
		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
L
Linus Torvalds 已提交
594

595
		WARN_ON(start > offset + len);
596

E
Eric Dumazet 已提交
597
		end = start + skb_frag_size(frag);
L
Linus Torvalds 已提交
598
		if ((copy = end - offset) > 0) {
599
			__wsum csum2;
L
Linus Torvalds 已提交
600 601
			int err = 0;
			u8  *vaddr;
602
			struct page *page = skb_frag_page(frag);
L
Linus Torvalds 已提交
603 604 605 606 607

			if (copy > len)
				copy = len;
			vaddr = kmap(page);
			csum2 = csum_and_copy_to_user(vaddr +
608 609
							frag->page_offset +
							offset - start,
L
Linus Torvalds 已提交
610 611 612 613 614 615 616 617 618 619 620
						      to, copy, 0, &err);
			kunmap(page);
			if (err)
				goto fault;
			*csump = csum_block_add(*csump, csum2, pos);
			if (!(len -= copy))
				return 0;
			offset += copy;
			to += copy;
			pos += copy;
		}
621
		start = end;
L
Linus Torvalds 已提交
622 623
	}

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
	skb_walk_frags(skb, frag_iter) {
		int end;

		WARN_ON(start > offset + len);

		end = start + frag_iter->len;
		if ((copy = end - offset) > 0) {
			__wsum csum2 = 0;
			if (copy > len)
				copy = len;
			if (skb_copy_and_csum_datagram(frag_iter,
						       offset - start,
						       to, copy,
						       &csum2))
				goto fault;
			*csump = csum_block_add(*csump, csum2, pos);
			if ((len -= copy) == 0)
				return 0;
			offset += copy;
			to += copy;
			pos += copy;
L
Linus Torvalds 已提交
645
		}
646
		start = end;
L
Linus Torvalds 已提交
647 648 649 650 651 652 653 654
	}
	if (!len)
		return 0;

fault:
	return -EFAULT;
}

655
__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
656
{
657
	__sum16 sum;
658

659
	sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
660
	if (likely(!sum)) {
661
		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
662 663 664 665 666
			netdev_rx_csum_fault(skb->dev);
		skb->ip_summed = CHECKSUM_UNNECESSARY;
	}
	return sum;
}
667 668 669 670 671 672
EXPORT_SYMBOL(__skb_checksum_complete_head);

__sum16 __skb_checksum_complete(struct sk_buff *skb)
{
	return __skb_checksum_complete_head(skb, skb->len);
}
673 674
EXPORT_SYMBOL(__skb_checksum_complete);

L
Linus Torvalds 已提交
675 676
/**
 *	skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
677 678
 *	@skb: skbuff
 *	@hlen: hardware length
679
 *	@iov: io vector
680
 *
L
Linus Torvalds 已提交
681 682 683 684 685 686 687
 *	Caller _must_ check that skb will fit to this iovec.
 *
 *	Returns: 0       - success.
 *		 -EINVAL - checksum failure.
 *		 -EFAULT - fault during copy. Beware, in this case iovec
 *			   can be modified!
 */
688
int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
L
Linus Torvalds 已提交
689 690
				     int hlen, struct iovec *iov)
{
691
	__wsum csum;
L
Linus Torvalds 已提交
692 693
	int chunk = skb->len - hlen;

694 695 696
	if (!chunk)
		return 0;

L
Linus Torvalds 已提交
697 698 699 700 701 702 703
	/* Skip filled elements.
	 * Pretty silly, look at memcpy_toiovec, though 8)
	 */
	while (!iov->iov_len)
		iov++;

	if (iov->iov_len < chunk) {
704
		if (__skb_checksum_complete(skb))
L
Linus Torvalds 已提交
705 706 707 708 709 710 711 712
			goto csum_error;
		if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
			goto fault;
	} else {
		csum = csum_partial(skb->data, hlen, skb->csum);
		if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
					       chunk, &csum))
			goto fault;
713
		if (csum_fold(csum))
L
Linus Torvalds 已提交
714
			goto csum_error;
715
		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
716
			netdev_rx_csum_fault(skb->dev);
L
Linus Torvalds 已提交
717 718 719 720 721 722 723 724 725
		iov->iov_len -= chunk;
		iov->iov_base += chunk;
	}
	return 0;
csum_error:
	return -EINVAL;
fault:
	return -EFAULT;
}
E
Eric Dumazet 已提交
726
EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
L
Linus Torvalds 已提交
727 728 729

/**
 * 	datagram_poll - generic datagram poll
730 731 732
 *	@file: file struct
 *	@sock: socket
 *	@wait: poll table
L
Linus Torvalds 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
 *
 *	Datagram poll: Again totally generic. This also handles
 *	sequenced packet sockets providing the socket receive queue
 *	is only ever holding data ready to receive.
 *
 *	Note: when you _don't_ use this routine for this protocol,
 *	and you use a different write policy from sock_writeable()
 *	then please supply your own write_space callback.
 */
unsigned int datagram_poll(struct file *file, struct socket *sock,
			   poll_table *wait)
{
	struct sock *sk = sock->sk;
	unsigned int mask;

E
Eric Dumazet 已提交
748
	sock_poll_wait(file, sk_sleep(sk), wait);
L
Linus Torvalds 已提交
749 750 751 752 753
	mask = 0;

	/* exceptional events? */
	if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
		mask |= POLLERR;
754
	if (sk->sk_shutdown & RCV_SHUTDOWN)
E
Eric Dumazet 已提交
755
		mask |= POLLRDHUP | POLLIN | POLLRDNORM;
L
Linus Torvalds 已提交
756 757 758 759
	if (sk->sk_shutdown == SHUTDOWN_MASK)
		mask |= POLLHUP;

	/* readable? */
E
Eric Dumazet 已提交
760
	if (!skb_queue_empty(&sk->sk_receive_queue))
L
Linus Torvalds 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
		mask |= POLLIN | POLLRDNORM;

	/* Connection-based need to check for termination and startup */
	if (connection_based(sk)) {
		if (sk->sk_state == TCP_CLOSE)
			mask |= POLLHUP;
		/* connection hasn't started yet? */
		if (sk->sk_state == TCP_SYN_SENT)
			return mask;
	}

	/* writable? */
	if (sock_writeable(sk))
		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
	else
		set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);

	return mask;
}
EXPORT_SYMBOL(datagram_poll);