keysock.c revision 1.30 1 1.30 rtr /* $NetBSD: keysock.c,v 1.30 2014/07/06 03:33:33 rtr Exp $ */
2 1.1 jonathan /* $FreeBSD: src/sys/netipsec/keysock.c,v 1.3.2.1 2003/01/24 05:11:36 sam Exp $ */
3 1.1 jonathan /* $KAME: keysock.c,v 1.25 2001/08/13 20:07:41 itojun Exp $ */
4 1.1 jonathan
5 1.1 jonathan /*
6 1.1 jonathan * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 1.1 jonathan * All rights reserved.
8 1.1 jonathan *
9 1.1 jonathan * Redistribution and use in source and binary forms, with or without
10 1.1 jonathan * modification, are permitted provided that the following conditions
11 1.1 jonathan * are met:
12 1.1 jonathan * 1. Redistributions of source code must retain the above copyright
13 1.1 jonathan * notice, this list of conditions and the following disclaimer.
14 1.1 jonathan * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 jonathan * notice, this list of conditions and the following disclaimer in the
16 1.1 jonathan * documentation and/or other materials provided with the distribution.
17 1.1 jonathan * 3. Neither the name of the project nor the names of its contributors
18 1.1 jonathan * may be used to endorse or promote products derived from this software
19 1.1 jonathan * without specific prior written permission.
20 1.1 jonathan *
21 1.1 jonathan * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 1.1 jonathan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 jonathan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 jonathan * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 1.1 jonathan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 jonathan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 jonathan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 jonathan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 jonathan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 jonathan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 jonathan * SUCH DAMAGE.
32 1.1 jonathan */
33 1.1 jonathan
34 1.1 jonathan #include <sys/cdefs.h>
35 1.30 rtr __KERNEL_RCSID(0, "$NetBSD: keysock.c,v 1.30 2014/07/06 03:33:33 rtr Exp $");
36 1.1 jonathan
37 1.1 jonathan #include "opt_ipsec.h"
38 1.1 jonathan
39 1.1 jonathan /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
40 1.1 jonathan
41 1.1 jonathan #include <sys/types.h>
42 1.1 jonathan #include <sys/param.h>
43 1.1 jonathan #include <sys/domain.h>
44 1.1 jonathan #include <sys/errno.h>
45 1.1 jonathan #include <sys/kernel.h>
46 1.23 rmind #include <sys/kmem.h>
47 1.1 jonathan #include <sys/mbuf.h>
48 1.1 jonathan #include <sys/protosw.h>
49 1.1 jonathan #include <sys/signalvar.h>
50 1.1 jonathan #include <sys/socket.h>
51 1.1 jonathan #include <sys/socketvar.h>
52 1.1 jonathan #include <sys/sysctl.h>
53 1.1 jonathan #include <sys/systm.h>
54 1.1 jonathan
55 1.1 jonathan #include <net/raw_cb.h>
56 1.1 jonathan #include <net/route.h>
57 1.1 jonathan
58 1.1 jonathan #include <net/pfkeyv2.h>
59 1.1 jonathan #include <netipsec/key.h>
60 1.1 jonathan #include <netipsec/keysock.h>
61 1.1 jonathan #include <netipsec/key_debug.h>
62 1.1 jonathan
63 1.1 jonathan #include <netipsec/ipsec_osdep.h>
64 1.15 thorpej #include <netipsec/ipsec_private.h>
65 1.1 jonathan
66 1.1 jonathan typedef int pr_output_t (struct mbuf *, struct socket *);
67 1.1 jonathan
68 1.1 jonathan struct key_cb {
69 1.1 jonathan int key_count;
70 1.1 jonathan int any_count;
71 1.1 jonathan };
72 1.1 jonathan static struct key_cb key_cb;
73 1.1 jonathan
74 1.11 christos static struct sockaddr key_dst = {
75 1.11 christos .sa_len = 2,
76 1.11 christos .sa_family = PF_KEY,
77 1.11 christos };
78 1.11 christos static struct sockaddr key_src = {
79 1.11 christos .sa_len = 2,
80 1.11 christos .sa_family = PF_KEY,
81 1.11 christos };
82 1.1 jonathan
83 1.5 jonathan
84 1.17 dsl static int key_sendup0(struct rawcb *, struct mbuf *, int, int);
85 1.1 jonathan
86 1.19 joerg int key_registered_sb_max = (2048 * MHLEN); /* XXX arbitrary */
87 1.5 jonathan
88 1.1 jonathan /*
89 1.1 jonathan * key_output()
90 1.1 jonathan */
91 1.1 jonathan int
92 1.1 jonathan key_output(struct mbuf *m, ...)
93 1.1 jonathan {
94 1.1 jonathan struct sadb_msg *msg;
95 1.1 jonathan int len, error = 0;
96 1.1 jonathan int s;
97 1.1 jonathan struct socket *so;
98 1.1 jonathan va_list ap;
99 1.1 jonathan
100 1.1 jonathan va_start(ap, m);
101 1.1 jonathan so = va_arg(ap, struct socket *);
102 1.1 jonathan va_end(ap);
103 1.1 jonathan
104 1.1 jonathan if (m == 0)
105 1.8 christos panic("key_output: NULL pointer was passed");
106 1.1 jonathan
107 1.15 thorpej {
108 1.15 thorpej uint64_t *ps = PFKEY_STAT_GETREF();
109 1.15 thorpej ps[PFKEY_STAT_OUT_TOTAL]++;
110 1.15 thorpej ps[PFKEY_STAT_OUT_BYTES] += m->m_pkthdr.len;
111 1.15 thorpej PFKEY_STAT_PUTREF();
112 1.15 thorpej }
113 1.1 jonathan
114 1.1 jonathan len = m->m_pkthdr.len;
115 1.1 jonathan if (len < sizeof(struct sadb_msg)) {
116 1.15 thorpej PFKEY_STATINC(PFKEY_STAT_OUT_TOOSHORT);
117 1.1 jonathan error = EINVAL;
118 1.1 jonathan goto end;
119 1.1 jonathan }
120 1.1 jonathan
121 1.1 jonathan if (m->m_len < sizeof(struct sadb_msg)) {
122 1.1 jonathan if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
123 1.15 thorpej PFKEY_STATINC(PFKEY_STAT_OUT_NOMEM);
124 1.1 jonathan error = ENOBUFS;
125 1.1 jonathan goto end;
126 1.1 jonathan }
127 1.1 jonathan }
128 1.1 jonathan
129 1.1 jonathan if ((m->m_flags & M_PKTHDR) == 0)
130 1.1 jonathan panic("key_output: not M_PKTHDR ??");
131 1.1 jonathan
132 1.1 jonathan KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
133 1.1 jonathan
134 1.1 jonathan msg = mtod(m, struct sadb_msg *);
135 1.15 thorpej PFKEY_STATINC(PFKEY_STAT_OUT_MSGTYPE + msg->sadb_msg_type);
136 1.1 jonathan if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
137 1.15 thorpej PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN);
138 1.1 jonathan error = EINVAL;
139 1.1 jonathan goto end;
140 1.1 jonathan }
141 1.1 jonathan
142 1.1 jonathan /*XXX giant lock*/
143 1.1 jonathan s = splsoftnet();
144 1.1 jonathan error = key_parse(m, so);
145 1.1 jonathan m = NULL;
146 1.1 jonathan splx(s);
147 1.1 jonathan end:
148 1.1 jonathan if (m)
149 1.1 jonathan m_freem(m);
150 1.1 jonathan return error;
151 1.1 jonathan }
152 1.1 jonathan
153 1.1 jonathan /*
154 1.1 jonathan * send message to the socket.
155 1.1 jonathan */
156 1.1 jonathan static int
157 1.11 christos key_sendup0(
158 1.11 christos struct rawcb *rp,
159 1.11 christos struct mbuf *m,
160 1.11 christos int promisc,
161 1.11 christos int sbprio
162 1.11 christos )
163 1.1 jonathan {
164 1.1 jonathan int error;
165 1.5 jonathan int ok;
166 1.1 jonathan
167 1.1 jonathan if (promisc) {
168 1.1 jonathan struct sadb_msg *pmsg;
169 1.1 jonathan
170 1.1 jonathan M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT);
171 1.1 jonathan if (m && m->m_len < sizeof(struct sadb_msg))
172 1.1 jonathan m = m_pullup(m, sizeof(struct sadb_msg));
173 1.1 jonathan if (!m) {
174 1.15 thorpej PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
175 1.1 jonathan return ENOBUFS;
176 1.1 jonathan }
177 1.1 jonathan m->m_pkthdr.len += sizeof(*pmsg);
178 1.1 jonathan
179 1.1 jonathan pmsg = mtod(m, struct sadb_msg *);
180 1.18 cegger memset(pmsg, 0, sizeof(*pmsg));
181 1.1 jonathan pmsg->sadb_msg_version = PF_KEY_V2;
182 1.1 jonathan pmsg->sadb_msg_type = SADB_X_PROMISC;
183 1.1 jonathan pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
184 1.1 jonathan /* pid and seq? */
185 1.1 jonathan
186 1.15 thorpej PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + pmsg->sadb_msg_type);
187 1.1 jonathan }
188 1.1 jonathan
189 1.5 jonathan if (sbprio == 0)
190 1.5 jonathan ok = sbappendaddr(&rp->rcb_socket->so_rcv,
191 1.5 jonathan (struct sockaddr *)&key_src, m, NULL);
192 1.5 jonathan else
193 1.5 jonathan ok = sbappendaddrchain(&rp->rcb_socket->so_rcv,
194 1.5 jonathan (struct sockaddr *)&key_src, m, sbprio);
195 1.5 jonathan
196 1.5 jonathan if (!ok) {
197 1.15 thorpej PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
198 1.1 jonathan m_freem(m);
199 1.1 jonathan error = ENOBUFS;
200 1.1 jonathan } else
201 1.1 jonathan error = 0;
202 1.1 jonathan sorwakeup(rp->rcb_socket);
203 1.1 jonathan return error;
204 1.1 jonathan }
205 1.1 jonathan
206 1.1 jonathan /* XXX this interface should be obsoleted. */
207 1.1 jonathan int
208 1.14 degroote key_sendup(struct socket *so, struct sadb_msg *msg, u_int len,
209 1.14 degroote int target) /*target of the resulting message*/
210 1.1 jonathan {
211 1.1 jonathan struct mbuf *m, *n, *mprev;
212 1.1 jonathan int tlen;
213 1.1 jonathan
214 1.1 jonathan /* sanity check */
215 1.1 jonathan if (so == 0 || msg == 0)
216 1.8 christos panic("key_sendup: NULL pointer was passed");
217 1.1 jonathan
218 1.1 jonathan KEYDEBUG(KEYDEBUG_KEY_DUMP,
219 1.1 jonathan printf("key_sendup: \n");
220 1.1 jonathan kdebug_sadb(msg));
221 1.1 jonathan
222 1.1 jonathan /*
223 1.1 jonathan * we increment statistics here, just in case we have ENOBUFS
224 1.1 jonathan * in this function.
225 1.1 jonathan */
226 1.15 thorpej {
227 1.15 thorpej uint64_t *ps = PFKEY_STAT_GETREF();
228 1.15 thorpej ps[PFKEY_STAT_IN_TOTAL]++;
229 1.15 thorpej ps[PFKEY_STAT_IN_BYTES] += len;
230 1.15 thorpej ps[PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type]++;
231 1.15 thorpej PFKEY_STAT_PUTREF();
232 1.15 thorpej }
233 1.1 jonathan
234 1.1 jonathan /*
235 1.1 jonathan * Get mbuf chain whenever possible (not clusters),
236 1.1 jonathan * to save socket buffer. We'll be generating many SADB_ACQUIRE
237 1.1 jonathan * messages to listening key sockets. If we simply allocate clusters,
238 1.1 jonathan * sbappendaddr() will raise ENOBUFS due to too little sbspace().
239 1.1 jonathan * sbspace() computes # of actual data bytes AND mbuf region.
240 1.1 jonathan *
241 1.1 jonathan * TODO: SADB_ACQUIRE filters should be implemented.
242 1.1 jonathan */
243 1.1 jonathan tlen = len;
244 1.1 jonathan m = mprev = NULL;
245 1.1 jonathan while (tlen > 0) {
246 1.27 christos int mlen;
247 1.1 jonathan if (tlen == len) {
248 1.1 jonathan MGETHDR(n, M_DONTWAIT, MT_DATA);
249 1.27 christos mlen = MHLEN;
250 1.1 jonathan } else {
251 1.1 jonathan MGET(n, M_DONTWAIT, MT_DATA);
252 1.27 christos mlen = MLEN;
253 1.1 jonathan }
254 1.1 jonathan if (!n) {
255 1.15 thorpej PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
256 1.1 jonathan return ENOBUFS;
257 1.1 jonathan }
258 1.27 christos n->m_len = mlen;
259 1.1 jonathan if (tlen >= MCLBYTES) { /*XXX better threshold? */
260 1.1 jonathan MCLGET(n, M_DONTWAIT);
261 1.1 jonathan if ((n->m_flags & M_EXT) == 0) {
262 1.1 jonathan m_free(n);
263 1.1 jonathan m_freem(m);
264 1.15 thorpej PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
265 1.1 jonathan return ENOBUFS;
266 1.1 jonathan }
267 1.1 jonathan n->m_len = MCLBYTES;
268 1.1 jonathan }
269 1.1 jonathan
270 1.1 jonathan if (tlen < n->m_len)
271 1.1 jonathan n->m_len = tlen;
272 1.1 jonathan n->m_next = NULL;
273 1.1 jonathan if (m == NULL)
274 1.1 jonathan m = mprev = n;
275 1.1 jonathan else {
276 1.1 jonathan mprev->m_next = n;
277 1.1 jonathan mprev = n;
278 1.1 jonathan }
279 1.1 jonathan tlen -= n->m_len;
280 1.1 jonathan n = NULL;
281 1.1 jonathan }
282 1.1 jonathan m->m_pkthdr.len = len;
283 1.1 jonathan m->m_pkthdr.rcvif = NULL;
284 1.13 degroote m_copyback(m, 0, len, msg);
285 1.1 jonathan
286 1.1 jonathan /* avoid duplicated statistics */
287 1.15 thorpej {
288 1.15 thorpej uint64_t *ps = PFKEY_STAT_GETREF();
289 1.15 thorpej ps[PFKEY_STAT_IN_TOTAL]--;
290 1.15 thorpej ps[PFKEY_STAT_IN_BYTES] -= len;
291 1.15 thorpej ps[PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type]--;
292 1.15 thorpej PFKEY_STAT_PUTREF();
293 1.15 thorpej }
294 1.1 jonathan
295 1.1 jonathan return key_sendup_mbuf(so, m, target);
296 1.1 jonathan }
297 1.1 jonathan
298 1.1 jonathan /* so can be NULL if target != KEY_SENDUP_ONE */
299 1.1 jonathan int
300 1.14 degroote key_sendup_mbuf(struct socket *so, struct mbuf *m,
301 1.14 degroote int target/*, sbprio */)
302 1.1 jonathan {
303 1.1 jonathan struct mbuf *n;
304 1.1 jonathan struct keycb *kp;
305 1.1 jonathan int sendup;
306 1.1 jonathan struct rawcb *rp;
307 1.1 jonathan int error = 0;
308 1.5 jonathan int sbprio = 0; /* XXX should be a parameter */
309 1.1 jonathan
310 1.1 jonathan if (m == NULL)
311 1.8 christos panic("key_sendup_mbuf: NULL pointer was passed");
312 1.1 jonathan if (so == NULL && target == KEY_SENDUP_ONE)
313 1.8 christos panic("key_sendup_mbuf: NULL pointer was passed");
314 1.7 perry
315 1.5 jonathan /*
316 1.5 jonathan * RFC 2367 says ACQUIRE and other kernel-generated messages
317 1.5 jonathan * are special. We treat all KEY_SENDUP_REGISTERED messages
318 1.5 jonathan * as special, delivering them to all registered sockets
319 1.5 jonathan * even if the socket is at or above its so->so_rcv.sb_max limits.
320 1.5 jonathan * The only constraint is that the so_rcv data fall below
321 1.5 jonathan * key_registered_sb_max.
322 1.5 jonathan * Doing that check here avoids reworking every key_sendup_mbuf()
323 1.5 jonathan * in the short term. . The rework will be done after a technical
324 1.5 jonathan * conensus that this approach is appropriate.
325 1.5 jonathan */
326 1.5 jonathan if (target == KEY_SENDUP_REGISTERED) {
327 1.5 jonathan sbprio = SB_PRIO_BESTEFFORT;
328 1.5 jonathan }
329 1.1 jonathan
330 1.15 thorpej {
331 1.15 thorpej uint64_t *ps = PFKEY_STAT_GETREF();
332 1.15 thorpej ps[PFKEY_STAT_IN_TOTAL]++;
333 1.15 thorpej ps[PFKEY_STAT_IN_BYTES] += m->m_pkthdr.len;
334 1.15 thorpej PFKEY_STAT_PUTREF();
335 1.15 thorpej }
336 1.1 jonathan if (m->m_len < sizeof(struct sadb_msg)) {
337 1.1 jonathan #if 1
338 1.1 jonathan m = m_pullup(m, sizeof(struct sadb_msg));
339 1.1 jonathan if (m == NULL) {
340 1.15 thorpej PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
341 1.1 jonathan return ENOBUFS;
342 1.1 jonathan }
343 1.1 jonathan #else
344 1.1 jonathan /* don't bother pulling it up just for stats */
345 1.1 jonathan #endif
346 1.1 jonathan }
347 1.1 jonathan if (m->m_len >= sizeof(struct sadb_msg)) {
348 1.1 jonathan struct sadb_msg *msg;
349 1.1 jonathan msg = mtod(m, struct sadb_msg *);
350 1.15 thorpej PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type);
351 1.1 jonathan }
352 1.1 jonathan
353 1.1 jonathan LIST_FOREACH(rp, &rawcb_list, rcb_list)
354 1.1 jonathan {
355 1.5 jonathan struct socket * kso = rp->rcb_socket;
356 1.1 jonathan if (rp->rcb_proto.sp_family != PF_KEY)
357 1.1 jonathan continue;
358 1.1 jonathan if (rp->rcb_proto.sp_protocol
359 1.1 jonathan && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
360 1.1 jonathan continue;
361 1.1 jonathan }
362 1.1 jonathan
363 1.1 jonathan kp = (struct keycb *)rp;
364 1.1 jonathan
365 1.1 jonathan /*
366 1.1 jonathan * If you are in promiscuous mode, and when you get broadcasted
367 1.1 jonathan * reply, you'll get two PF_KEY messages.
368 1.1 jonathan * (based on pf_key (at) inner.net message on 14 Oct 1998)
369 1.1 jonathan */
370 1.1 jonathan if (((struct keycb *)rp)->kp_promisc) {
371 1.1 jonathan if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
372 1.5 jonathan (void)key_sendup0(rp, n, 1, 0);
373 1.1 jonathan n = NULL;
374 1.1 jonathan }
375 1.1 jonathan }
376 1.1 jonathan
377 1.1 jonathan /* the exact target will be processed later */
378 1.1 jonathan if (so && sotorawcb(so) == rp)
379 1.1 jonathan continue;
380 1.1 jonathan
381 1.1 jonathan sendup = 0;
382 1.1 jonathan switch (target) {
383 1.1 jonathan case KEY_SENDUP_ONE:
384 1.1 jonathan /* the statement has no effect */
385 1.1 jonathan if (so && sotorawcb(so) == rp)
386 1.1 jonathan sendup++;
387 1.1 jonathan break;
388 1.1 jonathan case KEY_SENDUP_ALL:
389 1.1 jonathan sendup++;
390 1.1 jonathan break;
391 1.1 jonathan case KEY_SENDUP_REGISTERED:
392 1.5 jonathan if (kp->kp_registered) {
393 1.5 jonathan if (kso->so_rcv.sb_cc <= key_registered_sb_max)
394 1.5 jonathan sendup++;
395 1.5 jonathan else
396 1.5 jonathan printf("keysock: "
397 1.5 jonathan "registered sendup dropped, "
398 1.5 jonathan "sb_cc %ld max %d\n",
399 1.5 jonathan kso->so_rcv.sb_cc,
400 1.5 jonathan key_registered_sb_max);
401 1.5 jonathan }
402 1.1 jonathan break;
403 1.1 jonathan }
404 1.15 thorpej PFKEY_STATINC(PFKEY_STAT_IN_MSGTARGET + target);
405 1.1 jonathan
406 1.1 jonathan if (!sendup)
407 1.1 jonathan continue;
408 1.1 jonathan
409 1.1 jonathan if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
410 1.1 jonathan m_freem(m);
411 1.15 thorpej PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
412 1.1 jonathan return ENOBUFS;
413 1.1 jonathan }
414 1.1 jonathan
415 1.5 jonathan if ((error = key_sendup0(rp, n, 0, 0)) != 0) {
416 1.1 jonathan m_freem(m);
417 1.1 jonathan return error;
418 1.1 jonathan }
419 1.1 jonathan
420 1.1 jonathan n = NULL;
421 1.1 jonathan }
422 1.1 jonathan
423 1.5 jonathan /* The 'later' time for processing the exact target has arrived */
424 1.1 jonathan if (so) {
425 1.5 jonathan error = key_sendup0(sotorawcb(so), m, 0, sbprio);
426 1.1 jonathan m = NULL;
427 1.1 jonathan } else {
428 1.1 jonathan error = 0;
429 1.1 jonathan m_freem(m);
430 1.1 jonathan }
431 1.1 jonathan return error;
432 1.1 jonathan }
433 1.1 jonathan
434 1.23 rmind static int
435 1.23 rmind key_attach(struct socket *so, int proto)
436 1.23 rmind {
437 1.23 rmind struct keycb *kp;
438 1.23 rmind int s, error;
439 1.23 rmind
440 1.23 rmind KASSERT(sotorawcb(so) == NULL);
441 1.23 rmind kp = kmem_zalloc(sizeof(*kp), KM_SLEEP);
442 1.25 rmind kp->kp_raw.rcb_len = sizeof(*kp);
443 1.23 rmind so->so_pcb = kp;
444 1.23 rmind
445 1.23 rmind s = splsoftnet();
446 1.23 rmind error = raw_attach(so, proto);
447 1.23 rmind if (error) {
448 1.23 rmind PFKEY_STATINC(PFKEY_STAT_SOCKERR);
449 1.23 rmind kmem_free(kp, sizeof(*kp));
450 1.23 rmind so->so_pcb = NULL;
451 1.23 rmind goto out;
452 1.23 rmind }
453 1.23 rmind
454 1.23 rmind kp->kp_promisc = kp->kp_registered = 0;
455 1.23 rmind
456 1.23 rmind if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
457 1.23 rmind key_cb.key_count++;
458 1.23 rmind key_cb.any_count++;
459 1.23 rmind kp->kp_raw.rcb_laddr = &key_src;
460 1.23 rmind kp->kp_raw.rcb_faddr = &key_dst;
461 1.23 rmind soisconnected(so);
462 1.23 rmind so->so_options |= SO_USELOOPBACK;
463 1.23 rmind out:
464 1.23 rmind KASSERT(solocked(so));
465 1.23 rmind splx(s);
466 1.23 rmind return error;
467 1.23 rmind }
468 1.23 rmind
469 1.23 rmind static void
470 1.23 rmind key_detach(struct socket *so)
471 1.23 rmind {
472 1.23 rmind struct keycb *kp = (struct keycb *)sotorawcb(so);
473 1.23 rmind int s;
474 1.23 rmind
475 1.23 rmind KASSERT(solocked(so));
476 1.23 rmind KASSERT(kp != NULL);
477 1.23 rmind
478 1.23 rmind s = splsoftnet();
479 1.23 rmind if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
480 1.23 rmind key_cb.key_count--;
481 1.23 rmind key_cb.any_count--;
482 1.23 rmind key_freereg(so);
483 1.23 rmind raw_detach(so);
484 1.23 rmind splx(s);
485 1.23 rmind }
486 1.23 rmind
487 1.28 rtr static int
488 1.29 rtr key_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
489 1.28 rtr {
490 1.28 rtr return EOPNOTSUPP;
491 1.28 rtr }
492 1.28 rtr
493 1.30 rtr static int
494 1.30 rtr key_stat(struct socket *so, struct stat *ub)
495 1.30 rtr {
496 1.30 rtr return 0;
497 1.30 rtr }
498 1.30 rtr
499 1.1 jonathan /*
500 1.1 jonathan * key_usrreq()
501 1.1 jonathan * derived from net/rtsock.c:route_usrreq()
502 1.1 jonathan */
503 1.22 rmind static int
504 1.22 rmind key_usrreq(struct socket *so, int req,struct mbuf *m, struct mbuf *nam,
505 1.22 rmind struct mbuf *control, struct lwp *l)
506 1.1 jonathan {
507 1.23 rmind int s, error = 0;
508 1.23 rmind
509 1.23 rmind KASSERT(req != PRU_ATTACH);
510 1.23 rmind KASSERT(req != PRU_DETACH);
511 1.28 rtr KASSERT(req != PRU_CONTROL);
512 1.30 rtr KASSERT(req != PRU_SENSE);
513 1.1 jonathan
514 1.1 jonathan s = splsoftnet();
515 1.9 christos error = raw_usrreq(so, req, m, nam, control, l);
516 1.1 jonathan m = control = NULL; /* reclaimed in raw_usrreq */
517 1.1 jonathan splx(s);
518 1.23 rmind
519 1.23 rmind return error;
520 1.1 jonathan }
521 1.1 jonathan
522 1.1 jonathan /*
523 1.1 jonathan * Definitions of protocols supported in the KEY domain.
524 1.1 jonathan */
525 1.1 jonathan
526 1.6 matt DOMAIN_DEFINE(keydomain);
527 1.1 jonathan
528 1.24 rmind PR_WRAP_USRREQS(key)
529 1.24 rmind #define key_attach key_attach_wrapper
530 1.24 rmind #define key_detach key_detach_wrapper
531 1.28 rtr #define key_ioctl key_ioctl_wrapper
532 1.30 rtr #define key_stat key_stat_wrapper
533 1.22 rmind #define key_usrreq key_usrreq_wrapper
534 1.22 rmind
535 1.22 rmind const struct pr_usrreqs key_usrreqs = {
536 1.23 rmind .pr_attach = key_attach,
537 1.23 rmind .pr_detach = key_detach,
538 1.28 rtr .pr_ioctl = key_ioctl,
539 1.30 rtr .pr_stat = key_stat,
540 1.22 rmind .pr_generic = key_usrreq,
541 1.22 rmind };
542 1.22 rmind
543 1.10 matt const struct protosw keysw[] = {
544 1.10 matt {
545 1.10 matt .pr_type = SOCK_RAW,
546 1.10 matt .pr_domain = &keydomain,
547 1.10 matt .pr_protocol = PF_KEY_V2,
548 1.10 matt .pr_flags = PR_ATOMIC|PR_ADDR,
549 1.10 matt .pr_output = key_output,
550 1.10 matt .pr_ctlinput = raw_ctlinput,
551 1.22 rmind .pr_usrreqs = &key_usrreqs,
552 1.10 matt .pr_init = raw_init,
553 1.10 matt }
554 1.1 jonathan };
555 1.1 jonathan
556 1.10 matt struct domain keydomain = {
557 1.10 matt .dom_family = PF_KEY,
558 1.10 matt .dom_name = "key",
559 1.10 matt .dom_init = key_init,
560 1.10 matt .dom_protosw = keysw,
561 1.10 matt .dom_protoswNPROTOSW = &keysw[__arraycount(keysw)],
562 1.10 matt };
563