1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright 2013 Google Inc.
4 * Author: Willem de Bruijn <willemb@google.com>
5 * Daniel Borkmann <dborkman@redhat.com>
11 #include <sys/types.h>
12 #include <sys/socket.h>
14 #include <arpa/inet.h>
17 #include "kselftest.h"
21 #define DATA_CHAR_1 'b'
23 #define PORT_BASE 8000
25 #ifndef __maybe_unused
26 # define __maybe_unused __attribute__ ((__unused__))
29 static __maybe_unused
void pair_udp_setfilter(int fd
)
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
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:
39 * jne #0x800, drop ; ETH_P_IP
41 * jneq #17, drop ; IPPROTO_UDP
42 * ld len ; ld skb->len
43 * jlt #100, drop ; DATA_LEN
45 * jeq #97, pass ; DATA_CHAR
46 * jne #98, drop ; DATA_CHAR_1
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 },
65 struct sock_fprog bpf_prog
;
67 bpf_prog
.filter
= bpf_filter
;
68 bpf_prog
.len
= ARRAY_SIZE(bpf_filter
);
70 if (setsockopt(fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bpf_prog
,
72 perror("setsockopt SO_ATTACH_FILTER");
77 static __maybe_unused
void pair_udp_open(int fds
[], uint16_t port
)
79 struct sockaddr_in saddr
, daddr
;
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");
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
);
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
);
98 /* must bind both to get consistent hash result */
99 if (bind(fds
[1], (void *) &daddr
, sizeof(daddr
))) {
103 if (bind(fds
[0], (void *) &saddr
, sizeof(saddr
))) {
107 if (connect(fds
[0], (void *) &daddr
, sizeof(daddr
))) {
113 static __maybe_unused
void pair_udp_send_char(int fds
[], int num
, char payload
)
115 char buf
[DATA_LEN
], rbuf
[DATA_LEN
];
117 memset(buf
, payload
, sizeof(buf
));
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
);
124 if (read(fds
[1], rbuf
, sizeof(rbuf
)) != sizeof(rbuf
)) {
125 fprintf(stderr
, "ERROR: recv failed left=%d\n", num
);
128 if (memcmp(buf
, rbuf
, sizeof(buf
))) {
129 fprintf(stderr
, "ERROR: data failed left=%d\n", num
);
135 static __maybe_unused
void pair_udp_send(int fds
[], int num
)
137 return pair_udp_send_char(fds
, num
, DATA_CHAR
);
140 static __maybe_unused
void pair_udp_close(int fds
[])
146 #endif /* PSOCK_LIB_H */