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