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