xref: /openbmc/linux/tools/testing/selftests/net/psock_lib.h (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1fc44ef5aSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
223a95442SDaniel Borkmann /*
323a95442SDaniel Borkmann  * Copyright 2013 Google Inc.
423a95442SDaniel Borkmann  * Author: Willem de Bruijn <willemb@google.com>
523a95442SDaniel Borkmann  *         Daniel Borkmann <dborkman@redhat.com>
623a95442SDaniel Borkmann  */
723a95442SDaniel Borkmann 
823a95442SDaniel Borkmann #ifndef PSOCK_LIB_H
923a95442SDaniel Borkmann #define PSOCK_LIB_H
1023a95442SDaniel Borkmann 
1123a95442SDaniel Borkmann #include <sys/types.h>
1223a95442SDaniel Borkmann #include <sys/socket.h>
1323a95442SDaniel Borkmann #include <string.h>
1423a95442SDaniel Borkmann #include <arpa/inet.h>
1523a95442SDaniel Borkmann #include <unistd.h>
1623a95442SDaniel Borkmann 
17*3645c71bSMahmoud Maatuq #include "kselftest.h"
18*3645c71bSMahmoud Maatuq 
1923a95442SDaniel Borkmann #define DATA_LEN			100
2023a95442SDaniel Borkmann #define DATA_CHAR			'a'
2195e22792SWillem de Bruijn #define DATA_CHAR_1			'b'
2223a95442SDaniel Borkmann 
2323a95442SDaniel Borkmann #define PORT_BASE			8000
2423a95442SDaniel Borkmann 
2523a95442SDaniel Borkmann #ifndef __maybe_unused
2623a95442SDaniel Borkmann # define __maybe_unused		__attribute__ ((__unused__))
2723a95442SDaniel Borkmann #endif
2823a95442SDaniel Borkmann 
pair_udp_setfilter(int fd)29c1f8d0f9SMike Maloney static __maybe_unused void pair_udp_setfilter(int fd)
3023a95442SDaniel Borkmann {
314d7b9dc1SSowmini Varadhan 	/* the filter below checks for all of the following conditions that
324d7b9dc1SSowmini Varadhan 	 * are based on the contents of create_payload()
334d7b9dc1SSowmini Varadhan 	 *  ether type 0x800 and
344d7b9dc1SSowmini Varadhan 	 *  ip proto udp     and
354d7b9dc1SSowmini Varadhan 	 *  skb->len == DATA_LEN and
364d7b9dc1SSowmini Varadhan 	 *  udp[38] == 'a' or udp[38] == 'b'
374d7b9dc1SSowmini Varadhan 	 * It can be generated from the following bpf_asm input:
384d7b9dc1SSowmini Varadhan 	 *	ldh [12]
394d7b9dc1SSowmini Varadhan 	 *	jne #0x800, drop	; ETH_P_IP
404d7b9dc1SSowmini Varadhan 	 *	ldb [23]
414d7b9dc1SSowmini Varadhan 	 *	jneq #17, drop		; IPPROTO_UDP
424d7b9dc1SSowmini Varadhan 	 *	ld len			; ld skb->len
434d7b9dc1SSowmini Varadhan 	 *	jlt #100, drop		; DATA_LEN
444d7b9dc1SSowmini Varadhan 	 *	ldb [80]
454d7b9dc1SSowmini Varadhan 	 *	jeq #97, pass		; DATA_CHAR
464d7b9dc1SSowmini Varadhan 	 *	jne #98, drop		; DATA_CHAR_1
474d7b9dc1SSowmini Varadhan 	 *	pass:
484d7b9dc1SSowmini Varadhan 	 *	  ret #-1
494d7b9dc1SSowmini Varadhan 	 *	drop:
504d7b9dc1SSowmini Varadhan 	 *	  ret #0
514d7b9dc1SSowmini Varadhan 	 */
5223a95442SDaniel Borkmann 	struct sock_filter bpf_filter[] = {
534d7b9dc1SSowmini Varadhan 		{ 0x28,  0,  0, 0x0000000c },
544d7b9dc1SSowmini Varadhan 		{ 0x15,  0,  8, 0x00000800 },
554d7b9dc1SSowmini Varadhan 		{ 0x30,  0,  0, 0x00000017 },
564d7b9dc1SSowmini Varadhan 		{ 0x15,  0,  6, 0x00000011 },
574d7b9dc1SSowmini Varadhan 		{ 0x80,  0,  0, 0000000000 },
584d7b9dc1SSowmini Varadhan 		{ 0x35,  0,  4, 0x00000064 },
594d7b9dc1SSowmini Varadhan 		{ 0x30,  0,  0, 0x00000050 },
604d7b9dc1SSowmini Varadhan 		{ 0x15,  1,  0, 0x00000061 },
614d7b9dc1SSowmini Varadhan 		{ 0x15,  0,  1, 0x00000062 },
624d7b9dc1SSowmini Varadhan 		{ 0x06,  0,  0, 0xffffffff },
634d7b9dc1SSowmini Varadhan 		{ 0x06,  0,  0, 0000000000 },
6423a95442SDaniel Borkmann 	};
6523a95442SDaniel Borkmann 	struct sock_fprog bpf_prog;
6623a95442SDaniel Borkmann 
6723a95442SDaniel Borkmann 	bpf_prog.filter = bpf_filter;
68*3645c71bSMahmoud Maatuq 	bpf_prog.len = ARRAY_SIZE(bpf_filter);
69c1f8d0f9SMike Maloney 
70c1f8d0f9SMike Maloney 	if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf_prog,
7123a95442SDaniel Borkmann 		       sizeof(bpf_prog))) {
7223a95442SDaniel Borkmann 		perror("setsockopt SO_ATTACH_FILTER");
7323a95442SDaniel Borkmann 		exit(1);
7423a95442SDaniel Borkmann 	}
7523a95442SDaniel Borkmann }
7623a95442SDaniel Borkmann 
pair_udp_open(int fds[],uint16_t port)7723a95442SDaniel Borkmann static __maybe_unused void pair_udp_open(int fds[], uint16_t port)
7823a95442SDaniel Borkmann {
7923a95442SDaniel Borkmann 	struct sockaddr_in saddr, daddr;
8023a95442SDaniel Borkmann 
8123a95442SDaniel Borkmann 	fds[0] = socket(PF_INET, SOCK_DGRAM, 0);
8223a95442SDaniel Borkmann 	fds[1] = socket(PF_INET, SOCK_DGRAM, 0);
8323a95442SDaniel Borkmann 	if (fds[0] == -1 || fds[1] == -1) {
8423a95442SDaniel Borkmann 		fprintf(stderr, "ERROR: socket dgram\n");
8523a95442SDaniel Borkmann 		exit(1);
8623a95442SDaniel Borkmann 	}
8723a95442SDaniel Borkmann 
8823a95442SDaniel Borkmann 	memset(&saddr, 0, sizeof(saddr));
8923a95442SDaniel Borkmann 	saddr.sin_family = AF_INET;
9023a95442SDaniel Borkmann 	saddr.sin_port = htons(port);
9123a95442SDaniel Borkmann 	saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
9223a95442SDaniel Borkmann 
9323a95442SDaniel Borkmann 	memset(&daddr, 0, sizeof(daddr));
9423a95442SDaniel Borkmann 	daddr.sin_family = AF_INET;
9523a95442SDaniel Borkmann 	daddr.sin_port = htons(port + 1);
9623a95442SDaniel Borkmann 	daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
9723a95442SDaniel Borkmann 
9823a95442SDaniel Borkmann 	/* must bind both to get consistent hash result */
9923a95442SDaniel Borkmann 	if (bind(fds[1], (void *) &daddr, sizeof(daddr))) {
10023a95442SDaniel Borkmann 		perror("bind");
10123a95442SDaniel Borkmann 		exit(1);
10223a95442SDaniel Borkmann 	}
10323a95442SDaniel Borkmann 	if (bind(fds[0], (void *) &saddr, sizeof(saddr))) {
10423a95442SDaniel Borkmann 		perror("bind");
10523a95442SDaniel Borkmann 		exit(1);
10623a95442SDaniel Borkmann 	}
10723a95442SDaniel Borkmann 	if (connect(fds[0], (void *) &daddr, sizeof(daddr))) {
10823a95442SDaniel Borkmann 		perror("connect");
10923a95442SDaniel Borkmann 		exit(1);
11023a95442SDaniel Borkmann 	}
11123a95442SDaniel Borkmann }
11223a95442SDaniel Borkmann 
pair_udp_send_char(int fds[],int num,char payload)11395e22792SWillem de Bruijn static __maybe_unused void pair_udp_send_char(int fds[], int num, char payload)
11423a95442SDaniel Borkmann {
11523a95442SDaniel Borkmann 	char buf[DATA_LEN], rbuf[DATA_LEN];
11623a95442SDaniel Borkmann 
11795e22792SWillem de Bruijn 	memset(buf, payload, sizeof(buf));
11823a95442SDaniel Borkmann 	while (num--) {
11923a95442SDaniel Borkmann 		/* Should really handle EINTR and EAGAIN */
12023a95442SDaniel Borkmann 		if (write(fds[0], buf, sizeof(buf)) != sizeof(buf)) {
12123a95442SDaniel Borkmann 			fprintf(stderr, "ERROR: send failed left=%d\n", num);
12223a95442SDaniel Borkmann 			exit(1);
12323a95442SDaniel Borkmann 		}
12423a95442SDaniel Borkmann 		if (read(fds[1], rbuf, sizeof(rbuf)) != sizeof(rbuf)) {
12523a95442SDaniel Borkmann 			fprintf(stderr, "ERROR: recv failed left=%d\n", num);
12623a95442SDaniel Borkmann 			exit(1);
12723a95442SDaniel Borkmann 		}
12823a95442SDaniel Borkmann 		if (memcmp(buf, rbuf, sizeof(buf))) {
12923a95442SDaniel Borkmann 			fprintf(stderr, "ERROR: data failed left=%d\n", num);
13023a95442SDaniel Borkmann 			exit(1);
13123a95442SDaniel Borkmann 		}
13223a95442SDaniel Borkmann 	}
13323a95442SDaniel Borkmann }
13423a95442SDaniel Borkmann 
pair_udp_send(int fds[],int num)13595e22792SWillem de Bruijn static __maybe_unused void pair_udp_send(int fds[], int num)
13695e22792SWillem de Bruijn {
13795e22792SWillem de Bruijn 	return pair_udp_send_char(fds, num, DATA_CHAR);
13895e22792SWillem de Bruijn }
13995e22792SWillem de Bruijn 
pair_udp_close(int fds[])14023a95442SDaniel Borkmann static __maybe_unused void pair_udp_close(int fds[])
14123a95442SDaniel Borkmann {
14223a95442SDaniel Borkmann 	close(fds[0]);
14323a95442SDaniel Borkmann 	close(fds[1]);
14423a95442SDaniel Borkmann }
14523a95442SDaniel Borkmann 
14623a95442SDaniel Borkmann #endif /* PSOCK_LIB_H */
147