Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / bpf / connect4_prog.c
blob5a88a681d2abc0d698701f0fa5ede0bfc626aaa2
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
4 #include <string.h>
6 #include <linux/stddef.h>
7 #include <linux/bpf.h>
8 #include <linux/in.h>
9 #include <linux/in6.h>
10 #include <sys/socket.h>
12 #include "bpf_helpers.h"
13 #include "bpf_endian.h"
15 #define SRC_REWRITE_IP4 0x7f000004U
16 #define DST_REWRITE_IP4 0x7f000001U
17 #define DST_REWRITE_PORT4 4444
19 int _version SEC("version") = 1;
21 SEC("cgroup/connect4")
22 int connect_v4_prog(struct bpf_sock_addr *ctx)
24 struct sockaddr_in sa;
26 /* Rewrite destination. */
27 ctx->user_ip4 = bpf_htonl(DST_REWRITE_IP4);
28 ctx->user_port = bpf_htons(DST_REWRITE_PORT4);
30 if (ctx->type == SOCK_DGRAM || ctx->type == SOCK_STREAM) {
31 ///* Rewrite source. */
32 memset(&sa, 0, sizeof(sa));
34 sa.sin_family = AF_INET;
35 sa.sin_port = bpf_htons(0);
36 sa.sin_addr.s_addr = bpf_htonl(SRC_REWRITE_IP4);
38 if (bpf_bind(ctx, (struct sockaddr *)&sa, sizeof(sa)) != 0)
39 return 0;
42 return 1;
45 char _license[] SEC("license") = "GPL";