well, if netproto doesnt need old prototypes inet6 doesnt either
[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.4 2003/08/23 11:02:45 rob 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
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 *ifa;
181         struct rr_prefix *rpp;
182
183         /* search matched prefix */
184         ifpr = in6_prefixwithifp(ifp, ipr->ipr_plen,
185                                  &ipr->ipr_prefix.sin6_addr);
186         if (ifpr != NULL)
187                 return ifpr2rp(ifpr);
188
189         /*
190          * search matched addr, and then search prefix
191          * which matches the addr
192          */
193
194         TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
195         {
196                 if (ifa->ifa_addr->sa_family != AF_INET6)
197                         continue;
198                 if (ipr->ipr_plen <=
199                     in6_matchlen(&ipr->ipr_prefix.sin6_addr, IFA_IN6(ifa)))
200                         break;
201         }
202         if (ifa == NULL)
203                 return NULL;
204
205         rpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr);
206         if (rpp != 0)
207                 return rpp;
208
209         for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
210              ifpr = TAILQ_NEXT(ifpr, ifpr_list))
211         {
212                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
213                     ifpr->ifpr_type != IN6_PREFIX_RR)
214                         continue;
215                 if (ifpr->ifpr_plen <= in6_matchlen(IFA_IN6(ifa),
216                                                     IFPR_IN6(ifpr)))
217                         break;
218         }
219         if (ifpr != NULL)
220                 log(LOG_ERR,  "in6_prefix.c: search_matched_prefix: addr %s"
221                     "has no pointer to prefix %s\n", ip6_sprintf(IFA_IN6(ifa)),
222                     ip6_sprintf(IFPR_IN6(ifpr)));
223         return ifpr2rp(ifpr);
224 }
225
226 /*
227  * Search prefix which matches arg prefix as specified in
228  * draft-ietf-ipngwg-router-renum-08.txt, and mark it if exists.
229  * Return 1 if anything matched, and 0 if nothing matched.
230  */
231 static int
232 mark_matched_prefixes(u_long cmd, struct ifnet *ifp, struct in6_rrenumreq *irr)
233 {
234         struct ifprefix *ifpr;
235         struct ifaddr *ifa;
236         int matchlen, matched = 0;
237
238         /* search matched prefixes */
239         for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
240              ifpr = TAILQ_NEXT(ifpr, ifpr_list))
241         {
242                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
243                     ifpr->ifpr_type != IN6_PREFIX_RR)
244                         continue;
245                 matchlen = in6_matchlen(&irr->irr_matchprefix.sin6_addr,
246                                         IFPR_IN6(ifpr));
247                 if (irr->irr_m_minlen > ifpr->ifpr_plen ||
248                     irr->irr_m_maxlen < ifpr->ifpr_plen ||
249                     irr->irr_m_len > matchlen)
250                         continue;
251                 matched = 1;
252                 ifpr2rp(ifpr)->rp_statef_addmark = 1;
253                 if (cmd == SIOCCIFPREFIX_IN6)
254                         ifpr2rp(ifpr)->rp_statef_delmark = 1;
255         }
256
257         /*
258          * search matched addr, and then search prefixes
259          * which matche the addr
260          */
261         TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
262         {
263                 struct rr_prefix *rpp;
264
265                 if (ifa->ifa_addr->sa_family != AF_INET6)
266                         continue;
267                 matchlen = in6_matchlen(&irr->irr_matchprefix.sin6_addr,
268                                         IFA_IN6(ifa));
269                 if (irr->irr_m_minlen > matchlen ||
270                     irr->irr_m_maxlen < matchlen || irr->irr_m_len > matchlen)
271                         continue;
272                 rpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr);
273                 if (rpp != 0) {
274                         matched = 1;
275                         rpp->rp_statef_addmark = 1;
276                         if (cmd == SIOCCIFPREFIX_IN6)
277                                 rpp->rp_statef_delmark = 1;
278                 } else
279                         log(LOG_WARNING, "in6_prefix.c: mark_matched_prefixes:"
280                             "no back pointer to ifprefix for %s. "
281                             "ND autoconfigured addr?\n",
282                             ip6_sprintf(IFA_IN6(ifa)));
283         }
284         return matched;
285 }
286
287 /*
288  * Mark global prefixes as to be deleted.
289  */
290 static void
291 delmark_global_prefixes(struct ifnet *ifp, struct in6_rrenumreq *irr)
292 {
293         struct ifprefix *ifpr;
294
295         /* search matched prefixes */
296         for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
297              ifpr = TAILQ_NEXT(ifpr, ifpr_list))
298         {
299                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
300                     ifpr->ifpr_type != IN6_PREFIX_RR)
301                         continue;
302                 /* mark delete global prefix */
303                 if (in6_addrscope(RP_IN6(ifpr2rp(ifpr))) ==
304                     IPV6_ADDR_SCOPE_GLOBAL)
305                         ifpr2rp(ifpr)->rp_statef_delmark = 1;
306         }
307 }
308
309 /* Unmark prefixes */
310 static void
311 unmark_prefixes(struct ifnet *ifp)
312 {
313         struct ifprefix *ifpr;
314
315         /* unmark all prefix */
316         for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
317              ifpr = TAILQ_NEXT(ifpr, ifpr_list))
318         {
319                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
320                     ifpr->ifpr_type != IN6_PREFIX_RR)
321                         continue;
322                 /* unmark prefix */
323                 ifpr2rp(ifpr)->rp_statef_addmark = 0;
324                 ifpr2rp(ifpr)->rp_statef_delmark = 0;
325         }
326 }
327
328 static void
329 init_prefix_ltimes(struct rr_prefix *rpp)
330 {
331
332         if (rpp->rp_pltime == RR_INFINITE_LIFETIME ||
333             rpp->rp_rrf_decrprefd == 0)
334                 rpp->rp_preferred = 0;
335         else
336                 rpp->rp_preferred = time_second + rpp->rp_pltime;
337         if (rpp->rp_vltime == RR_INFINITE_LIFETIME ||
338             rpp->rp_rrf_decrvalid == 0)
339                 rpp->rp_expire = 0;
340         else
341                 rpp->rp_expire = time_second + rpp->rp_vltime;
342 }
343
344 static int
345 rr_are_ifid_equal(struct in6_addr *ii1, struct in6_addr *ii2, int ii_len)
346 {
347         int ii_bytelen, ii_bitlen;
348         int p_bytelen, p_bitlen;
349
350         /* sanity check */
351         if (1 > ii_len ||
352             ii_len > 124) { /* as RFC2373, prefix is at least 4 bit */
353                 log(LOG_ERR, "rr_are_ifid_equal: invalid ifid length(%d)\n",
354                     ii_len);
355                 return(0);
356         }
357
358         ii_bytelen = ii_len / 8;
359         ii_bitlen = ii_len % 8;
360
361         p_bytelen = sizeof(struct in6_addr) - ii_bytelen - 1;
362         p_bitlen = 8 - ii_bitlen;
363
364         if (bcmp(ii1->s6_addr + p_bytelen + 1, ii2->s6_addr + p_bytelen + 1,
365                  ii_bytelen))
366                 return(0);
367         if (((ii1->s6_addr[p_bytelen] << p_bitlen) & 0xff) !=
368             ((ii2->s6_addr[p_bytelen] << p_bitlen) & 0xff))
369                 return(0);
370
371         return(1);
372 }
373
374 static struct rp_addr *
375 search_ifidwithprefix(struct rr_prefix *rpp, struct in6_addr *ifid)
376 {
377         struct rp_addr *rap;
378
379         LIST_FOREACH(rap, &rpp->rp_addrhead, ra_entry)
380         {
381                 if (rr_are_ifid_equal(ifid, &rap->ra_ifid,
382                                       (sizeof(struct in6_addr) << 3) -
383                                       rpp->rp_plen))
384                         break;
385         }
386         return rap;
387 }
388
389 static int
390 assign_ra_entry(struct rr_prefix *rpp, int iilen, struct in6_ifaddr *ia)
391 {
392         int error = 0;
393         struct rp_addr *rap;
394         int s;
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         s = splnet();
410         LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
411         splx(s);
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, s;
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                 s = splnet();
446                 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
447                 splx(s);
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                 int s = splnet();
549                 LIST_REMOVE(rap, ra_entry);
550                 splx(s);
551                 if (rap->ra_addr)
552                         IFAFREE(&rap->ra_addr->ia_ifa);
553                 free(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(ifp)
562         struct ifnet *ifp;
563 {
564         struct ifprefix *ifpr, *nextifpr;
565
566         /* delete prefixes before ifnet goes away */
567         for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr;
568              ifpr = nextifpr)
569         {
570                 nextifpr = TAILQ_NEXT(ifpr, ifpr_list);
571                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
572                     ifpr->ifpr_type != IN6_PREFIX_RR)
573                         continue;
574                 (void)delete_each_prefix(ifpr2rp(ifpr), PR_ORIG_KERNEL);
575         }
576 }
577
578 static void
579 add_each_addr(struct socket *so, struct rr_prefix *rpp, struct rp_addr *rap)
580 {
581         struct in6_ifaddr *ia6;
582         struct in6_aliasreq ifra;
583         int error;
584
585         /* init ifra */
586         bzero(&ifra, sizeof(ifra));
587         strncpy(ifra.ifra_name, if_name(rpp->rp_ifp), sizeof(ifra.ifra_name));
588         ifra.ifra_addr.sin6_family = ifra.ifra_prefixmask.sin6_family =
589                 AF_INET6;
590         ifra.ifra_addr.sin6_len = ifra.ifra_prefixmask.sin6_len =
591                 sizeof(ifra.ifra_addr);
592         /* copy prefix part */
593         bit_copy((char *)&ifra.ifra_addr.sin6_addr,
594                  sizeof(ifra.ifra_addr.sin6_addr) << 3,
595                  (char *)RP_IN6(rpp), sizeof(*RP_IN6(rpp)) << 3,
596                  0, rpp->rp_plen);
597         /* copy interface id part */
598         bit_copy((char *)&ifra.ifra_addr.sin6_addr,
599                  sizeof(ifra.ifra_addr.sin6_addr) << 3,
600                  (char *)&rap->ra_ifid, sizeof(rap->ra_ifid) << 3,
601                  rpp->rp_plen, (sizeof(rap->ra_ifid) << 3) - rpp->rp_plen);
602         in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, rpp->rp_plen);
603         /* don't care ifra_flags for now */
604
605         /*
606          * XXX: if we did this with finite lifetime values, the lifetimes would
607          *      decrese in time and never incremented.
608          *      we should need more clarifications on the prefix mechanism...
609          */
610         ifra.ifra_lifetime.ia6t_vltime = rpp->rp_vltime;
611         ifra.ifra_lifetime.ia6t_pltime = rpp->rp_pltime;
612
613         ia6 = in6ifa_ifpwithaddr(rpp->rp_ifp, &ifra.ifra_addr.sin6_addr);
614         if (ia6 != NULL) {
615                 if (ia6->ia6_ifpr == NULL) {
616                         /* link this addr and the prefix each other */
617                         if (rap->ra_addr)
618                                 IFAFREE(&rap->ra_addr->ia_ifa);
619                         rap->ra_addr = ia6;
620                         IFAREF(&rap->ra_addr->ia_ifa);
621                         ia6->ia6_ifpr = rp2ifpr(rpp);
622                         return;
623                 }
624                 if (ia6->ia6_ifpr == rp2ifpr(rpp)) {
625                         if (rap->ra_addr)
626                                 IFAFREE(&rap->ra_addr->ia_ifa);
627                         rap->ra_addr = ia6;
628                         IFAREF(&rap->ra_addr->ia_ifa);
629                         return;
630                 }
631                 /*
632                  * The addr is already assigned to other
633                  * prefix.
634                  * There may be some inconsistencies between
635                  * prefixes.
636                  * e.g. overraped prefixes with common starting
637                  *      part and different plefixlen.
638                  *      Or, completely duplicated prefixes?
639                  * log it and return.
640                  */
641                 log(LOG_ERR,
642                     "in6_prefix.c: add_each_addr: addition of an addr %s/%d "
643                     "failed because there is already another addr %s/%d\n",
644                     ip6_sprintf(&ifra.ifra_addr.sin6_addr), rpp->rp_plen,
645                     ip6_sprintf(IA6_IN6(ia6)),
646                     in6_mask2len(&ia6->ia_prefixmask.sin6_addr, NULL));
647                 return;
648         }
649         /* propagate ANYCAST flag if it is set for ancestor addr */
650         if (rap->ra_flags.anycast != 0)
651                 ifra.ifra_flags |= IN6_IFF_ANYCAST;
652         error = in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, rpp->rp_ifp,
653                             curthread);
654         if (error != 0) {
655                 log(LOG_ERR, "in6_prefix.c: add_each_addr: addition of an addr"
656                     "%s/%d failed because in6_control failed for error %d\n",
657                     ip6_sprintf(&ifra.ifra_addr.sin6_addr), rpp->rp_plen,
658                     error);
659                 return;
660         }
661
662         /*
663          * link beween this addr and the prefix will be done
664          * in in6_prefix_add_ifid
665          */
666 }
667
668 static int
669 rrpr_update(struct socket *so, struct rr_prefix *new)
670 {
671         struct rr_prefix *rpp;
672         struct ifprefix *ifpr;
673         struct rp_addr *rap;
674         int s;
675
676         /* search existing prefix */
677         for (ifpr = TAILQ_FIRST(&new->rp_ifp->if_prefixhead); ifpr;
678              ifpr = TAILQ_NEXT(ifpr, ifpr_list))
679         {
680                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
681                     ifpr->ifpr_type != IN6_PREFIX_RR)
682                         continue;
683                 if (ifpr->ifpr_plen == new->rp_plen &&
684                     in6_are_prefix_equal(IFPR_IN6(ifpr), RP_IN6(new),
685                                          ifpr->ifpr_plen))
686                         break;
687         }
688         rpp = ifpr2rp(ifpr);
689         if (rpp != NULL) {
690                 /*
691                  * We got a prefix which we have seen in the past.
692                  */
693                 /*
694                  * If the origin of the already-installed prefix is more
695                  * preferable than the new one, ignore installation request.
696                  */
697                 if (rpp->rp_origin > new->rp_origin)
698                         return(EPERM);
699
700                 /* update prefix information */
701                 rpp->rp_flags.prf_ra = new->rp_flags.prf_ra;
702                 if (rpp->rp_origin >= PR_ORIG_RR)
703                         rpp->rp_flags.prf_rr = new->rp_flags.prf_rr;
704                 rpp->rp_vltime = new->rp_vltime;
705                 rpp->rp_pltime = new->rp_pltime;
706                 rpp->rp_expire = new->rp_expire;
707                 rpp->rp_preferred = new->rp_preferred;
708                 rpp->rp_statef_delmark = 0; /* cancel deletion */
709                 /*
710                  * Interface id related update.
711                  *  add rp_addr entries in new into rpp, if they have not
712                  *  been already included in rpp.
713                  */
714                 while (!LIST_EMPTY(&new->rp_addrhead))
715                 {
716                         rap = LIST_FIRST(&new->rp_addrhead);
717                         LIST_REMOVE(rap, ra_entry);
718                         if (search_ifidwithprefix(rpp, &rap->ra_ifid)
719                             != NULL) {
720                                 if (rap->ra_addr)
721                                         IFAFREE(&rap->ra_addr->ia_ifa);
722                                 free(rap, M_RR_ADDR);
723                                 continue;
724                         }
725                         s = splnet();
726                         LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
727                         splx(s);
728                 }
729         } else {
730                 /*
731                  * We got a fresh prefix.
732                  */
733                 /* create new prefix */
734                 rpp = (struct rr_prefix *)malloc(sizeof(*rpp), M_IP6RR,
735                                                  M_NOWAIT);
736                 if (rpp == NULL) {
737                         log(LOG_ERR, "in6_prefix.c: rrpr_update:%d"
738                             ": ENOBUFS for rr_prefix\n", __LINE__);
739                         return(ENOBUFS);
740                 }
741                 /* initilization */
742                 *rpp = *new;
743                 LIST_INIT(&rpp->rp_addrhead);
744                 /*  move rp_addr entries of new to rpp */
745                 while (!LIST_EMPTY(&new->rp_addrhead))
746                 {
747                         rap = LIST_FIRST(&new->rp_addrhead);
748                         LIST_REMOVE(rap, ra_entry);
749                         LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
750                 }
751
752                 /* let rp_ifpr.ifpr_prefix point rr_prefix. */
753                 rpp->rp_ifpr.ifpr_prefix = (struct sockaddr *)&rpp->rp_prefix;
754                 /* link rr_prefix entry to if_prefixlist */
755                 {
756                         struct ifnet *ifp = rpp->rp_ifp;
757                         struct ifprefix *ifpr;
758
759                         if ((ifpr = TAILQ_FIRST(&ifp->if_prefixhead))
760                             != NULL) {
761                                 for ( ; TAILQ_NEXT(ifpr, ifpr_list);
762                                       ifpr = TAILQ_NEXT(ifpr, ifpr_list))
763                                         continue;
764                                 TAILQ_NEXT(ifpr, ifpr_list) = rp2ifpr(rpp);
765                         } else
766                                 TAILQ_FIRST(&ifp->if_prefixhead) =
767                                         rp2ifpr(rpp);
768                         rp2ifpr(rpp)->ifpr_type = IN6_PREFIX_RR;
769                 }
770                 /* link rr_prefix entry to rr_prefix list */
771                 s = splnet();
772                 LIST_INSERT_HEAD(&rr_prefix, rpp, rp_entry);
773                 splx(s);
774         }
775
776         if (!new->rp_raf_auto)
777                 return 0;
778
779         /*
780          * Add an address for each interface id, if it is not yet
781          * If it existed but not pointing to the prefix yet,
782          * init the prefix pointer.
783          */
784         LIST_FOREACH(rap, &rpp->rp_addrhead, ra_entry)
785         {
786                 if (rap->ra_addr != NULL) {
787                         if (rap->ra_addr->ia6_ifpr == NULL)
788                                 rap->ra_addr->ia6_ifpr = rp2ifpr(rpp);
789                         continue;
790                 }
791                 add_each_addr(so, rpp, rap);
792         }
793         return 0;
794 }
795
796 static int
797 add_each_prefix(struct socket *so, struct rr_prefix *rpp)
798 {
799         init_prefix_ltimes(rpp);
800         return(rrpr_update(so, rpp));
801 }
802
803 static void
804 rp_remove(struct rr_prefix *rpp)
805 {
806         int s;
807
808         s = splnet();
809         /* unlink rp_entry from if_prefixlist */
810         {
811                 struct ifnet *ifp = rpp->rp_ifp;
812                 struct ifprefix *ifpr;
813
814                 if ((ifpr = TAILQ_FIRST(&ifp->if_prefixhead)) == rp2ifpr(rpp))
815                         TAILQ_FIRST(&ifp->if_prefixhead) =
816                                 TAILQ_NEXT(ifpr, ifpr_list);
817                 else {
818                         while (TAILQ_NEXT(ifpr, ifpr_list) != NULL &&
819                                (TAILQ_NEXT(ifpr, ifpr_list) != rp2ifpr(rpp)))
820                                 ifpr = TAILQ_NEXT(ifpr, ifpr_list);
821                         if (TAILQ_NEXT(ifpr, ifpr_list))
822                                 TAILQ_NEXT(ifpr, ifpr_list) =
823                                         TAILQ_NEXT(rp2ifpr(rpp), ifpr_list);
824                         else
825                                 printf("Couldn't unlink rr_prefix from ifp\n");
826                 }
827         }
828         /* unlink rp_entry from rr_prefix list */
829         LIST_REMOVE(rpp, rp_entry);
830         splx(s);
831         free(rpp, M_IP6RR);
832 }
833
834 static int
835 create_ra_entry(struct rp_addr **rapp)
836 {
837         *rapp = (struct rp_addr *)malloc(sizeof(struct rp_addr), M_RR_ADDR,
838                                          M_NOWAIT);
839         if (*rapp == NULL) {
840                 log(LOG_ERR, "in6_prefix.c: init_newprefix:%d: ENOBUFS"
841                     "for rp_addr\n", __LINE__);
842                 return ENOBUFS;
843         }
844         bzero(*rapp, sizeof(*(*rapp)));
845
846         return 0;
847 }
848
849 static int
850 init_newprefix(struct in6_rrenumreq *irr, struct ifprefix *ifpr,
851                struct rr_prefix *rpp)
852 {
853         struct rp_addr *orap;
854
855         /* init rp */
856         bzero(rpp, sizeof(*rpp));
857         rpp->rp_type = IN6_PREFIX_RR;
858         rpp->rp_ifp = ifpr->ifpr_ifp;
859         rpp->rp_plen = ifpr->ifpr_plen;
860         rpp->rp_prefix.sin6_len = sizeof(rpp->rp_prefix);
861         rpp->rp_prefix.sin6_family = AF_INET6;
862         bit_copy((char *)RP_IN6(rpp), sizeof(*RP_IN6(rpp)) << 3,
863                  (char *)&irr->irr_useprefix.sin6_addr,
864                  sizeof(irr->irr_useprefix.sin6_addr) << 3,
865                  0, irr->irr_u_uselen);
866         /* copy keeplen part if necessary as necessary len */
867         if (irr->irr_u_uselen < ifpr->ifpr_plen)
868                 bit_copy((char *)RP_IN6(rpp), sizeof(*RP_IN6(rpp)) << 3,
869                          (char *)IFPR_IN6(ifpr), sizeof(*IFPR_IN6(ifpr)) << 3,
870                          irr->irr_u_uselen,
871                          min(ifpr->ifpr_plen - irr->irr_u_uselen,
872                              irr->irr_u_keeplen));
873         LIST_FOREACH(orap, &(ifpr2rp(ifpr)->rp_addrhead), ra_entry)
874         {
875                 struct rp_addr *rap;
876                 int error = 0;
877
878                 if ((error = create_ra_entry(&rap)) != 0)
879                         return error;
880                 rap->ra_ifid = orap->ra_ifid;
881                 rap->ra_flags.anycast = (orap->ra_addr != NULL &&
882                                          (orap->ra_addr->ia6_flags &
883                                           IN6_IFF_ANYCAST) != 0) ? 1 : 0;
884                 LIST_INSERT_HEAD(&rpp->rp_addrhead, rap, ra_entry);
885         }
886         rpp->rp_vltime = irr->irr_vltime;
887         rpp->rp_pltime = irr->irr_pltime;
888         rpp->rp_raf_onlink = irr->irr_raf_mask_onlink ? irr->irr_raf_onlink :
889                 ifpr2rp(ifpr)->rp_raf_onlink;
890         rpp->rp_raf_auto = irr->irr_raf_mask_auto ? irr->irr_raf_auto :
891                 ifpr2rp(ifpr)->rp_raf_auto;
892         /* Is some FlagMasks for rrf necessary? */
893         rpp->rp_rrf = irr->irr_rrf;
894         rpp->rp_origin = irr->irr_origin;
895
896         return 0;
897 }
898
899 static void
900 free_rp_entries(struct rr_prefix *rpp)
901 {
902         /*
903          * This func is only called with rpp on stack(not on list).
904          * So no splnet() here
905          */
906         while (!LIST_EMPTY(&rpp->rp_addrhead))
907         {
908                 struct rp_addr *rap;
909
910                 rap = LIST_FIRST(&rpp->rp_addrhead);
911                 LIST_REMOVE(rap, ra_entry);
912                 if (rap->ra_addr)
913                         IFAFREE(&rap->ra_addr->ia_ifa);
914                 free(rap, M_RR_ADDR);
915         }
916 }
917
918 static int
919 add_useprefixes(struct socket *so, struct ifnet *ifp,
920                 struct in6_rrenumreq *irr)
921 {
922         struct ifprefix *ifpr, *nextifpr;
923         struct rr_prefix rp;
924         int error = 0;
925
926         /* add prefixes to each of marked prefix */
927         for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; ifpr = nextifpr)
928         {
929                 nextifpr = TAILQ_NEXT(ifpr, ifpr_list);
930                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
931                     ifpr->ifpr_type != IN6_PREFIX_RR)
932                         continue;
933                 if (ifpr2rp(ifpr)->rp_statef_addmark) {
934                         if ((error = init_newprefix(irr, ifpr, &rp)) != 0)
935                                 break;
936                         error = add_each_prefix(so, &rp);
937                 }
938         }
939         /* free each rp_addr entry */
940         free_rp_entries(&rp);
941
942         return error;
943 }
944
945 static void
946 unprefer_prefix(struct rr_prefix *rpp)
947 {
948         struct rp_addr *rap;
949
950         for (rap = rpp->rp_addrhead.lh_first; rap != NULL;
951              rap = rap->ra_entry.le_next) {
952                 if (rap->ra_addr == NULL)
953                         continue;
954                 rap->ra_addr->ia6_lifetime.ia6t_preferred = time_second;
955                 rap->ra_addr->ia6_lifetime.ia6t_pltime = 0;
956         }
957 }
958
959 int
960 delete_each_prefix(struct rr_prefix *rpp, u_char origin)
961 {
962         int error = 0;
963
964         if (rpp->rp_origin > origin)
965                 return(EPERM);
966
967         while (rpp->rp_addrhead.lh_first != NULL) {
968                 struct rp_addr *rap;
969                 int s;
970
971                 s = splnet();
972                 rap = LIST_FIRST(&rpp->rp_addrhead);
973                 if (rap == NULL) {
974                         splx(s);
975                         break;
976                 }
977                 LIST_REMOVE(rap, ra_entry);
978                 splx(s);
979                 if (rap->ra_addr == NULL) {
980                         free(rap, M_RR_ADDR);
981                         continue;
982                 }
983                 rap->ra_addr->ia6_ifpr = NULL;
984
985                 in6_purgeaddr(&rap->ra_addr->ia_ifa);
986                 IFAFREE(&rap->ra_addr->ia_ifa);
987                 free(rap, M_RR_ADDR);
988         }
989         rp_remove(rpp);
990
991         return error;
992 }
993
994 static void
995 delete_prefixes(struct ifnet *ifp, u_char origin)
996 {
997         struct ifprefix *ifpr, *nextifpr;
998
999         /* delete prefixes marked as tobe deleted */
1000         for (ifpr = TAILQ_FIRST(&ifp->if_prefixhead); ifpr; ifpr = nextifpr)
1001         {
1002                 nextifpr = TAILQ_NEXT(ifpr, ifpr_list);
1003                 if (ifpr->ifpr_prefix->sa_family != AF_INET6 ||
1004                     ifpr->ifpr_type != IN6_PREFIX_RR)
1005                         continue;
1006                 if (ifpr2rp(ifpr)->rp_statef_delmark)
1007                         (void)delete_each_prefix(ifpr2rp(ifpr), origin);
1008         }
1009 }
1010
1011 static int
1012 link_stray_ia6s(struct rr_prefix *rpp)
1013 {
1014         struct ifaddr *ifa;
1015
1016         for (ifa = rpp->rp_ifp->if_addrlist.tqh_first; ifa;
1017              ifa = ifa->ifa_list.tqe_next)
1018         {
1019                 struct rp_addr *rap;
1020                 struct rr_prefix *orpp;
1021                 int error = 0;
1022
1023                 if (ifa->ifa_addr->sa_family != AF_INET6)
1024                         continue;
1025                 if (rpp->rp_plen > in6_matchlen(RP_IN6(rpp), IFA_IN6(ifa)))
1026                         continue;
1027
1028                 orpp = ifpr2rp(((struct in6_ifaddr *)ifa)->ia6_ifpr);
1029                 if (orpp != NULL) {
1030                         if (!in6_are_prefix_equal(RP_IN6(orpp), RP_IN6(rpp),
1031                                                   rpp->rp_plen))
1032                                 log(LOG_ERR, "in6_prefix.c: link_stray_ia6s:"
1033                                     "addr %s/%d already linked to a prefix"
1034                                     "and it matches also %s/%d\n",
1035                                     ip6_sprintf(IFA_IN6(ifa)), orpp->rp_plen,
1036                                     ip6_sprintf(RP_IN6(rpp)),
1037                                     rpp->rp_plen);
1038                         continue;
1039                 }
1040                 if ((error = assign_ra_entry(rpp,
1041                                               (sizeof(rap->ra_ifid) << 3) -
1042                                               rpp->rp_plen,
1043                                               (struct in6_ifaddr *)ifa)) != 0)
1044                         return error;
1045         }
1046         return 0;
1047 }
1048
1049 /* XXX assumes that permission is already checked by the caller */
1050 int
1051 in6_prefix_ioctl(struct socket *so, u_long cmd, caddr_t data,
1052                  struct ifnet *ifp)
1053 {
1054         struct rr_prefix *rpp, rp_tmp;
1055         struct rp_addr *rap;
1056         struct in6_prefixreq *ipr = (struct in6_prefixreq *)data;
1057         struct in6_rrenumreq *irr = (struct in6_rrenumreq *)data;
1058         struct ifaddr *ifa;
1059         int error = 0;
1060
1061         /*
1062          * Failsafe for errneous address config program.
1063          * Let's hope rrenumd don't make a mistakes.
1064          */
1065         if (ipr->ipr_origin <= PR_ORIG_RA)
1066                 ipr->ipr_origin = PR_ORIG_STATIC;
1067
1068         switch (cmd) {
1069         case SIOCSGIFPREFIX_IN6:
1070                 delmark_global_prefixes(ifp, irr);
1071                 /* FALL THROUGH */
1072         case SIOCAIFPREFIX_IN6:
1073         case SIOCCIFPREFIX_IN6:
1074                 /* check if preferred lifetime > valid lifetime */
1075                 if (irr->irr_pltime > irr->irr_vltime) {
1076                         log(LOG_NOTICE,
1077                             "in6_prefix_ioctl: preferred lifetime"
1078                             "(%ld) is greater than valid lifetime(%ld)\n",
1079                             (u_long)irr->irr_pltime, (u_long)irr->irr_vltime);
1080                         error = EINVAL;
1081                         break;
1082                 }
1083                 if (mark_matched_prefixes(cmd, ifp, irr)) {
1084                         if (irr->irr_u_uselen != 0)
1085                                 if ((error = add_useprefixes(so, ifp, irr))
1086                                     != 0)
1087                                         goto failed;
1088                         if (cmd != SIOCAIFPREFIX_IN6)
1089                                 delete_prefixes(ifp, irr->irr_origin);
1090                 } else
1091                         return (EADDRNOTAVAIL);
1092         failed:
1093                 unmark_prefixes(ifp);
1094                 break;
1095         case SIOCGIFPREFIX_IN6:
1096                 rpp = search_matched_prefix(ifp, ipr);
1097                 if (rpp == NULL || ifp != rpp->rp_ifp)
1098                         return (EADDRNOTAVAIL);
1099
1100                 ipr->ipr_origin = rpp->rp_origin;
1101                 ipr->ipr_plen = rpp->rp_plen;
1102                 ipr->ipr_vltime = rpp->rp_vltime;
1103                 ipr->ipr_pltime = rpp->rp_pltime;
1104                 ipr->ipr_flags = rpp->rp_flags;
1105                 ipr->ipr_prefix = rpp->rp_prefix;
1106
1107                 break;
1108         case SIOCSIFPREFIX_IN6:
1109                 /* check if preferred lifetime > valid lifetime */
1110                 if (ipr->ipr_pltime > ipr->ipr_vltime) {
1111                         log(LOG_NOTICE,
1112                             "in6_prefix_ioctl: preferred lifetime"
1113                             "(%ld) is greater than valid lifetime(%ld)\n",
1114                             (u_long)ipr->ipr_pltime, (u_long)ipr->ipr_vltime);
1115                         error = EINVAL;
1116                         break;
1117                 }
1118
1119                 /* init rp_tmp */
1120                 bzero((caddr_t)&rp_tmp, sizeof(rp_tmp));
1121                 rp_tmp.rp_ifp = ifp;
1122                 rp_tmp.rp_plen = ipr->ipr_plen;
1123                 rp_tmp.rp_prefix = ipr->ipr_prefix;
1124                 rp_tmp.rp_vltime = ipr->ipr_vltime;
1125                 rp_tmp.rp_pltime = ipr->ipr_pltime;
1126                 rp_tmp.rp_flags = ipr->ipr_flags;
1127                 rp_tmp.rp_origin = ipr->ipr_origin;
1128
1129                 /* create rp_addr entries, usually at least for lladdr */
1130                 if ((error = link_stray_ia6s(&rp_tmp)) != 0) {
1131                         free_rp_entries(&rp_tmp);
1132                         break;
1133                 }
1134                 for (ifa = ifp->if_addrlist.tqh_first;
1135                      ifa;
1136                      ifa = ifa->ifa_list.tqe_next)
1137                 {
1138                         if (ifa->ifa_addr == NULL)
1139                                 continue;       /* just for safety */
1140                         if (ifa->ifa_addr->sa_family != AF_INET6)
1141                                 continue;
1142                         if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa)) == 0)
1143                                 continue;
1144
1145                         if ((error = create_ra_entry(&rap)) != 0) {
1146                                 free_rp_entries(&rp_tmp);
1147                                 goto bad;
1148                         }
1149                         /* copy interface id part */
1150                         bit_copy((caddr_t)&rap->ra_ifid,
1151                                  sizeof(rap->ra_ifid) << 3,
1152                                  (caddr_t)IFA_IN6(ifa),
1153                                  sizeof(*IFA_IN6(ifa)) << 3,
1154                                  rp_tmp.rp_plen,
1155                                  (sizeof(rap->ra_ifid) << 3) - rp_tmp.rp_plen);
1156                         /* insert into list */
1157                         LIST_INSERT_HEAD(&rp_tmp.rp_addrhead, rap, ra_entry);
1158                 }
1159
1160                 error = add_each_prefix(so, &rp_tmp);
1161
1162                 /* free each rp_addr entry */
1163                 free_rp_entries(&rp_tmp);
1164
1165                 break;
1166         case SIOCDIFPREFIX_IN6:
1167                 rpp = search_matched_prefix(ifp, ipr);
1168                 if (rpp == NULL || ifp != rpp->rp_ifp)
1169                         return (EADDRNOTAVAIL);
1170
1171                 error = delete_each_prefix(rpp, ipr->ipr_origin);
1172                 break;
1173         }
1174  bad:
1175         return error;
1176 }
1177
1178 void
1179 in6_rr_timer(void *ignored_arg)
1180 {
1181         int s;
1182         struct rr_prefix *rpp;
1183
1184         callout_reset(&in6_rr_timer_ch, ip6_rr_prune * hz,
1185             in6_rr_timer, NULL);
1186
1187         s = splnet();
1188         /* expire */
1189         rpp = LIST_FIRST(&rr_prefix);
1190         while (rpp) {
1191                 if (rpp->rp_expire && rpp->rp_expire < time_second) {
1192                         struct rr_prefix *next_rpp;
1193
1194                         next_rpp = LIST_NEXT(rpp, rp_entry);
1195                         delete_each_prefix(rpp, PR_ORIG_KERNEL);
1196                         rpp = next_rpp;
1197                         continue;
1198                 }
1199                 if (rpp->rp_preferred && rpp->rp_preferred < time_second)
1200                         unprefer_prefix(rpp);
1201                 rpp = LIST_NEXT(rpp, rp_entry);
1202         }
1203         splx(s);
1204 }