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
);
83 static int wait_for_packet(struct sock
*sk
, int *err
, long *timeo_p
)
86 DEFINE_WAIT_FUNC(wait
, receiver_wake_function
);
88 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
91 error
= sock_error(sk
);
95 if (!skb_queue_empty(&sk
->sk_receive_queue
))
98 /* Socket shut down? */
99 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
102 /* Sequenced packets can come disconnected.
103 * If so we report the problem
106 if (connection_based(sk
) &&
107 !(sk
->sk_state
== TCP_ESTABLISHED
|| sk
->sk_state
== TCP_LISTEN
))
111 if (signal_pending(current
))
115 *timeo_p
= schedule_timeout(*timeo_p
);
117 finish_wait(sk_sleep(sk
), &wait
);
120 error
= sock_intr_errno(*timeo_p
);
131 * __skb_recv_datagram - Receive a datagram skbuff
134 * @off: an offset in bytes to peek skb from. Returns an offset
135 * within an skb where data actually starts
136 * @peeked: returns non-zero if this packet has been seen before
137 * @err: error code returned
139 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
140 * and possible races. This replaces identical code in packet, raw and
141 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
142 * the long standing peek and read race for datagram sockets. If you
143 * alter this routine remember it must be re-entrant.
145 * This function will lock the socket if a skb is returned, so the caller
146 * needs to unlock the socket in that case (usually by calling
149 * * It does not lock socket since today. This function is
150 * * free of race conditions. This measure should/can improve
151 * * significantly datagram socket latencies at high loads,
152 * * when data copying to user space takes lots of time.
153 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
157 * The order of the tests when we find no data waiting are specified
158 * quite explicitly by POSIX 1003.1g, don't change them without having
159 * the standard around please.
161 struct sk_buff
*__skb_recv_datagram(struct sock
*sk
, unsigned int flags
,
162 int *peeked
, int *off
, int *err
)
167 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
169 int error
= sock_error(sk
);
174 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
177 /* Again only user level code calls this function, so nothing
178 * interrupt level will suddenly eat the receive_queue.
180 * Look at current nfs client by the way...
181 * However, this function was correct in any case. 8)
183 unsigned long cpu_flags
;
184 struct sk_buff_head
*queue
= &sk
->sk_receive_queue
;
186 spin_lock_irqsave(&queue
->lock
, cpu_flags
);
187 skb_queue_walk(queue
, skb
) {
188 *peeked
= skb
->peeked
;
189 if (flags
& MSG_PEEK
) {
190 if (*off
>= skb
->len
) {
195 atomic_inc(&skb
->users
);
197 __skb_unlink(skb
, queue
);
199 spin_unlock_irqrestore(&queue
->lock
, cpu_flags
);
202 spin_unlock_irqrestore(&queue
->lock
, cpu_flags
);
204 /* User doesn't want to wait */
209 } while (!wait_for_packet(sk
, err
, &timeo
));
217 EXPORT_SYMBOL(__skb_recv_datagram
);
219 struct sk_buff
*skb_recv_datagram(struct sock
*sk
, unsigned int flags
,
220 int noblock
, int *err
)
224 return __skb_recv_datagram(sk
, flags
| (noblock
? MSG_DONTWAIT
: 0),
227 EXPORT_SYMBOL(skb_recv_datagram
);
229 void skb_free_datagram(struct sock
*sk
, struct sk_buff
*skb
)
232 sk_mem_reclaim_partial(sk
);
234 EXPORT_SYMBOL(skb_free_datagram
);
236 void skb_free_datagram_locked(struct sock
*sk
, struct sk_buff
*skb
)
240 if (likely(atomic_read(&skb
->users
) == 1))
242 else if (likely(!atomic_dec_and_test(&skb
->users
)))
245 slow
= lock_sock_fast(sk
);
247 sk_mem_reclaim_partial(sk
);
248 unlock_sock_fast(sk
, slow
);
250 /* skb is now orphaned, can be freed outside of locked section */
251 trace_kfree_skb(skb
, skb_free_datagram_locked
);
254 EXPORT_SYMBOL(skb_free_datagram_locked
);
257 * skb_kill_datagram - Free a datagram skbuff forcibly
259 * @skb: datagram skbuff
262 * This function frees a datagram skbuff that was received by
263 * skb_recv_datagram. The flags argument must match the one
264 * used for skb_recv_datagram.
266 * If the MSG_PEEK flag is set, and the packet is still on the
267 * receive queue of the socket, it will be taken off the queue
268 * before it is freed.
270 * This function currently only disables BH when acquiring the
271 * sk_receive_queue lock. Therefore it must not be used in a
272 * context where that lock is acquired in an IRQ context.
274 * It returns 0 if the packet was removed by us.
277 int skb_kill_datagram(struct sock
*sk
, struct sk_buff
*skb
, unsigned int flags
)
281 if (flags
& MSG_PEEK
) {
283 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
284 if (skb
== skb_peek(&sk
->sk_receive_queue
)) {
285 __skb_unlink(skb
, &sk
->sk_receive_queue
);
286 atomic_dec(&skb
->users
);
289 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
293 atomic_inc(&sk
->sk_drops
);
294 sk_mem_reclaim_partial(sk
);
298 EXPORT_SYMBOL(skb_kill_datagram
);
301 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
302 * @skb: buffer to copy
303 * @offset: offset in the buffer to start copying from
304 * @to: io vector to copy to
305 * @len: amount of data to copy from buffer to iovec
307 * Note: the iovec is modified during the copy.
309 int skb_copy_datagram_iovec(const struct sk_buff
*skb
, int offset
,
310 struct iovec
*to
, int len
)
312 int start
= skb_headlen(skb
);
313 int i
, copy
= start
- offset
;
314 struct sk_buff
*frag_iter
;
316 trace_skb_copy_datagram_iovec(skb
, len
);
322 if (memcpy_toiovec(to
, skb
->data
+ offset
, copy
))
324 if ((len
-= copy
) == 0)
329 /* Copy paged appendix. Hmm... why does this look so complicated? */
330 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
332 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
334 WARN_ON(start
> offset
+ len
);
336 end
= start
+ skb_frag_size(frag
);
337 if ((copy
= end
- offset
) > 0) {
340 struct page
*page
= skb_frag_page(frag
);
345 err
= memcpy_toiovec(to
, vaddr
+ frag
->page_offset
+
346 offset
- start
, copy
);
357 skb_walk_frags(skb
, frag_iter
) {
360 WARN_ON(start
> offset
+ len
);
362 end
= start
+ frag_iter
->len
;
363 if ((copy
= end
- offset
) > 0) {
366 if (skb_copy_datagram_iovec(frag_iter
,
370 if ((len
-= copy
) == 0)
382 EXPORT_SYMBOL(skb_copy_datagram_iovec
);
385 * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
386 * @skb: buffer to copy
387 * @offset: offset in the buffer to start copying from
388 * @to: io vector to copy to
389 * @to_offset: offset in the io vector to start copying to
390 * @len: amount of data to copy from buffer to iovec
392 * Returns 0 or -EFAULT.
393 * Note: the iovec is not modified during the copy.
395 int skb_copy_datagram_const_iovec(const struct sk_buff
*skb
, int offset
,
396 const struct iovec
*to
, int to_offset
,
399 int start
= skb_headlen(skb
);
400 int i
, copy
= start
- offset
;
401 struct sk_buff
*frag_iter
;
407 if (memcpy_toiovecend(to
, skb
->data
+ offset
, to_offset
, copy
))
409 if ((len
-= copy
) == 0)
415 /* Copy paged appendix. Hmm... why does this look so complicated? */
416 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
418 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
420 WARN_ON(start
> offset
+ len
);
422 end
= start
+ skb_frag_size(frag
);
423 if ((copy
= end
- offset
) > 0) {
426 struct page
*page
= skb_frag_page(frag
);
431 err
= memcpy_toiovecend(to
, vaddr
+ frag
->page_offset
+
432 offset
- start
, to_offset
, copy
);
444 skb_walk_frags(skb
, frag_iter
) {
447 WARN_ON(start
> offset
+ len
);
449 end
= start
+ frag_iter
->len
;
450 if ((copy
= end
- offset
) > 0) {
453 if (skb_copy_datagram_const_iovec(frag_iter
,
458 if ((len
-= copy
) == 0)
471 EXPORT_SYMBOL(skb_copy_datagram_const_iovec
);
474 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
475 * @skb: buffer to copy
476 * @offset: offset in the buffer to start copying to
477 * @from: io vector to copy to
478 * @from_offset: offset in the io vector to start copying from
479 * @len: amount of data to copy to buffer from iovec
481 * Returns 0 or -EFAULT.
482 * Note: the iovec is not modified during the copy.
484 int skb_copy_datagram_from_iovec(struct sk_buff
*skb
, int offset
,
485 const struct iovec
*from
, int from_offset
,
488 int start
= skb_headlen(skb
);
489 int i
, copy
= start
- offset
;
490 struct sk_buff
*frag_iter
;
496 if (memcpy_fromiovecend(skb
->data
+ offset
, from
, from_offset
,
499 if ((len
-= copy
) == 0)
505 /* Copy paged appendix. Hmm... why does this look so complicated? */
506 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
508 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
510 WARN_ON(start
> offset
+ len
);
512 end
= start
+ skb_frag_size(frag
);
513 if ((copy
= end
- offset
) > 0) {
516 struct page
*page
= skb_frag_page(frag
);
521 err
= memcpy_fromiovecend(vaddr
+ frag
->page_offset
+
523 from
, from_offset
, copy
);
536 skb_walk_frags(skb
, frag_iter
) {
539 WARN_ON(start
> offset
+ len
);
541 end
= start
+ frag_iter
->len
;
542 if ((copy
= end
- offset
) > 0) {
545 if (skb_copy_datagram_from_iovec(frag_iter
,
551 if ((len
-= copy
) == 0)
564 EXPORT_SYMBOL(skb_copy_datagram_from_iovec
);
566 static int skb_copy_and_csum_datagram(const struct sk_buff
*skb
, int offset
,
567 u8 __user
*to
, int len
,
570 int start
= skb_headlen(skb
);
571 int i
, copy
= start
- offset
;
572 struct sk_buff
*frag_iter
;
580 *csump
= csum_and_copy_to_user(skb
->data
+ offset
, to
, copy
,
584 if ((len
-= copy
) == 0)
591 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
593 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
595 WARN_ON(start
> offset
+ len
);
597 end
= start
+ skb_frag_size(frag
);
598 if ((copy
= end
- offset
) > 0) {
602 struct page
*page
= skb_frag_page(frag
);
607 csum2
= csum_and_copy_to_user(vaddr
+
614 *csump
= csum_block_add(*csump
, csum2
, pos
);
624 skb_walk_frags(skb
, frag_iter
) {
627 WARN_ON(start
> offset
+ len
);
629 end
= start
+ frag_iter
->len
;
630 if ((copy
= end
- offset
) > 0) {
634 if (skb_copy_and_csum_datagram(frag_iter
,
639 *csump
= csum_block_add(*csump
, csum2
, pos
);
640 if ((len
-= copy
) == 0)
655 __sum16
__skb_checksum_complete_head(struct sk_buff
*skb
, int len
)
659 sum
= csum_fold(skb_checksum(skb
, 0, len
, skb
->csum
));
661 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
662 netdev_rx_csum_fault(skb
->dev
);
663 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
667 EXPORT_SYMBOL(__skb_checksum_complete_head
);
669 __sum16
__skb_checksum_complete(struct sk_buff
*skb
)
671 return __skb_checksum_complete_head(skb
, skb
->len
);
673 EXPORT_SYMBOL(__skb_checksum_complete
);
676 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
678 * @hlen: hardware length
681 * Caller _must_ check that skb will fit to this iovec.
683 * Returns: 0 - success.
684 * -EINVAL - checksum failure.
685 * -EFAULT - fault during copy. Beware, in this case iovec
688 int skb_copy_and_csum_datagram_iovec(struct sk_buff
*skb
,
689 int hlen
, struct iovec
*iov
)
692 int chunk
= skb
->len
- hlen
;
697 /* Skip filled elements.
698 * Pretty silly, look at memcpy_toiovec, though 8)
700 while (!iov
->iov_len
)
703 if (iov
->iov_len
< chunk
) {
704 if (__skb_checksum_complete(skb
))
706 if (skb_copy_datagram_iovec(skb
, hlen
, iov
, chunk
))
709 csum
= csum_partial(skb
->data
, hlen
, skb
->csum
);
710 if (skb_copy_and_csum_datagram(skb
, hlen
, iov
->iov_base
,
715 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
716 netdev_rx_csum_fault(skb
->dev
);
717 iov
->iov_len
-= chunk
;
718 iov
->iov_base
+= chunk
;
726 EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec
);
729 * datagram_poll - generic datagram poll
734 * Datagram poll: Again totally generic. This also handles
735 * sequenced packet sockets providing the socket receive queue
736 * is only ever holding data ready to receive.
738 * Note: when you _don't_ use this routine for this protocol,
739 * and you use a different write policy from sock_writeable()
740 * then please supply your own write_space callback.
742 unsigned int datagram_poll(struct file
*file
, struct socket
*sock
,
745 struct sock
*sk
= sock
->sk
;
748 sock_poll_wait(file
, sk_sleep(sk
), wait
);
751 /* exceptional events? */
752 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
754 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
755 mask
|= POLLRDHUP
| POLLIN
| POLLRDNORM
;
756 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
760 if (!skb_queue_empty(&sk
->sk_receive_queue
))
761 mask
|= POLLIN
| POLLRDNORM
;
763 /* Connection-based need to check for termination and startup */
764 if (connection_based(sk
)) {
765 if (sk
->sk_state
== TCP_CLOSE
)
767 /* connection hasn't started yet? */
768 if (sk
->sk_state
== TCP_SYN_SENT
)
773 if (sock_writeable(sk
))
774 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
776 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
780 EXPORT_SYMBOL(datagram_poll
);