]>
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 /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/errno.h>
48 #include <net/raw_cb.h>
49 #include <net/route.h>
51 #include <net/pfkeyv2.h>
52 #include <netkey/keydb.h>
53 #include <netkey/key.h>
54 #include <netkey/keysock.h>
55 #include <netkey/key_debug.h>
57 struct sockaddr key_dst
= { 2, PF_KEY
, };
58 struct sockaddr key_src
= { 2, PF_KEY
, };
60 static int key_sendup0
__P((struct rawcb
*, struct mbuf
*, int));
62 struct pfkeystat pfkeystat
;
69 /* No variable argument support? */
70 key_output(struct mbuf
*m
, struct socket
*so
)
73 key_output(struct mbuf
*m
, ...)
75 key_output(m
, va_alist
)
89 so
= va_arg(ap
, struct socket
*);
94 panic("key_output: NULL pointer was passed.\n");
96 pfkeystat
.out_total
++;
97 pfkeystat
.out_bytes
+= m
->m_pkthdr
.len
;
99 len
= m
->m_pkthdr
.len
;
100 if (len
< sizeof(struct sadb_msg
)) {
102 printf("key_output: Invalid message length.\n");
104 pfkeystat
.out_tooshort
++;
109 if (m
->m_len
< sizeof(struct sadb_msg
)) {
110 if ((m
= m_pullup(m
, sizeof(struct sadb_msg
))) == 0) {
112 printf("key_output: can't pullup mbuf\n");
114 pfkeystat
.out_nomem
++;
120 if ((m
->m_flags
& M_PKTHDR
) == 0)
121 panic("key_output: not M_PKTHDR ??");
124 KEYDEBUG(KEYDEBUG_KEY_DUMP
, kdebug_mbuf(m
));
125 #endif /* defined(IPSEC_DEBUG) */
127 msg
= mtod(m
, struct sadb_msg
*);
128 pfkeystat
.out_msgtype
[msg
->sadb_msg_type
]++;
129 if (len
!= PFKEY_UNUNIT64(msg
->sadb_msg_len
)) {
131 printf("key_output: Invalid message length.\n");
133 pfkeystat
.out_invlen
++;
140 error
= key_parse(m
, so
);
150 * send message to the socket.
153 key_sendup0(rp
, m
, promisc
)
161 struct sadb_msg
*pmsg
;
163 M_PREPEND(m
, sizeof(struct sadb_msg
), M_NOWAIT
);
164 if (m
&& m
->m_len
< sizeof(struct sadb_msg
))
165 m
= m_pullup(m
, sizeof(struct sadb_msg
));
168 printf("key_sendup0: cannot pullup\n");
170 pfkeystat
.in_nomem
++;
174 m
->m_pkthdr
.len
+= sizeof(*pmsg
);
176 pmsg
= mtod(m
, struct sadb_msg
*);
177 bzero(pmsg
, sizeof(*pmsg
));
178 pmsg
->sadb_msg_version
= PF_KEY_V2
;
179 pmsg
->sadb_msg_type
= SADB_X_PROMISC
;
180 pmsg
->sadb_msg_len
= PFKEY_UNIT64(m
->m_pkthdr
.len
);
183 pfkeystat
.in_msgtype
[pmsg
->sadb_msg_type
]++;
186 if (!sbappendaddr(&rp
->rcb_socket
->so_rcv
, (struct sockaddr
*)&key_src
,
189 printf("key_sendup0: sbappendaddr failed\n");
191 pfkeystat
.in_nomem
++;
196 sorwakeup(rp
->rcb_socket
);
200 /* XXX this interface should be obsoleted. */
202 key_sendup(so
, msg
, len
, target
)
204 struct sadb_msg
*msg
;
206 int target
; /*target of the resulting message*/
208 struct mbuf
*m
, *n
, *mprev
;
212 if (so
== 0 || msg
== 0)
213 panic("key_sendup: NULL pointer was passed.\n");
215 KEYDEBUG(KEYDEBUG_KEY_DUMP
,
216 printf("key_sendup: \n");
220 * we increment statistics here, just in case we have ENOBUFS
223 pfkeystat
.in_total
++;
224 pfkeystat
.in_bytes
+= len
;
225 pfkeystat
.in_msgtype
[msg
->sadb_msg_type
]++;
228 * Get mbuf chain whenever possible (not clusters),
229 * to save socket buffer. We'll be generating many SADB_ACQUIRE
230 * messages to listening key sockets. If we simply allocate clusters,
231 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
232 * sbspace() computes # of actual data bytes AND mbuf region.
234 * TODO: SADB_ACQUIRE filters should be implemented.
240 MGETHDR(n
, M_DONTWAIT
, MT_DATA
);
243 MGET(n
, M_DONTWAIT
, MT_DATA
);
247 pfkeystat
.in_nomem
++;
250 if (tlen
>= MCLBYTES
) { /*XXX better threshold? */
251 MCLGET(n
, M_DONTWAIT
);
252 if ((n
->m_flags
& M_EXT
) == 0) {
255 pfkeystat
.in_nomem
++;
273 m
->m_pkthdr
.len
= len
;
274 m
->m_pkthdr
.rcvif
= NULL
;
275 m_copyback(m
, 0, len
, (caddr_t
)msg
);
277 /* avoid duplicated statistics */
278 pfkeystat
.in_total
--;
279 pfkeystat
.in_bytes
-= len
;
280 pfkeystat
.in_msgtype
[msg
->sadb_msg_type
]--;
282 return key_sendup_mbuf(so
, m
, target
);
285 /* so can be NULL if target != KEY_SENDUP_ONE */
287 key_sendup_mbuf(so
, m
, target
)
299 panic("key_sendup_mbuf: NULL pointer was passed.\n");
300 if (so
== NULL
&& target
== KEY_SENDUP_ONE
)
301 panic("key_sendup_mbuf: NULL pointer was passed.\n");
303 pfkeystat
.in_total
++;
304 pfkeystat
.in_bytes
+= m
->m_pkthdr
.len
;
305 if (m
->m_len
< sizeof(struct sadb_msg
)) {
307 m
= m_pullup(m
, sizeof(struct sadb_msg
));
309 pfkeystat
.in_nomem
++;
313 /* don't bother pulling it up just for stats */
316 if (m
->m_len
>= sizeof(struct sadb_msg
)) {
317 struct sadb_msg
*msg
;
318 msg
= mtod(m
, struct sadb_msg
*);
319 pfkeystat
.in_msgtype
[msg
->sadb_msg_type
]++;
322 LIST_FOREACH(rp
, &rawcb_list
, list
)
324 if (rp
->rcb_proto
.sp_family
!= PF_KEY
)
326 if (rp
->rcb_proto
.sp_protocol
327 && rp
->rcb_proto
.sp_protocol
!= PF_KEY_V2
) {
331 kp
= (struct keycb
*)rp
;
334 * If you are in promiscuous mode, and when you get broadcasted
335 * reply, you'll get two PF_KEY messages.
336 * (based on pf_key@inner.net message on 14 Oct 1998)
338 if (((struct keycb
*)rp
)->kp_promisc
) {
339 if ((n
= m_copy(m
, 0, (int)M_COPYALL
)) != NULL
) {
340 (void)key_sendup0(rp
, n
, 1);
345 /* the exact target will be processed later */
346 if (so
&& sotorawcb(so
) == rp
)
352 /* the statement has no effect */
353 if (so
&& sotorawcb(so
) == rp
)
359 case KEY_SENDUP_REGISTERED
:
360 if (kp
->kp_registered
)
364 pfkeystat
.in_msgtarget
[target
]++;
369 if ((n
= m_copy(m
, 0, (int)M_COPYALL
)) == NULL
) {
371 printf("key_sendup: m_copy fail\n");
374 pfkeystat
.in_nomem
++;
378 if ((error
= key_sendup0(rp
, n
, 0)) != 0) {
387 error
= key_sendup0(sotorawcb(so
), m
, 0);
398 * derived from net/rtsock.c:rts_abort()
401 key_abort(struct socket
*so
)
405 error
= raw_usrreqs
.pru_abort(so
);
412 * derived from net/rtsock.c:rts_attach()
415 key_attach(struct socket
*so
, int proto
, struct proc
*p
)
420 if (sotorawcb(so
) != 0)
421 return EISCONN
; /* XXX panic? */
422 kp
= (struct keycb
*)_MALLOC(sizeof *kp
, M_PCB
, M_WAITOK
); /* XXX */
425 bzero(kp
, sizeof *kp
);
428 * The splnet() is necessary to block protocols from sending
429 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
430 * this PCB is extant but incompletely initialized.
431 * Probably we should try to do more of this work beforehand and
435 so
->so_pcb
= (caddr_t
)kp
;
436 error
= raw_usrreqs
.pru_attach(so
, proto
, p
);
437 kp
= (struct keycb
*)sotorawcb(so
);
440 so
->so_pcb
= (caddr_t
) 0;
442 printf("key_usrreq: key_usrreq results %d\n", error
);
446 kp
->kp_promisc
= kp
->kp_registered
= 0;
448 if (kp
->kp_raw
.rcb_proto
.sp_protocol
== PF_KEY
) /* XXX: AF_KEY */
451 kp
->kp_raw
.rcb_laddr
= &key_src
;
452 kp
->kp_raw
.rcb_faddr
= &key_dst
;
454 so
->so_options
|= SO_USELOOPBACK
;
462 * derived from net/rtsock.c:rts_bind()
465 key_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
469 error
= raw_usrreqs
.pru_bind(so
, nam
, p
); /* xxx just EINVAL */
476 * derived from net/rtsock.c:rts_connect()
479 key_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
483 error
= raw_usrreqs
.pru_connect(so
, nam
, p
); /* XXX just EINVAL */
490 * derived from net/rtsock.c:rts_detach()
493 key_detach(struct socket
*so
)
495 struct keycb
*kp
= (struct keycb
*)sotorawcb(so
);
500 if (kp
->kp_raw
.rcb_proto
.sp_protocol
501 == PF_KEY
) /* XXX: AF_KEY */
507 error
= raw_usrreqs
.pru_detach(so
);
514 * derived from net/rtsock.c:key_disconnect()
517 key_disconnect(struct socket
*so
)
521 error
= raw_usrreqs
.pru_disconnect(so
);
528 * derived from net/rtsock.c:rts_peeraddr()
531 key_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
535 error
= raw_usrreqs
.pru_peeraddr(so
, nam
);
542 * derived from net/rtsock.c:rts_send()
545 key_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
546 struct mbuf
*control
, struct proc
*p
)
550 error
= raw_usrreqs
.pru_send(so
, flags
, m
, nam
, control
, p
);
557 * derived from net/rtsock.c:rts_shutdown()
560 key_shutdown(struct socket
*so
)
564 error
= raw_usrreqs
.pru_shutdown(so
);
571 * derived from net/rtsock.c:rts_sockaddr()
574 key_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
578 error
= raw_usrreqs
.pru_sockaddr(so
, nam
);
583 struct pr_usrreqs key_usrreqs
= {
584 key_abort
, pru_accept_notsupp
, key_attach
, key_bind
,
586 pru_connect2_notsupp
, pru_control_notsupp
, key_detach
,
587 key_disconnect
, pru_listen_notsupp
, key_peeraddr
,
589 pru_rcvoob_notsupp
, key_send
, pru_sense_null
, key_shutdown
,
590 key_sockaddr
, sosend
, soreceive
, sopoll
594 SYSCTL_NODE(_net
, PF_KEY
, key
, CTLFLAG_RW
, 0, "Key Family");
597 * Definitions of protocols supported in the KEY domain.
600 extern struct domain keydomain
;
602 struct protosw keysw
[] = {
603 { SOCK_RAW
, &keydomain
, PF_KEY_V2
, PR_ATOMIC
|PR_ADDR
,
604 0, key_output
, raw_ctlinput
, 0,
611 struct domain keydomain
=
612 { PF_KEY
, "key", key_init
, 0, 0,
615 sizeof(struct key_cb
), 0