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