keysock.c revision 1.48 1 /* $NetBSD: keysock.c,v 1.48 2015/05/02 17:18:04 rtr Exp $ */
2 /* $FreeBSD: src/sys/netipsec/keysock.c,v 1.3.2.1 2003/01/24 05:11:36 sam Exp $ */
3 /* $KAME: keysock.c,v 1.25 2001/08/13 20:07:41 itojun Exp $ */
4
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: keysock.c,v 1.48 2015/05/02 17:18:04 rtr Exp $");
36
37 /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
38
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/domain.h>
42 #include <sys/errno.h>
43 #include <sys/kernel.h>
44 #include <sys/kmem.h>
45 #include <sys/mbuf.h>
46 #include <sys/protosw.h>
47 #include <sys/signalvar.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
52
53 #include <net/raw_cb.h>
54 #include <net/route.h>
55
56 #include <net/pfkeyv2.h>
57 #include <netipsec/key.h>
58 #include <netipsec/keysock.h>
59 #include <netipsec/key_debug.h>
60
61 #include <netipsec/ipsec_osdep.h>
62 #include <netipsec/ipsec_private.h>
63
64 typedef int pr_output_t (struct mbuf *, struct socket *);
65
66 struct key_cb {
67 int key_count;
68 int any_count;
69 };
70 static struct key_cb key_cb;
71
72 static struct sockaddr key_dst = {
73 .sa_len = 2,
74 .sa_family = PF_KEY,
75 };
76 static struct sockaddr key_src = {
77 .sa_len = 2,
78 .sa_family = PF_KEY,
79 };
80
81
82 static int key_sendup0(struct rawcb *, struct mbuf *, int, int);
83
84 int key_registered_sb_max = (2048 * MHLEN); /* XXX arbitrary */
85
86 /*
87 * key_output()
88 */
89 int
90 key_output(struct mbuf *m, ...)
91 {
92 struct sadb_msg *msg;
93 int len, error = 0;
94 int s;
95 struct socket *so;
96 va_list ap;
97
98 va_start(ap, m);
99 so = va_arg(ap, struct socket *);
100 va_end(ap);
101
102 if (m == 0)
103 panic("key_output: NULL pointer was passed");
104
105 {
106 uint64_t *ps = PFKEY_STAT_GETREF();
107 ps[PFKEY_STAT_OUT_TOTAL]++;
108 ps[PFKEY_STAT_OUT_BYTES] += m->m_pkthdr.len;
109 PFKEY_STAT_PUTREF();
110 }
111
112 len = m->m_pkthdr.len;
113 if (len < sizeof(struct sadb_msg)) {
114 PFKEY_STATINC(PFKEY_STAT_OUT_TOOSHORT);
115 error = EINVAL;
116 goto end;
117 }
118
119 if (m->m_len < sizeof(struct sadb_msg)) {
120 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
121 PFKEY_STATINC(PFKEY_STAT_OUT_NOMEM);
122 error = ENOBUFS;
123 goto end;
124 }
125 }
126
127 if ((m->m_flags & M_PKTHDR) == 0)
128 panic("key_output: not M_PKTHDR ??");
129
130 KEYDEBUG(KEYDEBUG_KEY_DUMP, kdebug_mbuf(m));
131
132 msg = mtod(m, struct sadb_msg *);
133 PFKEY_STATINC(PFKEY_STAT_OUT_MSGTYPE + msg->sadb_msg_type);
134 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
135 PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN);
136 error = EINVAL;
137 goto end;
138 }
139
140 /*XXX giant lock*/
141 s = splsoftnet();
142 error = key_parse(m, so);
143 m = NULL;
144 splx(s);
145 end:
146 if (m)
147 m_freem(m);
148 return error;
149 }
150
151 /*
152 * send message to the socket.
153 */
154 static int
155 key_sendup0(
156 struct rawcb *rp,
157 struct mbuf *m,
158 int promisc,
159 int sbprio
160 )
161 {
162 int error;
163 int ok;
164
165 if (promisc) {
166 struct sadb_msg *pmsg;
167
168 M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT);
169 if (m && m->m_len < sizeof(struct sadb_msg))
170 m = m_pullup(m, sizeof(struct sadb_msg));
171 if (!m) {
172 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
173 return ENOBUFS;
174 }
175 m->m_pkthdr.len += sizeof(*pmsg);
176
177 pmsg = mtod(m, struct sadb_msg *);
178 memset(pmsg, 0, sizeof(*pmsg));
179 pmsg->sadb_msg_version = PF_KEY_V2;
180 pmsg->sadb_msg_type = SADB_X_PROMISC;
181 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
182 /* pid and seq? */
183
184 PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + pmsg->sadb_msg_type);
185 }
186
187 if (sbprio == 0)
188 ok = sbappendaddr(&rp->rcb_socket->so_rcv,
189 (struct sockaddr *)&key_src, m, NULL);
190 else
191 ok = sbappendaddrchain(&rp->rcb_socket->so_rcv,
192 (struct sockaddr *)&key_src, m, sbprio);
193
194 if (!ok) {
195 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
196 m_freem(m);
197 error = ENOBUFS;
198 } else
199 error = 0;
200 sorwakeup(rp->rcb_socket);
201 return error;
202 }
203
204 /* XXX this interface should be obsoleted. */
205 int
206 key_sendup(struct socket *so, struct sadb_msg *msg, u_int len,
207 int target) /*target of the resulting message*/
208 {
209 struct mbuf *m, *n, *mprev;
210 int tlen;
211
212 /* sanity check */
213 if (so == 0 || msg == 0)
214 panic("key_sendup: NULL pointer was passed");
215
216 KEYDEBUG(KEYDEBUG_KEY_DUMP,
217 printf("key_sendup: \n");
218 kdebug_sadb(msg));
219
220 /*
221 * we increment statistics here, just in case we have ENOBUFS
222 * in this function.
223 */
224 {
225 uint64_t *ps = PFKEY_STAT_GETREF();
226 ps[PFKEY_STAT_IN_TOTAL]++;
227 ps[PFKEY_STAT_IN_BYTES] += len;
228 ps[PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type]++;
229 PFKEY_STAT_PUTREF();
230 }
231
232 /*
233 * Get mbuf chain whenever possible (not clusters),
234 * to save socket buffer. We'll be generating many SADB_ACQUIRE
235 * messages to listening key sockets. If we simply allocate clusters,
236 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
237 * sbspace() computes # of actual data bytes AND mbuf region.
238 *
239 * TODO: SADB_ACQUIRE filters should be implemented.
240 */
241 tlen = len;
242 m = mprev = NULL;
243 while (tlen > 0) {
244 int mlen;
245 if (tlen == len) {
246 MGETHDR(n, M_DONTWAIT, MT_DATA);
247 mlen = MHLEN;
248 } else {
249 MGET(n, M_DONTWAIT, MT_DATA);
250 mlen = MLEN;
251 }
252 if (!n) {
253 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
254 return ENOBUFS;
255 }
256 n->m_len = mlen;
257 if (tlen >= MCLBYTES) { /*XXX better threshold? */
258 MCLGET(n, M_DONTWAIT);
259 if ((n->m_flags & M_EXT) == 0) {
260 m_free(n);
261 m_freem(m);
262 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
263 return ENOBUFS;
264 }
265 n->m_len = MCLBYTES;
266 }
267
268 if (tlen < n->m_len)
269 n->m_len = tlen;
270 n->m_next = NULL;
271 if (m == NULL)
272 m = mprev = n;
273 else {
274 mprev->m_next = n;
275 mprev = n;
276 }
277 tlen -= n->m_len;
278 n = NULL;
279 }
280 m->m_pkthdr.len = len;
281 m->m_pkthdr.rcvif = NULL;
282 m_copyback(m, 0, len, msg);
283
284 /* avoid duplicated statistics */
285 {
286 uint64_t *ps = PFKEY_STAT_GETREF();
287 ps[PFKEY_STAT_IN_TOTAL]--;
288 ps[PFKEY_STAT_IN_BYTES] -= len;
289 ps[PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type]--;
290 PFKEY_STAT_PUTREF();
291 }
292
293 return key_sendup_mbuf(so, m, target);
294 }
295
296 /* so can be NULL if target != KEY_SENDUP_ONE */
297 int
298 key_sendup_mbuf(struct socket *so, struct mbuf *m,
299 int target/*, sbprio */)
300 {
301 struct mbuf *n;
302 struct keycb *kp;
303 int sendup;
304 struct rawcb *rp;
305 int error = 0;
306 int sbprio = 0; /* XXX should be a parameter */
307
308 if (m == NULL)
309 panic("key_sendup_mbuf: NULL pointer was passed");
310 if (so == NULL && target == KEY_SENDUP_ONE)
311 panic("key_sendup_mbuf: NULL pointer was passed");
312
313 /*
314 * RFC 2367 says ACQUIRE and other kernel-generated messages
315 * are special. We treat all KEY_SENDUP_REGISTERED messages
316 * as special, delivering them to all registered sockets
317 * even if the socket is at or above its so->so_rcv.sb_max limits.
318 * The only constraint is that the so_rcv data fall below
319 * key_registered_sb_max.
320 * Doing that check here avoids reworking every key_sendup_mbuf()
321 * in the short term. . The rework will be done after a technical
322 * conensus that this approach is appropriate.
323 */
324 if (target == KEY_SENDUP_REGISTERED) {
325 sbprio = SB_PRIO_BESTEFFORT;
326 }
327
328 {
329 uint64_t *ps = PFKEY_STAT_GETREF();
330 ps[PFKEY_STAT_IN_TOTAL]++;
331 ps[PFKEY_STAT_IN_BYTES] += m->m_pkthdr.len;
332 PFKEY_STAT_PUTREF();
333 }
334 if (m->m_len < sizeof(struct sadb_msg)) {
335 #if 1
336 m = m_pullup(m, sizeof(struct sadb_msg));
337 if (m == NULL) {
338 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
339 return ENOBUFS;
340 }
341 #else
342 /* don't bother pulling it up just for stats */
343 #endif
344 }
345 if (m->m_len >= sizeof(struct sadb_msg)) {
346 struct sadb_msg *msg;
347 msg = mtod(m, struct sadb_msg *);
348 PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type);
349 }
350
351 LIST_FOREACH(rp, &rawcb_list, rcb_list)
352 {
353 struct socket * kso = rp->rcb_socket;
354 if (rp->rcb_proto.sp_family != PF_KEY)
355 continue;
356 if (rp->rcb_proto.sp_protocol
357 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
358 continue;
359 }
360
361 kp = (struct keycb *)rp;
362
363 /*
364 * If you are in promiscuous mode, and when you get broadcasted
365 * reply, you'll get two PF_KEY messages.
366 * (based on pf_key (at) inner.net message on 14 Oct 1998)
367 */
368 if (((struct keycb *)rp)->kp_promisc) {
369 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
370 (void)key_sendup0(rp, n, 1, 0);
371 n = NULL;
372 }
373 }
374
375 /* the exact target will be processed later */
376 if (so && sotorawcb(so) == rp)
377 continue;
378
379 sendup = 0;
380 switch (target) {
381 case KEY_SENDUP_ONE:
382 /* the statement has no effect */
383 if (so && sotorawcb(so) == rp)
384 sendup++;
385 break;
386 case KEY_SENDUP_ALL:
387 sendup++;
388 break;
389 case KEY_SENDUP_REGISTERED:
390 if (kp->kp_registered) {
391 if (kso->so_rcv.sb_cc <= key_registered_sb_max)
392 sendup++;
393 else
394 printf("keysock: "
395 "registered sendup dropped, "
396 "sb_cc %ld max %d\n",
397 kso->so_rcv.sb_cc,
398 key_registered_sb_max);
399 }
400 break;
401 }
402 PFKEY_STATINC(PFKEY_STAT_IN_MSGTARGET + target);
403
404 if (!sendup)
405 continue;
406
407 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
408 m_freem(m);
409 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
410 return ENOBUFS;
411 }
412
413 if ((error = key_sendup0(rp, n, 0, 0)) != 0) {
414 m_freem(m);
415 return error;
416 }
417
418 n = NULL;
419 }
420
421 /* The 'later' time for processing the exact target has arrived */
422 if (so) {
423 error = key_sendup0(sotorawcb(so), m, 0, sbprio);
424 m = NULL;
425 } else {
426 error = 0;
427 m_freem(m);
428 }
429 return error;
430 }
431
432 static int
433 key_attach(struct socket *so, int proto)
434 {
435 struct keycb *kp;
436 int s, error;
437
438 KASSERT(sotorawcb(so) == NULL);
439 kp = kmem_zalloc(sizeof(*kp), KM_SLEEP);
440 kp->kp_raw.rcb_len = sizeof(*kp);
441 so->so_pcb = kp;
442
443 s = splsoftnet();
444 error = raw_attach(so, proto);
445 if (error) {
446 PFKEY_STATINC(PFKEY_STAT_SOCKERR);
447 kmem_free(kp, sizeof(*kp));
448 so->so_pcb = NULL;
449 goto out;
450 }
451
452 kp->kp_promisc = kp->kp_registered = 0;
453
454 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
455 key_cb.key_count++;
456 key_cb.any_count++;
457 kp->kp_raw.rcb_laddr = &key_src;
458 kp->kp_raw.rcb_faddr = &key_dst;
459 soisconnected(so);
460 so->so_options |= SO_USELOOPBACK;
461 out:
462 KASSERT(solocked(so));
463 splx(s);
464 return error;
465 }
466
467 static void
468 key_detach(struct socket *so)
469 {
470 struct keycb *kp = (struct keycb *)sotorawcb(so);
471 int s;
472
473 KASSERT(solocked(so));
474 KASSERT(kp != NULL);
475
476 s = splsoftnet();
477 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
478 key_cb.key_count--;
479 key_cb.any_count--;
480 key_freereg(so);
481 raw_detach(so);
482 splx(s);
483 }
484
485 static int
486 key_accept(struct socket *so, struct sockaddr *nam)
487 {
488 KASSERT(solocked(so));
489
490 panic("key_accept");
491
492 return EOPNOTSUPP;
493 }
494
495 static int
496 key_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
497 {
498 KASSERT(solocked(so));
499
500 return EOPNOTSUPP;
501 }
502
503 static int
504 key_listen(struct socket *so, struct lwp *l)
505 {
506 KASSERT(solocked(so));
507
508 return EOPNOTSUPP;
509 }
510
511 static int
512 key_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
513 {
514 KASSERT(solocked(so));
515
516 return EOPNOTSUPP;
517 }
518
519 static int
520 key_connect2(struct socket *so, struct socket *so2)
521 {
522 KASSERT(solocked(so));
523
524 return EOPNOTSUPP;
525 }
526
527 static int
528 key_disconnect(struct socket *so)
529 {
530 struct rawcb *rp = sotorawcb(so);
531 int s;
532
533 KASSERT(solocked(so));
534 KASSERT(rp != NULL);
535
536 s = splsoftnet();
537 soisdisconnected(so);
538 raw_disconnect(rp);
539 splx(s);
540
541 return 0;
542 }
543
544 static int
545 key_shutdown(struct socket *so)
546 {
547 int s;
548
549 KASSERT(solocked(so));
550
551 /*
552 * Mark the connection as being incapable of further input.
553 */
554 s = splsoftnet();
555 socantsendmore(so);
556 splx(s);
557
558 return 0;
559 }
560
561 static int
562 key_abort(struct socket *so)
563 {
564 KASSERT(solocked(so));
565
566 panic("key_abort");
567
568 return EOPNOTSUPP;
569 }
570
571 static int
572 key_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
573 {
574 return EOPNOTSUPP;
575 }
576
577 static int
578 key_stat(struct socket *so, struct stat *ub)
579 {
580 KASSERT(solocked(so));
581
582 return 0;
583 }
584
585 static int
586 key_peeraddr(struct socket *so, struct sockaddr *nam)
587 {
588 struct rawcb *rp = sotorawcb(so);
589
590 KASSERT(solocked(so));
591 KASSERT(rp != NULL);
592 KASSERT(nam != NULL);
593
594 if (rp->rcb_faddr == NULL)
595 return ENOTCONN;
596
597 raw_setpeeraddr(rp, nam);
598 return 0;
599 }
600
601 static int
602 key_sockaddr(struct socket *so, struct sockaddr *nam)
603 {
604 struct rawcb *rp = sotorawcb(so);
605
606 KASSERT(solocked(so));
607 KASSERT(rp != NULL);
608 KASSERT(nam != NULL);
609
610 if (rp->rcb_faddr == NULL)
611 return ENOTCONN;
612
613 raw_setsockaddr(rp, nam);
614 return 0;
615 }
616
617 static int
618 key_rcvd(struct socket *so, int flags, struct lwp *l)
619 {
620 KASSERT(solocked(so));
621
622 return EOPNOTSUPP;
623 }
624
625 static int
626 key_recvoob(struct socket *so, struct mbuf *m, int flags)
627 {
628 KASSERT(solocked(so));
629
630 return EOPNOTSUPP;
631 }
632
633 static int
634 key_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
635 struct mbuf *control, struct lwp *l)
636 {
637 int error = 0;
638 int s;
639
640 KASSERT(solocked(so));
641
642 s = splsoftnet();
643 error = raw_send(so, m, nam, control, l);
644 splx(s);
645
646 return error;
647 }
648
649 static int
650 key_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
651 {
652 KASSERT(solocked(so));
653
654 m_freem(m);
655 m_freem(control);
656
657 return EOPNOTSUPP;
658 }
659
660 static int
661 key_purgeif(struct socket *so, struct ifnet *ifa)
662 {
663
664 panic("key_purgeif");
665
666 return EOPNOTSUPP;
667 }
668
669 /*
670 * Definitions of protocols supported in the KEY domain.
671 */
672
673 DOMAIN_DEFINE(keydomain);
674
675 PR_WRAP_USRREQS(key)
676 #define key_attach key_attach_wrapper
677 #define key_detach key_detach_wrapper
678 #define key_accept key_accept_wrapper
679 #define key_bind key_bind_wrapper
680 #define key_listen key_listen_wrapper
681 #define key_connect key_connect_wrapper
682 #define key_connect2 key_connect2_wrapper
683 #define key_disconnect key_disconnect_wrapper
684 #define key_shutdown key_shutdown_wrapper
685 #define key_abort key_abort_wrapper
686 #define key_ioctl key_ioctl_wrapper
687 #define key_stat key_stat_wrapper
688 #define key_peeraddr key_peeraddr_wrapper
689 #define key_sockaddr key_sockaddr_wrapper
690 #define key_rcvd key_rcvd_wrapper
691 #define key_recvoob key_recvoob_wrapper
692 #define key_send key_send_wrapper
693 #define key_sendoob key_sendoob_wrapper
694 #define key_purgeif key_purgeif_wrapper
695
696 const struct pr_usrreqs key_usrreqs = {
697 .pr_attach = key_attach,
698 .pr_detach = key_detach,
699 .pr_accept = key_accept,
700 .pr_bind = key_bind,
701 .pr_listen = key_listen,
702 .pr_connect = key_connect,
703 .pr_connect2 = key_connect2,
704 .pr_disconnect = key_disconnect,
705 .pr_shutdown = key_shutdown,
706 .pr_abort = key_abort,
707 .pr_ioctl = key_ioctl,
708 .pr_stat = key_stat,
709 .pr_peeraddr = key_peeraddr,
710 .pr_sockaddr = key_sockaddr,
711 .pr_rcvd = key_rcvd,
712 .pr_recvoob = key_recvoob,
713 .pr_send = key_send,
714 .pr_sendoob = key_sendoob,
715 .pr_purgeif = key_purgeif,
716 };
717
718 const struct protosw keysw[] = {
719 {
720 .pr_type = SOCK_RAW,
721 .pr_domain = &keydomain,
722 .pr_protocol = PF_KEY_V2,
723 .pr_flags = PR_ATOMIC|PR_ADDR,
724 .pr_output = key_output,
725 .pr_ctlinput = raw_ctlinput,
726 .pr_usrreqs = &key_usrreqs,
727 .pr_init = raw_init,
728 }
729 };
730
731 struct domain keydomain = {
732 .dom_family = PF_KEY,
733 .dom_name = "key",
734 .dom_init = key_init,
735 .dom_protosw = keysw,
736 .dom_protoswNPROTOSW = &keysw[__arraycount(keysw)],
737 };
738