cookie_uid_helper_example.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 -option
  8. * option:
  9. * -t: do traffic monitoring test, the program will continuously
  10. * print out network traffic happens after program started A sample
  11. * output is shown below:
  12. *
  13. * cookie: 877, uid: 0x3e8, Pakcet Count: 20, Bytes Count: 11058
  14. * cookie: 132, uid: 0x0, Pakcet Count: 2, Bytes Count: 286
  15. * cookie: 812, uid: 0x3e8, Pakcet Count: 3, Bytes Count: 1726
  16. * cookie: 802, uid: 0x3e8, Pakcet Count: 2, Bytes Count: 104
  17. * cookie: 877, uid: 0x3e8, Pakcet Count: 20, Bytes Count: 11058
  18. * cookie: 831, uid: 0x3e8, Pakcet Count: 2, Bytes Count: 104
  19. * cookie: 0, uid: 0x0, Pakcet Count: 6, Bytes Count: 712
  20. * cookie: 880, uid: 0xfffe, Pakcet Count: 1, Bytes Count: 70
  21. *
  22. * -s: do getsockopt SO_COOKIE test, the program will set up a pair of
  23. * UDP sockets and send packets between them. And read out the traffic data
  24. * directly from the ebpf map based on the socket cookie.
  25. *
  26. * Clean up: if using shell script, the script file will delete the iptables
  27. * rule and unmount the bpf program when exit. Else the iptables rule need
  28. * to be deleted by hand, see run_cookie_uid_helper_example.sh for detail.
  29. */
  30. #define _GNU_SOURCE
  31. #define offsetof(type, member) __builtin_offsetof(type, member)
  32. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
  33. #include <arpa/inet.h>
  34. #include <errno.h>
  35. #include <error.h>
  36. #include <limits.h>
  37. #include <linux/bpf.h>
  38. #include <linux/if_ether.h>
  39. #include <net/if.h>
  40. #include <signal.h>
  41. #include <stdbool.h>
  42. #include <stdint.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <sys/socket.h>
  47. #include <sys/stat.h>
  48. #include <sys/types.h>
  49. #include <unistd.h>
  50. #include <bpf/bpf.h>
  51. #include "bpf_insn.h"
  52. #define PORT 8888
  53. struct stats {
  54. uint32_t uid;
  55. uint64_t packets;
  56. uint64_t bytes;
  57. };
  58. static int map_fd, prog_fd;
  59. static bool test_finish;
  60. static void maps_create(void)
  61. {
  62. map_fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(uint32_t),
  63. sizeof(struct stats), 100, 0);
  64. if (map_fd < 0)
  65. error(1, errno, "map create failed!\n");
  66. }
  67. static void prog_load(void)
  68. {
  69. static char log_buf[1 << 16];
  70. struct bpf_insn prog[] = {
  71. /*
  72. * Save sk_buff for future usage. value stored in R6 to R10 will
  73. * not be reset after a bpf helper function call.
  74. */
  75. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  76. /*
  77. * pc1: BPF_FUNC_get_socket_cookie takes one parameter,
  78. * R1: sk_buff
  79. */
  80. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  81. BPF_FUNC_get_socket_cookie),
  82. /* pc2-4: save &socketCookie to r7 for future usage*/
  83. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
  84. BPF_MOV64_REG(BPF_REG_7, BPF_REG_10),
  85. BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, -8),
  86. /*
  87. * pc5-8: set up the registers for BPF_FUNC_map_lookup_elem,
  88. * it takes two parameters (R1: map_fd, R2: &socket_cookie)
  89. */
  90. BPF_LD_MAP_FD(BPF_REG_1, map_fd),
  91. BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
  92. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  93. BPF_FUNC_map_lookup_elem),
  94. /*
  95. * pc9. if r0 != 0x0, go to pc+14, since we have the cookie
  96. * stored already
  97. * Otherwise do pc10-22 to setup a new data entry.
  98. */
  99. BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 14),
  100. BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
  101. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  102. BPF_FUNC_get_socket_uid),
  103. /*
  104. * Place a struct stats in the R10 stack and sequentially
  105. * place the member value into the memory. Packets value
  106. * is set by directly place a IMM value 1 into the stack.
  107. */
  108. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0,
  109. -32 + (__s16)offsetof(struct stats, uid)),
  110. BPF_ST_MEM(BPF_DW, BPF_REG_10,
  111. -32 + (__s16)offsetof(struct stats, packets), 1),
  112. /*
  113. * __sk_buff is a special struct used for eBPF program to
  114. * directly access some sk_buff field.
  115. */
  116. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_6,
  117. offsetof(struct __sk_buff, len)),
  118. BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1,
  119. -32 + (__s16)offsetof(struct stats, bytes)),
  120. /*
  121. * add new map entry using BPF_FUNC_map_update_elem, it takes
  122. * 4 parameters (R1: map_fd, R2: &socket_cookie, R3: &stats,
  123. * R4: flags)
  124. */
  125. BPF_LD_MAP_FD(BPF_REG_1, map_fd),
  126. BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
  127. BPF_MOV64_REG(BPF_REG_3, BPF_REG_10),
  128. BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, -32),
  129. BPF_MOV64_IMM(BPF_REG_4, 0),
  130. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  131. BPF_FUNC_map_update_elem),
  132. BPF_JMP_IMM(BPF_JA, 0, 0, 5),
  133. /*
  134. * pc24-30 update the packet info to a exist data entry, it can
  135. * be done by directly write to pointers instead of using
  136. * BPF_FUNC_map_update_elem helper function
  137. */
  138. BPF_MOV64_REG(BPF_REG_9, BPF_REG_0),
  139. BPF_MOV64_IMM(BPF_REG_1, 1),
  140. BPF_STX_XADD(BPF_DW, BPF_REG_9, BPF_REG_1,
  141. offsetof(struct stats, packets)),
  142. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_6,
  143. offsetof(struct __sk_buff, len)),
  144. BPF_STX_XADD(BPF_DW, BPF_REG_9, BPF_REG_1,
  145. offsetof(struct stats, bytes)),
  146. BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_6,
  147. offsetof(struct __sk_buff, len)),
  148. BPF_EXIT_INSN(),
  149. };
  150. prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog,
  151. ARRAY_SIZE(prog), "GPL", 0,
  152. log_buf, sizeof(log_buf));
  153. if (prog_fd < 0)
  154. error(1, errno, "failed to load prog\n%s\n", log_buf);
  155. }
  156. static void prog_attach_iptables(char *file)
  157. {
  158. int ret;
  159. char rules[100];
  160. if (bpf_obj_pin(prog_fd, file))
  161. error(1, errno, "bpf_obj_pin");
  162. if (strlen(file) > 50) {
  163. printf("file path too long: %s\n", file);
  164. exit(1);
  165. }
  166. sprintf(rules, "iptables -A OUTPUT -m bpf --object-pinned %s -j ACCEPT",
  167. file);
  168. ret = system(rules);
  169. if (ret < 0) {
  170. printf("iptables rule update failed: %d/n", WEXITSTATUS(ret));
  171. exit(1);
  172. }
  173. }
  174. static void print_table(void)
  175. {
  176. struct stats curEntry;
  177. uint32_t curN = UINT32_MAX;
  178. uint32_t nextN;
  179. int res;
  180. while (bpf_map_get_next_key(map_fd, &curN, &nextN) > -1) {
  181. curN = nextN;
  182. res = bpf_map_lookup_elem(map_fd, &curN, &curEntry);
  183. if (res < 0) {
  184. error(1, errno, "fail to get entry value of Key: %u\n",
  185. curN);
  186. } else {
  187. printf("cookie: %u, uid: 0x%x, Packet Count: %lu,"
  188. " Bytes Count: %lu\n", curN, curEntry.uid,
  189. curEntry.packets, curEntry.bytes);
  190. }
  191. }
  192. }
  193. static void udp_client(void)
  194. {
  195. struct sockaddr_in si_other = {0};
  196. struct sockaddr_in si_me = {0};
  197. struct stats dataEntry;
  198. int s_rcv, s_send, i, recv_len;
  199. char message = 'a';
  200. char buf;
  201. uint64_t cookie;
  202. int res;
  203. socklen_t cookie_len = sizeof(cookie);
  204. socklen_t slen = sizeof(si_other);
  205. s_rcv = socket(PF_INET, SOCK_DGRAM, 0);
  206. if (s_rcv < 0)
  207. error(1, errno, "rcv socket creat failed!\n");
  208. si_other.sin_family = AF_INET;
  209. si_other.sin_port = htons(PORT);
  210. if (inet_aton("127.0.0.1", &si_other.sin_addr) == 0)
  211. error(1, errno, "inet_aton\n");
  212. if (bind(s_rcv, (struct sockaddr *)&si_other, sizeof(si_other)) == -1)
  213. error(1, errno, "bind\n");
  214. s_send = socket(PF_INET, SOCK_DGRAM, 0);
  215. if (s_send < 0)
  216. error(1, errno, "send socket creat failed!\n");
  217. res = getsockopt(s_send, SOL_SOCKET, SO_COOKIE, &cookie, &cookie_len);
  218. if (res < 0)
  219. printf("get cookie failed: %s\n", strerror(errno));
  220. res = bpf_map_lookup_elem(map_fd, &cookie, &dataEntry);
  221. if (res != -1)
  222. error(1, errno, "socket stat found while flow not active\n");
  223. for (i = 0; i < 10; i++) {
  224. res = sendto(s_send, &message, sizeof(message), 0,
  225. (struct sockaddr *)&si_other, slen);
  226. if (res == -1)
  227. error(1, errno, "send\n");
  228. if (res != sizeof(message))
  229. error(1, 0, "%uB != %luB\n", res, sizeof(message));
  230. recv_len = recvfrom(s_rcv, &buf, sizeof(buf), 0,
  231. (struct sockaddr *)&si_me, &slen);
  232. if (recv_len < 0)
  233. error(1, errno, "receive\n");
  234. res = memcmp(&(si_other.sin_addr), &(si_me.sin_addr),
  235. sizeof(si_me.sin_addr));
  236. if (res != 0)
  237. error(1, EFAULT, "sender addr error: %d\n", res);
  238. printf("Message received: %c\n", buf);
  239. res = bpf_map_lookup_elem(map_fd, &cookie, &dataEntry);
  240. if (res < 0)
  241. error(1, errno, "lookup sk stat failed, cookie: %lu\n",
  242. cookie);
  243. printf("cookie: %lu, uid: 0x%x, Packet Count: %lu,"
  244. " Bytes Count: %lu\n\n", cookie, dataEntry.uid,
  245. dataEntry.packets, dataEntry.bytes);
  246. }
  247. close(s_send);
  248. close(s_rcv);
  249. }
  250. static int usage(void)
  251. {
  252. printf("Usage: ./run_cookie_uid_helper_example.sh"
  253. " bpfObjName -option\n"
  254. " -t traffic monitor test\n"
  255. " -s getsockopt cookie test\n");
  256. return 1;
  257. }
  258. static void finish(int ret)
  259. {
  260. test_finish = true;
  261. }
  262. int main(int argc, char *argv[])
  263. {
  264. int opt;
  265. bool cfg_test_traffic = false;
  266. bool cfg_test_cookie = false;
  267. if (argc != 3)
  268. return usage();
  269. while ((opt = getopt(argc, argv, "ts")) != -1) {
  270. switch (opt) {
  271. case 't':
  272. cfg_test_traffic = true;
  273. break;
  274. case 's':
  275. cfg_test_cookie = true;
  276. break;
  277. default:
  278. printf("unknown option %c\n", opt);
  279. usage();
  280. return -1;
  281. }
  282. }
  283. maps_create();
  284. prog_load();
  285. prog_attach_iptables(argv[2]);
  286. if (cfg_test_traffic) {
  287. if (signal(SIGINT, finish) == SIG_ERR)
  288. error(1, errno, "register SIGINT handler failed");
  289. if (signal(SIGTERM, finish) == SIG_ERR)
  290. error(1, errno, "register SIGTERM handler failed");
  291. while (!test_finish) {
  292. print_table();
  293. printf("\n");
  294. sleep(1);
  295. };
  296. } else if (cfg_test_cookie) {
  297. udp_client();
  298. }
  299. close(prog_fd);
  300. close(map_fd);
  301. return 0;
  302. }