]> git.saurik.com Git - apple/xnu.git/blob - bsd/netkey/keysock.c
xnu-2782.30.5.tar.gz
[apple/xnu.git] / bsd / netkey / keysock.c
1 /* $KAME: keysock.c,v 1.13 2000/03/25 07:24:13 sumikawa Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39 #include <sys/mbuf.h>
40 #include <sys/mcache.h>
41 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/domain.h>
45 #include <sys/protosw.h>
46 #include <sys/errno.h>
47
48 #include <kern/locks.h>
49
50 #include <net/raw_cb.h>
51 #include <net/route.h>
52
53 #include <net/pfkeyv2.h>
54 #include <netkey/keydb.h>
55 #include <netkey/key.h>
56 #include <netkey/keysock.h>
57 #include <netkey/key_debug.h>
58
59 extern lck_mtx_t *raw_mtx;
60 extern void key_init(struct protosw *, struct domain *);
61
62 struct sockaddr key_dst = { 2, PF_KEY, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,} };
63 struct sockaddr key_src = { 2, PF_KEY, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,} };
64
65 static void key_dinit(struct domain *);
66 static int key_sendup0(struct rawcb *, struct mbuf *, int);
67
68 struct pfkeystat pfkeystat;
69
70 static struct domain *keydomain = NULL;
71
72 extern lck_mtx_t *pfkey_stat_mutex;
73
74 /*
75 * key_output()
76 */
77 int
78 #ifdef __APPLE__
79 /* No variable argument support? */
80 key_output(struct mbuf *m, struct socket *so)
81 #else
82 #if __STDC__
83 key_output(struct mbuf *m, ...)
84 #else
85 key_output(m, va_alist)
86 struct mbuf *m;
87 va_dcl
88 #endif
89 #endif
90 {
91 struct sadb_msg *msg;
92 int len, error = 0;
93 #ifndef __APPLE__
94 struct socket *so;
95 va_list ap;
96
97 va_start(ap, m);
98 so = va_arg(ap, struct socket *);
99 va_end(ap);
100 #endif
101
102 if (m == 0)
103 panic("key_output: NULL pointer was passed.\n");
104
105 socket_unlock(so, 0);
106 lck_mtx_lock(pfkey_stat_mutex);
107 pfkeystat.out_total++;
108 pfkeystat.out_bytes += m->m_pkthdr.len;
109 lck_mtx_unlock(pfkey_stat_mutex);
110
111 len = m->m_pkthdr.len;
112 if (len < sizeof(struct sadb_msg)) {
113 #if IPSEC_DEBUG
114 printf("key_output: Invalid message length.\n");
115 #endif
116 PFKEY_STAT_INCREMENT(pfkeystat.out_tooshort);
117 error = EINVAL;
118 goto end;
119 }
120
121 if (m->m_len < sizeof(struct sadb_msg)) {
122 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
123 #if IPSEC_DEBUG
124 printf("key_output: can't pullup mbuf\n");
125 #endif
126 PFKEY_STAT_INCREMENT(pfkeystat.out_nomem);
127 error = ENOBUFS;
128 goto end;
129 }
130 }
131
132 if ((m->m_flags & M_PKTHDR) == 0)
133 panic("key_output: not M_PKTHDR ??");
134
135 #if IPSEC_DEBUG
136 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
137 #endif /* defined(IPSEC_DEBUG) */
138
139 msg = mtod(m, struct sadb_msg *);
140 PFKEY_STAT_INCREMENT(pfkeystat.out_msgtype[msg->sadb_msg_type]);
141 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
142 #if IPSEC_DEBUG
143 printf("key_output: Invalid message length.\n");
144 #endif
145 PFKEY_STAT_INCREMENT(pfkeystat.out_invlen);
146 error = EINVAL;
147 goto end;
148 }
149
150 error = key_parse(m, so);
151 m = NULL;
152
153 end:
154 if (m)
155 m_freem(m);
156 socket_lock(so, 0);
157 return error;
158 }
159
160 /*
161 * send message to the socket.
162 */
163 static int
164 key_sendup0(rp, m, promisc)
165 struct rawcb *rp;
166 struct mbuf *m;
167 int promisc;
168 {
169 int error;
170
171 if (promisc) {
172 struct sadb_msg *pmsg;
173
174 M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT);
175 if (m && m->m_len < sizeof(struct sadb_msg))
176 m = m_pullup(m, sizeof(struct sadb_msg));
177 if (!m) {
178 #if IPSEC_DEBUG
179 printf("key_sendup0: cannot pullup\n");
180 #endif
181 PFKEY_STAT_INCREMENT(pfkeystat.in_nomem);
182 m_freem(m);
183 return ENOBUFS;
184 }
185 m->m_pkthdr.len += sizeof(*pmsg);
186
187 pmsg = mtod(m, struct sadb_msg *);
188 bzero(pmsg, sizeof(*pmsg));
189 pmsg->sadb_msg_version = PF_KEY_V2;
190 pmsg->sadb_msg_type = SADB_X_PROMISC;
191 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
192 /* pid and seq? */
193
194 PFKEY_STAT_INCREMENT(pfkeystat.in_msgtype[pmsg->sadb_msg_type]);
195 }
196
197 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
198 m, NULL, &error)) {
199 #if IPSEC_DEBUG
200 printf("key_sendup0: sbappendaddr failed\n");
201 #endif
202 PFKEY_STAT_INCREMENT(pfkeystat.in_nomem);
203 }
204 else {
205 sorwakeup(rp->rcb_socket);
206 }
207 return error;
208 }
209
210
211 /* so can be NULL if target != KEY_SENDUP_ONE */
212 int
213 key_sendup_mbuf(so, m, target)
214 struct socket *so;
215 struct mbuf *m;
216 int target;
217 {
218 struct mbuf *n;
219 struct keycb *kp;
220 int sendup;
221 struct rawcb *rp;
222 int error = 0;
223
224 if (m == NULL)
225 panic("key_sendup_mbuf: NULL pointer was passed.\n");
226 if (so == NULL && target == KEY_SENDUP_ONE)
227 panic("key_sendup_mbuf: NULL pointer was passed.\n");
228
229 lck_mtx_lock(pfkey_stat_mutex);
230 pfkeystat.in_total++;
231 pfkeystat.in_bytes += m->m_pkthdr.len;
232 lck_mtx_unlock(pfkey_stat_mutex);
233 if (m->m_len < sizeof(struct sadb_msg)) {
234 #if 1
235 m = m_pullup(m, sizeof(struct sadb_msg));
236 if (m == NULL) {
237 PFKEY_STAT_INCREMENT(pfkeystat.in_nomem);
238 return ENOBUFS;
239 }
240 #else
241 /* don't bother pulling it up just for stats */
242 #endif
243 }
244 if (m->m_len >= sizeof(struct sadb_msg)) {
245 struct sadb_msg *msg;
246 msg = mtod(m, struct sadb_msg *);
247 PFKEY_STAT_INCREMENT(pfkeystat.in_msgtype[msg->sadb_msg_type]);
248 }
249
250 lck_mtx_lock(raw_mtx);
251 LIST_FOREACH(rp, &rawcb_list, list)
252 {
253 if (rp->rcb_proto.sp_family != PF_KEY)
254 continue;
255 if (rp->rcb_proto.sp_protocol
256 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
257 continue;
258 }
259
260 kp = (struct keycb *)rp;
261
262 socket_lock(rp->rcb_socket, 1);
263 /*
264 * If you are in promiscuous mode, and when you get broadcasted
265 * reply, you'll get two PF_KEY messages.
266 * (based on pf_key@inner.net message on 14 Oct 1998)
267 */
268 if (((struct keycb *)rp)->kp_promisc) {
269 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
270 (void)key_sendup0(rp, n, 1);
271 n = NULL;
272 }
273 }
274
275 /* the exact target will be processed later */
276 if (so && sotorawcb(so) == rp) {
277 socket_unlock(rp->rcb_socket, 1);
278 continue;
279 }
280
281 sendup = 0;
282 switch (target) {
283 case KEY_SENDUP_ONE:
284 /* the statement has no effect */
285 break;
286 case KEY_SENDUP_ALL:
287 sendup++;
288 break;
289 case KEY_SENDUP_REGISTERED:
290 if (kp->kp_registered)
291 sendup++;
292 break;
293 }
294 PFKEY_STAT_INCREMENT(pfkeystat.in_msgtarget[target]);
295
296 if (!sendup) {
297 socket_unlock(rp->rcb_socket, 1);
298 continue;
299 }
300 else
301 sendup = 0; // clear for next iteration
302
303 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
304 #if IPSEC_DEBUG
305 printf("key_sendup: m_copy fail\n");
306 #endif
307 m_freem(m);
308 PFKEY_STAT_INCREMENT(pfkeystat.in_nomem);
309 socket_unlock(rp->rcb_socket, 1);
310 lck_mtx_unlock(raw_mtx);
311 return ENOBUFS;
312 }
313
314 /*
315 * ignore error even if queue is full. PF_KEY does not
316 * guarantee the delivery of the message.
317 * this is important when target == KEY_SENDUP_ALL.
318 */
319 key_sendup0(rp, n, 0);
320 socket_unlock(rp->rcb_socket, 1);
321 n = NULL;
322 }
323
324 lck_mtx_unlock(raw_mtx);
325 if (so) {
326 socket_lock(so, 1);
327 error = key_sendup0(sotorawcb(so), m, 0);
328 socket_unlock(so, 1);
329 m = NULL;
330 } else {
331 error = 0;
332 m_freem(m);
333 }
334 return error;
335 }
336
337 /*
338 * key_abort()
339 * derived from net/rtsock.c:rts_abort()
340 */
341 static int
342 key_abort(struct socket *so)
343 {
344 int error;
345 error = raw_usrreqs.pru_abort(so);
346 return error;
347 }
348
349 /*
350 * key_attach()
351 * derived from net/rtsock.c:rts_attach()
352 */
353 static int
354 key_attach(struct socket *so, int proto, struct proc *p)
355 {
356 struct keycb *kp;
357 int error;
358
359 if (sotorawcb(so) != 0)
360 return EISCONN; /* XXX panic? */
361 kp = (struct keycb *)_MALLOC(sizeof *kp, M_PCB, M_WAITOK); /* XXX */
362 if (kp == 0)
363 return ENOBUFS;
364 bzero(kp, sizeof *kp);
365
366 so->so_pcb = (caddr_t)kp;
367 kp->kp_promisc = kp->kp_registered = 0;
368 kp->kp_raw.rcb_laddr = &key_src;
369 kp->kp_raw.rcb_faddr = &key_dst;
370
371 error = raw_usrreqs.pru_attach(so, proto, p);
372 kp = (struct keycb *)sotorawcb(so);
373 if (error) {
374 _FREE(kp, M_PCB);
375 so->so_pcb = (caddr_t) 0;
376 so->so_flags |= SOF_PCBCLEARING;
377 printf("key_usrreq: key_usrreq results %d\n", error);
378 return error;
379 }
380
381 /* so is already locked when calling key_attach */
382 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
383 key_cb.key_count++;
384 key_cb.any_count++;
385 soisconnected(so);
386 so->so_options |= SO_USELOOPBACK;
387
388 return 0;
389 }
390
391 /*
392 * key_bind()
393 * derived from net/rtsock.c:rts_bind()
394 */
395 static int
396 key_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
397 {
398 int error;
399 error = raw_usrreqs.pru_bind(so, nam, p); /* xxx just EINVAL */
400 return error;
401 }
402
403 /*
404 * key_connect()
405 * derived from net/rtsock.c:rts_connect()
406 */
407 static int
408 key_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
409 {
410 int error;
411 error = raw_usrreqs.pru_connect(so, nam, p); /* XXX just EINVAL */
412 return error;
413 }
414
415 /*
416 * key_detach()
417 * derived from net/rtsock.c:rts_detach()
418 */
419 static int
420 key_detach(struct socket *so)
421 {
422 struct keycb *kp = (struct keycb *)sotorawcb(so);
423 int error;
424
425 if (kp != 0) {
426 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
427 key_cb.key_count--;
428 key_cb.any_count--;
429 socket_unlock(so, 0);
430 key_freereg(so);
431 socket_lock(so, 0);
432 }
433 error = raw_usrreqs.pru_detach(so);
434 return error;
435 }
436
437 /*
438 * key_disconnect()
439 * derived from net/rtsock.c:key_disconnect()
440 */
441 static int
442 key_disconnect(struct socket *so)
443 {
444 int error;
445 error = raw_usrreqs.pru_disconnect(so);
446 return error;
447 }
448
449 /*
450 * key_peeraddr()
451 * derived from net/rtsock.c:rts_peeraddr()
452 */
453 static int
454 key_peeraddr(struct socket *so, struct sockaddr **nam)
455 {
456 int error;
457 error = raw_usrreqs.pru_peeraddr(so, nam);
458 return error;
459 }
460
461 /*
462 * key_send()
463 * derived from net/rtsock.c:rts_send()
464 */
465 static int
466 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
467 struct mbuf *control, struct proc *p)
468 {
469 int error;
470 error = raw_usrreqs.pru_send(so, flags, m, nam, control, p);
471 return error;
472 }
473
474 /*
475 * key_shutdown()
476 * derived from net/rtsock.c:rts_shutdown()
477 */
478 static int
479 key_shutdown(struct socket *so)
480 {
481 int error;
482 error = raw_usrreqs.pru_shutdown(so);
483 return error;
484 }
485
486 /*
487 * key_sockaddr()
488 * derived from net/rtsock.c:rts_sockaddr()
489 */
490 static int
491 key_sockaddr(struct socket *so, struct sockaddr **nam)
492 {
493 int error;
494 error = raw_usrreqs.pru_sockaddr(so, nam);
495 return error;
496 }
497
498 static struct pr_usrreqs key_usrreqs = {
499 .pru_abort = key_abort,
500 .pru_attach = key_attach,
501 .pru_bind = key_bind,
502 .pru_connect = key_connect,
503 .pru_detach = key_detach,
504 .pru_disconnect = key_disconnect,
505 .pru_peeraddr = key_peeraddr,
506 .pru_send = key_send,
507 .pru_shutdown = key_shutdown,
508 .pru_sockaddr = key_sockaddr,
509 .pru_sosend = sosend,
510 .pru_soreceive = soreceive,
511 };
512
513 /* sysctl */
514 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW|CTLFLAG_LOCKED, 0, "Key Family");
515
516 /*
517 * Definitions of protocols supported in the KEY domain.
518 */
519
520 extern struct domain keydomain_s;
521
522 static struct protosw keysw[] = {
523 {
524 .pr_type = SOCK_RAW,
525 .pr_protocol = PF_KEY_V2,
526 .pr_flags = PR_ATOMIC|PR_ADDR,
527 .pr_output = key_output,
528 .pr_ctlinput = raw_ctlinput,
529 .pr_init = key_init,
530 .pr_usrreqs = &key_usrreqs,
531 }
532 };
533
534 static int key_proto_count = (sizeof (keysw) / sizeof (struct protosw));
535
536 struct domain keydomain_s = {
537 .dom_family = PF_KEY,
538 .dom_name = "key",
539 .dom_init = key_dinit,
540 .dom_maxrtkey = sizeof (struct key_cb),
541 };
542
543 static void
544 key_dinit(struct domain *dp)
545 {
546 struct protosw *pr;
547 int i;
548
549 VERIFY(!(dp->dom_flags & DOM_INITIALIZED));
550 VERIFY(keydomain == NULL);
551
552 keydomain = dp;
553
554 for (i = 0, pr = &keysw[0]; i < key_proto_count; i++, pr++)
555 net_add_proto(pr, dp, 1);
556 }