File: bpf_iter_setsockopt_unix.c

package info (click to toggle)
linux 6.16.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,724,468 kB
  • sloc: ansic: 26,560,391; asm: 271,356; sh: 143,999; python: 72,469; makefile: 57,129; perl: 36,821; xml: 19,553; cpp: 5,820; yacc: 4,915; lex: 2,955; awk: 1,667; sed: 28; ruby: 25
file content (60 lines) | stat: -rw-r--r-- 1,263 bytes parent folder | download | duplicates (7)
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 <vmlinux.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";