]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/net/ipsec/libipsec/v2_0/tests/test-policy.c
Initial revision
[karo-tx-redboot.git] / packages / net / ipsec / libipsec / v2_0 / tests / test-policy.c
1 //==========================================================================
2 //
3 //      test/test-policy.c
4 //
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
7 //
8 // -------------------------------------------
9 //
10 // Portions of this software may have been derived from OpenBSD, 
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
13 //
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16 //
17 // -------------------------------------------
18 //
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
21
22 /*      $KAME: test-policy.c,v 1.16 2003/08/26 03:24:08 itojun Exp $    */
23
24 /*
25  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
26  * All rights reserved.
27  *
28  * Redistribution and use in source and binary forms, with or without
29  * modification, are permitted provided that the following conditions
30  * are met:
31  * 1. Redistributions of source code must retain the above copyright
32  *    notice, this list of conditions and the following disclaimer.
33  * 2. Redistributions in binary form must reproduce the above copyright
34  *    notice, this list of conditions and the following disclaimer in the
35  *    documentation and/or other materials provided with the distribution.
36  * 3. Neither the name of the project nor the names of its contributors
37  *    may be used to endorse or promote products derived from this software
38  *    without specific prior written permission.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  */
52
53 #include "network.h"
54
55 #include <sys/types.h>
56 #include <sys/param.h>
57 #include <sys/socket.h>
58
59 #include <netinet/in.h>
60 #include <net/pfkeyv2.h>
61 #include <netkey/key_debug.h>
62 #include <netinet6/ipsec.h>
63
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <unistd.h>
67 #include <string.h>
68 #include <errno.h>
69
70 #if 1 //ECOS
71 #include <cyg/infra/testcase.h>
72 #include <cyg/infra/diag.h>
73
74 #define  errx(eval, fmt, ... ) \
75  CYG_MACRO_START             \
76  char buf[128];              \
77  diag_snprintf(buf, sizeof(buf), fmt, ##__VA_ARGS__); \
78  CYG_TEST_FAIL_FINISH(buf); \
79  CYG_MACRO_END 
80
81 #define warn  CYG_TEST_INFO
82 #define warnx CYG_TEST_INFO
83 #define err(eval, str) CYG_TEST_FAIL_FINISH(str)
84 //#define printf diag_printf
85 #else
86 //#include <err.h>
87 #endif
88
89 #include "libpfkey.h"
90
91 struct req_t {
92         int result;     /* expected result; 0:ok 1:ng */
93         char *str;
94 } reqs[] = {
95 { 0, "out ipsec" },
96 { 1, "must_error" },
97 { 1, "in ipsec must_error" },
98 { 1, "out ipsec esp/must_error" },
99 { 1, "out discard" },
100 { 1, "out none" },
101 { 0, "in entrust" },
102 { 0, "out entrust" },
103 { 1, "out ipsec esp" },
104 { 0, "in ipsec ah/transport" },
105 { 1, "in ipsec ah/tunnel" },
106 { 0, "out ipsec ah/transport/" },
107 { 1, "out ipsec ah/tunnel/" },
108 { 0, "in ipsec esp / transport / 10.0.0.1-10.0.0.2" },
109 #ifdef CYGPKG_NET_INET6
110 { 0, "in ipsec esp/tunnel/::1-::2" },
111 { 1, "in ipsec esp/tunnel/10.0.0.1-::2" },
112 { 0, "in ipsec esp/tunnel/::1-::2/require" },
113 #endif
114 { 0, "out ipsec ah/transport//use" },
115 { 1, "out ipsec ah/transport esp/use" },
116 { 1, "in ipsec ah/transport esp/tunnel" },
117 #ifdef CYGPKG_NET_INET6
118 { 0, "in ipsec ah/transport esp/tunnel/::1-::1" },
119 { 0, "in ipsec\n"
120 "       ah / transport\n"
121 "       esp / tunnel / ::1-::2" },
122 { 0, "out ipsec\n"
123 "       ah/transport/::1-::2 esp/tunnel/::3-::4/use ah/transport/::5-::6/require\n"
124 "       ah/transport/::1-::2 esp/tunnel/::3-::4/use ah/transport/::5-::6/require\n"
125 "       ah/transport/::1-::2 esp/tunnel/::3-::4/use ah/transport/::5-::6/require\n"
126 "       " },
127 { 0, "out ipsec esp/transport/fec0::10-fec0::11/use" },
128 #endif
129 };
130
131 int test1 __P((void));
132 int test1sub1 __P((struct req_t *));
133 int test1sub2 __P((char *, int));
134 int test2 __P((void));
135 int test2sub __P((int));
136
137 int
138 main(int ac, char **av)
139 {
140
141         init_all_network_interfaces();
142   
143         test1();
144         test2();
145
146         CYG_TEST_FINISH("done");
147         exit(0);
148 }
149
150 int
151 test1()
152 {
153         int i;
154         int result;
155
156         printf("TEST1\n");
157         for (i = 0; i < sizeof(reqs)/sizeof(reqs[0]); i++) {
158                 printf("#%d [%s]\n", i + 1, reqs[i].str);
159
160                 result = test1sub1(&reqs[i]);
161                 if (result == 0 && reqs[i].result == 1) {
162                         warnx("ERROR: expecting failure.");
163                 } else if (result == 1 && reqs[i].result == 0) {
164                         warnx("ERROR: expecting success.");
165                 }
166         }
167
168         return 0;
169 }
170
171 int
172 test1sub1(req)
173         struct req_t *req;
174 {
175         char *buf;
176
177         buf = ipsec_set_policy(req->str, strlen(req->str));
178         if (buf == NULL) {
179                 printf("ipsec_set_policy: %s\n", ipsec_strerror());
180                 return 1;
181         }
182
183         if (test1sub2(buf, PF_INET) != 0 || 
184 #ifdef CYG_PKG_NET_INET6
185             test1sub2(buf, PF_INET6) != 0
186 #else
187             0
188 #endif
189             ) {
190                 free(buf);
191                 return 1;
192         }
193 #if 0
194         kdebug_sadb_x_policy((struct sadb_ext *)buf);
195 #endif
196         free(buf);
197         return 0;
198 }
199
200 int
201 test1sub2(policy, family)
202         char *policy;
203         int family;
204 {
205         int so;
206         int proto = 0, optname = 0;
207         int len;
208         char getbuf[1024];
209
210         switch (family) {
211         case PF_INET:
212                 proto = IPPROTO_IP;
213                 optname = IP_IPSEC_POLICY;
214                 break;
215         case PF_INET6:
216                 proto = IPPROTO_IPV6;
217                 optname = IPV6_IPSEC_POLICY;
218                 break;
219         }
220
221         if ((so = socket(family, SOCK_DGRAM, 0)) < 0)
222                 err(1, "socket");
223
224         len = ipsec_get_policylen(policy);
225 #if 0
226         printf("\tsetlen:%d\n", len);
227 #endif
228
229         if (setsockopt(so, proto, optname, policy, len) < 0) {
230                 printf("fail to set sockopt; %s\n", strerror(errno));
231                 close(so);
232                 return 1;
233         }
234
235         memset(getbuf, 0, sizeof(getbuf));
236         memcpy(getbuf, policy, sizeof(struct sadb_x_policy));
237         if (getsockopt(so, proto, optname, getbuf, &len) < 0) {
238                 printf("fail to get sockopt; %s\n", strerror(errno));
239                 close(so);
240                 return 1;
241         }
242
243     {
244         char *buf = NULL;
245
246 #if 0
247         printf("\tgetlen:%d\n", len);
248 #endif
249
250         if ((buf = ipsec_dump_policy(getbuf, NULL)) == NULL) {
251                 printf("%s\n", ipsec_strerror());
252                 close(so);
253                 return 1;
254         }
255 #if 1
256         printf("\t[%s]\n", buf);
257 #endif
258         free(buf);
259     }
260
261         close (so);
262         return 0;
263 }
264
265 char addr[] = {
266         28, 28, 0, 0,
267         0, 0, 0, 0,
268         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
269         0, 0, 0, 0,
270 };
271
272 int
273 test2()
274 {
275         int so;
276         char *pol1 = "out ipsec";
277         char *pol2 = "out ipsec ah/transport//use";
278         char *sp1, *sp2;
279         int splen1, splen2;
280         int spid;
281         struct sadb_msg *m;
282
283         printf("TEST2\n");
284 #if 0
285         if (getuid() != 0)
286                 errx(1, "root privilege required.");
287 #endif
288         sp1 = ipsec_set_policy(pol1, strlen(pol1));
289         splen1 = ipsec_get_policylen(sp1);
290         sp2 = ipsec_set_policy(pol2, strlen(pol2));
291         splen2 = ipsec_get_policylen(sp2);
292
293         if ((so = pfkey_open()) < 0)
294                 errx(1, "ERROR: %s", ipsec_strerror());
295
296         printf("spdflush()\n");
297         if (pfkey_send_spdflush(so) < 0)
298                 errx(1, "ERROR: %s", ipsec_strerror());
299         m = pfkey_recv(so);
300         free(m);
301
302         printf("spdsetidx()\n");
303         if (pfkey_send_spdsetidx(so, (struct sockaddr *)addr, 128,
304                                 (struct sockaddr *)addr, 128,
305                                 255, sp1, splen1, 0) < 0)
306                 errx(1, "ERROR: %s", ipsec_strerror());
307         m = pfkey_recv(so);
308         free(m);
309         
310         printf("spdupdate()\n");
311         if (pfkey_send_spdupdate(so, (struct sockaddr *)addr, 128,
312                                 (struct sockaddr *)addr, 128,
313                                 255, sp2, splen2, 0) < 0)
314                 errx(1, "ERROR: %s", ipsec_strerror());
315         m = pfkey_recv(so);
316         free(m);
317
318         printf("sleep(4)\n");
319         sleep(4);
320
321         printf("spddelete()\n");
322         if (pfkey_send_spddelete(so, (struct sockaddr *)addr, 128,
323                                 (struct sockaddr *)addr, 128,
324                                 255, sp1, splen1, 0) < 0)
325                 errx(1, "ERROR: %s", ipsec_strerror());
326         m = pfkey_recv(so);
327         free(m);
328
329         printf("spdadd()\n");
330         if (pfkey_send_spdadd(so, (struct sockaddr *)addr, 128,
331                                 (struct sockaddr *)addr, 128,
332                                 255, sp2, splen2, 0) < 0)
333                 errx(1, "ERROR: %s", ipsec_strerror());
334         spid = test2sub(so);
335
336         printf("spdget(%u)\n", spid);
337         if (pfkey_send_spdget(so, spid) < 0)
338                 errx(1, "ERROR: %s", ipsec_strerror());
339         m = pfkey_recv(so);
340         free(m);
341
342         printf("sleep(4)\n");
343         sleep(4);
344
345         printf("spddelete2()\n");
346         if (pfkey_send_spddelete2(so, spid) < 0)
347                 errx(1, "ERROR: %s", ipsec_strerror());
348         m = pfkey_recv(so);
349         free(m);
350
351         printf("spdadd() with lifetime's 10(s)\n");
352         if (pfkey_send_spdadd2(so, (struct sockaddr *)addr, 128,
353                                 (struct sockaddr *)addr, 128,
354                                 255, 0, 10, sp2, splen2, 0) < 0)
355                 errx(1, "ERROR: %s", ipsec_strerror());
356         spid = test2sub(so);
357
358         /* expecting failure */
359         printf("spdupdate()\n");
360         if (pfkey_send_spdupdate(so, (struct sockaddr *)addr, 128,
361                                 (struct sockaddr *)addr, 128,
362                                 255, sp2, splen2, 0) == 0) {
363                 warnx("ERROR: expecting failure.");
364         }
365
366         return 0;
367 }
368
369 int
370 test2sub(so)
371         int so;
372 {
373         struct sadb_msg *msg;
374         caddr_t mhp[SADB_EXT_MAX + 1];
375
376         if ((msg = pfkey_recv(so)) == NULL)
377                 errx(1, "ERROR: pfkey_recv failure.");
378         if (pfkey_align(msg, mhp) < 0)
379                 errx(1, "ERROR: pfkey_align failure.");
380
381         return ((struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY])->sadb_x_policy_id;
382 }
383