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