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