ipsec.c revision 1.125 1 /* $NetBSD: ipsec.c,v 1.125 2018/02/08 20:57:41 maxv Exp $ */
2 /* $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/netipsec/ipsec.c,v 1.2.2.2 2003/07/01 01:38:13 sam Exp $ */
3 /* $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane 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: ipsec.c,v 1.125 2018/02/08 20:57:41 maxv Exp $");
36
37 /*
38 * IPsec controller part.
39 */
40
41 #if defined(_KERNEL_OPT)
42 #include "opt_inet.h"
43 #include "opt_ipsec.h"
44 #endif
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/domain.h>
50 #include <sys/protosw.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/errno.h>
54 #include <sys/time.h>
55 #include <sys/kernel.h>
56 #include <sys/syslog.h>
57 #include <sys/sysctl.h>
58 #include <sys/proc.h>
59 #include <sys/kauth.h>
60 #include <sys/cpu.h>
61 #include <sys/kmem.h>
62 #include <sys/pserialize.h>
63
64 #include <net/if.h>
65 #include <net/route.h>
66
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/in_var.h>
72 #include <netinet/udp.h>
73 #include <netinet/udp_var.h>
74 #include <netinet/tcp.h>
75 #include <netinet/udp.h>
76 #include <netinet/ip_icmp.h>
77 #include <netinet/ip_private.h>
78
79 #include <netinet/ip6.h>
80 #ifdef INET6
81 #include <netinet6/ip6_var.h>
82 #endif
83 #include <netinet/in_pcb.h>
84 #ifdef INET6
85 #include <netinet6/in6_pcb.h>
86 #include <netinet/icmp6.h>
87 #endif
88
89 #include <netipsec/ipsec.h>
90 #include <netipsec/ipsec_var.h>
91 #include <netipsec/ipsec_private.h>
92 #ifdef INET6
93 #include <netipsec/ipsec6.h>
94 #endif
95 #include <netipsec/ah_var.h>
96 #include <netipsec/esp_var.h>
97 #include <netipsec/ipcomp.h> /*XXX*/
98 #include <netipsec/ipcomp_var.h>
99
100 #include <netipsec/key.h>
101 #include <netipsec/keydb.h>
102 #include <netipsec/key_debug.h>
103
104 #include <netipsec/xform.h>
105
106 int ipsec_used = 0;
107 int ipsec_enabled = 1;
108
109 #ifdef IPSEC_DEBUG
110 int ipsec_debug = 1;
111
112 /*
113 * When set to 1, IPsec will send packets with the same sequence number.
114 * This allows to verify if the other side has proper replay attacks detection.
115 */
116 int ipsec_replay = 0;
117
118 /*
119 * When set 1, IPsec will send packets with corrupted HMAC.
120 * This allows to verify if the other side properly detects modified packets.
121 */
122 int ipsec_integrity = 0;
123 #else
124 int ipsec_debug = 0;
125 #endif
126
127 percpu_t *ipsecstat_percpu;
128 int ip4_ah_offsetmask = 0; /* maybe IP_DF? */
129 int ip4_ipsec_dfbit = 2; /* DF bit on encap. 0: clear 1: set 2: copy */
130 int ip4_esp_trans_deflev = IPSEC_LEVEL_USE;
131 int ip4_esp_net_deflev = IPSEC_LEVEL_USE;
132 int ip4_ah_trans_deflev = IPSEC_LEVEL_USE;
133 int ip4_ah_net_deflev = IPSEC_LEVEL_USE;
134 struct secpolicy ip4_def_policy;
135 int ip4_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
136 int ip4_esp_randpad = -1;
137
138 u_int ipsec_spdgen = 1; /* SPD generation # */
139
140 static struct secpolicy ipsec_dummy_sp __read_mostly = {
141 .state = IPSEC_SPSTATE_ALIVE,
142 /* If ENTRUST, the dummy SP never be used. See ipsec_getpolicybysock. */
143 .policy = IPSEC_POLICY_ENTRUST,
144 };
145
146 static struct secpolicy *ipsec_checkpcbcache (struct mbuf *,
147 struct inpcbpolicy *, int);
148 static int ipsec_fillpcbcache (struct inpcbpolicy *, struct mbuf *,
149 struct secpolicy *, int);
150 static int ipsec_invalpcbcache (struct inpcbpolicy *, int);
151
152 /*
153 * Crypto support requirements:
154 *
155 * 1 require hardware support
156 * -1 require software support
157 * 0 take anything
158 */
159 int crypto_support = 0;
160
161 static struct secpolicy *ipsec_getpolicybysock(struct mbuf *, u_int,
162 struct inpcb_hdr *, int *);
163
164 #ifdef __FreeBSD__
165 /* net.inet.ipsec */
166 SYSCTL_INT(_net_inet_ipsec, IPSECCTL_ESP_RANDPAD,
167 esp_randpad, CTLFLAG_RW, &ip4_esp_randpad, 0, "");
168 SYSCTL_INT(_net_inet_ipsec, OID_AUTO,
169 crypto_support, CTLFLAG_RW, &crypto_support,0, "");
170 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_replay, CTLFLAG_RW, &ipsec_replay, 0,
171 "Emulate replay attack");
172 SYSCTL_INT(_net_inet_ipsec, OID_AUTO, test_integrity, CTLFLAG_RW,
173 &ipsec_integrity, 0, "Emulate man-in-the-middle attack");
174 #endif /* __FreeBSD__ */
175
176 #ifdef INET6
177 int ip6_esp_trans_deflev = IPSEC_LEVEL_USE;
178 int ip6_esp_net_deflev = IPSEC_LEVEL_USE;
179 int ip6_ah_trans_deflev = IPSEC_LEVEL_USE;
180 int ip6_ah_net_deflev = IPSEC_LEVEL_USE;
181 struct secpolicy ip6_def_policy;
182 int ip6_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */
183 int ip6_esp_randpad = -1;
184
185
186 #ifdef __FreeBSD__
187 /* net.inet6.ipsec6 */
188 SYSCTL_INT(_net_inet6_ipsec6, IPSECCTL_ESP_RANDPAD,
189 esp_randpad, CTLFLAG_RW, &ip6_esp_randpad, 0, "");
190 #endif /* __FreeBSD__ */
191 #endif /* INET6 */
192
193 static int ipsec4_setspidx_inpcb (struct mbuf *, struct inpcb *);
194 #ifdef INET6
195 static int ipsec6_setspidx_in6pcb (struct mbuf *, struct in6pcb *);
196 #endif
197 static int ipsec_setspidx (struct mbuf *, struct secpolicyindex *, int);
198 static void ipsec4_get_ulp (struct mbuf *m, struct secpolicyindex *, int);
199 static int ipsec4_setspidx_ipaddr (struct mbuf *, struct secpolicyindex *);
200 #ifdef INET6
201 static void ipsec6_get_ulp (struct mbuf *m, struct secpolicyindex *, int);
202 static int ipsec6_setspidx_ipaddr (struct mbuf *, struct secpolicyindex *);
203 #endif
204 static void ipsec_delpcbpolicy (struct inpcbpolicy *);
205 #if 0 /* unused */
206 static struct secpolicy *ipsec_deepcopy_policy (const struct secpolicy *);
207 #endif
208 static int ipsec_set_policy (struct secpolicy **, int, const void *, size_t,
209 kauth_cred_t);
210 static int ipsec_get_policy (struct secpolicy *, struct mbuf **);
211 static void ipsec_destroy_policy(struct secpolicy *);
212 static void vshiftl (unsigned char *, int, int);
213 static size_t ipsec_hdrsiz(const struct secpolicy *, const struct mbuf *);
214
215 /*
216 * Try to validate and use cached policy on a PCB.
217 */
218 static struct secpolicy *
219 ipsec_checkpcbcache(struct mbuf *m, struct inpcbpolicy *pcbsp, int dir)
220 {
221 struct secpolicyindex spidx;
222 struct secpolicy *sp = NULL;
223 int s;
224
225 KASSERT(IPSEC_DIR_IS_VALID(dir));
226 KASSERT(pcbsp != NULL);
227 KASSERT(dir < __arraycount(pcbsp->sp_cache));
228 KASSERT(inph_locked(pcbsp->sp_inph));
229
230 /*
231 * Checking the generation and sp->state and taking a reference to an SP
232 * must be in a critical section of pserialize. See key_unlink_sp.
233 */
234 s = pserialize_read_enter();
235 /* SPD table change invalidate all the caches. */
236 if (ipsec_spdgen != pcbsp->sp_cache[dir].cachegen) {
237 ipsec_invalpcbcache(pcbsp, dir);
238 goto out;
239 }
240 sp = pcbsp->sp_cache[dir].cachesp;
241 if (sp == NULL)
242 goto out;
243 if (sp->state != IPSEC_SPSTATE_ALIVE) {
244 sp = NULL;
245 ipsec_invalpcbcache(pcbsp, dir);
246 goto out;
247 }
248 if ((pcbsp->sp_cacheflags & IPSEC_PCBSP_CONNECTED) == 0) {
249 /* NB: assume ipsec_setspidx never sleep */
250 if (ipsec_setspidx(m, &spidx, 1) != 0) {
251 sp = NULL;
252 goto out;
253 }
254
255 /*
256 * We have to make an exact match here since the cached rule
257 * might have lower priority than a rule that would otherwise
258 * have matched the packet.
259 */
260 if (memcmp(&pcbsp->sp_cache[dir].cacheidx, &spidx,
261 sizeof(spidx))) {
262 sp = NULL;
263 goto out;
264 }
265 } else {
266 /*
267 * The pcb is connected, and the L4 code is sure that:
268 * - outgoing side uses inp_[lf]addr
269 * - incoming side looks up policy after inpcb lookup
270 * and address pair is know to be stable. We do not need
271 * to generate spidx again, nor check the address match again.
272 *
273 * For IPv4/v6 SOCK_STREAM sockets, this assumptions holds
274 * and there are calls to ipsec_pcbconn() from in_pcbconnect().
275 */
276 }
277
278 sp->lastused = time_second;
279 KEY_SP_REF(sp);
280 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
281 "DP cause refcnt++:%d SP:%p\n",
282 key_sp_refcnt(sp), pcbsp->sp_cache[dir].cachesp);
283 out:
284 pserialize_read_exit(s);
285 return sp;
286 }
287
288 static int
289 ipsec_fillpcbcache(struct inpcbpolicy *pcbsp, struct mbuf *m,
290 struct secpolicy *sp, int dir)
291 {
292
293 KASSERT(IPSEC_DIR_IS_INOROUT(dir));
294 KASSERT(dir < __arraycount(pcbsp->sp_cache));
295 KASSERT(inph_locked(pcbsp->sp_inph));
296
297 pcbsp->sp_cache[dir].cachesp = NULL;
298 pcbsp->sp_cache[dir].cachehint = IPSEC_PCBHINT_UNKNOWN;
299 if (ipsec_setspidx(m, &pcbsp->sp_cache[dir].cacheidx, 1) != 0) {
300 return EINVAL;
301 }
302 pcbsp->sp_cache[dir].cachesp = sp;
303 if (pcbsp->sp_cache[dir].cachesp) {
304 /*
305 * If the PCB is connected, we can remember a hint to
306 * possibly short-circuit IPsec processing in other places.
307 */
308 if (pcbsp->sp_cacheflags & IPSEC_PCBSP_CONNECTED) {
309 switch (pcbsp->sp_cache[dir].cachesp->policy) {
310 case IPSEC_POLICY_NONE:
311 case IPSEC_POLICY_BYPASS:
312 pcbsp->sp_cache[dir].cachehint =
313 IPSEC_PCBHINT_NO;
314 break;
315 default:
316 pcbsp->sp_cache[dir].cachehint =
317 IPSEC_PCBHINT_YES;
318 }
319 }
320 }
321 pcbsp->sp_cache[dir].cachegen = ipsec_spdgen;
322
323 return 0;
324 }
325
326 static int
327 ipsec_invalpcbcache(struct inpcbpolicy *pcbsp, int dir)
328 {
329 int i;
330
331 KASSERT(inph_locked(pcbsp->sp_inph));
332
333 for (i = IPSEC_DIR_INBOUND; i <= IPSEC_DIR_OUTBOUND; i++) {
334 if (dir != IPSEC_DIR_ANY && i != dir)
335 continue;
336 pcbsp->sp_cache[i].cachesp = NULL;
337 pcbsp->sp_cache[i].cachehint = IPSEC_PCBHINT_UNKNOWN;
338 pcbsp->sp_cache[i].cachegen = 0;
339 memset(&pcbsp->sp_cache[i].cacheidx, 0,
340 sizeof(pcbsp->sp_cache[i].cacheidx));
341 }
342 return 0;
343 }
344
345 void
346 ipsec_pcbconn(struct inpcbpolicy *pcbsp)
347 {
348
349 KASSERT(inph_locked(pcbsp->sp_inph));
350
351 pcbsp->sp_cacheflags |= IPSEC_PCBSP_CONNECTED;
352 ipsec_invalpcbcache(pcbsp, IPSEC_DIR_ANY);
353 }
354
355 void
356 ipsec_pcbdisconn(struct inpcbpolicy *pcbsp)
357 {
358
359 KASSERT(inph_locked(pcbsp->sp_inph));
360
361 pcbsp->sp_cacheflags &= ~IPSEC_PCBSP_CONNECTED;
362 ipsec_invalpcbcache(pcbsp, IPSEC_DIR_ANY);
363 }
364
365 void
366 ipsec_invalpcbcacheall(void)
367 {
368
369 if (ipsec_spdgen == UINT_MAX)
370 ipsec_spdgen = 1;
371 else
372 ipsec_spdgen++;
373 }
374
375 /*
376 * Return a held reference to the default SP.
377 */
378 static struct secpolicy *
379 key_get_default_sp(int af, const char *where, int tag)
380 {
381 struct secpolicy *sp;
382
383 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag);
384
385 switch(af) {
386 case AF_INET:
387 sp = &ip4_def_policy;
388 break;
389 #ifdef INET6
390 case AF_INET6:
391 sp = &ip6_def_policy;
392 break;
393 #endif
394 default:
395 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
396 "unexpected protocol family %u\n", af);
397 return NULL;
398 }
399
400 if (sp->policy != IPSEC_POLICY_DISCARD &&
401 sp->policy != IPSEC_POLICY_NONE) {
402 IPSECLOG(LOG_INFO, "fixed system default policy: %d->%d\n",
403 sp->policy, IPSEC_POLICY_NONE);
404 sp->policy = IPSEC_POLICY_NONE;
405 }
406 KEY_SP_REF(sp);
407
408 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP returns SP:%p (%u)\n",
409 sp, key_sp_refcnt(sp));
410 return sp;
411 }
412 #define KEY_GET_DEFAULT_SP(af) \
413 key_get_default_sp((af), __func__, __LINE__)
414
415 /*
416 * For OUTBOUND packet having a socket. Searching SPD for packet,
417 * and return a pointer to SP.
418 * OUT: NULL: no apropreate SP found, the following value is set to error.
419 * 0 : bypass
420 * EACCES : discard packet.
421 * ENOENT : ipsec_acquire() in progress, maybe.
422 * others : error occurred.
423 * others: a pointer to SP
424 *
425 * NOTE: IPv6 mapped address concern is implemented here.
426 */
427 static struct secpolicy *
428 ipsec_getpolicybysock(struct mbuf *m, u_int dir, struct inpcb_hdr *inph,
429 int *error)
430 {
431 struct inpcbpolicy *pcbsp = NULL;
432 struct secpolicy *currsp = NULL; /* policy on socket */
433 struct secpolicy *sp;
434 int af;
435
436 KASSERT(m != NULL);
437 KASSERT(inph != NULL);
438 KASSERT(error != NULL);
439 KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
440
441 KASSERT(inph->inph_socket != NULL);
442 KASSERT(inph_locked(inph));
443
444 /* XXX FIXME inpcb/in6pcb vs socket*/
445 af = inph->inph_af;
446 KASSERTMSG(af == AF_INET || af == AF_INET6,
447 "unexpected protocol family %u", af);
448
449 KASSERT(inph->inph_sp != NULL);
450 /* If we have a cached entry, and if it is still valid, use it. */
451 IPSEC_STATINC(IPSEC_STAT_SPDCACHELOOKUP);
452 currsp = ipsec_checkpcbcache(m, inph->inph_sp, dir);
453 if (currsp) {
454 *error = 0;
455 return currsp;
456 }
457 IPSEC_STATINC(IPSEC_STAT_SPDCACHEMISS);
458
459 switch (af) {
460 case AF_INET: {
461 struct inpcb *in4p = (struct inpcb *)inph;
462 /* set spidx in pcb */
463 *error = ipsec4_setspidx_inpcb(m, in4p);
464 pcbsp = in4p->inp_sp;
465 break;
466 }
467
468 #if defined(INET6)
469 case AF_INET6: {
470 struct in6pcb *in6p = (struct in6pcb *)inph;
471 /* set spidx in pcb */
472 *error = ipsec6_setspidx_in6pcb(m, in6p);
473 pcbsp = in6p->in6p_sp;
474 break;
475 }
476 #endif
477 default:
478 *error = EPFNOSUPPORT;
479 break;
480 }
481 if (*error)
482 return NULL;
483
484 KASSERT(pcbsp != NULL);
485 switch (dir) {
486 case IPSEC_DIR_INBOUND:
487 currsp = pcbsp->sp_in;
488 break;
489 case IPSEC_DIR_OUTBOUND:
490 currsp = pcbsp->sp_out;
491 break;
492 }
493 KASSERT(currsp != NULL);
494
495 if (pcbsp->priv) { /* when privilieged socket */
496 switch (currsp->policy) {
497 case IPSEC_POLICY_BYPASS:
498 case IPSEC_POLICY_IPSEC:
499 KEY_SP_REF(currsp);
500 sp = currsp;
501 break;
502
503 case IPSEC_POLICY_ENTRUST:
504 /* look for a policy in SPD */
505 sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir);
506 if (sp == NULL) /* no SP found */
507 sp = KEY_GET_DEFAULT_SP(af);
508 break;
509
510 default:
511 IPSECLOG(LOG_ERR, "Invalid policy for PCB %d\n",
512 currsp->policy);
513 *error = EINVAL;
514 return NULL;
515 }
516 } else { /* unpriv, SPD has policy */
517 sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir);
518 if (sp == NULL) { /* no SP found */
519 switch (currsp->policy) {
520 case IPSEC_POLICY_BYPASS:
521 IPSECLOG(LOG_ERR, "Illegal policy for "
522 "non-priviliged defined %d\n",
523 currsp->policy);
524 *error = EINVAL;
525 return NULL;
526
527 case IPSEC_POLICY_ENTRUST:
528 sp = KEY_GET_DEFAULT_SP(af);
529 break;
530
531 case IPSEC_POLICY_IPSEC:
532 KEY_SP_REF(currsp);
533 sp = currsp;
534 break;
535
536 default:
537 IPSECLOG(LOG_ERR, "Invalid policy for "
538 "PCB %d\n", currsp->policy);
539 *error = EINVAL;
540 return NULL;
541 }
542 }
543 }
544 KASSERTMSG(sp != NULL, "null SP (priv %u policy %u", pcbsp->priv,
545 currsp->policy);
546 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP,
547 "DP (priv %u policy %u) allocates SP:%p (refcnt %u)\n",
548 pcbsp->priv, currsp->policy, sp, key_sp_refcnt(sp));
549 ipsec_fillpcbcache(pcbsp, m, sp, dir);
550 return sp;
551 }
552
553 /*
554 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
555 * and return a pointer to SP.
556 * OUT: positive: a pointer to the entry for security policy leaf matched.
557 * NULL: no apropreate SP found, the following value is set to error.
558 * 0 : bypass
559 * EACCES : discard packet.
560 * ENOENT : ipsec_acquire() in progress, maybe.
561 * others : error occurred.
562 */
563 struct secpolicy *
564 ipsec_getpolicybyaddr(struct mbuf *m, u_int dir, int flag, int *error)
565 {
566 struct secpolicyindex spidx;
567 struct secpolicy *sp;
568
569 KASSERT(m != NULL);
570 KASSERT(error != NULL);
571 KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir);
572
573 sp = NULL;
574
575 /* Make an index to look for a policy. */
576 *error = ipsec_setspidx(m, &spidx, (flag & IP_FORWARDING) ? 0 : 1);
577 if (*error != 0) {
578 IPSECLOG(LOG_DEBUG, "setpidx failed, dir %u flag %u\n", dir, flag);
579 memset(&spidx, 0, sizeof (spidx));
580 return NULL;
581 }
582
583 spidx.dir = dir;
584
585 if (key_havesp(dir)) {
586 sp = KEY_LOOKUP_SP_BYSPIDX(&spidx, dir);
587 }
588
589 if (sp == NULL) /* no SP found, use system default */
590 sp = KEY_GET_DEFAULT_SP(spidx.dst.sa.sa_family);
591 KASSERT(sp != NULL);
592 return sp;
593 }
594
595 struct secpolicy *
596 ipsec4_checkpolicy(struct mbuf *m, u_int dir, u_int flag, int *error,
597 struct inpcb *inp)
598 {
599 struct secpolicy *sp;
600
601 *error = 0;
602
603 if (inp == NULL) {
604 sp = ipsec_getpolicybyaddr(m, dir, flag, error);
605 } else {
606 KASSERT(inp->inp_socket != NULL);
607 sp = ipsec_getpolicybysock(m, dir, (struct inpcb_hdr *)inp, error);
608 }
609 if (sp == NULL) {
610 KASSERTMSG(*error != 0, "getpolicy failed w/o error");
611 IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
612 return NULL;
613 }
614 KASSERTMSG(*error == 0, "sp w/ error set to %u", *error);
615 switch (sp->policy) {
616 case IPSEC_POLICY_ENTRUST:
617 default:
618 printf("%s: invalid policy %u\n", __func__, sp->policy);
619 /* fall thru... */
620 case IPSEC_POLICY_DISCARD:
621 IPSEC_STATINC(IPSEC_STAT_OUT_POLVIO);
622 *error = -EINVAL; /* packet is discarded by caller */
623 break;
624 case IPSEC_POLICY_BYPASS:
625 case IPSEC_POLICY_NONE:
626 KEY_SP_UNREF(&sp);
627 sp = NULL; /* NB: force NULL result */
628 break;
629 case IPSEC_POLICY_IPSEC:
630 KASSERT(sp->req != NULL);
631 break;
632 }
633 if (*error != 0) {
634 KEY_SP_UNREF(&sp);
635 sp = NULL;
636 IPSECLOG(LOG_DEBUG, "done, error %d\n", *error);
637 }
638 return sp;
639 }
640
641 int
642 ipsec4_output(struct mbuf *m, struct inpcb *inp, int flags,
643 u_long *mtu, bool *natt_frag, bool *done)
644 {
645 struct secpolicy *sp = NULL;
646 int error, s;
647
648 /*
649 * Check the security policy (SP) for the packet and, if required,
650 * do IPsec-related processing. There are two cases here; the first
651 * time a packet is sent through it will be untagged and handled by
652 * ipsec4_checkpolicy(). If the packet is resubmitted to ip_output
653 * (e.g. after AH, ESP, etc. processing), there will be a tag to
654 * bypass the lookup and related policy checking.
655 */
656 if (ipsec_outdone(m)) {
657 return 0;
658 }
659 s = splsoftnet();
660 if (inp && ipsec_pcb_skip_ipsec(inp->inp_sp, IPSEC_DIR_OUTBOUND)) {
661 splx(s);
662 return 0;
663 }
664 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, inp);
665
666 /*
667 * There are four return cases:
668 * sp != NULL apply IPsec policy
669 * sp == NULL, error == 0 no IPsec handling needed
670 * sp == NULL, error == -EINVAL discard packet w/o error
671 * sp == NULL, error != 0 discard packet, report error
672 */
673 if (sp == NULL) {
674 splx(s);
675 if (error) {
676 /*
677 * Hack: -EINVAL is used to signal that a packet
678 * should be silently discarded. This is typically
679 * because we asked key management for an SA and
680 * it was delayed (e.g. kicked up to IKE).
681 */
682 if (error == -EINVAL)
683 error = 0;
684 m_freem(m);
685 *done = true;
686 return error;
687 }
688 /* No IPsec processing for this packet. */
689 return 0;
690 }
691
692 /*
693 * Do delayed checksums now because we send before
694 * this is done in the normal processing path.
695 */
696 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
697 in_delayed_cksum(m);
698 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
699 }
700
701 {
702 u_long _mtu = 0;
703
704 /* Note: callee frees mbuf */
705 error = ipsec4_process_packet(m, sp->req, &_mtu);
706
707 if (error == 0 && _mtu != 0) {
708 /*
709 * NAT-T ESP fragmentation: do not do IPSec processing
710 * now, we will do it on each fragmented packet.
711 */
712 *mtu = _mtu;
713 *natt_frag = true;
714 KEY_SP_UNREF(&sp);
715 splx(s);
716 return 0;
717 }
718 }
719 /*
720 * Preserve KAME behaviour: ENOENT can be returned
721 * when an SA acquire is in progress. Don't propagate
722 * this to user-level; it confuses applications.
723 *
724 * XXX this will go away when the SADB is redone.
725 */
726 if (error == ENOENT)
727 error = 0;
728 KEY_SP_UNREF(&sp);
729 splx(s);
730 *done = true;
731 return error;
732 }
733
734 int
735 ipsec4_input(struct mbuf *m, int flags)
736 {
737 struct secpolicy *sp;
738 int error, s;
739
740 s = splsoftnet();
741 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
742 if (sp == NULL) {
743 splx(s);
744 return EINVAL;
745 }
746
747 /*
748 * Check security policy against packet attributes.
749 */
750 error = ipsec_in_reject(sp, m);
751 KEY_SP_UNREF(&sp);
752 splx(s);
753 if (error) {
754 return error;
755 }
756
757 if (flags == 0) {
758 /* We are done. */
759 return 0;
760 }
761
762 /*
763 * Peek at the outbound SP for this packet to determine if
764 * it is a Fast Forward candidate.
765 */
766 s = splsoftnet();
767 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, NULL);
768 if (sp != NULL) {
769 m->m_flags &= ~M_CANFASTFWD;
770 KEY_SP_UNREF(&sp);
771 }
772 splx(s);
773 return 0;
774 }
775
776 int
777 ipsec4_forward(struct mbuf *m, int *destmtu)
778 {
779 /*
780 * If the packet is routed over IPsec tunnel, tell the
781 * originator the tunnel MTU.
782 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
783 * XXX quickhack!!!
784 */
785 struct secpolicy *sp;
786 size_t ipsechdr;
787 int error;
788
789 sp = ipsec4_getpolicybyaddr(m,
790 IPSEC_DIR_OUTBOUND, IP_FORWARDING, &error);
791 if (sp == NULL) {
792 return EINVAL;
793 }
794
795 /* Count IPsec header size. */
796 ipsechdr = ipsec4_hdrsiz(m, IPSEC_DIR_OUTBOUND, NULL);
797
798 /*
799 * Find the correct route for outer IPv4 header, compute tunnel MTU.
800 */
801 if (sp->req) {
802 struct secasvar *sav;
803
804 sav = ipsec_lookup_sa(sp->req, m);
805 if (sav != NULL) {
806 struct route *ro;
807 struct rtentry *rt;
808
809 ro = &sav->sah->sa_route;
810 rt = rtcache_validate(ro);
811 if (rt && rt->rt_ifp) {
812 *destmtu = rt->rt_rmx.rmx_mtu ?
813 rt->rt_rmx.rmx_mtu : rt->rt_ifp->if_mtu;
814 *destmtu -= ipsechdr;
815 }
816 rtcache_unref(rt, ro);
817 KEY_SA_UNREF(&sav);
818 }
819 }
820 KEY_SP_UNREF(&sp);
821 return 0;
822 }
823
824 #ifdef INET6
825 struct secpolicy *
826 ipsec6_checkpolicy(struct mbuf *m, u_int dir, u_int flag, int *error,
827 struct in6pcb *in6p)
828 {
829 struct secpolicy *sp;
830
831 *error = 0;
832
833 if (in6p == NULL) {
834 sp = ipsec_getpolicybyaddr(m, dir, flag, error);
835 } else {
836 KASSERT(in6p->in6p_socket != NULL);
837 sp = ipsec_getpolicybysock(m, dir, (struct inpcb_hdr *)in6p, error);
838 }
839 if (sp == NULL) {
840 KASSERTMSG(*error != 0, "getpolicy failed w/o error");
841 IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
842 return NULL;
843 }
844 KASSERTMSG(*error == 0, "sp w/ error set to %u", *error);
845 switch (sp->policy) {
846 case IPSEC_POLICY_ENTRUST:
847 default:
848 printf("%s: invalid policy %u\n", __func__, sp->policy);
849 /* fall thru... */
850 case IPSEC_POLICY_DISCARD:
851 IPSEC_STATINC(IPSEC_STAT_OUT_POLVIO);
852 *error = -EINVAL; /* packet is discarded by caller */
853 break;
854 case IPSEC_POLICY_BYPASS:
855 case IPSEC_POLICY_NONE:
856 KEY_SP_UNREF(&sp);
857 sp = NULL; /* NB: force NULL result */
858 break;
859 case IPSEC_POLICY_IPSEC:
860 KASSERT(sp->req != NULL);
861 break;
862 }
863 if (*error != 0) {
864 KEY_SP_UNREF(&sp);
865 sp = NULL;
866 IPSECLOG(LOG_DEBUG, "done, error %d\n", *error);
867 }
868 return sp;
869 }
870 #endif /* INET6 */
871
872 static int
873 ipsec4_setspidx_inpcb(struct mbuf *m, struct inpcb *pcb)
874 {
875 int error;
876
877 KASSERT(pcb != NULL);
878 KASSERT(pcb->inp_sp != NULL);
879 KASSERT(pcb->inp_sp->sp_out != NULL);
880 KASSERT(pcb->inp_sp->sp_in != NULL);
881
882 error = ipsec_setspidx(m, &pcb->inp_sp->sp_in->spidx, 1);
883 if (error == 0) {
884 pcb->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
885 pcb->inp_sp->sp_out->spidx = pcb->inp_sp->sp_in->spidx;
886 pcb->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
887 } else {
888 memset(&pcb->inp_sp->sp_in->spidx, 0,
889 sizeof(pcb->inp_sp->sp_in->spidx));
890 memset(&pcb->inp_sp->sp_out->spidx, 0,
891 sizeof(pcb->inp_sp->sp_in->spidx));
892 }
893 return error;
894 }
895
896 #ifdef INET6
897 static int
898 ipsec6_setspidx_in6pcb(struct mbuf *m, struct in6pcb *pcb)
899 {
900 struct secpolicyindex *spidx;
901 int error;
902
903 KASSERT(pcb != NULL);
904 KASSERT(pcb->in6p_sp != NULL);
905 KASSERT(pcb->in6p_sp->sp_out != NULL);
906 KASSERT(pcb->in6p_sp->sp_in != NULL);
907
908 memset(&pcb->in6p_sp->sp_in->spidx, 0, sizeof(*spidx));
909 memset(&pcb->in6p_sp->sp_out->spidx, 0, sizeof(*spidx));
910
911 spidx = &pcb->in6p_sp->sp_in->spidx;
912 error = ipsec_setspidx(m, spidx, 1);
913 if (error)
914 goto bad;
915 spidx->dir = IPSEC_DIR_INBOUND;
916
917 spidx = &pcb->in6p_sp->sp_out->spidx;
918 error = ipsec_setspidx(m, spidx, 1);
919 if (error)
920 goto bad;
921 spidx->dir = IPSEC_DIR_OUTBOUND;
922
923 return 0;
924
925 bad:
926 memset(&pcb->in6p_sp->sp_in->spidx, 0, sizeof(*spidx));
927 memset(&pcb->in6p_sp->sp_out->spidx, 0, sizeof(*spidx));
928 return error;
929 }
930 #endif
931
932 /*
933 * configure security policy index (src/dst/proto/sport/dport)
934 * by looking at the content of mbuf.
935 * the caller is responsible for error recovery (like clearing up spidx).
936 */
937 static int
938 ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int needport)
939 {
940 struct ip *ip = NULL;
941 struct ip ipbuf;
942 u_int v;
943 struct mbuf *n;
944 int len;
945 int error;
946
947 KASSERT(m != NULL);
948
949 /*
950 * validate m->m_pkthdr.len. we see incorrect length if we
951 * mistakenly call this function with inconsistent mbuf chain
952 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
953 */
954 len = 0;
955 for (n = m; n; n = n->m_next)
956 len += n->m_len;
957 if (m->m_pkthdr.len != len) {
958 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
959 "total of m_len(%d) != pkthdr.len(%d), ignored.\n",
960 len, m->m_pkthdr.len);
961 return EINVAL;
962 }
963
964 if (m->m_pkthdr.len < sizeof(struct ip)) {
965 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
966 "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
967 m->m_pkthdr.len);
968 return EINVAL;
969 }
970
971 if (m->m_len >= sizeof(*ip))
972 ip = mtod(m, struct ip *);
973 else {
974 m_copydata(m, 0, sizeof(ipbuf), &ipbuf);
975 ip = &ipbuf;
976 }
977 v = ip->ip_v;
978 switch (v) {
979 case 4:
980 error = ipsec4_setspidx_ipaddr(m, spidx);
981 if (error)
982 return error;
983 ipsec4_get_ulp(m, spidx, needport);
984 return 0;
985 #ifdef INET6
986 case 6:
987 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
988 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
989 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
990 "ignored.\n", m->m_pkthdr.len);
991 return EINVAL;
992 }
993 error = ipsec6_setspidx_ipaddr(m, spidx);
994 if (error)
995 return error;
996 ipsec6_get_ulp(m, spidx, needport);
997 return 0;
998 #endif
999 default:
1000 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
1001 "unknown IP version %u, ignored.\n", v);
1002 return EINVAL;
1003 }
1004 }
1005
1006 static void
1007 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
1008 {
1009 u_int8_t nxt;
1010 int off;
1011
1012 /* sanity check */
1013 KASSERT(m != NULL);
1014 KASSERTMSG(m->m_pkthdr.len >= sizeof(struct ip), "packet too short");
1015
1016 /* NB: ip_input() flips it into host endian XXX need more checking */
1017 if (m->m_len >= sizeof(struct ip)) {
1018 struct ip *ip = mtod(m, struct ip *);
1019 if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
1020 goto done;
1021 off = ip->ip_hl << 2;
1022 nxt = ip->ip_p;
1023 } else {
1024 struct ip ih;
1025
1026 m_copydata(m, 0, sizeof (struct ip), &ih);
1027 if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
1028 goto done;
1029 off = ih.ip_hl << 2;
1030 nxt = ih.ip_p;
1031 }
1032
1033 while (off < m->m_pkthdr.len) {
1034 struct ip6_ext ip6e;
1035 struct tcphdr th;
1036 struct udphdr uh;
1037 struct icmp icmph;
1038
1039 switch (nxt) {
1040 case IPPROTO_TCP:
1041 spidx->ul_proto = nxt;
1042 if (!needport)
1043 goto done_proto;
1044 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
1045 goto done;
1046 m_copydata(m, off, sizeof (th), &th);
1047 spidx->src.sin.sin_port = th.th_sport;
1048 spidx->dst.sin.sin_port = th.th_dport;
1049 return;
1050 case IPPROTO_UDP:
1051 spidx->ul_proto = nxt;
1052 if (!needport)
1053 goto done_proto;
1054 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
1055 goto done;
1056 m_copydata(m, off, sizeof (uh), &uh);
1057 spidx->src.sin.sin_port = uh.uh_sport;
1058 spidx->dst.sin.sin_port = uh.uh_dport;
1059 return;
1060 case IPPROTO_AH:
1061 if (m->m_pkthdr.len > off + sizeof(ip6e))
1062 goto done;
1063 /* XXX sigh, this works but is totally bogus */
1064 m_copydata(m, off, sizeof(ip6e), &ip6e);
1065 off += (ip6e.ip6e_len + 2) << 2;
1066 nxt = ip6e.ip6e_nxt;
1067 break;
1068 case IPPROTO_ICMP:
1069 spidx->ul_proto = nxt;
1070 if (off + sizeof(struct icmp) > m->m_pkthdr.len)
1071 return;
1072 m_copydata(m, off, sizeof(icmph), &icmph);
1073 ((struct sockaddr_in *)&spidx->src)->sin_port =
1074 htons((uint16_t)icmph.icmp_type);
1075 ((struct sockaddr_in *)&spidx->dst)->sin_port =
1076 htons((uint16_t)icmph.icmp_code);
1077 return;
1078 default:
1079 /* XXX intermediate headers??? */
1080 spidx->ul_proto = nxt;
1081 goto done_proto;
1082 }
1083 }
1084 done:
1085 spidx->ul_proto = IPSEC_ULPROTO_ANY;
1086 done_proto:
1087 spidx->src.sin.sin_port = IPSEC_PORT_ANY;
1088 spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
1089 }
1090
1091 /* assumes that m is sane */
1092 static int
1093 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
1094 {
1095 static const struct sockaddr_in template = {
1096 sizeof (struct sockaddr_in),
1097 AF_INET,
1098 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
1099 };
1100
1101 spidx->src.sin = template;
1102 spidx->dst.sin = template;
1103
1104 if (m->m_len < sizeof (struct ip)) {
1105 m_copydata(m, offsetof(struct ip, ip_src),
1106 sizeof(struct in_addr), &spidx->src.sin.sin_addr);
1107 m_copydata(m, offsetof(struct ip, ip_dst),
1108 sizeof(struct in_addr), &spidx->dst.sin.sin_addr);
1109 } else {
1110 struct ip *ip = mtod(m, struct ip *);
1111 spidx->src.sin.sin_addr = ip->ip_src;
1112 spidx->dst.sin.sin_addr = ip->ip_dst;
1113 }
1114
1115 spidx->prefs = sizeof(struct in_addr) << 3;
1116 spidx->prefd = sizeof(struct in_addr) << 3;
1117
1118 return 0;
1119 }
1120
1121 #ifdef INET6
1122 static void
1123 ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *spidx,
1124 int needport)
1125 {
1126 int off, nxt;
1127 struct tcphdr th;
1128 struct udphdr uh;
1129 struct icmp6_hdr icmph;
1130
1131 KASSERT(m != NULL);
1132
1133 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1134 kdebug_mbuf(__func__, m);
1135 }
1136
1137 /* set default */
1138 spidx->ul_proto = IPSEC_ULPROTO_ANY;
1139 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
1140 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
1141
1142 nxt = -1;
1143 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
1144 if (off < 0 || m->m_pkthdr.len < off)
1145 return;
1146
1147 switch (nxt) {
1148 case IPPROTO_TCP:
1149 spidx->ul_proto = nxt;
1150 if (!needport)
1151 break;
1152 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
1153 break;
1154 m_copydata(m, off, sizeof(th), &th);
1155 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
1156 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
1157 break;
1158 case IPPROTO_UDP:
1159 spidx->ul_proto = nxt;
1160 if (!needport)
1161 break;
1162 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
1163 break;
1164 m_copydata(m, off, sizeof(uh), &uh);
1165 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
1166 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
1167 break;
1168 case IPPROTO_ICMPV6:
1169 spidx->ul_proto = nxt;
1170 if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
1171 break;
1172 m_copydata(m, off, sizeof(icmph), &icmph);
1173 ((struct sockaddr_in6 *)&spidx->src)->sin6_port =
1174 htons((uint16_t)icmph.icmp6_type);
1175 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
1176 htons((uint16_t)icmph.icmp6_code);
1177 break;
1178 default:
1179 /* XXX intermediate headers??? */
1180 spidx->ul_proto = nxt;
1181 break;
1182 }
1183 }
1184
1185 /* assumes that m is sane */
1186 static int
1187 ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
1188 {
1189 struct ip6_hdr *ip6 = NULL;
1190 struct ip6_hdr ip6buf;
1191 struct sockaddr_in6 *sin6;
1192
1193 if (m->m_len >= sizeof(*ip6))
1194 ip6 = mtod(m, struct ip6_hdr *);
1195 else {
1196 m_copydata(m, 0, sizeof(ip6buf), &ip6buf);
1197 ip6 = &ip6buf;
1198 }
1199
1200 sin6 = (struct sockaddr_in6 *)&spidx->src;
1201 memset(sin6, 0, sizeof(*sin6));
1202 sin6->sin6_family = AF_INET6;
1203 sin6->sin6_len = sizeof(struct sockaddr_in6);
1204 memcpy(&sin6->sin6_addr, &ip6->ip6_src, sizeof(ip6->ip6_src));
1205 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
1206 sin6->sin6_addr.s6_addr16[1] = 0;
1207 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
1208 }
1209 spidx->prefs = sizeof(struct in6_addr) << 3;
1210
1211 sin6 = (struct sockaddr_in6 *)&spidx->dst;
1212 memset(sin6, 0, sizeof(*sin6));
1213 sin6->sin6_family = AF_INET6;
1214 sin6->sin6_len = sizeof(struct sockaddr_in6);
1215 memcpy(&sin6->sin6_addr, &ip6->ip6_dst, sizeof(ip6->ip6_dst));
1216 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
1217 sin6->sin6_addr.s6_addr16[1] = 0;
1218 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
1219 }
1220 spidx->prefd = sizeof(struct in6_addr) << 3;
1221
1222 return 0;
1223 }
1224 #endif
1225
1226 static void
1227 ipsec_delpcbpolicy(struct inpcbpolicy *p)
1228 {
1229
1230 kmem_intr_free(p, sizeof(*p));
1231 }
1232
1233 /* initialize policy in PCB */
1234 int
1235 ipsec_init_policy(struct socket *so, struct inpcbpolicy **policy)
1236 {
1237 struct inpcbpolicy *new;
1238
1239 KASSERT(so != NULL);
1240 KASSERT(policy != NULL);
1241
1242 new = kmem_intr_zalloc(sizeof(*new), KM_NOSLEEP);
1243 if (new == NULL) {
1244 IPSECLOG(LOG_DEBUG, "No more memory.\n");
1245 return ENOBUFS;
1246 }
1247
1248 if (IPSEC_PRIVILEGED_SO(so))
1249 new->priv = 1;
1250 else
1251 new->priv = 0;
1252
1253 /*
1254 * Set dummy SPs. Actual SPs will be allocated later if needed.
1255 */
1256 new->sp_in = &ipsec_dummy_sp;
1257 new->sp_out = &ipsec_dummy_sp;
1258
1259 *policy = new;
1260
1261 return 0;
1262 }
1263
1264 #if 0 /* unused */
1265 /* copy old ipsec policy into new */
1266 int
1267 ipsec_copy_policy(const struct inpcbpolicy *old, struct inpcbpolicy *new)
1268 {
1269 struct secpolicy *sp;
1270
1271 sp = ipsec_deepcopy_policy(old->sp_in);
1272 if (sp) {
1273 KEY_SP_UNREF(&new->sp_in);
1274 new->sp_in = sp;
1275 } else
1276 return ENOBUFS;
1277
1278 sp = ipsec_deepcopy_policy(old->sp_out);
1279 if (sp) {
1280 KEY_SP_UNREF(&new->sp_out);
1281 new->sp_out = sp;
1282 } else
1283 return ENOBUFS;
1284
1285 new->priv = old->priv;
1286
1287 return 0;
1288 }
1289
1290 /* deep-copy a policy in PCB */
1291 static struct secpolicy *
1292 ipsec_deepcopy_policy(const struct secpolicy *src)
1293 {
1294 struct ipsecrequest *newchain = NULL;
1295 const struct ipsecrequest *p;
1296 struct ipsecrequest **q;
1297 struct secpolicy *dst;
1298
1299 if (src == NULL)
1300 return NULL;
1301 dst = KEY_NEWSP();
1302 if (dst == NULL)
1303 return NULL;
1304
1305 /*
1306 * deep-copy IPsec request chain. This is required since struct
1307 * ipsecrequest is not reference counted.
1308 */
1309 q = &newchain;
1310 for (p = src->req; p; p = p->next) {
1311 *q = kmem_zalloc(sizeof(**q), KM_SLEEP);
1312 (*q)->next = NULL;
1313
1314 (*q)->saidx.proto = p->saidx.proto;
1315 (*q)->saidx.mode = p->saidx.mode;
1316 (*q)->level = p->level;
1317 (*q)->saidx.reqid = p->saidx.reqid;
1318
1319 memcpy(&(*q)->saidx.src, &p->saidx.src, sizeof((*q)->saidx.src));
1320 memcpy(&(*q)->saidx.dst, &p->saidx.dst, sizeof((*q)->saidx.dst));
1321
1322 (*q)->sp = dst;
1323
1324 q = &((*q)->next);
1325 }
1326
1327 dst->req = newchain;
1328 dst->state = src->state;
1329 dst->policy = src->policy;
1330 /* do not touch the refcnt fields */
1331
1332 return dst;
1333 }
1334 #endif
1335
1336 static void
1337 ipsec_destroy_policy(struct secpolicy *sp)
1338 {
1339
1340 if (sp == &ipsec_dummy_sp)
1341 ; /* It's dummy. No need to free it. */
1342 else {
1343 /*
1344 * We cannot destroy here because it can be called in
1345 * softint. So mark the SP as DEAD and let the timer
1346 * destroy it. See key_timehandler_spd.
1347 */
1348 sp->state = IPSEC_SPSTATE_DEAD;
1349 }
1350 }
1351
1352 /* set policy and ipsec request if present. */
1353 static int
1354 ipsec_set_policy(
1355 struct secpolicy **policy,
1356 int optname,
1357 const void *request,
1358 size_t len,
1359 kauth_cred_t cred
1360 )
1361 {
1362 const struct sadb_x_policy *xpl;
1363 struct secpolicy *newsp = NULL, *oldsp;
1364 int error;
1365
1366 KASSERT(!cpu_softintr_p());
1367
1368 /* sanity check. */
1369 if (policy == NULL || *policy == NULL || request == NULL)
1370 return EINVAL;
1371 if (len < sizeof(*xpl))
1372 return EINVAL;
1373 xpl = (const struct sadb_x_policy *)request;
1374
1375 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1376 kdebug_sadb_xpolicy("set passed policy", request);
1377 }
1378
1379 /* check policy type */
1380 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1381 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
1382 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
1383 return EINVAL;
1384
1385 /* check privileged socket */
1386 if (xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1387 error = kauth_authorize_network(cred, KAUTH_NETWORK_IPSEC,
1388 KAUTH_REQ_NETWORK_IPSEC_BYPASS, NULL, NULL, NULL);
1389 if (error)
1390 return (error);
1391 }
1392
1393 /* allocation new SP entry */
1394 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
1395 return error;
1396
1397 key_init_sp(newsp);
1398 newsp->created = time_uptime;
1399 /* Insert the global list for SPs for sockets */
1400 key_socksplist_add(newsp);
1401
1402 /* clear old SP and set new SP */
1403 oldsp = *policy;
1404 *policy = newsp;
1405 ipsec_destroy_policy(oldsp);
1406
1407 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1408 printf("%s: new policy\n", __func__);
1409 kdebug_secpolicy(newsp);
1410 }
1411
1412 return 0;
1413 }
1414
1415 static int
1416 ipsec_get_policy(struct secpolicy *policy, struct mbuf **mp)
1417 {
1418
1419 /* sanity check. */
1420 if (policy == NULL || mp == NULL)
1421 return EINVAL;
1422
1423 *mp = key_sp2msg(policy, M_NOWAIT);
1424 if (!*mp) {
1425 IPSECLOG(LOG_DEBUG, "No more memory.\n");
1426 return ENOBUFS;
1427 }
1428
1429 (*mp)->m_type = MT_DATA;
1430 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1431 kdebug_mbuf(__func__, *mp);
1432 }
1433
1434 return 0;
1435 }
1436
1437 int
1438 ipsec4_set_policy(struct inpcb *inp, int optname, const void *request,
1439 size_t len, kauth_cred_t cred)
1440 {
1441 const struct sadb_x_policy *xpl;
1442 struct secpolicy **policy;
1443
1444 KASSERT(!cpu_softintr_p());
1445 KASSERT(inp != NULL);
1446 KASSERT(inp_locked(inp));
1447 KASSERT(request != NULL);
1448
1449 if (len < sizeof(*xpl))
1450 return EINVAL;
1451 xpl = (const struct sadb_x_policy *)request;
1452
1453 KASSERT(inp->inp_sp != NULL);
1454
1455 /* select direction */
1456 switch (xpl->sadb_x_policy_dir) {
1457 case IPSEC_DIR_INBOUND:
1458 policy = &inp->inp_sp->sp_in;
1459 break;
1460 case IPSEC_DIR_OUTBOUND:
1461 policy = &inp->inp_sp->sp_out;
1462 break;
1463 default:
1464 IPSECLOG(LOG_ERR, "invalid direction=%u\n",
1465 xpl->sadb_x_policy_dir);
1466 return EINVAL;
1467 }
1468
1469 return ipsec_set_policy(policy, optname, request, len, cred);
1470 }
1471
1472 int
1473 ipsec4_get_policy(struct inpcb *inp, const void *request, size_t len,
1474 struct mbuf **mp)
1475 {
1476 const struct sadb_x_policy *xpl;
1477 struct secpolicy *policy;
1478
1479 /* sanity check. */
1480 if (inp == NULL || request == NULL || mp == NULL)
1481 return EINVAL;
1482 KASSERT(inp->inp_sp != NULL);
1483 if (len < sizeof(*xpl))
1484 return EINVAL;
1485 xpl = (const struct sadb_x_policy *)request;
1486
1487 /* select direction */
1488 switch (xpl->sadb_x_policy_dir) {
1489 case IPSEC_DIR_INBOUND:
1490 policy = inp->inp_sp->sp_in;
1491 break;
1492 case IPSEC_DIR_OUTBOUND:
1493 policy = inp->inp_sp->sp_out;
1494 break;
1495 default:
1496 IPSECLOG(LOG_ERR, "invalid direction=%u\n",
1497 xpl->sadb_x_policy_dir);
1498 return EINVAL;
1499 }
1500
1501 return ipsec_get_policy(policy, mp);
1502 }
1503
1504 /* delete policy in PCB */
1505 int
1506 ipsec4_delete_pcbpolicy(struct inpcb *inp)
1507 {
1508
1509 KASSERT(inp != NULL);
1510
1511 if (inp->inp_sp == NULL)
1512 return 0;
1513
1514 if (inp->inp_sp->sp_in != NULL)
1515 ipsec_destroy_policy(inp->inp_sp->sp_in);
1516
1517 if (inp->inp_sp->sp_out != NULL)
1518 ipsec_destroy_policy(inp->inp_sp->sp_out);
1519
1520 ipsec_invalpcbcache(inp->inp_sp, IPSEC_DIR_ANY);
1521
1522 ipsec_delpcbpolicy(inp->inp_sp);
1523 inp->inp_sp = NULL;
1524
1525 return 0;
1526 }
1527
1528 #ifdef INET6
1529 int
1530 ipsec6_set_policy(struct in6pcb *in6p, int optname, const void *request,
1531 size_t len, kauth_cred_t cred)
1532 {
1533 const struct sadb_x_policy *xpl;
1534 struct secpolicy **policy;
1535
1536 KASSERT(!cpu_softintr_p());
1537 KASSERT(in6p_locked(in6p));
1538
1539 /* sanity check. */
1540 if (in6p == NULL || request == NULL)
1541 return EINVAL;
1542 if (len < sizeof(*xpl))
1543 return EINVAL;
1544 xpl = (const struct sadb_x_policy *)request;
1545
1546 /* select direction */
1547 switch (xpl->sadb_x_policy_dir) {
1548 case IPSEC_DIR_INBOUND:
1549 policy = &in6p->in6p_sp->sp_in;
1550 break;
1551 case IPSEC_DIR_OUTBOUND:
1552 policy = &in6p->in6p_sp->sp_out;
1553 break;
1554 default:
1555 IPSECLOG(LOG_ERR, "invalid direction=%u\n",
1556 xpl->sadb_x_policy_dir);
1557 return EINVAL;
1558 }
1559
1560 return ipsec_set_policy(policy, optname, request, len, cred);
1561 }
1562
1563 int
1564 ipsec6_get_policy(struct in6pcb *in6p, const void *request, size_t len,
1565 struct mbuf **mp)
1566 {
1567 const struct sadb_x_policy *xpl;
1568 struct secpolicy *policy;
1569
1570 /* sanity check. */
1571 if (in6p == NULL || request == NULL || mp == NULL)
1572 return EINVAL;
1573 KASSERT(in6p->in6p_sp != NULL);
1574 if (len < sizeof(*xpl))
1575 return EINVAL;
1576 xpl = (const struct sadb_x_policy *)request;
1577
1578 /* select direction */
1579 switch (xpl->sadb_x_policy_dir) {
1580 case IPSEC_DIR_INBOUND:
1581 policy = in6p->in6p_sp->sp_in;
1582 break;
1583 case IPSEC_DIR_OUTBOUND:
1584 policy = in6p->in6p_sp->sp_out;
1585 break;
1586 default:
1587 IPSECLOG(LOG_ERR, "invalid direction=%u\n",
1588 xpl->sadb_x_policy_dir);
1589 return EINVAL;
1590 }
1591
1592 return ipsec_get_policy(policy, mp);
1593 }
1594
1595 int
1596 ipsec6_delete_pcbpolicy(struct in6pcb *in6p)
1597 {
1598
1599 KASSERT(in6p != NULL);
1600
1601 if (in6p->in6p_sp == NULL)
1602 return 0;
1603
1604 if (in6p->in6p_sp->sp_in != NULL)
1605 ipsec_destroy_policy(in6p->in6p_sp->sp_in);
1606
1607 if (in6p->in6p_sp->sp_out != NULL)
1608 ipsec_destroy_policy(in6p->in6p_sp->sp_out);
1609
1610 ipsec_invalpcbcache(in6p->in6p_sp, IPSEC_DIR_ANY);
1611
1612 ipsec_delpcbpolicy(in6p->in6p_sp);
1613 in6p->in6p_sp = NULL;
1614
1615 return 0;
1616 }
1617 #endif
1618
1619 /*
1620 * return current level.
1621 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1622 */
1623 u_int
1624 ipsec_get_reqlevel(const struct ipsecrequest *isr)
1625 {
1626 u_int level = 0;
1627 u_int esp_trans_deflev, esp_net_deflev;
1628 u_int ah_trans_deflev, ah_net_deflev;
1629
1630 KASSERT(isr != NULL);
1631 KASSERT(isr->sp != NULL);
1632 KASSERTMSG(
1633 isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1634 "af family mismatch, src %u, dst %u",
1635 isr->sp->spidx.src.sa.sa_family, isr->sp->spidx.dst.sa.sa_family);
1636
1637 /* XXX note that we have ipseclog() expanded here - code sync issue */
1638 #define IPSEC_CHECK_DEFAULT(lev) \
1639 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1640 && (lev) != IPSEC_LEVEL_UNIQUE) ? \
1641 (ipsec_debug ? log(LOG_INFO, "fixed system default level " #lev \
1642 ":%d->%d\n", (lev), IPSEC_LEVEL_REQUIRE) : (void)0), \
1643 (lev) = IPSEC_LEVEL_REQUIRE, (lev) \
1644 : (lev))
1645
1646 /* set default level */
1647 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1648 #ifdef INET
1649 case AF_INET:
1650 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
1651 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
1652 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
1653 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
1654 break;
1655 #endif
1656 #ifdef INET6
1657 case AF_INET6:
1658 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
1659 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
1660 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
1661 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
1662 break;
1663 #endif /* INET6 */
1664 default:
1665 panic("%s: unknown af %u", __func__,
1666 isr->sp->spidx.src.sa.sa_family);
1667 }
1668
1669 #undef IPSEC_CHECK_DEFAULT
1670
1671 /* set level */
1672 switch (isr->level) {
1673 case IPSEC_LEVEL_DEFAULT:
1674 switch (isr->saidx.proto) {
1675 case IPPROTO_ESP:
1676 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1677 level = esp_net_deflev;
1678 else
1679 level = esp_trans_deflev;
1680 break;
1681 case IPPROTO_AH:
1682 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1683 level = ah_net_deflev;
1684 else
1685 level = ah_trans_deflev;
1686 break;
1687 case IPPROTO_IPCOMP:
1688 /*
1689 * we don't really care, as IPcomp document says that
1690 * we shouldn't compress small packets
1691 */
1692 level = IPSEC_LEVEL_USE;
1693 break;
1694 default:
1695 panic("%s: Illegal protocol defined %u", __func__,
1696 isr->saidx.proto);
1697 }
1698 break;
1699
1700 case IPSEC_LEVEL_USE:
1701 case IPSEC_LEVEL_REQUIRE:
1702 level = isr->level;
1703 break;
1704 case IPSEC_LEVEL_UNIQUE:
1705 level = IPSEC_LEVEL_REQUIRE;
1706 break;
1707
1708 default:
1709 panic("%s: Illegal IPsec level %u", __func__, isr->level);
1710 }
1711
1712 return level;
1713 }
1714
1715 /*
1716 * Check security policy requirements against the actual
1717 * packet contents. Return one if the packet should be
1718 * reject as "invalid"; otherwiser return zero to have the
1719 * packet treated as "valid".
1720 *
1721 * OUT:
1722 * 0: valid
1723 * 1: invalid
1724 */
1725 int
1726 ipsec_in_reject(const struct secpolicy *sp, const struct mbuf *m)
1727 {
1728 struct ipsecrequest *isr;
1729
1730 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
1731 printf("%s: using SP\n", __func__);
1732 kdebug_secpolicy(sp);
1733 }
1734
1735 /* check policy */
1736 switch (sp->policy) {
1737 case IPSEC_POLICY_DISCARD:
1738 return 1;
1739 case IPSEC_POLICY_BYPASS:
1740 case IPSEC_POLICY_NONE:
1741 return 0;
1742 }
1743
1744 KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
1745 "invalid policy %u", sp->policy);
1746
1747 /* XXX should compare policy against ipsec header history */
1748
1749 for (isr = sp->req; isr != NULL; isr = isr->next) {
1750 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1751 continue;
1752 switch (isr->saidx.proto) {
1753 case IPPROTO_ESP:
1754 if ((m->m_flags & M_DECRYPTED) == 0) {
1755 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
1756 "ESP m_flags:%x\n", m->m_flags);
1757 return 1;
1758 }
1759 break;
1760 case IPPROTO_AH:
1761 if ((m->m_flags & M_AUTHIPHDR) == 0) {
1762 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
1763 "AH m_flags:%x\n", m->m_flags);
1764 return 1;
1765 }
1766 break;
1767 case IPPROTO_IPCOMP:
1768 /*
1769 * we don't really care, as IPcomp document
1770 * says that we shouldn't compress small
1771 * packets, IPComp policy should always be
1772 * treated as being in "use" level.
1773 */
1774 break;
1775 }
1776 }
1777 return 0; /* valid */
1778 }
1779
1780 /*
1781 * Check AH/ESP integrity.
1782 * This function is called from tcp_input(), udp_input(),
1783 * and {ah,esp}4_input for tunnel mode
1784 */
1785 int
1786 ipsec4_in_reject(struct mbuf *m, struct inpcb *inp)
1787 {
1788 struct secpolicy *sp;
1789 int error;
1790 int result;
1791
1792 KASSERT(m != NULL);
1793
1794 /* get SP for this packet.
1795 * When we are called from ip_forward(), we call
1796 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1797 */
1798 if (inp == NULL)
1799 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1800 else
1801 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND,
1802 (struct inpcb_hdr *)inp, &error);
1803
1804 if (sp != NULL) {
1805 result = ipsec_in_reject(sp, m);
1806 if (result)
1807 IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1808 KEY_SP_UNREF(&sp);
1809 } else {
1810 result = 0; /* XXX should be panic ?
1811 * -> No, there may be error. */
1812 }
1813 return result;
1814 }
1815
1816
1817 #ifdef INET6
1818 /*
1819 * Check AH/ESP integrity.
1820 * This function is called from tcp6_input(), udp6_input(),
1821 * and {ah,esp}6_input for tunnel mode
1822 */
1823 int
1824 ipsec6_in_reject(struct mbuf *m, struct in6pcb *in6p)
1825 {
1826 struct secpolicy *sp = NULL;
1827 int error;
1828 int result;
1829
1830 KASSERT(m != NULL);
1831
1832 /* get SP for this packet.
1833 * When we are called from ip_forward(), we call
1834 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1835 */
1836 if (in6p == NULL)
1837 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1838 else
1839 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND,
1840 (struct inpcb_hdr *)in6p,
1841 &error);
1842
1843 if (sp != NULL) {
1844 result = ipsec_in_reject(sp, m);
1845 if (result)
1846 IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1847 KEY_SP_UNREF(&sp);
1848 } else {
1849 result = 0;
1850 }
1851 return result;
1852 }
1853 #endif
1854
1855 /*
1856 * compute the byte size to be occupied by IPsec header.
1857 * in case it is tunneled, it includes the size of outer IP header.
1858 * NOTE: SP passed is free in this function.
1859 */
1860 static size_t
1861 ipsec_hdrsiz(const struct secpolicy *sp, const struct mbuf *m)
1862 {
1863 struct ipsecrequest *isr;
1864 size_t siz;
1865
1866 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
1867 printf("%s: using SP\n", __func__);
1868 kdebug_secpolicy(sp);
1869 }
1870
1871 switch (sp->policy) {
1872 case IPSEC_POLICY_DISCARD:
1873 case IPSEC_POLICY_BYPASS:
1874 case IPSEC_POLICY_NONE:
1875 return 0;
1876 }
1877
1878 KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
1879 "invalid policy %u", sp->policy);
1880
1881 siz = 0;
1882 for (isr = sp->req; isr != NULL; isr = isr->next) {
1883 size_t clen = 0;
1884 struct secasvar *sav;
1885
1886 switch (isr->saidx.proto) {
1887 case IPPROTO_ESP:
1888 sav = ipsec_lookup_sa(isr, m);
1889 if (sav != NULL) {
1890 clen = esp_hdrsiz(sav);
1891 KEY_SA_UNREF(&sav);
1892 } else
1893 clen = esp_hdrsiz(NULL);
1894 break;
1895 case IPPROTO_AH:
1896 sav = ipsec_lookup_sa(isr, m);
1897 if (sav != NULL) {
1898 clen = ah_hdrsiz(sav);
1899 KEY_SA_UNREF(&sav);
1900 } else
1901 clen = ah_hdrsiz(NULL);
1902 break;
1903 case IPPROTO_IPCOMP:
1904 clen = sizeof(struct ipcomp);
1905 break;
1906 }
1907
1908 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1909 switch (isr->saidx.dst.sa.sa_family) {
1910 case AF_INET:
1911 clen += sizeof(struct ip);
1912 break;
1913 #ifdef INET6
1914 case AF_INET6:
1915 clen += sizeof(struct ip6_hdr);
1916 break;
1917 #endif
1918 default:
1919 IPSECLOG(LOG_ERR, "unknown AF %d in "
1920 "IPsec tunnel SA\n",
1921 ((const struct sockaddr *)&isr->saidx.dst)
1922 ->sa_family);
1923 break;
1924 }
1925 }
1926 siz += clen;
1927 }
1928
1929 return siz;
1930 }
1931
1932 /* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */
1933 size_t
1934 ipsec4_hdrsiz(struct mbuf *m, u_int dir, struct inpcb *inp)
1935 {
1936 struct secpolicy *sp;
1937 int error;
1938 size_t size;
1939
1940 KASSERT(m != NULL);
1941 KASSERTMSG(inp == NULL || inp->inp_socket != NULL, "socket w/o inpcb");
1942
1943 /* get SP for this packet.
1944 * When we are called from ip_forward(), we call
1945 * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1946 */
1947 if (inp == NULL)
1948 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1949 else
1950 sp = ipsec_getpolicybysock(m, dir,
1951 (struct inpcb_hdr *)inp, &error);
1952
1953 if (sp != NULL) {
1954 size = ipsec_hdrsiz(sp, m);
1955 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DATA, "size:%lu.\n",
1956 (unsigned long)size);
1957
1958 KEY_SP_UNREF(&sp);
1959 } else {
1960 size = 0; /* XXX should be panic ? */
1961 }
1962 return size;
1963 }
1964
1965 #ifdef INET6
1966 /* This function is called from ipsec6_hdrsize_tcp(),
1967 * and maybe from ip6_forward.()
1968 */
1969 size_t
1970 ipsec6_hdrsiz(struct mbuf *m, u_int dir, struct in6pcb *in6p)
1971 {
1972 struct secpolicy *sp;
1973 int error;
1974 size_t size;
1975
1976 KASSERT(m != NULL);
1977 KASSERTMSG(in6p == NULL || in6p->in6p_socket != NULL,
1978 "socket w/o inpcb");
1979
1980 /* get SP for this packet */
1981 /* XXX Is it right to call with IP_FORWARDING. */
1982 if (in6p == NULL)
1983 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1984 else
1985 sp = ipsec_getpolicybysock(m, dir,
1986 (struct inpcb_hdr *)in6p,
1987 &error);
1988
1989 if (sp == NULL)
1990 return 0;
1991 size = ipsec_hdrsiz(sp, m);
1992 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DATA, "size:%zu.\n", size);
1993 KEY_SP_UNREF(&sp);
1994
1995 return size;
1996 }
1997 #endif /*INET6*/
1998
1999 /*
2000 * Check the variable replay window.
2001 * ipsec_chkreplay() performs replay check before ICV verification.
2002 * ipsec_updatereplay() updates replay bitmap. This must be called after
2003 * ICV verification (it also performs replay check, which is usually done
2004 * beforehand).
2005 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
2006 *
2007 * based on RFC 2401.
2008 */
2009 int
2010 ipsec_chkreplay(u_int32_t seq, const struct secasvar *sav)
2011 {
2012 const struct secreplay *replay;
2013 u_int32_t diff;
2014 int fr;
2015 u_int32_t wsizeb; /* constant: bits of window size */
2016 int frlast; /* constant: last frame */
2017
2018 IPSEC_SPLASSERT_SOFTNET(__func__);
2019
2020 KASSERT(sav != NULL);
2021 KASSERT(sav->replay != NULL);
2022
2023 replay = sav->replay;
2024
2025 if (replay->wsize == 0)
2026 return 1; /* no need to check replay. */
2027
2028 /* constant */
2029 frlast = replay->wsize - 1;
2030 wsizeb = replay->wsize << 3;
2031
2032 /* sequence number of 0 is invalid */
2033 if (seq == 0)
2034 return 0;
2035
2036 /* first time is always okay */
2037 if (replay->count == 0)
2038 return 1;
2039
2040 if (seq > replay->lastseq) {
2041 /* larger sequences are okay */
2042 return 1;
2043 } else {
2044 /* seq is equal or less than lastseq. */
2045 diff = replay->lastseq - seq;
2046
2047 /* over range to check, i.e. too old or wrapped */
2048 if (diff >= wsizeb)
2049 return 0;
2050
2051 fr = frlast - diff / 8;
2052
2053 /* this packet already seen ? */
2054 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
2055 return 0;
2056
2057 /* out of order but good */
2058 return 1;
2059 }
2060 }
2061
2062 /*
2063 * check replay counter whether to update or not.
2064 * OUT: 0: OK
2065 * 1: NG
2066 */
2067 int
2068 ipsec_updatereplay(u_int32_t seq, const struct secasvar *sav)
2069 {
2070 struct secreplay *replay;
2071 u_int32_t diff;
2072 int fr;
2073 u_int32_t wsizeb; /* constant: bits of window size */
2074 int frlast; /* constant: last frame */
2075
2076 IPSEC_SPLASSERT_SOFTNET(__func__);
2077
2078 KASSERT(sav != NULL);
2079 KASSERT(sav->replay != NULL);
2080
2081 replay = sav->replay;
2082
2083 if (replay->wsize == 0)
2084 goto ok; /* no need to check replay. */
2085
2086 /* constant */
2087 frlast = replay->wsize - 1;
2088 wsizeb = replay->wsize << 3;
2089
2090 /* sequence number of 0 is invalid */
2091 if (seq == 0)
2092 return 1;
2093
2094 /* first time */
2095 if (replay->count == 0) {
2096 replay->lastseq = seq;
2097 memset(replay->bitmap, 0, replay->wsize);
2098 (replay->bitmap)[frlast] = 1;
2099 goto ok;
2100 }
2101
2102 if (seq > replay->lastseq) {
2103 /* seq is larger than lastseq. */
2104 diff = seq - replay->lastseq;
2105
2106 /* new larger sequence number */
2107 if (diff < wsizeb) {
2108 /* In window */
2109 /* set bit for this packet */
2110 vshiftl(replay->bitmap, diff, replay->wsize);
2111 (replay->bitmap)[frlast] |= 1;
2112 } else {
2113 /* this packet has a "way larger" */
2114 memset(replay->bitmap, 0, replay->wsize);
2115 (replay->bitmap)[frlast] = 1;
2116 }
2117 replay->lastseq = seq;
2118
2119 /* larger is good */
2120 } else {
2121 /* seq is equal or less than lastseq. */
2122 diff = replay->lastseq - seq;
2123
2124 /* over range to check, i.e. too old or wrapped */
2125 if (diff >= wsizeb)
2126 return 1;
2127
2128 fr = frlast - diff / 8;
2129
2130 /* this packet already seen ? */
2131 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
2132 return 1;
2133
2134 /* mark as seen */
2135 (replay->bitmap)[fr] |= (1 << (diff % 8));
2136
2137 /* out of order but good */
2138 }
2139
2140 ok:
2141 if (replay->count == ~0) {
2142 char buf[IPSEC_LOGSASTRLEN];
2143
2144 /* set overflow flag */
2145 replay->overflow++;
2146
2147 /* don't increment, no more packets accepted */
2148 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
2149 return 1;
2150
2151 IPSECLOG(LOG_WARNING, "replay counter made %d cycle. %s\n",
2152 replay->overflow, ipsec_logsastr(sav, buf, sizeof(buf)));
2153 }
2154
2155 replay->count++;
2156
2157 return 0;
2158 }
2159
2160 /*
2161 * shift variable length bunffer to left.
2162 * IN: bitmap: pointer to the buffer
2163 * nbit: the number of to shift.
2164 * wsize: buffer size (bytes).
2165 */
2166 static void
2167 vshiftl(unsigned char *bitmap, int nbit, int wsize)
2168 {
2169 int s, j, i;
2170 unsigned char over;
2171
2172 for (j = 0; j < nbit; j += 8) {
2173 s = (nbit - j < 8) ? (nbit - j): 8;
2174 bitmap[0] <<= s;
2175 for (i = 1; i < wsize; i++) {
2176 over = (bitmap[i] >> (8 - s));
2177 bitmap[i] <<= s;
2178 bitmap[i-1] |= over;
2179 }
2180 }
2181
2182 return;
2183 }
2184
2185 /* Return a printable string for the address. */
2186 const char *
2187 ipsec_address(const union sockaddr_union *sa, char *buf, size_t size)
2188 {
2189 switch (sa->sa.sa_family) {
2190 #if INET
2191 case AF_INET:
2192 in_print(buf, size, &sa->sin.sin_addr);
2193 return buf;
2194 #endif /* INET */
2195
2196 #if INET6
2197 case AF_INET6:
2198 in6_print(buf, size, &sa->sin6.sin6_addr);
2199 return buf;
2200 #endif /* INET6 */
2201
2202 default:
2203 return "(unknown address family)";
2204 }
2205 }
2206
2207 const char *
2208 ipsec_logsastr(const struct secasvar *sav, char *buf, size_t size)
2209 {
2210 const struct secasindex *saidx = &sav->sah->saidx;
2211 char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
2212
2213 KASSERTMSG(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
2214 "af family mismatch, src %u, dst %u",
2215 saidx->src.sa.sa_family, saidx->dst.sa.sa_family);
2216
2217 snprintf(buf, size, "SA(SPI=%u src=%s dst=%s)",
2218 (u_int32_t)ntohl(sav->spi),
2219 ipsec_address(&saidx->src, sbuf, sizeof(sbuf)),
2220 ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)));
2221
2222 return buf;
2223 }
2224
2225 void
2226 ipsec_dumpmbuf(struct mbuf *m)
2227 {
2228 int totlen;
2229 int i;
2230 u_char *p;
2231
2232 totlen = 0;
2233 printf("---\n");
2234 while (m) {
2235 p = mtod(m, u_char *);
2236 for (i = 0; i < m->m_len; i++) {
2237 printf("%02x ", p[i]);
2238 totlen++;
2239 if (totlen % 16 == 0)
2240 printf("\n");
2241 }
2242 m = m->m_next;
2243 }
2244 if (totlen % 16 != 0)
2245 printf("\n");
2246 printf("---\n");
2247 }
2248
2249 #ifdef INET6
2250 struct secpolicy *
2251 ipsec6_check_policy(struct mbuf *m, struct in6pcb *in6p,
2252 int flags, int *needipsecp, int *errorp)
2253 {
2254 struct secpolicy *sp = NULL;
2255 int s;
2256 int error = 0;
2257 int needipsec = 0;
2258
2259 if (!ipsec_outdone(m)) {
2260 s = splsoftnet();
2261 if (in6p != NULL &&
2262 ipsec_pcb_skip_ipsec(in6p->in6p_sp, IPSEC_DIR_OUTBOUND)) {
2263 splx(s);
2264 goto skippolicycheck;
2265 }
2266 sp = ipsec6_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error,in6p);
2267
2268 /*
2269 * There are four return cases:
2270 * sp != NULL apply IPsec policy
2271 * sp == NULL, error == 0 no IPsec handling needed
2272 * sp == NULL, error == -EINVAL discard packet w/o error
2273 * sp == NULL, error != 0 discard packet, report error
2274 */
2275
2276 splx(s);
2277 if (sp == NULL) {
2278 /*
2279 * Caller must check the error return to see if it needs to discard
2280 * the packet.
2281 */
2282 needipsec = 0;
2283 } else {
2284 needipsec = 1;
2285 }
2286 }
2287 skippolicycheck:;
2288
2289 *errorp = error;
2290 *needipsecp = needipsec;
2291 return sp;
2292 }
2293
2294 int
2295 ipsec6_input(struct mbuf *m)
2296 {
2297 struct secpolicy *sp;
2298 int s, error;
2299
2300 s = splsoftnet();
2301 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
2302 if (sp != NULL) {
2303 /*
2304 * Check security policy against packet
2305 * attributes.
2306 */
2307 error = ipsec_in_reject(sp, m);
2308 KEY_SP_UNREF(&sp);
2309 } else {
2310 /* XXX error stat??? */
2311 error = EINVAL;
2312 IPSECLOG(LOG_DEBUG, "no SP, packet discarded\n");/*XXX*/
2313 }
2314 splx(s);
2315
2316 return error;
2317 }
2318 #endif /* INET6 */
2319
2320
2321
2322 /* XXX this stuff doesn't belong here... */
2323
2324 static struct xformsw *xforms = NULL;
2325
2326 /*
2327 * Register a transform; typically at system startup.
2328 */
2329 void
2330 xform_register(struct xformsw *xsp)
2331 {
2332 xsp->xf_next = xforms;
2333 xforms = xsp;
2334 }
2335
2336 /*
2337 * Initialize transform support in an sav.
2338 */
2339 int
2340 xform_init(struct secasvar *sav, int xftype)
2341 {
2342 struct xformsw *xsp;
2343
2344 if (sav->tdb_xform != NULL) /* previously initialized */
2345 return 0;
2346 for (xsp = xforms; xsp; xsp = xsp->xf_next)
2347 if (xsp->xf_type == xftype)
2348 return (*xsp->xf_init)(sav, xsp);
2349
2350 IPSECLOG(LOG_DEBUG, "no match for xform type %d\n", xftype);
2351 return EINVAL;
2352 }
2353
2354 void
2355 nat_t_ports_get(struct mbuf *m, u_int16_t *dport, u_int16_t *sport) {
2356 struct m_tag *tag;
2357
2358 if ((tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL))) {
2359 *sport = ((u_int16_t *)(tag + 1))[0];
2360 *dport = ((u_int16_t *)(tag + 1))[1];
2361 } else
2362 *sport = *dport = 0;
2363 }
2364
2365 /*
2366 * XXXJRT This should be done as a protosw init call.
2367 */
2368 void
2369 ipsec_attach(void)
2370 {
2371
2372 ipsec_output_init();
2373
2374 ipsecstat_percpu = percpu_alloc(sizeof(uint64_t) * IPSEC_NSTATS);
2375
2376 sysctl_net_inet_ipsec_setup(NULL);
2377 #ifdef INET6
2378 sysctl_net_inet6_ipsec6_setup(NULL);
2379 #endif
2380
2381 ah_attach();
2382 esp_attach();
2383 ipcomp_attach();
2384 ipe4_attach();
2385 #ifdef TCP_SIGNATURE
2386 tcpsignature_attach();
2387 #endif
2388 }
2389