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.10 2005/06/03 00:22:27 hmp 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>
52 #include <net/raw_cb.h>
53 #include <net/route.h>
55 #include <net/pfkeyv2.h>
56 #include <netproto/ipsec/key.h>
57 #include <netproto/ipsec/keysock.h>
58 #include <netproto/ipsec/key_debug.h>
60 #include <machine/stdarg.h>
62 typedef int pr_output_t (struct mbuf *, struct socket *);
68 static struct key_cb key_cb;
70 static struct sockaddr key_dst = { 2, PF_KEY, };
71 static struct sockaddr key_src = { 2, PF_KEY, };
73 static int key_sendup0 (struct rawcb *, struct mbuf *, int);
75 struct pfkeystat pfkeystat;
81 key_output(struct mbuf *m, struct socket *so, ...)
88 panic("key_output: NULL pointer was passed.\n");
90 pfkeystat.out_total++;
91 pfkeystat.out_bytes += m->m_pkthdr.len;
93 len = m->m_pkthdr.len;
94 if (len < sizeof(struct sadb_msg)) {
95 pfkeystat.out_tooshort++;
100 if (m->m_len < sizeof(struct sadb_msg)) {
101 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
102 pfkeystat.out_nomem++;
108 if ((m->m_flags & M_PKTHDR) == 0)
109 panic("key_output: not M_PKTHDR ??");
111 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
113 msg = mtod(m, struct sadb_msg *);
114 pfkeystat.out_msgtype[msg->sadb_msg_type]++;
115 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
116 pfkeystat.out_invlen++;
123 error = key_parse(m, so);
133 * send message to the socket.
136 key_sendup0(rp, m, promisc)
144 struct sadb_msg *pmsg;
146 M_PREPEND(m, sizeof(struct sadb_msg), MB_DONTWAIT);
147 if (m && m->m_len < sizeof(struct sadb_msg))
148 m = m_pullup(m, sizeof(struct sadb_msg));
150 pfkeystat.in_nomem++;
154 m->m_pkthdr.len += sizeof(*pmsg);
156 pmsg = mtod(m, struct sadb_msg *);
157 bzero(pmsg, sizeof(*pmsg));
158 pmsg->sadb_msg_version = PF_KEY_V2;
159 pmsg->sadb_msg_type = SADB_X_PROMISC;
160 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
163 pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
166 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
168 pfkeystat.in_nomem++;
173 sorwakeup(rp->rcb_socket);
177 /* XXX this interface should be obsoleted. */
179 key_sendup(so, msg, len, target)
181 struct sadb_msg *msg;
183 int target; /*target of the resulting message*/
185 struct mbuf *m, *n, *mprev;
189 if (so == 0 || msg == 0)
190 panic("key_sendup: NULL pointer was passed.\n");
192 KEYDEBUG(KEYDEBUG_KEY_DUMP,
193 printf("key_sendup: \n");
197 * we increment statistics here, just in case we have ENOBUFS
200 pfkeystat.in_total++;
201 pfkeystat.in_bytes += len;
202 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
205 * Get mbuf chain whenever possible (not clusters),
206 * to save socket buffer. We'll be generating many SADB_ACQUIRE
207 * messages to listening key sockets. If we simply allocate clusters,
208 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
209 * sbspace() computes # of actual data bytes AND mbuf region.
211 * TODO: SADB_ACQUIRE filters should be implemented.
217 MGETHDR(n, MB_DONTWAIT, MT_DATA);
220 MGET(n, MB_DONTWAIT, MT_DATA);
224 pfkeystat.in_nomem++;
227 if (tlen >= MCLBYTES) { /*XXX better threshold? */
228 MCLGET(n, MB_DONTWAIT);
229 if ((n->m_flags & M_EXT) == 0) {
232 pfkeystat.in_nomem++;
250 m->m_pkthdr.len = len;
251 m->m_pkthdr.rcvif = NULL;
252 m_copyback(m, 0, len, (caddr_t)msg);
254 /* avoid duplicated statistics */
255 pfkeystat.in_total--;
256 pfkeystat.in_bytes -= len;
257 pfkeystat.in_msgtype[msg->sadb_msg_type]--;
259 return key_sendup_mbuf(so, m, target);
262 /* so can be NULL if target != KEY_SENDUP_ONE */
264 key_sendup_mbuf(so, m, target)
276 panic("key_sendup_mbuf: NULL pointer was passed.\n");
277 if (so == NULL && target == KEY_SENDUP_ONE)
278 panic("key_sendup_mbuf: NULL pointer was passed.\n");
280 pfkeystat.in_total++;
281 pfkeystat.in_bytes += m->m_pkthdr.len;
282 if (m->m_len < sizeof(struct sadb_msg)) {
284 m = m_pullup(m, sizeof(struct sadb_msg));
286 pfkeystat.in_nomem++;
290 /* don't bother pulling it up just for stats */
293 if (m->m_len >= sizeof(struct sadb_msg)) {
294 struct sadb_msg *msg;
295 msg = mtod(m, struct sadb_msg *);
296 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
299 LIST_FOREACH(rp, &rawcb_list, list)
301 if (rp->rcb_proto.sp_family != PF_KEY)
303 if (rp->rcb_proto.sp_protocol
304 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
308 kp = (struct keycb *)rp;
311 * If you are in promiscuous mode, and when you get broadcasted
312 * reply, you'll get two PF_KEY messages.
313 * (based on pf_key@inner.net message on 14 Oct 1998)
315 if (((struct keycb *)rp)->kp_promisc) {
316 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
317 (void)key_sendup0(rp, n, 1);
322 /* the exact target will be processed later */
323 if (so && sotorawcb(so) == rp)
329 /* the statement has no effect */
330 if (so && sotorawcb(so) == rp)
336 case KEY_SENDUP_REGISTERED:
337 if (kp->kp_registered)
341 pfkeystat.in_msgtarget[target]++;
346 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
348 pfkeystat.in_nomem++;
352 if ((error = key_sendup0(rp, n, 0)) != 0) {
361 error = key_sendup0(sotorawcb(so), m, 0);
372 * derived from net/rtsock.c:rts_abort()
375 key_abort(struct socket *so)
379 error = raw_usrreqs.pru_abort(so);
386 * derived from net/rtsock.c:rts_attach()
389 key_attach(struct socket *so, int proto, struct pru_attach_info *ai)
394 if (sotorawcb(so) != 0)
395 return EISCONN; /* XXX panic? */
396 kp = (struct keycb *)malloc(sizeof *kp, M_PCB, M_WAITOK|M_ZERO); /* XXX */
401 * The critical section is necessary to block protocols from sending
402 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
403 * this PCB is extant but incompletely initialized.
404 * Probably we should try to do more of this work beforehand and
408 so->so_pcb = (caddr_t)kp;
409 error = raw_usrreqs.pru_attach(so, proto, ai);
410 kp = (struct keycb *)sotorawcb(so);
413 so->so_pcb = (caddr_t) 0;
418 kp->kp_promisc = kp->kp_registered = 0;
420 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
423 kp->kp_raw.rcb_laddr = &key_src;
424 kp->kp_raw.rcb_faddr = &key_dst;
426 so->so_options |= SO_USELOOPBACK;
434 * derived from net/rtsock.c:rts_bind()
437 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
441 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
448 * derived from net/rtsock.c:rts_connect()
451 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
455 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
462 * derived from net/rtsock.c:rts_detach()
465 key_detach(struct socket *so)
467 struct keycb *kp = (struct keycb *)sotorawcb(so);
472 if (kp->kp_raw.rcb_proto.sp_protocol
473 == PF_KEY) /* XXX: AF_KEY */
479 error = raw_usrreqs.pru_detach(so);
486 * derived from net/rtsock.c:key_disconnect()
489 key_disconnect(struct socket *so)
493 error = raw_usrreqs.pru_disconnect(so);
500 * derived from net/rtsock.c:rts_peeraddr()
503 key_peeraddr(struct socket *so, struct sockaddr **nam)
507 error = raw_usrreqs.pru_peeraddr(so, nam);
514 * derived from net/rtsock.c:rts_send()
517 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
518 struct mbuf *control, struct thread *td)
522 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
529 * derived from net/rtsock.c:rts_shutdown()
532 key_shutdown(struct socket *so)
536 error = raw_usrreqs.pru_shutdown(so);
543 * derived from net/rtsock.c:rts_sockaddr()
546 key_sockaddr(struct socket *so, struct sockaddr **nam)
550 error = raw_usrreqs.pru_sockaddr(so, nam);
555 struct pr_usrreqs key_usrreqs = {
556 key_abort, pru_accept_notsupp, key_attach, key_bind,
558 pru_connect2_notsupp, pru_control_notsupp, key_detach,
559 key_disconnect, pru_listen_notsupp, key_peeraddr,
561 pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
562 key_sockaddr, sosend, soreceive, sopoll
566 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
569 * Definitions of protocols supported in the KEY domain.
572 extern struct domain keydomain;
574 struct protosw keysw[] = {
575 { SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
576 0, key_output, raw_ctlinput, 0,
586 bzero((caddr_t)&key_cb, sizeof(key_cb));
590 struct domain keydomain = {
591 PF_KEY, "key", key_init0, NULL, NULL,
592 keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])],