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/domain.h>
40 #include <sys/errno.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
44 #include <sys/protosw.h>
45 #include <sys/signalvar.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
51 #include <net/raw_cb.h>
52 #include <net/route.h>
54 #include <net/pfkeyv2.h>
55 #include <netkey/keydb.h>
56 #include <netkey/key.h>
57 #include <netkey/keysock.h>
58 #include <netkey/key_debug.h>
60 #include <machine/stdarg.h>
62 struct sockaddr key_dst = { 2, PF_KEY, };
63 struct sockaddr key_src = { 2, PF_KEY, };
65 static int key_sendup0(struct rawcb *, struct mbuf *, int);
67 struct pfkeystat pfkeystat;
74 key_output(struct mbuf *m, ...)
76 key_output(m, va_alist)
88 so = va_arg(ap, struct socket *);
92 panic("key_output: NULL pointer was passed.\n");
94 pfkeystat.out_total++;
95 pfkeystat.out_bytes += m->m_pkthdr.len;
97 len = m->m_pkthdr.len;
98 if (len < sizeof(struct sadb_msg)) {
99 pfkeystat.out_tooshort++;
104 if (m->m_len < sizeof(struct sadb_msg)) {
105 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
106 pfkeystat.out_nomem++;
112 if ((m->m_flags & M_PKTHDR) == 0)
113 panic("key_output: not M_PKTHDR ??");
115 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
117 msg = mtod(m, struct sadb_msg *);
118 pfkeystat.out_msgtype[msg->sadb_msg_type]++;
119 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
120 pfkeystat.out_invlen++;
127 error = key_parse(m, so);
137 * send message to the socket.
140 key_sendup0(rp, m, promisc)
148 struct sadb_msg *pmsg;
150 M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT);
151 if (m && m->m_len < sizeof(struct sadb_msg))
152 m = m_pullup(m, sizeof(struct sadb_msg));
154 pfkeystat.in_nomem++;
158 m->m_pkthdr.len += sizeof(*pmsg);
160 pmsg = mtod(m, struct sadb_msg *);
161 bzero(pmsg, sizeof(*pmsg));
162 pmsg->sadb_msg_version = PF_KEY_V2;
163 pmsg->sadb_msg_type = SADB_X_PROMISC;
164 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
167 pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
170 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
172 pfkeystat.in_nomem++;
177 sorwakeup(rp->rcb_socket);
181 /* XXX this interface should be obsoleted. */
183 key_sendup(so, msg, len, target)
185 struct sadb_msg *msg;
187 int target; /*target of the resulting message*/
189 struct mbuf *m, *n, *mprev;
193 if (so == 0 || msg == 0)
194 panic("key_sendup: NULL pointer was passed.\n");
196 KEYDEBUG(KEYDEBUG_KEY_DUMP,
197 printf("key_sendup: \n");
201 * we increment statistics here, just in case we have ENOBUFS
204 pfkeystat.in_total++;
205 pfkeystat.in_bytes += len;
206 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
209 * Get mbuf chain whenever possible (not clusters),
210 * to save socket buffer. We'll be generating many SADB_ACQUIRE
211 * messages to listening key sockets. If we simply allocate clusters,
212 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
213 * sbspace() computes # of actual data bytes AND mbuf region.
215 * TODO: SADB_ACQUIRE filters should be implemented.
221 MGETHDR(n, M_DONTWAIT, MT_DATA);
224 MGET(n, M_DONTWAIT, MT_DATA);
228 pfkeystat.in_nomem++;
231 if (tlen >= MCLBYTES) { /*XXX better threshold? */
232 MCLGET(n, M_DONTWAIT);
233 if ((n->m_flags & M_EXT) == 0) {
236 pfkeystat.in_nomem++;
254 m->m_pkthdr.len = len;
255 m->m_pkthdr.rcvif = NULL;
256 m_copyback(m, 0, len, (caddr_t)msg);
258 /* avoid duplicated statistics */
259 pfkeystat.in_total--;
260 pfkeystat.in_bytes -= len;
261 pfkeystat.in_msgtype[msg->sadb_msg_type]--;
263 return key_sendup_mbuf(so, m, target);
266 /* so can be NULL if target != KEY_SENDUP_ONE */
268 key_sendup_mbuf(so, m, target)
280 panic("key_sendup_mbuf: NULL pointer was passed.\n");
281 if (so == NULL && target == KEY_SENDUP_ONE)
282 panic("key_sendup_mbuf: NULL pointer was passed.\n");
284 pfkeystat.in_total++;
285 pfkeystat.in_bytes += m->m_pkthdr.len;
286 if (m->m_len < sizeof(struct sadb_msg)) {
288 m = m_pullup(m, sizeof(struct sadb_msg));
290 pfkeystat.in_nomem++;
294 /* don't bother pulling it up just for stats */
297 if (m->m_len >= sizeof(struct sadb_msg)) {
298 struct sadb_msg *msg;
299 msg = mtod(m, struct sadb_msg *);
300 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
303 LIST_FOREACH(rp, &rawcb_list, list)
305 if (rp->rcb_proto.sp_family != PF_KEY)
307 if (rp->rcb_proto.sp_protocol
308 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
312 kp = (struct keycb *)rp;
315 * If you are in promiscuous mode, and when you get broadcasted
316 * reply, you'll get two PF_KEY messages.
317 * (based on pf_key@inner.net message on 14 Oct 1998)
319 if (((struct keycb *)rp)->kp_promisc) {
320 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
321 (void)key_sendup0(rp, n, 1);
326 /* the exact target will be processed later */
327 if (so && sotorawcb(so) == rp)
333 /* the statement has no effect */
334 if (so && sotorawcb(so) == rp)
340 case KEY_SENDUP_REGISTERED:
341 if (kp->kp_registered)
345 pfkeystat.in_msgtarget[target]++;
350 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
352 pfkeystat.in_nomem++;
356 if ((error = key_sendup0(rp, n, 0)) != 0) {
365 error = key_sendup0(sotorawcb(so), m, 0);
376 * derived from net/rtsock.c:rts_abort()
379 key_abort(struct socket *so)
383 error = raw_usrreqs.pru_abort(so);
390 * derived from net/rtsock.c:rts_attach()
393 key_attach(struct socket *so, int proto, struct thread *td)
398 if (sotorawcb(so) != 0)
399 return EISCONN; /* XXX panic? */
400 kp = (struct keycb *)malloc(sizeof *kp, M_PCB, M_WAITOK); /* XXX */
403 bzero(kp, sizeof *kp);
406 * The splnet() is necessary to block protocols from sending
407 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
408 * this PCB is extant but incompletely initialized.
409 * Probably we should try to do more of this work beforehand and
413 so->so_pcb = (caddr_t)kp;
414 error = raw_usrreqs.pru_attach(so, proto, td);
415 kp = (struct keycb *)sotorawcb(so);
418 so->so_pcb = (caddr_t) 0;
423 kp->kp_promisc = kp->kp_registered = 0;
425 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
428 kp->kp_raw.rcb_laddr = &key_src;
429 kp->kp_raw.rcb_faddr = &key_dst;
431 so->so_options |= SO_USELOOPBACK;
439 * derived from net/rtsock.c:rts_bind()
442 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
446 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
453 * derived from net/rtsock.c:rts_connect()
456 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
460 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
467 * derived from net/rtsock.c:rts_detach()
470 key_detach(struct socket *so)
472 struct keycb *kp = (struct keycb *)sotorawcb(so);
477 if (kp->kp_raw.rcb_proto.sp_protocol
478 == PF_KEY) /* XXX: AF_KEY */
484 error = raw_usrreqs.pru_detach(so);
491 * derived from net/rtsock.c:key_disconnect()
494 key_disconnect(struct socket *so)
498 error = raw_usrreqs.pru_disconnect(so);
505 * derived from net/rtsock.c:rts_peeraddr()
508 key_peeraddr(struct socket *so, struct sockaddr **nam)
512 error = raw_usrreqs.pru_peeraddr(so, nam);
519 * derived from net/rtsock.c:rts_send()
522 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
523 struct mbuf *control, struct thread *td)
527 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
534 * derived from net/rtsock.c:rts_shutdown()
537 key_shutdown(struct socket *so)
541 error = raw_usrreqs.pru_shutdown(so);
548 * derived from net/rtsock.c:rts_sockaddr()
551 key_sockaddr(struct socket *so, struct sockaddr **nam)
555 error = raw_usrreqs.pru_sockaddr(so, nam);
560 struct pr_usrreqs key_usrreqs = {
561 key_abort, pru_accept_notsupp, key_attach, key_bind,
563 pru_connect2_notsupp, pru_control_notsupp, key_detach,
564 key_disconnect, pru_listen_notsupp, key_peeraddr,
566 pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
567 key_sockaddr, sosend, soreceive, sopoll
571 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
574 * Definitions of protocols supported in the KEY domain.
577 extern struct domain keydomain;
579 struct protosw keysw[] = {
580 { SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
581 0, (pr_output_t *)key_output, raw_ctlinput, 0,
588 struct domain keydomain =
589 { PF_KEY, "key", key_init, 0, 0,
590 keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])] };