test_tcpbpf_kern.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stddef.h>
  3. #include <string.h>
  4. #include <linux/bpf.h>
  5. #include <linux/if_ether.h>
  6. #include <linux/if_packet.h>
  7. #include <linux/ip.h>
  8. #include <linux/ipv6.h>
  9. #include <linux/types.h>
  10. #include <linux/socket.h>
  11. #include <linux/tcp.h>
  12. #include <netinet/in.h>
  13. #include "bpf_helpers.h"
  14. #include "bpf_endian.h"
  15. #include "test_tcpbpf.h"
  16. struct bpf_map_def SEC("maps") global_map = {
  17. .type = BPF_MAP_TYPE_ARRAY,
  18. .key_size = sizeof(__u32),
  19. .value_size = sizeof(struct tcpbpf_globals),
  20. .max_entries = 4,
  21. };
  22. struct bpf_map_def SEC("maps") sockopt_results = {
  23. .type = BPF_MAP_TYPE_ARRAY,
  24. .key_size = sizeof(__u32),
  25. .value_size = sizeof(int),
  26. .max_entries = 2,
  27. };
  28. static inline void update_event_map(int event)
  29. {
  30. __u32 key = 0;
  31. struct tcpbpf_globals g, *gp;
  32. gp = bpf_map_lookup_elem(&global_map, &key);
  33. if (gp == NULL) {
  34. struct tcpbpf_globals g = {0};
  35. g.event_map |= (1 << event);
  36. bpf_map_update_elem(&global_map, &key, &g,
  37. BPF_ANY);
  38. } else {
  39. g = *gp;
  40. g.event_map |= (1 << event);
  41. bpf_map_update_elem(&global_map, &key, &g,
  42. BPF_ANY);
  43. }
  44. }
  45. int _version SEC("version") = 1;
  46. SEC("sockops")
  47. int bpf_testcb(struct bpf_sock_ops *skops)
  48. {
  49. char header[sizeof(struct ipv6hdr) + sizeof(struct tcphdr)];
  50. struct tcphdr *thdr;
  51. int good_call_rv = 0;
  52. int bad_call_rv = 0;
  53. int save_syn = 1;
  54. int rv = -1;
  55. int v = 0;
  56. int op;
  57. op = (int) skops->op;
  58. update_event_map(op);
  59. switch (op) {
  60. case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
  61. /* Test failure to set largest cb flag (assumes not defined) */
  62. bad_call_rv = bpf_sock_ops_cb_flags_set(skops, 0x80);
  63. /* Set callback */
  64. good_call_rv = bpf_sock_ops_cb_flags_set(skops,
  65. BPF_SOCK_OPS_STATE_CB_FLAG);
  66. /* Update results */
  67. {
  68. __u32 key = 0;
  69. struct tcpbpf_globals g, *gp;
  70. gp = bpf_map_lookup_elem(&global_map, &key);
  71. if (!gp)
  72. break;
  73. g = *gp;
  74. g.bad_cb_test_rv = bad_call_rv;
  75. g.good_cb_test_rv = good_call_rv;
  76. bpf_map_update_elem(&global_map, &key, &g,
  77. BPF_ANY);
  78. }
  79. break;
  80. case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
  81. skops->sk_txhash = 0x12345f;
  82. v = 0xff;
  83. rv = bpf_setsockopt(skops, SOL_IPV6, IPV6_TCLASS, &v,
  84. sizeof(v));
  85. if (skops->family == AF_INET6) {
  86. v = bpf_getsockopt(skops, IPPROTO_TCP, TCP_SAVED_SYN,
  87. header, (sizeof(struct ipv6hdr) +
  88. sizeof(struct tcphdr)));
  89. if (!v) {
  90. int offset = sizeof(struct ipv6hdr);
  91. thdr = (struct tcphdr *)(header + offset);
  92. v = thdr->syn;
  93. __u32 key = 1;
  94. bpf_map_update_elem(&sockopt_results, &key, &v,
  95. BPF_ANY);
  96. }
  97. }
  98. break;
  99. case BPF_SOCK_OPS_RTO_CB:
  100. break;
  101. case BPF_SOCK_OPS_RETRANS_CB:
  102. break;
  103. case BPF_SOCK_OPS_STATE_CB:
  104. if (skops->args[1] == BPF_TCP_CLOSE) {
  105. __u32 key = 0;
  106. struct tcpbpf_globals g, *gp;
  107. gp = bpf_map_lookup_elem(&global_map, &key);
  108. if (!gp)
  109. break;
  110. g = *gp;
  111. if (skops->args[0] == BPF_TCP_LISTEN) {
  112. g.num_listen++;
  113. } else {
  114. g.total_retrans = skops->total_retrans;
  115. g.data_segs_in = skops->data_segs_in;
  116. g.data_segs_out = skops->data_segs_out;
  117. g.bytes_received = skops->bytes_received;
  118. g.bytes_acked = skops->bytes_acked;
  119. }
  120. bpf_map_update_elem(&global_map, &key, &g,
  121. BPF_ANY);
  122. }
  123. break;
  124. case BPF_SOCK_OPS_TCP_LISTEN_CB:
  125. bpf_sock_ops_cb_flags_set(skops, BPF_SOCK_OPS_STATE_CB_FLAG);
  126. v = bpf_setsockopt(skops, IPPROTO_TCP, TCP_SAVE_SYN,
  127. &save_syn, sizeof(save_syn));
  128. /* Update global map w/ result of setsock opt */
  129. __u32 key = 0;
  130. bpf_map_update_elem(&sockopt_results, &key, &v, BPF_ANY);
  131. break;
  132. default:
  133. rv = -1;
  134. }
  135. skops->reply = rv;
  136. return 1;
  137. }
  138. char _license[] SEC("license") = "GPL";