/* This test is a demo of using get_socket_uid and get_socket_cookie * helper function to do per socket based network traffic monitoring. * It requires iptables version higher then 1.6.1. to load pinned eBPF * program into the xt_bpf match. * * TEST: * ./run_cookie_uid_helper_example.sh -option * option: * -t: do traffic monitoring test, the program will continuously * print out network traffic happens after program started A sample * output is shown below: * * cookie: 877, uid: 0x3e8, Pakcet Count: 20, Bytes Count: 11058 * cookie: 132, uid: 0x0, Pakcet Count: 2, Bytes Count: 286 * cookie: 812, uid: 0x3e8, Pakcet Count: 3, Bytes Count: 1726 * cookie: 802, uid: 0x3e8, Pakcet Count: 2, Bytes Count: 104 * cookie: 877, uid: 0x3e8, Pakcet Count: 20, Bytes Count: 11058 * cookie: 831, uid: 0x3e8, Pakcet Count: 2, Bytes Count: 104 * cookie: 0, uid: 0x0, Pakcet Count: 6, Bytes Count: 712 * cookie: 880, uid: 0xfffe, Pakcet Count: 1, Bytes Count: 70 * * -s: do getsockopt SO_COOKIE test, the program will set up a pair of * UDP sockets and send packets between them. And read out the traffic data * directly from the ebpf map based on the socket cookie. * * Clean up: if using shell script, the script file will delete the iptables * rule and unmount the bpf program when exit. Else the iptables rule need * to be deleted by hand, see run_cookie_uid_helper_example.sh for detail.
*/
struct bpf_insn prog[] = { /* * Save sk_buff for future usage. value stored in R6 to R10 will * not be reset after a bpf helper function call.
*/
BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), /* * pc1: BPF_FUNC_get_socket_cookie takes one parameter, * R1: sk_buff
*/
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
BPF_FUNC_get_socket_cookie), /* pc2-4: save &socketCookie to r7 for future usage*/
BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
BPF_MOV64_REG(BPF_REG_7, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, -8), /* * pc5-8: set up the registers for BPF_FUNC_map_lookup_elem, * it takes two parameters (R1: map_fd, R2: &socket_cookie)
*/
BPF_LD_MAP_FD(BPF_REG_1, map_fd),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
BPF_FUNC_map_lookup_elem), /* * pc9. if r0 != 0x0, go to pc+14, since we have the cookie * stored already * Otherwise do pc10-22 to setup a new data entry.
*/
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 14),
BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
BPF_FUNC_get_socket_uid), /* * Place a struct stats in the R10 stack and sequentially * place the member value into the memory. Packets value * is set by directly place a IMM value 1 into the stack.
*/
BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0,
-32 + (__s16)offsetof(struct stats, uid)),
BPF_ST_MEM(BPF_DW, BPF_REG_10,
-32 + (__s16)offsetof(struct stats, packets), 1), /* * __sk_buff is a special struct used for eBPF program to * directly access some sk_buff field.
*/
BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_6,
offsetof(struct __sk_buff, len)),
BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_1,
-32 + (__s16)offsetof(struct stats, bytes)), /* * add new map entry using BPF_FUNC_map_update_elem, it takes * 4 parameters (R1: map_fd, R2: &socket_cookie, R3: &stats, * R4: flags)
*/
BPF_LD_MAP_FD(BPF_REG_1, map_fd),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_7),
BPF_MOV64_REG(BPF_REG_3, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, -32),
BPF_MOV64_IMM(BPF_REG_4, 0),
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
BPF_FUNC_map_update_elem),
BPF_JMP_IMM(BPF_JA, 0, 0, 5), /* * pc24-30 update the packet info to a exist data entry, it can * be done by directly write to pointers instead of using * BPF_FUNC_map_update_elem helper function
*/
BPF_MOV64_REG(BPF_REG_9, BPF_REG_0),
BPF_MOV64_IMM(BPF_REG_1, 1),
BPF_ATOMIC_OP(BPF_DW, BPF_ADD, BPF_REG_9, BPF_REG_1,
offsetof(struct stats, packets)),
BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_6,
offsetof(struct __sk_buff, len)),
BPF_ATOMIC_OP(BPF_DW, BPF_ADD, BPF_REG_9, BPF_REG_1,
offsetof(struct stats, bytes)),
BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_6,
offsetof(struct __sk_buff, len)),
BPF_EXIT_INSN(),
};
LIBBPF_OPTS(bpf_prog_load_opts, opts,
.log_buf = log_buf,
.log_size = sizeof(log_buf),
);
prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
prog, ARRAY_SIZE(prog), &opts); if (prog_fd < 0)
error(1, errno, "failed to load prog\n%s\n", log_buf);
}
staticvoid prog_attach_iptables(char *file)
{ int ret; char rules[256];
if (bpf_obj_pin(prog_fd, file))
error(1, errno, "bpf_obj_pin"); if (strlen(file) > 50) {
printf("file path too long: %s\n", file); exit(1);
}
ret = snprintf(rules, sizeof(rules), "iptables -A OUTPUT -m bpf --object-pinned %s -j ACCEPT",
file); if (ret < 0 || ret >= sizeof(rules)) {
printf("error constructing iptables command\n"); exit(1);
}
ret = system(rules); if (ret < 0) {
printf("iptables rule update failed: %d/n", WEXITSTATUS(ret)); exit(1);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.