blob: 4967262b27076af66347d20eca54b65a2e61d789 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * SUCS NET3:
3 *
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.
11 *
Alan Cox113aa832008-10-13 19:01:08 -070012 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * udp.c code)
14 *
15 * Fixes:
16 * Alan Cox : NULL return from skb_peek_copy()
17 * understood
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
23 * feasible.
24 * Alan Cox : Fixed write poll of non IP protocol
25 * crash.
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.
33 *
34 */
35
36#include <linux/module.h>
37#include <linux/types.h>
38#include <linux/kernel.h>
39#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/mm.h>
41#include <linux/interrupt.h>
42#include <linux/errno.h>
43#include <linux/sched.h>
44#include <linux/inet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/netdevice.h>
46#include <linux/rtnetlink.h>
47#include <linux/poll.h>
48#include <linux/highmem.h>
Herbert Xu3305b802005-12-13 23:16:37 -080049#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Jason Wang04335472013-08-06 17:45:07 +080051#include <linux/pagemap.h>
Herbert Xua8f820aa2014-11-07 21:22:22 +080052#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include <net/protocol.h>
55#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070057#include <net/checksum.h>
58#include <net/sock.h>
59#include <net/tcp_states.h>
Neil Hormane9b3cc12009-08-13 05:19:44 +000060#include <trace/events/skb.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +030061#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/*
64 * Is a socket 'connection oriented' ?
65 */
66static inline int connection_based(struct sock *sk)
67{
68 return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
69}
70
Eric Dumazet95c96172012-04-15 05:58:06 +000071static int receiver_wake_function(wait_queue_t *wait, unsigned int mode, int sync,
Eric Dumazetbf368e42009-04-28 02:24:21 -070072 void *key)
73{
74 unsigned long bits = (unsigned long)key;
75
76 /*
77 * Avoid a wakeup if event not interesting for us
78 */
79 if (bits && !(bits & (POLLIN | POLLERR)))
80 return 0;
81 return autoremove_wake_function(wait, mode, sync, key);
82}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
Benjamin Poirier39cc8612013-04-29 11:42:13 +000084 * Wait for the last received packet to be different from skb
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 */
Benjamin Poirier39cc8612013-04-29 11:42:13 +000086static int wait_for_more_packets(struct sock *sk, int *err, long *timeo_p,
87 const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 int error;
Eric Dumazetbf368e42009-04-28 02:24:21 -070090 DEFINE_WAIT_FUNC(wait, receiver_wake_function);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Eric Dumazetaa395142010-04-20 13:03:51 +000092 prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 /* Socket errors? */
95 error = sock_error(sk);
96 if (error)
97 goto out_err;
98
Benjamin Poirier39cc8612013-04-29 11:42:13 +000099 if (sk->sk_receive_queue.prev != skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 goto out;
101
102 /* Socket shut down? */
103 if (sk->sk_shutdown & RCV_SHUTDOWN)
104 goto out_noerr;
105
106 /* Sequenced packets can come disconnected.
107 * If so we report the problem
108 */
109 error = -ENOTCONN;
110 if (connection_based(sk) &&
111 !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
112 goto out_err;
113
114 /* handle signals */
115 if (signal_pending(current))
116 goto interrupted;
117
118 error = 0;
119 *timeo_p = schedule_timeout(*timeo_p);
120out:
Eric Dumazetaa395142010-04-20 13:03:51 +0000121 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return error;
123interrupted:
124 error = sock_intr_errno(*timeo_p);
125out_err:
126 *err = error;
127 goto out;
128out_noerr:
129 *err = 0;
130 error = 1;
131 goto out;
132}
133
Herbert Xu738ac1e2015-07-13 16:04:13 +0800134static int skb_set_peeked(struct sk_buff *skb)
135{
136 struct sk_buff *nskb;
137
138 if (skb->peeked)
139 return 0;
140
141 /* We have to unshare an skb before modifying it. */
142 if (!skb_shared(skb))
143 goto done;
144
145 nskb = skb_clone(skb, GFP_ATOMIC);
146 if (!nskb)
147 return -ENOMEM;
148
149 skb->prev->next = nskb;
150 skb->next->prev = nskb;
151 nskb->prev = skb->prev;
152 nskb->next = skb->next;
153
154 consume_skb(skb);
155 skb = nskb;
156
157done:
158 skb->peeked = 1;
159
160 return 0;
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/**
Herbert Xua59322b2007-12-05 01:53:40 -0800164 * __skb_recv_datagram - Receive a datagram skbuff
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700165 * @sk: socket
166 * @flags: MSG_ flags
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000167 * @peeked: returns non-zero if this packet has been seen before
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000168 * @off: an offset in bytes to peek skb from. Returns an offset
169 * within an skb where data actually starts
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700170 * @err: error code returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 *
172 * Get a datagram skbuff, understands the peeking, nonblocking wakeups
173 * and possible races. This replaces identical code in packet, raw and
174 * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
175 * the long standing peek and read race for datagram sockets. If you
176 * alter this routine remember it must be re-entrant.
177 *
178 * This function will lock the socket if a skb is returned, so the caller
179 * needs to unlock the socket in that case (usually by calling
180 * skb_free_datagram)
181 *
182 * * It does not lock socket since today. This function is
183 * * free of race conditions. This measure should/can improve
184 * * significantly datagram socket latencies at high loads,
185 * * when data copying to user space takes lots of time.
186 * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
187 * * 8) Great win.)
188 * * --ANK (980729)
189 *
190 * The order of the tests when we find no data waiting are specified
191 * quite explicitly by POSIX 1003.1g, don't change them without having
192 * the standard around please.
193 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000194struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000195 int *peeked, int *off, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Herbert Xu738ac1e2015-07-13 16:04:13 +0800197 struct sk_buff_head *queue = &sk->sk_receive_queue;
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000198 struct sk_buff *skb, *last;
Herbert Xu738ac1e2015-07-13 16:04:13 +0800199 unsigned long cpu_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 long timeo;
201 /*
202 * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
203 */
204 int error = sock_error(sk);
205
206 if (error)
207 goto no_packet;
208
Herbert Xua59322b2007-12-05 01:53:40 -0800209 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 do {
212 /* Again only user level code calls this function, so nothing
213 * interrupt level will suddenly eat the receive_queue.
214 *
215 * Look at current nfs client by the way...
David Shwatrz8917a3c2010-12-02 09:01:55 +0000216 * However, this function was correct in any case. 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 */
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000218 int _off = *off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000220 last = (struct sk_buff *)queue;
Pavel Emelyanov4934b032012-02-21 07:30:33 +0000221 spin_lock_irqsave(&queue->lock, cpu_flags);
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000222 skb_queue_walk(queue, skb) {
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000223 last = skb;
Herbert Xua59322b2007-12-05 01:53:40 -0800224 *peeked = skb->peeked;
225 if (flags & MSG_PEEK) {
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000226 if (_off >= skb->len && (skb->len || _off ||
Benjamin Poirieradd05ad2013-04-29 11:42:12 +0000227 skb->peeked)) {
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000228 _off -= skb->len;
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000229 continue;
230 }
Herbert Xu738ac1e2015-07-13 16:04:13 +0800231
232 error = skb_set_peeked(skb);
233 if (error)
234 goto unlock_err;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 atomic_inc(&skb->users);
Herbert Xua59322b2007-12-05 01:53:40 -0800237 } else
Pavel Emelyanov4934b032012-02-21 07:30:33 +0000238 __skb_unlink(skb, queue);
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000239
240 spin_unlock_irqrestore(&queue->lock, cpu_flags);
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000241 *off = _off;
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000242 return skb;
Herbert Xua59322b2007-12-05 01:53:40 -0800243 }
Pavel Emelyanov4934b032012-02-21 07:30:33 +0000244 spin_unlock_irqrestore(&queue->lock, cpu_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Eliezer Tamircbf55002013-07-08 16:20:34 +0300246 if (sk_can_busy_loop(sk) &&
247 sk_busy_loop(sk, flags & MSG_DONTWAIT))
Eliezer Tamira5b50472013-06-10 11:40:00 +0300248 continue;
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* User doesn't want to wait */
251 error = -EAGAIN;
252 if (!timeo)
253 goto no_packet;
254
Benjamin Poirier39cc8612013-04-29 11:42:13 +0000255 } while (!wait_for_more_packets(sk, err, &timeo, last));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 return NULL;
258
Herbert Xu738ac1e2015-07-13 16:04:13 +0800259unlock_err:
260 spin_unlock_irqrestore(&queue->lock, cpu_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261no_packet:
262 *err = error;
263 return NULL;
264}
Herbert Xua59322b2007-12-05 01:53:40 -0800265EXPORT_SYMBOL(__skb_recv_datagram);
266
Eric Dumazet95c96172012-04-15 05:58:06 +0000267struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
Herbert Xua59322b2007-12-05 01:53:40 -0800268 int noblock, int *err)
269{
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000270 int peeked, off = 0;
Herbert Xua59322b2007-12-05 01:53:40 -0800271
272 return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
Pavel Emelyanov3f518bf2012-02-21 07:30:58 +0000273 &peeked, &off, err);
Herbert Xua59322b2007-12-05 01:53:40 -0800274}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000275EXPORT_SYMBOL(skb_recv_datagram);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
278{
Neil Hormanead2ceb2009-03-11 09:49:55 +0000279 consume_skb(skb);
Eric Dumazet270acef2008-11-05 01:38:06 -0800280 sk_mem_reclaim_partial(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
Eric Dumazet9d410c72009-10-30 05:03:53 +0000282EXPORT_SYMBOL(skb_free_datagram);
283
284void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb)
285{
Eric Dumazet8a74ad62010-05-26 19:20:18 +0000286 bool slow;
287
Eric Dumazet93bb64e2010-05-03 23:18:14 -0700288 if (likely(atomic_read(&skb->users) == 1))
289 smp_rmb();
290 else if (likely(!atomic_dec_and_test(&skb->users)))
291 return;
292
Eric Dumazet8a74ad62010-05-26 19:20:18 +0000293 slow = lock_sock_fast(sk);
Eric Dumazet4b0b72f2010-04-28 14:35:48 -0700294 skb_orphan(skb);
295 sk_mem_reclaim_partial(sk);
Eric Dumazet8a74ad62010-05-26 19:20:18 +0000296 unlock_sock_fast(sk, slow);
Eric Dumazet4b0b72f2010-04-28 14:35:48 -0700297
Eric Dumazet93bb64e2010-05-03 23:18:14 -0700298 /* skb is now orphaned, can be freed outside of locked section */
299 __kfree_skb(skb);
Eric Dumazet9d410c72009-10-30 05:03:53 +0000300}
301EXPORT_SYMBOL(skb_free_datagram_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303/**
Herbert Xu3305b802005-12-13 23:16:37 -0800304 * skb_kill_datagram - Free a datagram skbuff forcibly
305 * @sk: socket
306 * @skb: datagram skbuff
307 * @flags: MSG_ flags
308 *
309 * This function frees a datagram skbuff that was received by
310 * skb_recv_datagram. The flags argument must match the one
311 * used for skb_recv_datagram.
312 *
313 * If the MSG_PEEK flag is set, and the packet is still on the
314 * receive queue of the socket, it will be taken off the queue
315 * before it is freed.
316 *
317 * This function currently only disables BH when acquiring the
318 * sk_receive_queue lock. Therefore it must not be used in a
319 * context where that lock is acquired in an IRQ context.
Herbert Xu27ab2562007-12-05 01:51:58 -0800320 *
321 * It returns 0 if the packet was removed by us.
Herbert Xu3305b802005-12-13 23:16:37 -0800322 */
323
Herbert Xu27ab2562007-12-05 01:51:58 -0800324int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
Herbert Xu3305b802005-12-13 23:16:37 -0800325{
Herbert Xu27ab2562007-12-05 01:51:58 -0800326 int err = 0;
327
Herbert Xu3305b802005-12-13 23:16:37 -0800328 if (flags & MSG_PEEK) {
Herbert Xu27ab2562007-12-05 01:51:58 -0800329 err = -ENOENT;
Herbert Xu3305b802005-12-13 23:16:37 -0800330 spin_lock_bh(&sk->sk_receive_queue.lock);
331 if (skb == skb_peek(&sk->sk_receive_queue)) {
332 __skb_unlink(skb, &sk->sk_receive_queue);
333 atomic_dec(&skb->users);
Herbert Xu27ab2562007-12-05 01:51:58 -0800334 err = 0;
Herbert Xu3305b802005-12-13 23:16:37 -0800335 }
336 spin_unlock_bh(&sk->sk_receive_queue.lock);
337 }
338
John Dykstra61de71c2009-05-08 14:57:01 -0700339 kfree_skb(skb);
Eric Dumazet8edf19c2009-10-15 00:12:40 +0000340 atomic_inc(&sk->sk_drops);
John Dykstra61de71c2009-05-08 14:57:01 -0700341 sk_mem_reclaim_partial(sk);
342
Herbert Xu27ab2562007-12-05 01:51:58 -0800343 return err;
Herbert Xu3305b802005-12-13 23:16:37 -0800344}
Herbert Xu3305b802005-12-13 23:16:37 -0800345EXPORT_SYMBOL(skb_kill_datagram);
346
347/**
Herbert Xua8f820aa2014-11-07 21:22:22 +0800348 * skb_copy_datagram_iter - Copy a datagram to an iovec iterator.
349 * @skb: buffer to copy
350 * @offset: offset in the buffer to start copying from
351 * @to: iovec iterator to copy to
352 * @len: amount of data to copy from buffer to iovec
353 */
354int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
355 struct iov_iter *to, int len)
356{
357 int start = skb_headlen(skb);
358 int i, copy = start - offset;
359 struct sk_buff *frag_iter;
360
361 trace_skb_copy_datagram_iovec(skb, len);
362
363 /* Copy header. */
364 if (copy > 0) {
365 if (copy > len)
366 copy = len;
367 if (copy_to_iter(skb->data + offset, copy, to) != copy)
368 goto short_copy;
369 if ((len -= copy) == 0)
370 return 0;
371 offset += copy;
372 }
373
374 /* Copy paged appendix. Hmm... why does this look so complicated? */
375 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
376 int end;
377 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
378
379 WARN_ON(start > offset + len);
380
381 end = start + skb_frag_size(frag);
382 if ((copy = end - offset) > 0) {
383 if (copy > len)
384 copy = len;
385 if (copy_page_to_iter(skb_frag_page(frag),
386 frag->page_offset + offset -
387 start, copy, to) != copy)
388 goto short_copy;
389 if (!(len -= copy))
390 return 0;
391 offset += copy;
392 }
393 start = end;
394 }
395
396 skb_walk_frags(skb, frag_iter) {
397 int end;
398
399 WARN_ON(start > offset + len);
400
401 end = start + frag_iter->len;
402 if ((copy = end - offset) > 0) {
403 if (copy > len)
404 copy = len;
405 if (skb_copy_datagram_iter(frag_iter, offset - start,
406 to, copy))
407 goto fault;
408 if ((len -= copy) == 0)
409 return 0;
410 offset += copy;
411 }
412 start = end;
413 }
414 if (!len)
415 return 0;
416
417 /* This is not really a user copy fault, but rather someone
418 * gave us a bogus length on the skb. We should probably
419 * print a warning here as it may indicate a kernel bug.
420 */
421
422fault:
423 return -EFAULT;
424
425short_copy:
426 if (iov_iter_count(to))
427 goto fault;
428
429 return 0;
430}
431EXPORT_SYMBOL(skb_copy_datagram_iter);
432
433/**
Al Viro8feb2fb2014-11-06 01:10:59 -0500434 * skb_copy_datagram_from_iter - Copy a datagram from an iov_iter.
Rusty Russelldb543c12008-08-15 15:13:53 -0700435 * @skb: buffer to copy
436 * @offset: offset in the buffer to start copying to
Al Viro8feb2fb2014-11-06 01:10:59 -0500437 * @from: the copy source
Rusty Russelldb543c12008-08-15 15:13:53 -0700438 * @len: amount of data to copy to buffer from iovec
439 *
440 * Returns 0 or -EFAULT.
Rusty Russelldb543c12008-08-15 15:13:53 -0700441 */
Al Viro3a654f92014-06-19 14:15:22 -0400442int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
443 struct iov_iter *from,
444 int len)
445{
446 int start = skb_headlen(skb);
447 int i, copy = start - offset;
448 struct sk_buff *frag_iter;
449
450 /* Copy header. */
451 if (copy > 0) {
452 if (copy > len)
453 copy = len;
454 if (copy_from_iter(skb->data + offset, copy, from) != copy)
455 goto fault;
456 if ((len -= copy) == 0)
457 return 0;
458 offset += copy;
459 }
460
461 /* Copy paged appendix. Hmm... why does this look so complicated? */
462 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
463 int end;
464 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
465
466 WARN_ON(start > offset + len);
467
468 end = start + skb_frag_size(frag);
469 if ((copy = end - offset) > 0) {
470 size_t copied;
471
472 if (copy > len)
473 copy = len;
474 copied = copy_page_from_iter(skb_frag_page(frag),
475 frag->page_offset + offset - start,
476 copy, from);
477 if (copied != copy)
478 goto fault;
479
480 if (!(len -= copy))
481 return 0;
482 offset += copy;
483 }
484 start = end;
485 }
486
487 skb_walk_frags(skb, frag_iter) {
488 int end;
489
490 WARN_ON(start > offset + len);
491
492 end = start + frag_iter->len;
493 if ((copy = end - offset) > 0) {
494 if (copy > len)
495 copy = len;
496 if (skb_copy_datagram_from_iter(frag_iter,
497 offset - start,
498 from, copy))
499 goto fault;
500 if ((len -= copy) == 0)
501 return 0;
502 offset += copy;
503 }
504 start = end;
505 }
506 if (!len)
507 return 0;
508
509fault:
510 return -EFAULT;
511}
512EXPORT_SYMBOL(skb_copy_datagram_from_iter);
513
Jason Wangc3bdeb52013-08-06 17:45:04 +0800514/**
Al Viro195e9522014-11-06 00:56:48 -0500515 * zerocopy_sg_from_iter - Build a zerocopy datagram from an iov_iter
Jason Wangc3bdeb52013-08-06 17:45:04 +0800516 * @skb: buffer to copy
Al Viro195e9522014-11-06 00:56:48 -0500517 * @from: the source to copy from
Jason Wangc3bdeb52013-08-06 17:45:04 +0800518 *
519 * The function will first copy up to headlen, and then pin the userspace
520 * pages and build frags through them.
521 *
522 * Returns 0, -EFAULT or -EMSGSIZE.
Jason Wangc3bdeb52013-08-06 17:45:04 +0800523 */
Al Viro3a654f92014-06-19 14:15:22 -0400524int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *from)
525{
526 int len = iov_iter_count(from);
527 int copy = min_t(int, skb_headlen(skb), len);
528 int frag = 0;
529
530 /* copy up to skb headlen */
531 if (skb_copy_datagram_from_iter(skb, 0, from, copy))
532 return -EFAULT;
533
534 while (iov_iter_count(from)) {
535 struct page *pages[MAX_SKB_FRAGS];
536 size_t start;
537 ssize_t copied;
538 unsigned long truesize;
539 int n = 0;
540
541 if (frag == MAX_SKB_FRAGS)
542 return -EMSGSIZE;
543
544 copied = iov_iter_get_pages(from, pages, ~0U,
545 MAX_SKB_FRAGS - frag, &start);
546 if (copied < 0)
547 return -EFAULT;
548
549 iov_iter_advance(from, copied);
550
551 truesize = PAGE_ALIGN(copied + start);
552 skb->data_len += copied;
553 skb->len += copied;
554 skb->truesize += truesize;
555 atomic_add(truesize, &skb->sk->sk_wmem_alloc);
556 while (copied) {
557 int size = min_t(int, copied, PAGE_SIZE - start);
558 skb_fill_page_desc(skb, frag++, pages[n], start, size);
559 start = 0;
560 copied -= size;
561 n++;
562 }
563 }
564 return 0;
565}
566EXPORT_SYMBOL(zerocopy_sg_from_iter);
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
Al Viroe5a4b0b2014-11-24 18:17:55 -0500569 struct iov_iter *to, int len,
Al Viro50842052006-11-14 21:36:34 -0800570 __wsum *csump)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
David S. Miller1a028e52007-04-27 15:21:23 -0700572 int start = skb_headlen(skb);
David S. Miller1a028e52007-04-27 15:21:23 -0700573 int i, copy = start - offset;
David S. Miller5b1a0022009-06-09 00:18:15 -0700574 struct sk_buff *frag_iter;
575 int pos = 0;
Al Viroe5a4b0b2014-11-24 18:17:55 -0500576 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 /* Copy header. */
579 if (copy > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (copy > len)
581 copy = len;
Al Viroe5a4b0b2014-11-24 18:17:55 -0500582 n = csum_and_copy_to_iter(skb->data + offset, copy, csump, to);
583 if (n != copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 goto fault;
585 if ((len -= copy) == 0)
586 return 0;
587 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 pos = copy;
589 }
590
591 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -0700592 int end;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000593 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700595 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -0700596
Eric Dumazet9e903e02011-10-18 21:00:24 +0000597 end = start + skb_frag_size(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if ((copy = end - offset) > 0) {
Al Viroe5a4b0b2014-11-24 18:17:55 -0500599 __wsum csum2 = 0;
Ian Campbellea2ab692011-08-22 23:44:58 +0000600 struct page *page = skb_frag_page(frag);
Al Viroe5a4b0b2014-11-24 18:17:55 -0500601 u8 *vaddr = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (copy > len)
604 copy = len;
Al Viroe5a4b0b2014-11-24 18:17:55 -0500605 n = csum_and_copy_to_iter(vaddr + frag->page_offset +
606 offset - start, copy,
607 &csum2, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 kunmap(page);
Al Viroe5a4b0b2014-11-24 18:17:55 -0500609 if (n != copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 goto fault;
611 *csump = csum_block_add(*csump, csum2, pos);
612 if (!(len -= copy))
613 return 0;
614 offset += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 pos += copy;
616 }
David S. Miller1a028e52007-04-27 15:21:23 -0700617 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
David S. Miller5b1a0022009-06-09 00:18:15 -0700620 skb_walk_frags(skb, frag_iter) {
621 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
David S. Miller5b1a0022009-06-09 00:18:15 -0700623 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
David S. Miller5b1a0022009-06-09 00:18:15 -0700625 end = start + frag_iter->len;
626 if ((copy = end - offset) > 0) {
627 __wsum csum2 = 0;
628 if (copy > len)
629 copy = len;
630 if (skb_copy_and_csum_datagram(frag_iter,
631 offset - start,
632 to, copy,
633 &csum2))
634 goto fault;
635 *csump = csum_block_add(*csump, csum2, pos);
636 if ((len -= copy) == 0)
637 return 0;
638 offset += copy;
David S. Miller5b1a0022009-06-09 00:18:15 -0700639 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
David S. Miller5b1a0022009-06-09 00:18:15 -0700641 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 if (!len)
644 return 0;
645
646fault:
647 return -EFAULT;
648}
649
Herbert Xu759e5d02007-03-25 20:10:56 -0700650__sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
Herbert Xufb286bb2005-11-10 13:01:24 -0800651{
Al Virod3bc23e2006-11-14 21:24:49 -0800652 __sum16 sum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800653
Herbert Xu759e5d02007-03-25 20:10:56 -0700654 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
Tom Herbert46fb51e2014-06-14 23:24:03 -0700655 if (likely(!sum)) {
656 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
657 !skb->csum_complete_sw)
658 netdev_rx_csum_fault(skb->dev);
659 }
Herbert Xu89c22d82015-07-13 20:01:42 +0800660 if (!skb_shared(skb))
661 skb->csum_valid = !sum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800662 return sum;
663}
Herbert Xu759e5d02007-03-25 20:10:56 -0700664EXPORT_SYMBOL(__skb_checksum_complete_head);
665
666__sum16 __skb_checksum_complete(struct sk_buff *skb)
667{
Tom Herbert46fb51e2014-06-14 23:24:03 -0700668 __wsum csum;
669 __sum16 sum;
670
671 csum = skb_checksum(skb, 0, skb->len, 0);
672
673 /* skb->csum holds pseudo checksum */
674 sum = csum_fold(csum_add(skb->csum, csum));
675 if (likely(!sum)) {
676 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
677 !skb->csum_complete_sw)
678 netdev_rx_csum_fault(skb->dev);
679 }
680
Herbert Xu89c22d82015-07-13 20:01:42 +0800681 if (!skb_shared(skb)) {
682 /* Save full packet checksum */
683 skb->csum = csum;
684 skb->ip_summed = CHECKSUM_COMPLETE;
685 skb->csum_complete_sw = 1;
686 skb->csum_valid = !sum;
687 }
Tom Herbert46fb51e2014-06-14 23:24:03 -0700688
689 return sum;
Herbert Xu759e5d02007-03-25 20:10:56 -0700690}
Herbert Xufb286bb2005-11-10 13:01:24 -0800691EXPORT_SYMBOL(__skb_checksum_complete);
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693/**
Al Viroe5a4b0b2014-11-24 18:17:55 -0500694 * skb_copy_and_csum_datagram_msg - Copy and checksum skb to user iovec.
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700695 * @skb: skbuff
696 * @hlen: hardware length
Al Viroe5a4b0b2014-11-24 18:17:55 -0500697 * @msg: destination
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900698 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * Caller _must_ check that skb will fit to this iovec.
700 *
701 * Returns: 0 - success.
702 * -EINVAL - checksum failure.
Al Viroe5a4b0b2014-11-24 18:17:55 -0500703 * -EFAULT - fault during copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
Al Viroe5a4b0b2014-11-24 18:17:55 -0500705int skb_copy_and_csum_datagram_msg(struct sk_buff *skb,
706 int hlen, struct msghdr *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Al Virod3bc23e2006-11-14 21:24:49 -0800708 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 int chunk = skb->len - hlen;
710
Herbert Xuef8aef52007-09-06 14:06:35 +0100711 if (!chunk)
712 return 0;
713
Al Viro01e97e62014-12-15 21:39:31 -0500714 if (msg_data_left(msg) < chunk) {
Herbert Xufb286bb2005-11-10 13:01:24 -0800715 if (__skb_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 goto csum_error;
Al Viroe5a4b0b2014-11-24 18:17:55 -0500717 if (skb_copy_datagram_msg(skb, hlen, msg, chunk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 goto fault;
719 } else {
720 csum = csum_partial(skb->data, hlen, skb->csum);
Al Viroe5a4b0b2014-11-24 18:17:55 -0500721 if (skb_copy_and_csum_datagram(skb, hlen, &msg->msg_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 chunk, &csum))
723 goto fault;
Al Virod3bc23e2006-11-14 21:24:49 -0800724 if (csum_fold(csum))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 goto csum_error;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700726 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
Herbert Xufb286bb2005-11-10 13:01:24 -0800727 netdev_rx_csum_fault(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729 return 0;
730csum_error:
731 return -EINVAL;
732fault:
733 return -EFAULT;
734}
Al Viroe5a4b0b2014-11-24 18:17:55 -0500735EXPORT_SYMBOL(skb_copy_and_csum_datagram_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737/**
738 * datagram_poll - generic datagram poll
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700739 * @file: file struct
740 * @sock: socket
741 * @wait: poll table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 *
743 * Datagram poll: Again totally generic. This also handles
744 * sequenced packet sockets providing the socket receive queue
745 * is only ever holding data ready to receive.
746 *
747 * Note: when you _don't_ use this routine for this protocol,
748 * and you use a different write policy from sock_writeable()
749 * then please supply your own write_space callback.
750 */
751unsigned int datagram_poll(struct file *file, struct socket *sock,
752 poll_table *wait)
753{
754 struct sock *sk = sock->sk;
755 unsigned int mask;
756
Eric Dumazetaa395142010-04-20 13:03:51 +0000757 sock_poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 mask = 0;
759
760 /* exceptional events? */
761 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +0000762 mask |= POLLERR |
Jacob Keller8facd5f2013-04-02 13:55:40 -0700763 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +0000764
Davide Libenzif348d702006-03-25 03:07:39 -0800765 if (sk->sk_shutdown & RCV_SHUTDOWN)
Eric Dumazetdb409802010-09-06 11:13:50 +0000766 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (sk->sk_shutdown == SHUTDOWN_MASK)
768 mask |= POLLHUP;
769
770 /* readable? */
Eric Dumazetdb409802010-09-06 11:13:50 +0000771 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 mask |= POLLIN | POLLRDNORM;
773
774 /* Connection-based need to check for termination and startup */
775 if (connection_based(sk)) {
776 if (sk->sk_state == TCP_CLOSE)
777 mask |= POLLHUP;
778 /* connection hasn't started yet? */
779 if (sk->sk_state == TCP_SYN_SENT)
780 return mask;
781 }
782
783 /* writable? */
784 if (sock_writeable(sk))
785 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
786 else
787 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
788
789 return mask;
790}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791EXPORT_SYMBOL(datagram_poll);