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/keysock.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 /*      $FreeBSD: releng/5.0/sys/netipsec/keysock.c 105197 2002-10-16 02:10:08Z sam $   */
    2 /*      $KAME: keysock.c,v 1.25 2001/08/13 20:07:41 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 "opt_ipsec.h"
   34 
   35 /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
   36 
   37 #include <sys/types.h>
   38 #include <sys/param.h>
   39 #include <sys/domain.h>
   40 #include <sys/errno.h>
   41 #include <sys/kernel.h>
   42 #include <sys/malloc.h>
   43 #include <sys/mbuf.h>
   44 #include <sys/protosw.h>
   45 #include <sys/signalvar.h>
   46 #include <sys/socket.h>
   47 #include <sys/socketvar.h>
   48 #include <sys/sysctl.h>
   49 #include <sys/systm.h>
   50 
   51 #include <net/raw_cb.h>
   52 #include <net/route.h>
   53 
   54 #include <net/pfkeyv2.h>
   55 #include <netipsec/key.h>
   56 #include <netipsec/keysock.h>
   57 #include <netipsec/key_debug.h>
   58 
   59 #include <machine/stdarg.h>
   60 
   61 struct key_cb {
   62         int key_count;
   63         int any_count;
   64 };
   65 static struct key_cb key_cb;
   66 
   67 static struct sockaddr key_dst = { 2, PF_KEY, };
   68 static struct sockaddr key_src = { 2, PF_KEY, };
   69 
   70 static int key_sendup0 __P((struct rawcb *, struct mbuf *, int));
   71 
   72 struct pfkeystat pfkeystat;
   73 
   74 /*
   75  * key_output()
   76  */
   77 int
   78 #if __STDC__
   79 key_output(struct mbuf *m, ...)
   80 #else
   81 key_output(m, va_alist)
   82         struct mbuf *m;
   83         va_dcl
   84 #endif
   85 {
   86         struct sadb_msg *msg;
   87         int len, error = 0;
   88         int s;
   89         struct socket *so;
   90         va_list ap;
   91 
   92         va_start(ap, m);
   93         so = va_arg(ap, struct socket *);
   94         va_end(ap);
   95 
   96         if (m == 0)
   97                 panic("key_output: NULL pointer was passed.\n");
   98 
   99         pfkeystat.out_total++;
  100         pfkeystat.out_bytes += m->m_pkthdr.len;
  101 
  102         len = m->m_pkthdr.len;
  103         if (len < sizeof(struct sadb_msg)) {
  104                 pfkeystat.out_tooshort++;
  105                 error = EINVAL;
  106                 goto end;
  107         }
  108 
  109         if (m->m_len < sizeof(struct sadb_msg)) {
  110                 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
  111                         pfkeystat.out_nomem++;
  112                         error = ENOBUFS;
  113                         goto end;
  114                 }
  115         }
  116 
  117         if ((m->m_flags & M_PKTHDR) == 0)
  118                 panic("key_output: not M_PKTHDR ??");
  119 
  120         KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
  121 
  122         msg = mtod(m, struct sadb_msg *);
  123         pfkeystat.out_msgtype[msg->sadb_msg_type]++;
  124         if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
  125                 pfkeystat.out_invlen++;
  126                 error = EINVAL;
  127                 goto end;
  128         }
  129 
  130         /*XXX giant lock*/
  131         s = splnet();
  132         error = key_parse(m, so);
  133         m = NULL;
  134         splx(s);
  135 end:
  136         if (m)
  137                 m_freem(m);
  138         return error;
  139 }
  140 
  141 /*
  142  * send message to the socket.
  143  */
  144 static int
  145 key_sendup0(rp, m, promisc)
  146         struct rawcb *rp;
  147         struct mbuf *m;
  148         int promisc;
  149 {
  150         int error;
  151 
  152         if (promisc) {
  153                 struct sadb_msg *pmsg;
  154 
  155                 M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT);
  156                 if (m && m->m_len < sizeof(struct sadb_msg))
  157                         m = m_pullup(m, sizeof(struct sadb_msg));
  158                 if (!m) {
  159                         pfkeystat.in_nomem++;
  160                         m_freem(m);
  161                         return ENOBUFS;
  162                 }
  163                 m->m_pkthdr.len += sizeof(*pmsg);
  164 
  165                 pmsg = mtod(m, struct sadb_msg *);
  166                 bzero(pmsg, sizeof(*pmsg));
  167                 pmsg->sadb_msg_version = PF_KEY_V2;
  168                 pmsg->sadb_msg_type = SADB_X_PROMISC;
  169                 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
  170                 /* pid and seq? */
  171 
  172                 pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
  173         }
  174 
  175         if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
  176             m, NULL)) {
  177                 pfkeystat.in_nomem++;
  178                 m_freem(m);
  179                 error = ENOBUFS;
  180         } else
  181                 error = 0;
  182         sorwakeup(rp->rcb_socket);
  183         return error;
  184 }
  185 
  186 /* XXX this interface should be obsoleted. */
  187 int
  188 key_sendup(so, msg, len, target)
  189         struct socket *so;
  190         struct sadb_msg *msg;
  191         u_int len;
  192         int target;     /*target of the resulting message*/
  193 {
  194         struct mbuf *m, *n, *mprev;
  195         int tlen;
  196 
  197         /* sanity check */
  198         if (so == 0 || msg == 0)
  199                 panic("key_sendup: NULL pointer was passed.\n");
  200 
  201         KEYDEBUG(KEYDEBUG_KEY_DUMP,
  202                 printf("key_sendup: \n");
  203                 kdebug_sadb(msg));
  204 
  205         /*
  206          * we increment statistics here, just in case we have ENOBUFS
  207          * in this function.
  208          */
  209         pfkeystat.in_total++;
  210         pfkeystat.in_bytes += len;
  211         pfkeystat.in_msgtype[msg->sadb_msg_type]++;
  212 
  213         /*
  214          * Get mbuf chain whenever possible (not clusters),
  215          * to save socket buffer.  We'll be generating many SADB_ACQUIRE
  216          * messages to listening key sockets.  If we simply allocate clusters,
  217          * sbappendaddr() will raise ENOBUFS due to too little sbspace().
  218          * sbspace() computes # of actual data bytes AND mbuf region.
  219          *
  220          * TODO: SADB_ACQUIRE filters should be implemented.
  221          */
  222         tlen = len;
  223         m = mprev = NULL;
  224         while (tlen > 0) {
  225                 if (tlen == len) {
  226                         MGETHDR(n, M_DONTWAIT, MT_DATA);
  227                         n->m_len = MHLEN;
  228                 } else {
  229                         MGET(n, M_DONTWAIT, MT_DATA);
  230                         n->m_len = MLEN;
  231                 }
  232                 if (!n) {
  233                         pfkeystat.in_nomem++;
  234                         return ENOBUFS;
  235                 }
  236                 if (tlen >= MCLBYTES) { /*XXX better threshold? */
  237                         MCLGET(n, M_DONTWAIT);
  238                         if ((n->m_flags & M_EXT) == 0) {
  239                                 m_free(n);
  240                                 m_freem(m);
  241                                 pfkeystat.in_nomem++;
  242                                 return ENOBUFS;
  243                         }
  244                         n->m_len = MCLBYTES;
  245                 }
  246 
  247                 if (tlen < n->m_len)
  248                         n->m_len = tlen;
  249                 n->m_next = NULL;
  250                 if (m == NULL)
  251                         m = mprev = n;
  252                 else {
  253                         mprev->m_next = n;
  254                         mprev = n;
  255                 }
  256                 tlen -= n->m_len;
  257                 n = NULL;
  258         }
  259         m->m_pkthdr.len = len;
  260         m->m_pkthdr.rcvif = NULL;
  261         m_copyback(m, 0, len, (caddr_t)msg);
  262 
  263         /* avoid duplicated statistics */
  264         pfkeystat.in_total--;
  265         pfkeystat.in_bytes -= len;
  266         pfkeystat.in_msgtype[msg->sadb_msg_type]--;
  267 
  268         return key_sendup_mbuf(so, m, target);
  269 }
  270 
  271 /* so can be NULL if target != KEY_SENDUP_ONE */
  272 int
  273 key_sendup_mbuf(so, m, target)
  274         struct socket *so;
  275         struct mbuf *m;
  276         int target;
  277 {
  278         struct mbuf *n;
  279         struct keycb *kp;
  280         int sendup;
  281         struct rawcb *rp;
  282         int error = 0;
  283 
  284         if (m == NULL)
  285                 panic("key_sendup_mbuf: NULL pointer was passed.\n");
  286         if (so == NULL && target == KEY_SENDUP_ONE)
  287                 panic("key_sendup_mbuf: NULL pointer was passed.\n");
  288 
  289         pfkeystat.in_total++;
  290         pfkeystat.in_bytes += m->m_pkthdr.len;
  291         if (m->m_len < sizeof(struct sadb_msg)) {
  292 #if 1
  293                 m = m_pullup(m, sizeof(struct sadb_msg));
  294                 if (m == NULL) {
  295                         pfkeystat.in_nomem++;
  296                         return ENOBUFS;
  297                 }
  298 #else
  299                 /* don't bother pulling it up just for stats */
  300 #endif
  301         }
  302         if (m->m_len >= sizeof(struct sadb_msg)) {
  303                 struct sadb_msg *msg;
  304                 msg = mtod(m, struct sadb_msg *);
  305                 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
  306         }
  307 
  308         LIST_FOREACH(rp, &rawcb_list, list)
  309         {
  310                 if (rp->rcb_proto.sp_family != PF_KEY)
  311                         continue;
  312                 if (rp->rcb_proto.sp_protocol
  313                  && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
  314                         continue;
  315                 }
  316 
  317                 kp = (struct keycb *)rp;
  318 
  319                 /*
  320                  * If you are in promiscuous mode, and when you get broadcasted
  321                  * reply, you'll get two PF_KEY messages.
  322                  * (based on pf_key@inner.net message on 14 Oct 1998)
  323                  */
  324                 if (((struct keycb *)rp)->kp_promisc) {
  325                         if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
  326                                 (void)key_sendup0(rp, n, 1);
  327                                 n = NULL;
  328                         }
  329                 }
  330 
  331                 /* the exact target will be processed later */
  332                 if (so && sotorawcb(so) == rp)
  333                         continue;
  334 
  335                 sendup = 0;
  336                 switch (target) {
  337                 case KEY_SENDUP_ONE:
  338                         /* the statement has no effect */
  339                         if (so && sotorawcb(so) == rp)
  340                                 sendup++;
  341                         break;
  342                 case KEY_SENDUP_ALL:
  343                         sendup++;
  344                         break;
  345                 case KEY_SENDUP_REGISTERED:
  346                         if (kp->kp_registered)
  347                                 sendup++;
  348                         break;
  349                 }
  350                 pfkeystat.in_msgtarget[target]++;
  351 
  352                 if (!sendup)
  353                         continue;
  354 
  355                 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
  356                         m_freem(m);
  357                         pfkeystat.in_nomem++;
  358                         return ENOBUFS;
  359                 }
  360 
  361                 if ((error = key_sendup0(rp, n, 0)) != 0) {
  362                         m_freem(m);
  363                         return error;
  364                 }
  365 
  366                 n = NULL;
  367         }
  368 
  369         if (so) {
  370                 error = key_sendup0(sotorawcb(so), m, 0);
  371                 m = NULL;
  372         } else {
  373                 error = 0;
  374                 m_freem(m);
  375         }
  376         return error;
  377 }
  378 
  379 /*
  380  * key_abort()
  381  * derived from net/rtsock.c:rts_abort()
  382  */
  383 static int
  384 key_abort(struct socket *so)
  385 {
  386         int s, error;
  387         s = splnet();
  388         error = raw_usrreqs.pru_abort(so);
  389         splx(s);
  390         return error;
  391 }
  392 
  393 /*
  394  * key_attach()
  395  * derived from net/rtsock.c:rts_attach()
  396  */
  397 static int
  398 key_attach(struct socket *so, int proto, struct thread *td)
  399 {
  400         struct keycb *kp;
  401         int s, error;
  402 
  403         if (sotorawcb(so) != 0)
  404                 return EISCONN; /* XXX panic? */
  405         kp = (struct keycb *)malloc(sizeof *kp, M_PCB, M_WAITOK|M_ZERO); /* XXX */
  406         if (kp == 0)
  407                 return ENOBUFS;
  408 
  409         /*
  410          * The splnet() is necessary to block protocols from sending
  411          * error notifications (like RTM_REDIRECT or RTM_LOSING) while
  412          * this PCB is extant but incompletely initialized.
  413          * Probably we should try to do more of this work beforehand and
  414          * eliminate the spl.
  415          */
  416         s = splnet();
  417         so->so_pcb = (caddr_t)kp;
  418         error = raw_usrreqs.pru_attach(so, proto, td);
  419         kp = (struct keycb *)sotorawcb(so);
  420         if (error) {
  421                 free(kp, M_PCB);
  422                 so->so_pcb = (caddr_t) 0;
  423                 splx(s);
  424                 return error;
  425         }
  426 
  427         kp->kp_promisc = kp->kp_registered = 0;
  428 
  429         if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
  430                 key_cb.key_count++;
  431         key_cb.any_count++;
  432         kp->kp_raw.rcb_laddr = &key_src;
  433         kp->kp_raw.rcb_faddr = &key_dst;
  434         soisconnected(so);
  435         so->so_options |= SO_USELOOPBACK;
  436 
  437         splx(s);
  438         return 0;
  439 }
  440 
  441 /*
  442  * key_bind()
  443  * derived from net/rtsock.c:rts_bind()
  444  */
  445 static int
  446 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
  447 {
  448         int s, error;
  449         s = splnet();
  450         error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
  451         splx(s);
  452         return error;
  453 }
  454 
  455 /*
  456  * key_connect()
  457  * derived from net/rtsock.c:rts_connect()
  458  */
  459 static int
  460 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
  461 {
  462         int s, error;
  463         s = splnet();
  464         error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
  465         splx(s);
  466         return error;
  467 }
  468 
  469 /*
  470  * key_detach()
  471  * derived from net/rtsock.c:rts_detach()
  472  */
  473 static int
  474 key_detach(struct socket *so)
  475 {
  476         struct keycb *kp = (struct keycb *)sotorawcb(so);
  477         int s, error;
  478 
  479         s = splnet();
  480         if (kp != 0) {
  481                 if (kp->kp_raw.rcb_proto.sp_protocol
  482                     == PF_KEY) /* XXX: AF_KEY */
  483                         key_cb.key_count--;
  484                 key_cb.any_count--;
  485 
  486                 key_freereg(so);
  487         }
  488         error = raw_usrreqs.pru_detach(so);
  489         splx(s);
  490         return error;
  491 }
  492 
  493 /*
  494  * key_disconnect()
  495  * derived from net/rtsock.c:key_disconnect()
  496  */
  497 static int
  498 key_disconnect(struct socket *so)
  499 {
  500         int s, error;
  501         s = splnet();
  502         error = raw_usrreqs.pru_disconnect(so);
  503         splx(s);
  504         return error;
  505 }
  506 
  507 /*
  508  * key_peeraddr()
  509  * derived from net/rtsock.c:rts_peeraddr()
  510  */
  511 static int
  512 key_peeraddr(struct socket *so, struct sockaddr **nam)
  513 {
  514         int s, error;
  515         s = splnet();
  516         error = raw_usrreqs.pru_peeraddr(so, nam);
  517         splx(s);
  518         return error;
  519 }
  520 
  521 /*
  522  * key_send()
  523  * derived from net/rtsock.c:rts_send()
  524  */
  525 static int
  526 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
  527          struct mbuf *control, struct thread *td)
  528 {
  529         int s, error;
  530         s = splnet();
  531         error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
  532         splx(s);
  533         return error;
  534 }
  535 
  536 /*
  537  * key_shutdown()
  538  * derived from net/rtsock.c:rts_shutdown()
  539  */
  540 static int
  541 key_shutdown(struct socket *so)
  542 {
  543         int s, error;
  544         s = splnet();
  545         error = raw_usrreqs.pru_shutdown(so);
  546         splx(s);
  547         return error;
  548 }
  549 
  550 /*
  551  * key_sockaddr()
  552  * derived from net/rtsock.c:rts_sockaddr()
  553  */
  554 static int
  555 key_sockaddr(struct socket *so, struct sockaddr **nam)
  556 {
  557         int s, error;
  558         s = splnet();
  559         error = raw_usrreqs.pru_sockaddr(so, nam);
  560         splx(s);
  561         return error;
  562 }
  563 
  564 struct pr_usrreqs key_usrreqs = {
  565         key_abort, pru_accept_notsupp, key_attach, key_bind,
  566         key_connect,
  567         pru_connect2_notsupp, pru_control_notsupp, key_detach,
  568         key_disconnect, pru_listen_notsupp, key_peeraddr,
  569         pru_rcvd_notsupp,
  570         pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
  571         key_sockaddr, sosend, soreceive, sopoll
  572 };
  573 
  574 /* sysctl */
  575 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
  576 
  577 /*
  578  * Definitions of protocols supported in the KEY domain.
  579  */
  580 
  581 extern struct domain keydomain;
  582 
  583 struct protosw keysw[] = {
  584 { SOCK_RAW,     &keydomain,     PF_KEY_V2,      PR_ATOMIC|PR_ADDR,
  585   0,            (pr_output_t *)key_output,      raw_ctlinput, 0,
  586   0,
  587   raw_init,     0,              0,              0,
  588   &key_usrreqs
  589 }
  590 };
  591 
  592 static void
  593 key_init0(void)
  594 {
  595         bzero((caddr_t)&key_cb, sizeof(key_cb));
  596         key_init();
  597 }
  598 
  599 struct domain keydomain =
  600     { PF_KEY, "key", key_init0, 0, 0,
  601       keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])] };
  602 
  603 DOMAIN_SET(key);

Cache object: fd123392305a8f2a4278cee03a904e9f


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