1 /* $FreeBSD$ */
2 /* $KAME: keysock.c,v 1.25 2001/08/13 20:07:41 itojun Exp $ */
3
4 /*-
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include "opt_ipsec.h"
34
35 /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/domain.h>
40 #include <sys/errno.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/mutex.h>
46 #include <sys/priv.h>
47 #include <sys/protosw.h>
48 #include <sys/signalvar.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
53
54 #include <net/if.h>
55 #include <net/vnet.h>
56 #include <net/raw_cb.h>
57
58 #include <netinet/in.h>
59
60 #include <net/pfkeyv2.h>
61 #include <netipsec/key.h>
62 #include <netipsec/keysock.h>
63 #include <netipsec/key_debug.h>
64 #include <netipsec/ipsec.h>
65
66 #include <machine/stdarg.h>
67
68 struct key_cb {
69 int key_count;
70 int any_count;
71 };
72 static VNET_DEFINE(struct key_cb, key_cb);
73 #define V_key_cb VNET(key_cb)
74
75 static struct sockaddr key_src = { 2, PF_KEY, };
76
77 static int key_sendup0(struct rawcb *, struct mbuf *, int);
78
79 VNET_PCPUSTAT_DEFINE(struct pfkeystat, pfkeystat);
80 VNET_PCPUSTAT_SYSINIT(pfkeystat);
81
82 #ifdef VIMAGE
83 VNET_PCPUSTAT_SYSUNINIT(pfkeystat);
84 #endif /* VIMAGE */
85
86 /*
87 * key_output()
88 */
89 int
90 key_output(struct mbuf *m, struct socket *so, ...)
91 {
92 struct sadb_msg *msg;
93 int len, error = 0;
94
95 if (m == NULL)
96 panic("%s: NULL pointer was passed.\n", __func__);
97
98 PFKEYSTAT_INC(out_total);
99 PFKEYSTAT_ADD(out_bytes, m->m_pkthdr.len);
100
101 len = m->m_pkthdr.len;
102 if (len < sizeof(struct sadb_msg)) {
103 PFKEYSTAT_INC(out_tooshort);
104 error = EINVAL;
105 goto end;
106 }
107
108 if (m->m_len < sizeof(struct sadb_msg)) {
109 if ((m = m_pullup(m, sizeof(struct sadb_msg))) == NULL) {
110 PFKEYSTAT_INC(out_nomem);
111 error = ENOBUFS;
112 goto end;
113 }
114 }
115
116 M_ASSERTPKTHDR(m);
117
118 KEYDBG(KEY_DUMP, kdebug_mbuf(m));
119
120 msg = mtod(m, struct sadb_msg *);
121 PFKEYSTAT_INC(out_msgtype[msg->sadb_msg_type]);
122 if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
123 PFKEYSTAT_INC(out_invlen);
124 error = EINVAL;
125 goto end;
126 }
127
128 error = key_parse(m, so);
129 m = NULL;
130 end:
131 if (m)
132 m_freem(m);
133 return error;
134 }
135
136 /*
137 * send message to the socket.
138 */
139 static int
140 key_sendup0(struct rawcb *rp, struct mbuf *m, int promisc)
141 {
142 int error;
143
144 if (promisc) {
145 struct sadb_msg *pmsg;
146
147 M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT);
148 if (m == NULL) {
149 PFKEYSTAT_INC(in_nomem);
150 return (ENOBUFS);
151 }
152 pmsg = mtod(m, struct sadb_msg *);
153 bzero(pmsg, sizeof(*pmsg));
154 pmsg->sadb_msg_version = PF_KEY_V2;
155 pmsg->sadb_msg_type = SADB_X_PROMISC;
156 pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
157 /* pid and seq? */
158
159 PFKEYSTAT_INC(in_msgtype[pmsg->sadb_msg_type]);
160 }
161
162 if (!sbappendaddr(&rp->rcb_socket->so_rcv, (struct sockaddr *)&key_src,
163 m, NULL)) {
164 PFKEYSTAT_INC(in_nomem);
165 m_freem(m);
166 error = ENOBUFS;
167 } else
168 error = 0;
169 sorwakeup(rp->rcb_socket);
170 return error;
171 }
172
173 /* so can be NULL if target != KEY_SENDUP_ONE */
174 int
175 key_sendup_mbuf(struct socket *so, struct mbuf *m, int target)
176 {
177 struct mbuf *n;
178 struct keycb *kp;
179 struct rawcb *rp;
180 int error = 0;
181
182 KASSERT(m != NULL, ("NULL mbuf pointer was passed."));
183 KASSERT(so != NULL || target != KEY_SENDUP_ONE,
184 ("NULL socket pointer was passed."));
185 KASSERT(target == KEY_SENDUP_ONE || target == KEY_SENDUP_ALL ||
186 target == KEY_SENDUP_REGISTERED, ("Wrong target %d", target));
187
188 PFKEYSTAT_INC(in_total);
189 PFKEYSTAT_ADD(in_bytes, m->m_pkthdr.len);
190 if (m->m_len < sizeof(struct sadb_msg)) {
191 m = m_pullup(m, sizeof(struct sadb_msg));
192 if (m == NULL) {
193 PFKEYSTAT_INC(in_nomem);
194 return ENOBUFS;
195 }
196 }
197 if (m->m_len >= sizeof(struct sadb_msg)) {
198 struct sadb_msg *msg;
199 msg = mtod(m, struct sadb_msg *);
200 PFKEYSTAT_INC(in_msgtype[msg->sadb_msg_type]);
201 }
202 mtx_lock(&rawcb_mtx);
203 if (V_key_cb.any_count == 0) {
204 mtx_unlock(&rawcb_mtx);
205 m_freem(m);
206 return (0);
207 }
208 LIST_FOREACH(rp, &V_rawcb_list, list)
209 {
210 if (rp->rcb_proto.sp_family != PF_KEY)
211 continue;
212 if (rp->rcb_proto.sp_protocol
213 && rp->rcb_proto.sp_protocol != PF_KEY_V2) {
214 continue;
215 }
216
217 /*
218 * If you are in promiscuous mode, and when you get broadcasted
219 * reply, you'll get two PF_KEY messages.
220 * (based on pf_key@inner.net message on 14 Oct 1998)
221 */
222 kp = (struct keycb *)rp;
223 if (kp->kp_promisc) {
224 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
225 if (n != NULL)
226 key_sendup0(rp, n, 1);
227 else
228 PFKEYSTAT_INC(in_nomem);
229 }
230
231 /* the exact target will be processed later */
232 if (so && sotorawcb(so) == rp)
233 continue;
234
235 if (target == KEY_SENDUP_ONE || (
236 target == KEY_SENDUP_REGISTERED && kp->kp_registered == 0))
237 continue;
238
239 /* KEY_SENDUP_ALL + KEY_SENDUP_REGISTERED */
240 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
241 if (n == NULL) {
242 PFKEYSTAT_INC(in_nomem);
243 /* Try send to another socket */
244 continue;
245 }
246
247 if (key_sendup0(rp, n, 0) == 0)
248 PFKEYSTAT_INC(in_msgtarget[target]);
249 }
250
251 if (so) { /* KEY_SENDUP_ONE */
252 error = key_sendup0(sotorawcb(so), m, 0);
253 if (error == 0)
254 PFKEYSTAT_INC(in_msgtarget[KEY_SENDUP_ONE]);
255 } else {
256 error = 0;
257 m_freem(m);
258 }
259 mtx_unlock(&rawcb_mtx);
260 return (error);
261 }
262
263 /*
264 * key_abort()
265 * derived from net/rtsock.c:rts_abort()
266 */
267 static void
268 key_abort(struct socket *so)
269 {
270 raw_usrreqs.pru_abort(so);
271 }
272
273 /*
274 * key_attach()
275 * derived from net/rtsock.c:rts_attach()
276 */
277 static int
278 key_attach(struct socket *so, int proto, struct thread *td)
279 {
280 struct keycb *kp;
281 int error;
282
283 KASSERT(so->so_pcb == NULL, ("key_attach: so_pcb != NULL"));
284
285 if (td != NULL) {
286 error = priv_check(td, PRIV_NET_RAW);
287 if (error)
288 return error;
289 }
290
291 /* XXX */
292 kp = malloc(sizeof *kp, M_PCB, M_WAITOK | M_ZERO);
293 if (kp == NULL)
294 return ENOBUFS;
295
296 so->so_pcb = (caddr_t)kp;
297 error = raw_attach(so, proto);
298 kp = (struct keycb *)sotorawcb(so);
299 if (error) {
300 free(kp, M_PCB);
301 so->so_pcb = (caddr_t) 0;
302 return error;
303 }
304
305 kp->kp_promisc = kp->kp_registered = 0;
306
307 if (kp->kp_raw.rcb_proto.sp_protocol == PF_KEY) /* XXX: AF_KEY */
308 V_key_cb.key_count++;
309 V_key_cb.any_count++;
310 soisconnected(so);
311 so->so_options |= SO_USELOOPBACK;
312
313 return 0;
314 }
315
316 /*
317 * key_bind()
318 * derived from net/rtsock.c:rts_bind()
319 */
320 static int
321 key_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
322 {
323 return EINVAL;
324 }
325
326 /*
327 * key_close()
328 * derived from net/rtsock.c:rts_close().
329 */
330 static void
331 key_close(struct socket *so)
332 {
333
334 raw_usrreqs.pru_close(so);
335 }
336
337 /*
338 * key_connect()
339 * derived from net/rtsock.c:rts_connect()
340 */
341 static int
342 key_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
343 {
344 return EINVAL;
345 }
346
347 /*
348 * key_detach()
349 * derived from net/rtsock.c:rts_detach()
350 */
351 static void
352 key_detach(struct socket *so)
353 {
354 struct keycb *kp = (struct keycb *)sotorawcb(so);
355
356 KASSERT(kp != NULL, ("key_detach: kp == NULL"));
357 if (kp->kp_raw.rcb_proto.sp_protocol
358 == PF_KEY) /* XXX: AF_KEY */
359 V_key_cb.key_count--;
360 V_key_cb.any_count--;
361
362 key_freereg(so);
363 raw_usrreqs.pru_detach(so);
364 }
365
366 /*
367 * key_disconnect()
368 * derived from net/rtsock.c:key_disconnect()
369 */
370 static int
371 key_disconnect(struct socket *so)
372 {
373 return(raw_usrreqs.pru_disconnect(so));
374 }
375
376 /*
377 * key_peeraddr()
378 * derived from net/rtsock.c:rts_peeraddr()
379 */
380 static int
381 key_peeraddr(struct socket *so, struct sockaddr **nam)
382 {
383 return(raw_usrreqs.pru_peeraddr(so, nam));
384 }
385
386 /*
387 * key_send()
388 * derived from net/rtsock.c:rts_send()
389 */
390 static int
391 key_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
392 struct mbuf *control, struct thread *td)
393 {
394 return(raw_usrreqs.pru_send(so, flags, m, nam, control, td));
395 }
396
397 /*
398 * key_shutdown()
399 * derived from net/rtsock.c:rts_shutdown()
400 */
401 static int
402 key_shutdown(struct socket *so)
403 {
404 return(raw_usrreqs.pru_shutdown(so));
405 }
406
407 /*
408 * key_sockaddr()
409 * derived from net/rtsock.c:rts_sockaddr()
410 */
411 static int
412 key_sockaddr(struct socket *so, struct sockaddr **nam)
413 {
414 return(raw_usrreqs.pru_sockaddr(so, nam));
415 }
416
417 struct pr_usrreqs key_usrreqs = {
418 .pru_abort = key_abort,
419 .pru_attach = key_attach,
420 .pru_bind = key_bind,
421 .pru_connect = key_connect,
422 .pru_detach = key_detach,
423 .pru_disconnect = key_disconnect,
424 .pru_peeraddr = key_peeraddr,
425 .pru_send = key_send,
426 .pru_shutdown = key_shutdown,
427 .pru_sockaddr = key_sockaddr,
428 .pru_close = key_close,
429 };
430
431 /* sysctl */
432 SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW, 0, "Key Family");
433
434 /*
435 * Definitions of protocols supported in the KEY domain.
436 */
437
438 extern struct domain keydomain;
439
440 struct protosw keysw[] = {
441 {
442 .pr_type = SOCK_RAW,
443 .pr_domain = &keydomain,
444 .pr_protocol = PF_KEY_V2,
445 .pr_flags = PR_ATOMIC|PR_ADDR,
446 .pr_output = key_output,
447 .pr_ctlinput = raw_ctlinput,
448 .pr_init = raw_init,
449 .pr_usrreqs = &key_usrreqs
450 }
451 };
452
453 static void
454 key_init0(void)
455 {
456
457 bzero((caddr_t)&V_key_cb, sizeof(V_key_cb));
458 key_init();
459 }
460
461 struct domain keydomain = {
462 .dom_family = PF_KEY,
463 .dom_name = "key",
464 .dom_init = key_init0,
465 #ifdef VIMAGE
466 .dom_destroy = key_destroy,
467 #endif
468 .dom_protosw = keysw,
469 .dom_protoswNPROTOSW = &keysw[nitems(keysw)]
470 };
471
472 VNET_DOMAIN_SET(key);
Cache object: dea4ae2b890387cda2cb243fb43e2ed1
|