Linux Audio

Check our new training course

Loading...
v6.13.7
 1/* SPDX-License-Identifier: GPL-2.0 */
 2// Copyright (c) 2018 Politecnico di Torino
 3#include <stddef.h>
 4#include <string.h>
 5#include <linux/bpf.h>
 6#include <linux/if_ether.h>
 7#include <linux/ip.h>
 8#include <linux/pkt_cls.h>
 9#include <bpf/bpf_helpers.h>
10
 
 
11struct {
12	__uint(type, MAP_TYPE);
13	__uint(max_entries, 32);
14	__uint(map_flags, 0);
15	__uint(key_size, 0);
16	__uint(value_size, sizeof(__u32));
17} map_in SEC(".maps");
18
19struct {
20	__uint(type, MAP_TYPE);
21	__uint(max_entries, 32);
22	__uint(map_flags, 0);
23	__uint(key_size, 0);
24	__uint(value_size, sizeof(__u32));
25} map_out SEC(".maps");
26
27SEC("tc")
28int _test(struct __sk_buff *skb)
29{
30	void *data_end = (void *)(long)skb->data_end;
31	void *data = (void *)(long)skb->data;
32	struct ethhdr *eth = (struct ethhdr *)(data);
33	__u32 value;
34	int err;
35
36	if (eth + 1 > data_end)
37		return TC_ACT_SHOT;
38
39	struct iphdr *iph = (struct iphdr *)(eth + 1);
40
41	if (iph + 1 > data_end)
42		return TC_ACT_SHOT;
43
44	err = bpf_map_pop_elem(&map_in, &value);
45	if (err)
46		return TC_ACT_SHOT;
47
48	iph->daddr = value;
49
50	err = bpf_map_push_elem(&map_out, &iph->saddr, 0);
51	if (err)
52		return TC_ACT_SHOT;
53
54	return TC_ACT_OK;
55}
56
57char _license[] SEC("license") = "GPL";
v5.9
 1/* SPDX-License-Identifier: GPL-2.0 */
 2// Copyright (c) 2018 Politecnico di Torino
 3#include <stddef.h>
 4#include <string.h>
 5#include <linux/bpf.h>
 6#include <linux/if_ether.h>
 7#include <linux/ip.h>
 8#include <linux/pkt_cls.h>
 9#include <bpf/bpf_helpers.h>
10
11int _version SEC("version") = 1;
12
13struct {
14	__uint(type, MAP_TYPE);
15	__uint(max_entries, 32);
16	__uint(map_flags, 0);
17	__uint(key_size, 0);
18	__uint(value_size, sizeof(__u32));
19} map_in SEC(".maps");
20
21struct {
22	__uint(type, MAP_TYPE);
23	__uint(max_entries, 32);
24	__uint(map_flags, 0);
25	__uint(key_size, 0);
26	__uint(value_size, sizeof(__u32));
27} map_out SEC(".maps");
28
29SEC("test")
30int _test(struct __sk_buff *skb)
31{
32	void *data_end = (void *)(long)skb->data_end;
33	void *data = (void *)(long)skb->data;
34	struct ethhdr *eth = (struct ethhdr *)(data);
35	__u32 value;
36	int err;
37
38	if (eth + 1 > data_end)
39		return TC_ACT_SHOT;
40
41	struct iphdr *iph = (struct iphdr *)(eth + 1);
42
43	if (iph + 1 > data_end)
44		return TC_ACT_SHOT;
45
46	err = bpf_map_pop_elem(&map_in, &value);
47	if (err)
48		return TC_ACT_SHOT;
49
50	iph->daddr = value;
51
52	err = bpf_map_push_elem(&map_out, &iph->saddr, 0);
53	if (err)
54		return TC_ACT_SHOT;
55
56	return TC_ACT_OK;
57}
58
59char _license[] SEC("license") = "GPL";