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>
53 #include <sys/vimage.h>
56 #include <net/raw_cb.h>
57 #include <net/route.h>
59 #include <netinet/in.h>
61 #include <net/pfkeyv2.h>
62 #include <netipsec/key.h>
63 #include <netipsec/keysock.h>
64 #include <netipsec/key_debug.h>
65 #include <netipsec/ipsec.h>
67 #include <machine/stdarg.h>
73 static struct key_cb key_cb;
75 static struct sockaddr key_src = { 2, PF_KEY, };
77 static int key_sendup0 __P((struct rawcb *, struct mbuf *, int));
79 struct pfkeystat pfkeystat;
85 key_output(struct mbuf *m, struct socket *so)
87 INIT_VNET_IPSEC(curvnet);
92 panic("%s: NULL pointer was passed.\n", __func__);
94 V_pfkeystat.out_total++;
95 V_pfkeystat.out_bytes += m->m_pkthdr.len;
97 len = m->m_pkthdr.len;
98 if (len < sizeof(struct sadb_msg)) {
99 V_pfkeystat.out_tooshort++;
104 if (m->m_len < sizeof(struct sadb_msg)) {
105 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
106 V_pfkeystat.out_nomem++;
114 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
116 msg = mtod(m, struct sadb_msg *);
117 V_pfkeystat.out_msgtype[msg->sadb_msg_type]++;
118 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
119 V_pfkeystat.out_invlen++;
124 error = key_parse(m, so);
133 * send message to the socket.
136 key_sendup0(rp, m, promisc)
141 INIT_VNET_IPSEC(curvnet);
145 struct sadb_msg *pmsg;
147 M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT);
148 if (m && m->m_len < sizeof(struct sadb_msg))
149 m = m_pullup(m, sizeof(struct sadb_msg));
151 V_pfkeystat.in_nomem++;
155 m->m_pkthdr.len += sizeof(*pmsg);
157 pmsg = mtod(m, struct sadb_msg *);
158 bzero(pmsg, sizeof(*pmsg));
159 pmsg->sadb_msg_version = PF_KEY_V2;
160 pmsg->sadb_msg_type = SADB_X_PROMISC;
161 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
164 V_pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
167 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&V_key_src,
169 V_pfkeystat.in_nomem++;
174 sorwakeup(rp->rcb_socket);
178 /* XXX this interface should be obsoleted. */
180 key_sendup(so, msg, len, target)
182 struct sadb_msg *msg;
184 int target; /*target of the resulting message*/
186 INIT_VNET_IPSEC(curvnet);
187 struct mbuf *m, *n, *mprev;
191 if (so == 0 || msg == 0)
192 panic("%s: NULL pointer was passed.\n", __func__);
194 KEYDEBUG(KEYDEBUG_KEY_DUMP,
195 printf("%s: \n", __func__);
199 * we increment statistics here, just in case we have ENOBUFS
202 V_pfkeystat.in_total++;
203 V_pfkeystat.in_bytes += len;
204 V_pfkeystat.in_msgtype[msg->sadb_msg_type]++;
207 * Get mbuf chain whenever possible (not clusters),
208 * to save socket buffer. We'll be generating many SADB_ACQUIRE
209 * messages to listening key sockets. If we simply allocate clusters,
210 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
211 * sbspace() computes # of actual data bytes AND mbuf region.
213 * TODO: SADB_ACQUIRE filters should be implemented.
219 MGETHDR(n, M_DONTWAIT, MT_DATA);
221 V_pfkeystat.in_nomem++;
226 MGET(n, M_DONTWAIT, MT_DATA);
228 V_pfkeystat.in_nomem++;
233 if (tlen >= MCLBYTES) { /*XXX better threshold? */
234 MCLGET(n, M_DONTWAIT);
235 if ((n->m_flags & M_EXT) == 0) {
238 V_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 V_pfkeystat.in_total--;
262 V_pfkeystat.in_bytes -= len;
263 V_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)
275 INIT_VNET_NET(curvnet);
276 INIT_VNET_IPSEC(curvnet);
284 panic("key_sendup_mbuf: NULL pointer was passed.\n");
285 if (so == NULL && target == KEY_SENDUP_ONE)
286 panic("%s: NULL pointer was passed.\n", __func__);
288 V_pfkeystat.in_total++;
289 V_pfkeystat.in_bytes += m->m_pkthdr.len;
290 if (m->m_len < sizeof(struct sadb_msg)) {
291 m = m_pullup(m, sizeof(struct sadb_msg));
293 V_pfkeystat.in_nomem++;
297 if (m->m_len >= sizeof(struct sadb_msg)) {
298 struct sadb_msg *msg;
299 msg = mtod(m, struct sadb_msg *);
300 V_pfkeystat.in_msgtype[msg->sadb_msg_type]++;
302 mtx_lock(&rawcb_mtx);
303 LIST_FOREACH(rp, &V_rawcb_list, list)
305 if (rp->rcb_proto.sp_family != PF_KEY)
307 if (rp->rcb_proto.sp_protocol
308 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
312 kp = (struct keycb *)rp;
315 * If you are in promiscuous mode, and when you get broadcasted
316 * reply, you'll get two PF_KEY messages.
317 * (based on pf_key@inner.net message on 14 Oct 1998)
319 if (((struct keycb *)rp)->kp_promisc) {
320 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
321 (void)key_sendup0(rp, n, 1);
326 /* the exact target will be processed later */
327 if (so && sotorawcb(so) == rp)
333 /* the statement has no effect */
334 if (so && sotorawcb(so) == rp)
340 case KEY_SENDUP_REGISTERED:
341 if (kp->kp_registered)
345 V_pfkeystat.in_msgtarget[target]++;
350 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
352 V_pfkeystat.in_nomem++;
353 mtx_unlock(&rawcb_mtx);
357 if ((error = key_sendup0(rp, n, 0)) != 0) {
359 mtx_unlock(&rawcb_mtx);
367 error = key_sendup0(sotorawcb(so), m, 0);
373 mtx_unlock(&rawcb_mtx);
379 * derived from net/rtsock.c:rts_abort()
382 key_abort(struct socket *so)
384 raw_usrreqs.pru_abort(so);
389 * derived from net/rtsock.c:rts_attach()
392 key_attach(struct socket *so, int proto, struct thread *td)
394 INIT_VNET_IPSEC(curvnet);
398 KASSERT(so->so_pcb == NULL, ("key_attach: so_pcb != NULL"));
401 error = priv_check(td, PRIV_NET_RAW);
407 MALLOC(kp, struct keycb *, sizeof *kp, M_PCB, M_WAITOK | M_ZERO);
411 so->so_pcb = (caddr_t)kp;
412 error = raw_attach(so, proto);
413 kp = (struct keycb *)sotorawcb(so);
416 so->so_pcb = (caddr_t) 0;
420 kp->kp_promisc = kp->kp_registered = 0;
422 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
423 V_key_cb.key_count++;
424 V_key_cb.any_count++;
426 so->so_options |= SO_USELOOPBACK;
433 * derived from net/rtsock.c:rts_bind()
436 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
443 * derived from net/rtsock.c:rts_close().
446 key_close(struct socket *so)
449 raw_usrreqs.pru_close(so);
454 * derived from net/rtsock.c:rts_connect()
457 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
464 * derived from net/rtsock.c:rts_detach()
467 key_detach(struct socket *so)
469 INIT_VNET_IPSEC(curvnet);
470 struct keycb *kp = (struct keycb *)sotorawcb(so);
472 KASSERT(kp != NULL, ("key_detach: kp == NULL"));
473 if (kp->kp_raw.rcb_proto.sp_protocol
474 == PF_KEY) /* XXX: AF_KEY */
475 V_key_cb.key_count--;
476 V_key_cb.any_count--;
479 raw_usrreqs.pru_detach(so);
484 * derived from net/rtsock.c:key_disconnect()
487 key_disconnect(struct socket *so)
489 return(raw_usrreqs.pru_disconnect(so));
494 * derived from net/rtsock.c:rts_peeraddr()
497 key_peeraddr(struct socket *so, struct sockaddr **nam)
499 return(raw_usrreqs.pru_peeraddr(so, nam));
504 * derived from net/rtsock.c:rts_send()
507 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
508 struct mbuf *control, struct thread *td)
510 return(raw_usrreqs.pru_send(so, flags, m, nam, control, td));
515 * derived from net/rtsock.c:rts_shutdown()
518 key_shutdown(struct socket *so)
520 return(raw_usrreqs.pru_shutdown(so));
525 * derived from net/rtsock.c:rts_sockaddr()
528 key_sockaddr(struct socket *so, struct sockaddr **nam)
530 return(raw_usrreqs.pru_sockaddr(so, nam));
533 struct pr_usrreqs key_usrreqs = {
534 .pru_abort = key_abort,
535 .pru_attach = key_attach,
536 .pru_bind = key_bind,
537 .pru_connect = key_connect,
538 .pru_detach = key_detach,
539 .pru_disconnect = key_disconnect,
540 .pru_peeraddr = key_peeraddr,
541 .pru_send = key_send,
542 .pru_shutdown = key_shutdown,
543 .pru_sockaddr = key_sockaddr,
544 .pru_close = key_close,
548 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
551 * Definitions of protocols supported in the KEY domain.
554 extern struct domain keydomain;
556 struct protosw keysw[] = {
559 .pr_domain = &keydomain,
560 .pr_protocol = PF_KEY_V2,
561 .pr_flags = PR_ATOMIC|PR_ADDR,
562 .pr_output = key_output,
563 .pr_ctlinput = raw_ctlinput,
565 .pr_usrreqs = &key_usrreqs
572 INIT_VNET_IPSEC(curvnet);
573 bzero((caddr_t)&V_key_cb, sizeof(V_key_cb));
577 struct domain keydomain = {
578 .dom_family = PF_KEY,
580 .dom_init = key_init0,
581 .dom_protosw = keysw,
582 .dom_protoswNPROTOSW = &keysw[sizeof(keysw)/sizeof(keysw[0])]