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>
46 #include <sys/protosw.h>
47 #include <sys/signalvar.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
53 #include <net/raw_cb.h>
54 #include <net/route.h>
56 #include <net/pfkeyv2.h>
57 #include <netipsec/key.h>
58 #include <netipsec/keysock.h>
59 #include <netipsec/key_debug.h>
61 #include <machine/stdarg.h>
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("%s: NULL pointer was passed.\n", __func__);
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++;
121 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
123 msg = mtod(m, struct sadb_msg *);
124 pfkeystat.out_msgtype[msg->sadb_msg_type]++;
125 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
126 pfkeystat.out_invlen++;
133 error = key_parse(m, so);
143 * send message to the socket.
146 key_sendup0(rp, m, promisc)
154 struct sadb_msg *pmsg;
156 M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT);
157 if (m && m->m_len < sizeof(struct sadb_msg))
158 m = m_pullup(m, sizeof(struct sadb_msg));
160 pfkeystat.in_nomem++;
164 m->m_pkthdr.len += sizeof(*pmsg);
166 pmsg = mtod(m, struct sadb_msg *);
167 bzero(pmsg, sizeof(*pmsg));
168 pmsg->sadb_msg_version = PF_KEY_V2;
169 pmsg->sadb_msg_type = SADB_X_PROMISC;
170 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
173 pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
176 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
178 pfkeystat.in_nomem++;
183 sorwakeup(rp->rcb_socket);
187 /* XXX this interface should be obsoleted. */
189 key_sendup(so, msg, len, target)
191 struct sadb_msg *msg;
193 int target; /*target of the resulting message*/
195 struct mbuf *m, *n, *mprev;
199 if (so == 0 || msg == 0)
200 panic("%s: NULL pointer was passed.\n", __func__);
202 KEYDEBUG(KEYDEBUG_KEY_DUMP,
203 printf("%s: \n", __func__);
207 * we increment statistics here, just in case we have ENOBUFS
210 pfkeystat.in_total++;
211 pfkeystat.in_bytes += len;
212 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
215 * Get mbuf chain whenever possible (not clusters),
216 * to save socket buffer. We'll be generating many SADB_ACQUIRE
217 * messages to listening key sockets. If we simply allocate clusters,
218 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
219 * sbspace() computes # of actual data bytes AND mbuf region.
221 * TODO: SADB_ACQUIRE filters should be implemented.
227 MGETHDR(n, M_DONTWAIT, MT_DATA);
230 MGET(n, M_DONTWAIT, MT_DATA);
234 pfkeystat.in_nomem++;
237 if (tlen >= MCLBYTES) { /*XXX better threshold? */
238 MCLGET(n, M_DONTWAIT);
239 if ((n->m_flags & M_EXT) == 0) {
242 pfkeystat.in_nomem++;
260 m->m_pkthdr.len = len;
261 m->m_pkthdr.rcvif = NULL;
262 m_copyback(m, 0, len, (caddr_t)msg);
264 /* avoid duplicated statistics */
265 pfkeystat.in_total--;
266 pfkeystat.in_bytes -= len;
267 pfkeystat.in_msgtype[msg->sadb_msg_type]--;
269 return key_sendup_mbuf(so, m, target);
272 /* so can be NULL if target != KEY_SENDUP_ONE */
274 key_sendup_mbuf(so, m, target)
286 panic("key_sendup_mbuf: NULL pointer was passed.\n");
287 if (so == NULL && target == KEY_SENDUP_ONE)
288 panic("%s: NULL pointer was passed.\n", __func__);
290 pfkeystat.in_total++;
291 pfkeystat.in_bytes += m->m_pkthdr.len;
292 if (m->m_len < sizeof(struct sadb_msg)) {
294 m = m_pullup(m, sizeof(struct sadb_msg));
296 pfkeystat.in_nomem++;
300 /* don't bother pulling it up just for stats */
303 if (m->m_len >= sizeof(struct sadb_msg)) {
304 struct sadb_msg *msg;
305 msg = mtod(m, struct sadb_msg *);
306 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
309 LIST_FOREACH(rp, &rawcb_list, list)
311 if (rp->rcb_proto.sp_family != PF_KEY)
313 if (rp->rcb_proto.sp_protocol
314 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
318 kp = (struct keycb *)rp;
321 * If you are in promiscuous mode, and when you get broadcasted
322 * reply, you'll get two PF_KEY messages.
323 * (based on pf_key@inner.net message on 14 Oct 1998)
325 if (((struct keycb *)rp)->kp_promisc) {
326 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
327 (void)key_sendup0(rp, n, 1);
332 /* the exact target will be processed later */
333 if (so && sotorawcb(so) == rp)
339 /* the statement has no effect */
340 if (so && sotorawcb(so) == rp)
346 case KEY_SENDUP_REGISTERED:
347 if (kp->kp_registered)
351 pfkeystat.in_msgtarget[target]++;
356 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
358 pfkeystat.in_nomem++;
362 if ((error = key_sendup0(rp, n, 0)) != 0) {
371 error = key_sendup0(sotorawcb(so), m, 0);
382 * derived from net/rtsock.c:rts_abort()
385 key_abort(struct socket *so)
389 error = raw_usrreqs.pru_abort(so);
396 * derived from net/rtsock.c:rts_attach()
399 key_attach(struct socket *so, int proto, struct thread *td)
404 if (sotorawcb(so) != 0)
405 return EISCONN; /* XXX panic? */
406 kp = (struct keycb *)malloc(sizeof *kp, M_PCB, M_WAITOK|M_ZERO); /* XXX */
411 * The splnet() is necessary to block protocols from sending
412 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
413 * this PCB is extant but incompletely initialized.
414 * Probably we should try to do more of this work beforehand and
418 so->so_pcb = (caddr_t)kp;
419 error = raw_usrreqs.pru_attach(so, proto, td);
420 kp = (struct keycb *)sotorawcb(so);
423 so->so_pcb = (caddr_t) 0;
428 kp->kp_promisc = kp->kp_registered = 0;
430 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
433 kp->kp_raw.rcb_laddr = &key_src;
434 kp->kp_raw.rcb_faddr = &key_dst;
436 so->so_options |= SO_USELOOPBACK;
444 * derived from net/rtsock.c:rts_bind()
447 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
451 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
458 * derived from net/rtsock.c:rts_connect()
461 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
465 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
472 * derived from net/rtsock.c:rts_detach()
475 key_detach(struct socket *so)
477 struct keycb *kp = (struct keycb *)sotorawcb(so);
482 if (kp->kp_raw.rcb_proto.sp_protocol
483 == PF_KEY) /* XXX: AF_KEY */
489 error = raw_usrreqs.pru_detach(so);
496 * derived from net/rtsock.c:key_disconnect()
499 key_disconnect(struct socket *so)
503 error = raw_usrreqs.pru_disconnect(so);
510 * derived from net/rtsock.c:rts_peeraddr()
513 key_peeraddr(struct socket *so, struct sockaddr **nam)
517 error = raw_usrreqs.pru_peeraddr(so, nam);
524 * derived from net/rtsock.c:rts_send()
527 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
528 struct mbuf *control, struct thread *td)
532 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
539 * derived from net/rtsock.c:rts_shutdown()
542 key_shutdown(struct socket *so)
546 error = raw_usrreqs.pru_shutdown(so);
553 * derived from net/rtsock.c:rts_sockaddr()
556 key_sockaddr(struct socket *so, struct sockaddr **nam)
560 error = raw_usrreqs.pru_sockaddr(so, nam);
565 struct pr_usrreqs key_usrreqs = {
566 .pru_abort = key_abort,
567 .pru_attach = key_attach,
568 .pru_bind = key_bind,
569 .pru_connect = key_connect,
570 .pru_detach = key_detach,
571 .pru_disconnect = key_disconnect,
572 .pru_peeraddr = key_peeraddr,
573 .pru_send = key_send,
574 .pru_shutdown = key_shutdown,
575 .pru_sockaddr = key_sockaddr,
579 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
582 * Definitions of protocols supported in the KEY domain.
585 extern struct domain keydomain;
587 struct protosw keysw[] = {
590 .pr_domain = &keydomain,
591 .pr_protocol = PF_KEY_V2,
592 .pr_flags = PR_ATOMIC|PR_ADDR,
593 .pr_output = (pr_output_t *)key_output,
594 .pr_ctlinput = raw_ctlinput,
596 .pr_usrreqs = &key_usrreqs
603 bzero((caddr_t)&key_cb, sizeof(key_cb));
607 struct domain keydomain = {
608 .dom_family = PF_KEY,
610 .dom_init = key_init0,
611 .dom_protosw = keysw,
612 .dom_protoswNPROTOSW = &keysw[sizeof(keysw)/sizeof(keysw[0])]