Skip to content

Commit

Permalink
This commit implements the SO_USER_COOKIE socket option, which lets
Browse files Browse the repository at this point in the history
you tag a socket with an uint32_t value. The cookie can then be
used by the kernel for various purposes, e.g. setting the skipto
rule or pipe number in ipfw (this is the reason SO_USER_COOKIE has
been implemented; however there is nothing ipfw-specific in its
implementation).

The ipfw-related code that uses the optopn will be committed separately.

This change adds a field to 'struct socket', but the struct is not
part of any driver or userland-visible ABI so the change should be
harmless.

See the discussion at
http://lists.freebsd.org/pipermail/freebsd-ipfw/2009-October/004001.html

Idea and code from Paul Joe, small modifications and manpage
changes by myself.

Submitted by:	Paul Joe
MFC after:	1 week
  • Loading branch information
luigi authored and luigi committed Nov 12, 2010
1 parent 20a2600 commit d5e8d23
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/libc/sys/getsockopt.2
Original file line number Diff line number Diff line change
Expand Up @@ -184,22 +184,26 @@ The following options are recognized in
.It Dv SO_LISTENQLIMIT Ta "get backlog limit of the socket (get only)"
.It Dv SO_LISTENQLEN Ta "get complete queue length of the socket (get only)"
.It Dv SO_LISTENINCQLEN Ta "get incomplete queue length of the socket (get only)"
.It Dv SO_USER_COOKIE Ta "set the 'so_user_cookie' value for the socket (uint32_t, set only)"
.El
.Pp
.Dv SO_DEBUG
enables debugging in the underlying protocol modules.
.Pp
.Dv SO_REUSEADDR
indicates that the rules used in validating addresses supplied
in a
.Xr bind 2
system call should allow reuse of local addresses.
.Pp
.Dv SO_REUSEPORT
allows completely duplicate bindings by multiple processes
if they all set
.Dv SO_REUSEPORT
before binding the port.
This option permits multiple instances of a program to each
receive UDP/IP multicast or broadcast datagrams destined for the bound port.
.Pp
.Dv SO_KEEPALIVE
enables the
periodic transmission of messages on a connected socket.
Expand All @@ -208,6 +212,7 @@ connected party fail to respond to these messages, the connection is
considered broken and processes using the socket are notified via a
.Dv SIGPIPE
signal when attempting to send data.
.Pp
.Dv SO_DONTROUTE
indicates that outgoing messages should
bypass the standard routing facilities.
Expand Down Expand Up @@ -244,6 +249,7 @@ The option
requests permission to send broadcast datagrams
on the socket.
Broadcast was a privileged operation in earlier versions of the system.
.Pp
With protocols that support out-of-band data, the
.Dv SO_OOBINLINE
option
Expand All @@ -256,6 +262,7 @@ calls without the
.Dv MSG_OOB
flag.
Some protocols always behave as if this option is set.
.Pp
.Dv SO_SNDBUF
and
.Dv SO_RCVBUF
Expand Down Expand Up @@ -285,6 +292,7 @@ only if the low water mark amount could be processed.
The default value for
.Dv SO_SNDLOWAT
is set to a convenient size for network efficiency, often 1024.
.Pp
.Dv SO_RCVLOWAT
is an option to set the minimum count for input operations.
In general, receive calls will block until any (non-zero) amount of data
Expand Down Expand Up @@ -317,6 +325,7 @@ In the current implementation, this timer is restarted each time additional
data are delivered to the protocol,
implying that the limit applies to output portions ranging in size
from the low water mark to the high water mark for output.
.Pp
.Dv SO_RCVTIMEO
is an option to set a timeout value for input operations.
It accepts a
Expand All @@ -338,6 +347,15 @@ The value must be from 0 to one less than the number returned from
the sysctl
.Em net.fibs .
.Pp
.Dv SO_USER_COOKIE
can be used to set the uint32_t so_user_cookie field in the socket.
The value is an uint32_t, and can be used in the kernel code that
manipulates traffic related to the socket.
The default value for the field is 0.
As an example, the value can be used as the skipto target or
pipe number in
.Nm ipfw/dummynet .
.Pp
.Dv SO_ACCEPTFILTER
places an
.Xr accept_filter 9
Expand Down
10 changes: 10 additions & 0 deletions sys/kern/uipc_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -2386,6 +2386,7 @@ sosetopt(struct socket *so, struct sockopt *sopt)
struct linger l;
struct timeval tv;
u_long val;
uint32_t val32;
#ifdef MAC
struct mac extmac;
#endif
Expand Down Expand Up @@ -2461,6 +2462,15 @@ sosetopt(struct socket *so, struct sockopt *sopt)
so->so_fibnum = 0;
}
break;

case SO_USER_COOKIE:
error = sooptcopyin(sopt, &val32, sizeof val32,
sizeof val32);
if (error)
goto bad;
so->so_user_cookie = val32;
break;

case SO_SNDBUF:
case SO_RCVBUF:
case SO_SNDLOWAT:
Expand Down
1 change: 1 addition & 0 deletions sys/sys/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ typedef __uid_t uid_t;
#define SO_LISTENQLEN 0x1012 /* socket's complete queue length */
#define SO_LISTENINCQLEN 0x1013 /* socket's incomplete queue length */
#define SO_SETFIB 0x1014 /* use this FIB to route */
#define SO_USER_COOKIE 0x1015 /* user cookie (dummynet etc.) */
#endif

/*
Expand Down
7 changes: 7 additions & 0 deletions sys/sys/socketvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,14 @@ struct socket {
void *so_accept_filter_arg; /* saved filter args */
char *so_accept_filter_str; /* saved user args */
} *so_accf;
/*
* so_fibnum, so_user_cookie and friends can be used to attach
* some user-specified metadata to a socket, which then can be
* used by the kernel for various actions.
* so_user_cookie is used by ipfw/dummynet.
*/
int so_fibnum; /* routing domain for this socket */
uint32_t so_user_cookie;
};

/*
Expand Down

0 comments on commit d5e8d23

Please sign in to comment.