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