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