keysock.c revision 1.14 1 1.14 degroote /* $NetBSD: keysock.c,v 1.14 2007/07/07 18:38:23 degroote 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.14 degroote __KERNEL_RCSID(0, "$NetBSD: keysock.c,v 1.14 2007/07/07 18:38:23 degroote 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.1 jonathan #include <sys/malloc.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.1 jonathan
65 1.1 jonathan #include <machine/stdarg.h>
66 1.1 jonathan
67 1.1 jonathan typedef int pr_output_t (struct mbuf *, struct socket *);
68 1.1 jonathan
69 1.1 jonathan struct key_cb {
70 1.1 jonathan int key_count;
71 1.1 jonathan int any_count;
72 1.1 jonathan };
73 1.1 jonathan static struct key_cb key_cb;
74 1.1 jonathan
75 1.11 christos static struct sockaddr key_dst = {
76 1.11 christos .sa_len = 2,
77 1.11 christos .sa_family = PF_KEY,
78 1.11 christos };
79 1.11 christos static struct sockaddr key_src = {
80 1.11 christos .sa_len = 2,
81 1.11 christos .sa_family = PF_KEY,
82 1.11 christos };
83 1.1 jonathan
84 1.5 jonathan
85 1.5 jonathan static int key_sendup0 __P((struct rawcb *, struct mbuf *, int, int));
86 1.1 jonathan
87 1.1 jonathan struct pfkeystat pfkeystat;
88 1.1 jonathan
89 1.5 jonathan int key_registered_sb_max = (NMBCLUSTERS * MHLEN); /* XXX arbitrary */
90 1.5 jonathan
91 1.5 jonathan /* XXX sysctl */
92 1.5 jonathan #ifdef __FreeBSD__
93 1.7 perry SYSCTL_INT(_net_key, OID_AUTO, registered_sbmax, CTLFLAG_RD,
94 1.5 jonathan &key_registered_sb_max , 0, "Maximum kernel-to-user PFKEY datagram size");
95 1.5 jonathan #endif
96 1.5 jonathan
97 1.1 jonathan /*
98 1.1 jonathan * key_output()
99 1.1 jonathan */
100 1.1 jonathan int
101 1.1 jonathan key_output(struct mbuf *m, ...)
102 1.1 jonathan {
103 1.1 jonathan struct sadb_msg *msg;
104 1.1 jonathan int len, error = 0;
105 1.1 jonathan int s;
106 1.1 jonathan struct socket *so;
107 1.1 jonathan va_list ap;
108 1.1 jonathan
109 1.1 jonathan va_start(ap, m);
110 1.1 jonathan so = va_arg(ap, struct socket *);
111 1.1 jonathan va_end(ap);
112 1.1 jonathan
113 1.1 jonathan if (m == 0)
114 1.8 christos panic("key_output: NULL pointer was passed");
115 1.1 jonathan
116 1.1 jonathan pfkeystat.out_total++;
117 1.1 jonathan pfkeystat.out_bytes += m->m_pkthdr.len;
118 1.1 jonathan
119 1.1 jonathan len = m->m_pkthdr.len;
120 1.1 jonathan if (len < sizeof(struct sadb_msg)) {
121 1.1 jonathan pfkeystat.out_tooshort++;
122 1.1 jonathan error = EINVAL;
123 1.1 jonathan goto end;
124 1.1 jonathan }
125 1.1 jonathan
126 1.1 jonathan if (m->m_len < sizeof(struct sadb_msg)) {
127 1.1 jonathan if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
128 1.1 jonathan pfkeystat.out_nomem++;
129 1.1 jonathan error = ENOBUFS;
130 1.1 jonathan goto end;
131 1.1 jonathan }
132 1.1 jonathan }
133 1.1 jonathan
134 1.1 jonathan if ((m->m_flags & M_PKTHDR) == 0)
135 1.1 jonathan panic("key_output: not M_PKTHDR ??");
136 1.1 jonathan
137 1.1 jonathan KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
138 1.1 jonathan
139 1.1 jonathan msg = mtod(m, struct sadb_msg *);
140 1.1 jonathan pfkeystat.out_msgtype[msg->sadb_msg_type]++;
141 1.1 jonathan if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
142 1.1 jonathan pfkeystat.out_invlen++;
143 1.1 jonathan error = EINVAL;
144 1.1 jonathan goto end;
145 1.1 jonathan }
146 1.1 jonathan
147 1.1 jonathan /*XXX giant lock*/
148 1.1 jonathan s = splsoftnet();
149 1.1 jonathan error = key_parse(m, so);
150 1.1 jonathan m = NULL;
151 1.1 jonathan splx(s);
152 1.1 jonathan end:
153 1.1 jonathan if (m)
154 1.1 jonathan m_freem(m);
155 1.1 jonathan return error;
156 1.1 jonathan }
157 1.1 jonathan
158 1.1 jonathan /*
159 1.1 jonathan * send message to the socket.
160 1.1 jonathan */
161 1.1 jonathan static int
162 1.11 christos key_sendup0(
163 1.11 christos struct rawcb *rp,
164 1.11 christos struct mbuf *m,
165 1.11 christos int promisc,
166 1.11 christos int sbprio
167 1.11 christos )
168 1.1 jonathan {
169 1.1 jonathan int error;
170 1.5 jonathan int ok;
171 1.1 jonathan
172 1.1 jonathan if (promisc) {
173 1.1 jonathan struct sadb_msg *pmsg;
174 1.1 jonathan
175 1.1 jonathan M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT);
176 1.1 jonathan if (m && m->m_len < sizeof(struct sadb_msg))
177 1.1 jonathan m = m_pullup(m, sizeof(struct sadb_msg));
178 1.1 jonathan if (!m) {
179 1.1 jonathan pfkeystat.in_nomem++;
180 1.1 jonathan m_freem(m);
181 1.1 jonathan return ENOBUFS;
182 1.1 jonathan }
183 1.1 jonathan m->m_pkthdr.len += sizeof(*pmsg);
184 1.1 jonathan
185 1.1 jonathan pmsg = mtod(m, struct sadb_msg *);
186 1.1 jonathan bzero(pmsg, sizeof(*pmsg));
187 1.1 jonathan pmsg->sadb_msg_version = PF_KEY_V2;
188 1.1 jonathan pmsg->sadb_msg_type = SADB_X_PROMISC;
189 1.1 jonathan pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
190 1.1 jonathan /* pid and seq? */
191 1.1 jonathan
192 1.1 jonathan pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
193 1.1 jonathan }
194 1.1 jonathan
195 1.5 jonathan if (sbprio == 0)
196 1.5 jonathan ok = sbappendaddr(&rp->rcb_socket->so_rcv,
197 1.5 jonathan (struct sockaddr *)&key_src, m, NULL);
198 1.5 jonathan else
199 1.5 jonathan ok = sbappendaddrchain(&rp->rcb_socket->so_rcv,
200 1.5 jonathan (struct sockaddr *)&key_src, m, sbprio);
201 1.5 jonathan
202 1.5 jonathan if (!ok) {
203 1.1 jonathan pfkeystat.in_nomem++;
204 1.1 jonathan m_freem(m);
205 1.1 jonathan error = ENOBUFS;
206 1.1 jonathan } else
207 1.1 jonathan error = 0;
208 1.1 jonathan sorwakeup(rp->rcb_socket);
209 1.1 jonathan return error;
210 1.1 jonathan }
211 1.1 jonathan
212 1.1 jonathan /* XXX this interface should be obsoleted. */
213 1.1 jonathan int
214 1.14 degroote key_sendup(struct socket *so, struct sadb_msg *msg, u_int len,
215 1.14 degroote int target) /*target of the resulting message*/
216 1.1 jonathan {
217 1.1 jonathan struct mbuf *m, *n, *mprev;
218 1.1 jonathan int tlen;
219 1.1 jonathan
220 1.1 jonathan /* sanity check */
221 1.1 jonathan if (so == 0 || msg == 0)
222 1.8 christos panic("key_sendup: NULL pointer was passed");
223 1.1 jonathan
224 1.1 jonathan KEYDEBUG(KEYDEBUG_KEY_DUMP,
225 1.1 jonathan printf("key_sendup: \n");
226 1.1 jonathan kdebug_sadb(msg));
227 1.1 jonathan
228 1.1 jonathan /*
229 1.1 jonathan * we increment statistics here, just in case we have ENOBUFS
230 1.1 jonathan * in this function.
231 1.1 jonathan */
232 1.1 jonathan pfkeystat.in_total++;
233 1.1 jonathan pfkeystat.in_bytes += len;
234 1.1 jonathan pfkeystat.in_msgtype[msg->sadb_msg_type]++;
235 1.1 jonathan
236 1.1 jonathan /*
237 1.1 jonathan * Get mbuf chain whenever possible (not clusters),
238 1.1 jonathan * to save socket buffer. We'll be generating many SADB_ACQUIRE
239 1.1 jonathan * messages to listening key sockets. If we simply allocate clusters,
240 1.1 jonathan * sbappendaddr() will raise ENOBUFS due to too little sbspace().
241 1.1 jonathan * sbspace() computes # of actual data bytes AND mbuf region.
242 1.1 jonathan *
243 1.1 jonathan * TODO: SADB_ACQUIRE filters should be implemented.
244 1.1 jonathan */
245 1.1 jonathan tlen = len;
246 1.1 jonathan m = mprev = NULL;
247 1.1 jonathan while (tlen > 0) {
248 1.1 jonathan if (tlen == len) {
249 1.1 jonathan MGETHDR(n, M_DONTWAIT, MT_DATA);
250 1.1 jonathan n->m_len = MHLEN;
251 1.1 jonathan } else {
252 1.1 jonathan MGET(n, M_DONTWAIT, MT_DATA);
253 1.1 jonathan n->m_len = MLEN;
254 1.1 jonathan }
255 1.1 jonathan if (!n) {
256 1.1 jonathan pfkeystat.in_nomem++;
257 1.1 jonathan return ENOBUFS;
258 1.1 jonathan }
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.1 jonathan pfkeystat.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.1 jonathan pfkeystat.in_total--;
288 1.1 jonathan pfkeystat.in_bytes -= len;
289 1.1 jonathan pfkeystat.in_msgtype[msg->sadb_msg_type]--;
290 1.1 jonathan
291 1.1 jonathan return key_sendup_mbuf(so, m, target);
292 1.1 jonathan }
293 1.1 jonathan
294 1.1 jonathan /* so can be NULL if target != KEY_SENDUP_ONE */
295 1.1 jonathan int
296 1.14 degroote key_sendup_mbuf(struct socket *so, struct mbuf *m,
297 1.14 degroote int target/*, sbprio */)
298 1.1 jonathan {
299 1.1 jonathan struct mbuf *n;
300 1.1 jonathan struct keycb *kp;
301 1.1 jonathan int sendup;
302 1.1 jonathan struct rawcb *rp;
303 1.1 jonathan int error = 0;
304 1.5 jonathan int sbprio = 0; /* XXX should be a parameter */
305 1.1 jonathan
306 1.1 jonathan if (m == NULL)
307 1.8 christos panic("key_sendup_mbuf: NULL pointer was passed");
308 1.1 jonathan if (so == NULL && target == KEY_SENDUP_ONE)
309 1.8 christos panic("key_sendup_mbuf: NULL pointer was passed");
310 1.7 perry
311 1.5 jonathan /*
312 1.5 jonathan * RFC 2367 says ACQUIRE and other kernel-generated messages
313 1.5 jonathan * are special. We treat all KEY_SENDUP_REGISTERED messages
314 1.5 jonathan * as special, delivering them to all registered sockets
315 1.5 jonathan * even if the socket is at or above its so->so_rcv.sb_max limits.
316 1.5 jonathan * The only constraint is that the so_rcv data fall below
317 1.5 jonathan * key_registered_sb_max.
318 1.5 jonathan * Doing that check here avoids reworking every key_sendup_mbuf()
319 1.5 jonathan * in the short term. . The rework will be done after a technical
320 1.5 jonathan * conensus that this approach is appropriate.
321 1.5 jonathan */
322 1.5 jonathan if (target == KEY_SENDUP_REGISTERED) {
323 1.5 jonathan sbprio = SB_PRIO_BESTEFFORT;
324 1.5 jonathan }
325 1.1 jonathan
326 1.1 jonathan pfkeystat.in_total++;
327 1.1 jonathan pfkeystat.in_bytes += m->m_pkthdr.len;
328 1.1 jonathan if (m->m_len < sizeof(struct sadb_msg)) {
329 1.1 jonathan #if 1
330 1.1 jonathan m = m_pullup(m, sizeof(struct sadb_msg));
331 1.1 jonathan if (m == NULL) {
332 1.1 jonathan pfkeystat.in_nomem++;
333 1.1 jonathan return ENOBUFS;
334 1.1 jonathan }
335 1.1 jonathan #else
336 1.1 jonathan /* don't bother pulling it up just for stats */
337 1.1 jonathan #endif
338 1.1 jonathan }
339 1.1 jonathan if (m->m_len >= sizeof(struct sadb_msg)) {
340 1.1 jonathan struct sadb_msg *msg;
341 1.1 jonathan msg = mtod(m, struct sadb_msg *);
342 1.1 jonathan pfkeystat.in_msgtype[msg->sadb_msg_type]++;
343 1.1 jonathan }
344 1.1 jonathan
345 1.1 jonathan LIST_FOREACH(rp, &rawcb_list, rcb_list)
346 1.1 jonathan {
347 1.5 jonathan struct socket * kso = rp->rcb_socket;
348 1.1 jonathan if (rp->rcb_proto.sp_family != PF_KEY)
349 1.1 jonathan continue;
350 1.1 jonathan if (rp->rcb_proto.sp_protocol
351 1.1 jonathan && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
352 1.1 jonathan continue;
353 1.1 jonathan }
354 1.1 jonathan
355 1.1 jonathan kp = (struct keycb *)rp;
356 1.1 jonathan
357 1.1 jonathan /*
358 1.1 jonathan * If you are in promiscuous mode, and when you get broadcasted
359 1.1 jonathan * reply, you'll get two PF_KEY messages.
360 1.1 jonathan * (based on pf_key (at) inner.net message on 14 Oct 1998)
361 1.1 jonathan */
362 1.1 jonathan if (((struct keycb *)rp)->kp_promisc) {
363 1.1 jonathan if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
364 1.5 jonathan (void)key_sendup0(rp, n, 1, 0);
365 1.1 jonathan n = NULL;
366 1.1 jonathan }
367 1.1 jonathan }
368 1.1 jonathan
369 1.1 jonathan /* the exact target will be processed later */
370 1.1 jonathan if (so && sotorawcb(so) == rp)
371 1.1 jonathan continue;
372 1.1 jonathan
373 1.1 jonathan sendup = 0;
374 1.1 jonathan switch (target) {
375 1.1 jonathan case KEY_SENDUP_ONE:
376 1.1 jonathan /* the statement has no effect */
377 1.1 jonathan if (so && sotorawcb(so) == rp)
378 1.1 jonathan sendup++;
379 1.1 jonathan break;
380 1.1 jonathan case KEY_SENDUP_ALL:
381 1.1 jonathan sendup++;
382 1.1 jonathan break;
383 1.1 jonathan case KEY_SENDUP_REGISTERED:
384 1.5 jonathan if (kp->kp_registered) {
385 1.5 jonathan if (kso->so_rcv.sb_cc <= key_registered_sb_max)
386 1.5 jonathan sendup++;
387 1.5 jonathan else
388 1.5 jonathan printf("keysock: "
389 1.5 jonathan "registered sendup dropped, "
390 1.5 jonathan "sb_cc %ld max %d\n",
391 1.5 jonathan kso->so_rcv.sb_cc,
392 1.5 jonathan key_registered_sb_max);
393 1.5 jonathan }
394 1.1 jonathan break;
395 1.1 jonathan }
396 1.1 jonathan pfkeystat.in_msgtarget[target]++;
397 1.1 jonathan
398 1.1 jonathan if (!sendup)
399 1.1 jonathan continue;
400 1.1 jonathan
401 1.1 jonathan if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
402 1.1 jonathan m_freem(m);
403 1.1 jonathan pfkeystat.in_nomem++;
404 1.1 jonathan return ENOBUFS;
405 1.1 jonathan }
406 1.1 jonathan
407 1.5 jonathan if ((error = key_sendup0(rp, n, 0, 0)) != 0) {
408 1.1 jonathan m_freem(m);
409 1.1 jonathan return error;
410 1.1 jonathan }
411 1.1 jonathan
412 1.1 jonathan n = NULL;
413 1.1 jonathan }
414 1.1 jonathan
415 1.5 jonathan /* The 'later' time for processing the exact target has arrived */
416 1.1 jonathan if (so) {
417 1.5 jonathan error = key_sendup0(sotorawcb(so), m, 0, sbprio);
418 1.1 jonathan m = NULL;
419 1.1 jonathan } else {
420 1.1 jonathan error = 0;
421 1.1 jonathan m_freem(m);
422 1.1 jonathan }
423 1.1 jonathan return error;
424 1.1 jonathan }
425 1.1 jonathan
426 1.1 jonathan #ifdef __FreeBSD__
427 1.1 jonathan
428 1.1 jonathan /*
429 1.1 jonathan * key_abort()
430 1.1 jonathan * derived from net/rtsock.c:rts_abort()
431 1.1 jonathan */
432 1.1 jonathan static int
433 1.1 jonathan key_abort(struct socket *so)
434 1.1 jonathan {
435 1.1 jonathan int s, error;
436 1.1 jonathan s = splnet(); /* FreeBSD */
437 1.1 jonathan error = raw_usrreqs.pru_abort(so);
438 1.1 jonathan splx(s);
439 1.1 jonathan return error;
440 1.1 jonathan }
441 1.1 jonathan
442 1.1 jonathan /*
443 1.1 jonathan * key_attach()
444 1.1 jonathan * derived from net/rtsock.c:rts_attach()
445 1.1 jonathan */
446 1.1 jonathan static int
447 1.1 jonathan key_attach(struct socket *so, int proto, struct proc *td)
448 1.1 jonathan {
449 1.1 jonathan struct keycb *kp;
450 1.1 jonathan int s, error;
451 1.1 jonathan
452 1.1 jonathan if (sotorawcb(so) != 0)
453 1.1 jonathan return EISCONN; /* XXX panic? */
454 1.1 jonathan kp = (struct keycb *)malloc(sizeof *kp, M_PCB, M_WAITOK|M_ZERO); /* XXX */
455 1.1 jonathan if (kp == 0)
456 1.1 jonathan return ENOBUFS;
457 1.1 jonathan
458 1.1 jonathan /*
459 1.1 jonathan * The spl[soft]net() is necessary to block protocols from sending
460 1.1 jonathan * error notifications (like RTM_REDIRECT or RTM_LOSING) while
461 1.1 jonathan * this PCB is extant but incompletely initialized.
462 1.1 jonathan * Probably we should try to do more of this work beforehand and
463 1.1 jonathan * eliminate the spl.
464 1.1 jonathan */
465 1.1 jonathan s = splnet(); /* FreeBSD */
466 1.13 degroote so->so_pcb = kp;
467 1.1 jonathan error = raw_usrreqs.pru_attach(so, proto, td);
468 1.1 jonathan kp = (struct keycb *)sotorawcb(so);
469 1.1 jonathan if (error) {
470 1.1 jonathan free(kp, M_PCB);
471 1.13 degroote so->so_pcb = NULL;
472 1.1 jonathan splx(s);
473 1.1 jonathan return error;
474 1.1 jonathan }
475 1.1 jonathan
476 1.1 jonathan kp->kp_promisc = kp->kp_registered = 0;
477 1.1 jonathan
478 1.1 jonathan if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
479 1.1 jonathan key_cb.key_count++;
480 1.1 jonathan key_cb.any_count++;
481 1.1 jonathan kp->kp_raw.rcb_laddr = &key_src;
482 1.1 jonathan kp->kp_raw.rcb_faddr = &key_dst;
483 1.1 jonathan soisconnected(so);
484 1.1 jonathan so->so_options |= SO_USELOOPBACK;
485 1.1 jonathan
486 1.1 jonathan splx(s);
487 1.1 jonathan return 0;
488 1.1 jonathan }
489 1.1 jonathan
490 1.1 jonathan /*
491 1.1 jonathan * key_bind()
492 1.1 jonathan * derived from net/rtsock.c:rts_bind()
493 1.1 jonathan */
494 1.1 jonathan static int
495 1.1 jonathan key_bind(struct socket *so, struct sockaddr *nam, struct proc *td)
496 1.1 jonathan {
497 1.1 jonathan int s, error;
498 1.1 jonathan s = splnet(); /* FreeBSD */
499 1.1 jonathan error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
500 1.1 jonathan splx(s);
501 1.1 jonathan return error;
502 1.1 jonathan }
503 1.1 jonathan
504 1.1 jonathan /*
505 1.1 jonathan * key_connect()
506 1.1 jonathan * derived from net/rtsock.c:rts_connect()
507 1.1 jonathan */
508 1.1 jonathan static int
509 1.1 jonathan key_connect(struct socket *so, struct sockaddr *nam, struct proc *td)
510 1.1 jonathan {
511 1.1 jonathan int s, error;
512 1.1 jonathan s = splnet(); /* FreeBSD */
513 1.1 jonathan error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
514 1.1 jonathan splx(s);
515 1.1 jonathan return error;
516 1.1 jonathan }
517 1.1 jonathan
518 1.1 jonathan /*
519 1.1 jonathan * key_detach()
520 1.1 jonathan * derived from net/rtsock.c:rts_detach()
521 1.1 jonathan */
522 1.1 jonathan static int
523 1.1 jonathan key_detach(struct socket *so)
524 1.1 jonathan {
525 1.1 jonathan struct keycb *kp = (struct keycb *)sotorawcb(so);
526 1.1 jonathan int s, error;
527 1.1 jonathan
528 1.1 jonathan s = splnet(); /* FreeBSD */
529 1.1 jonathan if (kp != 0) {
530 1.1 jonathan if (kp->kp_raw.rcb_proto.sp_protocol
531 1.1 jonathan == PF_KEY) /* XXX: AF_KEY */
532 1.1 jonathan key_cb.key_count--;
533 1.1 jonathan key_cb.any_count--;
534 1.1 jonathan
535 1.1 jonathan key_freereg(so);
536 1.1 jonathan }
537 1.1 jonathan error = raw_usrreqs.pru_detach(so);
538 1.1 jonathan splx(s);
539 1.1 jonathan return error;
540 1.1 jonathan }
541 1.1 jonathan
542 1.1 jonathan /*
543 1.1 jonathan * key_disconnect()
544 1.1 jonathan * derived from net/rtsock.c:key_disconnect()
545 1.1 jonathan */
546 1.1 jonathan static int
547 1.1 jonathan key_disconnect(struct socket *so)
548 1.1 jonathan {
549 1.1 jonathan int s, error;
550 1.1 jonathan s = splnet(); /* FreeBSD */
551 1.1 jonathan error = raw_usrreqs.pru_disconnect(so);
552 1.1 jonathan splx(s);
553 1.1 jonathan return error;
554 1.1 jonathan }
555 1.1 jonathan
556 1.1 jonathan /*
557 1.1 jonathan * key_peeraddr()
558 1.1 jonathan * derived from net/rtsock.c:rts_peeraddr()
559 1.1 jonathan */
560 1.1 jonathan static int
561 1.1 jonathan key_peeraddr(struct socket *so, struct sockaddr **nam)
562 1.1 jonathan {
563 1.1 jonathan int s, error;
564 1.1 jonathan s = splnet(); /* FreeBSD */
565 1.1 jonathan error = raw_usrreqs.pru_peeraddr(so, nam);
566 1.1 jonathan splx(s);
567 1.1 jonathan return error;
568 1.1 jonathan }
569 1.1 jonathan
570 1.1 jonathan /*
571 1.1 jonathan * key_send()
572 1.1 jonathan * derived from net/rtsock.c:rts_send()
573 1.1 jonathan */
574 1.1 jonathan static int
575 1.1 jonathan key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
576 1.1 jonathan struct mbuf *control, struct proc *td)
577 1.1 jonathan {
578 1.1 jonathan int s, error;
579 1.1 jonathan s = splnet(); /* FreeBSD */
580 1.1 jonathan error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
581 1.1 jonathan splx(s);
582 1.1 jonathan return error;
583 1.1 jonathan }
584 1.1 jonathan
585 1.1 jonathan /*
586 1.1 jonathan * key_shutdown()
587 1.1 jonathan * derived from net/rtsock.c:rts_shutdown()
588 1.1 jonathan */
589 1.1 jonathan static int
590 1.1 jonathan key_shutdown(struct socket *so)
591 1.1 jonathan {
592 1.1 jonathan int s, error;
593 1.1 jonathan s = splnet(); /* FreeBSD */
594 1.1 jonathan error = raw_usrreqs.pru_shutdown(so);
595 1.1 jonathan splx(s);
596 1.1 jonathan return error;
597 1.1 jonathan }
598 1.1 jonathan
599 1.1 jonathan /*
600 1.1 jonathan * key_sockaddr()
601 1.1 jonathan * derived from net/rtsock.c:rts_sockaddr()
602 1.1 jonathan */
603 1.1 jonathan static int
604 1.1 jonathan key_sockaddr(struct socket *so, struct sockaddr **nam)
605 1.1 jonathan {
606 1.1 jonathan int s, error;
607 1.1 jonathan s = splnet(); /* FreeBSD */
608 1.1 jonathan error = raw_usrreqs.pru_sockaddr(so, nam);
609 1.1 jonathan splx(s);
610 1.1 jonathan return error;
611 1.1 jonathan }
612 1.1 jonathan #else /*!__FreeBSD__ -- traditional proto_usrreq() switch */
613 1.1 jonathan
614 1.1 jonathan /*
615 1.1 jonathan * key_usrreq()
616 1.1 jonathan * derived from net/rtsock.c:route_usrreq()
617 1.1 jonathan */
618 1.1 jonathan int
619 1.14 degroote key_usrreq(struct socket *so, int req,struct mbuf *m, struct mbuf *nam,
620 1.14 degroote struct mbuf *control, struct lwp *l)
621 1.1 jonathan {
622 1.1 jonathan int error = 0;
623 1.1 jonathan struct keycb *kp = (struct keycb *)sotorawcb(so);
624 1.1 jonathan int s;
625 1.1 jonathan
626 1.1 jonathan s = splsoftnet();
627 1.1 jonathan if (req == PRU_ATTACH) {
628 1.1 jonathan kp = (struct keycb *)malloc(sizeof(*kp), M_PCB, M_WAITOK);
629 1.13 degroote so->so_pcb = kp;
630 1.1 jonathan if (so->so_pcb)
631 1.1 jonathan bzero(so->so_pcb, sizeof(*kp));
632 1.1 jonathan }
633 1.1 jonathan if (req == PRU_DETACH && kp) {
634 1.1 jonathan int af = kp->kp_raw.rcb_proto.sp_protocol;
635 1.1 jonathan if (af == PF_KEY) /* XXX: AF_KEY */
636 1.1 jonathan key_cb.key_count--;
637 1.1 jonathan key_cb.any_count--;
638 1.1 jonathan
639 1.1 jonathan key_freereg(so);
640 1.1 jonathan }
641 1.1 jonathan
642 1.9 christos error = raw_usrreq(so, req, m, nam, control, l);
643 1.1 jonathan m = control = NULL; /* reclaimed in raw_usrreq */
644 1.1 jonathan kp = (struct keycb *)sotorawcb(so);
645 1.1 jonathan if (req == PRU_ATTACH && kp) {
646 1.1 jonathan int af = kp->kp_raw.rcb_proto.sp_protocol;
647 1.1 jonathan if (error) {
648 1.1 jonathan pfkeystat.sockerr++;
649 1.13 degroote free(kp, M_PCB);
650 1.13 degroote so->so_pcb = NULL;
651 1.1 jonathan splx(s);
652 1.1 jonathan return (error);
653 1.1 jonathan }
654 1.1 jonathan
655 1.1 jonathan kp->kp_promisc = kp->kp_registered = 0;
656 1.1 jonathan
657 1.1 jonathan if (af == PF_KEY) /* XXX: AF_KEY */
658 1.1 jonathan key_cb.key_count++;
659 1.1 jonathan key_cb.any_count++;
660 1.1 jonathan kp->kp_raw.rcb_laddr = &key_src;
661 1.1 jonathan kp->kp_raw.rcb_faddr = &key_dst;
662 1.1 jonathan soisconnected(so);
663 1.1 jonathan so->so_options |= SO_USELOOPBACK;
664 1.1 jonathan }
665 1.1 jonathan splx(s);
666 1.1 jonathan return (error);
667 1.1 jonathan }
668 1.1 jonathan #endif /*!__FreeBSD__*/
669 1.1 jonathan
670 1.1 jonathan /* sysctl */
671 1.1 jonathan #ifdef SYSCTL_NODE
672 1.1 jonathan SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
673 1.2 tls #endif /* SYSCTL_NODE */
674 1.1 jonathan
675 1.1 jonathan /*
676 1.1 jonathan * Definitions of protocols supported in the KEY domain.
677 1.1 jonathan */
678 1.1 jonathan
679 1.6 matt #ifdef __FreeBSD__
680 1.1 jonathan extern struct domain keydomain;
681 1.1 jonathan
682 1.1 jonathan struct pr_usrreqs key_usrreqs = {
683 1.1 jonathan key_abort, pru_accept_notsupp, key_attach, key_bind,
684 1.1 jonathan key_connect,
685 1.1 jonathan pru_connect2_notsupp, pru_control_notsupp, key_detach,
686 1.1 jonathan key_disconnect, pru_listen_notsupp, key_peeraddr,
687 1.1 jonathan pru_rcvd_notsupp,
688 1.1 jonathan pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
689 1.1 jonathan key_sockaddr, sosend, soreceive, sopoll
690 1.1 jonathan };
691 1.1 jonathan
692 1.1 jonathan struct protosw keysw[] = {
693 1.1 jonathan { SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
694 1.1 jonathan 0, (pr_output_t *)key_output, raw_ctlinput, 0,
695 1.1 jonathan 0,
696 1.1 jonathan raw_init, 0, 0, 0,
697 1.1 jonathan &key_usrreqs
698 1.1 jonathan }
699 1.1 jonathan };
700 1.1 jonathan
701 1.1 jonathan static void
702 1.1 jonathan key_init0(void)
703 1.1 jonathan {
704 1.13 degroote bzero(&key_cb, sizeof(key_cb));
705 1.1 jonathan key_init();
706 1.1 jonathan }
707 1.1 jonathan
708 1.1 jonathan struct domain keydomain =
709 1.1 jonathan { PF_KEY, "key", key_init0, 0, 0,
710 1.1 jonathan keysw, &keysw[sizeof(keysw)/sizeof(keysw[0])] };
711 1.1 jonathan
712 1.1 jonathan DOMAIN_SET(key);
713 1.1 jonathan
714 1.1 jonathan #else /* !__FreeBSD__ */
715 1.1 jonathan
716 1.6 matt DOMAIN_DEFINE(keydomain);
717 1.1 jonathan
718 1.10 matt const struct protosw keysw[] = {
719 1.10 matt {
720 1.10 matt .pr_type = SOCK_RAW,
721 1.10 matt .pr_domain = &keydomain,
722 1.10 matt .pr_protocol = PF_KEY_V2,
723 1.10 matt .pr_flags = PR_ATOMIC|PR_ADDR,
724 1.10 matt .pr_output = key_output,
725 1.10 matt .pr_ctlinput = raw_ctlinput,
726 1.10 matt .pr_usrreq = key_usrreq,
727 1.10 matt .pr_init = raw_init,
728 1.10 matt }
729 1.1 jonathan };
730 1.1 jonathan
731 1.10 matt struct domain keydomain = {
732 1.10 matt .dom_family = PF_KEY,
733 1.10 matt .dom_name = "key",
734 1.10 matt .dom_init = key_init,
735 1.10 matt .dom_protosw = keysw,
736 1.10 matt .dom_protoswNPROTOSW = &keysw[__arraycount(keysw)],
737 1.10 matt };
738 1.1 jonathan
739 1.1 jonathan #endif
740