From owner-svn-src-projects@freebsd.org Sun Nov 20 12:18:11 2016 Return-Path: Delivered-To: svn-src-projects@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id BF713C4CBFE for ; Sun, 20 Nov 2016 12:18:11 +0000 (UTC) (envelope-from ae@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 71AC6F22; Sun, 20 Nov 2016 12:18:11 +0000 (UTC) (envelope-from ae@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id uAKCIAdX050673; Sun, 20 Nov 2016 12:18:10 GMT (envelope-from ae@FreeBSD.org) Received: (from ae@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id uAKCIAgC050672; Sun, 20 Nov 2016 12:18:10 GMT (envelope-from ae@FreeBSD.org) Message-Id: <201611201218.uAKCIAgC050672@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ae set sender to ae@FreeBSD.org using -f From: "Andrey V. Elsukov" Date: Sun, 20 Nov 2016 12:18:10 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r308883 - projects/ipsec/sys/netipsec X-SVN-Group: projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 20 Nov 2016 12:18:11 -0000 Author: ae Date: Sun Nov 20 12:18:10 2016 New Revision: 308883 URL: https://svnweb.freebsd.org/changeset/base/308883 Log: Add address family independed function ipsec_checkpolicy(). It takes security policy as argument and returns policy decision: NULL and *error == 0 means "no IPsec processig required"; NULL and *error != -EINVAL means "packet should be discarded"; not NULL means "packet should be handled by IPsec". Modified: projects/ipsec/sys/netipsec/ipsec.c Modified: projects/ipsec/sys/netipsec/ipsec.c ============================================================================== --- projects/ipsec/sys/netipsec/ipsec.c Sun Nov 20 11:57:34 2016 (r308882) +++ projects/ipsec/sys/netipsec/ipsec.c Sun Nov 20 12:18:10 2016 (r308883) @@ -285,6 +285,49 @@ key_allocsp_default(const char* where, i key_allocsp_default(__FILE__, __LINE__) static struct secpolicy * +ipsec_checkpolicy(struct secpolicy *sp, struct inpcb *inp, int *error) +{ + uint32_t genid; + + if (inp != NULL && + (inp->inp_sp->flags & INP_OUTBOUND_POLICY) == 0 && + inp->inp_sp->sp_out == NULL) { + /* + * Save found OUTBOUND policy into PCB SP cache. + */ + genid = key_getspgen(); + inp->inp_sp->sp_out = sp; + if (genid != inp->inp_sp->genid) { + /* Reset INBOUND cached policy if genid is changed */ + if ((inp->inp_sp->flags & INP_INBOUND_POLICY) == 0) + inp->inp_sp->sp_in = NULL; + inp->inp_sp->genid = genid; + } + KEYDBG(IPSEC_STAMP, + printf("%s: PCB(%p): cached SP(%p)\n", + __func__, inp, sp)); + } + switch (sp->policy) { + default: + printf("%s: invalid policy %u\n", __func__, sp->policy); + /* FALLTHROUGH */ + case IPSEC_POLICY_DISCARD: + *error = -EINVAL; /* Packet is discarded by caller. */ + /* FALLTHROUGH */ + case IPSEC_POLICY_BYPASS: + case IPSEC_POLICY_NONE: + key_freesp(&sp); + sp = NULL; /* NB: force NULL result. */ + break; + case IPSEC_POLICY_IPSEC: + break; + } + KEYDBG(IPSEC_DUMP, + printf("%s: get SP(%p), error %d\n", __func__, sp, *error)); + return (sp); +} + +static struct secpolicy * ipsec_getpcbpolicy(struct inpcb *inp, u_int dir) { struct secpolicy *sp;