psock_lib.h 3.4 KB

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