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>
30 #include "opt_inet6.h"
31 #include "opt_ipsec.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/fnv_hash.h>
39 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
45 #include <sys/errno.h>
46 #include <sys/sysctl.h>
52 #include <net/if_var.h>
53 #include <net/if_private.h>
54 #include <net/if_clone.h>
55 #include <net/if_types.h>
57 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_encap.h>
65 #include <netinet/ip6.h>
66 #include <netinet6/in6_var.h>
67 #include <netinet6/scope6_var.h>
69 #include <netipsec/ipsec.h>
71 #include <netipsec/ipsec6.h>
74 #include <net/if_ipsec.h>
75 #include <netipsec/key.h>
77 #include <security/mac/mac_framework.h>
79 static MALLOC_DEFINE(M_IPSEC, "ipsec", "IPsec Virtual Tunnel Interface");
80 static const char ipsecname[] = "ipsec";
82 #if defined(INET) && defined(INET6)
83 #define IPSEC_SPCOUNT 4
85 #define IPSEC_SPCOUNT 2
90 struct secpolicy *sp[IPSEC_SPCOUNT];
95 CK_LIST_ENTRY(ipsec_softc) idhash;
96 CK_LIST_ENTRY(ipsec_softc) srchash;
99 #define IPSEC_RLOCK_TRACKER struct epoch_tracker ipsec_et
100 #define IPSEC_RLOCK() epoch_enter_preempt(net_epoch_preempt, &ipsec_et)
101 #define IPSEC_RUNLOCK() epoch_exit_preempt(net_epoch_preempt, &ipsec_et)
102 #define IPSEC_WAIT() epoch_wait_preempt(net_epoch_preempt)
104 #ifndef IPSEC_HASH_SIZE
105 #define IPSEC_HASH_SIZE (1 << 5)
108 CK_LIST_HEAD(ipsec_iflist, ipsec_softc);
109 VNET_DEFINE_STATIC(struct ipsec_iflist *, ipsec_idhtbl) = NULL;
110 #define V_ipsec_idhtbl VNET(ipsec_idhtbl)
113 VNET_DEFINE_STATIC(struct ipsec_iflist *, ipsec4_srchtbl) = NULL;
114 #define V_ipsec4_srchtbl VNET(ipsec4_srchtbl)
115 static const struct srcaddrtab *ipsec4_srctab = NULL;
119 VNET_DEFINE_STATIC(struct ipsec_iflist *, ipsec6_srchtbl) = NULL;
120 #define V_ipsec6_srchtbl VNET(ipsec6_srchtbl)
121 static const struct srcaddrtab *ipsec6_srctab = NULL;
124 static struct ipsec_iflist *
125 ipsec_idhash(uint32_t id)
128 return (&V_ipsec_idhtbl[fnv_32_buf(&id, sizeof(id),
129 FNV1_32_INIT) & (IPSEC_HASH_SIZE - 1)]);
132 static struct ipsec_iflist *
133 ipsec_srchash(const struct sockaddr *sa)
137 switch (sa->sa_family) {
141 &((const struct sockaddr_in *)sa)->sin_addr.s_addr,
142 sizeof(in_addr_t), FNV1_32_INIT);
143 return (&V_ipsec4_srchtbl[hval & (IPSEC_HASH_SIZE - 1)]);
148 &((const struct sockaddr_in6 *)sa)->sin6_addr,
149 sizeof(struct in6_addr), FNV1_32_INIT);
150 return (&V_ipsec6_srchtbl[hval & (IPSEC_HASH_SIZE - 1)]);
157 * ipsec_ioctl_sx protects from concurrent ioctls.
159 static struct sx ipsec_ioctl_sx;
160 SX_SYSINIT(ipsec_ioctl_sx, &ipsec_ioctl_sx, "ipsec_ioctl");
162 static int ipsec_init_reqid(struct ipsec_softc *);
163 static int ipsec_set_tunnel(struct ipsec_softc *, struct sockaddr *,
164 struct sockaddr *, uint32_t);
165 static void ipsec_delete_tunnel(struct ipsec_softc *);
167 static int ipsec_set_addresses(struct ifnet *, struct sockaddr *,
169 static int ipsec_set_reqid(struct ipsec_softc *, uint32_t);
170 static void ipsec_set_running(struct ipsec_softc *);
173 static void ipsec_reassign(struct ifnet *, struct vnet *, char *);
175 static void ipsec_srcaddr(void *, const struct sockaddr *, int);
176 static int ipsec_ioctl(struct ifnet *, u_long, caddr_t);
177 static int ipsec_transmit(struct ifnet *, struct mbuf *);
178 static int ipsec_output(struct ifnet *, struct mbuf *,
179 const struct sockaddr *, struct route *);
180 static void ipsec_qflush(struct ifnet *);
181 static int ipsec_clone_create(struct if_clone *, int, caddr_t);
182 static void ipsec_clone_destroy(struct ifnet *);
184 VNET_DEFINE_STATIC(struct if_clone *, ipsec_cloner);
185 #define V_ipsec_cloner VNET(ipsec_cloner)
188 ipsec_clone_create(struct if_clone *ifc, int unit, caddr_t params)
190 struct ipsec_softc *sc;
193 sc = malloc(sizeof(*sc), M_IPSEC, M_WAITOK | M_ZERO);
194 sc->fibnum = curthread->td_proc->p_fibnum;
195 sc->ifp = ifp = if_alloc(IFT_TUNNEL);
197 if_initname(ifp, ipsecname, unit);
200 ifp->if_mtu = IPSEC_MTU;
201 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
202 ifp->if_ioctl = ipsec_ioctl;
203 ifp->if_transmit = ipsec_transmit;
204 ifp->if_qflush = ipsec_qflush;
205 ifp->if_output = ipsec_output;
207 ifp->if_reassign = ipsec_reassign;
210 bpfattach(ifp, DLT_NULL, sizeof(uint32_t));
217 ipsec_reassign(struct ifnet *ifp, struct vnet *new_vnet __unused,
218 char *unused __unused)
220 struct ipsec_softc *sc;
222 sx_xlock(&ipsec_ioctl_sx);
225 ipsec_delete_tunnel(sc);
226 sx_xunlock(&ipsec_ioctl_sx);
231 ipsec_clone_destroy(struct ifnet *ifp)
233 struct ipsec_softc *sc;
235 sx_xlock(&ipsec_ioctl_sx);
237 ipsec_delete_tunnel(sc);
239 * Delete softc from idhash on interface destroy, since
240 * ipsec_delete_tunnel() keeps reqid unchanged.
243 CK_LIST_REMOVE(sc, idhash);
246 ifp->if_softc = NULL;
247 sx_xunlock(&ipsec_ioctl_sx);
254 static struct ipsec_iflist *
257 struct ipsec_iflist *hash;
260 hash = malloc(sizeof(struct ipsec_iflist) * IPSEC_HASH_SIZE,
262 for (i = 0; i < IPSEC_HASH_SIZE; i++)
263 CK_LIST_INIT(&hash[i]);
269 vnet_ipsec_init(const void *unused __unused)
272 V_ipsec_idhtbl = ipsec_hashinit();
274 V_ipsec4_srchtbl = ipsec_hashinit();
275 if (IS_DEFAULT_VNET(curvnet))
276 ipsec4_srctab = ip_encap_register_srcaddr(ipsec_srcaddr,
280 V_ipsec6_srchtbl = ipsec_hashinit();
281 if (IS_DEFAULT_VNET(curvnet))
282 ipsec6_srctab = ip6_encap_register_srcaddr(ipsec_srcaddr,
285 V_ipsec_cloner = if_clone_simple(ipsecname, ipsec_clone_create,
286 ipsec_clone_destroy, 0);
288 VNET_SYSINIT(vnet_ipsec_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
289 vnet_ipsec_init, NULL);
292 vnet_ipsec_uninit(const void *unused __unused)
295 if_clone_detach(V_ipsec_cloner);
296 free(V_ipsec_idhtbl, M_IPSEC);
298 * Use V_ipsec_idhtbl pointer as indicator that VNET is going to be
299 * destroyed, it is used by ipsec_srcaddr() callback.
301 V_ipsec_idhtbl = NULL;
305 if (IS_DEFAULT_VNET(curvnet))
306 ip_encap_unregister_srcaddr(ipsec4_srctab);
307 free(V_ipsec4_srchtbl, M_IPSEC);
310 if (IS_DEFAULT_VNET(curvnet))
311 ip6_encap_unregister_srcaddr(ipsec6_srctab);
312 free(V_ipsec6_srchtbl, M_IPSEC);
315 VNET_SYSUNINIT(vnet_ipsec_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
316 vnet_ipsec_uninit, NULL);
318 static struct secpolicy *
319 ipsec_getpolicy(struct ipsec_softc *sc, int dir, sa_family_t af)
325 return (sc->sp[(dir == IPSEC_DIR_INBOUND ? 0: 1)]);
329 return (sc->sp[(dir == IPSEC_DIR_INBOUND ? 0: 1)
339 static struct secasindex *
340 ipsec_getsaidx(struct ipsec_softc *sc, int dir, sa_family_t af)
342 struct secpolicy *sp;
344 sp = ipsec_getpolicy(sc, dir, af);
347 return (&sp->req[0]->saidx);
351 ipsec_transmit(struct ifnet *ifp, struct mbuf *m)
354 struct ipsec_softc *sc;
355 struct secpolicy *sp;
362 error = mac_ifnet_check_transmit(ifp, m);
370 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
371 (ifp->if_flags & IFF_MONITOR) != 0 ||
372 (ifp->if_flags & IFF_UP) == 0 || sc->family == 0) {
377 /* Determine address family to correctly handle packet in BPF */
378 ip = mtod(m, struct ip *);
386 case (IPV6_VERSION >> 4):
391 error = EAFNOSUPPORT;
398 * XXX: for now just check presence of IPSEC_OUT_DONE mbuf tag.
399 * We can read full chain and compare destination address,
400 * proto and mode from xform_history with values from softc.
402 if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL) {
407 sp = ipsec_getpolicy(sc, IPSEC_DIR_OUTBOUND, af);
409 M_SETFIB(m, sc->fibnum);
411 BPF_MTAP2(ifp, &af, sizeof(af), m);
412 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
413 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
418 error = ipsec4_process_packet(m, sp, NULL);
423 error = ipsec6_process_packet(m, sp, NULL);
427 panic("%s: unknown address family\n", __func__);
431 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
437 ipsec_qflush(struct ifnet *ifp __unused)
443 ipsec_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
447 return (ifp->if_transmit(ifp, m));
451 ipsec_if_input(struct mbuf *m, struct secasvar *sav, uint32_t af)
454 struct secasindex *saidx;
455 struct ipsec_softc *sc;
458 if (sav->state != SADB_SASTATE_MATURE &&
459 sav->state != SADB_SASTATE_DYING) {
464 if (sav->sah->saidx.mode != IPSEC_MODE_TUNNEL ||
465 sav->sah->saidx.proto != IPPROTO_ESP)
469 CK_LIST_FOREACH(sc, ipsec_idhash(sav->sah->saidx.reqid), idhash) {
472 saidx = ipsec_getsaidx(sc, IPSEC_DIR_INBOUND,
473 sav->sah->saidx.src.sa.sa_family);
474 /* SA's reqid should match reqid in SP */
476 sav->sah->saidx.reqid != saidx->reqid)
478 /* SAH's addresses should match tunnel endpoints. */
479 if (key_sockaddrcmp(&sav->sah->saidx.dst.sa,
480 &saidx->dst.sa, 0) != 0)
482 if (key_sockaddrcmp(&sav->sah->saidx.src.sa,
483 &saidx->src.sa, 0) == 0)
488 /* Tunnel was not found. Nothing to do. */
492 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
493 (ifp->if_flags & IFF_UP) == 0) {
499 * We found matching and working tunnel.
500 * Set its ifnet as receiving interface.
502 m->m_pkthdr.rcvif = ifp;
505 M_SETFIB(m, ifp->if_fib);
506 BPF_MTAP2(ifp, &af, sizeof(af), m);
507 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
508 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
509 if ((ifp->if_flags & IFF_MONITOR) != 0) {
519 ipsec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
521 struct ifreq *ifr = (struct ifreq*)data;
522 struct sockaddr *dst, *src;
523 struct ipsec_softc *sc;
524 struct secasindex *saidx;
526 struct sockaddr_in *sin = NULL;
529 struct sockaddr_in6 *sin6 = NULL;
536 ifp->if_flags |= IFF_UP;
543 if (ifr->ifr_mtu < IPSEC_MTU_MIN ||
544 ifr->ifr_mtu > IPSEC_MTU_MAX)
547 ifp->if_mtu = ifr->ifr_mtu;
550 sx_xlock(&ipsec_ioctl_sx);
552 /* Check that softc is still here */
561 case SIOCSIFPHYADDR_IN6:
567 src = (struct sockaddr *)
568 &(((struct in_aliasreq *)data)->ifra_addr);
569 dst = (struct sockaddr *)
570 &(((struct in_aliasreq *)data)->ifra_dstaddr);
574 case SIOCSIFPHYADDR_IN6:
575 src = (struct sockaddr *)
576 &(((struct in6_aliasreq *)data)->ifra_addr);
577 dst = (struct sockaddr *)
578 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
584 /* sa_family must be equal */
585 if (src->sa_family != dst->sa_family ||
586 src->sa_len != dst->sa_len)
589 /* validate sa_len */
590 switch (src->sa_family) {
593 if (src->sa_len != sizeof(struct sockaddr_in))
599 if (src->sa_len != sizeof(struct sockaddr_in6))
604 error = EAFNOSUPPORT;
607 /* check sa_family looks sane for the cmd */
608 error = EAFNOSUPPORT;
612 if (src->sa_family == AF_INET)
617 case SIOCSIFPHYADDR_IN6:
618 if (src->sa_family == AF_INET6)
623 error = EADDRNOTAVAIL;
624 switch (src->sa_family) {
627 if (satosin(src)->sin_addr.s_addr == INADDR_ANY ||
628 satosin(dst)->sin_addr.s_addr == INADDR_ANY)
634 if (IN6_IS_ADDR_UNSPECIFIED(
635 &satosin6(src)->sin6_addr) ||
636 IN6_IS_ADDR_UNSPECIFIED(
637 &satosin6(dst)->sin6_addr))
640 * Check validity of the scope zone ID of the
641 * addresses, and convert it into the kernel
642 * internal form if necessary.
644 error = sa6_embedscope(satosin6(src), 0);
647 error = sa6_embedscope(satosin6(dst), 0);
652 error = ipsec_set_addresses(ifp, src, dst);
655 ipsec_delete_tunnel(sc);
657 case SIOCGIFPSRCADDR:
658 case SIOCGIFPDSTADDR:
660 case SIOCGIFPSRCADDR_IN6:
661 case SIOCGIFPDSTADDR_IN6:
663 if (sc->family == 0) {
664 error = EADDRNOTAVAIL;
667 saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND, sc->family);
674 case SIOCGIFPSRCADDR:
675 case SIOCGIFPDSTADDR:
676 if (saidx->src.sa.sa_family != AF_INET) {
677 error = EADDRNOTAVAIL;
680 sin = (struct sockaddr_in *)&ifr->ifr_addr;
681 memset(sin, 0, sizeof(*sin));
682 sin->sin_family = AF_INET;
683 sin->sin_len = sizeof(*sin);
687 case SIOCGIFPSRCADDR_IN6:
688 case SIOCGIFPDSTADDR_IN6:
689 if (saidx->src.sa.sa_family != AF_INET6) {
690 error = EADDRNOTAVAIL;
693 sin6 = (struct sockaddr_in6 *)
694 &(((struct in6_ifreq *)data)->ifr_addr);
695 memset(sin6, 0, sizeof(*sin6));
696 sin6->sin6_family = AF_INET6;
697 sin6->sin6_len = sizeof(*sin6);
701 error = EAFNOSUPPORT;
706 case SIOCGIFPSRCADDR:
707 sin->sin_addr = saidx->src.sin.sin_addr;
709 case SIOCGIFPDSTADDR:
710 sin->sin_addr = saidx->dst.sin.sin_addr;
714 case SIOCGIFPSRCADDR_IN6:
715 sin6->sin6_addr = saidx->src.sin6.sin6_addr;
717 case SIOCGIFPDSTADDR_IN6:
718 sin6->sin6_addr = saidx->dst.sin6.sin6_addr;
727 case SIOCGIFPSRCADDR:
728 case SIOCGIFPDSTADDR:
729 error = prison_if(curthread->td_ucred,
730 (struct sockaddr *)sin);
732 memset(sin, 0, sizeof(*sin));
736 case SIOCGIFPSRCADDR_IN6:
737 case SIOCGIFPDSTADDR_IN6:
738 error = prison_if(curthread->td_ucred,
739 (struct sockaddr *)sin6);
741 error = sa6_recoverscope(sin6);
743 memset(sin6, 0, sizeof(*sin6));
748 ifr->ifr_fib = sc->fibnum;
751 if ((error = priv_check(curthread, PRIV_NET_SETIFFIB)) != 0)
753 if (ifr->ifr_fib >= rt_numfibs)
756 sc->fibnum = ifr->ifr_fib;
760 error = copyout(&reqid, ifr_data_get_ptr(ifr), sizeof(reqid));
763 if ((error = priv_check(curthread, PRIV_NET_SETIFCAP)) != 0)
765 error = copyin(ifr_data_get_ptr(ifr), &reqid, sizeof(reqid));
768 error = ipsec_set_reqid(sc, reqid);
775 sx_xunlock(&ipsec_ioctl_sx);
780 * Check that ingress address belongs to local host.
783 ipsec_set_running(struct ipsec_softc *sc)
785 struct secasindex *saidx;
788 saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND, sc->family);
792 switch (sc->family) {
795 localip = in_localip(saidx->src.sin.sin_addr);
800 localip = in6_localip(&saidx->src.sin6.sin6_addr);
805 sc->ifp->if_drv_flags |= IFF_DRV_RUNNING;
807 sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
811 * ifaddr_event handler.
812 * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
813 * source address spoofing.
816 ipsec_srcaddr(void *arg __unused, const struct sockaddr *sa,
819 struct ipsec_softc *sc;
820 struct secasindex *saidx;
821 struct ipsec_iflist *iflist;
823 /* Check that VNET is ready */
824 if (V_ipsec_idhtbl == NULL)
828 iflist = ipsec_srchash(sa);
831 CK_LIST_FOREACH(sc, iflist, srchash) {
834 saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND, sa->sa_family);
836 key_sockaddrcmp(&saidx->src.sa, sa, 0) != 0)
838 ipsec_set_running(sc);
843 * Allocate new private security policies for tunneling interface.
844 * Each tunneling interface has following security policies for
846 * 0.0.0.0/0[any] 0.0.0.0/0[any] -P in \
847 * ipsec esp/tunnel/RemoteIP-LocalIP/unique:reqid
848 * 0.0.0.0/0[any] 0.0.0.0/0[any] -P out \
849 * ipsec esp/tunnel/LocalIP-RemoteIP/unique:reqid
852 ipsec_newpolicies(struct ipsec_softc *sc, struct secpolicy *sp[IPSEC_SPCOUNT],
853 const struct sockaddr *src, const struct sockaddr *dst, uint32_t reqid)
855 struct ipsecrequest *isr;
858 memset(sp, 0, sizeof(struct secpolicy *) * IPSEC_SPCOUNT);
859 for (i = 0; i < IPSEC_SPCOUNT; i++) {
860 if ((sp[i] = key_newsp()) == NULL)
862 if ((isr = ipsec_newisr()) == NULL)
865 sp[i]->policy = IPSEC_POLICY_IPSEC;
866 sp[i]->state = IPSEC_SPSTATE_DEAD;
867 sp[i]->req[sp[i]->tcount++] = isr;
868 sp[i]->created = time_second;
869 /* Use priority field to store if_index */
870 sp[i]->priority = sc->ifp->if_index;
871 isr->level = IPSEC_LEVEL_UNIQUE;
872 isr->saidx.proto = IPPROTO_ESP;
873 isr->saidx.mode = IPSEC_MODE_TUNNEL;
874 isr->saidx.reqid = reqid;
876 sp[i]->spidx.dir = IPSEC_DIR_INBOUND;
877 bcopy(src, &isr->saidx.dst, src->sa_len);
878 bcopy(dst, &isr->saidx.src, dst->sa_len);
880 sp[i]->spidx.dir = IPSEC_DIR_OUTBOUND;
881 bcopy(src, &isr->saidx.src, src->sa_len);
882 bcopy(dst, &isr->saidx.dst, dst->sa_len);
884 sp[i]->spidx.ul_proto = IPSEC_ULPROTO_ANY;
887 sp[i]->spidx.src.sa.sa_family =
888 sp[i]->spidx.dst.sa.sa_family = AF_INET;
889 sp[i]->spidx.src.sa.sa_len =
890 sp[i]->spidx.dst.sa.sa_len =
891 sizeof(struct sockaddr_in);
896 sp[i]->spidx.src.sa.sa_family =
897 sp[i]->spidx.dst.sa.sa_family = AF_INET6;
898 sp[i]->spidx.src.sa.sa_len =
899 sp[i]->spidx.dst.sa.sa_len = sizeof(struct sockaddr_in6);
904 for (i = 0; i < IPSEC_SPCOUNT; i++)
910 ipsec_check_reqid(uint32_t reqid)
912 struct ipsec_softc *sc;
914 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
915 CK_LIST_FOREACH(sc, ipsec_idhash(reqid), idhash) {
916 if (sc->reqid == reqid)
923 * We use key_newreqid() to automatically obtain unique reqid.
924 * Then we check that given id is unique, i.e. it is not used by
925 * another if_ipsec(4) interface. This macro limits the number of
926 * tries to get unique id.
928 #define IPSEC_REQID_TRYCNT 64
930 ipsec_init_reqid(struct ipsec_softc *sc)
935 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
936 if (sc->reqid != 0) /* already initialized */
939 trycount = IPSEC_REQID_TRYCNT;
940 while (--trycount > 0) {
941 reqid = key_newreqid();
942 if (ipsec_check_reqid(reqid) == 0)
948 CK_LIST_INSERT_HEAD(ipsec_idhash(reqid), sc, idhash);
953 * Set or update reqid for given tunneling interface.
954 * When specified reqid is zero, generate new one.
955 * We are protected by ioctl_sx lock from concurrent id generation.
956 * Also softc would not disappear while we hold ioctl_sx lock.
959 ipsec_set_reqid(struct ipsec_softc *sc, uint32_t reqid)
961 struct secasindex *saidx;
963 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
965 if (sc->reqid == reqid && reqid != 0)
969 /* Check that specified reqid doesn't exist */
970 if (ipsec_check_reqid(reqid) != 0)
972 if (sc->reqid != 0) {
973 CK_LIST_REMOVE(sc, idhash);
977 CK_LIST_INSERT_HEAD(ipsec_idhash(reqid), sc, idhash);
979 /* Generate new reqid */
980 if (ipsec_init_reqid(sc) != 0)
984 /* Tunnel isn't fully configured, just return. */
988 saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND, sc->family);
989 KASSERT(saidx != NULL,
990 ("saidx is NULL, but family is %d", sc->family));
991 return (ipsec_set_tunnel(sc, &saidx->src.sa, &saidx->dst.sa,
996 * Set tunnel endpoints addresses.
999 ipsec_set_addresses(struct ifnet *ifp, struct sockaddr *src,
1000 struct sockaddr *dst)
1002 struct ipsec_softc *sc;
1003 struct secasindex *saidx;
1005 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
1008 if (sc->family != 0) {
1009 saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND,
1011 if (saidx != NULL && saidx->reqid == sc->reqid &&
1012 key_sockaddrcmp(&saidx->src.sa, src, 0) == 0 &&
1013 key_sockaddrcmp(&saidx->dst.sa, dst, 0) == 0)
1014 return (0); /* Nothing has been changed. */
1016 /* If reqid is not set, generate new one. */
1017 if (ipsec_init_reqid(sc) != 0)
1019 return (ipsec_set_tunnel(sc, src, dst, sc->reqid));
1023 ipsec_set_tunnel(struct ipsec_softc *sc, struct sockaddr *src,
1024 struct sockaddr *dst, uint32_t reqid)
1026 struct epoch_tracker et;
1027 struct ipsec_iflist *iflist;
1028 struct secpolicy *sp[IPSEC_SPCOUNT];
1031 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
1033 /* Allocate SP with new addresses. */
1034 iflist = ipsec_srchash(src);
1035 if (iflist == NULL) {
1036 sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1037 return (EAFNOSUPPORT);
1039 if (ipsec_newpolicies(sc, sp, src, dst, reqid) == 0) {
1040 /* Add new policies to SPDB */
1041 if (key_register_ifnet(sp, IPSEC_SPCOUNT) != 0) {
1042 for (i = 0; i < IPSEC_SPCOUNT; i++)
1046 if (sc->family != 0)
1047 ipsec_delete_tunnel(sc);
1048 for (i = 0; i < IPSEC_SPCOUNT; i++)
1050 sc->family = src->sa_family;
1051 CK_LIST_INSERT_HEAD(iflist, sc, srchash);
1053 sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1056 NET_EPOCH_ENTER(et);
1057 ipsec_set_running(sc);
1063 ipsec_delete_tunnel(struct ipsec_softc *sc)
1067 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
1069 sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1070 if (sc->family != 0) {
1071 CK_LIST_REMOVE(sc, srchash);
1074 * Make sure that ipsec_if_input() will not do access
1075 * to softc's policies.
1079 key_unregister_ifnet(sc->sp, IPSEC_SPCOUNT);
1080 for (i = 0; i < IPSEC_SPCOUNT; i++)
1081 key_freesp(&sc->sp[i]);