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>
43 #include <sys/malloc.h>
45 #include <sys/mutex.h>
47 #include <sys/protosw.h>
48 #include <sys/signalvar.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
54 #include <net/raw_cb.h>
55 #include <net/route.h>
57 #include <net/pfkeyv2.h>
58 #include <netipsec/key.h>
59 #include <netipsec/keysock.h>
60 #include <netipsec/key_debug.h>
62 #include <machine/stdarg.h>
68 static struct key_cb key_cb;
70 static struct sockaddr key_src = { 2, PF_KEY, };
72 static int key_sendup0 __P((struct rawcb *, struct mbuf *, int));
74 struct pfkeystat pfkeystat;
80 key_output(struct mbuf *m, struct socket *so)
86 panic("%s: NULL pointer was passed.\n", __func__);
88 pfkeystat.out_total++;
89 pfkeystat.out_bytes += m->m_pkthdr.len;
91 len = m->m_pkthdr.len;
92 if (len < sizeof(struct sadb_msg)) {
93 pfkeystat.out_tooshort++;
98 if (m->m_len < sizeof(struct sadb_msg)) {
99 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
100 pfkeystat.out_nomem++;
108 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
110 msg = mtod(m, struct sadb_msg *);
111 pfkeystat.out_msgtype[msg->sadb_msg_type]++;
112 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
113 pfkeystat.out_invlen++;
118 error = key_parse(m, so);
127 * send message to the socket.
130 key_sendup0(rp, m, promisc)
138 struct sadb_msg *pmsg;
140 M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT);
141 if (m && m->m_len < sizeof(struct sadb_msg))
142 m = m_pullup(m, sizeof(struct sadb_msg));
144 pfkeystat.in_nomem++;
148 m->m_pkthdr.len += sizeof(*pmsg);
150 pmsg = mtod(m, struct sadb_msg *);
151 bzero(pmsg, sizeof(*pmsg));
152 pmsg->sadb_msg_version = PF_KEY_V2;
153 pmsg->sadb_msg_type = SADB_X_PROMISC;
154 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
157 pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
160 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
162 pfkeystat.in_nomem++;
167 sorwakeup(rp->rcb_socket);
171 /* XXX this interface should be obsoleted. */
173 key_sendup(so, msg, len, target)
175 struct sadb_msg *msg;
177 int target; /*target of the resulting message*/
179 struct mbuf *m, *n, *mprev;
183 if (so == 0 || msg == 0)
184 panic("%s: NULL pointer was passed.\n", __func__);
186 KEYDEBUG(KEYDEBUG_KEY_DUMP,
187 printf("%s: \n", __func__);
191 * we increment statistics here, just in case we have ENOBUFS
194 pfkeystat.in_total++;
195 pfkeystat.in_bytes += len;
196 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
199 * Get mbuf chain whenever possible (not clusters),
200 * to save socket buffer. We'll be generating many SADB_ACQUIRE
201 * messages to listening key sockets. If we simply allocate clusters,
202 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
203 * sbspace() computes # of actual data bytes AND mbuf region.
205 * TODO: SADB_ACQUIRE filters should be implemented.
211 MGETHDR(n, M_DONTWAIT, MT_DATA);
213 pfkeystat.in_nomem++;
218 MGET(n, M_DONTWAIT, MT_DATA);
220 pfkeystat.in_nomem++;
225 if (tlen >= MCLBYTES) { /*XXX better threshold? */
226 MCLGET(n, M_DONTWAIT);
227 if ((n->m_flags & M_EXT) == 0) {
230 pfkeystat.in_nomem++;
248 m->m_pkthdr.len = len;
249 m->m_pkthdr.rcvif = NULL;
250 m_copyback(m, 0, len, (caddr_t)msg);
252 /* avoid duplicated statistics */
253 pfkeystat.in_total--;
254 pfkeystat.in_bytes -= len;
255 pfkeystat.in_msgtype[msg->sadb_msg_type]--;
257 return key_sendup_mbuf(so, m, target);
260 /* so can be NULL if target != KEY_SENDUP_ONE */
262 key_sendup_mbuf(so, m, target)
274 panic("key_sendup_mbuf: NULL pointer was passed.\n");
275 if (so == NULL && target == KEY_SENDUP_ONE)
276 panic("%s: NULL pointer was passed.\n", __func__);
278 pfkeystat.in_total++;
279 pfkeystat.in_bytes += m->m_pkthdr.len;
280 if (m->m_len < sizeof(struct sadb_msg)) {
281 m = m_pullup(m, sizeof(struct sadb_msg));
283 pfkeystat.in_nomem++;
287 if (m->m_len >= sizeof(struct sadb_msg)) {
288 struct sadb_msg *msg;
289 msg = mtod(m, struct sadb_msg *);
290 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
292 mtx_lock(&rawcb_mtx);
293 LIST_FOREACH(rp, &rawcb_list, list)
295 if (rp->rcb_proto.sp_family != PF_KEY)
297 if (rp->rcb_proto.sp_protocol
298 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
302 kp = (struct keycb *)rp;
305 * If you are in promiscuous mode, and when you get broadcasted
306 * reply, you'll get two PF_KEY messages.
307 * (based on pf_key@inner.net message on 14 Oct 1998)
309 if (((struct keycb *)rp)->kp_promisc) {
310 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
311 (void)key_sendup0(rp, n, 1);
316 /* the exact target will be processed later */
317 if (so && sotorawcb(so) == rp)
323 /* the statement has no effect */
324 if (so && sotorawcb(so) == rp)
330 case KEY_SENDUP_REGISTERED:
331 if (kp->kp_registered)
335 pfkeystat.in_msgtarget[target]++;
340 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
342 pfkeystat.in_nomem++;
343 mtx_unlock(&rawcb_mtx);
347 if ((error = key_sendup0(rp, n, 0)) != 0) {
349 mtx_unlock(&rawcb_mtx);
357 error = key_sendup0(sotorawcb(so), m, 0);
363 mtx_unlock(&rawcb_mtx);
369 * derived from net/rtsock.c:rts_abort()
372 key_abort(struct socket *so)
374 raw_usrreqs.pru_abort(so);
379 * derived from net/rtsock.c:rts_attach()
382 key_attach(struct socket *so, int proto, struct thread *td)
387 KASSERT(so->so_pcb == NULL, ("key_attach: so_pcb != NULL"));
390 error = priv_check(td, PRIV_NET_RAW);
396 MALLOC(kp, struct keycb *, sizeof *kp, M_PCB, M_WAITOK | M_ZERO);
400 so->so_pcb = (caddr_t)kp;
401 error = raw_attach(so, proto);
402 kp = (struct keycb *)sotorawcb(so);
405 so->so_pcb = (caddr_t) 0;
409 kp->kp_promisc = kp->kp_registered = 0;
411 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
415 so->so_options |= SO_USELOOPBACK;
422 * derived from net/rtsock.c:rts_bind()
425 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
432 * derived from net/rtsock.c:rts_close().
435 key_close(struct socket *so)
438 raw_usrreqs.pru_close(so);
443 * derived from net/rtsock.c:rts_connect()
446 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
453 * derived from net/rtsock.c:rts_detach()
456 key_detach(struct socket *so)
458 struct keycb *kp = (struct keycb *)sotorawcb(so);
460 KASSERT(kp != NULL, ("key_detach: kp == NULL"));
461 if (kp->kp_raw.rcb_proto.sp_protocol
462 == PF_KEY) /* XXX: AF_KEY */
467 raw_usrreqs.pru_detach(so);
472 * derived from net/rtsock.c:key_disconnect()
475 key_disconnect(struct socket *so)
477 return(raw_usrreqs.pru_disconnect(so));
482 * derived from net/rtsock.c:rts_peeraddr()
485 key_peeraddr(struct socket *so, struct sockaddr **nam)
487 return(raw_usrreqs.pru_peeraddr(so, nam));
492 * derived from net/rtsock.c:rts_send()
495 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
496 struct mbuf *control, struct thread *td)
498 return(raw_usrreqs.pru_send(so, flags, m, nam, control, td));
503 * derived from net/rtsock.c:rts_shutdown()
506 key_shutdown(struct socket *so)
508 return(raw_usrreqs.pru_shutdown(so));
513 * derived from net/rtsock.c:rts_sockaddr()
516 key_sockaddr(struct socket *so, struct sockaddr **nam)
518 return(raw_usrreqs.pru_sockaddr(so, nam));
521 struct pr_usrreqs key_usrreqs = {
522 .pru_abort = key_abort,
523 .pru_attach = key_attach,
524 .pru_bind = key_bind,
525 .pru_connect = key_connect,
526 .pru_detach = key_detach,
527 .pru_disconnect = key_disconnect,
528 .pru_peeraddr = key_peeraddr,
529 .pru_send = key_send,
530 .pru_shutdown = key_shutdown,
531 .pru_sockaddr = key_sockaddr,
532 .pru_close = key_close,
536 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
539 * Definitions of protocols supported in the KEY domain.
542 extern struct domain keydomain;
544 struct protosw keysw[] = {
547 .pr_domain = &keydomain,
548 .pr_protocol = PF_KEY_V2,
549 .pr_flags = PR_ATOMIC|PR_ADDR,
550 .pr_output = key_output,
551 .pr_ctlinput = raw_ctlinput,
553 .pr_usrreqs = &key_usrreqs
560 bzero((caddr_t)&key_cb, sizeof(key_cb));
564 struct domain keydomain = {
565 .dom_family = PF_KEY,
567 .dom_init = key_init0,
568 .dom_protosw = keysw,
569 .dom_protoswNPROTOSW = &keysw[sizeof(keysw)/sizeof(keysw[0])]