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.13 2005/06/10 22:34:50 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(rp, m, promisc)
139 struct sadb_msg *pmsg;
141 M_PREPEND(m, sizeof(struct sadb_msg), MB_DONTWAIT);
142 if (m && m->m_len < sizeof(struct sadb_msg))
143 m = m_pullup(m, sizeof(struct sadb_msg));
145 pfkeystat.in_nomem++;
149 m->m_pkthdr.len += sizeof(*pmsg);
151 pmsg = mtod(m, struct sadb_msg *);
152 bzero(pmsg, sizeof(*pmsg));
153 pmsg->sadb_msg_version = PF_KEY_V2;
154 pmsg->sadb_msg_type = SADB_X_PROMISC;
155 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
158 pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
161 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
163 pfkeystat.in_nomem++;
168 sorwakeup(rp->rcb_socket);
172 /* XXX this interface should be obsoleted. */
174 key_sendup(so, msg, len, target)
176 struct sadb_msg *msg;
178 int target; /*target of the resulting message*/
180 struct mbuf *m, *n, *mprev;
184 if (so == 0 || msg == 0)
185 panic("key_sendup: NULL pointer was passed.\n");
187 KEYDEBUG(KEYDEBUG_KEY_DUMP,
188 printf("key_sendup: \n");
192 * we increment statistics here, just in case we have ENOBUFS
195 pfkeystat.in_total++;
196 pfkeystat.in_bytes += len;
197 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
200 * Get mbuf chain whenever possible (not clusters),
201 * to save socket buffer. We'll be generating many SADB_ACQUIRE
202 * messages to listening key sockets. If we simply allocate clusters,
203 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
204 * sbspace() computes # of actual data bytes AND mbuf region.
206 * TODO: SADB_ACQUIRE filters should be implemented.
212 MGETHDR(n, MB_DONTWAIT, MT_DATA);
215 MGET(n, MB_DONTWAIT, MT_DATA);
219 pfkeystat.in_nomem++;
222 if (tlen >= MCLBYTES) { /*XXX better threshold? */
223 MCLGET(n, MB_DONTWAIT);
224 if ((n->m_flags & M_EXT) == 0) {
227 pfkeystat.in_nomem++;
245 m->m_pkthdr.len = len;
246 m->m_pkthdr.rcvif = NULL;
247 m_copyback(m, 0, len, (caddr_t)msg);
249 /* avoid duplicated statistics */
250 pfkeystat.in_total--;
251 pfkeystat.in_bytes -= len;
252 pfkeystat.in_msgtype[msg->sadb_msg_type]--;
254 return key_sendup_mbuf(so, m, target);
257 /* so can be NULL if target != KEY_SENDUP_ONE */
259 key_sendup_mbuf(so, m, target)
271 panic("key_sendup_mbuf: NULL pointer was passed.\n");
272 if (so == NULL && target == KEY_SENDUP_ONE)
273 panic("key_sendup_mbuf: NULL pointer was passed.\n");
275 pfkeystat.in_total++;
276 pfkeystat.in_bytes += m->m_pkthdr.len;
277 if (m->m_len < sizeof(struct sadb_msg)) {
279 m = m_pullup(m, sizeof(struct sadb_msg));
281 pfkeystat.in_nomem++;
285 /* don't bother pulling it up just for stats */
288 if (m->m_len >= sizeof(struct sadb_msg)) {
289 struct sadb_msg *msg;
290 msg = mtod(m, struct sadb_msg *);
291 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
294 LIST_FOREACH(rp, &rawcb_list, list)
296 if (rp->rcb_proto.sp_family != PF_KEY)
298 if (rp->rcb_proto.sp_protocol
299 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
303 kp = (struct keycb *)rp;
306 * If you are in promiscuous mode, and when you get broadcasted
307 * reply, you'll get two PF_KEY messages.
308 * (based on pf_key@inner.net message on 14 Oct 1998)
310 if (((struct keycb *)rp)->kp_promisc) {
311 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
312 (void)key_sendup0(rp, n, 1);
317 /* the exact target will be processed later */
318 if (so && sotorawcb(so) == rp)
324 /* the statement has no effect */
325 if (so && sotorawcb(so) == rp)
331 case KEY_SENDUP_REGISTERED:
332 if (kp->kp_registered)
336 pfkeystat.in_msgtarget[target]++;
341 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
343 pfkeystat.in_nomem++;
347 if ((error = key_sendup0(rp, n, 0)) != 0) {
356 error = key_sendup0(sotorawcb(so), m, 0);
367 * derived from net/rtsock.c:rts_abort()
370 key_abort(struct socket *so)
375 error = raw_usrreqs.pru_abort(so);
382 * derived from net/rtsock.c:rts_attach()
385 key_attach(struct socket *so, int proto, struct pru_attach_info *ai)
390 if (sotorawcb(so) != 0)
391 return EISCONN; /* XXX panic? */
392 kp = (struct keycb *)malloc(sizeof *kp, M_PCB, M_WAITOK); /* XXX */
395 bzero(kp, sizeof *kp);
398 * The critical section is necessary to block protocols from sending
399 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
400 * this PCB is extant but incompletely initialized.
401 * Probably we should try to do more of this work beforehand and
402 * eliminate the critical section.
405 so->so_pcb = (caddr_t)kp;
406 error = raw_usrreqs.pru_attach(so, proto, ai);
407 kp = (struct keycb *)sotorawcb(so);
410 so->so_pcb = (caddr_t) 0;
415 kp->kp_promisc = kp->kp_registered = 0;
417 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
420 kp->kp_raw.rcb_laddr = &key_src;
421 kp->kp_raw.rcb_faddr = &key_dst;
423 so->so_options |= SO_USELOOPBACK;
431 * derived from net/rtsock.c:rts_bind()
434 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
439 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
446 * derived from net/rtsock.c:rts_connect()
449 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
454 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
461 * derived from net/rtsock.c:rts_detach()
464 key_detach(struct socket *so)
466 struct keycb *kp = (struct keycb *)sotorawcb(so);
471 if (kp->kp_raw.rcb_proto.sp_protocol
472 == PF_KEY) /* XXX: AF_KEY */
478 error = raw_usrreqs.pru_detach(so);
485 * derived from net/rtsock.c:key_disconnect()
488 key_disconnect(struct socket *so)
493 error = raw_usrreqs.pru_disconnect(so);
500 * derived from net/rtsock.c:rts_peeraddr()
503 key_peeraddr(struct socket *so, struct sockaddr **nam)
508 error = raw_usrreqs.pru_peeraddr(so, nam);
515 * derived from net/rtsock.c:rts_send()
518 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
519 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)
539 error = raw_usrreqs.pru_shutdown(so);
546 * derived from net/rtsock.c:rts_sockaddr()
549 key_sockaddr(struct socket *so, struct sockaddr **nam)
554 error = raw_usrreqs.pru_sockaddr(so, nam);
559 struct pr_usrreqs key_usrreqs = {
560 key_abort, pru_accept_notsupp, key_attach, key_bind,
562 pru_connect2_notsupp, pru_control_notsupp, key_detach,
563 key_disconnect, pru_listen_notsupp, key_peeraddr,
565 pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
566 key_sockaddr, sosend, soreceive, sopoll
570 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
573 * Definitions of protocols supported in the KEY domain.
576 extern struct domain keydomain;
578 struct protosw keysw[] = {
579 { SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
580 0, key_output, raw_ctlinput, 0,
587 struct domain keydomain = {
588 PF_KEY, "key", key_init, NULL, NULL,
589 keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])],