drm/etnaviv: rename etnaviv_gem_vaddr to etnaviv_gem_vmap
[linux-2.6-block.git] / net / core / datagram.c
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  */
66 static inline int connection_based(struct sock *sk)
67 {
68         return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
69 }
70
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  */
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
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  */
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
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
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
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
304 void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb)
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                 return;
312
313         slow = lock_sock_fast(sk);
314         skb_orphan(skb);
315         sk_mem_reclaim_partial(sk);
316         unlock_sock_fast(sk, slow);
317
318         /* skb is now orphaned, can be freed outside of locked section */
319         __kfree_skb(skb);
320 }
321 EXPORT_SYMBOL(skb_free_datagram_locked);
322
323 /**
324  *      skb_kill_datagram - Free a datagram skbuff forcibly
325  *      @sk: socket
326  *      @skb: datagram skbuff
327  *      @flags: MSG_ flags
328  *
329  *      This function frees a datagram skbuff that was received by
330  *      skb_recv_datagram.  The flags argument must match the one
331  *      used for skb_recv_datagram.
332  *
333  *      If the MSG_PEEK flag is set, and the packet is still on the
334  *      receive queue of the socket, it will be taken off the queue
335  *      before it is freed.
336  *
337  *      This function currently only disables BH when acquiring the
338  *      sk_receive_queue lock.  Therefore it must not be used in a
339  *      context where that lock is acquired in an IRQ context.
340  *
341  *      It returns 0 if the packet was removed by us.
342  */
343
344 int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
345 {
346         int err = 0;
347
348         if (flags & MSG_PEEK) {
349                 err = -ENOENT;
350                 spin_lock_bh(&sk->sk_receive_queue.lock);
351                 if (skb == skb_peek(&sk->sk_receive_queue)) {
352                         __skb_unlink(skb, &sk->sk_receive_queue);
353                         atomic_dec(&skb->users);
354                         err = 0;
355                 }
356                 spin_unlock_bh(&sk->sk_receive_queue.lock);
357         }
358
359         kfree_skb(skb);
360         atomic_inc(&sk->sk_drops);
361         sk_mem_reclaim_partial(sk);
362
363         return err;
364 }
365 EXPORT_SYMBOL(skb_kill_datagram);
366
367 /**
368  *      skb_copy_datagram_iter - Copy a datagram to an iovec iterator.
369  *      @skb: buffer to copy
370  *      @offset: offset in the buffer to start copying from
371  *      @to: iovec iterator to copy to
372  *      @len: amount of data to copy from buffer to iovec
373  */
374 int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
375                            struct iov_iter *to, int len)
376 {
377         int start = skb_headlen(skb);
378         int i, copy = start - offset;
379         struct sk_buff *frag_iter;
380
381         trace_skb_copy_datagram_iovec(skb, len);
382
383         /* Copy header. */
384         if (copy > 0) {
385                 if (copy > len)
386                         copy = len;
387                 if (copy_to_iter(skb->data + offset, copy, to) != copy)
388                         goto short_copy;
389                 if ((len -= copy) == 0)
390                         return 0;
391                 offset += copy;
392         }
393
394         /* Copy paged appendix. Hmm... why does this look so complicated? */
395         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
396                 int end;
397                 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
398
399                 WARN_ON(start > offset + len);
400
401                 end = start + skb_frag_size(frag);
402                 if ((copy = end - offset) > 0) {
403                         if (copy > len)
404                                 copy = len;
405                         if (copy_page_to_iter(skb_frag_page(frag),
406                                               frag->page_offset + offset -
407                                               start, copy, to) != copy)
408                                 goto short_copy;
409                         if (!(len -= copy))
410                                 return 0;
411                         offset += copy;
412                 }
413                 start = end;
414         }
415
416         skb_walk_frags(skb, frag_iter) {
417                 int end;
418
419                 WARN_ON(start > offset + len);
420
421                 end = start + frag_iter->len;
422                 if ((copy = end - offset) > 0) {
423                         if (copy > len)
424                                 copy = len;
425                         if (skb_copy_datagram_iter(frag_iter, offset - start,
426                                                    to, copy))
427                                 goto fault;
428                         if ((len -= copy) == 0)
429                                 return 0;
430                         offset += copy;
431                 }
432                 start = end;
433         }
434         if (!len)
435                 return 0;
436
437         /* This is not really a user copy fault, but rather someone
438          * gave us a bogus length on the skb.  We should probably
439          * print a warning here as it may indicate a kernel bug.
440          */
441
442 fault:
443         return -EFAULT;
444
445 short_copy:
446         if (iov_iter_count(to))
447                 goto fault;
448
449         return 0;
450 }
451 EXPORT_SYMBOL(skb_copy_datagram_iter);
452
453 /**
454  *      skb_copy_datagram_from_iter - Copy a datagram from an iov_iter.
455  *      @skb: buffer to copy
456  *      @offset: offset in the buffer to start copying to
457  *      @from: the copy source
458  *      @len: amount of data to copy to buffer from iovec
459  *
460  *      Returns 0 or -EFAULT.
461  */
462 int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
463                                  struct iov_iter *from,
464                                  int len)
465 {
466         int start = skb_headlen(skb);
467         int i, copy = start - offset;
468         struct sk_buff *frag_iter;
469
470         /* Copy header. */
471         if (copy > 0) {
472                 if (copy > len)
473                         copy = len;
474                 if (copy_from_iter(skb->data + offset, copy, from) != copy)
475                         goto fault;
476                 if ((len -= copy) == 0)
477                         return 0;
478                 offset += copy;
479         }
480
481         /* Copy paged appendix. Hmm... why does this look so complicated? */
482         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
483                 int end;
484                 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
485
486                 WARN_ON(start > offset + len);
487
488                 end = start + skb_frag_size(frag);
489                 if ((copy = end - offset) > 0) {
490                         size_t copied;
491
492                         if (copy > len)
493                                 copy = len;
494                         copied = copy_page_from_iter(skb_frag_page(frag),
495                                           frag->page_offset + offset - start,
496                                           copy, from);
497                         if (copied != copy)
498                                 goto fault;
499
500                         if (!(len -= copy))
501                                 return 0;
502                         offset += copy;
503                 }
504                 start = end;
505         }
506
507         skb_walk_frags(skb, frag_iter) {
508                 int end;
509
510                 WARN_ON(start > offset + len);
511
512                 end = start + frag_iter->len;
513                 if ((copy = end - offset) > 0) {
514                         if (copy > len)
515                                 copy = len;
516                         if (skb_copy_datagram_from_iter(frag_iter,
517                                                         offset - start,
518                                                         from, copy))
519                                 goto fault;
520                         if ((len -= copy) == 0)
521                                 return 0;
522                         offset += copy;
523                 }
524                 start = end;
525         }
526         if (!len)
527                 return 0;
528
529 fault:
530         return -EFAULT;
531 }
532 EXPORT_SYMBOL(skb_copy_datagram_from_iter);
533
534 /**
535  *      zerocopy_sg_from_iter - Build a zerocopy datagram from an iov_iter
536  *      @skb: buffer to copy
537  *      @from: the source to copy from
538  *
539  *      The function will first copy up to headlen, and then pin the userspace
540  *      pages and build frags through them.
541  *
542  *      Returns 0, -EFAULT or -EMSGSIZE.
543  */
544 int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *from)
545 {
546         int len = iov_iter_count(from);
547         int copy = min_t(int, skb_headlen(skb), len);
548         int frag = 0;
549
550         /* copy up to skb headlen */
551         if (skb_copy_datagram_from_iter(skb, 0, from, copy))
552                 return -EFAULT;
553
554         while (iov_iter_count(from)) {
555                 struct page *pages[MAX_SKB_FRAGS];
556                 size_t start;
557                 ssize_t copied;
558                 unsigned long truesize;
559                 int n = 0;
560
561                 if (frag == MAX_SKB_FRAGS)
562                         return -EMSGSIZE;
563
564                 copied = iov_iter_get_pages(from, pages, ~0U,
565                                             MAX_SKB_FRAGS - frag, &start);
566                 if (copied < 0)
567                         return -EFAULT;
568
569                 iov_iter_advance(from, copied);
570
571                 truesize = PAGE_ALIGN(copied + start);
572                 skb->data_len += copied;
573                 skb->len += copied;
574                 skb->truesize += truesize;
575                 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
576                 while (copied) {
577                         int size = min_t(int, copied, PAGE_SIZE - start);
578                         skb_fill_page_desc(skb, frag++, pages[n], start, size);
579                         start = 0;
580                         copied -= size;
581                         n++;
582                 }
583         }
584         return 0;
585 }
586 EXPORT_SYMBOL(zerocopy_sg_from_iter);
587
588 static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
589                                       struct iov_iter *to, int len,
590                                       __wsum *csump)
591 {
592         int start = skb_headlen(skb);
593         int i, copy = start - offset;
594         struct sk_buff *frag_iter;
595         int pos = 0;
596         int n;
597
598         /* Copy header. */
599         if (copy > 0) {
600                 if (copy > len)
601                         copy = len;
602                 n = csum_and_copy_to_iter(skb->data + offset, copy, csump, to);
603                 if (n != copy)
604                         goto fault;
605                 if ((len -= copy) == 0)
606                         return 0;
607                 offset += copy;
608                 pos = copy;
609         }
610
611         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
612                 int end;
613                 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
614
615                 WARN_ON(start > offset + len);
616
617                 end = start + skb_frag_size(frag);
618                 if ((copy = end - offset) > 0) {
619                         __wsum csum2 = 0;
620                         struct page *page = skb_frag_page(frag);
621                         u8  *vaddr = kmap(page);
622
623                         if (copy > len)
624                                 copy = len;
625                         n = csum_and_copy_to_iter(vaddr + frag->page_offset +
626                                                   offset - start, copy,
627                                                   &csum2, to);
628                         kunmap(page);
629                         if (n != copy)
630                                 goto fault;
631                         *csump = csum_block_add(*csump, csum2, pos);
632                         if (!(len -= copy))
633                                 return 0;
634                         offset += copy;
635                         pos += copy;
636                 }
637                 start = end;
638         }
639
640         skb_walk_frags(skb, frag_iter) {
641                 int end;
642
643                 WARN_ON(start > offset + len);
644
645                 end = start + frag_iter->len;
646                 if ((copy = end - offset) > 0) {
647                         __wsum csum2 = 0;
648                         if (copy > len)
649                                 copy = len;
650                         if (skb_copy_and_csum_datagram(frag_iter,
651                                                        offset - start,
652                                                        to, copy,
653                                                        &csum2))
654                                 goto fault;
655                         *csump = csum_block_add(*csump, csum2, pos);
656                         if ((len -= copy) == 0)
657                                 return 0;
658                         offset += copy;
659                         pos += copy;
660                 }
661                 start = end;
662         }
663         if (!len)
664                 return 0;
665
666 fault:
667         return -EFAULT;
668 }
669
670 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
671 {
672         __sum16 sum;
673
674         sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
675         if (likely(!sum)) {
676                 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
677                     !skb->csum_complete_sw)
678                         netdev_rx_csum_fault(skb->dev);
679         }
680         if (!skb_shared(skb))
681                 skb->csum_valid = !sum;
682         return sum;
683 }
684 EXPORT_SYMBOL(__skb_checksum_complete_head);
685
686 __sum16 __skb_checksum_complete(struct sk_buff *skb)
687 {
688         __wsum csum;
689         __sum16 sum;
690
691         csum = skb_checksum(skb, 0, skb->len, 0);
692
693         /* skb->csum holds pseudo checksum */
694         sum = csum_fold(csum_add(skb->csum, csum));
695         if (likely(!sum)) {
696                 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
697                     !skb->csum_complete_sw)
698                         netdev_rx_csum_fault(skb->dev);
699         }
700
701         if (!skb_shared(skb)) {
702                 /* Save full packet checksum */
703                 skb->csum = csum;
704                 skb->ip_summed = CHECKSUM_COMPLETE;
705                 skb->csum_complete_sw = 1;
706                 skb->csum_valid = !sum;
707         }
708
709         return sum;
710 }
711 EXPORT_SYMBOL(__skb_checksum_complete);
712
713 /**
714  *      skb_copy_and_csum_datagram_msg - Copy and checksum skb to user iovec.
715  *      @skb: skbuff
716  *      @hlen: hardware length
717  *      @msg: destination
718  *
719  *      Caller _must_ check that skb will fit to this iovec.
720  *
721  *      Returns: 0       - success.
722  *               -EINVAL - checksum failure.
723  *               -EFAULT - fault during copy.
724  */
725 int skb_copy_and_csum_datagram_msg(struct sk_buff *skb,
726                                    int hlen, struct msghdr *msg)
727 {
728         __wsum csum;
729         int chunk = skb->len - hlen;
730
731         if (!chunk)
732                 return 0;
733
734         if (msg_data_left(msg) < chunk) {
735                 if (__skb_checksum_complete(skb))
736                         goto csum_error;
737                 if (skb_copy_datagram_msg(skb, hlen, msg, chunk))
738                         goto fault;
739         } else {
740                 csum = csum_partial(skb->data, hlen, skb->csum);
741                 if (skb_copy_and_csum_datagram(skb, hlen, &msg->msg_iter,
742                                                chunk, &csum))
743                         goto fault;
744                 if (csum_fold(csum))
745                         goto csum_error;
746                 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
747                         netdev_rx_csum_fault(skb->dev);
748         }
749         return 0;
750 csum_error:
751         return -EINVAL;
752 fault:
753         return -EFAULT;
754 }
755 EXPORT_SYMBOL(skb_copy_and_csum_datagram_msg);
756
757 /**
758  *      datagram_poll - generic datagram poll
759  *      @file: file struct
760  *      @sock: socket
761  *      @wait: poll table
762  *
763  *      Datagram poll: Again totally generic. This also handles
764  *      sequenced packet sockets providing the socket receive queue
765  *      is only ever holding data ready to receive.
766  *
767  *      Note: when you _don't_ use this routine for this protocol,
768  *      and you use a different write policy from sock_writeable()
769  *      then please supply your own write_space callback.
770  */
771 unsigned int datagram_poll(struct file *file, struct socket *sock,
772                            poll_table *wait)
773 {
774         struct sock *sk = sock->sk;
775         unsigned int mask;
776
777         sock_poll_wait(file, sk_sleep(sk), wait);
778         mask = 0;
779
780         /* exceptional events? */
781         if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
782                 mask |= POLLERR |
783                         (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
784
785         if (sk->sk_shutdown & RCV_SHUTDOWN)
786                 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
787         if (sk->sk_shutdown == SHUTDOWN_MASK)
788                 mask |= POLLHUP;
789
790         /* readable? */
791         if (!skb_queue_empty(&sk->sk_receive_queue))
792                 mask |= POLLIN | POLLRDNORM;
793
794         /* Connection-based need to check for termination and startup */
795         if (connection_based(sk)) {
796                 if (sk->sk_state == TCP_CLOSE)
797                         mask |= POLLHUP;
798                 /* connection hasn't started yet? */
799                 if (sk->sk_state == TCP_SYN_SENT)
800                         return mask;
801         }
802
803         /* writable? */
804         if (sock_writeable(sk))
805                 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
806         else
807                 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
808
809         return mask;
810 }
811 EXPORT_SYMBOL(datagram_poll);