1*202aab43Stobhe /* $OpenBSD: ipsecctl.h,v 1.77 2023/10/09 15:32:14 tobhe Exp $ */ 2f484f2cfShshoexer /* 3f484f2cfShshoexer * Copyright (c) 2004, 2005 Hans-Joerg Hoexer <hshoexer@openbsd.org> 4f484f2cfShshoexer * 5f484f2cfShshoexer * Permission to use, copy, modify, and distribute this software for any 6f484f2cfShshoexer * purpose with or without fee is hereby granted, provided that the above 7f484f2cfShshoexer * copyright notice and this permission notice appear in all copies. 8f484f2cfShshoexer * 9f484f2cfShshoexer * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10f484f2cfShshoexer * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11f484f2cfShshoexer * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12f484f2cfShshoexer * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13f484f2cfShshoexer * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14f484f2cfShshoexer * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15f484f2cfShshoexer * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16f484f2cfShshoexer */ 17f484f2cfShshoexer 18f484f2cfShshoexer #ifndef _IPSECCTL_H_ 19f484f2cfShshoexer #define _IPSECCTL_H_ 20f484f2cfShshoexer 21f484f2cfShshoexer #define IPSECCTL_OPT_DISABLE 0x0001 22f484f2cfShshoexer #define IPSECCTL_OPT_ENABLE 0x0002 23f484f2cfShshoexer #define IPSECCTL_OPT_NOACTION 0x0004 24f484f2cfShshoexer #define IPSECCTL_OPT_VERBOSE 0x0010 25f484f2cfShshoexer #define IPSECCTL_OPT_VERBOSE2 0x0020 26f484f2cfShshoexer #define IPSECCTL_OPT_SHOW 0x0040 273dd59ad2Shshoexer #define IPSECCTL_OPT_SHOWALL 0x0080 28f484f2cfShshoexer #define IPSECCTL_OPT_FLUSH 0x0100 296d01698dShshoexer #define IPSECCTL_OPT_DELETE 0x0200 308a87fca6Smsf #define IPSECCTL_OPT_MONITOR 0x0400 313cb2a986Smarkus #define IPSECCTL_OPT_SHOWKEY 0x0800 32aa9f7a4dSmpi #define IPSECCTL_OPT_COLLAPSE 0x1000 33*202aab43Stobhe #define IPSECCTL_OPT_SHOWFLOWS 0x2000 34*202aab43Stobhe #define IPSECCTL_OPT_SHOWSAS 0x4000 35f484f2cfShshoexer 3690bd57a7Shshoexer enum { 3790bd57a7Shshoexer ACTION_ADD, ACTION_DELETE 3890bd57a7Shshoexer }; 3990bd57a7Shshoexer 4016bc4fb7Shshoexer #define RULE_FLOW 0x01 4116bc4fb7Shshoexer #define RULE_SA 0x02 4290bd57a7Shshoexer #define RULE_IKE 0x04 43a6bcba92Sbluhm #define RULE_BUNDLE 0x08 4416bc4fb7Shshoexer 45a5b7e777Shshoexer enum { 46f484f2cfShshoexer DIRECTION_UNKNOWN, IPSEC_IN, IPSEC_OUT, IPSEC_INOUT 47f484f2cfShshoexer }; 48f484f2cfShshoexer enum { 4988a8cceeSmarkus PROTO_UNKNOWN, IPSEC_ESP, IPSEC_AH, IPSEC_IPCOMP, IPSEC_TCPMD5, 5088a8cceeSmarkus IPSEC_IPIP 51f484f2cfShshoexer }; 52f484f2cfShshoexer enum { 53a38d220fShshoexer MODE_UNKNOWN, IPSEC_TRANSPORT, IPSEC_TUNNEL 54a38d220fShshoexer }; 55a38d220fShshoexer enum { 567d4bf23dSjsing ID_UNKNOWN, ID_PREFIX, ID_IPV4, ID_IPV6, ID_FQDN, ID_UFQDN 57f484f2cfShshoexer }; 586122c05eShshoexer enum { 596122c05eShshoexer TYPE_UNKNOWN, TYPE_USE, TYPE_ACQUIRE, TYPE_REQUIRE, TYPE_DENY, 606122c05eShshoexer TYPE_BYPASS, TYPE_DONTACQ 616122c05eShshoexer }; 62381a2422Shshoexer enum { 636525f06fShshoexer AUTHXF_UNKNOWN, AUTHXF_NONE, AUTHXF_HMAC_MD5, AUTHXF_HMAC_RIPEMD160, 646525f06fShshoexer AUTHXF_HMAC_SHA1, AUTHXF_HMAC_SHA2_256, AUTHXF_HMAC_SHA2_384, 6552b9c1d9Shshoexer AUTHXF_HMAC_SHA2_512 66381a2422Shshoexer }; 67381a2422Shshoexer enum { 680e800071Snaddy ENCXF_UNKNOWN, ENCXF_NONE, ENCXF_3DES_CBC, ENCXF_AES, 69a407e450Shshoexer ENCXF_AES_128, ENCXF_AES_192, ENCXF_AES_256, ENCXF_AESCTR, 705f649d51Snaddy ENCXF_AES_128_CTR, ENCXF_AES_192_CTR, ENCXF_AES_256_CTR, 71783454c1Smikeb ENCXF_AES_128_GCM, ENCXF_AES_192_GCM, ENCXF_AES_256_GCM, 72783454c1Smikeb ENCXF_AES_128_GMAC, ENCXF_AES_192_GMAC, ENCXF_AES_256_GMAC, 73f705960cSmikeb ENCXF_BLOWFISH, ENCXF_CAST128, ENCXF_CHACHA20_POLY1305, ENCXF_NULL 74381a2422Shshoexer }; 7590bd57a7Shshoexer enum { 767834797cSbluhm COMPXF_UNKNOWN, COMPXF_DEFLATE 7772e25333Shshoexer }; 7872e25333Shshoexer enum { 7926c588ccSmpi GROUPXF_UNKNOWN, GROUPXF_NONE, GROUPXF_1, GROUPXF_2, GROUPXF_5, 8026c588ccSmpi GROUPXF_14, GROUPXF_15, GROUPXF_16, GROUPXF_17, GROUPXF_18, 8126c588ccSmpi GROUPXF_19, GROUPXF_20, GROUPXF_21, GROUPXF_25, GROUPXF_26, 8226c588ccSmpi GROUPXF_27, GROUPXF_28, GROUPXF_29, GROUPXF_30 83791b89f7Shshoexer }; 84791b89f7Shshoexer enum { 85101a3da2Sreyk IKE_ACTIVE, IKE_PASSIVE, IKE_DYNAMIC 86aa4503c0Sreyk }; 87aa4503c0Sreyk enum { 88aa4503c0Sreyk IKE_AUTH_RSA, IKE_AUTH_PSK 8990bd57a7Shshoexer }; 90336f7f89Smcbride enum { 91336f7f89Smcbride IKE_MM=0, IKE_AM, IKE_QM 92336f7f89Smcbride }; 93336f7f89Smcbride 94f484f2cfShshoexer 95f484f2cfShshoexer struct ipsec_addr { 96f484f2cfShshoexer union { 97712e78baShshoexer struct in_addr v4; 9891f765ddShshoexer struct in6_addr v6; 9991f765ddShshoexer u_int8_t addr8[16]; 10091f765ddShshoexer u_int16_t addr16[8]; 10191f765ddShshoexer u_int32_t addr32[4]; 10291f765ddShshoexer } ipa; 10391f765ddShshoexer #define v4 ipa.v4 10491f765ddShshoexer #define v6 ipa.v6 10591f765ddShshoexer #define addr8 ipa.addr8 10691f765ddShshoexer #define addr16 ipa.addr16 10791f765ddShshoexer #define addr32 ipa.addr32 10891f765ddShshoexer }; 10991f765ddShshoexer 11091f765ddShshoexer struct ipsec_addr_wrap { 11191f765ddShshoexer struct ipsec_addr address; 11291f765ddShshoexer struct ipsec_addr mask; 113f484f2cfShshoexer int netaddress; 114f484f2cfShshoexer sa_family_t af; 11590bd57a7Shshoexer char *name; 116f6a3d216Shshoexer struct ipsec_addr_wrap *next; 117f6a3d216Shshoexer struct ipsec_addr_wrap *tail; 118c471c043Smpf struct ipsec_addr_wrap *srcnat; 119f484f2cfShshoexer }; 120f484f2cfShshoexer 12167acb912Shshoexer struct ipsec_hosts { 12267acb912Shshoexer struct ipsec_addr_wrap *src; 12367acb912Shshoexer struct ipsec_addr_wrap *dst; 12467acb912Shshoexer u_int16_t sport; 12567acb912Shshoexer u_int16_t dport; 12667acb912Shshoexer }; 12767acb912Shshoexer 128f484f2cfShshoexer struct ipsec_auth { 129f484f2cfShshoexer char *srcid; 130f484f2cfShshoexer char *dstid; 1312281ca6dSmarkus u_int8_t srcid_type; 1322281ca6dSmarkus u_int8_t dstid_type; 133f484f2cfShshoexer u_int16_t type; 134f484f2cfShshoexer }; 135f484f2cfShshoexer 136f032086dShshoexer struct ipsec_key { 137f032086dShshoexer size_t len; 138f032086dShshoexer u_int8_t *data; 139f032086dShshoexer }; 140f032086dShshoexer 141aa4503c0Sreyk struct ike_auth { 142aa4503c0Sreyk u_int8_t type; 143aa4503c0Sreyk char *string; 144aa4503c0Sreyk }; 145aa4503c0Sreyk 1466525f06fShshoexer struct ipsec_xf { 1476525f06fShshoexer char *name; 1486525f06fShshoexer u_int16_t id; 1496525f06fShshoexer size_t keymin; 1506525f06fShshoexer size_t keymax; 151bda04dedSnaddy u_int8_t noauth; 152bda04dedSnaddy u_int8_t nostatic; 1536525f06fShshoexer }; 1546525f06fShshoexer 155375db29dShshoexer struct ipsec_transforms { 156375db29dShshoexer const struct ipsec_xf *authxf; 157375db29dShshoexer const struct ipsec_xf *encxf; 15872e25333Shshoexer const struct ipsec_xf *compxf; 159791b89f7Shshoexer const struct ipsec_xf *groupxf; 160375db29dShshoexer }; 161375db29dShshoexer 162f94146a9Slteo struct ipsec_lifetime { 163f94146a9Slteo int lt_bytes; 164f94146a9Slteo int lt_seconds; 16525b7c325Shshoexer }; 16625b7c325Shshoexer 167da5999d7Shshoexer struct ike_mode { 168da5999d7Shshoexer struct ipsec_transforms *xfs; 169f94146a9Slteo struct ipsec_lifetime *life; 170336f7f89Smcbride u_int8_t ike_exch; 171da5999d7Shshoexer }; 172da5999d7Shshoexer 1736525f06fShshoexer extern const struct ipsec_xf authxfs[]; 1746525f06fShshoexer extern const struct ipsec_xf encxfs[]; 17545ad2739Smarkus extern const struct ipsec_xf compxfs[]; 1766525f06fShshoexer 177a6bcba92Sbluhm TAILQ_HEAD(dst_bundle_queue, ipsec_rule); 178b5d70521Shshoexer 179f484f2cfShshoexer /* Complete state of one rule. */ 180f484f2cfShshoexer struct ipsec_rule { 1811a3f035aShshoexer u_int8_t type; 1821a3f035aShshoexer 1838fd570bdSdlg unsigned int flags; 1848fd570bdSdlg #define IPSEC_RULE_F_IFACE (1 << 0) /* iface is valid */ 1858fd570bdSdlg 18691f765ddShshoexer struct ipsec_addr_wrap *src; 18791f765ddShshoexer struct ipsec_addr_wrap *dst; 1884e28125aShshoexer struct ipsec_addr_wrap *dst2; 189435bb41eSmarkus struct ipsec_addr_wrap *local; 19091f765ddShshoexer struct ipsec_addr_wrap *peer; 191abe65127Shshoexer struct ipsec_auth *auth; 192aa4503c0Sreyk struct ike_auth *ikeauth; 193375db29dShshoexer struct ipsec_transforms *xfs; 194336f7f89Smcbride struct ipsec_transforms *p1xfs; 195f94146a9Slteo struct ipsec_lifetime *p1life; 196336f7f89Smcbride struct ipsec_transforms *p2xfs; 197f94146a9Slteo struct ipsec_lifetime *p2life; 198670bad1aShshoexer struct ipsec_key *authkey; 199670bad1aShshoexer struct ipsec_key *enckey; 200f484f2cfShshoexer 201aa920ac7Sreyk char *tag; /* pf tag for SAs */ 202befd40c8Sbluhm char *p1name; /* Phase 1 Name */ 203e973ffa2Smarkus char *p2name; /* Phase 2 Name (IPsec-XX) */ 204e973ffa2Smarkus char *p2lid; /* Phase 2 source ID */ 205e973ffa2Smarkus char *p2rid; /* Phase 2 destination ID */ 206c471c043Smpf char *p2nid; /* Phase 2 source NAT-ID */ 2079182219dSmarkus u_int8_t satype; /* encapsulating prococol */ 2089182219dSmarkus u_int8_t proto; /* encapsulated protocol */ 2094e28125aShshoexer u_int8_t proto2; 210a38d220fShshoexer u_int8_t tmode; 211f484f2cfShshoexer u_int8_t direction; 2121a3f035aShshoexer u_int8_t flowtype; 21390bd57a7Shshoexer u_int8_t ikemode; 214336f7f89Smcbride u_int8_t p1ie; 215336f7f89Smcbride u_int8_t p2ie; 216a3b4c2abSbluhm u_int8_t udpencap; 217a3b4c2abSbluhm u_int16_t udpdport; 21857f58d0dSnaddy u_int16_t sport; 21957f58d0dSnaddy u_int16_t dport; 220f032086dShshoexer u_int32_t spi; 2214e28125aShshoexer u_int32_t spi2; 222f484f2cfShshoexer u_int32_t nr; 2238fd570bdSdlg unsigned int iface; 224f484f2cfShshoexer 225e48766bcShshoexer TAILQ_ENTRY(ipsec_rule) rule_entry; 226a6bcba92Sbluhm TAILQ_ENTRY(ipsec_rule) bundle_entry; 227a6bcba92Sbluhm TAILQ_ENTRY(ipsec_rule) dst_bundle_entry; 228b5d70521Shshoexer 229aa9f7a4dSmpi TAILQ_HEAD(, ipsec_rule) collapsed_rules; 230aa9f7a4dSmpi 231a6bcba92Sbluhm struct dst_bundle_queue dst_bundle_queue; 23207f0d149Sbluhm char *bundle; 233f484f2cfShshoexer }; 234f484f2cfShshoexer 235f484f2cfShshoexer TAILQ_HEAD(ipsec_rule_queue, ipsec_rule); 236a6bcba92Sbluhm TAILQ_HEAD(ipsec_bundle_queue, ipsec_rule); 237f484f2cfShshoexer 238f484f2cfShshoexer struct ipsecctl { 239f484f2cfShshoexer u_int32_t rule_nr; 240f484f2cfShshoexer int opts; 241f484f2cfShshoexer struct ipsec_rule_queue rule_queue; 242a6bcba92Sbluhm struct ipsec_bundle_queue bundle_queue; 243f484f2cfShshoexer }; 244f484f2cfShshoexer 24520741916Sderaadt int parse_rules(const char *, struct ipsecctl *); 2463d351c90Sreyk int cmdline_symset(char *); 247c033daa1Shshoexer int ipsecctl_add_rule(struct ipsecctl *, struct ipsec_rule *); 248c033daa1Shshoexer void ipsecctl_free_rule(struct ipsec_rule *); 2490c77d9beSmarkus void ipsecctl_print_rule(struct ipsec_rule *, int); 25090bd57a7Shshoexer int ike_print_config(struct ipsec_rule *, int); 251f8738d8bShenning int ike_ipsec_establish(int, struct ipsec_rule *, const char *); 252695c824bShshoexer void set_ipmask(struct ipsec_addr_wrap *, u_int8_t); 253f484f2cfShshoexer 254f484f2cfShshoexer #endif /* _IPSECCTL_H_ */ 255