ip_fil_solaris.c revision ca8c705492a4b40b684b4dbb187d208971f49f2c
/*
* Copyright (C) 1993-2001, 2003 by Darren Reed.
*
* See the IPFILTER.LICENCE file for details on licencing.
*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#if !defined(lint)
static const char sccsid[] = "@(#)ip_fil_solaris.c 1.7 07/22/06 (C) 1993-2000 Darren Reed";
static const char rcsid[] = "@(#)$Id: ip_fil_solaris.c,v 2.62.2.19 2005/07/13 21:40:46 darrenr Exp $";
#endif
#include <sys/dditypes.h>
#include <netinet/in_systm.h>
#include "netinet/ip_compat.h"
#ifdef USE_INET6
#endif
#include "netinet/ip_state.h"
#include "netinet/ip_proxy.h"
#ifdef IPFILTER_LOOKUP
# include "netinet/ip_lookup.h"
#endif
#if SOLARIS2 >= 7
#else
int fr_timer_id;
#endif
#if SOLARIS2 >= 10
extern int ipf_loopback;
#endif
static int fr_setipfloopback __P((int));
static hook_t ipfhook_in;
static hook_t ipfhook_out;
static hook_t ipfhook_loop_in;
static hook_t ipfhook_loop_out;
static hook_t ipfhook_nicevents;
/* flags to indicate whether hooks are registered. */
#if SOLARIS2 < 10
#if SOLARIS2 >= 7
# if SOLARIS2 >= 8
int *ip_forwarding = NULL;
# else
# endif
#else
int fr_timer_id;
#endif
#endif
#if SOLARIS2 >= 10
extern net_data_t ipf_ipv4;
extern net_data_t ipf_ipv6;
#endif
int ipf_locks_done = 0;
/* ------------------------------------------------------------------------ */
/* Function: ipldetach */
/* Returns: int - 0 == success, else error. */
/* Parameters: Nil */
/* */
/* This function is responsible for undoing anything that might have been */
/* done in a call to iplattach(). It must be able to clean up from a call */
/* to iplattach() that did not succeed. Why might that happen? Someone */
/* configures a table to be so large that we cannot allocate enough memory */
/* for it. */
/* ------------------------------------------------------------------------ */
int ipldetach()
{
#if SOLARIS2 < 10
if (fr_control_forwarding & 2) {
if (ip_forwarding != NULL)
*ip_forwarding = 0;
#if SOLARIS2 >= 8
if (ip6_forwarding != NULL)
*ip6_forwarding = 0;
#endif
}
#endif
/*
* This lock needs to be dropped around the net_unregister_hook calls
* because we can deadlock here with:
* W(ipf_global)->R(hook_family)->W(hei_lock) (this code path) vs
* R(hook_family)->R(hei_lock)->R(ipf_global) (active hook running)
*/
/*
* Remove IPv6 Hooks
*/
if (hook6_physical_in) {
NH_PHYSICAL_IN, &ipfhook_in) != 0);
}
if (hook6_physical_out) {
NH_PHYSICAL_OUT, &ipfhook_out) != 0);
}
if (hook6_nic_events) {
NH_NIC_EVENTS, &ipfhook_nicevents) != 0);
}
if (hook6_loopback_in) {
NH_LOOPBACK_IN, &ipfhook_loop_in) != 0);
}
if (hook6_loopback_out) {
NH_LOOPBACK_OUT, &ipfhook_loop_out) != 0);
}
if (net_release(ipf_ipv6) != 0)
goto detach_failed;
}
/*
* Remove IPv4 Hooks
*/
if (hook4_physical_in) {
NH_PHYSICAL_IN, &ipfhook_in) != 0);
}
if (hook4_physical_out) {
NH_PHYSICAL_OUT, &ipfhook_out) != 0);
}
if (hook4_nic_events) {
NH_NIC_EVENTS, &ipfhook_nicevents) != 0);
}
if (hook4_loopback_in) {
NH_LOOPBACK_IN, &ipfhook_loop_in) != 0);
}
if (hook4_loopback_out) {
NH_LOOPBACK_OUT, &ipfhook_loop_out) != 0);
}
if (net_release(ipf_ipv4) != 0)
goto detach_failed;
}
#ifdef IPFDEBUG
#endif
if (ipf_locks_done == 1) {
ipf_locks_done = 0;
}
return -1;
return 0;
return -1;
}
{
#if SOLARIS2 < 10
int i;
#endif
#ifdef IPFDEBUG
#endif
ipf_locks_done = 1;
if (fr_initialise() < 0)
return -1;
"ipfilter_hook_nicevents");
"ipfilter_hook_loop_out");
/*
* If we hold this lock over all of the net_register_hook calls, we
* can cause a deadlock to occur with the following lock ordering:
* W(ipf_global)->R(hook_family)->W(hei_lock) (this code path) vs
* R(hook_family)->R(hei_lock)->R(ipf_global) (packet path)
*/
/*
* Add IPv4 hooks
*/
goto hookup_failed;
&ipfhook_nicevents) == 0);
if (!hook4_nic_events)
goto hookup_failed;
&ipfhook_in) == 0);
if (!hook4_physical_in)
goto hookup_failed;
&ipfhook_out) == 0);
if (!hook4_physical_out)
goto hookup_failed;
if (ipf_loopback) {
NH_LOOPBACK_IN, &ipfhook_loop_in) == 0);
if (!hook4_loopback_in)
goto hookup_failed;
NH_LOOPBACK_OUT, &ipfhook_loop_out) == 0);
if (!hook4_loopback_out)
goto hookup_failed;
}
/*
* Add IPv6 hooks
*/
goto hookup_failed;
"ipfilter_hook_nicevents");
&ipfhook_nicevents) == 0);
if (!hook6_nic_events)
goto hookup_failed;
&ipfhook_in) == 0);
if (!hook6_physical_in)
goto hookup_failed;
&ipfhook_out) == 0);
if (!hook6_physical_out)
goto hookup_failed;
if (ipf_loopback) {
NH_LOOPBACK_IN, &ipfhook_loop_in) == 0);
if (!hook6_loopback_in)
goto hookup_failed;
NH_LOOPBACK_OUT, &ipfhook_loop_out) == 0);
if (!hook6_loopback_out)
goto hookup_failed;
}
/*
* Reacquire ipf_global, now it is safe.
*/
/* Do not use private interface ip_params_arr[] in Solaris 10 */
#if SOLARIS2 < 10
#if SOLARIS2 >= 8
#endif
/*
* XXX - There is no terminator for this array, so it is not possible
* to tell if what we are looking for is missing and go off the end
* of the array.
*/
#if SOLARIS2 <= 8
for (i = 0; ; i++) {
"ip_path_mtu_discovery")) {
}
#if SOLARIS2 < 8
"ip_forwarding")) {
}
#else
"ip6_forwarding")) {
}
#endif
#if SOLARIS2 >= 8
ip6_forwarding != NULL &&
#endif
ip_forwarding != NULL)
break;
}
#endif
if (fr_control_forwarding & 1) {
if (ip_forwarding != NULL)
*ip_forwarding = 1;
#if SOLARIS2 >= 8
if (ip6_forwarding != NULL)
*ip6_forwarding = 1;
#endif
}
#endif
return 0;
return -1;
}
static int fr_setipfloopback(set)
int set;
{
return EFAULT;
if (set && !ipf_loopback) {
ipf_loopback = 1;
NH_LOOPBACK_IN, &ipfhook_loop_in) == 0);
if (!hook4_loopback_in)
return EINVAL;
NH_LOOPBACK_OUT, &ipfhook_loop_out) == 0);
if (!hook4_loopback_out)
return EINVAL;
NH_LOOPBACK_IN, &ipfhook_loop_in) == 0);
if (!hook6_loopback_in)
return EINVAL;
NH_LOOPBACK_OUT, &ipfhook_loop_out) == 0);
if (!hook6_loopback_out)
return EINVAL;
} else if (!set && ipf_loopback) {
ipf_loopback = 0;
NH_LOOPBACK_IN, &ipfhook_loop_in) != 0);
if (hook4_loopback_in)
return EBUSY;
NH_LOOPBACK_OUT, &ipfhook_loop_out) != 0);
if (hook4_loopback_out)
return EBUSY;
NH_LOOPBACK_IN, &ipfhook_loop_in) != 0);
if (hook6_loopback_in)
return EBUSY;
NH_LOOPBACK_OUT, &ipfhook_loop_out) != 0);
if (hook6_loopback_out)
return EBUSY;
}
return 0;
}
/*
* Filter ioctl interface.
*/
/*ARGSUSED*/
int cmd;
#if SOLARIS2 >= 7
#else
int *data;
#endif
int mode;
int *rp;
{
#ifdef IPFDEBUG
#endif
if (IPL_LOGMAX < unit)
return ENXIO;
if (fr_running <= 0) {
if (unit != IPL_LOGIPF)
return EIO;
return EIO;
}
if (error != -1) {
return error;
}
error = 0;
switch (cmd)
{
case SIOCFRENB :
else {
sizeof(enable));
if (error != 0) {
break;
}
if (enable) {
if (fr_running > 0)
error = 0;
else
if (error == 0)
fr_running = 1;
else
(void) ipldetach();
} else {
if (error == 0)
fr_running = -1;
}
}
break;
case SIOCIPFSET :
break;
}
/* FALLTHRU */
case SIOCIPFGETNEXT :
case SIOCIPFGET :
break;
case SIOCSETFF :
else {
sizeof(fr_flags));
if (error != 0)
}
break;
case SIOCIPFLP :
sizeof(tmp));
if (error != 0)
else
break;
case SIOCGETFF :
sizeof(fr_flags));
if (error != 0)
break;
case SIOCFUNCL :
break;
case SIOCINAFR :
case SIOCRMAFR :
case SIOCADAFR :
case SIOCZRLST :
else
fr_active, 1);
break;
case SIOCINIFR :
case SIOCRMIFR :
case SIOCADIFR :
else
break;
case SIOCSWAPA :
else {
sizeof(fr_active));
if (error != 0)
else
}
break;
case SIOCGETFS :
fr_getstat(&fio);
break;
case SIOCFRZST :
else
break;
case SIOCIPFFL :
else {
sizeof(tmp));
if (!error) {
sizeof(tmp));
if (error != 0)
} else
}
break;
#ifdef USE_INET6
case SIOCIPFL6 :
else {
sizeof(tmp));
if (!error) {
sizeof(tmp));
if (error != 0)
} else
}
break;
#endif
case SIOCSTLCK :
if (error == 0) {
fr_state_lock = tmp;
fr_nat_lock = tmp;
fr_frag_lock = tmp;
fr_auth_lock = tmp;
} else
break;
#ifdef IPFILTER_LOG
case SIOCIPFFB :
else {
sizeof(tmp));
if (error)
}
break;
#endif /* IPFILTER_LOG */
case SIOCFRSYN :
else {
error = 0;
}
break;
case SIOCGFRST :
break;
case FIONREAD :
#ifdef IPFILTER_LOG
if (error != 0)
#endif
break;
default :
break;
}
return error;
}
char *name;
int v;
{
if (v == 4)
else if (v == 6)
else
return 0;
return (phy);
}
/*
* routines below for saving IP headers to buffer
*/
/*ARGSUSED*/
{
#ifdef IPFDEBUG
#endif
return ENXIO;
return min;
}
/*ARGSUSED*/
{
#ifdef IPFDEBUG
#endif
return min;
}
#ifdef IPFILTER_LOG
/*
* both of these must operate with at least splnet() lest they be
* called during packet processing and cause an inconsistancy to appear in
* the filter lists.
*/
/*ARGSUSED*/
{
# ifdef IPFDEBUG
# endif
if (fr_running < 1)
return EIO;
# ifdef IPFILTER_SYNC
return ipfsync_read(uio);
# endif
}
#endif /* IPFILTER_LOG */
/*
* both of these must operate with at least splnet() lest they be
* called during packet processing and cause an inconsistancy to appear in
* the filter lists.
*/
{
#ifdef IPFDEBUG
#endif
if (fr_running < 1)
return EIO;
#ifdef IPFILTER_SYNC
return ipfsync_write(uio);
#endif /* IPFILTER_SYNC */
return ENXIO;
}
/*
* fr_send_reset - this could conceivably be a call to tcp_respond(), but that
* requires a large amount of setting up and isn't any more efficient.
*/
int fr_send_reset(fin)
{
mblk_t *m;
#ifdef USE_INET6
#endif
return -1;
#ifndef IPFILTER_CKSUM
return -1;
#endif
#ifdef USE_INET6
else
#endif
return -1;
m->b_rptr += 64;
} else {
}
#ifdef USE_INET6
} else
#endif
{
}
return fr_send_ip(fin, m, &m);
}
/*
* Function: fr_send_ip
* Returns: 0: success
* -1: failed
* Parameters:
* fin: packet information
* m: the message block where ip head starts
*
* Send a new packet through the IP stack.
*
* For IPv4 packets, ip_len must be in host byte order, and ip_v,
* ip_ttl, ip_off, and ip_sum are ignored (filled in by this
* function).
*
* For IPv6 packets, ip6_flow, ip6_vfc, and ip6_hlim are filled
* in by this function.
*
* All other portions of the packet must be in on-the-wire format.
*/
/*ARGSUSED*/
{
int i, hlen;
#ifdef USE_INET6
} else
#endif
{
#if SOLARIS2 >= 10
#else
if (ip_ttl_ptr != NULL)
else
if (ip_mtudisc != NULL)
else
#endif
/*
* fr_fastroute, it expects them to be in host byte order but
* ipf_cksum expects them to be in network byte order.
*/
}
return i;
}
int type;
int dst;
{
#ifdef USE_INET6
#endif
mblk_t *m;
#ifdef USE_INET6
#endif
return -1;
#ifdef USE_INET6
return -1;
#endif
#ifndef IPFILTER_CKSUM
return -1;
#endif
#ifdef USE_INET6
if (type == ICMP6_DST_UNREACH)
} else
#endif
{
{
case ICMP_ECHO :
case ICMP_TSTAMP :
case ICMP_IREQ :
case ICMP_MASKREQ :
break;
default :
return 0;
}
}
return -1;
m->b_rptr += 64;
#ifdef USE_INET6
int csz;
if (dst == 0) {
FREE_MB_T(m);
return -1;
}
} else
} else
#endif
{
if (dst == 0) {
FREE_MB_T(m);
return -1;
}
} else {
}
}
/*
* Need to exit out of these so we don't recursively call rw_enter
* from fr_qout.
*/
return fr_send_ip(fin, m, &m);
}
#ifndef _KERNEL
#include <stdio.h>
#endif
/*
* Print out warning message at rate-limited speed.
*/
{
char msg_buf[256];
int need_printed = 0;
now = ddi_get_time();
/* make sure, no multiple entries */
need_printed = 1;
}
MUTEX_EXIT(&ipf_rw);
if (need_printed) {
#ifdef _KERNEL
#else
#endif
}
}
/*
* return the first IP Address associated with an interface
*/
/*ARGSUSED*/
int v, atype;
void *ifptr;
{
void *array;
switch (v)
{
case 4:
break;
case 6:
break;
default:
break;
}
return -1;
switch (atype)
{
case FRI_PEERADDR :
break;
case FRI_BROADCAST :
type[0] = NA_BROADCAST;
break;
default :
type[0] = NA_ADDRESS;
break;
}
return -1;
if (v == 6) {
}
}
{
static int iss_seq_off = 0;
/*
* Compute the base value of the ISS. It is a hash
* of (saddr, sport, daddr, dport, secret).
*/
/*
* Now increment our "timer", and add it in to
* the computed value.
*
* XXX Use `addin'?
* XXX TCP_ISSINCR too large to use?
*/
iss_seq_off += 0x00010000;
newiss += iss_seq_off;
return newiss;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_nextipid */
/* Returns: int - 0 == success, -1 == error (packet should be droppped) */
/* Parameters: fin(I) - pointer to packet information */
/* */
/* Returns the next IPv4 ID to use for this packet. */
/* ------------------------------------------------------------------------ */
{
} else
MUTEX_EXIT(&ipf_rw);
return id;
}
#ifndef IPFILTER_CKSUM
/* ARGSUSED */
#endif
{
#ifdef IPFILTER_CKSUM
#endif
}
#ifdef USE_INET6
# ifndef IPFILTER_CKSUM
/* ARGSUSED */
# endif
{
# ifdef IPFILTER_CKSUM
# endif
}
#endif /* USE_INET6 */
#if (SOLARIS2 < 7)
void fr_slowtimer()
#else
/*ARGSUSED*/
#endif
{
return;
}
fr_natexpire();
fr_ticks++;
else
fr_timer_id = NULL;
}
/* ------------------------------------------------------------------------ */
/* Function: fr_pullup */
/* Returns: NULL == pullup failed, else pointer to protocol header */
/* Parameters: m(I) - pointer to buffer where data packet starts */
/* fin(I) - pointer to packet information */
/* len(I) - number of bytes to pullup */
/* */
/* Attempt to move at least len bytes (from the start of the buffer) into a */
/* single buffer for ease of access. Operating system native functions are */
/* used to manage buffers - if necessary. If the entire packet ends up in */
/* a single buffer, set the FI_COALESCE flag even though fr_coalesce() has */
/* not been called. Both fin_ip and fin_dp are updated before exiting _IF_ */
/* and ONLY if the pullup succeeds. */
/* */
/* We assume that 'min' is a pointer to a buffer that is part of the chain */
/* of buffers that starts at *fin->fin_mp. */
/* ------------------------------------------------------------------------ */
int len;
{
char *ip;
if (m == NULL)
return NULL;
return ip;
else
dpoff = 0;
/*
* pfil_precheck ensures the IP header is on a 32bit
* aligned address so simply fail if that isn't currently
* the case (should never happen).
*/
int inc = 0;
if (ipoff > 0) {
if ((ipoff & 3) != 0) {
else
inc = 0;
}
}
/*
* XXX This is here as a work around for a bug with DEBUG
* XXX Solaris kernels. The problem is b_prev is used by IP
* XXX code as a way to stash the phyint_index for a packet,
* XXX this doesn't get reset by IP but freeb does an ASSERT()
* XXX for both of these to be NULL. See 6442390.
*/
m1 = m;
do {
} while (m1);
/*
* Need to preserve checksum information by copying them
* to newmp which heads the pulluped message.
*/
return NULL;
}
}
return ip;
}
/*
* Function: fr_verifysrc
* Returns: int (really boolean)
* Parameters: fin - packet information
*
* Check whether the packet has a valid source address for the interface on
* which the packet arrived, implementing the "fr_chksrc" feature.
* Returns true iff the packet's source address is valid.
*/
int fr_verifysrc(fin)
{
} else {
return (0);
}
/* Get the index corresponding to the if name */
}
/*
* Function: fr_fastroute
* Returns: 0: success;
* -1: failed
* Parameters:
* mb: the message block where ip head starts
* mpp: the pointer to the pointer of the orignal
* packet message
* fin: packet information
* fdp: destination interface information
* if it is NULL, no interface information provided.
*
* pfil_make_lay2_packet to search route, make lay-2 header
* ,and identify output queue for the IP packet.
* The destination address depends on the following conditions:
* 1: for fastroute rule, fdp is passed in as NULL, so the
* destination address is the IP Packet's destination address
* the interface name, this address is the as destination
* address. Otherwise IP Packet's destination address is used
*/
{
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
#ifndef sparc
#endif
} else {
return (-1);
}
/*
* If this is a duplicate mblk then we want ip to point at that
* data, not the original, if and only if it is already pointing at
* the current mblk data.
*
* Otherwise, if it's not a duplicate, and we're not already pointing
* at the current mblk data, then we want to ensure that the data
* points at ip.
*/
}
/*
* If there is another M_PROTO, we don't want it
*/
}
/*
* In case we're here due to "to <if>" being used with
* "keep state", check that we're going in the correct
* direction.
*/
goto bad_fastroute;
} else {
}
} else {
} else {
}
}
/* disable hardware checksum */
DB_CKSUMFLAGS(mb) = 0;
void *saveifp;
{
/* FALLTHROUGH */
case 0 :
case 1 :
break;
case -1 :
goto bad_fastroute;
}
}
#ifndef sparc
}
#endif
if (net_data_p) {
return (-1);
}
}
fr_frouteok[0]++;
return 0;
fr_frouteok[1]++;
return -1;
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_hook_out */
/* Returns: int - 0 == packet ok, else problem, free packet if not done */
/* Parameters: event(I) - pointer to event */
/* info(I) - pointer to hook information for firewalling */
/* */
/* Calling ipf_hook. */
/* ------------------------------------------------------------------------ */
/*ARGSUSED*/
{
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_hook_in */
/* Returns: int - 0 == packet ok, else problem, free packet if not done */
/* Parameters: event(I) - pointer to event */
/* info(I) - pointer to hook information for firewalling */
/* */
/* Calling ipf_hook. */
/* ------------------------------------------------------------------------ */
/*ARGSUSED*/
{
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_hook_loop_out */
/* Returns: int - 0 == packet ok, else problem, free packet if not done */
/* Parameters: event(I) - pointer to event */
/* info(I) - pointer to hook information for firewalling */
/* */
/* Calling ipf_hook. */
/* ------------------------------------------------------------------------ */
/*ARGSUSED*/
{
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_hook_loop_in */
/* Returns: int - 0 == packet ok, else problem, free packet if not done */
/* Parameters: event(I) - pointer to event */
/* info(I) - pointer to hook information for firewalling */
/* */
/* Calling ipf_hook. */
/* ------------------------------------------------------------------------ */
/*ARGSUSED*/
{
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_hook */
/* Returns: int - 0 == packet ok, else problem, free packet if not done */
/* Parameters: info(I) - pointer to hook information for firewalling */
/* out(I) - whether packet is going in or out */
/* loopback(I) - whether packet is a loopback packet or not */
/* */
/* Stepping stone function between the IP mainline and IPFilter. Extracts */
/* parameters out of the info structure and forms them up to be useful for */
/* calling ipfilter. */
/* ------------------------------------------------------------------------ */
{
if (v == IPV4_VERSION) {
} else
if (loopback)
else
/* For fastroute cases, fr_check returns 0 with mp set to NULL */
rval = 1;
/* Notify IP the packet mblk_t and IP header pointers. */
if ((rval == 0) && (v == IPV4_VERSION)) {
}
return rval;
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_nic_event_v4 */
/* Returns: int - 0 == no problems encountered */
/* Parameters: event(I) - pointer to event */
/* info(I) - pointer to information about a NIC event */
/* */
/* Function to receive asynchronous NIC events from IP */
/* ------------------------------------------------------------------------ */
/*ARGSUSED*/
{
struct sockaddr_in *sin;
{
case NE_PLUMB :
break;
case NE_UNPLUMB :
break;
case NE_ADDRESS_CHANGE :
break;
default :
break;
}
return 0;
}
/* ------------------------------------------------------------------------ */
/* Function: ipf_nic_event_v6 */
/* Returns: int - 0 == no problems encountered */
/* Parameters: event(I) - pointer to event */
/* info(I) - pointer to information about a NIC event */
/* */
/* Function to receive asynchronous NIC events from IP */
/* ------------------------------------------------------------------------ */
/*ARGSUSED*/
{
{
case NE_PLUMB :
break;
case NE_UNPLUMB :
break;
case NE_ADDRESS_CHANGE :
break;
default :
break;
}
return 0;
}