19713f5c1SHajimu UMEMOTO /* $KAME: ipsec_dump_policy.c,v 1.13 2002/06/27 14:35:11 itojun Exp $ */ 23c62e87aSJun-ichiro itojun Hagino 38a16b7a1SPedro F. Giffuni /*- 48a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 58a16b7a1SPedro F. Giffuni * 69a4365d0SYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. 79a4365d0SYoshinobu Inoue * All rights reserved. 89a4365d0SYoshinobu Inoue * 99a4365d0SYoshinobu Inoue * Redistribution and use in source and binary forms, with or without 109a4365d0SYoshinobu Inoue * modification, are permitted provided that the following conditions 119a4365d0SYoshinobu Inoue * are met: 129a4365d0SYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright 139a4365d0SYoshinobu Inoue * notice, this list of conditions and the following disclaimer. 149a4365d0SYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright 159a4365d0SYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the 169a4365d0SYoshinobu Inoue * documentation and/or other materials provided with the distribution. 179a4365d0SYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors 189a4365d0SYoshinobu Inoue * may be used to endorse or promote products derived from this software 199a4365d0SYoshinobu Inoue * without specific prior written permission. 209a4365d0SYoshinobu Inoue * 219a4365d0SYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 229a4365d0SYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239a4365d0SYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249a4365d0SYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 259a4365d0SYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269a4365d0SYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279a4365d0SYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289a4365d0SYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299a4365d0SYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309a4365d0SYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319a4365d0SYoshinobu Inoue * SUCH DAMAGE. 329a4365d0SYoshinobu Inoue */ 339a4365d0SYoshinobu Inoue 349a4365d0SYoshinobu Inoue #include <sys/types.h> 359a4365d0SYoshinobu Inoue #include <sys/param.h> 369a4365d0SYoshinobu Inoue #include <sys/socket.h> 379a4365d0SYoshinobu Inoue 388409aedfSGeorge V. Neville-Neil #include <netipsec/key_var.h> 399a4365d0SYoshinobu Inoue #include <netinet/in.h> 408409aedfSGeorge V. Neville-Neil #include <netipsec/ipsec.h> 419a4365d0SYoshinobu Inoue 429a4365d0SYoshinobu Inoue #include <arpa/inet.h> 439a4365d0SYoshinobu Inoue 443c62e87aSJun-ichiro itojun Hagino #include <stdio.h> 459a4365d0SYoshinobu Inoue #include <stdlib.h> 469a4365d0SYoshinobu Inoue #include <string.h> 479a4365d0SYoshinobu Inoue #include <netdb.h> 489a4365d0SYoshinobu Inoue 499a4365d0SYoshinobu Inoue #include "ipsec_strerror.h" 509a4365d0SYoshinobu Inoue 519a4365d0SYoshinobu Inoue static const char *ipsp_dir_strs[] = { 529a4365d0SYoshinobu Inoue "any", "in", "out", 539a4365d0SYoshinobu Inoue }; 549a4365d0SYoshinobu Inoue 559a4365d0SYoshinobu Inoue static const char *ipsp_policy_strs[] = { 569a4365d0SYoshinobu Inoue "discard", "none", "ipsec", "entrust", "bypass", 579a4365d0SYoshinobu Inoue }; 589a4365d0SYoshinobu Inoue 591372519bSDavid E. O'Brien static char *ipsec_dump_ipsecrequest(char *, size_t, 601372519bSDavid E. O'Brien struct sadb_x_ipsecrequest *, size_t); 611372519bSDavid E. O'Brien static int set_addresses(char *, size_t, struct sockaddr *, struct sockaddr *); 6269160b1eSDavid E. O'Brien static char *set_address(char *, size_t, struct sockaddr *); 639a4365d0SYoshinobu Inoue 649a4365d0SYoshinobu Inoue /* 659a4365d0SYoshinobu Inoue * policy is sadb_x_policy buffer. 669a4365d0SYoshinobu Inoue * Must call free() later. 679a4365d0SYoshinobu Inoue * When delimiter == NULL, alternatively ' '(space) is applied. 689a4365d0SYoshinobu Inoue */ 699a4365d0SYoshinobu Inoue char * 70*50ecbc51SLexi Winter ipsec_dump_policy(c_caddr_t policy, const char *delimiter) 719a4365d0SYoshinobu Inoue { 72*50ecbc51SLexi Winter const struct sadb_x_policy *xpl = (const struct sadb_x_policy *)policy; 739a4365d0SYoshinobu Inoue struct sadb_x_ipsecrequest *xisr; 743c62e87aSJun-ichiro itojun Hagino size_t off, buflen; 759a4365d0SYoshinobu Inoue char *buf; 763c62e87aSJun-ichiro itojun Hagino char isrbuf[1024]; 773c62e87aSJun-ichiro itojun Hagino char *newbuf; 789a4365d0SYoshinobu Inoue 799a4365d0SYoshinobu Inoue /* sanity check */ 809a4365d0SYoshinobu Inoue if (policy == NULL) 819a4365d0SYoshinobu Inoue return NULL; 829a4365d0SYoshinobu Inoue if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) { 833c62e87aSJun-ichiro itojun Hagino __ipsec_errcode = EIPSEC_INVAL_EXTTYPE; 849a4365d0SYoshinobu Inoue return NULL; 859a4365d0SYoshinobu Inoue } 869a4365d0SYoshinobu Inoue 879a4365d0SYoshinobu Inoue /* set delimiter */ 889a4365d0SYoshinobu Inoue if (delimiter == NULL) 899a4365d0SYoshinobu Inoue delimiter = " "; 909a4365d0SYoshinobu Inoue 919a4365d0SYoshinobu Inoue switch (xpl->sadb_x_policy_dir) { 929a4365d0SYoshinobu Inoue case IPSEC_DIR_ANY: 939a4365d0SYoshinobu Inoue case IPSEC_DIR_INBOUND: 949a4365d0SYoshinobu Inoue case IPSEC_DIR_OUTBOUND: 959a4365d0SYoshinobu Inoue break; 969a4365d0SYoshinobu Inoue default: 973c62e87aSJun-ichiro itojun Hagino __ipsec_errcode = EIPSEC_INVAL_DIR; 989a4365d0SYoshinobu Inoue return NULL; 999a4365d0SYoshinobu Inoue } 1009a4365d0SYoshinobu Inoue 1019a4365d0SYoshinobu Inoue switch (xpl->sadb_x_policy_type) { 1029a4365d0SYoshinobu Inoue case IPSEC_POLICY_DISCARD: 1039a4365d0SYoshinobu Inoue case IPSEC_POLICY_NONE: 1049a4365d0SYoshinobu Inoue case IPSEC_POLICY_IPSEC: 1059a4365d0SYoshinobu Inoue case IPSEC_POLICY_BYPASS: 1069a4365d0SYoshinobu Inoue case IPSEC_POLICY_ENTRUST: 1079a4365d0SYoshinobu Inoue break; 1089a4365d0SYoshinobu Inoue default: 1093c62e87aSJun-ichiro itojun Hagino __ipsec_errcode = EIPSEC_INVAL_POLICY; 1109a4365d0SYoshinobu Inoue return NULL; 1119a4365d0SYoshinobu Inoue } 1129a4365d0SYoshinobu Inoue 1139a4365d0SYoshinobu Inoue buflen = strlen(ipsp_dir_strs[xpl->sadb_x_policy_dir]) 1149a4365d0SYoshinobu Inoue + 1 /* space */ 1159a4365d0SYoshinobu Inoue + strlen(ipsp_policy_strs[xpl->sadb_x_policy_type]) 1169a4365d0SYoshinobu Inoue + 1; /* NUL */ 1179a4365d0SYoshinobu Inoue 1189a4365d0SYoshinobu Inoue if ((buf = malloc(buflen)) == NULL) { 1193c62e87aSJun-ichiro itojun Hagino __ipsec_errcode = EIPSEC_NO_BUFS; 1209a4365d0SYoshinobu Inoue return NULL; 1219a4365d0SYoshinobu Inoue } 1223c62e87aSJun-ichiro itojun Hagino snprintf(buf, buflen, "%s %s", ipsp_dir_strs[xpl->sadb_x_policy_dir], 1233c62e87aSJun-ichiro itojun Hagino ipsp_policy_strs[xpl->sadb_x_policy_type]); 1249a4365d0SYoshinobu Inoue 1259a4365d0SYoshinobu Inoue if (xpl->sadb_x_policy_type != IPSEC_POLICY_IPSEC) { 1263c62e87aSJun-ichiro itojun Hagino __ipsec_errcode = EIPSEC_NO_ERROR; 1279a4365d0SYoshinobu Inoue return buf; 1289a4365d0SYoshinobu Inoue } 1299a4365d0SYoshinobu Inoue 1309a4365d0SYoshinobu Inoue /* count length of buffer for use */ 1313c62e87aSJun-ichiro itojun Hagino off = sizeof(*xpl); 1323c62e87aSJun-ichiro itojun Hagino while (off < PFKEY_EXTLEN(xpl)) { 1333c62e87aSJun-ichiro itojun Hagino xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xpl + off); 1343c62e87aSJun-ichiro itojun Hagino off += xisr->sadb_x_ipsecrequest_len; 1359a4365d0SYoshinobu Inoue } 1369a4365d0SYoshinobu Inoue 1379a4365d0SYoshinobu Inoue /* validity check */ 1383c62e87aSJun-ichiro itojun Hagino if (off != PFKEY_EXTLEN(xpl)) { 1393c62e87aSJun-ichiro itojun Hagino __ipsec_errcode = EIPSEC_INVAL_SADBMSG; 1409a4365d0SYoshinobu Inoue free(buf); 1419a4365d0SYoshinobu Inoue return NULL; 1429a4365d0SYoshinobu Inoue } 1439a4365d0SYoshinobu Inoue 1443c62e87aSJun-ichiro itojun Hagino off = sizeof(*xpl); 1453c62e87aSJun-ichiro itojun Hagino while (off < PFKEY_EXTLEN(xpl)) { 1463c62e87aSJun-ichiro itojun Hagino xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xpl + off); 1473c62e87aSJun-ichiro itojun Hagino 1483c62e87aSJun-ichiro itojun Hagino if (ipsec_dump_ipsecrequest(isrbuf, sizeof(isrbuf), xisr, 1493c62e87aSJun-ichiro itojun Hagino PFKEY_EXTLEN(xpl) - off) == NULL) { 1503c62e87aSJun-ichiro itojun Hagino free(buf); 1519a4365d0SYoshinobu Inoue return NULL; 1529a4365d0SYoshinobu Inoue } 1539a4365d0SYoshinobu Inoue 1543c62e87aSJun-ichiro itojun Hagino buflen = strlen(buf) + strlen(delimiter) + strlen(isrbuf) + 1; 1553c62e87aSJun-ichiro itojun Hagino newbuf = (char *)realloc(buf, buflen); 1563c62e87aSJun-ichiro itojun Hagino if (newbuf == NULL) { 1573c62e87aSJun-ichiro itojun Hagino __ipsec_errcode = EIPSEC_NO_BUFS; 1583c62e87aSJun-ichiro itojun Hagino free(buf); 1593c62e87aSJun-ichiro itojun Hagino return NULL; 1603c62e87aSJun-ichiro itojun Hagino } 1613c62e87aSJun-ichiro itojun Hagino buf = newbuf; 162864187d8SKevin Lo snprintf(buf + strlen(buf), buflen - strlen(buf), 163864187d8SKevin Lo "%s%s", delimiter, isrbuf); 1649a4365d0SYoshinobu Inoue 1653c62e87aSJun-ichiro itojun Hagino off += xisr->sadb_x_ipsecrequest_len; 1663c62e87aSJun-ichiro itojun Hagino } 1673c62e87aSJun-ichiro itojun Hagino 1683c62e87aSJun-ichiro itojun Hagino __ipsec_errcode = EIPSEC_NO_ERROR; 1693c62e87aSJun-ichiro itojun Hagino return buf; 1703c62e87aSJun-ichiro itojun Hagino } 1713c62e87aSJun-ichiro itojun Hagino 1723c62e87aSJun-ichiro itojun Hagino static char * 17358436df3SMateusz Guzik ipsec_dump_ipsecrequest(char *buf, size_t len, struct sadb_x_ipsecrequest *xisr, 17458436df3SMateusz Guzik size_t bound) 1753c62e87aSJun-ichiro itojun Hagino { 1763c62e87aSJun-ichiro itojun Hagino const char *proto, *mode, *level; 1773c62e87aSJun-ichiro itojun Hagino char abuf[NI_MAXHOST * 2 + 2]; 1783c62e87aSJun-ichiro itojun Hagino 1793c62e87aSJun-ichiro itojun Hagino if (xisr->sadb_x_ipsecrequest_len > bound) { 1803c62e87aSJun-ichiro itojun Hagino __ipsec_errcode = EIPSEC_INVAL_PROTO; 1813c62e87aSJun-ichiro itojun Hagino return NULL; 1823c62e87aSJun-ichiro itojun Hagino } 1839a4365d0SYoshinobu Inoue 1849a4365d0SYoshinobu Inoue switch (xisr->sadb_x_ipsecrequest_proto) { 1859a4365d0SYoshinobu Inoue case IPPROTO_ESP: 1863c62e87aSJun-ichiro itojun Hagino proto = "esp"; 1879a4365d0SYoshinobu Inoue break; 1889a4365d0SYoshinobu Inoue case IPPROTO_AH: 1893c62e87aSJun-ichiro itojun Hagino proto = "ah"; 1909a4365d0SYoshinobu Inoue break; 1919a4365d0SYoshinobu Inoue case IPPROTO_IPCOMP: 1923c62e87aSJun-ichiro itojun Hagino proto = "ipcomp"; 1939a4365d0SYoshinobu Inoue break; 1941922fd12SBruce M Simpson case IPPROTO_TCP: 1951922fd12SBruce M Simpson proto = "tcp"; 1969e9865b6SChristian Brueffer break; 1979a4365d0SYoshinobu Inoue default: 1983c62e87aSJun-ichiro itojun Hagino __ipsec_errcode = EIPSEC_INVAL_PROTO; 1999a4365d0SYoshinobu Inoue return NULL; 2009a4365d0SYoshinobu Inoue } 2019a4365d0SYoshinobu Inoue 2029a4365d0SYoshinobu Inoue switch (xisr->sadb_x_ipsecrequest_mode) { 2039a4365d0SYoshinobu Inoue case IPSEC_MODE_ANY: 2043c62e87aSJun-ichiro itojun Hagino mode = "any"; 2059a4365d0SYoshinobu Inoue break; 2069a4365d0SYoshinobu Inoue case IPSEC_MODE_TRANSPORT: 2073c62e87aSJun-ichiro itojun Hagino mode = "transport"; 2089a4365d0SYoshinobu Inoue break; 2099a4365d0SYoshinobu Inoue case IPSEC_MODE_TUNNEL: 2103c62e87aSJun-ichiro itojun Hagino mode = "tunnel"; 2119a4365d0SYoshinobu Inoue break; 2129a4365d0SYoshinobu Inoue default: 2133c62e87aSJun-ichiro itojun Hagino __ipsec_errcode = EIPSEC_INVAL_MODE; 2149a4365d0SYoshinobu Inoue return NULL; 2159a4365d0SYoshinobu Inoue } 2169a4365d0SYoshinobu Inoue 2173c62e87aSJun-ichiro itojun Hagino abuf[0] = '\0'; 2189a4365d0SYoshinobu Inoue if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) { 2193c62e87aSJun-ichiro itojun Hagino struct sockaddr *sa1, *sa2; 2203c62e87aSJun-ichiro itojun Hagino caddr_t p; 2213c62e87aSJun-ichiro itojun Hagino 2223c62e87aSJun-ichiro itojun Hagino p = (caddr_t)(xisr + 1); 2233c62e87aSJun-ichiro itojun Hagino sa1 = (struct sockaddr *)p; 2243c62e87aSJun-ichiro itojun Hagino sa2 = (struct sockaddr *)(p + sa1->sa_len); 2253c62e87aSJun-ichiro itojun Hagino if (sizeof(*xisr) + sa1->sa_len + sa2->sa_len != 2263c62e87aSJun-ichiro itojun Hagino xisr->sadb_x_ipsecrequest_len) { 2273c62e87aSJun-ichiro itojun Hagino __ipsec_errcode = EIPSEC_INVAL_ADDRESS; 2283c62e87aSJun-ichiro itojun Hagino return NULL; 2293c62e87aSJun-ichiro itojun Hagino } 2303c62e87aSJun-ichiro itojun Hagino if (set_addresses(abuf, sizeof(abuf), sa1, sa2) != 0) { 2313c62e87aSJun-ichiro itojun Hagino __ipsec_errcode = EIPSEC_INVAL_ADDRESS; 2329a4365d0SYoshinobu Inoue return NULL; 2339a4365d0SYoshinobu Inoue } 2349a4365d0SYoshinobu Inoue } 2359a4365d0SYoshinobu Inoue 2369a4365d0SYoshinobu Inoue switch (xisr->sadb_x_ipsecrequest_level) { 2379a4365d0SYoshinobu Inoue case IPSEC_LEVEL_DEFAULT: 2383c62e87aSJun-ichiro itojun Hagino level = "default"; 2399a4365d0SYoshinobu Inoue break; 2409a4365d0SYoshinobu Inoue case IPSEC_LEVEL_USE: 2413c62e87aSJun-ichiro itojun Hagino level = "use"; 2429a4365d0SYoshinobu Inoue break; 2439a4365d0SYoshinobu Inoue case IPSEC_LEVEL_REQUIRE: 2443c62e87aSJun-ichiro itojun Hagino level = "require"; 2459a4365d0SYoshinobu Inoue break; 2469a4365d0SYoshinobu Inoue case IPSEC_LEVEL_UNIQUE: 2473c62e87aSJun-ichiro itojun Hagino level = "unique"; 2489a4365d0SYoshinobu Inoue break; 2499a4365d0SYoshinobu Inoue default: 2503c62e87aSJun-ichiro itojun Hagino __ipsec_errcode = EIPSEC_INVAL_LEVEL; 2519a4365d0SYoshinobu Inoue return NULL; 2529a4365d0SYoshinobu Inoue } 2539a4365d0SYoshinobu Inoue 2543c62e87aSJun-ichiro itojun Hagino if (xisr->sadb_x_ipsecrequest_reqid == 0) 2553c62e87aSJun-ichiro itojun Hagino snprintf(buf, len, "%s/%s/%s/%s", proto, mode, abuf, level); 2563c62e87aSJun-ichiro itojun Hagino else { 2573c62e87aSJun-ichiro itojun Hagino int ch; 2583c62e87aSJun-ichiro itojun Hagino 2593c62e87aSJun-ichiro itojun Hagino if (xisr->sadb_x_ipsecrequest_reqid > IPSEC_MANUAL_REQID_MAX) 2603c62e87aSJun-ichiro itojun Hagino ch = '#'; 2613c62e87aSJun-ichiro itojun Hagino else 2623c62e87aSJun-ichiro itojun Hagino ch = ':'; 2639713f5c1SHajimu UMEMOTO snprintf(buf, len, "%s/%s/%s/%s%c%u", proto, mode, abuf, level, 2643c62e87aSJun-ichiro itojun Hagino ch, xisr->sadb_x_ipsecrequest_reqid); 2659a4365d0SYoshinobu Inoue } 2669a4365d0SYoshinobu Inoue 2679a4365d0SYoshinobu Inoue return buf; 2689a4365d0SYoshinobu Inoue } 2699a4365d0SYoshinobu Inoue 2709a4365d0SYoshinobu Inoue static int 27158436df3SMateusz Guzik set_addresses(char *buf, size_t len, struct sockaddr *sa1, struct sockaddr *sa2) 2729a4365d0SYoshinobu Inoue { 2733c62e87aSJun-ichiro itojun Hagino char tmp1[NI_MAXHOST], tmp2[NI_MAXHOST]; 2749a4365d0SYoshinobu Inoue 2753c62e87aSJun-ichiro itojun Hagino if (set_address(tmp1, sizeof(tmp1), sa1) == NULL || 2763c62e87aSJun-ichiro itojun Hagino set_address(tmp2, sizeof(tmp2), sa2) == NULL) 2773c62e87aSJun-ichiro itojun Hagino return -1; 2783c62e87aSJun-ichiro itojun Hagino if (strlen(tmp1) + 1 + strlen(tmp2) + 1 > len) 2793c62e87aSJun-ichiro itojun Hagino return -1; 2803c62e87aSJun-ichiro itojun Hagino snprintf(buf, len, "%s-%s", tmp1, tmp2); 2819a4365d0SYoshinobu Inoue return 0; 2829a4365d0SYoshinobu Inoue } 2833c62e87aSJun-ichiro itojun Hagino 2843c62e87aSJun-ichiro itojun Hagino static char * 28558436df3SMateusz Guzik set_address(char *buf, size_t len, struct sockaddr *sa) 2863c62e87aSJun-ichiro itojun Hagino { 2873c62e87aSJun-ichiro itojun Hagino const int niflags = NI_NUMERICHOST; 2883c62e87aSJun-ichiro itojun Hagino 2893c62e87aSJun-ichiro itojun Hagino if (len < 1) 2903c62e87aSJun-ichiro itojun Hagino return NULL; 2913c62e87aSJun-ichiro itojun Hagino buf[0] = '\0'; 2923c62e87aSJun-ichiro itojun Hagino if (getnameinfo(sa, sa->sa_len, buf, len, NULL, 0, niflags) != 0) 2933c62e87aSJun-ichiro itojun Hagino return NULL; 2943c62e87aSJun-ichiro itojun Hagino return buf; 2953c62e87aSJun-ichiro itojun Hagino } 296