cookie_uid_helper_example.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* This test is a demo of using get_socket_uid and get_socket_cookie
  2. * helper function to do per socket based network traffic monitoring.
  3. * It requires iptables version higher then 1.6.1. to load pinned eBPF
  4. * program into the xt_bpf match.
  5. *
  6. * TEST:
  7. * ./run_cookie_uid_helper_example.sh
  8. * Then generate some traffic in variate ways. ping 0 -c 10 would work
  9. * but the cookie and uid in this case could both be 0. A sample output
  10. * with some traffic generated by web browser is shown below:
  11. *
  12. * cookie: 877, uid: 0x3e8, Pakcet Count: 20, Bytes Count: 11058
  13. * cookie: 132, uid: 0x0, Pakcet Count: 2, Bytes Count: 286
  14. * cookie: 812, uid: 0x3e8, Pakcet Count: 3, Bytes Count: 1726
  15. * cookie: 802, uid: 0x3e8, Pakcet Count: 2, Bytes Count: 104
  16. * cookie: 877, uid: 0x3e8, Pakcet Count: 20, Bytes Count: 11058
  17. * cookie: 831, uid: 0x3e8, Pakcet Count: 2, Bytes Count: 104
  18. * cookie: 0, uid: 0x0, Pakcet Count: 6, Bytes Count: 712
  19. * cookie: 880, uid: 0xfffe, Pakcet Count: 1, Bytes Count: 70
  20. *
  21. * Clean up: if using shell script, the script file will delete the iptables
  22. * rule and unmount the bpf program when exit. Else the iptables rule need
  23. * to be deleted by hand, see run_cookie_uid_helper_example.sh for detail.
  24. */
  25. #define _GNU_SOURCE
  26. #define offsetof(type, member) __builtin_offsetof(type, member)
  27. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
  28. #include <arpa/inet.h>
  29. #include <errno.h>
  30. #include <error.h>
  31. #include <limits.h>
  32. #include <linux/bpf.h>
  33. #include <linux/if_ether.h>
  34. #include <stdbool.h>
  35. #include <stdint.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <sys/socket.h>
  40. #include <sys/stat.h>
  41. #include <sys/types.h>
  42. #include <unistd.h>
  43. #include <bpf/bpf.h>
  44. #include "libbpf.h"
  45. struct stats {
  46. uint32_t uid;
  47. uint64_t packets;
  48. uint64_t bytes;
  49. };
  50. static int map_fd, prog_fd;
  51. static void maps_create(void)
  52. {
  53. map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(uint32_t),
  54. sizeof(struct stats), 100, 0);
  55. if (map_fd < 0)
  56. error(1, errno, "map create failed!\n");
  57. }
  58. static void prog_load(void)
  59. {
  60. static char log_buf[1 << 16];
  61. struct bpf_insn prog[] = {
  62. /*
  63. * Save sk_buff for future usage. value stored in R6 to R10 will
  64. * not be reset after a bpf helper function call.
  65. */
  66. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  67. /*
  68. * pc1: BPF_FUNC_get_socket_cookie takes one parameter,
  69. * R1: sk_buff
  70. */
  71. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  72. BPF_FUNC_get_socket_cookie),
  73. /* pc2-4: save &socketCookie to r7 for future usage*/
  74. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
  75. BPF_MOV64_REG(BPF_REG_7, BPF_REG_10),
  76. BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, -8),
  77. /*
  78. * pc5-8: set up the registers for BPF_FUNC_map_lookup_elem,
  79. * it takes two parameters (R1: map_fd, R2: &socket_cookie)
  80. */
  81. BPF_LD_MAP_FD(BPF_REG_1, map_fd),
  82. BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
  83. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  84. BPF_FUNC_map_lookup_elem),
  85. /*
  86. * pc9. if r0 != 0x0, go to pc+14, since we have the cookie
  87. * stored already
  88. * Otherwise do pc10-22 to setup a new data entry.
  89. */
  90. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 14),
  91. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  92. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  93. BPF_FUNC_get_socket_uid),
  94. /*
  95. * Place a struct stats in the R10 stack and sequentially
  96. * place the member value into the memory. Packets value
  97. * is set by directly place a IMM value 1 into the stack.
  98. */
  99. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0,
  100. -32 + offsetof(struct stats, uid)),
  101. BPF_ST_MEM(BPF_DW, BPF_REG_10,
  102. -32 + offsetof(struct stats, packets), 1),
  103. /*
  104. * __sk_buff is a special struct used for eBPF program to
  105. * directly access some sk_buff field.
  106. */
  107. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_6,
  108. offsetof(struct __sk_buff, len)),
  109. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1,
  110. -32 + offsetof(struct stats, bytes)),
  111. /*
  112. * add new map entry using BPF_FUNC_map_update_elem, it takes
  113. * 4 parameters (R1: map_fd, R2: &socket_cookie, R3: &stats,
  114. * R4: flags)
  115. */
  116. BPF_LD_MAP_FD(BPF_REG_1, map_fd),
  117. BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
  118. BPF_MOV64_REG(BPF_REG_3, BPF_REG_10),
  119. BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, -32),
  120. BPF_MOV64_IMM(BPF_REG_4, 0),
  121. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  122. BPF_FUNC_map_update_elem),
  123. BPF_JMP_IMM(BPF_JA, 0, 0, 5),
  124. /*
  125. * pc24-30 update the packet info to a exist data entry, it can
  126. * be done by directly write to pointers instead of using
  127. * BPF_FUNC_map_update_elem helper function
  128. */
  129. BPF_MOV64_REG(BPF_REG_9, BPF_REG_0),
  130. BPF_MOV64_IMM(BPF_REG_1, 1),
  131. BPF_STX_XADD(BPF_DW, BPF_REG_9, BPF_REG_1,
  132. offsetof(struct stats, packets)),
  133. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_6,
  134. offsetof(struct __sk_buff, len)),
  135. BPF_STX_XADD(BPF_DW, BPF_REG_9, BPF_REG_1,
  136. offsetof(struct stats, bytes)),
  137. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_6,
  138. offsetof(struct __sk_buff, len)),
  139. BPF_EXIT_INSN(),
  140. };
  141. prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog,
  142. ARRAY_SIZE(prog), "GPL", 0,
  143. log_buf, sizeof(log_buf));
  144. if (prog_fd < 0)
  145. error(1, errno, "failed to load prog\n%s\n", log_buf);
  146. }
  147. static void prog_attach_iptables(char *file)
  148. {
  149. int ret;
  150. char rules[100];
  151. if (bpf_obj_pin(prog_fd, file))
  152. error(1, errno, "bpf_obj_pin");
  153. if (strlen(file) > 50) {
  154. printf("file path too long: %s\n", file);
  155. exit(1);
  156. }
  157. sprintf(rules, "iptables -A INPUT -m bpf --object-pinned %s -j ACCEPT",
  158. file);
  159. ret = system(rules);
  160. if (ret < 0) {
  161. printf("iptables rule update failed: %d/n", WEXITSTATUS(ret));
  162. exit(1);
  163. }
  164. }
  165. static void print_table(void)
  166. {
  167. struct stats curEntry;
  168. uint32_t curN = UINT32_MAX;
  169. uint32_t nextN, res;
  170. while (bpf_map_get_next_key(map_fd, &curN, &nextN) > -1) {
  171. curN = nextN;
  172. res = bpf_map_lookup_elem(map_fd, &curN, &curEntry);
  173. if (res < 0) {
  174. error(1, errno, "fail to get entry value of Key: %u\n",
  175. curN);
  176. } else {
  177. printf("cookie: %u, uid: 0x%x, Packet Count: %lu,"
  178. " Bytes Count: %lu\n", curN, curEntry.uid,
  179. curEntry.packets, curEntry.bytes);
  180. }
  181. }
  182. }
  183. int main(int argc, char *argv[])
  184. {
  185. if (argc > 2) {
  186. printf("Too many argument provided\n");
  187. return 1;
  188. } else if (argc < 2) {
  189. printf("Usage: %s bpfObjName\n", argv[0]);
  190. return 1;
  191. }
  192. maps_create();
  193. prog_load();
  194. prog_attach_iptables(argv[1]);
  195. while (true) {
  196. print_table();
  197. printf("\n");
  198. sleep(1);
  199. };
  200. return 0;
  201. }