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>
19 #define DATA_CHAR_1 'b'
21 #define PORT_BASE 8000
23 #ifndef __maybe_unused
24 # define __maybe_unused __attribute__ ((__unused__))
27 static __maybe_unused
void pair_udp_setfilter(int fd
)
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
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:
37 * jne #0x800, drop ; ETH_P_IP
39 * jneq #17, drop ; IPPROTO_UDP
40 * ld len ; ld skb->len
41 * jlt #100, drop ; DATA_LEN
43 * jeq #97, pass ; DATA_CHAR
44 * jne #98, drop ; DATA_CHAR_1
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 },
63 struct sock_fprog bpf_prog
;
65 bpf_prog
.filter
= bpf_filter
;
66 bpf_prog
.len
= sizeof(bpf_filter
) / sizeof(struct sock_filter
);
68 if (setsockopt(fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bpf_prog
,
70 perror("setsockopt SO_ATTACH_FILTER");
75 static __maybe_unused
void pair_udp_open(int fds
[], uint16_t port
)
77 struct sockaddr_in saddr
, daddr
;
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");
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
);
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
);
96 /* must bind both to get consistent hash result */
97 if (bind(fds
[1], (void *) &daddr
, sizeof(daddr
))) {
101 if (bind(fds
[0], (void *) &saddr
, sizeof(saddr
))) {
105 if (connect(fds
[0], (void *) &daddr
, sizeof(daddr
))) {
111 static __maybe_unused
void pair_udp_send_char(int fds
[], int num
, char payload
)
113 char buf
[DATA_LEN
], rbuf
[DATA_LEN
];
115 memset(buf
, payload
, sizeof(buf
));
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
);
122 if (read(fds
[1], rbuf
, sizeof(rbuf
)) != sizeof(rbuf
)) {
123 fprintf(stderr
, "ERROR: recv failed left=%d\n", num
);
126 if (memcmp(buf
, rbuf
, sizeof(buf
))) {
127 fprintf(stderr
, "ERROR: data failed left=%d\n", num
);
133 static __maybe_unused
void pair_udp_send(int fds
[], int num
)
135 return pair_udp_send_char(fds
, num
, DATA_CHAR
);
138 static __maybe_unused
void pair_udp_close(int fds
[])
144 #endif /* PSOCK_LIB_H */