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.21 2008/11/01 04:22:16 sephe 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 <net/raw_cb.h>
53 #include <net/route.h>
54 #include <netinet/in.h>
56 #include <net/pfkeyv2.h>
60 #include "key_debug.h"
62 #include <machine/stdarg.h>
64 struct sockaddr key_dst = { 2, PF_KEY, };
65 struct sockaddr key_src = { 2, PF_KEY, };
67 static int key_sendup0 (struct rawcb *, struct mbuf *, int);
69 struct pfkeystat pfkeystat;
75 key_output(struct mbuf *m, struct socket *so, ...)
81 panic("key_output: NULL pointer was passed.\n");
83 pfkeystat.out_total++;
84 pfkeystat.out_bytes += m->m_pkthdr.len;
86 len = m->m_pkthdr.len;
87 if (len < sizeof(struct sadb_msg)) {
88 pfkeystat.out_tooshort++;
93 if (m->m_len < sizeof(struct sadb_msg)) {
94 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
95 pfkeystat.out_nomem++;
101 if ((m->m_flags & M_PKTHDR) == 0)
102 panic("key_output: not M_PKTHDR ??");
104 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
106 msg = mtod(m, struct sadb_msg *);
107 pfkeystat.out_msgtype[msg->sadb_msg_type]++;
108 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
109 pfkeystat.out_invlen++;
116 error = key_parse(m, so);
126 * send message to the socket.
129 key_sendup0(struct rawcb *rp, struct mbuf *m, int promisc)
134 struct sadb_msg *pmsg;
136 M_PREPEND(m, sizeof(struct sadb_msg), MB_DONTWAIT);
137 if (m && m->m_len < sizeof(struct sadb_msg))
138 m = m_pullup(m, sizeof(struct sadb_msg));
140 pfkeystat.in_nomem++;
144 m->m_pkthdr.len += sizeof(*pmsg);
146 pmsg = mtod(m, struct sadb_msg *);
147 bzero(pmsg, sizeof(*pmsg));
148 pmsg->sadb_msg_version = PF_KEY_V2;
149 pmsg->sadb_msg_type = SADB_X_PROMISC;
150 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
153 pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
156 if (!ssb_appendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
158 pfkeystat.in_nomem++;
163 sorwakeup(rp->rcb_socket);
168 * XXX this interface should be obsoleted.
171 * target: target of the resulting message
174 key_sendup(struct socket *so, struct sadb_msg *msg, u_int len,
177 struct mbuf *m, *n, *mprev;
181 if (so == 0 || msg == 0)
182 panic("key_sendup: NULL pointer was passed.\n");
184 KEYDEBUG(KEYDEBUG_KEY_DUMP,
185 kprintf("key_sendup: \n");
189 * we increment statistics here, just in case we have ENOBUFS
192 pfkeystat.in_total++;
193 pfkeystat.in_bytes += len;
194 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
197 * Get mbuf chain whenever possible (not clusters),
198 * to save socket buffer. We'll be generating many SADB_ACQUIRE
199 * messages to listening key sockets. If we simply allocate clusters,
200 * ssb_appendaddr() will raise ENOBUFS due to too little ssb_space().
201 * ssb_space() computes # of actual data bytes AND mbuf region.
203 * TODO: SADB_ACQUIRE filters should be implemented.
209 MGETHDR(n, MB_DONTWAIT, MT_DATA);
212 MGET(n, MB_DONTWAIT, MT_DATA);
216 pfkeystat.in_nomem++;
219 if (tlen >= MCLBYTES) { /*XXX better threshold? */
220 MCLGET(n, MB_DONTWAIT);
221 if ((n->m_flags & M_EXT) == 0) {
224 pfkeystat.in_nomem++;
242 m->m_pkthdr.len = len;
243 m->m_pkthdr.rcvif = NULL;
244 m_copyback(m, 0, len, (caddr_t)msg);
246 /* avoid duplicated statistics */
247 pfkeystat.in_total--;
248 pfkeystat.in_bytes -= len;
249 pfkeystat.in_msgtype[msg->sadb_msg_type]--;
251 return key_sendup_mbuf(so, m, target);
254 /* so can be NULL if target != KEY_SENDUP_ONE */
256 key_sendup_mbuf(struct socket *so, struct mbuf *m, int target)
265 panic("key_sendup_mbuf: NULL pointer was passed.\n");
266 if (so == NULL && target == KEY_SENDUP_ONE)
267 panic("key_sendup_mbuf: NULL pointer was passed.\n");
269 pfkeystat.in_total++;
270 pfkeystat.in_bytes += m->m_pkthdr.len;
271 if (m->m_len < sizeof(struct sadb_msg)) {
273 m = m_pullup(m, sizeof(struct sadb_msg));
275 pfkeystat.in_nomem++;
279 /* don't bother pulling it up just for stats */
282 if (m->m_len >= sizeof(struct sadb_msg)) {
283 struct sadb_msg *msg;
284 msg = mtod(m, struct sadb_msg *);
285 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
288 LIST_FOREACH(rp, &rawcb_list, list)
290 if (rp->rcb_proto.sp_family != PF_KEY)
292 if (rp->rcb_proto.sp_protocol
293 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
297 kp = (struct keycb *)rp;
300 * If you are in promiscuous mode, and when you get broadcasted
301 * reply, you'll get two PF_KEY messages.
302 * (based on pf_key@inner.net message on 14 Oct 1998)
304 if (((struct keycb *)rp)->kp_promisc) {
305 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
306 key_sendup0(rp, n, 1);
311 /* the exact target will be processed later */
312 if (so && sotorawcb(so) == rp)
318 /* the statement has no effect */
319 if (so && sotorawcb(so) == rp)
325 case KEY_SENDUP_REGISTERED:
326 if (kp->kp_registered)
330 pfkeystat.in_msgtarget[target]++;
335 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
337 pfkeystat.in_nomem++;
341 if ((error = key_sendup0(rp, n, 0)) != 0) {
350 error = key_sendup0(sotorawcb(so), m, 0);
361 * derived from net/rtsock.c:rts_abort()
364 key_abort(struct socket *so)
369 error = raw_usrreqs.pru_abort(so);
376 * derived from net/rtsock.c:rts_attach()
379 key_attach(struct socket *so, int proto, struct pru_attach_info *ai)
384 if (sotorawcb(so) != 0)
385 return EISCONN; /* XXX panic? */
386 kp = (struct keycb *)kmalloc(sizeof *kp, M_PCB, M_WAITOK | M_ZERO); /* XXX */
389 * The critical section is necessary to block protocols from sending
390 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
391 * this PCB is extant but incompletely initialized.
392 * Probably we should try to do more of this work beforehand and
393 * eliminate the critical section.
396 so->so_pcb = (caddr_t)kp;
397 error = raw_usrreqs.pru_attach(so, proto, ai);
398 kp = (struct keycb *)sotorawcb(so);
401 so->so_pcb = (caddr_t) 0;
406 kp->kp_promisc = kp->kp_registered = 0;
408 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
411 kp->kp_raw.rcb_laddr = &key_src;
412 kp->kp_raw.rcb_faddr = &key_dst;
414 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)
430 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
437 * derived from net/rtsock.c:rts_connect()
440 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
445 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
452 * derived from net/rtsock.c:rts_detach()
455 key_detach(struct socket *so)
457 struct keycb *kp = (struct keycb *)sotorawcb(so);
462 if (kp->kp_raw.rcb_proto.sp_protocol
463 == PF_KEY) /* XXX: AF_KEY */
469 error = raw_usrreqs.pru_detach(so);
476 * derived from net/rtsock.c:key_disconnect()
479 key_disconnect(struct socket *so)
484 error = raw_usrreqs.pru_disconnect(so);
491 * derived from net/rtsock.c:rts_peeraddr()
494 key_peeraddr(struct socket *so, struct sockaddr **nam)
499 error = raw_usrreqs.pru_peeraddr(so, nam);
506 * derived from net/rtsock.c:rts_send()
509 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
510 struct mbuf *control, struct thread *td)
515 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
522 * derived from net/rtsock.c:rts_shutdown()
525 key_shutdown(struct socket *so)
530 error = raw_usrreqs.pru_shutdown(so);
537 * derived from net/rtsock.c:rts_sockaddr()
540 key_sockaddr(struct socket *so, struct sockaddr **nam)
545 error = raw_usrreqs.pru_sockaddr(so, nam);
550 struct pr_usrreqs key_usrreqs = {
551 key_abort, pru_accept_notsupp, key_attach, key_bind,
553 pru_connect2_notsupp, pru_control_notsupp, key_detach,
554 key_disconnect, pru_listen_notsupp, key_peeraddr,
556 pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
557 key_sockaddr, sosend, soreceive
561 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
564 * Definitions of protocols supported in the KEY domain.
567 extern struct domain keydomain;
569 struct protosw keysw[] = {
570 { SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
571 0, key_output, raw_ctlinput, 0,
572 cpu0_soport, cpu0_ctlport,
578 struct domain keydomain = {
579 PF_KEY, "key", key_init, NULL, NULL,
580 keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])],