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);
132 * __skb_recv_datagram - Receive a datagram skbuff
135 * @off: an offset in bytes to peek skb from. Returns an offset
136 * within an skb where data actually starts
137 * @peeked: returns non-zero if this packet has been seen before
138 * @err: error code returned
140 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
141 * and possible races. This replaces identical code in packet, raw and
142 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
143 * the long standing peek and read race for datagram sockets. If you
144 * alter this routine remember it must be re-entrant.
146 * This function will lock the socket if a skb is returned, so the caller
147 * needs to unlock the socket in that case (usually by calling
150 * * It does not lock socket since today. This function is
151 * * free of race conditions. This measure should/can improve
152 * * significantly datagram socket latencies at high loads,
153 * * when data copying to user space takes lots of time.
154 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
158 * The order of the tests when we find no data waiting are specified
159 * quite explicitly by POSIX 1003.1g, don't change them without having
160 * the standard around please.
162 struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags,
163 int *peeked, int *off, int *err)
168 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
170 int error = sock_error(sk);
175 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
178 /* Again only user level code calls this function, so nothing
179 * interrupt level will suddenly eat the receive_queue.
181 * Look at current nfs client by the way...
182 * However, this function was correct in any case. 8)
184 unsigned long cpu_flags;
185 struct sk_buff_head *queue = &sk->sk_receive_queue;
187 spin_lock_irqsave(&queue->lock, cpu_flags);
188 skb_queue_walk(queue, skb) {
189 *peeked = skb->peeked;
190 if (flags & MSG_PEEK) {
191 if (*off >= skb->len) {
196 atomic_inc(&skb->users);
198 __skb_unlink(skb, queue);
200 spin_unlock_irqrestore(&queue->lock, cpu_flags);
203 spin_unlock_irqrestore(&queue->lock, cpu_flags);
205 /* User doesn't want to wait */
210 } while (!wait_for_packet(sk, err, &timeo));
218 EXPORT_SYMBOL(__skb_recv_datagram);
220 struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
221 int noblock, int *err)
225 return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
228 EXPORT_SYMBOL(skb_recv_datagram);
230 void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
233 sk_mem_reclaim_partial(sk);
235 EXPORT_SYMBOL(skb_free_datagram);
237 void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb)
241 if (likely(atomic_read(&skb->users) == 1))
243 else if (likely(!atomic_dec_and_test(&skb->users)))
246 slow = lock_sock_fast(sk);
248 sk_mem_reclaim_partial(sk);
249 unlock_sock_fast(sk, slow);
251 /* skb is now orphaned, can be freed outside of locked section */
252 trace_kfree_skb(skb, skb_free_datagram_locked);
255 EXPORT_SYMBOL(skb_free_datagram_locked);
258 * skb_kill_datagram - Free a datagram skbuff forcibly
260 * @skb: datagram skbuff
263 * This function frees a datagram skbuff that was received by
264 * skb_recv_datagram. The flags argument must match the one
265 * used for skb_recv_datagram.
267 * If the MSG_PEEK flag is set, and the packet is still on the
268 * receive queue of the socket, it will be taken off the queue
269 * before it is freed.
271 * This function currently only disables BH when acquiring the
272 * sk_receive_queue lock. Therefore it must not be used in a
273 * context where that lock is acquired in an IRQ context.
275 * It returns 0 if the packet was removed by us.
278 int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
282 if (flags & MSG_PEEK) {
284 spin_lock_bh(&sk->sk_receive_queue.lock);
285 if (skb == skb_peek(&sk->sk_receive_queue)) {
286 __skb_unlink(skb, &sk->sk_receive_queue);
287 atomic_dec(&skb->users);
290 spin_unlock_bh(&sk->sk_receive_queue.lock);
294 atomic_inc(&sk->sk_drops);
295 sk_mem_reclaim_partial(sk);
299 EXPORT_SYMBOL(skb_kill_datagram);
302 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
303 * @skb: buffer to copy
304 * @offset: offset in the buffer to start copying from
305 * @to: io vector to copy to
306 * @len: amount of data to copy from buffer to iovec
308 * Note: the iovec is modified during the copy.
310 int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
311 struct iovec *to, int len)
313 int start = skb_headlen(skb);
314 int i, copy = start - offset;
315 struct sk_buff *frag_iter;
317 trace_skb_copy_datagram_iovec(skb, len);
323 if (memcpy_toiovec(to, skb->data + offset, copy))
325 if ((len -= copy) == 0)
330 /* Copy paged appendix. Hmm... why does this look so complicated? */
331 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
333 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
335 WARN_ON(start > offset + len);
337 end = start + skb_frag_size(frag);
338 if ((copy = end - offset) > 0) {
341 struct page *page = skb_frag_page(frag);
346 err = memcpy_toiovec(to, vaddr + frag->page_offset +
347 offset - start, copy);
358 skb_walk_frags(skb, frag_iter) {
361 WARN_ON(start > offset + len);
363 end = start + frag_iter->len;
364 if ((copy = end - offset) > 0) {
367 if (skb_copy_datagram_iovec(frag_iter,
371 if ((len -= copy) == 0)
383 EXPORT_SYMBOL(skb_copy_datagram_iovec);
386 * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
387 * @skb: buffer to copy
388 * @offset: offset in the buffer to start copying from
389 * @to: io vector to copy to
390 * @to_offset: offset in the io vector to start copying to
391 * @len: amount of data to copy from buffer to iovec
393 * Returns 0 or -EFAULT.
394 * Note: the iovec is not modified during the copy.
396 int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset,
397 const struct iovec *to, int to_offset,
400 int start = skb_headlen(skb);
401 int i, copy = start - offset;
402 struct sk_buff *frag_iter;
408 if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy))
410 if ((len -= copy) == 0)
416 /* Copy paged appendix. Hmm... why does this look so complicated? */
417 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
419 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
421 WARN_ON(start > offset + len);
423 end = start + skb_frag_size(frag);
424 if ((copy = end - offset) > 0) {
427 struct page *page = skb_frag_page(frag);
432 err = memcpy_toiovecend(to, vaddr + frag->page_offset +
433 offset - start, to_offset, copy);
445 skb_walk_frags(skb, frag_iter) {
448 WARN_ON(start > offset + len);
450 end = start + frag_iter->len;
451 if ((copy = end - offset) > 0) {
454 if (skb_copy_datagram_const_iovec(frag_iter,
459 if ((len -= copy) == 0)
472 EXPORT_SYMBOL(skb_copy_datagram_const_iovec);
475 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
476 * @skb: buffer to copy
477 * @offset: offset in the buffer to start copying to
478 * @from: io vector to copy to
479 * @from_offset: offset in the io vector to start copying from
480 * @len: amount of data to copy to buffer from iovec
482 * Returns 0 or -EFAULT.
483 * Note: the iovec is not modified during the copy.
485 int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
486 const struct iovec *from, int from_offset,
489 int start = skb_headlen(skb);
490 int i, copy = start - offset;
491 struct sk_buff *frag_iter;
497 if (memcpy_fromiovecend(skb->data + offset, from, from_offset,
500 if ((len -= copy) == 0)
506 /* Copy paged appendix. Hmm... why does this look so complicated? */
507 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
509 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
511 WARN_ON(start > offset + len);
513 end = start + skb_frag_size(frag);
514 if ((copy = end - offset) > 0) {
517 struct page *page = skb_frag_page(frag);
522 err = memcpy_fromiovecend(vaddr + frag->page_offset +
524 from, from_offset, copy);
537 skb_walk_frags(skb, frag_iter) {
540 WARN_ON(start > offset + len);
542 end = start + frag_iter->len;
543 if ((copy = end - offset) > 0) {
546 if (skb_copy_datagram_from_iovec(frag_iter,
552 if ((len -= copy) == 0)
565 EXPORT_SYMBOL(skb_copy_datagram_from_iovec);
567 static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
568 u8 __user *to, int len,
571 int start = skb_headlen(skb);
572 int i, copy = start - offset;
573 struct sk_buff *frag_iter;
581 *csump = csum_and_copy_to_user(skb->data + offset, to, copy,
585 if ((len -= copy) == 0)
592 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
594 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
596 WARN_ON(start > offset + len);
598 end = start + skb_frag_size(frag);
599 if ((copy = end - offset) > 0) {
603 struct page *page = skb_frag_page(frag);
608 csum2 = csum_and_copy_to_user(vaddr +
615 *csump = csum_block_add(*csump, csum2, pos);
625 skb_walk_frags(skb, frag_iter) {
628 WARN_ON(start > offset + len);
630 end = start + frag_iter->len;
631 if ((copy = end - offset) > 0) {
635 if (skb_copy_and_csum_datagram(frag_iter,
640 *csump = csum_block_add(*csump, csum2, pos);
641 if ((len -= copy) == 0)
656 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
660 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
662 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
663 netdev_rx_csum_fault(skb->dev);
664 skb->ip_summed = CHECKSUM_UNNECESSARY;
668 EXPORT_SYMBOL(__skb_checksum_complete_head);
670 __sum16 __skb_checksum_complete(struct sk_buff *skb)
672 return __skb_checksum_complete_head(skb, skb->len);
674 EXPORT_SYMBOL(__skb_checksum_complete);
677 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
679 * @hlen: hardware length
682 * Caller _must_ check that skb will fit to this iovec.
684 * Returns: 0 - success.
685 * -EINVAL - checksum failure.
686 * -EFAULT - fault during copy. Beware, in this case iovec
689 int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
690 int hlen, struct iovec *iov)
693 int chunk = skb->len - hlen;
698 /* Skip filled elements.
699 * Pretty silly, look at memcpy_toiovec, though 8)
701 while (!iov->iov_len)
704 if (iov->iov_len < chunk) {
705 if (__skb_checksum_complete(skb))
707 if (skb_copy_datagram_iovec(skb, hlen, iov, chunk))
710 csum = csum_partial(skb->data, hlen, skb->csum);
711 if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base,
716 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
717 netdev_rx_csum_fault(skb->dev);
718 iov->iov_len -= chunk;
719 iov->iov_base += chunk;
727 EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec);
730 * datagram_poll - generic datagram poll
735 * Datagram poll: Again totally generic. This also handles
736 * sequenced packet sockets providing the socket receive queue
737 * is only ever holding data ready to receive.
739 * Note: when you _don't_ use this routine for this protocol,
740 * and you use a different write policy from sock_writeable()
741 * then please supply your own write_space callback.
743 unsigned int datagram_poll(struct file *file, struct socket *sock,
746 struct sock *sk = sock->sk;
749 sock_poll_wait(file, sk_sleep(sk), wait);
752 /* exceptional events? */
753 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
755 if (sk->sk_shutdown & RCV_SHUTDOWN)
756 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
757 if (sk->sk_shutdown == SHUTDOWN_MASK)
761 if (!skb_queue_empty(&sk->sk_receive_queue))
762 mask |= POLLIN | POLLRDNORM;
764 /* Connection-based need to check for termination and startup */
765 if (connection_based(sk)) {
766 if (sk->sk_state == TCP_CLOSE)
768 /* connection hasn't started yet? */
769 if (sk->sk_state == TCP_SYN_SENT)
774 if (sock_writeable(sk))
775 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
777 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
781 EXPORT_SYMBOL(datagram_poll);