test-policy.c revision 55505
1/*
2 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/sbin/setkey/test-policy.c 55505 2000-01-06 12:40:54Z shin $
30 */
31
32#include <sys/types.h>
33#include <sys/param.h>
34#include <sys/socket.h>
35#include <netinet/in.h>
36#include <netinet6/in6.h>
37#include <netkey/keyv2.h>
38#include <stdlib.h>
39#include <string.h>
40
41#include <netinet6/ipsec.h>
42
43char *requests[] = {
44"must_error",		/* must be error */
45"ipsec must_error",	/* must be error */
46"ipsec esp/must_error",	/* must be error */
47"discard",
48"none",
49"entrust",
50"bypass",		/* may be error */
51"ipsec esp",		/* must be error */
52"ipsec ah/require",
53"ipsec ah/use/",
54"ipsec esp/require ah/default/203.178.141.194",
55"ipsec ah/use/203.178.141.195 esp/use/203.178.141.194",
56"ipsec esp/elf.wide.ydc.co.jp esp/www.wide.ydc.co.jp"
57"
58ipsec esp/require ah/use esp/require/10.0.0.1
59ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
60ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
61ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
62ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
63ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
64ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
65ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1 ah/use/3ffe:501:481d::1
66ah/use/3ffe:501:481d::1  ah/use/3ffe:501:481d::1ah/use/3ffe:501:481d::1
67",
68};
69
70u_char	*p_secpolicy;
71
72int	test(char *buf, int family);
73char	*setpolicy(char *req);
74
75main()
76{
77	int i;
78	char *buf;
79
80	for (i = 0; i < sizeof(requests)/sizeof(requests[0]); i++) {
81		printf("* requests:[%s]\n", requests[i]);
82		if ((buf = setpolicy(requests[i])) == NULL)
83			continue;
84		printf("\tsetlen:%d\n", PFKEY_EXTLEN(buf));
85
86		printf("\tPF_INET:\n");
87		test(buf, PF_INET);
88
89		printf("\tPF_INET6:\n");
90		test(buf, PF_INET6);
91		free(buf);
92	}
93}
94
95int test(char *policy, int family)
96{
97	int so, proto, optname;
98	int len;
99	char getbuf[1024];
100
101	switch (family) {
102	case PF_INET:
103		proto = IPPROTO_IP;
104		optname = IP_IPSEC_POLICY;
105		break;
106	case PF_INET6:
107		proto = IPPROTO_IPV6;
108		optname = IPV6_IPSEC_POLICY;
109		break;
110	}
111
112	if ((so = socket(family, SOCK_DGRAM, 0)) < 0)
113		perror("socket");
114
115	if (setsockopt(so, proto, optname, policy, PFKEY_EXTLEN(policy)) < 0)
116		perror("setsockopt");
117
118	len = sizeof(getbuf);
119	memset(getbuf, 0, sizeof(getbuf));
120	if (getsockopt(so, proto, optname, getbuf, &len) < 0)
121		perror("getsockopt");
122
123    {
124	char *buf = NULL;
125
126	printf("\tgetlen:%d\n", len);
127
128	if ((buf = ipsec_dump_policy(getbuf, NULL)) == NULL)
129		ipsec_strerror();
130	else
131		printf("\t[%s]\n", buf);
132
133	free(buf);
134    }
135
136	close (so);
137}
138
139char *setpolicy(char *req)
140{
141	int len;
142	char *buf;
143
144	if ((len = ipsec_get_policylen(req)) < 0) {
145		printf("ipsec_get_policylen: %s\n", ipsec_strerror());
146		return NULL;
147	}
148
149	if ((buf = malloc(len)) == NULL) {
150		perror("malloc");
151		return NULL;
152	}
153
154	if ((len = ipsec_set_policy(buf, len, req)) < 0) {
155		printf("ipsec_set_policy: %s\n", ipsec_strerror());
156		free(buf);
157		return NULL;
158	}
159
160	return buf;
161}
162