aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/generic/pending-4.14/670-ipv6-allow-rejecting-with-source-address-failed-policy.patch
Commit message (Collapse)AuthorAgeFilesLines
* kernel: bump 4.14 to 4.14.52 for 18.06Stijn Segers2018-07-021-10/+10
| | | | | | | Compile-tested on: ramips/mt7621, x86/64. Run-tested on: ramips/mt7621. Signed-off-by: Stijn Segers <foss@volatilesystems.org>
* kernel: bump 4.14 to 4.14.43 for 18.06Koen Vandeputte2018-05-241-10/+10
| | | | | | | | | | | | | | | | | | | | | Refreshed all patches Dropped upstreamed patches: 522-PCI-aardvark-fix-logic-in-PCI-configuration-read-write-functions.patch 523-PCI-aardvark-set-PIO_ADDR_LS-correctly-in-advk_pcie_rd_conf.patch 525-PCI-aardvark-use-isr1-instead-of-isr0-interrupt-in-legacy-irq-mode.patch 527-PCI-aardvark-fix-PCIe-max-read-request-size-setting.patch updated patches: 524-PCI-aardvark-set-host-and-device-to-the-same-MAX-payload-size.patch 030-USB-serial-option-fix-dwm-158-3g-modem-interface.patch Added new ARM64 symbol: CONFIG_ARM64_ERRATUM_1024718 Compile-tested on: cns3xxx, imx6, mvebu (arm64), x86_64 Runtime-tested on: cns3xxx, imx6, x86_64 Signed-off-by: Koen Vandeputte <koen.vandeputte@ncentric.com>
* kernel: add missing in6_dev_put_clear call to an ipv6 network patchFelix Fietkau2018-05-021-4/+12
| | | | | | Fixes "unregister_netdevice: waiting for lo to become free. Usage count = 1" messages Signed-off-by: Felix Fietkau <nbd@nbd.name>
* kernel: bump 4.14 to 4.14.34Stijn Tintel2018-04-161-9/+9
| | | | | | | | | | | | | Refresh patches. Update patches that no longer apply: - backport/313-netfilter-remove-defensive-check-on-malformed-packet.patch - pending/642-net-8021q-support-hardware-flow-table-offload.patch Compile-tested: x86/64. Runtime-tested: x86/64. Signed-off-by: Stefan Lippers-Hollmann <s.l-h@gmx.de> Signed-off-by: Stijn Tintel <stijn@linux-ipv6.be>
* kernel: bump to version 4.14.25Kabuli Chana2018-03-091-9/+9
| | | | | | compile/test target mvebu/rango Signed-off-by: Kabuli Chana <newtownBuild@gmail.com>
* kernel: bump 4.14 to 4.14.11Stijn Tintel2018-01-031-5/+5
| | | | | | | | | | | | | | | Rename unwinder config symbols to match upstream changes. Refresh patches. Update patch that no longer applies: 202-reduce_module_size.patch Also enable CONFIG_PAGE_TABLE_ISOLATION. This feature was backported from 4.15 to the 4.14 stable series. It is enabled by default, so enable it in OpenWrt as well. Compile-tested on x86/64. Runtime-tested on x86/64. Signed-off-by: Stijn Tintel <stijn@linux-ipv6.be>
* kernel: generic: Add kernel 4.14 supportHauke Mehrtens2017-12-161-0/+247
This adds initial support for kernel 4.14 based on the patches for kernel 4.9. In the configuration I deactivated some of the new possible security features like: CONFIG_REFCOUNT_FULL CONFIG_SLAB_FREELIST_HARDENED CONFIG_SOFTLOCKUP_DETECTOR CONFIG_WARN_ALL_UNSEEDED_RANDOM And these overlay FS options are also deactivated: CONFIG_OVERLAY_FS_INDEX CONFIG_OVERLAY_FS_REDIRECT_DIR I activated this: CONFIG_FORTIFY_SOURCE CONFIG_POSIX_TIMERS CONFIG_SLAB_MERGE_DEFAULT CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED I am not sure if I did the porting correct for the following patches: target/linux/generic/backport-4.14/020-backport_netfilter_rtcache.patch target/linux/generic/hack-4.14/220-gc_sections.patch target/linux/generic/hack-4.14/321-powerpc_crtsavres_prereq.patch target/linux/generic/pending-4.14/305-mips_module_reloc.patch target/linux/generic/pending-4.14/611-netfilter_match_bypass_default_table.patch target/linux/generic/pending-4.14/680-NET-skip-GRO-for-foreign-MAC-addresses.patch Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
6'>316 317 318 319 320 321 322 323 324 325 326 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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 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 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 491 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 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 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 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 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 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 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 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 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 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 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 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 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 961 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 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 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
/*	$OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $	*/

/*-
 * Linux port done by David McCullough <david_mccullough@mcafee.com>
 * Copyright (C) 2006-2010 David McCullough
 * Copyright (C) 2004-2005 Intel Corporation.
 * The license and original author are listed below.
 *
 * Copyright (c) 2001 Theo de Raadt
 * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Effort sponsored in part by the Defense Advanced Research Projects
 * Agency (DARPA) and Air Force Research Laboratory, Air Force
 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
 *
__FBSDID("$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.34 2007/05/09 19:37:02 gnn Exp $");
 */

#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) && !defined(AUTOCONF_INCLUDED)
#include <linux/config.h>
#endif
#include <linux/types.h>
#include <linux/time.h>
#include <linux/delay.h>
#include <linux/list.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/unistd.h>
#include <linux/module.h>
#include <linux/wait.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/dcache.h>
#include <linux/file.h>
#include <linux/mount.h>
#include <linux/miscdevice.h>
#include <asm/uaccess.h>

#include <cryptodev.h>
#include <uio.h>

extern asmlinkage long sys_dup(unsigned int fildes);

#define debug cryptodev_debug
int cryptodev_debug = 0;
module_param(cryptodev_debug, int, 0644);
MODULE_PARM_DESC(cryptodev_debug, "Enable cryptodev debug");

struct csession_info {
	u_int16_t	blocksize;
	u_int16_t	minkey, maxkey;

	u_int16_t	keysize;
	/* u_int16_t	hashsize;  */
	u_int16_t	authsize;
	u_int16_t	authkey;
	/* u_int16_t	ctxsize; */
};

struct csession {
	struct list_head	list;
	u_int64_t	sid;
	u_int32_t	ses;

	wait_queue_head_t waitq;

	u_int32_t	cipher;

	u_int32_t	mac;

	caddr_t		key;
	int		keylen;
	u_char		tmp_iv[EALG_MAX_BLOCK_LEN];

	caddr_t		mackey;
	int		mackeylen;

	struct csession_info info;

	struct iovec	iovec;
	struct uio	uio;
	int		error;
};

struct fcrypt {
	struct list_head	csessions;
	int		sesn;
};

static struct csession *csefind(struct fcrypt *, u_int);
static int csedelete(struct fcrypt *, struct csession *);
static struct csession *cseadd(struct fcrypt *, struct csession *);
static struct csession *csecreate(struct fcrypt *, u_int64_t,
		struct cryptoini *crie, struct cryptoini *cria, struct csession_info *);
static int csefree(struct csession *);

static	int cryptodev_op(struct csession *, struct crypt_op *);
static	int cryptodev_key(struct crypt_kop *);
static	int cryptodev_find(struct crypt_find_op *);

static int cryptodev_cb(void *);
static int cryptodev_open(struct inode *inode, struct file *filp);

/*
 * Check a crypto identifier to see if it requested
 * a valid crid and it's capabilities match.
 */
static int
checkcrid(int crid)
{
	int hid = crid & ~(CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
	int typ = crid & (CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
	int caps = 0;
	
	/* if the user hasn't selected a driver, then just call newsession */
	if (hid == 0 && typ != 0)
		return 0;

	caps = crypto_getcaps(hid);

	/* didn't find anything with capabilities */
	if (caps == 0) {
		dprintk("%s: hid=%x typ=%x not matched\n", __FUNCTION__, hid, typ);
		return EINVAL;
	}
	
	/* the user didn't specify SW or HW, so the driver is ok */
	if (typ == 0)
		return 0;

	/* if the type specified didn't match */
	if (typ != (caps & (CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE))) {
		dprintk("%s: hid=%x typ=%x caps=%x not matched\n", __FUNCTION__,
				hid, typ, caps);
		return EINVAL;
	}

	return 0;
}

static int
cryptodev_op(struct csession *cse, struct crypt_op *cop)
{
	struct cryptop *crp = NULL;
	struct cryptodesc *crde = NULL, *crda = NULL;
	int error = 0;

	dprintk("%s()\n", __FUNCTION__);
	if (cop->len > CRYPTO_MAX_DATA_LEN) {
		dprintk("%s: %d > %d\n", __FUNCTION__, cop->len, CRYPTO_MAX_DATA_LEN);
		return (E2BIG);
	}

	if (cse->info.blocksize && (cop->len % cse->info.blocksize) != 0) {
		dprintk("%s: blocksize=%d len=%d\n", __FUNCTION__, cse->info.blocksize,
				cop->len);
		return (EINVAL);
	}

	cse->uio.uio_iov = &cse->iovec;
	cse->uio.uio_iovcnt = 1;
	cse->uio.uio_offset = 0;
#if 0
	cse->uio.uio_resid = cop->len;
	cse->uio.uio_segflg = UIO_SYSSPACE;
	cse->uio.uio_rw = UIO_WRITE;
	cse->uio.uio_td = td;
#endif
	cse->uio.uio_iov[0].iov_len = cop->len;
	if (cse->info.authsize)
		cse->uio.uio_iov[0].iov_len += cse->info.authsize;
	cse->uio.uio_iov[0].iov_base = kmalloc(cse->uio.uio_iov[0].iov_len,
			GFP_KERNEL);

	if (cse->uio.uio_iov[0].iov_base == NULL) {
		dprintk("%s: iov_base kmalloc(%d) failed\n", __FUNCTION__,
				(int)cse->uio.uio_iov[0].iov_len);
		return (ENOMEM);
	}

	crp = crypto_getreq((cse->info.blocksize != 0) + (cse->info.authsize != 0));
	if (crp == NULL) {
		dprintk("%s: ENOMEM\n", __FUNCTION__);
		error = ENOMEM;
		goto bail;
	}

	if (cse->info.authsize && cse->info.blocksize) {
		if (cop->op == COP_ENCRYPT) {
			crde = crp->crp_desc;
			crda = crde->crd_next;
		} else {
			crda = crp->crp_desc;
			crde = crda->crd_next;
		}
	} else if (cse->info.authsize) {
		crda = crp->crp_desc;
	} else if (cse->info.blocksize) {
		crde = crp->crp_desc;
	} else {
		dprintk("%s: bad request\n", __FUNCTION__);
		error = EINVAL;
		goto bail;
	}

	if ((error = copy_from_user(cse->uio.uio_iov[0].iov_base, cop->src,
					cop->len))) {
		dprintk("%s: bad copy\n", __FUNCTION__);
		goto bail;
	}

	if (crda) {
		crda->crd_skip = 0;
		crda->crd_len = cop->len;
		crda->crd_inject = cop->len;

		crda->crd_alg = cse->mac;
		crda->crd_key = cse->mackey;
		crda->crd_klen = cse->mackeylen * 8;
	}

	if (crde) {
		if (cop->op == COP_ENCRYPT)
			crde->crd_flags |= CRD_F_ENCRYPT;
		else
			crde->crd_flags &= ~CRD_F_ENCRYPT;
		crde->crd_len = cop->len;
		crde->crd_inject = 0;

		crde->crd_alg = cse->cipher;
		crde->crd_key = cse->key;
		crde->crd_klen = cse->keylen * 8;
	}

	crp->crp_ilen = cse->uio.uio_iov[0].iov_len;
	crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
		       | (cop->flags & COP_F_BATCH);
	crp->crp_buf = (caddr_t)&cse->uio;
	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
	crp->crp_sid = cse->sid;
	crp->crp_opaque = (void *)cse;

	if (cop->iv) {
		if (crde == NULL) {
			error = EINVAL;
			dprintk("%s no crde\n", __FUNCTION__);
			goto bail;
		}
		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
			error = EINVAL;
			dprintk("%s arc4 with IV\n", __FUNCTION__);
			goto bail;
		}
		if ((error = copy_from_user(cse->tmp_iv, cop->iv,
						cse->info.blocksize))) {
			dprintk("%s bad iv copy\n", __FUNCTION__);
			goto bail;
		}
		memcpy(crde->crd_iv, cse->tmp_iv, cse->info.blocksize);
		crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
		crde->crd_skip = 0;
	} else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
		crde->crd_skip = 0;
	} else if (crde) {
		crde->crd_flags |= CRD_F_IV_PRESENT;
		crde->crd_skip = cse->info.blocksize;
		crde->crd_len -= cse->info.blocksize;
	}

	if (cop->mac && crda == NULL) {
		error = EINVAL;
		dprintk("%s no crda\n", __FUNCTION__);
		goto bail;
	}

	/*
	 * Let the dispatch run unlocked, then, interlock against the
	 * callback before checking if the operation completed and going
	 * to sleep.  This insures drivers don't inherit our lock which
	 * results in a lock order reversal between crypto_dispatch forced
	 * entry and the crypto_done callback into us.
	 */
	error = crypto_dispatch(crp);
	if (error) {
		dprintk("%s error in crypto_dispatch\n", __FUNCTION__);
		goto bail;
	}

	dprintk("%s about to WAIT\n", __FUNCTION__);
	/*
	 * we really need to wait for driver to complete to maintain
	 * state,  luckily interrupts will be remembered
	 */
	do {
		error = wait_event_interruptible(crp->crp_waitq,
				((crp->crp_flags & CRYPTO_F_DONE) != 0));
		/*
		 * we can't break out of this loop or we will leave behind
		 * a huge mess,  however,  staying here means if your driver
		 * is broken user applications can hang and not be killed.
		 * The solution,  fix your driver :-)
		 */
		if (error) {
			schedule();
			error = 0;
		}
	} while ((crp->crp_flags & CRYPTO_F_DONE) == 0);
	dprintk("%s finished WAITING error=%d\n", __FUNCTION__, error);

	if (crp->crp_etype != 0) {
		error = crp->crp_etype;
		dprintk("%s error in crp processing\n", __FUNCTION__);
		goto bail;
	}

	if (cse->error) {
		error = cse->error;
		dprintk("%s error in cse processing\n", __FUNCTION__);
		goto bail;
	}

	if (cop->dst && (error = copy_to_user(cop->dst,
					cse->uio.uio_iov[0].iov_base, cop->len))) {
		dprintk("%s bad dst copy\n", __FUNCTION__);
		goto bail;
	}

	if (cop->mac &&
			(error=copy_to_user(cop->mac,
				(caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
				cse->info.authsize))) {
		dprintk("%s bad mac copy\n", __FUNCTION__);
		goto bail;
	}

bail:
	if (crp)
		crypto_freereq(crp);
	if (cse->uio.uio_iov[0].iov_base)
		kfree(cse->uio.uio_iov[0].iov_base);

	return (error);
}

static int
cryptodev_cb(void *op)
{
	struct cryptop *crp = (struct cryptop *) op;
	struct csession *cse = (struct csession *)crp->crp_opaque;
	int error;

	dprintk("%s()\n", __FUNCTION__);
	error = crp->crp_etype;
	if (error == EAGAIN) {
		crp->crp_flags &= ~CRYPTO_F_DONE;
#ifdef NOTYET
		/*
		 * DAVIDM I am fairly sure that we should turn this into a batch
		 * request to stop bad karma/lockup, revisit
		 */
		crp->crp_flags |= CRYPTO_F_BATCH;
#endif
		return crypto_dispatch(crp);
	}
	if (error != 0 || (crp->crp_flags & CRYPTO_F_DONE)) {
		cse->error = error;
		wake_up_interruptible(&crp->crp_waitq);
	}
	return (0);
}

static int
cryptodevkey_cb(void *op)
{
	struct cryptkop *krp = (struct cryptkop *) op;
	dprintk("%s()\n", __FUNCTION__);
	wake_up_interruptible(&krp->krp_waitq);
	return (0);
}

static int
cryptodev_key(struct crypt_kop *kop)
{
	struct cryptkop *krp = NULL;
	int error = EINVAL;
	int in, out, size, i;

	dprintk("%s()\n", __FUNCTION__);
	if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
		dprintk("%s params too big\n", __FUNCTION__);
		return (EFBIG);
	}

	in = kop->crk_iparams;
	out = kop->crk_oparams;
	switch (kop->crk_op) {
	case CRK_MOD_EXP:
		if (in == 3 && out == 1)
			break;
		return (EINVAL);
	case CRK_MOD_EXP_CRT:
		if (in == 6 && out == 1)
			break;
		return (EINVAL);
	case CRK_DSA_SIGN:
		if (in == 5 && out == 2)
			break;
		return (EINVAL);
	case CRK_DSA_VERIFY:
		if (in == 7 && out == 0)
			break;
		return (EINVAL);
	case CRK_DH_COMPUTE_KEY:
		if (in == 3 && out == 1)
			break;
		return (EINVAL);
	default:
		return (EINVAL);
	}

	krp = (struct cryptkop *)kmalloc(sizeof *krp, GFP_KERNEL);
	if (!krp)
		return (ENOMEM);
	bzero(krp, sizeof *krp);
	krp->krp_op = kop->crk_op;
	krp->krp_status = kop->crk_status;
	krp->krp_iparams = kop->crk_iparams;
	krp->krp_oparams = kop->crk_oparams;
	krp->krp_crid = kop->crk_crid;
	krp->krp_status = 0;
	krp->krp_flags = CRYPTO_KF_CBIMM;
	krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
	init_waitqueue_head(&krp->krp_waitq);

	for (i = 0; i < CRK_MAXPARAM; i++)
		krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
	for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
		size = (krp->krp_param[i].crp_nbits + 7) / 8;
		if (size == 0)
			continue;
		krp->krp_param[i].crp_p = (caddr_t) kmalloc(size, GFP_KERNEL);
		if (i >= krp->krp_iparams)
			continue;
		error = copy_from_user(krp->krp_param[i].crp_p,
				kop->crk_param[i].crp_p, size);
		if (error)
			goto fail;
	}

	error = crypto_kdispatch(krp);
	if (error)
		goto fail;

	do {
		error = wait_event_interruptible(krp->krp_waitq,
				((krp->krp_flags & CRYPTO_KF_DONE) != 0));
		/*
		 * we can't break out of this loop or we will leave behind
		 * a huge mess,  however,  staying here means if your driver
		 * is broken user applications can hang and not be killed.
		 * The solution,  fix your driver :-)
		 */
		if (error) {
			schedule();
			error = 0;
		}
	} while ((krp->krp_flags & CRYPTO_KF_DONE) == 0);

	dprintk("%s finished WAITING error=%d\n", __FUNCTION__, error);
	
	kop->crk_crid = krp->krp_crid;		/* device that did the work */
	if (krp->krp_status != 0) {
		error = krp->krp_status;
		goto fail;
	}

	for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
		size = (krp->krp_param[i].crp_nbits + 7) / 8;
		if (size == 0)
			continue;
		error = copy_to_user(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p,
				size);
		if (error)
			goto fail;
	}

fail:
	if (krp) {
		kop->crk_status = krp->krp_status;
		for (i = 0; i < CRK_MAXPARAM; i++) {
			if (krp->krp_param[i].crp_p)
				kfree(krp->krp_param[i].crp_p);
		}
		kfree(krp);
	}
	return (error);
}

static int
cryptodev_find(struct crypt_find_op *find)
{
	device_t dev;

	if (find->crid != -1) {
		dev = crypto_find_device_byhid(find->crid);
		if (dev == NULL)
			return (ENOENT);
		strlcpy(find->name, device_get_nameunit(dev),
		    sizeof(find->name));
	} else {
		find->crid = crypto_find_driver(find->name);
		if (find->crid == -1)
			return (ENOENT);
	}
	return (0);
}

static struct csession *
csefind(struct fcrypt *fcr, u_int ses)
{
	struct csession *cse;

	dprintk("%s()\n", __FUNCTION__);
	list_for_each_entry(cse, &fcr->csessions, list)
		if (cse->ses == ses)
			return (cse);
	return (NULL);
}

static int
csedelete(struct fcrypt *fcr, struct csession *cse_del)
{
	struct csession *cse;

	dprintk("%s()\n", __FUNCTION__);
	list_for_each_entry(cse, &fcr->csessions, list) {
		if (cse == cse_del) {
			list_del(&cse->list);
			return (1);
		}
	}
	return (0);
}
	
static struct csession *
cseadd(struct fcrypt *fcr, struct csession *cse)
{
	dprintk("%s()\n", __FUNCTION__);
	list_add_tail(&cse->list, &fcr->csessions);
	cse->ses = fcr->sesn++;
	return (cse);
}

static struct csession *
csecreate(struct fcrypt *fcr, u_int64_t sid, struct cryptoini *crie,
	struct cryptoini *cria, struct csession_info *info)
{
	struct csession *cse;

	dprintk("%s()\n", __FUNCTION__);
	cse = (struct csession *) kmalloc(sizeof(struct csession), GFP_KERNEL);
	if (cse == NULL)
		return NULL;
	memset(cse, 0, sizeof(struct csession));

	INIT_LIST_HEAD(&cse->list);
	init_waitqueue_head(&cse->waitq);

	cse->key = crie->cri_key;
	cse->keylen = crie->cri_klen/8;
	cse->mackey = cria->cri_key;
	cse->mackeylen = cria->cri_klen/8;
	cse->sid = sid;
	cse->cipher = crie->cri_alg;
	cse->mac = cria->cri_alg;
	cse->info = *info;
	cseadd(fcr, cse);
	return (cse);
}

static int
csefree(struct csession *cse)
{
	int error;

	dprintk("%s()\n", __FUNCTION__);
	error = crypto_freesession(cse->sid);
	if (cse->key)
		kfree(cse->key);
	if (cse->mackey)
		kfree(cse->mackey);
	kfree(cse);
	return(error);
}

static int
cryptodev_ioctl(
	struct inode *inode,
	struct file *filp,
	unsigned int cmd,
	unsigned long arg)
{
	struct cryptoini cria, crie;
	struct fcrypt *fcr = filp->private_data;
	struct csession *cse;
	struct csession_info info;
	struct session2_op sop;
	struct crypt_op cop;
	struct crypt_kop kop;
	struct crypt_find_op fop;
	u_int64_t sid;
	u_int32_t ses = 0;
	int feat, fd, error = 0, crid;
	mm_segment_t fs;

	dprintk("%s(cmd=%x arg=%lx)\n", __FUNCTION__, cmd, arg);

	switch (cmd) {

	case CRIOGET: {
		dprintk("%s(CRIOGET)\n", __FUNCTION__);
		fs = get_fs();
		set_fs(get_ds());
		for (fd = 0; fd < files_fdtable(current->files)->max_fds; fd++)
			if (files_fdtable(current->files)->fd[fd] == filp)
				break;
		fd = sys_dup(fd);
		set_fs(fs);
		put_user(fd, (int *) arg);
		return IS_ERR_VALUE(fd) ? fd : 0;
		}

#define	CIOCGSESSSTR	(cmd == CIOCGSESSION ? "CIOCGSESSION" : "CIOCGSESSION2")
	case CIOCGSESSION:
	case CIOCGSESSION2:
		dprintk("%s(%s)\n", __FUNCTION__, CIOCGSESSSTR);
		memset(&crie, 0, sizeof(crie));
		memset(&cria, 0, sizeof(cria));
		memset(&info, 0, sizeof(info));
		memset(&sop, 0, sizeof(sop));

		if (copy_from_user(&sop, (void*)arg, (cmd == CIOCGSESSION) ?
					sizeof(struct session_op) : sizeof(sop))) {
			dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
			error = EFAULT;
			goto bail;
		}

		switch (sop.cipher) {
		case 0:
			dprintk("%s(%s) - no cipher\n", __FUNCTION__, CIOCGSESSSTR);
			break;
		case CRYPTO_NULL_CBC:
			info.blocksize = NULL_BLOCK_LEN;
			info.minkey = NULL_MIN_KEY_LEN;
			info.maxkey = NULL_MAX_KEY_LEN;
			break;
		case CRYPTO_DES_CBC:
			info.blocksize = DES_BLOCK_LEN;
			info.minkey = DES_MIN_KEY_LEN;
			info.maxkey = DES_MAX_KEY_LEN;
			break;
		case CRYPTO_3DES_CBC:
			info.blocksize = DES3_BLOCK_LEN;
			info.minkey = DES3_MIN_KEY_LEN;
			info.maxkey = DES3_MAX_KEY_LEN;
			break;
		case CRYPTO_BLF_CBC:
			info.blocksize = BLOWFISH_BLOCK_LEN;
			info.minkey = BLOWFISH_MIN_KEY_LEN;
			info.maxkey = BLOWFISH_MAX_KEY_LEN;
			break;
		case CRYPTO_CAST_CBC:
			info.blocksize = CAST128_BLOCK_LEN;
			info.minkey = CAST128_MIN_KEY_LEN;
			info.maxkey = CAST128_MAX_KEY_LEN;
			break;
		case CRYPTO_SKIPJACK_CBC:
			info.blocksize = SKIPJACK_BLOCK_LEN;
			info.minkey = SKIPJACK_MIN_KEY_LEN;
			info.maxkey = SKIPJACK_MAX_KEY_LEN;
			break;
		case CRYPTO_AES_CBC:
			info.blocksize = AES_BLOCK_LEN;
			info.minkey = AES_MIN_KEY_LEN;
			info.maxkey = AES_MAX_KEY_LEN;
			break;
		case CRYPTO_ARC4:
			info.blocksize = ARC4_BLOCK_LEN;
			info.minkey = ARC4_MIN_KEY_LEN;
			info.maxkey = ARC4_MAX_KEY_LEN;
			break;
		case CRYPTO_CAMELLIA_CBC:
			info.blocksize = CAMELLIA_BLOCK_LEN;
			info.minkey = CAMELLIA_MIN_KEY_LEN;
			info.maxkey = CAMELLIA_MAX_KEY_LEN;
			break;
		default:
			dprintk("%s(%s) - bad cipher\n", __FUNCTION__, CIOCGSESSSTR);
			error = EINVAL;
			goto bail;
		}

		switch (sop.mac) {
		case 0:
			dprintk("%s(%s) - no mac\n", __FUNCTION__, CIOCGSESSSTR);
			break;
		case CRYPTO_NULL_HMAC:
			info.authsize = NULL_HASH_LEN;
			break;
		case CRYPTO_MD5:
			info.authsize = MD5_HASH_LEN;
			break;
		case CRYPTO_SHA1:
			info.authsize = SHA1_HASH_LEN;
			break;
		case CRYPTO_SHA2_256:
			info.authsize = SHA2_256_HASH_LEN;
			break;
		case CRYPTO_SHA2_384:
			info.authsize = SHA2_384_HASH_LEN;
  			break;
		case CRYPTO_SHA2_512:
			info.authsize = SHA2_512_HASH_LEN;
			break;
		case CRYPTO_RIPEMD160:
			info.authsize = RIPEMD160_HASH_LEN;
			break;
		case CRYPTO_MD5_HMAC:
			info.authsize = MD5_HASH_LEN;
			info.authkey = 16;
			break;
		case CRYPTO_SHA1_HMAC:
			info.authsize = SHA1_HASH_LEN;
			info.authkey = 20;
			break;
		case CRYPTO_SHA2_256_HMAC:
			info.authsize = SHA2_256_HASH_LEN;
			info.authkey = 32;
			break;
		case CRYPTO_SHA2_384_HMAC:
			info.authsize = SHA2_384_HASH_LEN;
			info.authkey = 48;
  			break;
		case CRYPTO_SHA2_512_HMAC:
			info.authsize = SHA2_512_HASH_LEN;
			info.authkey = 64;
			break;
		case CRYPTO_RIPEMD160_HMAC:
			info.authsize = RIPEMD160_HASH_LEN;
			info.authkey = 20;
			break;
		default:
			dprintk("%s(%s) - bad mac\n", __FUNCTION__, CIOCGSESSSTR);
			error = EINVAL;
			goto bail;
		}

		if (info.blocksize) {
			crie.cri_alg = sop.cipher;
			crie.cri_klen = sop.keylen * 8;
			if ((info.maxkey && sop.keylen > info.maxkey) ||
				   	sop.keylen < info.minkey) {
				dprintk("%s(%s) - bad key\n", __FUNCTION__, CIOCGSESSSTR);
				error = EINVAL;
				goto bail;
			}

			crie.cri_key = (u_int8_t *) kmalloc(crie.cri_klen/8+1, GFP_KERNEL);
			if (copy_from_user(crie.cri_key, sop.key,
							crie.cri_klen/8)) {
				dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
				error = EFAULT;
				goto bail;
			}
			if (info.authsize)
				crie.cri_next = &cria;
		}

		if (info.authsize) {
			cria.cri_alg = sop.mac;
			cria.cri_klen = sop.mackeylen * 8;
			if (info.authkey && sop.mackeylen != info.authkey) {
				dprintk("%s(%s) - mackeylen %d != %d\n", __FUNCTION__,
						CIOCGSESSSTR, sop.mackeylen, info.authkey);
				error = EINVAL;
				goto bail;
			}

			if (cria.cri_klen) {
				cria.cri_key = (u_int8_t *) kmalloc(cria.cri_klen/8,GFP_KERNEL);
				if (copy_from_user(cria.cri_key, sop.mackey,
								cria.cri_klen / 8)) {
					dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
					error = EFAULT;
					goto bail;
				}
			}
		}

		/* NB: CIOGSESSION2 has the crid */
		if (cmd == CIOCGSESSION2) {
			crid = sop.crid;
			error = checkcrid(crid);
			if (error) {
				dprintk("%s(%s) - checkcrid %x\n", __FUNCTION__,
						CIOCGSESSSTR, error);
				goto bail;
			}
		} else {
			/* allow either HW or SW to be used */
			crid = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
		}
		error = crypto_newsession(&sid, (info.blocksize ? &crie : &cria), crid);
		if (error) {
			dprintk("%s(%s) - newsession %d\n",__FUNCTION__,CIOCGSESSSTR,error);
			goto bail;
		}

		cse = csecreate(fcr, sid, &crie, &cria, &info);
		if (cse == NULL) {
			crypto_freesession(sid);
			error = EINVAL;
			dprintk("%s(%s) - csecreate failed\n", __FUNCTION__, CIOCGSESSSTR);
			goto bail;
		}
		sop.ses = cse->ses;

		if (cmd == CIOCGSESSION2) {
			/* return hardware/driver id */
			sop.crid = CRYPTO_SESID2HID(cse->sid);
		}

		if (copy_to_user((void*)arg, &sop, (cmd == CIOCGSESSION) ?
					sizeof(struct session_op) : sizeof(sop))) {
			dprintk("%s(%s) - bad copy\n", __FUNCTION__, CIOCGSESSSTR);
			error = EFAULT;
		}
bail:
		if (error) {
			dprintk("%s(%s) - bail %d\n", __FUNCTION__, CIOCGSESSSTR, error);
			if (crie.cri_key)
				kfree(crie.cri_key);
			if (cria.cri_key)
				kfree(cria.cri_key);
		}
		break;
	case CIOCFSESSION:
		dprintk("%s(CIOCFSESSION)\n", __FUNCTION__);
		get_user(ses, (uint32_t*)arg);
		cse = csefind(fcr, ses);
		if (cse == NULL) {
			error = EINVAL;
			dprintk("%s(CIOCFSESSION) - Fail %d\n", __FUNCTION__, error);
			break;
		}
		csedelete(fcr, cse);
		error = csefree(cse);
		break;
	case CIOCCRYPT:
		dprintk("%s(CIOCCRYPT)\n", __FUNCTION__);
		if(copy_from_user(&cop, (void*)arg, sizeof(cop))) {
			dprintk("%s(CIOCCRYPT) - bad copy\n", __FUNCTION__);
			error = EFAULT;
			goto bail;
		}
		cse = csefind(fcr, cop.ses);
		if (cse == NULL) {
			error = EINVAL;
			dprintk("%s(CIOCCRYPT) - Fail %d\n", __FUNCTION__, error);
			break;
		}
		error = cryptodev_op(cse, &cop);
		if(copy_to_user((void*)arg, &cop, sizeof(cop))) {
			dprintk("%s(CIOCCRYPT) - bad return copy\n", __FUNCTION__);
			error = EFAULT;
			goto bail;
		}
		break;
	case CIOCKEY:
	case CIOCKEY2:
		dprintk("%s(CIOCKEY)\n", __FUNCTION__);
		if (!crypto_userasymcrypto)
			return (EPERM);		/* XXX compat? */
		if(copy_from_user(&kop, (void*)arg, sizeof(kop))) {
			dprintk("%s(CIOCKEY) - bad copy\n", __FUNCTION__);
			error = EFAULT;
			goto bail;
		}
		if (cmd == CIOCKEY) {
			/* NB: crypto core enforces s/w driver use */
			kop.crk_crid =
			    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
		}
		error = cryptodev_key(&kop);
		if(copy_to_user((void*)arg, &kop, sizeof(kop))) {
			dprintk("%s(CIOCGKEY) - bad return copy\n", __FUNCTION__);
			error = EFAULT;
			goto bail;
		}
		break;
	case CIOCASYMFEAT:
		dprintk("%s(CIOCASYMFEAT)\n", __FUNCTION__);
		if (!crypto_userasymcrypto) {
			/*
			 * NB: if user asym crypto operations are
			 * not permitted return "no algorithms"
			 * so well-behaved applications will just
			 * fallback to doing them in software.
			 */
			feat = 0;
		} else
			error = crypto_getfeat(&feat);
		if (!error) {
		  error = copy_to_user((void*)arg, &feat, sizeof(feat));
		}
		break;
	case CIOCFINDDEV:
		if (copy_from_user(&fop, (void*)arg, sizeof(fop))) {
			dprintk("%s(CIOCFINDDEV) - bad copy\n", __FUNCTION__);
			error = EFAULT;
			goto bail;
		}
		error = cryptodev_find(&fop);
		if (copy_to_user((void*)arg, &fop, sizeof(fop))) {
			dprintk("%s(CIOCFINDDEV) - bad return copy\n", __FUNCTION__);
			error = EFAULT;
			goto bail;
		}
		break;
	default:
		dprintk("%s(unknown ioctl 0x%x)\n", __FUNCTION__, cmd);
		error = EINVAL;
		break;
	}
	return(-error);
}

#ifdef HAVE_UNLOCKED_IOCTL
static long
cryptodev_unlocked_ioctl(
	struct file *filp,
	unsigned int cmd,
	unsigned long arg)
{
	return cryptodev_ioctl(NULL, filp, cmd, arg);
}
#endif

static int
cryptodev_open(struct inode *inode, struct file *filp)
{
	struct fcrypt *fcr;

	dprintk("%s()\n", __FUNCTION__);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
	/*
	 * on 2.6.35 private_data points to a miscdevice structure, we override
	 * it,  which is currently safe to do.
	 */
	if (filp->private_data) {
		printk("cryptodev: Private data already exists - %p!\n", filp->private_data);
		return(-ENODEV);
	}
#endif

	fcr = kmalloc(sizeof(*fcr), GFP_KERNEL);
	if (!fcr) {
		dprintk("%s() - malloc failed\n", __FUNCTION__);
		return(-ENOMEM);
	}
	memset(fcr, 0, sizeof(*fcr));

	INIT_LIST_HEAD(&fcr->csessions);
	filp->private_data = fcr;
	return(0);
}

static int
cryptodev_release(struct inode *inode, struct file *filp)
{
	struct fcrypt *fcr = filp->private_data;
	struct csession *cse, *tmp;

	dprintk("%s()\n", __FUNCTION__);
	if (!filp) {
		printk("cryptodev: No private data on release\n");
		return(0);
	}

	list_for_each_entry_safe(cse, tmp, &fcr->csessions, list) {
		list_del(&cse->list);
		(void)csefree(cse);
	}
	filp->private_data = NULL;
	kfree(fcr);
	return(0);
}

static struct file_operations cryptodev_fops = {
	.owner = THIS_MODULE,
	.open = cryptodev_open,
	.release = cryptodev_release,
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
	.ioctl = cryptodev_ioctl,
#endif
#ifdef HAVE_UNLOCKED_IOCTL
	.unlocked_ioctl = cryptodev_unlocked_ioctl,
#endif
};

static struct miscdevice cryptodev = {
	.minor = CRYPTODEV_MINOR,
	.name = "crypto",
	.fops = &cryptodev_fops,
};

static int __init
cryptodev_init(void)
{
	int rc;

	dprintk("%s(%p)\n", __FUNCTION__, cryptodev_init);
	rc = misc_register(&cryptodev);
	if (rc) {
		printk(KERN_ERR "cryptodev: registration of /dev/crypto failed\n");
		return(rc);
	}

	return(0);
}

static void __exit
cryptodev_exit(void)
{
	dprintk("%s()\n", __FUNCTION__);
	misc_deregister(&cryptodev);
}

module_init(cryptodev_init);
module_exit(cryptodev_exit);

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("David McCullough <david_mccullough@mcafee.com>");
MODULE_DESCRIPTION("Cryptodev (user interface to OCF)");