]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/core/datagram.c
netns: add dummy struct inside "struct net_generic"
[karo-tx-linux.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  *      @destructor: invoked under the receive lock on successful dequeue
169  *      @peeked: returns non-zero if this packet has been seen before
170  *      @off: an offset in bytes to peek skb from. Returns an offset
171  *            within an skb where data actually starts
172  *      @err: error code returned
173  *      @last: set to last peeked message to inform the wait function
174  *             what to look for when peeking
175  *
176  *      Get a datagram skbuff, understands the peeking, nonblocking wakeups
177  *      and possible races. This replaces identical code in packet, raw and
178  *      udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
179  *      the long standing peek and read race for datagram sockets. If you
180  *      alter this routine remember it must be re-entrant.
181  *
182  *      This function will lock the socket if a skb is returned, so
183  *      the caller needs to unlock the socket in that case (usually by
184  *      calling skb_free_datagram). Returns NULL with *err set to
185  *      -EAGAIN if no data was available or to some other value if an
186  *      error was detected.
187  *
188  *      * It does not lock socket since today. This function is
189  *      * free of race conditions. This measure should/can improve
190  *      * significantly datagram socket latencies at high loads,
191  *      * when data copying to user space takes lots of time.
192  *      * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
193  *      *  8) Great win.)
194  *      *                                           --ANK (980729)
195  *
196  *      The order of the tests when we find no data waiting are specified
197  *      quite explicitly by POSIX 1003.1g, don't change them without having
198  *      the standard around please.
199  */
200 struct sk_buff *__skb_try_recv_datagram(struct sock *sk, unsigned int flags,
201                                         void (*destructor)(struct sock *sk,
202                                                            struct sk_buff *skb),
203                                         int *peeked, int *off, int *err,
204                                         struct sk_buff **last)
205 {
206         struct sk_buff_head *queue = &sk->sk_receive_queue;
207         struct sk_buff *skb;
208         unsigned long cpu_flags;
209         /*
210          * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
211          */
212         int error = sock_error(sk);
213
214         if (error)
215                 goto no_packet;
216
217         do {
218                 /* Again only user level code calls this function, so nothing
219                  * interrupt level will suddenly eat the receive_queue.
220                  *
221                  * Look at current nfs client by the way...
222                  * However, this function was correct in any case. 8)
223                  */
224                 int _off = *off;
225
226                 *last = (struct sk_buff *)queue;
227                 spin_lock_irqsave(&queue->lock, cpu_flags);
228                 skb_queue_walk(queue, skb) {
229                         *last = skb;
230                         *peeked = skb->peeked;
231                         if (flags & MSG_PEEK) {
232                                 if (_off >= skb->len && (skb->len || _off ||
233                                                          skb->peeked)) {
234                                         _off -= skb->len;
235                                         continue;
236                                 }
237
238                                 skb = skb_set_peeked(skb);
239                                 error = PTR_ERR(skb);
240                                 if (IS_ERR(skb)) {
241                                         spin_unlock_irqrestore(&queue->lock,
242                                                                cpu_flags);
243                                         goto no_packet;
244                                 }
245
246                                 atomic_inc(&skb->users);
247                         } else {
248                                 __skb_unlink(skb, queue);
249                                 if (destructor)
250                                         destructor(sk, skb);
251                         }
252                         spin_unlock_irqrestore(&queue->lock, cpu_flags);
253                         *off = _off;
254                         return skb;
255                 }
256
257                 spin_unlock_irqrestore(&queue->lock, cpu_flags);
258         } while (sk_can_busy_loop(sk) &&
259                  sk_busy_loop(sk, flags & MSG_DONTWAIT));
260
261         error = -EAGAIN;
262
263 no_packet:
264         *err = error;
265         return NULL;
266 }
267 EXPORT_SYMBOL(__skb_try_recv_datagram);
268
269 struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
270                                     void (*destructor)(struct sock *sk,
271                                                        struct sk_buff *skb),
272                                     int *peeked, int *off, int *err)
273 {
274         struct sk_buff *skb, *last;
275         long timeo;
276
277         timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
278
279         do {
280                 skb = __skb_try_recv_datagram(sk, flags, destructor, peeked,
281                                               off, err, &last);
282                 if (skb)
283                         return skb;
284
285                 if (*err != -EAGAIN)
286                         break;
287         } while (timeo &&
288                 !__skb_wait_for_more_packets(sk, err, &timeo, last));
289
290         return NULL;
291 }
292 EXPORT_SYMBOL(__skb_recv_datagram);
293
294 struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
295                                   int noblock, int *err)
296 {
297         int peeked, off = 0;
298
299         return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
300                                    NULL, &peeked, &off, err);
301 }
302 EXPORT_SYMBOL(skb_recv_datagram);
303
304 void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
305 {
306         consume_skb(skb);
307         sk_mem_reclaim_partial(sk);
308 }
309 EXPORT_SYMBOL(skb_free_datagram);
310
311 void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len)
312 {
313         bool slow;
314
315         if (likely(atomic_read(&skb->users) == 1))
316                 smp_rmb();
317         else if (likely(!atomic_dec_and_test(&skb->users))) {
318                 sk_peek_offset_bwd(sk, len);
319                 return;
320         }
321
322         slow = lock_sock_fast(sk);
323         sk_peek_offset_bwd(sk, len);
324         skb_orphan(skb);
325         sk_mem_reclaim_partial(sk);
326         unlock_sock_fast(sk, slow);
327
328         /* skb is now orphaned, can be freed outside of locked section */
329         __kfree_skb(skb);
330 }
331 EXPORT_SYMBOL(__skb_free_datagram_locked);
332
333 int __sk_queue_drop_skb(struct sock *sk, struct sk_buff *skb,
334                         unsigned int flags)
335 {
336         int err = 0;
337
338         if (flags & MSG_PEEK) {
339                 err = -ENOENT;
340                 spin_lock_bh(&sk->sk_receive_queue.lock);
341                 if (skb == skb_peek(&sk->sk_receive_queue)) {
342                         __skb_unlink(skb, &sk->sk_receive_queue);
343                         atomic_dec(&skb->users);
344                         err = 0;
345                 }
346                 spin_unlock_bh(&sk->sk_receive_queue.lock);
347         }
348
349         atomic_inc(&sk->sk_drops);
350         return err;
351 }
352 EXPORT_SYMBOL(__sk_queue_drop_skb);
353
354 /**
355  *      skb_kill_datagram - Free a datagram skbuff forcibly
356  *      @sk: socket
357  *      @skb: datagram skbuff
358  *      @flags: MSG_ flags
359  *
360  *      This function frees a datagram skbuff that was received by
361  *      skb_recv_datagram.  The flags argument must match the one
362  *      used for skb_recv_datagram.
363  *
364  *      If the MSG_PEEK flag is set, and the packet is still on the
365  *      receive queue of the socket, it will be taken off the queue
366  *      before it is freed.
367  *
368  *      This function currently only disables BH when acquiring the
369  *      sk_receive_queue lock.  Therefore it must not be used in a
370  *      context where that lock is acquired in an IRQ context.
371  *
372  *      It returns 0 if the packet was removed by us.
373  */
374
375 int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
376 {
377         int err = __sk_queue_drop_skb(sk, skb, flags);
378
379         kfree_skb(skb);
380         sk_mem_reclaim_partial(sk);
381         return err;
382 }
383 EXPORT_SYMBOL(skb_kill_datagram);
384
385 /**
386  *      skb_copy_datagram_iter - Copy a datagram to an iovec iterator.
387  *      @skb: buffer to copy
388  *      @offset: offset in the buffer to start copying from
389  *      @to: iovec iterator to copy to
390  *      @len: amount of data to copy from buffer to iovec
391  */
392 int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
393                            struct iov_iter *to, int len)
394 {
395         int start = skb_headlen(skb);
396         int i, copy = start - offset;
397         struct sk_buff *frag_iter;
398
399         trace_skb_copy_datagram_iovec(skb, len);
400
401         /* Copy header. */
402         if (copy > 0) {
403                 if (copy > len)
404                         copy = len;
405                 if (copy_to_iter(skb->data + offset, copy, to) != copy)
406                         goto short_copy;
407                 if ((len -= copy) == 0)
408                         return 0;
409                 offset += copy;
410         }
411
412         /* Copy paged appendix. Hmm... why does this look so complicated? */
413         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
414                 int end;
415                 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
416
417                 WARN_ON(start > offset + len);
418
419                 end = start + skb_frag_size(frag);
420                 if ((copy = end - offset) > 0) {
421                         if (copy > len)
422                                 copy = len;
423                         if (copy_page_to_iter(skb_frag_page(frag),
424                                               frag->page_offset + offset -
425                                               start, copy, to) != copy)
426                                 goto short_copy;
427                         if (!(len -= copy))
428                                 return 0;
429                         offset += copy;
430                 }
431                 start = end;
432         }
433
434         skb_walk_frags(skb, frag_iter) {
435                 int end;
436
437                 WARN_ON(start > offset + len);
438
439                 end = start + frag_iter->len;
440                 if ((copy = end - offset) > 0) {
441                         if (copy > len)
442                                 copy = len;
443                         if (skb_copy_datagram_iter(frag_iter, offset - start,
444                                                    to, copy))
445                                 goto fault;
446                         if ((len -= copy) == 0)
447                                 return 0;
448                         offset += copy;
449                 }
450                 start = end;
451         }
452         if (!len)
453                 return 0;
454
455         /* This is not really a user copy fault, but rather someone
456          * gave us a bogus length on the skb.  We should probably
457          * print a warning here as it may indicate a kernel bug.
458          */
459
460 fault:
461         return -EFAULT;
462
463 short_copy:
464         if (iov_iter_count(to))
465                 goto fault;
466
467         return 0;
468 }
469 EXPORT_SYMBOL(skb_copy_datagram_iter);
470
471 /**
472  *      skb_copy_datagram_from_iter - Copy a datagram from an iov_iter.
473  *      @skb: buffer to copy
474  *      @offset: offset in the buffer to start copying to
475  *      @from: the copy source
476  *      @len: amount of data to copy to buffer from iovec
477  *
478  *      Returns 0 or -EFAULT.
479  */
480 int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
481                                  struct iov_iter *from,
482                                  int len)
483 {
484         int start = skb_headlen(skb);
485         int i, copy = start - offset;
486         struct sk_buff *frag_iter;
487
488         /* Copy header. */
489         if (copy > 0) {
490                 if (copy > len)
491                         copy = len;
492                 if (copy_from_iter(skb->data + offset, copy, from) != copy)
493                         goto fault;
494                 if ((len -= copy) == 0)
495                         return 0;
496                 offset += copy;
497         }
498
499         /* Copy paged appendix. Hmm... why does this look so complicated? */
500         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
501                 int end;
502                 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
503
504                 WARN_ON(start > offset + len);
505
506                 end = start + skb_frag_size(frag);
507                 if ((copy = end - offset) > 0) {
508                         size_t copied;
509
510                         if (copy > len)
511                                 copy = len;
512                         copied = copy_page_from_iter(skb_frag_page(frag),
513                                           frag->page_offset + offset - start,
514                                           copy, from);
515                         if (copied != copy)
516                                 goto fault;
517
518                         if (!(len -= copy))
519                                 return 0;
520                         offset += copy;
521                 }
522                 start = end;
523         }
524
525         skb_walk_frags(skb, frag_iter) {
526                 int end;
527
528                 WARN_ON(start > offset + len);
529
530                 end = start + frag_iter->len;
531                 if ((copy = end - offset) > 0) {
532                         if (copy > len)
533                                 copy = len;
534                         if (skb_copy_datagram_from_iter(frag_iter,
535                                                         offset - start,
536                                                         from, copy))
537                                 goto fault;
538                         if ((len -= copy) == 0)
539                                 return 0;
540                         offset += copy;
541                 }
542                 start = end;
543         }
544         if (!len)
545                 return 0;
546
547 fault:
548         return -EFAULT;
549 }
550 EXPORT_SYMBOL(skb_copy_datagram_from_iter);
551
552 /**
553  *      zerocopy_sg_from_iter - Build a zerocopy datagram from an iov_iter
554  *      @skb: buffer to copy
555  *      @from: the source to copy from
556  *
557  *      The function will first copy up to headlen, and then pin the userspace
558  *      pages and build frags through them.
559  *
560  *      Returns 0, -EFAULT or -EMSGSIZE.
561  */
562 int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *from)
563 {
564         int len = iov_iter_count(from);
565         int copy = min_t(int, skb_headlen(skb), len);
566         int frag = 0;
567
568         /* copy up to skb headlen */
569         if (skb_copy_datagram_from_iter(skb, 0, from, copy))
570                 return -EFAULT;
571
572         while (iov_iter_count(from)) {
573                 struct page *pages[MAX_SKB_FRAGS];
574                 size_t start;
575                 ssize_t copied;
576                 unsigned long truesize;
577                 int n = 0;
578
579                 if (frag == MAX_SKB_FRAGS)
580                         return -EMSGSIZE;
581
582                 copied = iov_iter_get_pages(from, pages, ~0U,
583                                             MAX_SKB_FRAGS - frag, &start);
584                 if (copied < 0)
585                         return -EFAULT;
586
587                 iov_iter_advance(from, copied);
588
589                 truesize = PAGE_ALIGN(copied + start);
590                 skb->data_len += copied;
591                 skb->len += copied;
592                 skb->truesize += truesize;
593                 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
594                 while (copied) {
595                         int size = min_t(int, copied, PAGE_SIZE - start);
596                         skb_fill_page_desc(skb, frag++, pages[n], start, size);
597                         start = 0;
598                         copied -= size;
599                         n++;
600                 }
601         }
602         return 0;
603 }
604 EXPORT_SYMBOL(zerocopy_sg_from_iter);
605
606 static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
607                                       struct iov_iter *to, int len,
608                                       __wsum *csump)
609 {
610         int start = skb_headlen(skb);
611         int i, copy = start - offset;
612         struct sk_buff *frag_iter;
613         int pos = 0;
614         int n;
615
616         /* Copy header. */
617         if (copy > 0) {
618                 if (copy > len)
619                         copy = len;
620                 n = csum_and_copy_to_iter(skb->data + offset, copy, csump, to);
621                 if (n != copy)
622                         goto fault;
623                 if ((len -= copy) == 0)
624                         return 0;
625                 offset += copy;
626                 pos = copy;
627         }
628
629         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
630                 int end;
631                 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
632
633                 WARN_ON(start > offset + len);
634
635                 end = start + skb_frag_size(frag);
636                 if ((copy = end - offset) > 0) {
637                         __wsum csum2 = 0;
638                         struct page *page = skb_frag_page(frag);
639                         u8  *vaddr = kmap(page);
640
641                         if (copy > len)
642                                 copy = len;
643                         n = csum_and_copy_to_iter(vaddr + frag->page_offset +
644                                                   offset - start, copy,
645                                                   &csum2, to);
646                         kunmap(page);
647                         if (n != copy)
648                                 goto fault;
649                         *csump = csum_block_add(*csump, csum2, pos);
650                         if (!(len -= copy))
651                                 return 0;
652                         offset += copy;
653                         pos += copy;
654                 }
655                 start = end;
656         }
657
658         skb_walk_frags(skb, frag_iter) {
659                 int end;
660
661                 WARN_ON(start > offset + len);
662
663                 end = start + frag_iter->len;
664                 if ((copy = end - offset) > 0) {
665                         __wsum csum2 = 0;
666                         if (copy > len)
667                                 copy = len;
668                         if (skb_copy_and_csum_datagram(frag_iter,
669                                                        offset - start,
670                                                        to, copy,
671                                                        &csum2))
672                                 goto fault;
673                         *csump = csum_block_add(*csump, csum2, pos);
674                         if ((len -= copy) == 0)
675                                 return 0;
676                         offset += copy;
677                         pos += copy;
678                 }
679                 start = end;
680         }
681         if (!len)
682                 return 0;
683
684 fault:
685         return -EFAULT;
686 }
687
688 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
689 {
690         __sum16 sum;
691
692         sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
693         if (likely(!sum)) {
694                 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
695                     !skb->csum_complete_sw)
696                         netdev_rx_csum_fault(skb->dev);
697         }
698         if (!skb_shared(skb))
699                 skb->csum_valid = !sum;
700         return sum;
701 }
702 EXPORT_SYMBOL(__skb_checksum_complete_head);
703
704 __sum16 __skb_checksum_complete(struct sk_buff *skb)
705 {
706         __wsum csum;
707         __sum16 sum;
708
709         csum = skb_checksum(skb, 0, skb->len, 0);
710
711         /* skb->csum holds pseudo checksum */
712         sum = csum_fold(csum_add(skb->csum, csum));
713         if (likely(!sum)) {
714                 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
715                     !skb->csum_complete_sw)
716                         netdev_rx_csum_fault(skb->dev);
717         }
718
719         if (!skb_shared(skb)) {
720                 /* Save full packet checksum */
721                 skb->csum = csum;
722                 skb->ip_summed = CHECKSUM_COMPLETE;
723                 skb->csum_complete_sw = 1;
724                 skb->csum_valid = !sum;
725         }
726
727         return sum;
728 }
729 EXPORT_SYMBOL(__skb_checksum_complete);
730
731 /**
732  *      skb_copy_and_csum_datagram_msg - Copy and checksum skb to user iovec.
733  *      @skb: skbuff
734  *      @hlen: hardware length
735  *      @msg: destination
736  *
737  *      Caller _must_ check that skb will fit to this iovec.
738  *
739  *      Returns: 0       - success.
740  *               -EINVAL - checksum failure.
741  *               -EFAULT - fault during copy.
742  */
743 int skb_copy_and_csum_datagram_msg(struct sk_buff *skb,
744                                    int hlen, struct msghdr *msg)
745 {
746         __wsum csum;
747         int chunk = skb->len - hlen;
748
749         if (!chunk)
750                 return 0;
751
752         if (msg_data_left(msg) < chunk) {
753                 if (__skb_checksum_complete(skb))
754                         goto csum_error;
755                 if (skb_copy_datagram_msg(skb, hlen, msg, chunk))
756                         goto fault;
757         } else {
758                 csum = csum_partial(skb->data, hlen, skb->csum);
759                 if (skb_copy_and_csum_datagram(skb, hlen, &msg->msg_iter,
760                                                chunk, &csum))
761                         goto fault;
762                 if (csum_fold(csum))
763                         goto csum_error;
764                 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
765                         netdev_rx_csum_fault(skb->dev);
766         }
767         return 0;
768 csum_error:
769         return -EINVAL;
770 fault:
771         return -EFAULT;
772 }
773 EXPORT_SYMBOL(skb_copy_and_csum_datagram_msg);
774
775 /**
776  *      datagram_poll - generic datagram poll
777  *      @file: file struct
778  *      @sock: socket
779  *      @wait: poll table
780  *
781  *      Datagram poll: Again totally generic. This also handles
782  *      sequenced packet sockets providing the socket receive queue
783  *      is only ever holding data ready to receive.
784  *
785  *      Note: when you _don't_ use this routine for this protocol,
786  *      and you use a different write policy from sock_writeable()
787  *      then please supply your own write_space callback.
788  */
789 unsigned int datagram_poll(struct file *file, struct socket *sock,
790                            poll_table *wait)
791 {
792         struct sock *sk = sock->sk;
793         unsigned int mask;
794
795         sock_poll_wait(file, sk_sleep(sk), wait);
796         mask = 0;
797
798         /* exceptional events? */
799         if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
800                 mask |= POLLERR |
801                         (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
802
803         if (sk->sk_shutdown & RCV_SHUTDOWN)
804                 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
805         if (sk->sk_shutdown == SHUTDOWN_MASK)
806                 mask |= POLLHUP;
807
808         /* readable? */
809         if (!skb_queue_empty(&sk->sk_receive_queue))
810                 mask |= POLLIN | POLLRDNORM;
811
812         /* Connection-based need to check for termination and startup */
813         if (connection_based(sk)) {
814                 if (sk->sk_state == TCP_CLOSE)
815                         mask |= POLLHUP;
816                 /* connection hasn't started yet? */
817                 if (sk->sk_state == TCP_SYN_SENT)
818                         return mask;
819         }
820
821         /* writable? */
822         if (sock_writeable(sk))
823                 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
824         else
825                 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
826
827         return mask;
828 }
829 EXPORT_SYMBOL(datagram_poll);