2 * Copyright (c) 2016-2018 Yandex LLC
3 * Copyright (c) 2016-2018 Andrey V. Elsukov <ae@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include "opt_inet6.h"
33 #include "opt_ipsec.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/fnv_hash.h>
41 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
47 #include <sys/errno.h>
48 #include <sys/sysctl.h>
54 #include <net/if_var.h>
55 #include <net/if_private.h>
56 #include <net/if_clone.h>
57 #include <net/if_types.h>
59 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip.h>
65 #include <netinet/ip_encap.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/in6_var.h>
69 #include <netinet6/scope6_var.h>
71 #include <netipsec/ipsec.h>
73 #include <netipsec/ipsec6.h>
76 #include <net/if_ipsec.h>
77 #include <netipsec/key.h>
79 #include <security/mac/mac_framework.h>
81 static MALLOC_DEFINE(M_IPSEC, "ipsec", "IPsec Virtual Tunnel Interface");
82 static const char ipsecname[] = "ipsec";
84 #if defined(INET) && defined(INET6)
85 #define IPSEC_SPCOUNT 4
87 #define IPSEC_SPCOUNT 2
92 struct secpolicy *sp[IPSEC_SPCOUNT];
97 CK_LIST_ENTRY(ipsec_softc) idhash;
98 CK_LIST_ENTRY(ipsec_softc) srchash;
101 #define IPSEC_RLOCK_TRACKER struct epoch_tracker ipsec_et
102 #define IPSEC_RLOCK() epoch_enter_preempt(net_epoch_preempt, &ipsec_et)
103 #define IPSEC_RUNLOCK() epoch_exit_preempt(net_epoch_preempt, &ipsec_et)
104 #define IPSEC_WAIT() epoch_wait_preempt(net_epoch_preempt)
106 #ifndef IPSEC_HASH_SIZE
107 #define IPSEC_HASH_SIZE (1 << 5)
110 CK_LIST_HEAD(ipsec_iflist, ipsec_softc);
111 VNET_DEFINE_STATIC(struct ipsec_iflist *, ipsec_idhtbl) = NULL;
112 #define V_ipsec_idhtbl VNET(ipsec_idhtbl)
115 VNET_DEFINE_STATIC(struct ipsec_iflist *, ipsec4_srchtbl) = NULL;
116 #define V_ipsec4_srchtbl VNET(ipsec4_srchtbl)
117 static const struct srcaddrtab *ipsec4_srctab = NULL;
121 VNET_DEFINE_STATIC(struct ipsec_iflist *, ipsec6_srchtbl) = NULL;
122 #define V_ipsec6_srchtbl VNET(ipsec6_srchtbl)
123 static const struct srcaddrtab *ipsec6_srctab = NULL;
126 static struct ipsec_iflist *
127 ipsec_idhash(uint32_t id)
130 return (&V_ipsec_idhtbl[fnv_32_buf(&id, sizeof(id),
131 FNV1_32_INIT) & (IPSEC_HASH_SIZE - 1)]);
134 static struct ipsec_iflist *
135 ipsec_srchash(const struct sockaddr *sa)
139 switch (sa->sa_family) {
143 &((const struct sockaddr_in *)sa)->sin_addr.s_addr,
144 sizeof(in_addr_t), FNV1_32_INIT);
145 return (&V_ipsec4_srchtbl[hval & (IPSEC_HASH_SIZE - 1)]);
150 &((const struct sockaddr_in6 *)sa)->sin6_addr,
151 sizeof(struct in6_addr), FNV1_32_INIT);
152 return (&V_ipsec6_srchtbl[hval & (IPSEC_HASH_SIZE - 1)]);
159 * ipsec_ioctl_sx protects from concurrent ioctls.
161 static struct sx ipsec_ioctl_sx;
162 SX_SYSINIT(ipsec_ioctl_sx, &ipsec_ioctl_sx, "ipsec_ioctl");
164 static int ipsec_init_reqid(struct ipsec_softc *);
165 static int ipsec_set_tunnel(struct ipsec_softc *, struct sockaddr *,
166 struct sockaddr *, uint32_t);
167 static void ipsec_delete_tunnel(struct ipsec_softc *);
169 static int ipsec_set_addresses(struct ifnet *, struct sockaddr *,
171 static int ipsec_set_reqid(struct ipsec_softc *, uint32_t);
172 static void ipsec_set_running(struct ipsec_softc *);
175 static void ipsec_reassign(struct ifnet *, struct vnet *, char *);
177 static void ipsec_srcaddr(void *, const struct sockaddr *, int);
178 static int ipsec_ioctl(struct ifnet *, u_long, caddr_t);
179 static int ipsec_transmit(struct ifnet *, struct mbuf *);
180 static int ipsec_output(struct ifnet *, struct mbuf *,
181 const struct sockaddr *, struct route *);
182 static void ipsec_qflush(struct ifnet *);
183 static int ipsec_clone_create(struct if_clone *, int, caddr_t);
184 static void ipsec_clone_destroy(struct ifnet *);
186 VNET_DEFINE_STATIC(struct if_clone *, ipsec_cloner);
187 #define V_ipsec_cloner VNET(ipsec_cloner)
190 ipsec_clone_create(struct if_clone *ifc, int unit, caddr_t params)
192 struct ipsec_softc *sc;
195 sc = malloc(sizeof(*sc), M_IPSEC, M_WAITOK | M_ZERO);
196 sc->fibnum = curthread->td_proc->p_fibnum;
197 sc->ifp = ifp = if_alloc(IFT_TUNNEL);
199 if_initname(ifp, ipsecname, unit);
202 ifp->if_mtu = IPSEC_MTU;
203 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
204 ifp->if_ioctl = ipsec_ioctl;
205 ifp->if_transmit = ipsec_transmit;
206 ifp->if_qflush = ipsec_qflush;
207 ifp->if_output = ipsec_output;
209 ifp->if_reassign = ipsec_reassign;
212 bpfattach(ifp, DLT_NULL, sizeof(uint32_t));
219 ipsec_reassign(struct ifnet *ifp, struct vnet *new_vnet __unused,
220 char *unused __unused)
222 struct ipsec_softc *sc;
224 sx_xlock(&ipsec_ioctl_sx);
227 ipsec_delete_tunnel(sc);
228 sx_xunlock(&ipsec_ioctl_sx);
233 ipsec_clone_destroy(struct ifnet *ifp)
235 struct ipsec_softc *sc;
237 sx_xlock(&ipsec_ioctl_sx);
239 ipsec_delete_tunnel(sc);
241 * Delete softc from idhash on interface destroy, since
242 * ipsec_delete_tunnel() keeps reqid unchanged.
245 CK_LIST_REMOVE(sc, idhash);
248 ifp->if_softc = NULL;
249 sx_xunlock(&ipsec_ioctl_sx);
256 static struct ipsec_iflist *
259 struct ipsec_iflist *hash;
262 hash = malloc(sizeof(struct ipsec_iflist) * IPSEC_HASH_SIZE,
264 for (i = 0; i < IPSEC_HASH_SIZE; i++)
265 CK_LIST_INIT(&hash[i]);
271 vnet_ipsec_init(const void *unused __unused)
274 V_ipsec_idhtbl = ipsec_hashinit();
276 V_ipsec4_srchtbl = ipsec_hashinit();
277 if (IS_DEFAULT_VNET(curvnet))
278 ipsec4_srctab = ip_encap_register_srcaddr(ipsec_srcaddr,
282 V_ipsec6_srchtbl = ipsec_hashinit();
283 if (IS_DEFAULT_VNET(curvnet))
284 ipsec6_srctab = ip6_encap_register_srcaddr(ipsec_srcaddr,
287 V_ipsec_cloner = if_clone_simple(ipsecname, ipsec_clone_create,
288 ipsec_clone_destroy, 0);
290 VNET_SYSINIT(vnet_ipsec_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
291 vnet_ipsec_init, NULL);
294 vnet_ipsec_uninit(const void *unused __unused)
297 if_clone_detach(V_ipsec_cloner);
298 free(V_ipsec_idhtbl, M_IPSEC);
300 * Use V_ipsec_idhtbl pointer as indicator that VNET is going to be
301 * destroyed, it is used by ipsec_srcaddr() callback.
303 V_ipsec_idhtbl = NULL;
307 if (IS_DEFAULT_VNET(curvnet))
308 ip_encap_unregister_srcaddr(ipsec4_srctab);
309 free(V_ipsec4_srchtbl, M_IPSEC);
312 if (IS_DEFAULT_VNET(curvnet))
313 ip6_encap_unregister_srcaddr(ipsec6_srctab);
314 free(V_ipsec6_srchtbl, M_IPSEC);
317 VNET_SYSUNINIT(vnet_ipsec_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
318 vnet_ipsec_uninit, NULL);
320 static struct secpolicy *
321 ipsec_getpolicy(struct ipsec_softc *sc, int dir, sa_family_t af)
327 return (sc->sp[(dir == IPSEC_DIR_INBOUND ? 0: 1)]);
331 return (sc->sp[(dir == IPSEC_DIR_INBOUND ? 0: 1)
341 static struct secasindex *
342 ipsec_getsaidx(struct ipsec_softc *sc, int dir, sa_family_t af)
344 struct secpolicy *sp;
346 sp = ipsec_getpolicy(sc, dir, af);
349 return (&sp->req[0]->saidx);
353 ipsec_transmit(struct ifnet *ifp, struct mbuf *m)
356 struct ipsec_softc *sc;
357 struct secpolicy *sp;
364 error = mac_ifnet_check_transmit(ifp, m);
372 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
373 (ifp->if_flags & IFF_MONITOR) != 0 ||
374 (ifp->if_flags & IFF_UP) == 0 || sc->family == 0) {
379 /* Determine address family to correctly handle packet in BPF */
380 ip = mtod(m, struct ip *);
388 case (IPV6_VERSION >> 4):
393 error = EAFNOSUPPORT;
400 * XXX: for now just check presence of IPSEC_OUT_DONE mbuf tag.
401 * We can read full chain and compare destination address,
402 * proto and mode from xform_history with values from softc.
404 if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL) {
409 sp = ipsec_getpolicy(sc, IPSEC_DIR_OUTBOUND, af);
411 M_SETFIB(m, sc->fibnum);
413 BPF_MTAP2(ifp, &af, sizeof(af), m);
414 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
415 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
420 error = ipsec4_process_packet(m, sp, NULL);
425 error = ipsec6_process_packet(m, sp, NULL);
429 panic("%s: unknown address family\n", __func__);
433 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
439 ipsec_qflush(struct ifnet *ifp __unused)
445 ipsec_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
449 return (ifp->if_transmit(ifp, m));
453 ipsec_if_input(struct mbuf *m, struct secasvar *sav, uint32_t af)
456 struct secasindex *saidx;
457 struct ipsec_softc *sc;
460 if (sav->state != SADB_SASTATE_MATURE &&
461 sav->state != SADB_SASTATE_DYING) {
466 if (sav->sah->saidx.mode != IPSEC_MODE_TUNNEL ||
467 sav->sah->saidx.proto != IPPROTO_ESP)
471 CK_LIST_FOREACH(sc, ipsec_idhash(sav->sah->saidx.reqid), idhash) {
474 saidx = ipsec_getsaidx(sc, IPSEC_DIR_INBOUND,
475 sav->sah->saidx.src.sa.sa_family);
476 /* SA's reqid should match reqid in SP */
478 sav->sah->saidx.reqid != saidx->reqid)
480 /* SAH's addresses should match tunnel endpoints. */
481 if (key_sockaddrcmp(&sav->sah->saidx.dst.sa,
482 &saidx->dst.sa, 0) != 0)
484 if (key_sockaddrcmp(&sav->sah->saidx.src.sa,
485 &saidx->src.sa, 0) == 0)
490 /* Tunnel was not found. Nothing to do. */
494 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
495 (ifp->if_flags & IFF_UP) == 0) {
501 * We found matching and working tunnel.
502 * Set its ifnet as receiving interface.
504 m->m_pkthdr.rcvif = ifp;
507 M_SETFIB(m, ifp->if_fib);
508 BPF_MTAP2(ifp, &af, sizeof(af), m);
509 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
510 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
511 if ((ifp->if_flags & IFF_MONITOR) != 0) {
521 ipsec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
523 struct ifreq *ifr = (struct ifreq*)data;
524 struct sockaddr *dst, *src;
525 struct ipsec_softc *sc;
526 struct secasindex *saidx;
528 struct sockaddr_in *sin = NULL;
531 struct sockaddr_in6 *sin6 = NULL;
538 ifp->if_flags |= IFF_UP;
545 if (ifr->ifr_mtu < IPSEC_MTU_MIN ||
546 ifr->ifr_mtu > IPSEC_MTU_MAX)
549 ifp->if_mtu = ifr->ifr_mtu;
552 sx_xlock(&ipsec_ioctl_sx);
554 /* Check that softc is still here */
563 case SIOCSIFPHYADDR_IN6:
569 src = (struct sockaddr *)
570 &(((struct in_aliasreq *)data)->ifra_addr);
571 dst = (struct sockaddr *)
572 &(((struct in_aliasreq *)data)->ifra_dstaddr);
576 case SIOCSIFPHYADDR_IN6:
577 src = (struct sockaddr *)
578 &(((struct in6_aliasreq *)data)->ifra_addr);
579 dst = (struct sockaddr *)
580 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
586 /* sa_family must be equal */
587 if (src->sa_family != dst->sa_family ||
588 src->sa_len != dst->sa_len)
591 /* validate sa_len */
592 switch (src->sa_family) {
595 if (src->sa_len != sizeof(struct sockaddr_in))
601 if (src->sa_len != sizeof(struct sockaddr_in6))
606 error = EAFNOSUPPORT;
609 /* check sa_family looks sane for the cmd */
610 error = EAFNOSUPPORT;
614 if (src->sa_family == AF_INET)
619 case SIOCSIFPHYADDR_IN6:
620 if (src->sa_family == AF_INET6)
625 error = EADDRNOTAVAIL;
626 switch (src->sa_family) {
629 if (satosin(src)->sin_addr.s_addr == INADDR_ANY ||
630 satosin(dst)->sin_addr.s_addr == INADDR_ANY)
636 if (IN6_IS_ADDR_UNSPECIFIED(
637 &satosin6(src)->sin6_addr) ||
638 IN6_IS_ADDR_UNSPECIFIED(
639 &satosin6(dst)->sin6_addr))
642 * Check validity of the scope zone ID of the
643 * addresses, and convert it into the kernel
644 * internal form if necessary.
646 error = sa6_embedscope(satosin6(src), 0);
649 error = sa6_embedscope(satosin6(dst), 0);
654 error = ipsec_set_addresses(ifp, src, dst);
657 ipsec_delete_tunnel(sc);
659 case SIOCGIFPSRCADDR:
660 case SIOCGIFPDSTADDR:
662 case SIOCGIFPSRCADDR_IN6:
663 case SIOCGIFPDSTADDR_IN6:
665 if (sc->family == 0) {
666 error = EADDRNOTAVAIL;
669 saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND, sc->family);
676 case SIOCGIFPSRCADDR:
677 case SIOCGIFPDSTADDR:
678 if (saidx->src.sa.sa_family != AF_INET) {
679 error = EADDRNOTAVAIL;
682 sin = (struct sockaddr_in *)&ifr->ifr_addr;
683 memset(sin, 0, sizeof(*sin));
684 sin->sin_family = AF_INET;
685 sin->sin_len = sizeof(*sin);
689 case SIOCGIFPSRCADDR_IN6:
690 case SIOCGIFPDSTADDR_IN6:
691 if (saidx->src.sa.sa_family != AF_INET6) {
692 error = EADDRNOTAVAIL;
695 sin6 = (struct sockaddr_in6 *)
696 &(((struct in6_ifreq *)data)->ifr_addr);
697 memset(sin6, 0, sizeof(*sin6));
698 sin6->sin6_family = AF_INET6;
699 sin6->sin6_len = sizeof(*sin6);
703 error = EAFNOSUPPORT;
708 case SIOCGIFPSRCADDR:
709 sin->sin_addr = saidx->src.sin.sin_addr;
711 case SIOCGIFPDSTADDR:
712 sin->sin_addr = saidx->dst.sin.sin_addr;
716 case SIOCGIFPSRCADDR_IN6:
717 sin6->sin6_addr = saidx->src.sin6.sin6_addr;
719 case SIOCGIFPDSTADDR_IN6:
720 sin6->sin6_addr = saidx->dst.sin6.sin6_addr;
729 case SIOCGIFPSRCADDR:
730 case SIOCGIFPDSTADDR:
731 error = prison_if(curthread->td_ucred,
732 (struct sockaddr *)sin);
734 memset(sin, 0, sizeof(*sin));
738 case SIOCGIFPSRCADDR_IN6:
739 case SIOCGIFPDSTADDR_IN6:
740 error = prison_if(curthread->td_ucred,
741 (struct sockaddr *)sin6);
743 error = sa6_recoverscope(sin6);
745 memset(sin6, 0, sizeof(*sin6));
750 ifr->ifr_fib = sc->fibnum;
753 if ((error = priv_check(curthread, PRIV_NET_SETIFFIB)) != 0)
755 if (ifr->ifr_fib >= rt_numfibs)
758 sc->fibnum = ifr->ifr_fib;
762 error = copyout(&reqid, ifr_data_get_ptr(ifr), sizeof(reqid));
765 if ((error = priv_check(curthread, PRIV_NET_SETIFCAP)) != 0)
767 error = copyin(ifr_data_get_ptr(ifr), &reqid, sizeof(reqid));
770 error = ipsec_set_reqid(sc, reqid);
777 sx_xunlock(&ipsec_ioctl_sx);
782 * Check that ingress address belongs to local host.
785 ipsec_set_running(struct ipsec_softc *sc)
787 struct secasindex *saidx;
790 saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND, sc->family);
794 switch (sc->family) {
797 localip = in_localip(saidx->src.sin.sin_addr);
802 localip = in6_localip(&saidx->src.sin6.sin6_addr);
807 sc->ifp->if_drv_flags |= IFF_DRV_RUNNING;
809 sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
813 * ifaddr_event handler.
814 * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
815 * source address spoofing.
818 ipsec_srcaddr(void *arg __unused, const struct sockaddr *sa,
821 struct ipsec_softc *sc;
822 struct secasindex *saidx;
823 struct ipsec_iflist *iflist;
825 /* Check that VNET is ready */
826 if (V_ipsec_idhtbl == NULL)
830 iflist = ipsec_srchash(sa);
833 CK_LIST_FOREACH(sc, iflist, srchash) {
836 saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND, sa->sa_family);
838 key_sockaddrcmp(&saidx->src.sa, sa, 0) != 0)
840 ipsec_set_running(sc);
845 * Allocate new private security policies for tunneling interface.
846 * Each tunneling interface has following security policies for
848 * 0.0.0.0/0[any] 0.0.0.0/0[any] -P in \
849 * ipsec esp/tunnel/RemoteIP-LocalIP/unique:reqid
850 * 0.0.0.0/0[any] 0.0.0.0/0[any] -P out \
851 * ipsec esp/tunnel/LocalIP-RemoteIP/unique:reqid
854 ipsec_newpolicies(struct ipsec_softc *sc, struct secpolicy *sp[IPSEC_SPCOUNT],
855 const struct sockaddr *src, const struct sockaddr *dst, uint32_t reqid)
857 struct ipsecrequest *isr;
860 memset(sp, 0, sizeof(struct secpolicy *) * IPSEC_SPCOUNT);
861 for (i = 0; i < IPSEC_SPCOUNT; i++) {
862 if ((sp[i] = key_newsp()) == NULL)
864 if ((isr = ipsec_newisr()) == NULL)
867 sp[i]->policy = IPSEC_POLICY_IPSEC;
868 sp[i]->state = IPSEC_SPSTATE_DEAD;
869 sp[i]->req[sp[i]->tcount++] = isr;
870 sp[i]->created = time_second;
871 /* Use priority field to store if_index */
872 sp[i]->priority = sc->ifp->if_index;
873 isr->level = IPSEC_LEVEL_UNIQUE;
874 isr->saidx.proto = IPPROTO_ESP;
875 isr->saidx.mode = IPSEC_MODE_TUNNEL;
876 isr->saidx.reqid = reqid;
878 sp[i]->spidx.dir = IPSEC_DIR_INBOUND;
879 bcopy(src, &isr->saidx.dst, src->sa_len);
880 bcopy(dst, &isr->saidx.src, dst->sa_len);
882 sp[i]->spidx.dir = IPSEC_DIR_OUTBOUND;
883 bcopy(src, &isr->saidx.src, src->sa_len);
884 bcopy(dst, &isr->saidx.dst, dst->sa_len);
886 sp[i]->spidx.ul_proto = IPSEC_ULPROTO_ANY;
889 sp[i]->spidx.src.sa.sa_family =
890 sp[i]->spidx.dst.sa.sa_family = AF_INET;
891 sp[i]->spidx.src.sa.sa_len =
892 sp[i]->spidx.dst.sa.sa_len =
893 sizeof(struct sockaddr_in);
898 sp[i]->spidx.src.sa.sa_family =
899 sp[i]->spidx.dst.sa.sa_family = AF_INET6;
900 sp[i]->spidx.src.sa.sa_len =
901 sp[i]->spidx.dst.sa.sa_len = sizeof(struct sockaddr_in6);
906 for (i = 0; i < IPSEC_SPCOUNT; i++)
912 ipsec_check_reqid(uint32_t reqid)
914 struct ipsec_softc *sc;
916 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
917 CK_LIST_FOREACH(sc, ipsec_idhash(reqid), idhash) {
918 if (sc->reqid == reqid)
925 * We use key_newreqid() to automatically obtain unique reqid.
926 * Then we check that given id is unique, i.e. it is not used by
927 * another if_ipsec(4) interface. This macro limits the number of
928 * tries to get unique id.
930 #define IPSEC_REQID_TRYCNT 64
932 ipsec_init_reqid(struct ipsec_softc *sc)
937 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
938 if (sc->reqid != 0) /* already initialized */
941 trycount = IPSEC_REQID_TRYCNT;
942 while (--trycount > 0) {
943 reqid = key_newreqid();
944 if (ipsec_check_reqid(reqid) == 0)
950 CK_LIST_INSERT_HEAD(ipsec_idhash(reqid), sc, idhash);
955 * Set or update reqid for given tunneling interface.
956 * When specified reqid is zero, generate new one.
957 * We are protected by ioctl_sx lock from concurrent id generation.
958 * Also softc would not disappear while we hold ioctl_sx lock.
961 ipsec_set_reqid(struct ipsec_softc *sc, uint32_t reqid)
963 struct secasindex *saidx;
965 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
967 if (sc->reqid == reqid && reqid != 0)
971 /* Check that specified reqid doesn't exist */
972 if (ipsec_check_reqid(reqid) != 0)
974 if (sc->reqid != 0) {
975 CK_LIST_REMOVE(sc, idhash);
979 CK_LIST_INSERT_HEAD(ipsec_idhash(reqid), sc, idhash);
981 /* Generate new reqid */
982 if (ipsec_init_reqid(sc) != 0)
986 /* Tunnel isn't fully configured, just return. */
990 saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND, sc->family);
991 KASSERT(saidx != NULL,
992 ("saidx is NULL, but family is %d", sc->family));
993 return (ipsec_set_tunnel(sc, &saidx->src.sa, &saidx->dst.sa,
998 * Set tunnel endpoints addresses.
1001 ipsec_set_addresses(struct ifnet *ifp, struct sockaddr *src,
1002 struct sockaddr *dst)
1004 struct ipsec_softc *sc;
1005 struct secasindex *saidx;
1007 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
1010 if (sc->family != 0) {
1011 saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND,
1013 if (saidx != NULL && saidx->reqid == sc->reqid &&
1014 key_sockaddrcmp(&saidx->src.sa, src, 0) == 0 &&
1015 key_sockaddrcmp(&saidx->dst.sa, dst, 0) == 0)
1016 return (0); /* Nothing has been changed. */
1018 /* If reqid is not set, generate new one. */
1019 if (ipsec_init_reqid(sc) != 0)
1021 return (ipsec_set_tunnel(sc, src, dst, sc->reqid));
1025 ipsec_set_tunnel(struct ipsec_softc *sc, struct sockaddr *src,
1026 struct sockaddr *dst, uint32_t reqid)
1028 struct epoch_tracker et;
1029 struct ipsec_iflist *iflist;
1030 struct secpolicy *sp[IPSEC_SPCOUNT];
1033 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
1035 /* Allocate SP with new addresses. */
1036 iflist = ipsec_srchash(src);
1037 if (iflist == NULL) {
1038 sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1039 return (EAFNOSUPPORT);
1041 if (ipsec_newpolicies(sc, sp, src, dst, reqid) == 0) {
1042 /* Add new policies to SPDB */
1043 if (key_register_ifnet(sp, IPSEC_SPCOUNT) != 0) {
1044 for (i = 0; i < IPSEC_SPCOUNT; i++)
1048 if (sc->family != 0)
1049 ipsec_delete_tunnel(sc);
1050 for (i = 0; i < IPSEC_SPCOUNT; i++)
1052 sc->family = src->sa_family;
1053 CK_LIST_INSERT_HEAD(iflist, sc, srchash);
1055 sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1058 NET_EPOCH_ENTER(et);
1059 ipsec_set_running(sc);
1065 ipsec_delete_tunnel(struct ipsec_softc *sc)
1069 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
1071 sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1072 if (sc->family != 0) {
1073 CK_LIST_REMOVE(sc, srchash);
1076 * Make sure that ipsec_if_input() will not do access
1077 * to softc's policies.
1081 key_unregister_ifnet(sc->sp, IPSEC_SPCOUNT);
1082 for (i = 0; i < IPSEC_SPCOUNT; i++)
1083 key_freesp(&sc->sp[i]);