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/netkey/keydb.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: keydb.c,v 1.17 2005/12/11 12:25:16 christos Exp $      */
    2 /*      $KAME: keydb.c,v 1.81 2003/09/07 05:25:20 itojun Exp $  */
    3 
    4 /*
    5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. Neither the name of the project nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __KERNEL_RCSID(0, "$NetBSD: keydb.c,v 1.17 2005/12/11 12:25:16 christos Exp $");
   35 
   36 #include "opt_inet.h"
   37 #include "opt_ipsec.h"
   38 
   39 #include <sys/param.h>
   40 #include <sys/socket.h>
   41 #include <sys/systm.h>
   42 #include <sys/kernel.h>
   43 #include <sys/malloc.h>
   44 #include <sys/errno.h>
   45 #include <sys/queue.h>
   46 #include <sys/mbuf.h>
   47 
   48 #include <net/if.h>
   49 #include <net/route.h>
   50 
   51 #include <netinet/in.h>
   52 
   53 #include <net/pfkeyv2.h>
   54 #include <netkey/keydb.h>
   55 #include <netkey/key.h>
   56 #include <netinet6/ipsec.h>
   57 
   58 #include <net/net_osdep.h>
   59 
   60 MALLOC_DEFINE(M_SECA, "key mgmt", "security associations, key management");
   61 
   62 /*
   63  * secpolicy management
   64  */
   65 struct secpolicy *
   66 keydb_newsecpolicy()
   67 {
   68         struct secpolicy *p;
   69 
   70         p = (struct secpolicy *)malloc(sizeof(*p), M_SECA, M_NOWAIT|M_ZERO);
   71         if (!p)
   72                 return p;
   73         TAILQ_INSERT_TAIL(&sptailq, p, tailq);
   74 
   75         return p;
   76 }
   77 
   78 u_int32_t
   79 keydb_newspid(void)
   80 {
   81         u_int32_t newid = 0;
   82         static u_int32_t lastalloc = IPSEC_MANUAL_POLICYID_MAX;
   83         struct secpolicy *sp;
   84 
   85         newid = lastalloc + 1;
   86         /* XXX possible infinite loop */
   87 again:
   88         TAILQ_FOREACH(sp, &sptailq, tailq) {
   89                 if (sp->id == newid)
   90                         break;
   91         }
   92         if (sp != NULL) {
   93                 if (newid + 1 < newid)  /* wraparound */
   94                         newid = IPSEC_MANUAL_POLICYID_MAX + 1;
   95                 else
   96                         newid++;
   97                 goto again;
   98         }
   99         lastalloc = newid;
  100 
  101         return newid;
  102 }
  103 
  104 void
  105 keydb_delsecpolicy(p)
  106         struct secpolicy *p;
  107 {
  108 
  109         TAILQ_REMOVE(&sptailq, p, tailq);
  110         if (p->spidx)
  111                 free(p->spidx, M_SECA);
  112 #ifdef SADB_X_EXT_TAG
  113         if (p->tag)
  114                 m_nametag_unref(p->tag);
  115 #endif
  116         free(p, M_SECA);
  117 }
  118 
  119 int
  120 keydb_setsecpolicyindex(p, idx)
  121         struct secpolicy *p;
  122         struct secpolicyindex *idx;
  123 {
  124 
  125         if (!p->spidx)
  126                 p->spidx = (struct secpolicyindex *)malloc(sizeof(*p->spidx),
  127                     M_SECA, M_NOWAIT);
  128         if (!p->spidx)
  129                 return ENOMEM;
  130         *p->spidx = *idx;
  131         return 0;
  132 }
  133 
  134 /*
  135  * secashead management
  136  */
  137 struct secashead *
  138 keydb_newsecashead()
  139 {
  140         struct secashead *p;
  141         int i;
  142 
  143         p = (struct secashead *)malloc(sizeof(*p), M_SECA, M_NOWAIT|M_ZERO);
  144         if (!p)
  145                 return p;
  146         for (i = 0; i < sizeof(p->savtree)/sizeof(p->savtree[0]); i++)
  147                 LIST_INIT(&p->savtree[i]);
  148         return p;
  149 }
  150 
  151 void
  152 keydb_delsecashead(p)
  153         struct secashead *p;
  154 {
  155 
  156         free(p, M_SECA);
  157 }
  158 
  159 /*
  160  * secasvar management (reference counted)
  161  */
  162 struct secasvar *
  163 keydb_newsecasvar()
  164 {
  165         struct secasvar *p, *q;
  166         static u_int32_t said = 0;
  167 
  168         p = (struct secasvar *)malloc(sizeof(*p), M_SECA, M_NOWAIT|M_ZERO);
  169         if (!p)
  170                 return p;
  171 
  172 again:
  173         said++;
  174         if (said == 0)
  175                 said++;
  176         TAILQ_FOREACH(q, &satailq, tailq) {
  177                 if (q->id == said)
  178                         goto again;
  179                 if (TAILQ_NEXT(q, tailq)) {
  180                         if (q->id < said && said < TAILQ_NEXT(q, tailq)->id)
  181                                 break;
  182                         if (q->id + 1 < TAILQ_NEXT(q, tailq)->id) {
  183                                 said = q->id + 1;
  184                                 break;
  185                         }
  186                 }
  187         }
  188 
  189         p->id = said;
  190         if (q)
  191                 TAILQ_INSERT_AFTER(&satailq, q, p, tailq);
  192         else
  193                 TAILQ_INSERT_TAIL(&satailq, p, tailq);
  194         return p;
  195 }
  196 
  197 void
  198 keydb_delsecasvar(p)
  199         struct secasvar *p;
  200 {
  201 
  202         TAILQ_REMOVE(&satailq, p, tailq);
  203 
  204         free(p, M_SECA);
  205 }
  206 
  207 /*
  208  * secreplay management
  209  */
  210 struct secreplay *
  211 keydb_newsecreplay(wsize)
  212         size_t wsize;
  213 {
  214         struct secreplay *p;
  215 
  216         p = (struct secreplay *)malloc(sizeof(*p), M_SECA, M_NOWAIT|M_ZERO);
  217         if (!p)
  218                 return p;
  219 
  220         if (wsize != 0) {
  221                 p->bitmap = malloc(wsize, M_SECA, M_NOWAIT|M_ZERO);
  222                 if (!p->bitmap) {
  223                         free(p, M_SECA);
  224                         return NULL;
  225                 }
  226         }
  227         p->wsize = wsize;
  228         return p;
  229 }
  230 
  231 void
  232 keydb_delsecreplay(p)
  233         struct secreplay *p;
  234 {
  235 
  236         if (p->bitmap)
  237                 free(p->bitmap, M_SECA);
  238         free(p, M_SECA);
  239 }
  240 
  241 /*
  242  * secreg management
  243  */
  244 struct secreg *
  245 keydb_newsecreg()
  246 {
  247         struct secreg *p;
  248 
  249         p = (struct secreg *)malloc(sizeof(*p), M_SECA, M_NOWAIT|M_ZERO);
  250         return p;
  251 }
  252 
  253 void
  254 keydb_delsecreg(p)
  255         struct secreg *p;
  256 {
  257 
  258         free(p, M_SECA);
  259 }

Cache object: 50554e825a35f77625e93aef79542a19


[ 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.