keysock.c revision 1.57 1 /* $NetBSD: keysock.c,v 1.57 2017/05/25 04:35:02 ozaki-r 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.57 2017/05/25 04:35:02 ozaki-r 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 #include <sys/cpu.h>
53 #include <sys/syslog.h>
54
55 #include <net/raw_cb.h>
56 #include <net/route.h>
57
58 #include <net/pfkeyv2.h>
59 #include <netipsec/key.h>
60 #include <netipsec/keysock.h>
61 #include <netipsec/key_debug.h>
62
63 #include <netipsec/ipsec_private.h>
64
65 struct key_cb {
66 int key_count;
67 int any_count;
68 };
69 static struct key_cb key_cb;
70
71 static struct sockaddr key_dst = {
72 .sa_len = 2,
73 .sa_family = PF_KEY,
74 };
75 static struct sockaddr key_src = {
76 .sa_len = 2,
77 .sa_family = PF_KEY,
78 };
79
80 static const struct protosw keysw[];
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 static int
90 key_output(struct mbuf *m, struct socket *so)
91 {
92 struct sadb_msg *msg;
93 int len, error = 0;
94 int s;
95
96 KASSERT(m != NULL);
97
98 {
99 uint64_t *ps = PFKEY_STAT_GETREF();
100 ps[PFKEY_STAT_OUT_TOTAL]++;
101 ps[PFKEY_STAT_OUT_BYTES] += m->m_pkthdr.len;
102 PFKEY_STAT_PUTREF();
103 }
104
105 len = m->m_pkthdr.len;
106 if (len < sizeof(struct sadb_msg)) {
107 PFKEY_STATINC(PFKEY_STAT_OUT_TOOSHORT);
108 error = EINVAL;
109 goto end;
110 }
111
112 if (m->m_len < sizeof(struct sadb_msg)) {
113 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == 0) {
114 PFKEY_STATINC(PFKEY_STAT_OUT_NOMEM);
115 error = ENOBUFS;
116 goto end;
117 }
118 }
119
120 KASSERT((m->m_flags & M_PKTHDR) != 0);
121
122 if (KEYDEBUG_ON(KEYDEBUG_KEY_DUMP))
123 kdebug_mbuf(m);
124
125 msg = mtod(m, struct sadb_msg *);
126 PFKEY_STATINC(PFKEY_STAT_OUT_MSGTYPE + msg->sadb_msg_type);
127 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
128 PFKEY_STATINC(PFKEY_STAT_OUT_INVLEN);
129 error = EINVAL;
130 goto end;
131 }
132
133 /*XXX giant lock*/
134 s = splsoftnet();
135 error = key_parse(m, so);
136 m = NULL;
137 splx(s);
138 end:
139 if (m)
140 m_freem(m);
141 return error;
142 }
143
144 /*
145 * send message to the socket.
146 */
147 static int
148 key_sendup0(
149 struct rawcb *rp,
150 struct mbuf *m,
151 int promisc,
152 int sbprio
153 )
154 {
155 int error;
156 int ok;
157
158 if (promisc) {
159 struct sadb_msg *pmsg;
160
161 M_PREPEND(m, sizeof(struct sadb_msg), M_DONTWAIT);
162 if (m && m->m_len < sizeof(struct sadb_msg))
163 m = m_pullup(m, sizeof(struct sadb_msg));
164 if (!m) {
165 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
166 return ENOBUFS;
167 }
168 m->m_pkthdr.len += sizeof(*pmsg);
169
170 pmsg = mtod(m, struct sadb_msg *);
171 memset(pmsg, 0, sizeof(*pmsg));
172 pmsg->sadb_msg_version = PF_KEY_V2;
173 pmsg->sadb_msg_type = SADB_X_PROMISC;
174 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
175 /* pid and seq? */
176
177 PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + pmsg->sadb_msg_type);
178 }
179
180 if (sbprio == 0)
181 ok = sbappendaddr(&rp->rcb_socket->so_rcv,
182 (struct sockaddr *)&key_src, m, NULL);
183 else
184 ok = sbappendaddrchain(&rp->rcb_socket->so_rcv,
185 (struct sockaddr *)&key_src, m, sbprio);
186
187 if (!ok) {
188 log(LOG_WARNING,
189 "%s: couldn't send PF_KEY message to the socket\n",
190 __func__);
191 PFKEY_STATINC(PFKEY_STAT_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(struct socket *so, struct sadb_msg *msg, u_int len,
203 int target) /*target of the resulting message*/
204 {
205 struct mbuf *m, *n, *mprev;
206 int tlen;
207
208 KASSERT(so != NULL);
209 KASSERT(msg != NULL);
210
211 if (KEYDEBUG_ON(KEYDEBUG_KEY_DUMP)) {
212 printf("key_sendup: \n");
213 kdebug_sadb(msg);
214 }
215
216 /*
217 * we increment statistics here, just in case we have ENOBUFS
218 * in this function.
219 */
220 {
221 uint64_t *ps = PFKEY_STAT_GETREF();
222 ps[PFKEY_STAT_IN_TOTAL]++;
223 ps[PFKEY_STAT_IN_BYTES] += len;
224 ps[PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type]++;
225 PFKEY_STAT_PUTREF();
226 }
227
228 /*
229 * Get mbuf chain whenever possible (not clusters),
230 * to save socket buffer. We'll be generating many SADB_ACQUIRE
231 * messages to listening key sockets. If we simply allocate clusters,
232 * sbappendaddr() will raise ENOBUFS due to too little sbspace().
233 * sbspace() computes # of actual data bytes AND mbuf region.
234 *
235 * TODO: SADB_ACQUIRE filters should be implemented.
236 */
237 tlen = len;
238 m = mprev = NULL;
239 while (tlen > 0) {
240 int mlen;
241 if (tlen == len) {
242 MGETHDR(n, M_DONTWAIT, MT_DATA);
243 mlen = MHLEN;
244 } else {
245 MGET(n, M_DONTWAIT, MT_DATA);
246 mlen = MLEN;
247 }
248 if (!n) {
249 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
250 return ENOBUFS;
251 }
252 n->m_len = mlen;
253 if (tlen >= MCLBYTES) { /*XXX better threshold? */
254 MCLGET(n, M_DONTWAIT);
255 if ((n->m_flags & M_EXT) == 0) {
256 m_free(n);
257 m_freem(m);
258 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
259 return ENOBUFS;
260 }
261 n->m_len = MCLBYTES;
262 }
263
264 if (tlen < n->m_len)
265 n->m_len = tlen;
266 n->m_next = NULL;
267 if (m == NULL)
268 m = mprev = n;
269 else {
270 mprev->m_next = n;
271 mprev = n;
272 }
273 tlen -= n->m_len;
274 n = NULL;
275 }
276 m->m_pkthdr.len = len;
277 m_reset_rcvif(m);
278 m_copyback(m, 0, len, msg);
279
280 /* avoid duplicated statistics */
281 {
282 uint64_t *ps = PFKEY_STAT_GETREF();
283 ps[PFKEY_STAT_IN_TOTAL]--;
284 ps[PFKEY_STAT_IN_BYTES] -= len;
285 ps[PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type]--;
286 PFKEY_STAT_PUTREF();
287 }
288
289 return key_sendup_mbuf(so, m, target);
290 }
291
292 /* so can be NULL if target != KEY_SENDUP_ONE */
293 int
294 key_sendup_mbuf(struct socket *so, struct mbuf *m,
295 int target/*, sbprio */)
296 {
297 struct mbuf *n;
298 struct keycb *kp;
299 int sendup;
300 struct rawcb *rp;
301 int error = 0;
302 int sbprio = 0; /* XXX should be a parameter */
303
304 KASSERT(m != NULL);
305 KASSERT(so != NULL || target != KEY_SENDUP_ONE);
306
307 /*
308 * RFC 2367 says ACQUIRE and other kernel-generated messages
309 * are special. We treat all KEY_SENDUP_REGISTERED messages
310 * as special, delivering them to all registered sockets
311 * even if the socket is at or above its so->so_rcv.sb_max limits.
312 * The only constraint is that the so_rcv data fall below
313 * key_registered_sb_max.
314 * Doing that check here avoids reworking every key_sendup_mbuf()
315 * in the short term. . The rework will be done after a technical
316 * conensus that this approach is appropriate.
317 */
318 if (target == KEY_SENDUP_REGISTERED) {
319 sbprio = SB_PRIO_BESTEFFORT;
320 }
321
322 {
323 uint64_t *ps = PFKEY_STAT_GETREF();
324 ps[PFKEY_STAT_IN_TOTAL]++;
325 ps[PFKEY_STAT_IN_BYTES] += m->m_pkthdr.len;
326 PFKEY_STAT_PUTREF();
327 }
328 if (m->m_len < sizeof(struct sadb_msg)) {
329 #if 1
330 m = m_pullup(m, sizeof(struct sadb_msg));
331 if (m == NULL) {
332 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
333 return ENOBUFS;
334 }
335 #else
336 /* don't bother pulling it up just for stats */
337 #endif
338 }
339 if (m->m_len >= sizeof(struct sadb_msg)) {
340 struct sadb_msg *msg;
341 msg = mtod(m, struct sadb_msg *);
342 PFKEY_STATINC(PFKEY_STAT_IN_MSGTYPE + msg->sadb_msg_type);
343 }
344
345 LIST_FOREACH(rp, &rawcb, rcb_list)
346 {
347 struct socket * kso = rp->rcb_socket;
348 if (rp->rcb_proto.sp_family != PF_KEY)
349 continue;
350 if (rp->rcb_proto.sp_protocol
351 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
352 continue;
353 }
354
355 kp = (struct keycb *)rp;
356
357 /*
358 * If you are in promiscuous mode, and when you get broadcasted
359 * reply, you'll get two PF_KEY messages.
360 * (based on pf_key (at) inner.net message on 14 Oct 1998)
361 */
362 if (((struct keycb *)rp)->kp_promisc) {
363 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
364 (void)key_sendup0(rp, n, 1, 0);
365 n = NULL;
366 }
367 }
368
369 /* the exact target will be processed later */
370 if (so && sotorawcb(so) == rp)
371 continue;
372
373 sendup = 0;
374 switch (target) {
375 case KEY_SENDUP_ONE:
376 /* the statement has no effect */
377 if (so && sotorawcb(so) == rp)
378 sendup++;
379 break;
380 case KEY_SENDUP_ALL:
381 sendup++;
382 break;
383 case KEY_SENDUP_REGISTERED:
384 if (kp->kp_registered) {
385 if (kso->so_rcv.sb_cc <= key_registered_sb_max)
386 sendup++;
387 else
388 printf("keysock: "
389 "registered sendup dropped, "
390 "sb_cc %ld max %d\n",
391 kso->so_rcv.sb_cc,
392 key_registered_sb_max);
393 }
394 break;
395 }
396 PFKEY_STATINC(PFKEY_STAT_IN_MSGTARGET + target);
397
398 if (!sendup)
399 continue;
400
401 if ((n = m_copy(m, 0, (int)M_COPYALL)) == NULL) {
402 m_freem(m);
403 PFKEY_STATINC(PFKEY_STAT_IN_NOMEM);
404 return ENOBUFS;
405 }
406
407 if ((error = key_sendup0(rp, n, 0, 0)) != 0) {
408 m_freem(m);
409 return error;
410 }
411
412 n = NULL;
413 }
414
415 /* The 'later' time for processing the exact target has arrived */
416 if (so) {
417 error = key_sendup0(sotorawcb(so), m, 0, sbprio);
418 m = NULL;
419 } else {
420 error = 0;
421 m_freem(m);
422 }
423 return error;
424 }
425
426 static int
427 key_attach(struct socket *so, int proto)
428 {
429 struct keycb *kp;
430 int s, error;
431
432 KASSERT(sotorawcb(so) == NULL);
433 kp = kmem_zalloc(sizeof(*kp), KM_SLEEP);
434 kp->kp_raw.rcb_len = sizeof(*kp);
435 so->so_pcb = kp;
436
437 s = splsoftnet();
438 error = raw_attach(so, proto);
439 if (error) {
440 PFKEY_STATINC(PFKEY_STAT_SOCKERR);
441 kmem_free(kp, sizeof(*kp));
442 so->so_pcb = NULL;
443 goto out;
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 out:
456 KASSERT(solocked(so));
457 splx(s);
458 return error;
459 }
460
461 static void
462 key_detach(struct socket *so)
463 {
464 struct keycb *kp = (struct keycb *)sotorawcb(so);
465 int s;
466
467 KASSERT(!cpu_softintr_p());
468 KASSERT(solocked(so));
469 KASSERT(kp != NULL);
470
471 s = splsoftnet();
472 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
473 key_cb.key_count--;
474 key_cb.any_count--;
475 key_freereg(so);
476 raw_detach(so);
477 splx(s);
478 }
479
480 static int
481 key_accept(struct socket *so, struct sockaddr *nam)
482 {
483 KASSERT(solocked(so));
484
485 panic("key_accept");
486
487 return EOPNOTSUPP;
488 }
489
490 static int
491 key_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
492 {
493 KASSERT(solocked(so));
494
495 return EOPNOTSUPP;
496 }
497
498 static int
499 key_listen(struct socket *so, struct lwp *l)
500 {
501 KASSERT(solocked(so));
502
503 return EOPNOTSUPP;
504 }
505
506 static int
507 key_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
508 {
509 KASSERT(solocked(so));
510
511 return EOPNOTSUPP;
512 }
513
514 static int
515 key_connect2(struct socket *so, struct socket *so2)
516 {
517 KASSERT(solocked(so));
518
519 return EOPNOTSUPP;
520 }
521
522 static int
523 key_disconnect(struct socket *so)
524 {
525 struct rawcb *rp = sotorawcb(so);
526 int s;
527
528 KASSERT(solocked(so));
529 KASSERT(rp != NULL);
530
531 s = splsoftnet();
532 soisdisconnected(so);
533 raw_disconnect(rp);
534 splx(s);
535
536 return 0;
537 }
538
539 static int
540 key_shutdown(struct socket *so)
541 {
542 int s;
543
544 KASSERT(solocked(so));
545
546 /*
547 * Mark the connection as being incapable of further input.
548 */
549 s = splsoftnet();
550 socantsendmore(so);
551 splx(s);
552
553 return 0;
554 }
555
556 static int
557 key_abort(struct socket *so)
558 {
559 KASSERT(solocked(so));
560
561 panic("key_abort");
562
563 return EOPNOTSUPP;
564 }
565
566 static int
567 key_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
568 {
569 return EOPNOTSUPP;
570 }
571
572 static int
573 key_stat(struct socket *so, struct stat *ub)
574 {
575 KASSERT(solocked(so));
576
577 return 0;
578 }
579
580 static int
581 key_peeraddr(struct socket *so, struct sockaddr *nam)
582 {
583 struct rawcb *rp = sotorawcb(so);
584
585 KASSERT(solocked(so));
586 KASSERT(rp != NULL);
587 KASSERT(nam != NULL);
588
589 if (rp->rcb_faddr == NULL)
590 return ENOTCONN;
591
592 raw_setpeeraddr(rp, nam);
593 return 0;
594 }
595
596 static int
597 key_sockaddr(struct socket *so, struct sockaddr *nam)
598 {
599 struct rawcb *rp = sotorawcb(so);
600
601 KASSERT(solocked(so));
602 KASSERT(rp != NULL);
603 KASSERT(nam != NULL);
604
605 if (rp->rcb_faddr == NULL)
606 return ENOTCONN;
607
608 raw_setsockaddr(rp, nam);
609 return 0;
610 }
611
612 static int
613 key_rcvd(struct socket *so, int flags, struct lwp *l)
614 {
615 KASSERT(solocked(so));
616
617 return EOPNOTSUPP;
618 }
619
620 static int
621 key_recvoob(struct socket *so, struct mbuf *m, int flags)
622 {
623 KASSERT(solocked(so));
624
625 return EOPNOTSUPP;
626 }
627
628 static int
629 key_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
630 struct mbuf *control, struct lwp *l)
631 {
632 int error = 0;
633 int s;
634
635 KASSERT(solocked(so));
636 KASSERT(so->so_proto == &keysw[0]);
637
638 s = splsoftnet();
639 error = raw_send(so, m, nam, control, l, &key_output);
640 splx(s);
641
642 return error;
643 }
644
645 static int
646 key_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
647 {
648 KASSERT(solocked(so));
649
650 m_freem(m);
651 m_freem(control);
652
653 return EOPNOTSUPP;
654 }
655
656 static int
657 key_purgeif(struct socket *so, struct ifnet *ifa)
658 {
659
660 panic("key_purgeif");
661
662 return EOPNOTSUPP;
663 }
664
665 /*
666 * Definitions of protocols supported in the KEY domain.
667 */
668
669 DOMAIN_DEFINE(keydomain);
670
671 PR_WRAP_USRREQS(key)
672 #define key_attach key_attach_wrapper
673 #define key_detach key_detach_wrapper
674 #define key_accept key_accept_wrapper
675 #define key_bind key_bind_wrapper
676 #define key_listen key_listen_wrapper
677 #define key_connect key_connect_wrapper
678 #define key_connect2 key_connect2_wrapper
679 #define key_disconnect key_disconnect_wrapper
680 #define key_shutdown key_shutdown_wrapper
681 #define key_abort key_abort_wrapper
682 #define key_ioctl key_ioctl_wrapper
683 #define key_stat key_stat_wrapper
684 #define key_peeraddr key_peeraddr_wrapper
685 #define key_sockaddr key_sockaddr_wrapper
686 #define key_rcvd key_rcvd_wrapper
687 #define key_recvoob key_recvoob_wrapper
688 #define key_send key_send_wrapper
689 #define key_sendoob key_sendoob_wrapper
690 #define key_purgeif key_purgeif_wrapper
691
692 static const struct pr_usrreqs key_usrreqs = {
693 .pr_attach = key_attach,
694 .pr_detach = key_detach,
695 .pr_accept = key_accept,
696 .pr_bind = key_bind,
697 .pr_listen = key_listen,
698 .pr_connect = key_connect,
699 .pr_connect2 = key_connect2,
700 .pr_disconnect = key_disconnect,
701 .pr_shutdown = key_shutdown,
702 .pr_abort = key_abort,
703 .pr_ioctl = key_ioctl,
704 .pr_stat = key_stat,
705 .pr_peeraddr = key_peeraddr,
706 .pr_sockaddr = key_sockaddr,
707 .pr_rcvd = key_rcvd,
708 .pr_recvoob = key_recvoob,
709 .pr_send = key_send,
710 .pr_sendoob = key_sendoob,
711 .pr_purgeif = key_purgeif,
712 };
713
714 static const struct protosw keysw[] = {
715 {
716 .pr_type = SOCK_RAW,
717 .pr_domain = &keydomain,
718 .pr_protocol = PF_KEY_V2,
719 .pr_flags = PR_ATOMIC|PR_ADDR,
720 .pr_ctlinput = raw_ctlinput,
721 .pr_usrreqs = &key_usrreqs,
722 .pr_init = raw_init,
723 }
724 };
725
726 struct domain keydomain = {
727 .dom_family = PF_KEY,
728 .dom_name = "key",
729 .dom_init = key_init,
730 .dom_protosw = keysw,
731 .dom_protoswNPROTOSW = &keysw[__arraycount(keysw)],
732 };
733