psock_lib.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright 2013 Google Inc.
  4. * Author: Willem de Bruijn <[email protected]>
  5. * Daniel Borkmann <[email protected]>
  6. */
  7. #ifndef PSOCK_LIB_H
  8. #define PSOCK_LIB_H
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <string.h>
  12. #include <arpa/inet.h>
  13. #include <unistd.h>
  14. #define DATA_LEN 100
  15. #define DATA_CHAR 'a'
  16. #define DATA_CHAR_1 'b'
  17. #define PORT_BASE 8000
  18. #ifndef __maybe_unused
  19. # define __maybe_unused __attribute__ ((__unused__))
  20. #endif
  21. static __maybe_unused void pair_udp_setfilter(int fd)
  22. {
  23. /* the filter below checks for all of the following conditions that
  24. * are based on the contents of create_payload()
  25. * ether type 0x800 and
  26. * ip proto udp and
  27. * skb->len == DATA_LEN and
  28. * udp[38] == 'a' or udp[38] == 'b'
  29. * It can be generated from the following bpf_asm input:
  30. * ldh [12]
  31. * jne #0x800, drop ; ETH_P_IP
  32. * ldb [23]
  33. * jneq #17, drop ; IPPROTO_UDP
  34. * ld len ; ld skb->len
  35. * jlt #100, drop ; DATA_LEN
  36. * ldb [80]
  37. * jeq #97, pass ; DATA_CHAR
  38. * jne #98, drop ; DATA_CHAR_1
  39. * pass:
  40. * ret #-1
  41. * drop:
  42. * ret #0
  43. */
  44. struct sock_filter bpf_filter[] = {
  45. { 0x28, 0, 0, 0x0000000c },
  46. { 0x15, 0, 8, 0x00000800 },
  47. { 0x30, 0, 0, 0x00000017 },
  48. { 0x15, 0, 6, 0x00000011 },
  49. { 0x80, 0, 0, 0000000000 },
  50. { 0x35, 0, 4, 0x00000064 },
  51. { 0x30, 0, 0, 0x00000050 },
  52. { 0x15, 1, 0, 0x00000061 },
  53. { 0x15, 0, 1, 0x00000062 },
  54. { 0x06, 0, 0, 0xffffffff },
  55. { 0x06, 0, 0, 0000000000 },
  56. };
  57. struct sock_fprog bpf_prog;
  58. bpf_prog.filter = bpf_filter;
  59. bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
  60. if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf_prog,
  61. sizeof(bpf_prog))) {
  62. perror("setsockopt SO_ATTACH_FILTER");
  63. exit(1);
  64. }
  65. }
  66. static __maybe_unused void pair_udp_open(int fds[], uint16_t port)
  67. {
  68. struct sockaddr_in saddr, daddr;
  69. fds[0] = socket(PF_INET, SOCK_DGRAM, 0);
  70. fds[1] = socket(PF_INET, SOCK_DGRAM, 0);
  71. if (fds[0] == -1 || fds[1] == -1) {
  72. fprintf(stderr, "ERROR: socket dgram\n");
  73. exit(1);
  74. }
  75. memset(&saddr, 0, sizeof(saddr));
  76. saddr.sin_family = AF_INET;
  77. saddr.sin_port = htons(port);
  78. saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  79. memset(&daddr, 0, sizeof(daddr));
  80. daddr.sin_family = AF_INET;
  81. daddr.sin_port = htons(port + 1);
  82. daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  83. /* must bind both to get consistent hash result */
  84. if (bind(fds[1], (void *) &daddr, sizeof(daddr))) {
  85. perror("bind");
  86. exit(1);
  87. }
  88. if (bind(fds[0], (void *) &saddr, sizeof(saddr))) {
  89. perror("bind");
  90. exit(1);
  91. }
  92. if (connect(fds[0], (void *) &daddr, sizeof(daddr))) {
  93. perror("connect");
  94. exit(1);
  95. }
  96. }
  97. static __maybe_unused void pair_udp_send_char(int fds[], int num, char payload)
  98. {
  99. char buf[DATA_LEN], rbuf[DATA_LEN];
  100. memset(buf, payload, sizeof(buf));
  101. while (num--) {
  102. /* Should really handle EINTR and EAGAIN */
  103. if (write(fds[0], buf, sizeof(buf)) != sizeof(buf)) {
  104. fprintf(stderr, "ERROR: send failed left=%d\n", num);
  105. exit(1);
  106. }
  107. if (read(fds[1], rbuf, sizeof(rbuf)) != sizeof(rbuf)) {
  108. fprintf(stderr, "ERROR: recv failed left=%d\n", num);
  109. exit(1);
  110. }
  111. if (memcmp(buf, rbuf, sizeof(buf))) {
  112. fprintf(stderr, "ERROR: data failed left=%d\n", num);
  113. exit(1);
  114. }
  115. }
  116. }
  117. static __maybe_unused void pair_udp_send(int fds[], int num)
  118. {
  119. return pair_udp_send_char(fds, num, DATA_CHAR);
  120. }
  121. static __maybe_unused void pair_udp_close(int fds[])
  122. {
  123. close(fds[0]);
  124. close(fds[1]);
  125. }
  126. #endif /* PSOCK_LIB_H */