~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/core/datagram.c

Version: ~ [ linux-5.5-rc1 ] ~ [ linux-5.4.2 ] ~ [ linux-5.3.15 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.88 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.158 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.206 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.206 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.78 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp