1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdio.h>
3 #include <assert.h>
4 #include <unistd.h>
5 #include <bpf/bpf.h>
6 #include <bpf/libbpf.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10 
main(int ac,char ** argv)11 int main(int ac, char **argv)
12 {
13 	struct sockaddr_in *serv_addr_in, *mapped_addr_in, *tmp_addr_in;
14 	struct sockaddr serv_addr, mapped_addr, tmp_addr;
15 	int serverfd, serverconnfd, clientfd, map_fd;
16 	struct bpf_link *link = NULL;
17 	struct bpf_program *prog;
18 	struct bpf_object *obj;
19 	socklen_t sockaddr_len;
20 	char filename[256];
21 	char *ip;
22 
23 	serv_addr_in = (struct sockaddr_in *)&serv_addr;
24 	mapped_addr_in = (struct sockaddr_in *)&mapped_addr;
25 	tmp_addr_in = (struct sockaddr_in *)&tmp_addr;
26 
27 	snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]);
28 	obj = bpf_object__open_file(filename, NULL);
29 	if (libbpf_get_error(obj)) {
30 		fprintf(stderr, "ERROR: opening BPF object file failed\n");
31 		return 0;
32 	}
33 
34 	prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
35 	if (libbpf_get_error(prog)) {
36 		fprintf(stderr, "ERROR: finding a prog in obj file failed\n");
37 		goto cleanup;
38 	}
39 
40 	/* load BPF program */
41 	if (bpf_object__load(obj)) {
42 		fprintf(stderr, "ERROR: loading BPF object file failed\n");
43 		goto cleanup;
44 	}
45 
46 	map_fd = bpf_object__find_map_fd_by_name(obj, "dnat_map");
47 	if (map_fd < 0) {
48 		fprintf(stderr, "ERROR: finding a map in obj file failed\n");
49 		goto cleanup;
50 	}
51 
52 	link = bpf_program__attach(prog);
53 	if (libbpf_get_error(link)) {
54 		fprintf(stderr, "ERROR: bpf_program__attach failed\n");
55 		link = NULL;
56 		goto cleanup;
57 	}
58 
59 	assert((serverfd = socket(AF_INET, SOCK_STREAM, 0)) > 0);
60 	assert((clientfd = socket(AF_INET, SOCK_STREAM, 0)) > 0);
61 
62 	/* Bind server to ephemeral port on lo */
63 	memset(&serv_addr, 0, sizeof(serv_addr));
64 	serv_addr_in->sin_family = AF_INET;
65 	serv_addr_in->sin_port = 0;
66 	serv_addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
67 
68 	assert(bind(serverfd, &serv_addr, sizeof(serv_addr)) == 0);
69 
70 	sockaddr_len = sizeof(serv_addr);
71 	assert(getsockname(serverfd, &serv_addr, &sockaddr_len) == 0);
72 	ip = inet_ntoa(serv_addr_in->sin_addr);
73 	printf("Server bound to: %s:%d\n", ip, ntohs(serv_addr_in->sin_port));
74 
75 	memset(&mapped_addr, 0, sizeof(mapped_addr));
76 	mapped_addr_in->sin_family = AF_INET;
77 	mapped_addr_in->sin_port = htons(5555);
78 	mapped_addr_in->sin_addr.s_addr = inet_addr("255.255.255.255");
79 
80 	assert(!bpf_map_update_elem(map_fd, &mapped_addr, &serv_addr, BPF_ANY));
81 
82 	assert(listen(serverfd, 5) == 0);
83 
84 	ip = inet_ntoa(mapped_addr_in->sin_addr);
85 	printf("Client connecting to: %s:%d\n",
86 	       ip, ntohs(mapped_addr_in->sin_port));
87 	assert(connect(clientfd, &mapped_addr, sizeof(mapped_addr)) == 0);
88 
89 	sockaddr_len = sizeof(tmp_addr);
90 	ip = inet_ntoa(tmp_addr_in->sin_addr);
91 	assert((serverconnfd = accept(serverfd, &tmp_addr, &sockaddr_len)) > 0);
92 	printf("Server received connection from: %s:%d\n",
93 	       ip, ntohs(tmp_addr_in->sin_port));
94 
95 	sockaddr_len = sizeof(tmp_addr);
96 	assert(getpeername(clientfd, &tmp_addr, &sockaddr_len) == 0);
97 	ip = inet_ntoa(tmp_addr_in->sin_addr);
98 	printf("Client's peer address: %s:%d\n",
99 	       ip, ntohs(tmp_addr_in->sin_port));
100 
101 	/* Is the server's getsockname = the socket getpeername */
102 	assert(memcmp(&serv_addr, &tmp_addr, sizeof(struct sockaddr_in)) == 0);
103 
104 cleanup:
105 	bpf_link__destroy(link);
106 	bpf_object__close(obj);
107 	return 0;
108 }
109