xref: /netbsd-src/tests/net/ipsec/natt_terminator.c (revision d1268e2e867abd97f55662dac658e00277bd45fa)
1 /*	$NetBSD: natt_terminator.c,v 1.2 2018/11/22 04:51:41 knakahara Exp $	*/
2 
3 /*-
4  * Copyright (c) 2017 Internet Initiative Japan Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/wait.h>
32 #include <sys/time.h>
33 
34 #include <netinet/in.h>
35 #include <netinet/udp.h>
36 
37 #include <stdio.h>
38 #include <err.h>
39 #include <netdb.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 
44 static void
usage(void)45 usage(void)
46 {
47 	const char *prog = "natt_terminator";
48 
49 	fprintf(stderr, "Usage: %s [-46] <addr> <port>\n", prog);
50 }
51 
52 int
main(int argc,char ** argv)53 main(int argc, char **argv)
54 {
55 	struct addrinfo hints;
56 	struct addrinfo *res;
57 	int s, e;
58 	const char *addr, *port;
59 	int option;
60 	int c, family = AF_INET;
61 
62 	while ((c = getopt(argc, argv, "46")) != -1) {
63 		switch (c) {
64 		case '4':
65 			family = AF_INET;
66 			break;
67 		case '6':
68 			family = AF_INET6;
69 			break;
70 		default:
71 			usage();
72 			return 1;
73 		}
74 	}
75 	argc -= optind;
76 	argv += optind;
77 
78 	if (argc != 2) {
79 		usage();
80 		return 1;
81 	}
82 
83 	addr = argv[0];
84 	port = argv[1];
85 
86 	memset(&hints, 0, sizeof(hints));
87 	hints.ai_family = family;
88 	hints.ai_socktype = SOCK_DGRAM;
89 	hints.ai_protocol = IPPROTO_UDP;
90 	hints.ai_flags = 0;
91 
92 	e = getaddrinfo(addr, port, &hints, &res);
93 	if (e != 0)
94 		errx(EXIT_FAILURE, "getaddrinfo failed: %s", gai_strerror(e));
95 
96 	s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
97 	if (s == -1)
98 		err(EXIT_FAILURE, "socket");
99 
100 	/*
101 	 * Set the option to tell the kernel that the socket can handle
102 	 * UDP-encapsulated ESP packets for NAT-T.
103 	 */
104 	option = UDP_ENCAP_ESPINUDP;
105 	e = setsockopt(s, IPPROTO_UDP, UDP_ENCAP, &option, sizeof(option));
106 	if (e == -1)
107 		err(EXIT_FAILURE, "setsockopt(UDP_ENCAP)");
108 
109 	e = bind(s, res->ai_addr, res->ai_addrlen);
110 	if (e == -1)
111 		err(EXIT_FAILURE, "bind");
112 
113 	/* Receiving a packet make the NAPT create a mapping. */
114 	{
115 		char buf[64];
116 		struct sockaddr_storage z;
117 		socklen_t len = sizeof(z);
118 
119 		e = recvfrom(s, buf, 64, MSG_PEEK,
120 		    (struct sockaddr *)&z, &len);
121 		if (e == -1)
122 			err(EXIT_FAILURE, "recvfrom");
123 	}
124 
125 	/*
126 	 * Keep the socket in the kernel to handle UDP-encapsulated ESP packets.
127 	 */
128 	pause();
129 
130 	close(s);
131 
132 	return 0;
133 }
134