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