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>
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 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
->sk_sleep
, &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
->sk_sleep
, &wait
);
120 error
= sock_intr_errno(*timeo_p
);
131 * __skb_recv_datagram - Receive a datagram skbuff
134 * @peeked: returns non-zero if this packet has been seen before
135 * @err: error code returned
137 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
138 * and possible races. This replaces identical code in packet, raw and
139 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
140 * the long standing peek and read race for datagram sockets. If you
141 * alter this routine remember it must be re-entrant.
143 * This function will lock the socket if a skb is returned, so the caller
144 * needs to unlock the socket in that case (usually by calling
147 * * It does not lock socket since today. This function is
148 * * free of race conditions. This measure should/can improve
149 * * significantly datagram socket latencies at high loads,
150 * * when data copying to user space takes lots of time.
151 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
155 * The order of the tests when we find no data waiting are specified
156 * quite explicitly by POSIX 1003.1g, don't change them without having
157 * the standard around please.
159 struct sk_buff
*__skb_recv_datagram(struct sock
*sk
, unsigned flags
,
160 int *peeked
, int *err
)
165 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
167 int error
= sock_error(sk
);
172 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
175 /* Again only user level code calls this function, so nothing
176 * interrupt level will suddenly eat the receive_queue.
178 * Look at current nfs client by the way...
179 * However, this function was corrent in any case. 8)
181 unsigned long cpu_flags
;
183 spin_lock_irqsave(&sk
->sk_receive_queue
.lock
, cpu_flags
);
184 skb
= skb_peek(&sk
->sk_receive_queue
);
186 *peeked
= skb
->peeked
;
187 if (flags
& MSG_PEEK
) {
189 atomic_inc(&skb
->users
);
191 __skb_unlink(skb
, &sk
->sk_receive_queue
);
193 spin_unlock_irqrestore(&sk
->sk_receive_queue
.lock
, cpu_flags
);
198 /* User doesn't want to wait */
203 } while (!wait_for_packet(sk
, err
, &timeo
));
211 EXPORT_SYMBOL(__skb_recv_datagram
);
213 struct sk_buff
*skb_recv_datagram(struct sock
*sk
, unsigned flags
,
214 int noblock
, int *err
)
218 return __skb_recv_datagram(sk
, flags
| (noblock
? MSG_DONTWAIT
: 0),
222 void skb_free_datagram(struct sock
*sk
, struct sk_buff
*skb
)
225 sk_mem_reclaim_partial(sk
);
229 * skb_kill_datagram - Free a datagram skbuff forcibly
231 * @skb: datagram skbuff
234 * This function frees a datagram skbuff that was received by
235 * skb_recv_datagram. The flags argument must match the one
236 * used for skb_recv_datagram.
238 * If the MSG_PEEK flag is set, and the packet is still on the
239 * receive queue of the socket, it will be taken off the queue
240 * before it is freed.
242 * This function currently only disables BH when acquiring the
243 * sk_receive_queue lock. Therefore it must not be used in a
244 * context where that lock is acquired in an IRQ context.
246 * It returns 0 if the packet was removed by us.
249 int skb_kill_datagram(struct sock
*sk
, struct sk_buff
*skb
, unsigned int flags
)
253 if (flags
& MSG_PEEK
) {
255 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
256 if (skb
== skb_peek(&sk
->sk_receive_queue
)) {
257 __skb_unlink(skb
, &sk
->sk_receive_queue
);
258 atomic_dec(&skb
->users
);
261 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
265 sk_mem_reclaim_partial(sk
);
270 EXPORT_SYMBOL(skb_kill_datagram
);
273 * skb_copy_datagram_iovec - Copy a datagram to an iovec.
274 * @skb: buffer to copy
275 * @offset: offset in the buffer to start copying from
276 * @to: io vector to copy to
277 * @len: amount of data to copy from buffer to iovec
279 * Note: the iovec is modified during the copy.
281 int skb_copy_datagram_iovec(const struct sk_buff
*skb
, int offset
,
282 struct iovec
*to
, int len
)
284 int start
= skb_headlen(skb
);
285 int i
, copy
= start
- offset
;
286 struct sk_buff
*frag_iter
;
288 trace_skb_copy_datagram_iovec(skb
, len
);
294 if (memcpy_toiovec(to
, skb
->data
+ offset
, copy
))
296 if ((len
-= copy
) == 0)
301 /* Copy paged appendix. Hmm... why does this look so complicated? */
302 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
305 WARN_ON(start
> offset
+ len
);
307 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
308 if ((copy
= end
- offset
) > 0) {
311 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
312 struct page
*page
= frag
->page
;
317 err
= memcpy_toiovec(to
, vaddr
+ frag
->page_offset
+
318 offset
- start
, copy
);
329 skb_walk_frags(skb
, frag_iter
) {
332 WARN_ON(start
> offset
+ len
);
334 end
= start
+ frag_iter
->len
;
335 if ((copy
= end
- offset
) > 0) {
338 if (skb_copy_datagram_iovec(frag_iter
,
342 if ((len
-= copy
) == 0)
356 * skb_copy_datagram_const_iovec - Copy a datagram to an iovec.
357 * @skb: buffer to copy
358 * @offset: offset in the buffer to start copying from
359 * @to: io vector to copy to
360 * @to_offset: offset in the io vector to start copying to
361 * @len: amount of data to copy from buffer to iovec
363 * Returns 0 or -EFAULT.
364 * Note: the iovec is not modified during the copy.
366 int skb_copy_datagram_const_iovec(const struct sk_buff
*skb
, int offset
,
367 const struct iovec
*to
, int to_offset
,
370 int start
= skb_headlen(skb
);
371 int i
, copy
= start
- offset
;
372 struct sk_buff
*frag_iter
;
378 if (memcpy_toiovecend(to
, skb
->data
+ offset
, to_offset
, copy
))
380 if ((len
-= copy
) == 0)
386 /* Copy paged appendix. Hmm... why does this look so complicated? */
387 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
390 WARN_ON(start
> offset
+ len
);
392 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
393 if ((copy
= end
- offset
) > 0) {
396 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
397 struct page
*page
= frag
->page
;
402 err
= memcpy_toiovecend(to
, vaddr
+ frag
->page_offset
+
403 offset
- start
, to_offset
, copy
);
415 skb_walk_frags(skb
, frag_iter
) {
418 WARN_ON(start
> offset
+ len
);
420 end
= start
+ frag_iter
->len
;
421 if ((copy
= end
- offset
) > 0) {
424 if (skb_copy_datagram_const_iovec(frag_iter
,
429 if ((len
-= copy
) == 0)
442 EXPORT_SYMBOL(skb_copy_datagram_const_iovec
);
445 * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
446 * @skb: buffer to copy
447 * @offset: offset in the buffer to start copying to
448 * @from: io vector to copy to
449 * @from_offset: offset in the io vector to start copying from
450 * @len: amount of data to copy to buffer from iovec
452 * Returns 0 or -EFAULT.
453 * Note: the iovec is not modified during the copy.
455 int skb_copy_datagram_from_iovec(struct sk_buff
*skb
, int offset
,
456 const struct iovec
*from
, int from_offset
,
459 int start
= skb_headlen(skb
);
460 int i
, copy
= start
- offset
;
461 struct sk_buff
*frag_iter
;
467 if (memcpy_fromiovecend(skb
->data
+ offset
, from
, from_offset
,
470 if ((len
-= copy
) == 0)
476 /* Copy paged appendix. Hmm... why does this look so complicated? */
477 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
480 WARN_ON(start
> offset
+ len
);
482 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
483 if ((copy
= end
- offset
) > 0) {
486 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
487 struct page
*page
= frag
->page
;
492 err
= memcpy_fromiovecend(vaddr
+ frag
->page_offset
+
494 from
, from_offset
, copy
);
507 skb_walk_frags(skb
, frag_iter
) {
510 WARN_ON(start
> offset
+ len
);
512 end
= start
+ frag_iter
->len
;
513 if ((copy
= end
- offset
) > 0) {
516 if (skb_copy_datagram_from_iovec(frag_iter
,
522 if ((len
-= copy
) == 0)
535 EXPORT_SYMBOL(skb_copy_datagram_from_iovec
);
537 static int skb_copy_and_csum_datagram(const struct sk_buff
*skb
, int offset
,
538 u8 __user
*to
, int len
,
541 int start
= skb_headlen(skb
);
542 int i
, copy
= start
- offset
;
543 struct sk_buff
*frag_iter
;
551 *csump
= csum_and_copy_to_user(skb
->data
+ offset
, to
, copy
,
555 if ((len
-= copy
) == 0)
562 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
565 WARN_ON(start
> offset
+ len
);
567 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
568 if ((copy
= end
- offset
) > 0) {
572 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
573 struct page
*page
= frag
->page
;
578 csum2
= csum_and_copy_to_user(vaddr
+
585 *csump
= csum_block_add(*csump
, csum2
, pos
);
595 skb_walk_frags(skb
, frag_iter
) {
598 WARN_ON(start
> offset
+ len
);
600 end
= start
+ frag_iter
->len
;
601 if ((copy
= end
- offset
) > 0) {
605 if (skb_copy_and_csum_datagram(frag_iter
,
610 *csump
= csum_block_add(*csump
, csum2
, pos
);
611 if ((len
-= copy
) == 0)
626 __sum16
__skb_checksum_complete_head(struct sk_buff
*skb
, int len
)
630 sum
= csum_fold(skb_checksum(skb
, 0, len
, skb
->csum
));
632 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
633 netdev_rx_csum_fault(skb
->dev
);
634 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
638 EXPORT_SYMBOL(__skb_checksum_complete_head
);
640 __sum16
__skb_checksum_complete(struct sk_buff
*skb
)
642 return __skb_checksum_complete_head(skb
, skb
->len
);
644 EXPORT_SYMBOL(__skb_checksum_complete
);
647 * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec.
649 * @hlen: hardware length
652 * Caller _must_ check that skb will fit to this iovec.
654 * Returns: 0 - success.
655 * -EINVAL - checksum failure.
656 * -EFAULT - fault during copy. Beware, in this case iovec
659 int skb_copy_and_csum_datagram_iovec(struct sk_buff
*skb
,
660 int hlen
, struct iovec
*iov
)
663 int chunk
= skb
->len
- hlen
;
668 /* Skip filled elements.
669 * Pretty silly, look at memcpy_toiovec, though 8)
671 while (!iov
->iov_len
)
674 if (iov
->iov_len
< chunk
) {
675 if (__skb_checksum_complete(skb
))
677 if (skb_copy_datagram_iovec(skb
, hlen
, iov
, chunk
))
680 csum
= csum_partial(skb
->data
, hlen
, skb
->csum
);
681 if (skb_copy_and_csum_datagram(skb
, hlen
, iov
->iov_base
,
686 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
))
687 netdev_rx_csum_fault(skb
->dev
);
688 iov
->iov_len
-= chunk
;
689 iov
->iov_base
+= chunk
;
699 * datagram_poll - generic datagram poll
704 * Datagram poll: Again totally generic. This also handles
705 * sequenced packet sockets providing the socket receive queue
706 * is only ever holding data ready to receive.
708 * Note: when you _don't_ use this routine for this protocol,
709 * and you use a different write policy from sock_writeable()
710 * then please supply your own write_space callback.
712 unsigned int datagram_poll(struct file
*file
, struct socket
*sock
,
715 struct sock
*sk
= sock
->sk
;
718 sock_poll_wait(file
, sk
->sk_sleep
, wait
);
721 /* exceptional events? */
722 if (sk
->sk_err
|| !skb_queue_empty(&sk
->sk_error_queue
))
724 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
726 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
730 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
731 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
732 mask
|= POLLIN
| POLLRDNORM
;
734 /* Connection-based need to check for termination and startup */
735 if (connection_based(sk
)) {
736 if (sk
->sk_state
== TCP_CLOSE
)
738 /* connection hasn't started yet? */
739 if (sk
->sk_state
== TCP_SYN_SENT
)
744 if (sock_writeable(sk
))
745 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
747 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
752 EXPORT_SYMBOL(datagram_poll
);
753 EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec
);
754 EXPORT_SYMBOL(skb_copy_datagram_iovec
);
755 EXPORT_SYMBOL(skb_free_datagram
);
756 EXPORT_SYMBOL(skb_recv_datagram
);