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.13 2006/09/05 00:55:49 dillon 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 (!sbappendaddr(&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 printf("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 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
208 * sbspace() computes # of actual data bytes AND mbuf region.
210 * TODO: SADB_ACQUIRE filters should be implemented.
216 MGETHDR(n, MB_DONTWAIT, MT_DATA);
219 MGET(n, MB_DONTWAIT, MT_DATA);
223 pfkeystat.in_nomem++;
226 if (tlen >= MCLBYTES) { /*XXX better threshold? */
227 MCLGET(n, MB_DONTWAIT);
228 if ((n->m_flags & M_EXT) == 0) {
231 pfkeystat.in_nomem++;
249 m->m_pkthdr.len = len;
250 m->m_pkthdr.rcvif = NULL;
251 m_copyback(m, 0, len, (caddr_t)msg);
253 /* avoid duplicated statistics */
254 pfkeystat.in_total--;
255 pfkeystat.in_bytes -= len;
256 pfkeystat.in_msgtype[msg->sadb_msg_type]--;
258 return key_sendup_mbuf(so, m, target);
261 /* so can be NULL if target != KEY_SENDUP_ONE */
263 key_sendup_mbuf(struct socket *so, struct mbuf *m, int target)
272 panic("key_sendup_mbuf: NULL pointer was passed.\n");
273 if (so == NULL && target == KEY_SENDUP_ONE)
274 panic("key_sendup_mbuf: NULL pointer was passed.\n");
276 pfkeystat.in_total++;
277 pfkeystat.in_bytes += m->m_pkthdr.len;
278 if (m->m_len < sizeof(struct sadb_msg)) {
280 m = m_pullup(m, sizeof(struct sadb_msg));
282 pfkeystat.in_nomem++;
286 /* don't bother pulling it up just for stats */
289 if (m->m_len >= sizeof(struct sadb_msg)) {
290 struct sadb_msg *msg;
291 msg = mtod(m, struct sadb_msg *);
292 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
295 LIST_FOREACH(rp, &rawcb_list, list)
297 if (rp->rcb_proto.sp_family != PF_KEY)
299 if (rp->rcb_proto.sp_protocol
300 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
304 kp = (struct keycb *)rp;
307 * If you are in promiscuous mode, and when you get broadcasted
308 * reply, you'll get two PF_KEY messages.
309 * (based on pf_key@inner.net message on 14 Oct 1998)
311 if (((struct keycb *)rp)->kp_promisc) {
312 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
313 key_sendup0(rp, n, 1);
318 /* the exact target will be processed later */
319 if (so && sotorawcb(so) == rp)
325 /* the statement has no effect */
326 if (so && sotorawcb(so) == rp)
332 case KEY_SENDUP_REGISTERED:
333 if (kp->kp_registered)
337 pfkeystat.in_msgtarget[target]++;
342 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
344 pfkeystat.in_nomem++;
348 if ((error = key_sendup0(rp, n, 0)) != 0) {
357 error = key_sendup0(sotorawcb(so), m, 0);
368 * derived from net/rtsock.c:rts_abort()
371 key_abort(struct socket *so)
375 error = raw_usrreqs.pru_abort(so);
382 * derived from net/rtsock.c:rts_attach()
385 key_attach(struct socket *so, int proto, struct pru_attach_info *ai)
390 if (sotorawcb(so) != 0)
391 return EISCONN; /* XXX panic? */
392 kp = (struct keycb *)kmalloc(sizeof *kp, M_PCB, M_WAITOK|M_ZERO); /* XXX */
397 * The critical section is necessary to block protocols from sending
398 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
399 * this PCB is extant but incompletely initialized.
400 * Probably we should try to do more of this work beforehand and
404 so->so_pcb = (caddr_t)kp;
405 error = raw_usrreqs.pru_attach(so, proto, ai);
406 kp = (struct keycb *)sotorawcb(so);
409 so->so_pcb = (caddr_t) 0;
414 kp->kp_promisc = kp->kp_registered = 0;
416 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
419 kp->kp_raw.rcb_laddr = &key_src;
420 kp->kp_raw.rcb_faddr = &key_dst;
422 so->so_options |= SO_USELOOPBACK;
430 * derived from net/rtsock.c:rts_bind()
433 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
437 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
444 * derived from net/rtsock.c:rts_connect()
447 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
451 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
458 * derived from net/rtsock.c:rts_detach()
461 key_detach(struct socket *so)
463 struct keycb *kp = (struct keycb *)sotorawcb(so);
468 if (kp->kp_raw.rcb_proto.sp_protocol
469 == PF_KEY) /* XXX: AF_KEY */
475 error = raw_usrreqs.pru_detach(so);
482 * derived from net/rtsock.c:key_disconnect()
485 key_disconnect(struct socket *so)
489 error = raw_usrreqs.pru_disconnect(so);
496 * derived from net/rtsock.c:rts_peeraddr()
499 key_peeraddr(struct socket *so, struct sockaddr **nam)
503 error = raw_usrreqs.pru_peeraddr(so, nam);
510 * derived from net/rtsock.c:rts_send()
513 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
514 struct mbuf *control, struct thread *td)
518 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
525 * derived from net/rtsock.c:rts_shutdown()
528 key_shutdown(struct socket *so)
532 error = raw_usrreqs.pru_shutdown(so);
539 * derived from net/rtsock.c:rts_sockaddr()
542 key_sockaddr(struct socket *so, struct sockaddr **nam)
546 error = raw_usrreqs.pru_sockaddr(so, nam);
551 struct pr_usrreqs key_usrreqs = {
552 key_abort, pru_accept_notsupp, key_attach, key_bind,
554 pru_connect2_notsupp, pru_control_notsupp, key_detach,
555 key_disconnect, pru_listen_notsupp, key_peeraddr,
557 pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
558 key_sockaddr, sosend, soreceive, sopoll
562 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
565 * Definitions of protocols supported in the KEY domain.
568 extern struct domain keydomain;
570 struct protosw keysw[] = {
571 { SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
572 0, key_output, raw_ctlinput, 0,
582 bzero((caddr_t)&key_cb, sizeof(key_cb));
586 struct domain keydomain = {
587 PF_KEY, "key", key_init0, NULL, NULL,
588 keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])],