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.7 2003/11/09 02:22:37 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>
61 #include "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 (struct rawcb *, struct mbuf *, int);
70 struct pfkeystat pfkeystat;
76 key_output(struct mbuf *m, ...)
85 so = __va_arg(ap, struct socket *);
89 panic("key_output: NULL pointer was passed.\n");
91 pfkeystat.out_total++;
92 pfkeystat.out_bytes += m->m_pkthdr.len;
94 len = m->m_pkthdr.len;
95 if (len < sizeof(struct sadb_msg)) {
96 pfkeystat.out_tooshort++;
101 if (m->m_len < sizeof(struct sadb_msg)) {
102 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
103 pfkeystat.out_nomem++;
109 if ((m->m_flags & M_PKTHDR) == 0)
110 panic("key_output: not M_PKTHDR ??");
112 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
114 msg = mtod(m, struct sadb_msg *);
115 pfkeystat.out_msgtype[msg->sadb_msg_type]++;
116 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
117 pfkeystat.out_invlen++;
124 error = key_parse(m, so);
134 * send message to the socket.
137 key_sendup0(rp, m, promisc)
145 struct sadb_msg *pmsg;
147 M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT);
148 if (m && m->m_len < sizeof(struct sadb_msg))
149 m = m_pullup(m, sizeof(struct sadb_msg));
151 pfkeystat.in_nomem++;
155 m->m_pkthdr.len += sizeof(*pmsg);
157 pmsg = mtod(m, struct sadb_msg *);
158 bzero(pmsg, sizeof(*pmsg));
159 pmsg->sadb_msg_version = PF_KEY_V2;
160 pmsg->sadb_msg_type = SADB_X_PROMISC;
161 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
164 pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
167 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
169 pfkeystat.in_nomem++;
174 sorwakeup(rp->rcb_socket);
178 /* XXX this interface should be obsoleted. */
180 key_sendup(so, msg, len, target)
182 struct sadb_msg *msg;
184 int target; /*target of the resulting message*/
186 struct mbuf *m, *n, *mprev;
190 if (so == 0 || msg == 0)
191 panic("key_sendup: NULL pointer was passed.\n");
193 KEYDEBUG(KEYDEBUG_KEY_DUMP,
194 printf("key_sendup: \n");
198 * we increment statistics here, just in case we have ENOBUFS
201 pfkeystat.in_total++;
202 pfkeystat.in_bytes += len;
203 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
206 * Get mbuf chain whenever possible (not clusters),
207 * to save socket buffer. We'll be generating many SADB_ACQUIRE
208 * messages to listening key sockets. If we simply allocate clusters,
209 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
210 * sbspace() computes # of actual data bytes AND mbuf region.
212 * TODO: SADB_ACQUIRE filters should be implemented.
218 MGETHDR(n, M_DONTWAIT, MT_DATA);
221 MGET(n, M_DONTWAIT, MT_DATA);
225 pfkeystat.in_nomem++;
228 if (tlen >= MCLBYTES) { /*XXX better threshold? */
229 MCLGET(n, M_DONTWAIT);
230 if ((n->m_flags & M_EXT) == 0) {
233 pfkeystat.in_nomem++;
251 m->m_pkthdr.len = len;
252 m->m_pkthdr.rcvif = NULL;
253 m_copyback(m, 0, len, (caddr_t)msg);
255 /* avoid duplicated statistics */
256 pfkeystat.in_total--;
257 pfkeystat.in_bytes -= len;
258 pfkeystat.in_msgtype[msg->sadb_msg_type]--;
260 return key_sendup_mbuf(so, m, target);
263 /* so can be NULL if target != KEY_SENDUP_ONE */
265 key_sendup_mbuf(so, m, target)
277 panic("key_sendup_mbuf: NULL pointer was passed.\n");
278 if (so == NULL && target == KEY_SENDUP_ONE)
279 panic("key_sendup_mbuf: NULL pointer was passed.\n");
281 pfkeystat.in_total++;
282 pfkeystat.in_bytes += m->m_pkthdr.len;
283 if (m->m_len < sizeof(struct sadb_msg)) {
285 m = m_pullup(m, sizeof(struct sadb_msg));
287 pfkeystat.in_nomem++;
291 /* don't bother pulling it up just for stats */
294 if (m->m_len >= sizeof(struct sadb_msg)) {
295 struct sadb_msg *msg;
296 msg = mtod(m, struct sadb_msg *);
297 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
300 LIST_FOREACH(rp, &rawcb_list, list)
302 if (rp->rcb_proto.sp_family != PF_KEY)
304 if (rp->rcb_proto.sp_protocol
305 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
309 kp = (struct keycb *)rp;
312 * If you are in promiscuous mode, and when you get broadcasted
313 * reply, you'll get two PF_KEY messages.
314 * (based on pf_key@inner.net message on 14 Oct 1998)
316 if (((struct keycb *)rp)->kp_promisc) {
317 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
318 (void)key_sendup0(rp, n, 1);
323 /* the exact target will be processed later */
324 if (so && sotorawcb(so) == rp)
330 /* the statement has no effect */
331 if (so && sotorawcb(so) == rp)
337 case KEY_SENDUP_REGISTERED:
338 if (kp->kp_registered)
342 pfkeystat.in_msgtarget[target]++;
347 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
349 pfkeystat.in_nomem++;
353 if ((error = key_sendup0(rp, n, 0)) != 0) {
362 error = key_sendup0(sotorawcb(so), m, 0);
373 * derived from net/rtsock.c:rts_abort()
376 key_abort(struct socket *so)
380 error = raw_usrreqs.pru_abort(so);
387 * derived from net/rtsock.c:rts_attach()
390 key_attach(struct socket *so, int proto, struct thread *td)
395 if (sotorawcb(so) != 0)
396 return EISCONN; /* XXX panic? */
397 kp = (struct keycb *)malloc(sizeof *kp, M_PCB, M_WAITOK); /* XXX */
400 bzero(kp, sizeof *kp);
403 * The splnet() is necessary to block protocols from sending
404 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
405 * this PCB is extant but incompletely initialized.
406 * Probably we should try to do more of this work beforehand and
410 so->so_pcb = (caddr_t)kp;
411 error = raw_usrreqs.pru_attach(so, proto, td);
412 kp = (struct keycb *)sotorawcb(so);
415 so->so_pcb = (caddr_t) 0;
420 kp->kp_promisc = kp->kp_registered = 0;
422 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
425 kp->kp_raw.rcb_laddr = &key_src;
426 kp->kp_raw.rcb_faddr = &key_dst;
428 so->so_options |= SO_USELOOPBACK;
436 * derived from net/rtsock.c:rts_bind()
439 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
443 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
450 * derived from net/rtsock.c:rts_connect()
453 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
457 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
464 * derived from net/rtsock.c:rts_detach()
467 key_detach(struct socket *so)
469 struct keycb *kp = (struct keycb *)sotorawcb(so);
474 if (kp->kp_raw.rcb_proto.sp_protocol
475 == PF_KEY) /* XXX: AF_KEY */
481 error = raw_usrreqs.pru_detach(so);
488 * derived from net/rtsock.c:key_disconnect()
491 key_disconnect(struct socket *so)
495 error = raw_usrreqs.pru_disconnect(so);
502 * derived from net/rtsock.c:rts_peeraddr()
505 key_peeraddr(struct socket *so, struct sockaddr **nam)
509 error = raw_usrreqs.pru_peeraddr(so, nam);
516 * derived from net/rtsock.c:rts_send()
519 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
520 struct mbuf *control, struct thread *td)
524 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
531 * derived from net/rtsock.c:rts_shutdown()
534 key_shutdown(struct socket *so)
538 error = raw_usrreqs.pru_shutdown(so);
545 * derived from net/rtsock.c:rts_sockaddr()
548 key_sockaddr(struct socket *so, struct sockaddr **nam)
552 error = raw_usrreqs.pru_sockaddr(so, nam);
557 struct pr_usrreqs key_usrreqs = {
558 key_abort, pru_accept_notsupp, key_attach, key_bind,
560 pru_connect2_notsupp, pru_control_notsupp, key_detach,
561 key_disconnect, pru_listen_notsupp, key_peeraddr,
563 pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
564 key_sockaddr, sosend, soreceive, sopoll
568 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
571 * Definitions of protocols supported in the KEY domain.
574 extern struct domain keydomain;
576 struct protosw keysw[] = {
577 { SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
578 0, key_output, raw_ctlinput, 0,
585 struct domain keydomain =
586 { PF_KEY, "key", key_init, 0, 0,
587 keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])] };