2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* NET An implementation of the SOCKET network access protocol.
|
|
|
|
* This is the master header file for the Linux NET layer,
|
|
|
|
* or, in plain English: the networking handling part of the
|
|
|
|
* kernel.
|
|
|
|
*
|
|
|
|
* Version: @(#)net.h 1.0.3 05/25/93
|
|
|
|
*
|
|
|
|
* Authors: Orest Zborowski, <obz@Kodak.COM>
|
2005-05-06 03:16:16 +04:00
|
|
|
* Ross Biro
|
2005-04-17 02:20:36 +04:00
|
|
|
* Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
#ifndef _LINUX_NET_H
|
|
|
|
#define _LINUX_NET_H
|
|
|
|
|
2008-03-23 03:18:47 +03:00
|
|
|
#include <linux/socket.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
#include <asm/socket.h>
|
|
|
|
|
2008-03-23 03:18:47 +03:00
|
|
|
#define NPROTO AF_MAX
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
#define SYS_SOCKET 1 /* sys_socket(2) */
|
|
|
|
#define SYS_BIND 2 /* sys_bind(2) */
|
|
|
|
#define SYS_CONNECT 3 /* sys_connect(2) */
|
|
|
|
#define SYS_LISTEN 4 /* sys_listen(2) */
|
|
|
|
#define SYS_ACCEPT 5 /* sys_accept(2) */
|
|
|
|
#define SYS_GETSOCKNAME 6 /* sys_getsockname(2) */
|
|
|
|
#define SYS_GETPEERNAME 7 /* sys_getpeername(2) */
|
|
|
|
#define SYS_SOCKETPAIR 8 /* sys_socketpair(2) */
|
|
|
|
#define SYS_SEND 9 /* sys_send(2) */
|
|
|
|
#define SYS_RECV 10 /* sys_recv(2) */
|
|
|
|
#define SYS_SENDTO 11 /* sys_sendto(2) */
|
|
|
|
#define SYS_RECVFROM 12 /* sys_recvfrom(2) */
|
|
|
|
#define SYS_SHUTDOWN 13 /* sys_shutdown(2) */
|
|
|
|
#define SYS_SETSOCKOPT 14 /* sys_setsockopt(2) */
|
|
|
|
#define SYS_GETSOCKOPT 15 /* sys_getsockopt(2) */
|
|
|
|
#define SYS_SENDMSG 16 /* sys_sendmsg(2) */
|
|
|
|
#define SYS_RECVMSG 17 /* sys_recvmsg(2) */
|
reintroduce accept4
Introduce a new accept4() system call. The addition of this system call
matches analogous changes in 2.6.27 (dup3(), evenfd2(), signalfd4(),
inotify_init1(), epoll_create1(), pipe2()) which added new system calls
that differed from analogous traditional system calls in adding a flags
argument that can be used to access additional functionality.
The accept4() system call is exactly the same as accept(), except that
it adds a flags bit-mask argument. Two flags are initially implemented.
(Most of the new system calls in 2.6.27 also had both of these flags.)
SOCK_CLOEXEC causes the close-on-exec (FD_CLOEXEC) flag to be enabled
for the new file descriptor returned by accept4(). This is a useful
security feature to avoid leaking information in a multithreaded
program where one thread is doing an accept() at the same time as
another thread is doing a fork() plus exec(). More details here:
http://udrepper.livejournal.com/20407.html "Secure File Descriptor Handling",
Ulrich Drepper).
The other flag is SOCK_NONBLOCK, which causes the O_NONBLOCK flag
to be enabled on the new open file description created by accept4().
(This flag is merely a convenience, saving the use of additional calls
fcntl(F_GETFL) and fcntl (F_SETFL) to achieve the same result.
Here's a test program. Works on x86-32. Should work on x86-64, but
I (mtk) don't have a system to hand to test with.
It tests accept4() with each of the four possible combinations of
SOCK_CLOEXEC and SOCK_NONBLOCK set/clear in 'flags', and verifies
that the appropriate flags are set on the file descriptor/open file
description returned by accept4().
I tested Ulrich's patch in this thread by applying against 2.6.28-rc2,
and it passes according to my test program.
/* test_accept4.c
Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
<mtk.manpages@gmail.com>
Licensed under the GNU GPLv2 or later.
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#define PORT_NUM 33333
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
/**********************************************************************/
/* The following is what we need until glibc gets a wrapper for
accept4() */
/* Flags for socket(), socketpair(), accept4() */
#ifndef SOCK_CLOEXEC
#define SOCK_CLOEXEC O_CLOEXEC
#endif
#ifndef SOCK_NONBLOCK
#define SOCK_NONBLOCK O_NONBLOCK
#endif
#ifdef __x86_64__
#define SYS_accept4 288
#elif __i386__
#define USE_SOCKETCALL 1
#define SYS_ACCEPT4 18
#else
#error "Sorry -- don't know the syscall # on this architecture"
#endif
static int
accept4(int fd, struct sockaddr *sockaddr, socklen_t *addrlen, int flags)
{
printf("Calling accept4(): flags = %x", flags);
if (flags != 0) {
printf(" (");
if (flags & SOCK_CLOEXEC)
printf("SOCK_CLOEXEC");
if ((flags & SOCK_CLOEXEC) && (flags & SOCK_NONBLOCK))
printf(" ");
if (flags & SOCK_NONBLOCK)
printf("SOCK_NONBLOCK");
printf(")");
}
printf("\n");
#if USE_SOCKETCALL
long args[6];
args[0] = fd;
args[1] = (long) sockaddr;
args[2] = (long) addrlen;
args[3] = flags;
return syscall(SYS_socketcall, SYS_ACCEPT4, args);
#else
return syscall(SYS_accept4, fd, sockaddr, addrlen, flags);
#endif
}
/**********************************************************************/
static int
do_test(int lfd, struct sockaddr_in *conn_addr,
int closeonexec_flag, int nonblock_flag)
{
int connfd, acceptfd;
int fdf, flf, fdf_pass, flf_pass;
struct sockaddr_in claddr;
socklen_t addrlen;
printf("=======================================\n");
connfd = socket(AF_INET, SOCK_STREAM, 0);
if (connfd == -1)
die("socket");
if (connect(connfd, (struct sockaddr *) conn_addr,
sizeof(struct sockaddr_in)) == -1)
die("connect");
addrlen = sizeof(struct sockaddr_in);
acceptfd = accept4(lfd, (struct sockaddr *) &claddr, &addrlen,
closeonexec_flag | nonblock_flag);
if (acceptfd == -1) {
perror("accept4()");
close(connfd);
return 0;
}
fdf = fcntl(acceptfd, F_GETFD);
if (fdf == -1)
die("fcntl:F_GETFD");
fdf_pass = ((fdf & FD_CLOEXEC) != 0) ==
((closeonexec_flag & SOCK_CLOEXEC) != 0);
printf("Close-on-exec flag is %sset (%s); ",
(fdf & FD_CLOEXEC) ? "" : "not ",
fdf_pass ? "OK" : "failed");
flf = fcntl(acceptfd, F_GETFL);
if (flf == -1)
die("fcntl:F_GETFD");
flf_pass = ((flf & O_NONBLOCK) != 0) ==
((nonblock_flag & SOCK_NONBLOCK) !=0);
printf("nonblock flag is %sset (%s)\n",
(flf & O_NONBLOCK) ? "" : "not ",
flf_pass ? "OK" : "failed");
close(acceptfd);
close(connfd);
printf("Test result: %s\n", (fdf_pass && flf_pass) ? "PASS" : "FAIL");
return fdf_pass && flf_pass;
}
static int
create_listening_socket(int port_num)
{
struct sockaddr_in svaddr;
int lfd;
int optval;
memset(&svaddr, 0, sizeof(struct sockaddr_in));
svaddr.sin_family = AF_INET;
svaddr.sin_addr.s_addr = htonl(INADDR_ANY);
svaddr.sin_port = htons(port_num);
lfd = socket(AF_INET, SOCK_STREAM, 0);
if (lfd == -1)
die("socket");
optval = 1;
if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval)) == -1)
die("setsockopt");
if (bind(lfd, (struct sockaddr *) &svaddr,
sizeof(struct sockaddr_in)) == -1)
die("bind");
if (listen(lfd, 5) == -1)
die("listen");
return lfd;
}
int
main(int argc, char *argv[])
{
struct sockaddr_in conn_addr;
int lfd;
int port_num;
int passed;
passed = 1;
port_num = (argc > 1) ? atoi(argv[1]) : PORT_NUM;
memset(&conn_addr, 0, sizeof(struct sockaddr_in));
conn_addr.sin_family = AF_INET;
conn_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
conn_addr.sin_port = htons(port_num);
lfd = create_listening_socket(port_num);
if (!do_test(lfd, &conn_addr, 0, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, 0, SOCK_NONBLOCK))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, SOCK_NONBLOCK))
passed = 0;
close(lfd);
exit(passed ? EXIT_SUCCESS : EXIT_FAILURE);
}
[mtk.manpages@gmail.com: rewrote changelog, updated test program]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Tested-by: Michael Kerrisk <mtk.manpages@gmail.com>
Acked-by: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: <linux-api@vger.kernel.org>
Cc: <linux-arch@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-11-20 02:36:14 +03:00
|
|
|
#define SYS_ACCEPT4 18 /* sys_accept4(2) */
|
2009-10-13 10:40:10 +04:00
|
|
|
#define SYS_RECVMMSG 19 /* sys_recvmmsg(2) */
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
SS_FREE = 0, /* not allocated */
|
|
|
|
SS_UNCONNECTED, /* unconnected to any socket */
|
|
|
|
SS_CONNECTING, /* in process of connecting */
|
|
|
|
SS_CONNECTED, /* connected to socket */
|
|
|
|
SS_DISCONNECTING /* in process of disconnecting */
|
|
|
|
} socket_state;
|
|
|
|
|
|
|
|
#define __SO_ACCEPTCON (1 << 16) /* performed a listen */
|
|
|
|
|
|
|
|
#ifdef __KERNEL__
|
2006-04-25 17:46:09 +04:00
|
|
|
#include <linux/stringify.h>
|
2006-12-29 08:21:55 +03:00
|
|
|
#include <linux/random.h>
|
2008-08-26 18:29:22 +04:00
|
|
|
#include <linux/wait.h>
|
|
|
|
#include <linux/fcntl.h> /* For O_CLOEXEC and O_NONBLOCK */
|
2009-09-15 13:39:20 +04:00
|
|
|
#include <linux/kmemcheck.h>
|
2010-04-29 15:01:49 +04:00
|
|
|
#include <linux/rcupdate.h>
|
2008-08-26 18:29:22 +04:00
|
|
|
|
|
|
|
struct poll_table_struct;
|
|
|
|
struct pipe_inode_info;
|
|
|
|
struct inode;
|
|
|
|
struct net;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
#define SOCK_ASYNC_NOSPACE 0
|
|
|
|
#define SOCK_ASYNC_WAITDATA 1
|
|
|
|
#define SOCK_NOSPACE 2
|
|
|
|
#define SOCK_PASSCRED 3
|
[AF_UNIX]: Datagram getpeersec
This patch implements an API whereby an application can determine the
label of its peer's Unix datagram sockets via the auxiliary data mechanism of
recvmsg.
Patch purpose:
This patch enables a security-aware application to retrieve the
security context of the peer of a Unix datagram socket. The application
can then use this security context to determine the security context for
processing on behalf of the peer who sent the packet.
Patch design and implementation:
The design and implementation is very similar to the UDP case for INET
sockets. Basically we 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). To retrieve the security
context, the application first indicates to the kernel such desire by
setting the SO_PASSSEC option via getsockopt. Then the application
retrieves the security context using the auxiliary data mechanism.
An example server application for Unix datagram socket should look like this:
toggle = 1;
toggle_len = sizeof(toggle);
setsockopt(sockfd, SOL_SOCKET, SO_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_SOCKET &&
cmsg_hdr->cmsg_type == SCM_SECURITY) {
memcpy(&scontext, CMSG_DATA(cmsg_hdr), sizeof(scontext));
}
}
sock_setsockopt is enhanced with a new socket option SOCK_PASSSEC to allow
a server socket to receive security context of the peer.
Testing:
We have tested the patch by setting up Unix datagram client and server
applications. We verified that the server can retrieve the security context
using the auxiliary data mechanism of recvmsg.
Signed-off-by: Catherine Zhang <cxzhang@watson.ibm.com>
Acked-by: Acked-by: James Morris <jmorris@namei.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-06-29 23:27:47 +04:00
|
|
|
#define SOCK_PASSSEC 4
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
#ifndef ARCH_HAS_SOCKET_TYPES
|
2005-05-01 19:59:25 +04:00
|
|
|
/**
|
|
|
|
* enum sock_type - Socket types
|
|
|
|
* @SOCK_STREAM: stream (connection) socket
|
|
|
|
* @SOCK_DGRAM: datagram (conn.less) socket
|
|
|
|
* @SOCK_RAW: raw socket
|
|
|
|
* @SOCK_RDM: reliably-delivered message
|
|
|
|
* @SOCK_SEQPACKET: sequential packet socket
|
2005-11-07 12:01:05 +03:00
|
|
|
* @SOCK_DCCP: Datagram Congestion Control Protocol socket
|
2005-05-01 19:59:25 +04:00
|
|
|
* @SOCK_PACKET: linux specific way of getting packets at the dev level.
|
|
|
|
* For writing rarp and other similar things on the user level.
|
|
|
|
*
|
2005-04-17 02:20:36 +04:00
|
|
|
* When adding some new socket type please
|
|
|
|
* grep ARCH_HAS_SOCKET_TYPE include/asm-* /socket.h, at least MIPS
|
|
|
|
* overrides this enum for binary compat reasons.
|
|
|
|
*/
|
|
|
|
enum sock_type {
|
|
|
|
SOCK_STREAM = 1,
|
|
|
|
SOCK_DGRAM = 2,
|
|
|
|
SOCK_RAW = 3,
|
|
|
|
SOCK_RDM = 4,
|
|
|
|
SOCK_SEQPACKET = 5,
|
2005-08-10 07:14:34 +04:00
|
|
|
SOCK_DCCP = 6,
|
2005-04-17 02:20:36 +04:00
|
|
|
SOCK_PACKET = 10,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SOCK_MAX (SOCK_PACKET + 1)
|
flag parameters: socket and socketpair
This patch adds support for flag values which are ORed to the type passwd
to socket and socketpair. The additional code is minimal. The flag
values in this implementation can and must match the O_* flags. This
avoids overhead in the conversion.
The internal functions sock_alloc_fd and sock_map_fd get a new parameters
and all callers are changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 57392
/* For Linux these must be the same. */
#define SOCK_CLOEXEC O_CLOEXEC
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
puts ("socket(0) set close-on-exec flag");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (fd == -1)
{
puts ("socket(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
puts ("socket(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
printf ("socketpair(0) set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds) == -1)
{
puts ("socketpair(SOCK_CLOEXEC) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
printf ("socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 08:29:17 +04:00
|
|
|
/* Mask which covers at least up to SOCK_MASK-1. The
|
|
|
|
* remaining bits are used as flags. */
|
|
|
|
#define SOCK_TYPE_MASK 0xf
|
|
|
|
|
reintroduce accept4
Introduce a new accept4() system call. The addition of this system call
matches analogous changes in 2.6.27 (dup3(), evenfd2(), signalfd4(),
inotify_init1(), epoll_create1(), pipe2()) which added new system calls
that differed from analogous traditional system calls in adding a flags
argument that can be used to access additional functionality.
The accept4() system call is exactly the same as accept(), except that
it adds a flags bit-mask argument. Two flags are initially implemented.
(Most of the new system calls in 2.6.27 also had both of these flags.)
SOCK_CLOEXEC causes the close-on-exec (FD_CLOEXEC) flag to be enabled
for the new file descriptor returned by accept4(). This is a useful
security feature to avoid leaking information in a multithreaded
program where one thread is doing an accept() at the same time as
another thread is doing a fork() plus exec(). More details here:
http://udrepper.livejournal.com/20407.html "Secure File Descriptor Handling",
Ulrich Drepper).
The other flag is SOCK_NONBLOCK, which causes the O_NONBLOCK flag
to be enabled on the new open file description created by accept4().
(This flag is merely a convenience, saving the use of additional calls
fcntl(F_GETFL) and fcntl (F_SETFL) to achieve the same result.
Here's a test program. Works on x86-32. Should work on x86-64, but
I (mtk) don't have a system to hand to test with.
It tests accept4() with each of the four possible combinations of
SOCK_CLOEXEC and SOCK_NONBLOCK set/clear in 'flags', and verifies
that the appropriate flags are set on the file descriptor/open file
description returned by accept4().
I tested Ulrich's patch in this thread by applying against 2.6.28-rc2,
and it passes according to my test program.
/* test_accept4.c
Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
<mtk.manpages@gmail.com>
Licensed under the GNU GPLv2 or later.
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#define PORT_NUM 33333
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
/**********************************************************************/
/* The following is what we need until glibc gets a wrapper for
accept4() */
/* Flags for socket(), socketpair(), accept4() */
#ifndef SOCK_CLOEXEC
#define SOCK_CLOEXEC O_CLOEXEC
#endif
#ifndef SOCK_NONBLOCK
#define SOCK_NONBLOCK O_NONBLOCK
#endif
#ifdef __x86_64__
#define SYS_accept4 288
#elif __i386__
#define USE_SOCKETCALL 1
#define SYS_ACCEPT4 18
#else
#error "Sorry -- don't know the syscall # on this architecture"
#endif
static int
accept4(int fd, struct sockaddr *sockaddr, socklen_t *addrlen, int flags)
{
printf("Calling accept4(): flags = %x", flags);
if (flags != 0) {
printf(" (");
if (flags & SOCK_CLOEXEC)
printf("SOCK_CLOEXEC");
if ((flags & SOCK_CLOEXEC) && (flags & SOCK_NONBLOCK))
printf(" ");
if (flags & SOCK_NONBLOCK)
printf("SOCK_NONBLOCK");
printf(")");
}
printf("\n");
#if USE_SOCKETCALL
long args[6];
args[0] = fd;
args[1] = (long) sockaddr;
args[2] = (long) addrlen;
args[3] = flags;
return syscall(SYS_socketcall, SYS_ACCEPT4, args);
#else
return syscall(SYS_accept4, fd, sockaddr, addrlen, flags);
#endif
}
/**********************************************************************/
static int
do_test(int lfd, struct sockaddr_in *conn_addr,
int closeonexec_flag, int nonblock_flag)
{
int connfd, acceptfd;
int fdf, flf, fdf_pass, flf_pass;
struct sockaddr_in claddr;
socklen_t addrlen;
printf("=======================================\n");
connfd = socket(AF_INET, SOCK_STREAM, 0);
if (connfd == -1)
die("socket");
if (connect(connfd, (struct sockaddr *) conn_addr,
sizeof(struct sockaddr_in)) == -1)
die("connect");
addrlen = sizeof(struct sockaddr_in);
acceptfd = accept4(lfd, (struct sockaddr *) &claddr, &addrlen,
closeonexec_flag | nonblock_flag);
if (acceptfd == -1) {
perror("accept4()");
close(connfd);
return 0;
}
fdf = fcntl(acceptfd, F_GETFD);
if (fdf == -1)
die("fcntl:F_GETFD");
fdf_pass = ((fdf & FD_CLOEXEC) != 0) ==
((closeonexec_flag & SOCK_CLOEXEC) != 0);
printf("Close-on-exec flag is %sset (%s); ",
(fdf & FD_CLOEXEC) ? "" : "not ",
fdf_pass ? "OK" : "failed");
flf = fcntl(acceptfd, F_GETFL);
if (flf == -1)
die("fcntl:F_GETFD");
flf_pass = ((flf & O_NONBLOCK) != 0) ==
((nonblock_flag & SOCK_NONBLOCK) !=0);
printf("nonblock flag is %sset (%s)\n",
(flf & O_NONBLOCK) ? "" : "not ",
flf_pass ? "OK" : "failed");
close(acceptfd);
close(connfd);
printf("Test result: %s\n", (fdf_pass && flf_pass) ? "PASS" : "FAIL");
return fdf_pass && flf_pass;
}
static int
create_listening_socket(int port_num)
{
struct sockaddr_in svaddr;
int lfd;
int optval;
memset(&svaddr, 0, sizeof(struct sockaddr_in));
svaddr.sin_family = AF_INET;
svaddr.sin_addr.s_addr = htonl(INADDR_ANY);
svaddr.sin_port = htons(port_num);
lfd = socket(AF_INET, SOCK_STREAM, 0);
if (lfd == -1)
die("socket");
optval = 1;
if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval)) == -1)
die("setsockopt");
if (bind(lfd, (struct sockaddr *) &svaddr,
sizeof(struct sockaddr_in)) == -1)
die("bind");
if (listen(lfd, 5) == -1)
die("listen");
return lfd;
}
int
main(int argc, char *argv[])
{
struct sockaddr_in conn_addr;
int lfd;
int port_num;
int passed;
passed = 1;
port_num = (argc > 1) ? atoi(argv[1]) : PORT_NUM;
memset(&conn_addr, 0, sizeof(struct sockaddr_in));
conn_addr.sin_family = AF_INET;
conn_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
conn_addr.sin_port = htons(port_num);
lfd = create_listening_socket(port_num);
if (!do_test(lfd, &conn_addr, 0, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, 0))
passed = 0;
if (!do_test(lfd, &conn_addr, 0, SOCK_NONBLOCK))
passed = 0;
if (!do_test(lfd, &conn_addr, SOCK_CLOEXEC, SOCK_NONBLOCK))
passed = 0;
close(lfd);
exit(passed ? EXIT_SUCCESS : EXIT_FAILURE);
}
[mtk.manpages@gmail.com: rewrote changelog, updated test program]
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Tested-by: Michael Kerrisk <mtk.manpages@gmail.com>
Acked-by: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: <linux-api@vger.kernel.org>
Cc: <linux-arch@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-11-20 02:36:14 +03:00
|
|
|
/* Flags for socket, socketpair, accept4 */
|
flag parameters: socket and socketpair
This patch adds support for flag values which are ORed to the type passwd
to socket and socketpair. The additional code is minimal. The flag
values in this implementation can and must match the O_* flags. This
avoids overhead in the conversion.
The internal functions sock_alloc_fd and sock_map_fd get a new parameters
and all callers are changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 57392
/* For Linux these must be the same. */
#define SOCK_CLOEXEC O_CLOEXEC
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
puts ("socket(0) set close-on-exec flag");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (fd == -1)
{
puts ("socket(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
puts ("socket(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
printf ("socketpair(0) set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds) == -1)
{
puts ("socketpair(SOCK_CLOEXEC) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
printf ("socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 08:29:17 +04:00
|
|
|
#define SOCK_CLOEXEC O_CLOEXEC
|
2008-07-24 08:29:21 +04:00
|
|
|
#ifndef SOCK_NONBLOCK
|
|
|
|
#define SOCK_NONBLOCK O_NONBLOCK
|
|
|
|
#endif
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
#endif /* ARCH_HAS_SOCKET_TYPES */
|
|
|
|
|
2007-11-13 05:10:39 +03:00
|
|
|
enum sock_shutdown_cmd {
|
|
|
|
SHUT_RD = 0,
|
|
|
|
SHUT_WR = 1,
|
|
|
|
SHUT_RDWR = 2,
|
|
|
|
};
|
|
|
|
|
2010-04-29 15:01:49 +04:00
|
|
|
struct socket_wq {
|
|
|
|
wait_queue_head_t wait;
|
|
|
|
struct fasync_struct *fasync_list;
|
|
|
|
struct rcu_head rcu;
|
|
|
|
} ____cacheline_aligned_in_smp;
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/**
|
|
|
|
* struct socket - general BSD socket
|
2005-05-01 19:59:25 +04:00
|
|
|
* @state: socket state (%SS_CONNECTED, etc)
|
2008-07-08 14:03:01 +04:00
|
|
|
* @type: socket type (%SOCK_STREAM, etc)
|
2005-05-01 19:59:25 +04:00
|
|
|
* @flags: socket flags (%SOCK_ASYNC_NOSPACE, etc)
|
|
|
|
* @ops: protocol specific socket operations
|
|
|
|
* @file: File back pointer for gc
|
|
|
|
* @sk: internal networking protocol agnostic socket representation
|
2010-07-01 17:18:58 +04:00
|
|
|
* @wq: wait queue for several uses
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
|
|
|
struct socket {
|
|
|
|
socket_state state;
|
2009-09-15 13:39:20 +04:00
|
|
|
|
|
|
|
kmemcheck_bitfield_begin(type);
|
2008-07-08 14:03:01 +04:00
|
|
|
short type;
|
2009-09-15 13:39:20 +04:00
|
|
|
kmemcheck_bitfield_end(type);
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
unsigned long flags;
|
2010-04-29 15:01:49 +04:00
|
|
|
|
|
|
|
struct socket_wq *wq;
|
2009-03-16 05:59:13 +03:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
struct file *file;
|
|
|
|
struct sock *sk;
|
2009-03-16 05:59:13 +03:00
|
|
|
const struct proto_ops *ops;
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct vm_area_struct;
|
|
|
|
struct page;
|
|
|
|
struct kiocb;
|
|
|
|
struct sockaddr;
|
|
|
|
struct msghdr;
|
|
|
|
struct module;
|
|
|
|
|
|
|
|
struct proto_ops {
|
|
|
|
int family;
|
|
|
|
struct module *owner;
|
|
|
|
int (*release) (struct socket *sock);
|
|
|
|
int (*bind) (struct socket *sock,
|
|
|
|
struct sockaddr *myaddr,
|
|
|
|
int sockaddr_len);
|
|
|
|
int (*connect) (struct socket *sock,
|
|
|
|
struct sockaddr *vaddr,
|
|
|
|
int sockaddr_len, int flags);
|
|
|
|
int (*socketpair)(struct socket *sock1,
|
|
|
|
struct socket *sock2);
|
|
|
|
int (*accept) (struct socket *sock,
|
|
|
|
struct socket *newsock, int flags);
|
|
|
|
int (*getname) (struct socket *sock,
|
|
|
|
struct sockaddr *addr,
|
|
|
|
int *sockaddr_len, int peer);
|
|
|
|
unsigned int (*poll) (struct file *file, struct socket *sock,
|
|
|
|
struct poll_table_struct *wait);
|
|
|
|
int (*ioctl) (struct socket *sock, unsigned int cmd,
|
|
|
|
unsigned long arg);
|
2010-02-01 12:44:19 +03:00
|
|
|
#ifdef CONFIG_COMPAT
|
2006-03-22 10:58:08 +03:00
|
|
|
int (*compat_ioctl) (struct socket *sock, unsigned int cmd,
|
|
|
|
unsigned long arg);
|
2010-02-01 12:44:19 +03:00
|
|
|
#endif
|
2005-04-17 02:20:36 +04:00
|
|
|
int (*listen) (struct socket *sock, int len);
|
|
|
|
int (*shutdown) (struct socket *sock, int flags);
|
|
|
|
int (*setsockopt)(struct socket *sock, int level,
|
2009-10-01 03:12:20 +04:00
|
|
|
int optname, char __user *optval, unsigned int optlen);
|
2005-04-17 02:20:36 +04:00
|
|
|
int (*getsockopt)(struct socket *sock, int level,
|
|
|
|
int optname, char __user *optval, int __user *optlen);
|
2010-02-01 12:44:19 +03:00
|
|
|
#ifdef CONFIG_COMPAT
|
2006-03-21 09:45:21 +03:00
|
|
|
int (*compat_setsockopt)(struct socket *sock, int level,
|
2009-10-01 03:12:20 +04:00
|
|
|
int optname, char __user *optval, unsigned int optlen);
|
2006-03-21 09:45:21 +03:00
|
|
|
int (*compat_getsockopt)(struct socket *sock, int level,
|
|
|
|
int optname, char __user *optval, int __user *optlen);
|
2010-02-01 12:44:19 +03:00
|
|
|
#endif
|
2005-04-17 02:20:36 +04:00
|
|
|
int (*sendmsg) (struct kiocb *iocb, struct socket *sock,
|
|
|
|
struct msghdr *m, size_t total_len);
|
|
|
|
int (*recvmsg) (struct kiocb *iocb, struct socket *sock,
|
|
|
|
struct msghdr *m, size_t total_len,
|
|
|
|
int flags);
|
|
|
|
int (*mmap) (struct file *file, struct socket *sock,
|
|
|
|
struct vm_area_struct * vma);
|
|
|
|
ssize_t (*sendpage) (struct socket *sock, struct page *page,
|
|
|
|
int offset, size_t size, int flags);
|
2007-11-07 10:30:13 +03:00
|
|
|
ssize_t (*splice_read)(struct socket *sock, loff_t *ppos,
|
|
|
|
struct pipe_inode_info *pipe, size_t len, unsigned int flags);
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
|
2009-10-29 12:59:18 +03:00
|
|
|
#define DECLARE_SOCKADDR(type, dst, src) \
|
|
|
|
type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; })
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
struct net_proto_family {
|
|
|
|
int family;
|
2009-11-06 09:18:14 +03:00
|
|
|
int (*create)(struct net *net, struct socket *sock,
|
|
|
|
int protocol, int kern);
|
2005-04-17 02:20:36 +04:00
|
|
|
struct module *owner;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct iovec;
|
|
|
|
struct kvec;
|
|
|
|
|
2007-11-26 15:10:50 +03:00
|
|
|
enum {
|
|
|
|
SOCK_WAKE_IO,
|
|
|
|
SOCK_WAKE_WAITD,
|
|
|
|
SOCK_WAKE_SPACE,
|
|
|
|
SOCK_WAKE_URG,
|
|
|
|
};
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
extern int sock_wake_async(struct socket *sk, int how, int band);
|
2006-08-10 08:03:17 +04:00
|
|
|
extern int sock_register(const struct net_proto_family *fam);
|
|
|
|
extern void sock_unregister(int family);
|
2010-09-29 16:06:32 +04:00
|
|
|
extern int __sock_create(struct net *net, int family, int type, int proto,
|
|
|
|
struct socket **res, int kern);
|
2005-04-17 02:20:36 +04:00
|
|
|
extern int sock_create(int family, int type, int proto,
|
|
|
|
struct socket **res);
|
|
|
|
extern int sock_create_kern(int family, int type, int proto,
|
|
|
|
struct socket **res);
|
|
|
|
extern int sock_create_lite(int family, int type, int proto,
|
|
|
|
struct socket **res);
|
|
|
|
extern void sock_release(struct socket *sock);
|
|
|
|
extern int sock_sendmsg(struct socket *sock, struct msghdr *msg,
|
|
|
|
size_t len);
|
|
|
|
extern int sock_recvmsg(struct socket *sock, struct msghdr *msg,
|
|
|
|
size_t size, int flags);
|
flag parameters: socket and socketpair
This patch adds support for flag values which are ORed to the type passwd
to socket and socketpair. The additional code is minimal. The flag
values in this implementation can and must match the O_* flags. This
avoids overhead in the conversion.
The internal functions sock_alloc_fd and sock_map_fd get a new parameters
and all callers are changed.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 57392
/* For Linux these must be the same. */
#define SOCK_CLOEXEC O_CLOEXEC
int
main (void)
{
int fd;
fd = socket (PF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
puts ("socket(0) failed");
return 1;
}
int coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
puts ("socket(0) set close-on-exec flag");
return 1;
}
close (fd);
fd = socket (PF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (fd == -1)
{
puts ("socket(SOCK_CLOEXEC) failed");
return 1;
}
coe = fcntl (fd, F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
puts ("socket(SOCK_CLOEXEC) does not set close-on-exec flag");
return 1;
}
close (fd);
int fds[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, fds) == -1)
{
puts ("socketpair(0) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if (coe & FD_CLOEXEC)
{
printf ("socketpair(0) set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
if (socketpair (PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds) == -1)
{
puts ("socketpair(SOCK_CLOEXEC) failed");
return 1;
}
for (int i = 0; i < 2; ++i)
{
coe = fcntl (fds[i], F_GETFD);
if (coe == -1)
{
puts ("fcntl failed");
return 1;
}
if ((coe & FD_CLOEXEC) == 0)
{
printf ("socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n", i);
return 1;
}
close (fds[i]);
}
puts ("OK");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signed-off-by: Ulrich Drepper <drepper@redhat.com>
Acked-by: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@googlemail.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-24 08:29:17 +04:00
|
|
|
extern int sock_map_fd(struct socket *sock, int flags);
|
2005-04-17 02:20:36 +04:00
|
|
|
extern struct socket *sockfd_lookup(int fd, int *err);
|
|
|
|
#define sockfd_put(sock) fput(sock->file)
|
|
|
|
extern int net_ratelimit(void);
|
2006-10-17 11:09:42 +04:00
|
|
|
|
|
|
|
#define net_random() random32()
|
2006-11-15 08:12:29 +03:00
|
|
|
#define net_srandom(seed) srandom32((__force u32)seed)
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
extern int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
|
|
|
|
struct kvec *vec, size_t num, size_t len);
|
|
|
|
extern int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
|
|
|
|
struct kvec *vec, size_t num,
|
|
|
|
size_t len, int flags);
|
|
|
|
|
2006-08-08 07:57:31 +04:00
|
|
|
extern int kernel_bind(struct socket *sock, struct sockaddr *addr,
|
|
|
|
int addrlen);
|
|
|
|
extern int kernel_listen(struct socket *sock, int backlog);
|
|
|
|
extern int kernel_accept(struct socket *sock, struct socket **newsock,
|
|
|
|
int flags);
|
|
|
|
extern int kernel_connect(struct socket *sock, struct sockaddr *addr,
|
|
|
|
int addrlen, int flags);
|
|
|
|
extern int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
|
|
|
|
int *addrlen);
|
|
|
|
extern int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
|
|
|
|
int *addrlen);
|
|
|
|
extern int kernel_getsockopt(struct socket *sock, int level, int optname,
|
|
|
|
char *optval, int *optlen);
|
|
|
|
extern int kernel_setsockopt(struct socket *sock, int level, int optname,
|
2009-10-01 03:12:20 +04:00
|
|
|
char *optval, unsigned int optlen);
|
2006-08-08 07:57:31 +04:00
|
|
|
extern int kernel_sendpage(struct socket *sock, struct page *page, int offset,
|
|
|
|
size_t size, int flags);
|
|
|
|
extern int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg);
|
2007-11-13 05:10:39 +03:00
|
|
|
extern int kernel_sock_shutdown(struct socket *sock,
|
|
|
|
enum sock_shutdown_cmd how);
|
2006-08-08 07:57:31 +04:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
#define MODULE_ALIAS_NETPROTO(proto) \
|
|
|
|
MODULE_ALIAS("net-pf-" __stringify(proto))
|
|
|
|
|
2005-08-10 06:40:55 +04:00
|
|
|
#define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \
|
|
|
|
MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto))
|
|
|
|
|
2007-10-22 03:44:04 +04:00
|
|
|
#define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \
|
|
|
|
MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
|
|
|
|
"-type-" __stringify(type))
|
|
|
|
|
2005-08-16 09:18:02 +04:00
|
|
|
#ifdef CONFIG_SYSCTL
|
|
|
|
#include <linux/sysctl.h>
|
2009-09-22 18:18:09 +04:00
|
|
|
#include <linux/ratelimit.h>
|
2008-07-25 12:45:58 +04:00
|
|
|
extern struct ratelimit_state net_ratelimit_state;
|
2005-08-16 09:18:02 +04:00
|
|
|
#endif
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
#endif /* __KERNEL__ */
|
|
|
|
#endif /* _LINUX_NET_H */
|