pfkey.c revision 122107
1/* $KAME: pfkey.c,v 1.46 2003/08/26 03:37:06 itojun Exp $ */ 2 3/* 4 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD: head/lib/libipsec/pfkey.c 122107 2003-11-05 09:41:23Z ume $"); 34 35#include <sys/types.h> 36#include <sys/param.h> 37#include <sys/socket.h> 38#include <net/pfkeyv2.h> 39#include <netkey/key_var.h> 40#include <netinet/in.h> 41#include <netinet6/ipsec.h> 42 43#include <stdlib.h> 44#include <unistd.h> 45#include <string.h> 46#include <errno.h> 47 48#include "ipsec_strerror.h" 49#include "libpfkey.h" 50 51#define CALLOC(size, cast) (cast)calloc(1, (size)) 52 53static int findsupportedmap(int); 54static int setsupportedmap(struct sadb_supported *); 55static struct sadb_alg *findsupportedalg(u_int, u_int); 56static int pfkey_send_x1(int, u_int, u_int, u_int, struct sockaddr *, 57 struct sockaddr *, u_int32_t, u_int32_t, u_int, caddr_t, 58 u_int, u_int, u_int, u_int, u_int, u_int32_t, u_int32_t, 59 u_int32_t, u_int32_t, u_int32_t); 60static int pfkey_send_x2(int, u_int, u_int, u_int, 61 struct sockaddr *, struct sockaddr *, u_int32_t); 62static int pfkey_send_x3(int, u_int, u_int); 63static int pfkey_send_x4(int, u_int, struct sockaddr *, u_int, 64 struct sockaddr *, u_int, u_int, u_int64_t, u_int64_t, 65 char *, int, u_int32_t); 66static int pfkey_send_x5(int, u_int, u_int32_t); 67 68static caddr_t pfkey_setsadbmsg(caddr_t, caddr_t, u_int, u_int, 69 u_int, u_int32_t, pid_t); 70static caddr_t pfkey_setsadbsa(caddr_t, caddr_t, u_int32_t, u_int, 71 u_int, u_int, u_int32_t); 72static caddr_t pfkey_setsadbaddr(caddr_t, caddr_t, u_int, 73 struct sockaddr *, u_int, u_int); 74static caddr_t pfkey_setsadbkey(caddr_t, caddr_t, u_int, caddr_t, u_int); 75static caddr_t pfkey_setsadblifetime(caddr_t, caddr_t, u_int, u_int32_t, 76 u_int32_t, u_int32_t, u_int32_t); 77static caddr_t pfkey_setsadbxsa2(caddr_t, caddr_t, u_int32_t, u_int32_t); 78 79/* 80 * make and search supported algorithm structure. 81 */ 82static struct sadb_supported *ipsec_supported[] = { NULL, NULL, NULL, }; 83 84static int supported_map[] = { 85 SADB_SATYPE_AH, 86 SADB_SATYPE_ESP, 87 SADB_X_SATYPE_IPCOMP, 88}; 89 90static int 91findsupportedmap(satype) 92 int satype; 93{ 94 int i; 95 96 for (i = 0; i < sizeof(supported_map)/sizeof(supported_map[0]); i++) 97 if (supported_map[i] == satype) 98 return i; 99 return -1; 100} 101 102static struct sadb_alg * 103findsupportedalg(satype, alg_id) 104 u_int satype, alg_id; 105{ 106 int algno; 107 int tlen; 108 caddr_t p; 109 110 /* validity check */ 111 algno = findsupportedmap(satype); 112 if (algno == -1) { 113 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 114 return NULL; 115 } 116 if (ipsec_supported[algno] == NULL) { 117 __ipsec_errcode = EIPSEC_DO_GET_SUPP_LIST; 118 return NULL; 119 } 120 121 tlen = ipsec_supported[algno]->sadb_supported_len 122 - sizeof(struct sadb_supported); 123 p = (caddr_t)(ipsec_supported[algno] + 1); 124 while (tlen > 0) { 125 if (tlen < sizeof(struct sadb_alg)) { 126 /* invalid format */ 127 break; 128 } 129 if (((struct sadb_alg *)p)->sadb_alg_id == alg_id) 130 return (struct sadb_alg *)p; 131 132 tlen -= sizeof(struct sadb_alg); 133 p += sizeof(struct sadb_alg); 134 } 135 136 __ipsec_errcode = EIPSEC_NOT_SUPPORTED; 137 return NULL; 138} 139 140static int 141setsupportedmap(sup) 142 struct sadb_supported *sup; 143{ 144 struct sadb_supported **ipsup; 145 146 switch (sup->sadb_supported_exttype) { 147 case SADB_EXT_SUPPORTED_AUTH: 148 ipsup = &ipsec_supported[findsupportedmap(SADB_SATYPE_AH)]; 149 break; 150 case SADB_EXT_SUPPORTED_ENCRYPT: 151 ipsup = &ipsec_supported[findsupportedmap(SADB_SATYPE_ESP)]; 152 break; 153 default: 154 __ipsec_errcode = EIPSEC_INVAL_SATYPE; 155 return -1; 156 } 157 158 if (*ipsup) 159 free(*ipsup); 160 161 *ipsup = malloc(sup->sadb_supported_len); 162 if (!*ipsup) { 163 __ipsec_set_strerror(strerror(errno)); 164 return -1; 165 } 166 memcpy(*ipsup, sup, sup->sadb_supported_len); 167 168 return 0; 169} 170 171/* 172 * check key length against algorithm specified. 173 * This function is called with SADB_EXT_SUPPORTED_{AUTH,ENCRYPT} as the 174 * augument, and only calls to ipsec_check_keylen2(); 175 * keylen is the unit of bit. 176 * OUT: 177 * -1: invalid. 178 * 0: valid. 179 */ 180int 181ipsec_check_keylen(supported, alg_id, keylen) 182 u_int supported; 183 u_int alg_id; 184 u_int keylen; 185{ 186 int satype; 187 188 /* validity check */ 189 switch (supported) { 190 case SADB_EXT_SUPPORTED_AUTH: 191 satype = SADB_SATYPE_AH; 192 break; 193 case SADB_EXT_SUPPORTED_ENCRYPT: 194 satype = SADB_SATYPE_ESP; 195 break; 196 default: 197 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 198 return -1; 199 } 200 201 return ipsec_check_keylen2(satype, alg_id, keylen); 202} 203 204/* 205 * check key length against algorithm specified. 206 * satype is one of satype defined at pfkeyv2.h. 207 * keylen is the unit of bit. 208 * OUT: 209 * -1: invalid. 210 * 0: valid. 211 */ 212int 213ipsec_check_keylen2(satype, alg_id, keylen) 214 u_int satype; 215 u_int alg_id; 216 u_int keylen; 217{ 218 struct sadb_alg *alg; 219 220 alg = findsupportedalg(satype, alg_id); 221 if (!alg) 222 return -1; 223 224 if (keylen < alg->sadb_alg_minbits || keylen > alg->sadb_alg_maxbits) { 225 __ipsec_errcode = EIPSEC_INVAL_KEYLEN; 226 return -1; 227 } 228 229 __ipsec_errcode = EIPSEC_NO_ERROR; 230 return 0; 231} 232 233/* 234 * get max/min key length against algorithm specified. 235 * satype is one of satype defined at pfkeyv2.h. 236 * keylen is the unit of bit. 237 * OUT: 238 * -1: invalid. 239 * 0: valid. 240 */ 241int 242ipsec_get_keylen(supported, alg_id, alg0) 243 u_int supported, alg_id; 244 struct sadb_alg *alg0; 245{ 246 struct sadb_alg *alg; 247 u_int satype; 248 249 /* validity check */ 250 if (!alg0) { 251 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 252 return -1; 253 } 254 255 switch (supported) { 256 case SADB_EXT_SUPPORTED_AUTH: 257 satype = SADB_SATYPE_AH; 258 break; 259 case SADB_EXT_SUPPORTED_ENCRYPT: 260 satype = SADB_SATYPE_ESP; 261 break; 262 default: 263 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 264 return -1; 265 } 266 267 alg = findsupportedalg(satype, alg_id); 268 if (!alg) 269 return -1; 270 271 memcpy(alg0, alg, sizeof(*alg0)); 272 273 __ipsec_errcode = EIPSEC_NO_ERROR; 274 return 0; 275} 276 277/* 278 * set the rate for SOFT lifetime against HARD one. 279 * If rate is more than 100 or equal to zero, then set to 100. 280 */ 281static u_int soft_lifetime_allocations_rate = PFKEY_SOFT_LIFETIME_RATE; 282static u_int soft_lifetime_bytes_rate = PFKEY_SOFT_LIFETIME_RATE; 283static u_int soft_lifetime_addtime_rate = PFKEY_SOFT_LIFETIME_RATE; 284static u_int soft_lifetime_usetime_rate = PFKEY_SOFT_LIFETIME_RATE; 285 286u_int 287pfkey_set_softrate(type, rate) 288 u_int type, rate; 289{ 290 __ipsec_errcode = EIPSEC_NO_ERROR; 291 292 if (rate > 100 || rate == 0) 293 rate = 100; 294 295 switch (type) { 296 case SADB_X_LIFETIME_ALLOCATIONS: 297 soft_lifetime_allocations_rate = rate; 298 return 0; 299 case SADB_X_LIFETIME_BYTES: 300 soft_lifetime_bytes_rate = rate; 301 return 0; 302 case SADB_X_LIFETIME_ADDTIME: 303 soft_lifetime_addtime_rate = rate; 304 return 0; 305 case SADB_X_LIFETIME_USETIME: 306 soft_lifetime_usetime_rate = rate; 307 return 0; 308 } 309 310 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 311 return 1; 312} 313 314/* 315 * get current rate for SOFT lifetime against HARD one. 316 * ATTENTION: ~0 is returned if invalid type was passed. 317 */ 318u_int 319pfkey_get_softrate(type) 320 u_int type; 321{ 322 switch (type) { 323 case SADB_X_LIFETIME_ALLOCATIONS: 324 return soft_lifetime_allocations_rate; 325 case SADB_X_LIFETIME_BYTES: 326 return soft_lifetime_bytes_rate; 327 case SADB_X_LIFETIME_ADDTIME: 328 return soft_lifetime_addtime_rate; 329 case SADB_X_LIFETIME_USETIME: 330 return soft_lifetime_usetime_rate; 331 } 332 333 return ~0; 334} 335 336/* 337 * sending SADB_GETSPI message to the kernel. 338 * OUT: 339 * positive: success and return length sent. 340 * -1 : error occured, and set errno. 341 */ 342int 343pfkey_send_getspi(so, satype, mode, src, dst, min, max, reqid, seq) 344 int so; 345 u_int satype, mode; 346 struct sockaddr *src, *dst; 347 u_int32_t min, max, reqid, seq; 348{ 349 struct sadb_msg *newmsg; 350 caddr_t ep; 351 int len; 352 int need_spirange = 0; 353 caddr_t p; 354 int plen; 355 356 /* validity check */ 357 if (src == NULL || dst == NULL) { 358 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 359 return -1; 360 } 361 if (src->sa_family != dst->sa_family) { 362 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH; 363 return -1; 364 } 365 if (min > max || (min > 0 && min <= 255)) { 366 __ipsec_errcode = EIPSEC_INVAL_SPI; 367 return -1; 368 } 369 switch (src->sa_family) { 370 case AF_INET: 371 plen = sizeof(struct in_addr) << 3; 372 break; 373 case AF_INET6: 374 plen = sizeof(struct in6_addr) << 3; 375 break; 376 default: 377 __ipsec_errcode = EIPSEC_INVAL_FAMILY; 378 return -1; 379 } 380 381 /* create new sadb_msg to send. */ 382 len = sizeof(struct sadb_msg) 383 + sizeof(struct sadb_x_sa2) 384 + sizeof(struct sadb_address) 385 + PFKEY_ALIGN8(src->sa_len) 386 + sizeof(struct sadb_address) 387 + PFKEY_ALIGN8(dst->sa_len); 388 389 if (min > 255 && max < ~0) { 390 need_spirange++; 391 len += sizeof(struct sadb_spirange); 392 } 393 394 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) { 395 __ipsec_set_strerror(strerror(errno)); 396 return -1; 397 } 398 ep = ((caddr_t)newmsg) + len; 399 400 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, SADB_GETSPI, 401 len, satype, seq, getpid()); 402 if (!p) { 403 free(newmsg); 404 return -1; 405 } 406 407 p = pfkey_setsadbxsa2(p, ep, mode, reqid); 408 if (!p) { 409 free(newmsg); 410 return -1; 411 } 412 413 /* set sadb_address for source */ 414 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen, 415 IPSEC_ULPROTO_ANY); 416 if (!p) { 417 free(newmsg); 418 return -1; 419 } 420 421 /* set sadb_address for destination */ 422 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen, 423 IPSEC_ULPROTO_ANY); 424 if (!p) { 425 free(newmsg); 426 return -1; 427 } 428 429 /* proccessing spi range */ 430 if (need_spirange) { 431 struct sadb_spirange spirange; 432 433 if (p + sizeof(spirange) > ep) { 434 free(newmsg); 435 return -1; 436 } 437 438 memset(&spirange, 0, sizeof(spirange)); 439 spirange.sadb_spirange_len = PFKEY_UNIT64(sizeof(spirange)); 440 spirange.sadb_spirange_exttype = SADB_EXT_SPIRANGE; 441 spirange.sadb_spirange_min = min; 442 spirange.sadb_spirange_max = max; 443 444 memcpy(p, &spirange, sizeof(spirange)); 445 446 p += sizeof(spirange); 447 } 448 if (p != ep) { 449 free(newmsg); 450 return -1; 451 } 452 453 /* send message */ 454 len = pfkey_send(so, newmsg, len); 455 free(newmsg); 456 457 if (len < 0) 458 return -1; 459 460 __ipsec_errcode = EIPSEC_NO_ERROR; 461 return len; 462} 463 464/* 465 * sending SADB_UPDATE message to the kernel. 466 * The length of key material is a_keylen + e_keylen. 467 * OUT: 468 * positive: success and return length sent. 469 * -1 : error occured, and set errno. 470 */ 471int 472pfkey_send_update(so, satype, mode, src, dst, spi, reqid, wsize, 473 keymat, e_type, e_keylen, a_type, a_keylen, flags, 474 l_alloc, l_bytes, l_addtime, l_usetime, seq) 475 int so; 476 u_int satype, mode, wsize; 477 struct sockaddr *src, *dst; 478 u_int32_t spi, reqid; 479 caddr_t keymat; 480 u_int e_type, e_keylen, a_type, a_keylen, flags; 481 u_int32_t l_alloc; 482 u_int64_t l_bytes, l_addtime, l_usetime; 483 u_int32_t seq; 484{ 485 int len; 486 if ((len = pfkey_send_x1(so, SADB_UPDATE, satype, mode, src, dst, spi, 487 reqid, wsize, 488 keymat, e_type, e_keylen, a_type, a_keylen, flags, 489 l_alloc, l_bytes, l_addtime, l_usetime, seq)) < 0) 490 return -1; 491 492 return len; 493} 494 495/* 496 * sending SADB_ADD message to the kernel. 497 * The length of key material is a_keylen + e_keylen. 498 * OUT: 499 * positive: success and return length sent. 500 * -1 : error occured, and set errno. 501 */ 502int 503pfkey_send_add(so, satype, mode, src, dst, spi, reqid, wsize, 504 keymat, e_type, e_keylen, a_type, a_keylen, flags, 505 l_alloc, l_bytes, l_addtime, l_usetime, seq) 506 int so; 507 u_int satype, mode, wsize; 508 struct sockaddr *src, *dst; 509 u_int32_t spi, reqid; 510 caddr_t keymat; 511 u_int e_type, e_keylen, a_type, a_keylen, flags; 512 u_int32_t l_alloc; 513 u_int64_t l_bytes, l_addtime, l_usetime; 514 u_int32_t seq; 515{ 516 int len; 517 if ((len = pfkey_send_x1(so, SADB_ADD, satype, mode, src, dst, spi, 518 reqid, wsize, 519 keymat, e_type, e_keylen, a_type, a_keylen, flags, 520 l_alloc, l_bytes, l_addtime, l_usetime, seq)) < 0) 521 return -1; 522 523 return len; 524} 525 526/* 527 * sending SADB_DELETE message to the kernel. 528 * OUT: 529 * positive: success and return length sent. 530 * -1 : error occured, and set errno. 531 */ 532int 533pfkey_send_delete(so, satype, mode, src, dst, spi) 534 int so; 535 u_int satype, mode; 536 struct sockaddr *src, *dst; 537 u_int32_t spi; 538{ 539 int len; 540 if ((len = pfkey_send_x2(so, SADB_DELETE, satype, mode, src, dst, spi)) < 0) 541 return -1; 542 543 return len; 544} 545 546/* 547 * sending SADB_DELETE without spi to the kernel. This is 548 * the "delete all" request (an extension also present in 549 * Solaris). 550 * 551 * OUT: 552 * positive: success and return length sent 553 * -1 : error occured, and set errno 554 */ 555int 556pfkey_send_delete_all(so, satype, mode, src, dst) 557 int so; 558 u_int satype, mode; 559 struct sockaddr *src, *dst; 560{ 561 struct sadb_msg *newmsg; 562 int len; 563 caddr_t p; 564 int plen; 565 caddr_t ep; 566 567 /* validity check */ 568 if (src == NULL || dst == NULL) { 569 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 570 return -1; 571 } 572 if (src->sa_family != dst->sa_family) { 573 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH; 574 return -1; 575 } 576 switch (src->sa_family) { 577 case AF_INET: 578 plen = sizeof(struct in_addr) << 3; 579 break; 580 case AF_INET6: 581 plen = sizeof(struct in6_addr) << 3; 582 break; 583 default: 584 __ipsec_errcode = EIPSEC_INVAL_FAMILY; 585 return -1; 586 } 587 588 /* create new sadb_msg to reply. */ 589 len = sizeof(struct sadb_msg) 590 + sizeof(struct sadb_address) 591 + PFKEY_ALIGN8(src->sa_len) 592 + sizeof(struct sadb_address) 593 + PFKEY_ALIGN8(dst->sa_len); 594 595 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) { 596 __ipsec_set_strerror(strerror(errno)); 597 return -1; 598 } 599 ep = ((caddr_t)newmsg) + len; 600 601 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, SADB_DELETE, len, satype, 0, 602 getpid()); 603 if (!p) { 604 free(newmsg); 605 return -1; 606 } 607 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen, 608 IPSEC_ULPROTO_ANY); 609 if (!p) { 610 free(newmsg); 611 return -1; 612 } 613 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen, 614 IPSEC_ULPROTO_ANY); 615 if (!p || p != ep) { 616 free(newmsg); 617 return -1; 618 } 619 620 /* send message */ 621 len = pfkey_send(so, newmsg, len); 622 free(newmsg); 623 624 if (len < 0) 625 return -1; 626 627 __ipsec_errcode = EIPSEC_NO_ERROR; 628 return len; 629} 630 631/* 632 * sending SADB_GET message to the kernel. 633 * OUT: 634 * positive: success and return length sent. 635 * -1 : error occured, and set errno. 636 */ 637int 638pfkey_send_get(so, satype, mode, src, dst, spi) 639 int so; 640 u_int satype, mode; 641 struct sockaddr *src, *dst; 642 u_int32_t spi; 643{ 644 int len; 645 if ((len = pfkey_send_x2(so, SADB_GET, satype, mode, src, dst, spi)) < 0) 646 return -1; 647 648 return len; 649} 650 651/* 652 * sending SADB_REGISTER message to the kernel. 653 * OUT: 654 * positive: success and return length sent. 655 * -1 : error occured, and set errno. 656 */ 657int 658pfkey_send_register(so, satype) 659 int so; 660 u_int satype; 661{ 662 int len, algno; 663 664 if (satype == PF_UNSPEC) { 665 for (algno = 0; 666 algno < sizeof(supported_map)/sizeof(supported_map[0]); 667 algno++) { 668 if (ipsec_supported[algno]) { 669 free(ipsec_supported[algno]); 670 ipsec_supported[algno] = NULL; 671 } 672 } 673 } else { 674 algno = findsupportedmap(satype); 675 if (algno == -1) { 676 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 677 return -1; 678 } 679 680 if (ipsec_supported[algno]) { 681 free(ipsec_supported[algno]); 682 ipsec_supported[algno] = NULL; 683 } 684 } 685 686 if ((len = pfkey_send_x3(so, SADB_REGISTER, satype)) < 0) 687 return -1; 688 689 return len; 690} 691 692/* 693 * receiving SADB_REGISTER message from the kernel, and copy buffer for 694 * sadb_supported returned into ipsec_supported. 695 * OUT: 696 * 0: success and return length sent. 697 * -1: error occured, and set errno. 698 */ 699int 700pfkey_recv_register(so) 701 int so; 702{ 703 pid_t pid = getpid(); 704 struct sadb_msg *newmsg; 705 int error = -1; 706 707 /* receive message */ 708 for (;;) { 709 if ((newmsg = pfkey_recv(so)) == NULL) 710 return -1; 711 if (newmsg->sadb_msg_type == SADB_REGISTER && 712 newmsg->sadb_msg_pid == pid) 713 break; 714 free(newmsg); 715 } 716 717 /* check and fix */ 718 newmsg->sadb_msg_len = PFKEY_UNUNIT64(newmsg->sadb_msg_len); 719 720 error = pfkey_set_supported(newmsg, newmsg->sadb_msg_len); 721 free(newmsg); 722 723 if (error == 0) 724 __ipsec_errcode = EIPSEC_NO_ERROR; 725 726 return error; 727} 728 729/* 730 * receiving SADB_REGISTER message from the kernel, and copy buffer for 731 * sadb_supported returned into ipsec_supported. 732 * NOTE: sadb_msg_len must be host order. 733 * IN: 734 * tlen: msg length, it's to makeing sure. 735 * OUT: 736 * 0: success and return length sent. 737 * -1: error occured, and set errno. 738 */ 739int 740pfkey_set_supported(msg, tlen) 741 struct sadb_msg *msg; 742 int tlen; 743{ 744 struct sadb_supported *sup; 745 caddr_t p; 746 caddr_t ep; 747 748 /* validity */ 749 if (msg->sadb_msg_len != tlen) { 750 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 751 return -1; 752 } 753 754 p = (caddr_t)msg; 755 ep = p + tlen; 756 757 p += sizeof(struct sadb_msg); 758 759 while (p < ep) { 760 sup = (struct sadb_supported *)p; 761 if (ep < p + sizeof(*sup) || 762 PFKEY_EXTLEN(sup) < sizeof(*sup) || 763 ep < p + sup->sadb_supported_len) { 764 /* invalid format */ 765 break; 766 } 767 768 switch (sup->sadb_supported_exttype) { 769 case SADB_EXT_SUPPORTED_AUTH: 770 case SADB_EXT_SUPPORTED_ENCRYPT: 771 break; 772 default: 773 __ipsec_errcode = EIPSEC_INVAL_SATYPE; 774 return -1; 775 } 776 777 /* fixed length */ 778 sup->sadb_supported_len = PFKEY_EXTLEN(sup); 779 780 /* set supported map */ 781 if (setsupportedmap(sup) != 0) 782 return -1; 783 784 p += sup->sadb_supported_len; 785 } 786 787 if (p != ep) { 788 __ipsec_errcode = EIPSEC_INVAL_SATYPE; 789 return -1; 790 } 791 792 __ipsec_errcode = EIPSEC_NO_ERROR; 793 794 return 0; 795} 796 797/* 798 * sending SADB_FLUSH message to the kernel. 799 * OUT: 800 * positive: success and return length sent. 801 * -1 : error occured, and set errno. 802 */ 803int 804pfkey_send_flush(so, satype) 805 int so; 806 u_int satype; 807{ 808 int len; 809 810 if ((len = pfkey_send_x3(so, SADB_FLUSH, satype)) < 0) 811 return -1; 812 813 return len; 814} 815 816/* 817 * sending SADB_DUMP message to the kernel. 818 * OUT: 819 * positive: success and return length sent. 820 * -1 : error occured, and set errno. 821 */ 822int 823pfkey_send_dump(so, satype) 824 int so; 825 u_int satype; 826{ 827 int len; 828 829 if ((len = pfkey_send_x3(so, SADB_DUMP, satype)) < 0) 830 return -1; 831 832 return len; 833} 834 835/* 836 * sending SADB_X_PROMISC message to the kernel. 837 * NOTE that this function handles promisc mode toggle only. 838 * IN: 839 * flag: set promisc off if zero, set promisc on if non-zero. 840 * OUT: 841 * positive: success and return length sent. 842 * -1 : error occured, and set errno. 843 * 0 : error occured, and set errno. 844 * others: a pointer to new allocated buffer in which supported 845 * algorithms is. 846 */ 847int 848pfkey_send_promisc_toggle(so, flag) 849 int so; 850 int flag; 851{ 852 int len; 853 854 if ((len = pfkey_send_x3(so, SADB_X_PROMISC, (flag ? 1 : 0))) < 0) 855 return -1; 856 857 return len; 858} 859 860/* 861 * sending SADB_X_SPDADD message to the kernel. 862 * OUT: 863 * positive: success and return length sent. 864 * -1 : error occured, and set errno. 865 */ 866int 867pfkey_send_spdadd(so, src, prefs, dst, prefd, proto, policy, policylen, seq) 868 int so; 869 struct sockaddr *src, *dst; 870 u_int prefs, prefd, proto; 871 caddr_t policy; 872 int policylen; 873 u_int32_t seq; 874{ 875 int len; 876 877 if ((len = pfkey_send_x4(so, SADB_X_SPDADD, 878 src, prefs, dst, prefd, proto, 879 0, 0, 880 policy, policylen, seq)) < 0) 881 return -1; 882 883 return len; 884} 885 886/* 887 * sending SADB_X_SPDADD message to the kernel. 888 * OUT: 889 * positive: success and return length sent. 890 * -1 : error occured, and set errno. 891 */ 892int 893pfkey_send_spdadd2(so, src, prefs, dst, prefd, proto, ltime, vtime, 894 policy, policylen, seq) 895 int so; 896 struct sockaddr *src, *dst; 897 u_int prefs, prefd, proto; 898 u_int64_t ltime, vtime; 899 caddr_t policy; 900 int policylen; 901 u_int32_t seq; 902{ 903 int len; 904 905 if ((len = pfkey_send_x4(so, SADB_X_SPDADD, 906 src, prefs, dst, prefd, proto, 907 ltime, vtime, 908 policy, policylen, seq)) < 0) 909 return -1; 910 911 return len; 912} 913 914/* 915 * sending SADB_X_SPDUPDATE message to the kernel. 916 * OUT: 917 * positive: success and return length sent. 918 * -1 : error occured, and set errno. 919 */ 920int 921pfkey_send_spdupdate(so, src, prefs, dst, prefd, proto, policy, policylen, seq) 922 int so; 923 struct sockaddr *src, *dst; 924 u_int prefs, prefd, proto; 925 caddr_t policy; 926 int policylen; 927 u_int32_t seq; 928{ 929 int len; 930 931 if ((len = pfkey_send_x4(so, SADB_X_SPDUPDATE, 932 src, prefs, dst, prefd, proto, 933 0, 0, 934 policy, policylen, seq)) < 0) 935 return -1; 936 937 return len; 938} 939 940/* 941 * sending SADB_X_SPDUPDATE message to the kernel. 942 * OUT: 943 * positive: success and return length sent. 944 * -1 : error occured, and set errno. 945 */ 946int 947pfkey_send_spdupdate2(so, src, prefs, dst, prefd, proto, ltime, vtime, 948 policy, policylen, seq) 949 int so; 950 struct sockaddr *src, *dst; 951 u_int prefs, prefd, proto; 952 u_int64_t ltime, vtime; 953 caddr_t policy; 954 int policylen; 955 u_int32_t seq; 956{ 957 int len; 958 959 if ((len = pfkey_send_x4(so, SADB_X_SPDUPDATE, 960 src, prefs, dst, prefd, proto, 961 ltime, vtime, 962 policy, policylen, seq)) < 0) 963 return -1; 964 965 return len; 966} 967 968/* 969 * sending SADB_X_SPDDELETE message to the kernel. 970 * OUT: 971 * positive: success and return length sent. 972 * -1 : error occured, and set errno. 973 */ 974int 975pfkey_send_spddelete(so, src, prefs, dst, prefd, proto, policy, policylen, seq) 976 int so; 977 struct sockaddr *src, *dst; 978 u_int prefs, prefd, proto; 979 caddr_t policy; 980 int policylen; 981 u_int32_t seq; 982{ 983 int len; 984 985 if (policylen != sizeof(struct sadb_x_policy)) { 986 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 987 return -1; 988 } 989 990 if ((len = pfkey_send_x4(so, SADB_X_SPDDELETE, 991 src, prefs, dst, prefd, proto, 992 0, 0, 993 policy, policylen, seq)) < 0) 994 return -1; 995 996 return len; 997} 998 999/* 1000 * sending SADB_X_SPDDELETE message to the kernel. 1001 * OUT: 1002 * positive: success and return length sent. 1003 * -1 : error occured, and set errno. 1004 */ 1005int 1006pfkey_send_spddelete2(so, spid) 1007 int so; 1008 u_int32_t spid; 1009{ 1010 int len; 1011 1012 if ((len = pfkey_send_x5(so, SADB_X_SPDDELETE2, spid)) < 0) 1013 return -1; 1014 1015 return len; 1016} 1017 1018/* 1019 * sending SADB_X_SPDGET message to the kernel. 1020 * OUT: 1021 * positive: success and return length sent. 1022 * -1 : error occured, and set errno. 1023 */ 1024int 1025pfkey_send_spdget(so, spid) 1026 int so; 1027 u_int32_t spid; 1028{ 1029 int len; 1030 1031 if ((len = pfkey_send_x5(so, SADB_X_SPDGET, spid)) < 0) 1032 return -1; 1033 1034 return len; 1035} 1036 1037/* 1038 * sending SADB_X_SPDSETIDX message to the kernel. 1039 * OUT: 1040 * positive: success and return length sent. 1041 * -1 : error occured, and set errno. 1042 */ 1043int 1044pfkey_send_spdsetidx(so, src, prefs, dst, prefd, proto, policy, policylen, seq) 1045 int so; 1046 struct sockaddr *src, *dst; 1047 u_int prefs, prefd, proto; 1048 caddr_t policy; 1049 int policylen; 1050 u_int32_t seq; 1051{ 1052 int len; 1053 1054 if (policylen != sizeof(struct sadb_x_policy)) { 1055 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 1056 return -1; 1057 } 1058 1059 if ((len = pfkey_send_x4(so, SADB_X_SPDSETIDX, 1060 src, prefs, dst, prefd, proto, 1061 0, 0, 1062 policy, policylen, seq)) < 0) 1063 return -1; 1064 1065 return len; 1066} 1067 1068/* 1069 * sending SADB_SPDFLUSH message to the kernel. 1070 * OUT: 1071 * positive: success and return length sent. 1072 * -1 : error occured, and set errno. 1073 */ 1074int 1075pfkey_send_spdflush(so) 1076 int so; 1077{ 1078 int len; 1079 1080 if ((len = pfkey_send_x3(so, SADB_X_SPDFLUSH, SADB_SATYPE_UNSPEC)) < 0) 1081 return -1; 1082 1083 return len; 1084} 1085 1086/* 1087 * sending SADB_SPDDUMP message to the kernel. 1088 * OUT: 1089 * positive: success and return length sent. 1090 * -1 : error occured, and set errno. 1091 */ 1092int 1093pfkey_send_spddump(so) 1094 int so; 1095{ 1096 int len; 1097 1098 if ((len = pfkey_send_x3(so, SADB_X_SPDDUMP, SADB_SATYPE_UNSPEC)) < 0) 1099 return -1; 1100 1101 return len; 1102} 1103 1104/* sending SADB_ADD or SADB_UPDATE message to the kernel */ 1105static int 1106pfkey_send_x1(so, type, satype, mode, src, dst, spi, reqid, wsize, 1107 keymat, e_type, e_keylen, a_type, a_keylen, flags, 1108 l_alloc, l_bytes, l_addtime, l_usetime, seq) 1109 int so; 1110 u_int type, satype, mode; 1111 struct sockaddr *src, *dst; 1112 u_int32_t spi, reqid; 1113 u_int wsize; 1114 caddr_t keymat; 1115 u_int e_type, e_keylen, a_type, a_keylen, flags; 1116 u_int32_t l_alloc, l_bytes, l_addtime, l_usetime, seq; 1117{ 1118 struct sadb_msg *newmsg; 1119 int len; 1120 caddr_t p; 1121 int plen; 1122 caddr_t ep; 1123 1124 /* validity check */ 1125 if (src == NULL || dst == NULL) { 1126 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 1127 return -1; 1128 } 1129 if (src->sa_family != dst->sa_family) { 1130 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH; 1131 return -1; 1132 } 1133 switch (src->sa_family) { 1134 case AF_INET: 1135 plen = sizeof(struct in_addr) << 3; 1136 break; 1137 case AF_INET6: 1138 plen = sizeof(struct in6_addr) << 3; 1139 break; 1140 default: 1141 __ipsec_errcode = EIPSEC_INVAL_FAMILY; 1142 return -1; 1143 } 1144 1145 switch (satype) { 1146 case SADB_SATYPE_ESP: 1147 if (e_type == SADB_EALG_NONE) { 1148 __ipsec_errcode = EIPSEC_NO_ALGS; 1149 return -1; 1150 } 1151 break; 1152 case SADB_SATYPE_AH: 1153 if (e_type != SADB_EALG_NONE) { 1154 __ipsec_errcode = EIPSEC_INVAL_ALGS; 1155 return -1; 1156 } 1157 if (a_type == SADB_AALG_NONE) { 1158 __ipsec_errcode = EIPSEC_NO_ALGS; 1159 return -1; 1160 } 1161 break; 1162 case SADB_X_SATYPE_IPCOMP: 1163 if (e_type == SADB_X_CALG_NONE) { 1164 __ipsec_errcode = EIPSEC_INVAL_ALGS; 1165 return -1; 1166 } 1167 if (a_type != SADB_AALG_NONE) { 1168 __ipsec_errcode = EIPSEC_NO_ALGS; 1169 return -1; 1170 } 1171 break; 1172 default: 1173 __ipsec_errcode = EIPSEC_INVAL_SATYPE; 1174 return -1; 1175 } 1176 1177 /* create new sadb_msg to reply. */ 1178 len = sizeof(struct sadb_msg) 1179 + sizeof(struct sadb_sa) 1180 + sizeof(struct sadb_x_sa2) 1181 + sizeof(struct sadb_address) 1182 + PFKEY_ALIGN8(src->sa_len) 1183 + sizeof(struct sadb_address) 1184 + PFKEY_ALIGN8(dst->sa_len) 1185 + sizeof(struct sadb_lifetime) 1186 + sizeof(struct sadb_lifetime); 1187 1188 if (e_type != SADB_EALG_NONE) 1189 len += (sizeof(struct sadb_key) + PFKEY_ALIGN8(e_keylen)); 1190 if (a_type != SADB_AALG_NONE) 1191 len += (sizeof(struct sadb_key) + PFKEY_ALIGN8(a_keylen)); 1192 1193 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) { 1194 __ipsec_set_strerror(strerror(errno)); 1195 return -1; 1196 } 1197 ep = ((caddr_t)newmsg) + len; 1198 1199 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len, 1200 satype, seq, getpid()); 1201 if (!p) { 1202 free(newmsg); 1203 return -1; 1204 } 1205 p = pfkey_setsadbsa(p, ep, spi, wsize, a_type, e_type, flags); 1206 if (!p) { 1207 free(newmsg); 1208 return -1; 1209 } 1210 p = pfkey_setsadbxsa2(p, ep, mode, reqid); 1211 if (!p) { 1212 free(newmsg); 1213 return -1; 1214 } 1215 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen, 1216 IPSEC_ULPROTO_ANY); 1217 if (!p) { 1218 free(newmsg); 1219 return -1; 1220 } 1221 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen, 1222 IPSEC_ULPROTO_ANY); 1223 if (!p) { 1224 free(newmsg); 1225 return -1; 1226 } 1227 1228 if (e_type != SADB_EALG_NONE) { 1229 p = pfkey_setsadbkey(p, ep, SADB_EXT_KEY_ENCRYPT, 1230 keymat, e_keylen); 1231 if (!p) { 1232 free(newmsg); 1233 return -1; 1234 } 1235 } 1236 if (a_type != SADB_AALG_NONE) { 1237 p = pfkey_setsadbkey(p, ep, SADB_EXT_KEY_AUTH, 1238 keymat + e_keylen, a_keylen); 1239 if (!p) { 1240 free(newmsg); 1241 return -1; 1242 } 1243 } 1244 1245 /* set sadb_lifetime for destination */ 1246 p = pfkey_setsadblifetime(p, ep, SADB_EXT_LIFETIME_HARD, 1247 l_alloc, l_bytes, l_addtime, l_usetime); 1248 if (!p) { 1249 free(newmsg); 1250 return -1; 1251 } 1252 p = pfkey_setsadblifetime(p, ep, SADB_EXT_LIFETIME_SOFT, 1253 l_alloc, l_bytes, l_addtime, l_usetime); 1254 if (!p || p != ep) { 1255 free(newmsg); 1256 return -1; 1257 } 1258 1259 /* send message */ 1260 len = pfkey_send(so, newmsg, len); 1261 free(newmsg); 1262 1263 if (len < 0) 1264 return -1; 1265 1266 __ipsec_errcode = EIPSEC_NO_ERROR; 1267 return len; 1268} 1269 1270/* sending SADB_DELETE or SADB_GET message to the kernel */ 1271static int 1272pfkey_send_x2(so, type, satype, mode, src, dst, spi) 1273 int so; 1274 u_int type, satype, mode; 1275 struct sockaddr *src, *dst; 1276 u_int32_t spi; 1277{ 1278 struct sadb_msg *newmsg; 1279 int len; 1280 caddr_t p; 1281 int plen; 1282 caddr_t ep; 1283 1284 /* validity check */ 1285 if (src == NULL || dst == NULL) { 1286 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 1287 return -1; 1288 } 1289 if (src->sa_family != dst->sa_family) { 1290 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH; 1291 return -1; 1292 } 1293 switch (src->sa_family) { 1294 case AF_INET: 1295 plen = sizeof(struct in_addr) << 3; 1296 break; 1297 case AF_INET6: 1298 plen = sizeof(struct in6_addr) << 3; 1299 break; 1300 default: 1301 __ipsec_errcode = EIPSEC_INVAL_FAMILY; 1302 return -1; 1303 } 1304 1305 /* create new sadb_msg to reply. */ 1306 len = sizeof(struct sadb_msg) 1307 + sizeof(struct sadb_sa) 1308 + sizeof(struct sadb_address) 1309 + PFKEY_ALIGN8(src->sa_len) 1310 + sizeof(struct sadb_address) 1311 + PFKEY_ALIGN8(dst->sa_len); 1312 1313 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) { 1314 __ipsec_set_strerror(strerror(errno)); 1315 return -1; 1316 } 1317 ep = ((caddr_t)newmsg) + len; 1318 1319 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len, satype, 0, 1320 getpid()); 1321 if (!p) { 1322 free(newmsg); 1323 return -1; 1324 } 1325 p = pfkey_setsadbsa(p, ep, spi, 0, 0, 0, 0); 1326 if (!p) { 1327 free(newmsg); 1328 return -1; 1329 } 1330 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, plen, 1331 IPSEC_ULPROTO_ANY); 1332 if (!p) { 1333 free(newmsg); 1334 return -1; 1335 } 1336 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, plen, 1337 IPSEC_ULPROTO_ANY); 1338 if (!p || p != ep) { 1339 free(newmsg); 1340 return -1; 1341 } 1342 1343 /* send message */ 1344 len = pfkey_send(so, newmsg, len); 1345 free(newmsg); 1346 1347 if (len < 0) 1348 return -1; 1349 1350 __ipsec_errcode = EIPSEC_NO_ERROR; 1351 return len; 1352} 1353 1354/* 1355 * sending SADB_REGISTER, SADB_FLUSH, SADB_DUMP or SADB_X_PROMISC message 1356 * to the kernel 1357 */ 1358static int 1359pfkey_send_x3(so, type, satype) 1360 int so; 1361 u_int type, satype; 1362{ 1363 struct sadb_msg *newmsg; 1364 int len; 1365 caddr_t p; 1366 caddr_t ep; 1367 1368 /* validity check */ 1369 switch (type) { 1370 case SADB_X_PROMISC: 1371 if (satype != 0 && satype != 1) { 1372 __ipsec_errcode = EIPSEC_INVAL_SATYPE; 1373 return -1; 1374 } 1375 break; 1376 default: 1377 switch (satype) { 1378 case SADB_SATYPE_UNSPEC: 1379 case SADB_SATYPE_AH: 1380 case SADB_SATYPE_ESP: 1381 case SADB_X_SATYPE_IPCOMP: 1382 break; 1383 default: 1384 __ipsec_errcode = EIPSEC_INVAL_SATYPE; 1385 return -1; 1386 } 1387 } 1388 1389 /* create new sadb_msg to send. */ 1390 len = sizeof(struct sadb_msg); 1391 1392 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) { 1393 __ipsec_set_strerror(strerror(errno)); 1394 return -1; 1395 } 1396 ep = ((caddr_t)newmsg) + len; 1397 1398 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len, satype, 0, 1399 getpid()); 1400 if (!p || p != ep) { 1401 free(newmsg); 1402 return -1; 1403 } 1404 1405 /* send message */ 1406 len = pfkey_send(so, newmsg, len); 1407 free(newmsg); 1408 1409 if (len < 0) 1410 return -1; 1411 1412 __ipsec_errcode = EIPSEC_NO_ERROR; 1413 return len; 1414} 1415 1416/* sending SADB_X_SPDADD message to the kernel */ 1417static int 1418pfkey_send_x4(so, type, src, prefs, dst, prefd, proto, 1419 ltime, vtime, policy, policylen, seq) 1420 int so; 1421 struct sockaddr *src, *dst; 1422 u_int type, prefs, prefd, proto; 1423 u_int64_t ltime, vtime; 1424 char *policy; 1425 int policylen; 1426 u_int32_t seq; 1427{ 1428 struct sadb_msg *newmsg; 1429 int len; 1430 caddr_t p; 1431 int plen; 1432 caddr_t ep; 1433 1434 /* validity check */ 1435 if (src == NULL || dst == NULL) { 1436 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 1437 return -1; 1438 } 1439 if (src->sa_family != dst->sa_family) { 1440 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH; 1441 return -1; 1442 } 1443 1444 switch (src->sa_family) { 1445 case AF_INET: 1446 plen = sizeof(struct in_addr) << 3; 1447 break; 1448 case AF_INET6: 1449 plen = sizeof(struct in6_addr) << 3; 1450 break; 1451 default: 1452 __ipsec_errcode = EIPSEC_INVAL_FAMILY; 1453 return -1; 1454 } 1455 if (prefs > plen || prefd > plen) { 1456 __ipsec_errcode = EIPSEC_INVAL_PREFIXLEN; 1457 return -1; 1458 } 1459 1460 /* create new sadb_msg to reply. */ 1461 len = sizeof(struct sadb_msg) 1462 + sizeof(struct sadb_address) 1463 + PFKEY_ALIGN8(src->sa_len) 1464 + sizeof(struct sadb_address) 1465 + PFKEY_ALIGN8(src->sa_len) 1466 + sizeof(struct sadb_lifetime) 1467 + policylen; 1468 1469 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) { 1470 __ipsec_set_strerror(strerror(errno)); 1471 return -1; 1472 } 1473 ep = ((caddr_t)newmsg) + len; 1474 1475 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len, 1476 SADB_SATYPE_UNSPEC, seq, getpid()); 1477 if (!p) { 1478 free(newmsg); 1479 return -1; 1480 } 1481 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_SRC, src, prefs, proto); 1482 if (!p) { 1483 free(newmsg); 1484 return -1; 1485 } 1486 p = pfkey_setsadbaddr(p, ep, SADB_EXT_ADDRESS_DST, dst, prefd, proto); 1487 if (!p) { 1488 free(newmsg); 1489 return -1; 1490 } 1491 p = pfkey_setsadblifetime(p, ep, SADB_EXT_LIFETIME_HARD, 1492 0, 0, ltime, vtime); 1493 if (!p || p + policylen != ep) { 1494 free(newmsg); 1495 return -1; 1496 } 1497 memcpy(p, policy, policylen); 1498 1499 /* send message */ 1500 len = pfkey_send(so, newmsg, len); 1501 free(newmsg); 1502 1503 if (len < 0) 1504 return -1; 1505 1506 __ipsec_errcode = EIPSEC_NO_ERROR; 1507 return len; 1508} 1509 1510/* sending SADB_X_SPDGET or SADB_X_SPDDELETE message to the kernel */ 1511static int 1512pfkey_send_x5(so, type, spid) 1513 int so; 1514 u_int type; 1515 u_int32_t spid; 1516{ 1517 struct sadb_msg *newmsg; 1518 struct sadb_x_policy xpl; 1519 int len; 1520 caddr_t p; 1521 caddr_t ep; 1522 1523 /* create new sadb_msg to reply. */ 1524 len = sizeof(struct sadb_msg) 1525 + sizeof(xpl); 1526 1527 if ((newmsg = CALLOC(len, struct sadb_msg *)) == NULL) { 1528 __ipsec_set_strerror(strerror(errno)); 1529 return -1; 1530 } 1531 ep = ((caddr_t)newmsg) + len; 1532 1533 p = pfkey_setsadbmsg((caddr_t)newmsg, ep, type, len, 1534 SADB_SATYPE_UNSPEC, 0, getpid()); 1535 if (!p) { 1536 free(newmsg); 1537 return -1; 1538 } 1539 1540 if (p + sizeof(xpl) != ep) { 1541 free(newmsg); 1542 return -1; 1543 } 1544 memset(&xpl, 0, sizeof(xpl)); 1545 xpl.sadb_x_policy_len = PFKEY_UNIT64(sizeof(xpl)); 1546 xpl.sadb_x_policy_exttype = SADB_X_EXT_POLICY; 1547 xpl.sadb_x_policy_id = spid; 1548 memcpy(p, &xpl, sizeof(xpl)); 1549 1550 /* send message */ 1551 len = pfkey_send(so, newmsg, len); 1552 free(newmsg); 1553 1554 if (len < 0) 1555 return -1; 1556 1557 __ipsec_errcode = EIPSEC_NO_ERROR; 1558 return len; 1559} 1560 1561/* 1562 * open a socket. 1563 * OUT: 1564 * -1: fail. 1565 * others : success and return value of socket. 1566 */ 1567int 1568pfkey_open() 1569{ 1570 int so; 1571 const int bufsiz = 128 * 1024; /*is 128K enough?*/ 1572 1573 if ((so = socket(PF_KEY, SOCK_RAW, PF_KEY_V2)) < 0) { 1574 __ipsec_set_strerror(strerror(errno)); 1575 return -1; 1576 } 1577 1578 /* 1579 * This is a temporary workaround for KAME PR 154. 1580 * Don't really care even if it fails. 1581 */ 1582 (void)setsockopt(so, SOL_SOCKET, SO_SNDBUF, &bufsiz, sizeof(bufsiz)); 1583 (void)setsockopt(so, SOL_SOCKET, SO_RCVBUF, &bufsiz, sizeof(bufsiz)); 1584 1585 __ipsec_errcode = EIPSEC_NO_ERROR; 1586 return so; 1587} 1588 1589/* 1590 * close a socket. 1591 * OUT: 1592 * 0: success. 1593 * -1: fail. 1594 */ 1595void 1596pfkey_close(so) 1597 int so; 1598{ 1599 (void)close(so); 1600 1601 __ipsec_errcode = EIPSEC_NO_ERROR; 1602 return; 1603} 1604 1605/* 1606 * receive sadb_msg data, and return pointer to new buffer allocated. 1607 * Must free this buffer later. 1608 * OUT: 1609 * NULL : error occured. 1610 * others : a pointer to sadb_msg structure. 1611 * 1612 * XXX should be rewritten to pass length explicitly 1613 */ 1614struct sadb_msg * 1615pfkey_recv(so) 1616 int so; 1617{ 1618 struct sadb_msg buf, *newmsg; 1619 int len, reallen; 1620 1621 while ((len = recv(so, (caddr_t)&buf, sizeof(buf), MSG_PEEK)) < 0) { 1622 if (errno == EINTR) 1623 continue; 1624 __ipsec_set_strerror(strerror(errno)); 1625 return NULL; 1626 } 1627 1628 if (len < sizeof(buf)) { 1629 recv(so, (caddr_t)&buf, sizeof(buf), 0); 1630 __ipsec_errcode = EIPSEC_MAX; 1631 return NULL; 1632 } 1633 1634 /* read real message */ 1635 reallen = PFKEY_UNUNIT64(buf.sadb_msg_len); 1636 if ((newmsg = CALLOC(reallen, struct sadb_msg *)) == 0) { 1637 __ipsec_set_strerror(strerror(errno)); 1638 return NULL; 1639 } 1640 1641 while ((len = recv(so, (caddr_t)newmsg, reallen, 0)) < 0) { 1642 if (errno == EINTR) 1643 continue; 1644 __ipsec_set_strerror(strerror(errno)); 1645 free(newmsg); 1646 return NULL; 1647 } 1648 1649 if (len != reallen) { 1650 __ipsec_errcode = EIPSEC_SYSTEM_ERROR; 1651 free(newmsg); 1652 return NULL; 1653 } 1654 1655 /* don't trust what the kernel says, validate! */ 1656 if (PFKEY_UNUNIT64(newmsg->sadb_msg_len) != len) { 1657 __ipsec_errcode = EIPSEC_SYSTEM_ERROR; 1658 free(newmsg); 1659 return NULL; 1660 } 1661 1662 __ipsec_errcode = EIPSEC_NO_ERROR; 1663 return newmsg; 1664} 1665 1666/* 1667 * send message to a socket. 1668 * OUT: 1669 * others: success and return length sent. 1670 * -1 : fail. 1671 */ 1672int 1673pfkey_send(so, msg, len) 1674 int so; 1675 struct sadb_msg *msg; 1676 int len; 1677{ 1678 if ((len = send(so, (caddr_t)msg, len, 0)) < 0) { 1679 __ipsec_set_strerror(strerror(errno)); 1680 return -1; 1681 } 1682 1683 __ipsec_errcode = EIPSEC_NO_ERROR; 1684 return len; 1685} 1686 1687/* 1688 * %%% Utilities 1689 * NOTE: These functions are derived from netkey/key.c in KAME. 1690 */ 1691/* 1692 * set the pointer to each header in this message buffer. 1693 * IN: msg: pointer to message buffer. 1694 * mhp: pointer to the buffer initialized like below: 1695 * caddr_t mhp[SADB_EXT_MAX + 1]; 1696 * OUT: -1: invalid. 1697 * 0: valid. 1698 * 1699 * XXX should be rewritten to obtain length explicitly 1700 */ 1701int 1702pfkey_align(msg, mhp) 1703 struct sadb_msg *msg; 1704 caddr_t *mhp; 1705{ 1706 struct sadb_ext *ext; 1707 int i; 1708 caddr_t p; 1709 caddr_t ep; /* XXX should be passed from upper layer */ 1710 1711 /* validity check */ 1712 if (msg == NULL || mhp == NULL) { 1713 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 1714 return -1; 1715 } 1716 1717 /* initialize */ 1718 for (i = 0; i < SADB_EXT_MAX + 1; i++) 1719 mhp[i] = NULL; 1720 1721 mhp[0] = (caddr_t)msg; 1722 1723 /* initialize */ 1724 p = (caddr_t) msg; 1725 ep = p + PFKEY_UNUNIT64(msg->sadb_msg_len); 1726 1727 /* skip base header */ 1728 p += sizeof(struct sadb_msg); 1729 1730 while (p < ep) { 1731 ext = (struct sadb_ext *)p; 1732 if (ep < p + sizeof(*ext) || PFKEY_EXTLEN(ext) < sizeof(*ext) || 1733 ep < p + PFKEY_EXTLEN(ext)) { 1734 /* invalid format */ 1735 break; 1736 } 1737 1738 /* duplicate check */ 1739 /* XXX Are there duplication either KEY_AUTH or KEY_ENCRYPT ?*/ 1740 if (mhp[ext->sadb_ext_type] != NULL) { 1741 __ipsec_errcode = EIPSEC_INVAL_EXTTYPE; 1742 return -1; 1743 } 1744 1745 /* set pointer */ 1746 switch (ext->sadb_ext_type) { 1747 case SADB_EXT_SA: 1748 case SADB_EXT_LIFETIME_CURRENT: 1749 case SADB_EXT_LIFETIME_HARD: 1750 case SADB_EXT_LIFETIME_SOFT: 1751 case SADB_EXT_ADDRESS_SRC: 1752 case SADB_EXT_ADDRESS_DST: 1753 case SADB_EXT_ADDRESS_PROXY: 1754 case SADB_EXT_KEY_AUTH: 1755 /* XXX should to be check weak keys. */ 1756 case SADB_EXT_KEY_ENCRYPT: 1757 /* XXX should to be check weak keys. */ 1758 case SADB_EXT_IDENTITY_SRC: 1759 case SADB_EXT_IDENTITY_DST: 1760 case SADB_EXT_SENSITIVITY: 1761 case SADB_EXT_PROPOSAL: 1762 case SADB_EXT_SUPPORTED_AUTH: 1763 case SADB_EXT_SUPPORTED_ENCRYPT: 1764 case SADB_EXT_SPIRANGE: 1765 case SADB_X_EXT_POLICY: 1766 case SADB_X_EXT_SA2: 1767 mhp[ext->sadb_ext_type] = (caddr_t)ext; 1768 break; 1769 default: 1770 __ipsec_errcode = EIPSEC_INVAL_EXTTYPE; 1771 return -1; 1772 } 1773 1774 p += PFKEY_EXTLEN(ext); 1775 } 1776 1777 if (p != ep) { 1778 __ipsec_errcode = EIPSEC_INVAL_SADBMSG; 1779 return -1; 1780 } 1781 1782 __ipsec_errcode = EIPSEC_NO_ERROR; 1783 return 0; 1784} 1785 1786/* 1787 * check basic usage for sadb_msg, 1788 * NOTE: This routine is derived from netkey/key.c in KAME. 1789 * IN: msg: pointer to message buffer. 1790 * mhp: pointer to the buffer initialized like below: 1791 * 1792 * caddr_t mhp[SADB_EXT_MAX + 1]; 1793 * 1794 * OUT: -1: invalid. 1795 * 0: valid. 1796 */ 1797int 1798pfkey_check(mhp) 1799 caddr_t *mhp; 1800{ 1801 struct sadb_msg *msg; 1802 1803 /* validity check */ 1804 if (mhp == NULL || mhp[0] == NULL) { 1805 __ipsec_errcode = EIPSEC_INVAL_ARGUMENT; 1806 return -1; 1807 } 1808 1809 msg = (struct sadb_msg *)mhp[0]; 1810 1811 /* check version */ 1812 if (msg->sadb_msg_version != PF_KEY_V2) { 1813 __ipsec_errcode = EIPSEC_INVAL_VERSION; 1814 return -1; 1815 } 1816 1817 /* check type */ 1818 if (msg->sadb_msg_type > SADB_MAX) { 1819 __ipsec_errcode = EIPSEC_INVAL_MSGTYPE; 1820 return -1; 1821 } 1822 1823 /* check SA type */ 1824 switch (msg->sadb_msg_satype) { 1825 case SADB_SATYPE_UNSPEC: 1826 switch (msg->sadb_msg_type) { 1827 case SADB_GETSPI: 1828 case SADB_UPDATE: 1829 case SADB_ADD: 1830 case SADB_DELETE: 1831 case SADB_GET: 1832 case SADB_ACQUIRE: 1833 case SADB_EXPIRE: 1834 __ipsec_errcode = EIPSEC_INVAL_SATYPE; 1835 return -1; 1836 } 1837 break; 1838 case SADB_SATYPE_ESP: 1839 case SADB_SATYPE_AH: 1840 case SADB_X_SATYPE_IPCOMP: 1841 switch (msg->sadb_msg_type) { 1842 case SADB_X_SPDADD: 1843 case SADB_X_SPDDELETE: 1844 case SADB_X_SPDGET: 1845 case SADB_X_SPDDUMP: 1846 case SADB_X_SPDFLUSH: 1847 __ipsec_errcode = EIPSEC_INVAL_SATYPE; 1848 return -1; 1849 } 1850 break; 1851 case SADB_SATYPE_RSVP: 1852 case SADB_SATYPE_OSPFV2: 1853 case SADB_SATYPE_RIPV2: 1854 case SADB_SATYPE_MIP: 1855 __ipsec_errcode = EIPSEC_NOT_SUPPORTED; 1856 return -1; 1857 case 1: /* XXX: What does it do ? */ 1858 if (msg->sadb_msg_type == SADB_X_PROMISC) 1859 break; 1860 /*FALLTHROUGH*/ 1861 default: 1862 __ipsec_errcode = EIPSEC_INVAL_SATYPE; 1863 return -1; 1864 } 1865 1866 /* check field of upper layer protocol and address family */ 1867 if (mhp[SADB_EXT_ADDRESS_SRC] != NULL 1868 && mhp[SADB_EXT_ADDRESS_DST] != NULL) { 1869 struct sadb_address *src0, *dst0; 1870 1871 src0 = (struct sadb_address *)(mhp[SADB_EXT_ADDRESS_SRC]); 1872 dst0 = (struct sadb_address *)(mhp[SADB_EXT_ADDRESS_DST]); 1873 1874 if (src0->sadb_address_proto != dst0->sadb_address_proto) { 1875 __ipsec_errcode = EIPSEC_PROTO_MISMATCH; 1876 return -1; 1877 } 1878 1879 if (PFKEY_ADDR_SADDR(src0)->sa_family 1880 != PFKEY_ADDR_SADDR(dst0)->sa_family) { 1881 __ipsec_errcode = EIPSEC_FAMILY_MISMATCH; 1882 return -1; 1883 } 1884 1885 switch (PFKEY_ADDR_SADDR(src0)->sa_family) { 1886 case AF_INET: 1887 case AF_INET6: 1888 break; 1889 default: 1890 __ipsec_errcode = EIPSEC_INVAL_FAMILY; 1891 return -1; 1892 } 1893 1894 /* 1895 * prefixlen == 0 is valid because there must be the case 1896 * all addresses are matched. 1897 */ 1898 } 1899 1900 __ipsec_errcode = EIPSEC_NO_ERROR; 1901 return 0; 1902} 1903 1904/* 1905 * set data into sadb_msg. 1906 * `buf' must has been allocated sufficiently. 1907 */ 1908static caddr_t 1909pfkey_setsadbmsg(buf, lim, type, tlen, satype, seq, pid) 1910 caddr_t buf; 1911 caddr_t lim; 1912 u_int type, satype; 1913 u_int tlen; 1914 u_int32_t seq; 1915 pid_t pid; 1916{ 1917 struct sadb_msg *p; 1918 u_int len; 1919 1920 p = (struct sadb_msg *)buf; 1921 len = sizeof(struct sadb_msg); 1922 1923 if (buf + len > lim) 1924 return NULL; 1925 1926 memset(p, 0, len); 1927 p->sadb_msg_version = PF_KEY_V2; 1928 p->sadb_msg_type = type; 1929 p->sadb_msg_errno = 0; 1930 p->sadb_msg_satype = satype; 1931 p->sadb_msg_len = PFKEY_UNIT64(tlen); 1932 p->sadb_msg_reserved = 0; 1933 p->sadb_msg_seq = seq; 1934 p->sadb_msg_pid = (u_int32_t)pid; 1935 1936 return(buf + len); 1937} 1938 1939/* 1940 * copy secasvar data into sadb_address. 1941 * `buf' must has been allocated sufficiently. 1942 */ 1943static caddr_t 1944pfkey_setsadbsa(buf, lim, spi, wsize, auth, enc, flags) 1945 caddr_t buf; 1946 caddr_t lim; 1947 u_int32_t spi, flags; 1948 u_int wsize, auth, enc; 1949{ 1950 struct sadb_sa *p; 1951 u_int len; 1952 1953 p = (struct sadb_sa *)buf; 1954 len = sizeof(struct sadb_sa); 1955 1956 if (buf + len > lim) 1957 return NULL; 1958 1959 memset(p, 0, len); 1960 p->sadb_sa_len = PFKEY_UNIT64(len); 1961 p->sadb_sa_exttype = SADB_EXT_SA; 1962 p->sadb_sa_spi = spi; 1963 p->sadb_sa_replay = wsize; 1964 p->sadb_sa_state = SADB_SASTATE_LARVAL; 1965 p->sadb_sa_auth = auth; 1966 p->sadb_sa_encrypt = enc; 1967 p->sadb_sa_flags = flags; 1968 1969 return(buf + len); 1970} 1971 1972/* 1973 * set data into sadb_address. 1974 * `buf' must has been allocated sufficiently. 1975 * prefixlen is in bits. 1976 */ 1977static caddr_t 1978pfkey_setsadbaddr(buf, lim, exttype, saddr, prefixlen, ul_proto) 1979 caddr_t buf; 1980 caddr_t lim; 1981 u_int exttype; 1982 struct sockaddr *saddr; 1983 u_int prefixlen; 1984 u_int ul_proto; 1985{ 1986 struct sadb_address *p; 1987 u_int len; 1988 1989 p = (struct sadb_address *)buf; 1990 len = sizeof(struct sadb_address) + PFKEY_ALIGN8(saddr->sa_len); 1991 1992 if (buf + len > lim) 1993 return NULL; 1994 1995 memset(p, 0, len); 1996 p->sadb_address_len = PFKEY_UNIT64(len); 1997 p->sadb_address_exttype = exttype & 0xffff; 1998 p->sadb_address_proto = ul_proto & 0xff; 1999 p->sadb_address_prefixlen = prefixlen; 2000 p->sadb_address_reserved = 0; 2001 2002 memcpy(p + 1, saddr, saddr->sa_len); 2003 2004 return(buf + len); 2005} 2006 2007/* 2008 * set sadb_key structure after clearing buffer with zero. 2009 * OUT: the pointer of buf + len. 2010 */ 2011static caddr_t 2012pfkey_setsadbkey(buf, lim, type, key, keylen) 2013 caddr_t buf; 2014 caddr_t lim; 2015 caddr_t key; 2016 u_int type, keylen; 2017{ 2018 struct sadb_key *p; 2019 u_int len; 2020 2021 p = (struct sadb_key *)buf; 2022 len = sizeof(struct sadb_key) + PFKEY_ALIGN8(keylen); 2023 2024 if (buf + len > lim) 2025 return NULL; 2026 2027 memset(p, 0, len); 2028 p->sadb_key_len = PFKEY_UNIT64(len); 2029 p->sadb_key_exttype = type; 2030 p->sadb_key_bits = keylen << 3; 2031 p->sadb_key_reserved = 0; 2032 2033 memcpy(p + 1, key, keylen); 2034 2035 return buf + len; 2036} 2037 2038/* 2039 * set sadb_lifetime structure after clearing buffer with zero. 2040 * OUT: the pointer of buf + len. 2041 */ 2042static caddr_t 2043pfkey_setsadblifetime(buf, lim, type, l_alloc, l_bytes, l_addtime, l_usetime) 2044 caddr_t buf; 2045 caddr_t lim; 2046 u_int type; 2047 u_int32_t l_alloc, l_bytes, l_addtime, l_usetime; 2048{ 2049 struct sadb_lifetime *p; 2050 u_int len; 2051 2052 p = (struct sadb_lifetime *)buf; 2053 len = sizeof(struct sadb_lifetime); 2054 2055 if (buf + len > lim) 2056 return NULL; 2057 2058 memset(p, 0, len); 2059 p->sadb_lifetime_len = PFKEY_UNIT64(len); 2060 p->sadb_lifetime_exttype = type; 2061 2062 switch (type) { 2063 case SADB_EXT_LIFETIME_SOFT: 2064 p->sadb_lifetime_allocations 2065 = (l_alloc * soft_lifetime_allocations_rate) /100; 2066 p->sadb_lifetime_bytes 2067 = (l_bytes * soft_lifetime_bytes_rate) /100; 2068 p->sadb_lifetime_addtime 2069 = (l_addtime * soft_lifetime_addtime_rate) /100; 2070 p->sadb_lifetime_usetime 2071 = (l_usetime * soft_lifetime_usetime_rate) /100; 2072 break; 2073 case SADB_EXT_LIFETIME_HARD: 2074 p->sadb_lifetime_allocations = l_alloc; 2075 p->sadb_lifetime_bytes = l_bytes; 2076 p->sadb_lifetime_addtime = l_addtime; 2077 p->sadb_lifetime_usetime = l_usetime; 2078 break; 2079 } 2080 2081 return buf + len; 2082} 2083 2084/* 2085 * copy secasvar data into sadb_address. 2086 * `buf' must has been allocated sufficiently. 2087 */ 2088static caddr_t 2089pfkey_setsadbxsa2(buf, lim, mode0, reqid) 2090 caddr_t buf; 2091 caddr_t lim; 2092 u_int32_t mode0; 2093 u_int32_t reqid; 2094{ 2095 struct sadb_x_sa2 *p; 2096 u_int8_t mode = mode0 & 0xff; 2097 u_int len; 2098 2099 p = (struct sadb_x_sa2 *)buf; 2100 len = sizeof(struct sadb_x_sa2); 2101 2102 if (buf + len > lim) 2103 return NULL; 2104 2105 memset(p, 0, len); 2106 p->sadb_x_sa2_len = PFKEY_UNIT64(len); 2107 p->sadb_x_sa2_exttype = SADB_X_EXT_SA2; 2108 p->sadb_x_sa2_mode = mode; 2109 p->sadb_x_sa2_reqid = reqid; 2110 2111 return(buf + len); 2112} 2113