License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 14:07:57 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* SUCS NET3:
|
|
|
|
*
|
|
|
|
* Generic datagram handling routines. These are generic for all
|
|
|
|
* protocols. Possibly a generic IP version on top of these would
|
|
|
|
* make sense. Not tonight however 8-).
|
|
|
|
* This is used because UDP, RAW, PACKET, DDP, IPX, AX.25 and
|
|
|
|
* NetROM layer all have identical poll code and mostly
|
|
|
|
* identical recvmsg() code. So we share it here. The poll was
|
|
|
|
* shared before but buried in udp.c so I moved it.
|
|
|
|
*
|
2008-10-14 02:01:08 +00:00
|
|
|
* Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old
|
2005-04-16 22:20:36 +00:00
|
|
|
* udp.c code)
|
|
|
|
*
|
|
|
|
* Fixes:
|
|
|
|
* Alan Cox : NULL return from skb_peek_copy()
|
|
|
|
* understood
|
|
|
|
* Alan Cox : Rewrote skb_read_datagram to avoid the
|
|
|
|
* skb_peek_copy stuff.
|
|
|
|
* Alan Cox : Added support for SOCK_SEQPACKET.
|
|
|
|
* IPX can no longer use the SO_TYPE hack
|
|
|
|
* but AX.25 now works right, and SPX is
|
|
|
|
* feasible.
|
|
|
|
* Alan Cox : Fixed write poll of non IP protocol
|
|
|
|
* crash.
|
|
|
|
* Florian La Roche: Changed for my new skbuff handling.
|
|
|
|
* Darryl Miles : Fixed non-blocking SOCK_SEQPACKET.
|
|
|
|
* Linus Torvalds : BSD semantic fixes.
|
|
|
|
* Alan Cox : Datagram iovec handling
|
|
|
|
* Darryl Miles : Fixed non-blocking SOCK_STREAM.
|
|
|
|
* Alan Cox : POSIXisms
|
|
|
|
* Pete Wyckoff : Unconnected accept() fix.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
2016-12-24 19:46:01 +00:00
|
|
|
#include <linux/uaccess.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/inet.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/rtnetlink.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/highmem.h>
|
2005-12-14 07:16:37 +00:00
|
|
|
#include <linux/spinlock.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2013-08-06 09:45:07 +00:00
|
|
|
#include <linux/pagemap.h>
|
2014-11-07 13:22:22 +00:00
|
|
|
#include <linux/uio.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#include <net/protocol.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
|
2005-08-10 03:08:28 +00:00
|
|
|
#include <net/checksum.h>
|
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/tcp_states.h>
|
2009-08-13 05:19:44 +00:00
|
|
|
#include <trace/events/skb.h>
|
2013-07-10 14:13:17 +00:00
|
|
|
#include <net/busy_poll.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2019-03-25 16:17:23 +00:00
|
|
|
#include "datagram.h"
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Is a socket 'connection oriented' ?
|
|
|
|
*/
|
|
|
|
static inline int connection_based(struct sock *sk)
|
|
|
|
{
|
|
|
|
return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
|
|
|
|
}
|
|
|
|
|
2017-06-20 10:06:13 +00:00
|
|
|
static int receiver_wake_function(wait_queue_entry_t *wait, unsigned int mode, int sync,
|
2009-04-28 09:24:21 +00:00
|
|
|
void *key)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Avoid a wakeup if event not interesting for us
|
|
|
|
*/
|
2018-02-11 22:34:03 +00:00
|
|
|
if (key && !(key_to_poll(key) & (EPOLLIN | EPOLLERR)))
|
2009-04-28 09:24:21 +00:00
|
|
|
return 0;
|
|
|
|
return autoremove_wake_function(wait, mode, sync, key);
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2013-04-29 11:42:13 +00:00
|
|
|
* Wait for the last received packet to be different from skb
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2015-12-06 21:11:34 +00:00
|
|
|
int __skb_wait_for_more_packets(struct sock *sk, int *err, long *timeo_p,
|
|
|
|
const struct sk_buff *skb)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int error;
|
2009-04-28 09:24:21 +00:00
|
|
|
DEFINE_WAIT_FUNC(wait, receiver_wake_function);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2010-04-20 13:03:51 +00:00
|
|
|
prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* Socket errors? */
|
|
|
|
error = sock_error(sk);
|
|
|
|
if (error)
|
|
|
|
goto out_err;
|
|
|
|
|
2019-10-24 05:44:52 +00:00
|
|
|
if (READ_ONCE(sk->sk_receive_queue.prev) != skb)
|
2005-04-16 22:20:36 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Socket shut down? */
|
|
|
|
if (sk->sk_shutdown & RCV_SHUTDOWN)
|
|
|
|
goto out_noerr;
|
|
|
|
|
|
|
|
/* Sequenced packets can come disconnected.
|
|
|
|
* If so we report the problem
|
|
|
|
*/
|
|
|
|
error = -ENOTCONN;
|
|
|
|
if (connection_based(sk) &&
|
|
|
|
!(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
|
|
|
|
goto out_err;
|
|
|
|
|
|
|
|
/* handle signals */
|
|
|
|
if (signal_pending(current))
|
|
|
|
goto interrupted;
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
*timeo_p = schedule_timeout(*timeo_p);
|
|
|
|
out:
|
2010-04-20 13:03:51 +00:00
|
|
|
finish_wait(sk_sleep(sk), &wait);
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
|
|
|
interrupted:
|
|
|
|
error = sock_intr_errno(*timeo_p);
|
|
|
|
out_err:
|
|
|
|
*err = error;
|
|
|
|
goto out;
|
|
|
|
out_noerr:
|
|
|
|
*err = 0;
|
|
|
|
error = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
2015-12-06 21:11:34 +00:00
|
|
|
EXPORT_SYMBOL(__skb_wait_for_more_packets);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2015-08-04 07:42:47 +00:00
|
|
|
static struct sk_buff *skb_set_peeked(struct sk_buff *skb)
|
2015-07-13 08:04:13 +00:00
|
|
|
{
|
|
|
|
struct sk_buff *nskb;
|
|
|
|
|
|
|
|
if (skb->peeked)
|
2015-08-04 07:42:47 +00:00
|
|
|
return skb;
|
2015-07-13 08:04:13 +00:00
|
|
|
|
|
|
|
/* We have to unshare an skb before modifying it. */
|
|
|
|
if (!skb_shared(skb))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
nskb = skb_clone(skb, GFP_ATOMIC);
|
|
|
|
if (!nskb)
|
2015-08-04 07:42:47 +00:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2015-07-13 08:04:13 +00:00
|
|
|
|
|
|
|
skb->prev->next = nskb;
|
|
|
|
skb->next->prev = nskb;
|
|
|
|
nskb->prev = skb->prev;
|
|
|
|
nskb->next = skb->next;
|
|
|
|
|
|
|
|
consume_skb(skb);
|
|
|
|
skb = nskb;
|
|
|
|
|
|
|
|
done:
|
|
|
|
skb->peeked = 1;
|
|
|
|
|
2015-08-04 07:42:47 +00:00
|
|
|
return skb;
|
2015-07-13 08:04:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 09:20:13 +00:00
|
|
|
struct sk_buff *__skb_try_recv_from_queue(struct sock *sk,
|
|
|
|
struct sk_buff_head *queue,
|
|
|
|
unsigned int flags,
|
|
|
|
void (*destructor)(struct sock *sk,
|
|
|
|
struct sk_buff *skb),
|
2019-04-08 08:15:59 +00:00
|
|
|
int *off, int *err,
|
2017-05-16 09:20:13 +00:00
|
|
|
struct sk_buff **last)
|
|
|
|
{
|
datagram: When peeking datagrams with offset < 0 don't skip empty skbs
Due to commit e6afc8ace6dd5cef5e812f26c72579da8806f5ac ("udp: remove
headers from UDP packets before queueing"), when udp packets are being
peeked the requested extra offset is always 0 as there is no need to skip
the udp header. However, when the offset is 0 and the next skb is
of length 0, it is only returned once. The behaviour can be seen with
the following python script:
from socket import *;
f=socket(AF_INET6, SOCK_DGRAM | SOCK_NONBLOCK, 0);
g=socket(AF_INET6, SOCK_DGRAM | SOCK_NONBLOCK, 0);
f.bind(('::', 0));
addr=('::1', f.getsockname()[1]);
g.sendto(b'', addr)
g.sendto(b'b', addr)
print(f.recvfrom(10, MSG_PEEK));
print(f.recvfrom(10, MSG_PEEK));
Where the expected output should be the empty string twice.
Instead, make sk_peek_offset return negative values, and pass those values
to __skb_try_recv_datagram/__skb_try_recv_from_queue. If the passed offset
to __skb_try_recv_from_queue is negative, the checked skb is never skipped.
__skb_try_recv_from_queue will then ensure the offset is reset back to 0
if a peek is requested without an offset, unless no packets are found.
Also simplify the if condition in __skb_try_recv_from_queue. If _off is
greater then 0, and off is greater then or equal to skb->len, then
(_off || skb->len) must always be true assuming skb->len >= 0 is always
true.
Also remove a redundant check around a call to sk_peek_offset in af_unix.c,
as it double checked if MSG_PEEK was set in the flags.
V2:
- Moved the negative fixup into __skb_try_recv_from_queue, and remove now
redundant checks
- Fix peeking in udp{,v6}_recvmsg to report the right value when the
offset is 0
V3:
- Marked new branch in __skb_try_recv_from_queue as unlikely.
Signed-off-by: Matthew Dawson <matthew@mjdsystems.ca>
Acked-by: Willem de Bruijn <willemb@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-08-18 19:04:54 +00:00
|
|
|
bool peek_at_off = false;
|
2017-05-16 09:20:13 +00:00
|
|
|
struct sk_buff *skb;
|
datagram: When peeking datagrams with offset < 0 don't skip empty skbs
Due to commit e6afc8ace6dd5cef5e812f26c72579da8806f5ac ("udp: remove
headers from UDP packets before queueing"), when udp packets are being
peeked the requested extra offset is always 0 as there is no need to skip
the udp header. However, when the offset is 0 and the next skb is
of length 0, it is only returned once. The behaviour can be seen with
the following python script:
from socket import *;
f=socket(AF_INET6, SOCK_DGRAM | SOCK_NONBLOCK, 0);
g=socket(AF_INET6, SOCK_DGRAM | SOCK_NONBLOCK, 0);
f.bind(('::', 0));
addr=('::1', f.getsockname()[1]);
g.sendto(b'', addr)
g.sendto(b'b', addr)
print(f.recvfrom(10, MSG_PEEK));
print(f.recvfrom(10, MSG_PEEK));
Where the expected output should be the empty string twice.
Instead, make sk_peek_offset return negative values, and pass those values
to __skb_try_recv_datagram/__skb_try_recv_from_queue. If the passed offset
to __skb_try_recv_from_queue is negative, the checked skb is never skipped.
__skb_try_recv_from_queue will then ensure the offset is reset back to 0
if a peek is requested without an offset, unless no packets are found.
Also simplify the if condition in __skb_try_recv_from_queue. If _off is
greater then 0, and off is greater then or equal to skb->len, then
(_off || skb->len) must always be true assuming skb->len >= 0 is always
true.
Also remove a redundant check around a call to sk_peek_offset in af_unix.c,
as it double checked if MSG_PEEK was set in the flags.
V2:
- Moved the negative fixup into __skb_try_recv_from_queue, and remove now
redundant checks
- Fix peeking in udp{,v6}_recvmsg to report the right value when the
offset is 0
V3:
- Marked new branch in __skb_try_recv_from_queue as unlikely.
Signed-off-by: Matthew Dawson <matthew@mjdsystems.ca>
Acked-by: Willem de Bruijn <willemb@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-08-18 19:04:54 +00:00
|
|
|
int _off = 0;
|
|
|
|
|
|
|
|
if (unlikely(flags & MSG_PEEK && *off >= 0)) {
|
|
|
|
peek_at_off = true;
|
|
|
|
_off = *off;
|
|
|
|
}
|
2017-05-16 09:20:13 +00:00
|
|
|
|
|
|
|
*last = queue->prev;
|
|
|
|
skb_queue_walk(queue, skb) {
|
|
|
|
if (flags & MSG_PEEK) {
|
datagram: When peeking datagrams with offset < 0 don't skip empty skbs
Due to commit e6afc8ace6dd5cef5e812f26c72579da8806f5ac ("udp: remove
headers from UDP packets before queueing"), when udp packets are being
peeked the requested extra offset is always 0 as there is no need to skip
the udp header. However, when the offset is 0 and the next skb is
of length 0, it is only returned once. The behaviour can be seen with
the following python script:
from socket import *;
f=socket(AF_INET6, SOCK_DGRAM | SOCK_NONBLOCK, 0);
g=socket(AF_INET6, SOCK_DGRAM | SOCK_NONBLOCK, 0);
f.bind(('::', 0));
addr=('::1', f.getsockname()[1]);
g.sendto(b'', addr)
g.sendto(b'b', addr)
print(f.recvfrom(10, MSG_PEEK));
print(f.recvfrom(10, MSG_PEEK));
Where the expected output should be the empty string twice.
Instead, make sk_peek_offset return negative values, and pass those values
to __skb_try_recv_datagram/__skb_try_recv_from_queue. If the passed offset
to __skb_try_recv_from_queue is negative, the checked skb is never skipped.
__skb_try_recv_from_queue will then ensure the offset is reset back to 0
if a peek is requested without an offset, unless no packets are found.
Also simplify the if condition in __skb_try_recv_from_queue. If _off is
greater then 0, and off is greater then or equal to skb->len, then
(_off || skb->len) must always be true assuming skb->len >= 0 is always
true.
Also remove a redundant check around a call to sk_peek_offset in af_unix.c,
as it double checked if MSG_PEEK was set in the flags.
V2:
- Moved the negative fixup into __skb_try_recv_from_queue, and remove now
redundant checks
- Fix peeking in udp{,v6}_recvmsg to report the right value when the
offset is 0
V3:
- Marked new branch in __skb_try_recv_from_queue as unlikely.
Signed-off-by: Matthew Dawson <matthew@mjdsystems.ca>
Acked-by: Willem de Bruijn <willemb@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-08-18 19:04:54 +00:00
|
|
|
if (peek_at_off && _off >= skb->len &&
|
|
|
|
(_off || skb->peeked)) {
|
2017-05-17 18:39:05 +00:00
|
|
|
_off -= skb->len;
|
2017-05-16 09:20:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!skb->len) {
|
|
|
|
skb = skb_set_peeked(skb);
|
2017-09-26 09:21:42 +00:00
|
|
|
if (IS_ERR(skb)) {
|
2017-05-16 09:20:13 +00:00
|
|
|
*err = PTR_ERR(skb);
|
2017-05-17 18:39:05 +00:00
|
|
|
return NULL;
|
2017-05-16 09:20:13 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-30 10:07:58 +00:00
|
|
|
refcount_inc(&skb->users);
|
2017-05-16 09:20:13 +00:00
|
|
|
} else {
|
|
|
|
__skb_unlink(skb, queue);
|
|
|
|
if (destructor)
|
|
|
|
destructor(sk, skb);
|
|
|
|
}
|
2017-05-17 18:39:05 +00:00
|
|
|
*off = _off;
|
2017-05-16 09:20:13 +00:00
|
|
|
return skb;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
2015-12-06 21:11:34 +00:00
|
|
|
* __skb_try_recv_datagram - Receive a datagram skbuff
|
2005-05-01 15:59:25 +00:00
|
|
|
* @sk: socket
|
2017-07-12 16:29:07 +00:00
|
|
|
* @flags: MSG\_ flags
|
2016-11-04 10:28:59 +00:00
|
|
|
* @destructor: invoked under the receive lock on successful dequeue
|
2012-02-21 07:30:58 +00:00
|
|
|
* @off: an offset in bytes to peek skb from. Returns an offset
|
|
|
|
* within an skb where data actually starts
|
2005-05-01 15:59:25 +00:00
|
|
|
* @err: error code returned
|
2015-12-06 21:11:34 +00:00
|
|
|
* @last: set to last peeked message to inform the wait function
|
|
|
|
* what to look for when peeking
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* Get a datagram skbuff, understands the peeking, nonblocking wakeups
|
|
|
|
* and possible races. This replaces identical code in packet, raw and
|
|
|
|
* udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
|
|
|
|
* the long standing peek and read race for datagram sockets. If you
|
|
|
|
* alter this routine remember it must be re-entrant.
|
|
|
|
*
|
2015-12-06 21:11:34 +00:00
|
|
|
* This function will lock the socket if a skb is returned, so
|
|
|
|
* the caller needs to unlock the socket in that case (usually by
|
2017-05-12 12:35:46 +00:00
|
|
|
* calling skb_free_datagram). Returns NULL with @err set to
|
2015-12-06 21:11:34 +00:00
|
|
|
* -EAGAIN if no data was available or to some other value if an
|
|
|
|
* error was detected.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* * It does not lock socket since today. This function is
|
|
|
|
* * free of race conditions. This measure should/can improve
|
|
|
|
* * significantly datagram socket latencies at high loads,
|
|
|
|
* * when data copying to user space takes lots of time.
|
|
|
|
* * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
|
|
|
|
* * 8) Great win.)
|
|
|
|
* * --ANK (980729)
|
|
|
|
*
|
|
|
|
* The order of the tests when we find no data waiting are specified
|
|
|
|
* quite explicitly by POSIX 1003.1g, don't change them without having
|
|
|
|
* the standard around please.
|
|
|
|
*/
|
2015-12-06 21:11:34 +00:00
|
|
|
struct sk_buff *__skb_try_recv_datagram(struct sock *sk, unsigned int flags,
|
2016-11-04 10:28:59 +00:00
|
|
|
void (*destructor)(struct sock *sk,
|
|
|
|
struct sk_buff *skb),
|
2019-04-08 08:15:59 +00:00
|
|
|
int *off, int *err,
|
2015-12-06 21:11:34 +00:00
|
|
|
struct sk_buff **last)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2015-07-13 08:04:13 +00:00
|
|
|
struct sk_buff_head *queue = &sk->sk_receive_queue;
|
2015-12-06 21:11:34 +00:00
|
|
|
struct sk_buff *skb;
|
2015-07-13 08:04:13 +00:00
|
|
|
unsigned long cpu_flags;
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Caller is allowed not to check sk->sk_err before skb_recv_datagram()
|
|
|
|
*/
|
|
|
|
int error = sock_error(sk);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
goto no_packet;
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* Again only user level code calls this function, so nothing
|
|
|
|
* interrupt level will suddenly eat the receive_queue.
|
|
|
|
*
|
|
|
|
* Look at current nfs client by the way...
|
2010-12-02 09:01:55 +00:00
|
|
|
* However, this function was correct in any case. 8)
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2012-02-21 07:30:33 +00:00
|
|
|
spin_lock_irqsave(&queue->lock, cpu_flags);
|
2017-05-16 09:20:13 +00:00
|
|
|
skb = __skb_try_recv_from_queue(sk, queue, flags, destructor,
|
2019-04-08 08:15:59 +00:00
|
|
|
off, &error, last);
|
2012-02-21 07:30:58 +00:00
|
|
|
spin_unlock_irqrestore(&queue->lock, cpu_flags);
|
2017-05-17 18:39:05 +00:00
|
|
|
if (error)
|
|
|
|
goto no_packet;
|
2017-05-16 09:20:13 +00:00
|
|
|
if (skb)
|
|
|
|
return skb;
|
2017-03-24 17:08:12 +00:00
|
|
|
|
|
|
|
if (!sk_can_busy_loop(sk))
|
|
|
|
break;
|
|
|
|
|
|
|
|
sk_busy_loop(sk, flags & MSG_DONTWAIT);
|
2019-10-24 05:44:51 +00:00
|
|
|
} while (READ_ONCE(sk->sk_receive_queue.prev) != *last);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2015-12-06 21:11:34 +00:00
|
|
|
error = -EAGAIN;
|
2013-06-10 08:40:00 +00:00
|
|
|
|
2015-12-06 21:11:34 +00:00
|
|
|
no_packet:
|
|
|
|
*err = error;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__skb_try_recv_datagram);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2015-12-06 21:11:34 +00:00
|
|
|
struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
|
2016-11-04 10:28:59 +00:00
|
|
|
void (*destructor)(struct sock *sk,
|
|
|
|
struct sk_buff *skb),
|
2019-04-08 08:15:59 +00:00
|
|
|
int *off, int *err)
|
2015-12-06 21:11:34 +00:00
|
|
|
{
|
|
|
|
struct sk_buff *skb, *last;
|
|
|
|
long timeo;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2015-12-06 21:11:34 +00:00
|
|
|
timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
|
|
|
|
|
|
|
|
do {
|
2019-04-08 08:15:59 +00:00
|
|
|
skb = __skb_try_recv_datagram(sk, flags, destructor, off, err,
|
|
|
|
&last);
|
2015-12-06 21:11:34 +00:00
|
|
|
if (skb)
|
|
|
|
return skb;
|
|
|
|
|
2015-12-08 14:47:56 +00:00
|
|
|
if (*err != -EAGAIN)
|
2015-12-06 21:11:34 +00:00
|
|
|
break;
|
|
|
|
} while (timeo &&
|
|
|
|
!__skb_wait_for_more_packets(sk, err, &timeo, last));
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-12-05 09:53:40 +00:00
|
|
|
EXPORT_SYMBOL(__skb_recv_datagram);
|
|
|
|
|
2012-04-15 05:58:06 +00:00
|
|
|
struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
|
2007-12-05 09:53:40 +00:00
|
|
|
int noblock, int *err)
|
|
|
|
{
|
2019-04-08 08:15:59 +00:00
|
|
|
int off = 0;
|
2007-12-05 09:53:40 +00:00
|
|
|
|
|
|
|
return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
|
2019-04-08 08:15:59 +00:00
|
|
|
NULL, &off, err);
|
2007-12-05 09:53:40 +00:00
|
|
|
}
|
2010-07-09 21:22:04 +00:00
|
|
|
EXPORT_SYMBOL(skb_recv_datagram);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
2009-03-11 09:49:55 +00:00
|
|
|
consume_skb(skb);
|
2008-11-05 09:38:06 +00:00
|
|
|
sk_mem_reclaim_partial(sk);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2009-10-30 05:03:53 +00:00
|
|
|
EXPORT_SYMBOL(skb_free_datagram);
|
|
|
|
|
2016-04-05 16:41:16 +00:00
|
|
|
void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len)
|
2009-10-30 05:03:53 +00:00
|
|
|
{
|
2010-05-26 19:20:18 +00:00
|
|
|
bool slow;
|
|
|
|
|
2017-06-12 09:23:41 +00:00
|
|
|
if (!skb_unref(skb)) {
|
2016-04-05 16:41:16 +00:00
|
|
|
sk_peek_offset_bwd(sk, len);
|
2010-05-04 06:18:14 +00:00
|
|
|
return;
|
2016-04-05 16:41:16 +00:00
|
|
|
}
|
2010-05-04 06:18:14 +00:00
|
|
|
|
2010-05-26 19:20:18 +00:00
|
|
|
slow = lock_sock_fast(sk);
|
2016-04-05 16:41:16 +00:00
|
|
|
sk_peek_offset_bwd(sk, len);
|
2010-04-28 21:35:48 +00:00
|
|
|
skb_orphan(skb);
|
|
|
|
sk_mem_reclaim_partial(sk);
|
2010-05-26 19:20:18 +00:00
|
|
|
unlock_sock_fast(sk, slow);
|
2010-04-28 21:35:48 +00:00
|
|
|
|
2010-05-04 06:18:14 +00:00
|
|
|
/* skb is now orphaned, can be freed outside of locked section */
|
|
|
|
__kfree_skb(skb);
|
2009-10-30 05:03:53 +00:00
|
|
|
}
|
2016-04-05 16:41:16 +00:00
|
|
|
EXPORT_SYMBOL(__skb_free_datagram_locked);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2017-05-16 09:20:13 +00:00
|
|
|
int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue,
|
|
|
|
struct sk_buff *skb, unsigned int flags,
|
2017-02-05 17:25:24 +00:00
|
|
|
void (*destructor)(struct sock *sk,
|
|
|
|
struct sk_buff *skb))
|
2016-10-21 11:55:45 +00:00
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (flags & MSG_PEEK) {
|
|
|
|
err = -ENOENT;
|
2017-05-16 09:20:13 +00:00
|
|
|
spin_lock_bh(&sk_queue->lock);
|
2017-08-22 16:39:28 +00:00
|
|
|
if (skb->next) {
|
2017-05-16 09:20:13 +00:00
|
|
|
__skb_unlink(skb, sk_queue);
|
2017-06-30 10:07:58 +00:00
|
|
|
refcount_dec(&skb->users);
|
2017-02-05 17:25:24 +00:00
|
|
|
if (destructor)
|
|
|
|
destructor(sk, skb);
|
2016-10-21 11:55:45 +00:00
|
|
|
err = 0;
|
|
|
|
}
|
2017-05-16 09:20:13 +00:00
|
|
|
spin_unlock_bh(&sk_queue->lock);
|
2016-10-21 11:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
atomic_inc(&sk->sk_drops);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__sk_queue_drop_skb);
|
|
|
|
|
2005-12-14 07:16:37 +00:00
|
|
|
/**
|
|
|
|
* skb_kill_datagram - Free a datagram skbuff forcibly
|
|
|
|
* @sk: socket
|
|
|
|
* @skb: datagram skbuff
|
2017-07-12 16:29:07 +00:00
|
|
|
* @flags: MSG\_ flags
|
2005-12-14 07:16:37 +00:00
|
|
|
*
|
|
|
|
* This function frees a datagram skbuff that was received by
|
|
|
|
* skb_recv_datagram. The flags argument must match the one
|
|
|
|
* used for skb_recv_datagram.
|
|
|
|
*
|
|
|
|
* If the MSG_PEEK flag is set, and the packet is still on the
|
|
|
|
* receive queue of the socket, it will be taken off the queue
|
|
|
|
* before it is freed.
|
|
|
|
*
|
|
|
|
* This function currently only disables BH when acquiring the
|
|
|
|
* sk_receive_queue lock. Therefore it must not be used in a
|
|
|
|
* context where that lock is acquired in an IRQ context.
|
2007-12-05 09:51:58 +00:00
|
|
|
*
|
|
|
|
* It returns 0 if the packet was removed by us.
|
2005-12-14 07:16:37 +00:00
|
|
|
*/
|
|
|
|
|
2007-12-05 09:51:58 +00:00
|
|
|
int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
|
2005-12-14 07:16:37 +00:00
|
|
|
{
|
2017-05-16 09:20:13 +00:00
|
|
|
int err = __sk_queue_drop_skb(sk, &sk->sk_receive_queue, skb, flags,
|
|
|
|
NULL);
|
2005-12-14 07:16:37 +00:00
|
|
|
|
2009-05-08 21:57:01 +00:00
|
|
|
kfree_skb(skb);
|
|
|
|
sk_mem_reclaim_partial(sk);
|
2007-12-05 09:51:58 +00:00
|
|
|
return err;
|
2005-12-14 07:16:37 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(skb_kill_datagram);
|
|
|
|
|
2019-03-19 14:59:46 +00:00
|
|
|
static int __skb_datagram_iter(const struct sk_buff *skb, int offset,
|
|
|
|
struct iov_iter *to, int len, bool fault_short,
|
|
|
|
size_t (*cb)(const void *, size_t, void *,
|
|
|
|
struct iov_iter *), void *data)
|
2014-11-07 13:22:22 +00:00
|
|
|
{
|
|
|
|
int start = skb_headlen(skb);
|
2017-02-18 01:16:34 +00:00
|
|
|
int i, copy = start - offset, start_off = offset, n;
|
2014-11-07 13:22:22 +00:00
|
|
|
struct sk_buff *frag_iter;
|
|
|
|
|
|
|
|
/* Copy header. */
|
|
|
|
if (copy > 0) {
|
|
|
|
if (copy > len)
|
|
|
|
copy = len;
|
2018-12-04 01:52:08 +00:00
|
|
|
n = cb(skb->data + offset, copy, data, to);
|
2017-02-18 01:16:34 +00:00
|
|
|
offset += n;
|
|
|
|
if (n != copy)
|
2014-11-07 13:22:22 +00:00
|
|
|
goto short_copy;
|
|
|
|
if ((len -= copy) == 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy paged appendix. Hmm... why does this look so complicated? */
|
|
|
|
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
|
|
|
|
int end;
|
|
|
|
const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
|
|
|
|
|
|
|
|
WARN_ON(start > offset + len);
|
|
|
|
|
|
|
|
end = start + skb_frag_size(frag);
|
|
|
|
if ((copy = end - offset) > 0) {
|
2018-12-04 01:52:06 +00:00
|
|
|
struct page *page = skb_frag_page(frag);
|
|
|
|
u8 *vaddr = kmap(page);
|
|
|
|
|
2014-11-07 13:22:22 +00:00
|
|
|
if (copy > len)
|
|
|
|
copy = len;
|
2019-07-30 14:40:33 +00:00
|
|
|
n = cb(vaddr + skb_frag_off(frag) + offset - start,
|
|
|
|
copy, data, to);
|
2018-12-04 01:52:06 +00:00
|
|
|
kunmap(page);
|
2017-02-18 01:16:34 +00:00
|
|
|
offset += n;
|
|
|
|
if (n != copy)
|
2014-11-07 13:22:22 +00:00
|
|
|
goto short_copy;
|
|
|
|
if (!(len -= copy))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
start = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
skb_walk_frags(skb, frag_iter) {
|
|
|
|
int end;
|
|
|
|
|
|
|
|
WARN_ON(start > offset + len);
|
|
|
|
|
|
|
|
end = start + frag_iter->len;
|
|
|
|
if ((copy = end - offset) > 0) {
|
|
|
|
if (copy > len)
|
|
|
|
copy = len;
|
2018-12-04 01:52:08 +00:00
|
|
|
if (__skb_datagram_iter(frag_iter, offset - start,
|
2018-12-04 01:52:10 +00:00
|
|
|
to, copy, fault_short, cb, data))
|
2014-11-07 13:22:22 +00:00
|
|
|
goto fault;
|
|
|
|
if ((len -= copy) == 0)
|
|
|
|
return 0;
|
|
|
|
offset += copy;
|
|
|
|
}
|
|
|
|
start = end;
|
|
|
|
}
|
|
|
|
if (!len)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* This is not really a user copy fault, but rather someone
|
|
|
|
* gave us a bogus length on the skb. We should probably
|
|
|
|
* print a warning here as it may indicate a kernel bug.
|
|
|
|
*/
|
|
|
|
|
|
|
|
fault:
|
2017-02-18 01:16:34 +00:00
|
|
|
iov_iter_revert(to, offset - start_off);
|
2014-11-07 13:22:22 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
short_copy:
|
2018-12-04 01:52:08 +00:00
|
|
|
if (fault_short || iov_iter_count(to))
|
2014-11-07 13:22:22 +00:00
|
|
|
goto fault;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-12-04 01:52:08 +00:00
|
|
|
|
2018-12-04 01:52:10 +00:00
|
|
|
/**
|
|
|
|
* skb_copy_and_hash_datagram_iter - Copy datagram to an iovec iterator
|
|
|
|
* and update a hash.
|
|
|
|
* @skb: buffer to copy
|
|
|
|
* @offset: offset in the buffer to start copying from
|
|
|
|
* @to: iovec iterator to copy to
|
|
|
|
* @len: amount of data to copy from buffer to iovec
|
|
|
|
* @hash: hash request to update
|
|
|
|
*/
|
|
|
|
int skb_copy_and_hash_datagram_iter(const struct sk_buff *skb, int offset,
|
|
|
|
struct iov_iter *to, int len,
|
|
|
|
struct ahash_request *hash)
|
|
|
|
{
|
|
|
|
return __skb_datagram_iter(skb, offset, to, len, true,
|
|
|
|
hash_and_copy_to_iter, hash);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(skb_copy_and_hash_datagram_iter);
|
|
|
|
|
2018-12-04 01:52:08 +00:00
|
|
|
static size_t simple_copy_to_iter(const void *addr, size_t bytes,
|
|
|
|
void *data __always_unused, struct iov_iter *i)
|
|
|
|
{
|
|
|
|
return copy_to_iter(addr, bytes, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* skb_copy_datagram_iter - Copy a datagram to an iovec iterator.
|
|
|
|
* @skb: buffer to copy
|
|
|
|
* @offset: offset in the buffer to start copying from
|
|
|
|
* @to: iovec iterator to copy to
|
|
|
|
* @len: amount of data to copy from buffer to iovec
|
|
|
|
*/
|
|
|
|
int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
|
|
|
|
struct iov_iter *to, int len)
|
|
|
|
{
|
|
|
|
trace_skb_copy_datagram_iovec(skb, len);
|
|
|
|
return __skb_datagram_iter(skb, offset, to, len, false,
|
|
|
|
simple_copy_to_iter, NULL);
|
|
|
|
}
|
2014-11-07 13:22:22 +00:00
|
|
|
EXPORT_SYMBOL(skb_copy_datagram_iter);
|
|
|
|
|
2008-08-15 22:13:53 +00:00
|
|
|
/**
|
2014-11-06 06:10:59 +00:00
|
|
|
* skb_copy_datagram_from_iter - Copy a datagram from an iov_iter.
|
2008-08-15 22:13:53 +00:00
|
|
|
* @skb: buffer to copy
|
|
|
|
* @offset: offset in the buffer to start copying to
|
2014-11-06 06:10:59 +00:00
|
|
|
* @from: the copy source
|
2008-08-15 22:13:53 +00:00
|
|
|
* @len: amount of data to copy to buffer from iovec
|
|
|
|
*
|
|
|
|
* Returns 0 or -EFAULT.
|
|
|
|
*/
|
2014-06-19 18:15:22 +00:00
|
|
|
int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
|
|
|
|
struct iov_iter *from,
|
|
|
|
int len)
|
|
|
|
{
|
|
|
|
int start = skb_headlen(skb);
|
|
|
|
int i, copy = start - offset;
|
|
|
|
struct sk_buff *frag_iter;
|
|
|
|
|
|
|
|
/* Copy header. */
|
|
|
|
if (copy > 0) {
|
|
|
|
if (copy > len)
|
|
|
|
copy = len;
|
|
|
|
if (copy_from_iter(skb->data + offset, copy, from) != copy)
|
|
|
|
goto fault;
|
|
|
|
if ((len -= copy) == 0)
|
|
|
|
return 0;
|
|
|
|
offset += copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy paged appendix. Hmm... why does this look so complicated? */
|
|
|
|
for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
|
|
|
|
int end;
|
|
|
|
const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
|
|
|
|
|
|
|
|
WARN_ON(start > offset + len);
|
|
|
|
|
|
|
|
end = start + skb_frag_size(frag);
|
|
|
|
if ((copy = end - offset) > 0) {
|
|
|
|
size_t copied;
|
|
|
|
|
|
|
|
if (copy > len)
|
|
|
|
copy = len;
|
|
|
|
copied = copy_page_from_iter(skb_frag_page(frag),
|
2019-07-30 14:40:33 +00:00
|
|
|
skb_frag_off(frag) + offset - start,
|
2014-06-19 18:15:22 +00:00
|
|
|
copy, from);
|
|
|
|
if (copied != copy)
|
|
|
|
goto fault;
|
|
|
|
|
|
|
|
if (!(len -= copy))
|
|
|
|
return 0;
|
|
|
|
offset += copy;
|
|
|
|
}
|
|
|
|
start = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
skb_walk_frags(skb, frag_iter) {
|
|
|
|
int end;
|
|
|
|
|
|
|
|
WARN_ON(start > offset + len);
|
|
|
|
|
|
|
|
end = start + frag_iter->len;
|
|
|
|
if ((copy = end - offset) > 0) {
|
|
|
|
if (copy > len)
|
|
|
|
copy = len;
|
|
|
|
if (skb_copy_datagram_from_iter(frag_iter,
|
|
|
|
offset - start,
|
|
|
|
from, copy))
|
|
|
|
goto fault;
|
|
|
|
if ((len -= copy) == 0)
|
|
|
|
return 0;
|
|
|
|
offset += copy;
|
|
|
|
}
|
|
|
|
start = end;
|
|
|
|
}
|
|
|
|
if (!len)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fault:
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(skb_copy_datagram_from_iter);
|
|
|
|
|
2017-08-03 20:29:39 +00:00
|
|
|
int __zerocopy_sg_from_iter(struct sock *sk, struct sk_buff *skb,
|
|
|
|
struct iov_iter *from, size_t length)
|
2014-06-19 18:15:22 +00:00
|
|
|
{
|
2017-08-03 20:29:39 +00:00
|
|
|
int frag = skb_shinfo(skb)->nr_frags;
|
2014-06-19 18:15:22 +00:00
|
|
|
|
2017-08-03 20:29:39 +00:00
|
|
|
while (length && iov_iter_count(from)) {
|
2014-06-19 18:15:22 +00:00
|
|
|
struct page *pages[MAX_SKB_FRAGS];
|
|
|
|
size_t start;
|
|
|
|
ssize_t copied;
|
|
|
|
unsigned long truesize;
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
if (frag == MAX_SKB_FRAGS)
|
|
|
|
return -EMSGSIZE;
|
|
|
|
|
2017-08-03 20:29:39 +00:00
|
|
|
copied = iov_iter_get_pages(from, pages, length,
|
2014-06-19 18:15:22 +00:00
|
|
|
MAX_SKB_FRAGS - frag, &start);
|
|
|
|
if (copied < 0)
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
iov_iter_advance(from, copied);
|
2017-08-03 20:29:39 +00:00
|
|
|
length -= copied;
|
2014-06-19 18:15:22 +00:00
|
|
|
|
|
|
|
truesize = PAGE_ALIGN(copied + start);
|
|
|
|
skb->data_len += copied;
|
|
|
|
skb->len += copied;
|
|
|
|
skb->truesize += truesize;
|
2017-08-03 20:29:39 +00:00
|
|
|
if (sk && sk->sk_type == SOCK_STREAM) {
|
2019-10-11 03:17:46 +00:00
|
|
|
sk_wmem_queued_add(sk, truesize);
|
2017-08-03 20:29:39 +00:00
|
|
|
sk_mem_charge(sk, truesize);
|
|
|
|
} else {
|
|
|
|
refcount_add(truesize, &skb->sk->sk_wmem_alloc);
|
|
|
|
}
|
2014-06-19 18:15:22 +00:00
|
|
|
while (copied) {
|
|
|
|
int size = min_t(int, copied, PAGE_SIZE - start);
|
|
|
|
skb_fill_page_desc(skb, frag++, pages[n], start, size);
|
|
|
|
start = 0;
|
|
|
|
copied -= size;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-08-03 20:29:39 +00:00
|
|
|
EXPORT_SYMBOL(__zerocopy_sg_from_iter);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* zerocopy_sg_from_iter - Build a zerocopy datagram from an iov_iter
|
|
|
|
* @skb: buffer to copy
|
|
|
|
* @from: the source to copy from
|
|
|
|
*
|
|
|
|
* The function will first copy up to headlen, and then pin the userspace
|
|
|
|
* pages and build frags through them.
|
|
|
|
*
|
|
|
|
* Returns 0, -EFAULT or -EMSGSIZE.
|
|
|
|
*/
|
|
|
|
int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *from)
|
|
|
|
{
|
|
|
|
int copy = min_t(int, skb_headlen(skb), iov_iter_count(from));
|
|
|
|
|
|
|
|
/* copy up to skb headlen */
|
|
|
|
if (skb_copy_datagram_from_iter(skb, 0, from, copy))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return __zerocopy_sg_from_iter(NULL, skb, from, ~0U);
|
|
|
|
}
|
2014-06-19 18:15:22 +00:00
|
|
|
EXPORT_SYMBOL(zerocopy_sg_from_iter);
|
|
|
|
|
2018-12-04 01:52:08 +00:00
|
|
|
/**
|
|
|
|
* skb_copy_and_csum_datagram_iter - Copy datagram to an iovec iterator
|
|
|
|
* and update a checksum.
|
|
|
|
* @skb: buffer to copy
|
|
|
|
* @offset: offset in the buffer to start copying from
|
|
|
|
* @to: iovec iterator to copy to
|
|
|
|
* @len: amount of data to copy from buffer to iovec
|
|
|
|
* @csump: checksum pointer
|
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
|
2014-11-24 23:17:55 +00:00
|
|
|
struct iov_iter *to, int len,
|
2006-11-15 05:36:34 +00:00
|
|
|
__wsum *csump)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2018-12-04 01:52:08 +00:00
|
|
|
return __skb_datagram_iter(skb, offset, to, len, true,
|
|
|
|
csum_and_copy_to_iter, csump);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-24 23:17:55 +00:00
|
|
|
* skb_copy_and_csum_datagram_msg - Copy and checksum skb to user iovec.
|
2005-05-01 15:59:25 +00:00
|
|
|
* @skb: skbuff
|
|
|
|
* @hlen: hardware length
|
2014-11-24 23:17:55 +00:00
|
|
|
* @msg: destination
|
2007-02-09 14:24:36 +00:00
|
|
|
*
|
2005-04-16 22:20:36 +00:00
|
|
|
* Caller _must_ check that skb will fit to this iovec.
|
|
|
|
*
|
|
|
|
* Returns: 0 - success.
|
|
|
|
* -EINVAL - checksum failure.
|
2014-11-24 23:17:55 +00:00
|
|
|
* -EFAULT - fault during copy.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2014-11-24 23:17:55 +00:00
|
|
|
int skb_copy_and_csum_datagram_msg(struct sk_buff *skb,
|
|
|
|
int hlen, struct msghdr *msg)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-11-15 05:24:49 +00:00
|
|
|
__wsum csum;
|
2005-04-16 22:20:36 +00:00
|
|
|
int chunk = skb->len - hlen;
|
|
|
|
|
2007-09-06 13:06:35 +00:00
|
|
|
if (!chunk)
|
|
|
|
return 0;
|
|
|
|
|
2014-12-16 02:39:31 +00:00
|
|
|
if (msg_data_left(msg) < chunk) {
|
2005-11-10 21:01:24 +00:00
|
|
|
if (__skb_checksum_complete(skb))
|
2017-04-29 02:38:48 +00:00
|
|
|
return -EINVAL;
|
2014-11-24 23:17:55 +00:00
|
|
|
if (skb_copy_datagram_msg(skb, hlen, msg, chunk))
|
2005-04-16 22:20:36 +00:00
|
|
|
goto fault;
|
|
|
|
} else {
|
|
|
|
csum = csum_partial(skb->data, hlen, skb->csum);
|
2014-11-24 23:17:55 +00:00
|
|
|
if (skb_copy_and_csum_datagram(skb, hlen, &msg->msg_iter,
|
2005-04-16 22:20:36 +00:00
|
|
|
chunk, &csum))
|
|
|
|
goto fault;
|
2017-04-29 02:38:48 +00:00
|
|
|
|
|
|
|
if (csum_fold(csum)) {
|
|
|
|
iov_iter_revert(&msg->msg_iter, chunk);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
net: udp: fix handling of CHECKSUM_COMPLETE packets
Current handling of CHECKSUM_COMPLETE packets by the UDP stack is
incorrect for any packet that has an incorrect checksum value.
udp4/6_csum_init() will both make a call to
__skb_checksum_validate_complete() to initialize/validate the csum
field when receiving a CHECKSUM_COMPLETE packet. When this packet
fails validation, skb->csum will be overwritten with the pseudoheader
checksum so the packet can be fully validated by software, but the
skb->ip_summed value will be left as CHECKSUM_COMPLETE so that way
the stack can later warn the user about their hardware spewing bad
checksums. Unfortunately, leaving the SKB in this state can cause
problems later on in the checksum calculation.
Since the the packet is still marked as CHECKSUM_COMPLETE,
udp_csum_pull_header() will SUBTRACT the checksum of the UDP header
from skb->csum instead of adding it, leaving us with a garbage value
in that field. Once we try to copy the packet to userspace in the
udp4/6_recvmsg(), we'll make a call to skb_copy_and_csum_datagram_msg()
to checksum the packet data and add it in the garbage skb->csum value
to perform our final validation check.
Since the value we're validating is not the proper checksum, it's possible
that the folded value could come out to 0, causing us not to drop the
packet. Instead, we believe that the packet was checksummed incorrectly
by hardware since skb->ip_summed is still CHECKSUM_COMPLETE, and we attempt
to warn the user with netdev_rx_csum_fault(skb->dev);
Unfortunately, since this is the UDP path, skb->dev has been overwritten
by skb->dev_scratch and is no longer a valid pointer, so we end up
reading invalid memory.
This patch addresses this problem in two ways:
1) Do not use the dev pointer when calling netdev_rx_csum_fault()
from skb_copy_and_csum_datagram_msg(). Since this gets called
from the UDP path where skb->dev has been overwritten, we have
no way of knowing if the pointer is still valid. Also for the
sake of consistency with the other uses of
netdev_rx_csum_fault(), don't attempt to call it if the
packet was checksummed by software.
2) Add better CHECKSUM_COMPLETE handling to udp4/6_csum_init().
If we receive a packet that's CHECKSUM_COMPLETE that fails
verification (i.e. skb->csum_valid == 0), check who performed
the calculation. It's possible that the checksum was done in
software by the network stack earlier (such as Netfilter's
CONNTRACK module), and if that says the checksum is bad,
we can drop the packet immediately instead of waiting until
we try and copy it to userspace. Otherwise, we need to
mark the SKB as CHECKSUM_NONE, since the skb->csum field
no longer contains the full packet checksum after the
call to __skb_checksum_validate_complete().
Fixes: e6afc8ace6dd ("udp: remove headers from UDP packets before queueing")
Fixes: c84d949057ca ("udp: copy skb->truesize in the first cache line")
Cc: Sam Kumar <samanthakumar@google.com>
Cc: Eric Dumazet <edumazet@google.com>
Signed-off-by: Sean Tranchetti <stranche@codeaurora.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-10-23 22:04:31 +00:00
|
|
|
if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
|
|
|
|
!skb->csum_complete_sw)
|
2018-11-12 22:47:18 +00:00
|
|
|
netdev_rx_csum_fault(NULL, skb);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
fault:
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
2014-11-24 23:17:55 +00:00
|
|
|
EXPORT_SYMBOL(skb_copy_and_csum_datagram_msg);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* datagram_poll - generic datagram poll
|
2018-06-28 16:43:44 +00:00
|
|
|
* @file: file struct
|
2005-05-01 15:59:25 +00:00
|
|
|
* @sock: socket
|
2018-06-28 16:43:44 +00:00
|
|
|
* @wait: poll table
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* Datagram poll: Again totally generic. This also handles
|
|
|
|
* sequenced packet sockets providing the socket receive queue
|
|
|
|
* is only ever holding data ready to receive.
|
|
|
|
*
|
2017-07-12 16:29:07 +00:00
|
|
|
* Note: when you *don't* use this routine for this protocol,
|
2005-04-16 22:20:36 +00:00
|
|
|
* and you use a different write policy from sock_writeable()
|
|
|
|
* then please supply your own write_space callback.
|
|
|
|
*/
|
2018-06-28 16:43:44 +00:00
|
|
|
__poll_t datagram_poll(struct file *file, struct socket *sock,
|
|
|
|
poll_table *wait)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
2018-06-28 16:43:44 +00:00
|
|
|
__poll_t mask;
|
|
|
|
|
2018-10-23 11:40:39 +00:00
|
|
|
sock_poll_wait(file, sock, wait);
|
2018-06-28 16:43:44 +00:00
|
|
|
mask = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* exceptional events? */
|
2019-10-24 05:44:50 +00:00
|
|
|
if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
|
2018-02-11 22:34:03 +00:00
|
|
|
mask |= EPOLLERR |
|
|
|
|
(sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
|
2013-03-28 11:19:25 +00:00
|
|
|
|
2006-03-25 11:07:39 +00:00
|
|
|
if (sk->sk_shutdown & RCV_SHUTDOWN)
|
2018-02-11 22:34:03 +00:00
|
|
|
mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (sk->sk_shutdown == SHUTDOWN_MASK)
|
2018-02-11 22:34:03 +00:00
|
|
|
mask |= EPOLLHUP;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* readable? */
|
2019-10-24 05:44:50 +00:00
|
|
|
if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
|
2018-02-11 22:34:03 +00:00
|
|
|
mask |= EPOLLIN | EPOLLRDNORM;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* Connection-based need to check for termination and startup */
|
|
|
|
if (connection_based(sk)) {
|
|
|
|
if (sk->sk_state == TCP_CLOSE)
|
2018-02-11 22:34:03 +00:00
|
|
|
mask |= EPOLLHUP;
|
2005-04-16 22:20:36 +00:00
|
|
|
/* connection hasn't started yet? */
|
|
|
|
if (sk->sk_state == TCP_SYN_SENT)
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* writable? */
|
|
|
|
if (sock_writeable(sk))
|
2018-02-11 22:34:03 +00:00
|
|
|
mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
|
2005-04-16 22:20:36 +00:00
|
|
|
else
|
2015-11-30 04:03:10 +00:00
|
|
|
sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
2018-06-28 16:43:44 +00:00
|
|
|
EXPORT_SYMBOL(datagram_poll);
|