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.3 2003/08/07 21:17:37 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>
52 #include <net/raw_cb.h>
53 #include <net/route.h>
55 #include <net/pfkeyv2.h>
58 #include "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 __P((struct rawcb *, struct mbuf *, int));
75 struct pfkeystat pfkeystat;
82 key_output(struct mbuf *m, ...)
84 key_output(m, va_alist)
96 so = va_arg(ap, struct socket *);
100 panic("key_output: NULL pointer was passed.\n");
102 pfkeystat.out_total++;
103 pfkeystat.out_bytes += m->m_pkthdr.len;
105 len = m->m_pkthdr.len;
106 if (len < sizeof(struct sadb_msg)) {
107 pfkeystat.out_tooshort++;
112 if (m->m_len < sizeof(struct sadb_msg)) {
113 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
114 pfkeystat.out_nomem++;
120 if ((m->m_flags & M_PKTHDR) == 0)
121 panic("key_output: not M_PKTHDR ??");
123 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
125 msg = mtod(m, struct sadb_msg *);
126 pfkeystat.out_msgtype[msg->sadb_msg_type]++;
127 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
128 pfkeystat.out_invlen++;
135 error = key_parse(m, so);
145 * send message to the socket.
148 key_sendup0(rp, m, promisc)
156 struct sadb_msg *pmsg;
158 M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT);
159 if (m && m->m_len < sizeof(struct sadb_msg))
160 m = m_pullup(m, sizeof(struct sadb_msg));
162 pfkeystat.in_nomem++;
166 m->m_pkthdr.len += sizeof(*pmsg);
168 pmsg = mtod(m, struct sadb_msg *);
169 bzero(pmsg, sizeof(*pmsg));
170 pmsg->sadb_msg_version = PF_KEY_V2;
171 pmsg->sadb_msg_type = SADB_X_PROMISC;
172 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
175 pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
178 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
180 pfkeystat.in_nomem++;
185 sorwakeup(rp->rcb_socket);
189 /* XXX this interface should be obsoleted. */
191 key_sendup(so, msg, len, target)
193 struct sadb_msg *msg;
195 int target; /*target of the resulting message*/
197 struct mbuf *m, *n, *mprev;
201 if (so == 0 || msg == 0)
202 panic("key_sendup: NULL pointer was passed.\n");
204 KEYDEBUG(KEYDEBUG_KEY_DUMP,
205 printf("key_sendup: \n");
209 * we increment statistics here, just in case we have ENOBUFS
212 pfkeystat.in_total++;
213 pfkeystat.in_bytes += len;
214 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
217 * Get mbuf chain whenever possible (not clusters),
218 * to save socket buffer. We'll be generating many SADB_ACQUIRE
219 * messages to listening key sockets. If we simply allocate clusters,
220 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
221 * sbspace() computes # of actual data bytes AND mbuf region.
223 * TODO: SADB_ACQUIRE filters should be implemented.
229 MGETHDR(n, M_DONTWAIT, MT_DATA);
232 MGET(n, M_DONTWAIT, MT_DATA);
236 pfkeystat.in_nomem++;
239 if (tlen >= MCLBYTES) { /*XXX better threshold? */
240 MCLGET(n, M_DONTWAIT);
241 if ((n->m_flags & M_EXT) == 0) {
244 pfkeystat.in_nomem++;
262 m->m_pkthdr.len = len;
263 m->m_pkthdr.rcvif = NULL;
264 m_copyback(m, 0, len, (caddr_t)msg);
266 /* avoid duplicated statistics */
267 pfkeystat.in_total--;
268 pfkeystat.in_bytes -= len;
269 pfkeystat.in_msgtype[msg->sadb_msg_type]--;
271 return key_sendup_mbuf(so, m, target);
274 /* so can be NULL if target != KEY_SENDUP_ONE */
276 key_sendup_mbuf(so, m, target)
288 panic("key_sendup_mbuf: NULL pointer was passed.\n");
289 if (so == NULL && target == KEY_SENDUP_ONE)
290 panic("key_sendup_mbuf: NULL pointer was passed.\n");
292 pfkeystat.in_total++;
293 pfkeystat.in_bytes += m->m_pkthdr.len;
294 if (m->m_len < sizeof(struct sadb_msg)) {
296 m = m_pullup(m, sizeof(struct sadb_msg));
298 pfkeystat.in_nomem++;
302 /* don't bother pulling it up just for stats */
305 if (m->m_len >= sizeof(struct sadb_msg)) {
306 struct sadb_msg *msg;
307 msg = mtod(m, struct sadb_msg *);
308 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
311 LIST_FOREACH(rp, &rawcb_list, list)
313 if (rp->rcb_proto.sp_family != PF_KEY)
315 if (rp->rcb_proto.sp_protocol
316 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
320 kp = (struct keycb *)rp;
323 * If you are in promiscuous mode, and when you get broadcasted
324 * reply, you'll get two PF_KEY messages.
325 * (based on pf_key@inner.net message on 14 Oct 1998)
327 if (((struct keycb *)rp)->kp_promisc) {
328 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
329 (void)key_sendup0(rp, n, 1);
334 /* the exact target will be processed later */
335 if (so && sotorawcb(so) == rp)
341 /* the statement has no effect */
342 if (so && sotorawcb(so) == rp)
348 case KEY_SENDUP_REGISTERED:
349 if (kp->kp_registered)
353 pfkeystat.in_msgtarget[target]++;
358 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
360 pfkeystat.in_nomem++;
364 if ((error = key_sendup0(rp, n, 0)) != 0) {
373 error = key_sendup0(sotorawcb(so), m, 0);
384 * derived from net/rtsock.c:rts_abort()
387 key_abort(struct socket *so)
391 error = raw_usrreqs.pru_abort(so);
398 * derived from net/rtsock.c:rts_attach()
401 key_attach(struct socket *so, int proto, struct proc *td)
406 if (sotorawcb(so) != 0)
407 return EISCONN; /* XXX panic? */
408 kp = (struct keycb *)malloc(sizeof *kp, M_PCB, M_WAITOK|M_ZERO); /* XXX */
413 * The splnet() is necessary to block protocols from sending
414 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
415 * this PCB is extant but incompletely initialized.
416 * Probably we should try to do more of this work beforehand and
420 so->so_pcb = (caddr_t)kp;
421 error = raw_usrreqs.pru_attach(so, proto, td);
422 kp = (struct keycb *)sotorawcb(so);
425 so->so_pcb = (caddr_t) 0;
430 kp->kp_promisc = kp->kp_registered = 0;
432 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
435 kp->kp_raw.rcb_laddr = &key_src;
436 kp->kp_raw.rcb_faddr = &key_dst;
438 so->so_options |= SO_USELOOPBACK;
446 * derived from net/rtsock.c:rts_bind()
449 key_bind(struct socket *so, struct sockaddr *nam, struct proc *td)
453 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
460 * derived from net/rtsock.c:rts_connect()
463 key_connect(struct socket *so, struct sockaddr *nam, struct proc *td)
467 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
474 * derived from net/rtsock.c:rts_detach()
477 key_detach(struct socket *so)
479 struct keycb *kp = (struct keycb *)sotorawcb(so);
484 if (kp->kp_raw.rcb_proto.sp_protocol
485 == PF_KEY) /* XXX: AF_KEY */
491 error = raw_usrreqs.pru_detach(so);
498 * derived from net/rtsock.c:key_disconnect()
501 key_disconnect(struct socket *so)
505 error = raw_usrreqs.pru_disconnect(so);
512 * derived from net/rtsock.c:rts_peeraddr()
515 key_peeraddr(struct socket *so, struct sockaddr **nam)
519 error = raw_usrreqs.pru_peeraddr(so, nam);
526 * derived from net/rtsock.c:rts_send()
529 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
530 struct mbuf *control, struct proc *td)
534 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
541 * derived from net/rtsock.c:rts_shutdown()
544 key_shutdown(struct socket *so)
548 error = raw_usrreqs.pru_shutdown(so);
555 * derived from net/rtsock.c:rts_sockaddr()
558 key_sockaddr(struct socket *so, struct sockaddr **nam)
562 error = raw_usrreqs.pru_sockaddr(so, nam);
567 struct pr_usrreqs key_usrreqs = {
568 key_abort, pru_accept_notsupp, key_attach, key_bind,
570 pru_connect2_notsupp, pru_control_notsupp, key_detach,
571 key_disconnect, pru_listen_notsupp, key_peeraddr,
573 pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
574 key_sockaddr, sosend, soreceive, sopoll
578 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
581 * Definitions of protocols supported in the KEY domain.
584 extern struct domain keydomain;
586 struct protosw keysw[] = {
587 { SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
588 0, (pr_output_t *)key_output, raw_ctlinput, 0,
598 bzero((caddr_t)&key_cb, sizeof(key_cb));
602 struct domain keydomain =
603 { PF_KEY, "key", key_init0, 0, 0,
604 keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])] };