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.
12 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old
16 * Alan Cox : NULL return from skb_peek_copy()
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
24 * Alan Cox : Fixed write poll of non IP protocol
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.
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>
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>
53 #include <net/protocol.h>
54 #include <linux/skbuff.h>
56 #include <net/checksum.h>
58 #include <net/tcp_states.h>
59 #include <trace/events/skb.h>
62 * Is a socket 'connection oriented' ?
64 static inline int connection_based(struct sock *sk)
66 return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
69 static int receiver_wake_function(wait_queue_t *wait, unsigned mode, int sync,
72 unsigned long bits = (unsigned long)key;
75 * Avoid a wakeup if event not interesting for us
77 if (bits && !(bits & (POLLIN | POLLERR)))
79 return autoremove_wake_function(wait, mode, sync, key);
84 static int wait_for_packet(struct sock *sk, int *err, long *timeo_p)
87 DEFINE_WAIT_FUNC(wait, receiver_wake_function);
89 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
92 error = sock_error(sk);
96 if (!skb_queue_empty(&sk->sk_receive_queue))
99 /* Socket shut down? */
100 if (sk->sk_shutdown & RCV_SHUTDOWN)
103 /* Sequenced packets can come disconnected.
104 * If so we report the problem
107 if (connection_based(sk) &&
108 !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
112 if (signal_pending(current))
116 *timeo_p = schedule_timeout(*timeo_p);
118 finish_wait(sk_sleep(sk), &wait);
121 error = sock_intr_errno(*timeo_p);
131 static int skb_set_peeked(struct sk_buff *skb)
133 struct sk_buff *nskb;
138 /* We have to unshare an skb before modifying it. */
139 if (!skb_shared(skb))
142 nskb = skb_clone(skb, GFP_ATOMIC);
146 skb->prev->next = nskb;
147 skb->next->prev = nskb;
148 nskb->prev = skb->prev;
149 nskb->next = skb->next;
161 * __skb_recv_datagram - Receive a datagram skbuff
164 * @peeked: returns non-zero if this packet has been seen before
165 * @err: error code returned
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.
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
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
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.
189 struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags,
190 int *peeked, int *err)
192 struct sk_buff_head *queue = &sk->sk_receive_queue;
194 unsigned long cpu_flags;
197 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
199 int error = sock_error(sk);
204 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
207 /* Again only user level code calls this function, so nothing
208 * interrupt level will suddenly eat the receive_queue.
210 * Look at current nfs client by the way...
211 * However, this function was correct in any case. 8)
213 spin_lock_irqsave(&queue->lock, cpu_flags);
214 skb = skb_peek(queue);
216 *peeked = skb->peeked;
217 if (flags & MSG_PEEK) {
219 error = skb_set_peeked(skb);
223 atomic_inc(&skb->users);
225 __skb_unlink(skb, queue);
227 spin_unlock_irqrestore(&queue->lock, cpu_flags);
232 /* User doesn't want to wait */
237 } while (!wait_for_packet(sk, err, &timeo));
242 spin_unlock_irqrestore(&queue->lock, cpu_flags);
247 EXPORT_SYMBOL(__skb_recv_datagram);
249 struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
250 int noblock, int *err)
254 return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
257 EXPORT_SYMBOL(skb_recv_datagram);
259 void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
262 sk_mem_reclaim_partial(sk);
264 EXPORT_SYMBOL(skb_free_datagram);
266 void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb)
270 if (likely(atomic_read(&skb->users) == 1))
272 else if (likely(!atomic_dec_and_test(&skb->users)))
275 slow = lock_sock_fast(sk);
277 sk_mem_reclaim_partial(sk);
278 unlock_sock_fast(sk, slow);
280 /* skb is now orphaned, can be freed outside of locked section */
281 trace_kfree_skb(skb, skb_free_datagram_locked);
284 EXPORT_SYMBOL(skb_free_datagram_locked);
287 * skb_kill_datagram - Free a datagram skbuff forcibly
289 * @skb: datagram skbuff
292 * This function frees a datagram skbuff that was received by
293 * skb_recv_datagram. The flags argument must match the one
294 * used for skb_recv_datagram.
296 * If the MSG_PEEK flag is set, and the packet is still on the
297 * receive queue of the socket, it will be taken off the queue
298 * before it is freed.
300 * This function currently only disables BH when acquiring the
301 * sk_receive_queue lock. Therefore it must not be used in a
302 * context where that lock is acquired in an IRQ context.
304 * It returns 0 if the packet was removed by us.
307 int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
311 if (flags & MSG_PEEK) {
313 spin_lock_bh(&sk->sk_receive_queue.lock);
314 if (skb == skb_peek(&sk->sk_receive_queue)) {
315 __skb_unlink(skb, &sk->sk_receive_queue);
316 atomic_dec(&skb->users);
319 spin_unlock_bh(&sk->sk_receive_queue.lock);
323 atomic_inc(&sk->sk_drops);
324 sk_mem_reclaim_partial(sk);
328 EXPORT_SYMBOL(skb_kill_datagram);
331 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
332 * @skb: buffer to copy
333 * @offset: offset in the buffer to start copying from
334 * @to: io vector to copy to
335 * @len: amount of data to copy from buffer to iovec
337 * Note: the iovec is modified during the copy.
339 int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
340 struct iovec *to, int len)
342 int start = skb_headlen(skb);
343 int i, copy = start - offset;
344 struct sk_buff *frag_iter;
346 trace_skb_copy_datagram_iovec(skb, len);
352 if (memcpy_toiovec(to, skb->data + offset, copy))
354 if ((len -= copy) == 0)
359 /* Copy paged appendix. Hmm... why does this look so complicated? */
360 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
362 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
364 WARN_ON(start > offset + len);
366 end = start + skb_frag_size(frag);
367 if ((copy = end - offset) > 0) {
370 struct page *page = skb_frag_page(frag);
375 err = memcpy_toiovec(to, vaddr + frag->page_offset +
376 offset - start, copy);
387 skb_walk_frags(skb, frag_iter) {
390 WARN_ON(start > offset + len);
392 end = start + frag_iter->len;
393 if ((copy = end - offset) > 0) {
396 if (skb_copy_datagram_iovec(frag_iter,
400 if ((len -= copy) == 0)
412 EXPORT_SYMBOL(skb_copy_datagram_iovec);
415 * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
416 * @skb: buffer to copy
417 * @offset: offset in the buffer to start copying from
418 * @to: io vector to copy to
419 * @to_offset: offset in the io vector to start copying to
420 * @len: amount of data to copy from buffer to iovec
422 * Returns 0 or -EFAULT.
423 * Note: the iovec is not modified during the copy.
425 int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset,
426 const struct iovec *to, int to_offset,
429 int start = skb_headlen(skb);
430 int i, copy = start - offset;
431 struct sk_buff *frag_iter;
437 if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy))
439 if ((len -= copy) == 0)
445 /* Copy paged appendix. Hmm... why does this look so complicated? */
446 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
448 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
450 WARN_ON(start > offset + len);
452 end = start + skb_frag_size(frag);
453 if ((copy = end - offset) > 0) {
456 struct page *page = skb_frag_page(frag);
461 err = memcpy_toiovecend(to, vaddr + frag->page_offset +
462 offset - start, to_offset, copy);
474 skb_walk_frags(skb, frag_iter) {
477 WARN_ON(start > offset + len);
479 end = start + frag_iter->len;
480 if ((copy = end - offset) > 0) {
483 if (skb_copy_datagram_const_iovec(frag_iter,
488 if ((len -= copy) == 0)
501 EXPORT_SYMBOL(skb_copy_datagram_const_iovec);
504 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
505 * @skb: buffer to copy
506 * @offset: offset in the buffer to start copying to
507 * @from: io vector to copy to
508 * @from_offset: offset in the io vector to start copying from
509 * @len: amount of data to copy to buffer from iovec
511 * Returns 0 or -EFAULT.
512 * Note: the iovec is not modified during the copy.
514 int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
515 const struct iovec *from, int from_offset,
518 int start = skb_headlen(skb);
519 int i, copy = start - offset;
520 struct sk_buff *frag_iter;
526 if (memcpy_fromiovecend(skb->data + offset, from, from_offset,
529 if ((len -= copy) == 0)
535 /* Copy paged appendix. Hmm... why does this look so complicated? */
536 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
538 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
540 WARN_ON(start > offset + len);
542 end = start + skb_frag_size(frag);
543 if ((copy = end - offset) > 0) {
546 struct page *page = skb_frag_page(frag);
551 err = memcpy_fromiovecend(vaddr + frag->page_offset +
553 from, from_offset, copy);
566 skb_walk_frags(skb, frag_iter) {
569 WARN_ON(start > offset + len);
571 end = start + frag_iter->len;
572 if ((copy = end - offset) > 0) {
575 if (skb_copy_datagram_from_iovec(frag_iter,
581 if ((len -= copy) == 0)
594 EXPORT_SYMBOL(skb_copy_datagram_from_iovec);
596 static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
597 u8 __user *to, int len,
600 int start = skb_headlen(skb);
601 int i, copy = start - offset;
602 struct sk_buff *frag_iter;
610 *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
614 if ((len -= copy) == 0)
621 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
623 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
625 WARN_ON(start > offset + len);
627 end = start + skb_frag_size(frag);
628 if ((copy = end - offset) > 0) {
632 struct page *page = skb_frag_page(frag);
637 csum2 = csum_and_copy_to_user(vaddr +
644 *csump = csum_block_add(*csump, csum2, pos);
654 skb_walk_frags(skb, frag_iter) {
657 WARN_ON(start > offset + len);
659 end = start + frag_iter->len;
660 if ((copy = end - offset) > 0) {
664 if (skb_copy_and_csum_datagram(frag_iter,
669 *csump = csum_block_add(*csump, csum2, pos);
670 if ((len -= copy) == 0)
685 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
689 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
691 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
692 netdev_rx_csum_fault(skb->dev);
693 skb->ip_summed = CHECKSUM_UNNECESSARY;
697 EXPORT_SYMBOL(__skb_checksum_complete_head);
699 __sum16 __skb_checksum_complete(struct sk_buff *skb)
701 return __skb_checksum_complete_head(skb, skb->len);
703 EXPORT_SYMBOL(__skb_checksum_complete);
706 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
708 * @hlen: hardware length
711 * Caller _must_ check that skb will fit to this iovec.
713 * Returns: 0 - success.
714 * -EINVAL - checksum failure.
715 * -EFAULT - fault during copy. Beware, in this case iovec
718 int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
719 int hlen, struct iovec *iov)
722 int chunk = skb->len - hlen;
727 /* Skip filled elements.
728 * Pretty silly, look at memcpy_toiovec, though 8)
730 while (!iov->iov_len)
733 if (iov->iov_len < chunk) {
734 if (__skb_checksum_complete(skb))
736 if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
739 csum = csum_partial(skb->data, hlen, skb->csum);
740 if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
745 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
746 netdev_rx_csum_fault(skb->dev);
747 iov->iov_len -= chunk;
748 iov->iov_base += chunk;
756 EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
759 * datagram_poll - generic datagram poll
764 * Datagram poll: Again totally generic. This also handles
765 * sequenced packet sockets providing the socket receive queue
766 * is only ever holding data ready to receive.
768 * Note: when you _don't_ use this routine for this protocol,
769 * and you use a different write policy from sock_writeable()
770 * then please supply your own write_space callback.
772 unsigned int datagram_poll(struct file *file, struct socket *sock,
775 struct sock *sk = sock->sk;
778 sock_poll_wait(file, sk_sleep(sk), wait);
781 /* exceptional events? */
782 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
784 if (sk->sk_shutdown & RCV_SHUTDOWN)
785 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
786 if (sk->sk_shutdown == SHUTDOWN_MASK)
790 if (!skb_queue_empty(&sk->sk_receive_queue))
791 mask |= POLLIN | POLLRDNORM;
793 /* Connection-based need to check for termination and startup */
794 if (connection_based(sk)) {
795 if (sk->sk_state == TCP_CLOSE)
797 /* connection hasn't started yet? */
798 if (sk->sk_state == TCP_SYN_SENT)
803 if (sock_writeable(sk))
804 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
806 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
810 EXPORT_SYMBOL(datagram_poll);