]> git.saurik.com Git - apple/xnu.git/blob - bsd/netkey/keysock.c
xnu-344.49.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/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/errno.h>
46
47
48 #include <net/raw_cb.h>
49 #include <net/route.h>
50
51 #include <net/pfkeyv2.h>
52 #include <netkey/keydb.h>
53 #include <netkey/key.h>
54 #include <netkey/keysock.h>
55 #include <netkey/key_debug.h>
56
57 struct sockaddr key_dst = { 2, PF_KEY, };
58 struct sockaddr key_src = { 2, PF_KEY, };
59
60 static int key_sendup0 __P((struct rawcb *, struct mbuf *, int));
61
62 struct pfkeystat pfkeystat;
63
64 /*
65 * key_output()
66 */
67 int
68 #ifdef __APPLE__
69 /* No variable argument support? */
70 key_output(struct mbuf *m, struct socket *so)
71 #else
72 #if __STDC__
73 key_output(struct mbuf *m, ...)
74 #else
75 key_output(m, va_alist)
76 struct mbuf *m;
77 va_dcl
78 #endif
79 #endif
80 {
81 struct sadb_msg *msg;
82 int len, error = 0;
83 int s;
84 #ifndef __APPLE__
85 struct socket *so;
86 va_list ap;
87
88 va_start(ap, m);
89 so = va_arg(ap, struct socket *);
90 va_end(ap);
91 #endif
92
93 if (m == 0)
94 panic("key_output: NULL pointer was passed.\n");
95
96 pfkeystat.out_total++;
97 pfkeystat.out_bytes += m->m_pkthdr.len;
98
99 len = m->m_pkthdr.len;
100 if (len < sizeof(struct sadb_msg)) {
101 #if IPSEC_DEBUG
102 printf("key_output: Invalid message length.\n");
103 #endif
104 pfkeystat.out_tooshort++;
105 error = EINVAL;
106 goto end;
107 }
108
109 if (m->m_len < sizeof(struct sadb_msg)) {
110 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
111 #if IPSEC_DEBUG
112 printf("key_output: can't pullup mbuf\n");
113 #endif
114 pfkeystat.out_nomem++;
115 error = ENOBUFS;
116 goto end;
117 }
118 }
119
120 if ((m->m_flags & M_PKTHDR) == 0)
121 panic("key_output: not M_PKTHDR ??");
122
123 #if IPSEC_DEBUG
124 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
125 #endif /* defined(IPSEC_DEBUG) */
126
127 msg = mtod(m, struct sadb_msg *);
128 pfkeystat.out_msgtype[msg->sadb_msg_type]++;
129 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
130 #if IPSEC_DEBUG
131 printf("key_output: Invalid message length.\n");
132 #endif
133 pfkeystat.out_invlen++;
134 error = EINVAL;
135 goto end;
136 }
137
138 /*XXX giant lock*/
139 s = splnet();
140 error = key_parse(m, so);
141 m = NULL;
142 splx(s);
143 end:
144 if (m)
145 m_freem(m);
146 return error;
147 }
148
149 /*
150 * send message to the socket.
151 */
152 static int
153 key_sendup0(rp, m, promisc)
154 struct rawcb *rp;
155 struct mbuf *m;
156 int promisc;
157 {
158 int error;
159
160 if (promisc) {
161 struct sadb_msg *pmsg;
162
163 M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT);
164 if (m && m->m_len < sizeof(struct sadb_msg))
165 m = m_pullup(m, sizeof(struct sadb_msg));
166 if (!m) {
167 #if IPSEC_DEBUG
168 printf("key_sendup0: cannot pullup\n");
169 #endif
170 pfkeystat.in_nomem++;
171 m_freem(m);
172 return ENOBUFS;
173 }
174 m->m_pkthdr.len += sizeof(*pmsg);
175
176 pmsg = mtod(m, struct sadb_msg *);
177 bzero(pmsg, sizeof(*pmsg));
178 pmsg->sadb_msg_version = PF_KEY_V2;
179 pmsg->sadb_msg_type = SADB_X_PROMISC;
180 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
181 /* pid and seq? */
182
183 pfkeystat.in_msgtype[pmsg->sadb_msg_type]++;
184 }
185
186 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
187 m, NULL)) {
188 #if IPSEC_DEBUG
189 printf("key_sendup0: sbappendaddr failed\n");
190 #endif
191 pfkeystat.in_nomem++;
192 m_freem(m);
193 error = ENOBUFS;
194 } else
195 error = 0;
196 sorwakeup(rp->rcb_socket);
197 return error;
198 }
199
200 /* XXX this interface should be obsoleted. */
201 int
202 key_sendup(so, msg, len, target)
203 struct socket *so;
204 struct sadb_msg *msg;
205 u_int len;
206 int target; /*target of the resulting message*/
207 {
208 struct mbuf *m, *n, *mprev;
209 int tlen;
210
211 /* sanity check */
212 if (so == 0 || msg == 0)
213 panic("key_sendup: NULL pointer was passed.\n");
214
215 KEYDEBUG(KEYDEBUG_KEY_DUMP,
216 printf("key_sendup: \n");
217 kdebug_sadb(msg));
218
219 /*
220 * we increment statistics here, just in case we have ENOBUFS
221 * in this function.
222 */
223 pfkeystat.in_total++;
224 pfkeystat.in_bytes += len;
225 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
226
227 /*
228 * Get mbuf chain whenever possible (not clusters),
229 * to save socket buffer. We'll be generating many SADB_ACQUIRE
230 * messages to listening key sockets. If we simply allocate clusters,
231 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
232 * sbspace() computes # of actual data bytes AND mbuf region.
233 *
234 * TODO: SADB_ACQUIRE filters should be implemented.
235 */
236 tlen = len;
237 m = mprev = NULL;
238 while (tlen > 0) {
239 if (tlen == len) {
240 MGETHDR(n, M_DONTWAIT, MT_DATA);
241 n->m_len = MHLEN;
242 } else {
243 MGET(n, M_DONTWAIT, MT_DATA);
244 n->m_len = MLEN;
245 }
246 if (!n) {
247 pfkeystat.in_nomem++;
248 return ENOBUFS;
249 }
250 if (tlen >= MCLBYTES) { /*XXX better threshold? */
251 MCLGET(n, M_DONTWAIT);
252 if ((n->m_flags & M_EXT) == 0) {
253 m_free(n);
254 m_freem(m);
255 pfkeystat.in_nomem++;
256 return ENOBUFS;
257 }
258 n->m_len = MCLBYTES;
259 }
260
261 if (tlen < n->m_len)
262 n->m_len = tlen;
263 n->m_next = NULL;
264 if (m == NULL)
265 m = mprev = n;
266 else {
267 mprev->m_next = n;
268 mprev = n;
269 }
270 tlen -= n->m_len;
271 n = NULL;
272 }
273 m->m_pkthdr.len = len;
274 m->m_pkthdr.rcvif = NULL;
275 m_copyback(m, 0, len, (caddr_t)msg);
276
277 /* avoid duplicated statistics */
278 pfkeystat.in_total--;
279 pfkeystat.in_bytes -= len;
280 pfkeystat.in_msgtype[msg->sadb_msg_type]--;
281
282 return key_sendup_mbuf(so, m, target);
283 }
284
285 /* so can be NULL if target != KEY_SENDUP_ONE */
286 int
287 key_sendup_mbuf(so, m, target)
288 struct socket *so;
289 struct mbuf *m;
290 int target;
291 {
292 struct mbuf *n;
293 struct keycb *kp;
294 int sendup;
295 struct rawcb *rp;
296 int error = 0;
297
298 if (m == NULL)
299 panic("key_sendup_mbuf: NULL pointer was passed.\n");
300 if (so == NULL && target == KEY_SENDUP_ONE)
301 panic("key_sendup_mbuf: NULL pointer was passed.\n");
302
303 pfkeystat.in_total++;
304 pfkeystat.in_bytes += m->m_pkthdr.len;
305 if (m->m_len < sizeof(struct sadb_msg)) {
306 #if 1
307 m = m_pullup(m, sizeof(struct sadb_msg));
308 if (m == NULL) {
309 pfkeystat.in_nomem++;
310 return ENOBUFS;
311 }
312 #else
313 /* don't bother pulling it up just for stats */
314 #endif
315 }
316 if (m->m_len >= sizeof(struct sadb_msg)) {
317 struct sadb_msg *msg;
318 msg = mtod(m, struct sadb_msg *);
319 pfkeystat.in_msgtype[msg->sadb_msg_type]++;
320 }
321
322 LIST_FOREACH(rp, &rawcb_list, list)
323 {
324 if (rp->rcb_proto.sp_family != PF_KEY)
325 continue;
326 if (rp->rcb_proto.sp_protocol
327 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
328 continue;
329 }
330
331 kp = (struct keycb *)rp;
332
333 /*
334 * If you are in promiscuous mode, and when you get broadcasted
335 * reply, you'll get two PF_KEY messages.
336 * (based on pf_key@inner.net message on 14 Oct 1998)
337 */
338 if (((struct keycb *)rp)->kp_promisc) {
339 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
340 (void)key_sendup0(rp, n, 1);
341 n = NULL;
342 }
343 }
344
345 /* the exact target will be processed later */
346 if (so && sotorawcb(so) == rp)
347 continue;
348
349 sendup = 0;
350 switch (target) {
351 case KEY_SENDUP_ONE:
352 /* the statement has no effect */
353 if (so && sotorawcb(so) == rp)
354 sendup++;
355 break;
356 case KEY_SENDUP_ALL:
357 sendup++;
358 break;
359 case KEY_SENDUP_REGISTERED:
360 if (kp->kp_registered)
361 sendup++;
362 break;
363 }
364 pfkeystat.in_msgtarget[target]++;
365
366 if (!sendup)
367 continue;
368
369 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
370 #if IPSEC_DEBUG
371 printf("key_sendup: m_copy fail\n");
372 #endif
373 m_freem(m);
374 pfkeystat.in_nomem++;
375 return ENOBUFS;
376 }
377
378 if ((error = key_sendup0(rp, n, 0)) != 0) {
379 m_freem(m);
380 return error;
381 }
382
383 n = NULL;
384 }
385
386 if (so) {
387 error = key_sendup0(sotorawcb(so), m, 0);
388 m = NULL;
389 } else {
390 error = 0;
391 m_freem(m);
392 }
393 return error;
394 }
395
396 /*
397 * key_abort()
398 * derived from net/rtsock.c:rts_abort()
399 */
400 static int
401 key_abort(struct socket *so)
402 {
403 int s, error;
404 s = splnet();
405 error = raw_usrreqs.pru_abort(so);
406 splx(s);
407 return error;
408 }
409
410 /*
411 * key_attach()
412 * derived from net/rtsock.c:rts_attach()
413 */
414 static int
415 key_attach(struct socket *so, int proto, struct proc *p)
416 {
417 struct keycb *kp;
418 int s, error;
419
420 if (sotorawcb(so) != 0)
421 return EISCONN; /* XXX panic? */
422 kp = (struct keycb *)_MALLOC(sizeof *kp, M_PCB, M_WAITOK); /* XXX */
423 if (kp == 0)
424 return ENOBUFS;
425 bzero(kp, sizeof *kp);
426
427 /*
428 * The splnet() is necessary to block protocols from sending
429 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
430 * this PCB is extant but incompletely initialized.
431 * Probably we should try to do more of this work beforehand and
432 * eliminate the spl.
433 */
434 s = splnet();
435 so->so_pcb = (caddr_t)kp;
436 error = raw_usrreqs.pru_attach(so, proto, p);
437 kp = (struct keycb *)sotorawcb(so);
438 if (error) {
439 _FREE(kp, M_PCB);
440 so->so_pcb = (caddr_t) 0;
441 splx(s);
442 printf("key_usrreq: key_usrreq results %d\n", error);
443 return error;
444 }
445
446 kp->kp_promisc = kp->kp_registered = 0;
447
448 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
449 key_cb.key_count++;
450 key_cb.any_count++;
451 kp->kp_raw.rcb_laddr = &key_src;
452 kp->kp_raw.rcb_faddr = &key_dst;
453 soisconnected(so);
454 so->so_options |= SO_USELOOPBACK;
455
456 splx(s);
457 return 0;
458 }
459
460 /*
461 * key_bind()
462 * derived from net/rtsock.c:rts_bind()
463 */
464 static int
465 key_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
466 {
467 int s, error;
468 s = splnet();
469 error = raw_usrreqs.pru_bind(so, nam, p); /* xxx just EINVAL */
470 splx(s);
471 return error;
472 }
473
474 /*
475 * key_connect()
476 * derived from net/rtsock.c:rts_connect()
477 */
478 static int
479 key_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
480 {
481 int s, error;
482 s = splnet();
483 error = raw_usrreqs.pru_connect(so, nam, p); /* XXX just EINVAL */
484 splx(s);
485 return error;
486 }
487
488 /*
489 * key_detach()
490 * derived from net/rtsock.c:rts_detach()
491 */
492 static int
493 key_detach(struct socket *so)
494 {
495 struct keycb *kp = (struct keycb *)sotorawcb(so);
496 int s, error;
497
498 s = splnet();
499 if (kp != 0) {
500 if (kp->kp_raw.rcb_proto.sp_protocol
501 == PF_KEY) /* XXX: AF_KEY */
502 key_cb.key_count--;
503 key_cb.any_count--;
504
505 key_freereg(so);
506 }
507 error = raw_usrreqs.pru_detach(so);
508 splx(s);
509 return error;
510 }
511
512 /*
513 * key_disconnect()
514 * derived from net/rtsock.c:key_disconnect()
515 */
516 static int
517 key_disconnect(struct socket *so)
518 {
519 int s, error;
520 s = splnet();
521 error = raw_usrreqs.pru_disconnect(so);
522 splx(s);
523 return error;
524 }
525
526 /*
527 * key_peeraddr()
528 * derived from net/rtsock.c:rts_peeraddr()
529 */
530 static int
531 key_peeraddr(struct socket *so, struct sockaddr **nam)
532 {
533 int s, error;
534 s = splnet();
535 error = raw_usrreqs.pru_peeraddr(so, nam);
536 splx(s);
537 return error;
538 }
539
540 /*
541 * key_send()
542 * derived from net/rtsock.c:rts_send()
543 */
544 static int
545 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
546 struct mbuf *control, struct proc *p)
547 {
548 int s, error;
549 s = splnet();
550 error = raw_usrreqs.pru_send(so, flags, m, nam, control, p);
551 splx(s);
552 return error;
553 }
554
555 /*
556 * key_shutdown()
557 * derived from net/rtsock.c:rts_shutdown()
558 */
559 static int
560 key_shutdown(struct socket *so)
561 {
562 int s, error;
563 s = splnet();
564 error = raw_usrreqs.pru_shutdown(so);
565 splx(s);
566 return error;
567 }
568
569 /*
570 * key_sockaddr()
571 * derived from net/rtsock.c:rts_sockaddr()
572 */
573 static int
574 key_sockaddr(struct socket *so, struct sockaddr **nam)
575 {
576 int s, error;
577 s = splnet();
578 error = raw_usrreqs.pru_sockaddr(so, nam);
579 splx(s);
580 return error;
581 }
582
583 struct pr_usrreqs key_usrreqs = {
584 key_abort, pru_accept_notsupp, key_attach, key_bind,
585 key_connect,
586 pru_connect2_notsupp, pru_control_notsupp, key_detach,
587 key_disconnect, pru_listen_notsupp, key_peeraddr,
588 pru_rcvd_notsupp,
589 pru_rcvoob_notsupp, key_send, pru_sense_null, key_shutdown,
590 key_sockaddr, sosend, soreceive, sopoll
591 };
592
593 /* sysctl */
594 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
595
596 /*
597 * Definitions of protocols supported in the KEY domain.
598 */
599
600 extern struct domain keydomain;
601
602 struct protosw keysw[] = {
603 { SOCK_RAW, &keydomain, PF_KEY_V2, PR_ATOMIC|PR_ADDR,
604 0, key_output, raw_ctlinput, 0,
605 0,
606 raw_init, 0, 0, 0,
607 0, &key_usrreqs
608 }
609 };
610
611 struct domain keydomain =
612 { PF_KEY, "key", key_init, 0, 0,
613 keysw, 0,
614 0,0,
615 sizeof(struct key_cb), 0
616 };
617
618 DOMAIN_SET(key);