The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netipsec/ipsec_osdep.h

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $FreeBSD$       */
    2 /*      $NetBSD: ipsec_osdep.h,v 1.1 2003/08/13 20:06:51 jonathan Exp $ */
    3 
    4 /*-
    5  * Copyright (c) 2003 Jonathan Stone (jonathan@cs.stanford.edu)
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
   20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   26  * POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 
   29 #ifndef NETIPSEC_OSDEP_H
   30 #define NETIPSEC_OSDEP_H
   31 
   32 /* 
   33  *  Hide porting differences across different 4.4BSD-derived platforms.
   34  * 
   35  * 1.  KASSERT() differences: 
   36  * 2.  Kernel  Random-number API differences.
   37  * 3.  Is packet data in an mbuf object writeable?
   38  * 4.  Packet-header semantics.
   39  * 5.  Fast mbuf-cluster allocation.
   40  * 6.  Network packet-output macros.
   41  * 7.  Elased time, in seconds.
   42  * 8.  Test if a  socket object opened by  a privileged (super) user.
   43  * 9.  Global SLIST of all open raw sockets.
   44  * 10. Global SLIST of known interface addresses.
   45  */
   46 
   47 /*
   48  *  1. KASSERT and spl differences 
   49  *
   50  * FreeBSD takes an expression and  parenthesized printf() argument-list.
   51  * NetBSD takes one arg: the expression being asserted.
   52  * FreeBSD's SPLASSERT() takes an SPL level as 1st arg and a
   53  * parenthesized printf-format argument list as the second argument.
   54  *
   55  * This difference is hidden by two 2-argument macros and one 1-arg macro:
   56  *    IPSEC_ASSERT(expr, msg)
   57  *    IPSEC_SPLASSERT(spl, msg)
   58  * One further difference is the spl names:
   59  *    NetBSD splsoftnet equates to FreeBSD splnet;
   60  *    NetBSD splnet equates to FreeBSD splimp.
   61  * which is hidden by the macro IPSEC_SPLASSERT_SOFTNET(msg).
   62  */
   63 #ifdef __FreeBSD__
   64 #if __FreeBSD_version < 500000
   65 #define IPSEC_SPLASSERT(_x,_y) SPLASSERT(_x, _y)
   66 #else
   67 #define IPSEC_SPLASSERT(_x,_y)
   68 #endif
   69 #define IPSEC_SPLASSERT_SOFTNET(_m) IPSEC_SPLASSERT(net,_m)
   70 #define IPSEC_ASSERT(_c,_m) KASSERT(_c, _m)
   71 #endif  /* __FreeBSD__ */
   72 
   73 #ifdef  __NetBSD__
   74 #define IPSEC_SPLASSERT(x,y) (void)0
   75 #define IPSEC_ASSERT(c,m) KASSERT(c)
   76 #define IPSEC_SPLASSERT_SOFTNET(m) IPSEC_SPLASSERT(softnet, m)
   77 #endif  /* __NetBSD__ */
   78 
   79 /*
   80  * 2. Kernel Randomness API.
   81  * FreeBSD uses:
   82  *    u_int read_random(void *outbuf, int nbytes).
   83  */
   84 #ifdef __FreeBSD__
   85 #include <sys/random.h>
   86 /* do nothing, use native random code. */
   87 #endif /* __FreeBSD__ */
   88 
   89 #ifdef  __NetBSD__
   90 #include <sys/rnd.h>
   91 static __inline u_int read_random(void *p, u_int len);
   92 
   93 static __inline u_int
   94 read_random(void *bufp, u_int len) 
   95 { 
   96         return rnd_extract_data(bufp, len, RND_EXTRACT_ANY /*XXX FIXME */);
   97 }
   98 #endif  /* __NetBSD__ */
   99 
  100 /*
  101  * 3. Test for mbuf mutability
  102  * FreeBSD 4.x uses: M_EXT_WRITABLE
  103  * NetBSD has M_READONLY(). Use !M_READONLY().
  104  * Not an exact match to FreeBSD semantics, but adequate for IPsec purposes.
  105  * 
  106  */
  107 #ifdef __NetBSD__
  108 /* XXX wrong, but close enough for restricted ipsec usage. */
  109 #define M_EXT_WRITABLE(m) (!M_READONLY(m))
  110 #endif  /* __NetBSD__ */
  111 
  112 /*
  113  * 4. mbuf packet-header/packet-tag semantics.
  114  * Sam Leffler explains, in private email, some problems with
  115  * M_COPY_PKTHDR(), and why FreeBSD deprecated it and replaced it
  116  * with new, explicit macros M_MOVE_PKTHDR()/M_DUP_PKTHDR().
  117  * he original fast-ipsec source uses M_MOVE_PKTHDR.
  118  * NetBSD currently still uses M_COPY_PKTHDR(), so we define
  119  * M_MOVE_PKTHDR in terms of M_COPY_PKTHDR().  Fast-IPsec
  120  * will delete the source mbuf shortly after copying packet tags,
  121  * so we are safe for fast-ipsec but not in general..
  122  */
  123 #ifdef __NetBSD__
  124 #define M_MOVE_PKTHDR(_f, _t) M_COPY_PKTHDR(_f, _t)
  125 #endif /* __NetBSD__ */
  126 
  127 
  128 /*
  129  * 5. Fast mbuf-cluster allocation.
  130  * FreeBSD 4.6 introduce m_getcl(), which performs `fast' allocation
  131  * mbuf clusters from a cache of recently-freed clusters. (If the  cache
  132  * is empty, new clusters are allocated en-masse).
  133  *   On NetBSD, for now, implement the `cache' as an inline  function
  134  *using normal NetBSD mbuf/cluster allocation macros. Replace this
  135  * with fast-cache code, if and when netBSD  implements one.
  136  */
  137 #ifdef __NetBSD__
  138 static __inline struct mbuf *
  139 m_getcl(int how, short type, int flags)
  140 {
  141         struct mbuf *mp;
  142         if (flags & M_PKTHDR) 
  143                 MGETHDR(mp, how, type);
  144         else
  145                 MGET(mp, how,  type);
  146         if (mp == NULL)
  147                 return NULL;
  148 
  149         MCLGET(mp, how);
  150         return mp;
  151 }
  152 #endif /* __NetBSD__ */
  153 
  154 /*
  155  * 6. Network output macros
  156  * FreeBSD uses the  IF_HANDOFF(), which raises SPL, enqueues
  157  * a packet, and updates interface counters. NetBSD has IFQ_ENQUE(),
  158  * which leaves SPL changes up to the caller. 
  159  * For now, we provide an emulation of IF_HANOOFF() which works
  160  * for protocol input queues.
  161  */
  162 #ifdef __FreeBSD__ 
  163 /* nothing to do */
  164 #endif /* __FreeBSD__ */
  165 #ifdef __NetBSD__
  166 #define IF_HANDOFF(ifq, m, f) if_handoff(ifq, m, f, 0)
  167   
  168 #include <net/if.h>
  169 
  170 static __inline int
  171 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
  172 {
  173         int need_if_start = 0;
  174         int s = splnet();
  175 
  176         if (IF_QFULL(ifq)) {
  177                 IF_DROP(ifq);
  178                 splx(s);
  179                 m_freem(m);
  180                 return (0);
  181         }
  182         if (ifp != NULL) {
  183                 ifp->if_obytes += m->m_pkthdr.len + adjust;
  184                 if (m->m_flags & M_MCAST)
  185                         ifp->if_omcasts++;
  186                 need_if_start = !(ifp->if_flags & IFF_OACTIVE);
  187         }
  188         IF_ENQUEUE(ifq, m);
  189         if (need_if_start)
  190                 (*ifp->if_start)(ifp);
  191         splx(s);
  192         return (1);
  193 }
  194 #endif /* __NetBSD__ */
  195 
  196 /*
  197  * 7. Elapsed Time: time_second as time in seconds.
  198  * Original FreeBSD fast-ipsec code references a FreeBSD kernel global,
  199  * time_second().  NetBSD: kludge #define to use time_mono_time.tv_sec.
  200  */
  201 #ifdef __NetBSD__
  202 #include <sys/kernel.h>
  203 #define time_second mono_time.tv_sec
  204 #endif  /* __NetBSD__ */
  205 
  206 /* protosw glue */
  207 #ifdef __NetBSD__
  208 #include <sys/protosw.h>
  209 #define ipprotosw protosw
  210 #endif  /* __NetBSD__ */
  211 
  212 /*
  213  * 8. Test for "privileged" socket opened by superuser.
  214  * FreeBSD tests  ((so)->so_cred != NULL && priv_check_cred((so)->so_cred,
  215  * PRIV_NETINET_IPSEC, 0) == 0).
  216  * NetBSD (1.6N) tests (so)->so_uid == 0).
  217  * This difference is wrapped inside  the IPSEC_PRIVILEGED_SO() macro.
  218  */
  219 #ifdef __FreeBSD__ 
  220 #define IPSEC_IS_PRIVILEGED_SO(_so) \
  221         ((_so)->so_cred != NULL && \
  222          priv_check_cred((_so)->so_cred, PRIV_NETINET_IPSEC, 0) \
  223          == 0)
  224 #endif  /* __FreeBSD__ */
  225 
  226 #ifdef __NetBSD__
  227 /* superuser opened socket? */
  228 #define IPSEC_IS_PRIVILEGED_SO(so) ((so)->so_uid == 0)
  229 #endif  /* __NetBSD__ */
  230 
  231 /*
  232  * 9. Raw socket list
  233  * FreeBSD uses: listhead = rawcb_list, SLIST()-next field "list".
  234  * NetBSD  uses: listhead = rawcb, SLIST()-next field "list"
  235  *
  236  * This version of fast-ipsec source code  uses rawcb_list as the head,
  237  *  and (to avoid namespace collisions) uses rcb_list as the "next" field.
  238  */
  239 #ifdef __FreeBSD__
  240 #define rcb_list list
  241 #endif /* __FreeBSD__ */
  242 #ifdef __NetBSD__
  243 #define rawcb_list rawcb
  244 #endif  /* __NetBSD__ */
  245 
  246 
  247 /*
  248  * 10. List of all known network interfaces.
  249  * FreeBSD has listhead in_ifaddread, with ia_link as link.
  250  * NetBSD has listhead in_ifaddr, with ia_list as link.
  251  * No name-clahses, so just #define the appropriate names on NetBSD.
  252  * NB: Is it worth introducing iterator (find-first-list/find-next-list)
  253  * functions or macros to encapsulate these?
  254  */
  255 #ifdef __FreeBSD__
  256 /* nothing to do for raw interface list */
  257 #endif  /* FreeBSD */
  258 #ifdef __NetBSD__
  259 /* For now, use FreeBSD-compatible names for raw interface list. */
  260 #define in_ifaddrhead in_ifaddr
  261 #define ia_link ia_list
  262 #endif  /* __NetBSD__ */
  263 
  264 
  265 
  266 
  267 /*
  268  * Differences that we don't attempt to hide:
  269  *
  270  * A. Initialization code.  This  is the largest difference of all.
  271  *
  272  *   FreeBSD uses compile/link-time perl hackery to generate special 
  273  * .o files  with linker sections  that give the moral equivalent of
  274  * C++ file-level-object constructors. NetBSD has no such facility.
  275  *
  276  * Either we implement it (ideally, in a way that can emulate
  277  * FreeBSD's SYSINIT() macros), or we must take other means
  278  * to have the per-file init functions called at some appropriate time.
  279  *
  280  * In the absence of SYSINIT(), all the file-level init functions
  281  * now have "extern" linkage. There is a new fast-ipsec init()
  282  * function which calls each of the per-file in an appropriate order. 
  283  * init_main will arrange to call the fast-ipsec init function
  284  * after the crypto framework has registered its transforms (including
  285  * any autoconfigured hardware crypto  accelerators) but before
  286  * initializing the network stack to send or receive  packet.
  287  *
  288  * B. Protosw() differences. 
  289  * CSRG-style BSD TCP/IP uses a generic protocol-dispatch-function
  290  * where the specific request is identified by an enum argument.
  291  * FreeBSD replaced that with an array of request-specific
  292  * function pointers.
  293  *
  294  * These differences affect the handlers for key-protocol user requests
  295  * so pervasively that I gave up on the fast-ipsec code, and re-worked the
  296  * NetBSD KAME code to match the (relative few) API differences
  297  * between NetBSD and FreeBSD's KAME netkey, and Fast-IPsec netkey.
  298  *
  299  * C. Timeout() versus callout(9):
  300  * The FreeBSD 4.x netipsec/ code still uses timeout().
  301  * FreeBSD 4.7 has callout(9), so I just replaced 
  302  * timeout_*() with the nearest callout_*() equivalents,
  303  * and added a callout handle to the ipsec context.
  304  *
  305  * D. SPL name differences.
  306  * FreeBSD splnet() equates directly to NetBSD's splsoftnet();
  307  * FreeBSD uses splimp() where (for networking) NetBSD would use splnet().
  308  */
  309 #endif /* NETIPSEC_OSDEP_H */

Cache object: fb814ab870a4962b181c142654da4d1f


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.