13c62e87aSJun-ichiro itojun Hagino /* $KAME: test-pfkey.c,v 1.4 2000/06/07 00:29:14 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 #include <net/route.h>
389a4365d0SYoshinobu Inoue #include <net/pfkeyv2.h>
399a4365d0SYoshinobu Inoue #include <netinet/in.h>
408409aedfSGeorge V. Neville-Neil #include <netipsec/keydb.h>
418409aedfSGeorge V. Neville-Neil #include <netipsec/key_var.h>
428409aedfSGeorge V. Neville-Neil #include <netipsec/key_debug.h>
439a4365d0SYoshinobu Inoue
449a4365d0SYoshinobu Inoue #include <stdio.h>
459a4365d0SYoshinobu Inoue #include <stdlib.h>
469a4365d0SYoshinobu Inoue #include <limits.h>
479a4365d0SYoshinobu Inoue #include <string.h>
489a4365d0SYoshinobu Inoue #include <ctype.h>
499a4365d0SYoshinobu Inoue #include <unistd.h>
509a4365d0SYoshinobu Inoue #include <errno.h>
513c62e87aSJun-ichiro itojun Hagino #include <netdb.h>
529a4365d0SYoshinobu Inoue
539a4365d0SYoshinobu Inoue u_char m_buf[BUFSIZ];
549a4365d0SYoshinobu Inoue u_int m_len;
559a4365d0SYoshinobu Inoue char *pname;
569a4365d0SYoshinobu Inoue
57784bddbcSKevin Lo void Usage(void);
58784bddbcSKevin Lo int sendkeymsg(void);
59784bddbcSKevin Lo void key_setsadbmsg(u_int);
60784bddbcSKevin Lo void key_setsadbsens(void);
61784bddbcSKevin Lo void key_setsadbprop(void);
62784bddbcSKevin Lo void key_setsadbid(u_int, caddr_t);
63784bddbcSKevin Lo void key_setsadblft(u_int, u_int);
64784bddbcSKevin Lo void key_setspirange(void);
65784bddbcSKevin Lo void key_setsadbkey(u_int, caddr_t);
66784bddbcSKevin Lo void key_setsadbsa(void);
67784bddbcSKevin Lo void key_setsadbaddr(u_int, u_int, caddr_t);
68784bddbcSKevin Lo void key_setsadbextbuf(caddr_t, int, caddr_t, int, caddr_t, int);
699a4365d0SYoshinobu Inoue
709a4365d0SYoshinobu Inoue void
Usage()719a4365d0SYoshinobu Inoue Usage()
729a4365d0SYoshinobu Inoue {
739a4365d0SYoshinobu Inoue printf("Usage:\t%s number\n", pname);
749a4365d0SYoshinobu Inoue exit(0);
759a4365d0SYoshinobu Inoue }
769a4365d0SYoshinobu Inoue
779a4365d0SYoshinobu Inoue int
main(ac,av)789a4365d0SYoshinobu Inoue main(ac, av)
799a4365d0SYoshinobu Inoue int ac;
809a4365d0SYoshinobu Inoue char **av;
819a4365d0SYoshinobu Inoue {
829a4365d0SYoshinobu Inoue pname = *av;
839a4365d0SYoshinobu Inoue
849a4365d0SYoshinobu Inoue if (ac == 1) Usage();
859a4365d0SYoshinobu Inoue
869a4365d0SYoshinobu Inoue key_setsadbmsg(atoi(*(av+1)));
879a4365d0SYoshinobu Inoue sendkeymsg();
889a4365d0SYoshinobu Inoue
899a4365d0SYoshinobu Inoue exit(0);
909a4365d0SYoshinobu Inoue }
919a4365d0SYoshinobu Inoue
929a4365d0SYoshinobu Inoue /* %%% */
939a4365d0SYoshinobu Inoue int
sendkeymsg()949a4365d0SYoshinobu Inoue sendkeymsg()
959a4365d0SYoshinobu Inoue {
969a4365d0SYoshinobu Inoue u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */
979a4365d0SYoshinobu Inoue int so, len;
989a4365d0SYoshinobu Inoue
999a4365d0SYoshinobu Inoue if ((so = socket(PF_KEY, SOCK_RAW, PF_KEY_V2)) < 0) {
1009a4365d0SYoshinobu Inoue perror("socket(PF_KEY)");
1019a4365d0SYoshinobu Inoue goto end;
1029a4365d0SYoshinobu Inoue }
1033c62e87aSJun-ichiro itojun Hagino #if 0
1043c62e87aSJun-ichiro itojun Hagino {
1053c62e87aSJun-ichiro itojun Hagino #include <sys/time.h>
1063c62e87aSJun-ichiro itojun Hagino struct timeval tv;
1073c62e87aSJun-ichiro itojun Hagino tv.tv_sec = 1;
1083c62e87aSJun-ichiro itojun Hagino tv.tv_usec = 0;
1093c62e87aSJun-ichiro itojun Hagino if (setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
1103c62e87aSJun-ichiro itojun Hagino perror("setsockopt");
1113c62e87aSJun-ichiro itojun Hagino goto end;
1123c62e87aSJun-ichiro itojun Hagino }
1133c62e87aSJun-ichiro itojun Hagino }
1143c62e87aSJun-ichiro itojun Hagino #endif
1159a4365d0SYoshinobu Inoue
1169a4365d0SYoshinobu Inoue pfkey_sadump((struct sadb_msg *)m_buf);
1179a4365d0SYoshinobu Inoue
1189a4365d0SYoshinobu Inoue if ((len = send(so, m_buf, m_len, 0)) < 0) {
1199a4365d0SYoshinobu Inoue perror("send");
1209a4365d0SYoshinobu Inoue goto end;
1219a4365d0SYoshinobu Inoue }
1229a4365d0SYoshinobu Inoue
1239a4365d0SYoshinobu Inoue if ((len = recv(so, rbuf, sizeof(rbuf), 0)) < 0) {
1249a4365d0SYoshinobu Inoue perror("recv");
1259a4365d0SYoshinobu Inoue goto end;
1269a4365d0SYoshinobu Inoue }
1279a4365d0SYoshinobu Inoue
1289a4365d0SYoshinobu Inoue pfkey_sadump((struct sadb_msg *)rbuf);
1299a4365d0SYoshinobu Inoue
1309a4365d0SYoshinobu Inoue end:
1319a4365d0SYoshinobu Inoue (void)close(so);
1329a4365d0SYoshinobu Inoue return(0);
1339a4365d0SYoshinobu Inoue }
1349a4365d0SYoshinobu Inoue
1359a4365d0SYoshinobu Inoue void
key_setsadbmsg(type)1369a4365d0SYoshinobu Inoue key_setsadbmsg(type)
1379a4365d0SYoshinobu Inoue u_int type;
1389a4365d0SYoshinobu Inoue {
1399a4365d0SYoshinobu Inoue struct sadb_msg m_msg;
1409a4365d0SYoshinobu Inoue
1413c62e87aSJun-ichiro itojun Hagino memset(&m_msg, 0, sizeof(m_msg));
1429a4365d0SYoshinobu Inoue m_msg.sadb_msg_version = PF_KEY_V2;
1439a4365d0SYoshinobu Inoue m_msg.sadb_msg_type = type;
1449a4365d0SYoshinobu Inoue m_msg.sadb_msg_errno = 0;
1459a4365d0SYoshinobu Inoue m_msg.sadb_msg_satype = SADB_SATYPE_ESP;
1463c62e87aSJun-ichiro itojun Hagino #if 0
1479a4365d0SYoshinobu Inoue m_msg.sadb_msg_reserved = 0;
1483c62e87aSJun-ichiro itojun Hagino #endif
1499a4365d0SYoshinobu Inoue m_msg.sadb_msg_seq = 0;
1509a4365d0SYoshinobu Inoue m_msg.sadb_msg_pid = getpid();
1519a4365d0SYoshinobu Inoue
1529a4365d0SYoshinobu Inoue m_len = sizeof(struct sadb_msg);
1539a4365d0SYoshinobu Inoue memcpy(m_buf, &m_msg, m_len);
1549a4365d0SYoshinobu Inoue
1559a4365d0SYoshinobu Inoue switch (type) {
1569a4365d0SYoshinobu Inoue case SADB_GETSPI:
1579a4365d0SYoshinobu Inoue /*<base, address(SD), SPI range>*/
1589a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_SRC, AF_INET, "10.0.3.4");
1599a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_DST, AF_INET, "127.0.0.1");
1609a4365d0SYoshinobu Inoue key_setspirange();
1619a4365d0SYoshinobu Inoue /*<base, SA(*), address(SD)>*/
1629a4365d0SYoshinobu Inoue break;
1639a4365d0SYoshinobu Inoue
1649a4365d0SYoshinobu Inoue case SADB_ADD:
1659a4365d0SYoshinobu Inoue /* <base, SA, (lifetime(HSC),) address(SD), (address(P),)
1669a4365d0SYoshinobu Inoue key(AE), (identity(SD),) (sensitivity)> */
1679a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_PROXY, AF_INET6, "3ffe::1");
1689a4365d0SYoshinobu Inoue case SADB_UPDATE:
1699a4365d0SYoshinobu Inoue key_setsadbsa();
1709a4365d0SYoshinobu Inoue key_setsadblft(SADB_EXT_LIFETIME_HARD, 10);
1719a4365d0SYoshinobu Inoue key_setsadblft(SADB_EXT_LIFETIME_SOFT, 5);
1729a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_SRC, AF_INET, "192.168.1.1");
1739a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_DST, AF_INET, "10.0.3.4");
1749a4365d0SYoshinobu Inoue /* XXX key_setsadbkey(SADB_EXT_KEY_AUTH, "abcde"); */
1759a4365d0SYoshinobu Inoue key_setsadbkey(SADB_EXT_KEY_AUTH, "1234567812345678");
1769a4365d0SYoshinobu Inoue key_setsadbkey(SADB_EXT_KEY_ENCRYPT, "12345678");
1779a4365d0SYoshinobu Inoue key_setsadbid(SADB_EXT_IDENTITY_SRC, "hoge1234@hoge.com");
1789a4365d0SYoshinobu Inoue key_setsadbid(SADB_EXT_IDENTITY_DST, "hage5678@hage.net");
1799a4365d0SYoshinobu Inoue key_setsadbsens();
1809a4365d0SYoshinobu Inoue /* <base, SA, (lifetime(HSC),) address(SD), (address(P),)
1819a4365d0SYoshinobu Inoue (identity(SD),) (sensitivity)> */
1829a4365d0SYoshinobu Inoue break;
1839a4365d0SYoshinobu Inoue
1849a4365d0SYoshinobu Inoue case SADB_DELETE:
1859a4365d0SYoshinobu Inoue /* <base, SA(*), address(SDP)> */
1869a4365d0SYoshinobu Inoue key_setsadbsa();
1879a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_SRC, AF_INET, "192.168.1.1");
1889a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_DST, AF_INET, "10.0.3.4");
1899a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_PROXY, AF_INET6, "3ffe::1");
1909a4365d0SYoshinobu Inoue /* <base, SA(*), address(SDP)> */
1919a4365d0SYoshinobu Inoue break;
1929a4365d0SYoshinobu Inoue
1939a4365d0SYoshinobu Inoue case SADB_GET:
1949a4365d0SYoshinobu Inoue /* <base, SA(*), address(SDP)> */
1959a4365d0SYoshinobu Inoue key_setsadbsa();
1969a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_SRC, AF_INET, "192.168.1.1");
1979a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_DST, AF_INET, "10.0.3.4");
1989a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_PROXY, AF_INET6, "3ffe::1");
1999a4365d0SYoshinobu Inoue /* <base, SA, (lifetime(HSC),) address(SD), (address(P),)
2009a4365d0SYoshinobu Inoue key(AE), (identity(SD),) (sensitivity)> */
2019a4365d0SYoshinobu Inoue break;
2029a4365d0SYoshinobu Inoue
2039a4365d0SYoshinobu Inoue case SADB_ACQUIRE:
2049a4365d0SYoshinobu Inoue /* <base, address(SD), (address(P),) (identity(SD),)
2059a4365d0SYoshinobu Inoue (sensitivity,) proposal> */
2069a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_SRC, AF_INET, "192.168.1.1");
2079a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_DST, AF_INET, "10.0.3.4");
2089a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_PROXY, AF_INET6, "3ffe::1");
2099a4365d0SYoshinobu Inoue key_setsadbid(SADB_EXT_IDENTITY_SRC, "hoge1234@hoge.com");
2109a4365d0SYoshinobu Inoue key_setsadbid(SADB_EXT_IDENTITY_DST, "hage5678@hage.net");
2119a4365d0SYoshinobu Inoue key_setsadbsens();
2129a4365d0SYoshinobu Inoue key_setsadbprop();
2139a4365d0SYoshinobu Inoue /* <base, address(SD), (address(P),) (identity(SD),)
2149a4365d0SYoshinobu Inoue (sensitivity,) proposal> */
2159a4365d0SYoshinobu Inoue break;
2169a4365d0SYoshinobu Inoue
2179a4365d0SYoshinobu Inoue case SADB_REGISTER:
2189a4365d0SYoshinobu Inoue /* <base> */
2199a4365d0SYoshinobu Inoue /* <base, supported> */
2209a4365d0SYoshinobu Inoue break;
2219a4365d0SYoshinobu Inoue
2229a4365d0SYoshinobu Inoue case SADB_EXPIRE:
2239a4365d0SYoshinobu Inoue case SADB_FLUSH:
2249a4365d0SYoshinobu Inoue break;
2259a4365d0SYoshinobu Inoue
2269a4365d0SYoshinobu Inoue case SADB_DUMP:
2279a4365d0SYoshinobu Inoue break;
2289a4365d0SYoshinobu Inoue
2299a4365d0SYoshinobu Inoue case SADB_X_PROMISC:
2309a4365d0SYoshinobu Inoue /* <base> */
2319a4365d0SYoshinobu Inoue /* <base, base(, others)> */
2329a4365d0SYoshinobu Inoue break;
2339a4365d0SYoshinobu Inoue
2349a4365d0SYoshinobu Inoue case SADB_X_PCHANGE:
2359a4365d0SYoshinobu Inoue break;
2369a4365d0SYoshinobu Inoue
2379a4365d0SYoshinobu Inoue /* for SPD management */
2389a4365d0SYoshinobu Inoue case SADB_X_SPDFLUSH:
2399a4365d0SYoshinobu Inoue case SADB_X_SPDDUMP:
2409a4365d0SYoshinobu Inoue break;
2419a4365d0SYoshinobu Inoue
2429a4365d0SYoshinobu Inoue case SADB_X_SPDADD:
2433c62e87aSJun-ichiro itojun Hagino #if 0
2443c62e87aSJun-ichiro itojun Hagino {
2453c62e87aSJun-ichiro itojun Hagino struct sadb_x_policy m_policy;
2463c62e87aSJun-ichiro itojun Hagino
2473c62e87aSJun-ichiro itojun Hagino m_policy.sadb_x_policy_len = PFKEY_UNIT64(sizeof(m_policy));
2483c62e87aSJun-ichiro itojun Hagino m_policy.sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2493c62e87aSJun-ichiro itojun Hagino m_policy.sadb_x_policy_type = SADB_X_PL_IPSEC;
2503c62e87aSJun-ichiro itojun Hagino m_policy.sadb_x_policy_esp_trans = 1;
2513c62e87aSJun-ichiro itojun Hagino m_policy.sadb_x_policy_ah_trans = 2;
2523c62e87aSJun-ichiro itojun Hagino m_policy.sadb_x_policy_esp_network = 3;
2533c62e87aSJun-ichiro itojun Hagino m_policy.sadb_x_policy_ah_network = 4;
2543c62e87aSJun-ichiro itojun Hagino m_policy.sadb_x_policy_reserved = 0;
2553c62e87aSJun-ichiro itojun Hagino
2563c62e87aSJun-ichiro itojun Hagino memcpy(m_buf + m_len, &m_policy, sizeof(struct sadb_x_policy));
2573c62e87aSJun-ichiro itojun Hagino m_len += sizeof(struct sadb_x_policy);
2583c62e87aSJun-ichiro itojun Hagino }
2593c62e87aSJun-ichiro itojun Hagino #endif
2603c62e87aSJun-ichiro itojun Hagino
2619a4365d0SYoshinobu Inoue case SADB_X_SPDDELETE:
2629a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_SRC, AF_INET, "192.168.1.1");
2639a4365d0SYoshinobu Inoue key_setsadbaddr(SADB_EXT_ADDRESS_DST, AF_INET, "10.0.3.4");
2649a4365d0SYoshinobu Inoue break;
2659a4365d0SYoshinobu Inoue }
2669a4365d0SYoshinobu Inoue
2679a4365d0SYoshinobu Inoue ((struct sadb_msg *)m_buf)->sadb_msg_len = PFKEY_UNIT64(m_len);
2689a4365d0SYoshinobu Inoue
2699a4365d0SYoshinobu Inoue return;
2709a4365d0SYoshinobu Inoue }
2719a4365d0SYoshinobu Inoue
2729a4365d0SYoshinobu Inoue void
key_setsadbsens()2739a4365d0SYoshinobu Inoue key_setsadbsens()
2749a4365d0SYoshinobu Inoue {
2759a4365d0SYoshinobu Inoue struct sadb_sens m_sens;
2769a4365d0SYoshinobu Inoue u_char buf[64];
2779a4365d0SYoshinobu Inoue u_int s, i, slen, ilen, len;
2789a4365d0SYoshinobu Inoue
2799a4365d0SYoshinobu Inoue /* make sens & integ */
2809a4365d0SYoshinobu Inoue s = htonl(0x01234567);
2819a4365d0SYoshinobu Inoue i = htonl(0x89abcdef);
2829a4365d0SYoshinobu Inoue slen = sizeof(s);
2839a4365d0SYoshinobu Inoue ilen = sizeof(i);
2849a4365d0SYoshinobu Inoue memcpy(buf, &s, slen);
2859a4365d0SYoshinobu Inoue memcpy(buf + slen, &i, ilen);
2869a4365d0SYoshinobu Inoue
2879a4365d0SYoshinobu Inoue len = sizeof(m_sens) + PFKEY_ALIGN8(slen) + PFKEY_ALIGN8(ilen);
2889a4365d0SYoshinobu Inoue m_sens.sadb_sens_len = PFKEY_UNIT64(len);
2899a4365d0SYoshinobu Inoue m_sens.sadb_sens_exttype = SADB_EXT_SENSITIVITY;
2909a4365d0SYoshinobu Inoue m_sens.sadb_sens_dpd = 1;
2919a4365d0SYoshinobu Inoue m_sens.sadb_sens_sens_level = 2;
2929a4365d0SYoshinobu Inoue m_sens.sadb_sens_sens_len = PFKEY_ALIGN8(slen);
2939a4365d0SYoshinobu Inoue m_sens.sadb_sens_integ_level = 3;
2949a4365d0SYoshinobu Inoue m_sens.sadb_sens_integ_len = PFKEY_ALIGN8(ilen);
2959a4365d0SYoshinobu Inoue m_sens.sadb_sens_reserved = 0;
2969a4365d0SYoshinobu Inoue
2979a4365d0SYoshinobu Inoue key_setsadbextbuf(m_buf, m_len,
2989a4365d0SYoshinobu Inoue (caddr_t)&m_sens, sizeof(struct sadb_sens),
2999a4365d0SYoshinobu Inoue buf, slen + ilen);
3009a4365d0SYoshinobu Inoue m_len += len;
3019a4365d0SYoshinobu Inoue
3029a4365d0SYoshinobu Inoue return;
3039a4365d0SYoshinobu Inoue }
3049a4365d0SYoshinobu Inoue
3059a4365d0SYoshinobu Inoue void
key_setsadbprop()3069a4365d0SYoshinobu Inoue key_setsadbprop()
3079a4365d0SYoshinobu Inoue {
3089a4365d0SYoshinobu Inoue struct sadb_prop m_prop;
3099a4365d0SYoshinobu Inoue struct sadb_comb *m_comb;
3109a4365d0SYoshinobu Inoue u_char buf[256];
3119a4365d0SYoshinobu Inoue u_int len = sizeof(m_prop) + sizeof(m_comb) * 2;
3129a4365d0SYoshinobu Inoue
3139a4365d0SYoshinobu Inoue /* make prop & comb */
3149a4365d0SYoshinobu Inoue m_prop.sadb_prop_len = PFKEY_UNIT64(len);
3159a4365d0SYoshinobu Inoue m_prop.sadb_prop_exttype = SADB_EXT_PROPOSAL;
3169a4365d0SYoshinobu Inoue m_prop.sadb_prop_replay = 0;
3179a4365d0SYoshinobu Inoue m_prop.sadb_prop_reserved[0] = 0;
3189a4365d0SYoshinobu Inoue m_prop.sadb_prop_reserved[1] = 0;
3199a4365d0SYoshinobu Inoue m_prop.sadb_prop_reserved[2] = 0;
3209a4365d0SYoshinobu Inoue
321*16aabb76SJohn Baldwin /* the 1st is ESP AES-GCM-16 */
3229a4365d0SYoshinobu Inoue m_comb = (struct sadb_comb *)buf;
323*16aabb76SJohn Baldwin m_comb->sadb_comb_auth = SADB_AALG_NONE;
324*16aabb76SJohn Baldwin m_comb->sadb_comb_encrypt = SADB_X_EALG_AESGCM16;
3259a4365d0SYoshinobu Inoue m_comb->sadb_comb_flags = 0;
326*16aabb76SJohn Baldwin m_comb->sadb_comb_auth_minbits = 0;
327*16aabb76SJohn Baldwin m_comb->sadb_comb_auth_maxbits = 0;
328*16aabb76SJohn Baldwin m_comb->sadb_comb_encrypt_minbits = 128;
329*16aabb76SJohn Baldwin m_comb->sadb_comb_encrypt_maxbits = 256;
3309a4365d0SYoshinobu Inoue m_comb->sadb_comb_reserved = 0;
3319a4365d0SYoshinobu Inoue m_comb->sadb_comb_soft_allocations = 0;
3329a4365d0SYoshinobu Inoue m_comb->sadb_comb_hard_allocations = 0;
3339a4365d0SYoshinobu Inoue m_comb->sadb_comb_soft_bytes = 0;
3349a4365d0SYoshinobu Inoue m_comb->sadb_comb_hard_bytes = 0;
3359a4365d0SYoshinobu Inoue m_comb->sadb_comb_soft_addtime = 0;
3369a4365d0SYoshinobu Inoue m_comb->sadb_comb_hard_addtime = 0;
3379a4365d0SYoshinobu Inoue m_comb->sadb_comb_soft_usetime = 0;
3389a4365d0SYoshinobu Inoue m_comb->sadb_comb_hard_usetime = 0;
3399a4365d0SYoshinobu Inoue
340*16aabb76SJohn Baldwin /* the 2nd is ESP AES-CBC and AH HMAC-SHA2-256 */
3419a4365d0SYoshinobu Inoue m_comb = (struct sadb_comb *)(buf + sizeof(*m_comb));
342*16aabb76SJohn Baldwin m_comb->sadb_comb_auth = SADB_X_AALG_SHA2_256;
343*16aabb76SJohn Baldwin m_comb->sadb_comb_encrypt = SADB_X_EALG_RIJNDAELCBC;
3449a4365d0SYoshinobu Inoue m_comb->sadb_comb_flags = 0;
345*16aabb76SJohn Baldwin m_comb->sadb_comb_auth_minbits = 256;
346*16aabb76SJohn Baldwin m_comb->sadb_comb_auth_maxbits = 256;
347*16aabb76SJohn Baldwin m_comb->sadb_comb_encrypt_minbits = 128;
348*16aabb76SJohn Baldwin m_comb->sadb_comb_encrypt_maxbits = 256;
3499a4365d0SYoshinobu Inoue m_comb->sadb_comb_reserved = 0;
3509a4365d0SYoshinobu Inoue m_comb->sadb_comb_soft_allocations = 0;
3519a4365d0SYoshinobu Inoue m_comb->sadb_comb_hard_allocations = 0;
3529a4365d0SYoshinobu Inoue m_comb->sadb_comb_soft_bytes = 0;
3539a4365d0SYoshinobu Inoue m_comb->sadb_comb_hard_bytes = 0;
3549a4365d0SYoshinobu Inoue m_comb->sadb_comb_soft_addtime = 0;
3559a4365d0SYoshinobu Inoue m_comb->sadb_comb_hard_addtime = 0;
3569a4365d0SYoshinobu Inoue m_comb->sadb_comb_soft_usetime = 0;
3579a4365d0SYoshinobu Inoue m_comb->sadb_comb_hard_usetime = 0;
3589a4365d0SYoshinobu Inoue
3599a4365d0SYoshinobu Inoue key_setsadbextbuf(m_buf, m_len,
3609a4365d0SYoshinobu Inoue (caddr_t)&m_prop, sizeof(struct sadb_prop),
3619a4365d0SYoshinobu Inoue buf, sizeof(*m_comb) * 2);
3629a4365d0SYoshinobu Inoue m_len += len;
3639a4365d0SYoshinobu Inoue
3649a4365d0SYoshinobu Inoue return;
3659a4365d0SYoshinobu Inoue }
3669a4365d0SYoshinobu Inoue
3679a4365d0SYoshinobu Inoue void
key_setsadbid(ext,str)3689a4365d0SYoshinobu Inoue key_setsadbid(ext, str)
3699a4365d0SYoshinobu Inoue u_int ext;
3709a4365d0SYoshinobu Inoue caddr_t str;
3719a4365d0SYoshinobu Inoue {
3729a4365d0SYoshinobu Inoue struct sadb_ident m_id;
3739a4365d0SYoshinobu Inoue u_int idlen = strlen(str), len;
3749a4365d0SYoshinobu Inoue
3759a4365d0SYoshinobu Inoue len = sizeof(m_id) + PFKEY_ALIGN8(idlen);
3769a4365d0SYoshinobu Inoue m_id.sadb_ident_len = PFKEY_UNIT64(len);
3779a4365d0SYoshinobu Inoue m_id.sadb_ident_exttype = ext;
3789a4365d0SYoshinobu Inoue m_id.sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
3799a4365d0SYoshinobu Inoue m_id.sadb_ident_reserved = 0;
3809a4365d0SYoshinobu Inoue m_id.sadb_ident_id = getpid();
3819a4365d0SYoshinobu Inoue
3829a4365d0SYoshinobu Inoue key_setsadbextbuf(m_buf, m_len,
3839a4365d0SYoshinobu Inoue (caddr_t)&m_id, sizeof(struct sadb_ident),
3849a4365d0SYoshinobu Inoue str, idlen);
3859a4365d0SYoshinobu Inoue m_len += len;
3869a4365d0SYoshinobu Inoue
3879a4365d0SYoshinobu Inoue return;
3889a4365d0SYoshinobu Inoue }
3899a4365d0SYoshinobu Inoue
3909a4365d0SYoshinobu Inoue void
key_setsadblft(ext,time)3919a4365d0SYoshinobu Inoue key_setsadblft(ext, time)
3929a4365d0SYoshinobu Inoue u_int ext, time;
3939a4365d0SYoshinobu Inoue {
3949a4365d0SYoshinobu Inoue struct sadb_lifetime m_lft;
3959a4365d0SYoshinobu Inoue
3969a4365d0SYoshinobu Inoue m_lft.sadb_lifetime_len = PFKEY_UNIT64(sizeof(m_lft));
3979a4365d0SYoshinobu Inoue m_lft.sadb_lifetime_exttype = ext;
3989a4365d0SYoshinobu Inoue m_lft.sadb_lifetime_allocations = 0x2;
3999a4365d0SYoshinobu Inoue m_lft.sadb_lifetime_bytes = 0x1000;
4009a4365d0SYoshinobu Inoue m_lft.sadb_lifetime_addtime = time;
4019a4365d0SYoshinobu Inoue m_lft.sadb_lifetime_usetime = 0x0020;
4029a4365d0SYoshinobu Inoue
4039a4365d0SYoshinobu Inoue memcpy(m_buf + m_len, &m_lft, sizeof(struct sadb_lifetime));
4049a4365d0SYoshinobu Inoue m_len += sizeof(struct sadb_lifetime);
4059a4365d0SYoshinobu Inoue
4069a4365d0SYoshinobu Inoue return;
4079a4365d0SYoshinobu Inoue }
4089a4365d0SYoshinobu Inoue
4099a4365d0SYoshinobu Inoue void
key_setspirange()4109a4365d0SYoshinobu Inoue key_setspirange()
4119a4365d0SYoshinobu Inoue {
4129a4365d0SYoshinobu Inoue struct sadb_spirange m_spi;
4139a4365d0SYoshinobu Inoue
4149a4365d0SYoshinobu Inoue m_spi.sadb_spirange_len = PFKEY_UNIT64(sizeof(m_spi));
4159a4365d0SYoshinobu Inoue m_spi.sadb_spirange_exttype = SADB_EXT_SPIRANGE;
4169a4365d0SYoshinobu Inoue m_spi.sadb_spirange_min = 0x00001000;
4179a4365d0SYoshinobu Inoue m_spi.sadb_spirange_max = 0x00002000;
4189a4365d0SYoshinobu Inoue m_spi.sadb_spirange_reserved = 0;
4199a4365d0SYoshinobu Inoue
4209a4365d0SYoshinobu Inoue memcpy(m_buf + m_len, &m_spi, sizeof(struct sadb_spirange));
4219a4365d0SYoshinobu Inoue m_len += sizeof(struct sadb_spirange);
4229a4365d0SYoshinobu Inoue
4239a4365d0SYoshinobu Inoue return;
4249a4365d0SYoshinobu Inoue }
4259a4365d0SYoshinobu Inoue
4269a4365d0SYoshinobu Inoue void
key_setsadbkey(ext,str)4279a4365d0SYoshinobu Inoue key_setsadbkey(ext, str)
4289a4365d0SYoshinobu Inoue u_int ext;
4299a4365d0SYoshinobu Inoue caddr_t str;
4309a4365d0SYoshinobu Inoue {
4319a4365d0SYoshinobu Inoue struct sadb_key m_key;
4329a4365d0SYoshinobu Inoue u_int keylen = strlen(str);
4339a4365d0SYoshinobu Inoue u_int len;
4349a4365d0SYoshinobu Inoue
4359a4365d0SYoshinobu Inoue len = sizeof(struct sadb_key) + PFKEY_ALIGN8(keylen);
4369a4365d0SYoshinobu Inoue m_key.sadb_key_len = PFKEY_UNIT64(len);
4379a4365d0SYoshinobu Inoue m_key.sadb_key_exttype = ext;
4389a4365d0SYoshinobu Inoue m_key.sadb_key_bits = keylen * 8;
4399a4365d0SYoshinobu Inoue m_key.sadb_key_reserved = 0;
4409a4365d0SYoshinobu Inoue
4419a4365d0SYoshinobu Inoue key_setsadbextbuf(m_buf, m_len,
4429a4365d0SYoshinobu Inoue (caddr_t)&m_key, sizeof(struct sadb_key),
4439a4365d0SYoshinobu Inoue str, keylen);
4449a4365d0SYoshinobu Inoue m_len += len;
4459a4365d0SYoshinobu Inoue
4469a4365d0SYoshinobu Inoue return;
4479a4365d0SYoshinobu Inoue }
4489a4365d0SYoshinobu Inoue
4499a4365d0SYoshinobu Inoue void
key_setsadbsa()4509a4365d0SYoshinobu Inoue key_setsadbsa()
4519a4365d0SYoshinobu Inoue {
4529a4365d0SYoshinobu Inoue struct sadb_sa m_sa;
4539a4365d0SYoshinobu Inoue
4549a4365d0SYoshinobu Inoue m_sa.sadb_sa_len = PFKEY_UNIT64(sizeof(struct sadb_sa));
4559a4365d0SYoshinobu Inoue m_sa.sadb_sa_exttype = SADB_EXT_SA;
4569a4365d0SYoshinobu Inoue m_sa.sadb_sa_spi = htonl(0x12345678);
4579a4365d0SYoshinobu Inoue m_sa.sadb_sa_replay = 4;
4589a4365d0SYoshinobu Inoue m_sa.sadb_sa_state = 0;
459*16aabb76SJohn Baldwin m_sa.sadb_sa_auth = SADB_AALG_NONE;
460*16aabb76SJohn Baldwin m_sa.sadb_sa_encrypt = SADB_X_EALG_AESGCM16;
4619a4365d0SYoshinobu Inoue m_sa.sadb_sa_flags = 0;
4629a4365d0SYoshinobu Inoue
4639a4365d0SYoshinobu Inoue memcpy(m_buf + m_len, &m_sa, sizeof(struct sadb_sa));
4649a4365d0SYoshinobu Inoue m_len += sizeof(struct sadb_sa);
4659a4365d0SYoshinobu Inoue
4669a4365d0SYoshinobu Inoue return;
4679a4365d0SYoshinobu Inoue }
4689a4365d0SYoshinobu Inoue
4699a4365d0SYoshinobu Inoue void
key_setsadbaddr(ext,af,str)4709a4365d0SYoshinobu Inoue key_setsadbaddr(ext, af, str)
4719a4365d0SYoshinobu Inoue u_int ext, af;
4729a4365d0SYoshinobu Inoue caddr_t str;
4739a4365d0SYoshinobu Inoue {
4749a4365d0SYoshinobu Inoue struct sadb_address m_addr;
4759a4365d0SYoshinobu Inoue u_int len;
4763c62e87aSJun-ichiro itojun Hagino struct addrinfo hints, *res;
4773c62e87aSJun-ichiro itojun Hagino const char *serv;
4783c62e87aSJun-ichiro itojun Hagino int plen;
4793c62e87aSJun-ichiro itojun Hagino
4803c62e87aSJun-ichiro itojun Hagino switch (af) {
4813c62e87aSJun-ichiro itojun Hagino case AF_INET:
4823c62e87aSJun-ichiro itojun Hagino plen = sizeof(struct in_addr) << 3;
4833c62e87aSJun-ichiro itojun Hagino break;
4843c62e87aSJun-ichiro itojun Hagino case AF_INET6:
4853c62e87aSJun-ichiro itojun Hagino plen = sizeof(struct in6_addr) << 3;
4863c62e87aSJun-ichiro itojun Hagino break;
4873c62e87aSJun-ichiro itojun Hagino default:
4883c62e87aSJun-ichiro itojun Hagino /* XXX bark */
4893c62e87aSJun-ichiro itojun Hagino exit(1);
4903c62e87aSJun-ichiro itojun Hagino }
4919a4365d0SYoshinobu Inoue
4929a4365d0SYoshinobu Inoue /* make sockaddr buffer */
4933c62e87aSJun-ichiro itojun Hagino memset(&hints, 0, sizeof(hints));
4943c62e87aSJun-ichiro itojun Hagino hints.ai_family = af;
4953c62e87aSJun-ichiro itojun Hagino hints.ai_socktype = SOCK_DGRAM; /*dummy*/
4963c62e87aSJun-ichiro itojun Hagino hints.ai_flags = AI_NUMERICHOST;
4973c62e87aSJun-ichiro itojun Hagino serv = (ext == SADB_EXT_ADDRESS_PROXY ? "0" : "4660"); /*0x1234*/
4983c62e87aSJun-ichiro itojun Hagino if (getaddrinfo(str, serv, &hints, &res) != 0 || res->ai_next) {
4993c62e87aSJun-ichiro itojun Hagino /* XXX bark */
5003c62e87aSJun-ichiro itojun Hagino exit(1);
5013c62e87aSJun-ichiro itojun Hagino }
5029a4365d0SYoshinobu Inoue
5033c62e87aSJun-ichiro itojun Hagino len = sizeof(struct sadb_address) + PFKEY_ALIGN8(res->ai_addrlen);
5049a4365d0SYoshinobu Inoue m_addr.sadb_address_len = PFKEY_UNIT64(len);
5059a4365d0SYoshinobu Inoue m_addr.sadb_address_exttype = ext;
5069a4365d0SYoshinobu Inoue m_addr.sadb_address_proto =
5079a4365d0SYoshinobu Inoue (ext == SADB_EXT_ADDRESS_PROXY ? 0 : IPPROTO_TCP);
5083c62e87aSJun-ichiro itojun Hagino m_addr.sadb_address_prefixlen = plen;
5099a4365d0SYoshinobu Inoue m_addr.sadb_address_reserved = 0;
5109a4365d0SYoshinobu Inoue
5119a4365d0SYoshinobu Inoue key_setsadbextbuf(m_buf, m_len,
5129a4365d0SYoshinobu Inoue (caddr_t)&m_addr, sizeof(struct sadb_address),
5133c62e87aSJun-ichiro itojun Hagino (caddr_t)res->ai_addr, res->ai_addrlen);
5149a4365d0SYoshinobu Inoue m_len += len;
5159a4365d0SYoshinobu Inoue
5163c62e87aSJun-ichiro itojun Hagino freeaddrinfo(res);
5173c62e87aSJun-ichiro itojun Hagino
5189a4365d0SYoshinobu Inoue return;
5199a4365d0SYoshinobu Inoue }
5209a4365d0SYoshinobu Inoue
5219a4365d0SYoshinobu Inoue void
key_setsadbextbuf(dst,off,ebuf,elen,vbuf,vlen)5229a4365d0SYoshinobu Inoue key_setsadbextbuf(dst, off, ebuf, elen, vbuf, vlen)
5239a4365d0SYoshinobu Inoue caddr_t dst, ebuf, vbuf;
5249a4365d0SYoshinobu Inoue int off, elen, vlen;
5259a4365d0SYoshinobu Inoue {
5269a4365d0SYoshinobu Inoue memset(dst + off, 0, elen + vlen);
5279a4365d0SYoshinobu Inoue memcpy(dst + off, (caddr_t)ebuf, elen);
5289a4365d0SYoshinobu Inoue memcpy(dst + off + elen, vbuf, vlen);
5299a4365d0SYoshinobu Inoue
5309a4365d0SYoshinobu Inoue return;
5319a4365d0SYoshinobu Inoue }
5329a4365d0SYoshinobu Inoue
533