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/netinet6/send.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 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2009-2010 Ana Kukec <anchie@FreeBSD.org>
    5  * All rights reserved.
    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 AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 
   32 #include <sys/param.h>
   33 #include <sys/kernel.h>
   34 #include <sys/malloc.h>
   35 #include <sys/mbuf.h>
   36 #include <sys/module.h>
   37 #include <sys/priv.h>
   38 #include <sys/protosw.h>
   39 #include <sys/sdt.h>
   40 #include <sys/systm.h>
   41 #include <sys/socket.h>
   42 #include <sys/sockbuf.h>
   43 #include <sys/socketvar.h>
   44 #include <sys/types.h>
   45 
   46 #include <net/route.h>
   47 #include <net/if.h>
   48 #include <net/if_var.h>
   49 #include <net/vnet.h>
   50 
   51 #include <netinet/in.h>
   52 #include <netinet/in_kdtrace.h>
   53 #include <netinet/ip_var.h>
   54 #include <netinet/ip6.h>
   55 #include <netinet/icmp6.h>
   56 
   57 #include <netinet6/in6_var.h>
   58 #include <netinet6/nd6.h>
   59 #include <netinet6/scope6_var.h>
   60 #include <netinet6/send.h>
   61 
   62 static MALLOC_DEFINE(M_SEND, "send", "Secure Neighbour Discovery");
   63 
   64 /*
   65  * The socket used to communicate with the SeND daemon.
   66  */
   67 VNET_DEFINE_STATIC(struct socket *, send_so);
   68 #define V_send_so       VNET(send_so)
   69 
   70 u_long  send_sendspace  = 8 * (1024 + sizeof(struct sockaddr_send));
   71 u_long  send_recvspace  = 9216;
   72 
   73 struct mtx      send_mtx;
   74 #define SEND_LOCK_INIT()        mtx_init(&send_mtx, "send_mtx", NULL, MTX_DEF)
   75 #define SEND_LOCK()             mtx_lock(&send_mtx)
   76 #define SEND_UNLOCK()           mtx_unlock(&send_mtx)
   77 #define SEND_LOCK_DESTROY()     mtx_destroy(&send_mtx)
   78 
   79 static int
   80 send_attach(struct socket *so, int proto, struct thread *td)
   81 {
   82         int error;
   83 
   84         SEND_LOCK();
   85         if (V_send_so != NULL) {
   86                 SEND_UNLOCK();
   87                 return (EEXIST);
   88         }
   89 
   90         error = priv_check(td, PRIV_NETINET_RAW);
   91         if (error) {
   92                 SEND_UNLOCK();
   93                 return(error);
   94         }
   95 
   96         if (proto != IPPROTO_SEND) {
   97                 SEND_UNLOCK();
   98                 return (EPROTONOSUPPORT);
   99         }
  100         error = soreserve(so, send_sendspace, send_recvspace);
  101         if (error) {
  102                 SEND_UNLOCK();
  103                 return(error);
  104         }
  105 
  106         V_send_so = so;
  107         SEND_UNLOCK();
  108 
  109         return (0);
  110 }
  111 
  112 static int
  113 send_output(struct mbuf *m, struct ifnet *ifp, int direction)
  114 {
  115         struct ip6_hdr *ip6;
  116         struct sockaddr_in6 dst;
  117         struct icmp6_hdr *icmp6;
  118         struct epoch_tracker et;
  119         int icmp6len;
  120         int error;
  121 
  122         /*
  123          * Receive incoming (SeND-protected) or outgoing traffic
  124          * (SeND-validated) from the SeND user space application.
  125          */
  126 
  127         switch (direction) {
  128         case SND_IN:
  129                 if (m->m_len < (sizeof(struct ip6_hdr) +
  130                     sizeof(struct icmp6_hdr))) {
  131                         m = m_pullup(m, sizeof(struct ip6_hdr) +
  132                             sizeof(struct icmp6_hdr));
  133                         if (!m)
  134                                 return (ENOBUFS);
  135                 }
  136 
  137                 /* Before passing off the mbuf record the proper interface. */
  138                 m->m_pkthdr.rcvif = ifp;
  139 
  140                 if (m->m_flags & M_PKTHDR)
  141                         icmp6len = m->m_pkthdr.len - sizeof(struct ip6_hdr);
  142                 else
  143                         panic("Doh! not the first mbuf.");
  144 
  145                 ip6 = mtod(m, struct ip6_hdr *);
  146                 icmp6 = (struct icmp6_hdr *)(ip6 + 1);
  147                 error = 0;
  148 
  149                 /*
  150                  * Output the packet as icmp6.c:icpm6_input() would do.
  151                  * The mbuf is always consumed, so we do not have to
  152                  * care about that.
  153                  */
  154                 NET_EPOCH_ENTER(et);
  155                 switch (icmp6->icmp6_type) {
  156                 case ND_NEIGHBOR_SOLICIT:
  157                         nd6_ns_input(m, sizeof(struct ip6_hdr), icmp6len);
  158                         break;
  159                 case ND_NEIGHBOR_ADVERT:
  160                         nd6_na_input(m, sizeof(struct ip6_hdr), icmp6len);
  161                         break;
  162                 case ND_REDIRECT:
  163                         icmp6_redirect_input(m, sizeof(struct ip6_hdr));
  164                         break;
  165                 case ND_ROUTER_SOLICIT:
  166                         nd6_rs_input(m, sizeof(struct ip6_hdr), icmp6len);
  167                         break;
  168                 case ND_ROUTER_ADVERT:
  169                         nd6_ra_input(m, sizeof(struct ip6_hdr), icmp6len);
  170                         break;
  171                 default:
  172                         m_freem(m);
  173                         error = ENOSYS;
  174                 }
  175                 NET_EPOCH_EXIT(et);
  176 
  177                 return (error);
  178 
  179         case SND_OUT:
  180                 if (m->m_len < sizeof(struct ip6_hdr)) {
  181                         m = m_pullup(m, sizeof(struct ip6_hdr));
  182                         if (!m)
  183                                 return (ENOBUFS);
  184                 }
  185                 ip6 = mtod(m, struct ip6_hdr *);
  186                 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
  187                         m->m_flags |= M_MCAST;
  188 
  189                 bzero(&dst, sizeof(dst));
  190                 dst.sin6_family = AF_INET6;
  191                 dst.sin6_len = sizeof(dst);
  192                 dst.sin6_addr = ip6->ip6_dst;
  193 
  194                 m_clrprotoflags(m);     /* Avoid confusing lower layers. */
  195 
  196                 IP_PROBE(send, NULL, NULL, ip6, ifp, NULL, ip6);
  197 
  198                 /*
  199                  * Output the packet as nd6.c:nd6_output_lle() would do.
  200                  * The mbuf is always consumed, so we do not have to care
  201                  * about that.
  202                  * XXX-BZ as we added data, what about fragmenting,
  203                  * if now needed?
  204                  */
  205                 error = ((*ifp->if_output)(ifp, m, (struct sockaddr *)&dst,
  206                     NULL));
  207                 if (error)
  208                         error = ENOENT;
  209                 return (error);
  210 
  211         default:
  212                 panic("%s: direction %d neither SND_IN nor SND_OUT.",
  213                      __func__, direction);
  214         }
  215 }
  216 
  217 /*
  218  * Receive a SeND message from user space to be either send out by the kernel
  219  * or, with SeND ICMPv6 options removed, to be further processed by the icmp6
  220  * input path.
  221  */
  222 static int
  223 send_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
  224     struct mbuf *control, struct thread *td)
  225 {
  226         struct sockaddr_send *sendsrc;
  227         struct ifnet *ifp;
  228         int error;
  229 
  230         KASSERT(V_send_so == so, ("%s: socket %p not send socket %p",
  231                 __func__, so, V_send_so));
  232 
  233         sendsrc = (struct sockaddr_send *)nam;
  234         if (sendsrc->send_family != AF_INET6) {
  235                 error = EAFNOSUPPORT;
  236                 goto err;
  237         }
  238         if (sendsrc->send_len != sizeof(*sendsrc)) {
  239                 error = EINVAL;
  240                 goto err;
  241         }
  242         ifp = ifnet_byindex_ref(sendsrc->send_ifidx);
  243         if (ifp == NULL) {
  244                 error = ENETUNREACH;
  245                 goto err;
  246         }
  247 
  248         error = send_output(m, ifp, sendsrc->send_direction);
  249         if_rele(ifp);
  250         m = NULL;
  251 
  252 err:
  253         if (control != NULL)
  254                 m_freem(control);
  255         if (m != NULL)
  256                 m_freem(m);
  257 
  258         return (error);
  259 }
  260 
  261 static void
  262 send_close(struct socket *so)
  263 {
  264 
  265         SEND_LOCK();
  266         if (V_send_so)
  267                 V_send_so = NULL;
  268         SEND_UNLOCK();
  269 }
  270 
  271 /*
  272  * Send a SeND message to user space, that was either received and has to be
  273  * validated or was about to be send out and has to be handled by the SEND
  274  * daemon adding SeND ICMPv6 options.
  275  */
  276 static int
  277 send_input(struct mbuf *m, struct ifnet *ifp, int direction, int msglen __unused)
  278 {
  279         struct ip6_hdr *ip6;
  280         struct sockaddr_send sendsrc;
  281 
  282         SEND_LOCK();
  283         if (V_send_so == NULL) {
  284                 SEND_UNLOCK();
  285                 return (-1);
  286         }
  287 
  288         /*
  289          * Make sure to clear any possible internally embedded scope before
  290          * passing the packet to user space for SeND cryptographic signature
  291          * validation to succeed.
  292          */
  293         ip6 = mtod(m, struct ip6_hdr *);
  294         in6_clearscope(&ip6->ip6_src);
  295         in6_clearscope(&ip6->ip6_dst);
  296 
  297         bzero(&sendsrc, sizeof(sendsrc));
  298         sendsrc.send_len = sizeof(sendsrc);
  299         sendsrc.send_family = AF_INET6;
  300         sendsrc.send_direction = direction;
  301         sendsrc.send_ifidx = ifp->if_index;
  302 
  303         /*
  304          * Send incoming or outgoing traffic to user space either to be
  305          * protected (outgoing) or validated (incoming) according to rfc3971.
  306          */
  307         SOCKBUF_LOCK(&V_send_so->so_rcv);
  308         if (sbappendaddr_locked(&V_send_so->so_rcv,
  309             (struct sockaddr *)&sendsrc, m, NULL) == 0) {
  310                 soroverflow_locked(V_send_so);
  311                 /* XXX stats. */
  312                 m_freem(m);
  313         } else {
  314                 sorwakeup_locked(V_send_so);
  315         }
  316 
  317         SEND_UNLOCK();
  318         return (0);
  319 }
  320 
  321 static struct protosw send_protosw = {
  322         .pr_type =              SOCK_RAW,
  323         .pr_flags =             PR_ATOMIC|PR_ADDR,
  324         .pr_protocol =          IPPROTO_SEND,
  325         .pr_attach =            send_attach,
  326         .pr_send =              send_send,
  327         .pr_detach =            send_close
  328 };
  329 
  330 static int
  331 send_modevent(module_t mod, int type, void *unused)
  332 {
  333 #ifdef __notyet__
  334         VNET_ITERATOR_DECL(vnet_iter);
  335 #endif
  336         int error;
  337 
  338         switch (type) {
  339         case MOD_LOAD:
  340                 SEND_LOCK_INIT();
  341 
  342                 error = protosw_register(&inet6domain, &send_protosw);
  343                 if (error != 0) {
  344                         printf("%s:%d: MOD_LOAD pf_proto_register(): %d\n",
  345                            __func__, __LINE__, error);
  346                         SEND_LOCK_DESTROY();
  347                         break;
  348                 }
  349                 send_sendso_input_hook = send_input;
  350                 break;
  351         case MOD_UNLOAD:
  352                 /* Do not allow unloading w/o locking. */
  353                 return (EBUSY);
  354 #ifdef __notyet__
  355                 VNET_LIST_RLOCK_NOSLEEP();
  356                 SEND_LOCK();
  357                 VNET_FOREACH(vnet_iter) {
  358                         CURVNET_SET(vnet_iter);
  359                         if (V_send_so != NULL) {
  360                                 CURVNET_RESTORE();
  361                                 SEND_UNLOCK();
  362                                 VNET_LIST_RUNLOCK_NOSLEEP();
  363                                 return (EBUSY);
  364                         }
  365                         CURVNET_RESTORE();
  366                 }
  367                 SEND_UNLOCK();
  368                 VNET_LIST_RUNLOCK_NOSLEEP();
  369                 error = protosw_unregister(&send_protosw);
  370                 if (error == 0)
  371                         SEND_LOCK_DESTROY();
  372                 send_sendso_input_hook = NULL;
  373                 break;
  374 #endif
  375         default:
  376                 error = 0;
  377                 break;
  378         }
  379 
  380         return (error);
  381 }
  382 
  383 static moduledata_t sendmod = {
  384         "send",
  385         send_modevent,
  386         0
  387 };
  388 
  389 DECLARE_MODULE(send, sendmod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);

Cache object: cfde393dcb6f70ee8c38575820212f3c


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