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>
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>
52 #include <net/protocol.h>
53 #include <linux/skbuff.h>
55 #include <net/checksum.h>
57 #include <net/tcp_states.h>
58 #include <trace/events/skb.h>
61 * Is a socket 'connection oriented' ?
63 static inline int connection_based(struct sock *sk)
65 return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
68 static int receiver_wake_function(wait_queue_t *wait, unsigned int mode, int sync,
71 unsigned long bits = (unsigned long)key;
74 * Avoid a wakeup if event not interesting for us
76 if (bits && !(bits & (POLLIN | POLLERR)))
78 return autoremove_wake_function(wait, mode, sync, key);
81 * Wait for the last received packet to be different from skb
83 static int wait_for_more_packets(struct sock *sk, int *err, long *timeo_p,
84 const struct sk_buff *skb)
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 (sk->sk_receive_queue.prev != skb)
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 * @off: an offset in bytes to peek skb from. Returns an offset
166 * within an skb where data actually starts
167 * @err: error code returned
169 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
170 * and possible races. This replaces identical code in packet, raw and
171 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
172 * the long standing peek and read race for datagram sockets. If you
173 * alter this routine remember it must be re-entrant.
175 * This function will lock the socket if a skb is returned, so the caller
176 * needs to unlock the socket in that case (usually by calling
179 * * It does not lock socket since today. This function is
180 * * free of race conditions. This measure should/can improve
181 * * significantly datagram socket latencies at high loads,
182 * * when data copying to user space takes lots of time.
183 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
187 * The order of the tests when we find no data waiting are specified
188 * quite explicitly by POSIX 1003.1g, don't change them without having
189 * the standard around please.
191 struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
192 int *peeked, int *off, int *err)
194 struct sk_buff_head *queue = &sk->sk_receive_queue;
195 struct sk_buff *skb, *last;
196 unsigned long cpu_flags;
199 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
201 int error = sock_error(sk);
206 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
209 /* Again only user level code calls this function, so nothing
210 * interrupt level will suddenly eat the receive_queue.
212 * Look at current nfs client by the way...
213 * However, this function was correct in any case. 8)
217 last = (struct sk_buff *)queue;
218 spin_lock_irqsave(&queue->lock, cpu_flags);
219 skb_queue_walk(queue, skb) {
221 *peeked = skb->peeked;
222 if (flags & MSG_PEEK) {
223 if (_off >= skb->len && (skb->len || _off ||
229 error = skb_set_peeked(skb);
233 atomic_inc(&skb->users);
235 __skb_unlink(skb, queue);
237 spin_unlock_irqrestore(&queue->lock, cpu_flags);
241 spin_unlock_irqrestore(&queue->lock, cpu_flags);
243 /* User doesn't want to wait */
248 } while (!wait_for_more_packets(sk, err, &timeo, last));
253 spin_unlock_irqrestore(&queue->lock, cpu_flags);
258 EXPORT_SYMBOL(__skb_recv_datagram);
260 struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
261 int noblock, int *err)
265 return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
268 EXPORT_SYMBOL(skb_recv_datagram);
270 void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
273 sk_mem_reclaim_partial(sk);
275 EXPORT_SYMBOL(skb_free_datagram);
277 void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb)
281 if (likely(atomic_read(&skb->users) == 1))
283 else if (likely(!atomic_dec_and_test(&skb->users)))
286 slow = lock_sock_fast(sk);
288 sk_mem_reclaim_partial(sk);
289 unlock_sock_fast(sk, slow);
291 /* skb is now orphaned, can be freed outside of locked section */
294 EXPORT_SYMBOL(skb_free_datagram_locked);
297 * skb_kill_datagram - Free a datagram skbuff forcibly
299 * @skb: datagram skbuff
302 * This function frees a datagram skbuff that was received by
303 * skb_recv_datagram. The flags argument must match the one
304 * used for skb_recv_datagram.
306 * If the MSG_PEEK flag is set, and the packet is still on the
307 * receive queue of the socket, it will be taken off the queue
308 * before it is freed.
310 * This function currently only disables BH when acquiring the
311 * sk_receive_queue lock. Therefore it must not be used in a
312 * context where that lock is acquired in an IRQ context.
314 * It returns 0 if the packet was removed by us.
317 int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
321 if (flags & MSG_PEEK) {
323 spin_lock_bh(&sk->sk_receive_queue.lock);
324 if (skb == skb_peek(&sk->sk_receive_queue)) {
325 __skb_unlink(skb, &sk->sk_receive_queue);
326 atomic_dec(&skb->users);
329 spin_unlock_bh(&sk->sk_receive_queue.lock);
333 atomic_inc(&sk->sk_drops);
334 sk_mem_reclaim_partial(sk);
338 EXPORT_SYMBOL(skb_kill_datagram);
341 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
342 * @skb: buffer to copy
343 * @offset: offset in the buffer to start copying from
344 * @to: io vector to copy to
345 * @len: amount of data to copy from buffer to iovec
347 * Note: the iovec is modified during the copy.
349 int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
350 struct iovec *to, int len)
352 int start = skb_headlen(skb);
353 int i, copy = start - offset;
354 struct sk_buff *frag_iter;
356 trace_skb_copy_datagram_iovec(skb, len);
362 if (memcpy_toiovec(to, skb->data + offset, copy))
364 if ((len -= copy) == 0)
369 /* Copy paged appendix. Hmm... why does this look so complicated? */
370 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
372 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
374 WARN_ON(start > offset + len);
376 end = start + skb_frag_size(frag);
377 if ((copy = end - offset) > 0) {
380 struct page *page = skb_frag_page(frag);
385 err = memcpy_toiovec(to, vaddr + frag->page_offset +
386 offset - start, copy);
397 skb_walk_frags(skb, frag_iter) {
400 WARN_ON(start > offset + len);
402 end = start + frag_iter->len;
403 if ((copy = end - offset) > 0) {
406 if (skb_copy_datagram_iovec(frag_iter,
410 if ((len -= copy) == 0)
422 EXPORT_SYMBOL(skb_copy_datagram_iovec);
425 * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
426 * @skb: buffer to copy
427 * @offset: offset in the buffer to start copying from
428 * @to: io vector to copy to
429 * @to_offset: offset in the io vector to start copying to
430 * @len: amount of data to copy from buffer to iovec
432 * Returns 0 or -EFAULT.
433 * Note: the iovec is not modified during the copy.
435 int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset,
436 const struct iovec *to, int to_offset,
439 int start = skb_headlen(skb);
440 int i, copy = start - offset;
441 struct sk_buff *frag_iter;
447 if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy))
449 if ((len -= copy) == 0)
455 /* Copy paged appendix. Hmm... why does this look so complicated? */
456 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
458 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
460 WARN_ON(start > offset + len);
462 end = start + skb_frag_size(frag);
463 if ((copy = end - offset) > 0) {
466 struct page *page = skb_frag_page(frag);
471 err = memcpy_toiovecend(to, vaddr + frag->page_offset +
472 offset - start, to_offset, copy);
484 skb_walk_frags(skb, frag_iter) {
487 WARN_ON(start > offset + len);
489 end = start + frag_iter->len;
490 if ((copy = end - offset) > 0) {
493 if (skb_copy_datagram_const_iovec(frag_iter,
498 if ((len -= copy) == 0)
511 EXPORT_SYMBOL(skb_copy_datagram_const_iovec);
514 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
515 * @skb: buffer to copy
516 * @offset: offset in the buffer to start copying to
517 * @from: io vector to copy to
518 * @from_offset: offset in the io vector to start copying from
519 * @len: amount of data to copy to buffer from iovec
521 * Returns 0 or -EFAULT.
522 * Note: the iovec is not modified during the copy.
524 int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
525 const struct iovec *from, int from_offset,
528 int start = skb_headlen(skb);
529 int i, copy = start - offset;
530 struct sk_buff *frag_iter;
536 if (memcpy_fromiovecend(skb->data + offset, from, from_offset,
539 if ((len -= copy) == 0)
545 /* Copy paged appendix. Hmm... why does this look so complicated? */
546 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
548 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
550 WARN_ON(start > offset + len);
552 end = start + skb_frag_size(frag);
553 if ((copy = end - offset) > 0) {
556 struct page *page = skb_frag_page(frag);
561 err = memcpy_fromiovecend(vaddr + frag->page_offset +
563 from, from_offset, copy);
576 skb_walk_frags(skb, frag_iter) {
579 WARN_ON(start > offset + len);
581 end = start + frag_iter->len;
582 if ((copy = end - offset) > 0) {
585 if (skb_copy_datagram_from_iovec(frag_iter,
591 if ((len -= copy) == 0)
604 EXPORT_SYMBOL(skb_copy_datagram_from_iovec);
606 static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
607 u8 __user *to, int len,
610 int start = skb_headlen(skb);
611 int i, copy = start - offset;
612 struct sk_buff *frag_iter;
620 *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
624 if ((len -= copy) == 0)
631 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
633 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
635 WARN_ON(start > offset + len);
637 end = start + skb_frag_size(frag);
638 if ((copy = end - offset) > 0) {
642 struct page *page = skb_frag_page(frag);
647 csum2 = csum_and_copy_to_user(vaddr +
654 *csump = csum_block_add(*csump, csum2, pos);
664 skb_walk_frags(skb, frag_iter) {
667 WARN_ON(start > offset + len);
669 end = start + frag_iter->len;
670 if ((copy = end - offset) > 0) {
674 if (skb_copy_and_csum_datagram(frag_iter,
679 *csump = csum_block_add(*csump, csum2, pos);
680 if ((len -= copy) == 0)
695 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
699 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
701 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
702 netdev_rx_csum_fault(skb->dev);
703 if (!skb_shared(skb))
704 skb->ip_summed = CHECKSUM_UNNECESSARY;
708 EXPORT_SYMBOL(__skb_checksum_complete_head);
710 __sum16 __skb_checksum_complete(struct sk_buff *skb)
712 return __skb_checksum_complete_head(skb, skb->len);
714 EXPORT_SYMBOL(__skb_checksum_complete);
717 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
719 * @hlen: hardware length
722 * Caller _must_ check that skb will fit to this iovec.
724 * Returns: 0 - success.
725 * -EINVAL - checksum failure.
726 * -EFAULT - fault during copy. Beware, in this case iovec
729 int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
730 int hlen, struct iovec *iov)
733 int chunk = skb->len - hlen;
738 /* Skip filled elements.
739 * Pretty silly, look at memcpy_toiovec, though 8)
741 while (!iov->iov_len)
744 if (iov->iov_len < chunk) {
745 if (__skb_checksum_complete(skb))
747 if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
750 csum = csum_partial(skb->data, hlen, skb->csum);
751 if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
756 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
757 netdev_rx_csum_fault(skb->dev);
758 iov->iov_len -= chunk;
759 iov->iov_base += chunk;
767 EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
770 * datagram_poll - generic datagram poll
775 * Datagram poll: Again totally generic. This also handles
776 * sequenced packet sockets providing the socket receive queue
777 * is only ever holding data ready to receive.
779 * Note: when you _don't_ use this routine for this protocol,
780 * and you use a different write policy from sock_writeable()
781 * then please supply your own write_space callback.
783 unsigned int datagram_poll(struct file *file, struct socket *sock,
786 struct sock *sk = sock->sk;
789 sock_poll_wait(file, sk_sleep(sk), wait);
792 /* exceptional events? */
793 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
795 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
797 if (sk->sk_shutdown & RCV_SHUTDOWN)
798 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
799 if (sk->sk_shutdown == SHUTDOWN_MASK)
803 if (!skb_queue_empty(&sk->sk_receive_queue))
804 mask |= POLLIN | POLLRDNORM;
806 /* Connection-based need to check for termination and startup */
807 if (connection_based(sk)) {
808 if (sk->sk_state == TCP_CLOSE)
810 /* connection hasn't started yet? */
811 if (sk->sk_state == TCP_SYN_SENT)
816 if (sock_writeable(sk))
817 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
819 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
823 EXPORT_SYMBOL(datagram_poll);