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.6 2004/03/06 01:58:56 hsu 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 (struct rawcb *, struct mbuf *, int);
75 struct pfkeystat pfkeystat;
81 key_output(struct mbuf *m, ...)
90 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)) {
101 pfkeystat.out_tooshort++;
106 if (m->m_len < sizeof(struct sadb_msg)) {
107 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
108 pfkeystat.out_nomem++;
114 if ((m->m_flags & M_PKTHDR) == 0)
115 panic("key_output: not M_PKTHDR ??");
117 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
119 msg = mtod(m, struct sadb_msg *);
120 pfkeystat.out_msgtype[msg->sadb_msg_type]++;
121 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
122 pfkeystat.out_invlen++;
129 error = key_parse(m, so);
139 * send message to the socket.
142 key_sendup0(rp, m, promisc)
150 struct sadb_msg *pmsg;
152 M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT);
153 if (m && m->m_len < sizeof(struct sadb_msg))
154 m = m_pullup(m, sizeof(struct sadb_msg));
156 pfkeystat.in_nomem++;
160 m->m_pkthdr.len += sizeof(*pmsg);
162 pmsg = mtod(m, struct sadb_msg *);
163 bzero(pmsg, sizeof(*pmsg));
164 pmsg->sadb_msg_version = PF_KEY_V2;
165 pmsg->sadb_msg_type = SADB_X_PROMISC;
166 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
169 pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
172 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
174 pfkeystat.in_nomem++;
179 sorwakeup(rp->rcb_socket);
183 /* XXX this interface should be obsoleted. */
185 key_sendup(so, msg, len, target)
187 struct sadb_msg *msg;
189 int target; /*target of the resulting message*/
191 struct mbuf *m, *n, *mprev;
195 if (so == 0 || msg == 0)
196 panic("key_sendup: NULL pointer was passed.\n");
198 KEYDEBUG(KEYDEBUG_KEY_DUMP,
199 printf("key_sendup: \n");
203 * we increment statistics here, just in case we have ENOBUFS
206 pfkeystat.in_total++;
207 pfkeystat.in_bytes += len;
208 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
211 * Get mbuf chain whenever possible (not clusters),
212 * to save socket buffer. We'll be generating many SADB_ACQUIRE
213 * messages to listening key sockets. If we simply allocate clusters,
214 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
215 * sbspace() computes # of actual data bytes AND mbuf region.
217 * TODO: SADB_ACQUIRE filters should be implemented.
223 MGETHDR(n, M_DONTWAIT, MT_DATA);
226 MGET(n, M_DONTWAIT, MT_DATA);
230 pfkeystat.in_nomem++;
233 if (tlen >= MCLBYTES) { /*XXX better threshold? */
234 MCLGET(n, M_DONTWAIT);
235 if ((n->m_flags & M_EXT) == 0) {
238 pfkeystat.in_nomem++;
256 m->m_pkthdr.len = len;
257 m->m_pkthdr.rcvif = NULL;
258 m_copyback(m, 0, len, (caddr_t)msg);
260 /* avoid duplicated statistics */
261 pfkeystat.in_total--;
262 pfkeystat.in_bytes -= len;
263 pfkeystat.in_msgtype[msg->sadb_msg_type]--;
265 return key_sendup_mbuf(so, m, target);
268 /* so can be NULL if target != KEY_SENDUP_ONE */
270 key_sendup_mbuf(so, m, target)
282 panic("key_sendup_mbuf: NULL pointer was passed.\n");
283 if (so == NULL && target == KEY_SENDUP_ONE)
284 panic("key_sendup_mbuf: NULL pointer was passed.\n");
286 pfkeystat.in_total++;
287 pfkeystat.in_bytes += m->m_pkthdr.len;
288 if (m->m_len < sizeof(struct sadb_msg)) {
290 m = m_pullup(m, sizeof(struct sadb_msg));
292 pfkeystat.in_nomem++;
296 /* don't bother pulling it up just for stats */
299 if (m->m_len >= sizeof(struct sadb_msg)) {
300 struct sadb_msg *msg;
301 msg = mtod(m, struct sadb_msg *);
302 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
305 LIST_FOREACH(rp, &rawcb_list, list)
307 if (rp->rcb_proto.sp_family != PF_KEY)
309 if (rp->rcb_proto.sp_protocol
310 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
314 kp = (struct keycb *)rp;
317 * If you are in promiscuous mode, and when you get broadcasted
318 * reply, you'll get two PF_KEY messages.
319 * (based on pf_key@inner.net message on 14 Oct 1998)
321 if (((struct keycb *)rp)->kp_promisc) {
322 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
323 (void)key_sendup0(rp, n, 1);
328 /* the exact target will be processed later */
329 if (so && sotorawcb(so) == rp)
335 /* the statement has no effect */
336 if (so && sotorawcb(so) == rp)
342 case KEY_SENDUP_REGISTERED:
343 if (kp->kp_registered)
347 pfkeystat.in_msgtarget[target]++;
352 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
354 pfkeystat.in_nomem++;
358 if ((error = key_sendup0(rp, n, 0)) != 0) {
367 error = key_sendup0(sotorawcb(so), m, 0);
378 * derived from net/rtsock.c:rts_abort()
381 key_abort(struct socket *so)
385 error = raw_usrreqs.pru_abort(so);
392 * derived from net/rtsock.c:rts_attach()
395 key_attach(struct socket *so, int proto, struct proc *td)
400 if (sotorawcb(so) != 0)
401 return EISCONN; /* XXX panic? */
402 kp = (struct keycb *)malloc(sizeof *kp, M_PCB, M_WAITOK|M_ZERO); /* XXX */
407 * The splnet() is necessary to block protocols from sending
408 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
409 * this PCB is extant but incompletely initialized.
410 * Probably we should try to do more of this work beforehand and
414 so->so_pcb = (caddr_t)kp;
415 error = raw_usrreqs.pru_attach(so, proto, td);
416 kp = (struct keycb *)sotorawcb(so);
419 so->so_pcb = (caddr_t) 0;
424 kp->kp_promisc = kp->kp_registered = 0;
426 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
429 kp->kp_raw.rcb_laddr = &key_src;
430 kp->kp_raw.rcb_faddr = &key_dst;
432 so->so_options |= SO_USELOOPBACK;
440 * derived from net/rtsock.c:rts_bind()
443 key_bind(struct socket *so, struct sockaddr *nam, struct proc *td)
447 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
454 * derived from net/rtsock.c:rts_connect()
457 key_connect(struct socket *so, struct sockaddr *nam, struct proc *td)
461 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
468 * derived from net/rtsock.c:rts_detach()
471 key_detach(struct socket *so)
473 struct keycb *kp = (struct keycb *)sotorawcb(so);
478 if (kp->kp_raw.rcb_proto.sp_protocol
479 == PF_KEY) /* XXX: AF_KEY */
485 error = raw_usrreqs.pru_detach(so);
492 * derived from net/rtsock.c:key_disconnect()
495 key_disconnect(struct socket *so)
499 error = raw_usrreqs.pru_disconnect(so);
506 * derived from net/rtsock.c:rts_peeraddr()
509 key_peeraddr(struct socket *so, struct sockaddr **nam)
513 error = raw_usrreqs.pru_peeraddr(so, nam);
520 * derived from net/rtsock.c:rts_send()
523 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
524 struct mbuf *control, struct proc *td)
528 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
535 * derived from net/rtsock.c:rts_shutdown()
538 key_shutdown(struct socket *so)
542 error = raw_usrreqs.pru_shutdown(so);
549 * derived from net/rtsock.c:rts_sockaddr()
552 key_sockaddr(struct socket *so, struct sockaddr **nam)
556 error = raw_usrreqs.pru_sockaddr(so, nam);
561 struct pr_usrreqs key_usrreqs = {
562 key_abort, pru_accept_notsupp, key_attach, key_bind,
564 pru_connect2_notsupp, pru_control_notsupp, key_detach,
565 key_disconnect, pru_listen_notsupp, key_peeraddr,
567 pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
568 key_sockaddr, sosend, soreceive, sopoll
572 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
575 * Definitions of protocols supported in the KEY domain.
578 extern struct domain keydomain;
580 struct protosw keysw[] = {
581 { SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
582 0, (pr_output_t *)key_output, raw_ctlinput, 0,
592 bzero((caddr_t)&key_cb, sizeof(key_cb));
596 struct domain keydomain =
597 { PF_KEY, "key", key_init0, 0, 0,
598 keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])] };