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"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/fnv_hash.h>
40 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
46 #include <sys/errno.h>
47 #include <sys/sysctl.h>
53 #include <net/if_var.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 *);
172 static void ipsec_srcaddr(void *, const struct sockaddr *, int);
173 static int ipsec_ioctl(struct ifnet *, u_long, caddr_t);
174 static int ipsec_transmit(struct ifnet *, struct mbuf *);
175 static int ipsec_output(struct ifnet *, struct mbuf *,
176 const struct sockaddr *, struct route *);
177 static void ipsec_qflush(struct ifnet *);
178 static int ipsec_clone_create(struct if_clone *, int, caddr_t);
179 static void ipsec_clone_destroy(struct ifnet *);
181 VNET_DEFINE_STATIC(struct if_clone *, ipsec_cloner);
182 #define V_ipsec_cloner VNET(ipsec_cloner)
185 ipsec_clone_create(struct if_clone *ifc, int unit, caddr_t params)
187 struct ipsec_softc *sc;
190 sc = malloc(sizeof(*sc), M_IPSEC, M_WAITOK | M_ZERO);
191 sc->fibnum = curthread->td_proc->p_fibnum;
192 sc->ifp = ifp = if_alloc(IFT_TUNNEL);
194 if_initname(ifp, ipsecname, unit);
197 ifp->if_mtu = IPSEC_MTU;
198 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
199 ifp->if_ioctl = ipsec_ioctl;
200 ifp->if_transmit = ipsec_transmit;
201 ifp->if_qflush = ipsec_qflush;
202 ifp->if_output = ipsec_output;
204 bpfattach(ifp, DLT_NULL, sizeof(uint32_t));
210 ipsec_clone_destroy(struct ifnet *ifp)
212 struct ipsec_softc *sc;
214 sx_xlock(&ipsec_ioctl_sx);
216 ipsec_delete_tunnel(sc);
218 * Delete softc from idhash on interface destroy, since
219 * ipsec_delete_tunnel() keeps reqid unchanged.
222 CK_LIST_REMOVE(sc, idhash);
225 ifp->if_softc = NULL;
226 sx_xunlock(&ipsec_ioctl_sx);
233 static struct ipsec_iflist *
236 struct ipsec_iflist *hash;
239 hash = malloc(sizeof(struct ipsec_iflist) * IPSEC_HASH_SIZE,
241 for (i = 0; i < IPSEC_HASH_SIZE; i++)
242 CK_LIST_INIT(&hash[i]);
248 vnet_ipsec_init(const void *unused __unused)
251 V_ipsec_idhtbl = ipsec_hashinit();
253 V_ipsec4_srchtbl = ipsec_hashinit();
254 if (IS_DEFAULT_VNET(curvnet))
255 ipsec4_srctab = ip_encap_register_srcaddr(ipsec_srcaddr,
259 V_ipsec6_srchtbl = ipsec_hashinit();
260 if (IS_DEFAULT_VNET(curvnet))
261 ipsec6_srctab = ip6_encap_register_srcaddr(ipsec_srcaddr,
264 V_ipsec_cloner = if_clone_simple(ipsecname, ipsec_clone_create,
265 ipsec_clone_destroy, 0);
267 VNET_SYSINIT(vnet_ipsec_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
268 vnet_ipsec_init, NULL);
271 vnet_ipsec_uninit(const void *unused __unused)
274 if_clone_detach(V_ipsec_cloner);
275 free(V_ipsec_idhtbl, M_IPSEC);
277 * Use V_ipsec_idhtbl pointer as indicator that VNET is going to be
278 * destroyed, it is used by ipsec_srcaddr() callback.
280 V_ipsec_idhtbl = NULL;
284 if (IS_DEFAULT_VNET(curvnet))
285 ip_encap_unregister_srcaddr(ipsec4_srctab);
286 free(V_ipsec4_srchtbl, M_IPSEC);
289 if (IS_DEFAULT_VNET(curvnet))
290 ip6_encap_unregister_srcaddr(ipsec6_srctab);
291 free(V_ipsec6_srchtbl, M_IPSEC);
294 VNET_SYSUNINIT(vnet_ipsec_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
295 vnet_ipsec_uninit, NULL);
297 static struct secpolicy *
298 ipsec_getpolicy(struct ipsec_softc *sc, int dir, sa_family_t af)
304 return (sc->sp[(dir == IPSEC_DIR_INBOUND ? 0: 1)]);
308 return (sc->sp[(dir == IPSEC_DIR_INBOUND ? 0: 1)
318 static struct secasindex *
319 ipsec_getsaidx(struct ipsec_softc *sc, int dir, sa_family_t af)
321 struct secpolicy *sp;
323 sp = ipsec_getpolicy(sc, dir, af);
326 return (&sp->req[0]->saidx);
330 ipsec_transmit(struct ifnet *ifp, struct mbuf *m)
333 struct ipsec_softc *sc;
334 struct secpolicy *sp;
341 error = mac_ifnet_check_transmit(ifp, m);
349 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
350 (ifp->if_flags & IFF_MONITOR) != 0 ||
351 (ifp->if_flags & IFF_UP) == 0 || sc->family == 0) {
356 /* Determine address family to correctly handle packet in BPF */
357 ip = mtod(m, struct ip *);
365 case (IPV6_VERSION >> 4):
370 error = EAFNOSUPPORT;
377 * XXX: for now just check presence of IPSEC_OUT_DONE mbuf tag.
378 * We can read full chain and compare destination address,
379 * proto and mode from xform_history with values from softc.
381 if (m_tag_find(m, PACKET_TAG_IPSEC_OUT_DONE, NULL) != NULL) {
386 sp = ipsec_getpolicy(sc, IPSEC_DIR_OUTBOUND, af);
388 M_SETFIB(m, sc->fibnum);
390 BPF_MTAP2(ifp, &af, sizeof(af), m);
391 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
392 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
397 error = ipsec4_process_packet(m, sp, NULL);
402 error = ipsec6_process_packet(m, sp, NULL);
406 panic("%s: unknown address family\n", __func__);
410 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
416 ipsec_qflush(struct ifnet *ifp __unused)
422 ipsec_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
426 return (ifp->if_transmit(ifp, m));
430 ipsec_if_input(struct mbuf *m, struct secasvar *sav, uint32_t af)
433 struct secasindex *saidx;
434 struct ipsec_softc *sc;
437 if (sav->state != SADB_SASTATE_MATURE &&
438 sav->state != SADB_SASTATE_DYING) {
443 if (sav->sah->saidx.mode != IPSEC_MODE_TUNNEL ||
444 sav->sah->saidx.proto != IPPROTO_ESP)
448 CK_LIST_FOREACH(sc, ipsec_idhash(sav->sah->saidx.reqid), idhash) {
451 saidx = ipsec_getsaidx(sc, IPSEC_DIR_INBOUND,
452 sav->sah->saidx.src.sa.sa_family);
453 /* SA's reqid should match reqid in SP */
455 sav->sah->saidx.reqid != saidx->reqid)
457 /* SAH's addresses should match tunnel endpoints. */
458 if (key_sockaddrcmp(&sav->sah->saidx.dst.sa,
459 &saidx->dst.sa, 0) != 0)
461 if (key_sockaddrcmp(&sav->sah->saidx.src.sa,
462 &saidx->src.sa, 0) == 0)
467 /* Tunnel was not found. Nothing to do. */
471 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
472 (ifp->if_flags & IFF_UP) == 0) {
478 * We found matching and working tunnel.
479 * Set its ifnet as receiving interface.
481 m->m_pkthdr.rcvif = ifp;
484 M_SETFIB(m, ifp->if_fib);
485 BPF_MTAP2(ifp, &af, sizeof(af), m);
486 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
487 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
488 if ((ifp->if_flags & IFF_MONITOR) != 0) {
498 ipsec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
500 struct ifreq *ifr = (struct ifreq*)data;
501 struct sockaddr *dst, *src;
502 struct ipsec_softc *sc;
503 struct secasindex *saidx;
505 struct sockaddr_in *sin = NULL;
508 struct sockaddr_in6 *sin6 = NULL;
515 ifp->if_flags |= IFF_UP;
522 if (ifr->ifr_mtu < IPSEC_MTU_MIN ||
523 ifr->ifr_mtu > IPSEC_MTU_MAX)
526 ifp->if_mtu = ifr->ifr_mtu;
529 sx_xlock(&ipsec_ioctl_sx);
531 /* Check that softc is still here */
540 case SIOCSIFPHYADDR_IN6:
546 src = (struct sockaddr *)
547 &(((struct in_aliasreq *)data)->ifra_addr);
548 dst = (struct sockaddr *)
549 &(((struct in_aliasreq *)data)->ifra_dstaddr);
553 case SIOCSIFPHYADDR_IN6:
554 src = (struct sockaddr *)
555 &(((struct in6_aliasreq *)data)->ifra_addr);
556 dst = (struct sockaddr *)
557 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
563 /* sa_family must be equal */
564 if (src->sa_family != dst->sa_family ||
565 src->sa_len != dst->sa_len)
568 /* validate sa_len */
569 switch (src->sa_family) {
572 if (src->sa_len != sizeof(struct sockaddr_in))
578 if (src->sa_len != sizeof(struct sockaddr_in6))
583 error = EAFNOSUPPORT;
586 /* check sa_family looks sane for the cmd */
587 error = EAFNOSUPPORT;
591 if (src->sa_family == AF_INET)
596 case SIOCSIFPHYADDR_IN6:
597 if (src->sa_family == AF_INET6)
602 error = EADDRNOTAVAIL;
603 switch (src->sa_family) {
606 if (satosin(src)->sin_addr.s_addr == INADDR_ANY ||
607 satosin(dst)->sin_addr.s_addr == INADDR_ANY)
613 if (IN6_IS_ADDR_UNSPECIFIED(
614 &satosin6(src)->sin6_addr) ||
615 IN6_IS_ADDR_UNSPECIFIED(
616 &satosin6(dst)->sin6_addr))
619 * Check validity of the scope zone ID of the
620 * addresses, and convert it into the kernel
621 * internal form if necessary.
623 error = sa6_embedscope(satosin6(src), 0);
626 error = sa6_embedscope(satosin6(dst), 0);
631 error = ipsec_set_addresses(ifp, src, dst);
634 ipsec_delete_tunnel(sc);
636 case SIOCGIFPSRCADDR:
637 case SIOCGIFPDSTADDR:
639 case SIOCGIFPSRCADDR_IN6:
640 case SIOCGIFPDSTADDR_IN6:
642 if (sc->family == 0) {
643 error = EADDRNOTAVAIL;
646 saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND, sc->family);
649 case SIOCGIFPSRCADDR:
650 case SIOCGIFPDSTADDR:
651 if (saidx->src.sa.sa_family != AF_INET) {
652 error = EADDRNOTAVAIL;
655 sin = (struct sockaddr_in *)&ifr->ifr_addr;
656 memset(sin, 0, sizeof(*sin));
657 sin->sin_family = AF_INET;
658 sin->sin_len = sizeof(*sin);
662 case SIOCGIFPSRCADDR_IN6:
663 case SIOCGIFPDSTADDR_IN6:
664 if (saidx->src.sa.sa_family != AF_INET6) {
665 error = EADDRNOTAVAIL;
668 sin6 = (struct sockaddr_in6 *)
669 &(((struct in6_ifreq *)data)->ifr_addr);
670 memset(sin6, 0, sizeof(*sin6));
671 sin6->sin6_family = AF_INET6;
672 sin6->sin6_len = sizeof(*sin6);
676 error = EAFNOSUPPORT;
681 case SIOCGIFPSRCADDR:
682 sin->sin_addr = saidx->src.sin.sin_addr;
684 case SIOCGIFPDSTADDR:
685 sin->sin_addr = saidx->dst.sin.sin_addr;
689 case SIOCGIFPSRCADDR_IN6:
690 sin6->sin6_addr = saidx->src.sin6.sin6_addr;
692 case SIOCGIFPDSTADDR_IN6:
693 sin6->sin6_addr = saidx->dst.sin6.sin6_addr;
702 case SIOCGIFPSRCADDR:
703 case SIOCGIFPDSTADDR:
704 error = prison_if(curthread->td_ucred,
705 (struct sockaddr *)sin);
707 memset(sin, 0, sizeof(*sin));
711 case SIOCGIFPSRCADDR_IN6:
712 case SIOCGIFPDSTADDR_IN6:
713 error = prison_if(curthread->td_ucred,
714 (struct sockaddr *)sin6);
716 error = sa6_recoverscope(sin6);
718 memset(sin6, 0, sizeof(*sin6));
723 ifr->ifr_fib = sc->fibnum;
726 if ((error = priv_check(curthread, PRIV_NET_SETIFFIB)) != 0)
728 if (ifr->ifr_fib >= rt_numfibs)
731 sc->fibnum = ifr->ifr_fib;
735 error = copyout(&reqid, ifr_data_get_ptr(ifr), sizeof(reqid));
738 if ((error = priv_check(curthread, PRIV_NET_SETIFCAP)) != 0)
740 error = copyin(ifr_data_get_ptr(ifr), &reqid, sizeof(reqid));
743 error = ipsec_set_reqid(sc, reqid);
750 sx_xunlock(&ipsec_ioctl_sx);
755 * Check that ingress address belongs to local host.
758 ipsec_set_running(struct ipsec_softc *sc)
760 struct secasindex *saidx;
763 saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND, sc->family);
765 switch (sc->family) {
768 localip = in_localip(saidx->src.sin.sin_addr);
773 localip = in6_localip(&saidx->src.sin6.sin6_addr);
778 sc->ifp->if_drv_flags |= IFF_DRV_RUNNING;
780 sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
784 * ifaddr_event handler.
785 * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
786 * source address spoofing.
789 ipsec_srcaddr(void *arg __unused, const struct sockaddr *sa,
792 struct ipsec_softc *sc;
793 struct secasindex *saidx;
795 /* Check that VNET is ready */
796 if (V_ipsec_idhtbl == NULL)
799 MPASS(in_epoch(net_epoch_preempt));
800 CK_LIST_FOREACH(sc, ipsec_srchash(sa), srchash) {
803 saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND, sa->sa_family);
805 key_sockaddrcmp(&saidx->src.sa, sa, 0) != 0)
807 ipsec_set_running(sc);
812 * Allocate new private security policies for tunneling interface.
813 * Each tunneling interface has following security policies for
815 * 0.0.0.0/0[any] 0.0.0.0/0[any] -P in \
816 * ipsec esp/tunnel/RemoteIP-LocalIP/unique:reqid
817 * 0.0.0.0/0[any] 0.0.0.0/0[any] -P out \
818 * ipsec esp/tunnel/LocalIP-RemoteIP/unique:reqid
821 ipsec_newpolicies(struct ipsec_softc *sc, struct secpolicy *sp[IPSEC_SPCOUNT],
822 const struct sockaddr *src, const struct sockaddr *dst, uint32_t reqid)
824 struct ipsecrequest *isr;
827 memset(sp, 0, sizeof(struct secpolicy *) * IPSEC_SPCOUNT);
828 for (i = 0; i < IPSEC_SPCOUNT; i++) {
829 if ((sp[i] = key_newsp()) == NULL)
831 if ((isr = ipsec_newisr()) == NULL)
834 sp[i]->policy = IPSEC_POLICY_IPSEC;
835 sp[i]->state = IPSEC_SPSTATE_DEAD;
836 sp[i]->req[sp[i]->tcount++] = isr;
837 sp[i]->created = time_second;
838 /* Use priority field to store if_index */
839 sp[i]->priority = sc->ifp->if_index;
840 isr->level = IPSEC_LEVEL_UNIQUE;
841 isr->saidx.proto = IPPROTO_ESP;
842 isr->saidx.mode = IPSEC_MODE_TUNNEL;
843 isr->saidx.reqid = reqid;
845 sp[i]->spidx.dir = IPSEC_DIR_INBOUND;
846 bcopy(src, &isr->saidx.dst, src->sa_len);
847 bcopy(dst, &isr->saidx.src, dst->sa_len);
849 sp[i]->spidx.dir = IPSEC_DIR_OUTBOUND;
850 bcopy(src, &isr->saidx.src, src->sa_len);
851 bcopy(dst, &isr->saidx.dst, dst->sa_len);
853 sp[i]->spidx.ul_proto = IPSEC_ULPROTO_ANY;
856 sp[i]->spidx.src.sa.sa_family =
857 sp[i]->spidx.dst.sa.sa_family = AF_INET;
858 sp[i]->spidx.src.sa.sa_len =
859 sp[i]->spidx.dst.sa.sa_len =
860 sizeof(struct sockaddr_in);
865 sp[i]->spidx.src.sa.sa_family =
866 sp[i]->spidx.dst.sa.sa_family = AF_INET6;
867 sp[i]->spidx.src.sa.sa_len =
868 sp[i]->spidx.dst.sa.sa_len = sizeof(struct sockaddr_in6);
873 for (i = 0; i < IPSEC_SPCOUNT; i++)
879 ipsec_check_reqid(uint32_t reqid)
881 struct ipsec_softc *sc;
883 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
884 CK_LIST_FOREACH(sc, ipsec_idhash(reqid), idhash) {
885 if (sc->reqid == reqid)
892 * We use key_newreqid() to automatically obtain unique reqid.
893 * Then we check that given id is unique, i.e. it is not used by
894 * another if_ipsec(4) interface. This macro limits the number of
895 * tries to get unique id.
897 #define IPSEC_REQID_TRYCNT 64
899 ipsec_init_reqid(struct ipsec_softc *sc)
904 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
905 if (sc->reqid != 0) /* already initialized */
908 trycount = IPSEC_REQID_TRYCNT;
909 while (--trycount > 0) {
910 reqid = key_newreqid();
911 if (ipsec_check_reqid(reqid) == 0)
917 CK_LIST_INSERT_HEAD(ipsec_idhash(reqid), sc, idhash);
922 * Set or update reqid for given tunneling interface.
923 * When specified reqid is zero, generate new one.
924 * We are protected by ioctl_sx lock from concurrent id generation.
925 * Also softc would not disappear while we hold ioctl_sx lock.
928 ipsec_set_reqid(struct ipsec_softc *sc, uint32_t reqid)
930 struct secasindex *saidx;
932 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
934 if (sc->reqid == reqid && reqid != 0)
938 /* Check that specified reqid doesn't exist */
939 if (ipsec_check_reqid(reqid) != 0)
941 if (sc->reqid != 0) {
942 CK_LIST_REMOVE(sc, idhash);
946 CK_LIST_INSERT_HEAD(ipsec_idhash(reqid), sc, idhash);
948 /* Generate new reqid */
949 if (ipsec_init_reqid(sc) != 0)
953 /* Tunnel isn't fully configured, just return. */
957 saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND, sc->family);
958 KASSERT(saidx != NULL,
959 ("saidx is NULL, but family is %d", sc->family));
960 return (ipsec_set_tunnel(sc, &saidx->src.sa, &saidx->dst.sa,
965 * Set tunnel endpoints addresses.
968 ipsec_set_addresses(struct ifnet *ifp, struct sockaddr *src,
969 struct sockaddr *dst)
971 struct ipsec_softc *sc;
972 struct secasindex *saidx;
974 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
977 if (sc->family != 0) {
978 saidx = ipsec_getsaidx(sc, IPSEC_DIR_OUTBOUND,
980 if (saidx != NULL && saidx->reqid == sc->reqid &&
981 key_sockaddrcmp(&saidx->src.sa, src, 0) == 0 &&
982 key_sockaddrcmp(&saidx->dst.sa, dst, 0) == 0)
983 return (0); /* Nothing has been changed. */
986 /* If reqid is not set, generate new one. */
987 if (ipsec_init_reqid(sc) != 0)
989 return (ipsec_set_tunnel(sc, src, dst, sc->reqid));
993 ipsec_set_tunnel(struct ipsec_softc *sc, struct sockaddr *src,
994 struct sockaddr *dst, uint32_t reqid)
996 struct secpolicy *sp[IPSEC_SPCOUNT];
999 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
1001 /* Allocate SP with new addresses. */
1002 if (ipsec_newpolicies(sc, sp, src, dst, reqid) == 0) {
1003 /* Add new policies to SPDB */
1004 if (key_register_ifnet(sp, IPSEC_SPCOUNT) != 0) {
1005 for (i = 0; i < IPSEC_SPCOUNT; i++)
1009 if (sc->family != 0)
1010 ipsec_delete_tunnel(sc);
1011 for (i = 0; i < IPSEC_SPCOUNT; i++)
1013 sc->family = src->sa_family;
1014 CK_LIST_INSERT_HEAD(ipsec_srchash(src), sc, srchash);
1016 sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1019 ipsec_set_running(sc);
1024 ipsec_delete_tunnel(struct ipsec_softc *sc)
1028 sx_assert(&ipsec_ioctl_sx, SA_XLOCKED);
1030 sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1031 if (sc->family != 0) {
1032 CK_LIST_REMOVE(sc, srchash);
1035 * Make sure that ipsec_if_input() will not do access
1036 * to softc's policies.
1040 key_unregister_ifnet(sc->sp, IPSEC_SPCOUNT);
1041 for (i = 0; i < IPSEC_SPCOUNT; i++)
1042 key_freesp(&sc->sp[i]);