xref: /openbmc/linux/samples/bpf/test_probe_write_user.bpf.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1d4fffba4SDaniel T. Lee /* Copyright (c) 2016 Sargun Dhillon <sargun@sargun.me>
2d4fffba4SDaniel T. Lee  *
3d4fffba4SDaniel T. Lee  * This program is free software; you can redistribute it and/or
4d4fffba4SDaniel T. Lee  * modify it under the terms of version 2 of the GNU General Public
5d4fffba4SDaniel T. Lee  * License as published by the Free Software Foundation.
6d4fffba4SDaniel T. Lee  */
7d4fffba4SDaniel T. Lee #include "vmlinux.h"
8d4fffba4SDaniel T. Lee #include <string.h>
9d4fffba4SDaniel T. Lee #include <linux/version.h>
10d4fffba4SDaniel T. Lee #include <bpf/bpf_helpers.h>
11d4fffba4SDaniel T. Lee #include <bpf/bpf_tracing.h>
12d4fffba4SDaniel T. Lee #include <bpf/bpf_core_read.h>
13d4fffba4SDaniel T. Lee 
14d4fffba4SDaniel T. Lee struct {
15d4fffba4SDaniel T. Lee 	__uint(type, BPF_MAP_TYPE_HASH);
16d4fffba4SDaniel T. Lee 	__type(key, struct sockaddr_in);
17d4fffba4SDaniel T. Lee 	__type(value, struct sockaddr_in);
18d4fffba4SDaniel T. Lee 	__uint(max_entries, 256);
19d4fffba4SDaniel T. Lee } dnat_map SEC(".maps");
20d4fffba4SDaniel T. Lee 
21d4fffba4SDaniel T. Lee /* kprobe is NOT a stable ABI
22d4fffba4SDaniel T. Lee  * kernel functions can be removed, renamed or completely change semantics.
23d4fffba4SDaniel T. Lee  * Number of arguments and their positions can change, etc.
24d4fffba4SDaniel T. Lee  * In such case this bpf+kprobe example will no longer be meaningful
25d4fffba4SDaniel T. Lee  *
26d4fffba4SDaniel T. Lee  * This example sits on a syscall, and the syscall ABI is relatively stable
27d4fffba4SDaniel T. Lee  * of course, across platforms, and over time, the ABI may change.
28d4fffba4SDaniel T. Lee  */
29d4fffba4SDaniel T. Lee SEC("ksyscall/connect")
BPF_KSYSCALL(bpf_prog1,int fd,struct sockaddr_in * uservaddr,int addrlen)30*c5ffb263SDaniel T. Lee int BPF_KSYSCALL(bpf_prog1, int fd, struct sockaddr_in *uservaddr,
31*c5ffb263SDaniel T. Lee 		 int addrlen)
32d4fffba4SDaniel T. Lee {
33d4fffba4SDaniel T. Lee 	struct sockaddr_in new_addr, orig_addr = {};
34d4fffba4SDaniel T. Lee 	struct sockaddr_in *mapped_addr;
35d4fffba4SDaniel T. Lee 
36*c5ffb263SDaniel T. Lee 	if (addrlen > sizeof(orig_addr))
37d4fffba4SDaniel T. Lee 		return 0;
38d4fffba4SDaniel T. Lee 
39*c5ffb263SDaniel T. Lee 	if (bpf_probe_read_user(&orig_addr, sizeof(orig_addr), uservaddr) != 0)
40d4fffba4SDaniel T. Lee 		return 0;
41d4fffba4SDaniel T. Lee 
42d4fffba4SDaniel T. Lee 	mapped_addr = bpf_map_lookup_elem(&dnat_map, &orig_addr);
43d4fffba4SDaniel T. Lee 	if (mapped_addr != NULL) {
44d4fffba4SDaniel T. Lee 		memcpy(&new_addr, mapped_addr, sizeof(new_addr));
45*c5ffb263SDaniel T. Lee 		bpf_probe_write_user(uservaddr, &new_addr,
46d4fffba4SDaniel T. Lee 				     sizeof(new_addr));
47d4fffba4SDaniel T. Lee 	}
48d4fffba4SDaniel T. Lee 	return 0;
49d4fffba4SDaniel T. Lee }
50d4fffba4SDaniel T. Lee 
51d4fffba4SDaniel T. Lee char _license[] SEC("license") = "GPL";
52d4fffba4SDaniel T. Lee u32 _version SEC("version") = LINUX_VERSION_CODE;
53