xref: /OK3568_Linux_fs/kernel/samples/bpf/cookie_uid_helper_example.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* This test is a demo of using get_socket_uid and get_socket_cookie
2*4882a593Smuzhiyun  * helper function to do per socket based network traffic monitoring.
3*4882a593Smuzhiyun  * It requires iptables version higher then 1.6.1. to load pinned eBPF
4*4882a593Smuzhiyun  * program into the xt_bpf match.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * TEST:
7*4882a593Smuzhiyun  * ./run_cookie_uid_helper_example.sh -option
8*4882a593Smuzhiyun  * option:
9*4882a593Smuzhiyun  *	-t: do traffic monitoring test, the program will continuously
10*4882a593Smuzhiyun  * print out network traffic happens after program started A sample
11*4882a593Smuzhiyun  * output is shown below:
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * cookie: 877, uid: 0x3e8, Pakcet Count: 20, Bytes Count: 11058
14*4882a593Smuzhiyun  * cookie: 132, uid: 0x0, Pakcet Count: 2, Bytes Count: 286
15*4882a593Smuzhiyun  * cookie: 812, uid: 0x3e8, Pakcet Count: 3, Bytes Count: 1726
16*4882a593Smuzhiyun  * cookie: 802, uid: 0x3e8, Pakcet Count: 2, Bytes Count: 104
17*4882a593Smuzhiyun  * cookie: 877, uid: 0x3e8, Pakcet Count: 20, Bytes Count: 11058
18*4882a593Smuzhiyun  * cookie: 831, uid: 0x3e8, Pakcet Count: 2, Bytes Count: 104
19*4882a593Smuzhiyun  * cookie: 0, uid: 0x0, Pakcet Count: 6, Bytes Count: 712
20*4882a593Smuzhiyun  * cookie: 880, uid: 0xfffe, Pakcet Count: 1, Bytes Count: 70
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  *	-s: do getsockopt SO_COOKIE test, the program will set up a pair of
23*4882a593Smuzhiyun  * UDP sockets and send packets between them. And read out the traffic data
24*4882a593Smuzhiyun  * directly from the ebpf map based on the socket cookie.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * Clean up: if using shell script, the script file will delete the iptables
27*4882a593Smuzhiyun  * rule and unmount the bpf program when exit. Else the iptables rule need
28*4882a593Smuzhiyun  * to be deleted by hand, see run_cookie_uid_helper_example.sh for detail.
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define _GNU_SOURCE
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #define offsetof(type, member)	__builtin_offsetof(type, member)
34*4882a593Smuzhiyun #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #include <arpa/inet.h>
37*4882a593Smuzhiyun #include <errno.h>
38*4882a593Smuzhiyun #include <error.h>
39*4882a593Smuzhiyun #include <limits.h>
40*4882a593Smuzhiyun #include <linux/bpf.h>
41*4882a593Smuzhiyun #include <linux/if_ether.h>
42*4882a593Smuzhiyun #include <net/if.h>
43*4882a593Smuzhiyun #include <signal.h>
44*4882a593Smuzhiyun #include <stdbool.h>
45*4882a593Smuzhiyun #include <stdint.h>
46*4882a593Smuzhiyun #include <stdio.h>
47*4882a593Smuzhiyun #include <stdlib.h>
48*4882a593Smuzhiyun #include <string.h>
49*4882a593Smuzhiyun #include <sys/socket.h>
50*4882a593Smuzhiyun #include <sys/stat.h>
51*4882a593Smuzhiyun #include <sys/types.h>
52*4882a593Smuzhiyun #include <unistd.h>
53*4882a593Smuzhiyun #include <bpf/bpf.h>
54*4882a593Smuzhiyun #include "bpf_insn.h"
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define PORT 8888
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun struct stats {
59*4882a593Smuzhiyun 	uint32_t uid;
60*4882a593Smuzhiyun 	uint64_t packets;
61*4882a593Smuzhiyun 	uint64_t bytes;
62*4882a593Smuzhiyun };
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun static int map_fd, prog_fd;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun static bool test_finish;
67*4882a593Smuzhiyun 
maps_create(void)68*4882a593Smuzhiyun static void maps_create(void)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(uint32_t),
71*4882a593Smuzhiyun 				sizeof(struct stats), 100, 0);
72*4882a593Smuzhiyun 	if (map_fd < 0)
73*4882a593Smuzhiyun 		error(1, errno, "map create failed!\n");
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun 
prog_load(void)76*4882a593Smuzhiyun static void prog_load(void)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	static char log_buf[1 << 16];
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	struct bpf_insn prog[] = {
81*4882a593Smuzhiyun 		/*
82*4882a593Smuzhiyun 		 * Save sk_buff for future usage. value stored in R6 to R10 will
83*4882a593Smuzhiyun 		 * not be reset after a bpf helper function call.
84*4882a593Smuzhiyun 		 */
85*4882a593Smuzhiyun 		BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
86*4882a593Smuzhiyun 		/*
87*4882a593Smuzhiyun 		 * pc1: BPF_FUNC_get_socket_cookie takes one parameter,
88*4882a593Smuzhiyun 		 * R1: sk_buff
89*4882a593Smuzhiyun 		 */
90*4882a593Smuzhiyun 		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
91*4882a593Smuzhiyun 				BPF_FUNC_get_socket_cookie),
92*4882a593Smuzhiyun 		/* pc2-4: save &socketCookie to r7 for future usage*/
93*4882a593Smuzhiyun 		BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
94*4882a593Smuzhiyun 		BPF_MOV64_REG(BPF_REG_7, BPF_REG_10),
95*4882a593Smuzhiyun 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, -8),
96*4882a593Smuzhiyun 		/*
97*4882a593Smuzhiyun 		 * pc5-8: set up the registers for BPF_FUNC_map_lookup_elem,
98*4882a593Smuzhiyun 		 * it takes two parameters (R1: map_fd,  R2: &socket_cookie)
99*4882a593Smuzhiyun 		 */
100*4882a593Smuzhiyun 		BPF_LD_MAP_FD(BPF_REG_1, map_fd),
101*4882a593Smuzhiyun 		BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
102*4882a593Smuzhiyun 		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
103*4882a593Smuzhiyun 				BPF_FUNC_map_lookup_elem),
104*4882a593Smuzhiyun 		/*
105*4882a593Smuzhiyun 		 * pc9. if r0 != 0x0, go to pc+14, since we have the cookie
106*4882a593Smuzhiyun 		 * stored already
107*4882a593Smuzhiyun 		 * Otherwise do pc10-22 to setup a new data entry.
108*4882a593Smuzhiyun 		 */
109*4882a593Smuzhiyun 		BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 14),
110*4882a593Smuzhiyun 		BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
111*4882a593Smuzhiyun 		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
112*4882a593Smuzhiyun 				BPF_FUNC_get_socket_uid),
113*4882a593Smuzhiyun 		/*
114*4882a593Smuzhiyun 		 * Place a struct stats in the R10 stack and sequentially
115*4882a593Smuzhiyun 		 * place the member value into the memory. Packets value
116*4882a593Smuzhiyun 		 * is set by directly place a IMM value 1 into the stack.
117*4882a593Smuzhiyun 		 */
118*4882a593Smuzhiyun 		BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0,
119*4882a593Smuzhiyun 			    -32 + (__s16)offsetof(struct stats, uid)),
120*4882a593Smuzhiyun 		BPF_ST_MEM(BPF_DW, BPF_REG_10,
121*4882a593Smuzhiyun 			   -32 + (__s16)offsetof(struct stats, packets), 1),
122*4882a593Smuzhiyun 		/*
123*4882a593Smuzhiyun 		 * __sk_buff is a special struct used for eBPF program to
124*4882a593Smuzhiyun 		 * directly access some sk_buff field.
125*4882a593Smuzhiyun 		 */
126*4882a593Smuzhiyun 		BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_6,
127*4882a593Smuzhiyun 				offsetof(struct __sk_buff, len)),
128*4882a593Smuzhiyun 		BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1,
129*4882a593Smuzhiyun 			    -32 + (__s16)offsetof(struct stats, bytes)),
130*4882a593Smuzhiyun 		/*
131*4882a593Smuzhiyun 		 * add new map entry using BPF_FUNC_map_update_elem, it takes
132*4882a593Smuzhiyun 		 * 4 parameters (R1: map_fd, R2: &socket_cookie, R3: &stats,
133*4882a593Smuzhiyun 		 * R4: flags)
134*4882a593Smuzhiyun 		 */
135*4882a593Smuzhiyun 		BPF_LD_MAP_FD(BPF_REG_1, map_fd),
136*4882a593Smuzhiyun 		BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
137*4882a593Smuzhiyun 		BPF_MOV64_REG(BPF_REG_3, BPF_REG_10),
138*4882a593Smuzhiyun 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, -32),
139*4882a593Smuzhiyun 		BPF_MOV64_IMM(BPF_REG_4, 0),
140*4882a593Smuzhiyun 		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
141*4882a593Smuzhiyun 				BPF_FUNC_map_update_elem),
142*4882a593Smuzhiyun 		BPF_JMP_IMM(BPF_JA, 0, 0, 5),
143*4882a593Smuzhiyun 		/*
144*4882a593Smuzhiyun 		 * pc24-30 update the packet info to a exist data entry, it can
145*4882a593Smuzhiyun 		 * be done by directly write to pointers instead of using
146*4882a593Smuzhiyun 		 * BPF_FUNC_map_update_elem helper function
147*4882a593Smuzhiyun 		 */
148*4882a593Smuzhiyun 		BPF_MOV64_REG(BPF_REG_9, BPF_REG_0),
149*4882a593Smuzhiyun 		BPF_MOV64_IMM(BPF_REG_1, 1),
150*4882a593Smuzhiyun 		BPF_STX_XADD(BPF_DW, BPF_REG_9, BPF_REG_1,
151*4882a593Smuzhiyun 				offsetof(struct stats, packets)),
152*4882a593Smuzhiyun 		BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_6,
153*4882a593Smuzhiyun 				offsetof(struct __sk_buff, len)),
154*4882a593Smuzhiyun 		BPF_STX_XADD(BPF_DW, BPF_REG_9, BPF_REG_1,
155*4882a593Smuzhiyun 				offsetof(struct stats, bytes)),
156*4882a593Smuzhiyun 		BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_6,
157*4882a593Smuzhiyun 				offsetof(struct __sk_buff, len)),
158*4882a593Smuzhiyun 		BPF_EXIT_INSN(),
159*4882a593Smuzhiyun 	};
160*4882a593Smuzhiyun 	prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog,
161*4882a593Smuzhiyun 					ARRAY_SIZE(prog), "GPL", 0,
162*4882a593Smuzhiyun 					log_buf, sizeof(log_buf));
163*4882a593Smuzhiyun 	if (prog_fd < 0)
164*4882a593Smuzhiyun 		error(1, errno, "failed to load prog\n%s\n", log_buf);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
prog_attach_iptables(char * file)167*4882a593Smuzhiyun static void prog_attach_iptables(char *file)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	int ret;
170*4882a593Smuzhiyun 	char rules[100];
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	if (bpf_obj_pin(prog_fd, file))
173*4882a593Smuzhiyun 		error(1, errno, "bpf_obj_pin");
174*4882a593Smuzhiyun 	if (strlen(file) > 50) {
175*4882a593Smuzhiyun 		printf("file path too long: %s\n", file);
176*4882a593Smuzhiyun 		exit(1);
177*4882a593Smuzhiyun 	}
178*4882a593Smuzhiyun 	sprintf(rules, "iptables -A OUTPUT -m bpf --object-pinned %s -j ACCEPT",
179*4882a593Smuzhiyun 		file);
180*4882a593Smuzhiyun 	ret = system(rules);
181*4882a593Smuzhiyun 	if (ret < 0) {
182*4882a593Smuzhiyun 		printf("iptables rule update failed: %d/n", WEXITSTATUS(ret));
183*4882a593Smuzhiyun 		exit(1);
184*4882a593Smuzhiyun 	}
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun 
print_table(void)187*4882a593Smuzhiyun static void print_table(void)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun 	struct stats curEntry;
190*4882a593Smuzhiyun 	uint32_t curN = UINT32_MAX;
191*4882a593Smuzhiyun 	uint32_t nextN;
192*4882a593Smuzhiyun 	int res;
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	while (bpf_map_get_next_key(map_fd, &curN, &nextN) > -1) {
195*4882a593Smuzhiyun 		curN = nextN;
196*4882a593Smuzhiyun 		res = bpf_map_lookup_elem(map_fd, &curN, &curEntry);
197*4882a593Smuzhiyun 		if (res < 0) {
198*4882a593Smuzhiyun 			error(1, errno, "fail to get entry value of Key: %u\n",
199*4882a593Smuzhiyun 				curN);
200*4882a593Smuzhiyun 		} else {
201*4882a593Smuzhiyun 			printf("cookie: %u, uid: 0x%x, Packet Count: %lu,"
202*4882a593Smuzhiyun 				" Bytes Count: %lu\n", curN, curEntry.uid,
203*4882a593Smuzhiyun 				curEntry.packets, curEntry.bytes);
204*4882a593Smuzhiyun 		}
205*4882a593Smuzhiyun 	}
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun 
udp_client(void)208*4882a593Smuzhiyun static void udp_client(void)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun 	struct sockaddr_in si_other = {0};
211*4882a593Smuzhiyun 	struct sockaddr_in si_me = {0};
212*4882a593Smuzhiyun 	struct stats dataEntry;
213*4882a593Smuzhiyun 	int s_rcv, s_send, i, recv_len;
214*4882a593Smuzhiyun 	char message = 'a';
215*4882a593Smuzhiyun 	char buf;
216*4882a593Smuzhiyun 	uint64_t cookie;
217*4882a593Smuzhiyun 	int res;
218*4882a593Smuzhiyun 	socklen_t cookie_len = sizeof(cookie);
219*4882a593Smuzhiyun 	socklen_t slen = sizeof(si_other);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	s_rcv = socket(PF_INET, SOCK_DGRAM, 0);
222*4882a593Smuzhiyun 	if (s_rcv < 0)
223*4882a593Smuzhiyun 		error(1, errno, "rcv socket creat failed!\n");
224*4882a593Smuzhiyun 	si_other.sin_family = AF_INET;
225*4882a593Smuzhiyun 	si_other.sin_port = htons(PORT);
226*4882a593Smuzhiyun 	if (inet_aton("127.0.0.1", &si_other.sin_addr) == 0)
227*4882a593Smuzhiyun 		error(1, errno, "inet_aton\n");
228*4882a593Smuzhiyun 	if (bind(s_rcv, (struct sockaddr *)&si_other, sizeof(si_other)) == -1)
229*4882a593Smuzhiyun 		error(1, errno, "bind\n");
230*4882a593Smuzhiyun 	s_send = socket(PF_INET, SOCK_DGRAM, 0);
231*4882a593Smuzhiyun 	if (s_send < 0)
232*4882a593Smuzhiyun 		error(1, errno, "send socket creat failed!\n");
233*4882a593Smuzhiyun 	res = getsockopt(s_send, SOL_SOCKET, SO_COOKIE, &cookie, &cookie_len);
234*4882a593Smuzhiyun 	if (res < 0)
235*4882a593Smuzhiyun 		printf("get cookie failed: %s\n", strerror(errno));
236*4882a593Smuzhiyun 	res = bpf_map_lookup_elem(map_fd, &cookie, &dataEntry);
237*4882a593Smuzhiyun 	if (res != -1)
238*4882a593Smuzhiyun 		error(1, errno, "socket stat found while flow not active\n");
239*4882a593Smuzhiyun 	for (i = 0; i < 10; i++) {
240*4882a593Smuzhiyun 		res = sendto(s_send, &message, sizeof(message), 0,
241*4882a593Smuzhiyun 			     (struct sockaddr *)&si_other, slen);
242*4882a593Smuzhiyun 		if (res == -1)
243*4882a593Smuzhiyun 			error(1, errno, "send\n");
244*4882a593Smuzhiyun 		if (res != sizeof(message))
245*4882a593Smuzhiyun 			error(1, 0, "%uB != %luB\n", res, sizeof(message));
246*4882a593Smuzhiyun 		recv_len = recvfrom(s_rcv, &buf, sizeof(buf), 0,
247*4882a593Smuzhiyun 			     (struct sockaddr *)&si_me, &slen);
248*4882a593Smuzhiyun 		if (recv_len < 0)
249*4882a593Smuzhiyun 			error(1, errno, "receive\n");
250*4882a593Smuzhiyun 		res = memcmp(&(si_other.sin_addr), &(si_me.sin_addr),
251*4882a593Smuzhiyun 			   sizeof(si_me.sin_addr));
252*4882a593Smuzhiyun 		if (res != 0)
253*4882a593Smuzhiyun 			error(1, EFAULT, "sender addr error: %d\n", res);
254*4882a593Smuzhiyun 		printf("Message received: %c\n", buf);
255*4882a593Smuzhiyun 		res = bpf_map_lookup_elem(map_fd, &cookie, &dataEntry);
256*4882a593Smuzhiyun 		if (res < 0)
257*4882a593Smuzhiyun 			error(1, errno, "lookup sk stat failed, cookie: %lu\n",
258*4882a593Smuzhiyun 			      cookie);
259*4882a593Smuzhiyun 		printf("cookie: %lu, uid: 0x%x, Packet Count: %lu,"
260*4882a593Smuzhiyun 			" Bytes Count: %lu\n\n", cookie, dataEntry.uid,
261*4882a593Smuzhiyun 			dataEntry.packets, dataEntry.bytes);
262*4882a593Smuzhiyun 	}
263*4882a593Smuzhiyun 	close(s_send);
264*4882a593Smuzhiyun 	close(s_rcv);
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun 
usage(void)267*4882a593Smuzhiyun static int usage(void)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun 	printf("Usage: ./run_cookie_uid_helper_example.sh"
270*4882a593Smuzhiyun 		" bpfObjName -option\n"
271*4882a593Smuzhiyun 		"	-t	traffic monitor test\n"
272*4882a593Smuzhiyun 		"	-s	getsockopt cookie test\n");
273*4882a593Smuzhiyun 	return 1;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun 
finish(int ret)276*4882a593Smuzhiyun static void finish(int ret)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	test_finish = true;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun 
main(int argc,char * argv[])281*4882a593Smuzhiyun int main(int argc, char *argv[])
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun 	int opt;
284*4882a593Smuzhiyun 	bool cfg_test_traffic = false;
285*4882a593Smuzhiyun 	bool cfg_test_cookie = false;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	if (argc != 3)
288*4882a593Smuzhiyun 		return usage();
289*4882a593Smuzhiyun 	while ((opt = getopt(argc, argv, "ts")) != -1) {
290*4882a593Smuzhiyun 		switch (opt) {
291*4882a593Smuzhiyun 		case 't':
292*4882a593Smuzhiyun 			cfg_test_traffic = true;
293*4882a593Smuzhiyun 			break;
294*4882a593Smuzhiyun 		case 's':
295*4882a593Smuzhiyun 			cfg_test_cookie = true;
296*4882a593Smuzhiyun 			break;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 		default:
299*4882a593Smuzhiyun 			printf("unknown option %c\n", opt);
300*4882a593Smuzhiyun 			usage();
301*4882a593Smuzhiyun 			return -1;
302*4882a593Smuzhiyun 		}
303*4882a593Smuzhiyun 	}
304*4882a593Smuzhiyun 	maps_create();
305*4882a593Smuzhiyun 	prog_load();
306*4882a593Smuzhiyun 	prog_attach_iptables(argv[2]);
307*4882a593Smuzhiyun 	if (cfg_test_traffic) {
308*4882a593Smuzhiyun 		if (signal(SIGINT, finish) == SIG_ERR)
309*4882a593Smuzhiyun 			error(1, errno, "register SIGINT handler failed");
310*4882a593Smuzhiyun 		if (signal(SIGTERM, finish) == SIG_ERR)
311*4882a593Smuzhiyun 			error(1, errno, "register SIGTERM handler failed");
312*4882a593Smuzhiyun 		while (!test_finish) {
313*4882a593Smuzhiyun 			print_table();
314*4882a593Smuzhiyun 			printf("\n");
315*4882a593Smuzhiyun 			sleep(1);
316*4882a593Smuzhiyun 		};
317*4882a593Smuzhiyun 	} else if (cfg_test_cookie) {
318*4882a593Smuzhiyun 		udp_client();
319*4882a593Smuzhiyun 	}
320*4882a593Smuzhiyun 	close(prog_fd);
321*4882a593Smuzhiyun 	close(map_fd);
322*4882a593Smuzhiyun 	return 0;
323*4882a593Smuzhiyun }
324