xref: /linux/tools/testing/selftests/net/psock_lib.h (revision 8804d970fab45726b3c7cd7f240b31122aa94219)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright 2013 Google Inc.
4  * Author: Willem de Bruijn <willemb@google.com>
5  *         Daniel Borkmann <dborkman@redhat.com>
6  */
7 
8 #ifndef PSOCK_LIB_H
9 #define PSOCK_LIB_H
10 
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <string.h>
14 #include <arpa/inet.h>
15 #include <unistd.h>
16 
17 #include "kselftest.h"
18 
19 #define DATA_LEN			100
20 #define DATA_CHAR			'a'
21 #define DATA_CHAR_1			'b'
22 
23 #define PORT_BASE			8000
24 
pair_udp_setfilter(int fd)25 static __maybe_unused void pair_udp_setfilter(int fd)
26 {
27 	/* the filter below checks for all of the following conditions that
28 	 * are based on the contents of create_payload()
29 	 *  ether type 0x800 and
30 	 *  ip proto udp     and
31 	 *  skb->len == DATA_LEN and
32 	 *  udp[38] == 'a' or udp[38] == 'b'
33 	 * It can be generated from the following bpf_asm input:
34 	 *	ldh [12]
35 	 *	jne #0x800, drop	; ETH_P_IP
36 	 *	ldb [23]
37 	 *	jneq #17, drop		; IPPROTO_UDP
38 	 *	ld len			; ld skb->len
39 	 *	jlt #100, drop		; DATA_LEN
40 	 *	ldb [80]
41 	 *	jeq #97, pass		; DATA_CHAR
42 	 *	jne #98, drop		; DATA_CHAR_1
43 	 *	pass:
44 	 *	  ret #-1
45 	 *	drop:
46 	 *	  ret #0
47 	 */
48 	struct sock_filter bpf_filter[] = {
49 		{ 0x28,  0,  0, 0x0000000c },
50 		{ 0x15,  0,  8, 0x00000800 },
51 		{ 0x30,  0,  0, 0x00000017 },
52 		{ 0x15,  0,  6, 0x00000011 },
53 		{ 0x80,  0,  0, 0000000000 },
54 		{ 0x35,  0,  4, 0x00000064 },
55 		{ 0x30,  0,  0, 0x00000050 },
56 		{ 0x15,  1,  0, 0x00000061 },
57 		{ 0x15,  0,  1, 0x00000062 },
58 		{ 0x06,  0,  0, 0xffffffff },
59 		{ 0x06,  0,  0, 0000000000 },
60 	};
61 	struct sock_fprog bpf_prog;
62 
63 	bpf_prog.filter = bpf_filter;
64 	bpf_prog.len = ARRAY_SIZE(bpf_filter);
65 
66 	if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf_prog,
67 		       sizeof(bpf_prog))) {
68 		perror("setsockopt SO_ATTACH_FILTER");
69 		exit(1);
70 	}
71 }
72 
pair_udp_open(int fds[],uint16_t port)73 static __maybe_unused void pair_udp_open(int fds[], uint16_t port)
74 {
75 	struct sockaddr_in saddr, daddr;
76 
77 	fds[0] = socket(PF_INET, SOCK_DGRAM, 0);
78 	fds[1] = socket(PF_INET, SOCK_DGRAM, 0);
79 	if (fds[0] == -1 || fds[1] == -1) {
80 		fprintf(stderr, "ERROR: socket dgram\n");
81 		exit(1);
82 	}
83 
84 	memset(&saddr, 0, sizeof(saddr));
85 	saddr.sin_family = AF_INET;
86 	saddr.sin_port = htons(port);
87 	saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
88 
89 	memset(&daddr, 0, sizeof(daddr));
90 	daddr.sin_family = AF_INET;
91 	daddr.sin_port = htons(port + 1);
92 	daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
93 
94 	/* must bind both to get consistent hash result */
95 	if (bind(fds[1], (void *) &daddr, sizeof(daddr))) {
96 		perror("bind");
97 		exit(1);
98 	}
99 	if (bind(fds[0], (void *) &saddr, sizeof(saddr))) {
100 		perror("bind");
101 		exit(1);
102 	}
103 	if (connect(fds[0], (void *) &daddr, sizeof(daddr))) {
104 		perror("connect");
105 		exit(1);
106 	}
107 }
108 
pair_udp_send_char(int fds[],int num,char payload)109 static __maybe_unused void pair_udp_send_char(int fds[], int num, char payload)
110 {
111 	char buf[DATA_LEN], rbuf[DATA_LEN];
112 
113 	memset(buf, payload, sizeof(buf));
114 	while (num--) {
115 		/* Should really handle EINTR and EAGAIN */
116 		if (write(fds[0], buf, sizeof(buf)) != sizeof(buf)) {
117 			fprintf(stderr, "ERROR: send failed left=%d\n", num);
118 			exit(1);
119 		}
120 		if (read(fds[1], rbuf, sizeof(rbuf)) != sizeof(rbuf)) {
121 			fprintf(stderr, "ERROR: recv failed left=%d\n", num);
122 			exit(1);
123 		}
124 		if (memcmp(buf, rbuf, sizeof(buf))) {
125 			fprintf(stderr, "ERROR: data failed left=%d\n", num);
126 			exit(1);
127 		}
128 	}
129 }
130 
pair_udp_send(int fds[],int num)131 static __maybe_unused void pair_udp_send(int fds[], int num)
132 {
133 	return pair_udp_send_char(fds, num, DATA_CHAR);
134 }
135 
pair_udp_close(int fds[])136 static __maybe_unused void pair_udp_close(int fds[])
137 {
138 	close(fds[0]);
139 	close(fds[1]);
140 }
141 
142 #endif /* PSOCK_LIB_H */
143