1 /* $FreeBSD: src/sys/netipsec/keysock.c,v 1.3.2.1 2003/01/24 05:11:36 sam Exp $ */
2 /* $DragonFly: src/sys/netproto/ipsec/keysock.c,v 1.18 2008/01/06 16:55:52 swildner Exp $ */
3 /* $KAME: keysock.c,v 1.25 2001/08/13 20:07:41 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "opt_ipsec.h"
36 /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/domain.h>
41 #include <sys/errno.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
45 #include <sys/protosw.h>
46 #include <sys/signalvar.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 #include <sys/thread2.h>
53 #include <net/raw_cb.h>
54 #include <net/route.h>
56 #include <net/pfkeyv2.h>
57 #include <netproto/ipsec/key.h>
58 #include <netproto/ipsec/keysock.h>
59 #include <netproto/ipsec/key_debug.h>
61 #include <machine/stdarg.h>
63 typedef int pr_output_t (struct mbuf
*, struct socket
*);
69 static struct key_cb key_cb
;
71 static struct sockaddr key_dst
= { 2, PF_KEY
, };
72 static struct sockaddr key_src
= { 2, PF_KEY
, };
74 static int key_sendup0 (struct rawcb
*, struct mbuf
*, int);
76 struct pfkeystat pfkeystat
;
82 key_output(struct mbuf
*m
, struct socket
*so
, ...)
89 panic("key_output: NULL pointer was passed.\n");
91 pfkeystat
.out_total
++;
92 pfkeystat
.out_bytes
+= m
->m_pkthdr
.len
;
94 len
= m
->m_pkthdr
.len
;
95 if (len
< sizeof(struct sadb_msg
)) {
96 pfkeystat
.out_tooshort
++;
101 if (m
->m_len
< sizeof(struct sadb_msg
)) {
102 if ((m
= m_pullup(m
, sizeof(struct sadb_msg
))) == 0) {
103 pfkeystat
.out_nomem
++;
109 if ((m
->m_flags
& M_PKTHDR
) == 0)
110 panic("key_output: not M_PKTHDR ??");
112 KEYDEBUG(KEYDEBUG_KEY_DUMP
, kdebug_mbuf(m
));
114 msg
= mtod(m
, struct sadb_msg
*);
115 pfkeystat
.out_msgtype
[msg
->sadb_msg_type
]++;
116 if (len
!= PFKEY_UNUNIT64(msg
->sadb_msg_len
)) {
117 pfkeystat
.out_invlen
++;
124 error
= key_parse(m
, so
);
134 * send message to the socket.
137 key_sendup0(struct rawcb
*rp
, struct mbuf
*m
, int promisc
)
142 struct sadb_msg
*pmsg
;
144 M_PREPEND(m
, sizeof(struct sadb_msg
), MB_DONTWAIT
);
145 if (m
&& m
->m_len
< sizeof(struct sadb_msg
))
146 m
= m_pullup(m
, sizeof(struct sadb_msg
));
148 pfkeystat
.in_nomem
++;
152 m
->m_pkthdr
.len
+= sizeof(*pmsg
);
154 pmsg
= mtod(m
, struct sadb_msg
*);
155 bzero(pmsg
, sizeof(*pmsg
));
156 pmsg
->sadb_msg_version
= PF_KEY_V2
;
157 pmsg
->sadb_msg_type
= SADB_X_PROMISC
;
158 pmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
161 pfkeystat
.in_msgtype
[pmsg
->sadb_msg_type
]++;
164 if (!ssb_appendaddr(&rp
->rcb_socket
->so_rcv
, (struct sockaddr
*)&key_src
,
166 pfkeystat
.in_nomem
++;
171 sorwakeup(rp
->rcb_socket
);
176 * XXX this interface should be obsoleted.
179 * target: target of the resulting message
182 key_sendup(struct socket
*so
, struct sadb_msg
*msg
, u_int len
, int target
)
184 struct mbuf
*m
, *n
, *mprev
;
188 if (so
== 0 || msg
== 0)
189 panic("key_sendup: NULL pointer was passed.\n");
191 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
192 kprintf("key_sendup: \n");
196 * we increment statistics here, just in case we have ENOBUFS
199 pfkeystat
.in_total
++;
200 pfkeystat
.in_bytes
+= len
;
201 pfkeystat
.in_msgtype
[msg
->sadb_msg_type
]++;
204 * Get mbuf chain whenever possible (not clusters),
205 * to save socket buffer. We'll be generating many SADB_ACQUIRE
206 * messages to listening key sockets. If we simply allocate clusters,
207 * ssb_appendaddr() will raise ENOBUFS due to too little ssb_space().
208 * ssb_space() computes # of actual data bytes AND mbuf region.
210 * TODO: SADB_ACQUIRE filters should be implemented.
217 n
= m_getl(tlen
, MB_DONTWAIT
, MT_DATA
,
218 (tlen
== len
) ? M_PKTHDR
: 0, &nsize
);
221 pfkeystat
.in_nomem
++;
224 n
->m_len
= (tlen
< n
->m_len
) ? tlen
: nsize
;
234 m
->m_pkthdr
.len
= len
;
235 m_copyback(m
, 0, len
, (caddr_t
)msg
);
237 /* avoid duplicated statistics */
238 pfkeystat
.in_total
--;
239 pfkeystat
.in_bytes
-= len
;
240 pfkeystat
.in_msgtype
[msg
->sadb_msg_type
]--;
242 return key_sendup_mbuf(so
, m
, target
);
245 /* so can be NULL if target != KEY_SENDUP_ONE */
247 key_sendup_mbuf(struct socket
*so
, struct mbuf
*m
, int target
)
256 panic("key_sendup_mbuf: NULL pointer was passed.\n");
257 if (so
== NULL
&& target
== KEY_SENDUP_ONE
)
258 panic("key_sendup_mbuf: NULL pointer was passed.\n");
260 pfkeystat
.in_total
++;
261 pfkeystat
.in_bytes
+= m
->m_pkthdr
.len
;
262 if (m
->m_len
< sizeof(struct sadb_msg
)) {
264 m
= m_pullup(m
, sizeof(struct sadb_msg
));
266 pfkeystat
.in_nomem
++;
270 /* don't bother pulling it up just for stats */
273 if (m
->m_len
>= sizeof(struct sadb_msg
)) {
274 struct sadb_msg
*msg
;
275 msg
= mtod(m
, struct sadb_msg
*);
276 pfkeystat
.in_msgtype
[msg
->sadb_msg_type
]++;
279 LIST_FOREACH(rp
, &rawcb_list
, list
)
281 if (rp
->rcb_proto
.sp_family
!= PF_KEY
)
283 if (rp
->rcb_proto
.sp_protocol
284 && rp
->rcb_proto
.sp_protocol
!= PF_KEY_V2
) {
288 kp
= (struct keycb
*)rp
;
291 * If you are in promiscuous mode, and when you get broadcasted
292 * reply, you'll get two PF_KEY messages.
293 * (based on pf_key@inner.net message on 14 Oct 1998)
295 if (((struct keycb
*)rp
)->kp_promisc
) {
296 if ((n
= m_copy(m
, 0, (int)M_COPYALL
)) != NULL
) {
297 key_sendup0(rp
, n
, 1);
302 /* the exact target will be processed later */
303 if (so
&& sotorawcb(so
) == rp
)
309 /* the statement has no effect */
310 if (so
&& sotorawcb(so
) == rp
)
316 case KEY_SENDUP_REGISTERED
:
317 if (kp
->kp_registered
)
321 pfkeystat
.in_msgtarget
[target
]++;
326 if ((n
= m_copy(m
, 0, (int)M_COPYALL
)) == NULL
) {
328 pfkeystat
.in_nomem
++;
332 if ((error
= key_sendup0(rp
, n
, 0)) != 0) {
341 error
= key_sendup0(sotorawcb(so
), m
, 0);
352 * derived from net/rtsock.c:rts_abort()
355 key_abort(struct socket
*so
)
359 error
= raw_usrreqs
.pru_abort(so
);
366 * derived from net/rtsock.c:rts_attach()
369 key_attach(struct socket
*so
, int proto
, struct pru_attach_info
*ai
)
374 if (sotorawcb(so
) != 0)
375 return EISCONN
; /* XXX panic? */
376 kp
= (struct keycb
*)kmalloc(sizeof *kp
, M_PCB
, M_WAITOK
|M_ZERO
); /* XXX */
379 * The critical section is necessary to block protocols from sending
380 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
381 * this PCB is extant but incompletely initialized.
382 * Probably we should try to do more of this work beforehand and
386 so
->so_pcb
= (caddr_t
)kp
;
387 error
= raw_usrreqs
.pru_attach(so
, proto
, ai
);
388 kp
= (struct keycb
*)sotorawcb(so
);
391 so
->so_pcb
= (caddr_t
) 0;
396 kp
->kp_promisc
= kp
->kp_registered
= 0;
398 if (kp
->kp_raw
.rcb_proto
.sp_protocol
== PF_KEY
) /* XXX: AF_KEY */
401 kp
->kp_raw
.rcb_laddr
= &key_src
;
402 kp
->kp_raw
.rcb_faddr
= &key_dst
;
404 so
->so_options
|= SO_USELOOPBACK
;
412 * derived from net/rtsock.c:rts_bind()
415 key_bind(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
419 error
= raw_usrreqs
.pru_bind(so
, nam
, td
); /* xxx just EINVAL */
426 * derived from net/rtsock.c:rts_connect()
429 key_connect(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
433 error
= raw_usrreqs
.pru_connect(so
, nam
, td
); /* XXX just EINVAL */
440 * derived from net/rtsock.c:rts_detach()
443 key_detach(struct socket
*so
)
445 struct keycb
*kp
= (struct keycb
*)sotorawcb(so
);
450 if (kp
->kp_raw
.rcb_proto
.sp_protocol
451 == PF_KEY
) /* XXX: AF_KEY */
457 error
= raw_usrreqs
.pru_detach(so
);
464 * derived from net/rtsock.c:key_disconnect()
467 key_disconnect(struct socket
*so
)
471 error
= raw_usrreqs
.pru_disconnect(so
);
478 * derived from net/rtsock.c:rts_peeraddr()
481 key_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
485 error
= raw_usrreqs
.pru_peeraddr(so
, nam
);
492 * derived from net/rtsock.c:rts_send()
495 key_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
496 struct mbuf
*control
, struct thread
*td
)
500 error
= raw_usrreqs
.pru_send(so
, flags
, m
, nam
, control
, td
);
507 * derived from net/rtsock.c:rts_shutdown()
510 key_shutdown(struct socket
*so
)
514 error
= raw_usrreqs
.pru_shutdown(so
);
521 * derived from net/rtsock.c:rts_sockaddr()
524 key_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
528 error
= raw_usrreqs
.pru_sockaddr(so
, nam
);
533 struct pr_usrreqs key_usrreqs
= {
534 .pru_abort
= key_abort
,
535 .pru_accept
= pru_accept_notsupp
,
536 .pru_attach
= key_attach
,
537 .pru_bind
= key_bind
,
538 .pru_connect
= key_connect
,
539 .pru_connect2
= pru_connect2_notsupp
,
540 .pru_control
= pru_control_notsupp
,
541 .pru_detach
= key_detach
,
542 .pru_disconnect
= key_disconnect
,
543 .pru_listen
= pru_listen_notsupp
,
544 .pru_peeraddr
= key_peeraddr
,
545 .pru_rcvd
= pru_rcvd_notsupp
,
546 .pru_rcvoob
= pru_rcvoob_notsupp
,
547 .pru_send
= key_send
,
548 .pru_sense
= pru_sense_null
,
549 .pru_shutdown
= key_shutdown
,
550 .pru_sockaddr
= key_sockaddr
,
551 .pru_sosend
= sosend
,
552 .pru_soreceive
= soreceive
,
557 SYSCTL_NODE(_net
, PF_KEY
, key
, CTLFLAG_RW
, 0, "Key Family");
560 * Definitions of protocols supported in the KEY domain.
563 extern struct domain keydomain
;
565 struct protosw keysw
[] = {
566 { SOCK_RAW
, &keydomain
, PF_KEY_V2
, PR_ATOMIC
|PR_ADDR
,
567 0, key_output
, raw_ctlinput
, 0,
577 bzero((caddr_t
)&key_cb
, sizeof(key_cb
));
581 struct domain keydomain
= {
582 PF_KEY
, "key", key_init0
, NULL
, NULL
,
583 keysw
, &keysw
[sizeof(keysw
)/sizeof(keysw
[0])],