2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* INET An implementation of the TCP/IP protocol suite for the LINUX
|
|
|
|
* operating system. INET is implemented using the BSD Socket
|
|
|
|
* interface as the means of communication with the user level.
|
|
|
|
*
|
|
|
|
* The IP to API glue.
|
2007-02-09 22:24:47 +08:00
|
|
|
*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Authors: see ip.c
|
|
|
|
*
|
|
|
|
* Fixes:
|
|
|
|
* Many : Split from ip.c , see ip.c for history.
|
|
|
|
* Martin Mares : TOS setting fixed.
|
2007-02-09 22:24:47 +08:00
|
|
|
* Alan Cox : Fixed a couple of oopses in Martin's
|
2005-04-17 06:20:36 +08:00
|
|
|
* TOS tweaks.
|
|
|
|
* Mike McLagan : Routing by source
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/ip.h>
|
|
|
|
#include <linux/icmp.h>
|
2005-12-27 12:43:12 +08:00
|
|
|
#include <linux/inetdevice.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/netdevice.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 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/ip.h>
|
|
|
|
#include <net/icmp.h>
|
2005-12-14 15:26:10 +08:00
|
|
|
#include <net/tcp_states.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/udp.h>
|
|
|
|
#include <linux/igmp.h>
|
|
|
|
#include <linux/netfilter.h>
|
|
|
|
#include <linux/route.h>
|
|
|
|
#include <linux/mroute.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/xfrm.h>
|
2008-04-27 16:06:07 +08:00
|
|
|
#include <net/compat.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
|
|
|
|
#include <net/transp_v6.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <linux/errqueue.h>
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
|
|
|
|
#define IP_CMSG_PKTINFO 1
|
|
|
|
#define IP_CMSG_TTL 2
|
|
|
|
#define IP_CMSG_TOS 4
|
|
|
|
#define IP_CMSG_RECVOPTS 8
|
|
|
|
#define IP_CMSG_RETOPTS 16
|
[SECURITY]: TCP/UDP getpeersec
This patch implements an application of the LSM-IPSec networking
controls whereby an application can determine the label of the
security association its TCP or UDP sockets are currently connected to
via getsockopt and the auxiliary data mechanism of recvmsg.
Patch purpose:
This patch enables a security-aware application to retrieve the
security context of an IPSec security association a particular TCP or
UDP socket is using. The application can then use this security
context to determine the security context for processing on behalf of
the peer at the other end of this connection. In the case of UDP, the
security context is for each individual packet. An example
application is the inetd daemon, which could be modified to start
daemons running at security contexts dependent on the remote client.
Patch design approach:
- Design for TCP
The patch enables the SELinux LSM to set the peer security context for
a socket based on the security context of the IPSec security
association. The application may retrieve this context using
getsockopt. When called, the kernel determines if the socket is a
connected (TCP_ESTABLISHED) TCP socket and, if so, uses the dst_entry
cache on the socket to retrieve the security associations. If a
security association has a security context, the context string is
returned, as for UNIX domain sockets.
- Design for UDP
Unlike TCP, UDP is connectionless. This requires a somewhat different
API to retrieve the peer security context. With TCP, the peer
security context stays the same throughout the connection, thus it can
be retrieved at any time between when the connection is established
and when it is torn down. With UDP, each read/write can have
different peer and thus the security context might change every time.
As a result the security context retrieval must be done TOGETHER with
the packet retrieval.
The solution is to build upon the existing Unix domain socket API for
retrieving user credentials. Linux offers the API for obtaining user
credentials via ancillary messages (i.e., out of band/control messages
that are bundled together with a normal message).
Patch implementation details:
- Implementation for TCP
The security context can be retrieved by applications using getsockopt
with the existing SO_PEERSEC flag. As an example (ignoring error
checking):
getsockopt(sockfd, SOL_SOCKET, SO_PEERSEC, optbuf, &optlen);
printf("Socket peer context is: %s\n", optbuf);
The SELinux function, selinux_socket_getpeersec, is extended to check
for labeled security associations for connected (TCP_ESTABLISHED ==
sk->sk_state) TCP sockets only. If so, the socket has a dst_cache of
struct dst_entry values that may refer to security associations. If
these have security associations with security contexts, the security
context is returned.
getsockopt returns a buffer that contains a security context string or
the buffer is unmodified.
- Implementation for UDP
To retrieve the security context, the application first indicates to
the kernel such desire by setting the IP_PASSSEC option via
getsockopt. Then the application retrieves the security context using
the auxiliary data mechanism.
An example server application for UDP should look like this:
toggle = 1;
toggle_len = sizeof(toggle);
setsockopt(sockfd, SOL_IP, IP_PASSSEC, &toggle, &toggle_len);
recvmsg(sockfd, &msg_hdr, 0);
if (msg_hdr.msg_controllen > sizeof(struct cmsghdr)) {
cmsg_hdr = CMSG_FIRSTHDR(&msg_hdr);
if (cmsg_hdr->cmsg_len <= CMSG_LEN(sizeof(scontext)) &&
cmsg_hdr->cmsg_level == SOL_IP &&
cmsg_hdr->cmsg_type == SCM_SECURITY) {
memcpy(&scontext, CMSG_DATA(cmsg_hdr), sizeof(scontext));
}
}
ip_setsockopt is enhanced with a new socket option IP_PASSSEC to allow
a server socket to receive security context of the peer. A new
ancillary message type SCM_SECURITY.
When the packet is received we get the security context from the
sec_path pointer which is contained in the sk_buff, and copy it to the
ancillary message space. An additional LSM hook,
selinux_socket_getpeersec_udp, is defined to retrieve the security
context from the SELinux space. The existing function,
selinux_socket_getpeersec does not suit our purpose, because the
security context is copied directly to user space, rather than to
kernel space.
Testing:
We have tested the patch by setting up TCP and UDP connections between
applications on two machines using the IPSec policies that result in
labeled security associations being built. For TCP, we can then
extract the peer security context using getsockopt on either end. For
UDP, the receiving end can retrieve the security context using the
auxiliary data mechanism of recvmsg.
Signed-off-by: Catherine Zhang <cxzhang@watson.ibm.com>
Acked-by: James Morris <jmorris@namei.org>
Acked-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-03-21 14:41:23 +08:00
|
|
|
#define IP_CMSG_PASSSEC 32
|
2008-11-17 11:32:39 +08:00
|
|
|
#define IP_CMSG_ORIGDSTADDR 64
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SOL_IP control messages.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct in_pktinfo info;
|
2009-06-02 13:14:27 +08:00
|
|
|
struct rtable *rt = skb_rtable(skb);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-04-21 13:47:35 +08:00
|
|
|
info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (rt) {
|
|
|
|
info.ipi_ifindex = rt->rt_iif;
|
|
|
|
info.ipi_spec_dst.s_addr = rt->rt_spec_dst;
|
|
|
|
} else {
|
|
|
|
info.ipi_ifindex = 0;
|
|
|
|
info.ipi_spec_dst.s_addr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
|
|
|
|
{
|
2007-04-21 13:47:35 +08:00
|
|
|
int ttl = ip_hdr(skb)->ttl;
|
2005-04-17 06:20:36 +08:00
|
|
|
put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
|
|
|
|
{
|
2007-04-21 13:47:35 +08:00
|
|
|
put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
if (IPCB(skb)->opt.optlen == 0)
|
|
|
|
return;
|
|
|
|
|
2007-04-21 13:47:35 +08:00
|
|
|
put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
|
|
|
|
ip_hdr(skb) + 1);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
unsigned char optbuf[sizeof(struct ip_options) + 40];
|
2008-11-03 16:27:11 +08:00
|
|
|
struct ip_options * opt = (struct ip_options *)optbuf;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (IPCB(skb)->opt.optlen == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ip_options_echo(opt, skb)) {
|
|
|
|
msg->msg_flags |= MSG_CTRUNC;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ip_options_undo(opt);
|
|
|
|
|
|
|
|
put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
|
|
|
|
}
|
|
|
|
|
[SECURITY]: TCP/UDP getpeersec
This patch implements an application of the LSM-IPSec networking
controls whereby an application can determine the label of the
security association its TCP or UDP sockets are currently connected to
via getsockopt and the auxiliary data mechanism of recvmsg.
Patch purpose:
This patch enables a security-aware application to retrieve the
security context of an IPSec security association a particular TCP or
UDP socket is using. The application can then use this security
context to determine the security context for processing on behalf of
the peer at the other end of this connection. In the case of UDP, the
security context is for each individual packet. An example
application is the inetd daemon, which could be modified to start
daemons running at security contexts dependent on the remote client.
Patch design approach:
- Design for TCP
The patch enables the SELinux LSM to set the peer security context for
a socket based on the security context of the IPSec security
association. The application may retrieve this context using
getsockopt. When called, the kernel determines if the socket is a
connected (TCP_ESTABLISHED) TCP socket and, if so, uses the dst_entry
cache on the socket to retrieve the security associations. If a
security association has a security context, the context string is
returned, as for UNIX domain sockets.
- Design for UDP
Unlike TCP, UDP is connectionless. This requires a somewhat different
API to retrieve the peer security context. With TCP, the peer
security context stays the same throughout the connection, thus it can
be retrieved at any time between when the connection is established
and when it is torn down. With UDP, each read/write can have
different peer and thus the security context might change every time.
As a result the security context retrieval must be done TOGETHER with
the packet retrieval.
The solution is to build upon the existing Unix domain socket API for
retrieving user credentials. Linux offers the API for obtaining user
credentials via ancillary messages (i.e., out of band/control messages
that are bundled together with a normal message).
Patch implementation details:
- Implementation for TCP
The security context can be retrieved by applications using getsockopt
with the existing SO_PEERSEC flag. As an example (ignoring error
checking):
getsockopt(sockfd, SOL_SOCKET, SO_PEERSEC, optbuf, &optlen);
printf("Socket peer context is: %s\n", optbuf);
The SELinux function, selinux_socket_getpeersec, is extended to check
for labeled security associations for connected (TCP_ESTABLISHED ==
sk->sk_state) TCP sockets only. If so, the socket has a dst_cache of
struct dst_entry values that may refer to security associations. If
these have security associations with security contexts, the security
context is returned.
getsockopt returns a buffer that contains a security context string or
the buffer is unmodified.
- Implementation for UDP
To retrieve the security context, the application first indicates to
the kernel such desire by setting the IP_PASSSEC option via
getsockopt. Then the application retrieves the security context using
the auxiliary data mechanism.
An example server application for UDP should look like this:
toggle = 1;
toggle_len = sizeof(toggle);
setsockopt(sockfd, SOL_IP, IP_PASSSEC, &toggle, &toggle_len);
recvmsg(sockfd, &msg_hdr, 0);
if (msg_hdr.msg_controllen > sizeof(struct cmsghdr)) {
cmsg_hdr = CMSG_FIRSTHDR(&msg_hdr);
if (cmsg_hdr->cmsg_len <= CMSG_LEN(sizeof(scontext)) &&
cmsg_hdr->cmsg_level == SOL_IP &&
cmsg_hdr->cmsg_type == SCM_SECURITY) {
memcpy(&scontext, CMSG_DATA(cmsg_hdr), sizeof(scontext));
}
}
ip_setsockopt is enhanced with a new socket option IP_PASSSEC to allow
a server socket to receive security context of the peer. A new
ancillary message type SCM_SECURITY.
When the packet is received we get the security context from the
sec_path pointer which is contained in the sk_buff, and copy it to the
ancillary message space. An additional LSM hook,
selinux_socket_getpeersec_udp, is defined to retrieve the security
context from the SELinux space. The existing function,
selinux_socket_getpeersec does not suit our purpose, because the
security context is copied directly to user space, rather than to
kernel space.
Testing:
We have tested the patch by setting up TCP and UDP connections between
applications on two machines using the IPSec policies that result in
labeled security associations being built. For TCP, we can then
extract the peer security context using getsockopt on either end. For
UDP, the receiving end can retrieve the security context using the
auxiliary data mechanism of recvmsg.
Signed-off-by: Catherine Zhang <cxzhang@watson.ibm.com>
Acked-by: James Morris <jmorris@namei.org>
Acked-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-03-21 14:41:23 +08:00
|
|
|
static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
char *secdata;
|
2006-08-03 05:12:06 +08:00
|
|
|
u32 seclen, secid;
|
[SECURITY]: TCP/UDP getpeersec
This patch implements an application of the LSM-IPSec networking
controls whereby an application can determine the label of the
security association its TCP or UDP sockets are currently connected to
via getsockopt and the auxiliary data mechanism of recvmsg.
Patch purpose:
This patch enables a security-aware application to retrieve the
security context of an IPSec security association a particular TCP or
UDP socket is using. The application can then use this security
context to determine the security context for processing on behalf of
the peer at the other end of this connection. In the case of UDP, the
security context is for each individual packet. An example
application is the inetd daemon, which could be modified to start
daemons running at security contexts dependent on the remote client.
Patch design approach:
- Design for TCP
The patch enables the SELinux LSM to set the peer security context for
a socket based on the security context of the IPSec security
association. The application may retrieve this context using
getsockopt. When called, the kernel determines if the socket is a
connected (TCP_ESTABLISHED) TCP socket and, if so, uses the dst_entry
cache on the socket to retrieve the security associations. If a
security association has a security context, the context string is
returned, as for UNIX domain sockets.
- Design for UDP
Unlike TCP, UDP is connectionless. This requires a somewhat different
API to retrieve the peer security context. With TCP, the peer
security context stays the same throughout the connection, thus it can
be retrieved at any time between when the connection is established
and when it is torn down. With UDP, each read/write can have
different peer and thus the security context might change every time.
As a result the security context retrieval must be done TOGETHER with
the packet retrieval.
The solution is to build upon the existing Unix domain socket API for
retrieving user credentials. Linux offers the API for obtaining user
credentials via ancillary messages (i.e., out of band/control messages
that are bundled together with a normal message).
Patch implementation details:
- Implementation for TCP
The security context can be retrieved by applications using getsockopt
with the existing SO_PEERSEC flag. As an example (ignoring error
checking):
getsockopt(sockfd, SOL_SOCKET, SO_PEERSEC, optbuf, &optlen);
printf("Socket peer context is: %s\n", optbuf);
The SELinux function, selinux_socket_getpeersec, is extended to check
for labeled security associations for connected (TCP_ESTABLISHED ==
sk->sk_state) TCP sockets only. If so, the socket has a dst_cache of
struct dst_entry values that may refer to security associations. If
these have security associations with security contexts, the security
context is returned.
getsockopt returns a buffer that contains a security context string or
the buffer is unmodified.
- Implementation for UDP
To retrieve the security context, the application first indicates to
the kernel such desire by setting the IP_PASSSEC option via
getsockopt. Then the application retrieves the security context using
the auxiliary data mechanism.
An example server application for UDP should look like this:
toggle = 1;
toggle_len = sizeof(toggle);
setsockopt(sockfd, SOL_IP, IP_PASSSEC, &toggle, &toggle_len);
recvmsg(sockfd, &msg_hdr, 0);
if (msg_hdr.msg_controllen > sizeof(struct cmsghdr)) {
cmsg_hdr = CMSG_FIRSTHDR(&msg_hdr);
if (cmsg_hdr->cmsg_len <= CMSG_LEN(sizeof(scontext)) &&
cmsg_hdr->cmsg_level == SOL_IP &&
cmsg_hdr->cmsg_type == SCM_SECURITY) {
memcpy(&scontext, CMSG_DATA(cmsg_hdr), sizeof(scontext));
}
}
ip_setsockopt is enhanced with a new socket option IP_PASSSEC to allow
a server socket to receive security context of the peer. A new
ancillary message type SCM_SECURITY.
When the packet is received we get the security context from the
sec_path pointer which is contained in the sk_buff, and copy it to the
ancillary message space. An additional LSM hook,
selinux_socket_getpeersec_udp, is defined to retrieve the security
context from the SELinux space. The existing function,
selinux_socket_getpeersec does not suit our purpose, because the
security context is copied directly to user space, rather than to
kernel space.
Testing:
We have tested the patch by setting up TCP and UDP connections between
applications on two machines using the IPSec policies that result in
labeled security associations being built. For TCP, we can then
extract the peer security context using getsockopt on either end. For
UDP, the receiving end can retrieve the security context using the
auxiliary data mechanism of recvmsg.
Signed-off-by: Catherine Zhang <cxzhang@watson.ibm.com>
Acked-by: James Morris <jmorris@namei.org>
Acked-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-03-21 14:41:23 +08:00
|
|
|
int err;
|
|
|
|
|
2006-08-03 05:12:06 +08:00
|
|
|
err = security_socket_getpeersec_dgram(NULL, skb, &secid);
|
|
|
|
if (err)
|
|
|
|
return;
|
|
|
|
|
|
|
|
err = security_secid_to_secctx(secid, &secdata, &seclen);
|
[SECURITY]: TCP/UDP getpeersec
This patch implements an application of the LSM-IPSec networking
controls whereby an application can determine the label of the
security association its TCP or UDP sockets are currently connected to
via getsockopt and the auxiliary data mechanism of recvmsg.
Patch purpose:
This patch enables a security-aware application to retrieve the
security context of an IPSec security association a particular TCP or
UDP socket is using. The application can then use this security
context to determine the security context for processing on behalf of
the peer at the other end of this connection. In the case of UDP, the
security context is for each individual packet. An example
application is the inetd daemon, which could be modified to start
daemons running at security contexts dependent on the remote client.
Patch design approach:
- Design for TCP
The patch enables the SELinux LSM to set the peer security context for
a socket based on the security context of the IPSec security
association. The application may retrieve this context using
getsockopt. When called, the kernel determines if the socket is a
connected (TCP_ESTABLISHED) TCP socket and, if so, uses the dst_entry
cache on the socket to retrieve the security associations. If a
security association has a security context, the context string is
returned, as for UNIX domain sockets.
- Design for UDP
Unlike TCP, UDP is connectionless. This requires a somewhat different
API to retrieve the peer security context. With TCP, the peer
security context stays the same throughout the connection, thus it can
be retrieved at any time between when the connection is established
and when it is torn down. With UDP, each read/write can have
different peer and thus the security context might change every time.
As a result the security context retrieval must be done TOGETHER with
the packet retrieval.
The solution is to build upon the existing Unix domain socket API for
retrieving user credentials. Linux offers the API for obtaining user
credentials via ancillary messages (i.e., out of band/control messages
that are bundled together with a normal message).
Patch implementation details:
- Implementation for TCP
The security context can be retrieved by applications using getsockopt
with the existing SO_PEERSEC flag. As an example (ignoring error
checking):
getsockopt(sockfd, SOL_SOCKET, SO_PEERSEC, optbuf, &optlen);
printf("Socket peer context is: %s\n", optbuf);
The SELinux function, selinux_socket_getpeersec, is extended to check
for labeled security associations for connected (TCP_ESTABLISHED ==
sk->sk_state) TCP sockets only. If so, the socket has a dst_cache of
struct dst_entry values that may refer to security associations. If
these have security associations with security contexts, the security
context is returned.
getsockopt returns a buffer that contains a security context string or
the buffer is unmodified.
- Implementation for UDP
To retrieve the security context, the application first indicates to
the kernel such desire by setting the IP_PASSSEC option via
getsockopt. Then the application retrieves the security context using
the auxiliary data mechanism.
An example server application for UDP should look like this:
toggle = 1;
toggle_len = sizeof(toggle);
setsockopt(sockfd, SOL_IP, IP_PASSSEC, &toggle, &toggle_len);
recvmsg(sockfd, &msg_hdr, 0);
if (msg_hdr.msg_controllen > sizeof(struct cmsghdr)) {
cmsg_hdr = CMSG_FIRSTHDR(&msg_hdr);
if (cmsg_hdr->cmsg_len <= CMSG_LEN(sizeof(scontext)) &&
cmsg_hdr->cmsg_level == SOL_IP &&
cmsg_hdr->cmsg_type == SCM_SECURITY) {
memcpy(&scontext, CMSG_DATA(cmsg_hdr), sizeof(scontext));
}
}
ip_setsockopt is enhanced with a new socket option IP_PASSSEC to allow
a server socket to receive security context of the peer. A new
ancillary message type SCM_SECURITY.
When the packet is received we get the security context from the
sec_path pointer which is contained in the sk_buff, and copy it to the
ancillary message space. An additional LSM hook,
selinux_socket_getpeersec_udp, is defined to retrieve the security
context from the SELinux space. The existing function,
selinux_socket_getpeersec does not suit our purpose, because the
security context is copied directly to user space, rather than to
kernel space.
Testing:
We have tested the patch by setting up TCP and UDP connections between
applications on two machines using the IPSec policies that result in
labeled security associations being built. For TCP, we can then
extract the peer security context using getsockopt on either end. For
UDP, the receiving end can retrieve the security context using the
auxiliary data mechanism of recvmsg.
Signed-off-by: Catherine Zhang <cxzhang@watson.ibm.com>
Acked-by: James Morris <jmorris@namei.org>
Acked-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-03-21 14:41:23 +08:00
|
|
|
if (err)
|
|
|
|
return;
|
|
|
|
|
|
|
|
put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
|
2006-08-03 05:12:06 +08:00
|
|
|
security_release_secctx(secdata, seclen);
|
[SECURITY]: TCP/UDP getpeersec
This patch implements an application of the LSM-IPSec networking
controls whereby an application can determine the label of the
security association its TCP or UDP sockets are currently connected to
via getsockopt and the auxiliary data mechanism of recvmsg.
Patch purpose:
This patch enables a security-aware application to retrieve the
security context of an IPSec security association a particular TCP or
UDP socket is using. The application can then use this security
context to determine the security context for processing on behalf of
the peer at the other end of this connection. In the case of UDP, the
security context is for each individual packet. An example
application is the inetd daemon, which could be modified to start
daemons running at security contexts dependent on the remote client.
Patch design approach:
- Design for TCP
The patch enables the SELinux LSM to set the peer security context for
a socket based on the security context of the IPSec security
association. The application may retrieve this context using
getsockopt. When called, the kernel determines if the socket is a
connected (TCP_ESTABLISHED) TCP socket and, if so, uses the dst_entry
cache on the socket to retrieve the security associations. If a
security association has a security context, the context string is
returned, as for UNIX domain sockets.
- Design for UDP
Unlike TCP, UDP is connectionless. This requires a somewhat different
API to retrieve the peer security context. With TCP, the peer
security context stays the same throughout the connection, thus it can
be retrieved at any time between when the connection is established
and when it is torn down. With UDP, each read/write can have
different peer and thus the security context might change every time.
As a result the security context retrieval must be done TOGETHER with
the packet retrieval.
The solution is to build upon the existing Unix domain socket API for
retrieving user credentials. Linux offers the API for obtaining user
credentials via ancillary messages (i.e., out of band/control messages
that are bundled together with a normal message).
Patch implementation details:
- Implementation for TCP
The security context can be retrieved by applications using getsockopt
with the existing SO_PEERSEC flag. As an example (ignoring error
checking):
getsockopt(sockfd, SOL_SOCKET, SO_PEERSEC, optbuf, &optlen);
printf("Socket peer context is: %s\n", optbuf);
The SELinux function, selinux_socket_getpeersec, is extended to check
for labeled security associations for connected (TCP_ESTABLISHED ==
sk->sk_state) TCP sockets only. If so, the socket has a dst_cache of
struct dst_entry values that may refer to security associations. If
these have security associations with security contexts, the security
context is returned.
getsockopt returns a buffer that contains a security context string or
the buffer is unmodified.
- Implementation for UDP
To retrieve the security context, the application first indicates to
the kernel such desire by setting the IP_PASSSEC option via
getsockopt. Then the application retrieves the security context using
the auxiliary data mechanism.
An example server application for UDP should look like this:
toggle = 1;
toggle_len = sizeof(toggle);
setsockopt(sockfd, SOL_IP, IP_PASSSEC, &toggle, &toggle_len);
recvmsg(sockfd, &msg_hdr, 0);
if (msg_hdr.msg_controllen > sizeof(struct cmsghdr)) {
cmsg_hdr = CMSG_FIRSTHDR(&msg_hdr);
if (cmsg_hdr->cmsg_len <= CMSG_LEN(sizeof(scontext)) &&
cmsg_hdr->cmsg_level == SOL_IP &&
cmsg_hdr->cmsg_type == SCM_SECURITY) {
memcpy(&scontext, CMSG_DATA(cmsg_hdr), sizeof(scontext));
}
}
ip_setsockopt is enhanced with a new socket option IP_PASSSEC to allow
a server socket to receive security context of the peer. A new
ancillary message type SCM_SECURITY.
When the packet is received we get the security context from the
sec_path pointer which is contained in the sk_buff, and copy it to the
ancillary message space. An additional LSM hook,
selinux_socket_getpeersec_udp, is defined to retrieve the security
context from the SELinux space. The existing function,
selinux_socket_getpeersec does not suit our purpose, because the
security context is copied directly to user space, rather than to
kernel space.
Testing:
We have tested the patch by setting up TCP and UDP connections between
applications on two machines using the IPSec policies that result in
labeled security associations being built. For TCP, we can then
extract the peer security context using getsockopt on either end. For
UDP, the receiving end can retrieve the security context using the
auxiliary data mechanism of recvmsg.
Signed-off-by: Catherine Zhang <cxzhang@watson.ibm.com>
Acked-by: James Morris <jmorris@namei.org>
Acked-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-03-21 14:41:23 +08:00
|
|
|
}
|
|
|
|
|
2008-11-20 17:54:27 +08:00
|
|
|
static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
|
2008-11-17 11:32:39 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_in sin;
|
2011-04-22 12:53:02 +08:00
|
|
|
const struct iphdr *iph = ip_hdr(skb);
|
2008-11-20 17:54:27 +08:00
|
|
|
__be16 *ports = (__be16 *)skb_transport_header(skb);
|
2008-11-17 11:32:39 +08:00
|
|
|
|
|
|
|
if (skb_transport_offset(skb) + 4 > skb->len)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* All current transport protocols have the port numbers in the
|
|
|
|
* first four bytes of the transport header and this function is
|
|
|
|
* written with this assumption in mind.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_addr.s_addr = iph->daddr;
|
|
|
|
sin.sin_port = ports[1];
|
|
|
|
memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
|
|
|
|
|
|
|
|
put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct inet_sock *inet = inet_sk(skb->sk);
|
|
|
|
unsigned flags = inet->cmsg_flags;
|
|
|
|
|
|
|
|
/* Ordered by supposed usage frequency */
|
|
|
|
if (flags & 1)
|
|
|
|
ip_cmsg_recv_pktinfo(msg, skb);
|
2009-06-02 15:42:16 +08:00
|
|
|
if ((flags >>= 1) == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (flags & 1)
|
|
|
|
ip_cmsg_recv_ttl(msg, skb);
|
2009-06-02 15:42:16 +08:00
|
|
|
if ((flags >>= 1) == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (flags & 1)
|
|
|
|
ip_cmsg_recv_tos(msg, skb);
|
2009-06-02 15:42:16 +08:00
|
|
|
if ((flags >>= 1) == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (flags & 1)
|
|
|
|
ip_cmsg_recv_opts(msg, skb);
|
2009-06-02 15:42:16 +08:00
|
|
|
if ((flags >>= 1) == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (flags & 1)
|
|
|
|
ip_cmsg_recv_retopts(msg, skb);
|
2009-06-02 15:42:16 +08:00
|
|
|
if ((flags >>= 1) == 0)
|
[SECURITY]: TCP/UDP getpeersec
This patch implements an application of the LSM-IPSec networking
controls whereby an application can determine the label of the
security association its TCP or UDP sockets are currently connected to
via getsockopt and the auxiliary data mechanism of recvmsg.
Patch purpose:
This patch enables a security-aware application to retrieve the
security context of an IPSec security association a particular TCP or
UDP socket is using. The application can then use this security
context to determine the security context for processing on behalf of
the peer at the other end of this connection. In the case of UDP, the
security context is for each individual packet. An example
application is the inetd daemon, which could be modified to start
daemons running at security contexts dependent on the remote client.
Patch design approach:
- Design for TCP
The patch enables the SELinux LSM to set the peer security context for
a socket based on the security context of the IPSec security
association. The application may retrieve this context using
getsockopt. When called, the kernel determines if the socket is a
connected (TCP_ESTABLISHED) TCP socket and, if so, uses the dst_entry
cache on the socket to retrieve the security associations. If a
security association has a security context, the context string is
returned, as for UNIX domain sockets.
- Design for UDP
Unlike TCP, UDP is connectionless. This requires a somewhat different
API to retrieve the peer security context. With TCP, the peer
security context stays the same throughout the connection, thus it can
be retrieved at any time between when the connection is established
and when it is torn down. With UDP, each read/write can have
different peer and thus the security context might change every time.
As a result the security context retrieval must be done TOGETHER with
the packet retrieval.
The solution is to build upon the existing Unix domain socket API for
retrieving user credentials. Linux offers the API for obtaining user
credentials via ancillary messages (i.e., out of band/control messages
that are bundled together with a normal message).
Patch implementation details:
- Implementation for TCP
The security context can be retrieved by applications using getsockopt
with the existing SO_PEERSEC flag. As an example (ignoring error
checking):
getsockopt(sockfd, SOL_SOCKET, SO_PEERSEC, optbuf, &optlen);
printf("Socket peer context is: %s\n", optbuf);
The SELinux function, selinux_socket_getpeersec, is extended to check
for labeled security associations for connected (TCP_ESTABLISHED ==
sk->sk_state) TCP sockets only. If so, the socket has a dst_cache of
struct dst_entry values that may refer to security associations. If
these have security associations with security contexts, the security
context is returned.
getsockopt returns a buffer that contains a security context string or
the buffer is unmodified.
- Implementation for UDP
To retrieve the security context, the application first indicates to
the kernel such desire by setting the IP_PASSSEC option via
getsockopt. Then the application retrieves the security context using
the auxiliary data mechanism.
An example server application for UDP should look like this:
toggle = 1;
toggle_len = sizeof(toggle);
setsockopt(sockfd, SOL_IP, IP_PASSSEC, &toggle, &toggle_len);
recvmsg(sockfd, &msg_hdr, 0);
if (msg_hdr.msg_controllen > sizeof(struct cmsghdr)) {
cmsg_hdr = CMSG_FIRSTHDR(&msg_hdr);
if (cmsg_hdr->cmsg_len <= CMSG_LEN(sizeof(scontext)) &&
cmsg_hdr->cmsg_level == SOL_IP &&
cmsg_hdr->cmsg_type == SCM_SECURITY) {
memcpy(&scontext, CMSG_DATA(cmsg_hdr), sizeof(scontext));
}
}
ip_setsockopt is enhanced with a new socket option IP_PASSSEC to allow
a server socket to receive security context of the peer. A new
ancillary message type SCM_SECURITY.
When the packet is received we get the security context from the
sec_path pointer which is contained in the sk_buff, and copy it to the
ancillary message space. An additional LSM hook,
selinux_socket_getpeersec_udp, is defined to retrieve the security
context from the SELinux space. The existing function,
selinux_socket_getpeersec does not suit our purpose, because the
security context is copied directly to user space, rather than to
kernel space.
Testing:
We have tested the patch by setting up TCP and UDP connections between
applications on two machines using the IPSec policies that result in
labeled security associations being built. For TCP, we can then
extract the peer security context using getsockopt on either end. For
UDP, the receiving end can retrieve the security context using the
auxiliary data mechanism of recvmsg.
Signed-off-by: Catherine Zhang <cxzhang@watson.ibm.com>
Acked-by: James Morris <jmorris@namei.org>
Acked-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-03-21 14:41:23 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (flags & 1)
|
|
|
|
ip_cmsg_recv_security(msg, skb);
|
2008-11-17 11:32:39 +08:00
|
|
|
|
2009-06-02 15:42:16 +08:00
|
|
|
if ((flags >>= 1) == 0)
|
2008-11-17 11:32:39 +08:00
|
|
|
return;
|
|
|
|
if (flags & 1)
|
|
|
|
ip_cmsg_recv_dstaddr(msg, skb);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2009-06-02 15:42:16 +08:00
|
|
|
EXPORT_SYMBOL(ip_cmsg_recv);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-03-25 06:30:27 +08:00
|
|
|
int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
|
|
|
|
for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
|
|
|
|
if (!CMSG_OK(msg, cmsg))
|
|
|
|
return -EINVAL;
|
|
|
|
if (cmsg->cmsg_level != SOL_IP)
|
|
|
|
continue;
|
|
|
|
switch (cmsg->cmsg_type) {
|
|
|
|
case IP_RETOPTS:
|
|
|
|
err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
|
2009-06-02 15:42:16 +08:00
|
|
|
err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
|
|
|
|
err < 40 ? err : 40);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
break;
|
|
|
|
case IP_PKTINFO:
|
|
|
|
{
|
|
|
|
struct in_pktinfo *info;
|
|
|
|
if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
|
|
|
|
return -EINVAL;
|
|
|
|
info = (struct in_pktinfo *)CMSG_DATA(cmsg);
|
|
|
|
ipc->oif = info->ipi_ifindex;
|
|
|
|
ipc->addr = info->ipi_spec_dst.s_addr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Special input handler for packets caught by router alert option.
|
|
|
|
They are selected only by protocol field, and then processed likely
|
|
|
|
local ones; but only if someone wants them! Otherwise, router
|
|
|
|
not running rsvpd will kill RSVP.
|
|
|
|
|
|
|
|
It is user level problem, what it will make with them.
|
|
|
|
I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
|
|
|
|
but receiver should be enough clever f.e. to forward mtrace requests,
|
|
|
|
sent to multicast group to reach destination designated router.
|
|
|
|
*/
|
2010-10-25 11:32:44 +08:00
|
|
|
struct ip_ra_chain __rcu *ip_ra_chain;
|
2010-06-07 11:12:08 +08:00
|
|
|
static DEFINE_SPINLOCK(ip_ra_lock);
|
|
|
|
|
2010-06-10 00:21:07 +08:00
|
|
|
|
|
|
|
static void ip_ra_destroy_rcu(struct rcu_head *head)
|
2010-06-07 11:12:08 +08:00
|
|
|
{
|
2010-06-10 00:21:07 +08:00
|
|
|
struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
|
|
|
|
|
|
|
|
sock_put(ra->saved_sk);
|
|
|
|
kfree(ra);
|
2010-06-07 11:12:08 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-06-02 15:42:16 +08:00
|
|
|
int ip_ra_control(struct sock *sk, unsigned char on,
|
|
|
|
void (*destructor)(struct sock *))
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2010-10-25 11:32:44 +08:00
|
|
|
struct ip_ra_chain *ra, *new_ra;
|
|
|
|
struct ip_ra_chain __rcu **rap;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-10-15 14:30:45 +08:00
|
|
|
if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
|
|
|
|
|
2010-06-07 11:12:08 +08:00
|
|
|
spin_lock_bh(&ip_ra_lock);
|
2010-10-25 11:32:44 +08:00
|
|
|
for (rap = &ip_ra_chain;
|
|
|
|
(ra = rcu_dereference_protected(*rap,
|
|
|
|
lockdep_is_held(&ip_ra_lock))) != NULL;
|
|
|
|
rap = &ra->next) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ra->sk == sk) {
|
|
|
|
if (on) {
|
2010-06-07 11:12:08 +08:00
|
|
|
spin_unlock_bh(&ip_ra_lock);
|
2005-11-09 01:41:34 +08:00
|
|
|
kfree(new_ra);
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EADDRINUSE;
|
|
|
|
}
|
2010-06-10 00:21:07 +08:00
|
|
|
/* dont let ip_call_ra_chain() use sk again */
|
|
|
|
ra->sk = NULL;
|
2010-06-07 11:12:08 +08:00
|
|
|
rcu_assign_pointer(*rap, ra->next);
|
|
|
|
spin_unlock_bh(&ip_ra_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (ra->destructor)
|
|
|
|
ra->destructor(sk);
|
2010-06-10 00:21:07 +08:00
|
|
|
/*
|
|
|
|
* Delay sock_put(sk) and kfree(ra) after one rcu grace
|
|
|
|
* period. This guarantee ip_call_ra_chain() dont need
|
|
|
|
* to mess with socket refcounts.
|
|
|
|
*/
|
|
|
|
ra->saved_sk = sk;
|
|
|
|
call_rcu(&ra->rcu, ip_ra_destroy_rcu);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (new_ra == NULL) {
|
2010-06-07 11:12:08 +08:00
|
|
|
spin_unlock_bh(&ip_ra_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
new_ra->sk = sk;
|
|
|
|
new_ra->destructor = destructor;
|
|
|
|
|
|
|
|
new_ra->next = ra;
|
2010-06-07 11:12:08 +08:00
|
|
|
rcu_assign_pointer(*rap, new_ra);
|
2005-04-17 06:20:36 +08:00
|
|
|
sock_hold(sk);
|
2010-06-07 11:12:08 +08:00
|
|
|
spin_unlock_bh(&ip_ra_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-09 22:24:47 +08:00
|
|
|
void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
|
2006-09-28 09:34:21 +08:00
|
|
|
__be16 port, u32 info, u8 *payload)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct sock_exterr_skb *serr;
|
|
|
|
|
|
|
|
skb = skb_clone(skb, GFP_ATOMIC);
|
|
|
|
if (!skb)
|
|
|
|
return;
|
|
|
|
|
2007-02-09 22:24:47 +08:00
|
|
|
serr = SKB_EXT_ERR(skb);
|
2005-04-17 06:20:36 +08:00
|
|
|
serr->ee.ee_errno = err;
|
|
|
|
serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
|
2007-03-14 01:43:18 +08:00
|
|
|
serr->ee.ee_type = icmp_hdr(skb)->type;
|
|
|
|
serr->ee.ee_code = icmp_hdr(skb)->code;
|
2005-04-17 06:20:36 +08:00
|
|
|
serr->ee.ee_pad = 0;
|
|
|
|
serr->ee.ee_info = info;
|
|
|
|
serr->ee.ee_data = 0;
|
2007-03-14 01:43:18 +08:00
|
|
|
serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
|
2007-04-11 11:50:43 +08:00
|
|
|
skb_network_header(skb);
|
2005-04-17 06:20:36 +08:00
|
|
|
serr->port = port;
|
|
|
|
|
2007-03-14 04:10:43 +08:00
|
|
|
if (skb_pull(skb, payload - skb->data) != NULL) {
|
|
|
|
skb_reset_transport_header(skb);
|
|
|
|
if (sock_queue_err_skb(sk, skb) == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
kfree_skb(skb);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-09-28 09:33:40 +08:00
|
|
|
void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct inet_sock *inet = inet_sk(sk);
|
|
|
|
struct sock_exterr_skb *serr;
|
|
|
|
struct iphdr *iph;
|
|
|
|
struct sk_buff *skb;
|
|
|
|
|
|
|
|
if (!inet->recverr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
|
|
|
|
if (!skb)
|
|
|
|
return;
|
|
|
|
|
2007-03-11 06:15:25 +08:00
|
|
|
skb_put(skb, sizeof(struct iphdr));
|
|
|
|
skb_reset_network_header(skb);
|
2007-04-21 13:47:35 +08:00
|
|
|
iph = ip_hdr(skb);
|
2005-04-17 06:20:36 +08:00
|
|
|
iph->daddr = daddr;
|
|
|
|
|
2007-02-09 22:24:47 +08:00
|
|
|
serr = SKB_EXT_ERR(skb);
|
2005-04-17 06:20:36 +08:00
|
|
|
serr->ee.ee_errno = err;
|
|
|
|
serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
|
2007-02-09 22:24:47 +08:00
|
|
|
serr->ee.ee_type = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
serr->ee.ee_code = 0;
|
|
|
|
serr->ee.ee_pad = 0;
|
|
|
|
serr->ee.ee_info = info;
|
|
|
|
serr->ee.ee_data = 0;
|
2007-04-11 11:50:43 +08:00
|
|
|
serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
|
2005-04-17 06:20:36 +08:00
|
|
|
serr->port = port;
|
|
|
|
|
2007-04-20 11:29:13 +08:00
|
|
|
__skb_pull(skb, skb_tail_pointer(skb) - skb->data);
|
2007-03-14 04:10:43 +08:00
|
|
|
skb_reset_transport_header(skb);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (sock_queue_err_skb(sk, skb))
|
|
|
|
kfree_skb(skb);
|
|
|
|
}
|
|
|
|
|
2007-02-09 22:24:47 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Handle MSG_ERRQUEUE
|
|
|
|
*/
|
|
|
|
int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
|
|
|
|
{
|
|
|
|
struct sock_exterr_skb *serr;
|
|
|
|
struct sk_buff *skb, *skb2;
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
struct {
|
|
|
|
struct sock_extended_err ee;
|
|
|
|
struct sockaddr_in offender;
|
|
|
|
} errhdr;
|
|
|
|
int err;
|
|
|
|
int copied;
|
|
|
|
|
|
|
|
err = -EAGAIN;
|
|
|
|
skb = skb_dequeue(&sk->sk_error_queue);
|
|
|
|
if (skb == NULL)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
copied = skb->len;
|
|
|
|
if (copied > len) {
|
|
|
|
msg->msg_flags |= MSG_TRUNC;
|
|
|
|
copied = len;
|
|
|
|
}
|
|
|
|
err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
|
|
|
|
if (err)
|
|
|
|
goto out_free_skb;
|
|
|
|
|
|
|
|
sock_recv_timestamp(msg, sk, skb);
|
|
|
|
|
|
|
|
serr = SKB_EXT_ERR(skb);
|
|
|
|
|
|
|
|
sin = (struct sockaddr_in *)msg->msg_name;
|
|
|
|
if (sin) {
|
|
|
|
sin->sin_family = AF_INET;
|
2007-04-11 11:50:43 +08:00
|
|
|
sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
|
|
|
|
serr->addr_offset);
|
2005-04-17 06:20:36 +08:00
|
|
|
sin->sin_port = serr->port;
|
|
|
|
memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
|
|
|
|
sin = &errhdr.offender;
|
|
|
|
sin->sin_family = AF_UNSPEC;
|
|
|
|
if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
|
|
|
|
struct inet_sock *inet = inet_sk(sk);
|
|
|
|
|
|
|
|
sin->sin_family = AF_INET;
|
2007-04-21 13:47:35 +08:00
|
|
|
sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
|
2005-04-17 06:20:36 +08:00
|
|
|
sin->sin_port = 0;
|
|
|
|
memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
|
|
|
|
if (inet->cmsg_flags)
|
|
|
|
ip_cmsg_recv(msg, skb);
|
|
|
|
}
|
|
|
|
|
|
|
|
put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
|
|
|
|
|
|
|
|
/* Now we could try to dump offended packet options */
|
|
|
|
|
|
|
|
msg->msg_flags |= MSG_ERRQUEUE;
|
|
|
|
err = copied;
|
|
|
|
|
|
|
|
/* Reset and regenerate socket error */
|
2005-06-19 13:56:18 +08:00
|
|
|
spin_lock_bh(&sk->sk_error_queue.lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
sk->sk_err = 0;
|
2009-06-02 15:42:16 +08:00
|
|
|
skb2 = skb_peek(&sk->sk_error_queue);
|
|
|
|
if (skb2 != NULL) {
|
2005-04-17 06:20:36 +08:00
|
|
|
sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
|
2005-06-19 13:56:18 +08:00
|
|
|
spin_unlock_bh(&sk->sk_error_queue.lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
sk->sk_error_report(sk);
|
|
|
|
} else
|
2005-06-19 13:56:18 +08:00
|
|
|
spin_unlock_bh(&sk->sk_error_queue.lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-02-09 22:24:47 +08:00
|
|
|
out_free_skb:
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree_skb(skb);
|
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-21 17:45:37 +08:00
|
|
|
static void opt_kfree_rcu(struct rcu_head *head)
|
|
|
|
{
|
|
|
|
kfree(container_of(head, struct ip_options_rcu, rcu));
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2009-06-02 15:42:16 +08:00
|
|
|
* Socket option code for IP. This is the end of the line after any
|
|
|
|
* TCP,UDP etc options on an IP socket.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
2006-03-21 14:45:21 +08:00
|
|
|
static int do_ip_setsockopt(struct sock *sk, int level,
|
2009-10-01 07:12:20 +08:00
|
|
|
int optname, char __user *optval, unsigned int optlen)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct inet_sock *inet = inet_sk(sk);
|
2008-11-03 16:27:11 +08:00
|
|
|
int val = 0, err;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-02-09 22:24:47 +08:00
|
|
|
if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
|
2007-03-09 12:44:43 +08:00
|
|
|
(1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
|
|
|
|
(1<<IP_RETOPTS) | (1<<IP_TOS) |
|
|
|
|
(1<<IP_TTL) | (1<<IP_HDRINCL) |
|
|
|
|
(1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
|
|
|
|
(1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
|
2010-01-12 08:28:01 +08:00
|
|
|
(1<<IP_PASSSEC) | (1<<IP_TRANSPARENT) |
|
2010-06-15 09:07:31 +08:00
|
|
|
(1<<IP_MINTTL) | (1<<IP_NODEFRAG))) ||
|
2007-03-09 12:44:43 +08:00
|
|
|
optname == IP_MULTICAST_TTL ||
|
2009-05-28 15:00:46 +08:00
|
|
|
optname == IP_MULTICAST_ALL ||
|
2008-11-17 11:32:39 +08:00
|
|
|
optname == IP_MULTICAST_LOOP ||
|
|
|
|
optname == IP_RECVORIGDSTADDR) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (optlen >= sizeof(int)) {
|
|
|
|
if (get_user(val, (int __user *) optval))
|
|
|
|
return -EFAULT;
|
|
|
|
} else if (optlen >= sizeof(char)) {
|
|
|
|
unsigned char ucval;
|
|
|
|
|
|
|
|
if (get_user(ucval, (unsigned char __user *) optval))
|
|
|
|
return -EFAULT;
|
|
|
|
val = (int) ucval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If optlen==0, it is equivalent to val == 0 */
|
|
|
|
|
2007-11-06 13:32:31 +08:00
|
|
|
if (ip_mroute_opt(optname))
|
2008-11-03 16:27:11 +08:00
|
|
|
return ip_mroute_setsockopt(sk, optname, optval, optlen);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
err = 0;
|
|
|
|
lock_sock(sk);
|
|
|
|
|
|
|
|
switch (optname) {
|
2007-03-09 12:44:43 +08:00
|
|
|
case IP_OPTIONS:
|
|
|
|
{
|
2011-04-21 17:45:37 +08:00
|
|
|
struct ip_options_rcu *old, *opt = NULL;
|
|
|
|
|
2009-10-23 13:59:21 +08:00
|
|
|
if (optlen > 40)
|
2007-03-09 12:44:43 +08:00
|
|
|
goto e_inval;
|
2008-03-26 01:26:21 +08:00
|
|
|
err = ip_options_get_from_user(sock_net(sk), &opt,
|
2008-03-25 06:31:00 +08:00
|
|
|
optval, optlen);
|
2007-03-09 12:44:43 +08:00
|
|
|
if (err)
|
|
|
|
break;
|
2011-04-21 17:45:37 +08:00
|
|
|
old = rcu_dereference_protected(inet->inet_opt,
|
|
|
|
sock_owned_by_user(sk));
|
2007-03-09 12:44:43 +08:00
|
|
|
if (inet->is_icsk) {
|
|
|
|
struct inet_connection_sock *icsk = inet_csk(sk);
|
2005-04-17 06:20:36 +08:00
|
|
|
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
|
2007-03-09 12:44:43 +08:00
|
|
|
if (sk->sk_family == PF_INET ||
|
|
|
|
(!((1 << sk->sk_state) &
|
|
|
|
(TCPF_LISTEN | TCPF_CLOSE)) &&
|
2009-10-15 14:30:45 +08:00
|
|
|
inet->inet_daddr != LOOPBACK4_IPV6)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
2011-04-21 17:45:37 +08:00
|
|
|
if (old)
|
|
|
|
icsk->icsk_ext_hdr_len -= old->opt.optlen;
|
2007-03-09 12:44:43 +08:00
|
|
|
if (opt)
|
2011-04-21 17:45:37 +08:00
|
|
|
icsk->icsk_ext_hdr_len += opt->opt.optlen;
|
2007-03-09 12:44:43 +08:00
|
|
|
icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
|
2005-04-17 06:20:36 +08:00
|
|
|
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
|
|
|
|
}
|
2007-03-09 12:44:43 +08:00
|
|
|
#endif
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2011-04-21 17:45:37 +08:00
|
|
|
rcu_assign_pointer(inet->inet_opt, opt);
|
|
|
|
if (old)
|
|
|
|
call_rcu(&old->rcu, opt_kfree_rcu);
|
2007-03-09 12:44:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IP_PKTINFO:
|
|
|
|
if (val)
|
|
|
|
inet->cmsg_flags |= IP_CMSG_PKTINFO;
|
|
|
|
else
|
|
|
|
inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
|
|
|
|
break;
|
|
|
|
case IP_RECVTTL:
|
|
|
|
if (val)
|
|
|
|
inet->cmsg_flags |= IP_CMSG_TTL;
|
|
|
|
else
|
|
|
|
inet->cmsg_flags &= ~IP_CMSG_TTL;
|
|
|
|
break;
|
|
|
|
case IP_RECVTOS:
|
|
|
|
if (val)
|
|
|
|
inet->cmsg_flags |= IP_CMSG_TOS;
|
|
|
|
else
|
|
|
|
inet->cmsg_flags &= ~IP_CMSG_TOS;
|
|
|
|
break;
|
|
|
|
case IP_RECVOPTS:
|
|
|
|
if (val)
|
|
|
|
inet->cmsg_flags |= IP_CMSG_RECVOPTS;
|
|
|
|
else
|
|
|
|
inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
|
|
|
|
break;
|
|
|
|
case IP_RETOPTS:
|
|
|
|
if (val)
|
|
|
|
inet->cmsg_flags |= IP_CMSG_RETOPTS;
|
|
|
|
else
|
|
|
|
inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
|
|
|
|
break;
|
|
|
|
case IP_PASSSEC:
|
|
|
|
if (val)
|
|
|
|
inet->cmsg_flags |= IP_CMSG_PASSSEC;
|
|
|
|
else
|
|
|
|
inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
|
|
|
|
break;
|
2008-11-17 11:32:39 +08:00
|
|
|
case IP_RECVORIGDSTADDR:
|
|
|
|
if (val)
|
|
|
|
inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
|
|
|
|
else
|
|
|
|
inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
|
|
|
|
break;
|
2007-03-09 12:44:43 +08:00
|
|
|
case IP_TOS: /* This sets both TOS and Precedence */
|
|
|
|
if (sk->sk_type == SOCK_STREAM) {
|
|
|
|
val &= ~3;
|
|
|
|
val |= inet->tos & 3;
|
|
|
|
}
|
|
|
|
if (inet->tos != val) {
|
|
|
|
inet->tos = val;
|
|
|
|
sk->sk_priority = rt_tos2priority(val);
|
|
|
|
sk_dst_reset(sk);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IP_TTL:
|
2009-06-02 15:42:16 +08:00
|
|
|
if (optlen < 1)
|
2007-03-09 12:44:43 +08:00
|
|
|
goto e_inval;
|
2009-06-02 15:42:16 +08:00
|
|
|
if (val != -1 && (val < 0 || val > 255))
|
2007-03-09 12:44:43 +08:00
|
|
|
goto e_inval;
|
|
|
|
inet->uc_ttl = val;
|
|
|
|
break;
|
|
|
|
case IP_HDRINCL:
|
|
|
|
if (sk->sk_type != SOCK_RAW) {
|
|
|
|
err = -ENOPROTOOPT;
|
[SECURITY]: TCP/UDP getpeersec
This patch implements an application of the LSM-IPSec networking
controls whereby an application can determine the label of the
security association its TCP or UDP sockets are currently connected to
via getsockopt and the auxiliary data mechanism of recvmsg.
Patch purpose:
This patch enables a security-aware application to retrieve the
security context of an IPSec security association a particular TCP or
UDP socket is using. The application can then use this security
context to determine the security context for processing on behalf of
the peer at the other end of this connection. In the case of UDP, the
security context is for each individual packet. An example
application is the inetd daemon, which could be modified to start
daemons running at security contexts dependent on the remote client.
Patch design approach:
- Design for TCP
The patch enables the SELinux LSM to set the peer security context for
a socket based on the security context of the IPSec security
association. The application may retrieve this context using
getsockopt. When called, the kernel determines if the socket is a
connected (TCP_ESTABLISHED) TCP socket and, if so, uses the dst_entry
cache on the socket to retrieve the security associations. If a
security association has a security context, the context string is
returned, as for UNIX domain sockets.
- Design for UDP
Unlike TCP, UDP is connectionless. This requires a somewhat different
API to retrieve the peer security context. With TCP, the peer
security context stays the same throughout the connection, thus it can
be retrieved at any time between when the connection is established
and when it is torn down. With UDP, each read/write can have
different peer and thus the security context might change every time.
As a result the security context retrieval must be done TOGETHER with
the packet retrieval.
The solution is to build upon the existing Unix domain socket API for
retrieving user credentials. Linux offers the API for obtaining user
credentials via ancillary messages (i.e., out of band/control messages
that are bundled together with a normal message).
Patch implementation details:
- Implementation for TCP
The security context can be retrieved by applications using getsockopt
with the existing SO_PEERSEC flag. As an example (ignoring error
checking):
getsockopt(sockfd, SOL_SOCKET, SO_PEERSEC, optbuf, &optlen);
printf("Socket peer context is: %s\n", optbuf);
The SELinux function, selinux_socket_getpeersec, is extended to check
for labeled security associations for connected (TCP_ESTABLISHED ==
sk->sk_state) TCP sockets only. If so, the socket has a dst_cache of
struct dst_entry values that may refer to security associations. If
these have security associations with security contexts, the security
context is returned.
getsockopt returns a buffer that contains a security context string or
the buffer is unmodified.
- Implementation for UDP
To retrieve the security context, the application first indicates to
the kernel such desire by setting the IP_PASSSEC option via
getsockopt. Then the application retrieves the security context using
the auxiliary data mechanism.
An example server application for UDP should look like this:
toggle = 1;
toggle_len = sizeof(toggle);
setsockopt(sockfd, SOL_IP, IP_PASSSEC, &toggle, &toggle_len);
recvmsg(sockfd, &msg_hdr, 0);
if (msg_hdr.msg_controllen > sizeof(struct cmsghdr)) {
cmsg_hdr = CMSG_FIRSTHDR(&msg_hdr);
if (cmsg_hdr->cmsg_len <= CMSG_LEN(sizeof(scontext)) &&
cmsg_hdr->cmsg_level == SOL_IP &&
cmsg_hdr->cmsg_type == SCM_SECURITY) {
memcpy(&scontext, CMSG_DATA(cmsg_hdr), sizeof(scontext));
}
}
ip_setsockopt is enhanced with a new socket option IP_PASSSEC to allow
a server socket to receive security context of the peer. A new
ancillary message type SCM_SECURITY.
When the packet is received we get the security context from the
sec_path pointer which is contained in the sk_buff, and copy it to the
ancillary message space. An additional LSM hook,
selinux_socket_getpeersec_udp, is defined to retrieve the security
context from the SELinux space. The existing function,
selinux_socket_getpeersec does not suit our purpose, because the
security context is copied directly to user space, rather than to
kernel space.
Testing:
We have tested the patch by setting up TCP and UDP connections between
applications on two machines using the IPSec policies that result in
labeled security associations being built. For TCP, we can then
extract the peer security context using getsockopt on either end. For
UDP, the receiving end can retrieve the security context using the
auxiliary data mechanism of recvmsg.
Signed-off-by: Catherine Zhang <cxzhang@watson.ibm.com>
Acked-by: James Morris <jmorris@namei.org>
Acked-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-03-21 14:41:23 +08:00
|
|
|
break;
|
2007-03-09 12:44:43 +08:00
|
|
|
}
|
|
|
|
inet->hdrincl = val ? 1 : 0;
|
|
|
|
break;
|
2010-06-15 09:07:31 +08:00
|
|
|
case IP_NODEFRAG:
|
|
|
|
if (sk->sk_type != SOCK_RAW) {
|
|
|
|
err = -ENOPROTOOPT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
inet->nodefrag = val ? 1 : 0;
|
|
|
|
break;
|
2007-03-09 12:44:43 +08:00
|
|
|
case IP_MTU_DISCOVER:
|
2009-10-20 12:53:53 +08:00
|
|
|
if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE)
|
2007-03-09 12:44:43 +08:00
|
|
|
goto e_inval;
|
|
|
|
inet->pmtudisc = val;
|
|
|
|
break;
|
|
|
|
case IP_RECVERR:
|
|
|
|
inet->recverr = !!val;
|
|
|
|
if (!val)
|
|
|
|
skb_queue_purge(&sk->sk_error_queue);
|
|
|
|
break;
|
|
|
|
case IP_MULTICAST_TTL:
|
|
|
|
if (sk->sk_type == SOCK_STREAM)
|
|
|
|
goto e_inval;
|
2009-06-02 15:42:16 +08:00
|
|
|
if (optlen < 1)
|
2007-03-09 12:44:43 +08:00
|
|
|
goto e_inval;
|
2008-11-03 16:27:11 +08:00
|
|
|
if (val == -1)
|
2007-03-09 12:44:43 +08:00
|
|
|
val = 1;
|
|
|
|
if (val < 0 || val > 255)
|
|
|
|
goto e_inval;
|
|
|
|
inet->mc_ttl = val;
|
|
|
|
break;
|
|
|
|
case IP_MULTICAST_LOOP:
|
2009-06-02 15:42:16 +08:00
|
|
|
if (optlen < 1)
|
2007-03-09 12:44:43 +08:00
|
|
|
goto e_inval;
|
|
|
|
inet->mc_loop = !!val;
|
|
|
|
break;
|
|
|
|
case IP_MULTICAST_IF:
|
|
|
|
{
|
|
|
|
struct ip_mreqn mreq;
|
|
|
|
struct net_device *dev = NULL;
|
|
|
|
|
|
|
|
if (sk->sk_type == SOCK_STREAM)
|
|
|
|
goto e_inval;
|
|
|
|
/*
|
|
|
|
* Check the arguments are allowable
|
|
|
|
*/
|
|
|
|
|
2009-09-22 23:41:10 +08:00
|
|
|
if (optlen < sizeof(struct in_addr))
|
|
|
|
goto e_inval;
|
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
err = -EFAULT;
|
|
|
|
if (optlen >= sizeof(struct ip_mreqn)) {
|
2008-11-03 16:27:11 +08:00
|
|
|
if (copy_from_user(&mreq, optval, sizeof(mreq)))
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
2007-03-09 12:44:43 +08:00
|
|
|
} else {
|
|
|
|
memset(&mreq, 0, sizeof(mreq));
|
|
|
|
if (optlen >= sizeof(struct in_addr) &&
|
2009-06-02 15:42:16 +08:00
|
|
|
copy_from_user(&mreq.imr_address, optval,
|
|
|
|
sizeof(struct in_addr)))
|
2007-03-09 12:44:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mreq.imr_ifindex) {
|
2008-03-18 13:44:53 +08:00
|
|
|
if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
|
2007-03-09 12:44:43 +08:00
|
|
|
inet->mc_index = 0;
|
|
|
|
inet->mc_addr = 0;
|
|
|
|
err = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-03-26 01:26:21 +08:00
|
|
|
dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
|
2009-10-19 14:41:58 +08:00
|
|
|
if (dev)
|
2007-03-09 12:44:43 +08:00
|
|
|
mreq.imr_ifindex = dev->ifindex;
|
|
|
|
} else
|
2009-10-19 14:41:58 +08:00
|
|
|
dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
err = -EADDRNOTAVAIL;
|
|
|
|
if (!dev)
|
|
|
|
break;
|
2009-10-19 14:41:58 +08:00
|
|
|
dev_put(dev);
|
2007-03-09 12:44:43 +08:00
|
|
|
|
|
|
|
err = -EINVAL;
|
|
|
|
if (sk->sk_bound_dev_if &&
|
|
|
|
mreq.imr_ifindex != sk->sk_bound_dev_if)
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
inet->mc_index = mreq.imr_ifindex;
|
|
|
|
inet->mc_addr = mreq.imr_address.s_addr;
|
|
|
|
err = 0;
|
|
|
|
break;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
case IP_ADD_MEMBERSHIP:
|
|
|
|
case IP_DROP_MEMBERSHIP:
|
|
|
|
{
|
|
|
|
struct ip_mreqn mreq;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-08-25 13:16:39 +08:00
|
|
|
err = -EPROTO;
|
|
|
|
if (inet_sk(sk)->is_icsk)
|
|
|
|
break;
|
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
if (optlen < sizeof(struct ip_mreq))
|
|
|
|
goto e_inval;
|
|
|
|
err = -EFAULT;
|
|
|
|
if (optlen >= sizeof(struct ip_mreqn)) {
|
2008-11-03 16:27:11 +08:00
|
|
|
if (copy_from_user(&mreq, optval, sizeof(mreq)))
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
2007-03-09 12:44:43 +08:00
|
|
|
} else {
|
|
|
|
memset(&mreq, 0, sizeof(mreq));
|
2008-11-03 16:27:11 +08:00
|
|
|
if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
2007-03-09 12:44:43 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
if (optname == IP_ADD_MEMBERSHIP)
|
|
|
|
err = ip_mc_join_group(sk, &mreq);
|
|
|
|
else
|
|
|
|
err = ip_mc_leave_group(sk, &mreq);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IP_MSFILTER:
|
|
|
|
{
|
|
|
|
struct ip_msfilter *msf;
|
|
|
|
|
|
|
|
if (optlen < IP_MSFILTER_SIZE(0))
|
|
|
|
goto e_inval;
|
|
|
|
if (optlen > sysctl_optmem_max) {
|
|
|
|
err = -ENOBUFS;
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
}
|
2007-03-09 12:44:43 +08:00
|
|
|
msf = kmalloc(optlen, GFP_KERNEL);
|
2007-10-09 16:59:42 +08:00
|
|
|
if (!msf) {
|
2007-03-09 12:44:43 +08:00
|
|
|
err = -ENOBUFS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
err = -EFAULT;
|
|
|
|
if (copy_from_user(msf, optval, optlen)) {
|
|
|
|
kfree(msf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* numsrc >= (1G-4) overflow in 32 bits */
|
|
|
|
if (msf->imsf_numsrc >= 0x3ffffffcU ||
|
|
|
|
msf->imsf_numsrc > sysctl_igmp_max_msf) {
|
|
|
|
kfree(msf);
|
|
|
|
err = -ENOBUFS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
|
|
|
|
kfree(msf);
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
err = ip_mc_msfilter(sk, msf, 0);
|
|
|
|
kfree(msf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IP_BLOCK_SOURCE:
|
|
|
|
case IP_UNBLOCK_SOURCE:
|
|
|
|
case IP_ADD_SOURCE_MEMBERSHIP:
|
|
|
|
case IP_DROP_SOURCE_MEMBERSHIP:
|
|
|
|
{
|
|
|
|
struct ip_mreq_source mreqs;
|
|
|
|
int omode, add;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
if (optlen != sizeof(struct ip_mreq_source))
|
|
|
|
goto e_inval;
|
|
|
|
if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
|
2005-04-17 06:20:36 +08:00
|
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-09 12:44:43 +08:00
|
|
|
if (optname == IP_BLOCK_SOURCE) {
|
|
|
|
omode = MCAST_EXCLUDE;
|
|
|
|
add = 1;
|
|
|
|
} else if (optname == IP_UNBLOCK_SOURCE) {
|
|
|
|
omode = MCAST_EXCLUDE;
|
|
|
|
add = 0;
|
|
|
|
} else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
|
|
|
|
struct ip_mreqn mreq;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
|
|
|
|
mreq.imr_address.s_addr = mreqs.imr_interface;
|
|
|
|
mreq.imr_ifindex = 0;
|
|
|
|
err = ip_mc_join_group(sk, &mreq);
|
|
|
|
if (err && err != -EADDRINUSE)
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
2007-03-09 12:44:43 +08:00
|
|
|
omode = MCAST_INCLUDE;
|
|
|
|
add = 1;
|
|
|
|
} else /* IP_DROP_SOURCE_MEMBERSHIP */ {
|
|
|
|
omode = MCAST_INCLUDE;
|
|
|
|
add = 0;
|
|
|
|
}
|
|
|
|
err = ip_mc_source(add, omode, sk, &mreqs, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MCAST_JOIN_GROUP:
|
|
|
|
case MCAST_LEAVE_GROUP:
|
|
|
|
{
|
|
|
|
struct group_req greq;
|
|
|
|
struct sockaddr_in *psin;
|
|
|
|
struct ip_mreqn mreq;
|
|
|
|
|
|
|
|
if (optlen < sizeof(struct group_req))
|
|
|
|
goto e_inval;
|
|
|
|
err = -EFAULT;
|
|
|
|
if (copy_from_user(&greq, optval, sizeof(greq)))
|
|
|
|
break;
|
|
|
|
psin = (struct sockaddr_in *)&greq.gr_group;
|
|
|
|
if (psin->sin_family != AF_INET)
|
|
|
|
goto e_inval;
|
|
|
|
memset(&mreq, 0, sizeof(mreq));
|
|
|
|
mreq.imr_multiaddr = psin->sin_addr;
|
|
|
|
mreq.imr_ifindex = greq.gr_interface;
|
|
|
|
|
|
|
|
if (optname == MCAST_JOIN_GROUP)
|
|
|
|
err = ip_mc_join_group(sk, &mreq);
|
|
|
|
else
|
|
|
|
err = ip_mc_leave_group(sk, &mreq);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MCAST_JOIN_SOURCE_GROUP:
|
|
|
|
case MCAST_LEAVE_SOURCE_GROUP:
|
|
|
|
case MCAST_BLOCK_SOURCE:
|
|
|
|
case MCAST_UNBLOCK_SOURCE:
|
|
|
|
{
|
|
|
|
struct group_source_req greqs;
|
|
|
|
struct ip_mreq_source mreqs;
|
|
|
|
struct sockaddr_in *psin;
|
|
|
|
int omode, add;
|
|
|
|
|
|
|
|
if (optlen != sizeof(struct group_source_req))
|
|
|
|
goto e_inval;
|
|
|
|
if (copy_from_user(&greqs, optval, sizeof(greqs))) {
|
2005-04-17 06:20:36 +08:00
|
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-09 12:44:43 +08:00
|
|
|
if (greqs.gsr_group.ss_family != AF_INET ||
|
|
|
|
greqs.gsr_source.ss_family != AF_INET) {
|
|
|
|
err = -EADDRNOTAVAIL;
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
}
|
2007-03-09 12:44:43 +08:00
|
|
|
psin = (struct sockaddr_in *)&greqs.gsr_group;
|
|
|
|
mreqs.imr_multiaddr = psin->sin_addr.s_addr;
|
|
|
|
psin = (struct sockaddr_in *)&greqs.gsr_source;
|
|
|
|
mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
|
|
|
|
mreqs.imr_interface = 0; /* use index for mc_source */
|
|
|
|
|
|
|
|
if (optname == MCAST_BLOCK_SOURCE) {
|
|
|
|
omode = MCAST_EXCLUDE;
|
|
|
|
add = 1;
|
|
|
|
} else if (optname == MCAST_UNBLOCK_SOURCE) {
|
|
|
|
omode = MCAST_EXCLUDE;
|
|
|
|
add = 0;
|
|
|
|
} else if (optname == MCAST_JOIN_SOURCE_GROUP) {
|
2005-04-17 06:20:36 +08:00
|
|
|
struct ip_mreqn mreq;
|
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
psin = (struct sockaddr_in *)&greqs.gsr_group;
|
2005-04-17 06:20:36 +08:00
|
|
|
mreq.imr_multiaddr = psin->sin_addr;
|
2007-03-09 12:44:43 +08:00
|
|
|
mreq.imr_address.s_addr = 0;
|
|
|
|
mreq.imr_ifindex = greqs.gsr_interface;
|
|
|
|
err = ip_mc_join_group(sk, &mreq);
|
|
|
|
if (err && err != -EADDRINUSE)
|
|
|
|
break;
|
|
|
|
greqs.gsr_interface = mreq.imr_ifindex;
|
|
|
|
omode = MCAST_INCLUDE;
|
|
|
|
add = 1;
|
|
|
|
} else /* MCAST_LEAVE_SOURCE_GROUP */ {
|
|
|
|
omode = MCAST_INCLUDE;
|
|
|
|
add = 0;
|
|
|
|
}
|
|
|
|
err = ip_mc_source(add, omode, sk, &mreqs,
|
|
|
|
greqs.gsr_interface);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MCAST_MSFILTER:
|
|
|
|
{
|
|
|
|
struct sockaddr_in *psin;
|
|
|
|
struct ip_msfilter *msf = NULL;
|
|
|
|
struct group_filter *gsf = NULL;
|
|
|
|
int msize, i, ifindex;
|
|
|
|
|
|
|
|
if (optlen < GROUP_FILTER_SIZE(0))
|
|
|
|
goto e_inval;
|
|
|
|
if (optlen > sysctl_optmem_max) {
|
|
|
|
err = -ENOBUFS;
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-11-03 16:27:11 +08:00
|
|
|
gsf = kmalloc(optlen, GFP_KERNEL);
|
2007-10-09 16:59:42 +08:00
|
|
|
if (!gsf) {
|
2007-03-09 12:44:43 +08:00
|
|
|
err = -ENOBUFS;
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
}
|
2007-03-09 12:44:43 +08:00
|
|
|
err = -EFAULT;
|
2009-06-02 15:42:16 +08:00
|
|
|
if (copy_from_user(gsf, optval, optlen))
|
2007-03-09 12:44:43 +08:00
|
|
|
goto mc_msf_out;
|
2009-06-02 15:42:16 +08:00
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
/* numsrc >= (4G-140)/128 overflow in 32 bits */
|
|
|
|
if (gsf->gf_numsrc >= 0x1ffffff ||
|
|
|
|
gsf->gf_numsrc > sysctl_igmp_max_msf) {
|
|
|
|
err = -ENOBUFS;
|
|
|
|
goto mc_msf_out;
|
|
|
|
}
|
|
|
|
if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto mc_msf_out;
|
|
|
|
}
|
|
|
|
msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
|
2008-11-03 16:27:11 +08:00
|
|
|
msf = kmalloc(msize, GFP_KERNEL);
|
2007-10-09 16:59:42 +08:00
|
|
|
if (!msf) {
|
2007-03-09 12:44:43 +08:00
|
|
|
err = -ENOBUFS;
|
|
|
|
goto mc_msf_out;
|
|
|
|
}
|
|
|
|
ifindex = gsf->gf_interface;
|
|
|
|
psin = (struct sockaddr_in *)&gsf->gf_group;
|
|
|
|
if (psin->sin_family != AF_INET) {
|
2005-04-17 06:20:36 +08:00
|
|
|
err = -EADDRNOTAVAIL;
|
2007-03-09 12:44:43 +08:00
|
|
|
goto mc_msf_out;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-03-09 12:44:43 +08:00
|
|
|
msf->imsf_multiaddr = psin->sin_addr.s_addr;
|
|
|
|
msf->imsf_interface = 0;
|
|
|
|
msf->imsf_fmode = gsf->gf_fmode;
|
|
|
|
msf->imsf_numsrc = gsf->gf_numsrc;
|
|
|
|
err = -EADDRNOTAVAIL;
|
2009-06-02 15:42:16 +08:00
|
|
|
for (i = 0; i < gsf->gf_numsrc; ++i) {
|
2007-03-09 12:44:43 +08:00
|
|
|
psin = (struct sockaddr_in *)&gsf->gf_slist[i];
|
2007-02-09 22:24:47 +08:00
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
if (psin->sin_family != AF_INET)
|
|
|
|
goto mc_msf_out;
|
|
|
|
msf->imsf_slist[i] = psin->sin_addr.s_addr;
|
|
|
|
}
|
|
|
|
kfree(gsf);
|
|
|
|
gsf = NULL;
|
|
|
|
|
|
|
|
err = ip_mc_msfilter(sk, msf, ifindex);
|
2009-06-02 15:42:16 +08:00
|
|
|
mc_msf_out:
|
2007-03-09 12:44:43 +08:00
|
|
|
kfree(msf);
|
|
|
|
kfree(gsf);
|
|
|
|
break;
|
|
|
|
}
|
2009-05-28 15:00:46 +08:00
|
|
|
case IP_MULTICAST_ALL:
|
|
|
|
if (optlen < 1)
|
|
|
|
goto e_inval;
|
|
|
|
if (val != 0 && val != 1)
|
|
|
|
goto e_inval;
|
|
|
|
inet->mc_all = val;
|
|
|
|
break;
|
2007-03-09 12:44:43 +08:00
|
|
|
case IP_ROUTER_ALERT:
|
|
|
|
err = ip_ra_control(sk, val ? 1 : 0, NULL);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IP_FREEBIND:
|
2009-06-02 15:42:16 +08:00
|
|
|
if (optlen < 1)
|
2007-03-09 12:44:43 +08:00
|
|
|
goto e_inval;
|
|
|
|
inet->freebind = !!val;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IP_IPSEC_POLICY:
|
|
|
|
case IP_XFRM_POLICY:
|
|
|
|
err = -EPERM;
|
|
|
|
if (!capable(CAP_NET_ADMIN))
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
2007-03-09 12:44:43 +08:00
|
|
|
err = xfrm_user_policy(sk, optname, optval, optlen);
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-10-01 22:30:02 +08:00
|
|
|
case IP_TRANSPARENT:
|
2011-10-21 06:21:36 +08:00
|
|
|
if (!!val && !capable(CAP_NET_RAW) && !capable(CAP_NET_ADMIN)) {
|
2008-10-01 22:30:02 +08:00
|
|
|
err = -EPERM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (optlen < 1)
|
|
|
|
goto e_inval;
|
|
|
|
inet->transparent = !!val;
|
|
|
|
break;
|
|
|
|
|
2010-01-12 08:28:01 +08:00
|
|
|
case IP_MINTTL:
|
|
|
|
if (optlen < 1)
|
|
|
|
goto e_inval;
|
|
|
|
if (val < 0 || val > 255)
|
|
|
|
goto e_inval;
|
|
|
|
inet->min_ttl = val;
|
|
|
|
break;
|
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
default:
|
|
|
|
err = -ENOPROTOOPT;
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
release_sock(sk);
|
|
|
|
return err;
|
|
|
|
|
|
|
|
e_inval:
|
|
|
|
release_sock(sk);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2010-04-29 06:31:51 +08:00
|
|
|
/**
|
|
|
|
* ip_queue_rcv_skb - Queue an skb into sock receive queue
|
|
|
|
* @sk: socket
|
|
|
|
* @skb: buffer
|
|
|
|
*
|
|
|
|
* Queues an skb into socket receive queue. If IP_CMSG_PKTINFO option
|
|
|
|
* is not set, we drop skb dst entry now, while dst cache line is hot.
|
|
|
|
*/
|
|
|
|
int ip_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
if (!(inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO))
|
|
|
|
skb_dst_drop(skb);
|
|
|
|
return sock_queue_rcv_skb(sk, skb);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ip_queue_rcv_skb);
|
|
|
|
|
2006-03-21 14:45:21 +08:00
|
|
|
int ip_setsockopt(struct sock *sk, int level,
|
2009-10-01 07:12:20 +08:00
|
|
|
int optname, char __user *optval, unsigned int optlen)
|
2006-03-21 14:45:21 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (level != SOL_IP)
|
|
|
|
return -ENOPROTOOPT;
|
|
|
|
|
|
|
|
err = do_ip_setsockopt(sk, level, optname, optval, optlen);
|
|
|
|
#ifdef CONFIG_NETFILTER
|
|
|
|
/* we need to exclude all possible ENOPROTOOPTs except default case */
|
|
|
|
if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
|
2007-11-06 13:32:31 +08:00
|
|
|
optname != IP_IPSEC_POLICY &&
|
|
|
|
optname != IP_XFRM_POLICY &&
|
|
|
|
!ip_mroute_opt(optname)) {
|
2006-03-21 14:45:21 +08:00
|
|
|
lock_sock(sk);
|
|
|
|
err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
|
|
|
|
release_sock(sk);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return err;
|
|
|
|
}
|
2009-06-02 15:42:16 +08:00
|
|
|
EXPORT_SYMBOL(ip_setsockopt);
|
2006-03-21 14:45:21 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_COMPAT
|
2006-03-21 14:48:35 +08:00
|
|
|
int compat_ip_setsockopt(struct sock *sk, int level, int optname,
|
2009-10-01 07:12:20 +08:00
|
|
|
char __user *optval, unsigned int optlen)
|
2006-03-21 14:45:21 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (level != SOL_IP)
|
|
|
|
return -ENOPROTOOPT;
|
|
|
|
|
2008-04-27 16:06:07 +08:00
|
|
|
if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
|
|
|
|
return compat_mc_setsockopt(sk, level, optname, optval, optlen,
|
|
|
|
ip_setsockopt);
|
|
|
|
|
2006-03-21 14:45:21 +08:00
|
|
|
err = do_ip_setsockopt(sk, level, optname, optval, optlen);
|
|
|
|
#ifdef CONFIG_NETFILTER
|
|
|
|
/* we need to exclude all possible ENOPROTOOPTs except default case */
|
|
|
|
if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
|
2007-11-06 13:32:31 +08:00
|
|
|
optname != IP_IPSEC_POLICY &&
|
|
|
|
optname != IP_XFRM_POLICY &&
|
|
|
|
!ip_mroute_opt(optname)) {
|
2006-03-21 14:45:21 +08:00
|
|
|
lock_sock(sk);
|
2006-03-21 14:48:35 +08:00
|
|
|
err = compat_nf_setsockopt(sk, PF_INET, optname,
|
|
|
|
optval, optlen);
|
2006-03-21 14:45:21 +08:00
|
|
|
release_sock(sk);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return err;
|
|
|
|
}
|
2006-03-21 14:48:35 +08:00
|
|
|
EXPORT_SYMBOL(compat_ip_setsockopt);
|
2006-03-21 14:45:21 +08:00
|
|
|
#endif
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2009-06-02 15:42:16 +08:00
|
|
|
* Get the options. Note for future reference. The GET of IP options gets
|
|
|
|
* the _received_ ones. The set sets the _sent_ ones.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
2006-03-21 14:45:21 +08:00
|
|
|
static int do_ip_getsockopt(struct sock *sk, int level, int optname,
|
2011-08-08 13:31:07 +08:00
|
|
|
char __user *optval, int __user *optlen, unsigned flags)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct inet_sock *inet = inet_sk(sk);
|
|
|
|
int val;
|
|
|
|
int len;
|
2007-02-09 22:24:47 +08:00
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
if (level != SOL_IP)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2007-11-06 13:32:31 +08:00
|
|
|
if (ip_mroute_opt(optname))
|
2008-11-03 16:27:11 +08:00
|
|
|
return ip_mroute_getsockopt(sk, optname, optval, optlen);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-11-03 16:27:11 +08:00
|
|
|
if (get_user(len, optlen))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EFAULT;
|
2007-03-09 12:44:43 +08:00
|
|
|
if (len < 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
2007-02-09 22:24:47 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
lock_sock(sk);
|
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
switch (optname) {
|
|
|
|
case IP_OPTIONS:
|
|
|
|
{
|
|
|
|
unsigned char optbuf[sizeof(struct ip_options)+40];
|
2011-04-21 17:45:37 +08:00
|
|
|
struct ip_options *opt = (struct ip_options *)optbuf;
|
|
|
|
struct ip_options_rcu *inet_opt;
|
|
|
|
|
|
|
|
inet_opt = rcu_dereference_protected(inet->inet_opt,
|
|
|
|
sock_owned_by_user(sk));
|
2007-03-09 12:44:43 +08:00
|
|
|
opt->optlen = 0;
|
2011-04-21 17:45:37 +08:00
|
|
|
if (inet_opt)
|
|
|
|
memcpy(optbuf, &inet_opt->opt,
|
|
|
|
sizeof(struct ip_options) +
|
|
|
|
inet_opt->opt.optlen);
|
2007-03-09 12:44:43 +08:00
|
|
|
release_sock(sk);
|
|
|
|
|
|
|
|
if (opt->optlen == 0)
|
|
|
|
return put_user(0, optlen);
|
|
|
|
|
|
|
|
ip_options_undo(opt);
|
|
|
|
|
|
|
|
len = min_t(unsigned int, len, opt->optlen);
|
|
|
|
if (put_user(len, optlen))
|
|
|
|
return -EFAULT;
|
|
|
|
if (copy_to_user(optval, opt->__data, len))
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case IP_PKTINFO:
|
|
|
|
val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
|
|
|
|
break;
|
|
|
|
case IP_RECVTTL:
|
|
|
|
val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
|
|
|
|
break;
|
|
|
|
case IP_RECVTOS:
|
|
|
|
val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
|
|
|
|
break;
|
|
|
|
case IP_RECVOPTS:
|
|
|
|
val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
|
|
|
|
break;
|
|
|
|
case IP_RETOPTS:
|
|
|
|
val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
|
|
|
|
break;
|
|
|
|
case IP_PASSSEC:
|
|
|
|
val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
|
|
|
|
break;
|
2008-11-17 11:32:39 +08:00
|
|
|
case IP_RECVORIGDSTADDR:
|
|
|
|
val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
|
|
|
|
break;
|
2007-03-09 12:44:43 +08:00
|
|
|
case IP_TOS:
|
|
|
|
val = inet->tos;
|
|
|
|
break;
|
|
|
|
case IP_TTL:
|
|
|
|
val = (inet->uc_ttl == -1 ?
|
|
|
|
sysctl_ip_default_ttl :
|
|
|
|
inet->uc_ttl);
|
|
|
|
break;
|
|
|
|
case IP_HDRINCL:
|
|
|
|
val = inet->hdrincl;
|
|
|
|
break;
|
2010-09-11 04:26:56 +08:00
|
|
|
case IP_NODEFRAG:
|
|
|
|
val = inet->nodefrag;
|
|
|
|
break;
|
2007-03-09 12:44:43 +08:00
|
|
|
case IP_MTU_DISCOVER:
|
|
|
|
val = inet->pmtudisc;
|
|
|
|
break;
|
|
|
|
case IP_MTU:
|
|
|
|
{
|
|
|
|
struct dst_entry *dst;
|
|
|
|
val = 0;
|
|
|
|
dst = sk_dst_get(sk);
|
|
|
|
if (dst) {
|
|
|
|
val = dst_mtu(dst);
|
|
|
|
dst_release(dst);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-03-09 12:44:43 +08:00
|
|
|
if (!val) {
|
2005-04-17 06:20:36 +08:00
|
|
|
release_sock(sk);
|
2007-03-09 12:44:43 +08:00
|
|
|
return -ENOTCONN;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-03-09 12:44:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IP_RECVERR:
|
|
|
|
val = inet->recverr;
|
|
|
|
break;
|
|
|
|
case IP_MULTICAST_TTL:
|
|
|
|
val = inet->mc_ttl;
|
|
|
|
break;
|
|
|
|
case IP_MULTICAST_LOOP:
|
|
|
|
val = inet->mc_loop;
|
|
|
|
break;
|
|
|
|
case IP_MULTICAST_IF:
|
|
|
|
{
|
|
|
|
struct in_addr addr;
|
|
|
|
len = min_t(unsigned int, len, sizeof(struct in_addr));
|
|
|
|
addr.s_addr = inet->mc_addr;
|
|
|
|
release_sock(sk);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
if (put_user(len, optlen))
|
|
|
|
return -EFAULT;
|
|
|
|
if (copy_to_user(optval, &addr, len))
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case IP_MSFILTER:
|
|
|
|
{
|
|
|
|
struct ip_msfilter msf;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (len < IP_MSFILTER_SIZE(0)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
release_sock(sk);
|
2007-03-09 12:44:43 +08:00
|
|
|
return -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-03-09 12:44:43 +08:00
|
|
|
if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
|
2005-04-17 06:20:36 +08:00
|
|
|
release_sock(sk);
|
2007-03-09 12:44:43 +08:00
|
|
|
return -EFAULT;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-03-09 12:44:43 +08:00
|
|
|
err = ip_mc_msfget(sk, &msf,
|
|
|
|
(struct ip_msfilter __user *)optval, optlen);
|
|
|
|
release_sock(sk);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
case MCAST_MSFILTER:
|
|
|
|
{
|
|
|
|
struct group_filter gsf;
|
|
|
|
int err;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
if (len < GROUP_FILTER_SIZE(0)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
release_sock(sk);
|
2007-03-09 12:44:43 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
|
|
|
|
release_sock(sk);
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
err = ip_mc_gsfget(sk, &gsf,
|
2009-06-02 15:42:16 +08:00
|
|
|
(struct group_filter __user *)optval,
|
|
|
|
optlen);
|
2007-03-09 12:44:43 +08:00
|
|
|
release_sock(sk);
|
|
|
|
return err;
|
|
|
|
}
|
2009-05-28 15:00:46 +08:00
|
|
|
case IP_MULTICAST_ALL:
|
|
|
|
val = inet->mc_all;
|
|
|
|
break;
|
2007-03-09 12:44:43 +08:00
|
|
|
case IP_PKTOPTIONS:
|
|
|
|
{
|
|
|
|
struct msghdr msg;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
release_sock(sk);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
if (sk->sk_type != SOCK_STREAM)
|
|
|
|
return -ENOPROTOOPT;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
msg.msg_control = optval;
|
|
|
|
msg.msg_controllen = len;
|
2011-08-08 13:31:07 +08:00
|
|
|
msg.msg_flags = flags;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-09 12:44:43 +08:00
|
|
|
if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
|
|
|
|
struct in_pktinfo info;
|
|
|
|
|
2009-10-15 14:30:45 +08:00
|
|
|
info.ipi_addr.s_addr = inet->inet_rcv_saddr;
|
|
|
|
info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
|
2007-03-09 12:44:43 +08:00
|
|
|
info.ipi_ifindex = inet->mc_index;
|
|
|
|
put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-03-09 12:44:43 +08:00
|
|
|
if (inet->cmsg_flags & IP_CMSG_TTL) {
|
|
|
|
int hlim = inet->mc_ttl;
|
|
|
|
put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
|
|
|
|
}
|
|
|
|
len -= msg.msg_controllen;
|
|
|
|
return put_user(len, optlen);
|
|
|
|
}
|
|
|
|
case IP_FREEBIND:
|
|
|
|
val = inet->freebind;
|
|
|
|
break;
|
2008-10-01 22:30:02 +08:00
|
|
|
case IP_TRANSPARENT:
|
|
|
|
val = inet->transparent;
|
|
|
|
break;
|
2010-01-12 08:28:01 +08:00
|
|
|
case IP_MINTTL:
|
|
|
|
val = inet->min_ttl;
|
|
|
|
break;
|
2007-03-09 12:44:43 +08:00
|
|
|
default:
|
|
|
|
release_sock(sk);
|
|
|
|
return -ENOPROTOOPT;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
release_sock(sk);
|
2007-02-09 22:24:47 +08:00
|
|
|
|
2009-06-02 15:42:16 +08:00
|
|
|
if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned char ucval = (unsigned char)val;
|
|
|
|
len = 1;
|
2007-03-09 12:44:43 +08:00
|
|
|
if (put_user(len, optlen))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EFAULT;
|
2008-11-03 16:27:11 +08:00
|
|
|
if (copy_to_user(optval, &ucval, 1))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EFAULT;
|
|
|
|
} else {
|
|
|
|
len = min_t(unsigned int, sizeof(int), len);
|
2007-03-09 12:44:43 +08:00
|
|
|
if (put_user(len, optlen))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EFAULT;
|
2008-11-03 16:27:11 +08:00
|
|
|
if (copy_to_user(optval, &val, len))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-21 14:45:21 +08:00
|
|
|
int ip_getsockopt(struct sock *sk, int level,
|
2007-03-09 12:44:43 +08:00
|
|
|
int optname, char __user *optval, int __user *optlen)
|
2006-03-21 14:45:21 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2011-08-08 13:31:07 +08:00
|
|
|
err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
|
2006-03-21 14:45:21 +08:00
|
|
|
#ifdef CONFIG_NETFILTER
|
|
|
|
/* we need to exclude all possible ENOPROTOOPTs except default case */
|
2007-11-06 13:32:31 +08:00
|
|
|
if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
|
|
|
|
!ip_mroute_opt(optname)) {
|
2007-02-09 22:24:47 +08:00
|
|
|
int len;
|
2006-03-21 14:45:21 +08:00
|
|
|
|
2008-11-03 16:27:11 +08:00
|
|
|
if (get_user(len, optlen))
|
2006-03-21 14:45:21 +08:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
lock_sock(sk);
|
|
|
|
err = nf_getsockopt(sk, PF_INET, optname, optval,
|
|
|
|
&len);
|
|
|
|
release_sock(sk);
|
|
|
|
if (err >= 0)
|
|
|
|
err = put_user(len, optlen);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return err;
|
|
|
|
}
|
2009-06-02 15:42:16 +08:00
|
|
|
EXPORT_SYMBOL(ip_getsockopt);
|
2006-03-21 14:45:21 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_COMPAT
|
2006-03-21 14:48:35 +08:00
|
|
|
int compat_ip_getsockopt(struct sock *sk, int level, int optname,
|
|
|
|
char __user *optval, int __user *optlen)
|
2006-03-21 14:45:21 +08:00
|
|
|
{
|
2008-04-29 18:23:22 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (optname == MCAST_MSFILTER)
|
|
|
|
return compat_mc_getsockopt(sk, level, optname, optval, optlen,
|
|
|
|
ip_getsockopt);
|
|
|
|
|
2011-08-08 13:31:07 +08:00
|
|
|
err = do_ip_getsockopt(sk, level, optname, optval, optlen,
|
|
|
|
MSG_CMSG_COMPAT);
|
2008-04-29 18:23:22 +08:00
|
|
|
|
2006-03-21 14:45:21 +08:00
|
|
|
#ifdef CONFIG_NETFILTER
|
|
|
|
/* we need to exclude all possible ENOPROTOOPTs except default case */
|
2007-11-06 13:32:31 +08:00
|
|
|
if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
|
|
|
|
!ip_mroute_opt(optname)) {
|
2007-02-09 22:24:47 +08:00
|
|
|
int len;
|
2006-03-21 14:45:21 +08:00
|
|
|
|
2006-03-21 14:48:35 +08:00
|
|
|
if (get_user(len, optlen))
|
2006-03-21 14:45:21 +08:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
lock_sock(sk);
|
2006-03-21 14:48:35 +08:00
|
|
|
err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
|
2006-03-21 14:45:21 +08:00
|
|
|
release_sock(sk);
|
|
|
|
if (err >= 0)
|
|
|
|
err = put_user(len, optlen);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return err;
|
|
|
|
}
|
2006-03-21 14:48:35 +08:00
|
|
|
EXPORT_SYMBOL(compat_ip_getsockopt);
|
2006-03-21 14:45:21 +08:00
|
|
|
#endif
|