1 /* $FreeBSD: src/sys/netkey/keysock.c,v 1.1.2.4 2003/01/11 19:10:59 ume Exp $ */
2 /* $DragonFly: src/sys/netproto/key/keysock.c,v 1.3 2003/07/21 07:57:51 dillon 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/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49 #include <sys/errno.h>
51 #include <machine/ipl.h>
53 #include <net/raw_cb.h>
54 #include <net/route.h>
55 #include <netinet/in.h>
57 #include <net/pfkeyv2.h>
58 #include <netkey/keydb.h>
59 #include <netkey/key.h>
60 #include <netkey/keysock.h>
61 #include <netkey/key_debug.h>
63 #include <machine/stdarg.h>
65 struct sockaddr key_dst = { 2, PF_KEY, };
66 struct sockaddr key_src = { 2, PF_KEY, };
68 static int key_sendup0 __P((struct rawcb *, struct mbuf *, int));
70 struct pfkeystat pfkeystat;
77 key_output(struct mbuf *m, ...)
79 key_output(m, va_alist)
91 so = va_arg(ap, struct socket *);
95 panic("key_output: NULL pointer was passed.\n");
97 pfkeystat.out_total++;
98 pfkeystat.out_bytes += m->m_pkthdr.len;
100 len = m->m_pkthdr.len;
101 if (len < sizeof(struct sadb_msg)) {
102 pfkeystat.out_tooshort++;
107 if (m->m_len < sizeof(struct sadb_msg)) {
108 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
109 pfkeystat.out_nomem++;
115 if ((m->m_flags & M_PKTHDR) == 0)
116 panic("key_output: not M_PKTHDR ??");
118 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
120 msg = mtod(m, struct sadb_msg *);
121 pfkeystat.out_msgtype[msg->sadb_msg_type]++;
122 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
123 pfkeystat.out_invlen++;
130 error = key_parse(m, so);
140 * send message to the socket.
143 key_sendup0(rp, m, promisc)
151 struct sadb_msg *pmsg;
153 M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT);
154 if (m && m->m_len < sizeof(struct sadb_msg))
155 m = m_pullup(m, sizeof(struct sadb_msg));
157 pfkeystat.in_nomem++;
161 m->m_pkthdr.len += sizeof(*pmsg);
163 pmsg = mtod(m, struct sadb_msg *);
164 bzero(pmsg, sizeof(*pmsg));
165 pmsg->sadb_msg_version = PF_KEY_V2;
166 pmsg->sadb_msg_type = SADB_X_PROMISC;
167 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
170 pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
173 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
175 pfkeystat.in_nomem++;
180 sorwakeup(rp->rcb_socket);
184 /* XXX this interface should be obsoleted. */
186 key_sendup(so, msg, len, target)
188 struct sadb_msg *msg;
190 int target; /*target of the resulting message*/
192 struct mbuf *m, *n, *mprev;
196 if (so == 0 || msg == 0)
197 panic("key_sendup: NULL pointer was passed.\n");
199 KEYDEBUG(KEYDEBUG_KEY_DUMP,
200 printf("key_sendup: \n");
204 * we increment statistics here, just in case we have ENOBUFS
207 pfkeystat.in_total++;
208 pfkeystat.in_bytes += len;
209 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
212 * Get mbuf chain whenever possible (not clusters),
213 * to save socket buffer. We'll be generating many SADB_ACQUIRE
214 * messages to listening key sockets. If we simply allocate clusters,
215 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
216 * sbspace() computes # of actual data bytes AND mbuf region.
218 * TODO: SADB_ACQUIRE filters should be implemented.
224 MGETHDR(n, M_DONTWAIT, MT_DATA);
227 MGET(n, M_DONTWAIT, MT_DATA);
231 pfkeystat.in_nomem++;
234 if (tlen >= MCLBYTES) { /*XXX better threshold? */
235 MCLGET(n, M_DONTWAIT);
236 if ((n->m_flags & M_EXT) == 0) {
239 pfkeystat.in_nomem++;
257 m->m_pkthdr.len = len;
258 m->m_pkthdr.rcvif = NULL;
259 m_copyback(m, 0, len, (caddr_t)msg);
261 /* avoid duplicated statistics */
262 pfkeystat.in_total--;
263 pfkeystat.in_bytes -= len;
264 pfkeystat.in_msgtype[msg->sadb_msg_type]--;
266 return key_sendup_mbuf(so, m, target);
269 /* so can be NULL if target != KEY_SENDUP_ONE */
271 key_sendup_mbuf(so, m, target)
283 panic("key_sendup_mbuf: NULL pointer was passed.\n");
284 if (so == NULL && target == KEY_SENDUP_ONE)
285 panic("key_sendup_mbuf: NULL pointer was passed.\n");
287 pfkeystat.in_total++;
288 pfkeystat.in_bytes += m->m_pkthdr.len;
289 if (m->m_len < sizeof(struct sadb_msg)) {
291 m = m_pullup(m, sizeof(struct sadb_msg));
293 pfkeystat.in_nomem++;
297 /* don't bother pulling it up just for stats */
300 if (m->m_len >= sizeof(struct sadb_msg)) {
301 struct sadb_msg *msg;
302 msg = mtod(m, struct sadb_msg *);
303 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
306 LIST_FOREACH(rp, &rawcb_list, list)
308 if (rp->rcb_proto.sp_family != PF_KEY)
310 if (rp->rcb_proto.sp_protocol
311 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
315 kp = (struct keycb *)rp;
318 * If you are in promiscuous mode, and when you get broadcasted
319 * reply, you'll get two PF_KEY messages.
320 * (based on pf_key@inner.net message on 14 Oct 1998)
322 if (((struct keycb *)rp)->kp_promisc) {
323 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
324 (void)key_sendup0(rp, n, 1);
329 /* the exact target will be processed later */
330 if (so && sotorawcb(so) == rp)
336 /* the statement has no effect */
337 if (so && sotorawcb(so) == rp)
343 case KEY_SENDUP_REGISTERED:
344 if (kp->kp_registered)
348 pfkeystat.in_msgtarget[target]++;
353 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
355 pfkeystat.in_nomem++;
359 if ((error = key_sendup0(rp, n, 0)) != 0) {
368 error = key_sendup0(sotorawcb(so), m, 0);
379 * derived from net/rtsock.c:rts_abort()
382 key_abort(struct socket *so)
386 error = raw_usrreqs.pru_abort(so);
393 * derived from net/rtsock.c:rts_attach()
396 key_attach(struct socket *so, int proto, struct thread *td)
401 if (sotorawcb(so) != 0)
402 return EISCONN; /* XXX panic? */
403 kp = (struct keycb *)malloc(sizeof *kp, M_PCB, M_WAITOK); /* XXX */
406 bzero(kp, sizeof *kp);
409 * The splnet() is necessary to block protocols from sending
410 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
411 * this PCB is extant but incompletely initialized.
412 * Probably we should try to do more of this work beforehand and
416 so->so_pcb = (caddr_t)kp;
417 error = raw_usrreqs.pru_attach(so, proto, td);
418 kp = (struct keycb *)sotorawcb(so);
421 so->so_pcb = (caddr_t) 0;
426 kp->kp_promisc = kp->kp_registered = 0;
428 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
431 kp->kp_raw.rcb_laddr = &key_src;
432 kp->kp_raw.rcb_faddr = &key_dst;
434 so->so_options |= SO_USELOOPBACK;
442 * derived from net/rtsock.c:rts_bind()
445 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
449 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
456 * derived from net/rtsock.c:rts_connect()
459 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
463 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
470 * derived from net/rtsock.c:rts_detach()
473 key_detach(struct socket *so)
475 struct keycb *kp = (struct keycb *)sotorawcb(so);
480 if (kp->kp_raw.rcb_proto.sp_protocol
481 == PF_KEY) /* XXX: AF_KEY */
487 error = raw_usrreqs.pru_detach(so);
494 * derived from net/rtsock.c:key_disconnect()
497 key_disconnect(struct socket *so)
501 error = raw_usrreqs.pru_disconnect(so);
508 * derived from net/rtsock.c:rts_peeraddr()
511 key_peeraddr(struct socket *so, struct sockaddr **nam)
515 error = raw_usrreqs.pru_peeraddr(so, nam);
522 * derived from net/rtsock.c:rts_send()
525 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
526 struct mbuf *control, struct thread *td)
530 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
537 * derived from net/rtsock.c:rts_shutdown()
540 key_shutdown(struct socket *so)
544 error = raw_usrreqs.pru_shutdown(so);
551 * derived from net/rtsock.c:rts_sockaddr()
554 key_sockaddr(struct socket *so, struct sockaddr **nam)
558 error = raw_usrreqs.pru_sockaddr(so, nam);
563 struct pr_usrreqs key_usrreqs = {
564 key_abort, pru_accept_notsupp, key_attach, key_bind,
566 pru_connect2_notsupp, pru_control_notsupp, key_detach,
567 key_disconnect, pru_listen_notsupp, key_peeraddr,
569 pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
570 key_sockaddr, sosend, soreceive, sopoll
574 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
577 * Definitions of protocols supported in the KEY domain.
580 extern struct domain keydomain;
582 struct protosw keysw[] = {
583 { SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
584 0, key_output, raw_ctlinput, 0,
591 struct domain keydomain =
592 { PF_KEY, "key", key_init, 0, 0,
593 keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])] };