Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright 2013 Google Inc.
0004  * Author: Willem de Bruijn <willemb@google.com>
0005  *         Daniel Borkmann <dborkman@redhat.com>
0006  */
0007 
0008 #ifndef PSOCK_LIB_H
0009 #define PSOCK_LIB_H
0010 
0011 #include <sys/types.h>
0012 #include <sys/socket.h>
0013 #include <string.h>
0014 #include <arpa/inet.h>
0015 #include <unistd.h>
0016 
0017 #define DATA_LEN            100
0018 #define DATA_CHAR           'a'
0019 #define DATA_CHAR_1         'b'
0020 
0021 #define PORT_BASE           8000
0022 
0023 #ifndef __maybe_unused
0024 # define __maybe_unused     __attribute__ ((__unused__))
0025 #endif
0026 
0027 static __maybe_unused void pair_udp_setfilter(int fd)
0028 {
0029     /* the filter below checks for all of the following conditions that
0030      * are based on the contents of create_payload()
0031      *  ether type 0x800 and
0032      *  ip proto udp     and
0033      *  skb->len == DATA_LEN and
0034      *  udp[38] == 'a' or udp[38] == 'b'
0035      * It can be generated from the following bpf_asm input:
0036      *  ldh [12]
0037      *  jne #0x800, drop    ; ETH_P_IP
0038      *  ldb [23]
0039      *  jneq #17, drop      ; IPPROTO_UDP
0040      *  ld len          ; ld skb->len
0041      *  jlt #100, drop      ; DATA_LEN
0042      *  ldb [80]
0043      *  jeq #97, pass       ; DATA_CHAR
0044      *  jne #98, drop       ; DATA_CHAR_1
0045      *  pass:
0046      *    ret #-1
0047      *  drop:
0048      *    ret #0
0049      */
0050     struct sock_filter bpf_filter[] = {
0051         { 0x28,  0,  0, 0x0000000c },
0052         { 0x15,  0,  8, 0x00000800 },
0053         { 0x30,  0,  0, 0x00000017 },
0054         { 0x15,  0,  6, 0x00000011 },
0055         { 0x80,  0,  0, 0000000000 },
0056         { 0x35,  0,  4, 0x00000064 },
0057         { 0x30,  0,  0, 0x00000050 },
0058         { 0x15,  1,  0, 0x00000061 },
0059         { 0x15,  0,  1, 0x00000062 },
0060         { 0x06,  0,  0, 0xffffffff },
0061         { 0x06,  0,  0, 0000000000 },
0062     };
0063     struct sock_fprog bpf_prog;
0064 
0065     bpf_prog.filter = bpf_filter;
0066     bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
0067 
0068     if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf_prog,
0069                sizeof(bpf_prog))) {
0070         perror("setsockopt SO_ATTACH_FILTER");
0071         exit(1);
0072     }
0073 }
0074 
0075 static __maybe_unused void pair_udp_open(int fds[], uint16_t port)
0076 {
0077     struct sockaddr_in saddr, daddr;
0078 
0079     fds[0] = socket(PF_INET, SOCK_DGRAM, 0);
0080     fds[1] = socket(PF_INET, SOCK_DGRAM, 0);
0081     if (fds[0] == -1 || fds[1] == -1) {
0082         fprintf(stderr, "ERROR: socket dgram\n");
0083         exit(1);
0084     }
0085 
0086     memset(&saddr, 0, sizeof(saddr));
0087     saddr.sin_family = AF_INET;
0088     saddr.sin_port = htons(port);
0089     saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
0090 
0091     memset(&daddr, 0, sizeof(daddr));
0092     daddr.sin_family = AF_INET;
0093     daddr.sin_port = htons(port + 1);
0094     daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
0095 
0096     /* must bind both to get consistent hash result */
0097     if (bind(fds[1], (void *) &daddr, sizeof(daddr))) {
0098         perror("bind");
0099         exit(1);
0100     }
0101     if (bind(fds[0], (void *) &saddr, sizeof(saddr))) {
0102         perror("bind");
0103         exit(1);
0104     }
0105     if (connect(fds[0], (void *) &daddr, sizeof(daddr))) {
0106         perror("connect");
0107         exit(1);
0108     }
0109 }
0110 
0111 static __maybe_unused void pair_udp_send_char(int fds[], int num, char payload)
0112 {
0113     char buf[DATA_LEN], rbuf[DATA_LEN];
0114 
0115     memset(buf, payload, sizeof(buf));
0116     while (num--) {
0117         /* Should really handle EINTR and EAGAIN */
0118         if (write(fds[0], buf, sizeof(buf)) != sizeof(buf)) {
0119             fprintf(stderr, "ERROR: send failed left=%d\n", num);
0120             exit(1);
0121         }
0122         if (read(fds[1], rbuf, sizeof(rbuf)) != sizeof(rbuf)) {
0123             fprintf(stderr, "ERROR: recv failed left=%d\n", num);
0124             exit(1);
0125         }
0126         if (memcmp(buf, rbuf, sizeof(buf))) {
0127             fprintf(stderr, "ERROR: data failed left=%d\n", num);
0128             exit(1);
0129         }
0130     }
0131 }
0132 
0133 static __maybe_unused void pair_udp_send(int fds[], int num)
0134 {
0135     return pair_udp_send_char(fds, num, DATA_CHAR);
0136 }
0137 
0138 static __maybe_unused void pair_udp_close(int fds[])
0139 {
0140     close(fds[0]);
0141     close(fds[1]);
0142 }
0143 
0144 #endif /* PSOCK_LIB_H */