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/in6_prefix.c

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

    1 /*      $FreeBSD: src/sys/netinet6/in6_prefix.c,v 1.4.2.3 2001/07/03 11:01:52 ume Exp $ */
    2 /*      $KAME: in6_prefix.c,v 1.47 2001/03/25 08:41:39 itojun Exp $     */
    3 
    4 /*
    5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. Neither the name of the project nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 /*
   34  * Copyright (c) 1982, 1986, 1991, 1993
   35  *      The Regents of the University of California.  All rights reserved.
   36  *
   37  * Redistribution and use in source and binary forms, with or without
   38  * modification, are permitted provided that the following conditions
   39  * are met:
   40  * 1. Redistributions of source code must retain the above copyright
   41  *    notice, this list of conditions and the following disclaimer.
   42  * 2. Redistributions in binary form must reproduce the above copyright
   43  *    notice, this list of conditions and the following disclaimer in the
   44  *    documentation and/or other materials provided with the distribution.
   45  * 3. Neither the name of the University nor the names of its contributors
   46  *    may be used to endorse or promote products derived from this software
   47  *    without specific prior written permission.
   48  *
   49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   59  * SUCH DAMAGE.
   60  *
   61  *      @(#)in.c        8.2 (Berkeley) 11/15/93
   62  */
   63 
   64 #include <sys/param.h>
   65 #include <sys/malloc.h>
   66 #include <sys/kernel.h>
   67 #include <sys/socket.h>
   68 #include <sys/socketvar.h>
   69 #include <sys/sockio.h>
   70 #include <sys/systm.h>
   71 #include <sys/syslog.h>
   72 #include <sys/proc.h>
   73 #include <sys/thread2.h>
   74 
   75 #include <net/if.h>
   76 
   77 #include <netinet/in.h>
   78 #include <netinet/in_var.h>
   79 #include <netinet/ip6.h>
   80 #include <netinet6/in6_prefix.h>
   81 #include <netinet6/ip6_var.h>
   82 
   83 static MALLOC_DEFINE(M_IP6RR, "ip6rr", "IPv6 Router Renumbering Prefix");
   84 static MALLOC_DEFINE(M_RR_ADDR, "rp_addr", "IPv6 Router Renumbering Ifid");
   85 
   86 struct rr_prhead rr_prefix;
   87 
   88 struct callout in6_rr_timer_ch;
   89 
   90 #include <net/net_osdep.h>
   91 
   92 static void     add_each_addr (struct socket *so, struct rr_prefix *rpp,
   93                                    struct rp_addr *rap);
   94 static int create_ra_entry (struct rp_addr **rapp);
   95 static int add_each_prefix (struct socket *so, struct rr_prefix *rpp);
   96 static void free_rp_entries (struct rr_prefix *rpp);
   97 static int link_stray_ia6s (struct rr_prefix *rpp);
   98 static void     rp_remove (struct rr_prefix *rpp);
   99 
  100 /*
  101  * Copy bits from src to tgt, from off bit for len bits.
  102  * Caller must specify collect tgtsize and srcsize.
  103  */
  104 static void
  105 bit_copy(char *tgt, u_int tgtsize, char *src, u_int srcsize,
  106          u_int off, u_int len)
  107 {
  108         char *sp, *tp;
  109 
  110         /* arg values check */
  111         if (srcsize < off || srcsize < (off + len) ||
  112             tgtsize < off || tgtsize < (off + len)) {
  113                 log(LOG_ERR,
  114                     "in6_prefix.c: bit_copy: invalid args: srcsize %d,\n"
  115                     "tgtsize %d, off %d, len %d\n", srcsize, tgtsize, off,
  116                     len);
  117                 return;
  118         }
  119 
  120         /* search start point */
  121         for (sp = src, tp = tgt; off >= 8; sp++, tp++)
  122                 off-=8;
  123         /* copy starting bits */
  124         if (off) {
  125                 char setbit;
  126                 int startbits;
  127 
  128                 startbits = min((8 - off), len);
  129 
  130                 for (setbit = (0x80 >> off); startbits;
  131                      setbit >>= 1, startbits--, len--)
  132                                 *tp  |= (setbit & *sp);
  133                 tp++;
  134                 sp++;
  135         }
  136         /* copy midium bits */
  137         for (; len >= 8; sp++, tp++) {
  138                 *tp = *sp;
  139                 len-=8;
  140         }
  141         /* copy ending bits */
  142         if (len) {
  143                 char setbit;
  144 
  145                 for (setbit = 0x80; len; setbit >>= 1, len--)
  146                         *tp  |= (setbit & *sp);
  147         }
  148 }
  149 
  150 static struct ifprefix *
  151 in6_prefixwithifp(struct ifnet *ifp, int plen, struct in6_addr *dst)
  152 {
  153         struct ifprefix *ifpr;
  154 
  155         /* search matched prefix */
  156         for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
  157              ifpr = TAILQ_NEXT(ifpr, ifpr_list))
  158         {
  159                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
  160                     ifpr->ifpr_type != IN6_PREFIX_RR)
  161                         continue;
  162                 if (plen <= in6_matchlen(dst, IFPR_IN6(ifpr)))
  163                         break;
  164         }
  165         return (ifpr);
  166 }
  167 
  168 /*
  169  * Search prefix which matches arg prefix as specified in
  170  * draft-ietf-ipngwg-router-renum-08.txt
  171  */
  172 static struct rr_prefix *
  173 search_matched_prefix(struct ifnet *ifp, struct in6_prefixreq *ipr)
  174 {
  175         struct ifprefix *ifpr;
  176         struct ifaddr_container *ifac;
  177         struct ifaddr *ifa;
  178         struct rr_prefix *rpp;
  179 
  180         /* search matched prefix */
  181         ifpr = in6_prefixwithifp(ifp, ipr->ipr_plen,
  182                                  &ipr->ipr_prefix.sin6_addr);
  183         if (ifpr != NULL)
  184                 return ifpr2rp(ifpr);
  185 
  186         /*
  187          * search matched addr, and then search prefix
  188          * which matches the addr
  189          */
  190 
  191         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
  192                 ifa = ifac->ifa;
  193 
  194                 if (ifa->ifa_addr->sa_family != AF_INET6)
  195                         continue;
  196                 if (ipr->ipr_plen <=
  197                     in6_matchlen(&ipr->ipr_prefix.sin6_addr, IFA_IN6(ifa)))
  198                         break;
  199         }
  200         if (ifac == NULL)
  201                 return NULL;
  202 
  203         rpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr);
  204         if (rpp != NULL)
  205                 return rpp;
  206 
  207         for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
  208              ifpr = TAILQ_NEXT(ifpr, ifpr_list))
  209         {
  210                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
  211                     ifpr->ifpr_type != IN6_PREFIX_RR)
  212                         continue;
  213                 if (ifpr->ifpr_plen <= in6_matchlen(IFA_IN6(ifa),
  214                                                     IFPR_IN6(ifpr)))
  215                         break;
  216         }
  217         if (ifpr != NULL)
  218                 log(LOG_ERR,  "in6_prefix.c: search_matched_prefix: addr %s"
  219                     "has no pointer to prefix %s\n", ip6_sprintf(IFA_IN6(ifa)),
  220                     ip6_sprintf(IFPR_IN6(ifpr)));
  221         return ifpr2rp(ifpr);
  222 }
  223 
  224 /*
  225  * Search prefix which matches arg prefix as specified in
  226  * draft-ietf-ipngwg-router-renum-08.txt, and mark it if exists.
  227  * Return 1 if anything matched, and 0 if nothing matched.
  228  */
  229 static int
  230 mark_matched_prefixes(u_long cmd, struct ifnet *ifp, struct in6_rrenumreq *irr)
  231 {
  232         struct ifaddr_container *ifac;
  233         struct ifprefix *ifpr;
  234         int matchlen, matched = 0;
  235 
  236         /* search matched prefixes */
  237         for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
  238              ifpr = TAILQ_NEXT(ifpr, ifpr_list))
  239         {
  240                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
  241                     ifpr->ifpr_type != IN6_PREFIX_RR)
  242                         continue;
  243                 matchlen = in6_matchlen(&irr->irr_matchprefix.sin6_addr,
  244                                         IFPR_IN6(ifpr));
  245                 if (irr->irr_m_minlen > ifpr->ifpr_plen ||
  246                     irr->irr_m_maxlen < ifpr->ifpr_plen ||
  247                     irr->irr_m_len > matchlen)
  248                         continue;
  249                 matched = 1;
  250                 ifpr2rp(ifpr)->rp_statef_addmark = 1;
  251                 if (cmd == SIOCCIFPREFIX_IN6)
  252                         ifpr2rp(ifpr)->rp_statef_delmark = 1;
  253         }
  254 
  255         /*
  256          * search matched addr, and then search prefixes
  257          * which matche the addr
  258          */
  259         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
  260                 struct ifaddr *ifa = ifac->ifa;
  261                 struct rr_prefix *rpp;
  262 
  263                 if (ifa->ifa_addr->sa_family != AF_INET6)
  264                         continue;
  265                 matchlen = in6_matchlen(&irr->irr_matchprefix.sin6_addr,
  266                                         IFA_IN6(ifa));
  267                 if (irr->irr_m_minlen > matchlen ||
  268                     irr->irr_m_maxlen < matchlen || irr->irr_m_len > matchlen)
  269                         continue;
  270                 rpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr);
  271                 if (rpp != NULL) {
  272                         matched = 1;
  273                         rpp->rp_statef_addmark = 1;
  274                         if (cmd == SIOCCIFPREFIX_IN6)
  275                                 rpp->rp_statef_delmark = 1;
  276                 } else
  277                         log(LOG_WARNING, "in6_prefix.c: mark_matched_prefixes:"
  278                             "no back pointer to ifprefix for %s. "
  279                             "ND autoconfigured addr?\n",
  280                             ip6_sprintf(IFA_IN6(ifa)));
  281         }
  282         return matched;
  283 }
  284 
  285 /*
  286  * Mark global prefixes as to be deleted.
  287  */
  288 static void
  289 delmark_global_prefixes(struct ifnet *ifp, struct in6_rrenumreq *irr)
  290 {
  291         struct ifprefix *ifpr;
  292 
  293         /* search matched prefixes */
  294         for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
  295              ifpr = TAILQ_NEXT(ifpr, ifpr_list))
  296         {
  297                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
  298                     ifpr->ifpr_type != IN6_PREFIX_RR)
  299                         continue;
  300                 /* mark delete global prefix */
  301                 if (in6_addrscope(RP_IN6(ifpr2rp(ifpr))) ==
  302                     IPV6_ADDR_SCOPE_GLOBAL)
  303                         ifpr2rp(ifpr)->rp_statef_delmark = 1;
  304         }
  305 }
  306 
  307 /* Unmark prefixes */
  308 static void
  309 unmark_prefixes(struct ifnet *ifp)
  310 {
  311         struct ifprefix *ifpr;
  312 
  313         /* unmark all prefix */
  314         for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
  315              ifpr = TAILQ_NEXT(ifpr, ifpr_list))
  316         {
  317                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
  318                     ifpr->ifpr_type != IN6_PREFIX_RR)
  319                         continue;
  320                 /* unmark prefix */
  321                 ifpr2rp(ifpr)->rp_statef_addmark = 0;
  322                 ifpr2rp(ifpr)->rp_statef_delmark = 0;
  323         }
  324 }
  325 
  326 static void
  327 init_prefix_ltimes(struct rr_prefix *rpp)
  328 {
  329 
  330         if (rpp->rp_pltime == RR_INFINITE_LIFETIME ||
  331             rpp->rp_rrf_decrprefd == 0)
  332                 rpp->rp_preferred = 0;
  333         else
  334                 rpp->rp_preferred = time_uptime + rpp->rp_pltime;
  335         if (rpp->rp_vltime == RR_INFINITE_LIFETIME ||
  336             rpp->rp_rrf_decrvalid == 0)
  337                 rpp->rp_expire = 0;
  338         else
  339                 rpp->rp_expire = time_uptime + rpp->rp_vltime;
  340 }
  341 
  342 static int
  343 rr_are_ifid_equal(struct in6_addr *ii1, struct in6_addr *ii2, int ii_len)
  344 {
  345         int ii_bytelen, ii_bitlen;
  346         int p_bytelen, p_bitlen;
  347 
  348         /* sanity check */
  349         if (1 > ii_len ||
  350             ii_len > 124) { /* as RFC2373, prefix is at least 4 bit */
  351                 log(LOG_ERR, "rr_are_ifid_equal: invalid ifid length(%d)\n",
  352                     ii_len);
  353                 return (0);
  354         }
  355 
  356         ii_bytelen = ii_len / 8;
  357         ii_bitlen = ii_len % 8;
  358 
  359         p_bytelen = sizeof(struct in6_addr) - ii_bytelen - 1;
  360         p_bitlen = 8 - ii_bitlen;
  361 
  362         if (bcmp(ii1->s6_addr + p_bytelen + 1, ii2->s6_addr + p_bytelen + 1,
  363                  ii_bytelen))
  364                 return (0);
  365         if (((ii1->s6_addr[p_bytelen] << p_bitlen) & 0xff) !=
  366             ((ii2->s6_addr[p_bytelen] << p_bitlen) & 0xff))
  367                 return (0);
  368 
  369         return (1);
  370 }
  371 
  372 static struct rp_addr *
  373 search_ifidwithprefix(struct rr_prefix *rpp, struct in6_addr *ifid)
  374 {
  375         struct rp_addr *rap;
  376 
  377         LIST_FOREACH(rap, &rpp->rp_addrhead, ra_entry)
  378         {
  379                 if (rr_are_ifid_equal(ifid, &rap->ra_ifid,
  380                                       (sizeof(struct in6_addr) << 3) -
  381                                       rpp->rp_plen))
  382                         break;
  383         }
  384         return rap;
  385 }
  386 
  387 static int
  388 assign_ra_entry(struct rr_prefix *rpp, int iilen, struct in6_ifaddr *ia)
  389 {
  390         int error = 0;
  391         struct rp_addr *rap;
  392 
  393         if ((error = create_ra_entry(&rap)) != 0)
  394                 return error;
  395 
  396         /* copy interface id part */
  397         bit_copy((caddr_t)&rap->ra_ifid, sizeof(rap->ra_ifid) << 3,
  398                  (caddr_t)IA6_IN6(ia),
  399                  sizeof(*IA6_IN6(ia)) << 3, rpp->rp_plen, iilen);
  400         /* link to ia, and put into list */
  401         rap->ra_addr = ia;
  402         crit_enter();
  403         _IFAREF(&rap->ra_addr->ia_ifa, 0);
  404 #if 0 /* Can't do this now, because rpp may be on th stack. should fix it? */
  405         ia->ia6_ifpr = rp2ifpr(rpp);
  406 #endif
  407         LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
  408         crit_exit();
  409 
  410         return 0;
  411 }
  412 
  413 /*
  414  * add a link-local address to an interface.  we will add new interface address
  415  * (prefix database + new interface id).
  416  */
  417 static int
  418 in6_prefix_add_llifid(int iilen, struct in6_ifaddr *ia)
  419 {
  420         struct rr_prefix *rpp;
  421         struct rp_addr *rap;
  422         struct socket so;
  423         int error;
  424 
  425         if ((error = create_ra_entry(&rap)) != 0)
  426                 return (error);
  427         /* copy interface id part */
  428         bit_copy((caddr_t)&rap->ra_ifid, sizeof(rap->ra_ifid) << 3,
  429                  (caddr_t)IA6_IN6(ia), sizeof(*IA6_IN6(ia)) << 3,
  430                  64, (sizeof(rap->ra_ifid) << 3) - 64);
  431         /* XXX: init dummy so */
  432         bzero(&so, sizeof(so));
  433         /* insert into list */
  434         LIST_FOREACH(rpp, &rr_prefix, rp_entry)
  435         {
  436                 /*
  437                  * do not attempt to add an address, if ifp does not match
  438                  */
  439                 if (rpp->rp_ifp != ia->ia_ifp)
  440                         continue;
  441 
  442                 crit_enter();
  443                 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
  444                 crit_exit();
  445                 add_each_addr(&so, rpp, rap);
  446         }
  447         return 0;
  448 }
  449 
  450 /*
  451  * add an address to an interface.  if the interface id portion is new,
  452  * we will add new interface address (prefix database + new interface id).
  453  */
  454 int
  455 in6_prefix_add_ifid(int iilen, struct in6_ifaddr *ia)
  456 {
  457         int plen = (sizeof(*IA6_IN6(ia)) << 3) - iilen;
  458         struct ifprefix *ifpr;
  459         struct rp_addr *rap;
  460         int error = 0;
  461 
  462         if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
  463                 return (in6_prefix_add_llifid(iilen, ia));
  464         ifpr = in6_prefixwithifp(ia->ia_ifp, plen, IA6_IN6(ia));
  465         if (ifpr == NULL) {
  466                 struct rr_prefix rp;
  467                 struct socket so;
  468                 int pplen = (plen == 128) ? 64 : plen; /* XXX hardcoded 64 is bad */
  469 
  470                 /* allocate a prefix for ia, with default properties */
  471 
  472                 /* init rp */
  473                 bzero(&rp, sizeof(rp));
  474                 rp.rp_type = IN6_PREFIX_RR;
  475                 rp.rp_ifp = ia->ia_ifp;
  476                 rp.rp_plen = pplen;
  477                 rp.rp_prefix.sin6_len = sizeof(rp.rp_prefix);
  478                 rp.rp_prefix.sin6_family = AF_INET6;
  479                 bit_copy((char *)RP_IN6(&rp), sizeof(*RP_IN6(&rp)) << 3,
  480                          (char *)&ia->ia_addr.sin6_addr,
  481                          sizeof(ia->ia_addr.sin6_addr) << 3,
  482                          0, pplen);
  483                 rp.rp_vltime = rp.rp_pltime = RR_INFINITE_LIFETIME;
  484                 rp.rp_raf_onlink = 1;
  485                 rp.rp_raf_auto = 1;
  486                 /* Is some FlagMasks for rrf necessary? */
  487                 rp.rp_rrf_decrvalid = rp.rp_rrf_decrprefd = 0;
  488                 rp.rp_origin = PR_ORIG_RR; /* can be renumbered */
  489 
  490                 /* create ra_entry */
  491                 error = link_stray_ia6s(&rp);
  492                 if (error != 0) {
  493                         free_rp_entries(&rp);
  494                         return error;
  495                 }
  496 
  497                 /* XXX: init dummy so */
  498                 bzero(&so, sizeof(so));
  499 
  500                 error = add_each_prefix(&so, &rp);
  501 
  502                 /* free each rp_addr entry */
  503                 free_rp_entries(&rp);
  504 
  505                 if (error != 0)
  506                         return error;
  507 
  508                 /* search again */
  509                 ifpr = in6_prefixwithifp(ia->ia_ifp, pplen, IA6_IN6(ia));
  510                 if (ifpr == NULL)
  511                         return 0;
  512         }
  513         rap = search_ifidwithprefix(ifpr2rp(ifpr), IA6_IN6(ia));
  514         if (rap != NULL) {
  515                 if (rap->ra_addr == NULL) {
  516                         rap->ra_addr = ia;
  517                         crit_enter();   /* XXX MP not MP safe */
  518                         _IFAREF(&rap->ra_addr->ia_ifa, 0);
  519                         crit_exit();
  520                 } else if (rap->ra_addr != ia) {
  521                         /* There may be some inconsistencies between addrs. */
  522                         log(LOG_ERR, "ip6_prefix.c: addr %s/%d matched prefix"
  523                             " already has another ia %p(%s) on its ifid list\n",
  524                             ip6_sprintf(IA6_IN6(ia)), plen,
  525                             rap->ra_addr,
  526                             ip6_sprintf(IA6_IN6(rap->ra_addr)));
  527                         return EADDRINUSE /* XXX */;
  528                 }
  529                 ia->ia6_ifpr = ifpr;
  530                 return 0;
  531         }
  532         error = assign_ra_entry(ifpr2rp(ifpr), iilen, ia);
  533         if (error == 0)
  534                 ia->ia6_ifpr = ifpr;
  535         return (error);
  536 }
  537 
  538 void
  539 in6_prefix_remove_ifid(int iilen, struct in6_ifaddr *ia)
  540 {
  541         struct rp_addr *rap;
  542 
  543         if (ia->ia6_ifpr == NULL)
  544                 return;
  545         rap = search_ifidwithprefix(ifpr2rp(ia->ia6_ifpr), IA6_IN6(ia));
  546         if (rap != NULL) {
  547                 crit_enter();
  548                 LIST_REMOVE(rap, ra_entry);
  549                 if (rap->ra_addr)
  550                         _IFAFREE(&rap->ra_addr->ia_ifa, 0);
  551                 crit_exit();
  552                 kfree(rap, M_RR_ADDR);
  553         }
  554 
  555         if (LIST_EMPTY(&ifpr2rp(ia->ia6_ifpr)->rp_addrhead))
  556                 rp_remove(ifpr2rp(ia->ia6_ifpr));
  557 }
  558 
  559 void
  560 in6_purgeprefix(struct ifnet *ifp)
  561 {
  562         struct ifprefix *ifpr, *nextifpr;
  563 
  564         /* delete prefixes before ifnet goes away */
  565         for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
  566              ifpr = nextifpr)
  567         {
  568                 nextifpr = TAILQ_NEXT(ifpr, ifpr_list);
  569                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
  570                     ifpr->ifpr_type != IN6_PREFIX_RR)
  571                         continue;
  572                 delete_each_prefix(ifpr2rp(ifpr), PR_ORIG_KERNEL);
  573         }
  574 }
  575 
  576 static void
  577 add_each_addr(struct socket *so, struct rr_prefix *rpp, struct rp_addr *rap)
  578 {
  579         struct in6_ifaddr *ia6;
  580         struct in6_aliasreq ifra;
  581         int error;
  582 
  583         /* init ifra */
  584         bzero(&ifra, sizeof(ifra));
  585         strncpy(ifra.ifra_name, if_name(rpp->rp_ifp), sizeof(ifra.ifra_name));
  586         ifra.ifra_addr.sin6_family = ifra.ifra_prefixmask.sin6_family =
  587                 AF_INET6;
  588         ifra.ifra_addr.sin6_len = ifra.ifra_prefixmask.sin6_len =
  589                 sizeof(ifra.ifra_addr);
  590         /* copy prefix part */
  591         bit_copy((char *)&ifra.ifra_addr.sin6_addr,
  592                  sizeof(ifra.ifra_addr.sin6_addr) << 3,
  593                  (char *)RP_IN6(rpp), sizeof(*RP_IN6(rpp)) << 3,
  594                  0, rpp->rp_plen);
  595         /* copy interface id part */
  596         bit_copy((char *)&ifra.ifra_addr.sin6_addr,
  597                  sizeof(ifra.ifra_addr.sin6_addr) << 3,
  598                  (char *)&rap->ra_ifid, sizeof(rap->ra_ifid) << 3,
  599                  rpp->rp_plen, (sizeof(rap->ra_ifid) << 3) - rpp->rp_plen);
  600         in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, rpp->rp_plen);
  601         /* don't care ifra_flags for now */
  602 
  603         /*
  604          * XXX: if we did this with finite lifetime values, the lifetimes would
  605          *      decrese in time and never incremented.
  606          *      we should need more clarifications on the prefix mechanism...
  607          */
  608         ifra.ifra_lifetime.ia6t_vltime = rpp->rp_vltime;
  609         ifra.ifra_lifetime.ia6t_pltime = rpp->rp_pltime;
  610 
  611         ia6 = in6ifa_ifpwithaddr(rpp->rp_ifp, &ifra.ifra_addr.sin6_addr);
  612         if (ia6 != NULL) {
  613                 if (ia6->ia6_ifpr == NULL) {
  614                         crit_enter();   /* XXX MP not MP safe */
  615                         /* link this addr and the prefix each other */
  616                         if (rap->ra_addr)
  617                                 _IFAFREE(&rap->ra_addr->ia_ifa, 0);
  618                         rap->ra_addr = ia6;
  619                         _IFAREF(&rap->ra_addr->ia_ifa, 0);
  620                         crit_exit();
  621                         ia6->ia6_ifpr = rp2ifpr(rpp);
  622                         return;
  623                 }
  624                 if (ia6->ia6_ifpr == rp2ifpr(rpp)) {
  625                         crit_enter();   /* XXX MP not MP safe */
  626                         if (rap->ra_addr)
  627                                 _IFAFREE(&rap->ra_addr->ia_ifa, 0);
  628                         rap->ra_addr = ia6;
  629                         _IFAREF(&rap->ra_addr->ia_ifa, 0);
  630                         crit_exit();
  631                         return;
  632                 }
  633                 /*
  634                  * The addr is already assigned to other
  635                  * prefix.
  636                  * There may be some inconsistencies between
  637                  * prefixes.
  638                  * e.g. overraped prefixes with common starting
  639                  *      part and different plefixlen.
  640                  *      Or, completely duplicated prefixes?
  641                  * log it and return.
  642                  */
  643                 log(LOG_ERR,
  644                     "in6_prefix.c: add_each_addr: addition of an addr %s/%d "
  645                     "failed because there is already another addr %s/%d\n",
  646                     ip6_sprintf(&ifra.ifra_addr.sin6_addr), rpp->rp_plen,
  647                     ip6_sprintf(IA6_IN6(ia6)),
  648                     in6_mask2len(&ia6->ia_prefixmask.sin6_addr, NULL));
  649                 return;
  650         }
  651         /* propagate ANYCAST flag if it is set for ancestor addr */
  652         if (rap->ra_flags.anycast != 0)
  653                 ifra.ifra_flags |= IN6_IFF_ANYCAST;
  654         error = in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, rpp->rp_ifp,
  655                             curthread);
  656         if (error != 0) {
  657                 log(LOG_ERR, "in6_prefix.c: add_each_addr: addition of an addr"
  658                     "%s/%d failed because in6_control failed for error %d\n",
  659                     ip6_sprintf(&ifra.ifra_addr.sin6_addr), rpp->rp_plen,
  660                     error);
  661                 return;
  662         }
  663 
  664         /*
  665          * link beween this addr and the prefix will be done
  666          * in in6_prefix_add_ifid
  667          */
  668 }
  669 
  670 static int
  671 rrpr_update(struct socket *so, struct rr_prefix *new)
  672 {
  673         struct rr_prefix *rpp;
  674         struct ifprefix *ifpr;
  675         struct rp_addr *rap;
  676 
  677         /* search existing prefix */
  678         for (ifpr = TAILQ_FIRST(&new->rp_ifp->if_prefixhead); ifpr;
  679              ifpr = TAILQ_NEXT(ifpr, ifpr_list))
  680         {
  681                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
  682                     ifpr->ifpr_type != IN6_PREFIX_RR)
  683                         continue;
  684                 if (ifpr->ifpr_plen == new->rp_plen &&
  685                     in6_are_prefix_equal(IFPR_IN6(ifpr), RP_IN6(new),
  686                                          ifpr->ifpr_plen))
  687                         break;
  688         }
  689         rpp = ifpr2rp(ifpr);
  690         if (rpp != NULL) {
  691                 /*
  692                  * We got a prefix which we have seen in the past.
  693                  */
  694                 /*
  695                  * If the origin of the already-installed prefix is more
  696                  * preferable than the new one, ignore installation request.
  697                  */
  698                 if (rpp->rp_origin > new->rp_origin)
  699                         return (EPERM);
  700 
  701                 /* update prefix information */
  702                 rpp->rp_flags.prf_ra = new->rp_flags.prf_ra;
  703                 if (rpp->rp_origin >= PR_ORIG_RR)
  704                         rpp->rp_flags.prf_rr = new->rp_flags.prf_rr;
  705                 rpp->rp_vltime = new->rp_vltime;
  706                 rpp->rp_pltime = new->rp_pltime;
  707                 rpp->rp_expire = new->rp_expire;
  708                 rpp->rp_preferred = new->rp_preferred;
  709                 rpp->rp_statef_delmark = 0; /* cancel deletion */
  710                 /*
  711                  * Interface id related update.
  712                  *  add rp_addr entries in new into rpp, if they have not
  713                  *  been already included in rpp.
  714                  */
  715                 while (!LIST_EMPTY(&new->rp_addrhead))
  716                 {
  717                         rap = LIST_FIRST(&new->rp_addrhead);
  718                         LIST_REMOVE(rap, ra_entry);
  719                         if (search_ifidwithprefix(rpp, &rap->ra_ifid)
  720                             != NULL) {
  721                                 if (rap->ra_addr) {
  722                                         crit_enter();   /* XXX MP not MP safe */
  723                                         _IFAFREE(&rap->ra_addr->ia_ifa, 0);
  724                                         crit_exit();
  725                                 }
  726                                 kfree(rap, M_RR_ADDR);
  727                                 continue;
  728                         }
  729                         crit_enter();
  730                         LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
  731                         crit_exit();
  732                 }
  733         } else {
  734                 /*
  735                  * We got a fresh prefix.
  736                  */
  737                 /* create new prefix */
  738                 rpp = (struct rr_prefix *)kmalloc(sizeof(*rpp), M_IP6RR,
  739                                                  M_NOWAIT);
  740                 if (rpp == NULL) {
  741                         log(LOG_ERR, "in6_prefix.c: rrpr_update:%d"
  742                             ": ENOBUFS for rr_prefix\n", __LINE__);
  743                         return (ENOBUFS);
  744                 }
  745                 /* initilization */
  746                 *rpp = *new;
  747                 LIST_INIT(&rpp->rp_addrhead);
  748                 /*  move rp_addr entries of new to rpp */
  749                 while (!LIST_EMPTY(&new->rp_addrhead))
  750                 {
  751                         rap = LIST_FIRST(&new->rp_addrhead);
  752                         LIST_REMOVE(rap, ra_entry);
  753                         LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
  754                 }
  755 
  756                 /* let rp_ifpr.ifpr_prefix point rr_prefix. */
  757                 rpp->rp_ifpr.ifpr_prefix = (struct sockaddr *)&rpp->rp_prefix;
  758                 /* link rr_prefix entry to if_prefixlist */
  759                 {
  760                         struct ifnet *ifp = rpp->rp_ifp;
  761                         struct ifprefix *ifpr;
  762 
  763                         if ((ifpr = TAILQ_FIRST(&ifp->if_prefixhead))
  764                             != NULL) {
  765                                 for ( ; TAILQ_NEXT(ifpr, ifpr_list);
  766                                       ifpr = TAILQ_NEXT(ifpr, ifpr_list))
  767                                         continue;
  768                                 TAILQ_NEXT(ifpr, ifpr_list) = rp2ifpr(rpp);
  769                         } else
  770                                 TAILQ_FIRST(&ifp->if_prefixhead) =
  771                                         rp2ifpr(rpp);
  772                         rp2ifpr(rpp)->ifpr_type = IN6_PREFIX_RR;
  773                 }
  774                 /* link rr_prefix entry to rr_prefix list */
  775                 crit_enter();
  776                 LIST_INSERT_HEAD(&rr_prefix, rpp, rp_entry);
  777                 crit_exit();
  778         }
  779 
  780         if (!new->rp_raf_auto)
  781                 return 0;
  782 
  783         /*
  784          * Add an address for each interface id, if it is not yet
  785          * If it existed but not pointing to the prefix yet,
  786          * init the prefix pointer.
  787          */
  788         LIST_FOREACH(rap, &rpp->rp_addrhead, ra_entry)
  789         {
  790                 if (rap->ra_addr != NULL) {
  791                         if (rap->ra_addr->ia6_ifpr == NULL)
  792                                 rap->ra_addr->ia6_ifpr = rp2ifpr(rpp);
  793                         continue;
  794                 }
  795                 add_each_addr(so, rpp, rap);
  796         }
  797         return 0;
  798 }
  799 
  800 static int
  801 add_each_prefix(struct socket *so, struct rr_prefix *rpp)
  802 {
  803         init_prefix_ltimes(rpp);
  804         return (rrpr_update(so, rpp));
  805 }
  806 
  807 static void
  808 rp_remove(struct rr_prefix *rpp)
  809 {
  810         crit_enter();
  811         /* unlink rp_entry from if_prefixlist */
  812         {
  813                 struct ifnet *ifp = rpp->rp_ifp;
  814                 struct ifprefix *ifpr;
  815 
  816                 if ((ifpr = TAILQ_FIRST(&ifp->if_prefixhead)) == rp2ifpr(rpp))
  817                         TAILQ_FIRST(&ifp->if_prefixhead) =
  818                                 TAILQ_NEXT(ifpr, ifpr_list);
  819                 else {
  820                         while (TAILQ_NEXT(ifpr, ifpr_list) != NULL &&
  821                                (TAILQ_NEXT(ifpr, ifpr_list) != rp2ifpr(rpp)))
  822                                 ifpr = TAILQ_NEXT(ifpr, ifpr_list);
  823                         if (TAILQ_NEXT(ifpr, ifpr_list))
  824                                 TAILQ_NEXT(ifpr, ifpr_list) =
  825                                         TAILQ_NEXT(rp2ifpr(rpp), ifpr_list);
  826                         else
  827                                 kprintf("Couldn't unlink rr_prefix from ifp\n");
  828                 }
  829         }
  830         /* unlink rp_entry from rr_prefix list */
  831         LIST_REMOVE(rpp, rp_entry);
  832         crit_exit();
  833         kfree(rpp, M_IP6RR);
  834 }
  835 
  836 static int
  837 create_ra_entry(struct rp_addr **rapp)
  838 {
  839         *rapp = (struct rp_addr *)kmalloc(sizeof(struct rp_addr), M_RR_ADDR,
  840                                          M_NOWAIT | M_ZERO);
  841         if (*rapp == NULL) {
  842                 log(LOG_ERR, "in6_prefix.c: init_newprefix:%d: ENOBUFS"
  843                     "for rp_addr\n", __LINE__);
  844                 return ENOBUFS;
  845         }
  846 
  847         return 0;
  848 }
  849 
  850 static int
  851 init_newprefix(struct in6_rrenumreq *irr, struct ifprefix *ifpr,
  852                struct rr_prefix *rpp)
  853 {
  854         struct rp_addr *orap;
  855 
  856         /* init rp */
  857         bzero(rpp, sizeof(*rpp));
  858         rpp->rp_type = IN6_PREFIX_RR;
  859         rpp->rp_ifp = ifpr->ifpr_ifp;
  860         rpp->rp_plen = ifpr->ifpr_plen;
  861         rpp->rp_prefix.sin6_len = sizeof(rpp->rp_prefix);
  862         rpp->rp_prefix.sin6_family = AF_INET6;
  863         bit_copy((char *)RP_IN6(rpp), sizeof(*RP_IN6(rpp)) << 3,
  864                  (char *)&irr->irr_useprefix.sin6_addr,
  865                  sizeof(irr->irr_useprefix.sin6_addr) << 3,
  866                  0, irr->irr_u_uselen);
  867         /* copy keeplen part if necessary as necessary len */
  868         if (irr->irr_u_uselen < ifpr->ifpr_plen)
  869                 bit_copy((char *)RP_IN6(rpp), sizeof(*RP_IN6(rpp)) << 3,
  870                          (char *)IFPR_IN6(ifpr), sizeof(*IFPR_IN6(ifpr)) << 3,
  871                          irr->irr_u_uselen,
  872                          min(ifpr->ifpr_plen - irr->irr_u_uselen,
  873                              irr->irr_u_keeplen));
  874         LIST_FOREACH(orap, &(ifpr2rp(ifpr)->rp_addrhead), ra_entry)
  875         {
  876                 struct rp_addr *rap;
  877                 int error = 0;
  878 
  879                 if ((error = create_ra_entry(&rap)) != 0)
  880                         return error;
  881                 rap->ra_ifid = orap->ra_ifid;
  882                 rap->ra_flags.anycast = (orap->ra_addr != NULL &&
  883                                          (orap->ra_addr->ia6_flags &
  884                                           IN6_IFF_ANYCAST) != 0) ? 1 : 0;
  885                 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
  886         }
  887         rpp->rp_vltime = irr->irr_vltime;
  888         rpp->rp_pltime = irr->irr_pltime;
  889         rpp->rp_raf_onlink = irr->irr_raf_mask_onlink ? irr->irr_raf_onlink :
  890                 ifpr2rp(ifpr)->rp_raf_onlink;
  891         rpp->rp_raf_auto = irr->irr_raf_mask_auto ? irr->irr_raf_auto :
  892                 ifpr2rp(ifpr)->rp_raf_auto;
  893         /* Is some FlagMasks for rrf necessary? */
  894         rpp->rp_rrf = irr->irr_rrf;
  895         rpp->rp_origin = irr->irr_origin;
  896 
  897         return 0;
  898 }
  899 
  900 static void
  901 free_rp_entries(struct rr_prefix *rpp)
  902 {
  903         /*
  904          * This func is only called with rpp on stack(not on list).
  905          * So no crit_enter() here
  906          */
  907         while (!LIST_EMPTY(&rpp->rp_addrhead))
  908         {
  909                 struct rp_addr *rap;
  910 
  911                 rap = LIST_FIRST(&rpp->rp_addrhead);
  912                 LIST_REMOVE(rap, ra_entry);
  913                 if (rap->ra_addr) {
  914                         crit_enter();   /* XXX MP not MP safe */
  915                         _IFAFREE(&rap->ra_addr->ia_ifa, 0);
  916                         crit_exit();
  917                 }
  918                 kfree(rap, M_RR_ADDR);
  919         }
  920 }
  921 
  922 static int
  923 add_useprefixes(struct socket *so, struct ifnet *ifp,
  924                 struct in6_rrenumreq *irr)
  925 {
  926         struct ifprefix *ifpr, *nextifpr;
  927         struct rr_prefix rp;
  928         int error = 0;
  929 
  930         /* add prefixes to each of marked prefix */
  931         for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; ifpr = nextifpr)
  932         {
  933                 nextifpr = TAILQ_NEXT(ifpr, ifpr_list);
  934                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
  935                     ifpr->ifpr_type != IN6_PREFIX_RR)
  936                         continue;
  937                 if (ifpr2rp(ifpr)->rp_statef_addmark) {
  938                         if ((error = init_newprefix(irr, ifpr, &rp)) != 0)
  939                                 break;
  940                         error = add_each_prefix(so, &rp);
  941                 }
  942         }
  943         /* free each rp_addr entry */
  944         free_rp_entries(&rp);
  945 
  946         return error;
  947 }
  948 
  949 static void
  950 unprefer_prefix(struct rr_prefix *rpp)
  951 {
  952         struct rp_addr *rap;
  953 
  954         for (rap = rpp->rp_addrhead.lh_first; rap != NULL;
  955              rap = rap->ra_entry.le_next) {
  956                 if (rap->ra_addr == NULL)
  957                         continue;
  958                 rap->ra_addr->ia6_lifetime.ia6t_preferred = time_uptime;
  959                 rap->ra_addr->ia6_lifetime.ia6t_pltime = 0;
  960         }
  961 }
  962 
  963 int
  964 delete_each_prefix(struct rr_prefix *rpp, u_char origin)
  965 {
  966         int error = 0;
  967 
  968         if (rpp->rp_origin > origin)
  969                 return (EPERM);
  970 
  971         while (rpp->rp_addrhead.lh_first != NULL) {
  972                 struct rp_addr *rap;
  973 
  974                 crit_enter();
  975                 rap = LIST_FIRST(&rpp->rp_addrhead);
  976                 if (rap == NULL) {
  977                         crit_exit();
  978                         break;
  979                 }
  980                 LIST_REMOVE(rap, ra_entry);
  981                 crit_exit();
  982                 if (rap->ra_addr == NULL) {
  983                         kfree(rap, M_RR_ADDR);
  984                         continue;
  985                 }
  986                 rap->ra_addr->ia6_ifpr = NULL;
  987 
  988                 in6_purgeaddr(&rap->ra_addr->ia_ifa);
  989                 crit_enter();   /* XXX MP not MP safe */
  990                 _IFAFREE(&rap->ra_addr->ia_ifa, 0);
  991                 crit_exit();
  992                 kfree(rap, M_RR_ADDR);
  993         }
  994         rp_remove(rpp);
  995 
  996         return error;
  997 }
  998 
  999 static void
 1000 delete_prefixes(struct ifnet *ifp, u_char origin)
 1001 {
 1002         struct ifprefix *ifpr, *nextifpr;
 1003 
 1004         /* delete prefixes marked as tobe deleted */
 1005         for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; ifpr = nextifpr)
 1006         {
 1007                 nextifpr = TAILQ_NEXT(ifpr, ifpr_list);
 1008                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
 1009                     ifpr->ifpr_type != IN6_PREFIX_RR)
 1010                         continue;
 1011                 if (ifpr2rp(ifpr)->rp_statef_delmark)
 1012                         delete_each_prefix(ifpr2rp(ifpr), origin);
 1013         }
 1014 }
 1015 
 1016 static int
 1017 link_stray_ia6s(struct rr_prefix *rpp)
 1018 {
 1019         struct ifaddr_container *ifac;
 1020 
 1021         TAILQ_FOREACH(ifac, &rpp->rp_ifp->if_addrheads[mycpuid], ifa_link) {
 1022                 struct ifaddr *ifa = ifac->ifa;
 1023                 struct rp_addr *rap;
 1024                 struct rr_prefix *orpp;
 1025                 int error = 0;
 1026 
 1027                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1028                         continue;
 1029                 if (rpp->rp_plen > in6_matchlen(RP_IN6(rpp), IFA_IN6(ifa)))
 1030                         continue;
 1031 
 1032                 orpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr);
 1033                 if (orpp != NULL) {
 1034                         if (!in6_are_prefix_equal(RP_IN6(orpp), RP_IN6(rpp),
 1035                                                   rpp->rp_plen))
 1036                                 log(LOG_ERR, "in6_prefix.c: link_stray_ia6s:"
 1037                                     "addr %s/%d already linked to a prefix"
 1038                                     "and it matches also %s/%d\n",
 1039                                     ip6_sprintf(IFA_IN6(ifa)), orpp->rp_plen,
 1040                                     ip6_sprintf(RP_IN6(rpp)),
 1041                                     rpp->rp_plen);
 1042                         continue;
 1043                 }
 1044                 if ((error = assign_ra_entry(rpp,
 1045                                               (sizeof(rap->ra_ifid) << 3) -
 1046                                               rpp->rp_plen,
 1047                                               (struct in6_ifaddr *)ifa)) != 0)
 1048                         return error;
 1049         }
 1050         return 0;
 1051 }
 1052 
 1053 /* XXX assumes that permission is already checked by the caller */
 1054 int
 1055 in6_prefix_ioctl(struct socket *so, u_long cmd, caddr_t data,
 1056                  struct ifnet *ifp)
 1057 {
 1058         struct ifaddr_container *ifac;
 1059         struct rr_prefix *rpp, rp_tmp;
 1060         struct rp_addr *rap;
 1061         struct in6_prefixreq *ipr = (struct in6_prefixreq *)data;
 1062         struct in6_rrenumreq *irr = (struct in6_rrenumreq *)data;
 1063         int error = 0;
 1064 
 1065         /*
 1066          * Failsafe for errneous address config program.
 1067          * Let's hope rrenumd don't make a mistakes.
 1068          */
 1069         if (ipr->ipr_origin <= PR_ORIG_RA)
 1070                 ipr->ipr_origin = PR_ORIG_STATIC;
 1071 
 1072         switch (cmd) {
 1073         case SIOCSGIFPREFIX_IN6:
 1074                 delmark_global_prefixes(ifp, irr);
 1075                 /* FALL THROUGH */
 1076         case SIOCAIFPREFIX_IN6:
 1077         case SIOCCIFPREFIX_IN6:
 1078                 /* check if preferred lifetime > valid lifetime */
 1079                 if (irr->irr_pltime > irr->irr_vltime) {
 1080                         log(LOG_NOTICE,
 1081                             "in6_prefix_ioctl: preferred lifetime"
 1082                             "(%ld) is greater than valid lifetime(%ld)\n",
 1083                             (u_long)irr->irr_pltime, (u_long)irr->irr_vltime);
 1084                         error = EINVAL;
 1085                         break;
 1086                 }
 1087                 if (mark_matched_prefixes(cmd, ifp, irr)) {
 1088                         if (irr->irr_u_uselen != 0)
 1089                                 if ((error = add_useprefixes(so, ifp, irr))
 1090                                     != 0)
 1091                                         goto failed;
 1092                         if (cmd != SIOCAIFPREFIX_IN6)
 1093                                 delete_prefixes(ifp, irr->irr_origin);
 1094                 } else
 1095                         return (EADDRNOTAVAIL);
 1096         failed:
 1097                 unmark_prefixes(ifp);
 1098                 break;
 1099         case SIOCGIFPREFIX_IN6:
 1100                 rpp = search_matched_prefix(ifp, ipr);
 1101                 if (rpp == NULL || ifp != rpp->rp_ifp)
 1102                         return (EADDRNOTAVAIL);
 1103 
 1104                 ipr->ipr_origin = rpp->rp_origin;
 1105                 ipr->ipr_plen = rpp->rp_plen;
 1106                 ipr->ipr_vltime = rpp->rp_vltime;
 1107                 ipr->ipr_pltime = rpp->rp_pltime;
 1108                 ipr->ipr_flags = rpp->rp_flags;
 1109                 ipr->ipr_prefix = rpp->rp_prefix;
 1110 
 1111                 break;
 1112         case SIOCSIFPREFIX_IN6:
 1113                 /* check if preferred lifetime > valid lifetime */
 1114                 if (ipr->ipr_pltime > ipr->ipr_vltime) {
 1115                         log(LOG_NOTICE,
 1116                             "in6_prefix_ioctl: preferred lifetime"
 1117                             "(%ld) is greater than valid lifetime(%ld)\n",
 1118                             (u_long)ipr->ipr_pltime, (u_long)ipr->ipr_vltime);
 1119                         error = EINVAL;
 1120                         break;
 1121                 }
 1122 
 1123                 /* init rp_tmp */
 1124                 bzero((caddr_t)&rp_tmp, sizeof(rp_tmp));
 1125                 rp_tmp.rp_ifp = ifp;
 1126                 rp_tmp.rp_plen = ipr->ipr_plen;
 1127                 rp_tmp.rp_prefix = ipr->ipr_prefix;
 1128                 rp_tmp.rp_vltime = ipr->ipr_vltime;
 1129                 rp_tmp.rp_pltime = ipr->ipr_pltime;
 1130                 rp_tmp.rp_flags = ipr->ipr_flags;
 1131                 rp_tmp.rp_origin = ipr->ipr_origin;
 1132 
 1133                 /* create rp_addr entries, usually at least for lladdr */
 1134                 if ((error = link_stray_ia6s(&rp_tmp)) != 0) {
 1135                         free_rp_entries(&rp_tmp);
 1136                         break;
 1137                 }
 1138                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_list) {
 1139                         struct ifaddr *ifa = ifac->ifa;
 1140 
 1141                         if (ifa->ifa_addr == NULL)
 1142                                 continue;       /* just for safety */
 1143                         if (ifa->ifa_addr->sa_family != AF_INET6)
 1144                                 continue;
 1145                         if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa)) == 0)
 1146                                 continue;
 1147 
 1148                         if ((error = create_ra_entry(&rap)) != 0) {
 1149                                 free_rp_entries(&rp_tmp);
 1150                                 goto bad;
 1151                         }
 1152                         /* copy interface id part */
 1153                         bit_copy((caddr_t)&rap->ra_ifid,
 1154                                  sizeof(rap->ra_ifid) << 3,
 1155                                  (caddr_t)IFA_IN6(ifa),
 1156                                  sizeof(*IFA_IN6(ifa)) << 3,
 1157                                  rp_tmp.rp_plen,
 1158                                  (sizeof(rap->ra_ifid) << 3) - rp_tmp.rp_plen);
 1159                         /* insert into list */
 1160                         LIST_INSERT_HEAD(&rp_tmp.rp_addrhead, rap, ra_entry);
 1161                 }
 1162 
 1163                 error = add_each_prefix(so, &rp_tmp);
 1164 
 1165                 /* free each rp_addr entry */
 1166                 free_rp_entries(&rp_tmp);
 1167 
 1168                 break;
 1169         case SIOCDIFPREFIX_IN6:
 1170                 rpp = search_matched_prefix(ifp, ipr);
 1171                 if (rpp == NULL || ifp != rpp->rp_ifp)
 1172                         return (EADDRNOTAVAIL);
 1173 
 1174                 error = delete_each_prefix(rpp, ipr->ipr_origin);
 1175                 break;
 1176         }
 1177 bad:
 1178         return error;
 1179 }
 1180 
 1181 void
 1182 in6_rr_timer(void *ignored_arg)
 1183 {
 1184         struct rr_prefix *rpp;
 1185 
 1186         callout_reset(&in6_rr_timer_ch, ip6_rr_prune * hz,
 1187             in6_rr_timer, NULL);
 1188 
 1189         crit_enter();
 1190         /* expire */
 1191         rpp = LIST_FIRST(&rr_prefix);
 1192         while (rpp) {
 1193                 if (rpp->rp_expire && rpp->rp_expire < time_uptime) {
 1194                         struct rr_prefix *next_rpp;
 1195 
 1196                         next_rpp = LIST_NEXT(rpp, rp_entry);
 1197                         delete_each_prefix(rpp, PR_ORIG_KERNEL);
 1198                         rpp = next_rpp;
 1199                         continue;
 1200                 }
 1201                 if (rpp->rp_preferred && rpp->rp_preferred < time_uptime)
 1202                         unprefer_prefix(rpp);
 1203                 rpp = LIST_NEXT(rpp, rp_entry);
 1204         }
 1205         crit_exit();
 1206 }

Cache object: 72417ade569391974e168824a1c4d2b0


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