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.15 2006/09/05 00:55:49 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>
50 #include <sys/thread2.h>
52 #include <machine/ipl.h>
54 #include <net/raw_cb.h>
55 #include <net/route.h>
56 #include <netinet/in.h>
58 #include <net/pfkeyv2.h>
62 #include "key_debug.h"
64 #include <machine/stdarg.h>
66 struct sockaddr key_dst = { 2, PF_KEY, };
67 struct sockaddr key_src = { 2, PF_KEY, };
69 static int key_sendup0 (struct rawcb *, struct mbuf *, int);
71 struct pfkeystat pfkeystat;
77 key_output(struct mbuf *m, struct socket *so, ...)
83 panic("key_output: NULL pointer was passed.\n");
85 pfkeystat.out_total++;
86 pfkeystat.out_bytes += m->m_pkthdr.len;
88 len = m->m_pkthdr.len;
89 if (len < sizeof(struct sadb_msg)) {
90 pfkeystat.out_tooshort++;
95 if (m->m_len < sizeof(struct sadb_msg)) {
96 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
97 pfkeystat.out_nomem++;
103 if ((m->m_flags & M_PKTHDR) == 0)
104 panic("key_output: not M_PKTHDR ??");
106 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
108 msg = mtod(m, struct sadb_msg *);
109 pfkeystat.out_msgtype[msg->sadb_msg_type]++;
110 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
111 pfkeystat.out_invlen++;
118 error = key_parse(m, so);
128 * send message to the socket.
131 key_sendup0(struct rawcb *rp, struct mbuf *m, int promisc)
136 struct sadb_msg *pmsg;
138 M_PREPEND(m, sizeof(struct sadb_msg), MB_DONTWAIT);
139 if (m && m->m_len < sizeof(struct sadb_msg))
140 m = m_pullup(m, sizeof(struct sadb_msg));
142 pfkeystat.in_nomem++;
146 m->m_pkthdr.len += sizeof(*pmsg);
148 pmsg = mtod(m, struct sadb_msg *);
149 bzero(pmsg, sizeof(*pmsg));
150 pmsg->sadb_msg_version = PF_KEY_V2;
151 pmsg->sadb_msg_type = SADB_X_PROMISC;
152 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
155 pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
158 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
160 pfkeystat.in_nomem++;
165 sorwakeup(rp->rcb_socket);
170 * XXX this interface should be obsoleted.
173 * target: target of the resulting message
176 key_sendup(struct socket *so, struct sadb_msg *msg, u_int len,
179 struct mbuf *m, *n, *mprev;
183 if (so == 0 || msg == 0)
184 panic("key_sendup: NULL pointer was passed.\n");
186 KEYDEBUG(KEYDEBUG_KEY_DUMP,
187 printf("key_sendup: \n");
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, MB_DONTWAIT, MT_DATA);
214 MGET(n, MB_DONTWAIT, MT_DATA);
218 pfkeystat.in_nomem++;
221 if (tlen >= MCLBYTES) { /*XXX better threshold? */
222 MCLGET(n, MB_DONTWAIT);
223 if ((n->m_flags & M_EXT) == 0) {
226 pfkeystat.in_nomem++;
244 m->m_pkthdr.len = len;
245 m->m_pkthdr.rcvif = NULL;
246 m_copyback(m, 0, len, (caddr_t)msg);
248 /* avoid duplicated statistics */
249 pfkeystat.in_total--;
250 pfkeystat.in_bytes -= len;
251 pfkeystat.in_msgtype[msg->sadb_msg_type]--;
253 return key_sendup_mbuf(so, m, target);
256 /* so can be NULL if target != KEY_SENDUP_ONE */
258 key_sendup_mbuf(struct socket *so, struct mbuf *m, int target)
267 panic("key_sendup_mbuf: NULL pointer was passed.\n");
268 if (so == NULL && target == KEY_SENDUP_ONE)
269 panic("key_sendup_mbuf: NULL pointer was passed.\n");
271 pfkeystat.in_total++;
272 pfkeystat.in_bytes += m->m_pkthdr.len;
273 if (m->m_len < sizeof(struct sadb_msg)) {
275 m = m_pullup(m, sizeof(struct sadb_msg));
277 pfkeystat.in_nomem++;
281 /* don't bother pulling it up just for stats */
284 if (m->m_len >= sizeof(struct sadb_msg)) {
285 struct sadb_msg *msg;
286 msg = mtod(m, struct sadb_msg *);
287 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
290 LIST_FOREACH(rp, &rawcb_list, list)
292 if (rp->rcb_proto.sp_family != PF_KEY)
294 if (rp->rcb_proto.sp_protocol
295 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
299 kp = (struct keycb *)rp;
302 * If you are in promiscuous mode, and when you get broadcasted
303 * reply, you'll get two PF_KEY messages.
304 * (based on pf_key@inner.net message on 14 Oct 1998)
306 if (((struct keycb *)rp)->kp_promisc) {
307 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
308 key_sendup0(rp, n, 1);
313 /* the exact target will be processed later */
314 if (so && sotorawcb(so) == rp)
320 /* the statement has no effect */
321 if (so && sotorawcb(so) == rp)
327 case KEY_SENDUP_REGISTERED:
328 if (kp->kp_registered)
332 pfkeystat.in_msgtarget[target]++;
337 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
339 pfkeystat.in_nomem++;
343 if ((error = key_sendup0(rp, n, 0)) != 0) {
352 error = key_sendup0(sotorawcb(so), m, 0);
363 * derived from net/rtsock.c:rts_abort()
366 key_abort(struct socket *so)
371 error = raw_usrreqs.pru_abort(so);
378 * derived from net/rtsock.c:rts_attach()
381 key_attach(struct socket *so, int proto, struct pru_attach_info *ai)
386 if (sotorawcb(so) != 0)
387 return EISCONN; /* XXX panic? */
388 kp = (struct keycb *)kmalloc(sizeof *kp, M_PCB, M_WAITOK); /* XXX */
391 bzero(kp, sizeof *kp);
394 * The critical section is necessary to block protocols from sending
395 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
396 * this PCB is extant but incompletely initialized.
397 * Probably we should try to do more of this work beforehand and
398 * eliminate the critical section.
401 so->so_pcb = (caddr_t)kp;
402 error = raw_usrreqs.pru_attach(so, proto, ai);
403 kp = (struct keycb *)sotorawcb(so);
406 so->so_pcb = (caddr_t) 0;
411 kp->kp_promisc = kp->kp_registered = 0;
413 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
416 kp->kp_raw.rcb_laddr = &key_src;
417 kp->kp_raw.rcb_faddr = &key_dst;
419 so->so_options |= SO_USELOOPBACK;
427 * derived from net/rtsock.c:rts_bind()
430 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
435 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
442 * derived from net/rtsock.c:rts_connect()
445 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
450 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
457 * derived from net/rtsock.c:rts_detach()
460 key_detach(struct socket *so)
462 struct keycb *kp = (struct keycb *)sotorawcb(so);
467 if (kp->kp_raw.rcb_proto.sp_protocol
468 == PF_KEY) /* XXX: AF_KEY */
474 error = raw_usrreqs.pru_detach(so);
481 * derived from net/rtsock.c:key_disconnect()
484 key_disconnect(struct socket *so)
489 error = raw_usrreqs.pru_disconnect(so);
496 * derived from net/rtsock.c:rts_peeraddr()
499 key_peeraddr(struct socket *so, struct sockaddr **nam)
504 error = raw_usrreqs.pru_peeraddr(so, nam);
511 * derived from net/rtsock.c:rts_send()
514 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
515 struct mbuf *control, struct thread *td)
520 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
527 * derived from net/rtsock.c:rts_shutdown()
530 key_shutdown(struct socket *so)
535 error = raw_usrreqs.pru_shutdown(so);
542 * derived from net/rtsock.c:rts_sockaddr()
545 key_sockaddr(struct socket *so, struct sockaddr **nam)
550 error = raw_usrreqs.pru_sockaddr(so, nam);
555 struct pr_usrreqs key_usrreqs = {
556 key_abort, pru_accept_notsupp, key_attach, key_bind,
558 pru_connect2_notsupp, pru_control_notsupp, key_detach,
559 key_disconnect, pru_listen_notsupp, key_peeraddr,
561 pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
562 key_sockaddr, sosend, soreceive, sopoll
566 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
569 * Definitions of protocols supported in the KEY domain.
572 extern struct domain keydomain;
574 struct protosw keysw[] = {
575 { SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
576 0, key_output, raw_ctlinput, 0,
583 struct domain keydomain = {
584 PF_KEY, "key", key_init, NULL, NULL,
585 keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])],