sockmod_pfp.c revision 3b4315d3f6ce29d16d3f8e2c62b2f9c24192c3a8
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2015 Joyent, Inc. All rights reserved.
*/
#include <sys/socketvar.h>
#include <sys/socket_proto.h>
#include <sys/priv_const.h>
#include <sys/ethernet.h>
#include <sys/mac_client.h>
#include <sys/mac_provider.h>
#include <sys/mac_client_priv.h>
static int pfp_dl_to_arphrd(int);
static int pfp_getpacket_sockopt(sock_lower_handle_t, int, void *,
socklen_t *);
int);
cred_t *);
static void pfp_release_bpf(struct pfpsock *);
static int pfp_setsocket_sockopt(sock_lower_handle_t, int, const void *,
static int pfp_setpacket_sockopt(sock_lower_handle_t, int, const void *,
/*
* PFP sockfs operations
* Most are currently no-ops because they have no meaning for a connectionless
* socket.
*/
sock_upcalls_t *, int, struct cred *);
struct cred *);
static void sdpfp_clr_flowctrl(sock_lower_handle_t);
static int sdpfp_getsockopt(sock_lower_handle_t, int, int, void *,
struct cred *);
struct cred *);
static int sdpfp_setsockopt(sock_lower_handle_t, int, int, const void *,
static int sockpfp_init(void);
static void sockpfp_fini(void);
static pfp_kstats_t ks_stats;
static pfp_kstats_t pfp_kstats = {
/*
* Each one of these kstats is a different return path in handling
* a packet received from the mac layer.
*/
{ "recvMacHeaderFail", KSTAT_DATA_UINT64 },
{ "recvBadProtocol", KSTAT_DATA_UINT64 },
{ "recvAllocbFail", KSTAT_DATA_UINT64 },
{ "recvOk", KSTAT_DATA_UINT64 },
{ "recvFail", KSTAT_DATA_UINT64 },
{ "recvFiltered", KSTAT_DATA_UINT64 },
{ "recvFlowControl", KSTAT_DATA_UINT64 },
/*
* A global set of counters is maintained to track the behaviour
* of the system (kernel & applications) in sending packets.
*/
{ "sendUnbound", KSTAT_DATA_UINT64 },
{ "sendFailed", KSTAT_DATA_UINT64 },
{ "sendTooBig", KSTAT_DATA_UINT64 },
{ "sendAllocFail", KSTAT_DATA_UINT64 },
{ "sendUiomoveFail", KSTAT_DATA_UINT64 },
{ "sendNoMemory", KSTAT_DATA_UINT64 },
{ "sendOpenFail", KSTAT_DATA_UINT64 },
{ "sendWrongFamily", KSTAT_DATA_UINT64 },
{ "sendShortMsg", KSTAT_DATA_UINT64 },
{ "sendOk", KSTAT_DATA_UINT64 }
};
NULL,
};
static smod_reg_t sinfo = {
"sockpfp",
};
{ ETH_P_ALL, 0 },
{ ETH_P_802_2, LLC_SNAP_SAP },
{ ETH_P_803_3, 0 },
};
/*
* This sets an upper bound on the size of the receive buffer for a PF_PACKET
* socket. More properly, this should be controlled through ipadm, ala TCP, UDP,
* SCTP, etc. Until that's done, this provides a hard cap of 4 MB and allows an
* opportunity for it to be changed, should it be needed.
*/
/*
* Module linkage information for the kernel.
*/
static struct modlsockmod modlsockmod = {
};
static struct modlinkage modlinkage = {
};
int
_init(void)
{
int error;
error = sockpfp_init();
if (error != 0)
return (error);
if (error != 0)
sockpfp_fini();
return (error);
}
int
_fini(void)
{
int error;
if (error == 0)
sockpfp_fini();
return (error);
}
int
{
}
/*
* sockpfp_init: called as part of the initialisation of the module when
* loaded into the kernel.
*
* Being able to create and record the kstats data in the kernel is not
* considered to be vital to the operation of this kernel module, thus
* its failure is tolerated.
*/
static int
sockpfp_init(void)
{
}
return (0);
}
/*
* sockpfp_fini: called when the operating system wants to unload the
* socket module from the kernel.
*/
static void
sockpfp_fini(void)
{
}
/*
* Due to sockets being created read-write by default, all PF_PACKET sockets
* therefore require the NET_RAWACCESS priviliege, even if the socket is only
* being used for reading packets from.
*
* This create function enforces this module only being used with PF_PACKET
* sockets and the policy that we support via the config file in sock2path.d:
* PF_PACKET sockets must be either SOCK_DGRAM or SOCK_RAW.
*/
/* ARGSUSED */
static sock_lower_handle_t
{
int kmflags;
int newproto;
int i;
if (secpolicy_net_rawaccess(cred) != 0) {
return (NULL);
}
*errorp = EAFNOSUPPORT;
return (NULL);
}
return (NULL);
}
/*
* First check to see if the protocol number passed in via the socket
* creation should be mapped to a different number for internal use.
*/
for (i = 0, newproto = -1;
i < sizeof (accepted_protos)/ sizeof (accepted_protos[0]); i++) {
if (accepted_protos[i][0] == proto) {
break;
}
}
/*
* If the mapping of the protocol that was under 0x800 failed to find
* a local equivalent then fail the socket creation. If the protocol
* for the socket is over 0x800 and it was not found in the mapping
* table above, then use the value as is.
*/
if (newproto == -1) {
if (proto < 0x800) {
*errorp = ENOPROTOOPT;
return (NULL);
}
}
return (NULL);
}
/*
* Setting this causes bytes from a packet that do not fit into the
* destination user buffer to be discarded. Thus the API is one
* packet per receive and callers are required to use a buffer large
* enough for the biggest packet that the interface can provide.
*/
return ((sock_lower_handle_t)ps);
}
/* ************************************************************************* */
/*
* pfp_packet is the callback function that is given to the mac layer for
* PF_PACKET to receive packets with. One packet at a time is passed into
* this function from the mac layer. Each packet is a private copy given
* to PF_PACKET to modify or free as it wishes and does not harm the original
* packet from which it was cloned.
*/
/* ARGSUSED */
static void
{
struct T_unitdata_ind *tunit;
struct sockaddr_ll *sll;
struct sockaddr_ll *sol;
int error;
return;
if (ps->ps_flow_ctrld) {
ps->ps_flow_ctrl_drops++;
return;
}
/*
* Can't decode the packet header information so drop it.
*/
return;
}
struct ether_vlan_header *evhp;
struct ether_vlan_header evh;
} else {
char *s = (char *)&evh;
int len;
}
}
}
/*
* The packet is not of interest to this socket so
* drop it on the floor. Here the SAP is being used
* as a very course filter.
*/
return;
}
/*
* This field is not often set, even for ethernet,
* by mac_header_info, so compute it if it is 0.
*/
if (hdr.mhi_pktsize == 0)
/*
* If a BPF filter is present, pass the raw packet into that.
* A failed match will result in zero being returned, indicating
* that this socket is not interested in the packet.
*/
int buflen;
} else {
buflen = 0;
}
return;
}
}
/*
* SOCK_DGRAM socket expect a "layer 3" packet, so advance
* past the link layer header.
*/
}
if (ps->ps_auxdata) {
}
/*
* It is tempting to think that this could be optimised by having
* the base mblk_t allocated and hung off the pfpsock structure,
* except that then another one would need to be allocated for the
* sockaddr_ll that is included. Even creating a template to copy
* from is of questionable value, as read-write from one structure
* to the other is going to be slower than all of the initialisation.
*/
return;
}
switch (hdr.mhi_dsttype) {
case MAC_ADDRTYPE_MULTICAST :
break;
case MAC_ADDRTYPE_BROADCAST :
break;
case MAC_ADDRTYPE_UNICAST :
else
break;
}
if (ps->ps_auxdata) {
struct tpacket_auxdata *aux;
sizeof (struct sockaddr_ll));
_TPI_ALIGN_TOPT(sizeof (struct tpacket_auxdata));
aux = (struct tpacket_auxdata *)
/*
* libpcap doesn't seem to use any other field,
* so it isn't clear how they should be filled in.
*/
}
if (error == 0) {
} else {
}
}
}
/*
* Bind a PF_PACKET socket to a network interface.
*
* The default operation of this bind() is to place the socket (and thus the
* network interface) into promiscuous mode. It is then up to the application
* to turn that down by issuing the relevant ioctls, if desired.
*/
static int
{
int error;
return (EINVAL);
return (EINVAL);
if (error != 0)
return (error);
/*
* Ensure that each socket is only bound once.
*/
return (EADDRINUSE);
}
/*
* Cache all of the information from bind so that it's in an easy
* place to get at when packets are received.
*/
if (error == 0) {
}
return (error);
}
/* ARGSUSED */
static void
{
}
/*
* This module only implements getting socket options for the new socket
* option level (SOL_PACKET) that it introduces. All other requests are
* passed back to the sockfs layer.
*/
/* ARGSUSED */
static int
{
int error = 0;
switch (level) {
case SOL_PACKET :
optlenp);
break;
case SOL_SOCKET :
if (option_name == SO_RCVBUF) {
return (EINVAL);
} else {
error = ENOPROTOOPT;
}
break;
default :
/*
* If sockfs code receives this error in return from the
* getsockopt downcall it handles the option locally, if
* it can.
*/
error = ENOPROTOOPT;
break;
}
return (error);
}
/*
* PF_PACKET supports setting socket options at only two levels:
* SOL_SOCKET and SOL_PACKET.
*/
/* ARGSUSED */
static int
{
int error = 0;
switch (level) {
case SOL_SOCKET :
optlen);
break;
case SOL_PACKET :
optlen);
break;
default :
break;
}
return (error);
}
/*
* This function is incredibly inefficient for sending any packet that
* comes with a msghdr asking to be sent to an interface to which the
* socket has not been bound. Some possibilities here are keeping a
* cache of all open mac's and mac_client's, for the purpose of sending,
* and closing them after some amount of inactivity. Clearly, applications
* should not be written to use one socket for multiple interfaces if
* performance is desired with the code as is.
*/
/* ARGSUSED */
static int
{
struct sockaddr_ll *sol;
int error;
/*
* If no sockaddr_ll has been provided with the send call,
* use the one constructed when the socket was bound to an
* interface and fail if it hasn't been bound.
*/
return (EPROTO);
}
} else {
/*
* Verify the sockaddr_ll message passed down before using
* it to send a packet out with. If it refers to an interface
* that has not been bound, it is necessary to open it.
*/
struct sockaddr_ll *sll;
return (EINVAL);
}
return (EAFNOSUPPORT);
}
cred);
if (error != 0) {
return (error);
}
}
}
goto done;
}
goto done;
}
if (error != 0) {
goto done;
}
goto done;
}
}
/*
* As this is sending datagrams and no promise is made about
* be expended in recovering from a situation where the packet
* cannot be sent - it is just dropped.
*/
if (error == 0) {
} else {
}
done:
if (new_open) {
}
return (error);
}
/*
* There's no use of a lock here, or at the bottom of pfp_packet() where
* ps_flow_ctrld is set to true, because in a situation where these two
* are racing to set the flag one way or the other, the end result is
* going to be ultimately determined by the scheduler anyway - which of
* the two threads gets the lock first? In such an operational environment,
* we've got packets arriving too fast to be delt with so packets are going
* to be dropped. Grabbing a lock just makes the drop more expensive.
*/
static void
{
}
/*
* The implementation of this ioctl() handler is intended to function
* in the absence of a bind() being made before it is called. Thus the
* function calls mac_open() itself to provide a handle
* This function is structured like this:
* - determine the linkid for the interface being targetted
* - open the interface with said linkid
* - perform ioctl
* - copy results back to caller
*
* The ioctls that interact with interface flags have been implented below
* to assume that the interface is always up and running (IFF_RUNNING) and
* to use the state of this socket to determine whether or not the network
* interface is in promiscuous mode. Thus an ioctl to get the interface flags
* of an interface that has been put in promiscuous mode by another socket
* (in the same program or different), will not report that status.
*/
/* ARGSUSED */
static int
{
struct sockaddr_dl *sock;
int error;
switch (cmd) {
/*
* ioctls that work on "struct lifreq"
*/
case SIOCSLIFFLAGS :
case SIOCGLIFINDEX :
case SIOCGLIFFLAGS :
case SIOCGLIFMTU :
case SIOCGLIFHWADDR :
if (error != 0)
return (error);
break;
/*
* ioctls that work on "struct ifreq".
* Not all of these have a "struct lifreq" partner, for example
* SIOCGIFHWADDR, for the simple reason that the logical interface
* does not have a hardware address.
*/
case SIOCSIFFLAGS :
case SIOCGIFINDEX :
case SIOCGIFFLAGS :
case SIOCGIFMTU :
case SIOCGIFHWADDR :
if (error != 0)
return (error);
break;
case SIOCGSTAMP :
if (get_udatamodel() == DATAMODEL_NATIVE) {
}
#ifdef _SYSCALL32_IMPL
else {
}
#endif
return (error);
}
if (error != 0)
return (error);
switch (cmd) {
case SIOCGLIFINDEX :
break;
case SIOCGIFINDEX :
break;
case SIOCGIFFLAGS :
break;
case SIOCGLIFFLAGS :
break;
case SIOCSIFFLAGS :
} else {
else
}
break;
case SIOCSLIFFLAGS :
} else {
else
}
break;
case SIOCGIFMTU :
break;
case SIOCGLIFMTU :
break;
case SIOCGIFHWADDR :
break;
}
if (mac_addr_len(mh) == 0) {
} else {
}
/*
* The behaviour here in setting sa_family is consistent
* with what applications such as tcpdump would expect
* for a Linux PF_PACKET socket.
*/
break;
case SIOCGLIFHWADDR :
break;
}
/*
* Fill in the sockaddr_dl with link layer details. Of note,
* the index is returned as 0 for a couple of reasons:
* (1) there is no public API that uses or requires it
* (2) the MAC index is currently 32bits and sdl_index is 16.
*/
if (mac_addr_len(mh) == 0) {
} else {
}
break;
default :
break;
}
if (error == 0) {
/*
* Only the "GET" ioctls need to copy data back to userace.
*/
switch (cmd) {
case SIOCGLIFINDEX :
case SIOCGLIFFLAGS :
case SIOCGLIFMTU :
case SIOCGLIFHWADDR :
break;
case SIOCGIFINDEX :
case SIOCGIFFLAGS :
case SIOCGIFMTU :
case SIOCGIFHWADDR :
break;
default :
break;
}
}
return (error);
}
/*
* Closing the socket requires that all open references to network
* interfaces be closed.
*/
/* ARGSUSED */
static int
{
}
}
}
return (0);
}
/* ************************************************************************* */
/*
* Given a pointer (arg) to a "struct ifreq" (potentially in user space),
* determine the linkid for the interface name stored in that structure.
* name is used as a buffer so that we can ensure a trailing \0 is appended
* to the name safely.
*/
static int
{
int error;
return (EFAULT);
if (error != 0)
return (error);
}
/*
* Given a pointer (arg) to a "struct lifreq" (potentially in user space),
* determine the linkid for the interface name stored in that structure.
* name is used as a buffer so that we can ensure a trailing \0 is appended
* to the name safely.
*/
static int
{
int error;
return (EFAULT);
if (error != 0)
return (error);
}
/*
* Although there are several new SOL_PACKET options that can be set and
* are specific to this implementation of PF_PACKET, the current API does
* not support doing a get on them to retrieve accompanying status. Thus
* it is only currently possible to use SOL_PACKET with getsockopt to
* retrieve statistical information. This remains consistant with the
* Linux API at the time of writing.
*/
static int
{
struct tpacket_stats_short tpss;
int error = 0;
switch (option_name) {
case PACKET_STATISTICS :
break;
}
break;
case PACKET_STATISTICS_SHORT :
break;
}
break;
default :
break;
}
return (error);
}
/*
* The SOL_PACKET level for socket options supports three options,
* PACKET_ADD_MEMBERSHIP, PACKET_DROP_MEMBERSHIP and PACKET_AUXDATA.
* This function is responsible for mapping the two socket options
* that manage multicast membership into the appropriate internal
* function calls to bring the option into effect. Whilst direct
* by calls directly into the mac module, changes to the promiscuos
* mode are vectored through pfp_set_promisc() so that the logic for
* managing the promiscuous mode is in one place.
*/
/* ARGSUSED */
static int
{
struct packet_mreq mreq;
int error = 0;
int opt;
return (EPROTO);
if ((option_name == PACKET_ADD_MEMBERSHIP) ||
(option_name == PACKET_DROP_MEMBERSHIP)) {
return (EPROTO);
return (EINVAL);
}
switch (option_name) {
case PACKET_ADD_MEMBERSHIP :
case PACKET_MR_MULTICAST :
return (EINVAL);
break;
case PACKET_MR_PROMISC :
break;
case PACKET_MR_ALLMULTI :
break;
}
break;
case PACKET_DROP_MEMBERSHIP :
case PACKET_MR_MULTICAST :
return (EINVAL);
break;
case PACKET_MR_PROMISC :
return (EINVAL);
break;
case PACKET_MR_ALLMULTI :
return (EINVAL);
break;
}
break;
case PACKET_AUXDATA :
if (optlen == sizeof (int)) {
} else {
}
break;
default :
break;
}
return (error);
}
/*
* There are only two special setsockopt's for SOL_SOCKET with PF_PACKET:
* SO_ATTACH_FILTER and SO_DETACH_FILTER.
*
* Both of these setsockopt values are candidates for being handled by the
* socket layer itself in future, however this requires understanding how
* they would interact with all other sockets.
*/
static int
{
struct bpf_program prog;
struct sock_proto_props sopp;
int error = 0;
int size;
switch (option_name) {
case SO_ATTACH_FILTER :
#ifdef _LP64
if (optlen == sizeof (struct bpf_program32)) {
struct bpf_program32 prog32;
} else
#endif
if (optlen == sizeof (struct bpf_program)) {
} else if (optlen != sizeof (struct bpf_program)) {
return (EINVAL);
}
return (EINVAL);
return (EFAULT);
}
return (0);
}
break;
case SO_DETACH_FILTER :
break;
case SO_RCVBUF :
return (ENOBUFS);
break;
default :
error = ENOPROTOOPT;
break;
}
return (error);
}
/*
* pfp_open_index is an internal function used to open a MAC device by
* its index. Both a mac_handle_t and mac_client_handle_t are acquired
* because some of the interfaces provided by the mac layer require either
* only the mac_handle_t or both it and mac_handle_t.
*
* Whilst inside the kernel we can access data structures supporting any
* zone, access to interfaces from non-global zones is restricted to those
* interfaces (if any) that are exclusively assigned to a zone.
*/
static int
{
int error;
mh = 0;
mch = 0;
if (error != 0)
goto bad_open;
if (error != 0)
goto bad_open;
if (zoneid != GLOBAL_ZONEID) {
if (error != 0)
goto bad_open;
goto bad_open;
}
}
return (0);
if (mch != 0)
mac_client_close(mch, 0);
if (mh != 0)
return (error);
}
static void
{
mac_client_close(mch, 0);
}
/*
* The purpose of this function is to provide a single place where we free
* it.
*/
static void
{
}
}
/*
* Set the promiscuous mode of a network interface.
* This function only calls the mac layer when there is a change to the
* status of a network interface's promiscous mode. Tracking of how many
* sockets have the network interface in promiscuous mode, and thus the
* control over the physical device's status, is left to the mac layer.
*/
static int
{
int error = 0;
int flags;
/*
* There are 4 combinations of turnon/ps_promisc.
* This if handles 2 (both false, both true) and the if() below
* handles the remaining one - when change is required.
*/
return (error);
/*
* ps_promisc is set here in case the call to mac_promisc_add
* fails: leaving it to indicate that the interface is still
* in some sort of promiscuous mode is false.
*/
} else {
flags = 0;
}
}
if (error == 0)
return (error);
}
/*
* This table maps the MAC types in Solaris to the ARPHRD_* values used
* on Linux. This is used with the SIOCGIFHWADDR/SIOCGLIFHWADDR ioctl.
*
* of supplementary information to the ARP table.
*/
{ ARPHRD_IEEE80211, DL_WIFI },
{ ARPHRD_TUNNEL, DL_IPV4 },
{ ARPHRD_TUNNEL, DL_IPV6 },
{ ARPHRD_TUNNEL, DL_6TO4 },
{ ARPHRD_AX25, DL_X25 },
{ ARPHRD_ATM, DL_ATM },
{ 0, 0 }
};
static int
pfp_dl_to_arphrd(int dltype)
{
int i;
for (i = 0; arphrd_to_dl[i][0] != 0; i++)
return (arphrd_to_dl[i][0]);
return (arp_hw_type(dltype));
}