psock_lib.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 pair_udp_setfilter(int fd)
  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. bpf_prog.filter = bpf_filter;
  73. bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
  74. if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf_prog,
  75. sizeof(bpf_prog))) {
  76. perror("setsockopt SO_ATTACH_FILTER");
  77. exit(1);
  78. }
  79. }
  80. static __maybe_unused void pair_udp_open(int fds[], uint16_t port)
  81. {
  82. struct sockaddr_in saddr, daddr;
  83. fds[0] = socket(PF_INET, SOCK_DGRAM, 0);
  84. fds[1] = socket(PF_INET, SOCK_DGRAM, 0);
  85. if (fds[0] == -1 || fds[1] == -1) {
  86. fprintf(stderr, "ERROR: socket dgram\n");
  87. exit(1);
  88. }
  89. memset(&saddr, 0, sizeof(saddr));
  90. saddr.sin_family = AF_INET;
  91. saddr.sin_port = htons(port);
  92. saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  93. memset(&daddr, 0, sizeof(daddr));
  94. daddr.sin_family = AF_INET;
  95. daddr.sin_port = htons(port + 1);
  96. daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  97. /* must bind both to get consistent hash result */
  98. if (bind(fds[1], (void *) &daddr, sizeof(daddr))) {
  99. perror("bind");
  100. exit(1);
  101. }
  102. if (bind(fds[0], (void *) &saddr, sizeof(saddr))) {
  103. perror("bind");
  104. exit(1);
  105. }
  106. if (connect(fds[0], (void *) &daddr, sizeof(daddr))) {
  107. perror("connect");
  108. exit(1);
  109. }
  110. }
  111. static __maybe_unused void pair_udp_send_char(int fds[], int num, char payload)
  112. {
  113. char buf[DATA_LEN], rbuf[DATA_LEN];
  114. memset(buf, payload, sizeof(buf));
  115. while (num--) {
  116. /* Should really handle EINTR and EAGAIN */
  117. if (write(fds[0], buf, sizeof(buf)) != sizeof(buf)) {
  118. fprintf(stderr, "ERROR: send failed left=%d\n", num);
  119. exit(1);
  120. }
  121. if (read(fds[1], rbuf, sizeof(rbuf)) != sizeof(rbuf)) {
  122. fprintf(stderr, "ERROR: recv failed left=%d\n", num);
  123. exit(1);
  124. }
  125. if (memcmp(buf, rbuf, sizeof(buf))) {
  126. fprintf(stderr, "ERROR: data failed left=%d\n", num);
  127. exit(1);
  128. }
  129. }
  130. }
  131. static __maybe_unused void pair_udp_send(int fds[], int num)
  132. {
  133. return pair_udp_send_char(fds, num, DATA_CHAR);
  134. }
  135. static __maybe_unused void pair_udp_close(int fds[])
  136. {
  137. close(fds[0]);
  138. close(fds[1]);
  139. }
  140. #endif /* PSOCK_LIB_H */