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