1 /*      $NetBSD: bpfdesc.h,v 1.29 2009/03/14 14:46:10 dsl Exp $ */
   2 
   3 /*
   4  * Copyright (c) 1990, 1991, 1993
   5  *      The Regents of the University of California.  All rights reserved.
   6  *
   7  * This code is derived from the Stanford/CMU enet packet filter,
   8  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
   9  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
  10  * Berkeley Laboratory.
  11  *
  12  * Redistribution and use in source and binary forms, with or without
  13  * modification, are permitted provided that the following conditions
  14  * are met:
  15  * 1. Redistributions of source code must retain the above copyright
  16  *    notice, this list of conditions and the following disclaimer.
  17  * 2. Redistributions in binary form must reproduce the above copyright
  18  *    notice, this list of conditions and the following disclaimer in the
  19  *    documentation and/or other materials provided with the distribution.
  20  * 3. Neither the name of the University nor the names of its contributors
  21  *    may be used to endorse or promote products derived from this software
  22  *    without specific prior written permission.
  23  *
  24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34  * SUCH DAMAGE.
  35  *
  36  *      @(#)bpfdesc.h   8.1 (Berkeley) 6/10/93
  37  *
  38  * @(#) Header: bpfdesc.h,v 1.14 96/06/16 22:28:07 leres Exp  (LBL)
  39  */
  40 /*
  41  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  42  * Use is subject to license terms.
  43  */
  44 
  45 #ifndef _NET_BPFDESC_H_
  46 #define _NET_BPFDESC_H_
  47 
  48 #include <net/if.h>                       /* for IFNAMSIZ */
  49 #include <sys/mutex.h>
  50 #include <sys/condvar.h>
  51 #include <sys/queue.h>
  52 
  53 /*
  54  * Access to "layer 2" networking is provided through each such provider
  55  * delcaring a set of functions to use in the structure below. It has been
  56  * modeled around what's required to use the mac layer. All of the functions
  57  * below must be declared, even if only filled by a stub function.
  58  */
  59 typedef struct bpf_provider_s {
  60         int             bpr_unit;
  61         int             (*bpr_open)(const char *, uintptr_t *, zoneid_t);
  62         void            (*bpr_close)(uintptr_t);
  63         const char      *(*bpr_name)(uintptr_t);
  64         int             (*bpr_type)(uintptr_t);
  65         void            (*bpr_sdu_get)(uintptr_t, uint_t *);
  66         int             (*bpr_tx)(uintptr_t, mblk_t *);
  67         uintptr_t       (*bpr_promisc_add)(uintptr_t, int, void *, uintptr_t *,
  68                             int);
  69         void            (*bpr_promisc_remove)(uintptr_t);
  70         int             (*bpr_getlinkid)(const char *, datalink_id_t *,
  71                             zoneid_t);
  72         void            (*bpr_client_close)(uintptr_t);
  73         const char      *(*bpr_client_name)(uintptr_t);
  74         int             (*bpr_client_open)(uintptr_t, uintptr_t *);
  75         int             (*bpr_getzone)(uintptr_t, zoneid_t *);
  76         int             (*bpr_getdlt)(uintptr_t, uint_t *);
  77 } bpf_provider_t;
  78 
  79 typedef struct bpf_provider_list {
  80         LIST_ENTRY(bpf_provider_list)   bpl_next;
  81         bpf_provider_t                  *bpl_what;
  82 } bpf_provider_list_t;
  83 
  84 /*
  85  * The bpr_field from bpf_provider_t expects an integer that comes from
  86  * the list of defines below.
  87  */
  88 #define BPR_MAC         1
  89 #define BPR_IPNET       2
  90 
  91 #define MBPF_OPEN(_m, _n, _p, _z)       (_m)->bpr_open(_n, (uintptr_t *)_p, _z)
  92 #define MBPF_CLOSE(_m, _h)              (_m)->bpr_close(_h)
  93 #define MBPF_NAME(_m, _h)               (_m)->bpr_name(_h)
  94 #define MBPF_TYPE(_m, _h)               (_m)->bpr_type(_h)
  95 #define MBPF_SDU_GET(_m, _h, _p)        (_m)->bpr_sdu_get(_h, _p)
  96 #define MBPF_TX(_m, _h, _pkt)           (_m)->bpr_tx(_h, _pkt)
  97 #define MBPF_PROMISC_ADD(_m, _h, _o, _d, _p, _f) \
  98                                 (_m)->bpr_promisc_add(_h, _o, _d, _p, _f)
  99 #define MBPF_PROMISC_REMOVE(_m, _h)     (_m)->bpr_promisc_remove(_h)
 100 #define MBPF_GET_LINKID(_m, _n, _ip, _z) \
 101                                         (_m)->bpr_getlinkid(_n, _ip, _z)
 102 #define MBPF_CLIENT_CLOSE(_m, _h)       (_m)->bpr_client_close(_h)
 103 #define MBPF_CLIENT_NAME(_m, _h)        (_m)->bpr_client_name(_h)
 104 #define MBPF_CLIENT_OPEN(_m, _h, _p)    (_m)->bpr_client_open((uintptr_t)_h, \
 105                                             (uintptr_t *)_p)
 106 #define MBPF_GET_ZONE(_m, _h, _zp)      (_m)->bpr_getzone(_h, _zp)
 107 #define MBPF_GET_DLT(_m, _h, _dp)       (_m)->bpr_getdlt(_h, _dp);
 108 #define MBPF_GET_HDRLEN(_m, _h, _dp)    (_m)->bpr_gethdrlen(_h, _dp);
 109 
 110 
 111 /*
 112  * Descriptor associated with each open bpf file.
 113  */
 114 struct bpf_d {
 115         LIST_ENTRY(bpf_d) bd_list;      /* List of bpf_d */
 116         LIST_ENTRY(bpf_d) bd_next;      /* List attaced to bif_if */
 117         /*
 118          * Buffer slots: two mbuf clusters buffer the incoming packets.
 119          *   The model has three slots.  Sbuf is always occupied.
 120          *   sbuf (store) - Receive interrupt puts packets here.
 121          *   hbuf (hold) - When sbuf is full, put cluster here and
 122          *                 wakeup read (replace sbuf with fbuf).
 123          *   fbuf (free) - When read is done, put cluster here.
 124          * On receiving, if sbuf is full and fbuf is 0, packet is dropped.
 125          */
 126         void *          bd_sbuf;        /* store slot */
 127         void *          bd_hbuf;        /* hold slot */
 128         void *          bd_fbuf;        /* free slot */
 129         int             bd_slen;        /* current length of store buffer */
 130         int             bd_hlen;        /* current length of hold buffer */
 131 
 132         int             bd_bufsize;     /* absolute length of buffers */
 133 
 134         uintptr_t       bd_bif;         /* interface pointer */
 135         ulong_t         bd_rtout;       /* Read timeout in 'ticks' */
 136         struct bpf_insn *bd_filter;     /* filter code */
 137         size_t          bd_filter_size;
 138         ulong_t         bd_rcount;      /* number of packets received */
 139         ulong_t         bd_dcount;      /* number of packets dropped */
 140         ulong_t         bd_ccount;      /* number of packets captured */
 141 
 142         uchar_t         bd_promisc;     /* true if listening promiscuously */
 143         uchar_t         bd_state;       /* idle, waiting, or timed out */
 144         uchar_t         bd_immediate;   /* true to return on packet arrival */
 145         int             bd_hdrcmplt;    /* false to fill in src lladdr */
 146         int             bd_seesent;     /* true if bpf should see sent pkts */
 147         int             bd_async;       /* non-zero if packet reception .. */
 148                                         /* .. should generate signal */
 149         int             bd_nonblock;    /* non-zero for non-blocking read */
 150         pid_t           bd_pgid;        /* process or group id for signal */
 151         int             bd_timedout;
 152         timeout_id_t    bd_callout;     /* for BPF timeouts with select */
 153         pid_t           bd_pid;         /* corresponding PID */
 154         void            *bd_sih;        /* soft interrupt handle */
 155         /*
 156          * Solaris specific bits after this.
 157          */
 158         kmutex_t        bd_lock;
 159         kcondvar_t      bd_wait;
 160         uintptr_t       bd_mh;          /* where mac_handle gets put */
 161         uintptr_t       bd_mcip;        /* Where mac_client_handle_t gets put */
 162         uintptr_t       bd_promisc_handle;
 163         minor_t         bd_dev;         /* device number for this handle */
 164         int             bd_fmode;       /* flags from bpfopen */
 165         zoneid_t        bd_zone;        /* zoneid of the opening process */
 166         int             bd_inuse;
 167         int             bd_waiting;
 168         char            bd_ifname[LIFNAMSIZ];
 169         int             bd_dlt;
 170         int             bd_hdrlen;
 171         bpf_provider_t  bd_mac;
 172         datalink_id_t   bd_linkid;
 173         /*
 174          * bd_promisc_flags is used to store the promiscuous state of the
 175          * the interface in BPF so that the correct mode of operation can
 176          * be kept across changing DLT or network interface.
 177          */
 178         int             bd_promisc_flags;
 179 };
 180 
 181 
 182 /* Values for bd_state */
 183 #define BPF_IDLE        0               /* no select in progress */
 184 #define BPF_WAITING     1               /* waiting for read timeout in select */
 185 #define BPF_TIMED_OUT   2               /* read timeout has expired in select */
 186 
 187 /*
 188  * Description associated with the external representation of each
 189  * open bpf file.
 190  */
 191 struct bpf_d_ext {
 192         int32_t         bde_bufsize;
 193         uint8_t         bde_promisc;
 194         uint8_t         bde_state;
 195         uint8_t         bde_immediate;
 196         int32_t         bde_hdrcmplt;
 197         int32_t         bde_seesent;
 198         pid_t           bde_pid;
 199         uint64_t        bde_rcount;             /* number of packets received */
 200         uint64_t        bde_dcount;             /* number of packets dropped */
 201         uint64_t        bde_ccount;             /* number of packets captured */
 202         char            bde_ifname[IFNAMSIZ];
 203 };
 204 
 205 #ifdef _KERNEL
 206 typedef struct bpf_kstats_s {
 207         kstat_named_t   kp_read_wait;
 208         kstat_named_t   kp_write_ok;
 209         kstat_named_t   kp_write_error;
 210         kstat_named_t   kp_receive;
 211         kstat_named_t   kp_capture;
 212         kstat_named_t   kp_dropped;
 213 } bpf_kstats_t;
 214 
 215 int      bpf_setf(struct bpf_d *, struct bpf_program *);
 216 #endif
 217 
 218 typedef void    (*bpf_attach_fn_t)(uintptr_t, int, zoneid_t, int);
 219 typedef void    (*bpf_detach_fn_t)(uintptr_t);
 220 typedef int     (*bpf_provider_reg_fn_t)(bpf_provider_t *);
 221 typedef LIST_HEAD(, bpf_provider_list) bpf_provider_head_t;
 222 
 223 extern bpf_provider_t   *bpf_find_provider_by_id(int);
 224 extern int              bpf_provider_tickle(char *, zoneid_t);
 225 extern bpf_provider_head_t bpf_providers;
 226 
 227 #endif /* !_NET_BPFDESC_H_ */