ipsec.c revision 186527
1/* $FreeBSD: head/sys/netipsec/ipsec.c 186527 2008-12-27 19:42:59Z 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(m, dir, flag, error) 466 struct mbuf *m; 467 u_int dir; 468 int flag; 469 int *error; 470{ 471 INIT_VNET_IPSEC(curvnet); 472 struct secpolicyindex spidx; 473 struct secpolicy *sp; 474 475 IPSEC_ASSERT(m != NULL, ("null mbuf")); 476 IPSEC_ASSERT(error != NULL, ("null error")); 477 IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND, 478 ("invalid direction %u", dir)); 479 480 sp = NULL; 481 if (key_havesp(dir)) { 482 /* Make an index to look for a policy. */ 483 *error = ipsec_setspidx(m, &spidx, 484 (flag & IP_FORWARDING) ? 0 : 1); 485 if (*error != 0) { 486 DPRINTF(("%s: setpidx failed, dir %u flag %u\n", 487 __func__, dir, flag)); 488 return NULL; 489 } 490 spidx.dir = dir; 491 492 sp = KEY_ALLOCSP(&spidx, dir); 493 } 494 if (sp == NULL) /* no SP found, use system default */ 495 sp = KEY_ALLOCSP_DEFAULT(); 496 IPSEC_ASSERT(sp != NULL, ("null SP")); 497 return sp; 498} 499 500struct secpolicy * 501ipsec4_checkpolicy(m, dir, flag, error, inp) 502 struct mbuf *m; 503 u_int dir, flag; 504 int *error; 505 struct inpcb *inp; 506{ 507 INIT_VNET_IPSEC(curvnet); 508 struct secpolicy *sp; 509 510 *error = 0; 511 if (inp == NULL) 512 sp = ipsec_getpolicybyaddr(m, dir, flag, error); 513 else 514 sp = ipsec_getpolicybysock(m, dir, inp, error); 515 if (sp == NULL) { 516 IPSEC_ASSERT(*error != 0, ("getpolicy failed w/o error")); 517 V_ipsec4stat.ips_out_inval++; 518 return NULL; 519 } 520 IPSEC_ASSERT(*error == 0, ("sp w/ error set to %u", *error)); 521 switch (sp->policy) { 522 case IPSEC_POLICY_ENTRUST: 523 default: 524 printf("%s: invalid policy %u\n", __func__, sp->policy); 525 /* fall thru... */ 526 case IPSEC_POLICY_DISCARD: 527 V_ipsec4stat.ips_out_polvio++; 528 *error = -EINVAL; /* packet is discarded by caller */ 529 break; 530 case IPSEC_POLICY_BYPASS: 531 case IPSEC_POLICY_NONE: 532 KEY_FREESP(&sp); 533 sp = NULL; /* NB: force NULL result */ 534 break; 535 case IPSEC_POLICY_IPSEC: 536 if (sp->req == NULL) /* acquire an SA */ 537 *error = key_spdacquire(sp); 538 break; 539 } 540 if (*error != 0) { 541 KEY_FREESP(&sp); 542 sp = NULL; 543 } 544 return sp; 545} 546 547static int 548ipsec4_setspidx_inpcb(m, pcb) 549 struct mbuf *m; 550 struct inpcb *pcb; 551{ 552 int error; 553 554 IPSEC_ASSERT(pcb != NULL, ("null pcb")); 555 IPSEC_ASSERT(pcb->inp_sp != NULL, ("null inp_sp")); 556 IPSEC_ASSERT(pcb->inp_sp->sp_out != NULL && pcb->inp_sp->sp_in != NULL, 557 ("null sp_in || sp_out")); 558 559 error = ipsec_setspidx(m, &pcb->inp_sp->sp_in->spidx, 1); 560 if (error == 0) { 561 pcb->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND; 562 pcb->inp_sp->sp_out->spidx = pcb->inp_sp->sp_in->spidx; 563 pcb->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND; 564 } else { 565 bzero(&pcb->inp_sp->sp_in->spidx, 566 sizeof (pcb->inp_sp->sp_in->spidx)); 567 bzero(&pcb->inp_sp->sp_out->spidx, 568 sizeof (pcb->inp_sp->sp_in->spidx)); 569 } 570 return error; 571} 572 573#ifdef INET6 574static int 575ipsec6_setspidx_inpcb(m, pcb) 576 struct mbuf *m; 577 struct inpcb *pcb; 578{ 579 //INIT_VNET_IPSEC(curvnet); 580 struct secpolicyindex *spidx; 581 int error; 582 583 IPSEC_ASSERT(pcb != NULL, ("null pcb")); 584 IPSEC_ASSERT(pcb->inp_sp != NULL, ("null inp_sp")); 585 IPSEC_ASSERT(pcb->inp_sp->sp_out != NULL && pcb->inp_sp->sp_in != NULL, 586 ("null sp_in || sp_out")); 587 588 bzero(&pcb->inp_sp->sp_in->spidx, sizeof(*spidx)); 589 bzero(&pcb->inp_sp->sp_out->spidx, sizeof(*spidx)); 590 591 spidx = &pcb->inp_sp->sp_in->spidx; 592 error = ipsec_setspidx(m, spidx, 1); 593 if (error) 594 goto bad; 595 spidx->dir = IPSEC_DIR_INBOUND; 596 597 spidx = &pcb->inp_sp->sp_out->spidx; 598 error = ipsec_setspidx(m, spidx, 1); 599 if (error) 600 goto bad; 601 spidx->dir = IPSEC_DIR_OUTBOUND; 602 603 return 0; 604 605bad: 606 bzero(&pcb->inp_sp->sp_in->spidx, sizeof(*spidx)); 607 bzero(&pcb->inp_sp->sp_out->spidx, sizeof(*spidx)); 608 return error; 609} 610#endif 611 612/* 613 * configure security policy index (src/dst/proto/sport/dport) 614 * by looking at the content of mbuf. 615 * the caller is responsible for error recovery (like clearing up spidx). 616 */ 617static int 618ipsec_setspidx(m, spidx, needport) 619 struct mbuf *m; 620 struct secpolicyindex *spidx; 621 int needport; 622{ 623 INIT_VNET_IPSEC(curvnet); 624 struct ip *ip = NULL; 625 struct ip ipbuf; 626 u_int v; 627 struct mbuf *n; 628 int len; 629 int error; 630 631 IPSEC_ASSERT(m != NULL, ("null mbuf")); 632 633 /* 634 * validate m->m_pkthdr.len. we see incorrect length if we 635 * mistakenly call this function with inconsistent mbuf chain 636 * (like 4.4BSD tcp/udp processing). XXX should we panic here? 637 */ 638 len = 0; 639 for (n = m; n; n = n->m_next) 640 len += n->m_len; 641 if (m->m_pkthdr.len != len) { 642 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 643 printf("%s: pkthdr len(%d) mismatch (%d), ignored.\n", 644 __func__, len, m->m_pkthdr.len)); 645 return EINVAL; 646 } 647 648 if (m->m_pkthdr.len < sizeof(struct ip)) { 649 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 650 printf("%s: pkthdr len(%d) too small (v4), ignored.\n", 651 __func__, m->m_pkthdr.len)); 652 return EINVAL; 653 } 654 655 if (m->m_len >= sizeof(*ip)) 656 ip = mtod(m, struct ip *); 657 else { 658 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf); 659 ip = &ipbuf; 660 } 661#ifdef _IP_VHL 662 v = _IP_VHL_V(ip->ip_vhl); 663#else 664 v = ip->ip_v; 665#endif 666 switch (v) { 667 case 4: 668 error = ipsec4_setspidx_ipaddr(m, spidx); 669 if (error) 670 return error; 671 ipsec4_get_ulp(m, spidx, needport); 672 return 0; 673#ifdef INET6 674 case 6: 675 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) { 676 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 677 printf("%s: pkthdr len(%d) too small (v6), " 678 "ignored\n", __func__, m->m_pkthdr.len)); 679 return EINVAL; 680 } 681 error = ipsec6_setspidx_ipaddr(m, spidx); 682 if (error) 683 return error; 684 ipsec6_get_ulp(m, spidx, needport); 685 return 0; 686#endif 687 default: 688 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 689 printf("%s: " "unknown IP version %u, ignored.\n", 690 __func__, v)); 691 return EINVAL; 692 } 693} 694 695static void 696ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport) 697{ 698 u_int8_t nxt; 699 int off; 700 701 /* sanity check */ 702 IPSEC_ASSERT(m != NULL, ("null mbuf")); 703 IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),("packet too short")); 704 705 /* NB: ip_input() flips it into host endian XXX need more checking */ 706 if (m->m_len < sizeof (struct ip)) { 707 struct ip *ip = mtod(m, struct ip *); 708 if (ip->ip_off & (IP_MF | IP_OFFMASK)) 709 goto done; 710#ifdef _IP_VHL 711 off = _IP_VHL_HL(ip->ip_vhl) << 2; 712#else 713 off = ip->ip_hl << 2; 714#endif 715 nxt = ip->ip_p; 716 } else { 717 struct ip ih; 718 719 m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih); 720 if (ih.ip_off & (IP_MF | IP_OFFMASK)) 721 goto done; 722#ifdef _IP_VHL 723 off = _IP_VHL_HL(ih.ip_vhl) << 2; 724#else 725 off = ih.ip_hl << 2; 726#endif 727 nxt = ih.ip_p; 728 } 729 730 while (off < m->m_pkthdr.len) { 731 struct ip6_ext ip6e; 732 struct tcphdr th; 733 struct udphdr uh; 734 735 switch (nxt) { 736 case IPPROTO_TCP: 737 spidx->ul_proto = nxt; 738 if (!needport) 739 goto done_proto; 740 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len) 741 goto done; 742 m_copydata(m, off, sizeof (th), (caddr_t) &th); 743 spidx->src.sin.sin_port = th.th_sport; 744 spidx->dst.sin.sin_port = th.th_dport; 745 return; 746 case IPPROTO_UDP: 747 spidx->ul_proto = nxt; 748 if (!needport) 749 goto done_proto; 750 if (off + sizeof(struct udphdr) > m->m_pkthdr.len) 751 goto done; 752 m_copydata(m, off, sizeof (uh), (caddr_t) &uh); 753 spidx->src.sin.sin_port = uh.uh_sport; 754 spidx->dst.sin.sin_port = uh.uh_dport; 755 return; 756 case IPPROTO_AH: 757 if (off + sizeof(ip6e) > m->m_pkthdr.len) 758 goto done; 759 /* XXX sigh, this works but is totally bogus */ 760 m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e); 761 off += (ip6e.ip6e_len + 2) << 2; 762 nxt = ip6e.ip6e_nxt; 763 break; 764 case IPPROTO_ICMP: 765 default: 766 /* XXX intermediate headers??? */ 767 spidx->ul_proto = nxt; 768 goto done_proto; 769 } 770 } 771done: 772 spidx->ul_proto = IPSEC_ULPROTO_ANY; 773done_proto: 774 spidx->src.sin.sin_port = IPSEC_PORT_ANY; 775 spidx->dst.sin.sin_port = IPSEC_PORT_ANY; 776} 777 778/* assumes that m is sane */ 779static int 780ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx) 781{ 782 static const struct sockaddr_in template = { 783 sizeof (struct sockaddr_in), 784 AF_INET, 785 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 } 786 }; 787 788 spidx->src.sin = template; 789 spidx->dst.sin = template; 790 791 if (m->m_len < sizeof (struct ip)) { 792 m_copydata(m, offsetof(struct ip, ip_src), 793 sizeof (struct in_addr), 794 (caddr_t) &spidx->src.sin.sin_addr); 795 m_copydata(m, offsetof(struct ip, ip_dst), 796 sizeof (struct in_addr), 797 (caddr_t) &spidx->dst.sin.sin_addr); 798 } else { 799 struct ip *ip = mtod(m, struct ip *); 800 spidx->src.sin.sin_addr = ip->ip_src; 801 spidx->dst.sin.sin_addr = ip->ip_dst; 802 } 803 804 spidx->prefs = sizeof(struct in_addr) << 3; 805 spidx->prefd = sizeof(struct in_addr) << 3; 806 807 return 0; 808} 809 810#ifdef INET6 811static void 812ipsec6_get_ulp(m, spidx, needport) 813 struct mbuf *m; 814 struct secpolicyindex *spidx; 815 int needport; 816{ 817 INIT_VNET_IPSEC(curvnet); 818 int off, nxt; 819 struct tcphdr th; 820 struct udphdr uh; 821 struct icmp6_hdr ih; 822 823 /* sanity check */ 824 if (m == NULL) 825 panic("%s: NULL pointer was passed.\n", __func__); 826 827 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 828 printf("%s:\n", __func__); kdebug_mbuf(m)); 829 830 /* set default */ 831 spidx->ul_proto = IPSEC_ULPROTO_ANY; 832 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY; 833 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY; 834 835 nxt = -1; 836 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 837 if (off < 0 || m->m_pkthdr.len < off) 838 return; 839 840 switch (nxt) { 841 case IPPROTO_TCP: 842 spidx->ul_proto = nxt; 843 if (!needport) 844 break; 845 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len) 846 break; 847 m_copydata(m, off, sizeof(th), (caddr_t)&th); 848 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport; 849 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport; 850 break; 851 case IPPROTO_UDP: 852 spidx->ul_proto = nxt; 853 if (!needport) 854 break; 855 if (off + sizeof(struct udphdr) > m->m_pkthdr.len) 856 break; 857 m_copydata(m, off, sizeof(uh), (caddr_t)&uh); 858 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport; 859 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport; 860 break; 861 case IPPROTO_ICMPV6: 862 spidx->ul_proto = nxt; 863 if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len) 864 break; 865 m_copydata(m, off, sizeof(ih), (caddr_t)&ih); 866 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = 867 htons((uint16_t)ih.icmp6_type); 868 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = 869 htons((uint16_t)ih.icmp6_code); 870 break; 871 default: 872 /* XXX intermediate headers??? */ 873 spidx->ul_proto = nxt; 874 break; 875 } 876} 877 878/* assumes that m is sane */ 879static int 880ipsec6_setspidx_ipaddr(m, spidx) 881 struct mbuf *m; 882 struct secpolicyindex *spidx; 883{ 884 struct ip6_hdr *ip6 = NULL; 885 struct ip6_hdr ip6buf; 886 struct sockaddr_in6 *sin6; 887 888 if (m->m_len >= sizeof(*ip6)) 889 ip6 = mtod(m, struct ip6_hdr *); 890 else { 891 m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf); 892 ip6 = &ip6buf; 893 } 894 895 sin6 = (struct sockaddr_in6 *)&spidx->src; 896 bzero(sin6, sizeof(*sin6)); 897 sin6->sin6_family = AF_INET6; 898 sin6->sin6_len = sizeof(struct sockaddr_in6); 899 bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src)); 900 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) { 901 sin6->sin6_addr.s6_addr16[1] = 0; 902 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]); 903 } 904 spidx->prefs = sizeof(struct in6_addr) << 3; 905 906 sin6 = (struct sockaddr_in6 *)&spidx->dst; 907 bzero(sin6, sizeof(*sin6)); 908 sin6->sin6_family = AF_INET6; 909 sin6->sin6_len = sizeof(struct sockaddr_in6); 910 bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst)); 911 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { 912 sin6->sin6_addr.s6_addr16[1] = 0; 913 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]); 914 } 915 spidx->prefd = sizeof(struct in6_addr) << 3; 916 917 return 0; 918} 919#endif 920 921static void 922ipsec_delpcbpolicy(p) 923 struct inpcbpolicy *p; 924{ 925 free(p, M_IPSEC_INPCB); 926} 927 928/* initialize policy in PCB */ 929int 930ipsec_init_policy(so, pcb_sp) 931 struct socket *so; 932 struct inpcbpolicy **pcb_sp; 933{ 934 INIT_VNET_IPSEC(curvnet); 935 struct inpcbpolicy *new; 936 937 /* sanity check. */ 938 if (so == NULL || pcb_sp == NULL) 939 panic("%s: NULL pointer was passed.\n", __func__); 940 941 new = (struct inpcbpolicy *) malloc(sizeof(struct inpcbpolicy), 942 M_IPSEC_INPCB, M_NOWAIT|M_ZERO); 943 if (new == NULL) { 944 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 945 return ENOBUFS; 946 } 947 948 new->priv = IPSEC_IS_PRIVILEGED_SO(so); 949 950 if ((new->sp_in = KEY_NEWSP()) == NULL) { 951 ipsec_delpcbpolicy(new); 952 return ENOBUFS; 953 } 954 new->sp_in->state = IPSEC_SPSTATE_ALIVE; 955 new->sp_in->policy = IPSEC_POLICY_ENTRUST; 956 957 if ((new->sp_out = KEY_NEWSP()) == NULL) { 958 KEY_FREESP(&new->sp_in); 959 ipsec_delpcbpolicy(new); 960 return ENOBUFS; 961 } 962 new->sp_out->state = IPSEC_SPSTATE_ALIVE; 963 new->sp_out->policy = IPSEC_POLICY_ENTRUST; 964 965 *pcb_sp = new; 966 967 return 0; 968} 969 970/* copy old ipsec policy into new */ 971int 972ipsec_copy_policy(old, new) 973 struct inpcbpolicy *old, *new; 974{ 975 struct secpolicy *sp; 976 977 sp = ipsec_deepcopy_policy(old->sp_in); 978 if (sp) { 979 KEY_FREESP(&new->sp_in); 980 new->sp_in = sp; 981 } else 982 return ENOBUFS; 983 984 sp = ipsec_deepcopy_policy(old->sp_out); 985 if (sp) { 986 KEY_FREESP(&new->sp_out); 987 new->sp_out = sp; 988 } else 989 return ENOBUFS; 990 991 new->priv = old->priv; 992 993 return 0; 994} 995 996struct ipsecrequest * 997ipsec_newisr(void) 998{ 999 struct ipsecrequest *p; 1000 1001 p = malloc(sizeof(struct ipsecrequest), M_IPSEC_SR, M_NOWAIT|M_ZERO); 1002 if (p != NULL) 1003 IPSECREQUEST_LOCK_INIT(p); 1004 return p; 1005} 1006 1007void 1008ipsec_delisr(struct ipsecrequest *p) 1009{ 1010 IPSECREQUEST_LOCK_DESTROY(p); 1011 free(p, M_IPSEC_SR); 1012} 1013 1014/* deep-copy a policy in PCB */ 1015static struct secpolicy * 1016ipsec_deepcopy_policy(src) 1017 struct secpolicy *src; 1018{ 1019 struct ipsecrequest *newchain = NULL; 1020 struct ipsecrequest *p; 1021 struct ipsecrequest **q; 1022 struct ipsecrequest *r; 1023 struct secpolicy *dst; 1024 1025 if (src == NULL) 1026 return NULL; 1027 dst = KEY_NEWSP(); 1028 if (dst == NULL) 1029 return NULL; 1030 1031 /* 1032 * deep-copy IPsec request chain. This is required since struct 1033 * ipsecrequest is not reference counted. 1034 */ 1035 q = &newchain; 1036 for (p = src->req; p; p = p->next) { 1037 *q = ipsec_newisr(); 1038 if (*q == NULL) 1039 goto fail; 1040 (*q)->saidx.proto = p->saidx.proto; 1041 (*q)->saidx.mode = p->saidx.mode; 1042 (*q)->level = p->level; 1043 (*q)->saidx.reqid = p->saidx.reqid; 1044 1045 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src)); 1046 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst)); 1047 1048 (*q)->sp = dst; 1049 1050 q = &((*q)->next); 1051 } 1052 1053 dst->req = newchain; 1054 dst->state = src->state; 1055 dst->policy = src->policy; 1056 /* do not touch the refcnt fields */ 1057 1058 return dst; 1059 1060fail: 1061 for (p = newchain; p; p = r) { 1062 r = p->next; 1063 ipsec_delisr(p); 1064 p = NULL; 1065 } 1066 return NULL; 1067} 1068 1069/* set policy and ipsec request if present. */ 1070static int 1071ipsec_set_policy(pcb_sp, optname, request, len, cred) 1072 struct secpolicy **pcb_sp; 1073 int optname; 1074 caddr_t request; 1075 size_t len; 1076 struct ucred *cred; 1077{ 1078 INIT_VNET_IPSEC(curvnet); 1079 struct sadb_x_policy *xpl; 1080 struct secpolicy *newsp = NULL; 1081 int error; 1082 1083 /* sanity check. */ 1084 if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL) 1085 return EINVAL; 1086 if (len < sizeof(*xpl)) 1087 return EINVAL; 1088 xpl = (struct sadb_x_policy *)request; 1089 1090 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1091 printf("%s: passed policy\n", __func__); 1092 kdebug_sadb_x_policy((struct sadb_ext *)xpl)); 1093 1094 /* check policy type */ 1095 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */ 1096 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD 1097 || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE) 1098 return EINVAL; 1099 1100 /* check privileged socket */ 1101 if (cred != NULL && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) { 1102 error = priv_check_cred(cred, PRIV_NETINET_IPSEC, 0); 1103 if (error) 1104 return EACCES; 1105 } 1106 1107 /* allocation new SP entry */ 1108 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL) 1109 return error; 1110 1111 newsp->state = IPSEC_SPSTATE_ALIVE; 1112 1113 /* clear old SP and set new SP */ 1114 KEY_FREESP(pcb_sp); 1115 *pcb_sp = newsp; 1116 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1117 printf("%s: new policy\n", __func__); 1118 kdebug_secpolicy(newsp)); 1119 1120 return 0; 1121} 1122 1123static int 1124ipsec_get_policy(pcb_sp, mp) 1125 struct secpolicy *pcb_sp; 1126 struct mbuf **mp; 1127{ 1128 INIT_VNET_IPSEC(curvnet); 1129 1130 /* sanity check. */ 1131 if (pcb_sp == NULL || mp == NULL) 1132 return EINVAL; 1133 1134 *mp = key_sp2msg(pcb_sp); 1135 if (!*mp) { 1136 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__)); 1137 return ENOBUFS; 1138 } 1139 1140 (*mp)->m_type = MT_DATA; 1141 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1142 printf("%s:\n", __func__); kdebug_mbuf(*mp)); 1143 1144 return 0; 1145} 1146 1147int 1148ipsec4_set_policy(inp, optname, request, len, cred) 1149 struct inpcb *inp; 1150 int optname; 1151 caddr_t request; 1152 size_t len; 1153 struct ucred *cred; 1154{ 1155 INIT_VNET_IPSEC(curvnet); 1156 struct sadb_x_policy *xpl; 1157 struct secpolicy **pcb_sp; 1158 1159 /* sanity check. */ 1160 if (inp == NULL || request == NULL) 1161 return EINVAL; 1162 if (len < sizeof(*xpl)) 1163 return EINVAL; 1164 xpl = (struct sadb_x_policy *)request; 1165 1166 /* select direction */ 1167 switch (xpl->sadb_x_policy_dir) { 1168 case IPSEC_DIR_INBOUND: 1169 pcb_sp = &inp->inp_sp->sp_in; 1170 break; 1171 case IPSEC_DIR_OUTBOUND: 1172 pcb_sp = &inp->inp_sp->sp_out; 1173 break; 1174 default: 1175 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__, 1176 xpl->sadb_x_policy_dir)); 1177 return EINVAL; 1178 } 1179 1180 return ipsec_set_policy(pcb_sp, optname, request, len, cred); 1181} 1182 1183int 1184ipsec4_get_policy(inp, request, len, mp) 1185 struct inpcb *inp; 1186 caddr_t request; 1187 size_t len; 1188 struct mbuf **mp; 1189{ 1190 INIT_VNET_IPSEC(curvnet); 1191 struct sadb_x_policy *xpl; 1192 struct secpolicy *pcb_sp; 1193 1194 /* sanity check. */ 1195 if (inp == NULL || request == NULL || mp == NULL) 1196 return EINVAL; 1197 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp")); 1198 if (len < sizeof(*xpl)) 1199 return EINVAL; 1200 xpl = (struct sadb_x_policy *)request; 1201 1202 /* select direction */ 1203 switch (xpl->sadb_x_policy_dir) { 1204 case IPSEC_DIR_INBOUND: 1205 pcb_sp = inp->inp_sp->sp_in; 1206 break; 1207 case IPSEC_DIR_OUTBOUND: 1208 pcb_sp = inp->inp_sp->sp_out; 1209 break; 1210 default: 1211 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__, 1212 xpl->sadb_x_policy_dir)); 1213 return EINVAL; 1214 } 1215 1216 return ipsec_get_policy(pcb_sp, mp); 1217} 1218 1219/* delete policy in PCB */ 1220int 1221ipsec_delete_pcbpolicy(inp) 1222 struct inpcb *inp; 1223{ 1224 IPSEC_ASSERT(inp != NULL, ("null inp")); 1225 1226 if (inp->inp_sp == NULL) 1227 return 0; 1228 1229 if (inp->inp_sp->sp_in != NULL) 1230 KEY_FREESP(&inp->inp_sp->sp_in); 1231 1232 if (inp->inp_sp->sp_out != NULL) 1233 KEY_FREESP(&inp->inp_sp->sp_out); 1234 1235 ipsec_delpcbpolicy(inp->inp_sp); 1236 inp->inp_sp = NULL; 1237 1238 return 0; 1239} 1240 1241#ifdef INET6 1242int 1243ipsec6_set_policy(inp, optname, request, len, cred) 1244 struct inpcb *inp; 1245 int optname; 1246 caddr_t request; 1247 size_t len; 1248 struct ucred *cred; 1249{ 1250 INIT_VNET_IPSEC(curvnet); 1251 struct sadb_x_policy *xpl; 1252 struct secpolicy **pcb_sp; 1253 1254 /* sanity check. */ 1255 if (inp == NULL || request == NULL) 1256 return EINVAL; 1257 if (len < sizeof(*xpl)) 1258 return EINVAL; 1259 xpl = (struct sadb_x_policy *)request; 1260 1261 /* select direction */ 1262 switch (xpl->sadb_x_policy_dir) { 1263 case IPSEC_DIR_INBOUND: 1264 pcb_sp = &inp->inp_sp->sp_in; 1265 break; 1266 case IPSEC_DIR_OUTBOUND: 1267 pcb_sp = &inp->inp_sp->sp_out; 1268 break; 1269 default: 1270 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__, 1271 xpl->sadb_x_policy_dir)); 1272 return EINVAL; 1273 } 1274 1275 return ipsec_set_policy(pcb_sp, optname, request, len, cred); 1276} 1277 1278int 1279ipsec6_get_policy(inp, request, len, mp) 1280 struct inpcb *inp; 1281 caddr_t request; 1282 size_t len; 1283 struct mbuf **mp; 1284{ 1285 INIT_VNET_IPSEC(curvnet); 1286 struct sadb_x_policy *xpl; 1287 struct secpolicy *pcb_sp; 1288 1289 /* sanity check. */ 1290 if (inp == NULL || request == NULL || mp == NULL) 1291 return EINVAL; 1292 IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp")); 1293 if (len < sizeof(*xpl)) 1294 return EINVAL; 1295 xpl = (struct sadb_x_policy *)request; 1296 1297 /* select direction */ 1298 switch (xpl->sadb_x_policy_dir) { 1299 case IPSEC_DIR_INBOUND: 1300 pcb_sp = inp->inp_sp->sp_in; 1301 break; 1302 case IPSEC_DIR_OUTBOUND: 1303 pcb_sp = inp->inp_sp->sp_out; 1304 break; 1305 default: 1306 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__, 1307 xpl->sadb_x_policy_dir)); 1308 return EINVAL; 1309 } 1310 1311 return ipsec_get_policy(pcb_sp, mp); 1312} 1313#endif 1314 1315/* 1316 * return current level. 1317 * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned. 1318 */ 1319u_int 1320ipsec_get_reqlevel(isr) 1321 struct ipsecrequest *isr; 1322{ 1323 INIT_VNET_IPSEC(curvnet); 1324 u_int level = 0; 1325 u_int esp_trans_deflev, esp_net_deflev; 1326 u_int ah_trans_deflev, ah_net_deflev; 1327 1328 IPSEC_ASSERT(isr != NULL && isr->sp != NULL, ("null argument")); 1329 IPSEC_ASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family, 1330 ("af family mismatch, src %u, dst %u", 1331 isr->sp->spidx.src.sa.sa_family, 1332 isr->sp->spidx.dst.sa.sa_family)); 1333 1334/* XXX note that we have ipseclog() expanded here - code sync issue */ 1335#define IPSEC_CHECK_DEFAULT(lev) \ 1336 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \ 1337 && (lev) != IPSEC_LEVEL_UNIQUE) \ 1338 ? (V_ipsec_debug \ 1339 ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\ 1340 (lev), IPSEC_LEVEL_REQUIRE) \ 1341 : 0), \ 1342 (lev) = IPSEC_LEVEL_REQUIRE, \ 1343 (lev) \ 1344 : (lev)) 1345 1346 /* set default level */ 1347 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) { 1348#ifdef INET 1349 case AF_INET: 1350 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_trans_deflev); 1351 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_net_deflev); 1352 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_trans_deflev); 1353 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_net_deflev); 1354 break; 1355#endif 1356#ifdef INET6 1357 case AF_INET6: 1358 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_trans_deflev); 1359 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_net_deflev); 1360 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_trans_deflev); 1361 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_net_deflev); 1362 break; 1363#endif /* INET6 */ 1364 default: 1365 panic("%s: unknown af %u", 1366 __func__, isr->sp->spidx.src.sa.sa_family); 1367 } 1368 1369#undef IPSEC_CHECK_DEFAULT 1370 1371 /* set level */ 1372 switch (isr->level) { 1373 case IPSEC_LEVEL_DEFAULT: 1374 switch (isr->saidx.proto) { 1375 case IPPROTO_ESP: 1376 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 1377 level = esp_net_deflev; 1378 else 1379 level = esp_trans_deflev; 1380 break; 1381 case IPPROTO_AH: 1382 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 1383 level = ah_net_deflev; 1384 else 1385 level = ah_trans_deflev; 1386 break; 1387 case IPPROTO_IPCOMP: 1388 /* 1389 * we don't really care, as IPcomp document says that 1390 * we shouldn't compress small packets 1391 */ 1392 level = IPSEC_LEVEL_USE; 1393 break; 1394 default: 1395 panic("%s: Illegal protocol defined %u\n", __func__, 1396 isr->saidx.proto); 1397 } 1398 break; 1399 1400 case IPSEC_LEVEL_USE: 1401 case IPSEC_LEVEL_REQUIRE: 1402 level = isr->level; 1403 break; 1404 case IPSEC_LEVEL_UNIQUE: 1405 level = IPSEC_LEVEL_REQUIRE; 1406 break; 1407 1408 default: 1409 panic("%s: Illegal IPsec level %u\n", __func__, isr->level); 1410 } 1411 1412 return level; 1413} 1414 1415/* 1416 * Check security policy requirements against the actual 1417 * packet contents. Return one if the packet should be 1418 * reject as "invalid"; otherwiser return zero to have the 1419 * packet treated as "valid". 1420 * 1421 * OUT: 1422 * 0: valid 1423 * 1: invalid 1424 */ 1425int 1426ipsec_in_reject(struct secpolicy *sp, struct mbuf *m) 1427{ 1428 INIT_VNET_IPSEC(curvnet); 1429 struct ipsecrequest *isr; 1430 int need_auth; 1431 1432 KEYDEBUG(KEYDEBUG_IPSEC_DATA, 1433 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp)); 1434 1435 /* check policy */ 1436 switch (sp->policy) { 1437 case IPSEC_POLICY_DISCARD: 1438 return 1; 1439 case IPSEC_POLICY_BYPASS: 1440 case IPSEC_POLICY_NONE: 1441 return 0; 1442 } 1443 1444 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC, 1445 ("invalid policy %u", sp->policy)); 1446 1447 /* XXX should compare policy against ipsec header history */ 1448 1449 need_auth = 0; 1450 for (isr = sp->req; isr != NULL; isr = isr->next) { 1451 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE) 1452 continue; 1453 switch (isr->saidx.proto) { 1454 case IPPROTO_ESP: 1455 if ((m->m_flags & M_DECRYPTED) == 0) { 1456 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1457 printf("%s: ESP m_flags:%x\n", __func__, 1458 m->m_flags)); 1459 return 1; 1460 } 1461 1462 if (!need_auth && 1463 isr->sav != NULL && 1464 isr->sav->tdb_authalgxform != NULL && 1465 (m->m_flags & M_AUTHIPDGM) == 0) { 1466 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1467 printf("%s: ESP/AH m_flags:%x\n", __func__, 1468 m->m_flags)); 1469 return 1; 1470 } 1471 break; 1472 case IPPROTO_AH: 1473 need_auth = 1; 1474 if ((m->m_flags & M_AUTHIPHDR) == 0) { 1475 KEYDEBUG(KEYDEBUG_IPSEC_DUMP, 1476 printf("%s: AH m_flags:%x\n", __func__, 1477 m->m_flags)); 1478 return 1; 1479 } 1480 break; 1481 case IPPROTO_IPCOMP: 1482 /* 1483 * we don't really care, as IPcomp document 1484 * says that we shouldn't compress small 1485 * packets, IPComp policy should always be 1486 * treated as being in "use" level. 1487 */ 1488 break; 1489 } 1490 } 1491 return 0; /* valid */ 1492} 1493 1494/* 1495 * Check AH/ESP integrity. 1496 * This function is called from tcp_input(), udp_input(), 1497 * and {ah,esp}4_input for tunnel mode 1498 */ 1499int 1500ipsec4_in_reject(m, inp) 1501 struct mbuf *m; 1502 struct inpcb *inp; 1503{ 1504 INIT_VNET_IPSEC(curvnet); 1505 struct secpolicy *sp; 1506 int error; 1507 int result; 1508 1509 IPSEC_ASSERT(m != NULL, ("null mbuf")); 1510 1511 /* get SP for this packet. 1512 * When we are called from ip_forward(), we call 1513 * ipsec_getpolicybyaddr() with IP_FORWARDING flag. 1514 */ 1515 if (inp == NULL) 1516 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error); 1517 else 1518 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error); 1519 1520 if (sp != NULL) { 1521 result = ipsec_in_reject(sp, m); 1522 if (result) 1523 V_ipsec4stat.ips_in_polvio++; 1524 KEY_FREESP(&sp); 1525 } else { 1526 result = 0; /* XXX should be panic ? 1527 * -> No, there may be error. */ 1528 } 1529 return result; 1530} 1531 1532#ifdef INET6 1533/* 1534 * Check AH/ESP integrity. 1535 * This function is called from tcp6_input(), udp6_input(), 1536 * and {ah,esp}6_input for tunnel mode 1537 */ 1538int 1539ipsec6_in_reject(m, inp) 1540 struct mbuf *m; 1541 struct inpcb *inp; 1542{ 1543 INIT_VNET_IPSEC(curvnet); 1544 struct secpolicy *sp = NULL; 1545 int error; 1546 int result; 1547 1548 /* sanity check */ 1549 if (m == NULL) 1550 return 0; /* XXX should be panic ? */ 1551 1552 /* get SP for this packet. 1553 * When we are called from ip_forward(), we call 1554 * ipsec_getpolicybyaddr() with IP_FORWARDING flag. 1555 */ 1556 if (inp == NULL) 1557 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error); 1558 else 1559 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error); 1560 1561 if (sp != NULL) { 1562 result = ipsec_in_reject(sp, m); 1563 if (result) 1564 V_ipsec6stat.ips_in_polvio++; 1565 KEY_FREESP(&sp); 1566 } else { 1567 result = 0; 1568 } 1569 return result; 1570} 1571#endif 1572 1573/* 1574 * compute the byte size to be occupied by IPsec header. 1575 * in case it is tunneled, it includes the size of outer IP header. 1576 * NOTE: SP passed is free in this function. 1577 */ 1578static size_t 1579ipsec_hdrsiz(struct secpolicy *sp) 1580{ 1581 INIT_VNET_IPSEC(curvnet); 1582 struct ipsecrequest *isr; 1583 size_t siz; 1584 1585 KEYDEBUG(KEYDEBUG_IPSEC_DATA, 1586 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp)); 1587 1588 switch (sp->policy) { 1589 case IPSEC_POLICY_DISCARD: 1590 case IPSEC_POLICY_BYPASS: 1591 case IPSEC_POLICY_NONE: 1592 return 0; 1593 } 1594 1595 IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC, 1596 ("invalid policy %u", sp->policy)); 1597 1598 siz = 0; 1599 for (isr = sp->req; isr != NULL; isr = isr->next) { 1600 size_t clen = 0; 1601 1602 switch (isr->saidx.proto) { 1603 case IPPROTO_ESP: 1604 clen = esp_hdrsiz(isr->sav); 1605 break; 1606 case IPPROTO_AH: 1607 clen = ah_hdrsiz(isr->sav); 1608 break; 1609 case IPPROTO_IPCOMP: 1610 clen = sizeof(struct ipcomp); 1611 break; 1612 } 1613 1614 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) { 1615 switch (isr->saidx.dst.sa.sa_family) { 1616 case AF_INET: 1617 clen += sizeof(struct ip); 1618 break; 1619#ifdef INET6 1620 case AF_INET6: 1621 clen += sizeof(struct ip6_hdr); 1622 break; 1623#endif 1624 default: 1625 ipseclog((LOG_ERR, "%s: unknown AF %d in " 1626 "IPsec tunnel SA\n", __func__, 1627 ((struct sockaddr *)&isr->saidx.dst)->sa_family)); 1628 break; 1629 } 1630 } 1631 siz += clen; 1632 } 1633 1634 return siz; 1635} 1636 1637/* This function is called from ip_forward() and ipsec4_hdrsize_tcp(). */ 1638size_t 1639ipsec4_hdrsiz(m, dir, inp) 1640 struct mbuf *m; 1641 u_int dir; 1642 struct inpcb *inp; 1643{ 1644 INIT_VNET_IPSEC(curvnet); 1645 struct secpolicy *sp; 1646 int error; 1647 size_t size; 1648 1649 IPSEC_ASSERT(m != NULL, ("null mbuf")); 1650 1651 /* get SP for this packet. 1652 * When we are called from ip_forward(), we call 1653 * ipsec_getpolicybyaddr() with IP_FORWARDING flag. 1654 */ 1655 if (inp == NULL) 1656 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error); 1657 else 1658 sp = ipsec_getpolicybysock(m, dir, inp, &error); 1659 1660 if (sp != NULL) { 1661 size = ipsec_hdrsiz(sp); 1662 KEYDEBUG(KEYDEBUG_IPSEC_DATA, 1663 printf("%s: size:%lu.\n", __func__, 1664 (unsigned long)size)); 1665 1666 KEY_FREESP(&sp); 1667 } else { 1668 size = 0; /* XXX should be panic ? 1669 * -> No, we are called w/o knowing if 1670 * IPsec processing is needed. */ 1671 } 1672 return size; 1673} 1674 1675#ifdef INET6 1676/* This function is called from ipsec6_hdrsize_tcp(), 1677 * and maybe from ip6_forward.() 1678 */ 1679size_t 1680ipsec6_hdrsiz(m, dir, inp) 1681 struct mbuf *m; 1682 u_int dir; 1683 struct inpcb *inp; 1684{ 1685 INIT_VNET_IPSEC(curvnet); 1686 struct secpolicy *sp; 1687 int error; 1688 size_t size; 1689 1690 IPSEC_ASSERT(m != NULL, ("null mbuf")); 1691 IPSEC_ASSERT(inp == NULL || inp->inp_socket != NULL, 1692 ("socket w/o inpcb")); 1693 1694 /* get SP for this packet */ 1695 /* XXX Is it right to call with IP_FORWARDING. */ 1696 if (inp == NULL) 1697 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error); 1698 else 1699 sp = ipsec_getpolicybysock(m, dir, inp, &error); 1700 1701 if (sp == NULL) 1702 return 0; 1703 size = ipsec_hdrsiz(sp); 1704 KEYDEBUG(KEYDEBUG_IPSEC_DATA, 1705 printf("%s: size:%lu.\n", __func__, (unsigned long)size)); 1706 KEY_FREESP(&sp); 1707 1708 return size; 1709} 1710#endif /*INET6*/ 1711 1712/* 1713 * Check the variable replay window. 1714 * ipsec_chkreplay() performs replay check before ICV verification. 1715 * ipsec_updatereplay() updates replay bitmap. This must be called after 1716 * ICV verification (it also performs replay check, which is usually done 1717 * beforehand). 1718 * 0 (zero) is returned if packet disallowed, 1 if packet permitted. 1719 * 1720 * based on RFC 2401. 1721 */ 1722int 1723ipsec_chkreplay(seq, sav) 1724 u_int32_t seq; 1725 struct secasvar *sav; 1726{ 1727 const struct secreplay *replay; 1728 u_int32_t diff; 1729 int fr; 1730 u_int32_t wsizeb; /* constant: bits of window size */ 1731 int frlast; /* constant: last frame */ 1732 1733 IPSEC_ASSERT(sav != NULL, ("Null SA")); 1734 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state")); 1735 1736 replay = sav->replay; 1737 1738 if (replay->wsize == 0) 1739 return 1; /* no need to check replay. */ 1740 1741 /* constant */ 1742 frlast = replay->wsize - 1; 1743 wsizeb = replay->wsize << 3; 1744 1745 /* sequence number of 0 is invalid */ 1746 if (seq == 0) 1747 return 0; 1748 1749 /* first time is always okay */ 1750 if (replay->count == 0) 1751 return 1; 1752 1753 if (seq > replay->lastseq) { 1754 /* larger sequences are okay */ 1755 return 1; 1756 } else { 1757 /* seq is equal or less than lastseq. */ 1758 diff = replay->lastseq - seq; 1759 1760 /* over range to check, i.e. too old or wrapped */ 1761 if (diff >= wsizeb) 1762 return 0; 1763 1764 fr = frlast - diff / 8; 1765 1766 /* this packet already seen ? */ 1767 if ((replay->bitmap)[fr] & (1 << (diff % 8))) 1768 return 0; 1769 1770 /* out of order but good */ 1771 return 1; 1772 } 1773} 1774 1775/* 1776 * check replay counter whether to update or not. 1777 * OUT: 0: OK 1778 * 1: NG 1779 */ 1780int 1781ipsec_updatereplay(seq, sav) 1782 u_int32_t seq; 1783 struct secasvar *sav; 1784{ 1785 INIT_VNET_IPSEC(curvnet); 1786 struct secreplay *replay; 1787 u_int32_t diff; 1788 int fr; 1789 u_int32_t wsizeb; /* constant: bits of window size */ 1790 int frlast; /* constant: last frame */ 1791 1792 IPSEC_ASSERT(sav != NULL, ("Null SA")); 1793 IPSEC_ASSERT(sav->replay != NULL, ("Null replay state")); 1794 1795 replay = sav->replay; 1796 1797 if (replay->wsize == 0) 1798 goto ok; /* no need to check replay. */ 1799 1800 /* constant */ 1801 frlast = replay->wsize - 1; 1802 wsizeb = replay->wsize << 3; 1803 1804 /* sequence number of 0 is invalid */ 1805 if (seq == 0) 1806 return 1; 1807 1808 /* first time */ 1809 if (replay->count == 0) { 1810 replay->lastseq = seq; 1811 bzero(replay->bitmap, replay->wsize); 1812 (replay->bitmap)[frlast] = 1; 1813 goto ok; 1814 } 1815 1816 if (seq > replay->lastseq) { 1817 /* seq is larger than lastseq. */ 1818 diff = seq - replay->lastseq; 1819 1820 /* new larger sequence number */ 1821 if (diff < wsizeb) { 1822 /* In window */ 1823 /* set bit for this packet */ 1824 vshiftl(replay->bitmap, diff, replay->wsize); 1825 (replay->bitmap)[frlast] |= 1; 1826 } else { 1827 /* this packet has a "way larger" */ 1828 bzero(replay->bitmap, replay->wsize); 1829 (replay->bitmap)[frlast] = 1; 1830 } 1831 replay->lastseq = seq; 1832 1833 /* larger is good */ 1834 } else { 1835 /* seq is equal or less than lastseq. */ 1836 diff = replay->lastseq - seq; 1837 1838 /* over range to check, i.e. too old or wrapped */ 1839 if (diff >= wsizeb) 1840 return 1; 1841 1842 fr = frlast - diff / 8; 1843 1844 /* this packet already seen ? */ 1845 if ((replay->bitmap)[fr] & (1 << (diff % 8))) 1846 return 1; 1847 1848 /* mark as seen */ 1849 (replay->bitmap)[fr] |= (1 << (diff % 8)); 1850 1851 /* out of order but good */ 1852 } 1853 1854ok: 1855 if (replay->count == ~0) { 1856 1857 /* set overflow flag */ 1858 replay->overflow++; 1859 1860 /* don't increment, no more packets accepted */ 1861 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) 1862 return 1; 1863 1864 ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n", 1865 __func__, replay->overflow, ipsec_logsastr(sav))); 1866 } 1867 1868 replay->count++; 1869 1870 return 0; 1871} 1872 1873/* 1874 * shift variable length buffer to left. 1875 * IN: bitmap: pointer to the buffer 1876 * nbit: the number of to shift. 1877 * wsize: buffer size (bytes). 1878 */ 1879static void 1880vshiftl(bitmap, nbit, wsize) 1881 unsigned char *bitmap; 1882 int nbit, wsize; 1883{ 1884 int s, j, i; 1885 unsigned char over; 1886 1887 for (j = 0; j < nbit; j += 8) { 1888 s = (nbit - j < 8) ? (nbit - j): 8; 1889 bitmap[0] <<= s; 1890 for (i = 1; i < wsize; i++) { 1891 over = (bitmap[i] >> (8 - s)); 1892 bitmap[i] <<= s; 1893 bitmap[i-1] |= over; 1894 } 1895 } 1896 1897 return; 1898} 1899 1900/* Return a printable string for the IPv4 address. */ 1901static char * 1902inet_ntoa4(struct in_addr ina) 1903{ 1904 static char buf[4][4 * sizeof "123" + 4]; 1905 unsigned char *ucp = (unsigned char *) &ina; 1906 static int i = 3; 1907 1908 /* XXX-BZ returns static buffer. */ 1909 i = (i + 1) % 4; 1910 sprintf(buf[i], "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff, 1911 ucp[2] & 0xff, ucp[3] & 0xff); 1912 return (buf[i]); 1913} 1914 1915/* Return a printable string for the address. */ 1916char * 1917ipsec_address(union sockaddr_union* sa) 1918{ 1919#ifdef INET6 1920 char ip6buf[INET6_ADDRSTRLEN]; 1921#endif 1922 switch (sa->sa.sa_family) { 1923#ifdef INET 1924 case AF_INET: 1925 return inet_ntoa4(sa->sin.sin_addr); 1926#endif /* INET */ 1927 1928#ifdef INET6 1929 case AF_INET6: 1930 return ip6_sprintf(ip6buf, &sa->sin6.sin6_addr); 1931#endif /* INET6 */ 1932 1933 default: 1934 return "(unknown address family)"; 1935 } 1936} 1937 1938const char * 1939ipsec_logsastr(sav) 1940 struct secasvar *sav; 1941{ 1942 static char buf[256]; 1943 char *p; 1944 struct secasindex *saidx = &sav->sah->saidx; 1945 1946 IPSEC_ASSERT(saidx->src.sa.sa_family == saidx->dst.sa.sa_family, 1947 ("address family mismatch")); 1948 1949 p = buf; 1950 snprintf(buf, sizeof(buf), "SA(SPI=%u ", (u_int32_t)ntohl(sav->spi)); 1951 while (p && *p) 1952 p++; 1953 /* NB: only use ipsec_address on one address at a time */ 1954 snprintf(p, sizeof (buf) - (p - buf), "src=%s ", 1955 ipsec_address(&saidx->src)); 1956 while (p && *p) 1957 p++; 1958 snprintf(p, sizeof (buf) - (p - buf), "dst=%s)", 1959 ipsec_address(&saidx->dst)); 1960 1961 return buf; 1962} 1963 1964void 1965ipsec_dumpmbuf(m) 1966 struct mbuf *m; 1967{ 1968 int totlen; 1969 int i; 1970 u_char *p; 1971 1972 totlen = 0; 1973 printf("---\n"); 1974 while (m) { 1975 p = mtod(m, u_char *); 1976 for (i = 0; i < m->m_len; i++) { 1977 printf("%02x ", p[i]); 1978 totlen++; 1979 if (totlen % 16 == 0) 1980 printf("\n"); 1981 } 1982 m = m->m_next; 1983 } 1984 if (totlen % 16 != 0) 1985 printf("\n"); 1986 printf("---\n"); 1987} 1988 1989static void 1990ipsec_attach(void) 1991{ 1992 SECPOLICY_LOCK_INIT(&V_ip4_def_policy); 1993 V_ip4_def_policy.refcnt = 1; /* NB: disallow free */ 1994} 1995SYSINIT(ipsec, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, ipsec_attach, NULL); 1996 1997 1998/* XXX this stuff doesn't belong here... */ 1999 2000static struct xformsw* xforms = NULL; 2001 2002/* 2003 * Register a transform; typically at system startup. 2004 */ 2005void 2006xform_register(struct xformsw* xsp) 2007{ 2008 xsp->xf_next = xforms; 2009 xforms = xsp; 2010} 2011 2012/* 2013 * Initialize transform support in an sav. 2014 */ 2015int 2016xform_init(struct secasvar *sav, int xftype) 2017{ 2018 struct xformsw *xsp; 2019 2020 if (sav->tdb_xform != NULL) /* previously initialized */ 2021 return 0; 2022 for (xsp = xforms; xsp; xsp = xsp->xf_next) 2023 if (xsp->xf_type == xftype) 2024 return (*xsp->xf_init)(sav, xsp); 2025 return EINVAL; 2026} 2027