xref: /openbsd-src/sys/net/pfkeyv2.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /* $OpenBSD: pfkeyv2.c,v 1.198 2019/07/17 18:52:46 bluhm Exp $ */
2 
3 /*
4  *	@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
5  *
6  * NRL grants permission for redistribution and use in source and binary
7  * forms, with or without modification, of the software and documentation
8  * created at NRL provided that the following conditions are met:
9  *
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.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgements:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  *	This product includes software developed at the Information
20  *	Technology Division, US Naval Research Laboratory.
21  * 4. Neither the name of the NRL nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
26  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
28  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  * The views and conclusions contained in the software and documentation
38  * are those of the authors and should not be interpreted as representing
39  * official policies, either expressed or implied, of the US Naval
40  * Research Laboratory (NRL).
41  */
42 
43 /*
44  * Copyright (c) 1995, 1996, 1997, 1998, 1999 Craig Metz. All rights reserved.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. Neither the name of the author nor the names of any contributors
55  *    may be used to endorse or promote products derived from this software
56  *    without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  */
70 
71 #include "pf.h"
72 
73 #include <sys/param.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/protosw.h>
77 #include <sys/domain.h>
78 #include <sys/systm.h>
79 #include <sys/mbuf.h>
80 #include <sys/kernel.h>
81 #include <sys/proc.h>
82 #include <sys/pool.h>
83 #include <sys/mutex.h>
84 
85 #include <net/route.h>
86 #include <netinet/ip_ipsp.h>
87 #include <net/pfkeyv2.h>
88 #include <net/radix.h>
89 #include <netinet/ip_ah.h>
90 #include <netinet/ip_esp.h>
91 #include <netinet/ip_ipcomp.h>
92 #include <crypto/blf.h>
93 
94 #if NPF > 0
95 #include <net/if.h>
96 #include <net/pfvar.h>
97 #endif
98 
99 #define	PFKEYSNDQ	8192
100 #define	PFKEYRCVQ	8192
101 
102 static const struct sadb_alg ealgs[] = {
103 	{ SADB_EALG_NULL, 0, 0, 0 },
104 	{ SADB_EALG_3DESCBC, 64, 192, 192 },
105 	{ SADB_X_EALG_BLF, 64, 40, BLF_MAXKEYLEN * 8},
106 	{ SADB_X_EALG_CAST, 64, 40, 128},
107 	{ SADB_X_EALG_AES, 128, 128, 256},
108 	{ SADB_X_EALG_AESCTR, 128, 128 + 32, 256 + 32}
109 };
110 
111 static const struct sadb_alg aalgs[] = {
112 	{ SADB_AALG_SHA1HMAC, 0, 160, 160 },
113 	{ SADB_AALG_MD5HMAC, 0, 128, 128 },
114 	{ SADB_X_AALG_RIPEMD160HMAC, 0, 160, 160 },
115 	{ SADB_X_AALG_SHA2_256, 0, 256, 256 },
116 	{ SADB_X_AALG_SHA2_384, 0, 384, 384 },
117 	{ SADB_X_AALG_SHA2_512, 0, 512, 512 }
118 };
119 
120 static const struct sadb_alg calgs[] = {
121 	{ SADB_X_CALG_DEFLATE, 0, 0, 0},
122 	{ SADB_X_CALG_LZS, 0, 0, 0}
123 };
124 
125 extern uint64_t sadb_exts_allowed_out[SADB_MAX+1];
126 extern uint64_t sadb_exts_required_out[SADB_MAX+1];
127 
128 extern struct pool ipsec_policy_pool;
129 
130 extern struct radix_node_head **spd_tables;
131 
132 struct pool pkpcb_pool;
133 #define PFKEY_MSG_MAXSZ 4096
134 const struct sockaddr pfkey_addr = { 2, PF_KEY, };
135 struct domain pfkeydomain;
136 
137 /*
138  * pfkey PCB
139  *
140  *  Locks used to protect struct members in this file:
141  *	I	immutable after creation
142  *	a	atomic operations
143  *	l	pkptable's lock
144  *	s	socket lock
145  */
146 struct pkpcb {
147 	struct socket		*kcb_socket;	/* [I] associated socket */
148 
149 	SRPL_ENTRY(pkpcb)	kcb_list;	/* [l] */
150 	struct refcnt		kcb_refcnt;	/* [a] */
151 	int			kcb_flags;	/* [s] */
152 	uint32_t		kcb_reg;	/* [s] Inc if SATYPE_MAX > 31 */
153 	uint32_t		kcb_pid;	/* [I] */
154 	unsigned int		kcb_rdomain;	/* [I] routing domain */
155 };
156 #define sotokeycb(so)		((struct pkpcb *)(so)->so_pcb)
157 #define keylock(kp)		solock((kp)->kcb_socket)
158 #define keyunlock(kp, s)	sounlock((kp)->kcb_socket, s)
159 
160 
161 struct dump_state {
162 	struct sadb_msg *sadb_msg;
163 	struct socket *socket;
164 };
165 
166 struct pkptable {
167 	SRPL_HEAD(, pkpcb)	pkp_list;
168 	struct srpl_rc		pkp_rc;
169 	struct rwlock		pkp_lk;
170 };
171 
172 struct pkptable pkptable;
173 struct mutex pfkeyv2_mtx = MUTEX_INITIALIZER(IPL_MPFLOOR);
174 static uint32_t pfkeyv2_seq = 1;
175 static int nregistered = 0;
176 static int npromisc = 0;
177 
178 void pfkey_init(void);
179 
180 int pfkeyv2_attach(struct socket *, int);
181 int pfkeyv2_detach(struct socket *);
182 int pfkeyv2_usrreq(struct socket *, int, struct mbuf *, struct mbuf *,
183     struct mbuf *, struct proc *);
184 int pfkeyv2_output(struct mbuf *, struct socket *, struct sockaddr *,
185     struct mbuf *);
186 int pfkey_sendup(struct pkpcb *, struct mbuf *, int);
187 int pfkeyv2_sa_flush(struct tdb *, void *, int);
188 int pfkeyv2_policy_flush(struct ipsec_policy *, void *, unsigned int);
189 int pfkeyv2_sysctl_policydumper(struct ipsec_policy *, void *, unsigned int);
190 
191 void	keycb_ref(void *, void *);
192 void	keycb_unref(void *, void *);
193 
194 /*
195  * Wrapper around m_devget(); copy data from contiguous buffer to mbuf
196  * chain.
197  */
198 int
199 pfdatatopacket(void *data, int len, struct mbuf **packet)
200 {
201 	if (!(*packet = m_devget(data, len, 0)))
202 		return (ENOMEM);
203 
204 	/* Make sure, all data gets zeroized on free */
205 	(*packet)->m_flags |= M_ZEROIZE;
206 
207 	return (0);
208 }
209 
210 static struct protosw pfkeysw[] = {
211 {
212   .pr_type      = SOCK_RAW,
213   .pr_domain    = &pfkeydomain,
214   .pr_protocol  = PF_KEY_V2,
215   .pr_flags     = PR_ATOMIC | PR_ADDR,
216   .pr_output    = pfkeyv2_output,
217   .pr_usrreq    = pfkeyv2_usrreq,
218   .pr_attach    = pfkeyv2_attach,
219   .pr_detach    = pfkeyv2_detach,
220   .pr_sysctl    = pfkeyv2_sysctl,
221 }
222 };
223 
224 struct domain pfkeydomain = {
225   .dom_family = PF_KEY,
226   .dom_name = "PF_KEY",
227   .dom_init = pfkey_init,
228   .dom_protosw = pfkeysw,
229   .dom_protoswNPROTOSW = &pfkeysw[nitems(pfkeysw)],
230 };
231 
232 void
233 keycb_ref(void *null, void *v)
234 {
235 	struct pkpcb *kp = v;
236 
237 	refcnt_take(&kp->kcb_refcnt);
238 }
239 
240 void
241 keycb_unref(void *null, void *v)
242 {
243 	struct pkpcb *kp = v;
244 
245 	refcnt_rele_wake(&kp->kcb_refcnt);
246 }
247 
248 void
249 pfkey_init(void)
250 {
251 	rn_init(sizeof(struct sockaddr_encap));
252 	srpl_rc_init(&pkptable.pkp_rc, keycb_ref, keycb_unref, NULL);
253 	rw_init(&pkptable.pkp_lk, "pfkey");
254 	SRPL_INIT(&pkptable.pkp_list);
255 	pool_init(&pkpcb_pool, sizeof(struct pkpcb), 0,
256 	    IPL_NONE, PR_WAITOK, "pkpcb", NULL);
257 }
258 
259 
260 /*
261  * Attach a new PF_KEYv2 socket.
262  */
263 int
264 pfkeyv2_attach(struct socket *so, int proto)
265 {
266 	struct pkpcb *kp;
267 	int error;
268 
269 	if ((so->so_state & SS_PRIV) == 0)
270 		return EACCES;
271 
272 	kp = pool_get(&pkpcb_pool, PR_WAITOK|PR_ZERO);
273 	so->so_pcb = kp;
274 	refcnt_init(&kp->kcb_refcnt);
275 
276 	error = soreserve(so, PFKEYSNDQ, PFKEYRCVQ);
277 	if (error) {
278 		pool_put(&pkpcb_pool, kp);
279 		return (error);
280 	}
281 
282 	kp->kcb_socket = so;
283 
284 	so->so_options |= SO_USELOOPBACK;
285 	soisconnected(so);
286 
287 	kp->kcb_pid = curproc->p_p->ps_pid;
288 	kp->kcb_rdomain = rtable_l2(curproc->p_p->ps_rtableid);
289 
290 	rw_enter(&pkptable.pkp_lk, RW_WRITE);
291 	SRPL_INSERT_HEAD_LOCKED(&pkptable.pkp_rc, &pkptable.pkp_list, kp, kcb_list);
292 	rw_exit(&pkptable.pkp_lk);
293 
294 	return (0);
295 }
296 
297 /*
298  * Close a PF_KEYv2 socket.
299  */
300 int
301 pfkeyv2_detach(struct socket *so)
302 {
303 	struct pkpcb *kp;
304 
305 	soassertlocked(so);
306 
307 	kp = sotokeycb(so);
308 	if (kp == NULL)
309 		return ENOTCONN;
310 
311 	if (kp->kcb_flags &
312 	    (PFKEYV2_SOCKETFLAGS_REGISTERED|PFKEYV2_SOCKETFLAGS_PROMISC)) {
313 		mtx_enter(&pfkeyv2_mtx);
314 		if (kp->kcb_flags & PFKEYV2_SOCKETFLAGS_REGISTERED)
315 			nregistered--;
316 
317 		if (kp->kcb_flags & PFKEYV2_SOCKETFLAGS_PROMISC)
318 			npromisc--;
319 		mtx_leave(&pfkeyv2_mtx);
320 	}
321 
322 	rw_enter(&pkptable.pkp_lk, RW_WRITE);
323 	SRPL_REMOVE_LOCKED(&pkptable.pkp_rc, &pkptable.pkp_list, kp, pkpcb,
324 	    kcb_list);
325 	rw_exit(&pkptable.pkp_lk);
326 
327 	/* wait for all references to drop */
328 	refcnt_finalize(&kp->kcb_refcnt, "pfkeyrefs");
329 
330 	so->so_pcb = NULL;
331 	KASSERT((so->so_state & SS_NOFDREF) == 0);
332 	pool_put(&pkpcb_pool, kp);
333 
334 	return (0);
335 }
336 
337 int
338 pfkeyv2_usrreq(struct socket *so, int req, struct mbuf *m,
339     struct mbuf *nam, struct mbuf *control, struct proc *p)
340 {
341 	struct pkpcb *kp;
342 	int error = 0;
343 
344 	if (req == PRU_CONTROL)
345 		return (EOPNOTSUPP);
346 
347 	soassertlocked(so);
348 
349 	if (control && control->m_len) {
350 		error = EOPNOTSUPP;
351 		goto release;
352 	}
353 
354 	kp = sotokeycb(so);
355 	if (kp == NULL) {
356 		error = EINVAL;
357 		goto release;
358 	}
359 
360 	switch (req) {
361 	/* no connect, bind, accept. Socket is connected from the start */
362 	case PRU_CONNECT:
363 	case PRU_BIND:
364 	case PRU_CONNECT2:
365 	case PRU_LISTEN:
366 	case PRU_ACCEPT:
367 		error = EOPNOTSUPP;
368 		break;
369 
370 	case PRU_DISCONNECT:
371 	case PRU_ABORT:
372 		soisdisconnected(so);
373 		break;
374 	case PRU_SHUTDOWN:
375 		socantsendmore(so);
376 		break;
377 	case PRU_SENSE:
378 		/* stat: don't bother with a blocksize. */
379 		break;
380 
381 	/* minimal support, just implement a fake peer address */
382 	case PRU_SOCKADDR:
383 		error = EINVAL;
384 		break;
385 	case PRU_PEERADDR:
386 		bcopy(&pfkey_addr, mtod(nam, caddr_t), pfkey_addr.sa_len);
387 		nam->m_len = pfkey_addr.sa_len;
388 		break;
389 
390 	case PRU_RCVOOB:
391 	case PRU_RCVD:
392 	case PRU_SENDOOB:
393 		error = EOPNOTSUPP;
394 		break;
395 	case PRU_SEND:
396 		if (nam) {
397 			error = EISCONN;
398 			break;
399 		}
400 		error = (*so->so_proto->pr_output)(m, so, NULL, NULL);
401 		m = NULL;
402 		break;
403 	default:
404 		panic("pfkeyv2_usrreq");
405 	}
406 
407  release:
408 	if (req != PRU_RCVD && req != PRU_RCVOOB && req != PRU_SENSE) {
409 		m_freem(control);
410 		m_freem(m);
411 	}
412 	return (error);
413 }
414 
415 int
416 pfkeyv2_output(struct mbuf *mbuf, struct socket *so,
417     struct sockaddr *dstaddr, struct mbuf *control)
418 {
419 	void *message;
420 	int error = 0;
421 
422 #ifdef DIAGNOSTIC
423 	if (!mbuf || !(mbuf->m_flags & M_PKTHDR)) {
424 		error = EINVAL;
425 		goto ret;
426 	}
427 #endif /* DIAGNOSTIC */
428 
429 	if (mbuf->m_pkthdr.len > PFKEY_MSG_MAXSZ) {
430 		error = EMSGSIZE;
431 		goto ret;
432 	}
433 
434 	if (!(message = malloc((unsigned long) mbuf->m_pkthdr.len,
435 	    M_PFKEY, M_DONTWAIT))) {
436 		error = ENOMEM;
437 		goto ret;
438 	}
439 
440 	m_copydata(mbuf, 0, mbuf->m_pkthdr.len, message);
441 
442 	error = pfkeyv2_send(so, message, mbuf->m_pkthdr.len);
443 
444 ret:
445 	m_freem(mbuf);
446 	return (error);
447 }
448 
449 int
450 pfkey_sendup(struct pkpcb *kp, struct mbuf *m0, int more)
451 {
452 	struct socket *so = kp->kcb_socket;
453 	struct mbuf *m;
454 
455 	soassertlocked(so);
456 
457 	if (more) {
458 		if (!(m = m_dup_pkt(m0, 0, M_DONTWAIT)))
459 			return (ENOMEM);
460 	} else
461 		m = m0;
462 
463 	if (!sbappendaddr(so, &so->so_rcv, &pfkey_addr, m, NULL)) {
464 		m_freem(m);
465 		return (ENOBUFS);
466 	}
467 
468 	sorwakeup(so);
469 	return (0);
470 }
471 
472 /*
473  * Send a PFKEYv2 message, possibly to many receivers, based on the
474  * satype of the socket (which is set by the REGISTER message), and the
475  * third argument.
476  */
477 int
478 pfkeyv2_sendmessage(void **headers, int mode, struct socket *so,
479     u_int8_t satype, int count, u_int rdomain)
480 {
481 	int i, j, rval, s;
482 	void *p, *buffer = NULL;
483 	struct mbuf *packet;
484 	struct pkpcb *kp;
485 	struct sadb_msg *smsg;
486 	struct srp_ref sr;
487 
488 	/* Find out how much space we'll need... */
489 	j = sizeof(struct sadb_msg);
490 
491 	for (i = 1; i <= SADB_EXT_MAX; i++)
492 		if (headers[i])
493 			j += ((struct sadb_ext *)headers[i])->sadb_ext_len *
494 			    sizeof(uint64_t);
495 
496 	/* ...and allocate it */
497 	if (!(buffer = malloc(j + sizeof(struct sadb_msg), M_PFKEY,
498 	    M_NOWAIT))) {
499 		rval = ENOMEM;
500 		goto ret;
501 	}
502 
503 	p = buffer + sizeof(struct sadb_msg);
504 	bcopy(headers[0], p, sizeof(struct sadb_msg));
505 	((struct sadb_msg *) p)->sadb_msg_len = j / sizeof(uint64_t);
506 	p += sizeof(struct sadb_msg);
507 
508 	/* Copy payloads in the packet */
509 	for (i = 1; i <= SADB_EXT_MAX; i++)
510 		if (headers[i]) {
511 			((struct sadb_ext *) headers[i])->sadb_ext_type = i;
512 			bcopy(headers[i], p, EXTLEN(headers[i]));
513 			p += EXTLEN(headers[i]);
514 		}
515 
516 	if ((rval = pfdatatopacket(buffer + sizeof(struct sadb_msg),
517 	    j, &packet)) != 0)
518 		goto ret;
519 
520 	switch (mode) {
521 	case PFKEYV2_SENDMESSAGE_UNICAST:
522 		/*
523 		 * Send message to the specified socket, plus all
524 		 * promiscuous listeners.
525 		 */
526 		s = solock(so);
527 		pfkey_sendup(sotokeycb(so), packet, 0);
528 		sounlock(so, s);
529 
530 		/*
531 		 * Promiscuous messages contain the original message
532 		 * encapsulated in another sadb_msg header.
533 		 */
534 		bzero(buffer, sizeof(struct sadb_msg));
535 		smsg = (struct sadb_msg *) buffer;
536 		smsg->sadb_msg_version = PF_KEY_V2;
537 		smsg->sadb_msg_type = SADB_X_PROMISC;
538 		smsg->sadb_msg_len = (sizeof(struct sadb_msg) + j) /
539 		    sizeof(uint64_t);
540 		smsg->sadb_msg_seq = 0;
541 
542 		/* Copy to mbuf chain */
543 		if ((rval = pfdatatopacket(buffer, sizeof(struct sadb_msg) + j,
544 		    &packet)) != 0)
545 			goto ret;
546 
547 		/*
548 		 * Search for promiscuous listeners, skipping the
549 		 * original destination.
550 		 */
551 		SRPL_FOREACH(kp, &sr, &pkptable.pkp_list, kcb_list) {
552 			if (kp->kcb_socket == so || kp->kcb_rdomain != rdomain)
553 				continue;
554 
555 			s = keylock(kp);
556 			if (kp->kcb_flags & PFKEYV2_SOCKETFLAGS_PROMISC)
557 				pfkey_sendup(kp, packet, 1);
558 			keyunlock(kp, s);
559 		}
560 		SRPL_LEAVE(&sr);
561 		m_freem(packet);
562 		break;
563 
564 	case PFKEYV2_SENDMESSAGE_REGISTERED:
565 		/*
566 		 * Send the message to all registered sockets that match
567 		 * the specified satype (e.g., all IPSEC-ESP negotiators)
568 		 */
569 		SRPL_FOREACH(kp, &sr, &pkptable.pkp_list, kcb_list) {
570 			if (kp->kcb_rdomain != rdomain)
571 				continue;
572 
573 			s = keylock(kp);
574 			if (kp->kcb_flags & PFKEYV2_SOCKETFLAGS_REGISTERED) {
575 				if (!satype) {
576 					/* Just send to everyone registered */
577 					pfkey_sendup(kp, packet, 1);
578 				} else {
579 					/* Check for specified satype */
580 					if ((1 << satype) & kp->kcb_reg)
581 						pfkey_sendup(kp, packet, 1);
582 				}
583 			}
584 			keyunlock(kp, s);
585 		}
586 		SRPL_LEAVE(&sr);
587 		/* Free last/original copy of the packet */
588 		m_freem(packet);
589 
590 		/* Encapsulate the original message "inside" an sadb_msg header */
591 		bzero(buffer, sizeof(struct sadb_msg));
592 		smsg = (struct sadb_msg *) buffer;
593 		smsg->sadb_msg_version = PF_KEY_V2;
594 		smsg->sadb_msg_type = SADB_X_PROMISC;
595 		smsg->sadb_msg_len = (sizeof(struct sadb_msg) + j) /
596 		    sizeof(uint64_t);
597 		smsg->sadb_msg_seq = 0;
598 
599 		/* Convert to mbuf chain */
600 		if ((rval = pfdatatopacket(buffer, sizeof(struct sadb_msg) + j,
601 		    &packet)) != 0)
602 			goto ret;
603 
604 		/* Send to all registered promiscuous listeners */
605 		SRPL_FOREACH(kp, &sr, &pkptable.pkp_list, kcb_list) {
606 			if (kp->kcb_rdomain != rdomain)
607 				continue;
608 
609 			s = keylock(kp);
610 			if ((kp->kcb_flags & PFKEYV2_SOCKETFLAGS_PROMISC) &&
611 			    !(kp->kcb_flags & PFKEYV2_SOCKETFLAGS_REGISTERED))
612 				pfkey_sendup(kp, packet, 1);
613 			keyunlock(kp, s);
614 		}
615 		SRPL_LEAVE(&sr);
616 		m_freem(packet);
617 		break;
618 
619 	case PFKEYV2_SENDMESSAGE_BROADCAST:
620 		/* Send message to all sockets */
621 		SRPL_FOREACH(kp, &sr, &pkptable.pkp_list, kcb_list) {
622 			if (kp->kcb_rdomain != rdomain)
623 				continue;
624 
625 			s = keylock(kp);
626 			pfkey_sendup(kp, packet, 1);
627 			keyunlock(kp, s);
628 		}
629 		SRPL_LEAVE(&sr);
630 		m_freem(packet);
631 		break;
632 	}
633 
634 ret:
635 	if (buffer != NULL) {
636 		bzero(buffer, j + sizeof(struct sadb_msg));
637 		free(buffer, M_PFKEY, 0);
638 	}
639 
640 	return (rval);
641 }
642 
643 /*
644  * Get SPD information for an ACQUIRE. We setup the message such that
645  * the SRC/DST payloads are relative to us (regardless of whether the
646  * SPD rule was for incoming or outgoing packets).
647  */
648 int
649 pfkeyv2_policy(struct ipsec_acquire *ipa, void **headers, void **buffer)
650 {
651 	union sockaddr_union sunion;
652 	struct sadb_protocol *sp;
653 	int rval, i, dir;
654 	void *p;
655 
656 	/* Find out how big a buffer we need */
657 	i = 4 * sizeof(struct sadb_address) + sizeof(struct sadb_protocol);
658 	bzero(&sunion, sizeof(union sockaddr_union));
659 
660 	switch (ipa->ipa_info.sen_type) {
661 	case SENT_IP4:
662 		i += 4 * PADUP(sizeof(struct sockaddr_in));
663 		sunion.sa.sa_family = AF_INET;
664 		sunion.sa.sa_len = sizeof(struct sockaddr_in);
665 		dir = ipa->ipa_info.sen_direction;
666 		break;
667 
668 #ifdef INET6
669 	case SENT_IP6:
670 		i += 4 * PADUP(sizeof(struct sockaddr_in6));
671 		sunion.sa.sa_family = AF_INET6;
672 		sunion.sa.sa_len = sizeof(struct sockaddr_in6);
673 		dir = ipa->ipa_info.sen_ip6_direction;
674 		break;
675 #endif /* INET6 */
676 
677 	default:
678 		return (EINVAL);
679 	}
680 
681 	if (!(p = malloc(i, M_PFKEY, M_NOWAIT | M_ZERO))) {
682 		rval = ENOMEM;
683 		goto ret;
684 	} else
685 		*buffer = p;
686 
687 	if (dir == IPSP_DIRECTION_OUT)
688 		headers[SADB_X_EXT_SRC_FLOW] = p;
689 	else
690 		headers[SADB_X_EXT_DST_FLOW] = p;
691 	switch (sunion.sa.sa_family) {
692 	case AF_INET:
693 		sunion.sin.sin_addr = ipa->ipa_info.sen_ip_src;
694 		sunion.sin.sin_port = ipa->ipa_info.sen_sport;
695 		break;
696 
697 #ifdef INET6
698 	case AF_INET6:
699 		sunion.sin6.sin6_addr = ipa->ipa_info.sen_ip6_src;
700 		sunion.sin6.sin6_port = ipa->ipa_info.sen_ip6_sport;
701 		break;
702 #endif /* INET6 */
703 	}
704 	export_address(&p, &sunion.sa);
705 
706 	if (dir == IPSP_DIRECTION_OUT)
707 		headers[SADB_X_EXT_SRC_MASK] = p;
708 	else
709 		headers[SADB_X_EXT_DST_MASK] = p;
710 	switch (sunion.sa.sa_family) {
711 	case AF_INET:
712 		sunion.sin.sin_addr = ipa->ipa_mask.sen_ip_src;
713 		sunion.sin.sin_port = ipa->ipa_mask.sen_sport;
714 		break;
715 
716 #ifdef INET6
717 	case AF_INET6:
718 		sunion.sin6.sin6_addr = ipa->ipa_mask.sen_ip6_src;
719 		sunion.sin6.sin6_port = ipa->ipa_mask.sen_ip6_sport;
720 		break;
721 #endif /* INET6 */
722 	}
723 	export_address(&p, &sunion.sa);
724 
725 	if (dir == IPSP_DIRECTION_OUT)
726 		headers[SADB_X_EXT_DST_FLOW] = p;
727 	else
728 		headers[SADB_X_EXT_SRC_FLOW] = p;
729 	switch (sunion.sa.sa_family) {
730 	case AF_INET:
731 		sunion.sin.sin_addr = ipa->ipa_info.sen_ip_dst;
732 		sunion.sin.sin_port = ipa->ipa_info.sen_dport;
733 		break;
734 
735 #ifdef INET6
736 	case AF_INET6:
737 		sunion.sin6.sin6_addr = ipa->ipa_info.sen_ip6_dst;
738 		sunion.sin6.sin6_port = ipa->ipa_info.sen_ip6_dport;
739 		break;
740 #endif /* INET6 */
741 	}
742 	export_address(&p, &sunion.sa);
743 
744 	if (dir == IPSP_DIRECTION_OUT)
745 		headers[SADB_X_EXT_DST_MASK] = p;
746 	else
747 		headers[SADB_X_EXT_SRC_MASK] = p;
748 	switch (sunion.sa.sa_family) {
749 	case AF_INET:
750 		sunion.sin.sin_addr = ipa->ipa_mask.sen_ip_dst;
751 		sunion.sin.sin_port = ipa->ipa_mask.sen_dport;
752 		break;
753 
754 #ifdef INET6
755 	case AF_INET6:
756 		sunion.sin6.sin6_addr = ipa->ipa_mask.sen_ip6_dst;
757 		sunion.sin6.sin6_port = ipa->ipa_mask.sen_ip6_dport;
758 		break;
759 #endif /* INET6 */
760 	}
761 	export_address(&p, &sunion.sa);
762 
763 	headers[SADB_X_EXT_FLOW_TYPE] = p;
764 	sp = p;
765 	sp->sadb_protocol_len = sizeof(struct sadb_protocol) /
766 	    sizeof(u_int64_t);
767 	switch (sunion.sa.sa_family) {
768 	case AF_INET:
769 		if (ipa->ipa_mask.sen_proto)
770 			sp->sadb_protocol_proto = ipa->ipa_info.sen_proto;
771 		sp->sadb_protocol_direction = ipa->ipa_info.sen_direction;
772 		break;
773 
774 #ifdef INET6
775 	case AF_INET6:
776 		if (ipa->ipa_mask.sen_ip6_proto)
777 			sp->sadb_protocol_proto = ipa->ipa_info.sen_ip6_proto;
778 		sp->sadb_protocol_direction = ipa->ipa_info.sen_ip6_direction;
779 		break;
780 #endif /* INET6 */
781 	}
782 
783 	rval = 0;
784 
785 ret:
786 	return (rval);
787 }
788 
789 /*
790  * Get all the information contained in an SA to a PFKEYV2 message.
791  */
792 int
793 pfkeyv2_get(struct tdb *tdb, void **headers, void **buffer, int *lenp)
794 {
795 	int rval, i;
796 	void *p;
797 
798 	/* Find how much space we need */
799 	i = sizeof(struct sadb_sa) + sizeof(struct sadb_lifetime) +
800 	    sizeof(struct sadb_x_counter);
801 
802 	if (tdb->tdb_soft_allocations || tdb->tdb_soft_bytes ||
803 	    tdb->tdb_soft_timeout || tdb->tdb_soft_first_use)
804 		i += sizeof(struct sadb_lifetime);
805 
806 	if (tdb->tdb_exp_allocations || tdb->tdb_exp_bytes ||
807 	    tdb->tdb_exp_timeout || tdb->tdb_exp_first_use)
808 		i += sizeof(struct sadb_lifetime);
809 
810 	if (tdb->tdb_last_used)
811 		i += sizeof(struct sadb_lifetime);
812 
813 	i += sizeof(struct sadb_address) + PADUP(tdb->tdb_src.sa.sa_len);
814 	i += sizeof(struct sadb_address) + PADUP(tdb->tdb_dst.sa.sa_len);
815 
816 	if (tdb->tdb_ids) {
817 		i += sizeof(struct sadb_ident) + PADUP(tdb->tdb_ids->id_local->len);
818 		i += sizeof(struct sadb_ident) + PADUP(tdb->tdb_ids->id_remote->len);
819 	}
820 
821 	if (tdb->tdb_amxkey)
822 		i += sizeof(struct sadb_key) + PADUP(tdb->tdb_amxkeylen);
823 
824 	if (tdb->tdb_emxkey)
825 		i += sizeof(struct sadb_key) + PADUP(tdb->tdb_emxkeylen);
826 
827 	if (tdb->tdb_filter.sen_type) {
828 		i += 2 * sizeof(struct sadb_protocol);
829 
830 		/* We'll need four of them: src, src mask, dst, dst mask. */
831 		switch (tdb->tdb_filter.sen_type) {
832 		case SENT_IP4:
833 			i += 4 * PADUP(sizeof(struct sockaddr_in));
834 			i += 4 * sizeof(struct sadb_address);
835 			break;
836 #ifdef INET6
837 		case SENT_IP6:
838 			i += 4 * PADUP(sizeof(struct sockaddr_in6));
839 			i += 4 * sizeof(struct sadb_address);
840 			break;
841 #endif /* INET6 */
842 		default:
843 			rval = EINVAL;
844 			goto ret;
845 		}
846 	}
847 
848 	if (tdb->tdb_onext) {
849 		i += sizeof(struct sadb_sa);
850 		i += sizeof(struct sadb_address) +
851 		    PADUP(tdb->tdb_onext->tdb_dst.sa.sa_len);
852 		i += sizeof(struct sadb_protocol);
853 	}
854 
855 	if (tdb->tdb_udpencap_port)
856 		i += sizeof(struct sadb_x_udpencap);
857 
858 #if NPF > 0
859 	if (tdb->tdb_tag)
860 		i += sizeof(struct sadb_x_tag) + PADUP(PF_TAG_NAME_SIZE);
861 	if (tdb->tdb_tap)
862 		i += sizeof(struct sadb_x_tap);
863 #endif
864 
865 	if (lenp)
866 		*lenp = i;
867 
868 	if (buffer == NULL) {
869 		rval = 0;
870 		goto ret;
871 	}
872 
873 	if (!(p = malloc(i, M_PFKEY, M_NOWAIT | M_ZERO))) {
874 		rval = ENOMEM;
875 		goto ret;
876 	} else
877 		*buffer = p;
878 
879 	headers[SADB_EXT_SA] = p;
880 
881 	export_sa(&p, tdb);  /* Export SA information (mostly flags) */
882 
883 	/* Export lifetimes where applicable */
884 	headers[SADB_EXT_LIFETIME_CURRENT] = p;
885 	export_lifetime(&p, tdb, PFKEYV2_LIFETIME_CURRENT);
886 
887 	if (tdb->tdb_soft_allocations || tdb->tdb_soft_bytes ||
888 	    tdb->tdb_soft_first_use || tdb->tdb_soft_timeout) {
889 		headers[SADB_EXT_LIFETIME_SOFT] = p;
890 		export_lifetime(&p, tdb, PFKEYV2_LIFETIME_SOFT);
891 	}
892 
893 	if (tdb->tdb_exp_allocations || tdb->tdb_exp_bytes ||
894 	    tdb->tdb_exp_first_use || tdb->tdb_exp_timeout) {
895 		headers[SADB_EXT_LIFETIME_HARD] = p;
896 		export_lifetime(&p, tdb, PFKEYV2_LIFETIME_HARD);
897 	}
898 
899 	if (tdb->tdb_last_used) {
900 		headers[SADB_X_EXT_LIFETIME_LASTUSE] = p;
901 		export_lifetime(&p, tdb, PFKEYV2_LIFETIME_LASTUSE);
902 	}
903 
904 	/* Export TDB source address */
905 	headers[SADB_EXT_ADDRESS_SRC] = p;
906 	export_address(&p, &tdb->tdb_src.sa);
907 
908 	/* Export TDB destination address */
909 	headers[SADB_EXT_ADDRESS_DST] = p;
910 	export_address(&p, &tdb->tdb_dst.sa);
911 
912 	/* Export source/destination identities, if present */
913 	if (tdb->tdb_ids)
914 		export_identities(&p, tdb->tdb_ids, tdb->tdb_ids_swapped, headers);
915 
916 	/* Export authentication key, if present */
917 	if (tdb->tdb_amxkey) {
918 		headers[SADB_EXT_KEY_AUTH] = p;
919 		export_key(&p, tdb, PFKEYV2_AUTHENTICATION_KEY);
920 	}
921 
922 	/* Export encryption key, if present */
923 	if (tdb->tdb_emxkey) {
924 		headers[SADB_EXT_KEY_ENCRYPT] = p;
925 		export_key(&p, tdb, PFKEYV2_ENCRYPTION_KEY);
926 	}
927 
928 	/* Export flow/filter, if present */
929 	if (tdb->tdb_filter.sen_type)
930 		export_flow(&p, IPSP_IPSEC_USE, &tdb->tdb_filter,
931 		    &tdb->tdb_filtermask, headers);
932 
933 	if (tdb->tdb_onext) {
934 		headers[SADB_X_EXT_SA2] = p;
935 		export_sa(&p, tdb->tdb_onext);
936 		headers[SADB_X_EXT_DST2] = p;
937 		export_address(&p, &tdb->tdb_onext->tdb_dst.sa);
938 		headers[SADB_X_EXT_SATYPE2] = p;
939 		export_satype(&p, tdb->tdb_onext);
940 	}
941 
942 	/* Export UDP encapsulation port, if present */
943 	if (tdb->tdb_udpencap_port) {
944 		headers[SADB_X_EXT_UDPENCAP] = p;
945 		export_udpencap(&p, tdb);
946 	}
947 
948 #if NPF > 0
949 	/* Export tag information, if present */
950 	if (tdb->tdb_tag) {
951 		headers[SADB_X_EXT_TAG] = p;
952 		export_tag(&p, tdb);
953 	}
954 
955 	/* Export tap enc(4) device information, if present */
956 	if (tdb->tdb_tap) {
957 		headers[SADB_X_EXT_TAP] = p;
958 		export_tap(&p, tdb);
959 	}
960 #endif
961 
962 	headers[SADB_X_EXT_COUNTER] = p;
963 	export_counter(&p, tdb);
964 
965 	rval = 0;
966 
967  ret:
968 	return (rval);
969 }
970 
971 /*
972  * Dump a TDB.
973  */
974 int
975 pfkeyv2_dump_walker(struct tdb *tdb, void *state, int last)
976 {
977 	struct dump_state *dump_state = (struct dump_state *) state;
978 	void *headers[SADB_EXT_MAX+1], *buffer;
979 	int rval;
980 
981 	/* If not satype was specified, dump all TDBs */
982 	if (!dump_state->sadb_msg->sadb_msg_satype ||
983 	    (tdb->tdb_satype == dump_state->sadb_msg->sadb_msg_satype)) {
984 		bzero(headers, sizeof(headers));
985 		headers[0] = (void *) dump_state->sadb_msg;
986 
987 		/* Get the information from the TDB to a PFKEYv2 message */
988 		if ((rval = pfkeyv2_get(tdb, headers, &buffer, NULL)) != 0)
989 			return (rval);
990 
991 		if (last)
992 			((struct sadb_msg *)headers[0])->sadb_msg_seq = 0;
993 
994 		/* Send the message to the specified socket */
995 		rval = pfkeyv2_sendmessage(headers,
996 		    PFKEYV2_SENDMESSAGE_UNICAST, dump_state->socket, 0, 0,
997 		    tdb->tdb_rdomain);
998 
999 		free(buffer, M_PFKEY, 0);
1000 		if (rval)
1001 			return (rval);
1002 	}
1003 
1004 	return (0);
1005 }
1006 
1007 /*
1008  * Delete an SA.
1009  */
1010 int
1011 pfkeyv2_sa_flush(struct tdb *tdb, void *satype_vp, int last)
1012 {
1013 	if (!(*((u_int8_t *) satype_vp)) ||
1014 	    tdb->tdb_satype == *((u_int8_t *) satype_vp))
1015 		tdb_delete(tdb);
1016 	return (0);
1017 }
1018 
1019 /*
1020  * Convert between SATYPEs and IPsec protocols, taking into consideration
1021  * sysctl variables enabling/disabling ESP/AH and the presence of the old
1022  * IPsec transforms.
1023  */
1024 int
1025 pfkeyv2_get_proto_alg(u_int8_t satype, u_int8_t *sproto, int *alg)
1026 {
1027 	switch (satype) {
1028 #ifdef IPSEC
1029 	case SADB_SATYPE_AH:
1030 		if (!ah_enable)
1031 			return (EOPNOTSUPP);
1032 
1033 		*sproto = IPPROTO_AH;
1034 
1035 		if(alg != NULL)
1036 			*alg = satype = XF_AH;
1037 
1038 		break;
1039 
1040 	case SADB_SATYPE_ESP:
1041 		if (!esp_enable)
1042 			return (EOPNOTSUPP);
1043 
1044 		*sproto = IPPROTO_ESP;
1045 
1046 		if(alg != NULL)
1047 			*alg = satype = XF_ESP;
1048 
1049 		break;
1050 
1051 	case SADB_X_SATYPE_IPIP:
1052 		*sproto = IPPROTO_IPIP;
1053 
1054 		if (alg != NULL)
1055 			*alg = XF_IP4;
1056 
1057 		break;
1058 
1059 	case SADB_X_SATYPE_IPCOMP:
1060 		if (!ipcomp_enable)
1061 			return (EOPNOTSUPP);
1062 
1063 		*sproto = IPPROTO_IPCOMP;
1064 
1065 		if(alg != NULL)
1066 			*alg = satype = XF_IPCOMP;
1067 
1068 		break;
1069 #endif /* IPSEC */
1070 #ifdef TCP_SIGNATURE
1071 	case SADB_X_SATYPE_TCPSIGNATURE:
1072 		*sproto = IPPROTO_TCP;
1073 
1074 		if (alg != NULL)
1075 			*alg = XF_TCPSIGNATURE;
1076 
1077 		break;
1078 #endif /* TCP_SIGNATURE */
1079 
1080 	default: /* Nothing else supported */
1081 		return (EOPNOTSUPP);
1082 	}
1083 
1084 	return (0);
1085 }
1086 
1087 /*
1088  * Handle all messages from userland to kernel.
1089  */
1090 int
1091 pfkeyv2_send(struct socket *so, void *message, int len)
1092 {
1093 	int i, j, rval = 0, mode = PFKEYV2_SENDMESSAGE_BROADCAST;
1094 	int delflag = 0;
1095 	struct sockaddr_encap encapdst, encapnetmask;
1096 	struct ipsec_policy *ipo;
1097 	struct ipsec_acquire *ipa;
1098 	struct radix_node_head *rnh;
1099 	struct radix_node *rn = NULL;
1100 	struct pkpcb *kp, *bkp;
1101 	void *freeme = NULL, *bckptr = NULL;
1102 	void *headers[SADB_EXT_MAX + 1];
1103 	union sockaddr_union *sunionp;
1104 	struct tdb *sa1 = NULL, *sa2 = NULL;
1105 	struct sadb_msg *smsg;
1106 	struct sadb_spirange *sprng;
1107 	struct sadb_sa *ssa;
1108 	struct sadb_supported *ssup;
1109 	struct sadb_ident *sid, *did;
1110 	struct srp_ref sr;
1111 	u_int rdomain;
1112 	int promisc, s;
1113 
1114 	mtx_enter(&pfkeyv2_mtx);
1115 	promisc = npromisc;
1116 	mtx_leave(&pfkeyv2_mtx);
1117 
1118 	/* Verify that we received this over a legitimate pfkeyv2 socket */
1119 	bzero(headers, sizeof(headers));
1120 
1121 	kp = sotokeycb(so);
1122 	if (!kp) {
1123 		rval = EINVAL;
1124 		goto ret;
1125 	}
1126 
1127 	rdomain = kp->kcb_rdomain;
1128 
1129 	/* If we have any promiscuous listeners, send them a copy of the message */
1130 	if (promisc) {
1131 		struct mbuf *packet;
1132 
1133 		if (!(freeme = malloc(sizeof(struct sadb_msg) + len, M_PFKEY,
1134 		    M_NOWAIT))) {
1135 			rval = ENOMEM;
1136 			goto ret;
1137 		}
1138 
1139 		/* Initialize encapsulating header */
1140 		bzero(freeme, sizeof(struct sadb_msg));
1141 		smsg = (struct sadb_msg *) freeme;
1142 		smsg->sadb_msg_version = PF_KEY_V2;
1143 		smsg->sadb_msg_type = SADB_X_PROMISC;
1144 		smsg->sadb_msg_len = (sizeof(struct sadb_msg) + len) /
1145 		    sizeof(uint64_t);
1146 		smsg->sadb_msg_seq = curproc->p_p->ps_pid;
1147 
1148 		bcopy(message, freeme + sizeof(struct sadb_msg), len);
1149 
1150 		/* Convert to mbuf chain */
1151 		if ((rval = pfdatatopacket(freeme,
1152 		    sizeof(struct sadb_msg) + len, &packet)) != 0)
1153 			goto ret;
1154 
1155 		/* Send to all promiscuous listeners */
1156 		SRPL_FOREACH(bkp, &sr, &pkptable.pkp_list, kcb_list) {
1157 			if (bkp->kcb_rdomain != rdomain)
1158 				continue;
1159 
1160 			s = keylock(bkp);
1161 			if (bkp->kcb_flags & PFKEYV2_SOCKETFLAGS_PROMISC)
1162 				pfkey_sendup(bkp, packet, 1);
1163 			keyunlock(bkp, s);
1164 		}
1165 		SRPL_LEAVE(&sr);
1166 
1167 		m_freem(packet);
1168 
1169 		/* Paranoid */
1170 		explicit_bzero(freeme, sizeof(struct sadb_msg) + len);
1171 		free(freeme, M_PFKEY, 0);
1172 		freeme = NULL;
1173 	}
1174 
1175 	/* Validate message format */
1176 	if ((rval = pfkeyv2_parsemessage(message, len, headers)) != 0)
1177 		goto ret;
1178 
1179 	smsg = (struct sadb_msg *) headers[0];
1180 	switch (smsg->sadb_msg_type) {
1181 	case SADB_GETSPI:  /* Reserve an SPI */
1182 		sa1 = malloc(sizeof (*sa1), M_PFKEY, M_NOWAIT | M_ZERO);
1183 		if (sa1 == NULL) {
1184 			rval = ENOMEM;
1185 			goto ret;
1186 		}
1187 
1188 		sa1->tdb_satype = smsg->sadb_msg_satype;
1189 		if ((rval = pfkeyv2_get_proto_alg(sa1->tdb_satype,
1190 		    &sa1->tdb_sproto, 0)))
1191 			goto ret;
1192 
1193 		import_address(&sa1->tdb_src.sa, headers[SADB_EXT_ADDRESS_SRC]);
1194 		import_address(&sa1->tdb_dst.sa, headers[SADB_EXT_ADDRESS_DST]);
1195 
1196 		/* Find an unused SA identifier */
1197 		sprng = (struct sadb_spirange *) headers[SADB_EXT_SPIRANGE];
1198 		NET_LOCK();
1199 		sa1->tdb_spi = reserve_spi(rdomain,
1200 		    sprng->sadb_spirange_min, sprng->sadb_spirange_max,
1201 		    &sa1->tdb_src, &sa1->tdb_dst, sa1->tdb_sproto, &rval);
1202 		if (sa1->tdb_spi == 0) {
1203 			NET_UNLOCK();
1204 			goto ret;
1205 		}
1206 
1207 		/* Send a message back telling what the SA (the SPI really) is */
1208 		if (!(freeme = malloc(sizeof(struct sadb_sa), M_PFKEY,
1209 		    M_NOWAIT | M_ZERO))) {
1210 			rval = ENOMEM;
1211 			NET_UNLOCK();
1212 			goto ret;
1213 		}
1214 
1215 		headers[SADB_EXT_SPIRANGE] = NULL;
1216 		headers[SADB_EXT_SA] = freeme;
1217 		bckptr = freeme;
1218 
1219 		/* We really only care about the SPI, but we'll export the SA */
1220 		export_sa((void **) &bckptr, sa1);
1221 		NET_UNLOCK();
1222 		break;
1223 
1224 	case SADB_UPDATE:
1225 		ssa = (struct sadb_sa *) headers[SADB_EXT_SA];
1226 		sunionp = (union sockaddr_union *) (headers[SADB_EXT_ADDRESS_DST] +
1227 		    sizeof(struct sadb_address));
1228 
1229 		/* Either all or none of the flow must be included */
1230 		if ((headers[SADB_X_EXT_SRC_FLOW] ||
1231 		    headers[SADB_X_EXT_PROTOCOL] ||
1232 		    headers[SADB_X_EXT_FLOW_TYPE] ||
1233 		    headers[SADB_X_EXT_DST_FLOW] ||
1234 		    headers[SADB_X_EXT_SRC_MASK] ||
1235 		    headers[SADB_X_EXT_DST_MASK]) &&
1236 		    !(headers[SADB_X_EXT_SRC_FLOW] &&
1237 		    headers[SADB_X_EXT_PROTOCOL] &&
1238 		    headers[SADB_X_EXT_FLOW_TYPE] &&
1239 		    headers[SADB_X_EXT_DST_FLOW] &&
1240 		    headers[SADB_X_EXT_SRC_MASK] &&
1241 		    headers[SADB_X_EXT_DST_MASK])) {
1242 			rval = EINVAL;
1243 			goto ret;
1244 		}
1245 #ifdef IPSEC
1246 		/* UDP encap has to be enabled and is only supported for ESP */
1247 		if (headers[SADB_X_EXT_UDPENCAP] &&
1248 		    (!udpencap_enable ||
1249 		    smsg->sadb_msg_satype != SADB_SATYPE_ESP)) {
1250 			rval = EINVAL;
1251 			goto ret;
1252 		}
1253 #endif /* IPSEC */
1254 
1255 		/* Find TDB */
1256 		NET_LOCK();
1257 		sa2 = gettdb(rdomain, ssa->sadb_sa_spi, sunionp,
1258 		    SADB_X_GETSPROTO(smsg->sadb_msg_satype));
1259 
1260 		/* If there's no such SA, we're done */
1261 		if (sa2 == NULL) {
1262 			rval = ESRCH;
1263 			NET_UNLOCK();
1264 			goto ret;
1265 		}
1266 
1267 		/* If this is a reserved SA */
1268 		if (sa2->tdb_flags & TDBF_INVALID) {
1269 			struct tdb *newsa;
1270 			struct ipsecinit ii;
1271 			int alg;
1272 
1273 			/* Create new TDB */
1274 			freeme = tdb_alloc(rdomain);
1275 			bzero(&ii, sizeof(struct ipsecinit));
1276 
1277 			newsa = (struct tdb *) freeme;
1278 			newsa->tdb_satype = smsg->sadb_msg_satype;
1279 
1280 			if ((rval = pfkeyv2_get_proto_alg(newsa->tdb_satype,
1281 			    &newsa->tdb_sproto, &alg))) {
1282 				tdb_free(freeme);
1283 				freeme = NULL;
1284 				NET_UNLOCK();
1285 				goto ret;
1286 			}
1287 
1288 			/* Initialize SA */
1289 			import_sa(newsa, headers[SADB_EXT_SA], &ii);
1290 			import_address(&newsa->tdb_src.sa,
1291 			    headers[SADB_EXT_ADDRESS_SRC]);
1292 			import_address(&newsa->tdb_dst.sa,
1293 			    headers[SADB_EXT_ADDRESS_DST]);
1294 			import_lifetime(newsa,
1295 			    headers[SADB_EXT_LIFETIME_CURRENT],
1296 			    PFKEYV2_LIFETIME_CURRENT);
1297 			import_lifetime(newsa, headers[SADB_EXT_LIFETIME_SOFT],
1298 			    PFKEYV2_LIFETIME_SOFT);
1299 			import_lifetime(newsa, headers[SADB_EXT_LIFETIME_HARD],
1300 			    PFKEYV2_LIFETIME_HARD);
1301 			import_key(&ii, headers[SADB_EXT_KEY_AUTH],
1302 			    PFKEYV2_AUTHENTICATION_KEY);
1303 			import_key(&ii, headers[SADB_EXT_KEY_ENCRYPT],
1304 			    PFKEYV2_ENCRYPTION_KEY);
1305 			newsa->tdb_ids_swapped = 1; /* only on TDB_UPDATE */
1306 			import_identities(&newsa->tdb_ids,
1307 			    newsa->tdb_ids_swapped,
1308 			    headers[SADB_EXT_IDENTITY_SRC],
1309 			    headers[SADB_EXT_IDENTITY_DST]);
1310 			import_flow(&newsa->tdb_filter, &newsa->tdb_filtermask,
1311 			    headers[SADB_X_EXT_SRC_FLOW],
1312 			    headers[SADB_X_EXT_SRC_MASK],
1313 			    headers[SADB_X_EXT_DST_FLOW],
1314 			    headers[SADB_X_EXT_DST_MASK],
1315 			    headers[SADB_X_EXT_PROTOCOL],
1316 			    headers[SADB_X_EXT_FLOW_TYPE]);
1317 			import_udpencap(newsa, headers[SADB_X_EXT_UDPENCAP]);
1318 #if NPF > 0
1319 			import_tag(newsa, headers[SADB_X_EXT_TAG]);
1320 			import_tap(newsa, headers[SADB_X_EXT_TAP]);
1321 #endif
1322 
1323 			/* Exclude sensitive data from reply message. */
1324 			headers[SADB_EXT_KEY_AUTH] = NULL;
1325 			headers[SADB_EXT_KEY_ENCRYPT] = NULL;
1326 			headers[SADB_X_EXT_LOCAL_AUTH] = NULL;
1327 			headers[SADB_X_EXT_REMOTE_AUTH] = NULL;
1328 
1329 			newsa->tdb_seq = smsg->sadb_msg_seq;
1330 
1331 			rval = tdb_init(newsa, alg, &ii);
1332 			if (rval) {
1333 				rval = EINVAL;
1334 				tdb_free(freeme);
1335 				freeme = NULL;
1336 				NET_UNLOCK();
1337 				goto ret;
1338 			}
1339 
1340 			newsa->tdb_cur_allocations = sa2->tdb_cur_allocations;
1341 
1342 			/* Delete old version of the SA, insert new one */
1343 			tdb_delete(sa2);
1344 			puttdb((struct tdb *) freeme);
1345 			sa2 = freeme = NULL;
1346 		} else {
1347 			/*
1348 			 * The SA is already initialized, so we're only allowed to
1349 			 * change lifetimes and some other information; we're
1350 			 * not allowed to change keys, addresses or identities.
1351 			 */
1352 			if (headers[SADB_EXT_KEY_AUTH] ||
1353 			    headers[SADB_EXT_KEY_ENCRYPT] ||
1354 			    headers[SADB_EXT_IDENTITY_SRC] ||
1355 			    headers[SADB_EXT_IDENTITY_DST] ||
1356 			    headers[SADB_EXT_SENSITIVITY]) {
1357 				rval = EINVAL;
1358 				NET_UNLOCK();
1359 				goto ret;
1360 			}
1361 
1362 			import_sa(sa2, headers[SADB_EXT_SA], NULL);
1363 			import_lifetime(sa2,
1364 			    headers[SADB_EXT_LIFETIME_CURRENT],
1365 			    PFKEYV2_LIFETIME_CURRENT);
1366 			import_lifetime(sa2, headers[SADB_EXT_LIFETIME_SOFT],
1367 			    PFKEYV2_LIFETIME_SOFT);
1368 			import_lifetime(sa2, headers[SADB_EXT_LIFETIME_HARD],
1369 			    PFKEYV2_LIFETIME_HARD);
1370 			import_udpencap(sa2, headers[SADB_X_EXT_UDPENCAP]);
1371 #if NPF > 0
1372 			import_tag(sa2, headers[SADB_X_EXT_TAG]);
1373 			import_tap(sa2, headers[SADB_X_EXT_TAP]);
1374 #endif
1375 			if (headers[SADB_EXT_ADDRESS_SRC] ||
1376 			    headers[SADB_EXT_ADDRESS_PROXY]) {
1377 				tdb_unlink(sa2);
1378 				import_address((struct sockaddr *)&sa2->tdb_src,
1379 				    headers[SADB_EXT_ADDRESS_SRC]);
1380 				import_address((struct sockaddr *)&sa2->tdb_dst,
1381 				    headers[SADB_EXT_ADDRESS_PROXY]);
1382 				puttdb(sa2);
1383 			}
1384 		}
1385 		NET_UNLOCK();
1386 
1387 		break;
1388 	case SADB_ADD:
1389 		ssa = (struct sadb_sa *) headers[SADB_EXT_SA];
1390 		sunionp = (union sockaddr_union *) (headers[SADB_EXT_ADDRESS_DST] +
1391 		    sizeof(struct sadb_address));
1392 
1393 		/* Either all or none of the flow must be included */
1394 		if ((headers[SADB_X_EXT_SRC_FLOW] ||
1395 		    headers[SADB_X_EXT_PROTOCOL] ||
1396 		    headers[SADB_X_EXT_FLOW_TYPE] ||
1397 		    headers[SADB_X_EXT_DST_FLOW] ||
1398 		    headers[SADB_X_EXT_SRC_MASK] ||
1399 		    headers[SADB_X_EXT_DST_MASK]) &&
1400 		    !(headers[SADB_X_EXT_SRC_FLOW] &&
1401 		    headers[SADB_X_EXT_PROTOCOL] &&
1402 		    headers[SADB_X_EXT_FLOW_TYPE] &&
1403 		    headers[SADB_X_EXT_DST_FLOW] &&
1404 		    headers[SADB_X_EXT_SRC_MASK] &&
1405 		    headers[SADB_X_EXT_DST_MASK])) {
1406 			rval = EINVAL;
1407 			goto ret;
1408 		}
1409 #ifdef IPSEC
1410 		/* UDP encap has to be enabled and is only supported for ESP */
1411 		if (headers[SADB_X_EXT_UDPENCAP] &&
1412 		    (!udpencap_enable ||
1413 		    smsg->sadb_msg_satype != SADB_SATYPE_ESP)) {
1414 			rval = EINVAL;
1415 			goto ret;
1416 		}
1417 #endif /* IPSEC */
1418 
1419 		NET_LOCK();
1420 		sa2 = gettdb(rdomain, ssa->sadb_sa_spi, sunionp,
1421 		    SADB_X_GETSPROTO(smsg->sadb_msg_satype));
1422 
1423 		/* We can't add an existing SA! */
1424 		if (sa2 != NULL) {
1425 			rval = EEXIST;
1426 			NET_UNLOCK();
1427 			goto ret;
1428 		}
1429 
1430 		/* We can only add "mature" SAs */
1431 		if (ssa->sadb_sa_state != SADB_SASTATE_MATURE) {
1432 			rval = EINVAL;
1433 			NET_UNLOCK();
1434 			goto ret;
1435 		}
1436 
1437 		/* Allocate and initialize new TDB */
1438 		freeme = tdb_alloc(rdomain);
1439 
1440 		{
1441 			struct tdb *newsa = (struct tdb *) freeme;
1442 			struct ipsecinit ii;
1443 			int alg;
1444 
1445 			bzero(&ii, sizeof(struct ipsecinit));
1446 
1447 			newsa->tdb_satype = smsg->sadb_msg_satype;
1448 			if ((rval = pfkeyv2_get_proto_alg(newsa->tdb_satype,
1449 			    &newsa->tdb_sproto, &alg))) {
1450 				tdb_free(freeme);
1451 				freeme = NULL;
1452 				NET_UNLOCK();
1453 				goto ret;
1454 			}
1455 
1456 			import_sa(newsa, headers[SADB_EXT_SA], &ii);
1457 			import_address(&newsa->tdb_src.sa,
1458 			    headers[SADB_EXT_ADDRESS_SRC]);
1459 			import_address(&newsa->tdb_dst.sa,
1460 			    headers[SADB_EXT_ADDRESS_DST]);
1461 
1462 			import_lifetime(newsa,
1463 			    headers[SADB_EXT_LIFETIME_CURRENT],
1464 			    PFKEYV2_LIFETIME_CURRENT);
1465 			import_lifetime(newsa, headers[SADB_EXT_LIFETIME_SOFT],
1466 			    PFKEYV2_LIFETIME_SOFT);
1467 			import_lifetime(newsa, headers[SADB_EXT_LIFETIME_HARD],
1468 			    PFKEYV2_LIFETIME_HARD);
1469 
1470 			import_key(&ii, headers[SADB_EXT_KEY_AUTH],
1471 			    PFKEYV2_AUTHENTICATION_KEY);
1472 			import_key(&ii, headers[SADB_EXT_KEY_ENCRYPT],
1473 			    PFKEYV2_ENCRYPTION_KEY);
1474 
1475 			import_identities(&newsa->tdb_ids,
1476 			    newsa->tdb_ids_swapped,
1477 			    headers[SADB_EXT_IDENTITY_SRC],
1478 			    headers[SADB_EXT_IDENTITY_DST]);
1479 
1480 			import_flow(&newsa->tdb_filter, &newsa->tdb_filtermask,
1481 			    headers[SADB_X_EXT_SRC_FLOW],
1482 			    headers[SADB_X_EXT_SRC_MASK],
1483 			    headers[SADB_X_EXT_DST_FLOW],
1484 			    headers[SADB_X_EXT_DST_MASK],
1485 			    headers[SADB_X_EXT_PROTOCOL],
1486 			    headers[SADB_X_EXT_FLOW_TYPE]);
1487 			import_udpencap(newsa, headers[SADB_X_EXT_UDPENCAP]);
1488 #if NPF > 0
1489 			import_tag(newsa, headers[SADB_X_EXT_TAG]);
1490 			import_tap(newsa, headers[SADB_X_EXT_TAP]);
1491 #endif
1492 
1493 			/* Exclude sensitive data from reply message. */
1494 			headers[SADB_EXT_KEY_AUTH] = NULL;
1495 			headers[SADB_EXT_KEY_ENCRYPT] = NULL;
1496 			headers[SADB_X_EXT_LOCAL_AUTH] = NULL;
1497 			headers[SADB_X_EXT_REMOTE_AUTH] = NULL;
1498 
1499 			newsa->tdb_seq = smsg->sadb_msg_seq;
1500 
1501 			rval = tdb_init(newsa, alg, &ii);
1502 			if (rval) {
1503 				rval = EINVAL;
1504 				tdb_free(freeme);
1505 				freeme = NULL;
1506 				NET_UNLOCK();
1507 				goto ret;
1508 			}
1509 		}
1510 
1511 		/* Add TDB in table */
1512 		puttdb((struct tdb *) freeme);
1513 		NET_UNLOCK();
1514 
1515 		freeme = NULL;
1516 		break;
1517 
1518 	case SADB_DELETE:
1519 		ssa = (struct sadb_sa *) headers[SADB_EXT_SA];
1520 		sunionp =
1521 		    (union sockaddr_union *)(headers[SADB_EXT_ADDRESS_DST] +
1522 			sizeof(struct sadb_address));
1523 
1524 		NET_LOCK();
1525 		sa2 = gettdb(rdomain, ssa->sadb_sa_spi, sunionp,
1526 		    SADB_X_GETSPROTO(smsg->sadb_msg_satype));
1527 		if (sa2 == NULL) {
1528 			rval = ESRCH;
1529 			NET_UNLOCK();
1530 			goto ret;
1531 		}
1532 
1533 		tdb_delete(sa2);
1534 		NET_UNLOCK();
1535 
1536 		sa2 = NULL;
1537 		break;
1538 
1539 	case SADB_X_ASKPOLICY:
1540 		/* Get the relevant policy */
1541 		NET_LOCK();
1542 		ipa = ipsec_get_acquire(((struct sadb_x_policy *) headers[SADB_X_EXT_POLICY])->sadb_x_policy_seq);
1543 		if (ipa == NULL) {
1544 			rval = ESRCH;
1545 			NET_UNLOCK();
1546 			goto ret;
1547 		}
1548 
1549 		rval = pfkeyv2_policy(ipa, headers, &freeme);
1550 		NET_UNLOCK();
1551 		if (rval)
1552 			mode = PFKEYV2_SENDMESSAGE_UNICAST;
1553 
1554 		break;
1555 
1556 	case SADB_GET:
1557 		ssa = (struct sadb_sa *) headers[SADB_EXT_SA];
1558 		sunionp =
1559 		    (union sockaddr_union *)(headers[SADB_EXT_ADDRESS_DST] +
1560 			sizeof(struct sadb_address));
1561 
1562 		NET_LOCK();
1563 		sa2 = gettdb(rdomain, ssa->sadb_sa_spi, sunionp,
1564 		    SADB_X_GETSPROTO(smsg->sadb_msg_satype));
1565 		if (sa2 == NULL) {
1566 			rval = ESRCH;
1567 			NET_UNLOCK();
1568 			goto ret;
1569 		}
1570 
1571 		rval = pfkeyv2_get(sa2, headers, &freeme, NULL);
1572 		NET_UNLOCK();
1573 		if (rval)
1574 			mode = PFKEYV2_SENDMESSAGE_UNICAST;
1575 
1576 		break;
1577 
1578 	case SADB_REGISTER:
1579 		s = keylock(kp);
1580 		if (!(kp->kcb_flags & PFKEYV2_SOCKETFLAGS_REGISTERED)) {
1581 			kp->kcb_flags |= PFKEYV2_SOCKETFLAGS_REGISTERED;
1582 			mtx_enter(&pfkeyv2_mtx);
1583 			nregistered++;
1584 			mtx_leave(&pfkeyv2_mtx);
1585 		}
1586 		keyunlock(kp, s);
1587 
1588 		i = sizeof(struct sadb_supported) + sizeof(ealgs);
1589 
1590 		if (!(freeme = malloc(i, M_PFKEY, M_NOWAIT | M_ZERO))) {
1591 			rval = ENOMEM;
1592 			goto ret;
1593 		}
1594 
1595 		ssup = (struct sadb_supported *) freeme;
1596 		ssup->sadb_supported_len = i / sizeof(uint64_t);
1597 
1598 		{
1599 			void *p = freeme + sizeof(struct sadb_supported);
1600 
1601 			bcopy(&ealgs[0], p, sizeof(ealgs));
1602 		}
1603 
1604 		headers[SADB_EXT_SUPPORTED_ENCRYPT] = freeme;
1605 
1606 		i = sizeof(struct sadb_supported) + sizeof(aalgs);
1607 
1608 		if (!(freeme = malloc(i, M_PFKEY, M_NOWAIT | M_ZERO))) {
1609 			rval = ENOMEM;
1610 			goto ret;
1611 		}
1612 
1613 		/* Keep track what this socket has registered for */
1614 		s = keylock(kp);
1615 		kp->kcb_reg |=
1616 		    (1 << ((struct sadb_msg *)message)->sadb_msg_satype);
1617 		keyunlock(kp, s);
1618 
1619 		ssup = (struct sadb_supported *) freeme;
1620 		ssup->sadb_supported_len = i / sizeof(uint64_t);
1621 
1622 		{
1623 			void *p = freeme + sizeof(struct sadb_supported);
1624 
1625 			bcopy(&aalgs[0], p, sizeof(aalgs));
1626 		}
1627 
1628 		headers[SADB_EXT_SUPPORTED_AUTH] = freeme;
1629 
1630 		i = sizeof(struct sadb_supported) + sizeof(calgs);
1631 
1632 		if (!(freeme = malloc(i, M_PFKEY, M_NOWAIT | M_ZERO))) {
1633 			rval = ENOMEM;
1634 			goto ret;
1635 		}
1636 
1637 		ssup = (struct sadb_supported *) freeme;
1638 		ssup->sadb_supported_len = i / sizeof(uint64_t);
1639 
1640 		{
1641 			void *p = freeme + sizeof(struct sadb_supported);
1642 
1643 			bcopy(&calgs[0], p, sizeof(calgs));
1644 		}
1645 
1646 		headers[SADB_X_EXT_SUPPORTED_COMP] = freeme;
1647 
1648 		break;
1649 
1650 	case SADB_ACQUIRE:
1651 	case SADB_EXPIRE:
1652 		/* Nothing to handle */
1653 		rval = 0;
1654 		break;
1655 
1656 	case SADB_FLUSH:
1657 		rval = 0;
1658 
1659 		NET_LOCK();
1660 		switch (smsg->sadb_msg_satype) {
1661 		case SADB_SATYPE_UNSPEC:
1662 			spd_table_walk(rdomain, pfkeyv2_policy_flush, NULL);
1663 			/* FALLTHROUGH */
1664 		case SADB_SATYPE_AH:
1665 		case SADB_SATYPE_ESP:
1666 		case SADB_X_SATYPE_IPIP:
1667 		case SADB_X_SATYPE_IPCOMP:
1668 #ifdef TCP_SIGNATURE
1669 		case SADB_X_SATYPE_TCPSIGNATURE:
1670 #endif /* TCP_SIGNATURE */
1671 			tdb_walk(rdomain, pfkeyv2_sa_flush,
1672 			    (u_int8_t *) &(smsg->sadb_msg_satype));
1673 
1674 			break;
1675 
1676 		default:
1677 			rval = EINVAL; /* Unknown/unsupported type */
1678 		}
1679 		NET_UNLOCK();
1680 
1681 		break;
1682 
1683 	case SADB_DUMP:
1684 	{
1685 		struct dump_state dump_state;
1686 		dump_state.sadb_msg = (struct sadb_msg *) headers[0];
1687 		dump_state.socket = so;
1688 
1689 		NET_LOCK();
1690 		rval = tdb_walk(rdomain, pfkeyv2_dump_walker, &dump_state);
1691 		NET_UNLOCK();
1692 		if (!rval)
1693 			goto realret;
1694 		if ((rval == ENOMEM) || (rval == ENOBUFS))
1695 			rval = 0;
1696 	}
1697 	break;
1698 
1699 	case SADB_X_GRPSPIS:
1700 	{
1701 		struct tdb *tdb1, *tdb2, *tdb3;
1702 		struct sadb_protocol *sa_proto;
1703 
1704 		ssa = (struct sadb_sa *) headers[SADB_EXT_SA];
1705 		sunionp = (union sockaddr_union *) (headers[SADB_EXT_ADDRESS_DST] +
1706 		    sizeof(struct sadb_address));
1707 
1708 		NET_LOCK();
1709 		tdb1 = gettdb(rdomain, ssa->sadb_sa_spi, sunionp,
1710 		    SADB_X_GETSPROTO(smsg->sadb_msg_satype));
1711 		if (tdb1 == NULL) {
1712 			rval = ESRCH;
1713 			NET_UNLOCK();
1714 			goto ret;
1715 		}
1716 
1717 		ssa = (struct sadb_sa *) headers[SADB_X_EXT_SA2];
1718 		sunionp = (union sockaddr_union *) (headers[SADB_X_EXT_DST2] +
1719 		    sizeof(struct sadb_address));
1720 		sa_proto = (struct sadb_protocol *) headers[SADB_X_EXT_SATYPE2];
1721 
1722 		tdb2 = gettdb(rdomain, ssa->sadb_sa_spi, sunionp,
1723 		    SADB_X_GETSPROTO(sa_proto->sadb_protocol_proto));
1724 		if (tdb2 == NULL) {
1725 			rval = ESRCH;
1726 			NET_UNLOCK();
1727 			goto ret;
1728 		}
1729 
1730 		/* Detect cycles */
1731 		for (tdb3 = tdb2; tdb3; tdb3 = tdb3->tdb_onext)
1732 			if (tdb3 == tdb1) {
1733 				rval = ESRCH;
1734 				NET_UNLOCK();
1735 				goto ret;
1736 			}
1737 
1738 		/* Maintenance */
1739 		if ((tdb1->tdb_onext) &&
1740 		    (tdb1->tdb_onext->tdb_inext == tdb1))
1741 			tdb1->tdb_onext->tdb_inext = NULL;
1742 
1743 		if ((tdb2->tdb_inext) &&
1744 		    (tdb2->tdb_inext->tdb_onext == tdb2))
1745 			tdb2->tdb_inext->tdb_onext = NULL;
1746 
1747 		/* Link them */
1748 		tdb1->tdb_onext = tdb2;
1749 		tdb2->tdb_inext = tdb1;
1750 		NET_UNLOCK();
1751 	}
1752 	break;
1753 
1754 	case SADB_X_DELFLOW:
1755 		delflag = 1;
1756 		/*FALLTHROUGH*/
1757 	case SADB_X_ADDFLOW:
1758 	{
1759 		struct sadb_protocol *sab;
1760 		union sockaddr_union *ssrc;
1761 		int exists = 0;
1762 
1763 		NET_LOCK();
1764 		if ((rnh = spd_table_add(rdomain)) == NULL) {
1765 			rval = ENOMEM;
1766 			NET_UNLOCK();
1767 			goto ret;
1768 		}
1769 
1770 		sab = (struct sadb_protocol *) headers[SADB_X_EXT_FLOW_TYPE];
1771 
1772 		if ((sab->sadb_protocol_direction != IPSP_DIRECTION_IN) &&
1773 		    (sab->sadb_protocol_direction != IPSP_DIRECTION_OUT)) {
1774 			rval = EINVAL;
1775 			NET_UNLOCK();
1776 			goto ret;
1777 		}
1778 
1779 		/* If the security protocol wasn't specified, pretend it was ESP */
1780 		if (smsg->sadb_msg_satype == 0)
1781 			smsg->sadb_msg_satype = SADB_SATYPE_ESP;
1782 
1783 		if (headers[SADB_EXT_ADDRESS_DST])
1784 			sunionp = (union sockaddr_union *)
1785 			    (headers[SADB_EXT_ADDRESS_DST] +
1786 				sizeof(struct sadb_address));
1787 		else
1788 			sunionp = NULL;
1789 
1790 		if (headers[SADB_EXT_ADDRESS_SRC])
1791 			ssrc = (union sockaddr_union *)
1792 			    (headers[SADB_EXT_ADDRESS_SRC] +
1793 				sizeof(struct sadb_address));
1794 		else
1795 			ssrc = NULL;
1796 
1797 		import_flow(&encapdst, &encapnetmask,
1798 		    headers[SADB_X_EXT_SRC_FLOW], headers[SADB_X_EXT_SRC_MASK],
1799 		    headers[SADB_X_EXT_DST_FLOW], headers[SADB_X_EXT_DST_MASK],
1800 		    headers[SADB_X_EXT_PROTOCOL], headers[SADB_X_EXT_FLOW_TYPE]);
1801 
1802 		/* Determine whether the exact same SPD entry already exists. */
1803 		if ((rn = rn_match(&encapdst, rnh)) != NULL) {
1804 			ipo = (struct ipsec_policy *)rn;
1805 
1806 			/* Verify that the entry is identical */
1807 			if (bcmp(&ipo->ipo_addr, &encapdst,
1808 				sizeof(struct sockaddr_encap)) ||
1809 			    bcmp(&ipo->ipo_mask, &encapnetmask,
1810 				sizeof(struct sockaddr_encap)))
1811 				ipo = NULL; /* Fall through */
1812 			else
1813 				exists = 1;
1814 		} else
1815 			ipo = NULL;
1816 
1817 		/*
1818 		 * If the existing policy is static, only delete or update
1819 		 * it if the new one is also static.
1820 		 */
1821 		if (exists && (ipo->ipo_flags & IPSP_POLICY_STATIC)) {
1822 			if (!(sab->sadb_protocol_flags &
1823 				SADB_X_POLICYFLAGS_POLICY)) {
1824 				NET_UNLOCK();
1825 				goto ret;
1826 			}
1827 		}
1828 
1829 		/* Delete ? */
1830 		if (delflag) {
1831 			if (exists) {
1832 				rval = ipsec_delete_policy(ipo);
1833 				NET_UNLOCK();
1834 				goto ret;
1835 			}
1836 
1837 			/* If we were asked to delete something non-existent, error. */
1838 			rval = ESRCH;
1839 			NET_UNLOCK();
1840 			break;
1841 		}
1842 
1843 		if (!exists) {
1844 			if (ipsec_policy_pool_initialized == 0) {
1845 				ipsec_policy_pool_initialized = 1;
1846 				pool_init(&ipsec_policy_pool,
1847 				    sizeof(struct ipsec_policy), 0,
1848 				    IPL_NONE, 0, "ipsec policy", NULL);
1849 			}
1850 
1851 			/* Allocate policy entry */
1852 			ipo = pool_get(&ipsec_policy_pool, PR_NOWAIT|PR_ZERO);
1853 			if (ipo == NULL) {
1854 				rval = ENOMEM;
1855 				NET_UNLOCK();
1856 				goto ret;
1857 			}
1858 		}
1859 
1860 		switch (sab->sadb_protocol_proto) {
1861 		case SADB_X_FLOW_TYPE_USE:
1862 			ipo->ipo_type = IPSP_IPSEC_USE;
1863 			break;
1864 
1865 		case SADB_X_FLOW_TYPE_ACQUIRE:
1866 			ipo->ipo_type = IPSP_IPSEC_ACQUIRE;
1867 			break;
1868 
1869 		case SADB_X_FLOW_TYPE_REQUIRE:
1870 			ipo->ipo_type = IPSP_IPSEC_REQUIRE;
1871 			break;
1872 
1873 		case SADB_X_FLOW_TYPE_DENY:
1874 			ipo->ipo_type = IPSP_DENY;
1875 			break;
1876 
1877 		case SADB_X_FLOW_TYPE_BYPASS:
1878 			ipo->ipo_type = IPSP_PERMIT;
1879 			break;
1880 
1881 		case SADB_X_FLOW_TYPE_DONTACQ:
1882 			ipo->ipo_type = IPSP_IPSEC_DONTACQ;
1883 			break;
1884 
1885 		default:
1886 			if (!exists)
1887 				pool_put(&ipsec_policy_pool, ipo);
1888 			else
1889 				ipsec_delete_policy(ipo);
1890 
1891 			rval = EINVAL;
1892 			NET_UNLOCK();
1893 			goto ret;
1894 		}
1895 
1896 		if (sab->sadb_protocol_flags & SADB_X_POLICYFLAGS_POLICY)
1897 			ipo->ipo_flags |= IPSP_POLICY_STATIC;
1898 
1899 		if (sunionp)
1900 			bcopy(sunionp, &ipo->ipo_dst,
1901 			    sizeof(union sockaddr_union));
1902 		else
1903 			bzero(&ipo->ipo_dst, sizeof(union sockaddr_union));
1904 
1905 		if (ssrc)
1906 			bcopy(ssrc, &ipo->ipo_src,
1907 			    sizeof(union sockaddr_union));
1908 		else
1909 			bzero(&ipo->ipo_src, sizeof(union sockaddr_union));
1910 
1911 		ipo->ipo_sproto = SADB_X_GETSPROTO(smsg->sadb_msg_satype);
1912 
1913 		if (ipo->ipo_ids) {
1914 			ipsp_ids_free(ipo->ipo_ids);
1915 			ipo->ipo_ids = NULL;
1916 		}
1917 
1918 		if ((sid = headers[SADB_EXT_IDENTITY_SRC]) != NULL &&
1919 		    (did = headers[SADB_EXT_IDENTITY_DST]) != NULL) {
1920 			import_identities(&ipo->ipo_ids, 0, sid, did);
1921 			if (ipo->ipo_ids == NULL) {
1922 				if (exists)
1923 					ipsec_delete_policy(ipo);
1924 				else
1925 					pool_put(&ipsec_policy_pool, ipo);
1926 				rval = ENOBUFS;
1927 				NET_UNLOCK();
1928 				goto ret;
1929 			}
1930 		}
1931 
1932 		/* Flow type */
1933 		if (!exists) {
1934 			/* Initialize policy entry */
1935 			bcopy(&encapdst, &ipo->ipo_addr,
1936 			    sizeof(struct sockaddr_encap));
1937 			bcopy(&encapnetmask, &ipo->ipo_mask,
1938 			    sizeof(struct sockaddr_encap));
1939 
1940 			TAILQ_INIT(&ipo->ipo_acquires);
1941 			ipo->ipo_rdomain = rdomain;
1942 			ipo->ipo_ref_count = 1;
1943 
1944 			/* Add SPD entry */
1945 			if ((rnh = spd_table_get(rdomain)) == NULL ||
1946 			    (rn = rn_addroute((caddr_t)&ipo->ipo_addr,
1947 				(caddr_t)&ipo->ipo_mask, rnh,
1948 				ipo->ipo_nodes, 0)) == NULL) {
1949 				/* Remove from linked list of policies on TDB */
1950 				if (ipo->ipo_tdb)
1951 					TAILQ_REMOVE(&ipo->ipo_tdb->tdb_policy_head,
1952 					    ipo, ipo_tdb_next);
1953 
1954 				if (ipo->ipo_ids)
1955 					ipsp_ids_free(ipo->ipo_ids);
1956 				pool_put(&ipsec_policy_pool, ipo);
1957 				NET_UNLOCK();
1958 				goto ret;
1959 			}
1960 			TAILQ_INSERT_HEAD(&ipsec_policy_head, ipo, ipo_list);
1961 			ipsec_in_use++;
1962 			/*
1963 			 * XXXSMP IPsec data structures are not ready to be
1964 			 * accessed by multiple Network threads in parallel,
1965 			 * so force all packets to be processed by the first
1966 			 * one.
1967 			 */
1968 			extern int nettaskqs;
1969 			nettaskqs = 1;
1970 		} else {
1971 			ipo->ipo_last_searched = ipo->ipo_flags = 0;
1972 		}
1973 		NET_UNLOCK();
1974 	}
1975 	break;
1976 
1977 	case SADB_X_PROMISC:
1978 		if (len >= 2 * sizeof(struct sadb_msg)) {
1979 			struct mbuf *packet;
1980 
1981 			if ((rval = pfdatatopacket(message, len, &packet)) != 0)
1982 				goto ret;
1983 
1984 			SRPL_FOREACH(bkp, &sr, &pkptable.pkp_list, kcb_list) {
1985 				if (bkp == kp || bkp->kcb_rdomain != rdomain)
1986 					continue;
1987 
1988 				if (!smsg->sadb_msg_seq ||
1989 				    (smsg->sadb_msg_seq == kp->kcb_pid)) {
1990 					s = keylock(bkp);
1991 					pfkey_sendup(bkp, packet, 1);
1992 					keyunlock(bkp, s);
1993 				}
1994 			}
1995 			SRPL_LEAVE(&sr);
1996 
1997 			m_freem(packet);
1998 		} else {
1999 			if (len != sizeof(struct sadb_msg)) {
2000 				rval = EINVAL;
2001 				goto ret;
2002 			}
2003 
2004 			s = keylock(kp);
2005 			i = (kp->kcb_flags &
2006 			    PFKEYV2_SOCKETFLAGS_PROMISC) ? 1 : 0;
2007 			j = smsg->sadb_msg_satype ? 1 : 0;
2008 
2009 			if (i ^ j) {
2010 				if (j) {
2011 					kp->kcb_flags |=
2012 					    PFKEYV2_SOCKETFLAGS_PROMISC;
2013 					mtx_enter(&pfkeyv2_mtx);
2014 					npromisc++;
2015 					mtx_leave(&pfkeyv2_mtx);
2016 				} else {
2017 					kp->kcb_flags &=
2018 					    ~PFKEYV2_SOCKETFLAGS_PROMISC;
2019 					mtx_enter(&pfkeyv2_mtx);
2020 					npromisc--;
2021 					mtx_leave(&pfkeyv2_mtx);
2022 				}
2023 			}
2024 			keyunlock(kp, s);
2025 		}
2026 
2027 		break;
2028 
2029 	default:
2030 		rval = EINVAL;
2031 		goto ret;
2032 	}
2033 
2034 ret:
2035 	if (rval) {
2036 		if ((rval == EINVAL) || (rval == ENOMEM) || (rval == ENOBUFS))
2037 			goto realret;
2038 
2039 		for (i = 1; i <= SADB_EXT_MAX; i++)
2040 			headers[i] = NULL;
2041 
2042 		smsg->sadb_msg_errno = abs(rval);
2043 	} else {
2044 		uint64_t seen = 0LL;
2045 
2046 		for (i = 1; i <= SADB_EXT_MAX; i++)
2047 			if (headers[i])
2048 				seen |= (1LL << i);
2049 
2050 		if ((seen & sadb_exts_allowed_out[smsg->sadb_msg_type])
2051 		    != seen) {
2052 		    	rval = EPERM;
2053 			goto realret;
2054 		}
2055 
2056 		if ((seen & sadb_exts_required_out[smsg->sadb_msg_type]) !=
2057 		    sadb_exts_required_out[smsg->sadb_msg_type]) {
2058 		    	rval = EPERM;
2059 			goto realret;
2060 		}
2061 	}
2062 
2063 	rval = pfkeyv2_sendmessage(headers, mode, so, 0, 0, rdomain);
2064 
2065 realret:
2066 
2067 	if (freeme)
2068 		free(freeme, M_PFKEY, 0);
2069 
2070 	explicit_bzero(message, len);
2071 	free(message, M_PFKEY, 0);
2072 
2073 	if (sa1)
2074 		free(sa1, M_PFKEY, 0);
2075 
2076 	return (rval);
2077 }
2078 
2079 /*
2080  * Send an ACQUIRE message to key management, to get a new SA.
2081  */
2082 int
2083 pfkeyv2_acquire(struct ipsec_policy *ipo, union sockaddr_union *gw,
2084     union sockaddr_union *laddr, u_int32_t *seq, struct sockaddr_encap *ddst)
2085 {
2086 	void *p, *headers[SADB_EXT_MAX + 1], *buffer = NULL;
2087 	struct sadb_comb *sadb_comb;
2088 	struct sadb_address *sadd;
2089 	struct sadb_prop *sa_prop;
2090 	struct sadb_msg *smsg;
2091 	int rval = 0;
2092 	int i, j, registered;
2093 
2094 	mtx_enter(&pfkeyv2_mtx);
2095 	*seq = pfkeyv2_seq++;
2096 
2097 	registered = nregistered;
2098 	mtx_leave(&pfkeyv2_mtx);
2099 
2100 	if (!registered) {
2101 		rval = ESRCH;
2102 		goto ret;
2103 	}
2104 
2105 	/* How large a buffer do we need... XXX we only do one proposal for now */
2106 	i = sizeof(struct sadb_msg) +
2107 	    (laddr == NULL ? 0 : sizeof(struct sadb_address) +
2108 		PADUP(ipo->ipo_src.sa.sa_len)) +
2109 	    sizeof(struct sadb_address) + PADUP(gw->sa.sa_len) +
2110 	    sizeof(struct sadb_prop) + 1 * sizeof(struct sadb_comb);
2111 
2112 	if (ipo->ipo_ids) {
2113 		i += sizeof(struct sadb_ident) + PADUP(ipo->ipo_ids->id_local->len);
2114 		i += sizeof(struct sadb_ident) + PADUP(ipo->ipo_ids->id_remote->len);
2115 	}
2116 
2117 	/* Allocate */
2118 	if (!(p = malloc(i, M_PFKEY, M_NOWAIT | M_ZERO))) {
2119 		rval = ENOMEM;
2120 		goto ret;
2121 	}
2122 
2123 	bzero(headers, sizeof(headers));
2124 
2125 	buffer = p;
2126 
2127 	headers[0] = p;
2128 	p += sizeof(struct sadb_msg);
2129 
2130 	smsg = (struct sadb_msg *) headers[0];
2131 	smsg->sadb_msg_version = PF_KEY_V2;
2132 	smsg->sadb_msg_type = SADB_ACQUIRE;
2133 	smsg->sadb_msg_len = i / sizeof(uint64_t);
2134 	smsg->sadb_msg_seq = *seq;
2135 
2136 	if (ipo->ipo_sproto == IPPROTO_ESP)
2137 		smsg->sadb_msg_satype = SADB_SATYPE_ESP;
2138 	else if (ipo->ipo_sproto == IPPROTO_AH)
2139 		smsg->sadb_msg_satype = SADB_SATYPE_AH;
2140 	else if (ipo->ipo_sproto == IPPROTO_IPCOMP)
2141 		smsg->sadb_msg_satype = SADB_X_SATYPE_IPCOMP;
2142 
2143 	if (laddr) {
2144 		headers[SADB_EXT_ADDRESS_SRC] = p;
2145 		p += sizeof(struct sadb_address) + PADUP(laddr->sa.sa_len);
2146 		sadd = (struct sadb_address *) headers[SADB_EXT_ADDRESS_SRC];
2147 		sadd->sadb_address_len = (sizeof(struct sadb_address) +
2148 		    laddr->sa.sa_len + sizeof(uint64_t) - 1) /
2149 		    sizeof(uint64_t);
2150 		bcopy(laddr, headers[SADB_EXT_ADDRESS_SRC] +
2151 		    sizeof(struct sadb_address), laddr->sa.sa_len);
2152 	}
2153 
2154 	headers[SADB_EXT_ADDRESS_DST] = p;
2155 	p += sizeof(struct sadb_address) + PADUP(gw->sa.sa_len);
2156 	sadd = (struct sadb_address *) headers[SADB_EXT_ADDRESS_DST];
2157 	sadd->sadb_address_len = (sizeof(struct sadb_address) +
2158 	    gw->sa.sa_len + sizeof(uint64_t) - 1) / sizeof(uint64_t);
2159 	bcopy(gw, headers[SADB_EXT_ADDRESS_DST] + sizeof(struct sadb_address),
2160 	    gw->sa.sa_len);
2161 
2162 	if (ipo->ipo_ids)
2163 		export_identities(&p, ipo->ipo_ids, 0, headers);
2164 
2165 	headers[SADB_EXT_PROPOSAL] = p;
2166 	p += sizeof(struct sadb_prop);
2167 	sa_prop = (struct sadb_prop *) headers[SADB_EXT_PROPOSAL];
2168 	sa_prop->sadb_prop_num = 1; /* XXX One proposal only */
2169 	sa_prop->sadb_prop_len = (sizeof(struct sadb_prop) +
2170 	    (sizeof(struct sadb_comb) * sa_prop->sadb_prop_num)) /
2171 	    sizeof(uint64_t);
2172 
2173 	sadb_comb = p;
2174 
2175 	/* XXX Should actually ask the crypto layer what's supported */
2176 	for (j = 0; j < sa_prop->sadb_prop_num; j++) {
2177 		sadb_comb->sadb_comb_flags = 0;
2178 #ifdef IPSEC
2179 		if (ipsec_require_pfs)
2180 			sadb_comb->sadb_comb_flags |= SADB_SAFLAGS_PFS;
2181 
2182 		/* Set the encryption algorithm */
2183 		if (ipo->ipo_sproto == IPPROTO_ESP) {
2184 			if (!strncasecmp(ipsec_def_enc, "aes",
2185 			    sizeof("aes"))) {
2186 				sadb_comb->sadb_comb_encrypt = SADB_X_EALG_AES;
2187 				sadb_comb->sadb_comb_encrypt_minbits = 128;
2188 				sadb_comb->sadb_comb_encrypt_maxbits = 256;
2189 			} else if (!strncasecmp(ipsec_def_enc, "aesctr",
2190 			    sizeof("aesctr"))) {
2191 				sadb_comb->sadb_comb_encrypt = SADB_X_EALG_AESCTR;
2192 				sadb_comb->sadb_comb_encrypt_minbits = 128+32;
2193 				sadb_comb->sadb_comb_encrypt_maxbits = 256+32;
2194 			} else if (!strncasecmp(ipsec_def_enc, "3des",
2195 			    sizeof("3des"))) {
2196 				sadb_comb->sadb_comb_encrypt = SADB_EALG_3DESCBC;
2197 				sadb_comb->sadb_comb_encrypt_minbits = 192;
2198 				sadb_comb->sadb_comb_encrypt_maxbits = 192;
2199 			} else if (!strncasecmp(ipsec_def_enc, "blowfish",
2200 			    sizeof("blowfish"))) {
2201 				sadb_comb->sadb_comb_encrypt = SADB_X_EALG_BLF;
2202 				sadb_comb->sadb_comb_encrypt_minbits = 40;
2203 				sadb_comb->sadb_comb_encrypt_maxbits = BLF_MAXKEYLEN * 8;
2204 			} else if (!strncasecmp(ipsec_def_enc, "cast128",
2205 			    sizeof("cast128"))) {
2206 				sadb_comb->sadb_comb_encrypt = SADB_X_EALG_CAST;
2207 				sadb_comb->sadb_comb_encrypt_minbits = 40;
2208 				sadb_comb->sadb_comb_encrypt_maxbits = 128;
2209 			}
2210 		} else if (ipo->ipo_sproto == IPPROTO_IPCOMP) {
2211 			/* Set the compression algorithm */
2212 			if (!strncasecmp(ipsec_def_comp, "deflate",
2213 			    sizeof("deflate"))) {
2214 				sadb_comb->sadb_comb_encrypt = SADB_X_CALG_DEFLATE;
2215 				sadb_comb->sadb_comb_encrypt_minbits = 0;
2216 				sadb_comb->sadb_comb_encrypt_maxbits = 0;
2217 			} else if (!strncasecmp(ipsec_def_comp, "lzs",
2218 			    sizeof("lzs"))) {
2219 				sadb_comb->sadb_comb_encrypt = SADB_X_CALG_LZS;
2220 				sadb_comb->sadb_comb_encrypt_minbits = 0;
2221 				sadb_comb->sadb_comb_encrypt_maxbits = 0;
2222 			}
2223 		}
2224 
2225 		/* Set the authentication algorithm */
2226 		if (!strncasecmp(ipsec_def_auth, "hmac-sha1",
2227 		    sizeof("hmac-sha1"))) {
2228 			sadb_comb->sadb_comb_auth = SADB_AALG_SHA1HMAC;
2229 			sadb_comb->sadb_comb_auth_minbits = 160;
2230 			sadb_comb->sadb_comb_auth_maxbits = 160;
2231 		} else if (!strncasecmp(ipsec_def_auth, "hmac-ripemd160",
2232 		    sizeof("hmac_ripemd160"))) {
2233 			sadb_comb->sadb_comb_auth = SADB_X_AALG_RIPEMD160HMAC;
2234 			sadb_comb->sadb_comb_auth_minbits = 160;
2235 			sadb_comb->sadb_comb_auth_maxbits = 160;
2236 		} else if (!strncasecmp(ipsec_def_auth, "hmac-md5",
2237 		    sizeof("hmac-md5"))) {
2238 			sadb_comb->sadb_comb_auth = SADB_AALG_MD5HMAC;
2239 			sadb_comb->sadb_comb_auth_minbits = 128;
2240 			sadb_comb->sadb_comb_auth_maxbits = 128;
2241 		} else if (!strncasecmp(ipsec_def_auth, "hmac-sha2-256",
2242 		    sizeof("hmac-sha2-256"))) {
2243 			sadb_comb->sadb_comb_auth = SADB_X_AALG_SHA2_256;
2244 			sadb_comb->sadb_comb_auth_minbits = 256;
2245 			sadb_comb->sadb_comb_auth_maxbits = 256;
2246 		} else if (!strncasecmp(ipsec_def_auth, "hmac-sha2-384",
2247 		    sizeof("hmac-sha2-384"))) {
2248 			sadb_comb->sadb_comb_auth = SADB_X_AALG_SHA2_384;
2249 			sadb_comb->sadb_comb_auth_minbits = 384;
2250 			sadb_comb->sadb_comb_auth_maxbits = 384;
2251 		} else if (!strncasecmp(ipsec_def_auth, "hmac-sha2-512",
2252 		    sizeof("hmac-sha2-512"))) {
2253 			sadb_comb->sadb_comb_auth = SADB_X_AALG_SHA2_512;
2254 			sadb_comb->sadb_comb_auth_minbits = 512;
2255 			sadb_comb->sadb_comb_auth_maxbits = 512;
2256 		}
2257 
2258 		sadb_comb->sadb_comb_soft_allocations = ipsec_soft_allocations;
2259 		sadb_comb->sadb_comb_hard_allocations = ipsec_exp_allocations;
2260 
2261 		sadb_comb->sadb_comb_soft_bytes = ipsec_soft_bytes;
2262 		sadb_comb->sadb_comb_hard_bytes = ipsec_exp_bytes;
2263 
2264 		sadb_comb->sadb_comb_soft_addtime = ipsec_soft_timeout;
2265 		sadb_comb->sadb_comb_hard_addtime = ipsec_exp_timeout;
2266 
2267 		sadb_comb->sadb_comb_soft_usetime = ipsec_soft_first_use;
2268 		sadb_comb->sadb_comb_hard_usetime = ipsec_exp_first_use;
2269 #endif
2270 		sadb_comb++;
2271 	}
2272 
2273 	/* Send the ACQUIRE message to all compliant registered listeners. */
2274 	if ((rval = pfkeyv2_sendmessage(headers,
2275 	    PFKEYV2_SENDMESSAGE_REGISTERED, NULL, smsg->sadb_msg_satype, 0,
2276 	    ipo->ipo_rdomain)) != 0)
2277 		goto ret;
2278 
2279 	rval = 0;
2280 ret:
2281 	if (buffer != NULL) {
2282 		bzero(buffer, i);
2283 		free(buffer, M_PFKEY, 0);
2284 	}
2285 
2286 	return (rval);
2287 }
2288 
2289 /*
2290  * Notify key management that an expiration went off. The second argument
2291  * specifies the type of expiration (soft or hard).
2292  */
2293 int
2294 pfkeyv2_expire(struct tdb *tdb, u_int16_t type)
2295 {
2296 	void *p, *headers[SADB_EXT_MAX+1], *buffer = NULL;
2297 	struct sadb_msg *smsg;
2298 	int rval = 0;
2299 	int i;
2300 
2301 	switch (tdb->tdb_sproto) {
2302 	case IPPROTO_AH:
2303 	case IPPROTO_ESP:
2304 	case IPPROTO_IPIP:
2305 	case IPPROTO_IPCOMP:
2306 #ifdef TCP_SIGNATURE
2307 	case IPPROTO_TCP:
2308 #endif /* TCP_SIGNATURE */
2309 		break;
2310 
2311 	default:
2312 		rval = EOPNOTSUPP;
2313 		goto ret;
2314 	}
2315 
2316 	i = sizeof(struct sadb_msg) + sizeof(struct sadb_sa) +
2317 	    2 * sizeof(struct sadb_lifetime) +
2318 	    sizeof(struct sadb_address) + PADUP(tdb->tdb_src.sa.sa_len) +
2319 	    sizeof(struct sadb_address) + PADUP(tdb->tdb_dst.sa.sa_len);
2320 
2321 	if (!(p = malloc(i, M_PFKEY, M_NOWAIT | M_ZERO))) {
2322 		rval = ENOMEM;
2323 		goto ret;
2324 	}
2325 
2326 	bzero(headers, sizeof(headers));
2327 
2328 	buffer = p;
2329 
2330 	headers[0] = p;
2331 	p += sizeof(struct sadb_msg);
2332 
2333 	smsg = (struct sadb_msg *) headers[0];
2334 	smsg->sadb_msg_version = PF_KEY_V2;
2335 	smsg->sadb_msg_type = SADB_EXPIRE;
2336 	smsg->sadb_msg_satype = tdb->tdb_satype;
2337 	smsg->sadb_msg_len = i / sizeof(uint64_t);
2338 
2339 	mtx_enter(&pfkeyv2_mtx);
2340 	smsg->sadb_msg_seq = pfkeyv2_seq++;
2341 	mtx_leave(&pfkeyv2_mtx);
2342 
2343 	headers[SADB_EXT_SA] = p;
2344 	export_sa(&p, tdb);
2345 
2346 	headers[SADB_EXT_LIFETIME_CURRENT] = p;
2347 	export_lifetime(&p, tdb, 2);
2348 
2349 	headers[type] = p;
2350 	export_lifetime(&p, tdb, type == SADB_EXT_LIFETIME_SOFT ?
2351 	    PFKEYV2_LIFETIME_SOFT : PFKEYV2_LIFETIME_HARD);
2352 
2353 	headers[SADB_EXT_ADDRESS_SRC] = p;
2354 	export_address(&p, &tdb->tdb_src.sa);
2355 
2356 	headers[SADB_EXT_ADDRESS_DST] = p;
2357 	export_address(&p, &tdb->tdb_dst.sa);
2358 
2359 	if ((rval = pfkeyv2_sendmessage(headers, PFKEYV2_SENDMESSAGE_BROADCAST,
2360 	    NULL, 0, 0, tdb->tdb_rdomain)) != 0)
2361 		goto ret;
2362 
2363 	rval = 0;
2364 
2365  ret:
2366 	if (buffer != NULL) {
2367 		bzero(buffer, i);
2368 		free(buffer, M_PFKEY, 0);
2369 	}
2370 
2371 	return (rval);
2372 }
2373 
2374 struct pfkeyv2_sysctl_walk {
2375 	void		*w_where;
2376 	size_t		 w_len;
2377 	int		 w_op;
2378 	u_int8_t	 w_satype;
2379 };
2380 
2381 int
2382 pfkeyv2_sysctl_walker(struct tdb *tdb, void *arg, int last)
2383 {
2384 	struct pfkeyv2_sysctl_walk *w = (struct pfkeyv2_sysctl_walk *)arg;
2385 	void *buffer = NULL;
2386 	int error = 0;
2387 	int buflen, i;
2388 
2389 	if (w->w_satype != SADB_SATYPE_UNSPEC &&
2390 	    w->w_satype != tdb->tdb_satype)
2391 		return (0);
2392 
2393 	if (w->w_where) {
2394 		void *headers[SADB_EXT_MAX+1];
2395 		struct sadb_msg msg;
2396 
2397 		bzero(headers, sizeof(headers));
2398 		if ((error = pfkeyv2_get(tdb, headers, &buffer, &buflen)) != 0)
2399 			goto done;
2400 		if (w->w_len < sizeof(msg) + buflen) {
2401 			error = ENOMEM;
2402 			goto done;
2403 		}
2404 		/* prepend header */
2405 		bzero(&msg, sizeof(msg));
2406 		msg.sadb_msg_version = PF_KEY_V2;
2407 		msg.sadb_msg_satype = tdb->tdb_satype;
2408 		msg.sadb_msg_type = SADB_DUMP;
2409 		msg.sadb_msg_len = (sizeof(msg) + buflen) / sizeof(uint64_t);
2410 		if ((error = copyout(&msg, w->w_where, sizeof(msg))) != 0)
2411 			goto done;
2412 		w->w_where += sizeof(msg);
2413 		w->w_len -= sizeof(msg);
2414 		/* set extension type */
2415 		for (i = 1; i <= SADB_EXT_MAX; i++)
2416 			if (headers[i])
2417 				((struct sadb_ext *)
2418 				    headers[i])->sadb_ext_type = i;
2419 		if ((error = copyout(buffer, w->w_where, buflen)) != 0)
2420 			goto done;
2421 		w->w_where += buflen;
2422 		w->w_len -= buflen;
2423 	} else {
2424 		if ((error = pfkeyv2_get(tdb, NULL, NULL, &buflen)) != 0)
2425 			return (error);
2426 		w->w_len += buflen;
2427 		w->w_len += sizeof(struct sadb_msg);
2428 	}
2429 
2430 done:
2431 	if (buffer)
2432 		free(buffer, M_PFKEY, 0);
2433 	return (error);
2434 }
2435 
2436 int
2437 pfkeyv2_dump_policy(struct ipsec_policy *ipo, void **headers, void **buffer,
2438     int *lenp)
2439 {
2440 	int i, rval, perm;
2441 	void *p;
2442 
2443 	/* Find how much space we need. */
2444 	i = 2 * sizeof(struct sadb_protocol);
2445 
2446 	/* We'll need four of them: src, src mask, dst, dst mask. */
2447 	switch (ipo->ipo_addr.sen_type) {
2448 	case SENT_IP4:
2449 		i += 4 * PADUP(sizeof(struct sockaddr_in));
2450 		i += 4 * sizeof(struct sadb_address);
2451 		break;
2452 #ifdef INET6
2453 	case SENT_IP6:
2454 		i += 4 * PADUP(sizeof(struct sockaddr_in6));
2455 		i += 4 * sizeof(struct sadb_address);
2456 		break;
2457 #endif /* INET6 */
2458 	default:
2459 		return (EINVAL);
2460 	}
2461 
2462 	/* Local address, might be zeroed. */
2463 	switch (ipo->ipo_src.sa.sa_family) {
2464 	case 0:
2465 		break;
2466 	case AF_INET:
2467 		i += PADUP(sizeof(struct sockaddr_in));
2468 		i += sizeof(struct sadb_address);
2469 		break;
2470 #ifdef INET6
2471 	case AF_INET6:
2472 		i += PADUP(sizeof(struct sockaddr_in6));
2473 		i += sizeof(struct sadb_address);
2474 		break;
2475 #endif /* INET6 */
2476 	default:
2477 		return (EINVAL);
2478 	}
2479 
2480 	/* Remote address, might be zeroed. XXX ??? */
2481 	switch (ipo->ipo_dst.sa.sa_family) {
2482 	case 0:
2483 		break;
2484 	case AF_INET:
2485 		i += PADUP(sizeof(struct sockaddr_in));
2486 		i += sizeof(struct sadb_address);
2487 		break;
2488 #ifdef INET6
2489 	case AF_INET6:
2490 		i += PADUP(sizeof(struct sockaddr_in6));
2491 		i += sizeof(struct sadb_address);
2492 		break;
2493 #endif /* INET6 */
2494 	default:
2495 		return (EINVAL);
2496 	}
2497 
2498 	if (ipo->ipo_ids) {
2499 		i += sizeof(struct sadb_ident) + PADUP(ipo->ipo_ids->id_local->len);
2500 		i += sizeof(struct sadb_ident) + PADUP(ipo->ipo_ids->id_remote->len);
2501 	}
2502 
2503 	if (lenp)
2504 		*lenp = i;
2505 
2506 	if (buffer == NULL) {
2507 		rval = 0;
2508 		goto ret;
2509 	}
2510 
2511 	if (!(p = malloc(i, M_PFKEY, M_NOWAIT | M_ZERO))) {
2512 		rval = ENOMEM;
2513 		goto ret;
2514 	} else
2515 		*buffer = p;
2516 
2517 	/* Local address. */
2518 	if (ipo->ipo_src.sa.sa_family) {
2519 		headers[SADB_EXT_ADDRESS_SRC] = p;
2520 		export_address(&p, &ipo->ipo_src.sa);
2521 	}
2522 
2523 	/* Remote address. */
2524 	if (ipo->ipo_dst.sa.sa_family) {
2525 		headers[SADB_EXT_ADDRESS_DST] = p;
2526 		export_address(&p, &ipo->ipo_dst.sa);
2527 	}
2528 
2529 	/* Get actual flow. */
2530 	export_flow(&p, ipo->ipo_type, &ipo->ipo_addr, &ipo->ipo_mask,
2531 	    headers);
2532 
2533 	/* Add ids only when we are root. */
2534 	perm = suser(curproc);
2535 	if (perm == 0 && ipo->ipo_ids)
2536 		export_identities(&p, ipo->ipo_ids, 0, headers);
2537 
2538 	rval = 0;
2539 ret:
2540 	return (rval);
2541 }
2542 
2543 int
2544 pfkeyv2_sysctl_policydumper(struct ipsec_policy *ipo, void *arg,
2545     unsigned int tableid)
2546 {
2547 	struct pfkeyv2_sysctl_walk *w = (struct pfkeyv2_sysctl_walk *)arg;
2548 	void *buffer = 0;
2549 	int i, buflen, error = 0;
2550 
2551 	if (w->w_where) {
2552 		void *headers[SADB_EXT_MAX + 1];
2553 		struct sadb_msg msg;
2554 
2555 		bzero(headers, sizeof(headers));
2556 		if ((error = pfkeyv2_dump_policy(ipo, headers, &buffer,
2557 		    &buflen)) != 0)
2558 			goto done;
2559 		if (w->w_len < buflen) {
2560 			error = ENOMEM;
2561 			goto done;
2562 		}
2563 		/* prepend header */
2564 		bzero(&msg, sizeof(msg));
2565 		msg.sadb_msg_version = PF_KEY_V2;
2566 		if (ipo->ipo_sproto == IPPROTO_ESP)
2567 			msg.sadb_msg_satype = SADB_SATYPE_ESP;
2568 		else if (ipo->ipo_sproto == IPPROTO_AH)
2569 			msg.sadb_msg_satype = SADB_SATYPE_AH;
2570 		else if (ipo->ipo_sproto == IPPROTO_IPCOMP)
2571 			msg.sadb_msg_satype = SADB_X_SATYPE_IPCOMP;
2572 		else if (ipo->ipo_sproto == IPPROTO_IPIP)
2573 			msg.sadb_msg_satype = SADB_X_SATYPE_IPIP;
2574 		msg.sadb_msg_type = SADB_X_SPDDUMP;
2575 		msg.sadb_msg_len = (sizeof(msg) + buflen) / sizeof(uint64_t);
2576 		if ((error = copyout(&msg, w->w_where, sizeof(msg))) != 0)
2577 			goto done;
2578 		w->w_where += sizeof(msg);
2579 		w->w_len -= sizeof(msg);
2580 		/* set extension type */
2581 		for (i = 1; i <= SADB_EXT_MAX; i++)
2582 			if (headers[i])
2583 				((struct sadb_ext *)
2584 				    headers[i])->sadb_ext_type = i;
2585 		if ((error = copyout(buffer, w->w_where, buflen)) != 0)
2586 			goto done;
2587 		w->w_where += buflen;
2588 		w->w_len -= buflen;
2589 	} else {
2590 		if ((error = pfkeyv2_dump_policy(ipo, NULL, NULL,
2591 		    &buflen)) != 0)
2592 			goto done;
2593 		w->w_len += buflen;
2594 		w->w_len += sizeof(struct sadb_msg);
2595 	}
2596 
2597 done:
2598 	if (buffer)
2599 		free(buffer, M_PFKEY, 0);
2600 	return (error);
2601 }
2602 
2603 int
2604 pfkeyv2_policy_flush(struct ipsec_policy *ipo, void *arg, unsigned int tableid)
2605 {
2606 	int error;
2607 
2608 	error = ipsec_delete_policy(ipo);
2609 	if (error == 0)
2610 		error = EAGAIN;
2611 
2612 	return (error);
2613 }
2614 
2615 int
2616 pfkeyv2_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2617     void *new, size_t newlen)
2618 {
2619 	struct pfkeyv2_sysctl_walk w;
2620 	int error = EINVAL;
2621 	u_int rdomain;
2622 
2623 	if (new)
2624 		return (EPERM);
2625 	if (namelen < 1)
2626 		return (EINVAL);
2627 	w.w_op = name[0];
2628 	w.w_satype = name[1];
2629 	w.w_where = oldp;
2630 	w.w_len = oldp ? *oldlenp : 0;
2631 
2632 	rdomain = rtable_l2(curproc->p_p->ps_rtableid);
2633 
2634 	switch(w.w_op) {
2635 	case NET_KEY_SADB_DUMP:
2636 		if ((error = suser(curproc)) != 0)
2637 			return (error);
2638 		NET_LOCK();
2639 		error = tdb_walk(rdomain, pfkeyv2_sysctl_walker, &w);
2640 		NET_UNLOCK();
2641 		if (oldp)
2642 			*oldlenp = w.w_where - oldp;
2643 		else
2644 			*oldlenp = w.w_len;
2645 		break;
2646 
2647 	case NET_KEY_SPD_DUMP:
2648 		NET_LOCK();
2649 		error = spd_table_walk(rdomain,
2650 		    pfkeyv2_sysctl_policydumper, &w);
2651 		NET_UNLOCK();
2652 		if (oldp)
2653 			*oldlenp = w.w_where - oldp;
2654 		else
2655 			*oldlenp = w.w_len;
2656 		break;
2657 	}
2658 
2659 	return (error);
2660 }
2661