psock_lib.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2013 Google Inc.
  3. * Author: Willem de Bruijn <willemb@google.com>
  4. * Daniel Borkmann <dborkman@redhat.com>
  5. *
  6. * License (GPLv2):
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #ifndef PSOCK_LIB_H
  22. #define PSOCK_LIB_H
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <string.h>
  26. #include <arpa/inet.h>
  27. #include <unistd.h>
  28. #define DATA_LEN 100
  29. #define DATA_CHAR 'a'
  30. #define DATA_CHAR_1 'b'
  31. #define PORT_BASE 8000
  32. #ifndef __maybe_unused
  33. # define __maybe_unused __attribute__ ((__unused__))
  34. #endif
  35. static __maybe_unused void sock_setfilter(int fd, int lvl, int optnum)
  36. {
  37. /* the filter below checks for all of the following conditions that
  38. * are based on the contents of create_payload()
  39. * ether type 0x800 and
  40. * ip proto udp and
  41. * skb->len == DATA_LEN and
  42. * udp[38] == 'a' or udp[38] == 'b'
  43. * It can be generated from the following bpf_asm input:
  44. * ldh [12]
  45. * jne #0x800, drop ; ETH_P_IP
  46. * ldb [23]
  47. * jneq #17, drop ; IPPROTO_UDP
  48. * ld len ; ld skb->len
  49. * jlt #100, drop ; DATA_LEN
  50. * ldb [80]
  51. * jeq #97, pass ; DATA_CHAR
  52. * jne #98, drop ; DATA_CHAR_1
  53. * pass:
  54. * ret #-1
  55. * drop:
  56. * ret #0
  57. */
  58. struct sock_filter bpf_filter[] = {
  59. { 0x28, 0, 0, 0x0000000c },
  60. { 0x15, 0, 8, 0x00000800 },
  61. { 0x30, 0, 0, 0x00000017 },
  62. { 0x15, 0, 6, 0x00000011 },
  63. { 0x80, 0, 0, 0000000000 },
  64. { 0x35, 0, 4, 0x00000064 },
  65. { 0x30, 0, 0, 0x00000050 },
  66. { 0x15, 1, 0, 0x00000061 },
  67. { 0x15, 0, 1, 0x00000062 },
  68. { 0x06, 0, 0, 0xffffffff },
  69. { 0x06, 0, 0, 0000000000 },
  70. };
  71. struct sock_fprog bpf_prog;
  72. if (lvl == SOL_PACKET && optnum == PACKET_FANOUT_DATA)
  73. bpf_filter[5].code = 0x16; /* RET A */
  74. bpf_prog.filter = bpf_filter;
  75. bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
  76. if (setsockopt(fd, lvl, optnum, &bpf_prog,
  77. sizeof(bpf_prog))) {
  78. perror("setsockopt SO_ATTACH_FILTER");
  79. exit(1);
  80. }
  81. }
  82. static __maybe_unused void pair_udp_setfilter(int fd)
  83. {
  84. sock_setfilter(fd, SOL_SOCKET, SO_ATTACH_FILTER);
  85. }
  86. static __maybe_unused void pair_udp_open(int fds[], uint16_t port)
  87. {
  88. struct sockaddr_in saddr, daddr;
  89. fds[0] = socket(PF_INET, SOCK_DGRAM, 0);
  90. fds[1] = socket(PF_INET, SOCK_DGRAM, 0);
  91. if (fds[0] == -1 || fds[1] == -1) {
  92. fprintf(stderr, "ERROR: socket dgram\n");
  93. exit(1);
  94. }
  95. memset(&saddr, 0, sizeof(saddr));
  96. saddr.sin_family = AF_INET;
  97. saddr.sin_port = htons(port);
  98. saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  99. memset(&daddr, 0, sizeof(daddr));
  100. daddr.sin_family = AF_INET;
  101. daddr.sin_port = htons(port + 1);
  102. daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  103. /* must bind both to get consistent hash result */
  104. if (bind(fds[1], (void *) &daddr, sizeof(daddr))) {
  105. perror("bind");
  106. exit(1);
  107. }
  108. if (bind(fds[0], (void *) &saddr, sizeof(saddr))) {
  109. perror("bind");
  110. exit(1);
  111. }
  112. if (connect(fds[0], (void *) &daddr, sizeof(daddr))) {
  113. perror("connect");
  114. exit(1);
  115. }
  116. }
  117. static __maybe_unused void pair_udp_send_char(int fds[], int num, char payload)
  118. {
  119. char buf[DATA_LEN], rbuf[DATA_LEN];
  120. memset(buf, payload, sizeof(buf));
  121. while (num--) {
  122. /* Should really handle EINTR and EAGAIN */
  123. if (write(fds[0], buf, sizeof(buf)) != sizeof(buf)) {
  124. fprintf(stderr, "ERROR: send failed left=%d\n", num);
  125. exit(1);
  126. }
  127. if (read(fds[1], rbuf, sizeof(rbuf)) != sizeof(rbuf)) {
  128. fprintf(stderr, "ERROR: recv failed left=%d\n", num);
  129. exit(1);
  130. }
  131. if (memcmp(buf, rbuf, sizeof(buf))) {
  132. fprintf(stderr, "ERROR: data failed left=%d\n", num);
  133. exit(1);
  134. }
  135. }
  136. }
  137. static __maybe_unused void pair_udp_send(int fds[], int num)
  138. {
  139. return pair_udp_send_char(fds, num, DATA_CHAR);
  140. }
  141. static __maybe_unused void pair_udp_close(int fds[])
  142. {
  143. close(fds[0]);
  144. close(fds[1]);
  145. }
  146. #endif /* PSOCK_LIB_H */