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/netinet/ip_output.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  * Copyright (c) 1982, 1986, 1988, 1990, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)ip_output.c 8.3 (Berkeley) 1/21/94
   30  * $FreeBSD: src/sys/netinet/ip_output.c,v 1.225.2.13 2006/01/18 23:36:49 emaste Exp $
   31  */
   32 
   33 #include "opt_ipfw.h"
   34 #include "opt_ipsec.h"
   35 #include "opt_mac.h"
   36 #include "opt_mbuf_stress_test.h"
   37 
   38 #include <sys/param.h>
   39 #include <sys/systm.h>
   40 #include <sys/kernel.h>
   41 #include <sys/mac.h>
   42 #include <sys/malloc.h>
   43 #include <sys/mbuf.h>
   44 #include <sys/protosw.h>
   45 #include <sys/socket.h>
   46 #include <sys/socketvar.h>
   47 #include <sys/sysctl.h>
   48 
   49 #include <net/if.h>
   50 #include <net/netisr.h>
   51 #include <net/pfil.h>
   52 #include <net/route.h>
   53 
   54 #include <netinet/in.h>
   55 #include <netinet/in_systm.h>
   56 #include <netinet/ip.h>
   57 #include <netinet/in_pcb.h>
   58 #include <netinet/in_var.h>
   59 #include <netinet/ip_var.h>
   60 
   61 #include <machine/in_cksum.h>
   62 
   63 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
   64 
   65 #ifdef IPSEC
   66 #include <netinet6/ipsec.h>
   67 #include <netkey/key.h>
   68 #ifdef IPSEC_DEBUG
   69 #include <netkey/key_debug.h>
   70 #else
   71 #define KEYDEBUG(lev,arg)
   72 #endif
   73 #endif /*IPSEC*/
   74 
   75 #ifdef FAST_IPSEC
   76 #include <netipsec/ipsec.h>
   77 #include <netipsec/xform.h>
   78 #include <netipsec/key.h>
   79 #endif /*FAST_IPSEC*/
   80 
   81 #define print_ip(x, a, y)        printf("%s %d.%d.%d.%d%s",\
   82                                 x, (ntohl(a.s_addr)>>24)&0xFF,\
   83                                   (ntohl(a.s_addr)>>16)&0xFF,\
   84                                   (ntohl(a.s_addr)>>8)&0xFF,\
   85                                   (ntohl(a.s_addr))&0xFF, y);
   86 
   87 u_short ip_id;
   88 
   89 #ifdef MBUF_STRESS_TEST
   90 int mbuf_frag_size = 0;
   91 SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
   92         &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
   93 #endif
   94 
   95 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
   96 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
   97 static void     ip_mloopback
   98         (struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
   99 static int      ip_getmoptions(struct inpcb *, struct sockopt *);
  100 static int      ip_pcbopts(struct inpcb *, int, struct mbuf *);
  101 static int      ip_setmoptions(struct inpcb *, struct sockopt *);
  102 static struct ip_moptions       *ip_findmoptions(struct inpcb *inp);
  103 
  104 int     ip_optcopy(struct ip *, struct ip *);
  105 
  106 
  107 extern  struct protosw inetsw[];
  108 
  109 /*
  110  * IP output.  The packet in mbuf chain m contains a skeletal IP
  111  * header (with len, off, ttl, proto, tos, src, dst).
  112  * The mbuf chain containing the packet will be freed.
  113  * The mbuf opt, if present, will not be freed.
  114  * In the IP forwarding case, the packet will arrive with options already
  115  * inserted, so must have a NULL opt pointer.
  116  */
  117 int
  118 ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro,
  119         int flags, struct ip_moptions *imo, struct inpcb *inp)
  120 {
  121         struct ip *ip;
  122         struct ifnet *ifp = NULL;       /* keep compiler happy */
  123         struct mbuf *m0;
  124         int hlen = sizeof (struct ip);
  125         int len, error = 0;
  126         struct sockaddr_in *dst = NULL; /* keep compiler happy */
  127         struct in_ifaddr *ia = NULL;
  128         int isbroadcast, sw_csum;
  129         struct route iproute;
  130         struct in_addr odst;
  131 #ifdef IPFIREWALL_FORWARD
  132         struct m_tag *fwd_tag = NULL;
  133 #endif
  134 #ifdef IPSEC
  135         struct secpolicy *sp = NULL;
  136 #endif
  137 #ifdef FAST_IPSEC
  138         struct secpolicy *sp = NULL;
  139         struct tdb_ident *tdbi;
  140         struct m_tag *mtag;
  141         int s;
  142 #endif /* FAST_IPSEC */
  143 
  144         M_ASSERTPKTHDR(m);
  145         
  146         if (ro == NULL) {
  147                 ro = &iproute;
  148                 bzero(ro, sizeof (*ro));
  149         }
  150 
  151         if (inp != NULL)
  152                 INP_LOCK_ASSERT(inp);
  153 
  154         if (opt) {
  155                 len = 0;
  156                 m = ip_insertoptions(m, opt, &len);
  157                 if (len != 0)
  158                         hlen = len;
  159         }
  160         ip = mtod(m, struct ip *);
  161 
  162         /*
  163          * Fill in IP header.  If we are not allowing fragmentation,
  164          * then the ip_id field is meaningless, but we don't set it
  165          * to zero.  Doing so causes various problems when devices along
  166          * the path (routers, load balancers, firewalls, etc.) illegally
  167          * disable DF on our packet.  Note that a 16-bit counter
  168          * will wrap around in less than 10 seconds at 100 Mbit/s on a
  169          * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
  170          * for Counting NATted Hosts", Proc. IMW'02, available at
  171          * <http://www.research.att.com/~smb/papers/fnat.pdf>.
  172          */
  173         if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
  174                 ip->ip_v = IPVERSION;
  175                 ip->ip_hl = hlen >> 2;
  176                 ip->ip_id = ip_newid();
  177                 ipstat.ips_localout++;
  178         } else {
  179                 hlen = ip->ip_hl << 2;
  180         }
  181 
  182         dst = (struct sockaddr_in *)&ro->ro_dst;
  183 again:
  184         /*
  185          * If there is a cached route,
  186          * check that it is to the same destination
  187          * and is still up.  If not, free it and try again.
  188          * The address family should also be checked in case of sharing the
  189          * cache with IPv6.
  190          */
  191         if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
  192                           dst->sin_family != AF_INET ||
  193                           dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
  194                 RTFREE(ro->ro_rt);
  195                 ro->ro_rt = (struct rtentry *)0;
  196         }
  197 #ifdef IPFIREWALL_FORWARD
  198         if (ro->ro_rt == NULL && fwd_tag == NULL) {
  199 #else
  200         if (ro->ro_rt == NULL) {
  201 #endif
  202                 bzero(dst, sizeof(*dst));
  203                 dst->sin_family = AF_INET;
  204                 dst->sin_len = sizeof(*dst);
  205                 dst->sin_addr = ip->ip_dst;
  206         }
  207         /*
  208          * If routing to interface only,
  209          * short circuit routing lookup.
  210          */
  211         if (flags & IP_ROUTETOIF) {
  212                 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
  213                     (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
  214                         ipstat.ips_noroute++;
  215                         error = ENETUNREACH;
  216                         goto bad;
  217                 }
  218                 ifp = ia->ia_ifp;
  219                 ip->ip_ttl = 1;
  220                 isbroadcast = in_broadcast(dst->sin_addr, ifp);
  221         } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
  222             imo != NULL && imo->imo_multicast_ifp != NULL) {
  223                 /*
  224                  * Bypass the normal routing lookup for multicast
  225                  * packets if the interface is specified.
  226                  */
  227                 ifp = imo->imo_multicast_ifp;
  228                 IFP_TO_IA(ifp, ia);
  229                 isbroadcast = 0;        /* fool gcc */
  230         } else {
  231                 /*
  232                  * We want to do any cloning requested by the link layer,
  233                  * as this is probably required in all cases for correct
  234                  * operation (as it is for ARP).
  235                  */
  236                 if (ro->ro_rt == NULL)
  237                         rtalloc_ign(ro, 0);
  238                 if (ro->ro_rt == NULL) {
  239                         ipstat.ips_noroute++;
  240                         error = EHOSTUNREACH;
  241                         goto bad;
  242                 }
  243                 ia = ifatoia(ro->ro_rt->rt_ifa);
  244                 ifp = ro->ro_rt->rt_ifp;
  245                 ro->ro_rt->rt_rmx.rmx_pksent++;
  246                 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
  247                         dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
  248                 if (ro->ro_rt->rt_flags & RTF_HOST)
  249                         isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
  250                 else
  251                         isbroadcast = in_broadcast(dst->sin_addr, ifp);
  252         }
  253         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
  254                 struct in_multi *inm;
  255 
  256                 m->m_flags |= M_MCAST;
  257                 /*
  258                  * IP destination address is multicast.  Make sure "dst"
  259                  * still points to the address in "ro".  (It may have been
  260                  * changed to point to a gateway address, above.)
  261                  */
  262                 dst = (struct sockaddr_in *)&ro->ro_dst;
  263                 /*
  264                  * See if the caller provided any multicast options
  265                  */
  266                 if (imo != NULL) {
  267                         ip->ip_ttl = imo->imo_multicast_ttl;
  268                         if (imo->imo_multicast_vif != -1)
  269                                 ip->ip_src.s_addr =
  270                                     ip_mcast_src ?
  271                                     ip_mcast_src(imo->imo_multicast_vif) :
  272                                     INADDR_ANY;
  273                 } else
  274                         ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
  275                 /*
  276                  * Confirm that the outgoing interface supports multicast.
  277                  */
  278                 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
  279                         if ((ifp->if_flags & IFF_MULTICAST) == 0) {
  280                                 ipstat.ips_noroute++;
  281                                 error = ENETUNREACH;
  282                                 goto bad;
  283                         }
  284                 }
  285                 /*
  286                  * If source address not specified yet, use address
  287                  * of outgoing interface.
  288                  */
  289                 if (ip->ip_src.s_addr == INADDR_ANY) {
  290                         /* Interface may have no addresses. */
  291                         if (ia != NULL)
  292                                 ip->ip_src = IA_SIN(ia)->sin_addr;
  293                 }
  294 
  295                 IN_MULTI_LOCK();
  296                 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
  297                 if (inm != NULL &&
  298                    (imo == NULL || imo->imo_multicast_loop)) {
  299                         IN_MULTI_UNLOCK();
  300                         /*
  301                          * If we belong to the destination multicast group
  302                          * on the outgoing interface, and the caller did not
  303                          * forbid loopback, loop back a copy.
  304                          */
  305                         ip_mloopback(ifp, m, dst, hlen);
  306                 }
  307                 else {
  308                         IN_MULTI_UNLOCK();
  309                         /*
  310                          * If we are acting as a multicast router, perform
  311                          * multicast forwarding as if the packet had just
  312                          * arrived on the interface to which we are about
  313                          * to send.  The multicast forwarding function
  314                          * recursively calls this function, using the
  315                          * IP_FORWARDING flag to prevent infinite recursion.
  316                          *
  317                          * Multicasts that are looped back by ip_mloopback(),
  318                          * above, will be forwarded by the ip_input() routine,
  319                          * if necessary.
  320                          */
  321                         if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
  322                                 /*
  323                                  * If rsvp daemon is not running, do not
  324                                  * set ip_moptions. This ensures that the packet
  325                                  * is multicast and not just sent down one link
  326                                  * as prescribed by rsvpd.
  327                                  */
  328                                 if (!rsvp_on)
  329                                         imo = NULL;
  330                                 if (ip_mforward &&
  331                                     ip_mforward(ip, ifp, m, imo) != 0) {
  332                                         m_freem(m);
  333                                         goto done;
  334                                 }
  335                         }
  336                 }
  337 
  338                 /*
  339                  * Multicasts with a time-to-live of zero may be looped-
  340                  * back, above, but must not be transmitted on a network.
  341                  * Also, multicasts addressed to the loopback interface
  342                  * are not sent -- the above call to ip_mloopback() will
  343                  * loop back a copy if this host actually belongs to the
  344                  * destination group on the loopback interface.
  345                  */
  346                 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
  347                         m_freem(m);
  348                         goto done;
  349                 }
  350 
  351                 goto sendit;
  352         }
  353 #ifndef notdef
  354         /*
  355          * If the source address is not specified yet, use the address
  356          * of the outoing interface.
  357          */
  358         if (ip->ip_src.s_addr == INADDR_ANY) {
  359                 /* Interface may have no addresses. */
  360                 if (ia != NULL) {
  361                         ip->ip_src = IA_SIN(ia)->sin_addr;
  362                 }
  363         }
  364 #endif /* notdef */
  365 #ifdef ALTQ
  366         /*
  367          * disable packet drop hack.
  368          * packetdrop should be done by queueing.
  369          */
  370 #else /* !ALTQ */
  371         /*
  372          * Verify that we have any chance at all of being able to queue
  373          *      the packet or packet fragments
  374          */
  375         if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
  376                 ifp->if_snd.ifq_maxlen) {
  377                         error = ENOBUFS;
  378                         ipstat.ips_odropped++;
  379                         ifp->if_snd.ifq_drops += (ip->ip_len / ifp->if_mtu + 1);
  380                         goto bad;
  381         }
  382 #endif /* !ALTQ */
  383 
  384         /*
  385          * Look for broadcast address and
  386          * verify user is allowed to send
  387          * such a packet.
  388          */
  389         if (isbroadcast) {
  390                 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
  391                         error = EADDRNOTAVAIL;
  392                         goto bad;
  393                 }
  394                 if ((flags & IP_ALLOWBROADCAST) == 0) {
  395                         error = EACCES;
  396                         goto bad;
  397                 }
  398                 /* don't allow broadcast messages to be fragmented */
  399                 if (ip->ip_len > ifp->if_mtu) {
  400                         error = EMSGSIZE;
  401                         goto bad;
  402                 }
  403                 if (flags & IP_SENDONES)
  404                         ip->ip_dst.s_addr = INADDR_BROADCAST;
  405                 m->m_flags |= M_BCAST;
  406         } else {
  407                 m->m_flags &= ~M_BCAST;
  408         }
  409 
  410 sendit:
  411 #ifdef IPSEC
  412         /* get SP for this packet */
  413         if (inp == NULL)
  414                 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
  415                     flags, &error);
  416         else
  417                 sp = ipsec4_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error);
  418 
  419         if (sp == NULL) {
  420                 ipsecstat.out_inval++;
  421                 goto bad;
  422         }
  423 
  424         error = 0;
  425 
  426         /* check policy */
  427         switch (sp->policy) {
  428         case IPSEC_POLICY_DISCARD:
  429                 /*
  430                  * This packet is just discarded.
  431                  */
  432                 ipsecstat.out_polvio++;
  433                 goto bad;
  434 
  435         case IPSEC_POLICY_BYPASS:
  436         case IPSEC_POLICY_NONE:
  437         case IPSEC_POLICY_TCP:
  438                 /* no need to do IPsec. */
  439                 goto skip_ipsec;
  440         
  441         case IPSEC_POLICY_IPSEC:
  442                 if (sp->req == NULL) {
  443                         /* acquire a policy */
  444                         error = key_spdacquire(sp);
  445                         goto bad;
  446                 }
  447                 break;
  448 
  449         case IPSEC_POLICY_ENTRUST:
  450         default:
  451                 printf("ip_output: Invalid policy found. %d\n", sp->policy);
  452         }
  453     {
  454         struct ipsec_output_state state;
  455         bzero(&state, sizeof(state));
  456         state.m = m;
  457         if (flags & IP_ROUTETOIF) {
  458                 state.ro = &iproute;
  459                 bzero(&iproute, sizeof(iproute));
  460         } else
  461                 state.ro = ro;
  462         state.dst = (struct sockaddr *)dst;
  463 
  464         ip->ip_sum = 0;
  465 
  466         /*
  467          * XXX
  468          * delayed checksums are not currently compatible with IPsec
  469          */
  470         if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
  471                 in_delayed_cksum(m);
  472                 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
  473         }
  474 
  475         ip->ip_len = htons(ip->ip_len);
  476         ip->ip_off = htons(ip->ip_off);
  477 
  478         error = ipsec4_output(&state, sp, flags);
  479 
  480         m = state.m;
  481         if (flags & IP_ROUTETOIF) {
  482                 /*
  483                  * if we have tunnel mode SA, we may need to ignore
  484                  * IP_ROUTETOIF.
  485                  */
  486                 if (state.ro != &iproute || state.ro->ro_rt != NULL) {
  487                         flags &= ~IP_ROUTETOIF;
  488                         ro = state.ro;
  489                 }
  490         } else
  491                 ro = state.ro;
  492         dst = (struct sockaddr_in *)state.dst;
  493         if (error) {
  494                 /* mbuf is already reclaimed in ipsec4_output. */
  495                 m = NULL;
  496                 switch (error) {
  497                 case EHOSTUNREACH:
  498                 case ENETUNREACH:
  499                 case EMSGSIZE:
  500                 case ENOBUFS:
  501                 case ENOMEM:
  502                         break;
  503                 default:
  504                         printf("ip4_output (ipsec): error code %d\n", error);
  505                         /*fall through*/
  506                 case ENOENT:
  507                         /* don't show these error codes to the user */
  508                         error = 0;
  509                         break;
  510                 }
  511                 goto bad;
  512         }
  513 
  514         /* be sure to update variables that are affected by ipsec4_output() */
  515         ip = mtod(m, struct ip *);
  516         hlen = ip->ip_hl << 2;
  517         if (ro->ro_rt == NULL) {
  518                 if ((flags & IP_ROUTETOIF) == 0) {
  519                         printf("ip_output: "
  520                                 "can't update route after IPsec processing\n");
  521                         error = EHOSTUNREACH;   /*XXX*/
  522                         goto bad;
  523                 }
  524         } else {
  525                 if (state.encap) {
  526                         ia = ifatoia(ro->ro_rt->rt_ifa);
  527                         ifp = ro->ro_rt->rt_ifp;
  528                 }
  529         }
  530     }
  531 
  532         /* make it flipped, again. */
  533         ip->ip_len = ntohs(ip->ip_len);
  534         ip->ip_off = ntohs(ip->ip_off);
  535 skip_ipsec:
  536 #endif /*IPSEC*/
  537 #ifdef FAST_IPSEC
  538         /*
  539          * Check the security policy (SP) for the packet and, if
  540          * required, do IPsec-related processing.  There are two
  541          * cases here; the first time a packet is sent through
  542          * it will be untagged and handled by ipsec4_checkpolicy.
  543          * If the packet is resubmitted to ip_output (e.g. after
  544          * AH, ESP, etc. processing), there will be a tag to bypass
  545          * the lookup and related policy checking.
  546          */
  547         mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
  548         s = splnet();
  549         if (mtag != NULL) {
  550                 tdbi = (struct tdb_ident *)(mtag + 1);
  551                 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
  552                 if (sp == NULL)
  553                         error = -EINVAL;        /* force silent drop */
  554                 m_tag_delete(m, mtag);
  555         } else {
  556                 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
  557                                         &error, inp);
  558         }
  559         /*
  560          * There are four return cases:
  561          *    sp != NULL                    apply IPsec policy
  562          *    sp == NULL, error == 0        no IPsec handling needed
  563          *    sp == NULL, error == -EINVAL  discard packet w/o error
  564          *    sp == NULL, error != 0        discard packet, report error
  565          */
  566         if (sp != NULL) {
  567                 /* Loop detection, check if ipsec processing already done */
  568                 KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
  569                 for (mtag = m_tag_first(m); mtag != NULL;
  570                      mtag = m_tag_next(m, mtag)) {
  571                         if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
  572                                 continue;
  573                         if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
  574                             mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
  575                                 continue;
  576                         /*
  577                          * Check if policy has an SA associated with it.
  578                          * This can happen when an SP has yet to acquire
  579                          * an SA; e.g. on first reference.  If it occurs,
  580                          * then we let ipsec4_process_packet do its thing.
  581                          */
  582                         if (sp->req->sav == NULL)
  583                                 break;
  584                         tdbi = (struct tdb_ident *)(mtag + 1);
  585                         if (tdbi->spi == sp->req->sav->spi &&
  586                             tdbi->proto == sp->req->sav->sah->saidx.proto &&
  587                             bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
  588                                  sizeof (union sockaddr_union)) == 0) {
  589                                 /*
  590                                  * No IPsec processing is needed, free
  591                                  * reference to SP.
  592                                  *
  593                                  * NB: null pointer to avoid free at
  594                                  *     done: below.
  595                                  */
  596                                 KEY_FREESP(&sp), sp = NULL;
  597                                 splx(s);
  598                                 goto spd_done;
  599                         }
  600                 }
  601 
  602                 /*
  603                  * Do delayed checksums now because we send before
  604                  * this is done in the normal processing path.
  605                  */
  606                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
  607                         in_delayed_cksum(m);
  608                         m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
  609                 }
  610 
  611                 ip->ip_len = htons(ip->ip_len);
  612                 ip->ip_off = htons(ip->ip_off);
  613 
  614                 /* NB: callee frees mbuf */
  615                 error = ipsec4_process_packet(m, sp->req, flags, 0);
  616                 /*
  617                  * Preserve KAME behaviour: ENOENT can be returned
  618                  * when an SA acquire is in progress.  Don't propagate
  619                  * this to user-level; it confuses applications.
  620                  *
  621                  * XXX this will go away when the SADB is redone.
  622                  */
  623                 if (error == ENOENT)
  624                         error = 0;
  625                 splx(s);
  626                 goto done;
  627         } else {
  628                 splx(s);
  629 
  630                 if (error != 0) {
  631                         /*
  632                          * Hack: -EINVAL is used to signal that a packet
  633                          * should be silently discarded.  This is typically
  634                          * because we asked key management for an SA and
  635                          * it was delayed (e.g. kicked up to IKE).
  636                          */
  637                         if (error == -EINVAL)
  638                                 error = 0;
  639                         goto bad;
  640                 } else {
  641                         /* No IPsec processing for this packet. */
  642                 }
  643 #ifdef notyet
  644                 /*
  645                  * If deferred crypto processing is needed, check that
  646                  * the interface supports it.
  647                  */ 
  648                 mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
  649                 if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) {
  650                         /* notify IPsec to do its own crypto */
  651                         ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
  652                         error = EHOSTUNREACH;
  653                         goto bad;
  654                 }
  655 #endif
  656         }
  657 spd_done:
  658 #endif /* FAST_IPSEC */
  659 
  660         /* Jump over all PFIL processing if hooks are not active. */
  661         if (inet_pfil_hook.ph_busy_count == -1)
  662                 goto passout;
  663 
  664         /* Run through list of hooks for output packets. */
  665         odst.s_addr = ip->ip_dst.s_addr;
  666         error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
  667         if (error != 0 || m == NULL)
  668                 goto done;
  669 
  670         ip = mtod(m, struct ip *);
  671 
  672         /* See if destination IP address was changed by packet filter. */
  673         if (odst.s_addr != ip->ip_dst.s_addr) {
  674                 m->m_flags |= M_SKIP_FIREWALL;
  675                 if (in_localip(ip->ip_dst)) {
  676                         m->m_flags |= M_FASTFWD_OURS;
  677                         if (m->m_pkthdr.rcvif == NULL)
  678                                 m->m_pkthdr.rcvif = loif;
  679                         if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
  680                                 m->m_pkthdr.csum_flags |=
  681                                     CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
  682                                 m->m_pkthdr.csum_data = 0xffff;
  683                         }
  684                         m->m_pkthdr.csum_flags |=
  685                             CSUM_IP_CHECKED | CSUM_IP_VALID;
  686 
  687                         error = netisr_queue(NETISR_IP, m);
  688                         goto done;
  689                 } else
  690                         goto again;
  691         }
  692 
  693 #ifdef IPFIREWALL_FORWARD
  694         /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
  695         if (m->m_flags & M_FASTFWD_OURS) {
  696                 if (m->m_pkthdr.rcvif == NULL)
  697                         m->m_pkthdr.rcvif = loif;
  698                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
  699                         m->m_pkthdr.csum_flags |=
  700                             CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
  701                         m->m_pkthdr.csum_data = 0xffff;
  702                 }
  703                 m->m_pkthdr.csum_flags |=
  704                             CSUM_IP_CHECKED | CSUM_IP_VALID;
  705 
  706                 error = netisr_queue(NETISR_IP, m);
  707                 goto done;
  708         }
  709         /* Or forward to some other address? */
  710         fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
  711         if (fwd_tag) {
  712 #ifndef IPFIREWALL_FORWARD_EXTENDED
  713                 if (!in_localip(ip->ip_src) && !in_localaddr(ip->ip_dst)) {
  714 #endif
  715                         dst = (struct sockaddr_in *)&ro->ro_dst;
  716                         bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
  717                         m->m_flags |= M_SKIP_FIREWALL;
  718                         m_tag_delete(m, fwd_tag);
  719                         goto again;
  720 #ifndef IPFIREWALL_FORWARD_EXTENDED
  721                 } else {
  722                         m_tag_delete(m, fwd_tag);
  723                         /* Continue. */
  724                 }
  725 #endif
  726         }
  727 #endif /* IPFIREWALL_FORWARD */
  728 
  729 passout:
  730         /* 127/8 must not appear on wire - RFC1122. */
  731         if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
  732             (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
  733                 if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
  734                         ipstat.ips_badaddr++;
  735                         error = EADDRNOTAVAIL;
  736                         goto bad;
  737                 }
  738         }
  739 
  740         m->m_pkthdr.csum_flags |= CSUM_IP;
  741         sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
  742         if (sw_csum & CSUM_DELAY_DATA) {
  743                 in_delayed_cksum(m);
  744                 sw_csum &= ~CSUM_DELAY_DATA;
  745         }
  746         m->m_pkthdr.csum_flags &= ifp->if_hwassist;
  747 
  748         /*
  749          * If small enough for interface, or the interface will take
  750          * care of the fragmentation for us, can just send directly.
  751          */
  752         if (ip->ip_len <= ifp->if_mtu || (ifp->if_hwassist & CSUM_FRAGMENT &&
  753             ((ip->ip_off & IP_DF) == 0))) {
  754                 ip->ip_len = htons(ip->ip_len);
  755                 ip->ip_off = htons(ip->ip_off);
  756                 ip->ip_sum = 0;
  757                 if (sw_csum & CSUM_DELAY_IP)
  758                         ip->ip_sum = in_cksum(m, hlen);
  759 
  760                 /* Record statistics for this interface address. */
  761                 if (!(flags & IP_FORWARDING) && ia) {
  762                         ia->ia_ifa.if_opackets++;
  763                         ia->ia_ifa.if_obytes += m->m_pkthdr.len;
  764                 }
  765 
  766 #ifdef IPSEC
  767                 /* clean ipsec history once it goes out of the node */
  768                 ipsec_delaux(m);
  769 #endif
  770 
  771 #ifdef MBUF_STRESS_TEST
  772                 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
  773                         m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
  774 #endif
  775                 error = (*ifp->if_output)(ifp, m,
  776                                 (struct sockaddr *)dst, ro->ro_rt);
  777                 goto done;
  778         }
  779 
  780         if (ip->ip_off & IP_DF) {
  781                 error = EMSGSIZE;
  782                 /*
  783                  * This case can happen if the user changed the MTU
  784                  * of an interface after enabling IP on it.  Because
  785                  * most netifs don't keep track of routes pointing to
  786                  * them, there is no way for one to update all its
  787                  * routes when the MTU is changed.
  788                  */
  789                 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
  790                     (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
  791                         ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
  792                 }
  793                 ipstat.ips_cantfrag++;
  794                 goto bad;
  795         }
  796 
  797         /*
  798          * Too large for interface; fragment if possible. If successful,
  799          * on return, m will point to a list of packets to be sent.
  800          */
  801         error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
  802         if (error)
  803                 goto bad;
  804         for (; m; m = m0) {
  805                 m0 = m->m_nextpkt;
  806                 m->m_nextpkt = 0;
  807 #ifdef IPSEC
  808                 /* clean ipsec history once it goes out of the node */
  809                 ipsec_delaux(m);
  810 #endif
  811                 if (error == 0) {
  812                         /* Record statistics for this interface address. */
  813                         if (ia != NULL) {
  814                                 ia->ia_ifa.if_opackets++;
  815                                 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
  816                         }
  817                         
  818                         error = (*ifp->if_output)(ifp, m,
  819                             (struct sockaddr *)dst, ro->ro_rt);
  820                 } else
  821                         m_freem(m);
  822         }
  823 
  824         if (error == 0)
  825                 ipstat.ips_fragmented++;
  826 
  827 done:
  828         if (ro == &iproute && ro->ro_rt) {
  829                 RTFREE(ro->ro_rt);
  830         }
  831 #ifdef IPSEC
  832         if (sp != NULL) {
  833                 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
  834                         printf("DP ip_output call free SP:%p\n", sp));
  835                 key_freesp(sp);
  836         }
  837 #endif
  838 #ifdef FAST_IPSEC
  839         if (sp != NULL)
  840                 KEY_FREESP(&sp);
  841 #endif
  842         return (error);
  843 bad:
  844         m_freem(m);
  845         goto done;
  846 }
  847 
  848 /*
  849  * Create a chain of fragments which fit the given mtu. m_frag points to the
  850  * mbuf to be fragmented; on return it points to the chain with the fragments.
  851  * Return 0 if no error. If error, m_frag may contain a partially built
  852  * chain of fragments that should be freed by the caller.
  853  *
  854  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
  855  * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
  856  */
  857 int
  858 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
  859             u_long if_hwassist_flags, int sw_csum)
  860 {
  861         int error = 0;
  862         int hlen = ip->ip_hl << 2;
  863         int len = (mtu - hlen) & ~7;    /* size of payload in each fragment */
  864         int off;
  865         struct mbuf *m0 = *m_frag;      /* the original packet          */
  866         int firstlen;
  867         struct mbuf **mnext;
  868         int nfrags;
  869 
  870         if (ip->ip_off & IP_DF) {       /* Fragmentation not allowed */
  871                 ipstat.ips_cantfrag++;
  872                 return EMSGSIZE;
  873         }
  874 
  875         /*
  876          * Must be able to put at least 8 bytes per fragment.
  877          */
  878         if (len < 8)
  879                 return EMSGSIZE;
  880 
  881         /*
  882          * If the interface will not calculate checksums on
  883          * fragmented packets, then do it here.
  884          */
  885         if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
  886             (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
  887                 in_delayed_cksum(m0);
  888                 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
  889         }
  890 
  891         if (len > PAGE_SIZE) {
  892                 /* 
  893                  * Fragment large datagrams such that each segment 
  894                  * contains a multiple of PAGE_SIZE amount of data, 
  895                  * plus headers. This enables a receiver to perform 
  896                  * page-flipping zero-copy optimizations.
  897                  *
  898                  * XXX When does this help given that sender and receiver
  899                  * could have different page sizes, and also mtu could
  900                  * be less than the receiver's page size ?
  901                  */
  902                 int newlen;
  903                 struct mbuf *m;
  904 
  905                 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
  906                         off += m->m_len;
  907 
  908                 /*
  909                  * firstlen (off - hlen) must be aligned on an 
  910                  * 8-byte boundary
  911                  */
  912                 if (off < hlen)
  913                         goto smart_frag_failure;
  914                 off = ((off - hlen) & ~7) + hlen;
  915                 newlen = (~PAGE_MASK) & mtu;
  916                 if ((newlen + sizeof (struct ip)) > mtu) {
  917                         /* we failed, go back the default */
  918 smart_frag_failure:
  919                         newlen = len;
  920                         off = hlen + len;
  921                 }
  922                 len = newlen;
  923 
  924         } else {
  925                 off = hlen + len;
  926         }
  927 
  928         firstlen = off - hlen;
  929         mnext = &m0->m_nextpkt;         /* pointer to next packet */
  930 
  931         /*
  932          * Loop through length of segment after first fragment,
  933          * make new header and copy data of each part and link onto chain.
  934          * Here, m0 is the original packet, m is the fragment being created.
  935          * The fragments are linked off the m_nextpkt of the original
  936          * packet, which after processing serves as the first fragment.
  937          */
  938         for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
  939                 struct ip *mhip;        /* ip header on the fragment */
  940                 struct mbuf *m;
  941                 int mhlen = sizeof (struct ip);
  942 
  943                 MGETHDR(m, M_DONTWAIT, MT_HEADER);
  944                 if (m == NULL) {
  945                         error = ENOBUFS;
  946                         ipstat.ips_odropped++;
  947                         goto done;
  948                 }
  949                 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
  950                 /*
  951                  * In the first mbuf, leave room for the link header, then
  952                  * copy the original IP header including options. The payload
  953                  * goes into an additional mbuf chain returned by m_copy().
  954                  */
  955                 m->m_data += max_linkhdr;
  956                 mhip = mtod(m, struct ip *);
  957                 *mhip = *ip;
  958                 if (hlen > sizeof (struct ip)) {
  959                         mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
  960                         mhip->ip_v = IPVERSION;
  961                         mhip->ip_hl = mhlen >> 2;
  962                 }
  963                 m->m_len = mhlen;
  964                 /* XXX do we need to add ip->ip_off below ? */
  965                 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
  966                 if (off + len >= ip->ip_len) {  /* last fragment */
  967                         len = ip->ip_len - off;
  968                         m->m_flags |= M_LASTFRAG;
  969                 } else
  970                         mhip->ip_off |= IP_MF;
  971                 mhip->ip_len = htons((u_short)(len + mhlen));
  972                 m->m_next = m_copy(m0, off, len);
  973                 if (m->m_next == NULL) {        /* copy failed */
  974                         m_free(m);
  975                         error = ENOBUFS;        /* ??? */
  976                         ipstat.ips_odropped++;
  977                         goto done;
  978                 }
  979                 m->m_pkthdr.len = mhlen + len;
  980                 m->m_pkthdr.rcvif = (struct ifnet *)0;
  981 #ifdef MAC
  982                 mac_create_fragment(m0, m);
  983 #endif
  984                 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
  985                 mhip->ip_off = htons(mhip->ip_off);
  986                 mhip->ip_sum = 0;
  987                 if (sw_csum & CSUM_DELAY_IP)
  988                         mhip->ip_sum = in_cksum(m, mhlen);
  989                 *mnext = m;
  990                 mnext = &m->m_nextpkt;
  991         }
  992         ipstat.ips_ofragments += nfrags;
  993 
  994         /* set first marker for fragment chain */
  995         m0->m_flags |= M_FIRSTFRAG | M_FRAG;
  996         m0->m_pkthdr.csum_data = nfrags;
  997 
  998         /*
  999          * Update first fragment by trimming what's been copied out
 1000          * and updating header.
 1001          */
 1002         m_adj(m0, hlen + firstlen - ip->ip_len);
 1003         m0->m_pkthdr.len = hlen + firstlen;
 1004         ip->ip_len = htons((u_short)m0->m_pkthdr.len);
 1005         ip->ip_off |= IP_MF;
 1006         ip->ip_off = htons(ip->ip_off);
 1007         ip->ip_sum = 0;
 1008         if (sw_csum & CSUM_DELAY_IP)
 1009                 ip->ip_sum = in_cksum(m0, hlen);
 1010 
 1011 done:
 1012         *m_frag = m0;
 1013         return error;
 1014 }
 1015 
 1016 void
 1017 in_delayed_cksum(struct mbuf *m)
 1018 {
 1019         struct ip *ip;
 1020         u_short csum, offset;
 1021 
 1022         ip = mtod(m, struct ip *);
 1023         offset = ip->ip_hl << 2 ;
 1024         csum = in_cksum_skip(m, ip->ip_len, offset);
 1025         if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
 1026                 csum = 0xffff;
 1027         offset += m->m_pkthdr.csum_data;        /* checksum offset */
 1028 
 1029         if (offset + sizeof(u_short) > m->m_len) {
 1030                 printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
 1031                     m->m_len, offset, ip->ip_p);
 1032                 /*
 1033                  * XXX
 1034                  * this shouldn't happen, but if it does, the
 1035                  * correct behavior may be to insert the checksum
 1036                  * in the existing chain instead of rearranging it.
 1037                  */
 1038                 m = m_pullup(m, offset + sizeof(u_short));
 1039         }
 1040         *(u_short *)(m->m_data + offset) = csum;
 1041 }
 1042 
 1043 /*
 1044  * Insert IP options into preformed packet.
 1045  * Adjust IP destination as required for IP source routing,
 1046  * as indicated by a non-zero in_addr at the start of the options.
 1047  *
 1048  * XXX This routine assumes that the packet has no options in place.
 1049  */
 1050 static struct mbuf *
 1051 ip_insertoptions(m, opt, phlen)
 1052         register struct mbuf *m;
 1053         struct mbuf *opt;
 1054         int *phlen;
 1055 {
 1056         register struct ipoption *p = mtod(opt, struct ipoption *);
 1057         struct mbuf *n;
 1058         register struct ip *ip = mtod(m, struct ip *);
 1059         unsigned optlen;
 1060 
 1061         optlen = opt->m_len - sizeof(p->ipopt_dst);
 1062         if (optlen + ip->ip_len > IP_MAXPACKET) {
 1063                 *phlen = 0;
 1064                 return (m);             /* XXX should fail */
 1065         }
 1066         if (p->ipopt_dst.s_addr)
 1067                 ip->ip_dst = p->ipopt_dst;
 1068         if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
 1069                 MGETHDR(n, M_DONTWAIT, MT_HEADER);
 1070                 if (n == NULL) {
 1071                         *phlen = 0;
 1072                         return (m);
 1073                 }
 1074                 M_MOVE_PKTHDR(n, m);
 1075                 n->m_pkthdr.rcvif = (struct ifnet *)0;
 1076 #ifdef MAC
 1077                 mac_create_mbuf_from_mbuf(m, n);
 1078 #endif
 1079                 n->m_pkthdr.len += optlen;
 1080                 m->m_len -= sizeof(struct ip);
 1081                 m->m_data += sizeof(struct ip);
 1082                 n->m_next = m;
 1083                 m = n;
 1084                 m->m_len = optlen + sizeof(struct ip);
 1085                 m->m_data += max_linkhdr;
 1086                 bcopy(ip, mtod(m, void *), sizeof(struct ip));
 1087         } else {
 1088                 m->m_data -= optlen;
 1089                 m->m_len += optlen;
 1090                 m->m_pkthdr.len += optlen;
 1091                 bcopy(ip, mtod(m, void *), sizeof(struct ip));
 1092         }
 1093         ip = mtod(m, struct ip *);
 1094         bcopy(p->ipopt_list, ip + 1, optlen);
 1095         *phlen = sizeof(struct ip) + optlen;
 1096         ip->ip_v = IPVERSION;
 1097         ip->ip_hl = *phlen >> 2;
 1098         ip->ip_len += optlen;
 1099         return (m);
 1100 }
 1101 
 1102 /*
 1103  * Copy options from ip to jp,
 1104  * omitting those not copied during fragmentation.
 1105  */
 1106 int
 1107 ip_optcopy(ip, jp)
 1108         struct ip *ip, *jp;
 1109 {
 1110         register u_char *cp, *dp;
 1111         int opt, optlen, cnt;
 1112 
 1113         cp = (u_char *)(ip + 1);
 1114         dp = (u_char *)(jp + 1);
 1115         cnt = (ip->ip_hl << 2) - sizeof (struct ip);
 1116         for (; cnt > 0; cnt -= optlen, cp += optlen) {
 1117                 opt = cp[0];
 1118                 if (opt == IPOPT_EOL)
 1119                         break;
 1120                 if (opt == IPOPT_NOP) {
 1121                         /* Preserve for IP mcast tunnel's LSRR alignment. */
 1122                         *dp++ = IPOPT_NOP;
 1123                         optlen = 1;
 1124                         continue;
 1125                 }
 1126 
 1127                 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
 1128                     ("ip_optcopy: malformed ipv4 option"));
 1129                 optlen = cp[IPOPT_OLEN];
 1130                 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
 1131                     ("ip_optcopy: malformed ipv4 option"));
 1132 
 1133                 /* bogus lengths should have been caught by ip_dooptions */
 1134                 if (optlen > cnt)
 1135                         optlen = cnt;
 1136                 if (IPOPT_COPIED(opt)) {
 1137                         bcopy(cp, dp, optlen);
 1138                         dp += optlen;
 1139                 }
 1140         }
 1141         for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
 1142                 *dp++ = IPOPT_EOL;
 1143         return (optlen);
 1144 }
 1145 
 1146 /*
 1147  * IP socket option processing.
 1148  */
 1149 int
 1150 ip_ctloutput(so, sopt)
 1151         struct socket *so;
 1152         struct sockopt *sopt;
 1153 {
 1154         struct  inpcb *inp = sotoinpcb(so);
 1155         int     error, optval;
 1156 
 1157         error = optval = 0;
 1158         if (sopt->sopt_level != IPPROTO_IP) {
 1159                 return (EINVAL);
 1160         }
 1161 
 1162         switch (sopt->sopt_dir) {
 1163         case SOPT_SET:
 1164                 switch (sopt->sopt_name) {
 1165                 case IP_OPTIONS:
 1166 #ifdef notyet
 1167                 case IP_RETOPTS:
 1168 #endif
 1169                 {
 1170                         struct mbuf *m;
 1171                         if (sopt->sopt_valsize > MLEN) {
 1172                                 error = EMSGSIZE;
 1173                                 break;
 1174                         }
 1175                         MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
 1176                         if (m == NULL) {
 1177                                 error = ENOBUFS;
 1178                                 break;
 1179                         }
 1180                         m->m_len = sopt->sopt_valsize;
 1181                         error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
 1182                                             m->m_len);
 1183                         INP_LOCK(inp);
 1184                         error = ip_pcbopts(inp, sopt->sopt_name, m);
 1185                         INP_UNLOCK(inp);
 1186                         return (error);
 1187                 }
 1188 
 1189                 case IP_TOS:
 1190                 case IP_TTL:
 1191                 case IP_RECVOPTS:
 1192                 case IP_RECVRETOPTS:
 1193                 case IP_RECVDSTADDR:
 1194                 case IP_RECVTTL:
 1195                 case IP_RECVIF:
 1196                 case IP_FAITH:
 1197                 case IP_ONESBCAST:
 1198                         error = sooptcopyin(sopt, &optval, sizeof optval,
 1199                                             sizeof optval);
 1200                         if (error)
 1201                                 break;
 1202 
 1203                         switch (sopt->sopt_name) {
 1204                         case IP_TOS:
 1205                                 inp->inp_ip_tos = optval;
 1206                                 break;
 1207 
 1208                         case IP_TTL:
 1209                                 inp->inp_ip_ttl = optval;
 1210                                 break;
 1211 #define OPTSET(bit) do {                                                \
 1212         INP_LOCK(inp);                                                  \
 1213         if (optval)                                                     \
 1214                 inp->inp_flags |= bit;                                  \
 1215         else                                                            \
 1216                 inp->inp_flags &= ~bit;                                 \
 1217         INP_UNLOCK(inp);                                                \
 1218 } while (0)
 1219 
 1220                         case IP_RECVOPTS:
 1221                                 OPTSET(INP_RECVOPTS);
 1222                                 break;
 1223 
 1224                         case IP_RECVRETOPTS:
 1225                                 OPTSET(INP_RECVRETOPTS);
 1226                                 break;
 1227 
 1228                         case IP_RECVDSTADDR:
 1229                                 OPTSET(INP_RECVDSTADDR);
 1230                                 break;
 1231 
 1232                         case IP_RECVTTL:
 1233                                 OPTSET(INP_RECVTTL);
 1234                                 break;
 1235 
 1236                         case IP_RECVIF:
 1237                                 OPTSET(INP_RECVIF);
 1238                                 break;
 1239 
 1240                         case IP_FAITH:
 1241                                 OPTSET(INP_FAITH);
 1242                                 break;
 1243 
 1244                         case IP_ONESBCAST:
 1245                                 OPTSET(INP_ONESBCAST);
 1246                                 break;
 1247                         }
 1248                         break;
 1249 #undef OPTSET
 1250 
 1251                 case IP_MULTICAST_IF:
 1252                 case IP_MULTICAST_VIF:
 1253                 case IP_MULTICAST_TTL:
 1254                 case IP_MULTICAST_LOOP:
 1255                 case IP_ADD_MEMBERSHIP:
 1256                 case IP_DROP_MEMBERSHIP:
 1257                         error = ip_setmoptions(inp, sopt);
 1258                         break;
 1259 
 1260                 case IP_PORTRANGE:
 1261                         error = sooptcopyin(sopt, &optval, sizeof optval,
 1262                                             sizeof optval);
 1263                         if (error)
 1264                                 break;
 1265 
 1266                         INP_LOCK(inp);
 1267                         switch (optval) {
 1268                         case IP_PORTRANGE_DEFAULT:
 1269                                 inp->inp_flags &= ~(INP_LOWPORT);
 1270                                 inp->inp_flags &= ~(INP_HIGHPORT);
 1271                                 break;
 1272 
 1273                         case IP_PORTRANGE_HIGH:
 1274                                 inp->inp_flags &= ~(INP_LOWPORT);
 1275                                 inp->inp_flags |= INP_HIGHPORT;
 1276                                 break;
 1277 
 1278                         case IP_PORTRANGE_LOW:
 1279                                 inp->inp_flags &= ~(INP_HIGHPORT);
 1280                                 inp->inp_flags |= INP_LOWPORT;
 1281                                 break;
 1282 
 1283                         default:
 1284                                 error = EINVAL;
 1285                                 break;
 1286                         }
 1287                         INP_UNLOCK(inp);
 1288                         break;
 1289 
 1290 #if defined(IPSEC) || defined(FAST_IPSEC)
 1291                 case IP_IPSEC_POLICY:
 1292                 {
 1293                         caddr_t req;
 1294                         size_t len = 0;
 1295                         int priv;
 1296                         struct mbuf *m;
 1297                         int optname;
 1298 
 1299                         if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
 1300                                 break;
 1301                         if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
 1302                                 break;
 1303                         priv = (sopt->sopt_td != NULL &&
 1304                                 suser(sopt->sopt_td) != 0) ? 0 : 1;
 1305                         req = mtod(m, caddr_t);
 1306                         len = m->m_len;
 1307                         optname = sopt->sopt_name;
 1308                         error = ipsec4_set_policy(inp, optname, req, len, priv);
 1309                         m_freem(m);
 1310                         break;
 1311                 }
 1312 #endif /*IPSEC*/
 1313 
 1314                 default:
 1315                         error = ENOPROTOOPT;
 1316                         break;
 1317                 }
 1318                 break;
 1319 
 1320         case SOPT_GET:
 1321                 switch (sopt->sopt_name) {
 1322                 case IP_OPTIONS:
 1323                 case IP_RETOPTS:
 1324                         if (inp->inp_options)
 1325                                 error = sooptcopyout(sopt, 
 1326                                                      mtod(inp->inp_options,
 1327                                                           char *),
 1328                                                      inp->inp_options->m_len);
 1329                         else
 1330                                 sopt->sopt_valsize = 0;
 1331                         break;
 1332 
 1333                 case IP_TOS:
 1334                 case IP_TTL:
 1335                 case IP_RECVOPTS:
 1336                 case IP_RECVRETOPTS:
 1337                 case IP_RECVDSTADDR:
 1338                 case IP_RECVTTL:
 1339                 case IP_RECVIF:
 1340                 case IP_PORTRANGE:
 1341                 case IP_FAITH:
 1342                 case IP_ONESBCAST:
 1343                         switch (sopt->sopt_name) {
 1344 
 1345                         case IP_TOS:
 1346                                 optval = inp->inp_ip_tos;
 1347                                 break;
 1348 
 1349                         case IP_TTL:
 1350                                 optval = inp->inp_ip_ttl;
 1351                                 break;
 1352 
 1353 #define OPTBIT(bit)     (inp->inp_flags & bit ? 1 : 0)
 1354 
 1355                         case IP_RECVOPTS:
 1356                                 optval = OPTBIT(INP_RECVOPTS);
 1357                                 break;
 1358 
 1359                         case IP_RECVRETOPTS:
 1360                                 optval = OPTBIT(INP_RECVRETOPTS);
 1361                                 break;
 1362 
 1363                         case IP_RECVDSTADDR:
 1364                                 optval = OPTBIT(INP_RECVDSTADDR);
 1365                                 break;
 1366 
 1367                         case IP_RECVTTL:
 1368                                 optval = OPTBIT(INP_RECVTTL);
 1369                                 break;
 1370 
 1371                         case IP_RECVIF:
 1372                                 optval = OPTBIT(INP_RECVIF);
 1373                                 break;
 1374 
 1375                         case IP_PORTRANGE:
 1376                                 if (inp->inp_flags & INP_HIGHPORT)
 1377                                         optval = IP_PORTRANGE_HIGH;
 1378                                 else if (inp->inp_flags & INP_LOWPORT)
 1379                                         optval = IP_PORTRANGE_LOW;
 1380                                 else
 1381                                         optval = 0;
 1382                                 break;
 1383 
 1384                         case IP_FAITH:
 1385                                 optval = OPTBIT(INP_FAITH);
 1386                                 break;
 1387 
 1388                         case IP_ONESBCAST:
 1389                                 optval = OPTBIT(INP_ONESBCAST);
 1390                                 break;
 1391                         }
 1392                         error = sooptcopyout(sopt, &optval, sizeof optval);
 1393                         break;
 1394 
 1395                 case IP_MULTICAST_IF:
 1396                 case IP_MULTICAST_VIF:
 1397                 case IP_MULTICAST_TTL:
 1398                 case IP_MULTICAST_LOOP:
 1399                 case IP_ADD_MEMBERSHIP:
 1400                 case IP_DROP_MEMBERSHIP:
 1401                         error = ip_getmoptions(inp, sopt);
 1402                         break;
 1403 
 1404 #if defined(IPSEC) || defined(FAST_IPSEC)
 1405                 case IP_IPSEC_POLICY:
 1406                 {
 1407                         struct mbuf *m = NULL;
 1408                         caddr_t req = NULL;
 1409                         size_t len = 0;
 1410 
 1411                         if (m != 0) {
 1412                                 req = mtod(m, caddr_t);
 1413                                 len = m->m_len;
 1414                         }
 1415                         error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
 1416                         if (error == 0)
 1417                                 error = soopt_mcopyout(sopt, m); /* XXX */
 1418                         if (error == 0)
 1419                                 m_freem(m);
 1420                         break;
 1421                 }
 1422 #endif /*IPSEC*/
 1423 
 1424                 default:
 1425                         error = ENOPROTOOPT;
 1426                         break;
 1427                 }
 1428                 break;
 1429         }
 1430         return (error);
 1431 }
 1432 
 1433 /*
 1434  * Set up IP options in pcb for insertion in output packets.
 1435  * Store in mbuf with pointer in pcbopt, adding pseudo-option
 1436  * with destination address if source routed.
 1437  */
 1438 static int
 1439 ip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m)
 1440 {
 1441         register int cnt, optlen;
 1442         register u_char *cp;
 1443         struct mbuf **pcbopt;
 1444         u_char opt;
 1445 
 1446         INP_LOCK_ASSERT(inp);
 1447 
 1448         pcbopt = &inp->inp_options;
 1449 
 1450         /* turn off any old options */
 1451         if (*pcbopt)
 1452                 (void)m_free(*pcbopt);
 1453         *pcbopt = 0;
 1454         if (m == NULL || m->m_len == 0) {
 1455                 /*
 1456                  * Only turning off any previous options.
 1457                  */
 1458                 if (m != NULL)
 1459                         (void)m_free(m);
 1460                 return (0);
 1461         }
 1462 
 1463         if (m->m_len % sizeof(int32_t))
 1464                 goto bad;
 1465         /*
 1466          * IP first-hop destination address will be stored before
 1467          * actual options; move other options back
 1468          * and clear it when none present.
 1469          */
 1470         if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
 1471                 goto bad;
 1472         cnt = m->m_len;
 1473         m->m_len += sizeof(struct in_addr);
 1474         cp = mtod(m, u_char *) + sizeof(struct in_addr);
 1475         bcopy(mtod(m, void *), cp, (unsigned)cnt);
 1476         bzero(mtod(m, void *), sizeof(struct in_addr));
 1477 
 1478         for (; cnt > 0; cnt -= optlen, cp += optlen) {
 1479                 opt = cp[IPOPT_OPTVAL];
 1480                 if (opt == IPOPT_EOL)
 1481                         break;
 1482                 if (opt == IPOPT_NOP)
 1483                         optlen = 1;
 1484                 else {
 1485                         if (cnt < IPOPT_OLEN + sizeof(*cp))
 1486                                 goto bad;
 1487                         optlen = cp[IPOPT_OLEN];
 1488                         if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
 1489                                 goto bad;
 1490                 }
 1491                 switch (opt) {
 1492 
 1493                 default:
 1494                         break;
 1495 
 1496                 case IPOPT_LSRR:
 1497                 case IPOPT_SSRR:
 1498                         /*
 1499                          * user process specifies route as:
 1500                          *      ->A->B->C->D
 1501                          * D must be our final destination (but we can't
 1502                          * check that since we may not have connected yet).
 1503                          * A is first hop destination, which doesn't appear in
 1504                          * actual IP option, but is stored before the options.
 1505                          */
 1506                         if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
 1507                                 goto bad;
 1508                         m->m_len -= sizeof(struct in_addr);
 1509                         cnt -= sizeof(struct in_addr);
 1510                         optlen -= sizeof(struct in_addr);
 1511                         cp[IPOPT_OLEN] = optlen;
 1512                         /*
 1513                          * Move first hop before start of options.
 1514                          */
 1515                         bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
 1516                             sizeof(struct in_addr));
 1517                         /*
 1518                          * Then copy rest of options back
 1519                          * to close up the deleted entry.
 1520                          */
 1521                         bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
 1522                             &cp[IPOPT_OFFSET+1],
 1523                             (unsigned)cnt - (IPOPT_MINOFF - 1));
 1524                         break;
 1525                 }
 1526         }
 1527         if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
 1528                 goto bad;
 1529         *pcbopt = m;
 1530         return (0);
 1531 
 1532 bad:
 1533         (void)m_free(m);
 1534         return (EINVAL);
 1535 }
 1536 
 1537 /*
 1538  * XXX
 1539  * The whole multicast option thing needs to be re-thought.
 1540  * Several of these options are equally applicable to non-multicast
 1541  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
 1542  * standard option (IP_TTL).
 1543  */
 1544 
 1545 /*
 1546  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
 1547  */
 1548 static struct ifnet *
 1549 ip_multicast_if(a, ifindexp)
 1550         struct in_addr *a;
 1551         int *ifindexp;
 1552 {
 1553         int ifindex;
 1554         struct ifnet *ifp;
 1555 
 1556         if (ifindexp)
 1557                 *ifindexp = 0;
 1558         if (ntohl(a->s_addr) >> 24 == 0) {
 1559                 ifindex = ntohl(a->s_addr) & 0xffffff;
 1560                 if (ifindex < 0 || if_index < ifindex)
 1561                         return NULL;
 1562                 ifp = ifnet_byindex(ifindex);
 1563                 if (ifindexp)
 1564                         *ifindexp = ifindex;
 1565         } else {
 1566                 INADDR_TO_IFP(*a, ifp);
 1567         }
 1568         return ifp;
 1569 }
 1570 
 1571 /*
 1572  * Given an inpcb, return its multicast options structure pointer.  Accepts
 1573  * an unlocked inpcb pointer, but will return it locked.  May sleep.
 1574  */
 1575 static struct ip_moptions *
 1576 ip_findmoptions(struct inpcb *inp)
 1577 {
 1578         struct ip_moptions *imo;
 1579 
 1580         INP_LOCK(inp);
 1581         if (inp->inp_moptions != NULL)
 1582                 return (inp->inp_moptions);
 1583 
 1584         INP_UNLOCK(inp);
 1585 
 1586         imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
 1587 
 1588         imo->imo_multicast_ifp = NULL;
 1589         imo->imo_multicast_addr.s_addr = INADDR_ANY;
 1590         imo->imo_multicast_vif = -1;
 1591         imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
 1592         imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
 1593         imo->imo_num_memberships = 0;
 1594 
 1595         INP_LOCK(inp);
 1596         if (inp->inp_moptions != NULL) {
 1597                 free(imo, M_IPMOPTS);
 1598                 return (inp->inp_moptions);
 1599         }
 1600         inp->inp_moptions = imo;
 1601         return (imo);
 1602 }
 1603 
 1604 /*
 1605  * Set the IP multicast options in response to user setsockopt().
 1606  */
 1607 static int
 1608 ip_setmoptions(struct inpcb *inp, struct sockopt *sopt)
 1609 {
 1610         int error = 0;
 1611         int i;
 1612         struct in_addr addr;
 1613         struct ip_mreq mreq;
 1614         struct ifnet *ifp;
 1615         struct ip_moptions *imo;
 1616         struct route ro;
 1617         struct sockaddr_in *dst;
 1618         int ifindex;
 1619         int s;
 1620 
 1621         switch (sopt->sopt_name) {
 1622         /* store an index number for the vif you wanna use in the send */
 1623         case IP_MULTICAST_VIF:
 1624                 if (legal_vif_num == 0) {
 1625                         error = EOPNOTSUPP;
 1626                         break;
 1627                 }
 1628                 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
 1629                 if (error)
 1630                         break;
 1631                 if (!legal_vif_num(i) && (i != -1)) {
 1632                         error = EINVAL;
 1633                         break;
 1634                 }
 1635                 imo = ip_findmoptions(inp);
 1636                 imo->imo_multicast_vif = i;
 1637                 INP_UNLOCK(inp);
 1638                 break;
 1639 
 1640         case IP_MULTICAST_IF:
 1641                 /*
 1642                  * Select the interface for outgoing multicast packets.
 1643                  */
 1644                 error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
 1645                 if (error)
 1646                         break;
 1647                 /*
 1648                  * INADDR_ANY is used to remove a previous selection.
 1649                  * When no interface is selected, a default one is
 1650                  * chosen every time a multicast packet is sent.
 1651                  */
 1652                 imo = ip_findmoptions(inp);
 1653                 if (addr.s_addr == INADDR_ANY) {
 1654                         imo->imo_multicast_ifp = NULL;
 1655                         INP_UNLOCK(inp);
 1656                         break;
 1657                 }
 1658                 /*
 1659                  * The selected interface is identified by its local
 1660                  * IP address.  Find the interface and confirm that
 1661                  * it supports multicasting.
 1662                  */
 1663                 s = splimp();
 1664                 ifp = ip_multicast_if(&addr, &ifindex);
 1665                 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
 1666                         INP_UNLOCK(inp);
 1667                         splx(s);
 1668                         error = EADDRNOTAVAIL;
 1669                         break;
 1670                 }
 1671                 imo->imo_multicast_ifp = ifp;
 1672                 if (ifindex)
 1673                         imo->imo_multicast_addr = addr;
 1674                 else
 1675                         imo->imo_multicast_addr.s_addr = INADDR_ANY;
 1676                 INP_UNLOCK(inp);
 1677                 splx(s);
 1678                 break;
 1679 
 1680         case IP_MULTICAST_TTL:
 1681                 /*
 1682                  * Set the IP time-to-live for outgoing multicast packets.
 1683                  * The original multicast API required a char argument,
 1684                  * which is inconsistent with the rest of the socket API.
 1685                  * We allow either a char or an int.
 1686                  */
 1687                 if (sopt->sopt_valsize == 1) {
 1688                         u_char ttl;
 1689                         error = sooptcopyin(sopt, &ttl, 1, 1);
 1690                         if (error)
 1691                                 break;
 1692                         imo = ip_findmoptions(inp);
 1693                         imo->imo_multicast_ttl = ttl;
 1694                         INP_UNLOCK(inp);
 1695                 } else {
 1696                         u_int ttl;
 1697                         error = sooptcopyin(sopt, &ttl, sizeof ttl, 
 1698                                             sizeof ttl);
 1699                         if (error)
 1700                                 break;
 1701                         if (ttl > 255)
 1702                                 error = EINVAL;
 1703                         else {
 1704                                 imo = ip_findmoptions(inp);
 1705                                 imo->imo_multicast_ttl = ttl;
 1706                                 INP_UNLOCK(inp);
 1707                         }
 1708                 }
 1709                 break;
 1710 
 1711         case IP_MULTICAST_LOOP:
 1712                 /*
 1713                  * Set the loopback flag for outgoing multicast packets.
 1714                  * Must be zero or one.  The original multicast API required a
 1715                  * char argument, which is inconsistent with the rest
 1716                  * of the socket API.  We allow either a char or an int.
 1717                  */
 1718                 if (sopt->sopt_valsize == 1) {
 1719                         u_char loop;
 1720                         error = sooptcopyin(sopt, &loop, 1, 1);
 1721                         if (error)
 1722                                 break;
 1723                         imo = ip_findmoptions(inp);
 1724                         imo->imo_multicast_loop = !!loop;
 1725                         INP_UNLOCK(inp);
 1726                 } else {
 1727                         u_int loop;
 1728                         error = sooptcopyin(sopt, &loop, sizeof loop,
 1729                                             sizeof loop);
 1730                         if (error)
 1731                                 break;
 1732                         imo = ip_findmoptions(inp);
 1733                         imo->imo_multicast_loop = !!loop;
 1734                         INP_UNLOCK(inp);
 1735                 }
 1736                 break;
 1737 
 1738         case IP_ADD_MEMBERSHIP:
 1739                 /*
 1740                  * Add a multicast group membership.
 1741                  * Group must be a valid IP multicast address.
 1742                  */
 1743                 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
 1744                 if (error)
 1745                         break;
 1746 
 1747                 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
 1748                         error = EINVAL;
 1749                         break;
 1750                 }
 1751                 s = splimp();
 1752                 /*
 1753                  * If no interface address was provided, use the interface of
 1754                  * the route to the given multicast address.
 1755                  */
 1756                 if (mreq.imr_interface.s_addr == INADDR_ANY) {
 1757                         bzero((caddr_t)&ro, sizeof(ro));
 1758                         dst = (struct sockaddr_in *)&ro.ro_dst;
 1759                         dst->sin_len = sizeof(*dst);
 1760                         dst->sin_family = AF_INET;
 1761                         dst->sin_addr = mreq.imr_multiaddr;
 1762                         rtalloc_ign(&ro, RTF_CLONING);
 1763                         if (ro.ro_rt == NULL) {
 1764                                 error = EADDRNOTAVAIL;
 1765                                 splx(s);
 1766                                 break;
 1767                         }
 1768                         ifp = ro.ro_rt->rt_ifp;
 1769                         RTFREE(ro.ro_rt);
 1770                 }
 1771                 else {
 1772                         ifp = ip_multicast_if(&mreq.imr_interface, NULL);
 1773                 }
 1774 
 1775                 /*
 1776                  * See if we found an interface, and confirm that it
 1777                  * supports multicast.
 1778                  */
 1779                 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
 1780                         error = EADDRNOTAVAIL;
 1781                         splx(s);
 1782                         break;
 1783                 }
 1784                 /*
 1785                  * See if the membership already exists or if all the
 1786                  * membership slots are full.
 1787                  */
 1788                 imo = ip_findmoptions(inp);
 1789                 for (i = 0; i < imo->imo_num_memberships; ++i) {
 1790                         if (imo->imo_membership[i]->inm_ifp == ifp &&
 1791                             imo->imo_membership[i]->inm_addr.s_addr
 1792                                                 == mreq.imr_multiaddr.s_addr)
 1793                                 break;
 1794                 }
 1795                 if (i < imo->imo_num_memberships) {
 1796                         INP_UNLOCK(inp);
 1797                         error = EADDRINUSE;
 1798                         splx(s);
 1799                         break;
 1800                 }
 1801                 if (i == IP_MAX_MEMBERSHIPS) {
 1802                         INP_UNLOCK(inp);
 1803                         error = ETOOMANYREFS;
 1804                         splx(s);
 1805                         break;
 1806                 }
 1807                 /*
 1808                  * Everything looks good; add a new record to the multicast
 1809                  * address list for the given interface.
 1810                  */
 1811                 if ((imo->imo_membership[i] =
 1812                     in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
 1813                         INP_UNLOCK(inp);
 1814                         error = ENOBUFS;
 1815                         splx(s);
 1816                         break;
 1817                 }
 1818                 ++imo->imo_num_memberships;
 1819                 INP_UNLOCK(inp);
 1820                 splx(s);
 1821                 break;
 1822 
 1823         case IP_DROP_MEMBERSHIP:
 1824                 /*
 1825                  * Drop a multicast group membership.
 1826                  * Group must be a valid IP multicast address.
 1827                  */
 1828                 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
 1829                 if (error)
 1830                         break;
 1831 
 1832                 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
 1833                         error = EINVAL;
 1834                         break;
 1835                 }
 1836 
 1837                 s = splimp();
 1838                 /*
 1839                  * If an interface address was specified, get a pointer
 1840                  * to its ifnet structure.
 1841                  */
 1842                 if (mreq.imr_interface.s_addr == INADDR_ANY)
 1843                         ifp = NULL;
 1844                 else {
 1845                         ifp = ip_multicast_if(&mreq.imr_interface, NULL);
 1846                         if (ifp == NULL) {
 1847                                 error = EADDRNOTAVAIL;
 1848                                 splx(s);
 1849                                 break;
 1850                         }
 1851                 }
 1852                 /*
 1853                  * Find the membership in the membership array.
 1854                  */
 1855                 imo = ip_findmoptions(inp);
 1856                 for (i = 0; i < imo->imo_num_memberships; ++i) {
 1857                         if ((ifp == NULL ||
 1858                              imo->imo_membership[i]->inm_ifp == ifp) &&
 1859                              imo->imo_membership[i]->inm_addr.s_addr ==
 1860                              mreq.imr_multiaddr.s_addr)
 1861                                 break;
 1862                 }
 1863                 if (i == imo->imo_num_memberships) {
 1864                         INP_UNLOCK(inp);
 1865                         error = EADDRNOTAVAIL;
 1866                         splx(s);
 1867                         break;
 1868                 }
 1869                 /*
 1870                  * Give up the multicast address record to which the
 1871                  * membership points.
 1872                  */
 1873                 in_delmulti(imo->imo_membership[i]);
 1874                 /*
 1875                  * Remove the gap in the membership array.
 1876                  */
 1877                 for (++i; i < imo->imo_num_memberships; ++i)
 1878                         imo->imo_membership[i-1] = imo->imo_membership[i];
 1879                 --imo->imo_num_memberships;
 1880                 INP_UNLOCK(inp);
 1881                 splx(s);
 1882                 break;
 1883 
 1884         default:
 1885                 error = EOPNOTSUPP;
 1886                 break;
 1887         }
 1888 
 1889         return (error);
 1890 }
 1891 
 1892 /*
 1893  * Return the IP multicast options in response to user getsockopt().
 1894  */
 1895 static int
 1896 ip_getmoptions(struct inpcb *inp, struct sockopt *sopt)
 1897 {
 1898         struct ip_moptions *imo;
 1899         struct in_addr addr;
 1900         struct in_ifaddr *ia;
 1901         int error, optval;
 1902         u_char coptval;
 1903 
 1904         INP_LOCK(inp);
 1905         imo = inp->inp_moptions;
 1906 
 1907         error = 0;
 1908         switch (sopt->sopt_name) {
 1909         case IP_MULTICAST_VIF: 
 1910                 if (imo != NULL)
 1911                         optval = imo->imo_multicast_vif;
 1912                 else
 1913                         optval = -1;
 1914                 INP_UNLOCK(inp);
 1915                 error = sooptcopyout(sopt, &optval, sizeof optval);
 1916                 break;
 1917 
 1918         case IP_MULTICAST_IF:
 1919                 if (imo == NULL || imo->imo_multicast_ifp == NULL)
 1920                         addr.s_addr = INADDR_ANY;
 1921                 else if (imo->imo_multicast_addr.s_addr) {
 1922                         /* return the value user has set */
 1923                         addr = imo->imo_multicast_addr;
 1924                 } else {
 1925                         IFP_TO_IA(imo->imo_multicast_ifp, ia);
 1926                         addr.s_addr = (ia == NULL) ? INADDR_ANY
 1927                                 : IA_SIN(ia)->sin_addr.s_addr;
 1928                 }
 1929                 INP_UNLOCK(inp);
 1930                 error = sooptcopyout(sopt, &addr, sizeof addr);
 1931                 break;
 1932 
 1933         case IP_MULTICAST_TTL:
 1934                 if (imo == 0)
 1935                         optval = coptval = IP_DEFAULT_MULTICAST_TTL;
 1936                 else
 1937                         optval = coptval = imo->imo_multicast_ttl;
 1938                 INP_UNLOCK(inp);
 1939                 if (sopt->sopt_valsize == 1)
 1940                         error = sooptcopyout(sopt, &coptval, 1);
 1941                 else
 1942                         error = sooptcopyout(sopt, &optval, sizeof optval);
 1943                 break;
 1944 
 1945         case IP_MULTICAST_LOOP:
 1946                 if (imo == 0)
 1947                         optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
 1948                 else
 1949                         optval = coptval = imo->imo_multicast_loop;
 1950                 INP_UNLOCK(inp);
 1951                 if (sopt->sopt_valsize == 1)
 1952                         error = sooptcopyout(sopt, &coptval, 1);
 1953                 else
 1954                         error = sooptcopyout(sopt, &optval, sizeof optval);
 1955                 break;
 1956 
 1957         default:
 1958                 INP_UNLOCK(inp);
 1959                 error = ENOPROTOOPT;
 1960                 break;
 1961         }
 1962         INP_UNLOCK_ASSERT(inp);
 1963 
 1964         return (error);
 1965 }
 1966 
 1967 /*
 1968  * Discard the IP multicast options.
 1969  */
 1970 void
 1971 ip_freemoptions(imo)
 1972         register struct ip_moptions *imo;
 1973 {
 1974         register int i;
 1975 
 1976         if (imo != NULL) {
 1977                 for (i = 0; i < imo->imo_num_memberships; ++i)
 1978                         in_delmulti(imo->imo_membership[i]);
 1979                 free(imo, M_IPMOPTS);
 1980         }
 1981 }
 1982 
 1983 /*
 1984  * Routine called from ip_output() to loop back a copy of an IP multicast
 1985  * packet to the input queue of a specified interface.  Note that this
 1986  * calls the output routine of the loopback "driver", but with an interface
 1987  * pointer that might NOT be a loopback interface -- evil, but easier than
 1988  * replicating that code here.
 1989  */
 1990 static void
 1991 ip_mloopback(ifp, m, dst, hlen)
 1992         struct ifnet *ifp;
 1993         register struct mbuf *m;
 1994         register struct sockaddr_in *dst;
 1995         int hlen;
 1996 {
 1997         register struct ip *ip;
 1998         struct mbuf *copym;
 1999 
 2000         copym = m_copy(m, 0, M_COPYALL);
 2001         if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
 2002                 copym = m_pullup(copym, hlen);
 2003         if (copym != NULL) {
 2004                 /* If needed, compute the checksum and mark it as valid. */
 2005                 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
 2006                         in_delayed_cksum(copym);
 2007                         copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
 2008                         copym->m_pkthdr.csum_flags |=
 2009                             CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
 2010                         copym->m_pkthdr.csum_data = 0xffff;
 2011                 }
 2012                 /*
 2013                  * We don't bother to fragment if the IP length is greater
 2014                  * than the interface's MTU.  Can this possibly matter?
 2015                  */
 2016                 ip = mtod(copym, struct ip *);
 2017                 ip->ip_len = htons(ip->ip_len);
 2018                 ip->ip_off = htons(ip->ip_off);
 2019                 ip->ip_sum = 0;
 2020                 ip->ip_sum = in_cksum(copym, hlen);
 2021                 /*
 2022                  * NB:
 2023                  * It's not clear whether there are any lingering
 2024                  * reentrancy problems in other areas which might
 2025                  * be exposed by using ip_input directly (in
 2026                  * particular, everything which modifies the packet
 2027                  * in-place).  Yet another option is using the
 2028                  * protosw directly to deliver the looped back
 2029                  * packet.  For the moment, we'll err on the side
 2030                  * of safety by using if_simloop().
 2031                  */
 2032 #if 1 /* XXX */
 2033                 if (dst->sin_family != AF_INET) {
 2034                         printf("ip_mloopback: bad address family %d\n",
 2035                                                 dst->sin_family);
 2036                         dst->sin_family = AF_INET;
 2037                 }
 2038 #endif
 2039 
 2040 #ifdef notdef
 2041                 copym->m_pkthdr.rcvif = ifp;
 2042                 ip_input(copym);
 2043 #else
 2044                 if_simloop(ifp, copym, dst->sin_family, 0);
 2045 #endif
 2046         }
 2047 }

Cache object: 62410ac2ed2b8b67098a6f5429121688


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