1 /* $FreeBSD: src/sys/netipsec/keysock.c,v 1.3.2.1 2003/01/24 05:11:36 sam Exp $ */
2 /* $KAME: keysock.c,v 1.25 2001/08/13 20:07:41 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include "opt_ipsec.h"
35 /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/domain.h>
40 #include <sys/errno.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
44 #include <sys/protosw.h>
45 #include <sys/signalvar.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
51 #include <net/raw_cb.h>
52 #include <net/route.h>
54 #include <net/pfkeyv2.h>
55 #include <netipsec/key.h>
56 #include <netipsec/keysock.h>
57 #include <netipsec/key_debug.h>
59 #include <machine/stdarg.h>
61 typedef int pr_output_t (struct mbuf *, struct socket *);
67 static struct key_cb key_cb;
69 static struct sockaddr key_dst = { 2, PF_KEY, };
70 static struct sockaddr key_src = { 2, PF_KEY, };
72 static int key_sendup0 __P((struct rawcb *, struct mbuf *, int));
74 struct pfkeystat pfkeystat;
81 key_output(struct mbuf *m, ...)
83 key_output(m, va_alist)
95 so = va_arg(ap, struct socket *);
99 panic("key_output: NULL pointer was passed.\n");
101 pfkeystat.out_total++;
102 pfkeystat.out_bytes += m->m_pkthdr.len;
104 len = m->m_pkthdr.len;
105 if (len < sizeof(struct sadb_msg)) {
106 pfkeystat.out_tooshort++;
111 if (m->m_len < sizeof(struct sadb_msg)) {
112 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
113 pfkeystat.out_nomem++;
119 if ((m->m_flags & M_PKTHDR) == 0)
120 panic("key_output: not M_PKTHDR ??");
122 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
124 msg = mtod(m, struct sadb_msg *);
125 pfkeystat.out_msgtype[msg->sadb_msg_type]++;
126 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
127 pfkeystat.out_invlen++;
134 error = key_parse(m, so);
144 * send message to the socket.
147 key_sendup0(rp, m, promisc)
155 struct sadb_msg *pmsg;
157 M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT);
158 if (m && m->m_len < sizeof(struct sadb_msg))
159 m = m_pullup(m, sizeof(struct sadb_msg));
161 pfkeystat.in_nomem++;
165 m->m_pkthdr.len += sizeof(*pmsg);
167 pmsg = mtod(m, struct sadb_msg *);
168 bzero(pmsg, sizeof(*pmsg));
169 pmsg->sadb_msg_version = PF_KEY_V2;
170 pmsg->sadb_msg_type = SADB_X_PROMISC;
171 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
174 pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
177 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
179 pfkeystat.in_nomem++;
184 sorwakeup(rp->rcb_socket);
188 /* XXX this interface should be obsoleted. */
190 key_sendup(so, msg, len, target)
192 struct sadb_msg *msg;
194 int target; /*target of the resulting message*/
196 struct mbuf *m, *n, *mprev;
200 if (so == 0 || msg == 0)
201 panic("key_sendup: NULL pointer was passed.\n");
203 KEYDEBUG(KEYDEBUG_KEY_DUMP,
204 printf("key_sendup: \n");
208 * we increment statistics here, just in case we have ENOBUFS
211 pfkeystat.in_total++;
212 pfkeystat.in_bytes += len;
213 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
216 * Get mbuf chain whenever possible (not clusters),
217 * to save socket buffer. We'll be generating many SADB_ACQUIRE
218 * messages to listening key sockets. If we simply allocate clusters,
219 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
220 * sbspace() computes # of actual data bytes AND mbuf region.
222 * TODO: SADB_ACQUIRE filters should be implemented.
228 MGETHDR(n, M_DONTWAIT, MT_DATA);
231 MGET(n, M_DONTWAIT, MT_DATA);
235 pfkeystat.in_nomem++;
238 if (tlen >= MCLBYTES) { /*XXX better threshold? */
239 MCLGET(n, M_DONTWAIT);
240 if ((n->m_flags & M_EXT) == 0) {
243 pfkeystat.in_nomem++;
261 m->m_pkthdr.len = len;
262 m->m_pkthdr.rcvif = NULL;
263 m_copyback(m, 0, len, (caddr_t)msg);
265 /* avoid duplicated statistics */
266 pfkeystat.in_total--;
267 pfkeystat.in_bytes -= len;
268 pfkeystat.in_msgtype[msg->sadb_msg_type]--;
270 return key_sendup_mbuf(so, m, target);
273 /* so can be NULL if target != KEY_SENDUP_ONE */
275 key_sendup_mbuf(so, m, target)
287 panic("key_sendup_mbuf: NULL pointer was passed.\n");
288 if (so == NULL && target == KEY_SENDUP_ONE)
289 panic("key_sendup_mbuf: NULL pointer was passed.\n");
291 pfkeystat.in_total++;
292 pfkeystat.in_bytes += m->m_pkthdr.len;
293 if (m->m_len < sizeof(struct sadb_msg)) {
295 m = m_pullup(m, sizeof(struct sadb_msg));
297 pfkeystat.in_nomem++;
301 /* don't bother pulling it up just for stats */
304 if (m->m_len >= sizeof(struct sadb_msg)) {
305 struct sadb_msg *msg;
306 msg = mtod(m, struct sadb_msg *);
307 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
310 LIST_FOREACH(rp, &rawcb_list, list)
312 if (rp->rcb_proto.sp_family != PF_KEY)
314 if (rp->rcb_proto.sp_protocol
315 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
319 kp = (struct keycb *)rp;
322 * If you are in promiscuous mode, and when you get broadcasted
323 * reply, you'll get two PF_KEY messages.
324 * (based on pf_key@inner.net message on 14 Oct 1998)
326 if (((struct keycb *)rp)->kp_promisc) {
327 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
328 (void)key_sendup0(rp, n, 1);
333 /* the exact target will be processed later */
334 if (so && sotorawcb(so) == rp)
340 /* the statement has no effect */
341 if (so && sotorawcb(so) == rp)
347 case KEY_SENDUP_REGISTERED:
348 if (kp->kp_registered)
352 pfkeystat.in_msgtarget[target]++;
357 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
359 pfkeystat.in_nomem++;
363 if ((error = key_sendup0(rp, n, 0)) != 0) {
372 error = key_sendup0(sotorawcb(so), m, 0);
383 * derived from net/rtsock.c:rts_abort()
386 key_abort(struct socket *so)
390 error = raw_usrreqs.pru_abort(so);
397 * derived from net/rtsock.c:rts_attach()
400 key_attach(struct socket *so, int proto, struct proc *td)
405 if (sotorawcb(so) != 0)
406 return EISCONN; /* XXX panic? */
407 kp = (struct keycb *)malloc(sizeof *kp, M_PCB, M_WAITOK|M_ZERO); /* XXX */
412 * The splnet() is necessary to block protocols from sending
413 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
414 * this PCB is extant but incompletely initialized.
415 * Probably we should try to do more of this work beforehand and
419 so->so_pcb = (caddr_t)kp;
420 error = raw_usrreqs.pru_attach(so, proto, td);
421 kp = (struct keycb *)sotorawcb(so);
424 so->so_pcb = (caddr_t) 0;
429 kp->kp_promisc = kp->kp_registered = 0;
431 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
434 kp->kp_raw.rcb_laddr = &key_src;
435 kp->kp_raw.rcb_faddr = &key_dst;
437 so->so_options |= SO_USELOOPBACK;
445 * derived from net/rtsock.c:rts_bind()
448 key_bind(struct socket *so, struct sockaddr *nam, struct proc *td)
452 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
459 * derived from net/rtsock.c:rts_connect()
462 key_connect(struct socket *so, struct sockaddr *nam, struct proc *td)
466 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
473 * derived from net/rtsock.c:rts_detach()
476 key_detach(struct socket *so)
478 struct keycb *kp = (struct keycb *)sotorawcb(so);
483 if (kp->kp_raw.rcb_proto.sp_protocol
484 == PF_KEY) /* XXX: AF_KEY */
490 error = raw_usrreqs.pru_detach(so);
497 * derived from net/rtsock.c:key_disconnect()
500 key_disconnect(struct socket *so)
504 error = raw_usrreqs.pru_disconnect(so);
511 * derived from net/rtsock.c:rts_peeraddr()
514 key_peeraddr(struct socket *so, struct sockaddr **nam)
518 error = raw_usrreqs.pru_peeraddr(so, nam);
525 * derived from net/rtsock.c:rts_send()
528 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
529 struct mbuf *control, struct proc *td)
533 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
540 * derived from net/rtsock.c:rts_shutdown()
543 key_shutdown(struct socket *so)
547 error = raw_usrreqs.pru_shutdown(so);
554 * derived from net/rtsock.c:rts_sockaddr()
557 key_sockaddr(struct socket *so, struct sockaddr **nam)
561 error = raw_usrreqs.pru_sockaddr(so, nam);
566 struct pr_usrreqs key_usrreqs = {
567 key_abort, pru_accept_notsupp, key_attach, key_bind,
569 pru_connect2_notsupp, pru_control_notsupp, key_detach,
570 key_disconnect, pru_listen_notsupp, key_peeraddr,
572 pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
573 key_sockaddr, sosend, soreceive, sopoll
577 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
580 * Definitions of protocols supported in the KEY domain.
583 extern struct domain keydomain;
585 struct protosw keysw[] = {
586 { SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
587 0, (pr_output_t *)key_output, raw_ctlinput, 0,
597 bzero((caddr_t)&key_cb, sizeof(key_cb));
601 struct domain keydomain =
602 { PF_KEY, "key", key_init0, 0, 0,
603 keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])] };