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