1/* $KAME: policy.h,v 1.18 2001/10/02 04:10:17 sakane Exp $ */ 2 3/* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/queue.h> 33 34/* refs. ipsec.h */ 35/* 36 * Security Policy Index 37 * NOTE: Ensure to be same address family and upper layer protocol. 38 * NOTE: ul_proto, port number, uid, gid: 39 * ANY: reserved for waldcard. 40 * 0 to (~0 - 1): is one of the number of each value. 41 */ 42struct policyindex { 43 u_int8_t dir; /* direction of packet flow, see blow */ 44 struct sockaddr_storage src; /* IP src address for SP */ 45 struct sockaddr_storage dst; /* IP dst address for SP */ 46 u_int8_t prefs; /* prefix length in bits for src */ 47 u_int8_t prefd; /* prefix length in bits for dst */ 48 u_int16_t ul_proto; /* upper layer Protocol */ 49}; 50 51/* Security Policy Data Base */ 52struct secpolicy { 53 TAILQ_ENTRY(secpolicy) chain; 54 55 struct policyindex spidx; /* selector */ 56 u_int32_t id; /* It's unique number on the system. */ 57 58 u_int policy; /* DISCARD, NONE or IPSEC, see keyv2.h */ 59 struct ipsecrequest *req; 60 /* pointer to the ipsec request tree, */ 61 /* if policy == IPSEC else this value == NULL.*/ 62}; 63 64/* Security Assocciation Index */ 65/* NOTE: Ensure to be same address family */ 66struct secasindex { 67 struct sockaddr_storage src; /* srouce address for SA */ 68 struct sockaddr_storage dst; /* destination address for SA */ 69 u_int16_t proto; /* IPPROTO_ESP or IPPROTO_AH */ 70 u_int8_t mode; /* mode of protocol, see ipsec.h */ 71 u_int32_t reqid; /* reqid id who owned this SA */ 72 /* see IPSEC_MANUAL_REQID_MAX. */ 73}; 74 75/* Request for IPsec */ 76struct ipsecrequest { 77 struct ipsecrequest *next; 78 /* pointer to next structure */ 79 /* If NULL, it means the end of chain. */ 80 81 struct secasindex saidx;/* hint for search proper SA */ 82 /* if __ss_len == 0 then no address specified.*/ 83 u_int level; /* IPsec level defined below. */ 84 85 struct secpolicy *sp; /* back pointer to SP */ 86}; 87 88#define KEY_SETSECSPIDX(_dir, s, d, ps, pd, ulp, idx) \ 89do { \ 90 bzero((idx), sizeof(struct policyindex)); \ 91 (idx)->dir = (_dir); \ 92 (idx)->prefs = (ps); \ 93 (idx)->prefd = (pd); \ 94 (idx)->ul_proto = (ulp); \ 95 memcpy(&(idx)->src, (s), sysdep_sa_len((struct sockaddr *)(s))); \ 96 memcpy(&(idx)->dst, (d), sysdep_sa_len((struct sockaddr *)(d))); \ 97} while (0) 98 99struct ph2handle; 100struct policyindex; 101extern struct secpolicy *getsp __P((struct policyindex *)); 102extern struct secpolicy *getsp_r __P((struct policyindex *)); 103struct secpolicy *getspbyspid __P((u_int32_t)); 104extern int cmpspidxstrict __P((struct policyindex *, struct policyindex *)); 105extern int cmpspidxwild __P((struct policyindex *, struct policyindex *)); 106extern struct secpolicy *newsp __P((void)); 107extern void delsp __P((struct secpolicy *)); 108extern void delsp_bothdir __P((struct policyindex *)); 109extern void inssp __P((struct secpolicy *)); 110extern void remsp __P((struct secpolicy *)); 111extern void flushsp __P((void)); 112extern void initsp __P((void)); 113extern struct ipsecrequest *newipsecreq __P((void)); 114 115extern const char *spidx2str __P((const struct policyindex *)); 116