xref: /netbsd-src/tests/net/ipsec/natt_terminator.c (revision 0de7b04927a623ceae627102c38349d9255afe3b)
1 /*	$NetBSD: natt_terminator.c,v 1.1 2017/10/30 15:59:23 ozaki-r 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 int
45 main(int argc, char **argv)
46 {
47 	struct addrinfo hints;
48 	struct addrinfo *res;
49 	int s, e;
50 	const char *addr, *port;
51 	int option;
52 
53 	if (argc != 3) {
54 		fprintf(stderr, "Usage: %s <addr> <port>\n", argv[0]);
55 		return 1;
56 	}
57 
58 	addr = argv[1];
59 	port = argv[2];
60 
61 	memset(&hints, 0, sizeof(hints));
62 	hints.ai_family = AF_INET;
63 	hints.ai_socktype = SOCK_DGRAM;
64 	hints.ai_protocol = IPPROTO_UDP;
65 	hints.ai_flags = 0;
66 
67 	e = getaddrinfo(addr, port, &hints, &res);
68 	if (e != 0)
69 		errx(EXIT_FAILURE, "getaddrinfo failed: %s", gai_strerror(e));
70 
71 	s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
72 	if (s == -1)
73 		err(EXIT_FAILURE, "socket");
74 
75 	/*
76 	 * Set the option to tell the kernel that the socket can handle
77 	 * UDP-encapsulated ESP packets for NAT-T.
78 	 */
79 	option = UDP_ENCAP_ESPINUDP;
80 	e = setsockopt(s, IPPROTO_UDP, UDP_ENCAP, &option, sizeof(option));
81 	if (e == -1)
82 		err(EXIT_FAILURE, "setsockopt(UDP_ENCAP)");
83 
84 	e = bind(s, res->ai_addr, res->ai_addrlen);
85 	if (e == -1)
86 		err(EXIT_FAILURE, "bind");
87 
88 	/* Receiving a packet make the NAPT create a mapping. */
89 	{
90 		char buf[64];
91 		struct sockaddr_storage z;
92 		socklen_t len = sizeof(z);
93 
94 		e = recvfrom(s, buf, 64, MSG_PEEK,
95 		    (struct sockaddr *)&z, &len);
96 		if (e == -1)
97 			err(EXIT_FAILURE, "recvfrom");
98 	}
99 
100 	/*
101 	 * Keep the socket in the kernel to handle UDP-encapsulated ESP packets.
102 	 */
103 	pause();
104 
105 	close(s);
106 
107 	return 0;
108 }
109