aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/bpf_iter_setsockopt_unix.c
blob: eafc877ea46038f24e485d1ec3031771ea805695 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// SPDX-License-Identifier: GPL-2.0
/* Copyright Amazon.com Inc. or its affiliates. */
#include "bpf_iter.h"
#include "bpf_tracing_net.h"
#include <bpf/bpf_helpers.h>
#include <limits.h>

#define AUTOBIND_LEN 6
char sun_path[AUTOBIND_LEN];

#define NR_CASES 5
int sndbuf_setsockopt[NR_CASES] = {-1, 0, 8192, INT_MAX / 2, INT_MAX};
int sndbuf_getsockopt[NR_CASES] = {-1, -1, -1, -1, -1};
int sndbuf_getsockopt_expected[NR_CASES];

static inline int cmpname(struct unix_sock *unix_sk)
{
	int i;

	for (i = 0; i < AUTOBIND_LEN; i++) {
		if (unix_sk->addr->name->sun_path[i] != sun_path[i])
			return -1;
	}

	return 0;
}

SEC("iter/unix")
int change_sndbuf(struct bpf_iter__unix *ctx)
{
	struct unix_sock *unix_sk = ctx->unix_sk;
	int i, err;

	if (!unix_sk || !unix_sk->addr)
		return 0;

	if (unix_sk->addr->name->sun_path[0])
		return 0;

	if (cmpname(unix_sk))
		return 0;

	for (i = 0; i < NR_CASES; i++) {
		err = bpf_setsockopt(unix_sk, SOL_SOCKET, SO_SNDBUF,
				     &sndbuf_setsockopt[i],
				     sizeof(sndbuf_setsockopt[i]));
		if (err)
			break;

		err = bpf_getsockopt(unix_sk, SOL_SOCKET, SO_SNDBUF,
				     &sndbuf_getsockopt[i],
				     sizeof(sndbuf_getsockopt[i]));
		if (err)
			break;
	}

	return 0;
}

char _license[] SEC("license") = "GPL";