Bedrock Computer Technologies, LLC v. Softlayer Technologies, Inc. et al

Filing 845

MOTION for Judgment as a Matter of Law Regarding Invalidity (Renewed) by Yahoo! Inc.. (Attachments: #1 Text of Proposed Order, #2 Exhibit 1 - Declaration of Alexey Kuznetsov - DX-48, #3 Exhibit 2 - Source Code - key.c - DX-37, #4 Exhibit 3 - U.S. Patent 5,121,495 - DX-65, #5 Exhibit 4 - Application Approval for Filing - DX-57, #6 Exhibit 5 - U. S. Patent 6,119,214 - DX101, #7 Exhibit 6 - U.S. Patent 4,996,663 - DX-64, #8 Exhibit 7 - Donald Knuth, Sorting and Searching, vol. 3, of The Art of Computer Programming - DX-98, #9 Exhibit 8 - Kruse, "Data Structures and Program Design" - DX-108, #10 Exhibit 9 - Daniel F. Stubbs and Neil W. Webre, Data Structures with Abstract Data Types and Pascal - DX-118, #11 Exhibit 10 - Kuznetsov email to Day re contact request - DX-436, #12 Exhibit 11 - Absher email to Kuznetsov re Linux route.c question - DX-440, #13 Exhibit 12 - Kuznetsov email to Absher re Linux route.c question - DX-441)(Doan, Jennifer)

Download PDF
Exhibit 2 9/28/1995 key.c 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the US Naval Research Laboratory (NRL). ----------------------------------------------------------------------*/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include <sys/types.h> <sys/param.h> <sys/proc.h> <sys/mbuf.h> <sys/socket.h> <sys/socketvar.h> <sys/time.h> <sys/kernel.h> <net/raw cb.h> <net/if.h> <net/if types.h> <net/if dl.h> <net/route.h> <netinet/in.h> <netinet/in var.h> <netinet/if_ether.h> #include #include #include #include #include <netinet6/in6.h> <netinet6/in6 var.h> <netinet6/ipsec.h> <netinet6/key.h> <netinet6/in6_debug.h> #define MAXHASHKEYLEN (2 * sizeof(int) + 2 * sizeof(struct sockaddr_in6) ) 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 /* * Not clear whether these values should be * tweakable at kernel config time. */ #define KEYTBLSIZE 61 #define KEYALLOCTBLSIZE 61 #define S02SPITBL8IZE 61 /* * * These values should be tweakable, .. perhaps by using sysetl */ #define MAXLARVALTIME 240; #define MAXKEYACQUIRE I i /* Lifetime of a larval key table entry */ /* Max number of key acquire messages sent */ /* 115 per destination address */ 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 #define MAXACQUIRETIME 15; /* Lifetime of acquire message */ /* * Key engine tables and global variables */ struct key tblnode key table [KEYTBLSIZE] ; struct key allocnode keyalloctbl[KEYALLOCTBLSIZE]; struct key_so2spinode so2spitbl[S02SPITBLSIZE]; struct keyso cb key so cb; struct key tblnode nullkeynode = { 0, 0, 0, 0, struct key registry *keyregtable; struct key acquirelist *key acquirelist; u long maxlarvallifetime = MAXLARVALTIME; int maxkeyacquire = MAXKEYACQUIRE; 0 }i 2 DEF00007943 key.c 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 9/28/1995 u_long maxacquiretime = MAXACQUIRETIME; extern void dump_secassoc(); /*---------------------------------------------------------------------* (temporary) Dump a data buffer ----------------------------------------------------------------------* / void dump buf(buf, len) -char *buf; int len; int i; printf("buf=Ox%x len=%d:\n", buf, len); for (i = 0; i < len; i++) { printf("Ox%x" (u int8)*(buf+i)); } printf("\n"); /*---------------------------------------------------------------------- * (temporary) Dump a key tblnode structrue ----------------------------------------------------------------------* / void dump keytblnode(ktblnode) -struct key_tblnode *ktblnode; if (!ktblnode) { printf("NULL key table node pOinter!\n"); return; } printf("solist=Ox%x", ktblnode->solist); printf("secassoc=Ox%x", ktblnode->secassoc) printf("next=Ox%x\n", ktblnode->next); i 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 /*---------------------------------------------------------------------* key secassoc2msghdr() : * Copy info from a security association into a key message buffer. * Assume message buffer is sufficiently large to hold all security * association information including src, dst, from, key and iv. ----------------------------------------------------------------------* / int key secassoc2msghdr(secassoc, km, keyinfo) - struct ipsec assoc *secassoc; struct key msghdr *km; struct key_msgdata *keyinfo; char *cp; DPRINTF(IDL_GROSS EVENT, if ((km == 0) return(-l); II ("Entering key_secassoc2msghdr\n")) II (keyinfo == 0) i (secassoc == 0)) km->type = secassoc->type; km->state = secassoc->statei km->label = secassoc->label; km->spi = secassoc->spi; 3 DEF00007944 key.c 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 9/28/1995 km->key1en = secassoc->key1en; km->iv1en = secassoc->ivlen; km->a1gorithm = secassoc->algorithm; km->lifetype = secassoc->lifetYP8; km->lifetimel secassoc->lifetimel; km->lifetime2 = secassoc->lifetime2; /* * Stuff src/dst/from/key/iv in buffer after * the message header. */ cp = (char *) (km + 1); #define ROUNDUP (a) \ «a) > 0 ? (1 + « (a) - 1) #define ADVANCE (x, n) \ { x += ROUNDUP(n); } (sizeof(long) - 1))) sizeof (long) ) DPRINTF(IDL FINISHED, ("sa2msghdr: l\n")); keyinfo->src = (struct sockaddr *)cp, if (secassoc->src.sin6 len) { bcopy«char *)&(secassoc->src), cp, secassoc->src.sin6_len); ADVANCE (cp, secassoc->src.sin6 len); else f bzero(cp, sizeof(struct sockaddr in6)), ADVANCE (cp, sizeof(struct sockaddr_in6)), } DPRINTF(IDL_FINISHED, ("sa2msghdr: 2\n")), keyinfo->dst = (struct sockaddr *)&(secassoc->dst); if (secassoc->dst.sin6 len) { bcopy«char *)&(secassoc->dst), cp, secassoc->dst.sin6 len); ADVANCE (cp, secassoc->dst.sin6 len); else f bzero(cp, sizeof(struct sockaddr in6)); ADVANCE (cp, sizeof(struct sockaddr_in6)); } DPRINTF(IDL_FINISHED, ("sa2msghdr: 3\n")); keyinfo->from = (struct sockaddr *)cp; if (secassoc->from.sin6 len) f bcopy«char *)&(secassoc->from), cp, secassoc->from.sin6 len); ADVANCE (cp, secassoc->fr:om.sin6_len), else { bzero(cp, sizeof(struct sockaddr in6)); ADVANCE (cp, sizeof(struct sockaddr_in6)); } DPRINTF(IDL_FINISHED, ("sa2msghdr: 4\n")); keyinfo->key = cp; keyinfo->keylen = secassoc->key18n; if (secassoc->keylen) { bcopy( (char *) (secassoc->key), cp, secassoc->key18n); ADVANCE (cp, secassoc->keylen); DPRINTF(IDL FINISHED, ("sa2msqhdr: 5\n")); keyinfo- >i v - = cp, keyinfo->ivlen = secassoc->ivlen; if (secassoc->ivlen) { bcopy( (char *) (secassoc->iv), cp, secassoc->ivlen); ADVANCE (cp, secassoc->ivlen); DDO(IDL FINISHED,printf("msgbuf(len=%d) :\n", (char *)cp - (char *)km)); DDO(IDL FINISHED,dump buf«char *)km, (char *)cp - (char *)km)); DPRINTF (IDL_FINISHED, - (" sa2msghdr:: 6\n")), 4 DEF00007945 key.c 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 9/28/1995 return(O) i /*---------------------------------------------------------------------* key msghdr2secassoc() : Copy info from a key message buffer into an ipsec_assoc * * structure ----------------------------------------------------------------------* / int key msghdr2secassoc(secassoc, km, keyinfo) - struct ipsec assoc *secassoci struct key msghdr *km; struct key_msgdata *keyinfoi DPRINTF (IDL_GROSS_EVENT, if «krn == 0) return (-1) ; II ("Entering key _msghdr2secassoc\n") ) ; (keyinfo == 0) II (secassoc == 0)) secassoc->len = sizeof(*secassoc); secassoc->type = km->type; secassoc->state = km->state; secassoc->label = km->label; secassoc->spi = km->spii secassoc->keylen = km->keylen; secassoc->ivlen = km->ivlen; secassoc->algorithm = km->algorithm; secassoc->lifetype = km->lifetype; secassoc->lifetimel km->lifetimel; secassoc->lifetime2 = km->lifetime2i if (keyinfo->src) bcopy«char *) (keyinfo->src), keyinfo->src->sa_len); (char *)&(secassoc->src), if (keyinfo->dst) bcopy«char *) (keyinfo->dst), keyinfo->dst->sa_len) ; (char *)&(secassoc->dst), if (keyinfo->from) bcopy( (char *) (keyinfo->from), keyinfo->from->sa_len) i (char *) &(secassoc->from), /* * Make copies of key and iv */ if (secassoc->ivlen) { K Malloc(secassoc->iv, caddr_t, secassoc->ivlen); i f (secassoc->iv == 0) { DPRINTF(IDL CRITICAL, ("rnsGhdr2secassoc: can't allocate mem for iv\n")); return (-1) i } bcopy( (char *)keyinfo->iv, else secassoc->iv = NULL; (char *)secassoc->iv, secassoc->ivlen); if (secassoc->keylen) { K Malloc(secassoc->key, caddr t, secassoc->keylen); if (secassoc->key == 0) { DPRINTF(IDL CRITICAL, ("msGhdr2secassoc: can't allocate mem for key\n")); ~ if (secassoc->iv) KFree(secassoc->iv) i return(-l) ; 5 DEF00007946 key.c 9/28/1995 } bcopy«char *)keyinfo->key. secassoc->keylen) ; else secassoc->key = NULL; return(O) ; 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 (char *)secassoc->key. /*---------------------------------------------------------------------* addrpart equal(): * Determine if the address portion of two sockaddrs are equal. * Currently handles only AF INET and AF INET6 address families. ----------------------------------------------------------------------* / int addrpart equal(sa1, sa2) struct sockaddr *sa1; struct sockaddr *sa2; if «sal->sa family == sa2->sa family») switch(sal->sa_family) { case AF INET: if «(struct sockaddr in *)sa1)->sin addr.s addr «struct sockaddr in *)sa2) ->sin addr.s addr) return(1) ; -break; case AF INET6: if (IN6 ADDR EQUAL«(struct sockaddr in6 *)sa1)->sin6 addr. «struct sockaddr in6 *)sa2)->sin6_addr)) return(1) ; break; } return(O) ; /*---------------------------------------------------------------------- * my addr() : * Determine if an address belongs to one of my configured interfaces. * Currently handles only AF INET and AF INET6 addresses. ----------------------------------------------------------------------* / int my_addr(sa) struct sockaddr *sa; extern extern struct struct struct in6 ifaddr *in6 ifaddr; struct in ifaddr *in ifaddr; in6 ifaddr *i6a = 0;in_ifaddr *ia = 0; switch(sa->sa family) { case AF INET6: for (i6a = in6 ifaddr; i6a; i6a = i6a->i6a next) { if (IN6 ADDR EQUAL«(struct sockaddr in6-*)sa)->sin6_addr. i6a->i6a addr.sin6 addr)) return(l) ; } break; case AF INET: for (ia = in ifaddr; ia; ia = ia->ia next) { if «(struct sockaddr in *)sa)->sin addr.s addr ia->ia addr.sin addr.s addr) return(l); 6 DEF00007947 key.c 9/28/1995 390 391 392 393 394 395 break; } return(O) ; 396 397 398 399 400 /*---------------------------------------------------------------------* key inittables(): * Allocate space and initialize key engine tables ----------------------------------------------------------------------* 401 402 403 404 405 406 407 void key inittables() { struct key tblnode *keynode; int i; - 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 int key_gethashval(buf, len, tblsize) char *buf; int len; int tblsize; / K Malloc(keyregtable, struct key_registry * sizeof(struct key registry)); 408 if (keyregtable == 0) 409 panic("key inittables"); 410 bzero((char *)keyregtable, sizeof(struct key registry)); 411 K Malloc(key acquirelist, struct key acquirelist * 412-sizeof(struct key_acquirelist)); 413 if (key acquirelist == 0) 414 panic ("key inittables"); 415 bzero((char *)key acquirelist, sizeof(scruct key_acquirelist)); 416 for (i = 0; i < KEYTBLSIZE; i++) 417 bzero((char *)&keytable[i], sizeof(struct key_tblnode)); 418 for (i = 0; i < KEYALLOCTBLSIZE; i++) 419 bzero((char *)&keyalloctbl[i], sizeof(struct key allocnode)); 420 for (i = 0; i < S02SPITBLSIZE; i++) 421 bzero((char *)&s02spitbl[i], sizeof(struct key_s02spinode)); 422 423 424 425 /*---------------------------------------------------------------------426 * key gethashval(): 427 * Determine key table hash value. 428 ----------------------------------------------------------------------* / int i, j = 0; /* * Todo: Use word size xor and check for alignment * and zero pad if necessary. Need to also pick * a good hash function and table size. */ i f (len <= 0) { DPRINTF(IDL_CRITICAL, ("key_gethashval got bogus len!\n")); return(-l); } for(i = 0; i < len; i++) jA (u_int8) (*(buf + i)); } return (j % tblsize); /*---------------------------------------------------------------------7 DEF00007948 key.c 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 49l 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 9/28/1995 * key createkey() : Create hash key for hash function * key is: type+src+dst if keytype = 1 * type+src+dst+spi if keytype = 0 * Uses only the address portion of the src and dst sockaddrs to * form key. Currently handles only AF INET and AF INET6 sockaddrs * ----------------------------------------------------------------------* / int key createkey(buf, type, src, dst, spi, keytype) - char *buf; u int type; struct sockaddr *src; struct sockaddr *dst; u int32 spi; u_int keytype; char *cp, *p; DPRINTF (IDL_FINISHED, ("Entering key _createkey\n") ) ; if (!buf II !src return(-l) ; II !dst) cp = buf; bcopy((char *)&type, cp, sizeof(type)); cp += sizeof(type); /* * Assume only IPv4 and IPv6 addresses. */ #define ADDRPART(a) \ ((a)->sa family ~= AF INET6) ? \ (char *)&(((struct sockaddr in6 *) (a))->sin6 addr) (char *) &( ((struct sockaddr in *) (a)) ->sin_addr) #define ADDRSIZE(a) \ ((a)->sa family == AF INET6) sizeof(struct in_addr) \ ? sizeof(struct in_addr6) \ DPRINTF (IDL GROSS EVENT, (" src addr: \n" ) ) ; DDO(IDL GROSS EVENT,dump smart sockaddr(src)); DPRINTF (IDL GROSS EVENT, (" ds t addr: \n" ) ) ; DDO (IDL,_GROSS_EVENT, dump_smart_sockaddr (dst) ) ; p = ADD RPART (src) ; bcopy(p, cp, ADDRSIZE(src)); cp += ADDRSIZE(src); p = ADDRPART(dst); bcopy(p, cp, ADDRSIZE(dst)); cp += ADDRSIZE(dst); #undef ADDRPART #undef ADDRSIZE if (keytype == 0) { bcopy((char *)&spi, cp, sizeof(spi)); cp += sizeof(spi); DPRINTF(IDL FINISHED, ("hash key:\n")); DDO(IDL FINISHED, dump_buf(buf, cp - buf)); return(cp - buf); 8 DEF00007949 key.c 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 9/28/1995 /*---------------------------------------------------------------------* key sosearch() : * Search the s02spi table for the security association allocated to the socket. Returns pOinter to a struct key_so2spinode which * can * be used to locate the security association entry in the key table. ----------------------------------------------------------------------* / struct key s02spinode * key_sosearch(type, src, dst, so) u int type; struct sockaddr *src; struct sockaddr *dst; struct socket *so; struct key_s02spinode *np = 0; if 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 5G8 569 570 571 572 573 574 575 576 (! (sre && dst)) { DPRINTF(IDL CRITICAL, (lIkey_sosearch: got null src or dst pointer!\n")) ; return (NULL) ; for (np = s02spitbl [( (u int32) so) % S02SPITBLSIZE) .next; np; np = np-> next) { if «so == np->socket) && (type == np->keynode->secassoc->type) && addrpart equal (src, (struct sockaddr *)&(np->keynode->secassoc->src)) && addrpart equal (dst, (struct sockaddr *)&(np->keynode->secassoc->dst))) return (np) ; } return (NULL) ; /*---------------------------------------------------------------------* key sodelete() : * Delete entries from the s02spi table. * flag = 1 purge all entries * flag = 0 delete entries with socket pointer matching socket ----------------------------------------------------------------------* / void key sodelete(socket, flag) - struct socket *socket; int flag; struct key s02spinode *prevnp, *np; int s = splnet(); DPRINTF (IDL MAJOR EVENT, ("Entering keysodelete w!so=Ox%x flag=%d\n", socket,flaq)); i f (flag) { int i; for (i = 0; i < S02SPITBLSIZE; i++) for(np = s02spitbl[i) .next; np; np KFree (np) ; } splx(s) ; return; np->next) { 577 578 prevnp &s02spitbl [( (u_int32) socket) % S02SPITBLSIZE) ; 9 DEF00007950 key.c 9/28/1995 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 for(np = prevnp->next; np; np = np->next) { if (np->socket == socket) { struct socketlist *socklp, *prevsocklp; (np->keynode->alloc_count)--; /* * * * * */ if (np->keynode->secassoc->state & K UNIQUE) if (key delete(np->keynode->secassoc) != 0) panic ("key_sodelete") ; np = prevnp; continue; } 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 If this socket maps to a unique secassoc, we go ahead and delete the secassoc, since it can no longer be allocated or used by any other socket. /* * We traverse the socketlist and remove the entry * for this socket */ DPRINTF(IDL FINISHED, ("keysodelete: deleting from socklist ... ")); prevsocklp = np->keynode->solist; for (socklp = prevsocklp->next; socklpi socklp = socklp->next) { if (socklp->socket == socket) { prevsocklp->next = socklp->next; KFree (socklp) ; break; } prevsocklp } = socklp; DPRINTF(IDL FINISHED, ("done\n")); prevnp->next = np->next; KFree (np) ; np = prevnp; prevnp = np; } splx(s); /*---------------------------------------------------------------------* key deleteacquire() : Delete an entry from the key acquirelist * ----------------------------------------------------------------------* / void key_deleteacquire(type, target) u int type; struct sockaddr *target; struct key_acquirelist *ap, *prev; prev = key acquirelist; for(ap = key acquirelist->next; ap; ap = ap->next) { if (addrpart equal (target, (struct sockaddr *)&(ap->target)) && (type == ap->type)) { DPRINTF(IDL MAJOR EVENT, ("Deleting entry from acquire list!\n")); prev->next = ap->next; KFree (ap) ; ap prev; prev ap; 10 DEF00007951 key.c 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 9/28/1995 /*---------------------------------------------------------------------* key search(): * Search the key table for an entry with same type, src addr, dest * addr, and spi. Returns a pointer to struct key tblnode i f found * else returns null. ----------------------------------------------------------------------* / struct key tblnode * key_search (type, src, dst, spi, indx, prevkeynode) u int type; struct sockaddr *src; struct sockaddr *dst; u int32 spi; int indx; struct key_tblnode **prevkeynode; struct key_tblnode *keynode, *prevnode; if (indx > KEYTBLSIZE I I indx < 0) return (NULL) i i f (! (&keytable [indx) ) ) return (NULL) i #define #define #define #define sec type keynode->secassoc->type sec spi keynode->secassoc->spi sec src keynode->secassoc->src sec_dst keynode->secassoc->dst prevnode = &keytable[indx); for (keynode ~ key table [indx) .next; keynode; keynode keynode - >next) { 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 if ((type == sec type) && (spi == sec spi) && addrpart equal (src, (struct sockaddr *)&(sec src» && addrpart equal (dst, (struct sockaddr *)&(sec dst») break; prevnode = keynode; } *prevkeynode = prevnode; return (keynode) ; /*---------------------------------------------------------------------- * key addnode() : i\ * Insert a key __ tblnode entry into the key table. Returns a pointer * to the newly created key tblnode. ----------------------------------------------------------------------* / struct key tblnode * key addnode(indx, secassoc) - int indx; struct ipsec_assoc *secassoc; struct key_tblnode *keynodei DPRINTF (IDL GROSS EVENT, ("Entering key addnode w/indx=%d secassoc=Ox%x\n", indx, (u_int32) secassoc) ) ; 701 702 if (! (&keytable[indx)) 703 return (NULL) ; 704 i f (! secas soc) { 705 panic(flkey addnode: Someone passed in a null secassoc!\nfl); 706} 11 DEF00007952 key.c 9/28/1995 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 K Malloc(keynode, struct key_tblnode * sizeof(struct key_tblnode)); if (keynode == 0) return (NULL) ; bzero( (char *)keynode, sizeof(struct key_tblnode)); K Malloc(keynode->solist, struct socketlist *, sizeof(struct socketlist) ) ; if (keynode->solist ;= 0) KFree(keynode) ; return (NULL) ; } bzero «char *) (keynode->solist), sizeof (struct socketlist)); keynode->secassoc = secassoc; keynode->solist->next ; NULL; keynode->next = key table [indx] .next; key table [indx] .next ; keynode; return (keynode) ; /*---------------------------------------------------------------------* key add () : Add a new security association to the key table. Caller is * * responsible for allocating memory for the struct ipsec_assoc as * well as the buffer space for the key and iv. security * association passed in is well-formed. Assumes the ----------------------------------------------------------------------* / int key_add (secassoc) struct ipsec_assoc *secassoc; char buf[MAXHASHKEYLEN]; int len, indx; int inbound = 0; int outbound; 0; struct key tblnode *keynode, *prevkeynode; struct key allocnode *np; int s; DPRINTF(IDL GROSS EVENT, secassoc)) ;if } (IJEntering key_add w/secassoc=Ox%x\n lJ , (l secassoc) { panic(nkey add: who the hell is passing me a null pointer lJ ) ; - /* * * For storage purposes, the two esp modes are treated the same. */ if (secassoc->type S8 ENCRYPTION NETWORK) secassoc->type ; SS_ENCRYPTION_TRAN8PORT; /* * Should we allow a null key to be inserted into the table ? * or can we use null key to indicate some policy action ... */ /* * For esp using des-cbc or tripple-des we call * des set odd parity. */ - - - 12 DEF00007953 key.c 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 9/28/1995 if (secassoc->key && (secassoc->type == SS ENCRYPTION TRANSPORT) && ((secassoc->algorithm == IPSEC ALGTYPE ESP DES CBC) I I (secassoc->algorithm == IPSEC ALGTYPE ESP 3DES))) des_set_oddyarity (secassoc->key) ; -- /* * Check if secassoc with same spi exists before adding */ bzero((char *)&buf, sizeof(buf)); len = key_createkey((char *)&buf, secassoc->type, (struct sockaddr *)&(secassoc->src), (struct sockaddr *)&(secassoc->dst), secassoc->spi, 0); indx = key gethashval((char *)&buf, len, KEYTBLSIZE); DPRINTF(IDL GROSS EVENT, (Ilkeyadd: keytbl hash position=%d\n ll , indx)); keynode key search(secassoc->type, (struct sockaddr *) &(secassoc-> src) , (struct sockaddr *)&(secassoc->dst), secassoc->spi, indx, &prevkeynode); if (keynode) { DPRINTF (IDL MAJOR EVENT, (II keyadd: secas soc already exists! \n II) ) ; return(-2) ;inbound = my addr((struct sockaddr *)&(secassoc->dst)); outbound = my addr((struct sockaddr *)&(secassoc->src)); DPRINTF (IDL_FINISHED, (" inbound=%d outbound=%d\n", inbound, outbound)); 796 /* 797 798 799 800 801 802 803 804 805 806 807 808 809 810 * * * * We allocate mem for an allocation entry if needed. This is done here instead of in the allocaton code segment so that we can easily recover/cleanup from a memory allocation error. */ if (outbound I I (! inbound && ! outbound)) { K Malloc(np, struct key allocnode *, sizeof(struct key_allocnode)); i f (np == 0) { DPRINTF(IDL CRITICAL, (Ilkeyadd: can't allocate allocnode!\n ll ) ) ; return (-1) ; - s = splnet(); 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 if ((keynode = key addnode(indx, secassoc)) == NULL) { DPRINTF (IDL CRITICAL, ("lceyadd: key_addnode failed! \n") ) ; i f (np) KFree (np) ; splx(s) ; return(-l) ; } DPRINTF (IDL EVENT, (" Added new keynode: \n") ) ; DDO(IDL GROSS EVENT, dump keytblnode(keynode)); DDO(IDL_GR088_EVENT, dump_secassoc(keynode->secassoc)); /* * * * * * * * We add an entry to the allocation table for this secas soc if the interfaces are up and the secassoc is outbound. In the case where the interfaces are not up, we go ahead and do it anyways. This wastes an allocation entry if the secas soc later turned out to be inbound when the interfaces are ifconfig up. */ if (outbound I I (!inbound && !outbound)) { len = key createkey((char *)&buf, secassoc->type, (struct sockaddr *)&(secassoc->src), 13 DEF00007954 9/28/1995 key.c (struct sockaddr *)&(secassoc->dst), 835 836 837 838 0, 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 1); indx = key gethashval«char *)&buf, len, KEYALLOCTBLSIZE); DPRINTF (IDL GROSS EVENT, ("keyadd: keyalloc hash position=%d\n", indx)) i np->keynode = keynodei np->next = keyalloctbl[indx) .next; keyalloctbl[indx) .next = np; } if (inbound) secassoc->state 1= K_INBOUND; i f (outbound) secassoc->state 1= K_OUTBOUND; key deleteacquire(secassoc->type, *)&(secassoc->dst)) i (struct sockaddr splx(s); return 0; /*---------------------------------------------------------------------* key get () : * Get a security association from the key table. ----------------------------------------------------------------------* / int key_get (type, src, dst, spit secassoc) u int type; struct sockaddr *srci struct sockaddr *dst; u int32 spi; struct ipsec_assoc **secassoc; char buf[MAX~~SHKEYLEN); struct key tblnode *keynode, *prevkeynode; int len, indx; /* * * For storage purposes, the two esp modes are treated the same. */ .i. f (type == 88 ENCRYPTION NETWORK) type = SS_ENCRYPTION_TRANSPORTi bzero(&buf, sizeof(buf)); *secassoc = NULL; len = key createkey«char *)&buf, type, src, dst, spi, 0); indx = key gethashval«char *)&buf, len, KEYTBLSIZE) i DPRINTF (IDL GROSS EVENT, ("keyget: indx=iI>d\n", indx) ) ; keynode = key search(type, src, dst, spi, indx, &prevkeynode) i if (keynode) { DPRINTF(IDL_EVENT, (IIkeyget: found it! keynode=Ox%x",keynode»); *secassoc = keynode->secassoc; return(O); else return(-l); /* Not found */ /*---------------------------------------------------------------------- * key dump(): * Dump all valid entries in the key table to a pf key socket. Each * security associaiton is sent one at a time in a pf_key message. A 897 * message with seqno = 0 signifies the end of the dump 14 DEF00007955 key.c 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 9/28/1995 transaction. ----------------------------------------------------------------------* / int key_dump (so) struct socket *so; int len. i; int seq = 1; struct mbuf *mi struct key msgdata keyinfo; struct key msghdr *km; struct key tblnode *keynode; extern struct sockaddr key srCi extern struct sockaddr key_dst; /* * Routine to dump the key table to a routing socket * Use for debugging only! */ 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 DPRINTF(IDL GROSS EVENT. ("Entering key_dump() ")) ; /* -* We need to speed this up later. Fortunately. key dump * messages are not sent often. */ for (i = 0; i < KEYTBLSIZE; itt) { for (keynode = keytable[iJ .next; keynode; keynode = keynode->next) /* * We exclude dead/larval/zombie security associations for now * but it may be useful to also send these up for debugging purposes */ if (keynode->secassoc->state & (K_DEAD continue; K LARVAL K_ZOMBIE)) len = (sizeof(struct key msghdr) + ROUNDUP(keynode->secassoc->src.sin6 len) t ROUNDUP(keynode->secassoc->dst.sin6 len) + ROUNDUP (keynode->secassoc->from. sin6_len) + ROUNDUP (keynode->secassoc->keylen) + ROUNDUP (keynode->secassoc->ivlen)) ; K Malloc (km. struct key msghdr *. len); i f (km == 0) return (ENOBUFS) ; if (key secassoc2msghdr(keynode->secassoe. km. &keyinfo) panic ("key dump") i km->key msglen = len; km->key msgvers = KEY VERSION; km->key msgtype = KEY DUMP; km->key pid = eurproe->p_pid; km->key seq = seq++; km->key errno = 0; MGETHDR(m. M WAIT. MT DATA) ; m->m len = m->m pkthdr.len 0; m->m next = 0; m->m nextpkt = 0; m->m pkthdr.rcvif = 0; m copyback(m. O. len. (caddr_t)l<m); KFree (km) ; if (sbappendaddr(&so->so rev. &key src. m. (struet mbuf m free (m) ; - else sorwakeup(so); } != *) 0) 0) 0) 961 15 DEF00007956 key.c 9/28/1995 K Malloc(km, struct key msghdr *, sizeof(struct key_msghdr)); if (km == 0) return (ENOBUFS) ; bzero«char *)km, sizeof(struct key msghdr)); km->key msglen = sizeof (struct key_msghdr); km->key msgvers = KEY VERSION; km->key msgtype = KEY DUMP; km->key pid = curproc->p pid; km->key seq = 0; km->key errno = 0; MGETHDR(m, M WAIT, MT DATA) i m->m len = m->m-pkthdr.len 0; ffi->m next = 0; m->m nextpkt = 0; m->m pkthdr.rcvif = 0; m copyback(m, 0, km->key msglen, (caddr t)km); KFree (km) ; if (sbappendaddr(&so->so_rcv, &key_src, m, (struct mbuf *)0) fll free (m) ; else sorwakeup (so) ; DPRINTF (IDL GROSS EVENT, ( II Leaving key_dump ( ) \n II) ) ; return(O); - 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 0) /*---------------------------------------------------------------------* key delete(): * Delete a security association from the key table. ----------------------------------------------------------------------* / 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 int key delete (secassoc) - struct ipsec_assoc *secassoc; char buf[MAXHASHKEYLEN]; int len, indx; struct key tblnode *keynode = 0; struct key tblnode *prevkeynode = 0; struct socketlist *socklp, *deadsocklp; struct key so2spinode *np, *prevnp; struct key_allocnode *ap, *prevap; int s; DPRINTF(IDL GROSS EVENT, ("Entering key delete w/secassoc=Ox%x\n", secassoc) ) ; if (secassoc->type == 8S ENCRYPTION NETWORK) secas soc - >type = SSENCRYPTION_TRANSPORT; bzero«char *)&buf, sizeof(buf)); len = key_createkey«char *)&buf, secassoc->type, (struct sockaddr *)&(secassoc->src), (struct sockaddr *)&(secassoc->dst), secassoc->spi, 0); indx = key gethashval«char *)&buf, len, KEYTBLSIZE); DPRINTF(IDL_GROSS_EVENT, (Ilkeydelete: keytbl hash position=%d\n", indx)); keynode key_search (secassoc->type, (struct sockaddr *)&(secassoc-> src) , (struct sockaddr *)&(secassoc->dst) , secassoc->spi, indx, &prevkeynode); if (keynode) { s = spInet () ; DPRINTF(IDL EVENT, ("keydelete: found keynode to delete\n")); keynode->secassoc->state 1= K_DEADi 16 DEF00007957 key.c 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 9/28/1995 if (keynode->ref count > 0) { DPRINTF(IDL MAJOR EVENT, ("keydelete: secas soc still held, marking for deletion only!\n")); splx(s) ; return (0) ; prevkeynode->next = keynode->next; /* * * * Walk the socket list and delete the entries mapping sockets to this secas soc from the so2spi table. */ DPRINTF (IDL GROSS EVENT, ("keydelete: deleting socklist ... ") ) ; for(socklp = keynode->solist->next; socklpi ) f prevnp = &S02spitbl [( (u int32) (socklp->socket)) % S02SPITBLSIZE) ; for(np = prevnp->next; np; np = np->next) { if ((np->socket == socklp->socket) && (np->keynode == keynode)) { prevnp->next = np->next; KFree (np) ; break; prevnp = np; } deadsocklp = socklPi socklp = socklp->next; KFree(deadsocklp) ; } DPRINTF (IDL GROSS EVENT, (" done \n" ) ) ; /* -- * If an allocation entry exist for this * secassoc, delete it. */ bzero((char *)&buf, sizeof(buf)); len = key createkey((char *)&buf, secassoc->type, (struct sockaddr *)&(secassoc->src). (struct sockaddr *)&(secassoc->dst), 0, 1); indx = key gethashval((char *)&buf, len, KEYALLOCTBLSIZE) i DPRINTF(IDL GROSS EVENT, ("keydelete: alloctbl hash position=%d\n", indx)) ; prevap = &keyalloctbl[indx); for (ap = prevap->next; ap; ap = ap->next) { if (ap->keynode == keynode) { prevap->next = ap->next; KFree (ap) i break; } prevap = ap; if (keynode->secassoc->iv) KFree(keynode->secassoc->iv); if (keynode->secassoc->key) KFree(keynode->secassoc->key) ; KFree(keynode->secassoc) i if (keynode->solist) KFree(keynode->solist) ; KFree(keynode); splx(s) i return(O) ; } return(-l) ; 17 DEF00007958 key.c 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 9/28/1995 /*---------------------------------------------------------------------* key flush () ; Delete all entries from the key table. * ----------------------------------------------------------------------* / void key flush (void) { - struct key_tblnode *keynode; int i; /* * T.his is slow, but. simple. */ DPRINTF(IDL FINISHED, ("Flushing key table ... "»); for (i = O;-i < KEYTBLSIZE; i++) f while (keynode = keytable[il .next) if (key delete (keynode- >secassoc) ! = 0) panic ("key_flush") ; } DPRINTF(IDL_FINISHED, (fldone\n"»; /*---------------------------------------------------------------------* key getspi () : Get a unique spi value for a key management daemon/program. * The spi value, once assigned, cannot be assigned again. * ----------------------------------------------------------------------* / int key getspi(type, src, dst, spi) - u int type; struct sockaddr *src; struct sockaddr *dst; u_int32 *spi; struct ipsec assoc *secassoc; struct key tblnode *keynode, *prevkeynode; int count ,-done , len, indx; int maxcount = 1000; u int32 val; char buf[MAXHASHKEYLEN); int S; DPRINTF(IDL MAJOR EVENT, ("Entering getspi w/type=%d\n" ,type) ); i f (! (src && dst) ret urn ( -1) ; /* * For storage purposes, the two esp modes are treated the same. * */ if (type == SS ENCRYPTION NETWORK) type = SS_ENCRYPT I ON_TRANSPORT; done = count = 0; do { count++; /* * Currently, valid spi values are 32 bits wide except for * the value of zero. This need to change to take into * account more restrictive spi ranges. * * TODO: Kebe says to allow key mgnt daemon to specify r:ange 18 DEF00007959 key.e 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 9/28/1995 * of valid spi to get. */ val random () ; DPRINTF(IDL FINISHED, ("%u ",val»; i f (val) { DPRINTF (IDL FINISHED, (" \n") ) ; bzero(&buf,-sizeof(buf») ; len = key ereatekey( (char *)&buf, type, sre, dst, val, 0); indx = key gethashval((ehar *)&buf, len, KEYTBLSIZE); if(!key search (type, sre, dst, val, indx, &prevkeynode» s = spInet () ; K Malloc(secassoc, struct ipsec as soc *, sizeof(struct ipsec assoc»; i f (secassoc == 0) { DPRINTF(IDL CRITICAL, ("key getspi: can't allocate memory\n"»; splx(s); return (-1) ; } bzero((char *)secassoc, sizeof(struct ipsec_assoc»; DPRINTF (IDL FINISHED, ("getspi: indx=%d\n", indx) ) ; secassoc->len = sizeof(struct ipsec_assoc); secassoc->type = type; secassoc->spi = val; secassoc->state 1= K LARVAL; if (my addr((struct sockaddr *)&(secassoc->dst») secassoc->state 1= K INBOu~D; if (my addr((struct sockaddr *)&(secassoc->src») secassoc->state 1= K_OUTBOUND; bcopy((char *)src, bcopy ((char *) dst, secassoc->from.sin6 secassoc->from.sin6 (char *)&(secassoc->src), src->sa len); (char *) &(secassoc->dst), dst->sa len); family - AF INET6; len sizeof(struct sockaddr_in6); /* * * * * * We need to add code to age these larval key table entries so they don't linger forever waiting for a KEY UPDATE message that may not come for various reasons. This is another task that key reaper can do once we have it coded. - */ secassoc->lifetime1 if time.tv_sec + maxlarvallifetime; (! (keynode = key addnode(indx, secassoc») { DPRINTF(IDL CRITICAL, ("key getspi: can't add node\n"»; splx(s); return (-1) ; } DPRINTF (IDL_FINISHED, ("key_getspi: added node Ox%x\n",keynode»; done++; splx(s) ; } } } whi le (( coun t < maxcount) && ! done) ; DPRINTF(IDL FINISHED, (Ilgetspi returns w/spi=%u,count=%d\n",val,count»; i f (done) { *spi = val; return (0) ; else { *spi = 0; return(-l) ; 19 DEF00007960 key.c 1219 1220 1221 1222 1223 9/28/1995 /*---------------------------------------------------------------------* key update () : * Update a key table entry that has an spi value assigned but is * incomplete (e.g. no key/iv). ----------------------------------------------------------------------* / 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 int key update (secassoc) - struct ipsec_assoc *secassoc; struct key tblnode *keynode, *prevkeynode; struct key allocnode *np = OJ 1.1 int8 newstate; int len, indx, inbound, outboundj char buf[MAXHASHKEYLEN]; int Si /* " " */ if For storage purposes, the two esp modes are treated the same. (secassoc->type == SS ENCRYPTION NETWORK) secassoc->type = SS_ENCRYPTION_TRANSPORTi bzero(&buf, sizeof(buf)); len = key createkey«char *)&buf, secassoc->type, (struct sockaddr *)&(secassoc->src), (struct sockaddr *)&(secassoc->dst), secassoc->spi, 0) i indx = key gethashval«char *)&buf, len, KEYTBLSIZE}; if(! (keynode = key search(secassoc->type, (struct sockaddr *)&(secassoc->src), (struct sockaddr *)&(secassoc->dst), secassoc->spi, indx, &prevkeynode))) return (ESRCH) ; } if (keynode->secassoc->state & K_DEAD) return (ESRCH) i /* Should we also restrict updating of only LARVAL entries ? */ s = spInet () i inbound = my addr«struct sockaddr *)&(secassoc->dst)); outbound = my_addr«struct sockaddr ")&(secassoc->src)) i newstate = keynode->secassoc->state; news tate &= -K LARVAL; i f ( inbound) newstate 1= K_INBOUNDi i f (outbound) newstate 1= K_OUTBOUNDi if (outbound II (!inbound && !outbound)) { K Malloc(np, struct key allocnode *, sizeof(struct key_allocnode)); i f (np == 0) { DPRINTF(IDL CRITICAL, (Hkeyupdate: can't allocate allocnode!\nH)); splx(s); return (ENOBUFS) i /* * * * We now copy the secassoc over. We don't need to copy the key and iv into new buffers since the calling routine does that already. */ 20 DEF00007961 9/28/1995 key.c 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 * (keynode->secassoc) = *secassoc; keynode->secassoc->state = newstate; /* * Should we allow a null key to be inserted into the table ? * or can we use null key to indicate some policy action ... */ if (keynode->secassoc->key && (keynode->secassoc->type == SS ENCRYPTION TRANSPORT) && «keynode->secassoc->algorithm-== IPSEC ALGTYPE ESP DES CBC) (keynode->s8cassoc->algorithm == IPSEC ALGTYPE_ESP_3DES)))des_set_odd_parity(keynode->secassoc->key) ; /* * 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 We now add an entry to the allocation table for this updated key table entry. * */ if (outbound II (! inbound && !outbound)) { len = key createkey( (char *)&buf, secassoc->type, (struct sockaddr *)&(secassoc->src), (struct sockaddr *)&(secassoc->dst), 0, 1) i indx = key gethashval«char *)&buf, len, KEYALLOCTBLSIZE) i DPRINTF(IDL FINISHED, ("keyupdate: keyalloc hash position=%d\n", indx) ) ; np->keynode = keynode; np->next = keyalloctbl[indx) .next; keyalloctbl[indx) .next = np; 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 II key deleteacquire (secassoc- >type, *)&(secassoc->dst)) ; (struct sockaddr splx(S) ; return(O) ; /*---------------------------------------------------------------------* key register(): * Register a socket as one capable of acquiring security associations * for the kernel. ----------------------------------------------------------------------* / int key register (socket, type) - struct socket *socket; u_int type; struct key registry *p, *new; int s = splnet(); DPRINTF (IDL MAJOR EVENT, ( "Entering key_register w/so=Ox%x, type=%d\n", socket,type)); if (! (keyregtable && socket)) panic ("key_register"); /* * Make sure entry is not already in table */ for(p = keyregtable->next; Pi P = p->next) { if «p->type == type) && (p->socket == socket)) splx(s) ; return (EEXIST) ; 21 DEF00007962 key.c 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1.359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 9/28/1995 K Malloc(new, struct key_registry * (new == 0) { splx(s) ; return(ENOBUFS) ; sizeof(struct key_registry)); if new->type = type; new->socket = socket; new->next = keyregtable->next; keyregtable->next = new; sp1x (8) ; return(O) ; /*---------------------------------------------------------------------* key unregister() : * Delete entries from the registry list. * a11f1ag = 1 : delete all entries with matching socket * allf1ag = 0 : delete only the entry matching socket and type ----------------------------------------------------------------------* / void key unregister(socket, type, allflag) - struct socket *socket; u int type; int allflag; struct key registry *p, *prev; int s = splnet(); DPRINTF (IDL Mll.JOR EVENT, ("Entering key unregister w/so=Ox%x,type=%d,flag=%d\n",socket, type, allflag)); (1 (keyregtable && socket)) panic ("key register"); prey = keyregtable; for(p = keyregtable->next; p; p = p->next) { if «allflag && (p->socket == socket)) II «p->type == type) && (p->socket == socket))) prev->next = p->next; KFree (p) ; p = prey; if prey = p; } splx (s) ; /*---------------------------------------------------------------------* key acquire() : * Send a key acquire message to all registered key mgnt daemons * capable of acquire security association of type type. * * * Return: 0 if succesfully called key mgnt. daemon(s) -1 if not successfull. ----------------------------------------------------------------------* / int key_acquire (type, src, dst) u int type; struct sockaddr *src; struct sockaddr *dsti { struct key_registr"y *p; 22 DEF00007963 9/28/1995 key.c 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 struct key acquirelist *ap, *prevap; int success = 0, created = 0; struct socket *last = 0; struct mbuf *m = 0; u int etype; extern struct sockaddr key_src; DPRINTF (IDL_MAJOR_EVENT, ( tlEntering key_acquire () \ntl) ) ; if (!keyregtable return (-1); 1456 1457 1465 1466 1467 1468 II !dst) /* * Since we're already looking at the list, we may as * well delete expired entries as we scan through the list. * This should really be done by a function like key reaper() * but until we code key reaper(), this is a quick and dirty * hack. */ DPRINTF(IDL MA.JOR EVENT, ("found an expired entry ... del.eting it!\n tl )); prevap->next = ap->next; KFree (ap) ; ap = prevapi 1454 1460 1461 1462 1463 1464 !src /* * We first check the acquirelist to see if a key acquire * message has been sent for this destination. */ etype = type; if (etype == SS ENCRYPTION NETWORK) etype = SS ENCRYPTION TRANSPORT; prevap = key acquirelist; for(ap = key acquirelist->next; ap; ap = ap->next) { if (addrpart equal (dst, (struct sockaddr *)&(ap->target)) && (etype == ap->type)) { DPRINTF(IDL MAJOR EVENT, (tlacquire message previously sent!\n tl )); if (ap->expiretime < time.tv sec) { DPRINTF(IDL MAJOR EVENT, (tlacquire message has expired!\n tl )); ap->count =-0; break; } if (ap->count < maxkeyacquire) { DPRINTF(IDL MAJOR EVENT, ("max acquire messages not yet exceeded!\n")); break; } return (0) ; else if (ap->expiretime < time.tv_sec) 1455 1458 1459 II prevap = ap; /* * Scan registry and send KEY ACQUIRE message to * appropriate key management daemons. */ for(p = keyregtable->next; p; p if (p->type != type) continue; = p->next) (! created) { struct key msghdr *km; int len; - 1469 if 1470 1471 1472 1473 1474 1475 len = sizeof(struct key_msghdr) + ROUNDUP (src->sa_len) ROUNDUP(dst->sa len) ; K_Malloc (km, st:ruct key_msghdr * len); + 23 DEF00007964 key.c 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 J.488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 J.507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 153J. 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 9/28/1995 i f (km == 0) { DPRINTF(IDL CRITICAL, ("key acquire: no memory\n"»; return ( -1) ; - } DPRINTF (IDL FINISHED, ("key acquire/created: 1 \n") ) ; bzero((char-*)km, len); km->key msglen = len; km->key msgvers = KEY VERSION; km->key msgtype = KEY ACQUIRE; km->type = type; DPRINTF (IDL FINISHED, ("key acquire/created: 2\n"»; /* * This is inefficient and slow. */ /* * We zero out sin zero here for AF INET addresses because * ip_output() currently does not do it for performance reasons. */ if (src->sa family == AF INET) bzero((char *) (&((struct sockaddr in *)src)->sin zero), sizeof(((struct sockaddr in-*)src)->sin zero»; if (dst->sa family == AF INET) bzero( (char *) (&( (struct sockaddr in *)dst) ->sin zero), sizeof (( (struct sockaddr_in---*) dst) ->sin_zero»; bcopy( (char *)src, (char *) (km + 1), src->sa len); bcopy( (char *)dst, (char *) ((int) (km + 1) + ROUNDUP(src->sa_len», dst->sa len); DPRINTF(IDL FINISHED, (Ilkey acquire/created: 3\n"»; MGETHDR(m, M WAIT, MT DATA); m->m len = m->m pkthdr.len = 0; m->m next = 0; m->m nextpkt = 0; m->m pkthdr.rcvif = 0; m copyback(m, 0, len, (caddr t)km); KFree (km) ; DPRINTF(IDL FINISHED, ("key acquire/created: 4\n"»); DDO(IDL FINISHED,dump mchain(m); created++; } (last) { stL'uct rnbuf -kn; if (n = m copy(m, 0, (int)M COPYALL» { i f (sbappendaddr(&last->so rcv, &key_src, n, (struct mbuf *)0) m freem (n) i else { sorwakeup(last) ; success++; } } DPRINTF(IDL_FINISHED, ("key_acquire/last: l\n") i if last = 0) p->socket; } i f (last) { if (sbappendaddr(&last->so_rcv, &key_src, rn, (struct mbuf *)0) -= 0) m freem (m) ; else { sorwakeup(last) ; success++; } DPRINTF(IDL_FINISHED, ("key_acquire/last: 2\n"» i else m_freem (m) ; /* 24 DEF00007965 key.c 9/28/1995 1543 1544 1545 1546 1547 1548 * (success) { (!ap) { DPRINTF(IDL MAJOR EVENT, ("Adding new entry in acquirelist\n")); K Malloc(ap, struct key acquirelist *, sizeof(struct key acquirelist)); i f (ap == 0) return(success ? 0 : -1); bzero((char *)ap, sizeof(struct key acquirelist)) i bcopy((char *)dst, (char *)&(ap->target), dst->sa_len)i ap->type = etypei ap->next = key acquireli.st->nexti key_acquirelist->next = ap; } DPRINTF (IDL EVENT, ("Updating acquire counter and expiration time\n") ); ap->count++; ap->expiretime = time.tv sec + rnaxacquiretirnei if if 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 157l 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 Update the acquirelist */ } DPRINTF (IDL MAJOR EVENT, (" key acquire: done! success=%d\n", success) ) ; return(success ? 0 : -1) i - /*---------------------------------------------------------------------- * key alloc(): * Allocate a security association to a socket. A socket requesting * unique keying (per-socket keying) is assigned a security assocation * exclusively for its use. Sockets not requiring unique keying are * assigned the first security association which rnay or may not be * used by another socket. ----------------------------------------------------------------------* / int key alloc(type, src, dst, socket, unique_key, keynodep) - u int type; struct sockaddr *src; struct sockaddr *dst; struct socket *socket; u int unique key; struct key_tblnode **keynodep; struct key tblnode *keynode; char buf[M~~HASHKEYLENl; struct key allocnode *np, *prevnp; struct key so2spinode *nemlp; int len; int indx; DPRINTF (IDL GROSS EVENT, ("Entering key _alloc w/type=%u! \n", type) ) ; i f (! (s rc && ds t)) { DPRINTF(IDL CRITICAL, ("key alloc: received null src or dst!\n")) i return(-I);- /* * * We treat esp-transport mode and esp-tunnel mode as a single type in the key table. */ if (type == S8 ENCRYPTION NETWORK) type = SS ENCRYPTION_TRANSPORT; /* * Sear'ch key allocation table 25 DEF00007966 9/28/1995 key.c 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 */ bzero((char *)&buf, sizeof(buf)); len = key createkey((char *)&buf, type, src, dst, 0, 1); indx = key_gethashval((char *)&buf, len, KEYALLOCTBLSIZE); #define #define #define #define np type np->keynode->secassoc->type np state np->keynode->secassoc->state np src (struct sockaddr *)&(np->keynode->secassoc->src) np_dst (struct sockaddr *)&(np->keynode->secassoc->dst) prevnp = &keyalloctbl[indx]; for (np = keyalloctbl[indx] .next; npi np = np->next) { if ((type == np type) && addrpart_equal(src, np_src) && addrpart equal (dst, np dst) && ! (np_state & (K LARVAL-, K_DEAD K_UNIQUE))) if (! (unique key)) break; if (! (np state & K_USED)) breaki - } prevnp if npi (np) { struct key s02spinode *newp; struct socketlist *newsPi int s = splnet(); DPRINTF(IDL MAJOR EVENT, ("keY alloc: found node to allocate\n")); keynode = np->keynode; K Malloc(newnp, struct key_so2spinode *, sizeof(struct key so2spinode)) i i f (newnp == 0) { DPRINTF(IDL CRITICAL, ("key alIoc: Can't alloc mem for s02spi node! \n") ) ; splx(s) ; return (ENOBUFS) i } K Malloc(newsp, struct socketlist *, sizeof(struct socketlist)); if (newsp == 0) { DPRINTF(IDL CRITICAL, ("key_alloc: Can't alloc mem for socketlist!\n")) ; if (newnp) KFree (newnp) ; splx(s); return (ENOBUFS) ; /* * * Add a hash entry into the so2spi table to map socket to allocated Eecassoc. */ DPRINTF(IDL GROSS EVENT, ("key alloc: adding entry to s02spi table ... " ) ) ; newnp->keynode = keynodei newnp->socket = socket; newnp->next = s02spitbl[((u int32) socket) % S02SPITBLSIZE] .next; so2spitbl [( (u int32) socket) -% S02SPITBLSIZE] .next = newnPi DPRINTF(IDL_GROSS_EVENT, ("done\n")); if (unique key) /* - { * Need to remove the allocation entry * since the secas soc is now unique and * can't be allocated to any other socket */ 26 DEF00007967 9/28/1995 key.c 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 16.89 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 DPRINTF(IDL MAJOR EVENT, ("key alloc: making keynode unique ... ")); keynode->secassoc->state 1= K_UNIQUE; prevnp->next = np->next; KFree (np) i DPRINTF (IDL_MAJOR_EVENT, ("done\n") ) ; } keynode->secassoc->state 1= K USED; keynode->secassoc->state 1= K_OUTBOUND; keynode->alloc_count++; /* * Add socket to list of socket using secassoc. */ DPRINTF(IDL GROSS EVENT, ("key alloc: adding so to solist ... ")); newsp->socket = socket; newsp->next = keynode->solist->next; keynode->solist->next = newsPi DPRINTF (IDL GROSS EVENT, (" done \n" ) ) i *keynodep =-keynode; splx(s) ; return(O) ; } *keynodep = NULL; return(O) ; /*---------------------------------------------------------------------* key free(): * Decrement the refcount for a key table entry. If the entry is * marked dead, and the refcount is zero, we go ahead and delete it. ----------------------------------------------------------------------* / void key free(keynode) - struct key_tblnode *keynode; DPRINTF (IDL MAJOR EVENT, ("Entering key_free w/keynode=Ox%x\n",keynode)) ; if (! keynode) { DPRINTF(IDL_CRITICAL, ("Warning: key_free got null pointer\n")); return; } (keynode->ref count)--; if (keynode->ref count < 0) { DPRINTF (IDL CRITICAL, ("Warning: key_free decremented refcount to %d\n" , keynode->ref_count) ) ; } if «keynode->secassoc->state & K_DEAD) && (keynode->ref_count <= 0)) 1704 1705 1706 1707 1708 1709 1710 1711 1712 { 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 DPRINTF(IDL MAJOR EVENT, ("key free: calling key_delete\n")) key_delete (keynode->secassoc) i i /*---------------------------------------------------------------------* getassocbyspi(): * Get a security association for a given type, src, dst, and spi. * * * Returns: 0 if sucessfull -1 if error/not found * * Caller must convert spi to host order. * in host order! Function assumes spi is ----------------------------------------------------------------------* 27 DEF00007968 key.c 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 9/28/1995 / int getassocbyspi(type, src, dst, spi, keyentry) u int type; struct sockaddr *src; struct sockaddr *dst; u int32 spi; struct key_tblnode **keyentry; char buf[MAXHASHKEYLEN]; int len, indx; struct key_tblnode *keynode, *prevkeynode = 0; DPRINTF (IDL GROSS EVENT, ("Entering getassocbyspi w/type=%u spi=%u\n", type,spi);- 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 /* * * We treat esp-transport mode and esp-tunnel mode as a single type in the key table. */ if (type == SS ENCRYPTION NETWORK) type = SS_ENCRYPTION_TRANSPORT; *keyentry = NULL; bzero(&buf, sizeof(buf»; len = key createkey((char *)&buf, type, src, dst, spi, 0); indx = key gethashval((char *)&buf, len, KEYTBLSIZE); DPRINTF (IDL FINISHED, ("getassocbyspi: indx=%d\n", indx» ; DDO(IDL FINISHED,dump sockaddr(src);dump sockaddr(dst» i keynode-= key search(type, src, dst, spi, indx, &prevkeynode); DPRINTF (IDL GROSS EVENT, (ligetassocbyspi: keysearch ret=Ox%x\n" ,keynode»; i f (keynode && ! (keynode->secassoc->state & (K DEAD I K LARVAL») DPRINTF(IDL EVENT, ("getassocbyspi: found secassoc!\n"»; (keynode->ref count)++; *keyentry = keynode; else { DPRINTF(IDL MAJOR EVENT, ("getassocbyspi: secas soc not found!\n"»; return (-1); } return(O) ; /*---------------------------------------------------------------------* getassocbysocket(): * Get a security association for a given type, src, dst, and socket. * If not found, try to allocate one. * Returns: 0 if successfull * -, if error condition/secassoc not found (*keyentry NULL) 1 if secassoc temporarily unavailable (*keynetry = * NULL) (e.g., key mgnt. daemon(s) called) * ----------------------------------------------------------------------* / int getassocbysocket(type, src, dst, socket, unique_key, keyentry) u int type; struct sockaddr *src; struct sockaddr *dst; struct socket *socket; u int uniq~e key; struct key_tblnode **keyentry; struct key_tblnode *keynode = 0; 28 DEF00007969 key.c 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 9/28/1995 struct key s02spinode *np; int len, indx; u int32 spi; u_int real type; DPRINTF(IDL GROSS EVENT, ("Entering getassocbysocket w/type=%u so=Ox%x\n",type,socket)) ; /* * * * * * We treat esp-transport mode and esp-tunnel mode as a single type in the key table. This has a side effect that socket USing both esp-transport and esp-tunnel will use the same security association for both modes. Is this a problem? */ real type = type; if (type == SS ENCRYPTION NETWORK) type = SS_ENCRYPTION_TRANSPORT; if (np = key sosearch(type, src, dst, socket)) { if (np->keynode && np->keynode->secassoc && ! (np->keynode->secassoc->state & (K DEAD I K LARVAL))) { DPRINTF (IDL FINISHED, ("getassocbysocket: found secas soc ! \n") ) ; (np->keynode->ref count)++; *keyentry = np->keynode; return (0) ; /* * No secassoc has been allocated to socket, * so allocate one, if available */ DPRINTF (IDL EVENT, (" getassocbyso: can I t find it, trying to allocate!\n")) i i f (key alloc(realtype, src, dst, socket, unique_key, &keynode) == 0) { if (keynode) { DPRINTF (IDL EVENT, ("getassocbyso: key_alloc found secas soc ! \n") ) ; keynode->ref count++; *keyentry = keynode; return (0) ; else { /* * Kick key mgnt. daemon(s) * (this should be done in ipsec output policy() lnstead or * selectively called based on a flag value) */ DPRINTF(IDL FINISHED, ("getassocbyso: calling key mgnt daemons! \n") ) ; *keyentry = NULL; if (key acquire (realtype, src, dst) == 0) return (1); else return(-l) ; } } *keyentry NULL; return(-l) ; 29 DEF00007970

Disclaimer: Justia Dockets & Filings provides public litigation records from the federal appellate and district courts. These filings and docket sheets should not be considered findings of fact or liability, nor do they necessarily reflect the view of Justia.


Why Is My Information Online?