1 /* eBPF example program:
5 * The eBPF program sets the sk_bound_dev_if index in new AF_INET{6}
6 * sockets opened by processes in the cgroup.
8 * - Attaches the new program to a cgroup using BPF_PROG_ATTACH
23 #include <linux/bpf.h>
28 char bpf_log_buf[BPF_LOG_BUF_SIZE];
30 static int prog_load(__u32 idx, __u32 mark, __u32 prio)
32 /* save pointer to context */
33 struct bpf_insn prog_start[] = {
34 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
36 struct bpf_insn prog_end[] = {
37 BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = verdict */
41 /* set sk_bound_dev_if on socket */
42 struct bpf_insn prog_dev[] = {
43 BPF_MOV64_IMM(BPF_REG_3, idx),
44 BPF_MOV64_IMM(BPF_REG_2, offsetof(struct bpf_sock, bound_dev_if)),
45 BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3, offsetof(struct bpf_sock, bound_dev_if)),
48 /* set mark on socket */
49 struct bpf_insn prog_mark[] = {
50 /* get uid of process */
51 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
52 BPF_FUNC_get_current_uid_gid),
53 BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 0xffffffff),
55 /* if uid is 0, use given mark, else use the uid as the mark */
56 BPF_MOV64_REG(BPF_REG_3, BPF_REG_0),
57 BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
58 BPF_MOV64_IMM(BPF_REG_3, mark),
60 /* set the mark on the new socket */
61 BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
62 BPF_MOV64_IMM(BPF_REG_2, offsetof(struct bpf_sock, mark)),
63 BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3, offsetof(struct bpf_sock, mark)),
66 /* set priority on socket */
67 struct bpf_insn prog_prio[] = {
68 BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
69 BPF_MOV64_IMM(BPF_REG_3, prio),
70 BPF_MOV64_IMM(BPF_REG_2, offsetof(struct bpf_sock, priority)),
71 BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3, offsetof(struct bpf_sock, priority)),
73 LIBBPF_OPTS(bpf_prog_load_opts, opts,
74 .log_buf = bpf_log_buf,
75 .log_size = BPF_LOG_BUF_SIZE,
78 struct bpf_insn *prog;
83 insns_cnt = sizeof(prog_start) + sizeof(prog_end);
85 insns_cnt += sizeof(prog_dev);
88 insns_cnt += sizeof(prog_mark);
91 insns_cnt += sizeof(prog_prio);
93 p = prog = malloc(insns_cnt);
95 fprintf(stderr, "Failed to allocate memory for instructions\n");
99 memcpy(p, prog_start, sizeof(prog_start));
100 p += sizeof(prog_start);
103 memcpy(p, prog_dev, sizeof(prog_dev));
104 p += sizeof(prog_dev);
108 memcpy(p, prog_mark, sizeof(prog_mark));
109 p += sizeof(prog_mark);
113 memcpy(p, prog_prio, sizeof(prog_prio));
114 p += sizeof(prog_prio);
117 memcpy(p, prog_end, sizeof(prog_end));
118 p += sizeof(prog_end);
120 insns_cnt /= sizeof(struct bpf_insn);
122 ret = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK, NULL, "GPL",
123 prog, insns_cnt, &opts);
130 static int get_bind_to_device(int sd, char *name, size_t len)
132 socklen_t optlen = len;
136 rc = getsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, name, &optlen);
138 perror("setsockopt(SO_BINDTODEVICE)");
143 static unsigned int get_somark(int sd)
145 unsigned int mark = 0;
146 socklen_t optlen = sizeof(mark);
149 rc = getsockopt(sd, SOL_SOCKET, SO_MARK, &mark, &optlen);
151 perror("getsockopt(SO_MARK)");
156 static unsigned int get_priority(int sd)
158 unsigned int prio = 0;
159 socklen_t optlen = sizeof(prio);
162 rc = getsockopt(sd, SOL_SOCKET, SO_PRIORITY, &prio, &optlen);
164 perror("getsockopt(SO_PRIORITY)");
169 static int show_sockopts(int family)
171 unsigned int mark, prio;
175 sd = socket(family, SOCK_DGRAM, 17);
181 if (get_bind_to_device(sd, name, sizeof(name)) < 0)
184 mark = get_somark(sd);
185 prio = get_priority(sd);
189 printf("sd %d: dev %s, mark %u, priority %u\n", sd, name, mark, prio);
194 static int usage(const char *argv0)
197 printf(" Attach a program\n");
198 printf(" %s -b bind-to-dev -m mark -p prio cg-path\n", argv0);
200 printf(" Detach a program\n");
201 printf(" %s -d cg-path\n", argv0);
203 printf(" Show inherited socket settings (mark, priority, and device)\n");
204 printf(" %s [-6]\n", argv0);
208 int main(int argc, char **argv)
210 __u32 idx = 0, mark = 0, prio = 0;
211 const char *cgrp_path = NULL;
212 int cg_fd, prog_fd, ret;
213 int family = PF_INET;
217 while ((rc = getopt(argc, argv, "db:m:p:6")) != -1) {
223 idx = if_nametoindex(optarg);
225 idx = strtoumax(optarg, NULL, 0);
227 printf("Invalid device name\n");
233 mark = strtoumax(optarg, NULL, 0);
236 prio = strtoumax(optarg, NULL, 0);
242 return usage(argv[0]);
247 return show_sockopts(family);
249 cgrp_path = argv[optind];
251 fprintf(stderr, "cgroup path not given\n");
255 if (do_attach && !idx && !mark && !prio) {
257 "One of device, mark or priority must be given\n");
261 cg_fd = open(cgrp_path, O_DIRECTORY | O_RDONLY);
263 printf("Failed to open cgroup path: '%s'\n", strerror(errno));
268 prog_fd = prog_load(idx, mark, prio);
270 printf("Failed to load prog: '%s'\n", strerror(errno));
271 printf("Output from kernel verifier:\n%s\n-------\n",
276 ret = bpf_prog_attach(prog_fd, cg_fd,
277 BPF_CGROUP_INET_SOCK_CREATE, 0);
279 printf("Failed to attach prog to cgroup: '%s'\n",
284 ret = bpf_prog_detach(cg_fd, BPF_CGROUP_INET_SOCK_CREATE);
286 printf("Failed to detach prog from cgroup: '%s'\n",