~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/net/psock_lib.h

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php