Fix UTIME_OMIT handling
[dragonfly.git] / sys / netinet6 / in6_src.c
blob8e3667a2d45e3c45259f551630f7e347e1688ed4
1 /* $FreeBSD: src/sys/netinet6/in6_src.c,v 1.1.2.3 2002/02/26 18:02:06 ume Exp $ */
2 /* $KAME: in6_src.c,v 1.37 2001/03/29 05:34:31 itojun Exp $ */
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
61 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
64 #include "opt_inet.h"
65 #include "opt_inet6.h"
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/jail.h>
70 #include <sys/kernel.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/sockio.h>
77 #include <sys/sysctl.h>
78 #include <sys/errno.h>
79 #include <sys/time.h>
80 #include <sys/proc.h>
81 #include <sys/caps.h>
83 #include <net/if.h>
84 #include <net/route.h>
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/ip.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet6/in6_var.h>
92 #include <netinet/ip6.h>
93 #include <netinet6/in6_pcb.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet6/nd6.h>
96 #ifdef ENABLE_DEFAULT_SCOPE
97 #include <netinet6/scope6_var.h>
98 #endif
100 #include <net/net_osdep.h>
102 #define ADDR_LABEL_NOTAPP (-1)
103 struct in6_addrpolicy defaultaddrpolicy;
105 static void init_policy_queue(void);
106 static int add_addrsel_policyent(struct in6_addrpolicy *);
107 static int delete_addrsel_policyent(struct in6_addrpolicy *);
108 static int walk_addrsel_policy(int (*)(struct in6_addrpolicy *, void *),
109 void *);
110 static int dump_addrsel_policyent(struct in6_addrpolicy *, void *);
114 * Return an IPv6 address, which is the most appropriate for a given
115 * destination and user specified options.
116 * If necessary, this function lookups the routing table and returns
117 * an entry to the caller for later use.
119 struct in6_addr *
120 in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
121 struct ip6_moptions *mopts, struct route_in6 *ro,
122 struct in6_addr *laddr, int *errorp, struct thread *td)
124 struct sockaddr_in6 jsin6;
125 struct ucred *cred = NULL;
126 struct in6_addr *dst;
127 struct in6_ifaddr *ia6 = NULL;
128 struct in6_pktinfo *pi = NULL;
129 int jailed = 0;
131 if (td && td->td_proc && td->td_proc->p_ucred)
132 cred = td->td_proc->p_ucred;
133 if (cred && cred->cr_prison)
134 jailed = 1;
135 jsin6.sin6_family = AF_INET6;
136 dst = &dstsock->sin6_addr;
137 *errorp = 0;
140 * If the source address is explicitly specified by the caller,
141 * use it.
143 if (opts && (pi = opts->ip6po_pktinfo) &&
144 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
145 jsin6.sin6_addr = pi->ipi6_addr;
146 if (jailed && !jailed_ip(cred->cr_prison,
147 (struct sockaddr *)&jsin6)) {
148 return(0);
149 } else {
150 return (&pi->ipi6_addr);
155 * If the source address is not specified but the socket(if any)
156 * is already bound, use the bound address.
158 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
159 jsin6.sin6_addr = *laddr;
160 if (jailed && !jailed_ip(cred->cr_prison,
161 (struct sockaddr *)&jsin6)) {
162 return(0);
163 } else {
164 return (laddr);
169 * If the caller doesn't specify the source address but
170 * the outgoing interface, use an address associated with
171 * the interface.
173 if (pi && pi->ipi6_ifindex) {
174 /* XXX boundary check is assumed to be already done. */
175 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
176 dst, cred);
178 if (ia6 && jailed) {
179 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
180 if (!jailed_ip(cred->cr_prison,
181 (struct sockaddr *)&jsin6))
182 ia6 = NULL;
185 if (ia6 == NULL) {
186 *errorp = EADDRNOTAVAIL;
187 return (0);
189 return (&satosin6(&ia6->ia_addr)->sin6_addr);
193 * If the destination address is a link-local unicast address or
194 * a multicast address, and if the outgoing interface is specified
195 * by the sin6_scope_id filed, use an address associated with the
196 * interface.
197 * XXX: We're now trying to define more specific semantics of
198 * sin6_scope_id field, so this part will be rewritten in
199 * the near future.
201 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
202 dstsock->sin6_scope_id) {
204 * I'm not sure if boundary check for scope_id is done
205 * somewhere...
207 if (dstsock->sin6_scope_id < 0 ||
208 if_index < dstsock->sin6_scope_id) {
209 *errorp = ENXIO; /* XXX: better error? */
210 return (0);
212 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
213 dst, cred);
215 if (ia6 && jailed) {
216 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
217 if (!jailed_ip(cred->cr_prison,
218 (struct sockaddr *)&jsin6))
219 ia6 = NULL;
222 if (ia6 == NULL) {
223 *errorp = EADDRNOTAVAIL;
224 return (0);
226 return (&satosin6(&ia6->ia_addr)->sin6_addr);
230 * If the destination address is a multicast address and
231 * the outgoing interface for the address is specified
232 * by the caller, use an address associated with the interface.
233 * There is a sanity check here; if the destination has node-local
234 * scope, the outgoing interfacde should be a loopback address.
235 * Even if the outgoing interface is not specified, we also
236 * choose a loopback interface as the outgoing interface.
238 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
239 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
241 if (ifp == NULL && IN6_IS_ADDR_MC_INTFACELOCAL(dst)) {
242 ifp = loif;
245 if (ifp) {
246 ia6 = in6_ifawithscope(ifp, dst, cred);
247 if (ia6 == NULL) {
248 *errorp = EADDRNOTAVAIL;
249 return (0);
251 return (&satosin6(&ia6->ia_addr)->sin6_addr);
256 * If the next hop address for the packet is specified
257 * by caller, use an address associated with the route
258 * to the next hop.
261 struct sockaddr_in6 *sin6_next;
262 struct rtentry *rt;
264 if (opts && opts->ip6po_nexthop) {
265 sin6_next = satosin6(opts->ip6po_nexthop);
266 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
267 if (rt) {
268 ia6 = in6_ifawithscope(rt->rt_ifp, dst, cred);
269 if (ia6 == NULL)
270 ia6 = ifatoia6(rt->rt_ifa);
272 if (ia6 && jailed) {
273 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
274 if (!jailed_ip(cred->cr_prison,
275 (struct sockaddr *)&jsin6))
276 ia6 = NULL;
279 if (ia6 == NULL) {
280 *errorp = EADDRNOTAVAIL;
281 return (0);
283 return (&satosin6(&ia6->ia_addr)->sin6_addr);
288 * If route is known or can be allocated now,
289 * our src addr is taken from the i/f, else punt.
291 if (ro) {
292 if (ro->ro_rt &&
293 (!(ro->ro_rt->rt_flags & RTF_UP) ||
294 satosin6(&ro->ro_dst)->sin6_family != AF_INET6 ||
295 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr,
296 dst))) {
297 RTFREE(ro->ro_rt);
298 ro->ro_rt = NULL;
300 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
301 struct sockaddr_in6 *sa6;
303 /* No route yet, so try to acquire one */
304 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
305 sa6 = &ro->ro_dst;
306 sa6->sin6_family = AF_INET6;
307 sa6->sin6_len = sizeof(struct sockaddr_in6);
308 sa6->sin6_addr = *dst;
309 sa6->sin6_scope_id = dstsock->sin6_scope_id;
310 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
311 ro->ro_rt =
312 rtpurelookup((struct sockaddr *)&ro->ro_dst);
313 } else {
314 rtalloc((struct route *)ro);
319 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
320 * the address. But we don't know why it does so.
321 * It is necessary to ensure the scope even for lo0
322 * so doesn't check out IFF_LOOPBACK.
325 if (ro->ro_rt) {
326 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst, cred);
327 if (ia6 && jailed) {
328 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
329 if (!jailed_ip(cred->cr_prison,
330 (struct sockaddr *)&jsin6))
331 ia6 = NULL;
334 if (ia6 == NULL) /* xxx scope error ?*/
335 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
337 if (ia6 && jailed) {
338 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
339 if (!jailed_ip(cred->cr_prison,
340 (struct sockaddr *)&jsin6))
341 ia6 = NULL;
344 if (ia6 == NULL) {
345 *errorp = EHOSTUNREACH; /* no route */
346 return (0);
348 return (&satosin6(&ia6->ia_addr)->sin6_addr);
351 *errorp = EADDRNOTAVAIL;
352 return (0);
356 * Default hop limit selection. The precedence is as follows:
357 * 1. Hoplimit value specified via ioctl.
358 * 2. (If the outgoing interface is detected) the current
359 * hop limit of the interface specified by router advertisement.
360 * 3. The system default hoplimit.
363 in6_selecthlim(struct in6pcb *in6p, struct ifnet *ifp)
365 int hlim;
367 if (in6p && in6p->in6p_hops >= 0) {
368 return (in6p->in6p_hops);
369 } else if (ifp) {
370 hlim = ND_IFINFO(ifp)->chlim;
371 if (hlim < ip6_minhlim)
372 hlim = ip6_minhlim;
373 } else {
374 hlim = ip6_defhlim;
376 return (hlim);
379 static boolean_t
380 in6_pcbporthash_update(struct inpcbportinfo *portinfo,
381 struct inpcb *inp, u_short lport, struct ucred *cred, int wild)
383 struct inpcbporthead *porthash;
386 * This has to be atomic. If the porthash is shared across multiple
387 * protocol threads, e.g. tcp and udp, then the token must be held.
389 porthash = in_pcbporthash_head(portinfo, lport);
390 GET_PORTHASH_TOKEN(porthash);
392 if (in6_pcblookup_local(porthash, &inp->in6p_laddr, lport,
393 wild, cred) != NULL) {
394 REL_PORTHASH_TOKEN(porthash);
395 return FALSE;
397 inp->inp_lport = lport;
398 in_pcbinsporthash(porthash, inp);
400 REL_PORTHASH_TOKEN(porthash);
401 return TRUE;
405 * XXX: this is borrowed from in6_pcbbind(). If possible, we should
406 * share this function by all *bsd*...
409 in6_pcbsetlport(struct in6_addr *laddr, struct inpcb *inp, struct thread *td)
411 struct socket *so = inp->inp_socket;
412 uint16_t lport, first, last, step, first0, last0;
413 int count, error = 0, wild = 0;
414 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
415 struct inpcbportinfo *portinfo;
416 struct ucred *cred = NULL;
417 int portinfo_first, portinfo_idx;
418 uint32_t cut;
420 /* XXX: this is redundant when called from in6_pcbbind */
421 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
422 wild = INPLOOKUP_WILDCARD;
423 if (td->td_proc && td->td_proc->p_ucred)
424 cred = td->td_proc->p_ucred;
426 inp->inp_flags |= INP_ANONPORT;
428 step = pcbinfo->portinfo_cnt;
429 portinfo_first = mycpuid % pcbinfo->portinfo_cnt;
430 portinfo_idx = portinfo_first;
432 if (inp->inp_flags & INP_HIGHPORT) {
433 first0 = ipport_hifirstauto; /* sysctl */
434 last0 = ipport_hilastauto;
435 } else if (inp->inp_flags & INP_LOWPORT) {
436 error = caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT);
437 if (error)
438 return error;
439 first0 = ipport_lowfirstauto; /* 1023 */
440 last0 = ipport_lowlastauto; /* 600 */
441 } else {
442 first0 = ipport_firstauto; /* sysctl */
443 last0 = ipport_lastauto;
445 if (first0 > last0) {
446 lport = last0;
447 last0 = first0;
448 first0 = lport;
450 KKASSERT(last0 >= first0);
452 cut = karc4random();
453 loop:
454 portinfo = &pcbinfo->portinfo[portinfo_idx];
455 first = first0;
456 last = last0;
459 * Simple check to ensure all ports are not used up causing
460 * a deadlock here.
462 in_pcbportrange(&last, &first, portinfo->offset, step);
463 lport = last - first;
464 count = lport / step;
466 lport = rounddown(cut % lport, step) + first;
467 KKASSERT(lport % step == portinfo->offset);
469 for (;;) {
470 if (count-- < 0) { /* completely used? */
471 error = EAGAIN;
472 break;
475 if (__predict_false(lport < first || lport > last)) {
476 lport = first;
477 KKASSERT(lport % step == portinfo->offset);
480 if (in6_pcbporthash_update(portinfo, inp, htons(lport),
481 cred, wild)) {
482 error = 0;
483 break;
486 lport += step;
487 KKASSERT(lport % step == portinfo->offset);
490 if (error) {
491 /* Try next portinfo */
492 portinfo_idx++;
493 portinfo_idx %= pcbinfo->portinfo_cnt;
494 if (portinfo_idx != portinfo_first)
495 goto loop;
497 /* Undo any address bind that may have occurred above. */
498 inp->in6p_laddr = kin6addr_any;
500 return error;
504 * Generate kernel-internal form (scopeid embedded into s6_addr16[1]).
505 * If the address scope of is link-local, embed the interface index in the
506 * address. The routine determines our precedence
507 * between advanced API scope/interface specification and basic API
508 * specification.
510 * This function should be nuked in the future, when we get rid of embedded
511 * scopeid thing.
513 * XXX actually, it is over-specification to return ifp against sin6_scope_id.
514 * there can be multiple interfaces that belong to a particular scope zone
515 * (in specification, we have 1:N mapping between a scope zone and interfaces).
516 * we may want to change the function to return something other than ifp.
519 in6_embedscope(struct in6_addr *in6,
520 const struct sockaddr_in6 *sin6,
521 #ifdef HAVE_NRL_INPCB
522 struct inpcb *in6p,
523 #define in6p_outputopts inp_outputopts6
524 #define in6p_moptions inp_moptions6
525 #else
526 struct in6pcb *in6p,
527 #endif
528 struct ifnet **ifpp)
530 struct ifnet *ifp = NULL;
531 u_int32_t scopeid;
533 *in6 = sin6->sin6_addr;
534 scopeid = sin6->sin6_scope_id;
535 if (ifpp)
536 *ifpp = NULL;
539 * don't try to read sin6->sin6_addr beyond here, since the caller may
540 * ask us to overwrite existing sockaddr_in6
543 #ifdef ENABLE_DEFAULT_SCOPE
544 if (scopeid == 0)
545 scopeid = scope6_addr2default(in6);
546 #endif
548 if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
549 struct in6_pktinfo *pi;
552 * KAME assumption: link id == interface id
555 if (in6p && in6p->in6p_outputopts &&
556 (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
557 pi->ipi6_ifindex) {
558 ifp = ifindex2ifnet[pi->ipi6_ifindex];
559 in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
560 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
561 in6p->in6p_moptions &&
562 in6p->in6p_moptions->im6o_multicast_ifp) {
563 ifp = in6p->in6p_moptions->im6o_multicast_ifp;
564 in6->s6_addr16[1] = htons(ifp->if_index);
565 } else if (scopeid) {
566 /* boundary check */
567 if (scopeid < 0 || if_index < scopeid)
568 return ENXIO; /* XXX EINVAL? */
569 ifp = ifindex2ifnet[scopeid];
570 /* XXX assignment to 16bit from 32bit variable */
571 in6->s6_addr16[1] = htons(scopeid & 0xffff);
574 if (ifpp)
575 *ifpp = ifp;
578 return 0;
580 #ifdef HAVE_NRL_INPCB
581 #undef in6p_outputopts
582 #undef in6p_moptions
583 #endif
586 * generate standard sockaddr_in6 from embedded form.
587 * touches sin6_addr and sin6_scope_id only.
589 * this function should be nuked in the future, when we get rid of
590 * embedded scopeid thing.
593 in6_recoverscope(struct sockaddr_in6 *sin6, const struct in6_addr *in6,
594 struct ifnet *ifp)
596 u_int32_t zoneid;
598 sin6->sin6_addr = *in6;
601 * don't try to read *in6 beyond here, since the caller may
602 * ask us to overwrite existing sockaddr_in6
605 sin6->sin6_scope_id = 0;
606 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
608 * KAME assumption: link id == interface id
610 zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
611 if (zoneid) {
612 /* sanity check */
613 if (zoneid < 0 || if_index < zoneid)
614 return ENXIO;
615 if (ifp && ifp->if_index != zoneid)
616 return ENXIO;
617 sin6->sin6_addr.s6_addr16[1] = 0;
618 sin6->sin6_scope_id = zoneid;
622 return 0;
626 * just clear the embedded scope identifier.
628 void
629 in6_clearscope(struct in6_addr *addr)
631 if (IN6_IS_SCOPE_LINKLOCAL(addr) || IN6_IS_ADDR_MC_INTFACELOCAL(addr))
632 addr->s6_addr16[1] = 0;
635 void
636 addrsel_policy_init(void)
639 init_policy_queue();
641 /* initialize the "last resort" policy */
642 bzero(&defaultaddrpolicy, sizeof(defaultaddrpolicy));
643 defaultaddrpolicy.label = ADDR_LABEL_NOTAPP;
647 * Subroutines to manage the address selection policy table via sysctl.
649 struct walkarg {
650 struct sysctl_req *w_req;
653 static int in6_src_sysctl(SYSCTL_HANDLER_ARGS);
654 SYSCTL_DECL(_net_inet6_ip6);
655 SYSCTL_NODE(_net_inet6_ip6, IPV6CTL_ADDRCTLPOLICY, addrctlpolicy,
656 CTLFLAG_RD, in6_src_sysctl, "Address selection policy");
658 static int
659 in6_src_sysctl(SYSCTL_HANDLER_ARGS)
661 struct walkarg w;
663 if (req->newptr)
664 return EPERM;
666 bzero(&w, sizeof(w));
667 w.w_req = req;
669 return (walk_addrsel_policy(dump_addrsel_policyent, &w));
673 in6_src_ioctl(u_long cmd, caddr_t data)
675 int i;
676 struct in6_addrpolicy ent0;
678 if (cmd != SIOCAADDRCTL_POLICY && cmd != SIOCDADDRCTL_POLICY)
679 return (EOPNOTSUPP); /* check for safety */
681 ent0 = *(struct in6_addrpolicy *)data;
683 if (ent0.label == ADDR_LABEL_NOTAPP)
684 return (EINVAL);
685 /* check if the prefix mask is consecutive. */
686 if (in6_mask2len(&ent0.addrmask.sin6_addr, NULL) < 0)
687 return (EINVAL);
688 /* clear trailing garbages (if any) of the prefix address. */
689 for (i = 0; i < 4; i++) {
690 ent0.addr.sin6_addr.s6_addr32[i] &=
691 ent0.addrmask.sin6_addr.s6_addr32[i];
693 ent0.use = 0;
695 switch (cmd) {
696 case SIOCAADDRCTL_POLICY:
697 return (add_addrsel_policyent(&ent0));
698 case SIOCDADDRCTL_POLICY:
699 return (delete_addrsel_policyent(&ent0));
702 return (0); /* XXX: compromise compilers */
706 * The followings are implementation of the policy table using a
707 * simple tail queue.
708 * XXX such details should be hidden.
709 * XXX implementation using binary tree should be more efficient.
711 struct addrsel_policyent {
712 TAILQ_ENTRY(addrsel_policyent) ape_entry;
713 struct in6_addrpolicy ape_policy;
716 TAILQ_HEAD(addrsel_policyhead, addrsel_policyent);
718 struct addrsel_policyhead addrsel_policytab;
720 static void
721 init_policy_queue(void)
723 TAILQ_INIT(&addrsel_policytab);
726 static int
727 add_addrsel_policyent(struct in6_addrpolicy *newpolicy)
729 struct addrsel_policyent *new, *pol;
731 /* duplication check */
732 for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
733 pol = TAILQ_NEXT(pol, ape_entry)) {
734 if (SA6_ARE_ADDR_EQUAL(&newpolicy->addr,
735 &pol->ape_policy.addr) &&
736 SA6_ARE_ADDR_EQUAL(&newpolicy->addrmask,
737 &pol->ape_policy.addrmask)) {
738 return (EEXIST); /* or override it? */
742 new = kmalloc(sizeof(*new), M_IFADDR, M_WAITOK | M_ZERO);
744 /* XXX: should validate entry */
745 new->ape_policy = *newpolicy;
747 TAILQ_INSERT_TAIL(&addrsel_policytab, new, ape_entry);
749 return (0);
752 static int
753 delete_addrsel_policyent(struct in6_addrpolicy *key)
755 struct addrsel_policyent *pol;
757 /* search for the entry in the table */
758 for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
759 pol = TAILQ_NEXT(pol, ape_entry)) {
760 if (SA6_ARE_ADDR_EQUAL(&key->addr, &pol->ape_policy.addr) &&
761 SA6_ARE_ADDR_EQUAL(&key->addrmask,
762 &pol->ape_policy.addrmask)) {
763 break;
766 if (pol == NULL)
767 return (ESRCH);
769 TAILQ_REMOVE(&addrsel_policytab, pol, ape_entry);
770 kfree(pol, M_IFADDR);
772 return (0);
775 static int
776 walk_addrsel_policy(int(*callback)(struct in6_addrpolicy *, void *), void *w)
778 struct addrsel_policyent *pol;
779 int error = 0;
781 for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
782 pol = TAILQ_NEXT(pol, ape_entry)) {
783 if ((error = (*callback)(&pol->ape_policy, w)) != 0)
784 return (error);
787 return (error);
790 static int
791 dump_addrsel_policyent(struct in6_addrpolicy *pol, void *arg)
793 int error = 0;
794 struct walkarg *w = arg;
796 error = SYSCTL_OUT(w->w_req, pol, sizeof(*pol));
798 return (error);