• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *	SUCS NET3:
3  *
4  *	Generic datagram handling routines. These are generic for all
5  *	protocols. Possibly a generic IP version on top of these would
6  *	make sense. Not tonight however 8-).
7  *	This is used because UDP, RAW, PACKET, DDP, IPX, AX.25 and
8  *	NetROM layer all have identical poll code and mostly
9  *	identical recvmsg() code. So we share it here. The poll was
10  *	shared before but buried in udp.c so I moved it.
11  *
12  *	Authors:	Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old
13  *						     udp.c code)
14  *
15  *	Fixes:
16  *		Alan Cox	:	NULL return from skb_peek_copy()
17  *					understood
18  *		Alan Cox	:	Rewrote skb_read_datagram to avoid the
19  *					skb_peek_copy stuff.
20  *		Alan Cox	:	Added support for SOCK_SEQPACKET.
21  *					IPX can no longer use the SO_TYPE hack
22  *					but AX.25 now works right, and SPX is
23  *					feasible.
24  *		Alan Cox	:	Fixed write poll of non IP protocol
25  *					crash.
26  *		Florian  La Roche:	Changed for my new skbuff handling.
27  *		Darryl Miles	:	Fixed non-blocking SOCK_SEQPACKET.
28  *		Linus Torvalds	:	BSD semantic fixes.
29  *		Alan Cox	:	Datagram iovec handling
30  *		Darryl Miles	:	Fixed non-blocking SOCK_STREAM.
31  *		Alan Cox	:	POSIXisms
32  *		Pete Wyckoff    :       Unconnected accept() fix.
33  *
34  */
35 
36 #include <linux/module.h>
37 #include <linux/types.h>
38 #include <linux/kernel.h>
39 #include <asm/uaccess.h>
40 #include <linux/mm.h>
41 #include <linux/interrupt.h>
42 #include <linux/errno.h>
43 #include <linux/sched.h>
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <linux/rtnetlink.h>
47 #include <linux/poll.h>
48 #include <linux/highmem.h>
49 #include <linux/spinlock.h>
50 #include <linux/slab.h>
51 #include <linux/pagemap.h>
52 #include <linux/uio.h>
53 
54 #include <net/protocol.h>
55 #include <linux/skbuff.h>
56 
57 #include <net/checksum.h>
58 #include <net/sock.h>
59 #include <net/tcp_states.h>
60 #include <trace/events/skb.h>
61 #include <net/busy_poll.h>
62 
63 /*
64  *	Is a socket 'connection oriented' ?
65  */
connection_based(struct sock * sk)66 static inline int connection_based(struct sock *sk)
67 {
68 	return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
69 }
70 
receiver_wake_function(wait_queue_t * wait,unsigned int mode,int sync,void * key)71 static int receiver_wake_function(wait_queue_t *wait, unsigned int mode, int sync,
72 				  void *key)
73 {
74 	unsigned long bits = (unsigned long)key;
75 
76 	/*
77 	 * Avoid a wakeup if event not interesting for us
78 	 */
79 	if (bits && !(bits & (POLLIN | POLLERR)))
80 		return 0;
81 	return autoremove_wake_function(wait, mode, sync, key);
82 }
83 /*
84  * Wait for the last received packet to be different from skb
85  */
__skb_wait_for_more_packets(struct sock * sk,int * err,long * timeo_p,const struct sk_buff * skb)86 int __skb_wait_for_more_packets(struct sock *sk, int *err, long *timeo_p,
87 				const struct sk_buff *skb)
88 {
89 	int error;
90 	DEFINE_WAIT_FUNC(wait, receiver_wake_function);
91 
92 	prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
93 
94 	/* Socket errors? */
95 	error = sock_error(sk);
96 	if (error)
97 		goto out_err;
98 
99 	if (sk->sk_receive_queue.prev != skb)
100 		goto out;
101 
102 	/* Socket shut down? */
103 	if (sk->sk_shutdown & RCV_SHUTDOWN)
104 		goto out_noerr;
105 
106 	/* Sequenced packets can come disconnected.
107 	 * If so we report the problem
108 	 */
109 	error = -ENOTCONN;
110 	if (connection_based(sk) &&
111 	    !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
112 		goto out_err;
113 
114 	/* handle signals */
115 	if (signal_pending(current))
116 		goto interrupted;
117 
118 	error = 0;
119 	*timeo_p = schedule_timeout(*timeo_p);
120 out:
121 	finish_wait(sk_sleep(sk), &wait);
122 	return error;
123 interrupted:
124 	error = sock_intr_errno(*timeo_p);
125 out_err:
126 	*err = error;
127 	goto out;
128 out_noerr:
129 	*err = 0;
130 	error = 1;
131 	goto out;
132 }
133 EXPORT_SYMBOL(__skb_wait_for_more_packets);
134 
skb_set_peeked(struct sk_buff * skb)135 static struct sk_buff *skb_set_peeked(struct sk_buff *skb)
136 {
137 	struct sk_buff *nskb;
138 
139 	if (skb->peeked)
140 		return skb;
141 
142 	/* We have to unshare an skb before modifying it. */
143 	if (!skb_shared(skb))
144 		goto done;
145 
146 	nskb = skb_clone(skb, GFP_ATOMIC);
147 	if (!nskb)
148 		return ERR_PTR(-ENOMEM);
149 
150 	skb->prev->next = nskb;
151 	skb->next->prev = nskb;
152 	nskb->prev = skb->prev;
153 	nskb->next = skb->next;
154 
155 	consume_skb(skb);
156 	skb = nskb;
157 
158 done:
159 	skb->peeked = 1;
160 
161 	return skb;
162 }
163 
164 /**
165  *	__skb_try_recv_datagram - Receive a datagram skbuff
166  *	@sk: socket
167  *	@flags: MSG_ flags
168  *	@peeked: returns non-zero if this packet has been seen before
169  *	@off: an offset in bytes to peek skb from. Returns an offset
170  *	      within an skb where data actually starts
171  *	@err: error code returned
172  *	@last: set to last peeked message to inform the wait function
173  *	       what to look for when peeking
174  *
175  *	Get a datagram skbuff, understands the peeking, nonblocking wakeups
176  *	and possible races. This replaces identical code in packet, raw and
177  *	udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
178  *	the long standing peek and read race for datagram sockets. If you
179  *	alter this routine remember it must be re-entrant.
180  *
181  *	This function will lock the socket if a skb is returned, so
182  *	the caller needs to unlock the socket in that case (usually by
183  *	calling skb_free_datagram). Returns NULL with *err set to
184  *	-EAGAIN if no data was available or to some other value if an
185  *	error was detected.
186  *
187  *	* It does not lock socket since today. This function is
188  *	* free of race conditions. This measure should/can improve
189  *	* significantly datagram socket latencies at high loads,
190  *	* when data copying to user space takes lots of time.
191  *	* (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
192  *	*  8) Great win.)
193  *	*			                    --ANK (980729)
194  *
195  *	The order of the tests when we find no data waiting are specified
196  *	quite explicitly by POSIX 1003.1g, don't change them without having
197  *	the standard around please.
198  */
__skb_try_recv_datagram(struct sock * sk,unsigned int flags,int * peeked,int * off,int * err,struct sk_buff ** last)199 struct sk_buff *__skb_try_recv_datagram(struct sock *sk, unsigned int flags,
200 					int *peeked, int *off, int *err,
201 					struct sk_buff **last)
202 {
203 	struct sk_buff_head *queue = &sk->sk_receive_queue;
204 	struct sk_buff *skb;
205 	unsigned long cpu_flags;
206 	/*
207 	 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
208 	 */
209 	int error = sock_error(sk);
210 
211 	if (error)
212 		goto no_packet;
213 
214 	do {
215 		/* Again only user level code calls this function, so nothing
216 		 * interrupt level will suddenly eat the receive_queue.
217 		 *
218 		 * Look at current nfs client by the way...
219 		 * However, this function was correct in any case. 8)
220 		 */
221 		int _off = *off;
222 
223 		*last = (struct sk_buff *)queue;
224 		spin_lock_irqsave(&queue->lock, cpu_flags);
225 		skb_queue_walk(queue, skb) {
226 			*last = skb;
227 			*peeked = skb->peeked;
228 			if (flags & MSG_PEEK) {
229 				if (_off >= skb->len && (skb->len || _off ||
230 							 skb->peeked)) {
231 					_off -= skb->len;
232 					continue;
233 				}
234 
235 				skb = skb_set_peeked(skb);
236 				error = PTR_ERR(skb);
237 				if (IS_ERR(skb)) {
238 					spin_unlock_irqrestore(&queue->lock,
239 							       cpu_flags);
240 					goto no_packet;
241 				}
242 
243 				atomic_inc(&skb->users);
244 			} else
245 				__skb_unlink(skb, queue);
246 
247 			spin_unlock_irqrestore(&queue->lock, cpu_flags);
248 			*off = _off;
249 			return skb;
250 		}
251 
252 		spin_unlock_irqrestore(&queue->lock, cpu_flags);
253 	} while (sk_can_busy_loop(sk) &&
254 		 sk_busy_loop(sk, flags & MSG_DONTWAIT));
255 
256 	error = -EAGAIN;
257 
258 no_packet:
259 	*err = error;
260 	return NULL;
261 }
262 EXPORT_SYMBOL(__skb_try_recv_datagram);
263 
__skb_recv_datagram(struct sock * sk,unsigned int flags,int * peeked,int * off,int * err)264 struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
265 				    int *peeked, int *off, int *err)
266 {
267 	struct sk_buff *skb, *last;
268 	long timeo;
269 
270 	timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
271 
272 	do {
273 		skb = __skb_try_recv_datagram(sk, flags, peeked, off, err,
274 					      &last);
275 		if (skb)
276 			return skb;
277 
278 		if (*err != -EAGAIN)
279 			break;
280 	} while (timeo &&
281 		!__skb_wait_for_more_packets(sk, err, &timeo, last));
282 
283 	return NULL;
284 }
285 EXPORT_SYMBOL(__skb_recv_datagram);
286 
skb_recv_datagram(struct sock * sk,unsigned int flags,int noblock,int * err)287 struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
288 				  int noblock, int *err)
289 {
290 	int peeked, off = 0;
291 
292 	return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
293 				   &peeked, &off, err);
294 }
295 EXPORT_SYMBOL(skb_recv_datagram);
296 
skb_free_datagram(struct sock * sk,struct sk_buff * skb)297 void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
298 {
299 	consume_skb(skb);
300 	sk_mem_reclaim_partial(sk);
301 }
302 EXPORT_SYMBOL(skb_free_datagram);
303 
__skb_free_datagram_locked(struct sock * sk,struct sk_buff * skb,int len)304 void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len)
305 {
306 	bool slow;
307 
308 	if (likely(atomic_read(&skb->users) == 1))
309 		smp_rmb();
310 	else if (likely(!atomic_dec_and_test(&skb->users))) {
311 		sk_peek_offset_bwd(sk, len);
312 		return;
313 	}
314 
315 	slow = lock_sock_fast(sk);
316 	sk_peek_offset_bwd(sk, len);
317 	skb_orphan(skb);
318 	sk_mem_reclaim_partial(sk);
319 	unlock_sock_fast(sk, slow);
320 
321 	/* skb is now orphaned, can be freed outside of locked section */
322 	__kfree_skb(skb);
323 }
324 EXPORT_SYMBOL(__skb_free_datagram_locked);
325 
326 /**
327  *	skb_kill_datagram - Free a datagram skbuff forcibly
328  *	@sk: socket
329  *	@skb: datagram skbuff
330  *	@flags: MSG_ flags
331  *
332  *	This function frees a datagram skbuff that was received by
333  *	skb_recv_datagram.  The flags argument must match the one
334  *	used for skb_recv_datagram.
335  *
336  *	If the MSG_PEEK flag is set, and the packet is still on the
337  *	receive queue of the socket, it will be taken off the queue
338  *	before it is freed.
339  *
340  *	This function currently only disables BH when acquiring the
341  *	sk_receive_queue lock.  Therefore it must not be used in a
342  *	context where that lock is acquired in an IRQ context.
343  *
344  *	It returns 0 if the packet was removed by us.
345  */
346 
skb_kill_datagram(struct sock * sk,struct sk_buff * skb,unsigned int flags)347 int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
348 {
349 	int err = 0;
350 
351 	if (flags & MSG_PEEK) {
352 		err = -ENOENT;
353 		spin_lock_bh(&sk->sk_receive_queue.lock);
354 		if (skb->next) {
355 			__skb_unlink(skb, &sk->sk_receive_queue);
356 			atomic_dec(&skb->users);
357 			err = 0;
358 		}
359 		spin_unlock_bh(&sk->sk_receive_queue.lock);
360 	}
361 
362 	kfree_skb(skb);
363 	atomic_inc(&sk->sk_drops);
364 	sk_mem_reclaim_partial(sk);
365 
366 	return err;
367 }
368 EXPORT_SYMBOL(skb_kill_datagram);
369 
370 /**
371  *	skb_copy_datagram_iter - Copy a datagram to an iovec iterator.
372  *	@skb: buffer to copy
373  *	@offset: offset in the buffer to start copying from
374  *	@to: iovec iterator to copy to
375  *	@len: amount of data to copy from buffer to iovec
376  */
skb_copy_datagram_iter(const struct sk_buff * skb,int offset,struct iov_iter * to,int len)377 int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
378 			   struct iov_iter *to, int len)
379 {
380 	int start = skb_headlen(skb);
381 	int i, copy = start - offset, start_off = offset, n;
382 	struct sk_buff *frag_iter;
383 
384 	trace_skb_copy_datagram_iovec(skb, len);
385 
386 	/* Copy header. */
387 	if (copy > 0) {
388 		if (copy > len)
389 			copy = len;
390 		n = copy_to_iter(skb->data + offset, copy, to);
391 		offset += n;
392 		if (n != copy)
393 			goto short_copy;
394 		if ((len -= copy) == 0)
395 			return 0;
396 	}
397 
398 	/* Copy paged appendix. Hmm... why does this look so complicated? */
399 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
400 		int end;
401 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
402 
403 		WARN_ON(start > offset + len);
404 
405 		end = start + skb_frag_size(frag);
406 		if ((copy = end - offset) > 0) {
407 			if (copy > len)
408 				copy = len;
409 			n = copy_page_to_iter(skb_frag_page(frag),
410 					      frag->page_offset + offset -
411 					      start, copy, to);
412 			offset += n;
413 			if (n != copy)
414 				goto short_copy;
415 			if (!(len -= copy))
416 				return 0;
417 		}
418 		start = end;
419 	}
420 
421 	skb_walk_frags(skb, frag_iter) {
422 		int end;
423 
424 		WARN_ON(start > offset + len);
425 
426 		end = start + frag_iter->len;
427 		if ((copy = end - offset) > 0) {
428 			if (copy > len)
429 				copy = len;
430 			if (skb_copy_datagram_iter(frag_iter, offset - start,
431 						   to, copy))
432 				goto fault;
433 			if ((len -= copy) == 0)
434 				return 0;
435 			offset += copy;
436 		}
437 		start = end;
438 	}
439 	if (!len)
440 		return 0;
441 
442 	/* This is not really a user copy fault, but rather someone
443 	 * gave us a bogus length on the skb.  We should probably
444 	 * print a warning here as it may indicate a kernel bug.
445 	 */
446 
447 fault:
448 	iov_iter_revert(to, offset - start_off);
449 	return -EFAULT;
450 
451 short_copy:
452 	if (iov_iter_count(to))
453 		goto fault;
454 
455 	return 0;
456 }
457 EXPORT_SYMBOL(skb_copy_datagram_iter);
458 
459 /**
460  *	skb_copy_datagram_from_iter - Copy a datagram from an iov_iter.
461  *	@skb: buffer to copy
462  *	@offset: offset in the buffer to start copying to
463  *	@from: the copy source
464  *	@len: amount of data to copy to buffer from iovec
465  *
466  *	Returns 0 or -EFAULT.
467  */
skb_copy_datagram_from_iter(struct sk_buff * skb,int offset,struct iov_iter * from,int len)468 int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
469 				 struct iov_iter *from,
470 				 int len)
471 {
472 	int start = skb_headlen(skb);
473 	int i, copy = start - offset;
474 	struct sk_buff *frag_iter;
475 
476 	/* Copy header. */
477 	if (copy > 0) {
478 		if (copy > len)
479 			copy = len;
480 		if (copy_from_iter(skb->data + offset, copy, from) != copy)
481 			goto fault;
482 		if ((len -= copy) == 0)
483 			return 0;
484 		offset += copy;
485 	}
486 
487 	/* Copy paged appendix. Hmm... why does this look so complicated? */
488 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
489 		int end;
490 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
491 
492 		WARN_ON(start > offset + len);
493 
494 		end = start + skb_frag_size(frag);
495 		if ((copy = end - offset) > 0) {
496 			size_t copied;
497 
498 			if (copy > len)
499 				copy = len;
500 			copied = copy_page_from_iter(skb_frag_page(frag),
501 					  frag->page_offset + offset - start,
502 					  copy, from);
503 			if (copied != copy)
504 				goto fault;
505 
506 			if (!(len -= copy))
507 				return 0;
508 			offset += copy;
509 		}
510 		start = end;
511 	}
512 
513 	skb_walk_frags(skb, frag_iter) {
514 		int end;
515 
516 		WARN_ON(start > offset + len);
517 
518 		end = start + frag_iter->len;
519 		if ((copy = end - offset) > 0) {
520 			if (copy > len)
521 				copy = len;
522 			if (skb_copy_datagram_from_iter(frag_iter,
523 							offset - start,
524 							from, copy))
525 				goto fault;
526 			if ((len -= copy) == 0)
527 				return 0;
528 			offset += copy;
529 		}
530 		start = end;
531 	}
532 	if (!len)
533 		return 0;
534 
535 fault:
536 	return -EFAULT;
537 }
538 EXPORT_SYMBOL(skb_copy_datagram_from_iter);
539 
540 /**
541  *	zerocopy_sg_from_iter - Build a zerocopy datagram from an iov_iter
542  *	@skb: buffer to copy
543  *	@from: the source to copy from
544  *
545  *	The function will first copy up to headlen, and then pin the userspace
546  *	pages and build frags through them.
547  *
548  *	Returns 0, -EFAULT or -EMSGSIZE.
549  */
zerocopy_sg_from_iter(struct sk_buff * skb,struct iov_iter * from)550 int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *from)
551 {
552 	int len = iov_iter_count(from);
553 	int copy = min_t(int, skb_headlen(skb), len);
554 	int frag = 0;
555 
556 	/* copy up to skb headlen */
557 	if (skb_copy_datagram_from_iter(skb, 0, from, copy))
558 		return -EFAULT;
559 
560 	while (iov_iter_count(from)) {
561 		struct page *pages[MAX_SKB_FRAGS];
562 		size_t start;
563 		ssize_t copied;
564 		unsigned long truesize;
565 		int n = 0;
566 
567 		if (frag == MAX_SKB_FRAGS)
568 			return -EMSGSIZE;
569 
570 		copied = iov_iter_get_pages(from, pages, ~0U,
571 					    MAX_SKB_FRAGS - frag, &start);
572 		if (copied < 0)
573 			return -EFAULT;
574 
575 		iov_iter_advance(from, copied);
576 
577 		truesize = PAGE_ALIGN(copied + start);
578 		skb->data_len += copied;
579 		skb->len += copied;
580 		skb->truesize += truesize;
581 		atomic_add(truesize, &skb->sk->sk_wmem_alloc);
582 		while (copied) {
583 			int size = min_t(int, copied, PAGE_SIZE - start);
584 			skb_fill_page_desc(skb, frag++, pages[n], start, size);
585 			start = 0;
586 			copied -= size;
587 			n++;
588 		}
589 	}
590 	return 0;
591 }
592 EXPORT_SYMBOL(zerocopy_sg_from_iter);
593 
skb_copy_and_csum_datagram(const struct sk_buff * skb,int offset,struct iov_iter * to,int len,__wsum * csump)594 static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
595 				      struct iov_iter *to, int len,
596 				      __wsum *csump)
597 {
598 	int start = skb_headlen(skb);
599 	int i, copy = start - offset, start_off = offset;
600 	struct sk_buff *frag_iter;
601 	int pos = 0;
602 	int n;
603 
604 	/* Copy header. */
605 	if (copy > 0) {
606 		if (copy > len)
607 			copy = len;
608 		n = csum_and_copy_to_iter(skb->data + offset, copy, csump, to);
609 		offset += n;
610 		if (n != copy)
611 			goto fault;
612 		if ((len -= copy) == 0)
613 			return 0;
614 		pos = copy;
615 	}
616 
617 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
618 		int end;
619 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
620 
621 		WARN_ON(start > offset + len);
622 
623 		end = start + skb_frag_size(frag);
624 		if ((copy = end - offset) > 0) {
625 			__wsum csum2 = 0;
626 			struct page *page = skb_frag_page(frag);
627 			u8  *vaddr = kmap(page);
628 
629 			if (copy > len)
630 				copy = len;
631 			n = csum_and_copy_to_iter(vaddr + frag->page_offset +
632 						  offset - start, copy,
633 						  &csum2, to);
634 			kunmap(page);
635 			offset += n;
636 			if (n != copy)
637 				goto fault;
638 			*csump = csum_block_add(*csump, csum2, pos);
639 			if (!(len -= copy))
640 				return 0;
641 			pos += copy;
642 		}
643 		start = end;
644 	}
645 
646 	skb_walk_frags(skb, frag_iter) {
647 		int end;
648 
649 		WARN_ON(start > offset + len);
650 
651 		end = start + frag_iter->len;
652 		if ((copy = end - offset) > 0) {
653 			__wsum csum2 = 0;
654 			if (copy > len)
655 				copy = len;
656 			if (skb_copy_and_csum_datagram(frag_iter,
657 						       offset - start,
658 						       to, copy,
659 						       &csum2))
660 				goto fault;
661 			*csump = csum_block_add(*csump, csum2, pos);
662 			if ((len -= copy) == 0)
663 				return 0;
664 			offset += copy;
665 			pos += copy;
666 		}
667 		start = end;
668 	}
669 	if (!len)
670 		return 0;
671 
672 fault:
673 	iov_iter_revert(to, offset - start_off);
674 	return -EFAULT;
675 }
676 
__skb_checksum_complete_head(struct sk_buff * skb,int len)677 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
678 {
679 	__sum16 sum;
680 
681 	sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
682 	if (likely(!sum)) {
683 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
684 		    !skb->csum_complete_sw)
685 			netdev_rx_csum_fault(skb->dev);
686 	}
687 	if (!skb_shared(skb))
688 		skb->csum_valid = !sum;
689 	return sum;
690 }
691 EXPORT_SYMBOL(__skb_checksum_complete_head);
692 
__skb_checksum_complete(struct sk_buff * skb)693 __sum16 __skb_checksum_complete(struct sk_buff *skb)
694 {
695 	__wsum csum;
696 	__sum16 sum;
697 
698 	csum = skb_checksum(skb, 0, skb->len, 0);
699 
700 	/* skb->csum holds pseudo checksum */
701 	sum = csum_fold(csum_add(skb->csum, csum));
702 	if (likely(!sum)) {
703 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
704 		    !skb->csum_complete_sw)
705 			netdev_rx_csum_fault(skb->dev);
706 	}
707 
708 	if (!skb_shared(skb)) {
709 		/* Save full packet checksum */
710 		skb->csum = csum;
711 		skb->ip_summed = CHECKSUM_COMPLETE;
712 		skb->csum_complete_sw = 1;
713 		skb->csum_valid = !sum;
714 	}
715 
716 	return sum;
717 }
718 EXPORT_SYMBOL(__skb_checksum_complete);
719 
720 /**
721  *	skb_copy_and_csum_datagram_msg - Copy and checksum skb to user iovec.
722  *	@skb: skbuff
723  *	@hlen: hardware length
724  *	@msg: destination
725  *
726  *	Caller _must_ check that skb will fit to this iovec.
727  *
728  *	Returns: 0       - success.
729  *		 -EINVAL - checksum failure.
730  *		 -EFAULT - fault during copy.
731  */
skb_copy_and_csum_datagram_msg(struct sk_buff * skb,int hlen,struct msghdr * msg)732 int skb_copy_and_csum_datagram_msg(struct sk_buff *skb,
733 				   int hlen, struct msghdr *msg)
734 {
735 	__wsum csum;
736 	int chunk = skb->len - hlen;
737 
738 	if (!chunk)
739 		return 0;
740 
741 	if (msg_data_left(msg) < chunk) {
742 		if (__skb_checksum_complete(skb))
743 			return -EINVAL;
744 		if (skb_copy_datagram_msg(skb, hlen, msg, chunk))
745 			goto fault;
746 	} else {
747 		csum = csum_partial(skb->data, hlen, skb->csum);
748 		if (skb_copy_and_csum_datagram(skb, hlen, &msg->msg_iter,
749 					       chunk, &csum))
750 			goto fault;
751 
752 		if (csum_fold(csum)) {
753 			iov_iter_revert(&msg->msg_iter, chunk);
754 			return -EINVAL;
755 		}
756 
757 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
758 			netdev_rx_csum_fault(skb->dev);
759 	}
760 	return 0;
761 fault:
762 	return -EFAULT;
763 }
764 EXPORT_SYMBOL(skb_copy_and_csum_datagram_msg);
765 
766 /**
767  * 	datagram_poll - generic datagram poll
768  *	@file: file struct
769  *	@sock: socket
770  *	@wait: poll table
771  *
772  *	Datagram poll: Again totally generic. This also handles
773  *	sequenced packet sockets providing the socket receive queue
774  *	is only ever holding data ready to receive.
775  *
776  *	Note: when you _don't_ use this routine for this protocol,
777  *	and you use a different write policy from sock_writeable()
778  *	then please supply your own write_space callback.
779  */
datagram_poll(struct file * file,struct socket * sock,poll_table * wait)780 unsigned int datagram_poll(struct file *file, struct socket *sock,
781 			   poll_table *wait)
782 {
783 	struct sock *sk = sock->sk;
784 	unsigned int mask;
785 
786 	sock_poll_wait(file, sk_sleep(sk), wait);
787 	mask = 0;
788 
789 	/* exceptional events? */
790 	if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
791 		mask |= POLLERR |
792 			(sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
793 
794 	if (sk->sk_shutdown & RCV_SHUTDOWN)
795 		mask |= POLLRDHUP | POLLIN | POLLRDNORM;
796 	if (sk->sk_shutdown == SHUTDOWN_MASK)
797 		mask |= POLLHUP;
798 
799 	/* readable? */
800 	if (!skb_queue_empty(&sk->sk_receive_queue))
801 		mask |= POLLIN | POLLRDNORM;
802 
803 	/* Connection-based need to check for termination and startup */
804 	if (connection_based(sk)) {
805 		if (sk->sk_state == TCP_CLOSE)
806 			mask |= POLLHUP;
807 		/* connection hasn't started yet? */
808 		if (sk->sk_state == TCP_SYN_SENT)
809 			return mask;
810 	}
811 
812 	/* writable? */
813 	if (sock_writeable(sk))
814 		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
815 	else
816 		sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
817 
818 	return mask;
819 }
820 EXPORT_SYMBOL(datagram_poll);
821