Import mdocml-1.9.14:
[netbsd-mini2440.git] / sys / netkey / keysock.c
blobefb673123dc7873cc4aa6cc1470c56e39d0e3686
1 /* $NetBSD: keysock.c,v 1.52 2009/03/18 16:00:23 cegger Exp $ */
2 /* $KAME: keysock.c,v 1.32 2003/08/22 05:45:08 itojun Exp $ */
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
30 * SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: keysock.c,v 1.52 2009/03/18 16:00:23 cegger Exp $");
36 #include "opt_inet.h"
38 /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/mbuf.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>
49 #include <sys/proc.h>
50 #include <sys/queue.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>
61 #include <netkey/key_private.h>
63 #include <machine/stdarg.h>
65 struct sockaddr key_dst = { .sa_len = 2, .sa_family = PF_KEY, };
66 struct sockaddr key_src = { .sa_len = 2, .sa_family = PF_KEY, };
68 static int key_receive(struct socket *, struct mbuf **, struct uio *,
69 struct mbuf **, struct mbuf **, int *);
71 static int key_sendup0(struct rawcb *, struct mbuf *, int, int);
73 static int
74 key_receive(struct socket *so, struct mbuf **paddr, struct uio *uio,
75 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
77 struct rawcb *rp = sotorawcb(so);
78 struct keycb *kp = (struct keycb *)rp;
79 int error;
81 error = (*kp->kp_receive)(so, paddr, uio, mp0, controlp, flagsp);
84 * now we might have enough receive buffer space.
85 * pull packets from kp_queue as many as possible.
87 mutex_enter(softnet_lock);
88 KERNEL_LOCK(1, NULL);
89 while (/*CONSTCOND*/ 1) {
90 struct mbuf *m;
92 m = kp->kp_queue;
93 if (m == NULL || sbspace(&so->so_rcv) < m->m_pkthdr.len)
94 break;
95 kp->kp_queue = m->m_nextpkt;
96 m->m_nextpkt = NULL; /* safety */
97 if (key_sendup0(rp, m, 0, 1))
98 break;
100 KERNEL_UNLOCK_ONE(NULL);
101 mutex_exit(softnet_lock);
103 return error;
107 * key_usrreq()
108 * derived from net/rtsock.c:route_usrreq()
111 key_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, struct mbuf *control, struct lwp *l)
113 int error = 0;
114 struct keycb *kp = (struct keycb *)sotorawcb(so);
115 int s;
117 s = splsoftnet();
118 if (req == PRU_ATTACH) {
119 sosetlock(so);
120 kp = (struct keycb *)malloc(sizeof(*kp), M_PCB,
121 M_WAITOK|M_ZERO);
122 so->so_pcb = (void *)kp;
123 kp->kp_receive = so->so_receive;
124 so->so_receive = key_receive;
126 if (req == PRU_DETACH && kp) {
127 int af = kp->kp_raw.rcb_proto.sp_protocol;
128 struct mbuf *n;
130 if (af == PF_KEY)
131 key_cb.key_count--;
132 key_cb.any_count--;
134 key_freereg(so);
136 while (kp->kp_queue) {
137 n = kp->kp_queue->m_nextpkt;
138 kp->kp_queue->m_nextpkt = NULL;
139 m_freem(kp->kp_queue);
140 kp->kp_queue = n;
144 error = raw_usrreq(so, req, m, nam, control, l);
145 m = control = NULL; /* reclaimed in raw_usrreq */
146 kp = (struct keycb *)sotorawcb(so);
147 if (req == PRU_ATTACH && kp) {
148 int af = kp->kp_raw.rcb_proto.sp_protocol;
149 if (error) {
150 PFKEY_STATINC(PFKEY_STAT_SOCKERR);
151 free((void *)kp, M_PCB);
152 so->so_pcb = (void *) 0;
153 splx(s);
154 return (error);
157 kp->kp_promisc = kp->kp_registered = 0;
159 if (af == PF_KEY)
160 key_cb.key_count++;
161 key_cb.any_count++;
162 kp->kp_raw.rcb_laddr = &key_src;
163 kp->kp_raw.rcb_faddr = &key_dst;
164 soisconnected(so);
165 so->so_options |= SO_USELOOPBACK;
167 splx(s);
168 return (error);
172 * key_output()
175 key_output(struct mbuf *m, ...)
177 struct sadb_msg *msg;
178 int len, error = 0;
179 int s;
180 struct socket *so;
181 va_list ap;
183 va_start(ap, m);
184 so = va_arg(ap, struct socket *);
185 va_end(ap);
187 if (m == 0)
188 panic("key_output: NULL pointer was passed.");
191 uint64_t *ps = PFKEY_STAT_GETREF();
192 ps[PFKEY_STAT_OUT_TOTAL]++;
193 ps[PFKEY_STAT_OUT_BYTES] += m->m_pkthdr.len;
194 PFKEY_STAT_PUTREF();
197 len = m->m_pkthdr.len;
198 if (len < sizeof(struct sadb_msg)) {
199 PFKEY_STATINC(PFKEY_STAT_OUT_TOOSHORT);
200 error = EINVAL;
201 goto end;
204 if (m->m_len < sizeof(struct sadb_msg)) {
205 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
206 PFKEY_STATINC(PFKEY_STAT_OUT_NOMEM);
207 error = ENOBUFS;
208 goto end;
212 if ((m->m_flags & M_PKTHDR) == 0)
213 panic("key_output: not M_PKTHDR ??");
215 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
217 msg = mtod(m, struct sadb_msg *);
218 PFKEY_STATINC(PFKEY_STAT_OUT_MSGTYPE + msg->sadb_msg_type);
219 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
220 PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN);
221 error = EINVAL;
222 goto end;
225 /*XXX giant lock*/
226 s = splsoftnet();
227 error = key_parse(m, so);
228 m = NULL;
229 splx(s);
230 end:
231 if (m)
232 m_freem(m);
233 return error;
237 * send message to the socket.
239 static int
240 key_sendup0(struct rawcb *rp, struct mbuf *m, int promisc, int canwait)
242 struct keycb *kp = (struct keycb *)rp;
243 struct mbuf *n;
244 int error = 0;
246 if (promisc) {
247 struct sadb_msg *pmsg;
249 M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT);
250 if (m && m->m_len < sizeof(struct sadb_msg))
251 m = m_pullup(m, sizeof(struct sadb_msg));
252 if (!m) {
253 PFKEY_STATINC(PFKEY_STAT_OUT_NOMEM);
254 return ENOBUFS;
256 m->m_pkthdr.len += sizeof(*pmsg);
258 pmsg = mtod(m, struct sadb_msg *);
259 memset(pmsg, 0, sizeof(*pmsg));
260 pmsg->sadb_msg_version = PF_KEY_V2;
261 pmsg->sadb_msg_type = SADB_X_PROMISC;
262 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
263 /* pid and seq? */
265 PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + pmsg->sadb_msg_type);
268 if (canwait) {
269 if (kp->kp_queue) {
270 for (n = kp->kp_queue; n && n->m_nextpkt;
271 n = n->m_nextpkt)
273 n->m_nextpkt = m;
274 m = kp->kp_queue;
275 kp->kp_queue = NULL;
276 } else
277 m->m_nextpkt = NULL; /* just for safety */
278 } else
279 m->m_nextpkt = NULL;
281 for (; m && error == 0; m = n) {
282 n = m->m_nextpkt;
284 if (canwait &&
285 sbspace(&rp->rcb_socket->so_rcv) < m->m_pkthdr.len) {
286 error = EAGAIN;
287 goto recovery;
290 m->m_nextpkt = NULL;
292 if (!sbappendaddr(&rp->rcb_socket->so_rcv,
293 (struct sockaddr *)&key_src, m, NULL)) {
294 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
295 error = ENOBUFS;
296 goto recovery;
297 } else {
298 sorwakeup(rp->rcb_socket);
299 error = 0;
302 return (error);
304 recovery:
305 if (kp->kp_queue) {
307 * kp_queue != NULL implies !canwait.
309 KASSERT(!canwait);
310 KASSERT(m->m_nextpkt == NULL);
312 * insert m to the head of queue, as normally mbuf on the queue
313 * is less important than others.
315 if (m) {
316 m->m_nextpkt = kp->kp_queue;
317 kp->kp_queue = m;
319 } else {
320 /* recover the queue */
321 if (!m) {
322 /* first ENOBUFS case */
323 kp->kp_queue = n;
324 } else {
325 kp->kp_queue = m;
326 m->m_nextpkt = n;
329 return (error);
332 /* so can be NULL if target != KEY_SENDUP_ONE */
334 key_sendup_mbuf(struct socket *so, struct mbuf *m, int target)
336 struct mbuf *n;
337 struct keycb *kp;
338 int sendup;
339 struct rawcb *rp;
340 int error = 0;
341 int canwait;
343 if (m == NULL)
344 panic("key_sendup_mbuf: NULL pointer was passed.");
345 if (so == NULL && target == KEY_SENDUP_ONE)
346 panic("key_sendup_mbuf: NULL pointer was passed.");
348 canwait = target & KEY_SENDUP_CANWAIT;
349 target &= ~KEY_SENDUP_CANWAIT;
352 uint64_t *ps = PFKEY_STAT_GETREF();
353 ps[PFKEY_STAT_IN_TOTAL]++;
354 ps[PFKEY_STAT_IN_BYTES] += m->m_pkthdr.len;
355 PFKEY_STAT_PUTREF();
357 if (m->m_len < sizeof(struct sadb_msg)) {
358 m = m_pullup(m, sizeof(struct sadb_msg));
359 if (m == NULL) {
360 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
361 return ENOBUFS;
364 if (m->m_len >= sizeof(struct sadb_msg)) {
365 struct sadb_msg *msg;
366 msg = mtod(m, struct sadb_msg *);
367 PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type);
370 for (rp = rawcb.lh_first; rp; rp = rp->rcb_list.le_next)
372 if (rp->rcb_proto.sp_family != PF_KEY)
373 continue;
374 if (rp->rcb_proto.sp_protocol &&
375 rp->rcb_proto.sp_protocol != PF_KEY_V2) {
376 continue;
379 kp = (struct keycb *)rp;
382 * If you are in promiscuous mode, and when you get broadcasted
383 * reply, you'll get two PF_KEY messages.
384 * (based on pf_key@inner.net message on 14 Oct 1998)
386 if (((struct keycb *)rp)->kp_promisc) {
387 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
388 (void)key_sendup0(rp, n, 1, canwait);
389 n = NULL;
393 /* the exact target will be processed later */
394 if (so && sotorawcb(so) == rp)
395 continue;
397 sendup = 0;
398 switch (target) {
399 case KEY_SENDUP_ONE:
400 /* the statement has no effect */
401 if (so && sotorawcb(so) == rp)
402 sendup++;
403 break;
404 case KEY_SENDUP_ALL:
405 sendup++;
406 break;
407 case KEY_SENDUP_REGISTERED:
408 if (kp->kp_registered)
409 sendup++;
410 break;
412 PFKEY_STATINC(PFKEY_STAT_IN_MSGTARGET + target);
414 if (!sendup)
415 continue;
417 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
418 m_freem(m);
419 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
420 return ENOBUFS;
424 * ignore error even if queue is full. PF_KEY does not
425 * guarantee the delivery of the message.
426 * this is important when target == KEY_SENDUP_ALL.
428 key_sendup0(rp, n, 0, canwait);
430 n = NULL;
433 if (so) {
434 error = key_sendup0(sotorawcb(so), m, 0, canwait);
435 m = NULL;
436 } else {
437 error = 0;
438 m_freem(m);
440 return error;
445 * Definitions of protocols supported in the KEY domain.
448 DOMAIN_DEFINE(keydomain);
450 PR_WRAP_USRREQ(key_usrreq)
452 #define key_usrreq key_usrreq_wrapper
454 const struct protosw keysw[] = {
455 { .pr_type = SOCK_RAW,
456 .pr_domain = &keydomain,
457 .pr_protocol = PF_KEY_V2,
458 .pr_flags = PR_ATOMIC|PR_ADDR,
459 .pr_input = 0,
460 .pr_output = key_output,
461 .pr_ctlinput = raw_ctlinput,
462 .pr_ctloutput = 0,
463 .pr_usrreq = key_usrreq,
464 .pr_init = raw_init,
465 .pr_fasttimo = 0,
466 .pr_slowtimo = 0,
467 .pr_drain = 0,
471 struct domain keydomain = {
472 .dom_family = PF_KEY,
473 .dom_name = "key",
474 .dom_init = key_init,
475 .dom_protosw = keysw,
476 .dom_protoswNPROTOSW = &keysw[sizeof(keysw)/sizeof(keysw[0])],