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