ipsec.c revision 1.138 1 /* $NetBSD: ipsec.c,v 1.138 2018/02/26 08:50:25 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.138 2018/02/26 08:50:25 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 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
727 if (sp == NULL) {
728 splx(s);
729 return EINVAL;
730 }
731
732 /*
733 * Check security policy against packet attributes.
734 */
735 error = ipsec_sp_reject(sp, m);
736 KEY_SP_UNREF(&sp);
737 splx(s);
738 if (error) {
739 return error;
740 }
741
742 if (flags == 0) {
743 /* We are done. */
744 return 0;
745 }
746
747 /*
748 * Peek at the outbound SP for this packet to determine if
749 * it is a Fast Forward candidate.
750 */
751 s = splsoftnet();
752 sp = ipsec_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, NULL);
753 if (sp != NULL) {
754 m->m_flags &= ~M_CANFASTFWD;
755 KEY_SP_UNREF(&sp);
756 }
757 splx(s);
758 return 0;
759 }
760
761 int
762 ipsec4_forward(struct mbuf *m, int *destmtu)
763 {
764 /*
765 * If the packet is routed over IPsec tunnel, tell the
766 * originator the tunnel MTU.
767 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
768 * XXX quickhack!!!
769 */
770 struct secpolicy *sp;
771 size_t ipsechdr;
772 int error;
773
774 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING,
775 &error);
776 if (sp == NULL) {
777 return EINVAL;
778 }
779
780 /* Count IPsec header size. */
781 ipsechdr = ipsec_hdrsiz(m, IPSEC_DIR_OUTBOUND, NULL);
782
783 /*
784 * Find the correct route for outer IPv4 header, compute tunnel MTU.
785 */
786 if (sp->req) {
787 struct secasvar *sav;
788
789 sav = ipsec_lookup_sa(sp->req, m);
790 if (sav != NULL) {
791 struct route *ro;
792 struct rtentry *rt;
793
794 ro = &sav->sah->sa_route;
795 rt = rtcache_validate(ro);
796 if (rt && rt->rt_ifp) {
797 *destmtu = rt->rt_rmx.rmx_mtu ?
798 rt->rt_rmx.rmx_mtu : rt->rt_ifp->if_mtu;
799 *destmtu -= ipsechdr;
800 }
801 rtcache_unref(rt, ro);
802 KEY_SA_UNREF(&sav);
803 }
804 }
805 KEY_SP_UNREF(&sp);
806 return 0;
807 }
808
809 static int
810 ipsec4_setspidx_inpcb(struct mbuf *m, struct inpcb *pcb)
811 {
812 int error;
813
814 KASSERT(pcb != NULL);
815 KASSERT(pcb->inp_sp != NULL);
816 KASSERT(pcb->inp_sp->sp_out != NULL);
817 KASSERT(pcb->inp_sp->sp_in != NULL);
818
819 error = ipsec_setspidx(m, &pcb->inp_sp->sp_in->spidx, 1);
820 if (error == 0) {
821 pcb->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
822 pcb->inp_sp->sp_out->spidx = pcb->inp_sp->sp_in->spidx;
823 pcb->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
824 } else {
825 memset(&pcb->inp_sp->sp_in->spidx, 0,
826 sizeof(pcb->inp_sp->sp_in->spidx));
827 memset(&pcb->inp_sp->sp_out->spidx, 0,
828 sizeof(pcb->inp_sp->sp_in->spidx));
829 }
830 return error;
831 }
832
833 #ifdef INET6
834 static int
835 ipsec6_setspidx_in6pcb(struct mbuf *m, struct in6pcb *pcb)
836 {
837 struct secpolicyindex *spidx;
838 int error;
839
840 KASSERT(pcb != NULL);
841 KASSERT(pcb->in6p_sp != NULL);
842 KASSERT(pcb->in6p_sp->sp_out != NULL);
843 KASSERT(pcb->in6p_sp->sp_in != NULL);
844
845 memset(&pcb->in6p_sp->sp_in->spidx, 0, sizeof(*spidx));
846 memset(&pcb->in6p_sp->sp_out->spidx, 0, sizeof(*spidx));
847
848 spidx = &pcb->in6p_sp->sp_in->spidx;
849 error = ipsec_setspidx(m, spidx, 1);
850 if (error)
851 goto bad;
852 spidx->dir = IPSEC_DIR_INBOUND;
853
854 spidx = &pcb->in6p_sp->sp_out->spidx;
855 error = ipsec_setspidx(m, spidx, 1);
856 if (error)
857 goto bad;
858 spidx->dir = IPSEC_DIR_OUTBOUND;
859
860 return 0;
861
862 bad:
863 memset(&pcb->in6p_sp->sp_in->spidx, 0, sizeof(*spidx));
864 memset(&pcb->in6p_sp->sp_out->spidx, 0, sizeof(*spidx));
865 return error;
866 }
867 #endif
868
869 /*
870 * configure security policy index (src/dst/proto/sport/dport)
871 * by looking at the content of mbuf.
872 * the caller is responsible for error recovery (like clearing up spidx).
873 */
874 static int
875 ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int needport)
876 {
877 struct ip *ip = NULL;
878 struct ip ipbuf;
879 u_int v;
880 struct mbuf *n;
881 int len;
882 int error;
883
884 KASSERT(m != NULL);
885
886 /*
887 * validate m->m_pkthdr.len. we see incorrect length if we
888 * mistakenly call this function with inconsistent mbuf chain
889 * (like 4.4BSD tcp/udp processing). XXX should we panic here?
890 */
891 len = 0;
892 for (n = m; n; n = n->m_next)
893 len += n->m_len;
894 if (m->m_pkthdr.len != len) {
895 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
896 "total of m_len(%d) != pkthdr.len(%d), ignored.\n",
897 len, m->m_pkthdr.len);
898 KASSERTMSG(0, "impossible");
899 return EINVAL;
900 }
901
902 if (m->m_pkthdr.len < sizeof(struct ip)) {
903 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
904 "pkthdr.len(%d) < sizeof(struct ip), ignored.\n",
905 m->m_pkthdr.len);
906 return EINVAL;
907 }
908
909 if (m->m_len >= sizeof(*ip)) {
910 ip = mtod(m, struct ip *);
911 } else {
912 m_copydata(m, 0, sizeof(ipbuf), &ipbuf);
913 ip = &ipbuf;
914 }
915 v = ip->ip_v;
916 switch (v) {
917 case 4:
918 error = ipsec4_setspidx_ipaddr(m, spidx);
919 if (error)
920 return error;
921 ipsec4_get_ulp(m, spidx, needport);
922 return 0;
923 #ifdef INET6
924 case 6:
925 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
926 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
927 "pkthdr.len(%d) < sizeof(struct ip6_hdr), "
928 "ignored.\n", m->m_pkthdr.len);
929 return EINVAL;
930 }
931 error = ipsec6_setspidx_ipaddr(m, spidx);
932 if (error)
933 return error;
934 ipsec6_get_ulp(m, spidx, needport);
935 return 0;
936 #endif
937 default:
938 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
939 "unknown IP version %u, ignored.\n", v);
940 return EINVAL;
941 }
942 }
943
944 static void
945 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
946 {
947 u_int8_t nxt;
948 int off;
949
950 KASSERT(m != NULL);
951 KASSERTMSG(m->m_pkthdr.len >= sizeof(struct ip), "packet too short");
952
953 /* NB: ip_input() flips it into host endian XXX need more checking */
954 if (m->m_len >= sizeof(struct ip)) {
955 struct ip *ip = mtod(m, struct ip *);
956 if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
957 goto done;
958 off = ip->ip_hl << 2;
959 nxt = ip->ip_p;
960 } else {
961 struct ip ih;
962
963 m_copydata(m, 0, sizeof(struct ip), &ih);
964 if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
965 goto done;
966 off = ih.ip_hl << 2;
967 nxt = ih.ip_p;
968 }
969
970 while (off < m->m_pkthdr.len) {
971 struct ip6_ext ip6e;
972 struct tcphdr th;
973 struct udphdr uh;
974 struct icmp icmph;
975
976 switch (nxt) {
977 case IPPROTO_TCP:
978 spidx->ul_proto = nxt;
979 if (!needport)
980 goto done_proto;
981 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
982 goto done;
983 m_copydata(m, off, sizeof(th), &th);
984 spidx->src.sin.sin_port = th.th_sport;
985 spidx->dst.sin.sin_port = th.th_dport;
986 return;
987 case IPPROTO_UDP:
988 spidx->ul_proto = nxt;
989 if (!needport)
990 goto done_proto;
991 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
992 goto done;
993 m_copydata(m, off, sizeof(uh), &uh);
994 spidx->src.sin.sin_port = uh.uh_sport;
995 spidx->dst.sin.sin_port = uh.uh_dport;
996 return;
997 case IPPROTO_AH:
998 if (off + sizeof(ip6e) > m->m_pkthdr.len)
999 goto done;
1000 /* XXX sigh, this works but is totally bogus */
1001 m_copydata(m, off, sizeof(ip6e), &ip6e);
1002 off += (ip6e.ip6e_len + 2) << 2;
1003 nxt = ip6e.ip6e_nxt;
1004 break;
1005 case IPPROTO_ICMP:
1006 spidx->ul_proto = nxt;
1007 if (off + sizeof(struct icmp) > m->m_pkthdr.len)
1008 goto done;
1009 m_copydata(m, off, sizeof(icmph), &icmph);
1010 ((struct sockaddr_in *)&spidx->src)->sin_port =
1011 htons((uint16_t)icmph.icmp_type);
1012 ((struct sockaddr_in *)&spidx->dst)->sin_port =
1013 htons((uint16_t)icmph.icmp_code);
1014 return;
1015 default:
1016 /* XXX intermediate headers??? */
1017 spidx->ul_proto = nxt;
1018 goto done_proto;
1019 }
1020 }
1021 done:
1022 spidx->ul_proto = IPSEC_ULPROTO_ANY;
1023 done_proto:
1024 spidx->src.sin.sin_port = IPSEC_PORT_ANY;
1025 spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
1026 }
1027
1028 static int
1029 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
1030 {
1031 static const struct sockaddr_in template = {
1032 sizeof(struct sockaddr_in),
1033 AF_INET,
1034 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
1035 };
1036
1037 spidx->src.sin = template;
1038 spidx->dst.sin = template;
1039
1040 if (m->m_len < sizeof(struct ip)) {
1041 m_copydata(m, offsetof(struct ip, ip_src),
1042 sizeof(struct in_addr), &spidx->src.sin.sin_addr);
1043 m_copydata(m, offsetof(struct ip, ip_dst),
1044 sizeof(struct in_addr), &spidx->dst.sin.sin_addr);
1045 } else {
1046 struct ip *ip = mtod(m, struct ip *);
1047 spidx->src.sin.sin_addr = ip->ip_src;
1048 spidx->dst.sin.sin_addr = ip->ip_dst;
1049 }
1050
1051 spidx->prefs = sizeof(struct in_addr) << 3;
1052 spidx->prefd = sizeof(struct in_addr) << 3;
1053
1054 return 0;
1055 }
1056
1057 #ifdef INET6
1058 static void
1059 ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
1060 {
1061 int off, nxt;
1062 struct tcphdr th;
1063 struct udphdr uh;
1064 struct icmp6_hdr icmph;
1065
1066 KASSERT(m != NULL);
1067
1068 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1069 kdebug_mbuf(__func__, m);
1070 }
1071
1072 /* set default */
1073 spidx->ul_proto = IPSEC_ULPROTO_ANY;
1074 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
1075 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
1076
1077 nxt = -1;
1078 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
1079 if (off < 0 || m->m_pkthdr.len < off)
1080 return;
1081
1082 switch (nxt) {
1083 case IPPROTO_TCP:
1084 spidx->ul_proto = nxt;
1085 if (!needport)
1086 break;
1087 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
1088 break;
1089 m_copydata(m, off, sizeof(th), &th);
1090 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
1091 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
1092 break;
1093 case IPPROTO_UDP:
1094 spidx->ul_proto = nxt;
1095 if (!needport)
1096 break;
1097 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
1098 break;
1099 m_copydata(m, off, sizeof(uh), &uh);
1100 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
1101 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
1102 break;
1103 case IPPROTO_ICMPV6:
1104 spidx->ul_proto = nxt;
1105 if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
1106 break;
1107 m_copydata(m, off, sizeof(icmph), &icmph);
1108 ((struct sockaddr_in6 *)&spidx->src)->sin6_port =
1109 htons((uint16_t)icmph.icmp6_type);
1110 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
1111 htons((uint16_t)icmph.icmp6_code);
1112 break;
1113 default:
1114 /* XXX intermediate headers??? */
1115 spidx->ul_proto = nxt;
1116 break;
1117 }
1118 }
1119
1120 static int
1121 ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
1122 {
1123 struct ip6_hdr *ip6 = NULL;
1124 struct ip6_hdr ip6buf;
1125 struct sockaddr_in6 *sin6;
1126
1127 if (m->m_len >= sizeof(*ip6))
1128 ip6 = mtod(m, struct ip6_hdr *);
1129 else {
1130 m_copydata(m, 0, sizeof(ip6buf), &ip6buf);
1131 ip6 = &ip6buf;
1132 }
1133
1134 sin6 = (struct sockaddr_in6 *)&spidx->src;
1135 memset(sin6, 0, sizeof(*sin6));
1136 sin6->sin6_family = AF_INET6;
1137 sin6->sin6_len = sizeof(struct sockaddr_in6);
1138 memcpy(&sin6->sin6_addr, &ip6->ip6_src, sizeof(ip6->ip6_src));
1139 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
1140 sin6->sin6_addr.s6_addr16[1] = 0;
1141 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
1142 }
1143 spidx->prefs = sizeof(struct in6_addr) << 3;
1144
1145 sin6 = (struct sockaddr_in6 *)&spidx->dst;
1146 memset(sin6, 0, sizeof(*sin6));
1147 sin6->sin6_family = AF_INET6;
1148 sin6->sin6_len = sizeof(struct sockaddr_in6);
1149 memcpy(&sin6->sin6_addr, &ip6->ip6_dst, sizeof(ip6->ip6_dst));
1150 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
1151 sin6->sin6_addr.s6_addr16[1] = 0;
1152 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
1153 }
1154 spidx->prefd = sizeof(struct in6_addr) << 3;
1155
1156 return 0;
1157 }
1158 #endif
1159
1160 static void
1161 ipsec_delpcbpolicy(struct inpcbpolicy *p)
1162 {
1163
1164 kmem_intr_free(p, sizeof(*p));
1165 }
1166
1167 /* initialize policy in PCB */
1168 int
1169 ipsec_init_policy(struct socket *so, struct inpcbpolicy **policy)
1170 {
1171 struct inpcbpolicy *new;
1172
1173 KASSERT(so != NULL);
1174 KASSERT(policy != NULL);
1175
1176 new = kmem_intr_zalloc(sizeof(*new), KM_NOSLEEP);
1177 if (new == NULL) {
1178 IPSECLOG(LOG_DEBUG, "No more memory.\n");
1179 return ENOBUFS;
1180 }
1181
1182 if (IPSEC_PRIVILEGED_SO(so))
1183 new->priv = 1;
1184 else
1185 new->priv = 0;
1186
1187 /*
1188 * Set dummy SPs. Actual SPs will be allocated later if needed.
1189 */
1190 new->sp_in = &ipsec_dummy_sp;
1191 new->sp_out = &ipsec_dummy_sp;
1192
1193 *policy = new;
1194
1195 return 0;
1196 }
1197
1198 #if 0 /* unused */
1199 /* copy old ipsec policy into new */
1200 int
1201 ipsec_copy_policy(const struct inpcbpolicy *old, struct inpcbpolicy *new)
1202 {
1203 struct secpolicy *sp;
1204
1205 sp = ipsec_deepcopy_policy(old->sp_in);
1206 if (sp) {
1207 KEY_SP_UNREF(&new->sp_in);
1208 new->sp_in = sp;
1209 } else
1210 return ENOBUFS;
1211
1212 sp = ipsec_deepcopy_policy(old->sp_out);
1213 if (sp) {
1214 KEY_SP_UNREF(&new->sp_out);
1215 new->sp_out = sp;
1216 } else
1217 return ENOBUFS;
1218
1219 new->priv = old->priv;
1220
1221 return 0;
1222 }
1223
1224 /* deep-copy a policy in PCB */
1225 static struct secpolicy *
1226 ipsec_deepcopy_policy(const struct secpolicy *src)
1227 {
1228 struct ipsecrequest *newchain = NULL;
1229 const struct ipsecrequest *p;
1230 struct ipsecrequest **q;
1231 struct secpolicy *dst;
1232
1233 if (src == NULL)
1234 return NULL;
1235 dst = KEY_NEWSP();
1236 if (dst == NULL)
1237 return NULL;
1238
1239 /*
1240 * deep-copy IPsec request chain. This is required since struct
1241 * ipsecrequest is not reference counted.
1242 */
1243 q = &newchain;
1244 for (p = src->req; p; p = p->next) {
1245 *q = kmem_zalloc(sizeof(**q), KM_SLEEP);
1246 (*q)->next = NULL;
1247
1248 (*q)->saidx.proto = p->saidx.proto;
1249 (*q)->saidx.mode = p->saidx.mode;
1250 (*q)->level = p->level;
1251 (*q)->saidx.reqid = p->saidx.reqid;
1252
1253 memcpy(&(*q)->saidx.src, &p->saidx.src, sizeof((*q)->saidx.src));
1254 memcpy(&(*q)->saidx.dst, &p->saidx.dst, sizeof((*q)->saidx.dst));
1255
1256 (*q)->sp = dst;
1257
1258 q = &((*q)->next);
1259 }
1260
1261 dst->req = newchain;
1262 dst->state = src->state;
1263 dst->policy = src->policy;
1264 /* do not touch the refcnt fields */
1265
1266 return dst;
1267 }
1268 #endif
1269
1270 static void
1271 ipsec_destroy_policy(struct secpolicy *sp)
1272 {
1273
1274 if (sp == &ipsec_dummy_sp)
1275 ; /* It's dummy. No need to free it. */
1276 else {
1277 /*
1278 * We cannot destroy here because it can be called in
1279 * softint. So mark the SP as DEAD and let the timer
1280 * destroy it. See key_timehandler_spd.
1281 */
1282 sp->state = IPSEC_SPSTATE_DEAD;
1283 }
1284 }
1285
1286 /* set policy and ipsec request if present. */
1287 static int
1288 ipsec_set_policy(struct secpolicy **policy, int optname, const void *request,
1289 size_t len, kauth_cred_t cred)
1290 {
1291 const struct sadb_x_policy *xpl;
1292 struct secpolicy *newsp = NULL, *oldsp;
1293 int error;
1294
1295 KASSERT(!cpu_softintr_p());
1296
1297 /* sanity check. */
1298 if (policy == NULL || *policy == NULL || request == NULL)
1299 return EINVAL;
1300 if (len < sizeof(*xpl))
1301 return EINVAL;
1302 xpl = (const struct sadb_x_policy *)request;
1303
1304 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1305 kdebug_sadb_xpolicy("set passed policy", request);
1306 }
1307
1308 /* check policy type */
1309 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */
1310 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD ||
1311 xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
1312 return EINVAL;
1313
1314 /* check privileged socket */
1315 if (xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
1316 error = kauth_authorize_network(cred, KAUTH_NETWORK_IPSEC,
1317 KAUTH_REQ_NETWORK_IPSEC_BYPASS, NULL, NULL, NULL);
1318 if (error)
1319 return error;
1320 }
1321
1322 /* allocation new SP entry */
1323 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
1324 return error;
1325
1326 key_init_sp(newsp);
1327 newsp->created = time_uptime;
1328 /* Insert the global list for SPs for sockets */
1329 key_socksplist_add(newsp);
1330
1331 /* clear old SP and set new SP */
1332 oldsp = *policy;
1333 *policy = newsp;
1334 ipsec_destroy_policy(oldsp);
1335
1336 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1337 printf("%s: new policy\n", __func__);
1338 kdebug_secpolicy(newsp);
1339 }
1340
1341 return 0;
1342 }
1343
1344 static int
1345 ipsec_get_policy(struct secpolicy *policy, struct mbuf **mp)
1346 {
1347
1348 /* sanity check. */
1349 if (policy == NULL || mp == NULL)
1350 return EINVAL;
1351
1352 *mp = key_sp2msg(policy, M_NOWAIT);
1353 if (!*mp) {
1354 IPSECLOG(LOG_DEBUG, "No more memory.\n");
1355 return ENOBUFS;
1356 }
1357
1358 (*mp)->m_type = MT_DATA;
1359 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) {
1360 kdebug_mbuf(__func__, *mp);
1361 }
1362
1363 return 0;
1364 }
1365
1366 int
1367 ipsec4_set_policy(struct inpcb *inp, int optname, const void *request,
1368 size_t len, kauth_cred_t cred)
1369 {
1370 const struct sadb_x_policy *xpl;
1371 struct secpolicy **policy;
1372
1373 KASSERT(!cpu_softintr_p());
1374 KASSERT(inp != NULL);
1375 KASSERT(inp_locked(inp));
1376 KASSERT(request != NULL);
1377
1378 if (len < sizeof(*xpl))
1379 return EINVAL;
1380 xpl = (const struct sadb_x_policy *)request;
1381
1382 KASSERT(inp->inp_sp != NULL);
1383
1384 /* select direction */
1385 switch (xpl->sadb_x_policy_dir) {
1386 case IPSEC_DIR_INBOUND:
1387 policy = &inp->inp_sp->sp_in;
1388 break;
1389 case IPSEC_DIR_OUTBOUND:
1390 policy = &inp->inp_sp->sp_out;
1391 break;
1392 default:
1393 IPSECLOG(LOG_ERR, "invalid direction=%u\n",
1394 xpl->sadb_x_policy_dir);
1395 return EINVAL;
1396 }
1397
1398 return ipsec_set_policy(policy, optname, request, len, cred);
1399 }
1400
1401 int
1402 ipsec4_get_policy(struct inpcb *inp, const void *request, size_t len,
1403 struct mbuf **mp)
1404 {
1405 const struct sadb_x_policy *xpl;
1406 struct secpolicy *policy;
1407
1408 /* sanity check. */
1409 if (inp == NULL || request == NULL || mp == NULL)
1410 return EINVAL;
1411 KASSERT(inp->inp_sp != NULL);
1412 if (len < sizeof(*xpl))
1413 return EINVAL;
1414 xpl = (const struct sadb_x_policy *)request;
1415
1416 /* select direction */
1417 switch (xpl->sadb_x_policy_dir) {
1418 case IPSEC_DIR_INBOUND:
1419 policy = inp->inp_sp->sp_in;
1420 break;
1421 case IPSEC_DIR_OUTBOUND:
1422 policy = inp->inp_sp->sp_out;
1423 break;
1424 default:
1425 IPSECLOG(LOG_ERR, "invalid direction=%u\n",
1426 xpl->sadb_x_policy_dir);
1427 return EINVAL;
1428 }
1429
1430 return ipsec_get_policy(policy, mp);
1431 }
1432
1433 int
1434 ipsec4_delete_pcbpolicy(struct inpcb *inp)
1435 {
1436
1437 KASSERT(inp != NULL);
1438
1439 if (inp->inp_sp == NULL)
1440 return 0;
1441
1442 if (inp->inp_sp->sp_in != NULL)
1443 ipsec_destroy_policy(inp->inp_sp->sp_in);
1444
1445 if (inp->inp_sp->sp_out != NULL)
1446 ipsec_destroy_policy(inp->inp_sp->sp_out);
1447
1448 ipsec_invalpcbcache(inp->inp_sp, IPSEC_DIR_ANY);
1449
1450 ipsec_delpcbpolicy(inp->inp_sp);
1451 inp->inp_sp = NULL;
1452
1453 return 0;
1454 }
1455
1456 #ifdef INET6
1457 int
1458 ipsec6_set_policy(struct in6pcb *in6p, int optname, const void *request,
1459 size_t len, kauth_cred_t cred)
1460 {
1461 const struct sadb_x_policy *xpl;
1462 struct secpolicy **policy;
1463
1464 KASSERT(!cpu_softintr_p());
1465 KASSERT(in6p != NULL);
1466 KASSERT(in6p_locked(in6p));
1467 KASSERT(request != NULL);
1468
1469 if (len < sizeof(*xpl))
1470 return EINVAL;
1471 xpl = (const struct sadb_x_policy *)request;
1472
1473 /* select direction */
1474 switch (xpl->sadb_x_policy_dir) {
1475 case IPSEC_DIR_INBOUND:
1476 policy = &in6p->in6p_sp->sp_in;
1477 break;
1478 case IPSEC_DIR_OUTBOUND:
1479 policy = &in6p->in6p_sp->sp_out;
1480 break;
1481 default:
1482 IPSECLOG(LOG_ERR, "invalid direction=%u\n",
1483 xpl->sadb_x_policy_dir);
1484 return EINVAL;
1485 }
1486
1487 return ipsec_set_policy(policy, optname, request, len, cred);
1488 }
1489
1490 int
1491 ipsec6_get_policy(struct in6pcb *in6p, const void *request, size_t len,
1492 struct mbuf **mp)
1493 {
1494 const struct sadb_x_policy *xpl;
1495 struct secpolicy *policy;
1496
1497 /* sanity check. */
1498 if (in6p == NULL || request == NULL || mp == NULL)
1499 return EINVAL;
1500 KASSERT(in6p->in6p_sp != NULL);
1501 if (len < sizeof(*xpl))
1502 return EINVAL;
1503 xpl = (const struct sadb_x_policy *)request;
1504
1505 /* select direction */
1506 switch (xpl->sadb_x_policy_dir) {
1507 case IPSEC_DIR_INBOUND:
1508 policy = in6p->in6p_sp->sp_in;
1509 break;
1510 case IPSEC_DIR_OUTBOUND:
1511 policy = in6p->in6p_sp->sp_out;
1512 break;
1513 default:
1514 IPSECLOG(LOG_ERR, "invalid direction=%u\n",
1515 xpl->sadb_x_policy_dir);
1516 return EINVAL;
1517 }
1518
1519 return ipsec_get_policy(policy, mp);
1520 }
1521
1522 int
1523 ipsec6_delete_pcbpolicy(struct in6pcb *in6p)
1524 {
1525
1526 KASSERT(in6p != NULL);
1527
1528 if (in6p->in6p_sp == NULL)
1529 return 0;
1530
1531 if (in6p->in6p_sp->sp_in != NULL)
1532 ipsec_destroy_policy(in6p->in6p_sp->sp_in);
1533
1534 if (in6p->in6p_sp->sp_out != NULL)
1535 ipsec_destroy_policy(in6p->in6p_sp->sp_out);
1536
1537 ipsec_invalpcbcache(in6p->in6p_sp, IPSEC_DIR_ANY);
1538
1539 ipsec_delpcbpolicy(in6p->in6p_sp);
1540 in6p->in6p_sp = NULL;
1541
1542 return 0;
1543 }
1544 #endif
1545
1546 /*
1547 * Return the current level (either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE).
1548 */
1549 u_int
1550 ipsec_get_reqlevel(const struct ipsecrequest *isr)
1551 {
1552 u_int level = 0;
1553 u_int esp_trans_deflev, esp_net_deflev;
1554 u_int ah_trans_deflev, ah_net_deflev;
1555
1556 KASSERT(isr != NULL);
1557 KASSERT(isr->sp != NULL);
1558 KASSERTMSG(
1559 isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1560 "af family mismatch, src %u, dst %u",
1561 isr->sp->spidx.src.sa.sa_family, isr->sp->spidx.dst.sa.sa_family);
1562
1563 /* XXX note that we have ipseclog() expanded here - code sync issue */
1564 #define IPSEC_CHECK_DEFAULT(lev) \
1565 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \
1566 && (lev) != IPSEC_LEVEL_UNIQUE) ? \
1567 (ipsec_debug ? log(LOG_INFO, "fixed system default level " #lev \
1568 ":%d->%d\n", (lev), IPSEC_LEVEL_REQUIRE) : (void)0), \
1569 (lev) = IPSEC_LEVEL_REQUIRE, (lev) \
1570 : (lev))
1571
1572 /* set default level */
1573 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1574 #ifdef INET
1575 case AF_INET:
1576 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev);
1577 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev);
1578 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev);
1579 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev);
1580 break;
1581 #endif
1582 #ifdef INET6
1583 case AF_INET6:
1584 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev);
1585 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev);
1586 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev);
1587 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev);
1588 break;
1589 #endif /* INET6 */
1590 default:
1591 panic("%s: unknown af %u", __func__,
1592 isr->sp->spidx.src.sa.sa_family);
1593 }
1594
1595 #undef IPSEC_CHECK_DEFAULT
1596
1597 /* set level */
1598 switch (isr->level) {
1599 case IPSEC_LEVEL_DEFAULT:
1600 switch (isr->saidx.proto) {
1601 case IPPROTO_ESP:
1602 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1603 level = esp_net_deflev;
1604 else
1605 level = esp_trans_deflev;
1606 break;
1607 case IPPROTO_AH:
1608 if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1609 level = ah_net_deflev;
1610 else
1611 level = ah_trans_deflev;
1612 break;
1613 case IPPROTO_IPCOMP:
1614 /*
1615 * we don't really care, as IPcomp document says that
1616 * we shouldn't compress small packets
1617 */
1618 level = IPSEC_LEVEL_USE;
1619 break;
1620 default:
1621 panic("%s: Illegal protocol defined %u", __func__,
1622 isr->saidx.proto);
1623 }
1624 break;
1625
1626 case IPSEC_LEVEL_USE:
1627 case IPSEC_LEVEL_REQUIRE:
1628 level = isr->level;
1629 break;
1630 case IPSEC_LEVEL_UNIQUE:
1631 level = IPSEC_LEVEL_REQUIRE;
1632 break;
1633
1634 default:
1635 panic("%s: Illegal IPsec level %u", __func__, isr->level);
1636 }
1637
1638 return level;
1639 }
1640
1641 /*
1642 * Check security policy requirements against the actual packet contents.
1643 *
1644 * If the SP requires an IPsec packet, and the packet was neither AH nor ESP,
1645 * then kick it.
1646 */
1647 int
1648 ipsec_sp_reject(const struct secpolicy *sp, const struct mbuf *m)
1649 {
1650 struct ipsecrequest *isr;
1651
1652 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
1653 printf("%s: using SP\n", __func__);
1654 kdebug_secpolicy(sp);
1655 }
1656
1657 /* check policy */
1658 switch (sp->policy) {
1659 case IPSEC_POLICY_DISCARD:
1660 return 1;
1661 case IPSEC_POLICY_BYPASS:
1662 case IPSEC_POLICY_NONE:
1663 return 0;
1664 }
1665
1666 KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
1667 "invalid policy %u", sp->policy);
1668
1669 /* XXX should compare policy against ipsec header history */
1670
1671 for (isr = sp->req; isr != NULL; isr = isr->next) {
1672 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1673 continue;
1674 switch (isr->saidx.proto) {
1675 case IPPROTO_ESP:
1676 if ((m->m_flags & M_DECRYPTED) == 0) {
1677 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
1678 "ESP m_flags:%x\n", m->m_flags);
1679 return 1;
1680 }
1681 break;
1682 case IPPROTO_AH:
1683 if ((m->m_flags & M_AUTHIPHDR) == 0) {
1684 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP,
1685 "AH m_flags:%x\n", m->m_flags);
1686 return 1;
1687 }
1688 break;
1689 case IPPROTO_IPCOMP:
1690 /*
1691 * We don't really care, as IPcomp document
1692 * says that we shouldn't compress small
1693 * packets, IPComp policy should always be
1694 * treated as being in "use" level.
1695 */
1696 break;
1697 }
1698 }
1699
1700 return 0;
1701 }
1702
1703 /*
1704 * Check AH/ESP integrity.
1705 * This function is called from tcp_input(), udp_input(),
1706 * and {ah,esp}4_input for tunnel mode
1707 */
1708 int
1709 ipsec4_in_reject(struct mbuf *m, struct inpcb *inp)
1710 {
1711 struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
1712 struct secpolicy *sp;
1713 int error;
1714 int result;
1715
1716 KASSERT(m != NULL);
1717
1718 if (inph == NULL)
1719 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
1720 IP_FORWARDING, &error);
1721 else
1722 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND,
1723 inph, &error);
1724
1725 if (sp != NULL) {
1726 result = ipsec_sp_reject(sp, m);
1727 if (result)
1728 IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1729 KEY_SP_UNREF(&sp);
1730 } else {
1731 result = 0;
1732 }
1733 return result;
1734 }
1735
1736 #ifdef INET6
1737 /*
1738 * Check AH/ESP integrity.
1739 * This function is called from tcp6_input(), udp6_input(),
1740 * and {ah,esp}6_input for tunnel mode
1741 */
1742 int
1743 ipsec6_in_reject(struct mbuf *m, struct in6pcb *in6p)
1744 {
1745 struct inpcb_hdr *inph = (struct inpcb_hdr *)in6p;
1746 struct secpolicy *sp;
1747 int error;
1748 int result;
1749
1750 KASSERT(m != NULL);
1751
1752 if (inph == NULL)
1753 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
1754 IP_FORWARDING, &error);
1755 else
1756 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND,
1757 inph, &error);
1758
1759 if (sp != NULL) {
1760 result = ipsec_sp_reject(sp, m);
1761 if (result)
1762 IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1763 KEY_SP_UNREF(&sp);
1764 } else {
1765 result = 0;
1766 }
1767 return result;
1768 }
1769 #endif
1770
1771 /*
1772 * Compute the byte size to be occupied by the IPsec header. If it is
1773 * tunneled, it includes the size of outer IP header.
1774 */
1775 static size_t
1776 ipsec_sp_hdrsiz(const struct secpolicy *sp, const struct mbuf *m)
1777 {
1778 struct ipsecrequest *isr;
1779 size_t siz;
1780
1781 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) {
1782 printf("%s: using SP\n", __func__);
1783 kdebug_secpolicy(sp);
1784 }
1785
1786 switch (sp->policy) {
1787 case IPSEC_POLICY_DISCARD:
1788 case IPSEC_POLICY_BYPASS:
1789 case IPSEC_POLICY_NONE:
1790 return 0;
1791 }
1792
1793 KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC,
1794 "invalid policy %u", sp->policy);
1795
1796 siz = 0;
1797 for (isr = sp->req; isr != NULL; isr = isr->next) {
1798 size_t clen = 0;
1799 struct secasvar *sav;
1800
1801 switch (isr->saidx.proto) {
1802 case IPPROTO_ESP:
1803 sav = ipsec_lookup_sa(isr, m);
1804 if (sav != NULL) {
1805 clen = esp_hdrsiz(sav);
1806 KEY_SA_UNREF(&sav);
1807 } else
1808 clen = esp_hdrsiz(NULL);
1809 break;
1810 case IPPROTO_AH:
1811 sav = ipsec_lookup_sa(isr, m);
1812 if (sav != NULL) {
1813 clen = ah_hdrsiz(sav);
1814 KEY_SA_UNREF(&sav);
1815 } else
1816 clen = ah_hdrsiz(NULL);
1817 break;
1818 case IPPROTO_IPCOMP:
1819 clen = sizeof(struct ipcomp);
1820 break;
1821 }
1822
1823 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1824 switch (isr->saidx.dst.sa.sa_family) {
1825 case AF_INET:
1826 clen += sizeof(struct ip);
1827 break;
1828 #ifdef INET6
1829 case AF_INET6:
1830 clen += sizeof(struct ip6_hdr);
1831 break;
1832 #endif
1833 default:
1834 IPSECLOG(LOG_ERR, "unknown AF %d in "
1835 "IPsec tunnel SA\n",
1836 ((const struct sockaddr *)&isr->saidx.dst)
1837 ->sa_family);
1838 break;
1839 }
1840 }
1841 siz += clen;
1842 }
1843
1844 return siz;
1845 }
1846
1847 size_t
1848 ipsec_hdrsiz(struct mbuf *m, u_int dir, void *inp)
1849 {
1850 struct inpcb_hdr *inph = (struct inpcb_hdr *)inp;
1851 struct secpolicy *sp;
1852 int error;
1853 size_t size;
1854
1855 KASSERT(m != NULL);
1856 KASSERTMSG(inph == NULL || inph->inph_socket != NULL,
1857 "socket w/o inpcb");
1858
1859 if (inph == NULL)
1860 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1861 else
1862 sp = ipsec_getpolicybysock(m, dir, inph, &error);
1863
1864 if (sp != NULL) {
1865 size = ipsec_sp_hdrsiz(sp, m);
1866 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DATA, "size:%zu.\n", size);
1867 KEY_SP_UNREF(&sp);
1868 } else {
1869 size = 0;
1870 }
1871
1872 return size;
1873 }
1874
1875 /*
1876 * Check the variable replay window.
1877 * ipsec_chkreplay() performs replay check before ICV verification.
1878 * ipsec_updatereplay() updates replay bitmap. This must be called after
1879 * ICV verification (it also performs replay check, which is usually done
1880 * beforehand).
1881 * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1882 *
1883 * based on RFC 2401.
1884 */
1885 int
1886 ipsec_chkreplay(u_int32_t seq, const struct secasvar *sav)
1887 {
1888 const struct secreplay *replay;
1889 u_int32_t diff;
1890 int fr;
1891 u_int32_t wsizeb; /* constant: bits of window size */
1892 int frlast; /* constant: last frame */
1893
1894 IPSEC_SPLASSERT_SOFTNET(__func__);
1895
1896 KASSERT(sav != NULL);
1897 KASSERT(sav->replay != NULL);
1898
1899 replay = sav->replay;
1900
1901 if (replay->wsize == 0)
1902 return 1; /* no need to check replay. */
1903
1904 /* constant */
1905 frlast = replay->wsize - 1;
1906 wsizeb = replay->wsize << 3;
1907
1908 /* sequence number of 0 is invalid */
1909 if (seq == 0)
1910 return 0;
1911
1912 /* first time is always okay */
1913 if (replay->count == 0)
1914 return 1;
1915
1916 if (seq > replay->lastseq) {
1917 /* larger sequences are okay */
1918 return 1;
1919 } else {
1920 /* seq is equal or less than lastseq. */
1921 diff = replay->lastseq - seq;
1922
1923 /* over range to check, i.e. too old or wrapped */
1924 if (diff >= wsizeb)
1925 return 0;
1926
1927 fr = frlast - diff / 8;
1928
1929 /* this packet already seen ? */
1930 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1931 return 0;
1932
1933 /* out of order but good */
1934 return 1;
1935 }
1936 }
1937
1938 /*
1939 * check replay counter whether to update or not.
1940 * OUT: 0: OK
1941 * 1: NG
1942 */
1943 int
1944 ipsec_updatereplay(u_int32_t seq, const struct secasvar *sav)
1945 {
1946 struct secreplay *replay;
1947 u_int32_t diff;
1948 int fr;
1949 u_int32_t wsizeb; /* constant: bits of window size */
1950 int frlast; /* constant: last frame */
1951
1952 IPSEC_SPLASSERT_SOFTNET(__func__);
1953
1954 KASSERT(sav != NULL);
1955 KASSERT(sav->replay != NULL);
1956
1957 replay = sav->replay;
1958
1959 if (replay->wsize == 0)
1960 goto ok; /* no need to check replay. */
1961
1962 /* constant */
1963 frlast = replay->wsize - 1;
1964 wsizeb = replay->wsize << 3;
1965
1966 /* sequence number of 0 is invalid */
1967 if (seq == 0)
1968 return 1;
1969
1970 /* first time */
1971 if (replay->count == 0) {
1972 replay->lastseq = seq;
1973 memset(replay->bitmap, 0, replay->wsize);
1974 (replay->bitmap)[frlast] = 1;
1975 goto ok;
1976 }
1977
1978 if (seq > replay->lastseq) {
1979 /* seq is larger than lastseq. */
1980 diff = seq - replay->lastseq;
1981
1982 /* new larger sequence number */
1983 if (diff < wsizeb) {
1984 /* In window */
1985 /* set bit for this packet */
1986 vshiftl(replay->bitmap, diff, replay->wsize);
1987 (replay->bitmap)[frlast] |= 1;
1988 } else {
1989 /* this packet has a "way larger" */
1990 memset(replay->bitmap, 0, replay->wsize);
1991 (replay->bitmap)[frlast] = 1;
1992 }
1993 replay->lastseq = seq;
1994
1995 /* larger is good */
1996 } else {
1997 /* seq is equal or less than lastseq. */
1998 diff = replay->lastseq - seq;
1999
2000 /* over range to check, i.e. too old or wrapped */
2001 if (diff >= wsizeb)
2002 return 1;
2003
2004 fr = frlast - diff / 8;
2005
2006 /* this packet already seen ? */
2007 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
2008 return 1;
2009
2010 /* mark as seen */
2011 (replay->bitmap)[fr] |= (1 << (diff % 8));
2012
2013 /* out of order but good */
2014 }
2015
2016 ok:
2017 if (replay->count == ~0) {
2018 char buf[IPSEC_LOGSASTRLEN];
2019
2020 /* set overflow flag */
2021 replay->overflow++;
2022
2023 /* don't increment, no more packets accepted */
2024 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
2025 return 1;
2026
2027 IPSECLOG(LOG_WARNING, "replay counter made %d cycle. %s\n",
2028 replay->overflow, ipsec_logsastr(sav, buf, sizeof(buf)));
2029 }
2030
2031 replay->count++;
2032
2033 return 0;
2034 }
2035
2036 /*
2037 * shift variable length buffer to left.
2038 * IN: bitmap: pointer to the buffer
2039 * nbit: the number of to shift.
2040 * wsize: buffer size (bytes).
2041 */
2042 static void
2043 vshiftl(unsigned char *bitmap, int nbit, int wsize)
2044 {
2045 int s, j, i;
2046 unsigned char over;
2047
2048 for (j = 0; j < nbit; j += 8) {
2049 s = (nbit - j < 8) ? (nbit - j): 8;
2050 bitmap[0] <<= s;
2051 for (i = 1; i < wsize; i++) {
2052 over = (bitmap[i] >> (8 - s));
2053 bitmap[i] <<= s;
2054 bitmap[i-1] |= over;
2055 }
2056 }
2057
2058 return;
2059 }
2060
2061 /* Return a printable string for the address. */
2062 const char *
2063 ipsec_address(const union sockaddr_union *sa, char *buf, size_t size)
2064 {
2065 switch (sa->sa.sa_family) {
2066 #if INET
2067 case AF_INET:
2068 in_print(buf, size, &sa->sin.sin_addr);
2069 return buf;
2070 #endif
2071 #if INET6
2072 case AF_INET6:
2073 in6_print(buf, size, &sa->sin6.sin6_addr);
2074 return buf;
2075 #endif
2076 default:
2077 return "(unknown address family)";
2078 }
2079 }
2080
2081 const char *
2082 ipsec_logsastr(const struct secasvar *sav, char *buf, size_t size)
2083 {
2084 const struct secasindex *saidx = &sav->sah->saidx;
2085 char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN];
2086
2087 KASSERTMSG(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
2088 "af family mismatch, src %u, dst %u",
2089 saidx->src.sa.sa_family, saidx->dst.sa.sa_family);
2090
2091 snprintf(buf, size, "SA(SPI=%u src=%s dst=%s)",
2092 (u_int32_t)ntohl(sav->spi),
2093 ipsec_address(&saidx->src, sbuf, sizeof(sbuf)),
2094 ipsec_address(&saidx->dst, dbuf, sizeof(dbuf)));
2095
2096 return buf;
2097 }
2098
2099 void
2100 ipsec_dumpmbuf(struct mbuf *m)
2101 {
2102 int totlen;
2103 int i;
2104 u_char *p;
2105
2106 totlen = 0;
2107 printf("---\n");
2108 while (m) {
2109 p = mtod(m, u_char *);
2110 for (i = 0; i < m->m_len; i++) {
2111 printf("%02x ", p[i]);
2112 totlen++;
2113 if (totlen % 16 == 0)
2114 printf("\n");
2115 }
2116 m = m->m_next;
2117 }
2118 if (totlen % 16 != 0)
2119 printf("\n");
2120 printf("---\n");
2121 }
2122
2123 #ifdef INET6
2124 struct secpolicy *
2125 ipsec6_check_policy(struct mbuf *m, struct in6pcb *in6p, int flags,
2126 int *needipsecp, int *errorp)
2127 {
2128 struct secpolicy *sp = NULL;
2129 int s;
2130 int error = 0;
2131 int needipsec = 0;
2132
2133 if (!ipsec_outdone(m)) {
2134 s = splsoftnet();
2135 if (in6p != NULL &&
2136 ipsec_pcb_skip_ipsec(in6p->in6p_sp, IPSEC_DIR_OUTBOUND)) {
2137 splx(s);
2138 goto skippolicycheck;
2139 }
2140 sp = ipsec_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error,
2141 in6p);
2142
2143 /*
2144 * There are four return cases:
2145 * sp != NULL apply IPsec policy
2146 * sp == NULL, error == 0 no IPsec handling needed
2147 * sp == NULL, error == -EINVAL discard packet w/o error
2148 * sp == NULL, error != 0 discard packet, report error
2149 */
2150
2151 splx(s);
2152 if (sp == NULL) {
2153 /*
2154 * Caller must check the error return to see if it needs to discard
2155 * the packet.
2156 */
2157 needipsec = 0;
2158 } else {
2159 needipsec = 1;
2160 }
2161 }
2162 skippolicycheck:;
2163
2164 *errorp = error;
2165 *needipsecp = needipsec;
2166 return sp;
2167 }
2168
2169 int
2170 ipsec6_input(struct mbuf *m)
2171 {
2172 struct secpolicy *sp;
2173 int s, error;
2174
2175 s = splsoftnet();
2176 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
2177 if (sp != NULL) {
2178 /*
2179 * Check security policy against packet
2180 * attributes.
2181 */
2182 error = ipsec_sp_reject(sp, m);
2183 KEY_SP_UNREF(&sp);
2184 } else {
2185 /* XXX error stat??? */
2186 error = EINVAL;
2187 IPSECLOG(LOG_DEBUG, "no SP, packet discarded\n");/*XXX*/
2188 }
2189 splx(s);
2190
2191 return error;
2192 }
2193 #endif /* INET6 */
2194
2195 /*
2196 * -----------------------------------------------------------------------------
2197 */
2198
2199 /* XXX this stuff doesn't belong here... */
2200
2201 static struct xformsw *xforms = NULL;
2202
2203 /*
2204 * Register a transform; typically at system startup.
2205 */
2206 void
2207 xform_register(struct xformsw *xsp)
2208 {
2209 xsp->xf_next = xforms;
2210 xforms = xsp;
2211 }
2212
2213 /*
2214 * Initialize transform support in an sav.
2215 */
2216 int
2217 xform_init(struct secasvar *sav, int xftype)
2218 {
2219 struct xformsw *xsp;
2220
2221 if (sav->tdb_xform != NULL) /* previously initialized */
2222 return 0;
2223 for (xsp = xforms; xsp; xsp = xsp->xf_next)
2224 if (xsp->xf_type == xftype)
2225 return (*xsp->xf_init)(sav, xsp);
2226
2227 IPSECLOG(LOG_DEBUG, "no match for xform type %d\n", xftype);
2228 return EINVAL;
2229 }
2230
2231 void
2232 nat_t_ports_get(struct mbuf *m, u_int16_t *dport, u_int16_t *sport)
2233 {
2234 struct m_tag *tag;
2235
2236 if ((tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL))) {
2237 *sport = ((u_int16_t *)(tag + 1))[0];
2238 *dport = ((u_int16_t *)(tag + 1))[1];
2239 } else
2240 *sport = *dport = 0;
2241 }
2242
2243 /*
2244 * XXXJRT This should be done as a protosw init call.
2245 */
2246 void
2247 ipsec_attach(void)
2248 {
2249
2250 ipsec_output_init();
2251
2252 ipsecstat_percpu = percpu_alloc(sizeof(uint64_t) * IPSEC_NSTATS);
2253
2254 sysctl_net_inet_ipsec_setup(NULL);
2255 #ifdef INET6
2256 sysctl_net_inet6_ipsec6_setup(NULL);
2257 #endif
2258
2259 ah_attach();
2260 esp_attach();
2261 ipcomp_attach();
2262 ipe4_attach();
2263 #ifdef TCP_SIGNATURE
2264 tcpsignature_attach();
2265 #endif
2266 }
2267