1 /* $FreeBSD$ */ 2 /* $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */ 3 4 /*- 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * IPsec controller part. 35 */ 36 37 #include "opt_inet.h" 38 #include "opt_inet6.h" 39 #include "opt_ipsec.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/malloc.h> 44 #include <sys/mbuf.h> 45 #include <sys/domain.h> 46 #include <sys/priv.h> 47 #include <sys/protosw.h> 48 #include <sys/socket.h> 49 #include <sys/socketvar.h> 50 #include <sys/errno.h> 51 #include <sys/time.h> 52 #include <sys/kernel.h> 53 #include <sys/syslog.h> 54 #include <sys/sysctl.h> 55 #include <sys/proc.h> 56 #include <sys/vimage.h> 57 58 #include <net/if.h> 59 #include <net/route.h> 60 61 #include <netinet/in.h> 62 #include <netinet/in_systm.h> 63 #include <netinet/ip.h> 64 #include <netinet/ip_var.h> 65 #include <netinet/in_var.h> 66 #include <netinet/udp.h> 67 #include <netinet/udp_var.h> 68 #include <netinet/tcp.h> 69 #include <netinet/udp.h> 70 71 #include <netinet/ip6.h> 72 #ifdef INET6 73 #include <netinet6/ip6_var.h> 74 #endif 75 #include <netinet/in_pcb.h> 76 #ifdef INET6 77 #include <netinet/icmp6.h> 78 #endif 79 80 #include <sys/types.h> 81 #include <netipsec/ipsec.h> 82 #ifdef INET6 83 #include <netipsec/ipsec6.h> 84 #endif 85 #include <netipsec/ah_var.h> 86 #include <netipsec/esp_var.h> 87 #include <netipsec/ipcomp.h> /*XXX*/ 88 #include <netipsec/ipcomp_var.h> 89 90 #include <netipsec/key.h> 91 #include <netipsec/keydb.h> 92 #include <netipsec/key_debug.h> 93 94 #include <netipsec/xform.h> 95 96 #include <machine/in_cksum.h> 97 98 #include <opencrypto/cryptodev.h> 99 100 #ifndef VIMAGE 101 #ifndef VIMAGE_GLOBALS 102 struct vnet_ipsec vnet_ipsec_0; 103 #endif 104 #endif 105 106 static int ipsec_iattach(const void *); 107 108 #ifdef VIMAGE_GLOBALS 109 /* NB: name changed so netstat doesn't use it. */ 110 struct ipsecstat ipsec4stat; 111 struct secpolicy ip4_def_policy; 112 int ipsec_debug; 113 int ip4_ah_offsetmask; 114 int ip4_ipsec_dfbit; 115 int ip4_esp_trans_deflev; 116 int ip4_esp_net_deflev; 117 int ip4_ah_trans_deflev; 118 int ip4_ah_net_deflev; 119 int ip4_ipsec_ecn; 120 int ip4_esp_randpad; 121 /* 122 * Crypto support requirements: 123 * 124 * 1 require hardware support 125 * -1 require software support 126 * 0 take anything 127 */ 128 int crypto_support; 129 #endif /* VIMAGE_GLOBALS */ 130 131 SYSCTL_DECL(_net_inet_ipsec); 132 133 /* net.inet.ipsec */ 134 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_DEF_POLICY, 135 def_policy, CTLFLAG_RW, ip4_def_policy.policy, 0, 136 "IPsec default policy."); 137 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, 138 esp_trans_deflev, CTLFLAG_RW, ip4_esp_trans_deflev, 0, 139 "Default ESP transport mode level"); 140 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, 141 esp_net_deflev, CTLFLAG_RW, ip4_esp_net_deflev, 0, 142 "Default ESP tunnel mode level."); 143 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, 144 ah_trans_deflev, CTLFLAG_RW, ip4_ah_trans_deflev, 0, 145 "AH transfer mode default level."); 146 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, 147 ah_net_deflev, CTLFLAG_RW, ip4_ah_net_deflev, 0, 148 "AH tunnel mode default level."); 149 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_AH_CLEARTOS, 150 ah_cleartos, CTLFLAG_RW, ah_cleartos, 0, 151 "If set clear type-of-service field when doing AH computation."); 152 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_AH_OFFSETMASK, 153 ah_offsetmask, CTLFLAG_RW, ip4_ah_offsetmask, 0, 154 "If not set clear offset field mask when doing AH computation."); 155 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_DFBIT, 156 dfbit, CTLFLAG_RW, ip4_ipsec_dfbit, 0, 157 "Do not fragment bit on encap."); 158 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_ECN, 159 ecn, CTLFLAG_RW, ip4_ipsec_ecn, 0, 160 "Explicit Congestion Notification handling."); 161 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, IPSECCTL_DEBUG, 162 debug, CTLFLAG_RW, ipsec_debug, 0, 163 "Enable IPsec debugging output when set."); 164 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, OID_AUTO, 165 crypto_support, CTLFLAG_RW, crypto_support,0, 166 "Crypto driver selection."); 167 SYSCTL_V_STRUCT(V_NET, vnet_ipsec, _net_inet_ipsec, OID_AUTO, 168 ipsecstats, CTLFLAG_RD, ipsec4stat, ipsecstat, 169 "IPsec IPv4 statistics."); 170 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ipsec, OID_AUTO, 171 filtertunnel, CTLFLAG_RW, ip4_ipsec_filtertunnel, 0, 172 "If set filter packets from an IPsec tunnel."); 173 174 #ifdef REGRESSION 175 #ifdef VIMAGE_GLOBALS 176 int ipsec_replay; 177 int ipsec_integrity; 178 #endif 179 /* 180 * When set to 1, IPsec will send packets with the same sequence number. 181 * This allows to verify if the other side has proper replay attacks detection. 182 */ 183 SYSCTL_V_INT(V_NET, vnet_ipsec,_net_inet_ipsec, OID_AUTO, test_replay, 184 CTLFLAG_RW, ipsec_replay, 0, "Emulate replay attack"); 185 /* 186 * When set 1, IPsec will send packets with corrupted HMAC. 187 * This allows to verify if the other side properly detects modified packets. 188 */ 189 SYSCTL_V_INT(V_NET, vnet_ipsec,_net_inet_ipsec, OID_AUTO, test_integrity, 190 CTLFLAG_RW, ipsec_integrity, 0, "Emulate man-in-the-middle attack"); 191 #endif 192 193 #ifdef INET6 194 #ifdef VIMAGE_GLOBALS 195 struct ipsecstat ipsec6stat; 196 int ip6_esp_trans_deflev; 197 int ip6_esp_net_deflev; 198 int ip6_ah_trans_deflev; 199 int ip6_ah_net_deflev; 200 int ip6_ipsec_ecn; 201 #endif 202 203 SYSCTL_DECL(_net_inet6_ipsec6); 204 205 /* net.inet6.ipsec6 */ 206 #ifdef COMPAT_KAME 207 SYSCTL_OID(_net_inet6_ipsec6, IPSECCTL_STATS, stats, CTLFLAG_RD, 208 0, 0, compat_ipsecstats_sysctl, "S", "IPsec IPv6 statistics."); 209 #endif /* COMPAT_KAME */ 210 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_DEF_POLICY, 211 def_policy, CTLFLAG_RW, ip4_def_policy.policy, 0, 212 "IPsec default policy."); 213 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, 214 esp_trans_deflev, CTLFLAG_RW, ip6_esp_trans_deflev, 0, 215 "Default ESP transport mode level."); 216 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, 217 esp_net_deflev, CTLFLAG_RW, ip6_esp_net_deflev, 0, 218 "Default ESP tunnel mode level."); 219 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, 220 ah_trans_deflev, CTLFLAG_RW, ip6_ah_trans_deflev, 0, 221 "AH transfer mode default level."); 222 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, 223 ah_net_deflev, CTLFLAG_RW, ip6_ah_net_deflev, 0, 224 "AH tunnel mode default level."); 225 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_ECN, 226 ecn, CTLFLAG_RW, ip6_ipsec_ecn, 0, 227 "Explicit Congestion Notification handling."); 228 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_DEBUG, 229 debug, CTLFLAG_RW, ipsec_debug, 0, 230 "Enable IPsec debugging output when set."); 231 SYSCTL_V_STRUCT(V_NET, vnet_ipsec, _net_inet6_ipsec6, IPSECCTL_STATS, 232 ipsecstats, CTLFLAG_RD, ipsec6stat, ipsecstat, 233 "IPsec IPv6 statistics."); 234 SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet6_ipsec6, OID_AUTO, 235 filtertunnel, CTLFLAG_RW, ip6_ipsec6_filtertunnel, 0, 236 "If set filter packets from an IPsec tunnel."); 237 #endif /* INET6 */ 238 239 static int ipsec_setspidx_inpcb __P((struct mbuf *, struct inpcb *)); 240 static int ipsec_setspidx __P((struct mbuf *, struct secpolicyindex *, int)); 241 static void ipsec4_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int)); 242 static int ipsec4_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *)); 243 #ifdef INET6 244 static void ipsec6_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int)); 245 static int ipsec6_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *)); 246 #endif 247 static void ipsec_delpcbpolicy __P((struct inpcbpolicy *)); 248 static struct secpolicy *ipsec_deepcopy_policy __P((struct secpolicy *src)); 249 static void vshiftl __P((unsigned char *, int, int)); 250 251 MALLOC_DEFINE(M_IPSEC_INPCB, "inpcbpolicy", "inpcb-resident ipsec policy"); 252 253 #ifndef VIMAGE_GLOBALS 254 static const vnet_modinfo_t vnet_ipsec_modinfo = { 255 .vmi_id = VNET_MOD_IPSEC, 256 .vmi_name = "ipsec", 257 .vmi_size = sizeof(struct vnet_ipsec), 258 .vmi_dependson = VNET_MOD_INET, /* XXX revisit - INET6 ? */ 259 .vmi_iattach = ipsec_iattach 260 }; 261 #endif /* !VIMAGE_GLOBALS */ 262 263 void 264 ipsec_init(void) 265 { 266 INIT_VNET_IPSEC(curvnet); 267 268 #ifdef IPSEC_DEBUG 269 V_ipsec_debug = 1; 270 #else 271 V_ipsec_debug = 0; 272 #endif 273 274 V_ip4_ah_offsetmask = 0; /* maybe IP_DF? */ 275 V_ip4_ipsec_dfbit = 0; /* DF bit on encap. 0: clear 1: set 2: copy */ 276 V_ip4_esp_trans_deflev = IPSEC_LEVEL_USE; 277 V_ip4_esp_net_deflev = IPSEC_LEVEL_USE; 278 V_ip4_ah_trans_deflev = IPSEC_LEVEL_USE; 279 V_ip4_ah_net_deflev = IPSEC_LEVEL_USE; 280 V_ip4_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */ 281 V_ip4_esp_randpad = -1; 282 #ifdef IPSEC_FILTERTUNNEL 283 V_ip4_ipsec_filtertunnel = 1; 284 #else 285 V_ip4_ipsec_filtertunnel = 0; 286 #endif 287 288 V_crypto_support = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE; 289 290 #ifdef REGRESSION 291 V_ipsec_replay = 0; 292 V_ipsec_integrity = 0; 293 #endif 294 295 #ifdef INET6 296 V_ip6_esp_trans_deflev = IPSEC_LEVEL_USE; 297 V_ip6_esp_net_deflev = IPSEC_LEVEL_USE; 298 V_ip6_ah_trans_deflev = IPSEC_LEVEL_USE; 299 V_ip6_ah_net_deflev = IPSEC_LEVEL_USE; 300 V_ip6_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */ 301 #ifdef IPSEC_FILTERTUNNEL 302 V_ip6_ipsec6_filtertunnel = 1; 303 #else 304 V_ip6_ipsec6_filtertunnel = 0; 305 #endif 306 #endif 307 } 308 309 /* 310 * Return a held reference to the default SP. 311 */ 312 static struct secpolicy * 313 key_allocsp_default(const char* where, int tag) 314 { 315 INIT_VNET_IPSEC(curvnet); 316 struct secpolicy *sp; 317 318 KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 319 printf("DP key_allocsp_default from %s:%u\n", where, tag)); 320 321 sp = &V_ip4_def_policy; 322 if (sp->policy != IPSEC_POLICY_DISCARD && 323 sp->policy != IPSEC_POLICY_NONE) { 324 ipseclog((LOG_INFO, "fixed system default policy: %d->%d\n", 325 sp->policy, IPSEC_POLICY_NONE)); 326 sp->policy = IPSEC_POLICY_NONE; 327 } 328 key_addref(sp); 329 330 KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 331 printf("DP key_allocsp_default returns SP:%p (%u)\n", 332 sp, sp->refcnt)); 333 return (sp); 334 } 335 #define KEY_ALLOCSP_DEFAULT() \ 336 key_allocsp_default(__FILE__, __LINE__) 337 338 /* 339 * For OUTBOUND packet having a socket. Searching SPD for packet, 340 * and return a pointer to SP. 341 * OUT: NULL: no apropreate SP found, the following value is set to error. 342 * 0 : bypass 343 * EACCES : discard packet. 344 * ENOENT : ipsec_acquire() in progress, maybe. 345 * others : error occured. 346 * others: a pointer to SP 347 * 348 * NOTE: IPv6 mapped adddress concern is implemented here. 349 */ 350 struct secpolicy * 351 ipsec_getpolicy(struct tdb_ident *tdbi, u_int dir) 352 { 353 struct secpolicy *sp; 354 355 IPSEC_ASSERT(tdbi != NULL, ("null tdbi")); 356 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND, 357 ("invalid direction %u", dir)); 358 359 sp = KEY_ALLOCSP2(tdbi->spi, &tdbi->dst, tdbi->proto, dir); 360 if (sp == NULL) /*XXX????*/ 361 sp = KEY_ALLOCSP_DEFAULT(); 362 IPSEC_ASSERT(sp != NULL, ("null SP")); 363 return (sp); 364 } 365 366 /* 367 * For OUTBOUND packet having a socket. Searching SPD for packet, 368 * and return a pointer to SP. 369 * OUT: NULL: no apropreate SP found, the following value is set to error. 370 * 0 : bypass 371 * EACCES : discard packet. 372 * ENOENT : ipsec_acquire() in progress, maybe. 373 * others : error occured. 374 * others: a pointer to SP 375 * 376 * NOTE: IPv6 mapped adddress concern is implemented here. 377 */ 378 static struct secpolicy * 379 ipsec_getpolicybysock(struct mbuf *m, u_int dir, struct inpcb *inp, int *error) 380 { 381 INIT_VNET_IPSEC(curvnet); 382 struct inpcbpolicy *pcbsp; 383 struct secpolicy *currsp = NULL; /* Policy on socket. */ 384 struct secpolicy *sp; 385 386 IPSEC_ASSERT(m != NULL, ("null mbuf")); 387 IPSEC_ASSERT(inp != NULL, ("null inpcb")); 388 IPSEC_ASSERT(error != NULL, ("null error")); 389 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND, 390 ("invalid direction %u", dir)); 391 392 /* Set spidx in pcb. */ 393 *error = ipsec_setspidx_inpcb(m, inp); 394 if (*error) 395 return (NULL); 396 397 pcbsp = inp->inp_sp; 398 IPSEC_ASSERT(pcbsp != NULL, ("null pcbsp")); 399 switch (dir) { 400 case IPSEC_DIR_INBOUND: 401 currsp = pcbsp->sp_in; 402 break; 403 case IPSEC_DIR_OUTBOUND: 404 currsp = pcbsp->sp_out; 405 break; 406 } 407 IPSEC_ASSERT(currsp != NULL, ("null currsp")); 408 409 if (pcbsp->priv) { /* When privilieged socket. */ 410 switch (currsp->policy) { 411 case IPSEC_POLICY_BYPASS: 412 case IPSEC_POLICY_IPSEC: 413 key_addref(currsp); 414 sp = currsp; 415 break; 416 417 case IPSEC_POLICY_ENTRUST: 418 /* Look for a policy in SPD. */ 419 sp = KEY_ALLOCSP(&currsp->spidx, dir); 420 if (sp == NULL) /* No SP found. */ 421 sp = KEY_ALLOCSP_DEFAULT(); 422 break; 423 424 default: 425 ipseclog((LOG_ERR, "%s: Invalid policy for PCB %d\n", 426 __func__, currsp->policy)); 427 *error = EINVAL; 428 return (NULL); 429 } 430 } else { /* Unpriv, SPD has policy. */ 431 sp = KEY_ALLOCSP(&currsp->spidx, dir); 432 if (sp == NULL) { /* No SP found. */ 433 switch (currsp->policy) { 434 case IPSEC_POLICY_BYPASS: 435 ipseclog((LOG_ERR, "%s: Illegal policy for " 436 "non-priviliged defined %d\n", 437 __func__, currsp->policy)); 438 *error = EINVAL; 439 return (NULL); 440 441 case IPSEC_POLICY_ENTRUST: 442 sp = KEY_ALLOCSP_DEFAULT(); 443 break; 444 445 case IPSEC_POLICY_IPSEC: 446 key_addref(currsp); 447 sp = currsp; 448 break; 449 450 default: 451 ipseclog((LOG_ERR, "%s: Invalid policy for " 452 "PCB %d\n", __func__, currsp->policy)); 453 *error = EINVAL; 454 return (NULL); 455 } 456 } 457 } 458 IPSEC_ASSERT(sp != NULL, 459 ("null SP (priv %u policy %u", pcbsp->priv, currsp->policy)); 460 KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 461 printf("DP %s (priv %u policy %u) allocate SP:%p (refcnt %u)\n", 462 __func__, pcbsp->priv, currsp->policy, sp, sp->refcnt)); 463 return (sp); 464 } 465 466 /* 467 * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet, 468 * and return a pointer to SP. 469 * OUT: positive: a pointer to the entry for security policy leaf matched. 470 * NULL: no apropreate SP found, the following value is set to error. 471 * 0 : bypass 472 * EACCES : discard packet. 473 * ENOENT : ipsec_acquire() in progress, maybe. 474 * others : error occured. 475 */ 476 struct secpolicy * 477 ipsec_getpolicybyaddr(struct mbuf *m, u_int dir, int flag, int *error) 478 { 479 INIT_VNET_IPSEC(curvnet); 480 struct secpolicyindex spidx; 481 struct secpolicy *sp; 482 483 IPSEC_ASSERT(m != NULL, ("null mbuf")); 484 IPSEC_ASSERT(error != NULL, ("null error")); 485 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND, 486 ("invalid direction %u", dir)); 487 488 sp = NULL; 489 if (key_havesp(dir)) { 490 /* Make an index to look for a policy. */ 491 *error = ipsec_setspidx(m, &spidx, 492 (flag & IP_FORWARDING) ? 0 : 1); 493 if (*error != 0) { 494 DPRINTF(("%s: setpidx failed, dir %u flag %u\n", 495 __func__, dir, flag)); 496 return (NULL); 497 } 498 spidx.dir = dir; 499 500 sp = KEY_ALLOCSP(&spidx, dir); 501 } 502 if (sp == NULL) /* No SP found, use system default. */ 503 sp = KEY_ALLOCSP_DEFAULT(); 504 IPSEC_ASSERT(sp != NULL, ("null SP")); 505 return (sp); 506 } 507 508 struct secpolicy * 509 ipsec4_checkpolicy(struct mbuf *m, u_int dir, u_int flag, int *error, 510 struct inpcb *inp) 511 { 512 INIT_VNET_IPSEC(curvnet); 513 struct secpolicy *sp; 514 515 *error = 0; 516 if (inp == NULL) 517 sp = ipsec_getpolicybyaddr(m, dir, flag, error); 518 else 519 sp = ipsec_getpolicybysock(m, dir, inp, error); 520 if (sp == NULL) { 521 IPSEC_ASSERT(*error != 0, ("getpolicy failed w/o error")); 522 V_ipsec4stat.ips_out_inval++; 523 return (NULL); 524 } 525 IPSEC_ASSERT(*error == 0, ("sp w/ error set to %u", *error)); 526 switch (sp->policy) { 527 case IPSEC_POLICY_ENTRUST: 528 default: 529 printf("%s: invalid policy %u\n", __func__, sp->policy); 530 /* FALLTHROUGH */ 531 case IPSEC_POLICY_DISCARD: 532 V_ipsec4stat.ips_out_polvio++; 533 *error = -EINVAL; /* Packet is discarded by caller. */ 534 break; 535 case IPSEC_POLICY_BYPASS: 536 case IPSEC_POLICY_NONE: 537 KEY_FREESP(&sp); 538 sp = NULL; /* NB: force NULL result. */ 539 break; 540 case IPSEC_POLICY_IPSEC: 541 if (sp->req == NULL) /* Acquire a SA. */ 542 *error = key_spdacquire(sp); 543 break; 544 } 545 if (*error != 0) { 546 KEY_FREESP(&sp); 547 sp = NULL; 548 } 549 return (sp); 550 } 551 552 static int 553 ipsec_setspidx_inpcb(struct mbuf *m, struct inpcb *inp) 554 { 555 int error; 556 557 IPSEC_ASSERT(inp != NULL, ("null inp")); 558 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp")); 559 IPSEC_ASSERT(inp->inp_sp->sp_out != NULL && inp->inp_sp->sp_in != NULL, 560 ("null sp_in || sp_out")); 561 562 error = ipsec_setspidx(m, &inp->inp_sp->sp_in->spidx, 1); 563 if (error == 0) { 564 inp->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND; 565 inp->inp_sp->sp_out->spidx = inp->inp_sp->sp_in->spidx; 566 inp->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND; 567 } else { 568 bzero(&inp->inp_sp->sp_in->spidx, 569 sizeof (inp->inp_sp->sp_in->spidx)); 570 bzero(&inp->inp_sp->sp_out->spidx, 571 sizeof (inp->inp_sp->sp_in->spidx)); 572 } 573 return (error); 574 } 575 576 /* 577 * Configure security policy index (src/dst/proto/sport/dport) 578 * by looking at the content of mbuf. 579 * The caller is responsible for error recovery (like clearing up spidx). 580 */ 581 static int 582 ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int needport) 583 { 584 INIT_VNET_IPSEC(curvnet); 585 struct ip *ip = NULL; 586 struct ip ipbuf; 587 u_int v; 588 struct mbuf *n; 589 int len; 590 int error; 591 592 IPSEC_ASSERT(m != NULL, ("null mbuf")); 593 594 /* 595 * Validate m->m_pkthdr.len. We see incorrect length if we 596 * mistakenly call this function with inconsistent mbuf chain 597 * (like 4.4BSD tcp/udp processing). XXX Should we panic here? 598 */ 599 len = 0; 600 for (n = m; n; n = n->m_next) 601 len += n->m_len; 602 if (m->m_pkthdr.len != len) { 603 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 604 printf("%s: pkthdr len(%d) mismatch (%d), ignored.\n", 605 __func__, len, m->m_pkthdr.len)); 606 return (EINVAL); 607 } 608 609 if (m->m_pkthdr.len < sizeof(struct ip)) { 610 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 611 printf("%s: pkthdr len(%d) too small (v4), ignored.\n", 612 __func__, m->m_pkthdr.len)); 613 return (EINVAL); 614 } 615 616 if (m->m_len >= sizeof(*ip)) 617 ip = mtod(m, struct ip *); 618 else { 619 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf); 620 ip = &ipbuf; 621 } 622 #ifdef _IP_VHL 623 v = _IP_VHL_V(ip->ip_vhl); 624 #else 625 v = ip->ip_v; 626 #endif 627 switch (v) { 628 case 4: 629 error = ipsec4_setspidx_ipaddr(m, spidx); 630 if (error) 631 return (error); 632 ipsec4_get_ulp(m, spidx, needport); 633 return (0); 634 #ifdef INET6 635 case 6: 636 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) { 637 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 638 printf("%s: pkthdr len(%d) too small (v6), " 639 "ignored\n", __func__, m->m_pkthdr.len)); 640 return (EINVAL); 641 } 642 error = ipsec6_setspidx_ipaddr(m, spidx); 643 if (error) 644 return (error); 645 ipsec6_get_ulp(m, spidx, needport); 646 return (0); 647 #endif 648 default: 649 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 650 printf("%s: " "unknown IP version %u, ignored.\n", 651 __func__, v)); 652 return (EINVAL); 653 } 654 } 655 656 static void 657 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport) 658 { 659 u_int8_t nxt; 660 int off; 661 662 /* Sanity check. */ 663 IPSEC_ASSERT(m != NULL, ("null mbuf")); 664 IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),("packet too short")); 665 666 /* NB: ip_input() flips it into host endian. XXX Need more checking. */ 667 if (m->m_len < sizeof (struct ip)) { 668 struct ip *ip = mtod(m, struct ip *); 669 if (ip->ip_off & (IP_MF | IP_OFFMASK)) 670 goto done; 671 #ifdef _IP_VHL 672 off = _IP_VHL_HL(ip->ip_vhl) << 2; 673 #else 674 off = ip->ip_hl << 2; 675 #endif 676 nxt = ip->ip_p; 677 } else { 678 struct ip ih; 679 680 m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih); 681 if (ih.ip_off & (IP_MF | IP_OFFMASK)) 682 goto done; 683 #ifdef _IP_VHL 684 off = _IP_VHL_HL(ih.ip_vhl) << 2; 685 #else 686 off = ih.ip_hl << 2; 687 #endif 688 nxt = ih.ip_p; 689 } 690 691 while (off < m->m_pkthdr.len) { 692 struct ip6_ext ip6e; 693 struct tcphdr th; 694 struct udphdr uh; 695 696 switch (nxt) { 697 case IPPROTO_TCP: 698 spidx->ul_proto = nxt; 699 if (!needport) 700 goto done_proto; 701 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len) 702 goto done; 703 m_copydata(m, off, sizeof (th), (caddr_t) &th); 704 spidx->src.sin.sin_port = th.th_sport; 705 spidx->dst.sin.sin_port = th.th_dport; 706 return; 707 case IPPROTO_UDP: 708 spidx->ul_proto = nxt; 709 if (!needport) 710 goto done_proto; 711 if (off + sizeof(struct udphdr) > m->m_pkthdr.len) 712 goto done; 713 m_copydata(m, off, sizeof (uh), (caddr_t) &uh); 714 spidx->src.sin.sin_port = uh.uh_sport; 715 spidx->dst.sin.sin_port = uh.uh_dport; 716 return; 717 case IPPROTO_AH: 718 if (off + sizeof(ip6e) > m->m_pkthdr.len) 719 goto done; 720 /* XXX Sigh, this works but is totally bogus. */ 721 m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e); 722 off += (ip6e.ip6e_len + 2) << 2; 723 nxt = ip6e.ip6e_nxt; 724 break; 725 case IPPROTO_ICMP: 726 default: 727 /* XXX Intermediate headers??? */ 728 spidx->ul_proto = nxt; 729 goto done_proto; 730 } 731 } 732 done: 733 spidx->ul_proto = IPSEC_ULPROTO_ANY; 734 done_proto: 735 spidx->src.sin.sin_port = IPSEC_PORT_ANY; 736 spidx->dst.sin.sin_port = IPSEC_PORT_ANY; 737 } 738 739 /* Assumes that m is sane. */ 740 static int 741 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx) 742 { 743 static const struct sockaddr_in template = { 744 sizeof (struct sockaddr_in), 745 AF_INET, 746 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 } 747 }; 748 749 spidx->src.sin = template; 750 spidx->dst.sin = template; 751 752 if (m->m_len < sizeof (struct ip)) { 753 m_copydata(m, offsetof(struct ip, ip_src), 754 sizeof (struct in_addr), 755 (caddr_t) &spidx->src.sin.sin_addr); 756 m_copydata(m, offsetof(struct ip, ip_dst), 757 sizeof (struct in_addr), 758 (caddr_t) &spidx->dst.sin.sin_addr); 759 } else { 760 struct ip *ip = mtod(m, struct ip *); 761 spidx->src.sin.sin_addr = ip->ip_src; 762 spidx->dst.sin.sin_addr = ip->ip_dst; 763 } 764 765 spidx->prefs = sizeof(struct in_addr) << 3; 766 spidx->prefd = sizeof(struct in_addr) << 3; 767 768 return (0); 769 } 770 771 #ifdef INET6 772 static void 773 ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport) 774 { 775 INIT_VNET_IPSEC(curvnet); 776 int off, nxt; 777 struct tcphdr th; 778 struct udphdr uh; 779 struct icmp6_hdr ih; 780 781 /* Sanity check. */ 782 if (m == NULL) 783 panic("%s: NULL pointer was passed.\n", __func__); 784 785 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 786 printf("%s:\n", __func__); kdebug_mbuf(m)); 787 788 /* Set default. */ 789 spidx->ul_proto = IPSEC_ULPROTO_ANY; 790 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY; 791 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY; 792 793 nxt = -1; 794 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 795 if (off < 0 || m->m_pkthdr.len < off) 796 return; 797 798 switch (nxt) { 799 case IPPROTO_TCP: 800 spidx->ul_proto = nxt; 801 if (!needport) 802 break; 803 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len) 804 break; 805 m_copydata(m, off, sizeof(th), (caddr_t)&th); 806 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport; 807 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport; 808 break; 809 case IPPROTO_UDP: 810 spidx->ul_proto = nxt; 811 if (!needport) 812 break; 813 if (off + sizeof(struct udphdr) > m->m_pkthdr.len) 814 break; 815 m_copydata(m, off, sizeof(uh), (caddr_t)&uh); 816 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport; 817 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport; 818 break; 819 case IPPROTO_ICMPV6: 820 spidx->ul_proto = nxt; 821 if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len) 822 break; 823 m_copydata(m, off, sizeof(ih), (caddr_t)&ih); 824 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = 825 htons((uint16_t)ih.icmp6_type); 826 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = 827 htons((uint16_t)ih.icmp6_code); 828 break; 829 default: 830 /* XXX Intermediate headers??? */ 831 spidx->ul_proto = nxt; 832 break; 833 } 834 } 835 836 /* Assumes that m is sane. */ 837 static int 838 ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx) 839 { 840 struct ip6_hdr *ip6 = NULL; 841 struct ip6_hdr ip6buf; 842 struct sockaddr_in6 *sin6; 843 844 if (m->m_len >= sizeof(*ip6)) 845 ip6 = mtod(m, struct ip6_hdr *); 846 else { 847 m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf); 848 ip6 = &ip6buf; 849 } 850 851 sin6 = (struct sockaddr_in6 *)&spidx->src; 852 bzero(sin6, sizeof(*sin6)); 853 sin6->sin6_family = AF_INET6; 854 sin6->sin6_len = sizeof(struct sockaddr_in6); 855 bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src)); 856 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) { 857 sin6->sin6_addr.s6_addr16[1] = 0; 858 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]); 859 } 860 spidx->prefs = sizeof(struct in6_addr) << 3; 861 862 sin6 = (struct sockaddr_in6 *)&spidx->dst; 863 bzero(sin6, sizeof(*sin6)); 864 sin6->sin6_family = AF_INET6; 865 sin6->sin6_len = sizeof(struct sockaddr_in6); 866 bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst)); 867 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { 868 sin6->sin6_addr.s6_addr16[1] = 0; 869 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]); 870 } 871 spidx->prefd = sizeof(struct in6_addr) << 3; 872 873 return (0); 874 } 875 #endif 876 877 static void 878 ipsec_delpcbpolicy(struct inpcbpolicy *p) 879 { 880 881 free(p, M_IPSEC_INPCB); 882 } 883 884 /* Initialize policy in PCB. */ 885 int 886 ipsec_init_policy(struct socket *so, struct inpcbpolicy **pcb_sp) 887 { 888 INIT_VNET_IPSEC(curvnet); 889 struct inpcbpolicy *new; 890 891 /* Sanity check. */ 892 if (so == NULL || pcb_sp == NULL) 893 panic("%s: NULL pointer was passed.\n", __func__); 894 895 new = (struct inpcbpolicy *) malloc(sizeof(struct inpcbpolicy), 896 M_IPSEC_INPCB, M_NOWAIT|M_ZERO); 897 if (new == NULL) { 898 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 899 return (ENOBUFS); 900 } 901 902 new->priv = IPSEC_IS_PRIVILEGED_SO(so); 903 904 if ((new->sp_in = KEY_NEWSP()) == NULL) { 905 ipsec_delpcbpolicy(new); 906 return (ENOBUFS); 907 } 908 new->sp_in->state = IPSEC_SPSTATE_ALIVE; 909 new->sp_in->policy = IPSEC_POLICY_ENTRUST; 910 911 if ((new->sp_out = KEY_NEWSP()) == NULL) { 912 KEY_FREESP(&new->sp_in); 913 ipsec_delpcbpolicy(new); 914 return (ENOBUFS); 915 } 916 new->sp_out->state = IPSEC_SPSTATE_ALIVE; 917 new->sp_out->policy = IPSEC_POLICY_ENTRUST; 918 919 *pcb_sp = new; 920 921 return (0); 922 } 923 924 /* Copy old IPsec policy into new. */ 925 int 926 ipsec_copy_policy(struct inpcbpolicy *old, struct inpcbpolicy *new) 927 { 928 struct secpolicy *sp; 929 930 sp = ipsec_deepcopy_policy(old->sp_in); 931 if (sp) { 932 KEY_FREESP(&new->sp_in); 933 new->sp_in = sp; 934 } else 935 return (ENOBUFS); 936 937 sp = ipsec_deepcopy_policy(old->sp_out); 938 if (sp) { 939 KEY_FREESP(&new->sp_out); 940 new->sp_out = sp; 941 } else 942 return (ENOBUFS); 943 944 new->priv = old->priv; 945 946 return (0); 947 } 948 949 struct ipsecrequest * 950 ipsec_newisr(void) 951 { 952 struct ipsecrequest *p; 953 954 p = malloc(sizeof(struct ipsecrequest), M_IPSEC_SR, M_NOWAIT|M_ZERO); 955 if (p != NULL) 956 IPSECREQUEST_LOCK_INIT(p); 957 return (p); 958 } 959 960 void 961 ipsec_delisr(struct ipsecrequest *p) 962 { 963 964 IPSECREQUEST_LOCK_DESTROY(p); 965 free(p, M_IPSEC_SR); 966 } 967 968 /* Deep-copy a policy in PCB. */ 969 static struct secpolicy * 970 ipsec_deepcopy_policy(struct secpolicy *src) 971 { 972 struct ipsecrequest *newchain = NULL; 973 struct ipsecrequest *p; 974 struct ipsecrequest **q; 975 struct ipsecrequest *r; 976 struct secpolicy *dst; 977 978 if (src == NULL) 979 return (NULL); 980 dst = KEY_NEWSP(); 981 if (dst == NULL) 982 return (NULL); 983 984 /* 985 * Deep-copy IPsec request chain. This is required since struct 986 * ipsecrequest is not reference counted. 987 */ 988 q = &newchain; 989 for (p = src->req; p; p = p->next) { 990 *q = ipsec_newisr(); 991 if (*q == NULL) 992 goto fail; 993 (*q)->saidx.proto = p->saidx.proto; 994 (*q)->saidx.mode = p->saidx.mode; 995 (*q)->level = p->level; 996 (*q)->saidx.reqid = p->saidx.reqid; 997 998 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src)); 999 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst)); 1000 1001 (*q)->sp = dst; 1002 1003 q = &((*q)->next); 1004 } 1005 1006 dst->req = newchain; 1007 dst->state = src->state; 1008 dst->policy = src->policy; 1009 /* Do not touch the refcnt fields. */ 1010 1011 return (dst); 1012 1013 fail: 1014 for (p = newchain; p; p = r) { 1015 r = p->next; 1016 ipsec_delisr(p); 1017 p = NULL; 1018 } 1019 return (NULL); 1020 } 1021 1022 /* Set policy and IPsec request if present. */ 1023 static int 1024 ipsec_set_policy_internal(struct secpolicy **pcb_sp, int optname, 1025 caddr_t request, size_t len, struct ucred *cred) 1026 { 1027 INIT_VNET_IPSEC(curvnet); 1028 struct sadb_x_policy *xpl; 1029 struct secpolicy *newsp = NULL; 1030 int error; 1031 1032 /* Sanity check. */ 1033 if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL) 1034 return (EINVAL); 1035 if (len < sizeof(*xpl)) 1036 return (EINVAL); 1037 xpl = (struct sadb_x_policy *)request; 1038 1039 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1040 printf("%s: passed policy\n", __func__); 1041 kdebug_sadb_x_policy((struct sadb_ext *)xpl)); 1042 1043 /* Check policy type. */ 1044 /* ipsec_set_policy_internal() accepts IPSEC, ENTRUST and BYPASS. */ 1045 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD 1046 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE) 1047 return (EINVAL); 1048 1049 /* Check privileged socket. */ 1050 if (cred != NULL && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) { 1051 error = priv_check_cred(cred, PRIV_NETINET_IPSEC, 0); 1052 if (error) 1053 return (EACCES); 1054 } 1055 1056 /* Allocating new SP entry. */ 1057 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL) 1058 return (error); 1059 1060 newsp->state = IPSEC_SPSTATE_ALIVE; 1061 1062 /* Clear old SP and set new SP. */ 1063 KEY_FREESP(pcb_sp); 1064 *pcb_sp = newsp; 1065 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1066 printf("%s: new policy\n", __func__); 1067 kdebug_secpolicy(newsp)); 1068 1069 return (0); 1070 } 1071 1072 int 1073 ipsec_set_policy(struct inpcb *inp, int optname, caddr_t request, 1074 size_t len, struct ucred *cred) 1075 { 1076 INIT_VNET_IPSEC(curvnet); 1077 struct sadb_x_policy *xpl; 1078 struct secpolicy **pcb_sp; 1079 1080 /* Sanity check. */ 1081 if (inp == NULL || request == NULL) 1082 return (EINVAL); 1083 if (len < sizeof(*xpl)) 1084 return (EINVAL); 1085 xpl = (struct sadb_x_policy *)request; 1086 1087 /* Select direction. */ 1088 switch (xpl->sadb_x_policy_dir) { 1089 case IPSEC_DIR_INBOUND: 1090 pcb_sp = &inp->inp_sp->sp_in; 1091 break; 1092 case IPSEC_DIR_OUTBOUND: 1093 pcb_sp = &inp->inp_sp->sp_out; 1094 break; 1095 default: 1096 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__, 1097 xpl->sadb_x_policy_dir)); 1098 return (EINVAL); 1099 } 1100 1101 return (ipsec_set_policy_internal(pcb_sp, optname, request, len, cred)); 1102 } 1103 1104 int 1105 ipsec_get_policy(struct inpcb *inp, caddr_t request, size_t len, 1106 struct mbuf **mp) 1107 { 1108 INIT_VNET_IPSEC(curvnet); 1109 struct sadb_x_policy *xpl; 1110 struct secpolicy *pcb_sp; 1111 1112 /* Sanity check. */ 1113 if (inp == NULL || request == NULL || mp == NULL) 1114 return (EINVAL); 1115 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp")); 1116 if (len < sizeof(*xpl)) 1117 return (EINVAL); 1118 xpl = (struct sadb_x_policy *)request; 1119 1120 /* Select direction. */ 1121 switch (xpl->sadb_x_policy_dir) { 1122 case IPSEC_DIR_INBOUND: 1123 pcb_sp = inp->inp_sp->sp_in; 1124 break; 1125 case IPSEC_DIR_OUTBOUND: 1126 pcb_sp = inp->inp_sp->sp_out; 1127 break; 1128 default: 1129 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__, 1130 xpl->sadb_x_policy_dir)); 1131 return (EINVAL); 1132 } 1133 1134 /* Sanity check. Should be an IPSEC_ASSERT. */ 1135 if (pcb_sp == NULL) 1136 return (EINVAL); 1137 1138 *mp = key_sp2msg(pcb_sp); 1139 if (!*mp) { 1140 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 1141 return (ENOBUFS); 1142 } 1143 1144 (*mp)->m_type = MT_DATA; 1145 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1146 printf("%s:\n", __func__); kdebug_mbuf(*mp)); 1147 1148 return (0); 1149 } 1150 1151 /* Delete policy in PCB. */ 1152 int 1153 ipsec_delete_pcbpolicy(struct inpcb *inp) 1154 { 1155 IPSEC_ASSERT(inp != NULL, ("null inp")); 1156 1157 if (inp->inp_sp == NULL) 1158 return (0); 1159 1160 if (inp->inp_sp->sp_in != NULL) 1161 KEY_FREESP(&inp->inp_sp->sp_in); 1162 1163 if (inp->inp_sp->sp_out != NULL) 1164 KEY_FREESP(&inp->inp_sp->sp_out); 1165 1166 ipsec_delpcbpolicy(inp->inp_sp); 1167 inp->inp_sp = NULL; 1168 1169 return (0); 1170 } 1171 1172 /* 1173 * Return current level. 1174 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned. 1175 */ 1176 u_int 1177 ipsec_get_reqlevel(struct ipsecrequest *isr) 1178 { 1179 INIT_VNET_IPSEC(curvnet); 1180 u_int level = 0; 1181 u_int esp_trans_deflev, esp_net_deflev; 1182 u_int ah_trans_deflev, ah_net_deflev; 1183 1184 IPSEC_ASSERT(isr != NULL && isr->sp != NULL, ("null argument")); 1185 IPSEC_ASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family, 1186 ("af family mismatch, src %u, dst %u", 1187 isr->sp->spidx.src.sa.sa_family, 1188 isr->sp->spidx.dst.sa.sa_family)); 1189 1190 /* XXX Note that we have ipseclog() expanded here - code sync issue. */ 1191 #define IPSEC_CHECK_DEFAULT(lev) \ 1192 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \ 1193 && (lev) != IPSEC_LEVEL_UNIQUE) \ 1194 ? (V_ipsec_debug \ 1195 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\ 1196 (lev), IPSEC_LEVEL_REQUIRE) \ 1197 : 0), \ 1198 (lev) = IPSEC_LEVEL_REQUIRE, \ 1199 (lev) \ 1200 : (lev)) 1201 1202 /* Set default level. */ 1203 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) { 1204 #ifdef INET 1205 case AF_INET: 1206 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_trans_deflev); 1207 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_net_deflev); 1208 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_trans_deflev); 1209 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_net_deflev); 1210 break; 1211 #endif 1212 #ifdef INET6 1213 case AF_INET6: 1214 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_trans_deflev); 1215 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_net_deflev); 1216 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_trans_deflev); 1217 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_net_deflev); 1218 break; 1219 #endif /* INET6 */ 1220 default: 1221 panic("%s: unknown af %u", 1222 __func__, isr->sp->spidx.src.sa.sa_family); 1223 } 1224 1225 #undef IPSEC_CHECK_DEFAULT 1226 1227 /* Set level. */ 1228 switch (isr->level) { 1229 case IPSEC_LEVEL_DEFAULT: 1230 switch (isr->saidx.proto) { 1231 case IPPROTO_ESP: 1232 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 1233 level = esp_net_deflev; 1234 else 1235 level = esp_trans_deflev; 1236 break; 1237 case IPPROTO_AH: 1238 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 1239 level = ah_net_deflev; 1240 else 1241 level = ah_trans_deflev; 1242 break; 1243 case IPPROTO_IPCOMP: 1244 /* 1245 * We don't really care, as IPcomp document says that 1246 * we shouldn't compress small packets. 1247 */ 1248 level = IPSEC_LEVEL_USE; 1249 break; 1250 default: 1251 panic("%s: Illegal protocol defined %u\n", __func__, 1252 isr->saidx.proto); 1253 } 1254 break; 1255 1256 case IPSEC_LEVEL_USE: 1257 case IPSEC_LEVEL_REQUIRE: 1258 level = isr->level; 1259 break; 1260 case IPSEC_LEVEL_UNIQUE: 1261 level = IPSEC_LEVEL_REQUIRE; 1262 break; 1263 1264 default: 1265 panic("%s: Illegal IPsec level %u\n", __func__, isr->level); 1266 } 1267 1268 return (level); 1269 } 1270 1271 /* 1272 * Check security policy requirements against the actual 1273 * packet contents. Return one if the packet should be 1274 * reject as "invalid"; otherwiser return zero to have the 1275 * packet treated as "valid". 1276 * 1277 * OUT: 1278 * 0: valid 1279 * 1: invalid 1280 */ 1281 int 1282 ipsec_in_reject(struct secpolicy *sp, struct mbuf *m) 1283 { 1284 INIT_VNET_IPSEC(curvnet); 1285 struct ipsecrequest *isr; 1286 int need_auth; 1287 1288 KEYDEBUG(KEYDEBUG_IPSEC_DATA, 1289 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp)); 1290 1291 /* Check policy. */ 1292 switch (sp->policy) { 1293 case IPSEC_POLICY_DISCARD: 1294 return (1); 1295 case IPSEC_POLICY_BYPASS: 1296 case IPSEC_POLICY_NONE: 1297 return (0); 1298 } 1299 1300 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC, 1301 ("invalid policy %u", sp->policy)); 1302 1303 /* XXX Should compare policy against IPsec header history. */ 1304 1305 need_auth = 0; 1306 for (isr = sp->req; isr != NULL; isr = isr->next) { 1307 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE) 1308 continue; 1309 switch (isr->saidx.proto) { 1310 case IPPROTO_ESP: 1311 if ((m->m_flags & M_DECRYPTED) == 0) { 1312 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1313 printf("%s: ESP m_flags:%x\n", __func__, 1314 m->m_flags)); 1315 return (1); 1316 } 1317 1318 if (!need_auth && 1319 isr->sav != NULL && 1320 isr->sav->tdb_authalgxform != NULL && 1321 (m->m_flags & M_AUTHIPDGM) == 0) { 1322 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1323 printf("%s: ESP/AH m_flags:%x\n", __func__, 1324 m->m_flags)); 1325 return (1); 1326 } 1327 break; 1328 case IPPROTO_AH: 1329 need_auth = 1; 1330 if ((m->m_flags & M_AUTHIPHDR) == 0) { 1331 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1332 printf("%s: AH m_flags:%x\n", __func__, 1333 m->m_flags)); 1334 return (1); 1335 } 1336 break; 1337 case IPPROTO_IPCOMP: 1338 /* 1339 * We don't really care, as IPcomp document 1340 * says that we shouldn't compress small 1341 * packets. IPComp policy should always be 1342 * treated as being in "use" level. 1343 */ 1344 break; 1345 } 1346 } 1347 return (0); /* Valid. */ 1348 } 1349 1350 static int 1351 ipsec46_in_reject(struct mbuf *m, struct inpcb *inp) 1352 { 1353 struct secpolicy *sp; 1354 int error; 1355 int result; 1356 1357 IPSEC_ASSERT(m != NULL, ("null mbuf")); 1358 1359 /* 1360 * Get SP for this packet. 1361 * When we are called from ip_forward(), we call 1362 * ipsec_getpolicybyaddr() with IP_FORWARDING flag. 1363 */ 1364 if (inp == NULL) 1365 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error); 1366 else 1367 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error); 1368 1369 if (sp != NULL) { 1370 result = ipsec_in_reject(sp, m); 1371 KEY_FREESP(&sp); 1372 } else { 1373 result = 0; /* XXX Should be panic? 1374 * -> No, there may be error. */ 1375 } 1376 return (result); 1377 } 1378 1379 /* 1380 * Check AH/ESP integrity. 1381 * This function is called from tcp_input(), udp_input(), 1382 * and {ah,esp}4_input for tunnel mode. 1383 */ 1384 int 1385 ipsec4_in_reject(struct mbuf *m, struct inpcb *inp) 1386 { 1387 INIT_VNET_IPSEC(curvnet); 1388 int result; 1389 1390 result = ipsec46_in_reject(m, inp); 1391 if (result) 1392 V_ipsec4stat.ips_in_polvio++; 1393 1394 return (result); 1395 } 1396 1397 #ifdef INET6 1398 /* 1399 * Check AH/ESP integrity. 1400 * This function is called from tcp6_input(), udp6_input(), 1401 * and {ah,esp}6_input for tunnel mode. 1402 */ 1403 int 1404 ipsec6_in_reject(struct mbuf *m, struct inpcb *inp) 1405 { 1406 INIT_VNET_IPSEC(curvnet); 1407 int result; 1408 1409 result = ipsec46_in_reject(m, inp); 1410 if (result) 1411 V_ipsec6stat.ips_in_polvio++; 1412 1413 return (result); 1414 } 1415 #endif 1416 1417 /* 1418 * Compute the byte size to be occupied by IPsec header. 1419 * In case it is tunnelled, it includes the size of outer IP header. 1420 * NOTE: SP passed is freed in this function. 1421 */ 1422 static size_t 1423 ipsec_hdrsiz_internal(struct secpolicy *sp) 1424 { 1425 INIT_VNET_IPSEC(curvnet); 1426 struct ipsecrequest *isr; 1427 size_t size; 1428 1429 KEYDEBUG(KEYDEBUG_IPSEC_DATA, 1430 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp)); 1431 1432 switch (sp->policy) { 1433 case IPSEC_POLICY_DISCARD: 1434 case IPSEC_POLICY_BYPASS: 1435 case IPSEC_POLICY_NONE: 1436 return (0); 1437 } 1438 1439 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC, 1440 ("invalid policy %u", sp->policy)); 1441 1442 size = 0; 1443 for (isr = sp->req; isr != NULL; isr = isr->next) { 1444 size_t clen = 0; 1445 1446 switch (isr->saidx.proto) { 1447 case IPPROTO_ESP: 1448 clen = esp_hdrsiz(isr->sav); 1449 break; 1450 case IPPROTO_AH: 1451 clen = ah_hdrsiz(isr->sav); 1452 break; 1453 case IPPROTO_IPCOMP: 1454 clen = sizeof(struct ipcomp); 1455 break; 1456 } 1457 1458 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) { 1459 switch (isr->saidx.dst.sa.sa_family) { 1460 case AF_INET: 1461 clen += sizeof(struct ip); 1462 break; 1463 #ifdef INET6 1464 case AF_INET6: 1465 clen += sizeof(struct ip6_hdr); 1466 break; 1467 #endif 1468 default: 1469 ipseclog((LOG_ERR, "%s: unknown AF %d in " 1470 "IPsec tunnel SA\n", __func__, 1471 ((struct sockaddr *)&isr->saidx.dst)->sa_family)); 1472 break; 1473 } 1474 } 1475 size += clen; 1476 } 1477 1478 return (size); 1479 } 1480 1481 /* 1482 * This function is called from ipsec_hdrsiz_tcp(), ip_ipsec_mtu(), 1483 * disabled ip6_ipsec_mtu() and ip6_forward(). 1484 */ 1485 size_t 1486 ipsec_hdrsiz(struct mbuf *m, u_int dir, struct inpcb *inp) 1487 { 1488 INIT_VNET_IPSEC(curvnet); 1489 struct secpolicy *sp; 1490 int error; 1491 size_t size; 1492 1493 IPSEC_ASSERT(m != NULL, ("null mbuf")); 1494 1495 /* Get SP for this packet. 1496 * When we are called from ip_forward(), we call 1497 * ipsec_getpolicybyaddr() with IP_FORWARDING flag. 1498 */ 1499 if (inp == NULL) 1500 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error); 1501 else 1502 sp = ipsec_getpolicybysock(m, dir, inp, &error); 1503 1504 if (sp != NULL) { 1505 size = ipsec_hdrsiz_internal(sp); 1506 KEYDEBUG(KEYDEBUG_IPSEC_DATA, 1507 printf("%s: size:%lu.\n", __func__, 1508 (unsigned long)size)); 1509 1510 KEY_FREESP(&sp); 1511 } else { 1512 size = 0; /* XXX Should be panic? 1513 * -> No, we are called w/o knowing if 1514 * IPsec processing is needed. */ 1515 } 1516 return (size); 1517 } 1518 1519 /* 1520 * Check the variable replay window. 1521 * ipsec_chkreplay() performs replay check before ICV verification. 1522 * ipsec_updatereplay() updates replay bitmap. This must be called after 1523 * ICV verification (it also performs replay check, which is usually done 1524 * beforehand). 1525 * 0 (zero) is returned if packet disallowed, 1 if packet permitted. 1526 * 1527 * Based on RFC 2401. 1528 */ 1529 int 1530 ipsec_chkreplay(u_int32_t seq, struct secasvar *sav) 1531 { 1532 const struct secreplay *replay; 1533 u_int32_t diff; 1534 int fr; 1535 u_int32_t wsizeb; /* Constant: bits of window size. */ 1536 int frlast; /* Constant: last frame. */ 1537 1538 IPSEC_ASSERT(sav != NULL, ("Null SA")); 1539 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state")); 1540 1541 replay = sav->replay; 1542 1543 if (replay->wsize == 0) 1544 return (1); /* No need to check replay. */ 1545 1546 /* Constant. */ 1547 frlast = replay->wsize - 1; 1548 wsizeb = replay->wsize << 3; 1549 1550 /* Sequence number of 0 is invalid. */ 1551 if (seq == 0) 1552 return (0); 1553 1554 /* First time is always okay. */ 1555 if (replay->count == 0) 1556 return (1); 1557 1558 if (seq > replay->lastseq) { 1559 /* Larger sequences are okay. */ 1560 return (1); 1561 } else { 1562 /* seq is equal or less than lastseq. */ 1563 diff = replay->lastseq - seq; 1564 1565 /* Over range to check, i.e. too old or wrapped. */ 1566 if (diff >= wsizeb) 1567 return (0); 1568 1569 fr = frlast - diff / 8; 1570 1571 /* This packet already seen? */ 1572 if ((replay->bitmap)[fr] & (1 << (diff % 8))) 1573 return (0); 1574 1575 /* Out of order but good. */ 1576 return (1); 1577 } 1578 } 1579 1580 /* 1581 * Check replay counter whether to update or not. 1582 * OUT: 0: OK 1583 * 1: NG 1584 */ 1585 int 1586 ipsec_updatereplay(u_int32_t seq, struct secasvar *sav) 1587 { 1588 INIT_VNET_IPSEC(curvnet); 1589 struct secreplay *replay; 1590 u_int32_t diff; 1591 int fr; 1592 u_int32_t wsizeb; /* Constant: bits of window size. */ 1593 int frlast; /* Constant: last frame. */ 1594 1595 IPSEC_ASSERT(sav != NULL, ("Null SA")); 1596 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state")); 1597 1598 replay = sav->replay; 1599 1600 if (replay->wsize == 0) 1601 goto ok; /* No need to check replay. */ 1602 1603 /* Constant. */ 1604 frlast = replay->wsize - 1; 1605 wsizeb = replay->wsize << 3; 1606 1607 /* Sequence number of 0 is invalid. */ 1608 if (seq == 0) 1609 return (1); 1610 1611 /* First time. */ 1612 if (replay->count == 0) { 1613 replay->lastseq = seq; 1614 bzero(replay->bitmap, replay->wsize); 1615 (replay->bitmap)[frlast] = 1; 1616 goto ok; 1617 } 1618 1619 if (seq > replay->lastseq) { 1620 /* seq is larger than lastseq. */ 1621 diff = seq - replay->lastseq; 1622 1623 /* New larger sequence number. */ 1624 if (diff < wsizeb) { 1625 /* In window. */ 1626 /* Set bit for this packet. */ 1627 vshiftl(replay->bitmap, diff, replay->wsize); 1628 (replay->bitmap)[frlast] |= 1; 1629 } else { 1630 /* This packet has a "way larger". */ 1631 bzero(replay->bitmap, replay->wsize); 1632 (replay->bitmap)[frlast] = 1; 1633 } 1634 replay->lastseq = seq; 1635 1636 /* Larger is good. */ 1637 } else { 1638 /* seq is equal or less than lastseq. */ 1639 diff = replay->lastseq - seq; 1640 1641 /* Over range to check, i.e. too old or wrapped. */ 1642 if (diff >= wsizeb) 1643 return (1); 1644 1645 fr = frlast - diff / 8; 1646 1647 /* This packet already seen? */ 1648 if ((replay->bitmap)[fr] & (1 << (diff % 8))) 1649 return (1); 1650 1651 /* Mark as seen. */ 1652 (replay->bitmap)[fr] |= (1 << (diff % 8)); 1653 1654 /* Out of order but good. */ 1655 } 1656 1657 ok: 1658 if (replay->count == ~0) { 1659 1660 /* Set overflow flag. */ 1661 replay->overflow++; 1662 1663 /* Don't increment, no more packets accepted. */ 1664 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) 1665 return (1); 1666 1667 ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n", 1668 __func__, replay->overflow, ipsec_logsastr(sav))); 1669 } 1670 1671 replay->count++; 1672 1673 return (0); 1674 } 1675 1676 /* 1677 * Shift variable length buffer to left. 1678 * IN: bitmap: pointer to the buffer 1679 * nbit: the number of to shift. 1680 * wsize: buffer size (bytes). 1681 */ 1682 static void 1683 vshiftl(unsigned char *bitmap, int nbit, int wsize) 1684 { 1685 int s, j, i; 1686 unsigned char over; 1687 1688 for (j = 0; j < nbit; j += 8) { 1689 s = (nbit - j < 8) ? (nbit - j): 8; 1690 bitmap[0] <<= s; 1691 for (i = 1; i < wsize; i++) { 1692 over = (bitmap[i] >> (8 - s)); 1693 bitmap[i] <<= s; 1694 bitmap[i-1] |= over; 1695 } 1696 } 1697 } 1698 1699 /* Return a printable string for the IPv4 address. */ 1700 static char * 1701 inet_ntoa4(struct in_addr ina) 1702 { 1703 static char buf[4][4 * sizeof "123" + 4]; 1704 unsigned char *ucp = (unsigned char *) &ina; 1705 static int i = 3; 1706 1707 /* XXX-BZ Returns static buffer. */ 1708 i = (i + 1) % 4; 1709 sprintf(buf[i], "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff, 1710 ucp[2] & 0xff, ucp[3] & 0xff); 1711 return (buf[i]); 1712 } 1713 1714 /* Return a printable string for the address. */ 1715 char * 1716 ipsec_address(union sockaddr_union* sa) 1717 { 1718 #ifdef INET6 1719 char ip6buf[INET6_ADDRSTRLEN]; 1720 #endif 1721 1722 switch (sa->sa.sa_family) { 1723 #ifdef INET 1724 case AF_INET: 1725 return (inet_ntoa4(sa->sin.sin_addr)); 1726 #endif /* INET */ 1727 #ifdef INET6 1728 case AF_INET6: 1729 return (ip6_sprintf(ip6buf, &sa->sin6.sin6_addr)); 1730 #endif /* INET6 */ 1731 default: 1732 return ("(unknown address family)"); 1733 } 1734 } 1735 1736 const char * 1737 ipsec_logsastr(struct secasvar *sav) 1738 { 1739 static char buf[256]; 1740 char *p; 1741 struct secasindex *saidx = &sav->sah->saidx; 1742 1743 IPSEC_ASSERT(saidx->src.sa.sa_family == saidx->dst.sa.sa_family, 1744 ("address family mismatch")); 1745 1746 p = buf; 1747 snprintf(buf, sizeof(buf), "SA(SPI=%u ", (u_int32_t)ntohl(sav->spi)); 1748 while (p && *p) 1749 p++; 1750 /* NB: only use ipsec_address on one address at a time. */ 1751 snprintf(p, sizeof (buf) - (p - buf), "src=%s ", 1752 ipsec_address(&saidx->src)); 1753 while (p && *p) 1754 p++; 1755 snprintf(p, sizeof (buf) - (p - buf), "dst=%s)", 1756 ipsec_address(&saidx->dst)); 1757 1758 return (buf); 1759 } 1760 1761 void 1762 ipsec_dumpmbuf(struct mbuf *m) 1763 { 1764 int totlen; 1765 int i; 1766 u_char *p; 1767 1768 totlen = 0; 1769 printf("---\n"); 1770 while (m) { 1771 p = mtod(m, u_char *); 1772 for (i = 0; i < m->m_len; i++) { 1773 printf("%02x ", p[i]); 1774 totlen++; 1775 if (totlen % 16 == 0) 1776 printf("\n"); 1777 } 1778 m = m->m_next; 1779 } 1780 if (totlen % 16 != 0) 1781 printf("\n"); 1782 printf("---\n"); 1783 } 1784 1785 static void 1786 ipsec_attach(void) 1787 { 1788 1789 #ifndef VIMAGE_GLOBALS 1790 vnet_mod_register(&vnet_ipsec_modinfo); 1791 #else 1792 ipsec_iattach(NULL); 1793 #endif 1794 1795 } 1796 1797 static int 1798 ipsec_iattach(const void *unused __unused) 1799 { 1800 INIT_VNET_IPSEC(curvnet); 1801 1802 SECPOLICY_LOCK_INIT(&V_ip4_def_policy); 1803 V_ip4_def_policy.refcnt = 1; /* NB: disallow free. */ 1804 1805 return (0); 1806 } 1807 SYSINIT(ipsec, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, ipsec_attach, NULL); 1808 1809 1810 /* XXX This stuff doesn't belong here... */ 1811 1812 static struct xformsw* xforms = NULL; 1813 1814 /* 1815 * Register a transform; typically at system startup. 1816 */ 1817 void 1818 xform_register(struct xformsw* xsp) 1819 { 1820 1821 xsp->xf_next = xforms; 1822 xforms = xsp; 1823 } 1824 1825 /* 1826 * Initialize transform support in an sav. 1827 */ 1828 int 1829 xform_init(struct secasvar *sav, int xftype) 1830 { 1831 struct xformsw *xsp; 1832 1833 if (sav->tdb_xform != NULL) /* Previously initialized. */ 1834 return (0); 1835 for (xsp = xforms; xsp; xsp = xsp->xf_next) 1836 if (xsp->xf_type == xftype) 1837 return ((*xsp->xf_init)(sav, xsp)); 1838 return (EINVAL); 1839 } 1840