1 /* $FreeBSD: src/sys/netkey/keysock.c,v 1.1.2.4 2003/01/11 19:10:59 ume Exp $ */
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/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
43 #include <sys/malloc.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/errno.h>
50 #include <machine/ipl.h>
52 #include <net/raw_cb.h>
53 #include <net/route.h>
54 #include <netinet/in.h>
56 #include <net/pfkeyv2.h>
57 #include <netkey/keydb.h>
58 #include <netkey/key.h>
59 #include <netkey/keysock.h>
60 #include <netkey/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 __P((struct rawcb *, struct mbuf *, int));
69 struct pfkeystat pfkeystat;
76 key_output(struct mbuf *m, ...)
78 key_output(m, va_alist)
90 so = va_arg(ap, struct socket *);
94 panic("key_output: NULL pointer was passed.\n");
96 pfkeystat.out_total++;
97 pfkeystat.out_bytes += m->m_pkthdr.len;
99 len = m->m_pkthdr.len;
100 if (len < sizeof(struct sadb_msg)) {
101 pfkeystat.out_tooshort++;
106 if (m->m_len < sizeof(struct sadb_msg)) {
107 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
108 pfkeystat.out_nomem++;
114 if ((m->m_flags & M_PKTHDR) == 0)
115 panic("key_output: not M_PKTHDR ??");
117 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
119 msg = mtod(m, struct sadb_msg *);
120 pfkeystat.out_msgtype[msg->sadb_msg_type]++;
121 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
122 pfkeystat.out_invlen++;
129 error = key_parse(m, so);
139 * send message to the socket.
142 key_sendup0(rp, m, promisc)
150 struct sadb_msg *pmsg;
152 M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT);
153 if (m && m->m_len < sizeof(struct sadb_msg))
154 m = m_pullup(m, sizeof(struct sadb_msg));
156 pfkeystat.in_nomem++;
160 m->m_pkthdr.len += sizeof(*pmsg);
162 pmsg = mtod(m, struct sadb_msg *);
163 bzero(pmsg, sizeof(*pmsg));
164 pmsg->sadb_msg_version = PF_KEY_V2;
165 pmsg->sadb_msg_type = SADB_X_PROMISC;
166 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
169 pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
172 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
174 pfkeystat.in_nomem++;
179 sorwakeup(rp->rcb_socket);
183 /* XXX this interface should be obsoleted. */
185 key_sendup(so, msg, len, target)
187 struct sadb_msg *msg;
189 int target; /*target of the resulting message*/
191 struct mbuf *m, *n, *mprev;
195 if (so == 0 || msg == 0)
196 panic("key_sendup: NULL pointer was passed.\n");
198 KEYDEBUG(KEYDEBUG_KEY_DUMP,
199 printf("key_sendup: \n");
203 * we increment statistics here, just in case we have ENOBUFS
206 pfkeystat.in_total++;
207 pfkeystat.in_bytes += len;
208 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
211 * Get mbuf chain whenever possible (not clusters),
212 * to save socket buffer. We'll be generating many SADB_ACQUIRE
213 * messages to listening key sockets. If we simply allocate clusters,
214 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
215 * sbspace() computes # of actual data bytes AND mbuf region.
217 * TODO: SADB_ACQUIRE filters should be implemented.
223 MGETHDR(n, M_DONTWAIT, MT_DATA);
226 MGET(n, M_DONTWAIT, MT_DATA);
230 pfkeystat.in_nomem++;
233 if (tlen >= MCLBYTES) { /*XXX better threshold? */
234 MCLGET(n, M_DONTWAIT);
235 if ((n->m_flags & M_EXT) == 0) {
238 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 pfkeystat.in_total--;
262 pfkeystat.in_bytes -= len;
263 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)
282 panic("key_sendup_mbuf: NULL pointer was passed.\n");
283 if (so == NULL && target == KEY_SENDUP_ONE)
284 panic("key_sendup_mbuf: NULL pointer was passed.\n");
286 pfkeystat.in_total++;
287 pfkeystat.in_bytes += m->m_pkthdr.len;
288 if (m->m_len < sizeof(struct sadb_msg)) {
290 m = m_pullup(m, sizeof(struct sadb_msg));
292 pfkeystat.in_nomem++;
296 /* don't bother pulling it up just for stats */
299 if (m->m_len >= sizeof(struct sadb_msg)) {
300 struct sadb_msg *msg;
301 msg = mtod(m, struct sadb_msg *);
302 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
305 LIST_FOREACH(rp, &rawcb_list, list)
307 if (rp->rcb_proto.sp_family != PF_KEY)
309 if (rp->rcb_proto.sp_protocol
310 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
314 kp = (struct keycb *)rp;
317 * If you are in promiscuous mode, and when you get broadcasted
318 * reply, you'll get two PF_KEY messages.
319 * (based on pf_key@inner.net message on 14 Oct 1998)
321 if (((struct keycb *)rp)->kp_promisc) {
322 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
323 (void)key_sendup0(rp, n, 1);
328 /* the exact target will be processed later */
329 if (so && sotorawcb(so) == rp)
335 /* the statement has no effect */
336 if (so && sotorawcb(so) == rp)
342 case KEY_SENDUP_REGISTERED:
343 if (kp->kp_registered)
347 pfkeystat.in_msgtarget[target]++;
352 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
354 pfkeystat.in_nomem++;
358 if ((error = key_sendup0(rp, n, 0)) != 0) {
367 error = key_sendup0(sotorawcb(so), m, 0);
378 * derived from net/rtsock.c:rts_abort()
381 key_abort(struct socket *so)
385 error = raw_usrreqs.pru_abort(so);
392 * derived from net/rtsock.c:rts_attach()
395 key_attach(struct socket *so, int proto, struct proc *p)
400 if (sotorawcb(so) != 0)
401 return EISCONN; /* XXX panic? */
402 kp = (struct keycb *)malloc(sizeof *kp, M_PCB, M_WAITOK); /* XXX */
405 bzero(kp, sizeof *kp);
408 * The splnet() is necessary to block protocols from sending
409 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
410 * this PCB is extant but incompletely initialized.
411 * Probably we should try to do more of this work beforehand and
415 so->so_pcb = (caddr_t)kp;
416 error = raw_usrreqs.pru_attach(so, proto, p);
417 kp = (struct keycb *)sotorawcb(so);
420 so->so_pcb = (caddr_t) 0;
425 kp->kp_promisc = kp->kp_registered = 0;
427 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
430 kp->kp_raw.rcb_laddr = &key_src;
431 kp->kp_raw.rcb_faddr = &key_dst;
433 so->so_options |= SO_USELOOPBACK;
441 * derived from net/rtsock.c:rts_bind()
444 key_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
448 error = raw_usrreqs.pru_bind(so, nam, p); /* xxx just EINVAL */
455 * derived from net/rtsock.c:rts_connect()
458 key_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
462 error = raw_usrreqs.pru_connect(so, nam, p); /* XXX just EINVAL */
469 * derived from net/rtsock.c:rts_detach()
472 key_detach(struct socket *so)
474 struct keycb *kp = (struct keycb *)sotorawcb(so);
479 if (kp->kp_raw.rcb_proto.sp_protocol
480 == PF_KEY) /* XXX: AF_KEY */
486 error = raw_usrreqs.pru_detach(so);
493 * derived from net/rtsock.c:key_disconnect()
496 key_disconnect(struct socket *so)
500 error = raw_usrreqs.pru_disconnect(so);
507 * derived from net/rtsock.c:rts_peeraddr()
510 key_peeraddr(struct socket *so, struct sockaddr **nam)
514 error = raw_usrreqs.pru_peeraddr(so, nam);
521 * derived from net/rtsock.c:rts_send()
524 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
525 struct mbuf *control, struct proc *p)
529 error = raw_usrreqs.pru_send(so, flags, m, nam, control, p);
536 * derived from net/rtsock.c:rts_shutdown()
539 key_shutdown(struct socket *so)
543 error = raw_usrreqs.pru_shutdown(so);
550 * derived from net/rtsock.c:rts_sockaddr()
553 key_sockaddr(struct socket *so, struct sockaddr **nam)
557 error = raw_usrreqs.pru_sockaddr(so, nam);
562 struct pr_usrreqs key_usrreqs = {
563 key_abort, pru_accept_notsupp, key_attach, key_bind,
565 pru_connect2_notsupp, pru_control_notsupp, key_detach,
566 key_disconnect, pru_listen_notsupp, key_peeraddr,
568 pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
569 key_sockaddr, sosend, soreceive, sopoll
573 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
576 * Definitions of protocols supported in the KEY domain.
579 extern struct domain keydomain;
581 struct protosw keysw[] = {
582 { SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
583 0, key_output, raw_ctlinput, 0,
590 struct domain keydomain =
591 { PF_KEY, "key", key_init, 0, 0,
592 keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])] };