]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netkey/keysock.c
1 /* $KAME: keysock.c,v 1.13 2000/03/25 07:24:13 sumikawa Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * 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.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
36 /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
38 #if defined(__NetBSD__) || defined (__APPLE__)
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #if defined(__FreeBSD__) || defined (__APPLE__)
49 #include <sys/sysctl.h>
52 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
53 #include <sys/malloc.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/domain.h>
58 #include <sys/protosw.h>
59 #include <sys/errno.h>
62 #include <sys/queue.h>
67 #include <machine/ipl.h>
69 #include <machine/spl.h>
73 #include <net/raw_cb.h>
74 #include <net/route.h>
76 #include <net/pfkeyv2.h>
77 #include <netkey/keydb.h>
78 #include <netkey/key.h>
79 #include <netkey/keysock.h>
80 #include <netkey/key_debug.h>
83 struct sockaddr key_dst
= { 2, PF_KEY
, };
84 struct sockaddr key_src
= { 2, PF_KEY
, };
85 struct sockproto key_proto
= { PF_KEY
, PF_KEY_V2
};
87 static int key_sendup0
__P((struct rawcb
*, struct mbuf
*, int));
89 struct pfkeystat pfkeystat
;
91 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__APPLE__)
94 * derived from net/rtsock.c:route_usrreq()
98 key_usrreq(so
, req
, m
, nam
, control
)
99 register struct socket
*so
;
101 struct mbuf
*m
, *nam
, *control
;
104 key_usrreq(so
, req
, m
, nam
, control
, p
)
105 register struct socket
*so
;
107 struct mbuf
*m
, *nam
, *control
;
109 #endif /*__NetBSD__*/
111 register int error
= 0;
112 register struct keycb
*kp
= (struct keycb
*)sotorawcb(so
);
120 if (req
== PRU_ATTACH
) {
121 kp
= (struct keycb
*)_MALLOC(sizeof(*kp
), M_PCB
, M_WAITOK
);
122 so
->so_pcb
= (caddr_t
)kp
;
124 bzero(so
->so_pcb
, sizeof(*kp
));
126 if (req
== PRU_DETACH
&& kp
) {
127 int af
= kp
->kp_raw
.rcb_proto
.sp_protocol
;
128 if (af
== PF_KEY
) /* XXX: AF_KEY */
136 error
= raw_usrreq(so
, req
, m
, nam
, control
);
138 error
= raw_usrreq(so
, req
, m
, nam
, control
, p
);
140 m
= control
= NULL
; /* reclaimed in raw_usrreq */
141 kp
= (struct keycb
*)sotorawcb(so
);
142 if (req
== PRU_ATTACH
&& kp
) {
143 int af
= kp
->kp_raw
.rcb_proto
.sp_protocol
;
146 printf("key_usrreq: key_usrreq results %d\n", error
);
149 _FREE((caddr_t
)kp
, M_PCB
);
150 so
->so_pcb
= (caddr_t
) 0;
155 kp
->kp_promisc
= kp
->kp_registered
= 0;
157 if (af
== PF_KEY
) /* XXX: AF_KEY */
161 kp
->kp_raw
.rcb_laddr
= &key_src
;
162 kp
->kp_raw
.rcb_faddr
= &key_dst
;
165 * XXX rcb_faddr must be dynamically allocated, otherwise
166 * raw_disconnect() will be angry.
170 MGET(m
, M_WAITOK
, MT_DATA
);
173 pfkeystat
.in_nomem
++;
174 _FREE((caddr_t
)kp
, M_PCB
);
175 so
->so_pcb
= (caddr_t
) 0;
179 MGET(n
, M_WAITOK
, MT_DATA
);
183 pfkeystat
.in_nomem
++;
184 _FREE((caddr_t
)kp
, M_PCB
);
185 so
->so_pcb
= (caddr_t
) 0;
189 m
->m_len
= sizeof(key_src
);
190 kp
->kp_raw
.rcb_laddr
= mtod(m
, struct sockaddr
*);
191 bcopy(&key_src
, kp
->kp_raw
.rcb_laddr
, sizeof(key_src
));
192 n
->m_len
= sizeof(key_dst
);
193 kp
->kp_raw
.rcb_faddr
= mtod(n
, struct sockaddr
*);
194 bcopy(&key_dst
, kp
->kp_raw
.rcb_faddr
, sizeof(key_dst
));
198 so
->so_options
|= SO_USELOOPBACK
;
203 #endif /* other than FreeBSD >= 3 */
210 register struct mbuf
*m
;
213 struct sadb_msg
*msg
= NULL
;
219 panic("key_output: NULL pointer was passed.\n");
221 pfkeystat
.out_total
++;
222 pfkeystat
.out_bytes
+= m
->m_pkthdr
.len
;
224 len
= m
->m_pkthdr
.len
;
225 if (len
< sizeof(struct sadb_msg
)) {
227 printf("key_output: Invalid message length.\n");
229 pfkeystat
.out_tooshort
++;
234 if (m
->m_len
< sizeof(struct sadb_msg
)) {
235 if ((m
= m_pullup(m
, sizeof(struct sadb_msg
))) == 0) {
237 printf("key_output: can't pullup mbuf\n");
239 pfkeystat
.out_nomem
++;
245 if ((m
->m_flags
& M_PKTHDR
) == 0)
246 panic("key_output: not M_PKTHDR ??");
248 #if defined(IPSEC_DEBUG)
249 KEYDEBUG(KEYDEBUG_KEY_DUMP
, kdebug_mbuf(m
));
250 #endif /* defined(IPSEC_DEBUG) */
252 msg
= mtod(m
, struct sadb_msg
*);
253 pfkeystat
.out_msgtype
[msg
->sadb_msg_type
]++;
254 if (len
!= PFKEY_UNUNIT64(msg
->sadb_msg_len
)) {
256 printf("key_output: Invalid message length.\n");
258 pfkeystat
.out_invlen
++;
264 * allocate memory for sadb_msg, and copy to sadb_msg from mbuf
265 * XXX: To be processed directly without a copy.
267 msg
= (struct sadb_msg
*)_MALLOC(len
, M_SECA
, M_NOWAIT
);
270 printf("key_output: No more memory.\n");
273 pfkeystat
.out_nomem
++;
277 m_copydata(m
, 0, len
, (caddr_t
)msg
);
285 if ((len
= key_parse(&msg
, so
, &target
)) == 0) {
286 /* discard. i.e. no need to reply. */
287 /* msg has been freed at key_parse() */
293 /* send up message to the socket */
294 error
= key_sendup(so
, msg
, len
, target
);
303 * send message to the socket.
306 key_sendup0(rp
, m
, promisc
)
312 struct sadb_msg
*pmsg
;
314 M_PREPEND(m
, sizeof(struct sadb_msg
), M_NOWAIT
);
315 if (m
&& m
->m_len
< sizeof(struct sadb_msg
))
316 m
= m_pullup(m
, sizeof(struct sadb_msg
));
319 printf("key_sendup0: cannot pullup\n");
321 pfkeystat
.in_nomem
++;
325 m
->m_pkthdr
.len
+= sizeof(*pmsg
);
327 pmsg
= mtod(m
, struct sadb_msg
*);
328 bzero(pmsg
, sizeof(*pmsg
));
329 pmsg
->sadb_msg_version
= PF_KEY_V2
;
330 pmsg
->sadb_msg_type
= SADB_X_PROMISC
;
331 pmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
334 pfkeystat
.in_msgtype
[pmsg
->sadb_msg_type
]++;
337 if (!sbappendaddr(&rp
->rcb_socket
->so_rcv
,
338 (struct sockaddr
*)&key_src
, m
, NULL
)) {
340 printf("key_sendup0: sbappendaddr failed\n");
342 pfkeystat
.in_nomem
++;
346 sorwakeup(rp
->rcb_socket
);
350 /* XXX this interface should be obsoleted. */
352 key_sendup(so
, msg
, len
, target
)
354 struct sadb_msg
*msg
;
356 int target
; /*target of the resulting message*/
358 struct mbuf
*m
, *n
, *mprev
;
362 if (so
== 0 || msg
== 0)
363 panic("key_sendup: NULL pointer was passed.\n");
365 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
366 printf("key_sendup: \n");
370 * we increment statistics here, just in case we have ENOBUFS
373 pfkeystat
.in_total
++;
374 pfkeystat
.in_bytes
+= len
;
375 pfkeystat
.in_msgtype
[msg
->sadb_msg_type
]++;
378 * Get mbuf chain whenever possible (not clusters),
379 * to save socket buffer. We'll be generating many SADB_ACQUIRE
380 * messages to listening key sockets. If we simmply allocate clusters,
381 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
382 * sbspace() computes # of actual data bytes AND mbuf region.
384 * TODO: SADB_ACQUIRE filters should be implemented.
390 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
393 MGET(n
, M_DONTWAIT
, MT_DATA
);
397 pfkeystat
.in_nomem
++;
400 if (tlen
>= MCLBYTES
) { /*XXX better threshold? */
401 MCLGET(n
, M_DONTWAIT
);
402 if ((n
->m_flags
& M_EXT
) == 0) {
405 pfkeystat
.in_nomem
++;
423 m
->m_pkthdr
.len
= len
;
424 m
->m_pkthdr
.rcvif
= NULL
;
425 m_copyback(m
, 0, len
, (caddr_t
)msg
);
427 /* avoid duplicated statistics */
428 pfkeystat
.in_total
--;
429 pfkeystat
.in_bytes
-= len
;
430 pfkeystat
.in_msgtype
[msg
->sadb_msg_type
]--;
432 return key_sendup_mbuf(so
, m
, target
);
436 key_sendup_mbuf(so
, m
, target
)
447 if (so
== NULL
|| m
== NULL
)
448 panic("key_sendup_mbuf: NULL pointer was passed.\n");
450 pfkeystat
.in_total
++;
451 pfkeystat
.in_bytes
+= m
->m_pkthdr
.len
;
452 if (m
->m_len
< sizeof(struct sadb_msg
)) {
454 m
= m_pullup(m
, sizeof(struct sadb_msg
));
456 pfkeystat
.in_nomem
++;
460 /* don't bother pulling it up just for stats */
463 if (m
->m_len
>= sizeof(struct sadb_msg
)) {
464 struct sadb_msg
*msg
;
465 msg
= mtod(m
, struct sadb_msg
*);
466 pfkeystat
.in_msgtype
[msg
->sadb_msg_type
]++;
470 for (rp
= rawcb
.lh_first
; rp
; rp
= rp
->rcb_list
.le_next
)
471 #elif defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
472 LIST_FOREACH(rp
, &rawcb_list
, list
)
474 for (rp
= rawcb
.rcb_next
; rp
!= &rawcb
; rp
= rp
->rcb_next
)
477 if (rp
->rcb_proto
.sp_family
!= PF_KEY
)
479 if (rp
->rcb_proto
.sp_protocol
480 && rp
->rcb_proto
.sp_protocol
!= PF_KEY_V2
) {
484 kp
= (struct keycb
*)rp
;
487 * If you are in promiscuous mode, and when you get broadcasted
488 * reply, you'll get two PF_KEY messages.
489 * (based on pf_key@inner.net message on 14 Oct 1998)
491 if (((struct keycb
*)rp
)->kp_promisc
) {
492 if ((n
= m_copy(m
, 0, (int)M_COPYALL
)) != NULL
) {
493 (void)key_sendup0(rp
, n
, 1);
498 /* the exact target will be processed later */
499 if (sotorawcb(so
) == rp
)
505 /* the statement has no effect */
506 if (sotorawcb(so
) == rp
)
512 case KEY_SENDUP_REGISTERED
:
513 if (kp
->kp_registered
)
517 pfkeystat
.in_msgtarget
[target
]++;
522 if ((n
= m_copy(m
, 0, (int)M_COPYALL
)) == NULL
) {
524 printf("key_sendup: m_copy fail\n");
527 pfkeystat
.in_nomem
++;
531 if ((error
= key_sendup0(rp
, n
, 0)) != 0) {
539 error
= key_sendup0(sotorawcb(so
), m
, 0);
544 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__)
547 * derived from net/rtsock.c:rts_abort()
550 key_abort(struct socket
*so
)
554 error
= raw_usrreqs
.pru_abort(so
);
561 * derived from net/rtsock.c:rts_attach()
564 key_attach(struct socket
*so
, int proto
, struct proc
*p
)
569 if (sotorawcb(so
) != 0)
570 return EISCONN
; /* XXX panic? */
571 kp
= (struct keycb
*)_MALLOC(sizeof *kp
, M_PCB
, M_WAITOK
); /* XXX */
574 bzero(kp
, sizeof *kp
);
577 * The splnet() is necessary to block protocols from sending
578 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
579 * this PCB is extant but incompletely initialized.
580 * Probably we should try to do more of this work beforehand and
584 so
->so_pcb
= (caddr_t
)kp
;
585 error
= raw_usrreqs
.pru_attach(so
, proto
, p
);
586 kp
= (struct keycb
*)sotorawcb(so
);
589 so
->so_pcb
= (caddr_t
) 0;
591 printf("key_usrreq: key_usrreq results %d\n", error
);
595 kp
->kp_promisc
= kp
->kp_registered
= 0;
597 if (kp
->kp_raw
.rcb_proto
.sp_protocol
== PF_KEY
) /* XXX: AF_KEY */
600 kp
->kp_raw
.rcb_laddr
= &key_src
;
601 kp
->kp_raw
.rcb_faddr
= &key_dst
;
603 so
->so_options
|= SO_USELOOPBACK
;
611 * derived from net/rtsock.c:rts_bind()
614 key_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
618 error
= raw_usrreqs
.pru_bind(so
, nam
, p
); /* xxx just EINVAL */
625 * derived from net/rtsock.c:rts_connect()
628 key_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
632 error
= raw_usrreqs
.pru_connect(so
, nam
, p
); /* XXX just EINVAL */
639 * derived from net/rtsock.c:rts_detach()
642 key_detach(struct socket
*so
)
644 struct keycb
*kp
= (struct keycb
*)sotorawcb(so
);
649 if (kp
->kp_raw
.rcb_proto
.sp_protocol
650 == PF_KEY
) /* XXX: AF_KEY */
656 error
= raw_usrreqs
.pru_detach(so
);
663 * derived from net/rtsock.c:key_disconnect()
666 key_disconnect(struct socket
*so
)
670 error
= raw_usrreqs
.pru_disconnect(so
);
677 * derived from net/rtsock.c:rts_peeraddr()
680 key_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
684 error
= raw_usrreqs
.pru_peeraddr(so
, nam
);
691 * derived from net/rtsock.c:rts_send()
694 key_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
695 struct mbuf
*control
, struct proc
*p
)
699 error
= raw_usrreqs
.pru_send(so
, flags
, m
, nam
, control
, p
);
706 * derived from net/rtsock.c:rts_shutdown()
709 key_shutdown(struct socket
*so
)
713 error
= raw_usrreqs
.pru_shutdown(so
);
720 * derived from net/rtsock.c:rts_sockaddr()
723 key_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
727 error
= raw_usrreqs
.pru_sockaddr(so
, nam
);
732 struct pr_usrreqs key_usrreqs
= {
733 key_abort
, pru_accept_notsupp
, key_attach
, key_bind
,
735 pru_connect2_notsupp
, pru_control_notsupp
, key_detach
,
736 key_disconnect
, pru_listen_notsupp
, key_peeraddr
,
738 pru_rcvoob_notsupp
, key_send
, pru_sense_null
, key_shutdown
,
739 key_sockaddr
, sosend
, soreceive
, sopoll
741 #endif /* __FreeBSD__ >= 3 */
743 #if __FreeBSD__ || defined (__APPLE__)
745 SYSCTL_NODE(_net
, PF_KEY
, key
, CTLFLAG_RW
, 0, "Key Family");
749 * Definitions of protocols supported in the KEY domain.
752 extern struct domain keydomain
;
754 struct protosw keysw
[] = {
755 { SOCK_RAW
, &keydomain
, PF_KEY_V2
, PR_ATOMIC
|PR_ADDR
,
756 0, key_output
, raw_ctlinput
, 0,
763 struct domain keydomain
=
764 { PF_KEY
, "key", key_init
, 0, 0,
767 sizeof(struct key_cb
), 0