]> git.saurik.com Git - apple/xnu.git/blob - bsd/netkey/keysock.c
xnu-6153.141.1.tar.gz
[apple/xnu.git] / bsd / netkey / keysock.c
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
87 extern lck_mtx_t *raw_mtx;
88 extern void key_init(struct protosw *, struct domain *);
89
90 struct 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, } };
91 struct 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
93 static void key_dinit(struct domain *);
94 static int key_sendup0(struct rawcb *, struct mbuf *, int);
95
96 struct pfkeystat pfkeystat;
97
98 static struct domain *keydomain = NULL;
99
100 extern lck_mtx_t *pfkey_stat_mutex;
101
102 /*
103 * key_output()
104 */
105 int
106 #ifdef __APPLE__
107 /* No variable argument support? */
108 key_output(struct mbuf *m, struct socket *so)
109 #else
110 #if __STDC__
111 key_output(struct mbuf *m, ...)
112 #else
113 key_output(m, va_alist)
114 struct mbuf *m;
115 va_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
183 end:
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 */
194 static int
195 key_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
216 pmsg = mtod(m, struct sadb_msg *);
217 bzero(pmsg, sizeof(*pmsg));
218 pmsg->sadb_msg_version = PF_KEY_V2;
219 pmsg->sadb_msg_type = SADB_X_PROMISC;
220 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
221 /* pid and seq? */
222
223 PFKEY_STAT_INCREMENT(pfkeystat.in_msgtype[pmsg->sadb_msg_type]);
224 }
225
226 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
227 m, NULL, &error)) {
228 #if IPSEC_DEBUG
229 printf("key_sendup0: sbappendaddr failed\n");
230 #endif
231 PFKEY_STAT_INCREMENT(pfkeystat.in_nomem);
232 } else {
233 sorwakeup(rp->rcb_socket);
234 }
235 return error;
236 }
237
238
239 /* so can be NULL if target != KEY_SENDUP_ONE */
240 int
241 key_sendup_mbuf(struct socket *so, struct mbuf *m, int target)
242 {
243 struct mbuf *n;
244 struct keycb *kp;
245 int sendup;
246 struct rawcb *rp;
247 int error = 0;
248
249 if (m == NULL) {
250 panic("key_sendup_mbuf: NULL pointer was passed.\n");
251 }
252 if (so == NULL && target == KEY_SENDUP_ONE) {
253 panic("key_sendup_mbuf: NULL pointer was passed.\n");
254 }
255
256 lck_mtx_lock(pfkey_stat_mutex);
257 pfkeystat.in_total++;
258 pfkeystat.in_bytes += m->m_pkthdr.len;
259 lck_mtx_unlock(pfkey_stat_mutex);
260 if (m->m_len < sizeof(struct sadb_msg)) {
261 #if 1
262 m = m_pullup(m, sizeof(struct sadb_msg));
263 if (m == NULL) {
264 PFKEY_STAT_INCREMENT(pfkeystat.in_nomem);
265 return ENOBUFS;
266 }
267 #else
268 /* don't bother pulling it up just for stats */
269 #endif
270 }
271 if (m->m_len >= sizeof(struct sadb_msg)) {
272 struct sadb_msg *msg;
273 msg = mtod(m, struct sadb_msg *);
274 PFKEY_STAT_INCREMENT(pfkeystat.in_msgtype[msg->sadb_msg_type]);
275 }
276
277 lck_mtx_lock(raw_mtx);
278 LIST_FOREACH(rp, &rawcb_list, list)
279 {
280 if (rp->rcb_proto.sp_family != PF_KEY) {
281 continue;
282 }
283 if (rp->rcb_proto.sp_protocol
284 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
285 continue;
286 }
287
288 kp = (struct keycb *)rp;
289
290 socket_lock(rp->rcb_socket, 1);
291 /*
292 * If you are in promiscuous mode, and when you get broadcasted
293 * reply, you'll get two PF_KEY messages.
294 * (based on pf_key@inner.net message on 14 Oct 1998)
295 */
296 if (((struct keycb *)rp)->kp_promisc) {
297 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
298 (void)key_sendup0(rp, n, 1);
299 n = NULL;
300 }
301 }
302
303 /* the exact target will be processed later */
304 if (so && sotorawcb(so) == rp) {
305 socket_unlock(rp->rcb_socket, 1);
306 continue;
307 }
308
309 sendup = 0;
310 switch (target) {
311 case KEY_SENDUP_ONE:
312 /* the statement has no effect */
313 break;
314 case KEY_SENDUP_ALL:
315 sendup++;
316 break;
317 case KEY_SENDUP_REGISTERED:
318 if (kp->kp_registered) {
319 sendup++;
320 }
321 break;
322 }
323 PFKEY_STAT_INCREMENT(pfkeystat.in_msgtarget[target]);
324
325 if (!sendup) {
326 socket_unlock(rp->rcb_socket, 1);
327 continue;
328 } else {
329 sendup = 0; // clear for next iteration
330 }
331 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
332 #if IPSEC_DEBUG
333 printf("key_sendup: m_copy fail\n");
334 #endif
335 m_freem(m);
336 PFKEY_STAT_INCREMENT(pfkeystat.in_nomem);
337 socket_unlock(rp->rcb_socket, 1);
338 lck_mtx_unlock(raw_mtx);
339 return ENOBUFS;
340 }
341
342 /*
343 * ignore error even if queue is full. PF_KEY does not
344 * guarantee the delivery of the message.
345 * this is important when target == KEY_SENDUP_ALL.
346 */
347 key_sendup0(rp, n, 0);
348 socket_unlock(rp->rcb_socket, 1);
349 n = NULL;
350 }
351
352 lck_mtx_unlock(raw_mtx);
353 if (so) {
354 socket_lock(so, 1);
355 error = key_sendup0(sotorawcb(so), m, 0);
356 socket_unlock(so, 1);
357 m = NULL;
358 } else {
359 error = 0;
360 m_freem(m);
361 }
362 return error;
363 }
364
365 /*
366 * key_abort()
367 * derived from net/rtsock.c:rts_abort()
368 */
369 static int
370 key_abort(struct socket *so)
371 {
372 int error;
373 error = raw_usrreqs.pru_abort(so);
374 return error;
375 }
376
377 /*
378 * key_attach()
379 * derived from net/rtsock.c:rts_attach()
380 */
381 static int
382 key_attach(struct socket *so, int proto, struct proc *p)
383 {
384 struct keycb *kp;
385 int error;
386
387 if (sotorawcb(so) != 0) {
388 return EISCONN; /* XXX panic? */
389 }
390 kp = (struct keycb *)_MALLOC(sizeof(*kp), M_PCB,
391 M_WAITOK | M_ZERO); /* XXX */
392 if (kp == 0) {
393 return ENOBUFS;
394 }
395
396 so->so_pcb = (caddr_t)kp;
397 kp->kp_promisc = kp->kp_registered = 0;
398 kp->kp_raw.rcb_laddr = &key_src;
399 kp->kp_raw.rcb_faddr = &key_dst;
400
401 error = raw_usrreqs.pru_attach(so, proto, p);
402 kp = (struct keycb *)sotorawcb(so);
403 if (error) {
404 _FREE(kp, M_PCB);
405 so->so_pcb = (caddr_t) 0;
406 so->so_flags |= SOF_PCBCLEARING;
407 printf("key_usrreq: key_usrreq results %d\n", error);
408 return error;
409 }
410
411 /* so is already locked when calling key_attach */
412 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) { /* XXX: AF_KEY */
413 key_cb.key_count++;
414 }
415 key_cb.any_count++;
416 soisconnected(so);
417 so->so_options |= SO_USELOOPBACK;
418
419 return 0;
420 }
421
422 /*
423 * key_bind()
424 * derived from net/rtsock.c:rts_bind()
425 */
426 static int
427 key_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
428 {
429 int error;
430 error = raw_usrreqs.pru_bind(so, nam, p); /* xxx just EINVAL */
431 return error;
432 }
433
434 /*
435 * key_connect()
436 * derived from net/rtsock.c:rts_connect()
437 */
438 static int
439 key_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
440 {
441 int error;
442 error = raw_usrreqs.pru_connect(so, nam, p); /* XXX just EINVAL */
443 return error;
444 }
445
446 /*
447 * key_detach()
448 * derived from net/rtsock.c:rts_detach()
449 */
450 static int
451 key_detach(struct socket *so)
452 {
453 struct keycb *kp = (struct keycb *)sotorawcb(so);
454 int error;
455
456 if (kp != 0) {
457 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) { /* XXX: AF_KEY */
458 key_cb.key_count--;
459 }
460 key_cb.any_count--;
461 socket_unlock(so, 0);
462 key_freereg(so);
463 socket_lock(so, 0);
464 }
465 error = raw_usrreqs.pru_detach(so);
466 return error;
467 }
468
469 /*
470 * key_disconnect()
471 * derived from net/rtsock.c:key_disconnect()
472 */
473 static int
474 key_disconnect(struct socket *so)
475 {
476 int error;
477 error = raw_usrreqs.pru_disconnect(so);
478 return error;
479 }
480
481 /*
482 * key_peeraddr()
483 * derived from net/rtsock.c:rts_peeraddr()
484 */
485 static int
486 key_peeraddr(struct socket *so, struct sockaddr **nam)
487 {
488 int error;
489 error = raw_usrreqs.pru_peeraddr(so, nam);
490 return error;
491 }
492
493 /*
494 * key_send()
495 * derived from net/rtsock.c:rts_send()
496 */
497 static int
498 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
499 struct mbuf *control, struct proc *p)
500 {
501 int error;
502 error = raw_usrreqs.pru_send(so, flags, m, nam, control, p);
503 return error;
504 }
505
506 /*
507 * key_shutdown()
508 * derived from net/rtsock.c:rts_shutdown()
509 */
510 static int
511 key_shutdown(struct socket *so)
512 {
513 int error;
514 error = raw_usrreqs.pru_shutdown(so);
515 return error;
516 }
517
518 /*
519 * key_sockaddr()
520 * derived from net/rtsock.c:rts_sockaddr()
521 */
522 static int
523 key_sockaddr(struct socket *so, struct sockaddr **nam)
524 {
525 int error;
526 error = raw_usrreqs.pru_sockaddr(so, nam);
527 return error;
528 }
529
530 static struct pr_usrreqs key_usrreqs = {
531 .pru_abort = key_abort,
532 .pru_attach = key_attach,
533 .pru_bind = key_bind,
534 .pru_connect = key_connect,
535 .pru_detach = key_detach,
536 .pru_disconnect = key_disconnect,
537 .pru_peeraddr = key_peeraddr,
538 .pru_send = key_send,
539 .pru_shutdown = key_shutdown,
540 .pru_sockaddr = key_sockaddr,
541 .pru_sosend = sosend,
542 .pru_soreceive = soreceive,
543 };
544
545 /* sysctl */
546 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Key Family");
547
548 /*
549 * Definitions of protocols supported in the KEY domain.
550 */
551
552 extern struct domain keydomain_s;
553
554 static struct protosw keysw[] = {
555 {
556 .pr_type = SOCK_RAW,
557 .pr_protocol = PF_KEY_V2,
558 .pr_flags = PR_ATOMIC | PR_ADDR,
559 .pr_output = key_output,
560 .pr_ctlinput = raw_ctlinput,
561 .pr_init = key_init,
562 .pr_usrreqs = &key_usrreqs,
563 }
564 };
565
566 static int key_proto_count = (sizeof(keysw) / sizeof(struct protosw));
567
568 struct domain keydomain_s = {
569 .dom_family = PF_KEY,
570 .dom_name = "key",
571 .dom_init = key_dinit,
572 .dom_maxrtkey = sizeof(struct key_cb),
573 };
574
575 static void
576 key_dinit(struct domain *dp)
577 {
578 struct protosw *pr;
579 int i;
580
581 VERIFY(!(dp->dom_flags & DOM_INITIALIZED));
582 VERIFY(keydomain == NULL);
583
584 keydomain = dp;
585
586 for (i = 0, pr = &keysw[0]; i < key_proto_count; i++, pr++) {
587 net_add_proto(pr, dp, 1);
588 }
589 }